@xortex/xcode 3.0.8 → 3.1.1

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 (57) hide show
  1. package/bin/xcode +33 -85
  2. package/bootstrap/state.ts +1758 -0
  3. package/context/QueuedMessageContext.tsx +63 -0
  4. package/context/fpsMetrics.tsx +30 -0
  5. package/context/mailbox.tsx +38 -0
  6. package/context/modalContext.tsx +58 -0
  7. package/context/notifications.tsx +240 -0
  8. package/context/overlayContext.tsx +151 -0
  9. package/context/promptOverlayContext.tsx +125 -0
  10. package/context/stats.tsx +220 -0
  11. package/context/voice.tsx +88 -0
  12. package/coordinator/coordinatorMode.ts +369 -0
  13. package/entrypoints/cli.tsx +1 -1
  14. package/ink.ts +85 -0
  15. package/interactiveHelpers.tsx +366 -0
  16. package/macro.ts +1 -1
  17. package/memdir/findRelevantMemories.ts +141 -0
  18. package/memdir/memdir.ts +511 -0
  19. package/memdir/memoryAge.ts +53 -0
  20. package/memdir/memoryScan.ts +94 -0
  21. package/memdir/memoryTypes.ts +271 -0
  22. package/memdir/paths.ts +291 -0
  23. package/memdir/teamMemPaths.ts +292 -0
  24. package/memdir/teamMemPrompts.ts +100 -0
  25. package/package.json +42 -28
  26. package/query/config.ts +46 -0
  27. package/query/deps.ts +40 -0
  28. package/query/stopHooks.ts +470 -0
  29. package/query/tokenBudget.ts +93 -0
  30. package/schemas/hooks.ts +222 -0
  31. package/screens/Doctor.tsx +575 -0
  32. package/screens/REPL.tsx +7107 -0
  33. package/screens/ResumeConversation.tsx +399 -0
  34. package/scripts/postinstall.js +90 -0
  35. package/setup.ts +477 -0
  36. package/tasks.ts +39 -0
  37. package/tools.ts +396 -0
  38. package/upstreamproxy/relay.ts +455 -0
  39. package/upstreamproxy/upstreamproxy.ts +285 -0
  40. package/voice/voiceModeEnabled.ts +54 -0
  41. package/inspect.js +0 -7
  42. package/patch-box.js +0 -54
  43. package/patch-compact.js +0 -13
  44. package/patch-condensed-center.js +0 -13
  45. package/patch-condensed-row.js +0 -13
  46. package/patch-condensed.js +0 -13
  47. package/patch-final.js +0 -58
  48. package/patch-input-body.js +0 -46
  49. package/patch-input-body2.js +0 -35
  50. package/patch-input-style.js +0 -13
  51. package/patch-input-width.js +0 -13
  52. package/patch-layout.js +0 -87
  53. package/patch-logo-row.js +0 -12
  54. package/patch-width.js +0 -13
  55. package/patch-width2.js +0 -13
  56. package/patch2.js +0 -74
  57. package/patch3.js +0 -13
@@ -0,0 +1,470 @@
1
+ import { feature } from 'bun:bundle'
2
+ import { getShortcutDisplay } from '../keybindings/shortcutFormat.js'
3
+ import { isExtractModeActive } from '../memdir/paths.js'
4
+ import {
5
+ type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
6
+ logEvent,
7
+ } from '../services/analytics/index.js'
8
+ import type { ToolUseContext } from '../Tool.js'
9
+ import type { HookProgress } from '../types/hooks.js'
10
+ import type {
11
+ AssistantMessage,
12
+ Message,
13
+ RequestStartEvent,
14
+ StopHookInfo,
15
+ StreamEvent,
16
+ TombstoneMessage,
17
+ ToolUseSummaryMessage,
18
+ } from '../types/message.js'
19
+ import { createAttachmentMessage } from '../utils/attachments.js'
20
+ import { logForDebugging } from '../utils/debug.js'
21
+ import { errorMessage } from '../utils/errors.js'
22
+ import type { REPLHookContext } from '../utils/hooks/postSamplingHooks.js'
23
+ import {
24
+ executeStopHooks,
25
+ executeTaskCompletedHooks,
26
+ executeTeammateIdleHooks,
27
+ getStopHookMessage,
28
+ getTaskCompletedHookMessage,
29
+ getTeammateIdleHookMessage,
30
+ } from '../utils/hooks.js'
31
+ import {
32
+ createStopHookSummaryMessage,
33
+ createSystemMessage,
34
+ createUserInterruptionMessage,
35
+ createUserMessage,
36
+ } from '../utils/messages.js'
37
+ import type { SystemPrompt } from '../utils/systemPromptType.js'
38
+ import { getTaskListId, listTasks } from '../utils/tasks.js'
39
+ import { getAgentName, getTeamName, isTeammate } from '../utils/teammate.js'
40
+
41
+ /* eslint-disable @typescript-eslint/no-require-imports */
42
+ const extractMemoriesModule = require('../services/extractMemories/extractMemories.js') as typeof import('../services/extractMemories/extractMemories.js')
43
+ const jobClassifierModule = feature('TEMPLATES')
44
+ ? (require('../jobs/classifier.js') as typeof import('../jobs/classifier.js'))
45
+ : null
46
+
47
+ /* eslint-enable @typescript-eslint/no-require-imports */
48
+
49
+ import type { QuerySource } from '../constants/querySource.js'
50
+ import { executeAutoDream } from '../services/autoDream/autoDream.js'
51
+ import { executePromptSuggestion } from '../services/PromptSuggestion/promptSuggestion.js'
52
+ import { isBareMode, isEnvDefinedFalsy } from '../utils/envUtils.js'
53
+ import {
54
+ createCacheSafeParams,
55
+ saveCacheSafeParams,
56
+ } from '../utils/forkedAgent.js'
57
+
58
+ type StopHookResult = {
59
+ blockingErrors: Message[]
60
+ preventContinuation: boolean
61
+ }
62
+
63
+ export async function* handleStopHooks(
64
+ messagesForQuery: Message[],
65
+ assistantMessages: AssistantMessage[],
66
+ systemPrompt: SystemPrompt,
67
+ userContext: { [k: string]: string },
68
+ systemContext: { [k: string]: string },
69
+ toolUseContext: ToolUseContext,
70
+ querySource: QuerySource,
71
+ stopHookActive?: boolean,
72
+ ): AsyncGenerator<
73
+ | StreamEvent
74
+ | RequestStartEvent
75
+ | Message
76
+ | TombstoneMessage
77
+ | ToolUseSummaryMessage,
78
+ StopHookResult
79
+ > {
80
+ const hookStartTime = Date.now()
81
+
82
+ const stopHookContext: REPLHookContext = {
83
+ messages: [...messagesForQuery, ...assistantMessages],
84
+ systemPrompt,
85
+ userContext,
86
+ systemContext,
87
+ toolUseContext,
88
+ querySource,
89
+ }
90
+ // Only save params for main session queries — subagents must not overwrite.
91
+ // Outside the prompt-suggestion gate: the REPL /btw command and the
92
+ // side_question SDK control_request both read this snapshot, and neither
93
+ // depends on prompt suggestions being enabled.
94
+ if (querySource === 'repl_main_thread' || querySource === 'sdk') {
95
+ saveCacheSafeParams(createCacheSafeParams(stopHookContext))
96
+ }
97
+
98
+ // Template job classification: when running as a dispatched job, classify
99
+ // state after each turn. Gate on repl_main_thread so background forks
100
+ // (extract-memories, auto-dream) don't pollute the timeline with their own
101
+ // assistant messages. Await the classifier so state.json is written before
102
+ // the turn returns — otherwise `claude list` shows stale state for the gap.
103
+ // Env key hardcoded (vs importing JOB_ENV_KEY from jobs/state) to match the
104
+ // require()-gated jobs/ import pattern above; spawn.test.ts asserts the
105
+ // string matches.
106
+ if (
107
+ feature('TEMPLATES') &&
108
+ process.env.CLAUDE_JOB_DIR &&
109
+ querySource.startsWith('repl_main_thread') &&
110
+ !toolUseContext.agentId
111
+ ) {
112
+ // Full turn history — assistantMessages resets each queryLoop iteration,
113
+ // so tool calls from earlier iterations (Agent spawn, then summary) need
114
+ // messagesForQuery to be visible in the tool-call summary.
115
+ const turnAssistantMessages = stopHookContext.messages.filter(
116
+ (m): m is AssistantMessage => m.type === 'assistant',
117
+ )
118
+ const p = jobClassifierModule!
119
+ .classifyAndWriteState(process.env.CLAUDE_JOB_DIR, turnAssistantMessages)
120
+ .catch(err => {
121
+ logForDebugging(`[job] classifier error: ${errorMessage(err)}`, {
122
+ level: 'error',
123
+ })
124
+ })
125
+ await Promise.race([
126
+ p,
127
+ // eslint-disable-next-line no-restricted-syntax -- sleep() has no .unref(); timer must not block exit
128
+ new Promise<void>(r => setTimeout(r, 60_000).unref()),
129
+ ])
130
+ }
131
+ // --bare / SIMPLE: skip background bookkeeping (prompt suggestion,
132
+ // memory extraction, auto-dream). Scripted -p calls don't want auto-memory
133
+ // or forked agents contending for resources during shutdown.
134
+ if (!isBareMode()) {
135
+ // Inline env check for dead code elimination in external builds
136
+ if (!isEnvDefinedFalsy(process.env.CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION)) {
137
+ void executePromptSuggestion(stopHookContext)
138
+ }
139
+ if (
140
+ !toolUseContext.agentId &&
141
+ isExtractModeActive()
142
+ ) {
143
+ // Fire-and-forget in both interactive and non-interactive. For -p/SDK,
144
+ // print.ts drains the in-flight promise after flushing the response
145
+ // but before gracefulShutdownSync (see drainPendingExtraction).
146
+ void extractMemoriesModule!.executeExtractMemories(
147
+ stopHookContext,
148
+ toolUseContext.appendSystemMessage,
149
+ )
150
+ }
151
+ if (!toolUseContext.agentId) {
152
+ void executeAutoDream(stopHookContext, toolUseContext.appendSystemMessage)
153
+ }
154
+ }
155
+
156
+ // chicago MCP: auto-unhide + lock release at turn end.
157
+ // Main thread only — the CU lock is a process-wide module-level variable,
158
+ // so a subagent's stopHooks releasing it leaves the main thread's cleanup
159
+ // seeing isLockHeldLocally()===false → no exit notification, and unhides
160
+ // mid-turn. Subagents don't start CU sessions so this is a pure skip.
161
+ if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
162
+ try {
163
+ const { cleanupComputerUseAfterTurn } = await import(
164
+ '../utils/computerUse/cleanup.js'
165
+ )
166
+ await cleanupComputerUseAfterTurn(toolUseContext)
167
+ } catch {
168
+ // Failures are silent — this is dogfooding cleanup, not critical path
169
+ }
170
+ }
171
+
172
+ try {
173
+ const blockingErrors = []
174
+ const appState = toolUseContext.getAppState()
175
+ const permissionMode = appState.toolPermissionContext.mode
176
+
177
+ const generator = executeStopHooks(
178
+ permissionMode,
179
+ toolUseContext.abortController.signal,
180
+ undefined,
181
+ stopHookActive ?? false,
182
+ toolUseContext.agentId,
183
+ toolUseContext,
184
+ [...messagesForQuery, ...assistantMessages],
185
+ toolUseContext.agentType,
186
+ )
187
+
188
+ // Consume all progress messages and get blocking errors
189
+ let stopHookToolUseID = ''
190
+ let hookCount = 0
191
+ let preventedContinuation = false
192
+ let stopReason = ''
193
+ let hasOutput = false
194
+ const hookErrors: string[] = []
195
+ const hookInfos: StopHookInfo[] = []
196
+
197
+ for await (const result of generator) {
198
+ if (result.message) {
199
+ yield result.message
200
+ // Track toolUseID from progress messages and count hooks
201
+ if (result.message.type === 'progress' && result.message.toolUseID) {
202
+ stopHookToolUseID = result.message.toolUseID
203
+ hookCount++
204
+ // Extract hook command and prompt text from progress data
205
+ const progressData = result.message.data as HookProgress
206
+ if (progressData.command) {
207
+ hookInfos.push({
208
+ command: progressData.command,
209
+ promptText: progressData.promptText,
210
+ })
211
+ }
212
+ }
213
+ // Track errors and output from attachments
214
+ if (result.message.type === 'attachment') {
215
+ const attachment = result.message.attachment
216
+ if (
217
+ 'hookEvent' in attachment &&
218
+ (attachment.hookEvent === 'Stop' ||
219
+ attachment.hookEvent === 'SubagentStop')
220
+ ) {
221
+ if (attachment.type === 'hook_non_blocking_error') {
222
+ hookErrors.push(
223
+ attachment.stderr || `Exit code ${attachment.exitCode}`,
224
+ )
225
+ // Non-blocking errors always have output
226
+ hasOutput = true
227
+ } else if (attachment.type === 'hook_error_during_execution') {
228
+ hookErrors.push(attachment.content)
229
+ hasOutput = true
230
+ } else if (attachment.type === 'hook_success') {
231
+ // Check if successful hook produced any stdout/stderr
232
+ if (
233
+ (attachment.stdout && attachment.stdout.trim()) ||
234
+ (attachment.stderr && attachment.stderr.trim())
235
+ ) {
236
+ hasOutput = true
237
+ }
238
+ }
239
+ // Extract per-hook duration for timing visibility.
240
+ // Hooks run in parallel; match by command + first unassigned entry.
241
+ if ('durationMs' in attachment && 'command' in attachment) {
242
+ const info = hookInfos.find(
243
+ i =>
244
+ i.command === attachment.command &&
245
+ i.durationMs === undefined,
246
+ )
247
+ if (info) {
248
+ info.durationMs = attachment.durationMs
249
+ }
250
+ }
251
+ }
252
+ }
253
+ }
254
+ if (result.blockingError) {
255
+ const userMessage = createUserMessage({
256
+ content: getStopHookMessage(result.blockingError),
257
+ isMeta: true, // Hide from UI (shown in summary message instead)
258
+ })
259
+ blockingErrors.push(userMessage)
260
+ yield userMessage
261
+ hasOutput = true
262
+ // Add to hookErrors so it appears in the summary
263
+ hookErrors.push(result.blockingError.blockingError)
264
+ }
265
+ // Check if hook wants to prevent continuation
266
+ if (result.preventContinuation) {
267
+ preventedContinuation = true
268
+ stopReason = result.stopReason || 'Stop hook prevented continuation'
269
+ // Create attachment to track the stopped continuation (for structured data)
270
+ yield createAttachmentMessage({
271
+ type: 'hook_stopped_continuation',
272
+ message: stopReason,
273
+ hookName: 'Stop',
274
+ toolUseID: stopHookToolUseID,
275
+ hookEvent: 'Stop',
276
+ })
277
+ }
278
+
279
+ // Check if we were aborted during hook execution
280
+ if (toolUseContext.abortController.signal.aborted) {
281
+ logEvent('tengu_pre_stop_hooks_cancelled', {
282
+ queryChainId: toolUseContext.queryTracking
283
+ ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
284
+
285
+ queryDepth: toolUseContext.queryTracking?.depth,
286
+ })
287
+ yield createUserInterruptionMessage({
288
+ toolUse: false,
289
+ })
290
+ return { blockingErrors: [], preventContinuation: true }
291
+ }
292
+ }
293
+
294
+ // Create summary system message if hooks ran
295
+ if (hookCount > 0) {
296
+ yield createStopHookSummaryMessage(
297
+ hookCount,
298
+ hookInfos,
299
+ hookErrors,
300
+ preventedContinuation,
301
+ stopReason,
302
+ hasOutput,
303
+ 'suggestion',
304
+ stopHookToolUseID,
305
+ )
306
+
307
+ // Send notification about errors (shown in verbose/transcript mode via ctrl+o)
308
+ if (hookErrors.length > 0) {
309
+ const expandShortcut = getShortcutDisplay(
310
+ 'app:toggleTranscript',
311
+ 'Global',
312
+ 'ctrl+o',
313
+ )
314
+ toolUseContext.addNotification?.({
315
+ key: 'stop-hook-error',
316
+ text: `Stop hook error occurred \u00b7 ${expandShortcut} to see`,
317
+ priority: 'immediate',
318
+ })
319
+ }
320
+ }
321
+
322
+ if (preventedContinuation) {
323
+ return { blockingErrors: [], preventContinuation: true }
324
+ }
325
+
326
+ // Collect blocking errors from stop hooks
327
+ if (blockingErrors.length > 0) {
328
+ return { blockingErrors, preventContinuation: false }
329
+ }
330
+
331
+ // After Stop hooks pass, run TeammateIdle and TaskCompleted hooks if this is a teammate
332
+ if (isTeammate()) {
333
+ const teammateName = getAgentName() ?? ''
334
+ const teamName = getTeamName() ?? ''
335
+ const teammateBlockingErrors: Message[] = []
336
+ let teammatePreventedContinuation = false
337
+ let teammateStopReason: string | undefined
338
+ // Each hook executor generates its own toolUseID — capture from progress
339
+ // messages (same pattern as stopHookToolUseID at L142), not the Stop ID.
340
+ let teammateHookToolUseID = ''
341
+
342
+ // Run TaskCompleted hooks for any in-progress tasks owned by this teammate
343
+ const taskListId = getTaskListId()
344
+ const tasks = await listTasks(taskListId)
345
+ const inProgressTasks = tasks.filter(
346
+ t => t.status === 'in_progress' && t.owner === teammateName,
347
+ )
348
+
349
+ for (const task of inProgressTasks) {
350
+ const taskCompletedGenerator = executeTaskCompletedHooks(
351
+ task.id,
352
+ task.subject,
353
+ task.description,
354
+ teammateName,
355
+ teamName,
356
+ permissionMode,
357
+ toolUseContext.abortController.signal,
358
+ undefined,
359
+ toolUseContext,
360
+ )
361
+
362
+ for await (const result of taskCompletedGenerator) {
363
+ if (result.message) {
364
+ if (
365
+ result.message.type === 'progress' &&
366
+ result.message.toolUseID
367
+ ) {
368
+ teammateHookToolUseID = result.message.toolUseID
369
+ }
370
+ yield result.message
371
+ }
372
+ if (result.blockingError) {
373
+ const userMessage = createUserMessage({
374
+ content: getTaskCompletedHookMessage(result.blockingError),
375
+ isMeta: true,
376
+ })
377
+ teammateBlockingErrors.push(userMessage)
378
+ yield userMessage
379
+ }
380
+ // Match Stop hook behavior: allow preventContinuation/stopReason
381
+ if (result.preventContinuation) {
382
+ teammatePreventedContinuation = true
383
+ teammateStopReason =
384
+ result.stopReason || 'TaskCompleted hook prevented continuation'
385
+ yield createAttachmentMessage({
386
+ type: 'hook_stopped_continuation',
387
+ message: teammateStopReason,
388
+ hookName: 'TaskCompleted',
389
+ toolUseID: teammateHookToolUseID,
390
+ hookEvent: 'TaskCompleted',
391
+ })
392
+ }
393
+ if (toolUseContext.abortController.signal.aborted) {
394
+ return { blockingErrors: [], preventContinuation: true }
395
+ }
396
+ }
397
+ }
398
+
399
+ // Run TeammateIdle hooks
400
+ const teammateIdleGenerator = executeTeammateIdleHooks(
401
+ teammateName,
402
+ teamName,
403
+ permissionMode,
404
+ toolUseContext.abortController.signal,
405
+ )
406
+
407
+ for await (const result of teammateIdleGenerator) {
408
+ if (result.message) {
409
+ if (result.message.type === 'progress' && result.message.toolUseID) {
410
+ teammateHookToolUseID = result.message.toolUseID
411
+ }
412
+ yield result.message
413
+ }
414
+ if (result.blockingError) {
415
+ const userMessage = createUserMessage({
416
+ content: getTeammateIdleHookMessage(result.blockingError),
417
+ isMeta: true,
418
+ })
419
+ teammateBlockingErrors.push(userMessage)
420
+ yield userMessage
421
+ }
422
+ // Match Stop hook behavior: allow preventContinuation/stopReason
423
+ if (result.preventContinuation) {
424
+ teammatePreventedContinuation = true
425
+ teammateStopReason =
426
+ result.stopReason || 'TeammateIdle hook prevented continuation'
427
+ yield createAttachmentMessage({
428
+ type: 'hook_stopped_continuation',
429
+ message: teammateStopReason,
430
+ hookName: 'TeammateIdle',
431
+ toolUseID: teammateHookToolUseID,
432
+ hookEvent: 'TeammateIdle',
433
+ })
434
+ }
435
+ if (toolUseContext.abortController.signal.aborted) {
436
+ return { blockingErrors: [], preventContinuation: true }
437
+ }
438
+ }
439
+
440
+ if (teammatePreventedContinuation) {
441
+ return { blockingErrors: [], preventContinuation: true }
442
+ }
443
+
444
+ if (teammateBlockingErrors.length > 0) {
445
+ return {
446
+ blockingErrors: teammateBlockingErrors,
447
+ preventContinuation: false,
448
+ }
449
+ }
450
+ }
451
+
452
+ return { blockingErrors: [], preventContinuation: false }
453
+ } catch (error) {
454
+ const durationMs = Date.now() - hookStartTime
455
+ logEvent('tengu_stop_hook_error', {
456
+ duration: durationMs,
457
+
458
+ queryChainId: toolUseContext.queryTracking
459
+ ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
460
+ queryDepth: toolUseContext.queryTracking?.depth,
461
+ })
462
+ // Yield a system message that is not visible to the model for the user
463
+ // to debug their hook.
464
+ yield createSystemMessage(
465
+ `Stop hook failed: ${errorMessage(error)}`,
466
+ 'warning',
467
+ )
468
+ return { blockingErrors: [], preventContinuation: false }
469
+ }
470
+ }
@@ -0,0 +1,93 @@
1
+ import { getBudgetContinuationMessage } from '../utils/tokenBudget.js'
2
+
3
+ const COMPLETION_THRESHOLD = 0.9
4
+ const DIMINISHING_THRESHOLD = 500
5
+
6
+ export type BudgetTracker = {
7
+ continuationCount: number
8
+ lastDeltaTokens: number
9
+ lastGlobalTurnTokens: number
10
+ startedAt: number
11
+ }
12
+
13
+ export function createBudgetTracker(): BudgetTracker {
14
+ return {
15
+ continuationCount: 0,
16
+ lastDeltaTokens: 0,
17
+ lastGlobalTurnTokens: 0,
18
+ startedAt: Date.now(),
19
+ }
20
+ }
21
+
22
+ type ContinueDecision = {
23
+ action: 'continue'
24
+ nudgeMessage: string
25
+ continuationCount: number
26
+ pct: number
27
+ turnTokens: number
28
+ budget: number
29
+ }
30
+
31
+ type StopDecision = {
32
+ action: 'stop'
33
+ completionEvent: {
34
+ continuationCount: number
35
+ pct: number
36
+ turnTokens: number
37
+ budget: number
38
+ diminishingReturns: boolean
39
+ durationMs: number
40
+ } | null
41
+ }
42
+
43
+ export type TokenBudgetDecision = ContinueDecision | StopDecision
44
+
45
+ export function checkTokenBudget(
46
+ tracker: BudgetTracker,
47
+ agentId: string | undefined,
48
+ budget: number | null,
49
+ globalTurnTokens: number,
50
+ ): TokenBudgetDecision {
51
+ if (agentId || budget === null || budget <= 0) {
52
+ return { action: 'stop', completionEvent: null }
53
+ }
54
+
55
+ const turnTokens = globalTurnTokens
56
+ const pct = Math.round((turnTokens / budget) * 100)
57
+ const deltaSinceLastCheck = globalTurnTokens - tracker.lastGlobalTurnTokens
58
+
59
+ const isDiminishing =
60
+ tracker.continuationCount >= 3 &&
61
+ deltaSinceLastCheck < DIMINISHING_THRESHOLD &&
62
+ tracker.lastDeltaTokens < DIMINISHING_THRESHOLD
63
+
64
+ if (!isDiminishing && turnTokens < budget * COMPLETION_THRESHOLD) {
65
+ tracker.continuationCount++
66
+ tracker.lastDeltaTokens = deltaSinceLastCheck
67
+ tracker.lastGlobalTurnTokens = globalTurnTokens
68
+ return {
69
+ action: 'continue',
70
+ nudgeMessage: getBudgetContinuationMessage(pct, turnTokens, budget),
71
+ continuationCount: tracker.continuationCount,
72
+ pct,
73
+ turnTokens,
74
+ budget,
75
+ }
76
+ }
77
+
78
+ if (isDiminishing || tracker.continuationCount > 0) {
79
+ return {
80
+ action: 'stop',
81
+ completionEvent: {
82
+ continuationCount: tracker.continuationCount,
83
+ pct,
84
+ turnTokens,
85
+ budget,
86
+ diminishingReturns: isDiminishing,
87
+ durationMs: Date.now() - tracker.startedAt,
88
+ },
89
+ }
90
+ }
91
+
92
+ return { action: 'stop', completionEvent: null }
93
+ }