mixdog 0.9.45 → 0.9.46

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 (116) hide show
  1. package/package.json +5 -2
  2. package/scripts/agent-dispatch-abort-compose-test.mjs +31 -0
  3. package/scripts/agent-model-liveness-test.mjs +618 -0
  4. package/scripts/agent-trace-io-test.mjs +68 -4
  5. package/scripts/bench-run.mjs +30 -3
  6. package/scripts/compact-pressure-test.mjs +187 -4
  7. package/scripts/compact-prior-context-flatten-test.mjs +252 -0
  8. package/scripts/compact-trigger-migration-smoke.mjs +8 -6
  9. package/scripts/compacted-placeholder-scrub-test.mjs +20 -34
  10. package/scripts/context-mcp-metering-test.mjs +75 -0
  11. package/scripts/explore-prompt-policy-test.mjs +15 -16
  12. package/scripts/explore-timeout-cancel-test.mjs +345 -0
  13. package/scripts/headless-pristine-execution-test.mjs +614 -0
  14. package/scripts/internal-comms-smoke.mjs +15 -6
  15. package/scripts/live-worker-smoke.mjs +1 -1
  16. package/scripts/memory-core-input-test.mjs +137 -0
  17. package/scripts/memory-rule-contract-test.mjs +5 -5
  18. package/scripts/openai-oauth-refresh-race-test.mjs +120 -0
  19. package/scripts/openai-oauth-ws-1006-retry-test.mjs +26 -0
  20. package/scripts/parent-abort-link-test.mjs +22 -0
  21. package/scripts/provider-toolcall-test.mjs +22 -0
  22. package/scripts/reactive-compact-persist-smoke.mjs +8 -4
  23. package/scripts/session-bench.mjs +3 -70
  24. package/scripts/task-bench.mjs +0 -2
  25. package/scripts/terminal-bench-isolation-guards-test.mjs +102 -0
  26. package/scripts/tool-smoke.mjs +21 -21
  27. package/scripts/tool-tui-presentation-test.mjs +68 -0
  28. package/scripts/tui-transcript-jitter-harness-entry.jsx +91 -10
  29. package/src/app.mjs +28 -103
  30. package/src/cli.mjs +17 -13
  31. package/src/headless-command.mjs +139 -0
  32. package/src/headless-role.mjs +121 -10
  33. package/src/help.mjs +4 -1
  34. package/src/rules/agent/00-common.md +3 -3
  35. package/src/rules/agent/00-core.md +8 -9
  36. package/src/rules/agent/20-skip-protocol.md +2 -3
  37. package/src/rules/agent/30-explorer.md +50 -56
  38. package/src/rules/agent/40-cycle1-agent.md +10 -12
  39. package/src/rules/agent/41-cycle2-agent.md +12 -9
  40. package/src/rules/agent/42-cycle3-agent.md +4 -6
  41. package/src/rules/lead/01-general.md +5 -6
  42. package/src/rules/lead/02-channels.md +1 -1
  43. package/src/rules/lead/lead-brief.md +14 -17
  44. package/src/rules/lead/lead-tool.md +3 -3
  45. package/src/rules/shared/01-tool.md +41 -43
  46. package/src/runtime/agent/orchestrator/agent-runtime/agent-dispatch.mjs +46 -10
  47. package/src/runtime/agent/orchestrator/agent-runtime/agent-progress-watchdog.mjs +44 -31
  48. package/src/runtime/agent/orchestrator/agent-trace-io.mjs +18 -3
  49. package/src/runtime/agent/orchestrator/config.mjs +96 -30
  50. package/src/runtime/agent/orchestrator/context/collect.mjs +9 -0
  51. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-credentials.mjs +3 -0
  52. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +18 -5
  53. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +6 -6
  54. package/src/runtime/agent/orchestrator/providers/gemini.mjs +6 -6
  55. package/src/runtime/agent/orchestrator/providers/grok-oauth.mjs +123 -3
  56. package/src/runtime/agent/orchestrator/providers/oauth-credential-probes.mjs +12 -3
  57. package/src/runtime/agent/orchestrator/providers/openai-compat-stream.mjs +148 -30
  58. package/src/runtime/agent/orchestrator/providers/openai-compat.mjs +5 -7
  59. package/src/runtime/agent/orchestrator/providers/openai-oauth-http-sse.mjs +62 -14
  60. package/src/runtime/agent/orchestrator/providers/openai-oauth-ws.mjs +141 -19
  61. package/src/runtime/agent/orchestrator/providers/openai-oauth.mjs +12 -4
  62. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +19 -1
  63. package/src/runtime/agent/orchestrator/providers/openai-ws-stream.mjs +85 -17
  64. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +6 -8
  65. package/src/runtime/agent/orchestrator/providers/registry.mjs +47 -17
  66. package/src/runtime/agent/orchestrator/session/compact/summary.mjs +159 -20
  67. package/src/runtime/agent/orchestrator/session/context-utils.mjs +83 -10
  68. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +75 -7
  69. package/src/runtime/agent/orchestrator/session/loop/steering-ladder.mjs +4 -374
  70. package/src/runtime/agent/orchestrator/session/loop/stored-tool-args.mjs +0 -75
  71. package/src/runtime/agent/orchestrator/session/loop/transcript-repair.mjs +0 -5
  72. package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +20 -3
  73. package/src/runtime/agent/orchestrator/session/manager/compaction-runner.mjs +32 -16
  74. package/src/runtime/agent/orchestrator/session/manager/context-meta.mjs +5 -2
  75. package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +86 -15
  76. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +30 -27
  77. package/src/runtime/agent/orchestrator/session/tool-batch.mjs +1 -14
  78. package/src/runtime/agent/orchestrator/tools/builtin/builtin-tools.mjs +21 -27
  79. package/src/runtime/agent/orchestrator/tools/builtin/search-tool.mjs +1 -3
  80. package/src/runtime/agent/orchestrator/tools/builtin.mjs +1 -51
  81. package/src/runtime/agent/orchestrator/tools/code-graph-tool-defs.mjs +4 -4
  82. package/src/runtime/agent/orchestrator/tools/patch/orchestrator.mjs +1 -1
  83. package/src/runtime/agent/orchestrator/tools/patch-manifest.json +26 -0
  84. package/src/runtime/memory/index.mjs +0 -1
  85. package/src/runtime/memory/lib/core-memory-store.mjs +44 -24
  86. package/src/runtime/memory/lib/http-router.mjs +0 -193
  87. package/src/runtime/memory/lib/memory-action-handlers.mjs +37 -11
  88. package/src/runtime/memory/tool-defs.mjs +5 -6
  89. package/src/runtime/shared/config.mjs +11 -34
  90. package/src/runtime/shared/pristine-execution-contract.json +75 -0
  91. package/src/runtime/shared/pristine-execution.mjs +356 -0
  92. package/src/runtime/shared/provider-api-key.mjs +43 -0
  93. package/src/runtime/shared/provider-auth-binding.mjs +21 -0
  94. package/src/session-runtime/context-status.mjs +61 -13
  95. package/src/session-runtime/mcp-glue.mjs +29 -2
  96. package/src/session-runtime/plugin-mcp.mjs +7 -0
  97. package/src/session-runtime/resource-api.mjs +38 -5
  98. package/src/session-runtime/runtime-core.mjs +5 -1
  99. package/src/session-runtime/session-turn-api.mjs +14 -2
  100. package/src/session-runtime/settings-api.mjs +5 -0
  101. package/src/session-runtime/tool-catalog.mjs +13 -2
  102. package/src/session-runtime/tool-defs.mjs +1 -3
  103. package/src/standalone/agent-task-status.mjs +50 -11
  104. package/src/standalone/agent-tool/tool-def.mjs +1 -1
  105. package/src/standalone/explore-tool.mjs +257 -49
  106. package/src/standalone/seeds.mjs +1 -0
  107. package/src/tui/App.jsx +23 -10
  108. package/src/tui/app/use-transcript-scroll.mjs +4 -3
  109. package/src/tui/app/use-transcript-window.mjs +12 -21
  110. package/src/tui/components/ContextPanel.jsx +19 -25
  111. package/src/tui/dist/index.mjs +77 -65
  112. package/src/tui/engine/agent-envelope.mjs +16 -5
  113. package/src/tui/engine/labels.mjs +1 -1
  114. package/src/workflows/default/WORKFLOW.md +21 -51
  115. package/src/workflows/solo/WORKFLOW.md +12 -17
  116. package/src/workflows/solo-review/WORKFLOW.md +0 -47
@@ -1,6 +1,6 @@
1
1
  import assert from 'node:assert/strict';
2
- import { spawnSync } from 'node:child_process';
3
- import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
+ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join, resolve } from 'node:path';
6
6
  import { fileURLToPath, pathToFileURL } from 'node:url';
@@ -11,7 +11,8 @@ const traceModuleUrl = pathToFileURL(join(root, 'src/runtime/agent/orchestrator/
11
11
 
12
12
  test('drainAgentTrace awaits delayed local JSONL append without a memory service', () => {
13
13
  const dir = mkdtempSync(join(tmpdir(), 'mixdog-agent-trace-io-test-'));
14
- const tracePath = join(dir, 'agent-trace.jsonl');
14
+ const traceParent = join(dir, 'nested');
15
+ const tracePath = join(traceParent, 'history', 'agent-trace.jsonl');
15
16
  const loaderPath = join(dir, 'loader.mjs');
16
17
  const delayedFsPath = join(dir, 'delayed-fs-promises.mjs');
17
18
  try {
@@ -36,9 +37,10 @@ test('drainAgentTrace awaits delayed local JSONL append without a memory service
36
37
  '--input-type=module',
37
38
  '-e',
38
39
  `
39
- import { readFileSync } from 'node:fs';
40
+ import { readFileSync, rmSync } from 'node:fs';
40
41
  import { appendAgentTrace, drainAgentTrace } from ${JSON.stringify(traceModuleUrl)};
41
42
  appendAgentTrace({ kind: 'delayed-local-test', session_id: 'sess_delayed' });
43
+ rmSync(process.env.TRACE_PARENT, { recursive: true, force: true });
42
44
  const started = Date.now();
43
45
  await drainAgentTrace();
44
46
  const rows = readFileSync(process.env.MIXDOG_AGENT_TRACE_PATH, 'utf8').trim().split(/\\r?\\n/).map(JSON.parse);
@@ -52,6 +54,7 @@ test('drainAgentTrace awaits delayed local JSONL append without a memory service
52
54
  MIXDOG_AGENT_TRACE_DISABLE: '',
53
55
  MIXDOG_AGENT_TRACE_LOCAL_DISABLE: '',
54
56
  MIXDOG_RUNTIME_ROOT: join(dir, 'no-service'),
57
+ TRACE_PARENT: traceParent,
55
58
  TRACE_APPEND_DELAY_MS: '75',
56
59
  },
57
60
  timeout: 5000,
@@ -67,3 +70,64 @@ test('drainAgentTrace awaits delayed local JSONL append without a memory service
67
70
  rmSync(dir, { recursive: true, force: true });
68
71
  }
69
72
  });
73
+
74
+ test('explicit trace path stays append-only across concurrent writers', async () => {
75
+ const dir = mkdtempSync(join(tmpdir(), 'mixdog-agent-trace-multiwriter-test-'));
76
+ const tracePath = join(dir, 'agent-trace.jsonl');
77
+ const writerCount = 4;
78
+ const rowsPerWriter = 20;
79
+ try {
80
+ // Start above the normal rotation threshold. An explicit shared sink must
81
+ // retain this row in the live file while every child appends to it.
82
+ writeFileSync(tracePath, `${JSON.stringify({
83
+ kind: 'multiwriter-seed',
84
+ session_id: 'sess_seed',
85
+ payload: 'x'.repeat(10 * 1024 * 1024),
86
+ })}\n`);
87
+ const childSource = `
88
+ import { appendAgentTrace, drainAgentTrace } from ${JSON.stringify(traceModuleUrl)};
89
+ for (let i = 0; i < Number(process.env.ROWS_PER_WRITER); i += 1) {
90
+ appendAgentTrace({
91
+ kind: 'multiwriter',
92
+ session_id: 'sess_writer_' + process.env.WRITER_ID,
93
+ payload: { writer: Number(process.env.WRITER_ID), index: i },
94
+ });
95
+ }
96
+ await drainAgentTrace();
97
+ `;
98
+ await Promise.all(Array.from({ length: writerCount }, (_, writer) => new Promise((resolveChild, rejectChild) => {
99
+ const child = spawn(process.execPath, ['--input-type=module', '-e', childSource], {
100
+ cwd: root,
101
+ env: {
102
+ ...process.env,
103
+ MIXDOG_AGENT_TRACE_PATH: tracePath,
104
+ MIXDOG_AGENT_TRACE_DISABLE: '',
105
+ MIXDOG_AGENT_TRACE_LOCAL_DISABLE: '',
106
+ MIXDOG_RUNTIME_ROOT: join(dir, `no-service-${writer}`),
107
+ ROWS_PER_WRITER: String(rowsPerWriter),
108
+ WRITER_ID: String(writer),
109
+ },
110
+ stdio: ['ignore', 'ignore', 'pipe'],
111
+ });
112
+ let stderr = '';
113
+ child.stderr.setEncoding('utf8');
114
+ child.stderr.on('data', (chunk) => { stderr += chunk; });
115
+ child.on('error', rejectChild);
116
+ child.on('exit', (code) => {
117
+ if (code === 0) resolveChild();
118
+ else rejectChild(new Error(`writer ${writer} exited ${code}: ${stderr}`));
119
+ });
120
+ })));
121
+ assert.equal(existsSync(`${tracePath}.1`), false);
122
+ const rows = readFileSync(tracePath, 'utf8').trim().split(/\r?\n/).map(JSON.parse);
123
+ assert.equal(rows[0].kind, 'multiwriter-seed');
124
+ const written = rows.filter((row) => row.kind === 'multiwriter');
125
+ assert.equal(written.length, writerCount * rowsPerWriter);
126
+ assert.deepEqual(
127
+ [...new Set(written.map((row) => row.session_id))].sort(),
128
+ Array.from({ length: writerCount }, (_, writer) => `sess_writer_${writer}`),
129
+ );
130
+ } finally {
131
+ rmSync(dir, { recursive: true, force: true });
132
+ }
133
+ });
@@ -18,9 +18,10 @@
18
18
  // vs codex/claude (tree-total vs solo) is a separate future mode; codex/claude
19
19
  // runners are left as slots.
20
20
  import { execFile, execFileSync, spawn } from 'node:child_process';
21
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
21
+ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
22
+ import { tmpdir } from 'node:os';
22
23
  import { fileURLToPath, pathToFileURL } from 'node:url';
23
- import { dirname, resolve } from 'node:path';
24
+ import { dirname, join, resolve } from 'node:path';
24
25
 
25
26
  const __dir = dirname(fileURLToPath(import.meta.url));
26
27
  const HEADLESS = pathToFileURL(resolve(__dir, '../src/headless-role.mjs')).href;
@@ -160,6 +161,11 @@ const RUNNERS = {
160
161
  return { sessionId: extractSessionId(raw), ok, ms: Date.now() - started, raw };
161
162
  },
162
163
  async mixdog(task, opts) {
164
+ if (!opts.provider || !opts.model) {
165
+ throw new Error(
166
+ 'mixdog headless runner requires explicit --provider and --model (or a model alias)',
167
+ );
168
+ }
163
169
  const driver = [
164
170
  `import { runHeadlessRole } from ${JSON.stringify(HEADLESS)};`,
165
171
  `const out = [];`,
@@ -302,6 +308,9 @@ function num(v) {
302
308
  // only harness-neutral metric. Unknown models fall back to gpt-5.5 rates.
303
309
  const USD_PER_1M = {
304
310
  'gpt-5.5': { in: 5.00, cachedIn: 0.50, out: 30.00 },
311
+ 'gpt-5.6-sol': { in: 5.00, cachedIn: 0.50, out: 30.00 },
312
+ 'gpt-5.6-terra': { in: 2.50, cachedIn: 0.25, out: 15.00 },
313
+ 'gpt-5.6-luna': { in: 1.00, cachedIn: 0.10, out: 6.00 },
305
314
  'claude-sonnet-5': { in: 2.00, cachedIn: 0.20, out: 10.00 },
306
315
  'claude-fable-5': { in: 5.00, cachedIn: 0.50, out: 25.00 },
307
316
  };
@@ -344,6 +353,13 @@ const _mo = resolveModelOpts(argValue('--model', null), argValue('--provider', n
344
353
  // A/B knobs -> child env. --read-max-lines is a convenience for the read-policy
345
354
  // bench; --env KEY=VAL (repeatable) passes any override verbatim.
346
355
  const _env = {};
356
+ let ownedBenchTraceDir = null;
357
+ function cleanupOwnedBenchTrace() {
358
+ if (!ownedBenchTraceDir) return;
359
+ const dir = ownedBenchTraceDir;
360
+ ownedBenchTraceDir = null;
361
+ rmSync(dir, { recursive: true, force: true });
362
+ }
347
363
  const _readMax = argValue('--read-max-lines', null);
348
364
  if (_readMax) _env.MIXDOG_READ_MAX_LINES = String(_readMax);
349
365
  for (let i = 0; i < process.argv.length; i++) {
@@ -352,6 +368,15 @@ for (let i = 0; i < process.argv.length; i++) {
352
368
  if (eq > 0) _env[process.argv[i + 1].slice(0, eq)] = process.argv[i + 1].slice(eq + 1);
353
369
  }
354
370
  }
371
+ if (runnerName === 'mixdog' && !_env.MIXDOG_AGENT_TRACE_PATH) {
372
+ if (process.env.MIXDOG_AGENT_TRACE_PATH) {
373
+ _env.MIXDOG_AGENT_TRACE_PATH = process.env.MIXDOG_AGENT_TRACE_PATH;
374
+ } else {
375
+ ownedBenchTraceDir = mkdtempSync(join(tmpdir(), 'mixdog-bench-trace-'));
376
+ _env.MIXDOG_AGENT_TRACE_PATH = join(ownedBenchTraceDir, 'agent-trace.jsonl');
377
+ process.once('exit', cleanupOwnedBenchTrace);
378
+ }
379
+ }
355
380
  const opts = {
356
381
  provider: _mo.provider,
357
382
  model: _mo.model,
@@ -478,4 +503,6 @@ if (jsonMode) {
478
503
  console.log(`group: wall=${Math.round((g.wall_ms || 0) / 1000)}s turns=${g.turns} tools=${g.tool_calls} tpt=${g.tools_per_turn} tool_ms=${Math.round((g.total_tool_ms || 0) / 1000)}s cache=${Math.round((g.cache_ratio || 0) * 100)}% antipatterns=${g.antipatterns}`);
479
504
  }
480
505
  }
481
- if (roundResult.task_complete === false || roundResult.score_complete === false) process.exit(1);
506
+ const incomplete = roundResult.task_complete === false || roundResult.score_complete === false;
507
+ cleanupOwnedBenchTrace();
508
+ if (incomplete) process.exit(1);
@@ -14,18 +14,23 @@ import {
14
14
  recordProviderContextBaseline,
15
15
  resolveCompactionPressureTokens,
16
16
  rememberCompactTelemetry,
17
+ compactTargetBudget,
17
18
  resolveWorkerCompactPolicy,
18
19
  shouldCompactForSession,
19
20
  } from '../src/runtime/agent/orchestrator/session/loop/compact-policy.mjs';
21
+ import { initialCompactionConfig } from '../src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs';
22
+ import { resolveSessionCompactionPolicy } from '../src/runtime/agent/orchestrator/session/manager/compaction-runner.mjs';
23
+ import { sendWithRecovery } from '../src/runtime/agent/orchestrator/session/send-with-recovery.mjs';
20
24
 
21
25
  function policyFor(session) {
22
26
  return resolveWorkerCompactPolicy(session, []);
23
27
  }
24
28
 
25
- test('Lead/main auto-compaction triggers at 95% of the effective boundary', () => {
29
+ test('Lead/main auto-compaction uses an independent configurable early buffer', () => {
26
30
  const leadPolicy = policyFor({ contextWindow: 100_000, compaction: {} });
27
31
  assert.equal(leadPolicy.triggerTokens, 95_000);
28
32
  assert.equal(leadPolicy.bufferTokens, 5_000);
33
+ assert.equal(leadPolicy.bufferRatio, 0.05);
29
34
 
30
35
  const noReserve = { ...leadPolicy, reserveTokens: 0 };
31
36
  assert.equal(shouldCompactForSession(94_999, noReserve), false);
@@ -33,6 +38,182 @@ test('Lead/main auto-compaction triggers at 95% of the effective boundary', () =
33
38
 
34
39
  const agentPolicy = policyFor({ owner: 'agent', contextWindow: 100_000, compaction: {} });
35
40
  assert.equal(agentPolicy.triggerTokens, 90_000, 'agent default 10% headroom must remain unchanged');
41
+ assert.equal(agentPolicy.bufferTokens, 10_000);
42
+ assert.equal(agentPolicy.bufferRatio, 0.1);
43
+
44
+ const configured = policyFor({ contextWindow: 100_000, compaction: { mainBufferPercent: 15 } });
45
+ assert.equal(configured.triggerTokens, 85_000);
46
+ assert.equal(configured.bufferTokens, 15_000);
47
+ assert.equal(configured.bufferRatio, 0.15);
48
+
49
+ const oldEnv = {
50
+ tokens: process.env.MIXDOG_MAIN_COMPACT_BUFFER_TOKENS,
51
+ percent: process.env.MIXDOG_MAIN_COMPACT_BUFFER_PERCENT,
52
+ ratio: process.env.MIXDOG_MAIN_COMPACT_BUFFER_RATIO,
53
+ };
54
+ try {
55
+ process.env.MIXDOG_MAIN_COMPACT_BUFFER_PERCENT = '20';
56
+ const envConfigured = policyFor({ contextWindow: 100_000, compaction: {} });
57
+ assert.equal(envConfigured.triggerTokens, 80_000);
58
+ assert.equal(envConfigured.bufferTokens, 20_000);
59
+ assert.equal(envConfigured.bufferRatio, 0.2);
60
+
61
+ delete process.env.MIXDOG_MAIN_COMPACT_BUFFER_PERCENT;
62
+ process.env.MIXDOG_MAIN_COMPACT_BUFFER_RATIO = '0.1';
63
+ const envRatioConfigured = policyFor({ contextWindow: 100_000, compaction: {} });
64
+ assert.equal(envRatioConfigured.bufferTokens, 10_000, 'env ratio must configure the main buffer');
65
+
66
+ process.env.MIXDOG_MAIN_COMPACT_BUFFER_TOKENS = '10000';
67
+ process.env.MIXDOG_MAIN_COMPACT_BUFFER_RATIO = '0.1';
68
+ const envTokenConfigured = policyFor({ contextWindow: 100_000, compaction: {} });
69
+ assert.equal(envTokenConfigured.bufferTokens, 10_000, 'env tokens must beat env percent/ratio');
70
+ assert.equal(envTokenConfigured.triggerTokens, 90_000);
71
+
72
+ const configBeatsEnv = policyFor({
73
+ contextWindow: 100_000,
74
+ compaction: { mainBufferRatio: 0.15 },
75
+ });
76
+ assert.equal(configBeatsEnv.bufferTokens, 15_000, 'config ratio must beat all env units');
77
+
78
+ const configTokenWins = policyFor({
79
+ contextWindow: 100_000,
80
+ compaction: { mainBufferTokens: 12_000, mainBufferPercent: 15, mainBufferRatio: 0.1 },
81
+ });
82
+ assert.equal(configTokenWins.bufferTokens, 12_000, 'config tokens must beat config percent/ratio');
83
+
84
+ const clamped = policyFor({
85
+ contextWindow: 100_000,
86
+ compaction: { mainBufferPercent: 250, mainBufferRatio: 0.1 },
87
+ });
88
+ assert.equal(clamped.bufferTokens, 25_000, 'buffer percent must cap at the configured 25% maximum');
89
+ } finally {
90
+ for (const [name, value] of Object.entries({
91
+ MIXDOG_MAIN_COMPACT_BUFFER_TOKENS: oldEnv.tokens,
92
+ MIXDOG_MAIN_COMPACT_BUFFER_PERCENT: oldEnv.percent,
93
+ MIXDOG_MAIN_COMPACT_BUFFER_RATIO: oldEnv.ratio,
94
+ })) {
95
+ if (value === undefined) delete process.env[name];
96
+ else process.env[name] = value;
97
+ }
98
+ }
99
+ });
100
+
101
+ test('fresh session compaction config preserves main buffer fields', () => {
102
+ const config = initialCompactionConfig({
103
+ mainBufferTokens: 12_000,
104
+ mainBufferPercent: 15,
105
+ mainBufferRatio: 0.2,
106
+ mainBufferFraction: 0.1,
107
+ }, { compactBoundaryTokens: 80_000 });
108
+ assert.equal(config.mainBufferTokens, 12_000);
109
+ assert.equal(config.mainBufferPercent, 15);
110
+ assert.equal(config.mainBufferRatio, 0.2);
111
+ assert.equal(config.mainBufferFraction, 0.1);
112
+ assert.equal(config.boundaryTokens, 80_000);
113
+ });
114
+
115
+ test('small main boundary leaves a margin above the post-compact target', () => {
116
+ const session = {
117
+ contextWindow: 8_000,
118
+ compaction: { reservedTokens: 2_000 },
119
+ tools: [],
120
+ messages: [],
121
+ };
122
+ const policy = policyFor(session);
123
+ const target = compactTargetBudget(policy);
124
+ assert.ok(policy.triggerTokens > target,
125
+ `trigger ${policy.triggerTokens} must exceed post-compact target ${target}`);
126
+ assert.equal(shouldCompactForSession(target - policy.reserveTokens, policy), false,
127
+ 'reaching the post-compact target must not immediately re-trigger compaction');
128
+ const { contextStatus } = createContextStatus({
129
+ getSession: () => session,
130
+ getRoute: () => ({ provider: 'openai', model: 'test-model' }),
131
+ getCurrentCwd: () => '',
132
+ getMode: () => 'default',
133
+ });
134
+ const statusCompact = contextStatus().compaction;
135
+ assert.equal(statusCompact.triggerTokens, policy.triggerTokens);
136
+ assert.equal(statusCompact.bufferTokens, policy.bufferTokens);
137
+ assert.equal(statusCompact.bufferRatio, policy.bufferRatio);
138
+ const manualPolicy = resolveSessionCompactionPolicy(session);
139
+ assert.equal(manualPolicy.triggerTokens, policy.triggerTokens);
140
+ assert.equal(compactTargetBudget(manualPolicy), target);
141
+ });
142
+
143
+ test('degenerate main budgets use a documented single-shot fallback', () => {
144
+ const reserveAtTrigger = {
145
+ contextWindow: 100_000,
146
+ // Explicit sub-boundary limit fixes the trigger at 90k while reserve
147
+ // remains below the context boundary.
148
+ autoCompactTokenLimit: 90_000,
149
+ compaction: { reservedTokens: 91_000 },
150
+ tools: [],
151
+ };
152
+ const reserveAtTriggerPolicy = policyFor(reserveAtTrigger);
153
+ assert.equal(reserveAtTriggerPolicy.singleShot, true,
154
+ 'reserve at/above the actual trigger must be single-shot even below the boundary');
155
+ assert.equal(compactTargetBudget(reserveAtTriggerPolicy), reserveAtTriggerPolicy.boundaryTokens,
156
+ 'automatic single-shot keeps the legacy target budget');
157
+ assert.equal(compactTargetBudget({ ...reserveAtTriggerPolicy, force: true }), reserveAtTriggerPolicy.boundaryTokens,
158
+ 'forced manual compaction must retain the viable legacy target budget');
159
+
160
+ const overReserved = {
161
+ contextWindow: 8_000,
162
+ compaction: { reservedTokens: 20_000 },
163
+ tools: [],
164
+ };
165
+ const overReservedPolicy = policyFor(overReserved);
166
+ assert.equal(overReservedPolicy.singleShot, true);
167
+ assert.equal(compactTargetBudget(overReservedPolicy), 8_000,
168
+ 'reserve at or above the boundary must retain the legacy bounded target');
169
+ assert.equal(shouldCompactForSession(0, overReservedPolicy, { sessionRef: overReserved }), true);
170
+ rememberCompactTelemetry(overReserved, overReservedPolicy, { stage: 'compacting' });
171
+ assert.equal(shouldCompactForSession(0, overReservedPolicy, { sessionRef: overReserved }), false,
172
+ 'single-shot fallback must suppress automatic repeats');
173
+
174
+ const oneToken = { contextWindow: 1, compaction: {}, tools: [] };
175
+ const oneTokenPolicy = policyFor(oneToken);
176
+ assert.ok(oneTokenPolicy.reserveTokens >= oneTokenPolicy.triggerTokens);
177
+ assert.equal(oneTokenPolicy.singleShot, true,
178
+ 'the request reserve, not the one-token boundary itself, makes this degenerate');
179
+ assert.equal(oneTokenPolicy.triggerTokens, 1);
180
+ assert.equal(compactTargetBudget(oneTokenPolicy), 1);
181
+
182
+ // The reactive retry is explicitly bounded by send-with-recovery's
183
+ // contextOverflowRetryUsed flag: forceReactive may admit this one retry
184
+ // after the consumed one-shot, but no second retry is scheduled.
185
+ assert.equal(shouldCompactForSession(0, reserveAtTriggerPolicy, {
186
+ sessionRef: { compaction: { singleShotConsumed: true } },
187
+ forceReactive: true,
188
+ }), true, 'the one bounded reactive retry remains available after one-shot consumption');
189
+ });
190
+
191
+ test('a one-shot overflow receives only one reactive compact retry', async () => {
192
+ const overflow = new Error('context length exceeded');
193
+ const ctx = {
194
+ provider: { send: async () => { throw overflow; } },
195
+ messages: [{ role: 'user', content: 'overflowing prompt' }],
196
+ model: 'test-model',
197
+ sendTools: [],
198
+ tools: [],
199
+ opts: {},
200
+ sessionId: 'single-shot-reactive-test',
201
+ sessionRef: {
202
+ contextWindow: 100_000,
203
+ autoCompactTokenLimit: 90_000,
204
+ compaction: { reservedTokens: 91_000 },
205
+ },
206
+ nextIteration: 1,
207
+ };
208
+ assert.deepEqual(await sendWithRecovery({
209
+ ...ctx,
210
+ contextOverflowRetryUsed: false,
211
+ }), { action: 'retry' });
212
+ await assert.rejects(
213
+ sendWithRecovery({ ...ctx, contextOverflowRetryUsed: true }),
214
+ err => err?.code === 'AGENT_CONTEXT_OVERFLOW',
215
+ 'a second overflow after the reactive compact must surface instead of retrying again',
216
+ );
36
217
  });
37
218
 
38
219
  test('image payload bytes do not inflate live gauge or fallback compaction estimates', () => {
@@ -82,8 +263,10 @@ test('image payload bytes do not inflate live gauge or fallback compaction estim
82
263
  `image-aware live gauge must remain below compaction threshold, got ${status.usedTokens}`);
83
264
  assert.ok(status.usedTokens < status.contextWindow,
84
265
  `raw base64 must not pin the live gauge at 100%, got ${status.usedTokens}/${status.contextWindow}`);
266
+ assert.equal(status.compaction.bufferRatio, 0.05,
267
+ 'context status buffer ratio must match the shared compaction policy');
85
268
 
86
- recordProviderContextBaseline(session, messages, { inputTokens: 80_000, outputTokens: 0 });
269
+ recordProviderContextBaseline(session, messages, { inputTokens: 70_000, outputTokens: 0 });
87
270
  const policy = { ...policyFor(session), reserveTokens: 0 };
88
271
  assert.equal(
89
272
  shouldCompactForSession(fallbackEstimate, policy, { messages, sessionRef: session }),
@@ -329,7 +512,7 @@ test('thinking-only continuation without assistant replay excludes provider outp
329
512
  recordProviderContextBaseline(session, messages, {
330
513
  inputTokens: 5_000,
331
514
  outputTokens: 2_000,
332
- cachedTokens: 88_000,
515
+ cachedTokens: 68_000,
333
516
  cacheWriteTokens: 1_000,
334
517
  }, { boundary: 'request' });
335
518
  const nudge = {
@@ -340,7 +523,7 @@ test('thinking-only continuation without assistant replay excludes provider outp
340
523
 
341
524
  const wholeEstimate = estimateMessagesTokens(messages);
342
525
  const pressure = compactionTelemetryPressureTokens(wholeEstimate, policy, { messages, sessionRef: session });
343
- const expectedPressure = 94_000 + estimateMessagesTokens([nudge]);
526
+ const expectedPressure = 74_000 + estimateMessagesTokens([nudge]);
344
527
  assert.equal(pressure, expectedPressure, 'unreplayed output must be removed while the later nudge is estimated');
345
528
  assert.equal(shouldCompactForSession(wholeEstimate, policy, {
346
529
  messages,
@@ -0,0 +1,252 @@
1
+ #!/usr/bin/env node
2
+ // Regression test for the repeated-compaction prior-context invariant: every
3
+ // generated recall-fasttrack summary carries AT MOST ONE
4
+ // <prior-compacted-context> wrapper (never nested/duplicated across cycles),
5
+ // preserves each prior requirement exactly once, and keeps repeated-cycle
6
+ // token size bounded even when the same content is re-fed every cycle.
7
+ import test from 'node:test';
8
+ import assert from 'node:assert/strict';
9
+ import {
10
+ formatPriorCompactedContextBlock,
11
+ stripPriorCompactedContextWrappers,
12
+ stripNestedSummaryHeaderLines,
13
+ fitRecallFastTrackSummaryMessage,
14
+ fitRecallRootsMessage,
15
+ } from '../src/runtime/agent/orchestrator/session/compact/summary.mjs';
16
+
17
+ const OPEN = '<prior-compacted-context>';
18
+ const CLOSE = '</prior-compacted-context>';
19
+ const countOpen = (s) => (String(s).match(/<prior-compacted-context>/g) || []).length;
20
+ const countClose = (s) => (String(s).match(/<\/prior-compacted-context>/g) || []).length;
21
+ const countAll = (s, needle) => (String(s).match(new RegExp(needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length;
22
+
23
+ // A stable, non-empty compaction head so compactHeader() is deterministic.
24
+ const OLD = [{ role: 'user', content: 'seed task' }, { role: 'assistant', content: 'ack' }];
25
+ // Large enough budget that no fit-time truncation fires — the flattening path,
26
+ // not the truncation path, is under test.
27
+ const BIG = 1_000_000;
28
+ // The engine feeds the previous summary body back as the next cycle's prior
29
+ // after stripping the summary header lines (compact/engine.mjs splitRecallFitInputs).
30
+ const nextPrior = (msg) => stripNestedSummaryHeaderLines(String(msg?.content || ''));
31
+
32
+ test('formatPriorCompactedContextBlock wraps bare prior text exactly once', () => {
33
+ const out = formatPriorCompactedContextBlock('REQ-A keep files\nREQ-B run tests');
34
+ assert.equal(countOpen(out), 1);
35
+ assert.equal(countClose(out), 1);
36
+ assert.match(out, /REQ-A keep files/);
37
+ assert.match(out, /REQ-B run tests/);
38
+ });
39
+
40
+ test('formatPriorCompactedContextBlock flattens an already-wrapped prior to one wrapper', () => {
41
+ const alreadyWrapped = `${OPEN}\nREQ-A\nREQ-B\n${CLOSE}\n\nREQ-C`;
42
+ const out = formatPriorCompactedContextBlock(alreadyWrapped);
43
+ assert.equal(countOpen(out), 1);
44
+ assert.equal(countClose(out), 1);
45
+ for (const req of ['REQ-A', 'REQ-B', 'REQ-C']) assert.match(out, new RegExp(req));
46
+ });
47
+
48
+ test('formatPriorCompactedContextBlock flattens deeply nested wrappers to one', () => {
49
+ const nested = `${OPEN}\n${OPEN}\n${OPEN}\nDEEP\n${CLOSE}\nMID\n${CLOSE}\nTOP\n${CLOSE}`;
50
+ const out = formatPriorCompactedContextBlock(nested);
51
+ assert.equal(countOpen(out), 1);
52
+ assert.equal(countClose(out), 1);
53
+ for (const req of ['DEEP', 'MID', 'TOP']) assert.match(out, new RegExp(req));
54
+ });
55
+
56
+ test('formatPriorCompactedContextBlock keeps duplicate blocks exactly once', () => {
57
+ const dupe = 'REQ-A keep\n\nREQ-B test\n\nREQ-A keep';
58
+ const out = formatPriorCompactedContextBlock(dupe);
59
+ assert.equal(countAll(out, 'REQ-A keep'), 1);
60
+ assert.equal(countAll(out, 'REQ-B test'), 1);
61
+ });
62
+
63
+ test('dedupe is byte/content preserving: distinct-whitespace blocks are NOT merged', () => {
64
+ // `printf 'a b'` (two spaces) and `printf 'a b'` (one space) are DISTINCT
65
+ // commands — collapsing whitespace for the dedupe key would wrongly merge
66
+ // them and silently corrupt a preserved command. Both must survive verbatim.
67
+ const prior = "printf 'a b'\n\nprintf 'a b'";
68
+ const out = formatPriorCompactedContextBlock(prior);
69
+ assert.match(out, /printf 'a b'/, 'two-space variant preserved verbatim');
70
+ assert.match(out, /printf 'a b'/, 'one-space variant preserved verbatim');
71
+ // Both distinct blocks are kept (neither is dropped as a "duplicate").
72
+ assert.equal(countAll(out, "printf 'a b'"), 1);
73
+ // Exactly one structural wrapper around the two distinct blocks.
74
+ assert.equal(countOpen(out), 1);
75
+ assert.equal(countClose(out), 1);
76
+ });
77
+
78
+ test('dedupe removes only structurally identical blocks and preserves inner whitespace', () => {
79
+ // The two byte-identical `run step` blocks collapse to one (bounded
80
+ // growth); a block with distinct inner whitespace is preserved untouched.
81
+ const prior = 'run step\n\nrun step\n\nkeep\tthis spacing';
82
+ const out = formatPriorCompactedContextBlock(prior);
83
+ assert.equal(countAll(out, 'run step'), 1, 'exact repeats collapse to one');
84
+ assert.match(out, /keep\tthis spacing/, 'inner whitespace never collapsed/trimmed');
85
+ });
86
+
87
+ test('formatPriorCompactedContextBlock returns empty for blank / tag-only input', () => {
88
+ assert.equal(formatPriorCompactedContextBlock(''), '');
89
+ assert.equal(formatPriorCompactedContextBlock(`${OPEN}\n${CLOSE}`), '');
90
+ });
91
+
92
+ test('empty / blank / boundary-only prior yields ZERO wrappers (optimization-safe at-most-one)', () => {
93
+ // The production summary body joins only non-empty parts, so an empty prior
94
+ // is canonicalized to NO wrapper rather than an empty tag pair — "exactly
95
+ // one wrapper" is realized as one-when-content-exists, none otherwise,
96
+ // never more than one and never nested.
97
+ assert.equal(formatPriorCompactedContextBlock(''), '');
98
+ assert.equal(formatPriorCompactedContextBlock(' \n \t '), '');
99
+ assert.equal(formatPriorCompactedContextBlock(`${OPEN}\n${CLOSE}`), '');
100
+ assert.equal(formatPriorCompactedContextBlock(`${OPEN}\n\n \n${CLOSE}`), '');
101
+ assert.equal(formatPriorCompactedContextBlock(`${OPEN}\n${OPEN}\n${CLOSE}\n${CLOSE}`), '');
102
+ });
103
+
104
+ test('flattening preserves inline marker-like content verbatim (no P1 corruption)', () => {
105
+ // Regression: an earlier inline-strip regex turned "keep <prior-compacted-context>
106
+ // literal" into "keepliteral". Only STRUCTURAL boundary lines may be removed;
107
+ // inline marker-like user content must survive byte-for-byte.
108
+ const note = 'keep <prior-compacted-context> literal in this note';
109
+ const bare = stripPriorCompactedContextWrappers(`${OPEN}\n${note}\n${CLOSE}`);
110
+ assert.equal(bare, note);
111
+ assert.doesNotMatch(bare, /keepliteral/);
112
+ });
113
+
114
+ test('formatPriorCompactedContextBlock does not corrupt inline marker-like content', () => {
115
+ const note = 'REQ keep <prior-compacted-context> literal';
116
+ const out = formatPriorCompactedContextBlock(`${OPEN}\n${note}\n${CLOSE}`);
117
+ // Exactly one STRUCTURAL wrapper: boundary tags each appear once on their
118
+ // own line (the inline literal is content, not a boundary).
119
+ const lines = out.split('\n');
120
+ assert.equal(lines.filter((l) => l.trim() === OPEN).length, 1);
121
+ assert.equal(lines.filter((l) => l.trim() === CLOSE).length, 1);
122
+ assert.match(out, /REQ keep <prior-compacted-context> literal/);
123
+ });
124
+
125
+ test('stripPriorCompactedContextWrappers removes every wrapper tag', () => {
126
+ const bare = stripPriorCompactedContextWrappers(`${OPEN}\ninner-A\ninner-B\n${CLOSE}`);
127
+ assert.doesNotMatch(bare, /prior-compacted-context/);
128
+ assert.equal(bare, 'inner-A\ninner-B');
129
+ });
130
+
131
+ test('stripNestedSummaryHeaderLines strips the prior-compacted-context wrapper', () => {
132
+ const body = `${OPEN}\nREQ-A\n${CLOSE}\n\nREQ-B`;
133
+ const out = stripNestedSummaryHeaderLines(body);
134
+ assert.doesNotMatch(out, /prior-compacted-context/);
135
+ assert.match(out, /REQ-A/);
136
+ assert.match(out, /REQ-B/);
137
+ });
138
+
139
+ test('canonicalization preserves leading, trailing, and repeated newline bytes', () => {
140
+ const prior = '\n\nleading\n\n\nmiddle\n\ntrailing\n';
141
+ const out = formatPriorCompactedContextBlock(prior);
142
+ assert.ok(out.includes(`${OPEN}\n${prior}\n${CLOSE}`), 'wrapper keeps the prior bytes untouched');
143
+ assert.equal(stripNestedSummaryHeaderLines(out), prior,
144
+ 'structural header removal does not trim or collapse surrounding bytes');
145
+ });
146
+
147
+ test('both recall fitters retain prior whitespace byte-for-byte', () => {
148
+ const prior = '\n leading\n\n\ntrailing \n';
149
+ const fast = fitRecallFastTrackSummaryMessage(OLD, 'recall', BIG, {}, prior);
150
+ const roots = fitRecallRootsMessage(OLD, '# chunk 1 root=1\nbody', BIG, {}, prior);
151
+ for (const message of [fast, roots]) {
152
+ assert.ok(String(message.content).includes(`${OPEN}\n${prior}\n${CLOSE}`));
153
+ }
154
+ });
155
+
156
+ test('repeated recall-fasttrack compaction keeps one prior wrapper and every req once', () => {
157
+ const reqs = [
158
+ 'REQ-1 initial spec',
159
+ 'REQ-2 second decision',
160
+ 'REQ-3 third step',
161
+ 'REQ-4 fourth fact',
162
+ 'REQ-5 fifth note',
163
+ ];
164
+ let prior = '';
165
+ let last = '';
166
+ for (let i = 0; i < reqs.length; i += 1) {
167
+ const msg = fitRecallFastTrackSummaryMessage(OLD, reqs[i], BIG, {}, prior);
168
+ assert.ok(msg, `cycle ${i} produced a summary message`);
169
+ const body = String(msg.content || '');
170
+ assert.ok(countOpen(body) <= 1, `cycle ${i} has at most one open wrapper`);
171
+ assert.equal(countOpen(body), countClose(body), `cycle ${i} wrappers are balanced`);
172
+ prior = nextPrior(msg);
173
+ last = body;
174
+ }
175
+ for (const req of reqs) {
176
+ assert.equal(countAll(last, req), 1, `${req} survives into the final summary exactly once`);
177
+ }
178
+ });
179
+
180
+ test('repeated compaction with identical recall is byte-stable after the first canonical cycle', () => {
181
+ const recall = 'STABLE REQUIREMENT preserve exactly one copy';
182
+ let prior = '';
183
+ const sizes = [];
184
+ const serialized = [];
185
+ for (let i = 0; i < 8; i += 1) {
186
+ const msg = fitRecallFastTrackSummaryMessage(OLD, recall, BIG, {}, prior);
187
+ const body = String(msg.content || '');
188
+ assert.ok(countOpen(body) <= 1, `cycle ${i} never nests wrappers`);
189
+ // At most one copy inside the prior wrapper + one live recall copy.
190
+ assert.ok(countAll(body, 'STABLE REQUIREMENT') <= 2, `cycle ${i} keeps bounded copies`);
191
+ sizes.push(body.length);
192
+ serialized.push(body);
193
+ prior = nextPrior(msg);
194
+ }
195
+ const stableSizes = sizes.slice(1);
196
+ const stableBodies = serialized.slice(1);
197
+ assert.ok(stableSizes.every((size) => size === stableSizes[0]),
198
+ `serialized size is exactly stable after canonical cycle (sizes=${sizes.join(',')})`);
199
+ assert.ok(stableBodies.every((body) => body === stableBodies[0]),
200
+ 'serialized output is exactly stable after canonical cycle');
201
+ });
202
+
203
+ test('many generated-summary refeeds add no newline bytes', () => {
204
+ const recall = 'NO NEWLINE GROWTH';
205
+ let prior = '';
206
+ let canonical = null;
207
+ for (let i = 0; i < 32; i += 1) {
208
+ const message = fitRecallFastTrackSummaryMessage(OLD, recall, BIG, {}, prior);
209
+ const body = String(message.content || '');
210
+ if (i === 1) canonical = body;
211
+ if (i > 1) assert.equal(body, canonical, `cycle ${i} must not add wrapper separator bytes`);
212
+ prior = nextPrior(message);
213
+ }
214
+ });
215
+
216
+ test('duplicate live recall never consumes prior-owned leading or trailing newlines', () => {
217
+ const variants = [
218
+ 'X\n',
219
+ 'X\n\n',
220
+ 'X\n\n\n',
221
+ '\nX',
222
+ '\n\nX',
223
+ '\nX\n\n',
224
+ ];
225
+ for (const initialPrior of variants) {
226
+ let prior = initialPrior;
227
+ let canonical = null;
228
+ for (let i = 0; i < 32; i += 1) {
229
+ const message = fitRecallFastTrackSummaryMessage(OLD, 'X', BIG, {}, prior);
230
+ const body = String(message.content || '');
231
+ if (i === 0) canonical = body;
232
+ assert.equal(body, canonical, `variant=${JSON.stringify(initialPrior)} cycle=${i} changed bytes`);
233
+ prior = nextPrior(message);
234
+ }
235
+ }
236
+ });
237
+
238
+ test('smart-arrival roots compaction also flattens the re-fed prior to one wrapper', () => {
239
+ const roots = '# chunk 1 root=1\nmember a\n\n# chunk 2 root=2\nmember b';
240
+ const first = fitRecallRootsMessage(OLD, roots, BIG, {}, '');
241
+ let prior = nextPrior(first);
242
+ const second = fitRecallRootsMessage(OLD, '# chunk 3 root=3\nmember c', BIG, {}, prior);
243
+ const body = String(second.content || '');
244
+ assert.equal(countOpen(body), 1);
245
+ assert.equal(countClose(body), 1);
246
+ // Feed once more to prove the wrapper never nests across a third cycle.
247
+ prior = nextPrior(second);
248
+ const third = fitRecallRootsMessage(OLD, '# chunk 4 root=4\nmember d', BIG, {}, prior);
249
+ const body3 = String(third.content || '');
250
+ assert.equal(countOpen(body3), 1);
251
+ assert.equal(countClose(body3), 1);
252
+ });