@xortex/xcode 3.0.8 → 3.1.0
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/INSTALLATION.md +285 -0
- package/QUICKSTART.md +151 -0
- package/SYSTEM_PROMPT.md +583 -0
- package/SYSTEM_PROMPT_EXTRACTED.md +1 -0
- package/Untitled +1 -0
- package/bin/xcode +33 -85
- package/bootstrap/state.ts +1758 -0
- package/bun.lock +645 -0
- package/context/QueuedMessageContext.tsx +63 -0
- package/context/fpsMetrics.tsx +30 -0
- package/context/mailbox.tsx +38 -0
- package/context/modalContext.tsx +58 -0
- package/context/notifications.tsx +240 -0
- package/context/overlayContext.tsx +151 -0
- package/context/promptOverlayContext.tsx +125 -0
- package/context/stats.tsx +220 -0
- package/context/voice.tsx +88 -0
- package/coordinator/coordinatorMode.ts +369 -0
- package/costHook.ts +22 -0
- package/dialogLaunchers.tsx +133 -0
- package/entrypoints/cli.tsx +1 -1
- package/extract_prompt.ts +304 -0
- package/ink.ts +85 -0
- package/install.sh +221 -0
- package/interactiveHelpers.tsx +366 -0
- package/macro.ts +1 -1
- package/memdir/findRelevantMemories.ts +141 -0
- package/memdir/memdir.ts +511 -0
- package/memdir/memoryAge.ts +53 -0
- package/memdir/memoryScan.ts +94 -0
- package/memdir/memoryTypes.ts +271 -0
- package/memdir/paths.ts +291 -0
- package/memdir/teamMemPaths.ts +292 -0
- package/memdir/teamMemPrompts.ts +100 -0
- package/moreright/useMoreRight.tsx +26 -0
- package/native-ts/color-diff/index.ts +999 -0
- package/native-ts/file-index/index.ts +370 -0
- package/native-ts/yoga-layout/enums.ts +134 -0
- package/native-ts/yoga-layout/index.ts +2578 -0
- package/outputStyles/loadOutputStylesDir.ts +98 -0
- package/package.json +3 -42
- package/plugins/builtinPlugins.ts +159 -0
- package/plugins/bundled/index.ts +23 -0
- package/projectOnboardingState.ts +83 -0
- package/public/claude-files.png +0 -0
- package/public/leak-tweet.png +0 -0
- package/query/config.ts +46 -0
- package/query/deps.ts +40 -0
- package/query/stopHooks.ts +470 -0
- package/query/tokenBudget.ts +93 -0
- package/replLauncher.tsx +27 -0
- package/schemas/hooks.ts +222 -0
- package/screens/Doctor.tsx +575 -0
- package/screens/REPL.tsx +7107 -0
- package/screens/ResumeConversation.tsx +399 -0
- package/scripts/postinstall.js +90 -0
- package/server/createDirectConnectSession.ts +88 -0
- package/server/directConnectManager.ts +213 -0
- package/server/types.ts +57 -0
- package/setup.ts +477 -0
- package/stub_types.sh +13 -0
- package/tasks.ts +39 -0
- package/tools.ts +396 -0
- package/upstreamproxy/relay.ts +455 -0
- package/upstreamproxy/upstreamproxy.ts +285 -0
- package/vim/motions.ts +82 -0
- package/vim/operators.ts +556 -0
- package/vim/textObjects.ts +186 -0
- package/vim/transitions.ts +490 -0
- package/vim/types.ts +199 -0
- package/voice/voiceModeEnabled.ts +54 -0
|
@@ -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
|
+
}
|
package/replLauncher.tsx
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { StatsStore } from "./context/stats.js";
|
|
3
|
+
import type { Root } from "./ink.js";
|
|
4
|
+
import type { Props as REPLProps } from "./screens/REPL.js";
|
|
5
|
+
import type { AppState } from "./state/AppStateStore.js";
|
|
6
|
+
import type { FpsMetrics } from "./utils/fpsTracker.js";
|
|
7
|
+
type AppWrapperProps = {
|
|
8
|
+
getFpsMetrics: () => FpsMetrics | undefined;
|
|
9
|
+
stats?: StatsStore;
|
|
10
|
+
initialState: AppState;
|
|
11
|
+
};
|
|
12
|
+
export async function launchRepl(
|
|
13
|
+
root: Root,
|
|
14
|
+
appProps: AppWrapperProps,
|
|
15
|
+
replProps: REPLProps,
|
|
16
|
+
renderAndRun: (root: Root, element: React.ReactNode) => Promise<void>,
|
|
17
|
+
): Promise<void> {
|
|
18
|
+
const { App } = await import("./components/App.js");
|
|
19
|
+
const { REPL } = await import("./screens/REPL.js");
|
|
20
|
+
await renderAndRun(
|
|
21
|
+
root,
|
|
22
|
+
<App {...appProps}>
|
|
23
|
+
<REPL {...replProps} />
|
|
24
|
+
</App>,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlN0YXRzU3RvcmUiLCJSb290IiwiUHJvcHMiLCJSRVBMUHJvcHMiLCJBcHBTdGF0ZSIsIkZwc01ldHJpY3MiLCJBcHBXcmFwcGVyUHJvcHMiLCJnZXRGcHNNZXRyaWNzIiwic3RhdHMiLCJpbml0aWFsU3RhdGUiLCJsYXVuY2hSZXBsIiwicm9vdCIsImFwcFByb3BzIiwicmVwbFByb3BzIiwicmVuZGVyQW5kUnVuIiwiZWxlbWVudCIsIlJlYWN0Tm9kZSIsIlByb21pc2UiLCJBcHAiLCJSRVBMIl0sInNvdXJjZXMiOlsicmVwbExhdW5jaGVyLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgdHlwZSB7IFN0YXRzU3RvcmUgfSBmcm9tICcuL2NvbnRleHQvc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