mlai-code 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/README.md +12 -2
  2. package/bin/mlai.mjs +3 -0
  3. package/package.json +18 -3
  4. package/src/browser-setup.mjs +77 -0
  5. package/src/cli-ext.mjs +146 -0
  6. package/src/cli.mjs +534 -0
  7. package/src/cloud.mjs +29 -0
  8. package/src/completion.mjs +159 -0
  9. package/src/credentials.mjs +54 -0
  10. package/src/deploy.mjs +134 -0
  11. package/src/distribution.mjs +373 -0
  12. package/src/engine-key.mjs +4 -0
  13. package/src/engine.mjs +167 -0
  14. package/src/headless.mjs +196 -0
  15. package/src/home.mjs +22 -0
  16. package/src/host.mjs +123 -0
  17. package/src/local-models.mjs +64 -0
  18. package/src/modes.mjs +38 -0
  19. package/src/netbox.mjs +97 -0
  20. package/src/netjail.py +59 -0
  21. package/src/offline.mjs +98 -0
  22. package/src/parity.mjs +86 -0
  23. package/src/project.mjs +68 -0
  24. package/src/relay.mjs +140 -0
  25. package/src/render.mjs +73 -0
  26. package/src/restricted.mjs +47 -0
  27. package/src/review.mjs +70 -0
  28. package/src/runner.mjs +127 -0
  29. package/src/runtime.mjs +75 -0
  30. package/src/sandbox.mjs +135 -0
  31. package/src/sdk.mjs +11 -0
  32. package/src/server.mjs +110 -0
  33. package/src/session.mjs +290 -0
  34. package/src/settings.mjs +135 -0
  35. package/src/shared-data.mjs +3 -0
  36. package/src/tui/acp.mjs +145 -0
  37. package/src/tui/agents-cli.mjs +114 -0
  38. package/src/tui/agents-local.mjs +65 -0
  39. package/src/tui/ansi.mjs +123 -0
  40. package/src/tui/app.mjs +710 -0
  41. package/src/tui/approval.mjs +88 -0
  42. package/src/tui/bg.mjs +172 -0
  43. package/src/tui/bgterm.mjs +79 -0
  44. package/src/tui/chat.mjs +230 -0
  45. package/src/tui/clipboard.mjs +74 -0
  46. package/src/tui/cloud-cli.mjs +57 -0
  47. package/src/tui/cloud-run.mjs +123 -0
  48. package/src/tui/cmd-agents.mjs +130 -0
  49. package/src/tui/cmd-auto-review.mjs +94 -0
  50. package/src/tui/cmd-cloud.mjs +17 -0
  51. package/src/tui/cmd-config.mjs +104 -0
  52. package/src/tui/cmd-core.mjs +160 -0
  53. package/src/tui/cmd-distribution.mjs +15 -0
  54. package/src/tui/cmd-ecosystem.mjs +169 -0
  55. package/src/tui/cmd-extensibility.mjs +333 -0
  56. package/src/tui/cmd-remote.mjs +18 -0
  57. package/src/tui/cmd-review.mjs +58 -0
  58. package/src/tui/cmd-session-extra.mjs +152 -0
  59. package/src/tui/cmd-tui.mjs +200 -0
  60. package/src/tui/commands.mjs +160 -0
  61. package/src/tui/custom-commands.mjs +98 -0
  62. package/src/tui/diff.mjs +60 -0
  63. package/src/tui/editor.mjs +190 -0
  64. package/src/tui/ext-state.mjs +16 -0
  65. package/src/tui/extensions-local.mjs +140 -0
  66. package/src/tui/files.mjs +72 -0
  67. package/src/tui/handoff.mjs +110 -0
  68. package/src/tui/import-setups.mjs +151 -0
  69. package/src/tui/init.mjs +104 -0
  70. package/src/tui/keymap.mjs +70 -0
  71. package/src/tui/keys.mjs +101 -0
  72. package/src/tui/loop.mjs +75 -0
  73. package/src/tui/markdown.mjs +118 -0
  74. package/src/tui/mcp-config.mjs +225 -0
  75. package/src/tui/mcp-oauth.mjs +162 -0
  76. package/src/tui/permissions.mjs +189 -0
  77. package/src/tui/plain.mjs +32 -0
  78. package/src/tui/plugins-local.mjs +103 -0
  79. package/src/tui/remote.mjs +125 -0
  80. package/src/tui/review-cli.mjs +332 -0
  81. package/src/tui/review-github.mjs +59 -0
  82. package/src/tui/screen.mjs +60 -0
  83. package/src/tui/session-ops.mjs +104 -0
  84. package/src/tui/skills-local.mjs +92 -0
  85. package/src/tui/snapshots.mjs +87 -0
  86. package/src/tui/statusline.mjs +98 -0
  87. package/src/tui/transcript.mjs +116 -0
  88. package/src/two-words.mjs +36 -0
  89. package/src/wakeups.mjs +53 -0
  90. package/src/winenv.mjs +94 -0
  91. package/src/winsandbox.py +351 -0
  92. package/src/workspace-api.mjs +621 -0
  93. package/src/worktree.mjs +128 -0
  94. package/vendor/client-sdk/controller.cjs +115 -0
  95. package/vendor/client-sdk/files.cjs +38 -0
  96. package/vendor/client-sdk/index.cjs +133 -0
  97. package/vendor/client-sdk/login.cjs +52 -0
  98. package/vendor/client-sdk/updates.cjs +41 -0
  99. package/vendor/cloud-cli/mlai.mjs +394 -0
  100. package/worker/architecture_parse.cjs +23 -0
  101. package/worker/architecture_worker.py +394 -0
  102. package/worker/ask_first.py +96 -0
  103. package/worker/attachment_reader.py +66 -0
  104. package/worker/background_jobs.py +111 -0
  105. package/worker/brief_questions.py +56 -0
  106. package/worker/browser_local.py +49 -0
  107. package/worker/browser_pane.py +89 -0
  108. package/worker/browser_tool.py +101 -0
  109. package/worker/build_planner.py +187 -0
  110. package/worker/chat_fork.py +277 -0
  111. package/worker/checkpoints.py +133 -0
  112. package/worker/cloud_pty.py +351 -0
  113. package/worker/command_policy.py +39 -0
  114. package/worker/command_safety.py +717 -0
  115. package/worker/computer_card.sh +23 -0
  116. package/worker/computer_setup.sh +86 -0
  117. package/worker/computer_use.py +221 -0
  118. package/worker/connector_search.py +43 -0
  119. package/worker/context_manager.py +60 -0
  120. package/worker/conversation.py +144 -0
  121. package/worker/deploy_pack.py +116 -0
  122. package/worker/design_director.py +240 -0
  123. package/worker/dev_jobs.py +166 -0
  124. package/worker/edit_guard.py +99 -0
  125. package/worker/env_setup.py +129 -0
  126. package/worker/evidence.py +109 -0
  127. package/worker/fast_search.py +24 -0
  128. package/worker/git_sync.py +333 -0
  129. package/worker/goals.py +156 -0
  130. package/worker/handover.py +98 -0
  131. package/worker/harness_memory.py +194 -0
  132. package/worker/image_generation.py +173 -0
  133. package/worker/intelligence.py +149 -0
  134. package/worker/intent_model.py +78 -0
  135. package/worker/intent_weights.json +1 -0
  136. package/worker/ios_simulator.py +87 -0
  137. package/worker/kimi_bridge.py +997 -0
  138. package/worker/kimi_compact.py +93 -0
  139. package/worker/kimi_gates.py +304 -0
  140. package/worker/kimi_hook.py +617 -0
  141. package/worker/lifecycle.py +95 -0
  142. package/worker/lsp.py +77 -0
  143. package/worker/mcp_local.py +293 -0
  144. package/worker/memory.py +99 -0
  145. package/worker/mlai_auto_review.py +177 -0
  146. package/worker/mlai_hooks.py +966 -0
  147. package/worker/mlai_mcp.py +441 -0
  148. package/worker/mlai_permissions.py +283 -0
  149. package/worker/model_stream.py +75 -0
  150. package/worker/nested_repos.py +76 -0
  151. package/worker/offline_guard.py +17 -0
  152. package/worker/preview.py +806 -0
  153. package/worker/proc_watch.py +124 -0
  154. package/worker/project_context.py +72 -0
  155. package/worker/project_rules.py +99 -0
  156. package/worker/quality.py +180 -0
  157. package/worker/reply_language.py +56 -0
  158. package/worker/repo_hygiene.py +132 -0
  159. package/worker/repo_memory.py +101 -0
  160. package/worker/repo_policy.py +269 -0
  161. package/worker/roles.json +3173 -0
  162. package/worker/roles.py +303 -0
  163. package/worker/runner.py +1795 -0
  164. package/worker/runtime_control.py +205 -0
  165. package/worker/secret_scan.py +168 -0
  166. package/worker/session_hub.py +80 -0
  167. package/worker/skill_library.py +129 -0
  168. package/worker/skill_pack.py +533 -0
  169. package/worker/subagents.py +91 -0
  170. package/worker/task_controller.py +139 -0
  171. package/worker/task_skills.py +159 -0
  172. package/worker/team_checkpoint.py +33 -0
  173. package/worker/token_count.py +32 -0
  174. package/worker/tool_catalog.py +38 -0
  175. package/worker/upload_storage.py +531 -0
  176. package/worker/verification.py +725 -0
  177. package/worker/vision.py +84 -0
  178. package/worker/wakeups.py +63 -0
  179. package/worker/web_tools.py +203 -0
  180. package/worker/winproc.py +135 -0
  181. package/worker/work_ledger.py +117 -0
  182. package/worker/worktrees.py +79 -0
@@ -0,0 +1,725 @@
1
+ """Verification gate pieces ported from the Pixis harness (verification/{claim-check,diff-checks,verdict}).
2
+
3
+ claim_check(final_text, commands) -> findings: the final message asserts that tests/build/types pass but no runner ran.
4
+ diff_checks(repo, base) -> findings: tests edited, deleted or skipped while code changed; assertions removed.
5
+ empty_runs(checks) -> findings: a "passing" check that ran zero tests.
6
+ verdict(checks, findings) -> {'status': passed|failed|inconclusive, 'reasons': [...]} (pure).
7
+ pre_existing(repo, base, command) -> True when the same command also fails on the base commit (not the agent's fault).
8
+
9
+ Findings are dicts {'kind', 'message', 'severity': 'block'|'note'}; the hook and bridge turn `block` findings into
10
+ repair rounds and every finding into a `verification_report` event for the activity feed.
11
+ """
12
+ import os, re, subprocess, tempfile, shutil
13
+ from pathlib import Path
14
+ from runtime_control import latest_checks, check_status, clip, EXTERNAL_SCOPES
15
+
16
+ RUNNER = re.compile(r'(^|[;&|(\s])(pytest|py\.test|python3? -m (pytest|unittest|mypy|ruff)|npm (run )?(test|build|lint|typecheck|type-check|check)|pnpm (run )?(test|build|lint|typecheck|check)|yarn (run )?(test|build|lint|typecheck)|npx (tsc|vitest|jest|playwright|eslint|biome|mocha|cypress|prettier --check)|tsc( |$)|vitest|jest|mocha|go (test|build|vet)|cargo (test|build|check|clippy)|make (test|check|build)?$|make (test|check|build)|bun (run )?test|deno (test|check)|mvn[w]?( -q)? (test|verify|package)|gradle[w]? (test|build)|ruff|mypy|pyright|flake8|eslint|node --test|dotnet (test|build)|mix test|rspec|phpunit|ctest|swift (test|build))', re.I | re.M)
17
+ INLINE_RUN = re.compile(r'(^|[;&|(\s])(python3?|node|bun|deno run|tsx|ts-node)\s+(-c\b|-e\b|-\s|-$|<<|[\w./-]+\.(py|[cm]?js|ts)\b)', re.M)
18
+ INLINE_ASSERT = re.compile(r'\bassert\b|console\.assert|\bexpect\(|\bassert\.\w+\(|[\w./-]*(test_\w+|\w+_test|\w+\.test|\w+\.spec)\.(py|[cm]?js|ts)\b')
19
+ # Claims about verification in the final message. Each tuple: (pattern, what it claims).
20
+ CLAIMS = (
21
+ (re.compile(r'\b(all |the )?(unit |integration |e2e |existing )?tests? (now |all |still )?(pass|passes|passed|passing|are passing|is passing|succeed|succeeded|green)\b', re.I), 'tests'),
22
+ (re.compile(r'\b(\d+|all) (tests?|specs?|cases?) (pass|passed|passing|succeeded|ok)\b', re.I), 'tests'),
23
+ (re.compile(r'\b(test suite|suite) (passes|passed|is green|green)\b', re.I), 'tests'),
24
+ (re.compile(r'\b(the )?build (now )?(succeeds|succeeded|passes|passed|is (clean|green|successful)|completes? successfully|works)\b', re.I), 'build'),
25
+ (re.compile(r'\b(builds?|compiles?) (cleanly|successfully|without errors|fine)\b', re.I), 'build'),
26
+ (re.compile(r'\b(type ?check|typecheck|tsc|type checking|types?) (now )?(passes|passed|is clean|clean|succeeds|succeeded|has no errors)\b', re.I), 'types'),
27
+ (re.compile(r'\b(no (type|lint|linting|compile|compilation) errors)\b', re.I), 'types'),
28
+ (re.compile(r'\b(lint|linter|linting|eslint|ruff) (now )?(passes|passed|is clean|clean|reports? no (errors|issues))\b', re.I), 'lint'),
29
+ (re.compile(r'\b(verified|confirmed) (that )?(it|this|the (fix|change|code|feature|implementation)) (works|runs|passes|behaves)\b', re.I), 'verified'),
30
+ (re.compile(r'\b(tested|verified) (and|&) (working|passing|confirmed)\b', re.I), 'verified'),
31
+ (re.compile(r'\b(everything|all checks?|checks) (pass|passes|passed|are green|is green|green)\b', re.I), 'tests'),
32
+ )
33
+ # Intentions, suggestions and negations around a claim are not claims.
34
+ HEDGE = re.compile(r"\b(should|would|will|could|might|may|once|when|after you|if you|to make sure|make sure|ensure|run .* to (verify|check|confirm)|please run|you can run|not yet|haven'?t|hasn'?t|didn'?t|did not|could not|couldn'?t|cannot|can'?t|unable|without running|not run|were not run|wasn'?t run|no tests? (were )?run|failing|fails?\b|failed|skipped|except|but)\b", re.I)
35
+ HEDGE_BEFORE = re.compile(r"\b(should|would|will|could|might|may|once|when|after you|if|whether|unless|until|to make sure|make sure|ensure|so that|run .* to (verify|check|confirm)|please run|you can run|not|never|no|haven'?t|hasn'?t|didn'?t|isn'?t|aren'?t|wasn'?t|weren'?t|won'?t|could not|couldn'?t|cannot|can'?t|unable|without)\b", re.I)
36
+ ZERO_TESTS = re.compile(r'\b(collected 0 items|no tests ran|ran 0 tests|0 passed|no tests found|no test files found|0 tests? (passed|completed|run)|tests:\s+0 total|no specs? found|No tests to run|\btests\s+0\b|0 passing|files\s+0 passed|Test Suites: 0)', re.I)
37
+ TEST_PATH = re.compile(r'(^|/)(tests?|__tests__|spec|specs|e2e|cypress|playwright)(/|$)|(^|/)(test_[^/]+\.py|[^/]+_test\.(py|go|rb|rs)|[^/]+\.(test|spec)\.[cm]?[jt]sx?|conftest\.py|jest\.config\.[cm]?[jt]s|vitest\.config\.[cm]?[jt]s|pytest\.ini|setup\.cfg|pyproject\.toml|tox\.ini|playwright\.config\.[cm]?[jt]s)$', re.I)
38
+ SKIP_ADDED = re.compile(r'^\+.*(\b(it|test|describe)\.(skip|only|todo)\(|\bx(it|test|describe)\(|@pytest\.mark\.skip|@pytest\.mark\.xfail|pytest\.skip\(|@unittest\.skip|unittest\.SkipTest|self\.skipTest\(|t\.Skip\(|#\[ignore\]|\.pending\(|\bskip:\s*true|\bfit\(|\bfdescribe\()', re.M)
39
+ ASSERT_LINE = re.compile(r'^\s*(assert\b|expect\(|self\.assert\w*\(|t\.(Fatal|Error|Errorf|Fatalf)\(|assert_eq!|assert!\(|\w+\.should\.|should\.|XCTAssert|Assert\.)')
40
+ HARDCODE = re.compile(r'^\+\s*(return\s+)?(\[\s*\d[\d,\s]*\]|"[^"]{0,40}"|\'[^\']{0,40}\'|\d+(\.\d+)?)\s*;?\s*(//|#)?\s*$', re.M)
41
+
42
+
43
+ def ran_verification(commands):
44
+ """True when at least one command in the turn is a recognised test/build/type/lint runner or an inline assertion run."""
45
+ for c in commands or []:
46
+ c = str(c or '')
47
+ if RUNNER.search(c) or (INLINE_RUN.search(c) and INLINE_ASSERT.search(c)): return True
48
+ return False
49
+
50
+
51
+ # Words after a claim that admit the same thing partly failed: an honest partial report, not a success claim.
52
+ ADMITS = re.compile(r"\b(except|apart from|other than|fail(s|ed|ing|ure|ures)?|skipped|still (red|broken))\b", re.I)
53
+
54
+ def claims(text):
55
+ """Clauses of the final message that assert verification happened, with the kind claimed.
56
+
57
+ The hedge is read per clause, not per sentence: "All tests pass, but lint was not run" still claims that tests pass.
58
+ Only the words leading up to the claim (back to the last ; or :) can turn it into an intention or a negation."""
59
+ found = []
60
+ for sentence in re.split(r'(?<=[.!?\n])\s+|\n+', str(text or '')):
61
+ s = sentence.strip()
62
+ if not s: continue
63
+ for pattern, kind in CLAIMS:
64
+ hit = None
65
+ for m in pattern.finditer(s):
66
+ before = re.split(r'[;:]', s[:m.start()])[-1]
67
+ if HEDGE_BEFORE.search(before) or ADMITS.search(s[m.end():]) or re.search(r'["\'`\u201c]\s*$', before): continue
68
+ hit = m; break
69
+ if hit: found.append({'kind': kind, 'text': s[:240]}); break
70
+ return found
71
+
72
+
73
+ def claim_check(final_text, commands, checks=None):
74
+ """Findings when the final message claims verification that no command of this turn performed."""
75
+ found = claims(final_text)
76
+ if not found: return []
77
+ current = latest_checks(checks)
78
+ def contradicts(check):
79
+ command = str(check.get('command') or check.get('label') or '').lower()
80
+ kinds = set()
81
+ if re.search(r'test|pytest|unittest|vitest|jest|spec|assert', command): kinds.add('tests')
82
+ if re.search(r'build|compile|package|cargo check', command): kinds.add('build')
83
+ if re.search(r'type.?check|tsc|mypy|pyright', command): kinds.add('types')
84
+ if re.search(r'lint|ruff|eslint|flake8|biome', command): kinds.add('lint')
85
+ return not kinds or any(f['kind'] in kinds or f['kind'] == 'verified' or re.search(r'everything|all checks|checks (pass|are)', f['text'], re.I) for f in found)
86
+ failed = [c for c in current if (c.get('scope') or 'repository') == 'repository' and check_status(c) == 'failed' and contradicts(c)]
87
+ if failed:
88
+ return [{'kind': 'contradicted_claim', 'severity': 'block',
89
+ 'message': 'The final message claims successful verification, but a current repository check failed: ' + clip(failed[0].get('command') or failed[0].get('label') or 'unnamed check', 240) + '. Fix and rerun it, or report the failure honestly.'}]
90
+ repository = [c for c in current if (c.get('scope') or 'repository') == 'repository']
91
+ if repository and not any(check_status(c) == 'passed' for c in repository):
92
+ return [{'kind': 'unbacked_claim', 'severity': 'block',
93
+ 'message': 'The final message claims successful verification, but no recorded repository check has a passing result. Report the missing or inconclusive verification instead.'}]
94
+ if ran_verification(commands): return []
95
+ passing = [c for c in checks or [] if c.get('executed') and c.get('returncode') == 0]
96
+ if passing: return []
97
+ kinds = sorted({c['kind'] for c in found})
98
+ return [{'kind': 'unbacked_claim', 'severity': 'block',
99
+ 'message': 'The final message claims verification (' + ', '.join(kinds) + ': "' + found[0]['text'][:140] + '") but no test, build, type-check or lint runner was executed in this turn. Run the real check with Bash and report its actual output, or remove the claim.'}]
100
+
101
+
102
+ def empty_runs(checks):
103
+ """A check that exited 0 but ran zero tests does not count as passing."""
104
+ out = []
105
+ for c in latest_checks(checks):
106
+ if c.get('executed') and c.get('returncode') == 0 and ZERO_TESTS.search(str(c.get('output') or '')):
107
+ out.append({'kind': 'zero_tests', 'severity': 'block', 'message': 'The check `' + clip(c.get('command') or c.get('label') or '', 240) + '` exited 0 but ran zero tests (its output says so). Point the runner at the real test files, or state clearly that no tests exist.'})
108
+ return out
109
+
110
+
111
+ def _git(repo, *args, timeout=20):
112
+ try:
113
+ r = subprocess.run(['git', *args], cwd=repo, capture_output=True, text=True, timeout=timeout)
114
+ return r.stdout if r.returncode == 0 else ''
115
+ except (OSError, subprocess.SubprocessError): return ''
116
+
117
+
118
+ def changed_paths(repo, base):
119
+ """(modified_or_added, deleted) relative paths versus base (plus untracked)."""
120
+ status = _git(repo, 'diff', '--name-status', base or 'HEAD')
121
+ changed, deleted = [], []
122
+ for line in status.splitlines():
123
+ parts = line.split('\t')
124
+ if len(parts) < 2: continue
125
+ code, path = parts[0][:1], parts[-1]
126
+ (deleted if code == 'D' else changed).append(path)
127
+ changed += [p for p in _git(repo, 'ls-files', '--others', '--exclude-standard').splitlines() if p and not p.startswith('.mini/')]
128
+ return list(dict.fromkeys(changed)), deleted
129
+
130
+
131
+ def diff_checks(repo, base, task=''):
132
+ """Reward-hacking signals in the working tree versus base: skipped/disabled tests, deleted test files, removed assertions, hard-coded outputs."""
133
+ findings = []
134
+ changed, deleted = changed_paths(repo, base)
135
+ if not changed and not deleted: return findings
136
+ tests_changed = [p for p in changed if TEST_PATH.search(p)]
137
+ tests_deleted = [p for p in deleted if TEST_PATH.search(p)]
138
+ code_changed = [p for p in changed if not TEST_PATH.search(p)]
139
+ task_wants_tests = re.search(r'\b(write|add|create|fix|update|remove|delete)\b[^.\n]{0,60}\btests?\b|\btests?\b[^.\n]{0,40}\b(write|add|create|fix|update|remove|delete)\b', str(task or ''), re.I)
140
+ if tests_deleted and not task_wants_tests:
141
+ findings.append({'kind': 'tests_deleted', 'severity': 'block', 'message': 'Test files were deleted (' + ', '.join(tests_deleted[:4]) + ') although the task did not ask for it. Restore them; make the implementation pass them instead.'})
142
+ # Added skips / only-focus anywhere in test diffs (including new untracked test files).
143
+ for path in tests_changed:
144
+ text = _file_diff(repo, base, path)
145
+ if SKIP_ADDED.search(text):
146
+ findings.append({'kind': 'tests_skipped', 'severity': 'block', 'message': 'A test was skipped, focused or marked expected-to-fail in ' + path + '. Remove the skip/only/xfail and make the implementation pass, unless the task explicitly asks to disable that test.'}); break
147
+ if tests_changed and code_changed and not task_wants_tests:
148
+ removed_asserts = added_asserts = 0
149
+ for path in tests_changed:
150
+ for line in _file_diff(repo, base, path).splitlines():
151
+ if line.startswith('---') or line.startswith('+++'): continue
152
+ if line.startswith('-') and ASSERT_LINE.search(line[1:]): removed_asserts += 1
153
+ elif line.startswith('+') and ASSERT_LINE.search(line[1:]): added_asserts += 1
154
+ if removed_asserts and removed_asserts > added_asserts:
155
+ findings.append({'kind': 'assertions_weakened', 'severity': 'block', 'message': '%d assertion%s removed from existing tests (%s) while implementation files changed. Existing tests define the expected behaviour: fix the code, not the tests, unless the task says the test was wrong (then explain which and why in the final message).' % (removed_asserts, '' if removed_asserts == 1 else 's were', ', '.join(tests_changed[:3]))})
156
+ elif removed_asserts or any(TEST_PATH.search(p) for p in tests_changed):
157
+ findings.append({'kind': 'tests_edited', 'severity': 'note', 'message': 'Existing tests were edited alongside the implementation (' + ', '.join(tests_changed[:3]) + '). State in the final message what changed in the tests and why.'})
158
+ for path in code_changed[:40]:
159
+ if not re.search(r'\.(py|[cm]?[jt]sx?|go|rb|rs|java|kt|php|cs|swift)$', path): continue
160
+ text = _file_diff(repo, base, path)
161
+ added = [l[1:] for l in text.splitlines() if l.startswith('+') and not l.startswith('+++')]
162
+ if 3 <= len(added) <= 12 and sum(1 for l in added if re.fullmatch(r'\s*(return\s+)?(\[[\d,\s]*\]|\{[^}]{0,80}\}|"[^"]{0,60}"|\'[^\']{0,60}\'|-?\d+(\.\d+)?|true|false|True|False|None|null)\s*;?\s*', l)) >= max(2, len(added) // 2):
163
+ findings.append({'kind': 'hardcoded_output', 'severity': 'note', 'message': 'The change to ' + path + ' mostly returns literal values. Make sure it implements the behaviour rather than the expected test outputs.'})
164
+ return findings
165
+
166
+
167
+ def _file_diff(repo, base, path):
168
+ text = _git(repo, 'diff', '--unified=0', base or 'HEAD', '--', path)
169
+ if text: return text
170
+ # Untracked file: every line is an addition.
171
+ try: return '\n'.join('+' + l for l in (Path(repo) / path).read_text(errors='replace').splitlines())
172
+ except OSError: return ''
173
+
174
+
175
+ def verdict(checks, findings):
176
+ """Pure verdict: passed when a real check ran and passed after the last edit and nothing blocks; failed when a check
177
+ failed or a blocking finding exists; inconclusive when nothing verifiable ran."""
178
+ reasons = []
179
+ blocking = [f for f in findings or [] if f.get('severity') == 'block']
180
+ current = [c for c in latest_checks(checks) if (c.get('scope') or 'repository') == 'repository']
181
+ executed = [c for c in current if c.get('executed') is True]
182
+ failed = [c for c in current if check_status(c) == 'failed']
183
+ passed = [c for c in current if check_status(c) == 'passed']
184
+ if blocking: reasons += [f['message'].split('.')[0] for f in blocking]
185
+ if failed: reasons.append('%d check%s failed' % (len(failed), '' if len(failed) == 1 else 's'))
186
+ if blocking or failed: return {'status': 'failed', 'reasons': reasons, 'checks': len(executed), 'passed': len(passed)}
187
+ if any(check_status(c) in ('not_run', 'inconclusive') for c in current):
188
+ return {'status': 'inconclusive', 'reasons': ['Some repository checks are not run or inconclusive'], 'checks': len(executed), 'passed': len(passed)}
189
+ if not passed: return {'status': 'inconclusive', 'reasons': ['no check with a real result ran'] + [f['message'].split('.')[0] for f in findings or []], 'checks': len(executed), 'passed': 0}
190
+ return {'status': 'passed', 'reasons': [f['message'].split('.')[0] for f in findings or []], 'checks': len(executed), 'passed': len(passed)}
191
+
192
+
193
+ def pre_existing(repo, base, command, timeout=300):
194
+ """Rerun `command` on a clean checkout of base. True when it fails there too (the failure pre-dates this task).
195
+ Returns None when it cannot be determined."""
196
+ if not base or not command: return None
197
+ tmp = tempfile.mkdtemp(prefix='mlai-base-')
198
+ try:
199
+ r = subprocess.run(['git', 'worktree', 'add', '--detach', tmp, base], cwd=repo, capture_output=True, text=True, timeout=60)
200
+ if r.returncode != 0: return None
201
+ for link in ('node_modules', '.venv', 'venv', 'vendor', 'target'):
202
+ src = Path(repo) / link
203
+ if src.exists() and not (Path(tmp) / link).exists():
204
+ try: os.symlink(src, Path(tmp) / link)
205
+ except OSError: pass
206
+ env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'MINI_KIMI_PROXY_TOKEN', 'VERCEL_OIDC_TOKEN')}
207
+ run = subprocess.run(['bash', '-lc', command], cwd=tmp, capture_output=True, text=True, timeout=timeout, env=env)
208
+ return run.returncode != 0
209
+ except (OSError, subprocess.SubprocessError): return None
210
+ finally:
211
+ subprocess.run(['git', 'worktree', 'remove', '--force', tmp], cwd=repo, capture_output=True, timeout=60)
212
+ shutil.rmtree(tmp, ignore_errors=True)
213
+
214
+
215
+ # ---------------------------------------------------------------- diff line rules, check weakening, JSON, scope
216
+ MAX_QUOTE = 200
217
+ MACHINE_LINE = 1000 # longer than anyone types: a bundler or minifier wrote it
218
+ MAX_DIFF_FILES = 200
219
+ VENDORED = re.compile(r'(?:^|/)(?:vendor|vendors|third[_-]party|node_modules)/|\.min\.(?:js|mjs|cjs|css)$|\.bundle\.js$', re.I)
220
+ PROSE = re.compile(r'\.(?:md|mdx|markdown|rst|adoc|txt)$', re.I)
221
+ LOCKFILE = re.compile(r'(?:^|/)(?:bun\.lockb?|package-lock\.json|npm-shrinkwrap\.json|yarn\.lock|pnpm-lock\.yaml|Cargo\.lock|poetry\.lock|uv\.lock|Gemfile\.lock|go\.sum|composer\.lock)$')
222
+ PACKAGE_JSON = re.compile(r'(?:^|/)package\.json$')
223
+ TSCONFIG = re.compile(r'(?:^|/)tsconfig[^/]*\.json$')
224
+ WORKFLOW = re.compile(r'(?:^|/)\.github/workflows/[^/]+\.ya?ml$')
225
+ CHECK_SCRIPT = re.compile(r'"(?:test|lint|typecheck|type-check|build|check)(?::[^"]*)?"\s*:')
226
+ NEUTRALISED = re.compile(r'\|\|\s*true\b|\bexit 0\b|--passWithNoTests\b|--no-verify\b|"\s*:\s*"\s*(?:true|echo\b[^"&|;]*)"')
227
+ STRICTNESS_OFF = re.compile(r'"(?:strict|noImplicitAny|strictNullChecks|noImplicitReturns|noUncheckedIndexedAccess)"\s*:\s*false')
228
+ JSON_WITH_COMMENTS = re.compile(r'(?:^|/)(?:tsconfig[^/]*|jsconfig[^/]*|devcontainer|\.vscode/[^/]+)\.json$')
229
+ STUB = re.compile(r"\bthrow\s+new\s+Error\(\s*[\"'`](?:not\s+(?:yet\s+)?implemented|unimplemented|todo)\b[^\"'`]*[\"'`]\s*\)|\braise\s+NotImplementedError\b|\bunimplemented!\s*\(|\btodo!\s*\(|\bthrow\s+new\s+NotImplementedException\b", re.I)
230
+ LINE_RULES = (
231
+ # kind, severity, skip tests, secret, sanction (task wording that permits it), fix, patterns
232
+ ('conflict_marker', 'block', False, False, None, 'Resolve the conflict and remove the markers.',
233
+ ((re.compile(r'^(?:<{7}|>{7})(?:\s|$)'), 'merge conflict marker'),)),
234
+ ('placeholder', 'block', True, False, re.compile(r'\b(stub|placeholder|scaffold|skeleton|not implemented|unimplemented)\b', re.I), 'Implement the behaviour instead of leaving a stub.',
235
+ ((STUB, 'not-implemented stub'),)),
236
+ ('debug_leftover', 'block', False, False, re.compile(r'\bdebug', re.I), 'Remove the breakpoint.',
237
+ ((re.compile(r'^\s*debugger\s*;?\s*(?://.*)?$'), 'debugger statement'), (re.compile(r'\bbreakpoint\(\s*\)|\b(?:i?pdb)\.set_trace\(\s*\)'), 'Python breakpoint'),
238
+ (re.compile(r'\bbinding\.pry\b'), 'Ruby breakpoint'), (re.compile(r'\bdbg!\s*\('), 'Rust dbg! macro'))),
239
+ # A suppression can be the right call, so it is reported to the user rather than sent back for repair.
240
+ ('check_suppression', 'note', False, False, re.compile(r'\b(suppress\w*|ts-ignore|ts-expect-error|eslint-disable|noqa|type:\s*ignore|lint)\b', re.I), 'Fix the error the suppression hides, or say why it is needed.',
241
+ ((re.compile(r'@ts-(?:ignore|nocheck|expect-error)\b'), 'TypeScript check suppression'), (re.compile(r'\b(?:eslint|oxlint)-disable\b|\bbiome-ignore\b'), 'lint suppression'),
242
+ (re.compile(r'#\s*type:\s*ignore\b|#\s*noqa\b|#\s*pylint:\s*disable\b'), 'Python check suppression'), (re.compile(r'//\s*nolint\b'), 'Go lint suppression'), (re.compile(r'@SuppressWarnings\b'), 'Java warning suppression'))),
243
+ ('secret', 'block', False, True, None, 'Remove the credential and load it from the environment or the vault.',
244
+ ((re.compile(r'-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY-----'), 'private key'), (re.compile(r'\bAKIA[0-9A-Z]{16}\b'), 'AWS access key id'),
245
+ (re.compile(r'\bgh[pousr]_[A-Za-z0-9]{36,}\b'), 'GitHub token'), (re.compile(r'\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{32,}\b'), 'API secret key'),
246
+ (re.compile(r'\bxox[abprs]-[A-Za-z0-9-]{10,}\b'), 'Slack token'), (re.compile(r'\bAIza[0-9A-Za-z_-]{35}\b'), 'Google API key'))),
247
+ )
248
+
249
+
250
+ def quote(text):
251
+ """A finding quotes a line, never a file: one minified line once cost 871,930 input tokens in a repair note."""
252
+ t = str(text or '').strip()
253
+ return t[:MAX_QUOTE] + '…' if len(t) > MAX_QUOTE else t
254
+
255
+
256
+ def is_prose(path):
257
+ return bool(PROSE.search(str(path or '')))
258
+
259
+
260
+ def parse_patch(text):
261
+ """(added, removed) as [(line_number, text)] from unified diff text, or from the '+line' form used for untracked files."""
262
+ added, removed, new, old = [], [], 1, 1
263
+ for raw in str(text or '').split('\n'):
264
+ header = re.match(r'^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@', raw)
265
+ if header: old, new = int(header.group(1)), int(header.group(2)); continue
266
+ if raw.startswith('+++') or raw.startswith('---') or raw.startswith('diff --git') or raw.startswith('index '): continue
267
+ if raw.startswith('+'): added.append((new, raw[1:])); new += 1
268
+ elif raw.startswith('-'): removed.append((old, raw[1:])); old += 1
269
+ else: new += 1; old += 1
270
+ return added, removed
271
+
272
+
273
+ def file_diffs(repo, base, changed=None, deleted=None):
274
+ """The working tree versus base as [{'path', 'added', 'removed', 'deleted'}], with real line numbers."""
275
+ if changed is None: changed, deleted = changed_paths(repo, base)
276
+ out = []
277
+ for path in list(changed)[:MAX_DIFF_FILES]:
278
+ try:
279
+ if (Path(repo) / path).stat().st_size > 2_000_000: continue
280
+ except OSError: pass
281
+ added, removed = parse_patch(_file_diff(repo, base, path))
282
+ out.append({'path': path, 'added': added, 'removed': removed, 'deleted': False})
283
+ out += [{'path': path, 'added': [], 'removed': [], 'deleted': True} for path in list(deleted or [])[:MAX_DIFF_FILES]]
284
+ return out
285
+
286
+
287
+ def _names(task, path):
288
+ """The request names this file: by path, file name, or the name before its first dot."""
289
+ name = str(path).split('/')[-1]; stem = name.split('.')[0] or name; low = str(task or '').lower()
290
+ return path.lower() in low or name.lower() in low or bool(len(stem) > 2 and re.search(r'\b' + re.escape(stem.lower()) + r'\b', low))
291
+
292
+
293
+ def _finding(kind, severity, path, line, problem, fix):
294
+ return {'kind': kind, 'severity': severity, 'file': path, 'line': line, 'message': '%s:%s %s %s' % (path, line, problem, fix)}
295
+
296
+
297
+ def line_rules(files, task=''):
298
+ """Things an agent should not leave behind in lines it added. Vendored and minified code is skipped, except for credentials,
299
+ which are looked for everywhere and never printed."""
300
+ findings = []
301
+ for kind, severity, skip_tests, secret, sanction, fix, patterns in LINE_RULES:
302
+ count = 0
303
+ for f in files:
304
+ if f['deleted'] or (skip_tests and TEST_PATH.search(f['path'])) or (not secret and VENDORED.search(f['path'])): continue
305
+ if sanction is not None and sanction.search(str(task or '')) and _names(task, f['path']): continue
306
+ for line, text in f['added']:
307
+ if not secret and len(text) > MACHINE_LINE: continue
308
+ label = next((label for pattern, label in patterns if pattern.search(text)), None)
309
+ if not label: continue
310
+ count += 1
311
+ if count <= 5: findings.append(_finding(kind, severity, f['path'], line, 'Added line looks like a %s.' % label if secret else 'Added %s: %s' % (label, quote(text)), fix))
312
+ break
313
+ return findings
314
+
315
+
316
+ def _script_line(text):
317
+ """A package.json line without layout: whitespace and the trailing comma do not change what a script runs."""
318
+ return re.sub(r'\s+', ' ', str(text)).strip().rstrip(',').strip()
319
+
320
+
321
+ def check_weakening(files, task=''):
322
+ """The checks loosened rather than the code fixed: a check script changed or neutralised, compiler strictness turned off,
323
+ a CI step allowed to fail. Permitted when the request names the file or asks for that kind of change."""
324
+ findings = []
325
+ wanted = re.search(r'\b(scripts?|tsconfig|strict(ness)?|ci|workflow|pipeline|package\.json)\b', str(task or ''), re.I)
326
+ for f in files:
327
+ path = f['path']
328
+ if f['deleted'] or _names(task, path) or VENDORED.search(path): continue
329
+ if PACKAGE_JSON.search(path):
330
+ hit = next(((n, t) for n, t in f['added'] if CHECK_SCRIPT.search(t) and NEUTRALISED.search(t)), None)
331
+ if hit: findings.append(_finding('check_weakening', 'block', path, hit[0], 'A check script was made unable to fail: ' + quote(hit[1]), 'Restore the script and fix the code it checks.'))
332
+ elif not wanted:
333
+ # A script line that only gained or lost a trailing comma (a new script was added after it) is unchanged.
334
+ kept = {_script_line(t) for _, t in f['added']}
335
+ hit = next(((n, t) for n, t in f['removed'] if CHECK_SCRIPT.search(t) and _script_line(t) not in kept), None)
336
+ if hit: findings.append(_finding('check_weakening', 'block', path, hit[0], 'A check script was changed or removed: ' + quote(hit[1]), 'Restore the script and fix the code it checks, or say in the final message why the script had to change.'))
337
+ elif TSCONFIG.search(path) and not wanted:
338
+ for n, t in [(n, t) for n, t in f['added'] if STRICTNESS_OFF.search(t)][:2]:
339
+ findings.append(_finding('check_weakening', 'block', path, n, 'Compiler strictness turned off: ' + quote(t), 'Keep strictness on and fix the type errors.'))
340
+ elif WORKFLOW.search(path) and not wanted:
341
+ hit = next(((n, t) for n, t in f['added'] if re.search(r'continue-on-error:\s*true|\|\|\s*true\b', t)), None)
342
+ if hit: findings.append(_finding('check_weakening', 'block', path, hit[0], 'A CI step was allowed to fail: ' + quote(hit[1]), 'Let the step fail and fix what it reports.'))
343
+ return findings
344
+
345
+
346
+ def invalid_json(repo, files):
347
+ """Changed strict-JSON files that no longer parse."""
348
+ import json
349
+ findings = []
350
+ for f in files:
351
+ path = f['path']
352
+ if f['deleted'] or not path.endswith('.json') or JSON_WITH_COMMENTS.search(path) or VENDORED.search(path): continue
353
+ try:
354
+ target = Path(repo) / path
355
+ if target.is_symlink() or target.stat().st_size > 2_000_000: continue
356
+ json.loads(target.read_text(errors='replace'))
357
+ except OSError: continue
358
+ except ValueError as exc:
359
+ findings.append(_finding('invalid_json', 'block', path, getattr(exc, 'lineno', 1) or 1, 'Not valid JSON: ' + quote(exc), 'Fix the syntax so the file parses.'))
360
+ return findings
361
+
362
+
363
+ def out_of_scope(files, scope):
364
+ """Files changed outside the areas the user approved for this task. Guided mode already asks before each new area is edited
365
+ with the edit tools, so a file here changed some other way (usually a shell command). Reported to the user, not repaired."""
366
+ if not isinstance(scope, dict): return []
367
+ declared = [str(e) for e in (scope.get('declared') or []) if str(e).strip()]
368
+ if not declared: return []
369
+ allowed = declared + [str(e) for e in list(scope.get('areas') or []) + list(scope.get('touched') or [])]
370
+ def inside(path):
371
+ for entry in allowed:
372
+ raw = entry.strip().replace('\\', '/')
373
+ if raw in ('.', '*', '**', '', './'): return True
374
+ e = re.sub(r'\*.*$', '', re.sub(r'^\./', '', raw)).rstrip('/')
375
+ if e == '' or path == e or path.startswith(e + '/'): return True
376
+ return False
377
+ outside = [f['path'] for f in files if not LOCKFILE.search(f['path']) and not f['path'].startswith('.mini/') and not inside(f['path'])]
378
+ if not outside: return []
379
+ return [{'kind': 'out_of_scope', 'severity': 'note', 'file': outside[0], 'line': 1,
380
+ 'message': '%d file%s changed outside the areas approved for this task (%s). Check that these changes are wanted.' % (len(outside), '' if len(outside) == 1 else 's', ', '.join(outside[:5]))}]
381
+
382
+
383
+ def empty_change(edits, files, task=''):
384
+ """Files were edited during the task but nothing is left behind: the edits cancelled out. An unchanged repository is
385
+ the correct outcome when the user asked for no changes (an assessment or review), so it is not flagged then."""
386
+ if not edits or files: return []
387
+ try:
388
+ from conversation import no_change_request
389
+ if no_change_request(task): return []
390
+ except ImportError: pass
391
+ return [{'kind': 'empty_change', 'severity': 'block', 'message': 'Files were edited during this task, but the net change is empty: the edits were undone. Make the change for real, or say plainly in the final message that nothing was changed and why.'}]
392
+
393
+
394
+ # ---------------------------------------------------------------- stale evidence
395
+ def stale_checks(checks, last_edit):
396
+ """Passing repository checks that ran before the last code edit. They describe code that no longer exists."""
397
+ if not last_edit: return []
398
+ return [c for c in latest_checks(checks) if (c.get('scope') or 'repository') == 'repository' and c.get('executed') is True
399
+ and c.get('returncode') == 0 and float(c.get('time') or 0) and float(c.get('time') or 0) < float(last_edit)]
400
+
401
+
402
+ def mark_stale(checks, last_edit):
403
+ """Copy of checks where passing repository checks older than the last code edit carry stale=True."""
404
+ old = {id(c) for c in stale_checks(checks, last_edit)}
405
+ return [({**c, 'stale': True} if id(c) in old else {k: v for k, v in c.items() if k != 'stale'}) for c in checks or [] if isinstance(c, dict)]
406
+
407
+
408
+ # ---------------------------------------------------------------- statements about the past, the code, and remote checks
409
+ CODE_EXT = r'(?:py|[cm]?[jt]sx?|json|go|rs|rb|java|kt|php|cs|swift|css|scss|html?|vue|svelte|sql|ya?ml|toml|sh|md|prisma|graphql)'
410
+ CITATION = re.compile(r'(?<![\w/:.-])((?:[\w@()\[\]-]+/)*[\w@()\[\].-]+\.' + CODE_EXT + r')(?::(\d+)|#L(\d+)|,? lines? (\d+))')
411
+ BEHAVIOUR = re.compile(r"\b(expir\w+|time[- ]?outs?|ttl|max[- ]?age|lifetime|rate[- ]?limit\w*|retention|sliding|session (?:lasts?|length|duration|window)|(?:refresh|renew|extend)\w* (?:on|every|each|with|when))\b", re.I)
412
+ QUANTITY = re.compile(r'\b(\d+(?:\.\d+)?)[- ]?(days?|hours?|hrs?|minutes?|mins?|seconds?|secs?|ms|milliseconds?|weeks?|months?|years?)\b', re.I)
413
+ UNSURE = re.compile(r"\b(unverified|not verified|have not (checked|read|verified)|haven'?t (checked|read|verified)|did not (check|read|verify)|didn'?t (check|read|verify)|i assume|assum(ing|ption)|typically|usually|commonly|in general|for example|e\.g\.|recommend\w*|suggest\w*|should be|could be|you (can|could|may|might) set|i (can|could|will|would) (set|make|change))\b", re.I)
414
+ ROLLING = re.compile(r'\b(sliding|rolling|renew\w*|refresh\w*|extend\w*|reset\w*|each (visit|request|login|sign-?in)|on (activity|every|each))\b', re.I)
415
+ FIXED = re.compile(r'\b(fixed|absolute|hard|does not (renew|refresh|extend|slide)|not (renewed|refreshed|extended|sliding)|regardless of activity)\b', re.I)
416
+ OWN_CHANGE = re.compile(r"\b(i|we|i've|we've) (have )?(just )?(set|added|changed|updated|implemented|increased|reduced|raised|lowered|made|configured|introduced|switched)\b|\b(now|changed to|set to|updated to|from \d+[^.]{0,20} to \d+)\b", re.I)
417
+ RETRACTS = re.compile(r"\b(earlier|previously|before|correction|i was wrong|was (wrong|incorrect|mistaken)|my mistake|i misstated|contrary to|not what i said|to correct)\b", re.I)
418
+ DENIAL = re.compile(r"\b(never|at no point|did(?: not|n't)|have(?: not|n't)|had(?: not|n't)|was(?: not|n't)|were(?: not|n't)|no)\b[^.!?\n]{0,70}?\b(browser|playwright|viewport|run|ran|tests?|tested|testing|verif(?:y|ied|ication)|check(?:ed|s)?)\b", re.I)
419
+ WHOLE_PAST = re.compile(r"\b(never|at no point|at all|actually|earlier|previous(ly)?|before|prior|so far|in this (session|conversation|project|thread)|to date|any (browser|real|actual))\b", re.I)
420
+ THIS_TURN = re.compile(r"\b(this (turn|time|step|change|round)|just now|since (the|my) (last|latest)|after (the|my) (last|latest)|yet for|again|re-?r[au]n)\b", re.I)
421
+ BROWSER_WORDS = re.compile(r'\b(browser|playwright|viewport|\d{3,4} ?px|mobile width|desktop width|screenshot)\b', re.I)
422
+ ALL_PASSED = re.compile(r"\b(all|every|everything)\b[^.!?\n]{0,50}?\b(pass(?:ed|es|ing)?|green|succe(?:ss|ed|eded|ssful))\b|\b(\d+)\s*(?:/|of|out of)\s*(\d+)\b[^.!?\n]{0,30}\b(pass(?:ed|es|ing)?)\b", re.I)
423
+ REMOTE_OPEN = re.compile(r"\b(ci|remote|github actions?|pipeline|workflow|deploy(?:ment)? check)\b[^.!?\n]{0,90}\b(pending|not (?:yet )?(?:run|finished|complete[d]?|verified|checked|confirmed|observed)|still running|in progress|unverified|queued|waiting|has(?: not|n't)|have(?: not|n't)|did(?: not|n't))\b|\b(pending|waiting on|not (?:yet )?(?:verified|checked|confirmed|observed)|unverified)\b[^.!?\n]{0,90}\b(ci|remote|github actions?|pipeline|workflow)\b", re.I)
424
+
425
+
426
+ def _sentences(text):
427
+ return [s.strip() for s in re.split(r'(?<=[.!?])\s+|\n+', str(text or '')) if s.strip()]
428
+
429
+
430
+ def behaviour_statements(text):
431
+ """Sentences that state a concrete figure for how the code behaves (an expiry, a timeout, a limit)."""
432
+ out = []
433
+ for s in _sentences(text):
434
+ topic, amounts = BEHAVIOUR.search(s), QUANTITY.findall(s)
435
+ if not topic or not amounts or UNSURE.search(s) or OWN_CHANGE.search(s) or s.endswith('?'): continue
436
+ key = re.sub(r'[^a-z]', '', topic.group(1).lower())[:5]
437
+ key = {'slidi': 'expir', 'maxag': 'expir', 'lifet': 'expir', 'sessi': 'expir', 'ttl': 'expir', 'refre': 'expir', 'renew': 'expir', 'exten': 'expir'}.get(key, key)
438
+ out.append({'topic': key, 'text': s[:300], 'amounts': sorted({a.lower() + u.lower().rstrip('s')[:3] for a, u in amounts}),
439
+ 'mode': 'fixed' if FIXED.search(s) else 'rolling' if ROLLING.search(s) else '', 'cited': bool(CITATION.search(s))})
440
+ return out[:8]
441
+
442
+
443
+ ABS_DIR = re.compile(r'(?<![\w.~-])(/(?:[\w@.-]+/)*[\w@.-]+)/?')
444
+
445
+
446
+ def citation_roots(repo, *texts):
447
+ """Folders a cited path can be relative to: the repository, repositories nested inside it (git lists a nested repo
448
+ as a single entry, never its files) and absolute project folders named in the request or answer, such as
449
+ /tmp/audit-work/node-cron, where the agent worked."""
450
+ roots, seen = [], set()
451
+ def add(path):
452
+ try: path = Path(path).resolve()
453
+ except OSError: return
454
+ if str(path) in seen or not path.is_dir(): return
455
+ seen.add(str(path)); roots.append(path)
456
+ add(repo)
457
+ listed = (_git(repo, 'ls-files', '-s') or '').splitlines()
458
+ nested = [l.split('\t', 1)[1] for l in listed if l.startswith('160000 ') and '\t' in l]
459
+ nested += [p.rstrip('/') for p in _git(repo, 'ls-files', '--others', '--exclude-standard', '--directory').splitlines() if p.endswith('/')]
460
+ for rel in nested[:20]:
461
+ if (Path(repo) / rel / '.git').exists(): add(Path(repo) / rel)
462
+ for text in texts:
463
+ for m in ABS_DIR.finditer(str(text or '')):
464
+ path = m.group(1)
465
+ if len(roots) >= 30: break
466
+ if path.count('/') < 2 or path.startswith(('/proc', '/sys', '/dev', '/etc', '/usr', '/bin')): continue
467
+ while path.count('/') >= 2 and not Path(path).is_dir(): path = path.rsplit('/', 1)[0]
468
+ if path.count('/') >= 2: add(path)
469
+ return roots
470
+
471
+
472
+ def _listing(root):
473
+ files = (_git(root, 'ls-files') + _git(root, 'ls-files', '--others', '--exclude-standard')).splitlines()
474
+ if files: return files[:30000]
475
+ out = [] # a plain folder (not a repository): bounded walk, skipping dependencies and caches
476
+ for base, dirs, names in os.walk(root):
477
+ dirs[:] = [d for d in dirs if d not in ('node_modules', '.git', 'dist', 'build', '.next', '__pycache__', '.venv')]
478
+ rel = os.path.relpath(base, root)
479
+ out += [n if rel == '.' else rel + '/' + n for n in names]
480
+ if len(out) > 30000: break
481
+ return out[:30000]
482
+
483
+
484
+ def resolve_citation(path, roots, cache):
485
+ """The file a cited path refers to, or None when no folder holds exactly one match."""
486
+ if path.startswith('/'):
487
+ return Path(path) if Path(path).is_file() else None
488
+ for root in roots:
489
+ if (root / path).is_file(): return root / path
490
+ found = []
491
+ for root in roots:
492
+ if root not in cache: cache[root] = _listing(root)
493
+ found += [root / t for t in cache[root] if t == path or t.endswith('/' + path)]
494
+ if len(found) > 1: return None
495
+ return found[0] if len(found) == 1 else None
496
+
497
+
498
+ def citation_check(text, repo, context=''):
499
+ """Every path:line the final message cites must exist. A citation that does not resolve is worse than none."""
500
+ seen, bad = set(), []
501
+ roots, cache = None, {}
502
+ for m in CITATION.finditer(str(text or '')):
503
+ path, line = re.sub(r'^\./', '', m.group(1)), int(m.group(2) or m.group(3) or m.group(4) or 1)
504
+ # "(auth.py:1)" is a citation in brackets, not a path starting with one; "app/(auth)/page.tsx" keeps its own.
505
+ while path[:1] in '([' and path.count(path[0]) > path.count(')' if path[0] == '(' else ']'): path = path[1:]
506
+ if (path, line) in seen: continue
507
+ seen.add((path, line))
508
+ target = Path(repo) / path
509
+ if not target.is_file():
510
+ if roots is None: roots = citation_roots(repo, context, text)
511
+ target = resolve_citation(path, roots, cache)
512
+ if target is None: bad.append('%s:%d (no such file)' % (path, line)); continue
513
+ try:
514
+ if target.stat().st_size > 4_000_000: continue
515
+ total = target.read_text(errors='replace').count('\n') + 1
516
+ except OSError: continue
517
+ if line > total: bad.append('%s:%d (the file has %d lines)' % (path, line, total))
518
+ if not bad: return []
519
+ return [{'kind': 'bad_citation', 'severity': 'block', 'message': 'The final message cites code that does not exist: ' + '; '.join(bad[:5]) + '. Open the real file, then cite the correct path and line or remove the claim.'}]
520
+
521
+
522
+ def behaviour_check(text, earlier=()):
523
+ """A figure for how existing code behaves needs the file and line it comes from; and a figure that differs from what an
524
+ earlier answer said about the same thing must say so."""
525
+ now = behaviour_statements(text)
526
+ if not now: return []
527
+ findings = []
528
+ if not CITATION.search(str(text or '')):
529
+ findings.append({'kind': 'uncited_behaviour', 'severity': 'block', 'message': 'The final message states how the code behaves ("' + quote(now[0]['text']) + '") without pointing at the code. Open the file that sets it, read the exact value and whether it renews, and cite path:line next to the statement. If you cannot find it, say it is unverified instead of stating it as fact.'})
530
+ if not RETRACTS.search(str(text or '')):
531
+ for s in now:
532
+ for old in earlier or ():
533
+ if not isinstance(old, dict) or old.get('topic') != s['topic']: continue
534
+ differs = (old.get('amounts') and s['amounts'] and not set(old['amounts']) & set(s['amounts'])) or (old.get('mode') and s['mode'] and old['mode'] != s['mode'])
535
+ if differs:
536
+ findings.append({'kind': 'unacknowledged_change', 'severity': 'block', 'message': 'An earlier answer in this project said: "' + quote(old.get('text')) + '". This answer says something different about the same thing ("' + quote(s['text']) + '") without saying so. Re-read the code, cite path:line, and tell the user plainly which statement is right and that the other was wrong.'})
537
+ return findings
538
+ return findings
539
+
540
+
541
+ def denial_check(text, record):
542
+ """The final message says something was never tested, while the run record holds passing results for it.
543
+ `record` is the work ledger: [{'time', 'checks': [{'command', 'status'}], 'browser': [{'width', 'ok'}]}]."""
544
+ import time as _time
545
+ for s in _sentences(text):
546
+ if not DENIAL.search(s) or not WHOLE_PAST.search(s) or THIS_TURN.search(s) or s.endswith('?'): continue
547
+ browser = bool(BROWSER_WORDS.search(s))
548
+ for run in reversed(list(record or [])):
549
+ if not isinstance(run, dict): continue
550
+ when = _time.strftime('%Y-%m-%d %H:%M UTC', _time.gmtime(float(run.get('time') or 0)))
551
+ if browser:
552
+ widths = sorted({int(b.get('width') or 0) for b in run.get('browser') or [] if isinstance(b, dict) and b.get('ok')})
553
+ if widths:
554
+ return [{'kind': 'contradicted_denial', 'severity': 'block', 'message': 'The final message says "' + quote(s) + '", but the run record shows passing browser checks on ' + when + ' at ' + ', '.join('%dpx' % w for w in widths) + '. Correct the statement: say what was tested and when, and whether the code has changed since.'}]
555
+ else:
556
+ passed = [clip(c.get('command') or '', 160) for c in run.get('checks') or [] if isinstance(c, dict) and c.get('status') == 'passed']
557
+ if passed:
558
+ return [{'kind': 'contradicted_denial', 'severity': 'block', 'message': 'The final message says "' + quote(s) + '", but the run record shows passing checks on ' + when + ' (' + '; '.join(passed[:3]) + '). Correct the statement: say what was run and when, and whether the code has changed since.'}]
559
+ return []
560
+
561
+
562
+ def remote_claim_check(text, checks):
563
+ """"Everything passed" while a remote or CI check has no result yet."""
564
+ pending = [c for c in latest_checks(checks) if (c.get('scope') or 'repository') in EXTERNAL_SCOPES and check_status(c) not in ('passed', 'failed')]
565
+ if not pending or REMOTE_OPEN.search(str(text or '')): return []
566
+ for s in _sentences(text):
567
+ m = ALL_PASSED.search(s)
568
+ # The hedge can sit before the match or inside it ("all tests should pass once CI finishes").
569
+ if not m or HEDGE_BEFORE.search(re.split(r'[;:]', s[:m.end()])[-1]): continue
570
+ if m.group(3) and m.group(3) != m.group(4): continue # "10 of 11 passed" already admits a gap
571
+ if re.search(r'\b(local(ly)?|unit)\b', s, re.I) and not re.search(r'\b(ci|remote|github actions?|pipeline|checks)\b', s, re.I): continue # scoped to local results
572
+ n = len(pending)
573
+ return [{'kind': 'remote_pending_claim', 'severity': 'block', 'message': 'The final message says "' + quote(s) + '", but %d remote/CI check%s no result yet (%s). Report local and remote results separately: what passed locally, and that the remote check is pending or was not observed.' % (n, ' has' if n == 1 else 's have', quote(pending[0].get('label') or pending[0].get('command')))}]
574
+ return []
575
+
576
+
577
+ # ---------------------------------------------------------------- repair rounds versus notes for the user
578
+ # A repair round re-sends the whole conversation, so it is spent only on something concrete the agent can fix.
579
+ MAX_NOTE_ISSUES, MAX_NOTE_ISSUE, MAX_NOTE_LINE, MAX_NOTE = 12, 1500, 400, 9000
580
+
581
+ def repairable(findings):
582
+ return [f for f in findings or [] if f.get('severity') == 'block']
583
+
584
+
585
+ def notes(findings):
586
+ return [f for f in findings or [] if f.get('severity') != 'block']
587
+
588
+
589
+ def bounded(text, limit=MAX_NOTE_ISSUE, line=MAX_NOTE_LINE):
590
+ """Clip every line and the whole text; say how much was dropped."""
591
+ out = []
592
+ for raw in str(text or '').split('\n'):
593
+ out.append(raw if len(raw) <= line else raw[:line] + '…(%d more characters)' % (len(raw) - line))
594
+ joined = '\n'.join(out)
595
+ return joined if len(joined) <= limit else joined[:limit] + '\n…(%d more characters)' % (len(joined) - limit)
596
+
597
+
598
+ def repair_note(issues, limit=MAX_NOTE):
599
+ """The issue list handed back to the agent, bounded whatever the findings hold."""
600
+ issues = [bounded(i) for i in issues or [] if str(i or '').strip()]
601
+ shown = issues[:MAX_NOTE_ISSUES]
602
+ if len(issues) > len(shown): shown.append('…and %d more issues of the same kinds.' % (len(issues) - len(shown)))
603
+ text = '- ' + '\n- '.join(shown)
604
+ return text if len(text) <= limit else text[:limit] + '\n…(%d more characters)' % (len(text) - limit)
605
+
606
+
607
+ def notice(report):
608
+ """The lines the harness tells the user itself when verification ends with points the agent was not sent back to fix."""
609
+ items = [bounded(f.get('message'), 320, 320) for f in notes((report or {}).get('findings'))][:4]
610
+ return ('Points to check (from the harness, not the agent):\n- ' + '\n- '.join(items)) if items else ''
611
+
612
+
613
+ # Checks that read the agent's wording are heuristics. Each is asked once; if the agent keeps the statement, the user is told
614
+ # in the harness's own words instead of the run being held open. A citation that does not resolve is a fact, so it stays blocking.
615
+ SOFT = frozenset(('uncited_behaviour', 'unacknowledged_change', 'contradicted_denial', 'remote_pending_claim'))
616
+
617
+ def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=()):
618
+ """Everything the gates need in one dict: findings, verdict, and the texts to show."""
619
+ findings = []
620
+ stale = []
621
+ try: stale = stale_checks(checks, last_edit)
622
+ except Exception: pass
623
+ old = {id(c) for c in stale}
624
+ fresh = [c for c in checks or [] if id(c) not in old]
625
+ after = float(last_edit or 0)
626
+ commands = [(c.get('command', '') if isinstance(c, dict) else c) for c in commands or [] if not isinstance(c, dict) or not after or float(c.get('time') or 0) >= after]
627
+ try:
628
+ found = claim_check(final_text, commands, fresh)
629
+ if found and stale and found[0]['kind'] == 'unbacked_claim':
630
+ found = [{'kind': 'stale_claim', 'severity': 'block', 'message': 'The final message claims successful verification, but the passing check (' + quote(stale[-1].get('command') or stale[-1].get('label')) + ') ran before the last code edit. Rerun it on the current code and report that result.'}]
631
+ findings += found
632
+ except Exception: pass
633
+ try: findings += empty_runs(fresh)
634
+ except Exception: pass
635
+ try: findings += diff_checks(repo, base, task)
636
+ except Exception: pass
637
+ files = None
638
+ try: files = file_diffs(repo, base)
639
+ except Exception: pass
640
+ if files is not None:
641
+ for check in (lambda: line_rules(files, task), lambda: check_weakening(files, task), lambda: invalid_json(repo, files), lambda: out_of_scope(files, scope), lambda: empty_change(edits, files, task)):
642
+ try: findings += check()
643
+ except Exception: pass
644
+ for check in (lambda: citation_check(final_text, repo, task), lambda: behaviour_check(final_text, earlier), lambda: denial_check(final_text, record), lambda: remote_claim_check(final_text, checks)):
645
+ try: findings += check()
646
+ except Exception: pass
647
+ findings = [({**f, 'severity': 'note'} if f.get('kind') in SOFT and f.get('kind') in (asked or ()) else f) for f in findings]
648
+ v = verdict(fresh, findings)
649
+ if stale and v['status'] == 'passed' and not v['passed']: v['status'] = 'inconclusive'
650
+ if stale: v['reasons'] = v['reasons'] + ['%d passing check%s ran before the last code edit' % (len(stale), '' if len(stale) == 1 else 's')]
651
+ return {'status': v['status'], 'reasons': v['reasons'], 'findings': findings, 'checks': v['checks'], 'passed': v['passed'], 'stale': len(stale)}
652
+
653
+
654
+ # Shell commands that count as checks (shared by the bridge and the PreToolUse hook).
655
+ TEST_COMMAND = re.compile(r'(^|[;&|(\s])(pytest|python3? -m (pytest|unittest)|npm (run )?(test|build|lint|typecheck)|pnpm (run )?(test|build|lint)|yarn (run )?(test|build|lint)|npx (tsc|vitest|jest|playwright|eslint)|tsc( |$)|vitest|jest|go (test|build|vet)|cargo (test|build|check)|make (test|check)|bun (run )?test|deno test|mvn( -q)? test|gradle test|ruff|mypy|node --test)', re.I)
656
+ # Inline checks: an interpreter run (heredoc, -c/-e or a script) that asserts on behaviour, or runs a test-named file.
657
+ INLINE_RUN = re.compile(r'(^|[;&|(\s])(python3?|node|bun|deno run|tsx|ts-node)\s+(-c\b|-e\b|-\s|-$|<<|[\w./-]+\.(py|[cm]?js|ts)\b)', re.M)
658
+ INLINE_ASSERT = re.compile(r'\bassert\b|console\.assert|\bexpect\(|\bassert\.\w+\(|[\w./-]*(test_\w+|\w+_test|\w+\.test|\w+\.spec)\.(py|[cm]?js|ts)\b')
659
+ def looks_like_check(command):
660
+ return bool(TEST_COMMAND.search(command) or (INLINE_RUN.search(command) and INLINE_ASSERT.search(command)))
661
+
662
+
663
+ # ---------------------------------------------------------------- exit-status integrity (EXEC-001)
664
+ # `npm test | tail` reports tail's exit status, not the test runner's. A check whose runner feeds a pipe
665
+ # without pipefail (or an explicit PIPESTATUS exit) cannot be trusted as a pass on its exit code alone.
666
+ import shlex as _shlex
667
+ FAILURE_SUMMARY = re.compile(r'\b[1-9]\d*\s+(?:failed|failing|failures?|errors?)\b|^\s*(?:FAIL|FAILED)\b|^\s*Tests?:\s.*\b[1-9]\d*\s+failed|^\s*[✖×✗]\s|\bTest Suites:\s.*\b[1-9]\d*\s+failed|^not ok\b|^\s*# fail\s+[1-9]', re.M)
668
+ PASS_SUMMARY = re.compile(r'\b[1-9]\d*\s+(?:passed|passing)\b|\bRan [1-9]\d* tests?\b[\s\S]*\bOK\b|^\s*# pass\s+[1-9]|^ok\s', re.M)
669
+
670
+ def _segments(command):
671
+ """Top-level shell segments as (separator_before, text). Quotes are respected; `||` is not a pipe."""
672
+ try:
673
+ lex = _shlex.shlex(command, posix=True, punctuation_chars=True); lex.whitespace_split = True
674
+ tokens = list(lex)
675
+ except ValueError:
676
+ return [('', command)]
677
+ out, cur, sep = [], [], ''
678
+ for t in tokens:
679
+ if t in ('|', '|&', ';', '&&', '||', '&', ';;', '\n'):
680
+ out.append((sep, ' '.join(cur))); cur, sep = [], t
681
+ else: cur.append(t)
682
+ out.append((sep, ' '.join(cur)))
683
+ return out
684
+
685
+ def masked_pipe(command, is_check):
686
+ """The runner whose result matters feeds a later pipe stage, and nothing propagates its status."""
687
+ if re.search(r'PIPESTATUS|exit \$\?', command): return False
688
+ pipefail = 'pipefail' in command
689
+ segs = _segments(command)
690
+ for i, (sep, text) in enumerate(segs):
691
+ if not is_check(text) or i + 1 >= len(segs): continue
692
+ nxt = segs[i + 1][0]
693
+ if nxt in ('|', '|&') and not pipefail: return True
694
+ # `npm test || true` and `npm test; echo done` also report a later command's status.
695
+ if nxt in ('||', ';', '&'): return True
696
+ return False
697
+
698
+ def output_failure(output):
699
+ clean = re.sub(r'\x1b\[[0-9;]*m', '', str(output or ''))
700
+ m = FAILURE_SUMMARY.search(clean)
701
+ return m.group(0).strip()[:120] if m else ''
702
+
703
+ def check_exit(command, code, output, is_check):
704
+ """Reconcile the shell exit status with what the runner printed.
705
+ Returns (returncode, note). returncode None means the result is inconclusive."""
706
+ if code: return code, ''
707
+ failure = output_failure(output)
708
+ if failure: return 1, 'Runner output reports failures (' + failure + ') although the command exited 0'
709
+ if masked_pipe(command, is_check):
710
+ return None, 'Exit status came from a later pipe stage or command, not the test runner. Rerun with set -o pipefail and without || or ; after the runner'
711
+ return 0, ''
712
+
713
+
714
+ def unverified_banner(report):
715
+ """Short header put above a final answer whose checks did not hold up."""
716
+ report = report or {}
717
+ parts = []
718
+ for key, label in (('checksFailed', 'failed'), ('checksStale', 'ran before the last code change'), ('checksNotRun', 'did not run'), ('checksInconclusive', 'were inconclusive')):
719
+ n = int(report.get(key) or 0)
720
+ if n: parts.append('%d %s' % (n, label))
721
+ unfinished = len(report.get('unfinishedTasks') or [])
722
+ if unfinished: parts.append('%d planned step%s unfinished' % (unfinished, '' if unfinished == 1 else 's'))
723
+ detail = ('; '.join(parts) + '. ') if parts else ''
724
+ return ('> **Not verified.** The final checks did not confirm this work (' + detail.rstrip('. ') + ').' if parts else '> **Not verified.** The final checks did not confirm this work.') + \
725
+ ' Any "verified" or "working" claims below are unconfirmed until the checks are rerun and pass.\n\n'