nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Add an ACP method to read timeline before-images (pre-edit snapshots) so clients can show only the agent's changes

Open addyCooks opened this issue 1 month ago • 3 comments

Description

Nanocoder already saves a copy of each file before a mutating tool call touches it (the action timeline behind timeline/revert). ACP clients can list those checkpoints with timeline/list, but cannot read the saved contents.

I'd like a read-only ACP extension method that returns the before-image of a path for a checkpoint, with a clear result when no before-image exists.

Background: Nano-Collective/docs#109 (Git diff cannot reliably attribute agent changes), where this was proposed. Tracked from Nano-Collective/nanocoder-desktop#12 (item 6).

Use Case

Nanocoder Desktop has a Git review view. When a user has uncommitted edits in a file the agent also edits, the Git diff mixes both sets of changes, and the user must re-review their own earlier edits. The desktop wants an "Agent changes" view that diffs each touched file against its before-image from just before the agent's turn, and keeps the full Git diff alongside it.

The before-image is exactly what Nanocoder's timeline already stores, so the baseline is already on disk. Clients can't read it. The same method would help any ACP client that shows per-turn diffs.

The desktop needs this by its Milestone 2 if the "Agent changes" view is in v1, and otherwise later. Without it, the desktop falls back to labelling files "touched this turn".

Proposed Solution

Add a read-only extension method, for example timeline/read, taking {sessionId, checkpointId, path} and returning one of:

  • {kind: "text", content}: the file's contents before that tool call.
  • {kind: "created"}: the file did not exist before that tool call. There is no before-image.
  • {kind: "unavailable", reason}: no before-image exists, for example the file is binary, the checkpoint was pruned, or the path is not part of the checkpoint.

Details:

  1. The method only reads. It never changes files, history or the index.
  2. The path must be one of the checkpoint's filesChanged, validated like the revert path (toRelativePath), so it cannot read arbitrary files.
  3. timeline/list also reports which filesChanged entries were created, so a client can tell created from edited without a read.
  4. Document how a client finds the baseline for a turn. Each checkpoint is one tool call, and a turn can have several, so the baseline for a path is the earliest checkpoint in that turn that touched it. Checkpoints from one turn share truncateToMessageIndex.
  5. Advertise the method under _meta in agentCapabilities, so clients detect support from the capability and not from the version.

Alternatives Considered

  • Return the contents inside timeline/list. Simple, but it makes every list call carry file contents, with up to 50 checkpoints per session.
  • Clients read .nanocoder/timeline/<sessionId>/entries/<id>/files/ directly. Works for a local client, but it couples clients to an internal storage layout, which Nanocoder may change, and it breaks if the agent is remote.
  • A higher-level method such as "agent changes since checkpoint N" that returns the before and after. More convenient, but it commits Nanocoder to diff semantics. A plain read keeps the policy in the client.
  • Git-only attribution. Rejected in docs#109: it can't separate the user's uncommitted edits from the agent's in the same file.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Current behavior, checked against main at fc01e23:

  • source/acp/acp-agent.ts (~L734): timeline/list returns {entries: session.timeline.list()}. Each entry has id, seq, toolCallId, toolName, title, timestamp, truncateToMessageIndex and filesChanged. It requires the session to be loaded (requireSession).
  • source/services/timeline-manager.ts:
    • Before-images are stored as UTF-8 under .nanocoder/timeline/<sessionId>/entries/<id>/files/<relative path> (~L287-295).
    • createdFiles is stored in the index, but toMeta (~L480) leaves it out of what timeline/list returns.
    • Binary files are dropped from snapshots (~L226).
    • MAX_TIMELINE_ENTRIES = 50, MAX_TIMELINE_SESSIONS = 20, and sessions older than 7 days are pruned (source/constants.ts ~L72-74).
    • revertTo deletes the entries it reverts (~L332).
  • source/acp/acp-timeline.ts: for shell commands and other tools whose targets are not known up front, a checkpoint is recorded only when the Git scan before and after the call is complete. A scan that hit its file cap or could not run produces no checkpoint (~L149-166, L195-198).
  • The existing methods (renameSession, timeline/list, timeline/revert, retryTurn) use bare names, not the _ prefix ACP asks for custom methods, and are not advertised in agentCapabilities. That is a separate topic.

Implementation Notes (optional)

The storage layout and list() already exist, so a TimelineManager.readBefore(checkpointId, relativePath) method would read the file under entries/<id>/files/ after checking that the path is in the entry's filesChanged (or createdFiles), and extMethod would add the new branch.

Open questions for maintainers:

  1. Method name. timeline/read follows the existing bare names. _nanocoder/timelineRead follows ACP's rule for custom methods. Is there a plan to rename the existing methods (nanocoder-desktop#12 item 7)?
  2. Should the response include created (as proposed) or should it be reported only in timeline/list?
  3. Size limit. Should there be a cap on the returned content, or a truncation flag, for very large files?
  4. Pruning. Should reads fail clearly after pruning (proposed unavailable) or should Nanocoder pin entries a client has asked for?

Acceptance criteria:

  • [ ] A client can read the before-image of a path for a checkpoint of a loaded session.
  • [ ] A created file returns created, with no content.
  • [ ] A binary, missing or pruned before-image returns unavailable with a reason, never an error or partial content.
  • [ ] A path outside the checkpoint's filesChanged, or one that escapes the workspace, is rejected.
  • [ ] The method never modifies files, the index or session history.
  • [ ] timeline/list reports which files were created.
  • [ ] The method is advertised under _meta in agentCapabilities.
  • [ ] Docs describe the method, how to find a turn's baseline, and the limits: binary files, 50 entries per session, pruning after 7 days or beyond 20 sessions, shell-command checkpoints only when the Git scan is complete, and the session must be loaded.

Test plan:

  • Unit (timeline-manager): reading a text before-image, a created file, a binary-skipped path, a pruned entry, and an invalid path.
  • ACP spec: edit a file twice in one turn and read the earliest checkpoint's before-image.
  • ACP spec: reading after timeline/revert returns unavailable.
  • ACP spec: reading a session that isn't loaded fails the same way timeline/list does.
  • Regression: timeline/list and timeline/revert behave as before, apart from the added created-file field.

Out of scope: the "Agent changes" UI, diffing, restoring or reverting files, and renaming the existing extension methods.

addyCooks avatar Oct 26 '26 16:10 addyCooks

@addyCooks if no one is on this on can you assign it to me

Vishesh-Verma-07 avatar Oct 26 '26 16:10 Vishesh-Verma-07

Thanks for asking @Vishesh-Verma-07, but I'm taking this one. It has a dependency, so it's a priority.

addyCooks avatar Oct 26 '26 16:10 addyCooks

okay no worries.

Vishesh-Verma-07 avatar Oct 26 '26 17:10 Vishesh-Verma-07