mixdog 0.9.40 → 0.9.41
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.
- package/package.json +2 -1
- package/scripts/anthropic-oauth-refresh-race-test.mjs +338 -0
- package/scripts/compact-pressure-test.mjs +128 -0
- package/scripts/compact-trigger-migration-smoke.mjs +8 -8
- package/scripts/internal-comms-smoke.mjs +66 -25
- package/scripts/max-output-recovery-persist-test.mjs +81 -0
- package/scripts/max-output-recovery-test.mjs +222 -0
- package/scripts/provider-toolcall-test.mjs +32 -0
- package/src/agents/reviewer/AGENT.md +4 -0
- package/src/rules/lead/lead-brief.md +11 -3
- package/src/rules/lead/lead-tool.md +0 -2
- package/src/rules/shared/01-tool.md +4 -0
- package/src/runtime/agent/orchestrator/agent-runtime/agent-progress-watchdog.mjs +20 -2
- package/src/runtime/agent/orchestrator/context/collect.mjs +7 -3
- package/src/runtime/agent/orchestrator/providers/anthropic-oauth-credentials.mjs +144 -7
- package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +15 -2
- package/src/runtime/agent/orchestrator/providers/anthropic.mjs +2 -2
- package/src/runtime/agent/orchestrator/providers/openai-ws-stream.mjs +57 -25
- package/src/runtime/agent/orchestrator/session/agent-loop.mjs +54 -12
- package/src/runtime/agent/orchestrator/session/context-utils.mjs +4 -3
- package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +106 -8
- package/src/runtime/agent/orchestrator/session/loop/steering-ladder.mjs +12 -1
- package/src/runtime/agent/orchestrator/session/loop/termination.mjs +22 -7
- package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +26 -1
- package/src/runtime/agent/orchestrator/session/manager/compaction-runner.mjs +2 -1
- package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +31 -4
- package/src/runtime/agent/orchestrator/session/pre-send-compact.mjs +11 -2
- package/src/runtime/agent/orchestrator/session/send-with-recovery.mjs +45 -0
- package/src/runtime/agent/orchestrator/tools/builtin/bash-tool.mjs +22 -21
- package/src/runtime/agent/orchestrator/tools/builtin/builtin-tools.mjs +1 -1
- package/src/runtime/agent/orchestrator/tools/shell-command.mjs +9 -10
- package/src/workflows/bench/WORKFLOW.md +51 -27
- package/src/workflows/default/WORKFLOW.md +29 -24
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// runRecallFastTrackCompact stays in the loop (it drives the recall pipeline
|
|
4
4
|
// against live session state).
|
|
5
5
|
import {
|
|
6
|
+
estimateMessagesTokens,
|
|
6
7
|
estimateRequestReserveTokens,
|
|
7
8
|
resolveSessionCompactPolicy,
|
|
8
9
|
} from '../context-utils.mjs';
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
} from '../compact.mjs';
|
|
19
20
|
import { positiveTokenInt, envFlag, envTokenInt } from './env.mjs';
|
|
20
21
|
import { isAgentOwner } from '../../agent-owner.mjs';
|
|
22
|
+
import { providerInputExcludesCache } from '../../providers/registry.mjs';
|
|
21
23
|
|
|
22
24
|
// Unified context-share rule (compact/constants.mjs CONTEXT_SHARE_RATIO): the
|
|
23
25
|
// post-compaction target is 10% of the boundary/context window — the same 10%
|
|
@@ -91,8 +93,8 @@ export function resolveWorkerCompactPolicy(sessionRef, tools) {
|
|
|
91
93
|
if (!boundaryTokens) return null;
|
|
92
94
|
const compactBoundaryTokens = Math.max(1, Math.floor(boundaryTokens * COMPACT_SAFETY_PERCENT));
|
|
93
95
|
// Shared session-compaction policy (context-utils): agent semantic keeps the
|
|
94
|
-
// default early-trigger buffer (90%); main/user
|
|
95
|
-
//
|
|
96
|
+
// default early-trigger buffer (90%); main/user keep 5% headroom (95%);
|
|
97
|
+
// a truly-explicit sub-boundary limit wins. explicitAutoCompactTokenLimit
|
|
96
98
|
// is the sanitized (null when legacy full-window) value so telemetry never
|
|
97
99
|
// re-persists a boundary-collapsing limit.
|
|
98
100
|
const policy = resolveSessionCompactPolicy(sessionRef, compactBoundaryTokens);
|
|
@@ -132,15 +134,103 @@ export function resolveWorkerCompactPolicy(sessionRef, tools) {
|
|
|
132
134
|
configuredReserveTokens: configuredReserve,
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
|
-
/** Transcript + request reserve
|
|
137
|
+
/** Transcript + request reserve fallback used until an aligned provider baseline exists. */
|
|
136
138
|
function compactPressureTokens(messageTokensEst, policy) {
|
|
137
139
|
if (messageTokensEst === null) return 0;
|
|
138
140
|
return Math.max(0, messageTokensEst + (policy?.reserveTokens || 0));
|
|
139
141
|
}
|
|
140
142
|
|
|
143
|
+
function providerPressureTokens(sessionRef, usage) {
|
|
144
|
+
if (!usage || typeof usage !== 'object') return 0;
|
|
145
|
+
const input = Math.max(0, Number(usage.inputTokens) || 0);
|
|
146
|
+
const cachedRead = Math.max(0, Number(usage.cachedTokens) || 0);
|
|
147
|
+
const cacheWrite = Math.max(0, Number(usage.cacheWriteTokens) || 0);
|
|
148
|
+
const explicitPrompt = Math.max(0, Number(usage.promptTokens) || 0);
|
|
149
|
+
const normalizedPrompt = providerInputExcludesCache(sessionRef?.provider)
|
|
150
|
+
? input + cachedRead + cacheWrite
|
|
151
|
+
: input;
|
|
152
|
+
const prompt = Math.max(explicitPrompt, normalizedPrompt);
|
|
153
|
+
const output = Math.max(0, Number(usage.outputTokens) || 0);
|
|
154
|
+
return Math.max(0, Math.round(prompt + output));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Align an authoritative provider usage snapshot to the message prefix it
|
|
159
|
+
* covers. Later pressure checks add estimates only for messages after this
|
|
160
|
+
* baseline, matching Claude Code's actual-usage-plus-growth accounting.
|
|
161
|
+
*/
|
|
162
|
+
export function recordProviderContextBaseline(sessionRef, messages, usage, { boundary = 'complete' } = {}) {
|
|
163
|
+
if (!sessionRef || !Array.isArray(messages)) return false;
|
|
164
|
+
const tokens = providerPressureTokens(sessionRef, usage);
|
|
165
|
+
if (!tokens) return false;
|
|
166
|
+
sessionRef.contextPressureBaselineTokens = tokens;
|
|
167
|
+
sessionRef.contextPressureBaselineOutputTokens = Math.max(0, Math.round(Number(usage?.outputTokens) || 0));
|
|
168
|
+
sessionRef.contextPressureBaselineMessageCount = messages.length;
|
|
169
|
+
// provider_send usage arrives before the response's assistant message is
|
|
170
|
+
// appended. Mark that request boundary so pressure resolution skips the
|
|
171
|
+
// first subsequent assistant representation: its output (including opaque
|
|
172
|
+
// reasoningItems/tool calls) is already authoritative provider usage.
|
|
173
|
+
sessionRef.contextPressureBaselineBoundary = boundary === 'request' ? 'request' : 'complete';
|
|
174
|
+
sessionRef.contextPressureBaselineUpdatedAt = Date.now();
|
|
175
|
+
sessionRef.lastContextTokensStaleAfterCompact = false;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** A changed transcript cannot reuse usage measured against its old prefix. */
|
|
180
|
+
export function invalidateProviderContextBaseline(sessionRef) {
|
|
181
|
+
if (!sessionRef) return;
|
|
182
|
+
sessionRef.contextPressureBaselineTokens = null;
|
|
183
|
+
sessionRef.contextPressureBaselineOutputTokens = null;
|
|
184
|
+
sessionRef.contextPressureBaselineMessageCount = null;
|
|
185
|
+
sessionRef.contextPressureBaselineBoundary = null;
|
|
186
|
+
sessionRef.contextPressureBaselineUpdatedAt = null;
|
|
187
|
+
sessionRef.lastContextTokensStaleAfterCompact = true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function providerBaselinePressureTokens(messages, sessionRef) {
|
|
191
|
+
if (!Array.isArray(messages) || !sessionRef
|
|
192
|
+
|| sessionRef.lastContextTokensStaleAfterCompact === true) return null;
|
|
193
|
+
let tokens = positiveTokenInt(sessionRef.contextPressureBaselineTokens);
|
|
194
|
+
const outputTokens = Math.max(0, Number(sessionRef.contextPressureBaselineOutputTokens) || 0);
|
|
195
|
+
let count = Number(sessionRef.contextPressureBaselineMessageCount);
|
|
196
|
+
const baselineAt = Number(sessionRef.contextPressureBaselineUpdatedAt || 0);
|
|
197
|
+
const compactAt = Number(sessionRef.compaction?.lastChangedAt || sessionRef.compaction?.lastCompactAt || 0);
|
|
198
|
+
if (!tokens || !Number.isInteger(count) || count < 0 || count > messages.length
|
|
199
|
+
|| (compactAt > 0 && baselineAt > 0 && baselineAt < compactAt)) return null;
|
|
200
|
+
if (sessionRef.contextPressureBaselineBoundary === 'request') {
|
|
201
|
+
const assistantOffset = messages.slice(count).findIndex(message => message?.role === 'assistant');
|
|
202
|
+
if (assistantOffset >= 0) {
|
|
203
|
+
// The represented assistant is covered by actual output usage.
|
|
204
|
+
count += assistantOffset + 1;
|
|
205
|
+
} else {
|
|
206
|
+
// Empty/thinking-only continuations append no assistant replay.
|
|
207
|
+
// Their output was billed but is absent from the next request, so
|
|
208
|
+
// remove it and estimate every genuinely later message (the nudge).
|
|
209
|
+
tokens = Math.max(0, tokens - outputTokens);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const growth = count < messages.length
|
|
214
|
+
? estimateMessagesTokens(messages.slice(count))
|
|
215
|
+
: 0;
|
|
216
|
+
return Math.max(0, tokens + growth);
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef } = {}) {
|
|
223
|
+
return providerBaselinePressureTokens(messages, sessionRef)
|
|
224
|
+
?? compactPressureTokens(messageTokensEst, policy);
|
|
225
|
+
}
|
|
226
|
+
|
|
141
227
|
/** Telemetry pressure when a reactive overflow retry forces the next compact. */
|
|
142
|
-
export function compactionTelemetryPressureTokens(messageTokensEst, policy, {
|
|
143
|
-
|
|
228
|
+
export function compactionTelemetryPressureTokens(messageTokensEst, policy, {
|
|
229
|
+
reactivePending = false,
|
|
230
|
+
messages,
|
|
231
|
+
sessionRef,
|
|
232
|
+
} = {}) {
|
|
233
|
+
const base = resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef });
|
|
144
234
|
if (!reactivePending) return base;
|
|
145
235
|
const floor = positiveTokenInt(policy?.triggerTokens) || positiveTokenInt(policy?.boundaryTokens) || 0;
|
|
146
236
|
return floor ? Math.max(base, floor) : base;
|
|
@@ -152,11 +242,19 @@ export function compactTargetBudget(policy) {
|
|
|
152
242
|
const targetEffective = resolveCompactTargetTokens(boundary, policy) || boundary;
|
|
153
243
|
return Math.max(1, Math.min(boundary, targetEffective + reserve));
|
|
154
244
|
}
|
|
155
|
-
export function shouldCompactForSession(messageTokensEst, policy, {
|
|
245
|
+
export function shouldCompactForSession(messageTokensEst, policy, {
|
|
246
|
+
forceReactive = false,
|
|
247
|
+
messages,
|
|
248
|
+
sessionRef,
|
|
249
|
+
pressureTokens,
|
|
250
|
+
} = {}) {
|
|
156
251
|
if (!policy?.auto || !policy.boundaryTokens) return false;
|
|
157
252
|
if (forceReactive) return true;
|
|
158
253
|
if (messageTokensEst === null) return true;
|
|
159
|
-
|
|
254
|
+
const pressure = Number.isFinite(Number(pressureTokens))
|
|
255
|
+
? Number(pressureTokens)
|
|
256
|
+
: resolveCompactionPressureTokens(messageTokensEst, policy, { messages, sessionRef });
|
|
257
|
+
return pressure >= (policy.triggerTokens || policy.boundaryTokens);
|
|
160
258
|
}
|
|
161
259
|
export function countPrunedToolOutputs(before, after) {
|
|
162
260
|
if (!Array.isArray(before) || !Array.isArray(after)) return 0;
|
|
@@ -228,7 +326,7 @@ export function rememberCompactTelemetry(sessionRef, policy, meta = {}) {
|
|
|
228
326
|
const changedAt = Date.now();
|
|
229
327
|
sessionRef.compaction.lastChangedAt = changedAt;
|
|
230
328
|
sessionRef.compaction.lastCompactAt = changedAt;
|
|
231
|
-
sessionRef
|
|
329
|
+
invalidateProviderContextBaseline(sessionRef);
|
|
232
330
|
}
|
|
233
331
|
sessionRef.contextWindow = policy.contextWindow || sessionRef.contextWindow;
|
|
234
332
|
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(
|
|
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
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
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,17 @@ 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
|
-
//
|
|
34
|
-
//
|
|
46
|
+
if (_finalOutputLimitStop || (!_finalHasContent && _finalIncompleteStop)) {
|
|
47
|
+
// Exhausted token-cap recovery is abnormal even with preserved partial
|
|
48
|
+
// text. pause_turn/OTHER retain their prior non-empty completion
|
|
49
|
+
// semantics, while their empty forms remain abnormal.
|
|
35
50
|
return 'truncated';
|
|
36
51
|
}
|
|
37
52
|
if (!_finalHasContent && !_finalIsHidden) {
|
|
@@ -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' });
|
|
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:
|
|
433
|
+
content: persistedAssistantContent,
|
|
410
434
|
...(typeof result.reasoningContent === 'string' && result.reasoningContent
|
|
411
435
|
? { reasoningContent: result.reasoningContent }
|
|
412
436
|
: {}),
|
|
@@ -458,6 +482,7 @@ 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);
|
|
461
486
|
// Agent Runtime cache stats — record hit/miss after every successful
|
|
462
487
|
// ask so the registry reflects all agent traffic, not just
|
|
463
488
|
// 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
|
|
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
|
-
&&
|
|
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
|
|
65
|
-
|
|
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
|
|
@@ -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
|
|
@@ -297,10 +297,9 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
297
297
|
// Keep foreground commands on a long tool-owned timeout. The MCP dispatch
|
|
298
298
|
// layer must not add a shorter fallback ceiling when timeout is omitted.
|
|
299
299
|
// Reference-CLI parity (opencode/codex/claude-code): sync-first, no hard
|
|
300
|
-
// upper ceiling on a caller-provided timeout. Default 120 s (2 min)
|
|
301
|
-
// omitted; BASH_DEFAULT_TIMEOUT_MS / BASH_MAX_TIMEOUT_MS env overrides
|
|
302
|
-
//
|
|
303
|
-
// default — an explicit args.timeout is honored UNCAPPED.
|
|
300
|
+
// upper ceiling on a caller-provided total timeout. Default 120 s (2 min)
|
|
301
|
+
// when omitted; BASH_DEFAULT_TIMEOUT_MS / BASH_MAX_TIMEOUT_MS env overrides
|
|
302
|
+
// bound the blocking window when timeout promotion is available.
|
|
304
303
|
const _envDefaultTimeout = parseInt(process.env.BASH_DEFAULT_TIMEOUT_MS ?? '', 10);
|
|
305
304
|
const DEFAULT_BASH_TIMEOUT_MS = _envDefaultTimeout > 0 ? _envDefaultTimeout : 120_000;
|
|
306
305
|
// Background (async / run_in_background) jobs get NO omitted default: 0
|
|
@@ -315,8 +314,14 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
315
314
|
: DEFAULT_BASH_TIMEOUT_MS;
|
|
316
315
|
const hasExplicitTimeout = typeof args.timeout === 'number' && args.timeout > 0;
|
|
317
316
|
const timeoutMs = hasExplicitTimeout ? args.timeout : defaultTimeoutMs;
|
|
318
|
-
|
|
319
|
-
|
|
317
|
+
const _bgTasksDisabled = /^(1|true|yes|on)$/i.test(
|
|
318
|
+
String(process.env.MIXDOG_SHELL_DISABLE_BACKGROUND_TASKS || '').trim(),
|
|
319
|
+
);
|
|
320
|
+
const backgroundOnTimeout = !runInBackground
|
|
321
|
+
&& !_bgTasksDisabled
|
|
322
|
+
&& isAutobackgroundingAllowed(command, resolvedSpec.shellType);
|
|
323
|
+
// Explicit caller timeout remains the total deadline. When promotion is
|
|
324
|
+
// available, cap only its foreground blocking portion at MAX.
|
|
320
325
|
// JS timers (setTimeout) and PS WaitForExit(ms) are 32-bit: a delay above
|
|
321
326
|
// 2^31-1 wraps to a tiny/negative value and fires immediately. Clamp the
|
|
322
327
|
// uncapped explicit timeout once here (~24.8 days ceiling) so every
|
|
@@ -325,11 +330,15 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
325
330
|
const TIMER_MAX_MS = 2_147_483_647;
|
|
326
331
|
// timeoutMs <= 0 (omitted background default) means unlimited: pass it
|
|
327
332
|
// through untouched — the min() clamps below must not turn 0 into a bound.
|
|
328
|
-
const
|
|
333
|
+
const totalTimeout = timeoutMs <= 0
|
|
329
334
|
? 0
|
|
330
|
-
: (hasExplicitTimeout
|
|
331
|
-
|
|
332
|
-
|
|
335
|
+
: Math.min(timeoutMs, wmicRewrite?.timeoutMs || (hasExplicitTimeout ? TIMER_MAX_MS : MAX_BASH_TIMEOUT_MS));
|
|
336
|
+
const timeout = hasExplicitTimeout && backgroundOnTimeout
|
|
337
|
+
? Math.min(totalTimeout, MAX_BASH_TIMEOUT_MS)
|
|
338
|
+
: totalTimeout;
|
|
339
|
+
const promotedTimeoutMs = hasExplicitTimeout && backgroundOnTimeout
|
|
340
|
+
? Math.max(0, totalTimeout - timeout)
|
|
341
|
+
: 0;
|
|
333
342
|
const mergeStderr = args.merge_stderr === true;
|
|
334
343
|
const longForegroundHint = foregroundLongCommandHint(command, timeout, { ...args, run_in_background: runInBackground });
|
|
335
344
|
if (longForegroundHint) return formatShellToolFailure(longForegroundHint);
|
|
@@ -458,12 +467,6 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
458
467
|
// base commands (isAutobackgroundingAllowed), (b) the truthy
|
|
459
468
|
// MIXDOG_SHELL_DISABLE_BACKGROUND_TASKS env. Never applies to
|
|
460
469
|
// run_in_background (already detached, handled above).
|
|
461
|
-
const _bgTasksDisabled = /^(1|true|yes|on)$/i.test(
|
|
462
|
-
String(process.env.MIXDOG_SHELL_DISABLE_BACKGROUND_TASKS || '').trim(),
|
|
463
|
-
);
|
|
464
|
-
const backgroundOnTimeout = !runInBackground
|
|
465
|
-
&& !_bgTasksDisabled
|
|
466
|
-
&& isAutobackgroundingAllowed(command, shellType);
|
|
467
470
|
const result = await execShellCommand({
|
|
468
471
|
shell, shellArg, shellArgs, command: syncCommand,
|
|
469
472
|
env: spawnEnv,
|
|
@@ -474,6 +477,7 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
474
477
|
// On a foreground timeout, promote the still-running child to a
|
|
475
478
|
// tracked background job (unlimited) instead of killing it.
|
|
476
479
|
backgroundOnTimeout,
|
|
480
|
+
promotedTimeoutMs,
|
|
477
481
|
// Threaded so an auto-backgrounded foreground job is stamped with
|
|
478
482
|
// the dispatching terminal's claude.exe pid (per-terminal scope).
|
|
479
483
|
clientHostPid: options?.clientHostPid,
|
|
@@ -507,10 +511,7 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
507
511
|
stdout: result.stdoutPath ? normalizeOutputPath(result.stdoutPath) : null,
|
|
508
512
|
stderr: (!mergeStderr && result.stderrPath) ? normalizeOutputPath(result.stderrPath) : null,
|
|
509
513
|
cwd: bashWorkDir,
|
|
510
|
-
|
|
511
|
-
// — matches the async default; the original
|
|
512
|
-
// foreground timeout no longer bounds the promoted job.
|
|
513
|
-
timeoutMs: 0,
|
|
514
|
+
timeoutMs: result.backgroundTimeoutMs || 0,
|
|
514
515
|
},
|
|
515
516
|
resultType: 'shell_task_result',
|
|
516
517
|
cancel: () => killShellJob(result.jobId),
|
|
@@ -530,7 +531,7 @@ export async function executeBashTool(args, workDir, options = {}) {
|
|
|
530
531
|
const lines = [
|
|
531
532
|
task ? renderBackgroundTask(task) : (result.jobId ? `[task_id: ${result.jobId}]` : null),
|
|
532
533
|
'',
|
|
533
|
-
result.backgroundMessage || 'auto-backgrounded; still running',
|
|
534
|
+
result.backgroundMessage || 'auto-backgrounded; still running — judge from the partial output whether waiting can finish in budget, or diagnose and pursue an alternative.',
|
|
534
535
|
partialStdout ? `\n[partial stdout]\n${partialStdout}` : '',
|
|
535
536
|
(!mergeStderr && partialStderr) ? `\n[partial stderr]\n${partialStderr}` : '',
|
|
536
537
|
].filter((l) => l !== null && l !== '');
|
|
@@ -83,7 +83,7 @@ export const BUILTIN_TOOLS = [
|
|
|
83
83
|
properties: {
|
|
84
84
|
command: { type: 'string', description: 'Command.' },
|
|
85
85
|
cwd: { type: 'string', description: 'Working directory; persists across calls. Omit to reuse; absolute path changes it.' },
|
|
86
|
-
timeout: { type: 'number', description: `Timeout ms. Default ${_shellDefaultTimeoutMs()} (${_shellDefaultTimeoutMs() / 60000} min) when omitted
|
|
86
|
+
timeout: { type: 'number', description: `Timeout ms. Default ${_shellDefaultTimeoutMs()} (${_shellDefaultTimeoutMs() / 60000} min) when omitted. On sync timeout the command is MOVED TO BACKGROUND (a task_id you can wait/status/read/cancel) and keeps running instead of being killed; an explicit timeout blocks for at most BASH_MAX_TIMEOUT_MS (default 10 min), and only its remainder beyond that cap is still enforced as a background deadline. Sleep-like commands and MIXDOG_SHELL_DISABLE_BACKGROUND_TASKS opt out (killed with a [timeout] marker after blocking for the full explicit timeout). async/background runs with timeout omitted have NO timeout (run until done/cancelled); an explicit timeout is still enforced.` },
|
|
87
87
|
merge_stderr: { type: 'boolean', description: 'Merge stderr.' },
|
|
88
88
|
mode: { type: 'string', enum: ['sync', 'async'], description: executionModeSchemaDescription('sync') },
|
|
89
89
|
shell: { type: 'string', enum: ['bash', 'powershell'], description: 'Force shell. Windows defaults to PowerShell; bash = Git Bash/POSIX.' },
|