mixdog 0.9.40 → 0.9.43

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 (66) hide show
  1. package/package.json +2 -1
  2. package/scripts/agent-tag-reuse-smoke.mjs +124 -10
  3. package/scripts/anthropic-oauth-refresh-race-test.mjs +397 -0
  4. package/scripts/arg-guard-test.mjs +16 -0
  5. package/scripts/compact-pressure-test.mjs +383 -0
  6. package/scripts/compact-trigger-migration-smoke.mjs +8 -8
  7. package/scripts/internal-comms-bench.mjs +0 -1
  8. package/scripts/internal-comms-smoke.mjs +54 -31
  9. package/scripts/internal-tools-normalization-test.mjs +52 -0
  10. package/scripts/max-output-recovery-persist-test.mjs +81 -0
  11. package/scripts/max-output-recovery-test.mjs +271 -0
  12. package/scripts/mcp-client-normalization-test.mjs +45 -0
  13. package/scripts/provider-toolcall-test.mjs +55 -0
  14. package/scripts/result-classification-test.mjs +75 -0
  15. package/scripts/smoke.mjs +1 -1
  16. package/scripts/submit-commandbusy-race-test.mjs +99 -7
  17. package/scripts/tui-transcript-perf-test.mjs +56 -1
  18. package/src/agents/reviewer/AGENT.md +8 -0
  19. package/src/rules/lead/lead-brief.md +11 -3
  20. package/src/rules/lead/lead-tool.md +0 -2
  21. package/src/rules/shared/01-tool.md +4 -0
  22. package/src/runtime/agent/orchestrator/agent-runtime/agent-progress-watchdog.mjs +20 -2
  23. package/src/runtime/agent/orchestrator/context/collect.mjs +7 -3
  24. package/src/runtime/agent/orchestrator/internal-tools.mjs +16 -3
  25. package/src/runtime/agent/orchestrator/mcp/client.mjs +17 -1
  26. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-credentials.mjs +193 -13
  27. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +15 -2
  28. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +14 -0
  29. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +2 -2
  30. package/src/runtime/agent/orchestrator/providers/media-normalization.mjs +59 -2
  31. package/src/runtime/agent/orchestrator/providers/openai-ws-stream.mjs +57 -25
  32. package/src/runtime/agent/orchestrator/session/agent-loop.mjs +72 -12
  33. package/src/runtime/agent/orchestrator/session/context-utils.mjs +144 -84
  34. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +124 -8
  35. package/src/runtime/agent/orchestrator/session/loop/steering-ladder.mjs +12 -1
  36. package/src/runtime/agent/orchestrator/session/loop/termination.mjs +25 -7
  37. package/src/runtime/agent/orchestrator/session/loop/tool-exec.mjs +12 -3
  38. package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +28 -1
  39. package/src/runtime/agent/orchestrator/session/manager/compaction-runner.mjs +2 -1
  40. package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +31 -4
  41. package/src/runtime/agent/orchestrator/session/pre-send-compact.mjs +11 -2
  42. package/src/runtime/agent/orchestrator/session/result-classification.mjs +4 -1
  43. package/src/runtime/agent/orchestrator/session/send-with-recovery.mjs +45 -0
  44. package/src/runtime/agent/orchestrator/session/tool-batch.mjs +7 -2
  45. package/src/runtime/agent/orchestrator/session/tool-envelope.mjs +8 -6
  46. package/src/runtime/agent/orchestrator/tools/builtin/arg-guard.mjs +15 -3
  47. package/src/runtime/agent/orchestrator/tools/builtin/bash-tool.mjs +22 -21
  48. package/src/runtime/agent/orchestrator/tools/builtin/builtin-tools.mjs +1 -1
  49. package/src/runtime/agent/orchestrator/tools/builtin/shell-analysis.mjs +1 -1
  50. package/src/runtime/agent/orchestrator/tools/shell-command.mjs +9 -10
  51. package/src/runtime/memory/tool-defs.mjs +4 -3
  52. package/src/runtime/shared/tool-surface.mjs +1 -2
  53. package/src/session-runtime/context-status.mjs +8 -2
  54. package/src/standalone/agent-tool/render.mjs +2 -0
  55. package/src/standalone/agent-tool.mjs +202 -22
  56. package/src/tui/components/StatusLine.jsx +4 -26
  57. package/src/tui/dist/index.mjs +46 -31
  58. package/src/tui/engine/session-api.mjs +3 -0
  59. package/src/tui/engine/session-flow.mjs +19 -8
  60. package/src/tui/engine/turn.mjs +24 -2
  61. package/src/tui/engine.mjs +0 -1
  62. package/src/ui/statusline-agents.mjs +0 -8
  63. package/src/ui/statusline.mjs +2 -4
  64. package/src/workflows/default/WORKFLOW.md +38 -24
  65. package/src/workflows/solo-review/WORKFLOW.md +47 -0
  66. package/src/workflows/bench/WORKFLOW.md +0 -36
@@ -3,8 +3,11 @@
3
3
  // runRecallFastTrackCompact stays in the loop (it drives the recall pipeline
4
4
  // against live session state).
5
5
  import {
6
+ contextMessagesSignature,
7
+ estimateMessagesTokens,
6
8
  estimateRequestReserveTokens,
7
9
  resolveSessionCompactPolicy,
10
+ toolSchemaSignature,
8
11
  } from '../context-utils.mjs';
9
12
  import {
10
13
  compactTypeIsRecallFastTrack,
@@ -18,6 +21,7 @@ import {
18
21
  } from '../compact.mjs';
19
22
  import { positiveTokenInt, envFlag, envTokenInt } from './env.mjs';
20
23
  import { isAgentOwner } from '../../agent-owner.mjs';
24
+ import { providerInputExcludesCache } from '../../providers/registry.mjs';
21
25
 
22
26
  // Unified context-share rule (compact/constants.mjs CONTEXT_SHARE_RATIO): the
23
27
  // post-compaction target is 10% of the boundary/context window — the same 10%
@@ -91,8 +95,8 @@ export function resolveWorkerCompactPolicy(sessionRef, tools) {
91
95
  if (!boundaryTokens) return null;
92
96
  const compactBoundaryTokens = Math.max(1, Math.floor(boundaryTokens * COMPACT_SAFETY_PERCENT));
93
97
  // Shared session-compaction policy (context-utils): agent semantic keeps the
94
- // default early-trigger buffer (90%); main/user compact on the boundary
95
- // (100%); a truly-explicit sub-boundary limit wins. explicitAutoCompactTokenLimit
98
+ // default early-trigger buffer (90%); main/user keep 5% headroom (95%);
99
+ // a truly-explicit sub-boundary limit wins. explicitAutoCompactTokenLimit
96
100
  // is the sanitized (null when legacy full-window) value so telemetry never
97
101
  // re-persists a boundary-collapsing limit.
98
102
  const policy = resolveSessionCompactPolicy(sessionRef, compactBoundaryTokens);
@@ -130,17 +134,121 @@ export function resolveWorkerCompactPolicy(sessionRef, tools) {
130
134
  reserveTokens: requestReserve + configuredReserve,
131
135
  requestReserveTokens: requestReserve,
132
136
  configuredReserveTokens: configuredReserve,
137
+ toolSchemaSignature: toolSchemaSignature(tools),
133
138
  };
134
139
  }
135
- /** Transcript + request reserve only (never provider lastContextTokens). */
140
+ /** Transcript + request reserve fallback used until an aligned provider baseline exists. */
136
141
  function compactPressureTokens(messageTokensEst, policy) {
137
142
  if (messageTokensEst === null) return 0;
138
143
  return Math.max(0, messageTokensEst + (policy?.reserveTokens || 0));
139
144
  }
140
145
 
146
+ function providerPressureTokens(sessionRef, usage) {
147
+ if (!usage || typeof usage !== 'object') return 0;
148
+ const input = Math.max(0, Number(usage.inputTokens) || 0);
149
+ const cachedRead = Math.max(0, Number(usage.cachedTokens) || 0);
150
+ const cacheWrite = Math.max(0, Number(usage.cacheWriteTokens) || 0);
151
+ const explicitPrompt = Math.max(0, Number(usage.promptTokens) || 0);
152
+ const normalizedPrompt = providerInputExcludesCache(sessionRef?.provider)
153
+ ? input + cachedRead + cacheWrite
154
+ : input;
155
+ const prompt = Math.max(explicitPrompt, normalizedPrompt);
156
+ const output = Math.max(0, Number(usage.outputTokens) || 0);
157
+ return Math.max(0, Math.round(prompt + output));
158
+ }
159
+
160
+ /**
161
+ * Align an authoritative provider usage snapshot to the message prefix it
162
+ * covers. Later pressure checks add estimates only for messages after this
163
+ * baseline, matching Claude Code's actual-usage-plus-growth accounting.
164
+ */
165
+ export function recordProviderContextBaseline(sessionRef, messages, usage, {
166
+ boundary = 'complete',
167
+ sendTools = sessionRef?.tools,
168
+ } = {}) {
169
+ if (!sessionRef || !Array.isArray(messages)) return false;
170
+ const tokens = providerPressureTokens(sessionRef, usage);
171
+ if (!tokens) return false;
172
+ sessionRef.contextPressureBaselineTokens = tokens;
173
+ sessionRef.contextPressureBaselineOutputTokens = Math.max(0, Math.round(Number(usage?.outputTokens) || 0));
174
+ sessionRef.contextPressureBaselineMessageCount = messages.length;
175
+ sessionRef.contextPressureBaselinePrefixSignature = contextMessagesSignature(messages);
176
+ sessionRef.contextPressureBaselineProvider = sessionRef.provider || null;
177
+ sessionRef.contextPressureBaselineModel = sessionRef.model || null;
178
+ sessionRef.contextPressureBaselineToolSignature = toolSchemaSignature(sendTools);
179
+ // provider_send usage arrives before the response's assistant message is
180
+ // appended. Mark that request boundary so pressure resolution skips the
181
+ // first subsequent assistant representation: its output (including opaque
182
+ // reasoningItems/tool calls) is already authoritative provider usage.
183
+ sessionRef.contextPressureBaselineBoundary = boundary === 'request' ? 'request' : 'complete';
184
+ sessionRef.contextPressureBaselineUpdatedAt = Date.now();
185
+ sessionRef.lastContextTokensStaleAfterCompact = false;
186
+ return true;
187
+ }
188
+
189
+ /** A changed transcript cannot reuse usage measured against its old prefix. */
190
+ export function invalidateProviderContextBaseline(sessionRef) {
191
+ if (!sessionRef) return;
192
+ sessionRef.contextPressureBaselineTokens = null;
193
+ sessionRef.contextPressureBaselineOutputTokens = null;
194
+ sessionRef.contextPressureBaselineMessageCount = null;
195
+ sessionRef.contextPressureBaselineBoundary = null;
196
+ sessionRef.contextPressureBaselinePrefixSignature = null;
197
+ sessionRef.contextPressureBaselineProvider = null;
198
+ sessionRef.contextPressureBaselineModel = null;
199
+ sessionRef.contextPressureBaselineToolSignature = null;
200
+ sessionRef.contextPressureBaselineUpdatedAt = null;
201
+ sessionRef.lastContextTokensStaleAfterCompact = true;
202
+ }
203
+
204
+ function providerBaselinePressureTokens(messages, sessionRef, policy) {
205
+ if (!Array.isArray(messages) || !sessionRef
206
+ || sessionRef.lastContextTokensStaleAfterCompact === true) return null;
207
+ let tokens = positiveTokenInt(sessionRef.contextPressureBaselineTokens);
208
+ const outputTokens = Math.max(0, Number(sessionRef.contextPressureBaselineOutputTokens) || 0);
209
+ let count = Number(sessionRef.contextPressureBaselineMessageCount);
210
+ const baselineAt = Number(sessionRef.contextPressureBaselineUpdatedAt || 0);
211
+ const compactAt = Number(sessionRef.compaction?.lastChangedAt || sessionRef.compaction?.lastCompactAt || 0);
212
+ if (!tokens || !Number.isInteger(count) || count < 0 || count > messages.length
213
+ || (compactAt > 0 && baselineAt > 0 && baselineAt < compactAt)
214
+ || sessionRef.contextPressureBaselineProvider !== (sessionRef.provider || null)
215
+ || sessionRef.contextPressureBaselineModel !== (sessionRef.model || null)
216
+ || sessionRef.contextPressureBaselineToolSignature !== policy?.toolSchemaSignature
217
+ || sessionRef.contextPressureBaselinePrefixSignature !== contextMessagesSignature(messages, count)) return null;
218
+ if (sessionRef.contextPressureBaselineBoundary === 'request') {
219
+ const assistantOffset = messages.slice(count).findIndex(message => message?.role === 'assistant');
220
+ if (assistantOffset >= 0) {
221
+ // The represented assistant is covered by actual output usage.
222
+ count += assistantOffset + 1;
223
+ } else {
224
+ // Empty/thinking-only continuations append no assistant replay.
225
+ // Their output was billed but is absent from the next request, so
226
+ // remove it and estimate every genuinely later message (the nudge).
227
+ tokens = Math.max(0, tokens - outputTokens);
228
+ }
229
+ }
230
+ try {
231
+ const growth = count < messages.length
232
+ ? estimateMessagesTokens(messages.slice(count))
233
+ : 0;
234
+ return Math.max(0, tokens + growth + Math.max(0, Number(policy?.configuredReserveTokens) || 0));
235
+ } catch {
236
+ return null;
237
+ }
238
+ }
239
+
240
+ export function resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef } = {}) {
241
+ return providerBaselinePressureTokens(messages, sessionRef, policy)
242
+ ?? compactPressureTokens(messageTokensEst, policy);
243
+ }
244
+
141
245
  /** Telemetry pressure when a reactive overflow retry forces the next compact. */
142
- export function compactionTelemetryPressureTokens(messageTokensEst, policy, { reactivePending = false } = {}) {
143
- const base = compactPressureTokens(messageTokensEst, policy);
246
+ export function compactionTelemetryPressureTokens(messageTokensEst, policy, {
247
+ reactivePending = false,
248
+ messages,
249
+ sessionRef,
250
+ } = {}) {
251
+ const base = resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef });
144
252
  if (!reactivePending) return base;
145
253
  const floor = positiveTokenInt(policy?.triggerTokens) || positiveTokenInt(policy?.boundaryTokens) || 0;
146
254
  return floor ? Math.max(base, floor) : base;
@@ -152,11 +260,19 @@ export function compactTargetBudget(policy) {
152
260
  const targetEffective = resolveCompactTargetTokens(boundary, policy) || boundary;
153
261
  return Math.max(1, Math.min(boundary, targetEffective + reserve));
154
262
  }
155
- export function shouldCompactForSession(messageTokensEst, policy, { forceReactive = false } = {}) {
263
+ export function shouldCompactForSession(messageTokensEst, policy, {
264
+ forceReactive = false,
265
+ messages,
266
+ sessionRef,
267
+ pressureTokens,
268
+ } = {}) {
156
269
  if (!policy?.auto || !policy.boundaryTokens) return false;
157
270
  if (forceReactive) return true;
158
271
  if (messageTokensEst === null) return true;
159
- return compactPressureTokens(messageTokensEst, policy) >= (policy.triggerTokens || policy.boundaryTokens);
272
+ const pressure = Number.isFinite(Number(pressureTokens))
273
+ ? Number(pressureTokens)
274
+ : resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef });
275
+ return pressure >= (policy.triggerTokens || policy.boundaryTokens);
160
276
  }
161
277
  export function countPrunedToolOutputs(before, after) {
162
278
  if (!Array.isArray(before) || !Array.isArray(after)) return 0;
@@ -228,7 +344,7 @@ export function rememberCompactTelemetry(sessionRef, policy, meta = {}) {
228
344
  const changedAt = Date.now();
229
345
  sessionRef.compaction.lastChangedAt = changedAt;
230
346
  sessionRef.compaction.lastCompactAt = changedAt;
231
- sessionRef.lastContextTokensStaleAfterCompact = true;
347
+ invalidateProviderContextBaseline(sessionRef);
232
348
  }
233
349
  sessionRef.contextWindow = policy.contextWindow || sessionRef.contextWindow;
234
350
  sessionRef.rawContextWindow = policy.rawContextWindow || sessionRef.rawContextWindow;
@@ -146,6 +146,14 @@ export function createSteeringLadder(ctx) {
146
146
  // read-permission sessions legitimately never edit, so they get the
147
147
  // report-oriented level-2 text.
148
148
  const readOnlyRole = ctx.readOnlyRole === true;
149
+ // Edit-push steering is EXPLORER-ONLY: leads legitimately read broadly
150
+ // before delegating, and reviewer/debugger-style roles read continuously
151
+ // by design. Pushing "start editing / apply the edit" at those roles
152
+ // suppresses delegation (observed on TB2.1: forced-delegation workflow
153
+ // leads went solo right after these nudges). Non-explorer roles keep the
154
+ // batching guidance but never receive an edit directive, and level-2
155
+ // edit-push is skipped for them entirely.
156
+ const editPushEligible = sessionAgent === 'explorer';
149
157
 
150
158
  // Step 1: escalation ladder. _level1FireCount is CUMULATIVE (never reset)
151
159
  // so repeated batching reminders accumulate across the whole session.
@@ -194,6 +202,7 @@ export function createSteeringLadder(ctx) {
194
202
  // level-1 streak and the independent all-read-only streak). Sets the latch
195
203
  // so it fires at most once per 5 turns regardless of which path triggered.
196
204
  const _emitLevel2Steer = () => {
205
+ if (!editPushEligible && !readOnlyRole) return;
197
206
  const iterations = getIterations();
198
207
  _level2LatchAtIteration = iterations;
199
208
  _level2FireCount += 1;
@@ -252,7 +261,9 @@ export function createSteeringLadder(ctx) {
252
261
  if (_canEscalate) {
253
262
  _emitLevel2Steer();
254
263
  } else {
255
- pushSystemReminder('Last 2 turns each ran a single read-only tool. Batch independent lookups (read/grep/glob/code_graph) into ONE turn, or start editing if you have enough context.');
264
+ pushSystemReminder(editPushEligible
265
+ ? 'Last 2 turns each ran a single read-only tool. Batch independent lookups (read/grep/glob/code_graph) into ONE turn, or start editing if you have enough context.'
266
+ : 'Last 2 turns each ran a single read-only tool. Batch independent lookups (read/grep/glob/code_graph) into ONE turn.');
256
267
  }
257
268
  _hintFiredThisTurn = true;
258
269
  }
@@ -3,14 +3,27 @@
3
3
  // the classification ladder is verbatim from the tail of agentLoop.
4
4
  import { HIDDEN_AGENT_NAMES } from './hidden-agents.mjs';
5
5
 
6
- // Stop reasons that signal the turn was cut short mid-synthesis (token cap,
7
- // provider pause). Empty content + one of these reasons means the worker
8
- // was not done. Covers Anthropic (pause_turn, max_tokens), OpenAI (length),
9
- // Gemini (MAX_TOKENS, OTHER), and case variants.
6
+ // Stop reasons that signal the turn was cut short mid-synthesis. This broad set
7
+ // retains the pre-existing EMPTY-turn nudge semantics for provider pauses and
8
+ // unknown Gemini stops; only OUTPUT_LIMIT_STOP_REASONS below is eligible for
9
+ // non-empty continuation recovery.
10
10
  export const INCOMPLETE_STOP_REASONS = new Set([
11
11
  'pause_turn', 'max_tokens', 'length', 'MAX_TOKENS', 'OTHER',
12
12
  ]);
13
13
 
14
+ // True provider output ceilings. pause_turn is a provider-controlled pause and
15
+ // Gemini OTHER is intentionally opaque; neither is safe to treat as a token-cap
16
+ // continuation. Compare case-insensitively so provider spelling variants do not
17
+ // create a second policy.
18
+ export const OUTPUT_LIMIT_STOP_REASONS = new Set([
19
+ 'length', 'max_tokens', 'max_output_tokens',
20
+ ]);
21
+
22
+ export function isOutputLimitStopReason(reason) {
23
+ return typeof reason === 'string'
24
+ && OUTPUT_LIMIT_STOP_REASONS.has(reason.trim().toLowerCase());
25
+ }
26
+
14
27
  // Classify WHY the loop ended so agent-tool can promote an empty/abnormal
15
28
  // finish to an explicit Lead-facing error instead of a silent empty
16
29
  // "completed". Determine "has content" exactly the way the no-tool-call
@@ -23,15 +36,20 @@ export function classifyTerminationReason(response, {
23
36
  || (typeof response?.reasoningContent === 'string' && response.reasoningContent.trim().length > 0);
24
37
  const _finalStopReason = response?.stopReason ?? response?.stop_reason ?? null;
25
38
  const _finalIncompleteStop = _finalStopReason && INCOMPLETE_STOP_REASONS.has(_finalStopReason);
39
+ const _finalOutputLimitStop = isOutputLimitStopReason(_finalStopReason);
26
40
  const _finalIsHidden = HIDDEN_AGENT_NAMES.has(sessionAgent);
27
41
  if (terminatedByCap) {
28
42
  // Real problem regardless of hidden/public: the loop never terminated
29
43
  // on its own contract.
30
44
  return 'iteration_cap';
31
45
  }
32
- if (!_finalHasContent && _finalIncompleteStop) {
33
- // Cut short mid-synthesis (token cap / provider pause). Real problem
34
- // for hidden agents too.
46
+ if (_finalStopReason === 'refusal') {
47
+ return 'refusal';
48
+ }
49
+ if (_finalOutputLimitStop || (!_finalHasContent && _finalIncompleteStop)) {
50
+ // Exhausted token-cap recovery is abnormal even with preserved partial
51
+ // text. pause_turn/OTHER retain their prior non-empty completion
52
+ // semantics, while their empty forms remain abnormal.
35
53
  return 'truncated';
36
54
  }
37
55
  if (!_finalHasContent && !_finalIsHidden) {
@@ -223,21 +223,30 @@ export async function executeTool(name, args, cwd, callerSessionId, sessionRef,
223
223
  })();
224
224
  if (typeof afterToolHook === 'function') {
225
225
  try {
226
+ // Tool outcome metadata is runtime-internal. Hooks receive the same
227
+ // model-visible result value they received before transient
228
+ // envelopes existed, never the envelope object itself.
229
+ const {
230
+ result: __res,
231
+ newMessages: __nm,
232
+ explicitSuccess: __explicitSuccess,
233
+ } = normalizeToolEnvelope(__result);
226
234
  const hookResult = await afterToolHook({
227
235
  name,
228
236
  args,
229
237
  cwd,
230
238
  sessionId: callerSessionId,
231
239
  toolCallId: executeOpts.toolCallId || null,
232
- result: __result,
240
+ result: __res,
233
241
  });
234
242
  // Envelope-aware hook override: a PostToolUse hook may override the
235
243
  // model-VISIBLE tool output (the envelope's `result` / stub), but it
236
244
  // must NEVER drop the `newMessages` channel. Split first, apply the
237
245
  // override to `result` only, then re-wrap so newMessages survive.
238
- const { result: __res, newMessages: __nm } = normalizeToolEnvelope(__result);
239
246
  const __overridden = resolveToolResultAfterHook(__res, hookResult);
240
- if (__nm.length) return makeToolEnvelope(__overridden, __nm);
247
+ if (__nm.length || __explicitSuccess) {
248
+ return makeToolEnvelope(__overridden, __nm, { explicitSuccess: __explicitSuccess });
249
+ }
241
250
  return __overridden;
242
251
  } catch {
243
252
  // PostToolUse hooks are best-effort; never let one break the tool result.
@@ -44,6 +44,7 @@ import { _tryBridgeExplicitPrefetch } from './prefetch-bridge.mjs';
44
44
  import { sanitizeSessionMessagesForModel, persistCompactedOutgoingAfterAskFailure } from './message-sanitize.mjs';
45
45
  import { _getAgentLoop } from './runtime-loaders.mjs';
46
46
  import { getAgentRuntimeSync } from './agent-runtime-singleton.mjs';
47
+ import { recordProviderContextBaseline } from '../loop/compact-policy.mjs';
47
48
 
48
49
  /**
49
50
  * Wrap an async call so that if the session's controller aborts mid-flight,
@@ -327,6 +328,21 @@ export async function askSession(sessionId, prompt, context, onToolCall, cwdOver
327
328
  onAssistantText: typeof askOpts?.onAssistantText === 'function' ? askOpts.onAssistantText : undefined,
328
329
  onUsageDelta: (d) => {
329
330
  persistIterationMetrics(d).catch(() => {});
331
+ // provider_send usage arrives before agentLoop appends
332
+ // the assistant response. Preserve the full actual
333
+ // input/cache/output count and mark this request
334
+ // boundary; compact pressure will skip that first
335
+ // assistant representation and estimate only later
336
+ // tool results/steering.
337
+ if (d?.source === 'provider_send') {
338
+ recordProviderContextBaseline(session, outgoing, {
339
+ inputTokens: d.deltaInput,
340
+ outputTokens: d.deltaOutput,
341
+ promptTokens: d.deltaPrompt,
342
+ cachedTokens: d.deltaCachedRead,
343
+ cacheWriteTokens: d.deltaCacheWrite,
344
+ }, { boundary: 'request', sendTools: d.sendTools });
345
+ }
330
346
  try { askOpts?.onUsageDelta?.(d); } catch {}
331
347
  },
332
348
  onToolResult: typeof askOpts?.onToolResult === 'function' ? askOpts.onToolResult : undefined,
@@ -401,12 +417,20 @@ export async function askSession(sessionId, prompt, context, onToolCall, cwdOver
401
417
  // contextStatus() reverts to the authoritative committed transcript.
402
418
  session.liveTurnMessages = null;
403
419
  if (result.content || result.reasoningContent) {
420
+ // Max-output recovery returns the complete concatenated text to
421
+ // callers/TUI, while outgoing already contains prior partial
422
+ // assistant turns and their continuation prompts. Persist only
423
+ // the terminal segment here so model history contains every byte
424
+ // exactly once.
425
+ const persistedAssistantContent = typeof result.historyContent === 'string'
426
+ ? result.historyContent
427
+ : (result.content || '');
404
428
  session.messages.push({
405
429
  role: 'assistant',
406
430
  // Keep content as-is in memory (model-visible). Image bytes,
407
431
  // if any, are swapped for a placeholder only at disk write
408
432
  // time inside the session store (store.mjs _sessionForDisk).
409
- content: result.content || '',
433
+ content: persistedAssistantContent,
410
434
  ...(typeof result.reasoningContent === 'string' && result.reasoningContent
411
435
  ? { reasoningContent: result.reasoningContent }
412
436
  : {}),
@@ -458,6 +482,9 @@ export async function askSession(sessionId, prompt, context, onToolCall, cwdOver
458
482
  applyAskTerminalUsageTotals(session, result, {
459
483
  skipTotalsIfIncremental: runtime?.usageMetricsTurnIncremental === true,
460
484
  });
485
+ recordProviderContextBaseline(session, session.messages, result.lastTurnUsage || result.usage, {
486
+ sendTools: result.lastSendTools,
487
+ });
461
488
  // Agent Runtime cache stats — record hit/miss after every successful
462
489
  // ask so the registry reflects all agent traffic, not just
463
490
  // maintenance cycles. Guarded against any agent-runtime error so
@@ -29,6 +29,7 @@ import { uncachedInputTokensForProvider } from './usage-metrics.mjs';
29
29
  import { pruneOffloadSession } from '../tool-result-offload.mjs';
30
30
  import { _getPendingMessagesForSession } from './pending-messages.mjs';
31
31
  import { isSessionCompactionBlocked } from './runtime-liveness.mjs';
32
+ import { invalidateProviderContextBaseline } from '../loop/compact-policy.mjs';
32
33
 
33
34
  // 'compacting' is a transient in-flight stage written just before semantic /
34
35
  // recall-fasttrack compaction runs. If the process crashes or only partially
@@ -539,7 +540,7 @@ export async function runSessionCompaction(session, opts = {}) {
539
540
  } : null,
540
541
  compactCount: (session.compaction?.compactCount || 0) + (changed ? 1 : 0),
541
542
  };
542
- if (changed && mode === 'auto') session.lastContextTokensStaleAfterCompact = true;
543
+ if (changed) invalidateProviderContextBaseline(session);
543
544
  return {
544
545
  changed,
545
546
  reason: unchangedReason,
@@ -11,7 +11,7 @@
11
11
  // avoid a circular import back into manager.mjs.
12
12
  //
13
13
  // Entry shape: {
14
- // stage, lastStreamDeltaAt, lastToolCall, lastError, updatedAt,
14
+ // stage, lastStreamDeltaAt, lastTransportAt, lastToolCall, lastError, updatedAt,
15
15
  // controller?: AbortController, // set while an ask is in flight
16
16
  // generation?: number, // snapshot taken at ask start
17
17
  // closed?: boolean, // flipped by closeSession()
@@ -46,7 +46,7 @@ export function configureRuntimeLiveness(deps = {}) {
46
46
  export function _touchRuntime(id) {
47
47
  let entry = _runtimeState.get(id);
48
48
  if (!entry) {
49
- entry = { stage: 'idle', lastStreamDeltaAt: null, lastToolCall: null, lastError: null, updatedAt: Date.now() };
49
+ entry = { stage: 'idle', lastStreamDeltaAt: null, lastTransportAt: null, lastToolCall: null, lastError: null, updatedAt: Date.now() };
50
50
  _runtimeState.set(id, entry);
51
51
  }
52
52
  return entry;
@@ -106,6 +106,8 @@ export function markSessionAskStart(id) {
106
106
  if (sessionForTurn) bumpUsageMetricsTurnId(sessionForTurn);
107
107
  entry.stage = 'connecting';
108
108
  entry.lastStreamDeltaAt = null;
109
+ entry.lastTransportAt = null;
110
+ entry.transportTrackingEnabled = false;
109
111
  entry.lastToolCall = null;
110
112
  entry.toolStartedAt = null;
111
113
  entry.toolSelfDeadlineMs = null;
@@ -134,6 +136,27 @@ export function markSessionAskStart(id) {
134
136
  // markSessionStreamDelta keeps refreshing once chunks arrive.
135
137
  publishHeartbeat(id, now);
136
138
  }
139
+ export function enableSessionTransportTracking(id) {
140
+ if (!id) return;
141
+ const entry = _runtimeState.get(id);
142
+ if (!entry || entry.closed || entry.controller?.signal?.aborted) return;
143
+ entry.transportTrackingEnabled = true;
144
+ }
145
+ export function disableSessionTransportTracking(id) {
146
+ if (!id) return;
147
+ const entry = _runtimeState.get(id);
148
+ if (!entry) return;
149
+ entry.transportTrackingEnabled = false;
150
+ }
151
+ export function markSessionTransportActivity(id) {
152
+ if (!id) return;
153
+ const entry = _runtimeState.get(id);
154
+ if (!entry || entry.closed || entry.controller?.signal?.aborted) return;
155
+ const now = Date.now();
156
+ entry.lastTransportAt = now;
157
+ entry.updatedAt = now;
158
+ publishHeartbeat(id, now);
159
+ }
137
160
  export async function markSessionStreamDelta(id) {
138
161
  if (!id) return;
139
162
  // Non-creating lookup: a live ask ALWAYS has a runtime entry (markSessionAskStart
@@ -285,16 +308,20 @@ export function getSessionProgressSnapshot(sessionId) {
285
308
  entry.toolStartedAt || 0,
286
309
  );
287
310
  const stage = entry.stage || 'idle';
311
+ const waitingStage = stage === 'connecting'
312
+ || stage === 'requesting'
313
+ || (stage === 'streaming' && entry.transportTrackingEnabled === true);
288
314
  const waitingForFirstActivity = Boolean(
289
315
  modelRequestStartedAt
290
- && (stage === 'connecting' || stage === 'requesting')
291
- && firstActivityAt <= modelRequestStartedAt
316
+ && waitingStage
317
+ && (!firstActivityAt || firstActivityAt <= modelRequestStartedAt)
292
318
  );
293
319
  return {
294
320
  stage,
295
321
  askStartedAt,
296
322
  modelRequestStartedAt,
297
323
  firstActivityAt,
324
+ lastTransportAt: entry.lastTransportAt || 0,
298
325
  lastStreamDeltaAt: entry.lastStreamDeltaAt || 0,
299
326
  toolStartedAt: entry.toolStartedAt || 0,
300
327
  currentTool: entry.lastToolCall || null,
@@ -61,8 +61,17 @@ export async function runPreSendCompactPass(state) {
61
61
  };
62
62
  const messageTokensEst = estimateMessagesTokensSafe(messages);
63
63
  const reactivePending = reactiveOverflowRetryPending === true;
64
- const shouldCompact = shouldCompactForSession(messageTokensEst, compactPolicy, { forceReactive: reactivePending });
65
- const pressureTokens = compactionTelemetryPressureTokens(messageTokensEst, compactPolicy, { reactivePending });
64
+ const pressureTokens = compactionTelemetryPressureTokens(messageTokensEst, compactPolicy, {
65
+ reactivePending,
66
+ messages,
67
+ sessionRef,
68
+ });
69
+ const shouldCompact = shouldCompactForSession(messageTokensEst, compactPolicy, {
70
+ forceReactive: reactivePending,
71
+ messages,
72
+ sessionRef,
73
+ pressureTokens,
74
+ });
66
75
  // A pending reactive-overflow retry makes THIS compact pass the
67
76
  // recovery from a provider overflow refusal, not the proactive
68
77
  // pressure trigger. Tag the emitted events so telemetry can tell
@@ -40,6 +40,8 @@
40
40
  * "(no lines in range" — read offset out-of-range (builtin.mjs:739, 3571)
41
41
  *
42
42
  * @param {unknown} result
43
+ * @param {boolean} [explicitSuccess=false] true only when the tool handler
44
+ * explicitly returned `isError: false`
43
45
  * @returns {'normal' | 'error' | 'zero-match'}
44
46
  */
45
47
  const ZERO_MATCH_PREFIXES = [
@@ -56,7 +58,8 @@ const ZERO_MATCH_PREFIXES = [
56
58
  '(no lines in range',
57
59
  ];
58
60
 
59
- export function classifyResultKind(result) {
61
+ export function classifyResultKind(result, explicitSuccess = false) {
62
+ if (explicitSuccess === true) return 'normal';
60
63
  if (typeof result !== 'string') return 'normal';
61
64
  const trimmed = result.trimStart();
62
65
  if (/^error(?:\s+\[code\b|\s*:)/i.test(trimmed) || /^\[error/i.test(trimmed) || /^\[exit code:/i.test(trimmed)) return 'error';
@@ -9,6 +9,24 @@ import { isContextOverflowError } from '../providers/retry-classifier.mjs';
9
9
  import { resolveWorkerCompactPolicy } from './loop/compact-policy.mjs';
10
10
  import { agentContextOverflowError } from './loop/context-overflow.mjs';
11
11
  import { estimateMessagesTokensSafe } from './loop/compact-debug.mjs';
12
+ import { isOutputLimitStopReason } from './loop/termination.mjs';
13
+
14
+ function normalizedIncompleteUsage(raw) {
15
+ if (!raw || typeof raw !== 'object') return undefined;
16
+ const inputTokens = Number(raw.promptTokenCount ?? raw.prompt_token_count ?? raw.input_tokens ?? raw.prompt_tokens ?? 0) || 0;
17
+ const candidateTokens = Number(raw.candidatesTokenCount ?? raw.candidates_token_count ?? 0) || 0;
18
+ const thoughtTokens = Number(raw.thoughtsTokenCount ?? raw.thoughts_token_count ?? 0) || 0;
19
+ const outputFallback = Number(raw.output_tokens ?? raw.completion_tokens ?? 0) || 0;
20
+ const cachedTokens = Number(raw.cachedContentTokenCount ?? raw.cached_content_token_count ?? raw.cached_tokens ?? 0) || 0;
21
+ return {
22
+ inputTokens,
23
+ outputTokens: candidateTokens + thoughtTokens || outputFallback,
24
+ cachedTokens,
25
+ cacheWriteTokens: 0,
26
+ promptTokens: inputTokens,
27
+ raw,
28
+ };
29
+ }
12
30
 
13
31
  export async function sendWithRecovery(ctx) {
14
32
  const {
@@ -19,6 +37,33 @@ export async function sendWithRecovery(ctx) {
19
37
  try {
20
38
  response = await provider.send(messages, model, sendTools.length ? sendTools : undefined, opts);
21
39
  } catch (sendErr) {
40
+ // Gemini REST/SDK reports MAX_TOKENS by throwing a typed
41
+ // ProviderIncompleteError after preserving the streamed candidate.
42
+ // Normalize only that exact, safe no-tool output-limit shape into a
43
+ // regular truncated response; all moderation/OTHER/tool-bearing and
44
+ // unrelated errors continue through their existing error paths.
45
+ if (
46
+ sendErr?.providerIncomplete === true
47
+ && sendErr.code === 'PROVIDER_INCOMPLETE'
48
+ && isOutputLimitStopReason(sendErr.finishReason)
49
+ && typeof sendErr.partialContent === 'string'
50
+ && sendErr.partialContent.trim().length > 0
51
+ && sendErr.pendingToolUse !== true
52
+ && sendErr.emittedToolCall !== true
53
+ && !(Array.isArray(sendErr.partialToolCalls) && sendErr.partialToolCalls.length > 0)
54
+ ) {
55
+ response = {
56
+ content: sendErr.partialContent,
57
+ model: sendErr.model || model,
58
+ toolCalls: undefined,
59
+ usage: normalizedIncompleteUsage(sendErr.rawUsage),
60
+ stopReason: sendErr.finishReason,
61
+ truncated: true,
62
+ providerState: opts.providerState,
63
+ providerIncompleteRecovery: true,
64
+ };
65
+ return { action: 'proceed', response };
66
+ } else
22
67
  // Partial-final recovery (owner-notify fix): the recurring "worker
23
68
  // finished but the task hung / no result delivered" case is a FINAL,
24
69
  // no-tool summary stream that wedges (ping-only) AFTER all real tool
@@ -32,6 +32,11 @@ import { crossTurnSignature, crossTurnDedupStub, isEditProgressTool } from './lo
32
32
  import { getToolKind, isEagerDispatchable, parseNativeToolSearchPayload } from './loop/tool-helpers.mjs';
33
33
  import { restoreToolCallBodyForId, dropCompactedBodyArgsForId } from './loop/stored-tool-args.mjs';
34
34
 
35
+ function classifyToolReturn(value) {
36
+ const normalized = normalizeToolEnvelope(value);
37
+ return classifyResultKind(normalized.result, normalized.explicitSuccess);
38
+ }
39
+
35
40
  export async function processToolBatch(ctx) {
36
41
  const {
37
42
  calls, messages, tools, cwd, sessionId, sessionRef, signal, opts,
@@ -258,7 +263,7 @@ export async function processToolBatch(ctx) {
258
263
  if (!settled.ok) throw settled.error;
259
264
  result = settled.value;
260
265
  toolEndedAt = eager.endedAt ?? Date.now();
261
- const _eagerKind = classifyResultKind(result);
266
+ const _eagerKind = classifyToolReturn(result);
262
267
  if (_eagerKind === 'error') {
263
268
  _resultKind = 'error';
264
269
  _executeOk = false;
@@ -286,7 +291,7 @@ export async function processToolBatch(ctx) {
286
291
  // Boundary: tool-return string convention → structural kind.
287
292
  // The only prefix check in this codebase; downstream layers
288
293
  // operate on _resultKind.
289
- if (classifyResultKind(result) === 'error') {
294
+ if (classifyToolReturn(result) === 'error') {
290
295
  _resultKind = 'error';
291
296
  _executeOk = false;
292
297
  } else {
@@ -5,7 +5,8 @@
5
5
  * - legacy: a string (or existing structured media object) — unchanged, OR
6
6
  * - an envelope object:
7
7
  * { __toolEnvelope: true, result: <string|structured>,
8
- * newMessages: [{ role:'user', content:'...' }, ...] }
8
+ * newMessages: [{ role:'user', content:'...' }, ...],
9
+ * explicitSuccess?: true }
9
10
  *
10
11
  * The `__toolEnvelope` marker is deliberately namespaced so it can NEVER be
11
12
  * confused with the existing structured media content objects that
@@ -37,17 +38,18 @@ function isValidNewMessage(m) {
37
38
  * sees as the tool_result; `newMessages` are appended (as their own
38
39
  * messages, e.g. role:'user') AFTER the batch's tool results.
39
40
  */
40
- export function makeToolEnvelope(result, newMessages = []) {
41
+ export function makeToolEnvelope(result, newMessages = [], options = {}) {
41
42
  return {
42
43
  [TOOL_ENVELOPE_MARKER]: true,
43
44
  result,
44
45
  newMessages: Array.isArray(newMessages) ? newMessages.filter(isValidNewMessage) : [],
46
+ ...(options.explicitSuccess === true ? { explicitSuccess: true } : {}),
45
47
  };
46
48
  }
47
49
 
48
50
  /**
49
- * Split a tool return value into `{ result, newMessages }`.
50
- * - legacy string/object → { result: value, newMessages: [] }
51
+ * Split a tool return value into `{ result, newMessages, explicitSuccess }`.
52
+ * - legacy string/object → { result: value, newMessages: [], explicitSuccess: false }
51
53
  * - envelope → { result, newMessages } (newMessages validated)
52
54
  */
53
55
  export function normalizeToolEnvelope(value) {
@@ -55,7 +57,7 @@ export function normalizeToolEnvelope(value) {
55
57
  const newMessages = Array.isArray(value.newMessages)
56
58
  ? value.newMessages.filter(isValidNewMessage)
57
59
  : [];
58
- return { result: value.result, newMessages };
60
+ return { result: value.result, newMessages, explicitSuccess: value.explicitSuccess === true };
59
61
  }
60
- return { result: value, newMessages: [] };
62
+ return { result: value, newMessages: [], explicitSuccess: false };
61
63
  }