# @agimon-ai/po-file-edit

Native session-scoped file-change tracking and review APIs for Po and po-web.

Part of the [Po distribution](https://www.npmjs.com/package/@agimon-ai/po).

The package tracks files changed through session tool calls and captures content for diffs, without
requiring a Git repository. `edit` and `write` name their file in the call, so the package can capture
the before and after content within its size and format limits. Bash calls instead trigger a bounded
tree comparison. See [What this cannot see](#what-this-cannot-see) for gaps in that coverage.

> **Alpha:** timeline and UI contracts may change between releases.

## Requirements

- Node.js 24 or newer
- Git for diffs against `HEAD`

## Install

`po init` includes File Edit in `default.packages` in `.po/presets.yaml`.
Remove it or move it to a named layer to change which presets load it.

| Entry                                       | Purpose                                                      |
| ------------------------------------------- | ------------------------------------------------------------ |
| `@agimon-ai/po-file-edit/extensions/server` | Native tracking, file API and changed-file channel lifecycle |
| `@agimon-ai/po-file-edit/extensions/web`    | po-web review surfaces                                       |
| `@agimon-ai/po-file-edit`                   | Shared library API                                           |

## In the cockpit

The `# files` group in the activity dock lists captured file changes. Selecting one opens a
temporary tab holding three views of it:

- **diff**: what the session did to the file, followed by each recorded change on its own, so a file
  edited four times shows four distinct diffs rather than one merged blur.
- **source**: the file as it stands, editable. Saving writes straight to disk and is refused if the
  file changed since it was opened, so a hand edit cannot silently discard what the agent wrote.
- **preview**: rendered Markdown, or HTML in a sandboxed frame.

Selecting text in any view raises a comment box. Comments queue on the tab and one **send review**
builds a single message quoting each anchor, so several comments cost one turn.

Diffs here do not use Git. The package captures the file's content before an `edit` or `write` runs
and keeps it, content-addressed, beside the session timeline; both go when the session ends.

### What this cannot see

- A file the session changed and then deleted. The dock lists things to open, so a row that can only
  answer "this is gone" is left out. The timeline still holds the change, so a tab already open on
  the file keeps working and says why it has nothing to show.
- A file changed by a background process that outlives its Bash call. The tree is compared when the
  call ends.
- A file changed outside any tool. There is no always-on watcher.
- Content past 1 MiB, and binary files. These are listed as changed, without content.
- A file a Bash command writes without naming it is found only through `git status`, so it has no
  baseline: it is listed, readable and editable, but never diffed. Rows and versions say so rather
  than showing an empty diff. Outside a git checkout, and on the edge host, such a file is not seen.
- A file a Bash command names (`> f`, `>> f`, `tee`, `sed -i`, `cp`/`mv` destination, `rm`, `touch`,
  `git checkout/restore -- f`) is read before the command runs and gets a real diff.
- Dependency and build directories are skipped, and a tracked file a script deletes without naming
  it is not reported.

## Data behavior

The timeline contains file paths, and the snapshot store holds copies of the file content the
session changed. That content can be sensitive. Both live under the Pi agent directory,
`$PI_CODING_AGENT_DIR` or `~/.po/agent/po-file-edit`, never inside the working tree, and should be
protected with the same access controls as Pi session data.

A parent session removes its own timeline and snapshots on final disposal. Child sessions share the parent timeline and do not clear it. Activity selection stops retain review data for reactivation. A session that is killed cannot, so
each session also sweeps state that no longer belongs to anyone: a timeline and its snapshots are
removed once nothing has written to them for a week. An older build kept this state in the
repository's git common directory, and that directory is cleared away under the same rule.

The session API reads and writes only files the timeline already records, so a request cannot reach
a file this session never changed.

## Public API

```ts
import { createFileEditDependencies } from '@agimon-ai/po-file-edit';
import type { FileDiff, FileEditEntry } from '@agimon-ai/po-file-edit';
```

The root also exports editor-resolution and timeline domain types for host integrations.

## Development

```bash
pnpm build
pnpm typecheck
pnpm test
pnpm lint
```

Maintained by [Agimon](https://agimon.ai/about).

## License

MIT. See [LICENSE](LICENSE).
