Skip to content

Deterministic cross-turn duplicate-write guard: make the persisted completed_writes enforceable, not advisory #1787

Closed

Description

@malibio

Follow-up to #1780 / PR #1786, raised in that PR's review.

Background

#1780 fixed a cross-turn duplicate write. The root cause was that the agent session is rebuilt from scratch every turn, and the persistence round-trip dropped all tool-call evidence — so turn N+1 saw the user's instruction plus an unverifiable prose claim and no proof the write had happened.

That PR persists completed_writes on the assistant message and replays them into rebuilt history as a system-role note.

The gap

The step from "evidence present" to "instruction not repeated" runs entirely through the model choosing to honour a prompt note. That is a strong mitigation of a correctly identified cause, but it is not a guarantee: the same reasoning that explains why the model discounted its own prose applies with reduced but nonzero force to a system note. It is stronger evidence, not binding evidence.

A correctness property this concrete — not writing the user's data twice — should not rest solely on a probabilistic component.

Proposal

completed_writes is now exactly the cross-turn state a deterministic guard needs, and #1780 established there was no such state before. So the expensive part is already done.

Add a check in the tool-execution path that compares an incoming write against the persisted (tool, canonical_args) set from prior turns in the same chat. On a hit, do not execute; return a tool result stating the write was already completed, with the node id.

Return an informative result rather than silently blocking, because a user genuinely re-asking for the same node is rare but real (noted in #1780). The model should be able to reason about the response and tell the user the thing already exists — or proceed deliberately if the repeat is intended.

Notes

  • Canonicalisation should reuse the arg-normalising approach the per-turn seen_calls guard already uses in packages/agent/src/local_agent/agent_loop.rs.
  • Scope carefully: update_node and update_task_status are legitimately repeatable with the same arguments (setting the same status twice is idempotent, not a duplicate). The creates are where this matters most.

Acceptance criteria

  • A repeated write with identical canonical args in a later turn does not execute a second time
  • The model receives an informative tool result naming the already-written node, not a silent failure
  • Legitimately idempotent repeats (e.g. re-setting the same task status) are not broken by the guard
  • A regression test covers the cross-turn duplicate at the execution layer, complementing the history-level test added in PR Persist completed graph writes so the next turn can see them (closes #1780) #1786

Activity

  1. malibio commented on Jul 28, 2026

    @malibio
    CollaboratorAuthor

    Additional context from the PR #1786 re-review, worth capturing here so the rationale is not lost.

    Hand-synced lists with no compile-time enforcement

    PR #1786 introduced two lists in packages/daemon/src/services/local_agent_service.rs:

    • is_write_tool — which tools change graph state
    • write_summary_arg — which argument identifies what each write acted on

    Both must stay in sync with a third list in a different crate: the tool registry in packages/agent/src/local_agent/tools.rs, whose own doc comment promises that "the lists can no longer drift — they are computed from here." That guarantee does not extend across the crate boundary.

    This drifted within the very commit that introduced it. update_schema was mapped to a name argument it does not have (it takes schema_id), so it silently degraded to a bare tool name — on one of the most destructive tools in the set, since rename_fields rekeys property data on every existing node of the type. Caught in re-review, fixed in e9052a3, but the failure mode is structural rather than a one-off slip.

    Suggested direction

    Derive the write classification from a method on Tool (e.g. Tool::is_write()) with an exhaustive match, so adding a tool forces a decision about its side-effect class rather than defaulting to silence. Likewise consider modelling the evidence shape as an enum (Args(&[&str]) / Relationship / NoLabel) so the intent lives in the type.

    This was deliberately declined in #1786 as out of scope: Tool lives in packages/agent while the classification lives in the daemon, so bridging means either exporting Tool into the daemon dependency surface or reshaping ToolExecutionRecord to carry a typed tool rather than a String name. That is a real refactor and did not belong in a bug fix on a live duplicate-write defect.

    It does belong with whoever next touches the tool registry — and it composes naturally with the deterministic guard proposed above, which needs to reason about tool identity and side-effect class anyway.

  2. malibio commented on Jul 28, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 170 test files passed, 4104 tests passed (Duration 45.58s).

  3. self-assigned this
    on Jul 28, 2026
  4. malibio commented on Jul 28, 2026

    @malibio
    CollaboratorAuthor

    Deferred from PR #1790 review (tracked here rather than dropped):

    1. delete_node accepts its target under both id and node_id (serde alias), so one logical call has two canonical forms and a repeat that switches key spelling slips the guard. Fail-open (a duplicate executes), not a wrong block.
    2. The 4096-char args cap structurally excludes create_nodes_from_markdown — the tool whose duplicate is most expensive, and the one the cap's own doc comment cites. Its args carry the whole import, so it will essentially always exceed the cap and go unguarded.
    3. load_prior_writes and load_node_history each fetch the same ai-chat node, one extra read per turn.

    (1) and (2) both narrow the guard's coverage; neither can cause a wrong refusal. Worth a follow-up issue if you want the guard's coverage tightened.

  5. malibio commented on Jul 29, 2026

    @malibio
    CollaboratorAuthor

    Deferred coverage gaps from this PR's reviews are now tracked in #1798 — the 4096-char args cap exempting create_nodes_from_markdown, and delete_node's node_id alias producing a second canonical identity. Both fail open (a duplicate slips through) rather than risking a wrong refusal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions