mixdog 0.9.37 → 0.9.39

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 (127) hide show
  1. package/package.json +9 -4
  2. package/scripts/abort-recovery-test.mjs +43 -2
  3. package/scripts/agent-tag-reuse-smoke.mjs +146 -6
  4. package/scripts/agent-terminal-reap-test.mjs +127 -0
  5. package/scripts/agent-trace-io-test.mjs +69 -0
  6. package/scripts/code-graph-disk-hit-test.mjs +224 -0
  7. package/scripts/dispatch-persist-recovery-test.mjs +141 -0
  8. package/scripts/execution-completion-dedup-test.mjs +157 -0
  9. package/scripts/execution-pending-resume-kick-test.mjs +57 -2
  10. package/scripts/execution-resume-esc-integration-test.mjs +174 -0
  11. package/scripts/explore-bench.mjs +101 -8
  12. package/scripts/explore-prompt-policy-test.mjs +156 -11
  13. package/scripts/find-fuzzy-hidden-test.mjs +122 -0
  14. package/scripts/internal-comms-bench-test.mjs +226 -0
  15. package/scripts/internal-comms-bench.mjs +185 -58
  16. package/scripts/internal-comms-smoke.mjs +171 -23
  17. package/scripts/live-worker-smoke.mjs +38 -2
  18. package/scripts/memory-cycle-routing-test.mjs +111 -0
  19. package/scripts/memory-rule-contract-test.mjs +93 -0
  20. package/scripts/notify-completion-mirror-test.mjs +73 -0
  21. package/scripts/output-style-smoke.mjs +2 -2
  22. package/scripts/rg-runner-test.mjs +240 -0
  23. package/scripts/routing-corpus-test.mjs +349 -0
  24. package/scripts/routing-corpus.mjs +211 -32
  25. package/scripts/session-orphan-sweep-test.mjs +83 -0
  26. package/scripts/session-sweep.mjs +266 -0
  27. package/scripts/steering-drain-buckets-test.mjs +179 -0
  28. package/scripts/tool-smoke.mjs +21 -13
  29. package/scripts/tool-tui-presentation-test.mjs +202 -0
  30. package/src/agents/heavy-worker/AGENT.md +10 -7
  31. package/src/agents/reviewer/AGENT.md +6 -4
  32. package/src/agents/worker/AGENT.md +7 -5
  33. package/src/rules/agent/00-common.md +4 -4
  34. package/src/rules/agent/00-core.md +11 -14
  35. package/src/rules/agent/20-skip-protocol.md +3 -3
  36. package/src/rules/agent/30-explorer.md +56 -48
  37. package/src/rules/agent/40-cycle1-agent.md +15 -24
  38. package/src/rules/agent/41-cycle2-agent.md +33 -57
  39. package/src/rules/agent/42-cycle3-agent.md +28 -42
  40. package/src/rules/lead/01-general.md +7 -10
  41. package/src/rules/lead/lead-brief.md +11 -14
  42. package/src/rules/lead/lead-tool.md +6 -5
  43. package/src/rules/shared/01-tool.md +44 -41
  44. package/src/runtime/agent/orchestrator/agent-trace-format.mjs +37 -1
  45. package/src/runtime/agent/orchestrator/agent-trace-io.mjs +20 -5
  46. package/src/runtime/agent/orchestrator/dispatch-persist.mjs +31 -8
  47. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-credentials.mjs +13 -7
  48. package/src/runtime/agent/orchestrator/providers/codex-client-meta.mjs +21 -1
  49. package/src/runtime/agent/orchestrator/providers/openai-oauth-ws.mjs +6 -6
  50. package/src/runtime/agent/orchestrator/providers/openai-oauth.mjs +17 -38
  51. package/src/runtime/agent/orchestrator/providers/openai-ws-delta.mjs +5 -4
  52. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +119 -3
  53. package/src/runtime/agent/orchestrator/session/agent-loop.mjs +17 -8
  54. package/src/runtime/agent/orchestrator/session/context-utils.mjs +270 -78
  55. package/src/runtime/agent/orchestrator/session/loop/tool-helpers.mjs +2 -1
  56. package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +18 -52
  57. package/src/runtime/agent/orchestrator/session/manager/delivered-completions.mjs +123 -0
  58. package/src/runtime/agent/orchestrator/session/manager/idle-cleanup.mjs +15 -2
  59. package/src/runtime/agent/orchestrator/session/manager/pending-messages.mjs +41 -2
  60. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +1 -1
  61. package/src/runtime/agent/orchestrator/session/store.mjs +339 -63
  62. package/src/runtime/agent/orchestrator/stall-policy.mjs +37 -0
  63. package/src/runtime/agent/orchestrator/tools/builtin/arg-guard.mjs +12 -1
  64. package/src/runtime/agent/orchestrator/tools/builtin/builtin-tools.mjs +25 -20
  65. package/src/runtime/agent/orchestrator/tools/builtin/fs-reachability.mjs +6 -2
  66. package/src/runtime/agent/orchestrator/tools/builtin/fuzzy-match.mjs +118 -53
  67. package/src/runtime/agent/orchestrator/tools/builtin/list-tool.mjs +106 -29
  68. package/src/runtime/agent/orchestrator/tools/builtin/path-utils.mjs +8 -1
  69. package/src/runtime/agent/orchestrator/tools/builtin/read-batch.mjs +4 -2
  70. package/src/runtime/agent/orchestrator/tools/builtin/read-range-index.mjs +3 -1
  71. package/src/runtime/agent/orchestrator/tools/builtin/read-snapshot-runtime.mjs +4 -1
  72. package/src/runtime/agent/orchestrator/tools/builtin/read-tool.mjs +33 -2
  73. package/src/runtime/agent/orchestrator/tools/builtin/rg-runner.mjs +151 -64
  74. package/src/runtime/agent/orchestrator/tools/builtin/search-path-diagnostics.mjs +37 -13
  75. package/src/runtime/agent/orchestrator/tools/builtin/search-tool.mjs +84 -49
  76. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +172 -23
  77. package/src/runtime/agent/orchestrator/tools/code-graph/constants.mjs +3 -0
  78. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +68 -5
  79. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +17 -3
  80. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary.mjs +24 -10
  81. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -3
  82. package/src/runtime/agent/orchestrator/tools/code-graph-tool-defs.mjs +4 -7
  83. package/src/runtime/agent/orchestrator/tools/code-graph.mjs +1 -0
  84. package/src/runtime/agent/orchestrator/tools/patch-tool-defs.mjs +1 -1
  85. package/src/runtime/memory/lib/memory-cycle2-gate.mjs +4 -4
  86. package/src/runtime/memory/lib/memory-cycle2-mutations.mjs +4 -3
  87. package/src/runtime/memory/lib/memory-cycle3.mjs +12 -2
  88. package/src/runtime/shared/tool-primitives.mjs +4 -1
  89. package/src/runtime/shared/tool-status.mjs +27 -0
  90. package/src/runtime/shared/tool-surface.mjs +6 -3
  91. package/src/session-runtime/config-helpers.mjs +14 -0
  92. package/src/session-runtime/context-status.mjs +1 -0
  93. package/src/session-runtime/effort.mjs +6 -2
  94. package/src/session-runtime/model-recency.mjs +5 -2
  95. package/src/session-runtime/provider-models.mjs +3 -3
  96. package/src/session-runtime/runtime-core.mjs +78 -10
  97. package/src/session-runtime/tool-catalog.mjs +34 -0
  98. package/src/session-runtime/warmup-schedulers.mjs +7 -1
  99. package/src/standalone/agent-tool/notify.mjs +13 -0
  100. package/src/standalone/agent-tool.mjs +45 -69
  101. package/src/standalone/explore-tool.mjs +6 -7
  102. package/src/tui/App.jsx +31 -0
  103. package/src/tui/app/model-options.mjs +5 -3
  104. package/src/tui/app/model-picker.mjs +12 -24
  105. package/src/tui/app/transcript-window.mjs +1 -0
  106. package/src/tui/components/ToolExecution.jsx +11 -6
  107. package/src/tui/components/TranscriptItem.jsx +1 -1
  108. package/src/tui/components/tool-execution/surface-detail.mjs +24 -10
  109. package/src/tui/components/tool-execution/text-format.mjs +10 -19
  110. package/src/tui/dist/index.mjs +533 -143
  111. package/src/tui/engine/agent-job-feed.mjs +153 -16
  112. package/src/tui/engine/agent-response-tail.mjs +68 -0
  113. package/src/tui/engine/notification-plan.mjs +16 -0
  114. package/src/tui/engine/queue-helpers.mjs +8 -0
  115. package/src/tui/engine/session-api.mjs +8 -2
  116. package/src/tui/engine/session-flow.mjs +34 -2
  117. package/src/tui/engine/tool-card-results.mjs +54 -32
  118. package/src/tui/engine/tool-result-status.mjs +75 -21
  119. package/src/tui/engine/turn.mjs +83 -43
  120. package/src/tui/engine.mjs +63 -2
  121. package/src/workflows/bench/WORKFLOW.md +25 -35
  122. package/src/workflows/default/WORKFLOW.md +38 -32
  123. package/src/workflows/solo/WORKFLOW.md +19 -22
  124. package/scripts/_jitter-fuzz.mjs +0 -44410
  125. package/scripts/_jitter-fuzz2.mjs +0 -44400
  126. package/scripts/_jitter-probe.mjs +0 -44397
  127. package/scripts/_jp2.mjs +0 -45614
@@ -0,0 +1,240 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
5
+ import { isAbsolute, join } from 'node:path';
6
+ import {
7
+ _resolveRgExecutable,
8
+ _rgEagainRetryArgs,
9
+ _rgThreadCap,
10
+ _withRgThreads,
11
+ ensureRgResolved,
12
+ rgSupportsPcre2,
13
+ } from '../src/runtime/agent/orchestrator/tools/builtin/rg-runner.mjs';
14
+
15
+ function makeRgFile(path) {
16
+ writeFileSync(path, '');
17
+ if (process.platform !== 'win32') chmodSync(path, 0o755);
18
+ }
19
+
20
+ test('rg resolver prefers direct current-directory/PATH search over where/which', async () => {
21
+ const originalCwd = process.cwd();
22
+ const originalPath = process.env.PATH;
23
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
24
+ const pathDir = join(dir, 'path');
25
+ mkdirSync(pathDir);
26
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
27
+ const current = join(dir, name);
28
+ const fromPath = join(pathDir, name);
29
+ makeRgFile(current);
30
+ makeRgFile(fromPath);
31
+ try {
32
+ process.chdir(dir);
33
+ process.env.PATH = pathDir;
34
+ assert.equal(await _resolveRgExecutable(), process.platform === 'win32' ? current : fromPath);
35
+ assert.ok(isAbsolute(await _resolveRgExecutable()));
36
+ } finally {
37
+ process.chdir(originalCwd);
38
+ if (originalPath === undefined) delete process.env.PATH;
39
+ else process.env.PATH = originalPath;
40
+ rmSync(dir, { recursive: true, force: true });
41
+ }
42
+ });
43
+
44
+ test('rg resolver honors PATH order when no current-directory executable exists', async () => {
45
+ const originalPath = process.env.PATH;
46
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
47
+ const first = join(dir, 'first');
48
+ const second = join(dir, 'second');
49
+ mkdirSync(first);
50
+ mkdirSync(second);
51
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
52
+ const expected = join(second, name);
53
+ makeRgFile(expected);
54
+ try {
55
+ process.env.PATH = `${first}${process.platform === 'win32' ? ';' : ':'}${second}`;
56
+ assert.equal(await _resolveRgExecutable(), expected);
57
+ } finally {
58
+ if (originalPath === undefined) delete process.env.PATH;
59
+ else process.env.PATH = originalPath;
60
+ rmSync(dir, { recursive: true, force: true });
61
+ }
62
+ });
63
+
64
+ test('rg resolver makes relative PATH candidates absolute', async () => {
65
+ const originalCwd = process.cwd();
66
+ const originalPath = process.env.PATH;
67
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
68
+ const bin = join(dir, 'bin');
69
+ mkdirSync(bin);
70
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
71
+ const expected = join(bin, name);
72
+ makeRgFile(expected);
73
+ try {
74
+ process.chdir(dir);
75
+ process.env.PATH = `.${process.platform === 'win32' ? ';' : ':'}bin`;
76
+ const resolved = await _resolveRgExecutable();
77
+ assert.equal(resolved, expected);
78
+ assert.ok(isAbsolute(resolved));
79
+ } finally {
80
+ process.chdir(originalCwd);
81
+ if (originalPath === undefined) delete process.env.PATH;
82
+ else process.env.PATH = originalPath;
83
+ rmSync(dir, { recursive: true, force: true });
84
+ }
85
+ });
86
+
87
+ test('rg resolver skips directories and non-executable POSIX files', async () => {
88
+ const originalPath = process.env.PATH;
89
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
90
+ const bad = join(dir, 'bad');
91
+ const good = join(dir, 'good');
92
+ mkdirSync(bad);
93
+ mkdirSync(good);
94
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
95
+ const nonExecutable = join(bad, name);
96
+ const expected = join(good, name);
97
+ if (process.platform === 'win32') mkdirSync(nonExecutable);
98
+ else writeFileSync(nonExecutable, '');
99
+ makeRgFile(expected);
100
+ try {
101
+ process.env.PATH = `${bad}${process.platform === 'win32' ? ';' : ':'}${good}`;
102
+ assert.equal(await _resolveRgExecutable(), expected);
103
+ } finally {
104
+ if (originalPath === undefined) delete process.env.PATH;
105
+ else process.env.PATH = originalPath;
106
+ rmSync(dir, { recursive: true, force: true });
107
+ }
108
+ });
109
+
110
+ test('rg resolver rejects Windows command wrappers', async (t) => {
111
+ if (process.platform !== 'win32') {
112
+ t.skip('Windows spawn resolution only');
113
+ return;
114
+ }
115
+ const originalPath = process.env.PATH;
116
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
117
+ writeFileSync(join(dir, 'rg.cmd'), '@echo off\r\n');
118
+ try {
119
+ process.env.PATH = dir;
120
+ assert.notEqual(await _resolveRgExecutable(), join(dir, 'rg.cmd'));
121
+ } finally {
122
+ if (originalPath === undefined) delete process.env.PATH;
123
+ else process.env.PATH = originalPath;
124
+ rmSync(dir, { recursive: true, force: true });
125
+ }
126
+ });
127
+
128
+ test('rg resolver preserves empty PATH components as current-directory entries', async () => {
129
+ const originalCwd = process.cwd();
130
+ const originalPath = process.env.PATH;
131
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
132
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
133
+ const expected = join(dir, name);
134
+ makeRgFile(expected);
135
+ try {
136
+ process.chdir(dir);
137
+ process.env.PATH = process.platform === 'win32' ? `;${dir}` : `:${dir}`;
138
+ assert.equal(await _resolveRgExecutable(), expected);
139
+ } finally {
140
+ process.chdir(originalCwd);
141
+ if (originalPath === undefined) delete process.env.PATH;
142
+ else process.env.PATH = originalPath;
143
+ rmSync(dir, { recursive: true, force: true });
144
+ }
145
+ });
146
+
147
+ test('rg resolver invalidates cached executable when PATH or file usability changes', async () => {
148
+ const originalPath = process.env.PATH;
149
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
150
+ const firstDir = join(dir, 'first');
151
+ const secondDir = join(dir, 'second');
152
+ mkdirSync(firstDir);
153
+ mkdirSync(secondDir);
154
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
155
+ const first = join(firstDir, name);
156
+ const second = join(secondDir, name);
157
+ makeRgFile(first);
158
+ makeRgFile(second);
159
+ try {
160
+ process.env.PATH = firstDir;
161
+ assert.equal(await ensureRgResolved(), first);
162
+ process.env.PATH = secondDir;
163
+ assert.equal(await ensureRgResolved(), second);
164
+ if (process.platform === 'win32') rmSync(second);
165
+ else chmodSync(second, 0o644);
166
+ assert.notEqual(await ensureRgResolved(), second);
167
+ if (process.platform !== 'win32') rmSync(second);
168
+ } finally {
169
+ if (originalPath === undefined) delete process.env.PATH;
170
+ else process.env.PATH = originalPath;
171
+ rmSync(dir, { recursive: true, force: true });
172
+ }
173
+ });
174
+
175
+ test('bare rg fallback is probed again after it becomes usable', async () => {
176
+ const originalPath = process.env.PATH;
177
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
178
+ try {
179
+ process.env.PATH = dir;
180
+ assert.equal(await ensureRgResolved(), 'rg');
181
+ const name = process.platform === 'win32' ? 'rg.exe' : 'rg';
182
+ makeRgFile(join(dir, name));
183
+ assert.notEqual(await ensureRgResolved(), 'rg');
184
+ } finally {
185
+ if (originalPath === undefined) delete process.env.PATH;
186
+ else process.env.PATH = originalPath;
187
+ rmSync(dir, { recursive: true, force: true });
188
+ }
189
+ });
190
+
191
+ test('PCRE2 capability cache follows the resolved executable', async (t) => {
192
+ if (process.platform === 'win32') {
193
+ t.skip('portable executable fixture unavailable on Windows');
194
+ return;
195
+ }
196
+ const originalPath = process.env.PATH;
197
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-rg-'));
198
+ const firstDir = join(dir, 'first');
199
+ const secondDir = join(dir, 'second');
200
+ mkdirSync(firstDir);
201
+ mkdirSync(secondDir);
202
+ const first = join(firstDir, 'rg');
203
+ const second = join(secondDir, 'rg');
204
+ writeFileSync(first, '#!/bin/sh\nprintf "PCRE2 10.40\\n"\n');
205
+ writeFileSync(second, '#!/bin/sh\nexit 1\n');
206
+ chmodSync(first, 0o755);
207
+ chmodSync(second, 0o755);
208
+ try {
209
+ process.env.PATH = firstDir;
210
+ assert.equal(await rgSupportsPcre2(), true);
211
+ process.env.PATH = secondDir;
212
+ assert.equal(await rgSupportsPcre2(), false);
213
+ } finally {
214
+ if (originalPath === undefined) delete process.env.PATH;
215
+ else process.env.PATH = originalPath;
216
+ rmSync(dir, { recursive: true, force: true });
217
+ }
218
+ });
219
+
220
+ test('rg thread cap honors the environment override', () => {
221
+ const original = process.env.MIXDOG_RG_THREADS;
222
+ try {
223
+ process.env.MIXDOG_RG_THREADS = '7.9';
224
+ assert.equal(_rgThreadCap(), 7);
225
+ process.env.MIXDOG_RG_THREADS = '0';
226
+ assert.ok(_rgThreadCap() >= 2);
227
+ } finally {
228
+ if (original === undefined) delete process.env.MIXDOG_RG_THREADS;
229
+ else process.env.MIXDOG_RG_THREADS = original;
230
+ }
231
+ });
232
+
233
+ test('rg treats a -e --threads pattern as a pattern during injection and retry', () => {
234
+ const args = ['-e', '--threads', 'needle'];
235
+ const injected = _withRgThreads(args);
236
+ assert.equal(injected[0], '--threads');
237
+ assert.equal(injected[2], '-e');
238
+ assert.deepEqual(injected.slice(3), ['--threads', 'needle']);
239
+ assert.deepEqual(_rgEagainRetryArgs(args), ['-j', '1', '-e', '--threads', 'needle']);
240
+ });
@@ -0,0 +1,349 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { mkdtempSync, writeFileSync } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { spawnSync } from 'node:child_process';
7
+ import { parseGrepCoverage, summarizeToolArgs } from '../src/runtime/agent/orchestrator/agent-trace-format.mjs';
8
+ import { executeGrepTool } from '../src/runtime/agent/orchestrator/tools/builtin/search-tool.mjs';
9
+ import { validateBuiltinArgs } from '../src/runtime/agent/orchestrator/tools/builtin/arg-guard.mjs';
10
+
11
+ test('grep coverage parser follows path:line and path-line formatter syntax', () => {
12
+ assert.deepEqual(
13
+ parseGrepCoverage('src/a.mjs:10:match\nsrc/a.mjs-11-context', 'grep', { output_mode: 'content_with_context' }, 'normal'),
14
+ [{ path: 'src/a.mjs', line: 10 }, { path: 'src/a.mjs', line: 11 }],
15
+ );
16
+ assert.deepEqual(
17
+ parseGrepCoverage('10:match\n11-context', 'grep', { path: 'src/a.mjs', output_mode: 'content_with_context' }, 'normal'),
18
+ [{ path: 'src/a.mjs', line: 10 }, { path: 'src/a.mjs', line: 11 }],
19
+ );
20
+ assert.deepEqual(
21
+ parseGrepCoverage('10:match', 'grep', { path: 'src/a.mjs' }, 'normal'),
22
+ [{ path: 'src/a.mjs', line: 10 }],
23
+ );
24
+ assert.equal(parseGrepCoverage('src/a.mjs:10:match', 'grep', { output_mode: 'files_with_matches' }, 'normal'), null);
25
+ assert.equal(parseGrepCoverage('Error: failed', 'grep', { output_mode: 'content_with_context' }, 'error'), null);
26
+ });
27
+
28
+ test('omitted grep mode receives contextual head-limit clamping', () => {
29
+ const args = { pattern: 'needle', head_limit: 999 };
30
+ assert.equal(validateBuiltinArgs('grep', args), null);
31
+ assert.equal(args.head_limit, 40);
32
+ });
33
+
34
+ test('production summaries retain code_graph files[]', () => {
35
+ assert.deepEqual(summarizeToolArgs('code_graph', { mode: 'symbols', files: ['src/a.mjs', 'src/b.mjs'] }).files, ['src/a.mjs', 'src/b.mjs']);
36
+ });
37
+
38
+ test('grep content_with_context supplies surrounding lines without explicit context', async () => {
39
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-grep-'));
40
+ writeFileSync(join(dir, 'sample.mjs'), 'before\nneedle\nafter\n');
41
+ const output = await executeGrepTool(
42
+ { pattern: 'needle', path: 'sample.mjs', output_mode: 'content_with_context' },
43
+ dir,
44
+ async () => '',
45
+ );
46
+ assert.match(String(output), /before/);
47
+ assert.match(String(output), /needle/);
48
+ assert.match(String(output), /after/);
49
+ });
50
+
51
+ test('grep output defaults to context while explicit content stays bare', async () => {
52
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-grep-mode-'));
53
+ writeFileSync(join(dir, 'sample.mjs'), 'before\nneedle\nafter\n');
54
+ const run = (args) => executeGrepTool(args, dir, async () => '');
55
+ const omitted = await run({ pattern: 'needle', path: 'sample.mjs' });
56
+ const bare = await run({ pattern: 'needle', path: 'sample.mjs', output_mode: 'content' });
57
+ const explicitZero = await run({ pattern: 'needle', path: 'sample.mjs', '-A': 0, '-B': 0, '-C': 0 });
58
+ assert.match(String(omitted), /before/);
59
+ assert.match(String(omitted), /after/);
60
+ assert.doesNotMatch(String(bare), /before/);
61
+ assert.doesNotMatch(String(bare), /after/);
62
+ assert.doesNotMatch(String(explicitZero), /before/);
63
+ assert.doesNotMatch(String(explicitZero), /after/);
64
+ });
65
+
66
+ test('grep path fan-out is bounded, ordered, isolated, and capped', async () => {
67
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-grep-paths-'));
68
+ const paths = Array.from({ length: 11 }, (_, index) => `missing-${index}.mjs`);
69
+ let active = 0;
70
+ let peak = 0;
71
+ const child = async () => {
72
+ active++;
73
+ peak = Math.max(peak, active);
74
+ await new Promise((resolve) => setTimeout(resolve, 10));
75
+ active--;
76
+ return '';
77
+ };
78
+ const scopedCacheOutcome = {};
79
+ const output = await executeGrepTool(
80
+ { pattern: 'needle', path: paths },
81
+ dir,
82
+ child,
83
+ null,
84
+ { scopedCacheOutcome },
85
+ );
86
+ assert.equal(peak, 4);
87
+ assert.match(output, /^# grep missing-0\.mjs\nError: path does not exist:/);
88
+ assert.match(output, /# grep missing-9\.mjs\nError: path does not exist:/);
89
+ assert.doesNotMatch(output, /# grep missing-10\.mjs\n/);
90
+ assert.match(output, /\[capped at 10 of 11 paths\]$/);
91
+ assert.equal(scopedCacheOutcome.complete, false);
92
+ });
93
+
94
+ test('nested grep path and pattern fan-out stays within four concurrent calls', async () => {
95
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-grep-nested-'));
96
+ let active = 0;
97
+ let peak = 0;
98
+ const child = async () => {
99
+ active++;
100
+ peak = Math.max(peak, active);
101
+ await new Promise((resolve) => setTimeout(resolve, 10));
102
+ active--;
103
+ return '';
104
+ };
105
+ await executeGrepTool(
106
+ { pattern: ['needle-a', 'needle-b', 'needle-c'], path: ['missing-a.mjs', 'missing-b.mjs', 'missing-c.mjs', 'missing-d.mjs'] },
107
+ dir,
108
+ child,
109
+ );
110
+ assert.equal(peak, 4);
111
+ });
112
+
113
+ test('path-section grep coverage survives legacy aggregate output', () => {
114
+ assert.deepEqual(
115
+ parseGrepCoverage(
116
+ '# grep src/a.mjs\n1:match\n# grep src/b.mjs\n2-context',
117
+ 'grep',
118
+ { path: ['src/a.mjs', 'src/b.mjs'], output_mode: 'content_with_context' },
119
+ 'normal',
120
+ ),
121
+ [{ path: 'src/a.mjs', line: 1 }, { path: 'src/b.mjs', line: 2 }],
122
+ );
123
+ assert.deepEqual(
124
+ parseGrepCoverage(
125
+ '# grep src/a.mjs\n# grep pattern:"one"\n1:match\n# grep pattern:"two"\n2-context\n# grep src/b.mjs\n# grep pattern:"one"\n3:match',
126
+ 'grep',
127
+ { path: ['src/a.mjs', 'src/b.mjs'], pattern: ['one', 'two'], output_mode: 'content_with_context' },
128
+ 'normal',
129
+ ),
130
+ [{ path: 'src/a.mjs', line: 1 }, { path: 'src/a.mjs', line: 2 }, { path: 'src/b.mjs', line: 3 }],
131
+ );
132
+ });
133
+
134
+ test('oversized path aggregate marks scoped cache incomplete before outer cap', async () => {
135
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-grep-output-cap-'));
136
+ const body = Array.from({ length: 1_500 }, () => 'needle\n').join('');
137
+ writeFileSync(join(dir, 'a.txt'), body);
138
+ writeFileSync(join(dir, 'b.txt'), body);
139
+ const scopedCacheOutcome = { complete: true };
140
+ const output = await executeGrepTool(
141
+ { pattern: 'needle', path: ['a.txt', 'b.txt'], output_mode: 'content', head_limit: 0 },
142
+ dir,
143
+ async () => '',
144
+ null,
145
+ { scopedCacheOutcome, toolOutputMaxBytes: 100 },
146
+ );
147
+ assert.ok(output.length > 100);
148
+ assert.equal(scopedCacheOutcome.complete, false);
149
+ });
150
+
151
+ function runCases(cases) {
152
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-routing-'));
153
+ const trace = join(dir, 'trace.jsonl');
154
+ const rows = cases.flatMap(([session_id, calls]) => calls.map((call, index) => ({
155
+ kind: 'tool', session_id, ts: index + 1, ...call,
156
+ })));
157
+ writeFileSync(trace, rows.map((row) => JSON.stringify(row)).join('\n'));
158
+ const result = spawnSync(process.execPath, ['scripts/routing-corpus.mjs', '--trace', trace, '--json'], { encoding: 'utf8' });
159
+ assert.equal(result.status, 0, result.stderr);
160
+ return JSON.parse(result.stdout);
161
+ }
162
+ function flags(corpus, session) {
163
+ return corpus.cases.find((item) => item.session_id === session)?.flags || [];
164
+ }
165
+ function observations(corpus, session) {
166
+ return corpus.cases.find((item) => item.session_id === session)?.observations || [];
167
+ }
168
+ const call = (tool_name, tool_args, extra = {}) => ({
169
+ tool_name, tool_args,
170
+ tool_args_hash: `synthetic-${tool_name}-${JSON.stringify(tool_args)}`,
171
+ ...extra,
172
+ });
173
+
174
+ test('routing corpus uses full hashes, result status, and mutation boundaries', () => {
175
+ const corpus = runCases([
176
+ ['hash-collision', [
177
+ call('grep', { pattern: 'same-summary' }, { tool_args_hash: 'hash-a', iteration: 1 }),
178
+ call('grep', { pattern: 'same-summary' }, { tool_args_hash: 'hash-b', iteration: 2 }),
179
+ ]],
180
+ ['failed-retry', [
181
+ call('read', { path: 'a.mjs' }, { result_kind: 'error', tool_args_hash: 'same', iteration: 1 }),
182
+ call('read', { path: 'a.mjs' }, { result_kind: 'success', tool_args_hash: 'same', iteration: 2 }),
183
+ ]],
184
+ ['mutation-reset', [
185
+ call('read', { path: 'a.mjs' }, { tool_args_hash: 'same', iteration: 1 }),
186
+ call('shell', { command: 'echo changed' }, { iteration: 2 }),
187
+ call('read', { path: 'a.mjs' }, { tool_args_hash: 'same', iteration: 3 }),
188
+ ]],
189
+ ['positive-relookup', [
190
+ call('grep', { pattern: 'same' }, { tool_args_hash: 'same-hash', iteration: 1 }),
191
+ call('grep', { pattern: 'same' }, { tool_args_hash: 'same-hash', iteration: 2 }),
192
+ ]],
193
+ ]);
194
+ assert.deepEqual(flags(corpus, 'hash-collision'), []);
195
+ assert.deepEqual(flags(corpus, 'failed-retry'), []);
196
+ assert.deepEqual(flags(corpus, 'mutation-reset'), []);
197
+ assert.ok(flags(corpus, 'positive-relookup').includes('grep_relookup'));
198
+ });
199
+
200
+ test('valid explore inspection is clean and context grep to same-file read is high-confidence', () => {
201
+ const corpus = runCases([
202
+ ['explore-inspection', [
203
+ call('explore', { query: 'where is config' }, { result_kind: 'success', iteration: 1 }),
204
+ call('read', { path: 'config.mjs' }, { result_kind: 'success', iteration: 2 }),
205
+ ]],
206
+ ['context-read', [
207
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { result_kind: 'success', result_lines_est: 4, grep_coverage: [{ path: 'a.mjs', line: 1 }, { path: 'a.mjs', line: 2 }, { path: 'a.mjs', line: 3 }], iteration: 1 }),
208
+ call('read', { path: 'a.mjs', offset: 0, limit: 3 }, { result_kind: 'success', iteration: 2 }),
209
+ ]],
210
+ ['files-only', [
211
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'files_with_matches' }, { result_kind: 'success', result_lines_est: 1, iteration: 1 }),
212
+ call('read', { path: 'a.mjs' }, { result_kind: 'success', iteration: 2 }),
213
+ ]],
214
+ ['different-path', [
215
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { result_kind: 'success', result_lines_est: 1, grep_coverage: [{ path: 'a.mjs', line: 1 }], iteration: 1 }),
216
+ call('read', { path: 'b.mjs', offset: 0, limit: 1 }, { result_kind: 'success', iteration: 2 }),
217
+ ]],
218
+ ['full-file-read', [
219
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { result_kind: 'success', grep_coverage: [{ path: 'a.mjs', line: 1 }], iteration: 1 }),
220
+ call('read', { path: 'a.mjs' }, { result_kind: 'success', iteration: 2 }),
221
+ ]],
222
+ ['zero-limit', [
223
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { grep_coverage: [{ path: 'a.mjs', line: 1 }], iteration: 1 }),
224
+ call('read', { path: 'a.mjs', offset: 0, limit: 0 }, { iteration: 2 }),
225
+ ]],
226
+ ['partial-coverage', [
227
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { grep_coverage: [{ path: 'a.mjs', line: 1 }, { path: 'a.mjs', line: 3 }], iteration: 1 }),
228
+ call('read', { path: 'a.mjs', offset: 0, limit: 3 }, { iteration: 2 }),
229
+ ]],
230
+ ['relative-absolute', [
231
+ call('grep', { pattern: 'needle', path: 'a.mjs', output_mode: 'content_with_context' }, { cwd: '/tmp/project', grep_coverage: [{ path: 'a.mjs', line: 1 }], iteration: 1 }),
232
+ call('read', { path: '/tmp/project/a.mjs', offset: 0, limit: 1 }, { cwd: '/tmp/project', iteration: 2 }),
233
+ ]],
234
+ ['posix-case', [
235
+ call('grep', { pattern: 'needle', path: 'A.mjs', output_mode: 'content_with_context' }, { cwd: '/tmp/project', grep_coverage: [{ path: 'A.mjs', line: 1 }], iteration: 1 }),
236
+ call('read', { path: '/tmp/project/a.mjs', offset: 0, limit: 1 }, { cwd: '/tmp/project', iteration: 2 }),
237
+ ]],
238
+ ]);
239
+ assert.deepEqual(flags(corpus, 'explore-inspection'), []);
240
+ assert.ok(flags(corpus, 'context-read').includes('grep_context_then_read'));
241
+ assert.ok(!flags(corpus, 'files-only').includes('grep_context_then_read'));
242
+ assert.ok(!flags(corpus, 'different-path').includes('grep_context_then_read'));
243
+ assert.ok(!flags(corpus, 'full-file-read').includes('grep_context_then_read'));
244
+ assert.ok(!flags(corpus, 'zero-limit').includes('grep_context_then_read'));
245
+ assert.ok(!flags(corpus, 'partial-coverage').includes('grep_context_then_read'));
246
+ assert.ok(flags(corpus, 'relative-absolute').includes('grep_context_then_read'));
247
+ assert.ok(!flags(corpus, 'posix-case').includes('grep_context_then_read'));
248
+ });
249
+
250
+ test('same-iteration compatible calls are visible observations, not defects', () => {
251
+ const families = [
252
+ ['read', [{ path: 'a', offset: 0, limit: 10 }, { path: 'b', offset: 10, limit: 10 }]],
253
+ ['grep', [{ pattern: 'a', path: '.' }, { pattern: 'b', path: '.' }]],
254
+ ['find', [{ query: 'a' }, { query: 'b' }]],
255
+ ['glob', [{ pattern: '*.a', path: '.' }, { pattern: '*.b', path: '.' }]],
256
+ ['list', [{ path: 'a' }, { path: 'b' }]],
257
+ ['explore', [{ query: 'a' }, { query: 'b' }]],
258
+ ['code_graph', [{ mode: 'symbols', symbols: ['A'] }, { mode: 'symbols', symbols: ['B'] }]],
259
+ ];
260
+ const cases = families.map(([tool, args], index) => [`batch-${index}`, args.map((tool_args) => call(tool, tool_args, { iteration: 1 }))]);
261
+ cases.push(
262
+ ['grep-paths', [
263
+ call('grep', { pattern: 'same', path: 'a' }, { iteration: 1 }),
264
+ call('grep', { pattern: 'same', path: 'b' }, { iteration: 1 }),
265
+ ]],
266
+ ['graph-files-in-symbol-mode', [
267
+ call('code_graph', { mode: 'symbols', files: ['src/a.mjs'] }, { iteration: 1 }),
268
+ call('code_graph', { mode: 'symbols', files: ['src/b.mjs'] }, { iteration: 1 }),
269
+ ]],
270
+ ['graph-singular-symbols-mode', [
271
+ call('code_graph', { mode: 'symbols', symbol: 'A' }, { iteration: 1 }),
272
+ call('code_graph', { mode: 'symbols', symbol: 'B' }, { iteration: 1 }),
273
+ ]],
274
+ ['unsupported-symbol-files', [
275
+ call('code_graph', { mode: 'symbol_search', files: ['src/a.mjs'] }, { iteration: 1 }),
276
+ call('code_graph', { mode: 'symbol_search', files: ['src/b.mjs'] }, { iteration: 1 }),
277
+ ]],
278
+ ['plain-reads', [
279
+ call('read', { path: 'a' }, { iteration: 1 }),
280
+ call('read', { path: 'b' }, { iteration: 1 }),
281
+ ]],
282
+ ['mixed-read-regions', [
283
+ call('read', { path: 'a', offset: 0, limit: 4 }, { iteration: 1 }),
284
+ call('read', { path: [{ path: 'b', offset: 4, limit: 4 }] }, { iteration: 1 }),
285
+ ]],
286
+ ['singleton-array-read', [
287
+ call('read', { path: ['a'] }, { iteration: 1 }),
288
+ call('read', { path: ['b'] }, { iteration: 1 }),
289
+ ]],
290
+ ['incompatible', [
291
+ call('grep', { pattern: 'a', path: '.', output_mode: 'count' }, { iteration: 1 }),
292
+ call('grep', { pattern: 'b', path: '.', output_mode: 'content' }, { iteration: 1 }),
293
+ ]],
294
+ ['mixed-three', [
295
+ call('grep', { pattern: 'a', path: '.' }, { iteration: 1 }),
296
+ call('grep', { pattern: 'b', path: '.' }, { iteration: 1 }),
297
+ call('grep', { pattern: 'c', path: '.', output_mode: 'count' }, { iteration: 1 }),
298
+ ]],
299
+ ['different-iteration', [
300
+ call('read', { path: 'a' }, { iteration: 1 }),
301
+ call('read', { path: 'b' }, { iteration: 2 }),
302
+ ]],
303
+ );
304
+ const corpus = runCases(cases);
305
+ for (let index = 0; index < families.length; index += 1) {
306
+ assert.ok(observations(corpus, `batch-${index}`).includes('same_turn_batch_opportunity'), `${families[index][0]}: ${observations(corpus, `batch-${index}`)}`);
307
+ assert.deepEqual(flags(corpus, `batch-${index}`), []);
308
+ }
309
+ for (const session of ['grep-paths', 'graph-files-in-symbol-mode', 'graph-singular-symbols-mode', 'plain-reads', 'mixed-read-regions', 'singleton-array-read']) {
310
+ assert.ok(observations(corpus, session).includes('same_turn_batch_opportunity'));
311
+ assert.deepEqual(flags(corpus, session), []);
312
+ }
313
+ assert.ok(!flags(corpus, 'incompatible').includes('missed_array_batch'));
314
+ assert.ok(observations(corpus, 'mixed-three').includes('same_turn_batch_opportunity'));
315
+ assert.deepEqual(flags(corpus, 'mixed-three'), []);
316
+ assert.ok(!flags(corpus, 'different-iteration').includes('missed_array_batch'));
317
+ });
318
+
319
+ test('successful overlapping reads flag once, while exact relookup does not double-count overlap', () => {
320
+ const corpus = runCases([
321
+ ['overlap', [
322
+ call('read', { path: 'src/a.mjs', offset: 0, limit: 10 }, { iteration: 1 }),
323
+ call('read', { path: './src/a.mjs', offset: 5, limit: 10 }, { iteration: 2 }),
324
+ ]],
325
+ ['duplicate', [
326
+ call('read', { path: 'src/a.mjs', offset: 0, limit: 10 }, { iteration: 1, tool_args_hash: 'same' }),
327
+ call('read', { path: 'src/a.mjs', offset: 0, limit: 10 }, { iteration: 2, tool_args_hash: 'same' }),
328
+ ]],
329
+ ['posix-case', [
330
+ call('read', { path: 'A.mjs', offset: 0, limit: 10 }, { cwd: '/tmp/project', iteration: 1 }),
331
+ call('read', { path: 'a.mjs', offset: 5, limit: 10 }, { cwd: '/tmp/project', iteration: 2 }),
332
+ ]],
333
+ ['windows-case', [
334
+ call('read', { path: 'C:\\Project\\A.mjs', offset: 0, limit: 10 }, { cwd: 'C:\\Project', iteration: 1 }),
335
+ call('read', { path: 'c:/project/a.mjs', offset: 5, limit: 10 }, { cwd: 'C:\\Project', iteration: 2 }),
336
+ ]],
337
+ ]);
338
+ assert.deepEqual(flags(corpus, 'overlap'), ['read_overlap']);
339
+ assert.deepEqual(flags(corpus, 'duplicate'), ['read_relookup']);
340
+ assert.deepEqual(flags(corpus, 'posix-case'), []);
341
+ assert.deepEqual(flags(corpus, 'windows-case'), ['read_overlap']);
342
+ });
343
+
344
+ test('corpus summary exposes precise flags', () => {
345
+ const corpus = runCases([['empty', [call('read', { path: 'a' })]]]);
346
+ for (const key of ['grep_relookup', 'read_relookup', 'read_overlap', 'grep_context_then_read', 'missed_array_batch']) {
347
+ assert.equal(typeof corpus.summary[key].count, 'number');
348
+ }
349
+ });