Repository navigation
Deterministic cross-turn duplicate-write guard: make the persisted completed_writes enforceable, not advisory - #1790
Conversation
The persisted completed_writes record from #1780 made the model aware a write had already happened, but the step from evidence to not-repeating ran entirely through the model choosing to honour a prompt note. Not writing the user's data twice should not rest on a probabilistic component. Enforce it at the tool-execution path instead. AiChatCompletedWrite now carries the call's canonical args, which together with the tool name is the write's identity. run_ai_chat_turn replays those into the rebuilt session as prior_writes, and the executor refuses a guarded call that matches one. Canonicalisation is the same function the per-turn seen_calls detector uses, promoted from a local closure, so the two cannot drift into disagreeing about what the same call means. Scope. Only the creates plus delete_node are guarded: re-setting a node to the same content, or a task to the same status, is idempotent, and blocking that would break a user legitimately re-asserting a value. The refusal returns an informative tool result naming the existing node, not a silent block — a user genuinely re-asking for the same node is rare but real, so the model must be able to say it already exists. Oversized args drop the identity rather than truncating it: a truncated string could compare equal to a different call sharing a long prefix, turning a size limit into a wrongly-blocked write. The guard fails open. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Pragmatic Code Review — PR #1790Review Type: Initial Review SummaryThis is a well-executed change that does what #1787 asked: it converts an advisory prompt note into an enforced check at the tool-execution path. The core design decision — reusing the same I independently verified the load-bearing claims rather than taking them at face value:
Two findings below are worth addressing. The first is a concrete correctness gap I reproduced; the second is an architectural one where the codebase already contains the mechanism that solves it. Neither is large, and neither undermines the design. Requirements Check
Code Review Findings🟡 Important: The recorded identity and the compared identity are produced by two different paths, and they diverge on the empty-args case📁 The guard compares let parsed_args = if tc.arguments_json.trim().is_empty() {
Ok(serde_json::json!({})) // <- substituted, not parsed
} else {
serde_json::from_str::<serde_json::Value>(&tc.arguments_json)
};When the model emits a call with empty arguments, the record stores So a guarded write emitted with empty args records an identity that can never match itself. This is not hypothetical: empty-argument tool calls are a known failure mode for the local models this targets — the predecessor work (#1781) exists specifically because "the agent emits malformed tool calls ... and calls with empty arguments." Practically the blast radius is small, because a create with no arguments fails its required-field check and let incoming = canonical_args(&args.to_string());This makes the recorded and compared identity provably the same expression applied to the same value, rather than two expressions that happen to agree on most inputs. It also removes the unparseable-args asymmetry for free (that arm is unreachable here, since the guard sits inside Engineering principle: an equality check is only sound if both sides are produced by the same function applied to the same representation. Deriving the two sides from different upstream values makes agreement a coincidence that must be re-verified whenever either path changes — precisely the drift this PR set out to eliminate by sharing 🟡 Important: A
|
malibio
left a comment
There was a problem hiding this comment.
Initial review posted above: COMMENT. All four acceptance criteria met; 12 new tests verified passing locally. One 🟡 worth fixing before merge (empty-args identity mismatch, one line) and one 🟡 worth a follow-up issue (Tool registry consolidation). No blockers.
…ix empty-args identity Two findings from the review of #1790. Registry consolidation. The guard's tool set was a hand-maintained string list, and the drift test guarding it iterated a hardcoded array — so it passed vacuously on exactly the change it existed to catch, a tool added to neither list. tools.rs already carries a Tool enum built to retire this pattern. Classification now lives there as write_semantics(), an exhaustive match: adding a variant fails to compile until its duplicate semantics are declared. is_write_tool and is_cross_turn_guarded_tool are computed from it, deleting the parallel list in the daemon. Verified by adding a probe variant — five non-exhaustive-match errors. Empty-args identity. The guard compared the raw argument text while the record side stored the parsed form. These agree everywhere except the empty-args path, where arguments are read as {} before execution: the stored identity was {} and the compared one , so such a write could never match itself and the guard was silently disarmed for it. Both sides now derive from the parsed value. The drift test now iterates Tool::ALL, and a companion test pins that every guarded tool can describe what it wrote — a refusal that cannot name the existing node is not the informative result #1787 asks for. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Pragmatic Code Review — PR #1790Review Type: Re-Review (delta Previous Review SummaryThe initial review posted COMMENT with 6 findings: 2 🟡 Important, 4 🟢 Suggestion.
New issues found in this delta: 1 🟡, 3 🟢. On the deferral of 3–6: defensible. Each is a bounded fail-open gap or a cosmetic nit — none can cause a wrong block, and all four remain exactly as true and as cheap to fix later as they were. Crucially, none became more urgent because of this delta: finding 4 ( Verification I performed independentlyI re-verified every load-bearing claim rather than accepting it:
Requirements Check (issue #1787)
All four criteria remain met. Findings🟡 Important:
|
malibio
left a comment
There was a problem hiding this comment.
Re-review posted above: COMMENT. Both 🟡 findings from the initial review are properly resolved — I mutation-tested the empty-args fix (reverting it fails exactly one test, 315 pass) and verified the registry has real compile-time teeth (probe variant → 5 E0004 errors). One new 🟡 worth addressing before merge: Tool::UpdateSchema is classified IdempotentWrite, but handle_update_schema hard-errors on a repeat of add_fields/add_relationships/rename_fields — the guarded behaviour is unchanged and fine, but the new source-of-truth enum now documents an idempotency property that does not hold. No blockers.
…mpleteness compiler-enforced update_schema was classified IdempotentWrite with a comment claiming a repeat is a no-op. It is not: a repeated add_fields rejects the field as already present, add_relationships likewise, and rename_fields fails because the source name is gone. The registry exists to answer exactly this question, so documenting a false property there is worse than not documenting it. Adds a SelfLimitingWrite variant for it. Deliberately not guarded: the guard keys on whole-call arguments, and update_schema batches independent edits, so a call repeating one applied rename alongside a new field is not the same write yet would be refused wholesale. Refusing a legitimate schema edit is worse than the error the schema layer already returns for the redundant part. Tool::ALL was itself hand-maintained, so a variant omitted from it was invisible to everything derived from it — the previous commit's claim that the lists can no longer drift was narrower than stated. ALL_IS_COMPLETE now proves completeness at compile time: an exhaustive match pins each variant to its index, and a count assertion catches a trailing omission the index check cannot see. Verified both failure modes produce E0080. Also corrects two doc comments the re-review found overstated: canonical_args is shared for its normalisation rules but its two callers deliberately pass different inputs, and WriteSemantics::Read means no user-visible state change (reads may still persist convergent lazy migration). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Pragmatic Code Review — PR #1790Review Type: Re-Review (third round, delta Previous Review SummaryRound 2 posted COMMENT with 1 🟡 Important and 3 🟢 Suggestions (carrying 4 deferred 🟢s from round 1).
New issues found in this delta: 1 🟡, 1 🟢. Verification I performed independentlyThe brief flagged that the first attempt at the
Case (c) is the one that matters, and it fails. Detail below in the 🟡 finding. Other claims verified:
Requirements Check (issue #1787)
All four criteria met. Findings🟡 Important:
|
malibio
left a comment
There was a problem hiding this comment.
Third-round re-review posted above: COMMENT. The update_schema reclassification is correct and I verified it is behaviourally inert (matrix diff across all 14 tools: only the sem= label changed). Both doc corrections are now literally true. One 🟡: ALL_IS_COMPLETE does not catch the case it was written for — I added a 15th variant with DuplicableWrite semantics, filled every exhaustive match, omitted it from Tool::ALL, and it compiles clean with all 316 tests passing. The hardcoded 14 is satisfied by the same edit it is meant to police. Cases (a) middle-removal, (b) trailing-removal, (d) reorder, (e) release profile all correctly fail — so the construct has real teeth, just not against additions. A successor-chain-derived COUNT fixes it (verified working, no new dependency); deleting the construct and narrowing the comment is an equally acceptable resolution. Not a live defect — an unlisted tool is unreachable via from_name — so this is documentation integrity, not correctness. All four acceptance criteria met. All mutations reverted; tree clean.
…ten for The check compared Tool::ALL.len() against the literal 14, which is satisfied by exactly the edit it was meant to police. Adding a variant forces an edit to the exhaustive index match — Tool::NewThing => 14 — and nothing about that edit touches ALL or moves the literal. Verified: a 15th variant with DuplicableWrite semantics, absent from ALL, compiled clean and passed all 316 tests including both registry drift tests. The count is now derived from Tool::COUNT, itself an exhaustive successor chain, so a new variant must be threaded through it and threading it in increments the count. mem::variant_count would say this directly but is nightly-only, and strum does not earn a dependency for one assertion. Verified by mutation, not inspection: a new variant omitted from ALL now fails with E0080, and removal of a middle entry, removal of the last entry, and reordering two entries all still fail. Clean on unmodified HEAD in both dev and release profiles. This construct has now been wrong twice in the same direction while passing inspection, so the doc comment states the mutation test as its standing acceptance criterion rather than describing the mechanism alone. Also narrows the SelfLimitingWrite doc, which defined the category as 'rejected by the tool's own validation'. remove_fields and remove_relationships are retain() calls, so a repeated remove converges instead of being rejected — both paths now stated, along with the batching rationale for leaving the category unguarded. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Resolves an import-block conflict in local_agent_service.rs: main added ChatModelSpec (#1791) while this branch added PriorWrite and the agent_loop imports for the duplicate-write guard. Both are needed, so the resolution is the union of the two import lists. No logic conflict. The SelfLimitingWrite doc's claims about handle_update_schema were re-verified against the merged schema/mod.rs (#1788 touched that file): adds still error on an existing name, removes are still retain() calls that converge. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HkymyKtLHm5NcNbf2CjrT9
Closes #1787