Skip to content

feat(trace): a contract action can match writes that add a file or modify one - #1408

Merged
napetrov merged 58 commits into
intel:mainfrom
napetrov:contract-modifies
Dec 3, 2026
Merged

napetrov merged 58 commits into
intel:mainfrom
napetrov:contract-modifies

Conversation

@napetrov

@napetrov napetrov commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

trace consistency --contract can distinguish new test files from edits to existing tests.

actions:
  new_test:  {change: add, file_path: 'tests/'}
  edit_test: {change: modify, file_path: 'tests/'}
rules:
  - forbids: new_test
whole-file write with no previous read or write → add
whole-file write to a file already seen         → modify
in-place edit or append                         → modify
replacement without a known absolute directory or with an expanded path → unlabelled

Change filters use stream-json traces and require split shell steps. A rule whose verdict depends on an unlabelled write is reported as inconclusive, with counts by action. A confirmed violation makes its run nonconforming. When a run's contract outcome remains inconclusive, the command exits 3.

Evidence

A multi-target tee produces one event per path, so one call can modify a previously read file and add another.

  • Before, cd a || cd /b && echo x > f.py could report an addition under /b even when cd a succeeded. A matching replacement in an unresolved directory could make forbids: new_test appear to pass without qualification.
  • After, the fallback example leaves the replacement unlabelled. The CLI reports the affected action and an inconclusive outcome in text and JSON. With a known directory, cd /w/tests && echo x > test_new.py matches the new-test filter. Regression cases also cover literal virtualenv activation, subagent shell moves, same-call CDPATH, functions, loops and normalized repo events.

Merge Danger

An action with change tests the captured and resolved spellings of a known path. An unlabelled replacement with an unknown path can make a matching action inconclusive. This includes writes whose target depends on shell variable, glob or ~ expansion.

Door: Two-way. The change affects trace consistency --contract and can be reverted without a persisted schema migration.

Blast radius: Trace reporting. Labels come from the recorded path and tool calls. An unread existing file can look new, and an append to a missing file still looks like a modification. See the trace limitations in Run consistency.

…dify one

`change: add` or `change: modify` on an action of `trace consistency --contract` narrows
it to the writes the trace labels so. A whole-file write (Write, >, tee) to a file no
earlier call read or wrote is an add; Edit, MultiEdit, NotebookEdit, sed -i, >> and
tee -a are modifies. The label is from the trace alone, so a file that existed and was
never read in it can be labelled add; the limits are in docs/run-consistency.md.

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
…unts as a write, and the redirect that names the file sets the mode

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
@napetrov
napetrov requested a review from a team as a code owner November 29, 2026 06:47
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Mutation of changed lines

⚠️ Tests miss 11 of 80 small breaks of the lines this PR changes, and not every break was tried

At f06a42c: 69 caught · 11 missed

Each row is a small deliberate break (a mutant) of a line this PR changes that every test reaching that line still passes. Add a test that fails on it or, if it cannot change behaviour, put # mutation-ok[<operator>]: <reason> at the end of the line or alone on the line above.

Where Operator Change no test notices Tests reaching the line
dokimi/dokimi/commands/context.py:152 drop-clause args.shell == "call" and contract and any(action.change for action in contract[" → (args.shell == 'call' and any((action.change for action in contract['actions'].v 11
dokimi/dokimi/context/consistency.py:213 drop-clause record.get("type") == "system" and record.get("subtype") == "init" a → (record.get('subtype') == 'init' and isinstance(cwd, str)) 297
dokimi/dokimi/context/consistency.py:246 drop-clause not isinstance(block, dict) or block.get("type") != "tool_use" or block.get("nam → (block.get('type') != 'tool_use' or block.get('name') != 'Bash') 3
dokimi/dokimi/context/consistency.py:285 delete-statement e.write = "" → pass 7
dokimi/dokimi/context/consistency.py:316 delete-statement continue → pass 153
dokimi/dokimi/context/consistency.py:362 drop-clause isinstance(edits, list) and edits and isinstance(edits[0], dict) → (edits and isinstance(edits[0], dict)) 175
dokimi/dokimi/context/consistency.py:422 bump-int 1 → 2 346
dokimi/dokimi/context/consistency.py:434 bump-int 1 → 2 346
dokimi/dokimi/context/consistency.py:448 flip-bool True → False 346
dokimi/dokimi/context/contract.py:19 drop-element {"Edit", "Write", "MultiEdit", "NotebookEdit"} → {"Edit", "Write", "MultiEdit"} 6
dokimi/dokimi/context/contract.py:46 drop-clause not self.change or key not in ("file_path", "notebook_path") or not ev.input.get → (key not in ('file_path', 'notebook_path') or not ev.input.get(key)) 66

Not checked:

  • 1112 more breaks of covered changed lines were over the limit and not tried
  • 17 changed lines were not mutated

Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary.

@codecov-commenter

codecov-commenter commented Nov 29, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 98.53420% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.51%. Comparing base (f490890) to head (f06a42c).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
dokimi/dokimi/context/shell_directory.py 96.68% 2 Missing and 3 partials ⚠️
dokimi/dokimi/context/consistency.py 97.61% 1 Missing and 1 partial ⚠️
dokimi/dokimi/context/contract.py 98.76% 1 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1408      +/-   ##
==========================================
+ Coverage   96.48%   96.51%   +0.03%     
==========================================
  Files         533      542       +9     
  Lines       71339    72587    +1248     
  Branches    12333    12532     +199     
==========================================
+ Hits        68829    70059    +1230     
- Misses       1536     1546      +10     
- Partials      974      982       +8     
Files with missing lines Coverage Δ
dokimi/dokimi/commands/context.py 97.89% <100.00%> (+2.07%) ⬆️
dokimi/dokimi/context/shell.py 99.36% <100.00%> (+0.38%) ⬆️
dokimi/dokimi/context/shell_edit.py 100.00% <100.00%> (ø)
dokimi/dokimi/repos/shell_functions.py 100.00% <100.00%> (ø)
dokimi/dokimi/repos/shell_walk.py 100.00% <100.00%> (ø)
dokimi/dokimi/repos/shell_words.py 99.11% <100.00%> (ø)
dokimi/dokimi/context/consistency.py 98.16% <97.61%> (+0.30%) ⬆️
dokimi/dokimi/context/contract.py 98.76% <98.76%> (ø)
dokimi/dokimi/context/shell_directory.py 96.68% <96.68%> (ø)

... and 84 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@exai-bot

exai-bot commented Nov 29, 2026 •

Copy link
Copy Markdown

OpenCodeReview: Commit f06a42c was not reviewed. This PR has reached its review limit (6 reviews).

CI at `f06a42c`: 6 still running, 20 passing, 3 skipped
Check Status
Mutation of changed lines still running
Test Linux Python 3.14 still running
Test Linux Wheels Python 3.14 still running
Tests (py3.14) still running
Windows smoke test (py3.13) still running
self / repo tasks check fix-degrade-pre still running

To review this commit: re-request review from exai-bot in the Reviewers list.

Last review (02db372): no findings

Open threads from the bot: 0

Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34Q
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34R
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34U
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34Y
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34R

Thread: PRRT_kwDOVBg7g869L34Y
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869L34R
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
Thread: PRRT_kwDOVBg7g869N20r
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869N20t
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Comment thread dokimi/dokimi/context/shell.py Resolved
Thread: PRRT_kwDOVBg7g869OCzS
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Comment thread dokimi/dokimi/context/shell.py Resolved
Thread: PRRT_kwDOVBg7g869OKz2
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Comment thread dokimi/dokimi/context/shell.py Resolved
Thread: PRRT_kwDOVBg7g869OWAd
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes needed · reviewed 02db372

To fix: a change: add contract can pass when it could not label a write, and a cd after || gives the wrong label (line comments); the smaller gaps there are optional.

✅ make ci-local --offline: 31,429 passed, changed lines 100% covered, changed tests pass with 20 seeds · ✅ strict docs build · ✅ the fixes from the earlier exai-bot threads hold (tee -ai, empty change, assignment, ANSI-C and CDPATH prefixes)

Labelling writes as added or modified is a useful split for test-file contracts, and it is documented with its limits.

Comment thread dokimi/dokimi/context/shell.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
Comment thread dokimi/docs/run-consistency.md Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869fgjJ
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869fgjX
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g869fgja
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 16:43
@napetrov

napetrov commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Re: review by vchamarthi

Could you fix the 5 problems I found (in the line comments)?
All five fixes and their regression tests are linked in the threads. I also negated the condition at contract.py:69 on the reviewed head. Two existing tests caught it, one for an empty pattern list and one for a nonempty alternation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
What changed in this PR

Adds write-change labeling (add vs modify) to trace consistency --contract, enabling contracts to distinguish new-file writes from edits, and introduces “inconclusive” reporting when a write can’t be safely labeled.

Changes:

  • Add change: add|modify support in contract actions, plus “unlabelled write” tracking and inconclusive rule evaluation.
  • Improve shell write identity by tracking per-command working directories (including CDPATH, subshells, backgrounding, and virtualenv activation) and detecting dynamically-expanded write paths.
  • Update CLI reporting/docs and add extensive regression tests, including exit code 3 for inconclusive contract outcomes.
File Description
dokimi/​tests/​test_context_change_review.py Adds regression coverage for add/modify labeling, unknown directories, dynamic paths, subagents, and CLI reporting.
dokimi/​dokimi/​repos/​shell_words.py Renames virtualenv activation helper to activates_virtualenv and updates call sites.
dokimi/​dokimi/​repos/​shell_walk.py Moves brace/function-definition tracking helpers into shell_functions and uses the new API.
dokimi/​dokimi/​repos/​shell_functions.py Introduces reusable regex/constants and FunctionDefinition / track_bodies helpers.
dokimi/​dokimi/​context/​shell_edit.py Extracts in-place edit operand parsing (sed/perl) into a dedicated module.
dokimi/​dokimi/​context/​shell.py Adds write mode detection, dynamic-path detection, and per-command directory inference for split shell calls.
dokimi/​dokimi/​context/​contract.py New module: contract parsing + evaluation with change filters and inconclusive verdicts for unlabelled writes.
dokimi/​dokimi/​context/​consistency.py Adds Event.write/change/directory/dynamic_path, labels writes post-parse, and integrates new contract reporting.
dokimi/​dokimi/​commands/​context.py CLI emits “partial” when contract is inconclusive and exits with EXIT_INCONCLUSIVE (3).
dokimi/​docs/​run-consistency.md Documents change filters, labeling rules, limitations, and new exit semantics.
dokimi/​changelog.d/​20261125_210000_napetrov_contract-modifies.md Changelog entry describing the feature and limitations.

Review effort: Lite

Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/consistency.py Resolved
Comment thread dokimi/dokimi/context/shell.py Resolved
napetrov and others added 5 commits December 1, 2026 10:58
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g86-Lkfc
Thread: PRRT_kwDOVBg7g86-LkgV
Thread: PRRT_kwDOVBg7g86-Lkg5
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g86-Lkhq
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
…n add

Claude Code's Edit and MultiEdit create a missing file when the
(first) old_string is empty, so labelling every Edit as modify let
`forbids: new_test` pass for a run that created a new test file.
Such an edit is now a whole-file write, labelled like Write.

Signed-off-by: Alexander Rybkin <alexander.rybkin@intel.com>
`echo x > tests/test_ol?.py` rewrites tests/test_old.py, but the
target was compared literally and labelled add, giving a confirmed
`forbids: new_test` violation. Unquoted *, ?, [ and a word-initial ~
now count as expansion, so the rule is inconclusive, as for $name.

Signed-off-by: Alexander Rybkin <alexander.rybkin@intel.com>
@xaleryb

xaleryb commented Dec 2, 2026

Copy link
Copy Markdown
Contributor
  • Ran: head 0dd320b merged with main 512e517: full suite, ruff, mypy, bandit, growth and comment-block gates, docs check --strict; re-ran the original input of every earlier thread (all fixed or answered).
  • Edit {file_path: tests/test_new.py, old_string: "", new_string: "x"} (also MultiEdit with an empty first old_string) -> labelled modify, forbids: new_test passes -> add and a violation: Claude Code creates the missing file. Fix: 1b07588.
  • echo x > tests/test_ol?.py (also *, [d], tee, ~/...) after Read of tests/test_old.py -> confirmed add, forbids: new_test violated -> unlabelled, rule inconclusive (bash rewrites test_old.py). Fix: 2a91623.
  • printf x | tee tests/test_old.py tests/test_new.py -> only the first target is an event, conform 100% -> the rule breaks or is inconclusive. Mark the write unlabelled when a target after the first is not an append.
  • With fix(context): the shell readers end a $(...) at the same ) and read $'...' whole in tokens #1450, shell.py reaches 921 lines and check_growth fails. Please move the write-directory tracking into its own module.
  • After merging main: add trace consistency --contract (an inconclusive rule) to row 3 of the exit-code table in docs/quickstart.md, and to the trace consistency line.

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Comment: IC_kwDOVBg7g88AAAABtTnMpw
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
@napetrov

napetrov commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Re: comment by xaleryb

printf x through tee with two files only records the first target.
The trace now records both literal paths. Replacing a previously read file and a new file in one call triggers forbids: new_test; append mode keeps both modify. The empty-edit and glob/tilde regressions pass on this head. Write-directory tracking moved to shell_directory.py, and both quickstart exit-code references now cover an inconclusive contract. cb3e7aa

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes needed · re-checked 3401d0a

To fix: a new test file written after a cd can still pass forbids: new_test as conforming (reply in its thread), and a function definition keeps a cd that may not have run (line comment).

✅ make ci-local --offline: 33,478 passed, mutation and docs build included · ✅ the round-1 repros: cd a || cd /b, venv activation, subagent cd, CDPATH=/export, functions and loops now give the right directory or none · ✅ tee old new reports the new file

The earlier points are fixed apart from one, and inconclusive rules are now reported, not counted as passes.

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>

# Conflicts:
#	dokimi/docs/quickstart.md
Thread: PRRT_kwDOVBg7g869fgjX
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g86-pOp_
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Review: PRR_kwDOVBg7g88AAAABbrQALg
@napetrov

napetrov commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Re: review by vlad-perevezentsev

A new test file written after a cd can still pass forbids: new_test as conforming
8fd8902 covers both cwd cases. 18ff6c4 covers function definitions.

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
(cherry picked from commit e249323eb321bbc70ac425c03170071d835a143c)

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to fix before merge: a brace-expanded tee target is labelled from its literal spelling (inline). The rest holds up.

What I ran, on a Linux review host at c8e0279:

  • make ci-local: 38 passed, 0 failed, 16 skipped; 34,161 tests pass.
  • Reproductions for the finding and about 70 shell write forms through parse_run (cd chains with &&, ;, ||, &, subshells, groups, if, tee, sed -i, perl -i, redirect forms, quoting, substitutions, globs, ~), with no model call.
  • Mutation: four mutants from the bot's survivor table (NotebookEdit dropped from _WRITE_TOOLS, the key not in (...) clause in _matches_pattern, e.write = "" in run_from_events, the contract clause of the --shell call refusal) are all caught by the wider context, CLI and repo test files, though not by the two test files that cover this feature.

Checked, no issues

  • Directory tracking: cd /w/tests; ..., cd after ||, { cd x; }, if cd x; then, cd -P, a backgrounded cd, $(cd ...) and a later cd /elsewhere all give the right directory or none.
  • Verdicts: the lower and upper bounds in _rule_verdict for requires, forbids, before and after_last match the docs, and the exit code 3 text in quickstart.md matches _emit_consistency.
  • Repo rules: no schema change; all 40 non-merge commits carry Signed-off-by; a changelog fragment is present.

Comment thread dokimi/dokimi/context/shell.py Resolved
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
Thread: PRRT_kwDOVBg7g86-3Qlp

Thread: PRR_kwDOVBg7g88AAAABbyjtPg
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
@napetrov

napetrov commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Re: review by antonwolfy

A brace-expanded tee target is labelled from its literal spelling.
9f317b3 covers the inconclusive verdict and quoted literals.

… hit

Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 LGTM · checked f06a42c · nothing required from @napetrov

🔀 Merge: safe now · merges cleanly into current main, 14 commits behind; its tests pass on the merge · after it, #1190 needs a rebase (commands/context.py) and #1450 too (context/shell.py)

✅ make ci-local --offline: 34,080 passed · ⚠️ 1 failure (test_cli_fault_contract.py[eval score ragas]) and schema_compat come from the base, not this PR · ✅ the new test fails with the fix reverted (2 failed)

The earlier points are fixed, and rules now say inconclusive wherever a write's path is unknown.

Fixed in this branch, up to f06a42c:

  • A change: modify rule decided with certainty on sed -i after an unknown cd: 21bb79b counts it as a possible hit, so forbids and requires are inconclusive; tests cover both rules.

@napetrov
napetrov merged commit a46a0d6 into intel:main Dec 3, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants