@sixb/agent 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/abort.d.ts +6 -0
- package/dist/abort.d.ts.map +1 -0
- package/dist/agent-prompt.d.ts +126 -0
- package/dist/agent-prompt.d.ts.map +1 -0
- package/dist/agent-runtime/errors.d.ts +13 -0
- package/dist/agent-runtime/errors.d.ts.map +1 -0
- package/dist/agent-runtime/preflight.d.ts +18 -0
- package/dist/agent-runtime/preflight.d.ts.map +1 -0
- package/dist/agent-runtime/profile.d.ts +14 -0
- package/dist/agent-runtime/profile.d.ts.map +1 -0
- package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
- package/dist/artifacts/materialize.d.ts +115 -0
- package/dist/artifacts/materialize.d.ts.map +1 -0
- package/dist/artifacts/publish-tool.d.ts +86 -0
- package/dist/artifacts/publish-tool.d.ts.map +1 -0
- package/dist/ask-parent-tool.d.ts +36 -0
- package/dist/ask-parent-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +109 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/child-files.d.ts +91 -0
- package/dist/child-files.d.ts.map +1 -0
- package/dist/child-questions.d.ts +24 -0
- package/dist/child-questions.d.ts.map +1 -0
- package/dist/computer-runner.d.ts +74 -0
- package/dist/computer-runner.d.ts.map +1 -0
- package/dist/context-budget.d.ts +34 -0
- package/dist/context-budget.d.ts.map +1 -0
- package/dist/context-compaction.d.ts +148 -0
- package/dist/context-compaction.d.ts.map +1 -0
- package/dist/context-guard.d.ts +69 -0
- package/dist/context-guard.d.ts.map +1 -0
- package/dist/conversation-memory.d.ts +21 -0
- package/dist/conversation-memory.d.ts.map +1 -0
- package/dist/conversation-messages.d.ts +33 -0
- package/dist/conversation-messages.d.ts.map +1 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/execution-plan.d.ts +38 -0
- package/dist/execution-plan.d.ts.map +1 -0
- package/dist/failure.d.ts +14 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/family-mailbox.d.ts +60 -0
- package/dist/family-mailbox.d.ts.map +1 -0
- package/dist/file-ref.d.ts +6 -0
- package/dist/file-ref.d.ts.map +1 -0
- package/dist/image-attachments.d.ts +34 -0
- package/dist/image-attachments.d.ts.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14387 -0
- package/dist/index.js.map +72 -0
- package/dist/inline-images.d.ts +28 -0
- package/dist/inline-images.d.ts.map +1 -0
- package/dist/message-authors.d.ts +34 -0
- package/dist/message-authors.d.ts.map +1 -0
- package/dist/message-times.d.ts +9 -0
- package/dist/message-times.d.ts.map +1 -0
- package/dist/model-adapters.d.ts +59 -0
- package/dist/model-adapters.d.ts.map +1 -0
- package/dist/model-lines.d.ts +16 -0
- package/dist/model-lines.d.ts.map +1 -0
- package/dist/prompt-cache.d.ts +28 -0
- package/dist/prompt-cache.d.ts.map +1 -0
- package/dist/requester.d.ts +15 -0
- package/dist/requester.d.ts.map +1 -0
- package/dist/run-agent-turn.d.ts +93 -0
- package/dist/run-agent-turn.d.ts.map +1 -0
- package/dist/run-child.d.ts +77 -0
- package/dist/run-child.d.ts.map +1 -0
- package/dist/run-conversation-turn.d.ts +42 -0
- package/dist/run-conversation-turn.d.ts.map +1 -0
- package/dist/run-credential.d.ts +22 -0
- package/dist/run-credential.d.ts.map +1 -0
- package/dist/run-environment.d.ts +145 -0
- package/dist/run-environment.d.ts.map +1 -0
- package/dist/run-fate.d.ts +15 -0
- package/dist/run-fate.d.ts.map +1 -0
- package/dist/run-files-lock.d.ts +16 -0
- package/dist/run-files-lock.d.ts.map +1 -0
- package/dist/run-sandbox.d.ts +60 -0
- package/dist/run-sandbox.d.ts.map +1 -0
- package/dist/run-subagent.d.ts +65 -0
- package/dist/run-subagent.d.ts.map +1 -0
- package/dist/run-workflow-agent-node.d.ts +46 -0
- package/dist/run-workflow-agent-node.d.ts.map +1 -0
- package/dist/sandbox-api-context.d.ts +34 -0
- package/dist/sandbox-api-context.d.ts.map +1 -0
- package/dist/sandbox-file-registry.d.ts +9 -0
- package/dist/sandbox-file-registry.d.ts.map +1 -0
- package/dist/sandbox-files.d.ts +71 -0
- package/dist/sandbox-files.d.ts.map +1 -0
- package/dist/sandbox-handle.d.ts +15 -0
- package/dist/sandbox-handle.d.ts.map +1 -0
- package/dist/sandbox-readiness.d.ts +8 -0
- package/dist/sandbox-readiness.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts +15 -0
- package/dist/skills/built-in.d.ts.map +1 -0
- package/dist/subagent-context.d.ts +50 -0
- package/dist/subagent-context.d.ts.map +1 -0
- package/dist/subagent-continuation.d.ts +33 -0
- package/dist/subagent-continuation.d.ts.map +1 -0
- package/dist/subagent-inbox.d.ts +53 -0
- package/dist/subagent-inbox.d.ts.map +1 -0
- package/dist/subagent-tools.d.ts +293 -0
- package/dist/subagent-tools.d.ts.map +1 -0
- package/dist/thread-context.d.ts +17 -0
- package/dist/thread-context.d.ts.map +1 -0
- package/dist/tools/attachments.d.ts +11 -0
- package/dist/tools/attachments.d.ts.map +1 -0
- package/dist/tools/bash.d.ts +68 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts +9 -0
- package/dist/tools/model-spec.d.ts.map +1 -0
- package/dist/tools/read.d.ts +43 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/result-media.d.ts +65 -0
- package/dist/tools/result-media.d.ts.map +1 -0
- package/dist/tools/result-output.d.ts +18 -0
- package/dist/tools/result-output.d.ts.map +1 -0
- package/dist/tools/tool-files.d.ts +59 -0
- package/dist/tools/tool-files.d.ts.map +1 -0
- package/dist/tools/tool-outputs.d.ts +63 -0
- package/dist/tools/tool-outputs.d.ts.map +1 -0
- package/dist/tools/view-file.d.ts +40 -0
- package/dist/tools/view-file.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/turn-runtime.d.ts +19 -0
- package/dist/turn-runtime.d.ts.map +1 -0
- package/dist/types.d.ts +83 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workflow-input-attachments.d.ts +18 -0
- package/dist/workflow-input-attachments.d.ts.map +1 -0
- package/package.json +59 -4
- package/src/abort.ts +40 -0
- package/src/agent-prompt.ts +860 -0
- package/src/agent-runtime/errors.ts +44 -0
- package/src/agent-runtime/preflight.ts +314 -0
- package/src/agent-runtime/profile.ts +23 -0
- package/src/artifacts/layout-check/check.cjs +478 -0
- package/src/artifacts/layout-check/layout-check.ts +460 -0
- package/src/artifacts/materialize.ts +380 -0
- package/src/artifacts/publish-tool.ts +700 -0
- package/src/ask-parent-tool.ts +100 -0
- package/src/attachments.ts +1041 -0
- package/src/child-files.ts +535 -0
- package/src/child-questions.ts +42 -0
- package/src/computer-runner.ts +336 -0
- package/src/context-budget.ts +177 -0
- package/src/context-compaction.ts +1349 -0
- package/src/context-guard.ts +826 -0
- package/src/conversation-memory.ts +45 -0
- package/src/conversation-messages.ts +86 -0
- package/src/errors.ts +85 -0
- package/src/execution-plan.ts +70 -0
- package/src/failure.ts +82 -0
- package/src/family-mailbox.ts +189 -0
- package/src/file-ref.ts +18 -0
- package/src/image-attachments.ts +322 -0
- package/src/index.ts +54 -0
- package/src/inline-images.ts +69 -0
- package/src/message-authors.ts +77 -0
- package/src/message-times.ts +24 -0
- package/src/model-adapters.ts +273 -0
- package/src/model-lines.ts +28 -0
- package/src/prompt-cache.ts +40 -0
- package/src/requester.ts +21 -0
- package/src/run-agent-turn.ts +509 -0
- package/src/run-child.ts +344 -0
- package/src/run-conversation-turn.ts +272 -0
- package/src/run-credential.ts +55 -0
- package/src/run-environment.ts +1216 -0
- package/src/run-fate.ts +51 -0
- package/src/run-files-lock.ts +118 -0
- package/src/run-sandbox.ts +63 -0
- package/src/run-subagent.ts +433 -0
- package/src/run-workflow-agent-node.ts +344 -0
- package/src/sandbox-api-context.ts +237 -0
- package/src/sandbox-file-registry.ts +26 -0
- package/src/sandbox-files.ts +326 -0
- package/src/sandbox-handle.ts +19 -0
- package/src/sandbox-readiness.ts +22 -0
- package/src/skills/built-in.ts +183 -0
- package/src/skills/sixb-artifacts/SKILL.md +95 -0
- package/src/skills/sixb-artifacts/canvas.md +104 -0
- package/src/skills/sixb-artifacts/document.md +63 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
- package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
- package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
- package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
- package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
- package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
- package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
- package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
- package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
- package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
- package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
- package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
- package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
- package/src/skills/sixb-artifacts/file.md +17 -0
- package/src/skills/sixb-artifacts/page.md +215 -0
- package/src/skills/sixb-artifacts/queries.md +66 -0
- package/src/skills/sixb-artifacts/references.md +68 -0
- package/src/skills/sixb-artifacts/sheet.md +223 -0
- package/src/skills/sixb-artifacts/slides.md +137 -0
- package/src/skills/sixb-memory/SKILL.md +68 -0
- package/src/skills/text-assets.d.ts +25 -0
- package/src/subagent-context.ts +182 -0
- package/src/subagent-continuation.ts +56 -0
- package/src/subagent-inbox.ts +101 -0
- package/src/subagent-tools.ts +1671 -0
- package/src/thread-context.ts +31 -0
- package/src/tools/attachments.ts +28 -0
- package/src/tools/bash.ts +233 -0
- package/src/tools/model-spec.ts +27 -0
- package/src/tools/read.ts +262 -0
- package/src/tools/result-media.ts +351 -0
- package/src/tools/result-output.ts +46 -0
- package/src/tools/tool-files.ts +204 -0
- package/src/tools/tool-outputs.ts +464 -0
- package/src/tools/view-file.ts +155 -0
- package/src/turn-runtime.ts +33 -0
- package/src/types.ts +91 -0
- package/src/workflow-input-attachments.ts +140 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AgentThreadModelContextMessage,
|
|
3
|
+
agentRunApprovalMode,
|
|
4
|
+
conversationThreadId,
|
|
5
|
+
shouldCompactAgentContext,
|
|
6
|
+
toModelMessages,
|
|
7
|
+
} from "@sixb/core/internal/agents"
|
|
8
|
+
import { createSixbError, withFailureMessage } from "@sixb/core/internal/errors"
|
|
9
|
+
import { type ModelCachingIntent, type ModelMessage, ModelProviderError } from "@sixb/core/models"
|
|
10
|
+
import type { AgentMessageRecord, AgentThreadRecord, SubagentRunRecord } from "@sixb/core/storage"
|
|
11
|
+
import {
|
|
12
|
+
attachmentKey,
|
|
13
|
+
type PreparedAgentAttachmentContext,
|
|
14
|
+
toolResultAttachmentKey,
|
|
15
|
+
} from "./attachments"
|
|
16
|
+
import {
|
|
17
|
+
type AgentThreadCompactionInput,
|
|
18
|
+
compactAgentThreadInRun,
|
|
19
|
+
estimateAgentConversation,
|
|
20
|
+
inRunCompactedTokens,
|
|
21
|
+
shouldCompactIdleContext,
|
|
22
|
+
} from "./context-compaction"
|
|
23
|
+
import { childCacheLifetime, conversationPromptCaching } from "./prompt-cache"
|
|
24
|
+
import type { SubagentHistory } from "./subagent-continuation"
|
|
25
|
+
import { agentThreadModelContext, type LoadedAgentThreadModelContext } from "./thread-context"
|
|
26
|
+
import { toolFilesStepKey } from "./tools/result-media"
|
|
27
|
+
|
|
28
|
+
// A child keeps its calls under its model's limit as a conversation's turn does (see
|
|
29
|
+
// createAgentContextGuard): its thread is its history, its task apart. Every request opens with
|
|
30
|
+
// the task as it was sent, then the thread: from its latest checkpoint's summary once it has one.
|
|
31
|
+
// A run that continues the thread compacts what came before the message it started from, on the
|
|
32
|
+
// child's own thread, before its environment is built, as a conversation's preflight does: past
|
|
33
|
+
// the limit, or idle and large. Its own steps are cleared, never summarized. A first run has
|
|
34
|
+
// nothing before its task, so it only clears.
|
|
35
|
+
|
|
36
|
+
/** A child's thread as its requests send it: its task, and the history after it. */
|
|
37
|
+
export interface SubagentThreadContext {
|
|
38
|
+
/** The thread's first message, which every request opens with. */
|
|
39
|
+
readonly task?: AgentMessageRecord
|
|
40
|
+
/** The thread without its task: from the latest checkpoint's summary once it has one. */
|
|
41
|
+
readonly threadContext: LoadedAgentThreadModelContext
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Split a child's history into its task and the rest of its thread. */
|
|
45
|
+
export function subagentThreadContext(history: SubagentHistory): SubagentThreadContext {
|
|
46
|
+
// Until a checkpoint summarized it, the task is the thread's first message.
|
|
47
|
+
const task = history.task ?? (history.checkpoint ? undefined : history.messages[0])
|
|
48
|
+
const messages = history.checkpoint ? history.messages : history.messages.slice(1)
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
...(task ? { task } : {}),
|
|
52
|
+
threadContext: agentThreadModelContext({ ...history, messages }, { historyReadable: false }),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The child's thread as its model reads it: the task, closed by the time the thread was created
|
|
58
|
+
* so each run of the child replays the same bytes, then the rest. Files a tool showed it are
|
|
59
|
+
* projected again as a turn projects its own.
|
|
60
|
+
*/
|
|
61
|
+
export function subagentModelMessages(input: {
|
|
62
|
+
readonly thread: SubagentThreadContext
|
|
63
|
+
readonly attachmentContext?: PreparedAgentAttachmentContext
|
|
64
|
+
/** When the thread was created, as the task closes with it. */
|
|
65
|
+
readonly createdAt: string
|
|
66
|
+
}): ModelMessage[] {
|
|
67
|
+
const { task, threadContext } = input.thread
|
|
68
|
+
const projections = input.attachmentContext
|
|
69
|
+
const byMessage = <T>(
|
|
70
|
+
message: AgentThreadModelContextMessage,
|
|
71
|
+
read: (messageId: string) => T | undefined
|
|
72
|
+
): T | undefined => (message.id ? read(message.id) : undefined)
|
|
73
|
+
|
|
74
|
+
return toModelMessages([...(task ? [task] : []), ...threadContext.modelMessages], {
|
|
75
|
+
userMessageSuffix: (message) => (task && message.id === task.id ? input.createdAt : undefined),
|
|
76
|
+
fileText: ({ message, partIndex }) =>
|
|
77
|
+
byMessage(message, (id) =>
|
|
78
|
+
projections?.promptTextByPartKey.get(attachmentKey(id, partIndex))
|
|
79
|
+
),
|
|
80
|
+
fileData: ({ message, partIndex }) =>
|
|
81
|
+
byMessage(message, (id) =>
|
|
82
|
+
projections?.modelFileDataByPartKey.get(attachmentKey(id, partIndex))
|
|
83
|
+
),
|
|
84
|
+
toolResultFileText: ({ message, partIndex, contentIndex }) =>
|
|
85
|
+
byMessage(message, (id) =>
|
|
86
|
+
projections?.promptTextByPartKey.get(toolResultAttachmentKey(id, partIndex, contentIndex))
|
|
87
|
+
),
|
|
88
|
+
afterToolResults: ({ message, partIndex }) =>
|
|
89
|
+
byMessage(message, (id) =>
|
|
90
|
+
projections?.toolFilesByStep?.get(toolFilesStepKey(id, partIndex))
|
|
91
|
+
),
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** How a child's calls are cached, its summary's included (see childCacheLifetime). */
|
|
96
|
+
export function subagentPromptCaching(input: {
|
|
97
|
+
readonly projectId: string
|
|
98
|
+
readonly thread: AgentThreadRecord
|
|
99
|
+
readonly run: SubagentRunRecord
|
|
100
|
+
readonly history: SubagentHistory
|
|
101
|
+
}): ModelCachingIntent {
|
|
102
|
+
const { run, history } = input
|
|
103
|
+
return conversationPromptCaching({
|
|
104
|
+
projectId: input.projectId,
|
|
105
|
+
threadId: conversationThreadId(input.thread),
|
|
106
|
+
lifetime: childCacheLifetime({
|
|
107
|
+
runsEveryCall: agentRunApprovalMode(run) === "auto",
|
|
108
|
+
// A run that continues the thread finds an earlier run's messages there, or their summary.
|
|
109
|
+
continues:
|
|
110
|
+
history.checkpoint !== null || history.messages.some((message) => message.runId !== run.id),
|
|
111
|
+
}),
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Weigh a child's thread before its first call, and compact it when it crosses the model's limit,
|
|
117
|
+
* or when it comes back idle and large (see shouldCompactIdleContext): what the run then starts
|
|
118
|
+
* from, and the bytes per token the thread was measured at.
|
|
119
|
+
*/
|
|
120
|
+
export async function preflightSubagentContext(
|
|
121
|
+
input: AgentThreadCompactionInput & { readonly threadContext: LoadedAgentThreadModelContext }
|
|
122
|
+
): Promise<{
|
|
123
|
+
readonly threadContext: LoadedAgentThreadModelContext
|
|
124
|
+
readonly bytesPerToken: number
|
|
125
|
+
}> {
|
|
126
|
+
const { plan, budget, threadContext } = input
|
|
127
|
+
const estimateShape = input.estimateShape(threadContext, undefined)
|
|
128
|
+
const estimate = estimateAgentConversation({ plan, budget, threadContext, ...estimateShape })
|
|
129
|
+
const overLimit = shouldCompactAgentContext({
|
|
130
|
+
estimatedInputTokens: estimate.tokens,
|
|
131
|
+
inputBudgetTokens: budget.inputBudgetTokens,
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
// The same rule as a conversation's turn: compacting brings an idle thread under its size.
|
|
135
|
+
const idle =
|
|
136
|
+
!overLimit &&
|
|
137
|
+
shouldCompactIdleContext({
|
|
138
|
+
history: threadContext.history,
|
|
139
|
+
caching: input.caching,
|
|
140
|
+
estimatedInputTokens: estimate.tokens,
|
|
141
|
+
compactedTokens: inRunCompactedTokens({
|
|
142
|
+
plan,
|
|
143
|
+
budget,
|
|
144
|
+
threadContext,
|
|
145
|
+
estimateShape,
|
|
146
|
+
bytesPerToken: estimate.bytesPerToken,
|
|
147
|
+
}),
|
|
148
|
+
})
|
|
149
|
+
if (!overLimit && !idle) return { threadContext, bytesPerToken: estimate.bytesPerToken }
|
|
150
|
+
|
|
151
|
+
const compacted = await compactAgentThreadInRun({
|
|
152
|
+
...input,
|
|
153
|
+
memory: undefined,
|
|
154
|
+
reason: "threshold",
|
|
155
|
+
// Below the limit, the thread compacts because it came back idle: its cache has gone cold.
|
|
156
|
+
coldCache: !overLimit,
|
|
157
|
+
})
|
|
158
|
+
return {
|
|
159
|
+
threadContext: compacted?.threadContext ?? threadContext,
|
|
160
|
+
bytesPerToken: estimate.bytesPerToken,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The failure of a child whose call its guard reduced and the provider still refused as too long
|
|
166
|
+
* for the window: what its parent can do about it. Undefined for any other error.
|
|
167
|
+
*/
|
|
168
|
+
export function subagentContextFailure(error: unknown, run: SubagentRunRecord): Error | undefined {
|
|
169
|
+
if (!(error instanceof ModelProviderError) || error.reason !== "context-window-exceeded") {
|
|
170
|
+
return undefined
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const remedy = "Spawn it again with a narrower task, or a model with a larger context window."
|
|
174
|
+
return withFailureMessage(
|
|
175
|
+
createSixbError(
|
|
176
|
+
"agent.execution_failed",
|
|
177
|
+
`[SixbAgent] Subagent run '${run.id}' still exceeds its model's context window after its context was reduced. ${remedy}`,
|
|
178
|
+
{ cause: error, details: { parentRunId: run.parentRunId, runId: run.id } }
|
|
179
|
+
),
|
|
180
|
+
`The subagent's context exceeds its model's context window, even reduced. ${remedy}`
|
|
181
|
+
)
|
|
182
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AgentMessagePart } from "@sixb/core"
|
|
2
|
+
import type { AgentTurnHistory } from "@sixb/core/internal/agents"
|
|
3
|
+
import type { AgentMessageRecord, SubagentRunRecord } from "@sixb/core/storage"
|
|
4
|
+
import { renderCurrentTime, type SentSettings } from "./agent-prompt"
|
|
5
|
+
import { mailPart } from "./subagent-inbox"
|
|
6
|
+
|
|
7
|
+
// A child that paused continues as the next run of its thread under a later turn: once its held
|
|
8
|
+
// calls are decided, in the turn that resumes, or once its parent answers the question it paused
|
|
9
|
+
// on, in the turn the parent answers it. The same key, model and task. Its history is its thread,
|
|
10
|
+
// and what it goes on with is the message that starts the run.
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What a child run reads of its thread (`AgentChildContext`): its latest checkpoint and the
|
|
14
|
+
* messages after it, its task when the checkpoint summarized it, and the paused run it continues.
|
|
15
|
+
*/
|
|
16
|
+
export interface SubagentHistory extends AgentTurnHistory {
|
|
17
|
+
/** The thread's first message, when a checkpoint summarized it: its requests keep it as it is. */
|
|
18
|
+
readonly task?: AgentMessageRecord
|
|
19
|
+
/** The run of the thread that paused on its held calls, when this run continues it. */
|
|
20
|
+
readonly paused?: SubagentRunRecord
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The message a continuation starts with: how each of its calls ended, as its own, or its parent's
|
|
25
|
+
* answer to its question; the messages its parent sent that the paused run never read; what came
|
|
26
|
+
* back to its new sandbox, and when it continues. It stays in
|
|
27
|
+
* the thread, so a later continuation replays the same bytes.
|
|
28
|
+
*
|
|
29
|
+
* A child whose workspace is kept finds it as it left it, and is told nothing about it here. If
|
|
30
|
+
* its files turn out lost, its system prompt says so, as a turn's does.
|
|
31
|
+
*/
|
|
32
|
+
export function continuationTrigger(input: {
|
|
33
|
+
/** The outcomes as the child reads them, without their caller, or the answer's `mail` part. */
|
|
34
|
+
readonly goesOnWith: readonly AgentMessagePart[]
|
|
35
|
+
readonly paused: SubagentRunRecord
|
|
36
|
+
/** Whether the sandbox provider keeps the child's workspace while it pauses. */
|
|
37
|
+
readonly keepsWorkspace: boolean
|
|
38
|
+
readonly createdAt: Date
|
|
39
|
+
readonly settings: SentSettings
|
|
40
|
+
}): readonly AgentMessagePart[] {
|
|
41
|
+
return [
|
|
42
|
+
...input.goesOnWith,
|
|
43
|
+
...(input.paused.result?.unread ?? []).map((mail) => mailPart(mail)),
|
|
44
|
+
...(input.keepsWorkspace
|
|
45
|
+
? []
|
|
46
|
+
: [{ type: "text" as const, text: sandboxRestarted(input.paused) }]),
|
|
47
|
+
{ type: "text", text: `\n\n${renderCurrentTime(input.createdAt, input.settings)}` },
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function sandboxRestarted(paused: SubagentRunRecord): string {
|
|
52
|
+
const outputsError = paused.result?.filesError
|
|
53
|
+
return outputsError === undefined
|
|
54
|
+
? "Your sandbox restarted: the files you were handed and your out/ are back; nothing else is."
|
|
55
|
+
: `Your sandbox restarted: the files you were handed are back, but not your out/, which could not be kept (${outputsError}). Nothing else is.`
|
|
56
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { type AgentMailPart, type AgentMessage, AgentToolPublicError } from "@sixb/core"
|
|
2
|
+
import type { SubagentRunRecord } from "@sixb/core/storage"
|
|
3
|
+
import type { QuestionRef } from "./child-questions"
|
|
4
|
+
import type { ChildMailbox, UnreadChildMail } from "./family-mailbox"
|
|
5
|
+
|
|
6
|
+
/** Messages the child read, and where it read them: after how many of its steps. */
|
|
7
|
+
export interface SubagentDelivery {
|
|
8
|
+
readonly afterSteps: number
|
|
9
|
+
readonly parts: readonly AgentMailPart[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** How its parent replied to the child's question: its answer, or none before its turn ended. */
|
|
13
|
+
export type ParentReply =
|
|
14
|
+
| { readonly kind: "answer"; readonly text: string }
|
|
15
|
+
| { readonly kind: "deferred"; readonly questionId: string }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What a child's parent sent it, read at each of its step boundaries (`take`) from its side of the
|
|
19
|
+
* turn's mailbox, and the questions it asks its parent (`ask`). What it read is the child's to
|
|
20
|
+
* record: its thread keeps each delivery where the model read it, and each question as its tool
|
|
21
|
+
* call. What it never read when it pauses is returned by `close`, for the run that continues it.
|
|
22
|
+
*/
|
|
23
|
+
export class SubagentInbox {
|
|
24
|
+
readonly #deliveries: SubagentDelivery[] = []
|
|
25
|
+
/** The question waiting for its reply: one at a time. */
|
|
26
|
+
#asking: string | undefined
|
|
27
|
+
#deferred: QuestionRef | undefined
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private readonly mailbox: ChildMailbox,
|
|
31
|
+
private readonly run: SubagentRunRecord
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
/** What the child read, in order. */
|
|
35
|
+
get deliveries(): readonly SubagentDelivery[] {
|
|
36
|
+
return this.#deliveries
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The question its parent's turn ended without answering: the child pauses on it. */
|
|
40
|
+
get deferred(): QuestionRef | undefined {
|
|
41
|
+
return this.#deferred
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Ask the parent and wait for its reply, bounded by `signal`: the child's own cancel and its
|
|
46
|
+
* parent's. Its id names the child's model call and tool call, so the parent lists it once.
|
|
47
|
+
*/
|
|
48
|
+
async ask(question: string, callId: string, signal: AbortSignal): Promise<ParentReply> {
|
|
49
|
+
if (this.#asking !== undefined) {
|
|
50
|
+
throw new AgentToolPublicError(
|
|
51
|
+
"[SixbAgent] Ask one question at a time: your parent has not answered the last one yet."
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
const id = `${this.run.id}:${callId}`
|
|
55
|
+
this.#asking = id
|
|
56
|
+
try {
|
|
57
|
+
const reply = await this.mailbox.ask({ id, text: question }, signal)
|
|
58
|
+
if (reply.kind === "answer") return { kind: "answer", text: reply.text }
|
|
59
|
+
this.#deferred = { id, text: question }
|
|
60
|
+
return { kind: "deferred", questionId: id }
|
|
61
|
+
} finally {
|
|
62
|
+
this.#asking = undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The messages that came since the last read, as the user message the child reads after
|
|
68
|
+
* `afterSteps` of its steps; none when nothing came. `afterAnswer` when the child just answered:
|
|
69
|
+
* it is told to write its result again in full.
|
|
70
|
+
*/
|
|
71
|
+
take(
|
|
72
|
+
afterSteps: number,
|
|
73
|
+
options: { readonly afterAnswer?: boolean } = {}
|
|
74
|
+
): AgentMessage | undefined {
|
|
75
|
+
const parts = this.mailbox.take().map((mail) => mailPart(mail, options))
|
|
76
|
+
if (parts.length === 0) return undefined
|
|
77
|
+
|
|
78
|
+
this.#deliveries.push({ afterSteps, parts })
|
|
79
|
+
return { role: "user", parts }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Stop reading, as the child stops: what it never read, for the run that continues it. */
|
|
83
|
+
close(): readonly UnreadChildMail[] {
|
|
84
|
+
return this.mailbox.close()
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A message from the parent, as the child's thread keeps it where the child read it. */
|
|
89
|
+
export function mailPart(
|
|
90
|
+
mail: UnreadChildMail,
|
|
91
|
+
options: { readonly afterAnswer?: boolean } = {}
|
|
92
|
+
): AgentMailPart {
|
|
93
|
+
return {
|
|
94
|
+
type: "mail",
|
|
95
|
+
id: mail.id,
|
|
96
|
+
from: "parent",
|
|
97
|
+
kind: "message",
|
|
98
|
+
text: mail.text,
|
|
99
|
+
...(options.afterAnswer ? { afterAnswer: true as const } : {}),
|
|
100
|
+
}
|
|
101
|
+
}
|