@sixb/agent 0.0.0-stage → 0.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/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 +120 -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 +68 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/child-files.d.ts +96 -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 +65 -0
- package/dist/computer-runner.d.ts.map +1 -0
- package/dist/context-budget.d.ts +28 -0
- package/dist/context-budget.d.ts.map +1 -0
- package/dist/context-compaction.d.ts +48 -0
- package/dist/context-compaction.d.ts.map +1 -0
- package/dist/conversation-memory.d.ts +21 -0
- package/dist/conversation-memory.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 +34 -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 +12168 -0
- package/dist/index.js.map +65 -0
- package/dist/message-authors.d.ts +34 -0
- package/dist/message-authors.d.ts.map +1 -0
- package/dist/message-times.d.ts +8 -0
- package/dist/message-times.d.ts.map +1 -0
- package/dist/model-adapters.d.ts +55 -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/requester.d.ts +15 -0
- package/dist/requester.d.ts.map +1 -0
- package/dist/run-agent-turn.d.ts +63 -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 +115 -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-sandbox.d.ts +60 -0
- package/dist/run-sandbox.d.ts.map +1 -0
- package/dist/run-subagent.d.ts +51 -0
- package/dist/run-subagent.d.ts.map +1 -0
- package/dist/run-workflow-agent-node.d.ts +41 -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-continuation.d.ts +27 -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 +282 -0
- package/dist/subagent-tools.d.ts.map +1 -0
- package/dist/thread-context.d.ts +12 -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 +60 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts +4 -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 +22 -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/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 +76 -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 +758 -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 +850 -0
- package/src/child-files.ts +540 -0
- package/src/child-questions.ts +42 -0
- package/src/computer-runner.ts +284 -0
- package/src/context-budget.ts +161 -0
- package/src/context-compaction.ts +529 -0
- package/src/conversation-memory.ts +45 -0
- package/src/errors.ts +85 -0
- package/src/execution-plan.ts +50 -0
- package/src/failure.ts +71 -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/message-authors.ts +77 -0
- package/src/message-times.ts +21 -0
- package/src/model-adapters.ts +227 -0
- package/src/model-lines.ts +28 -0
- package/src/requester.ts +21 -0
- package/src/run-agent-turn.ts +347 -0
- package/src/run-child.ts +319 -0
- package/src/run-conversation-turn.ts +257 -0
- package/src/run-credential.ts +55 -0
- package/src/run-environment.ts +769 -0
- package/src/run-fate.ts +51 -0
- package/src/run-sandbox.ts +63 -0
- package/src/run-subagent.ts +347 -0
- package/src/run-workflow-agent-node.ts +270 -0
- package/src/sandbox-api-context.ts +234 -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-continuation.ts +51 -0
- package/src/subagent-inbox.ts +101 -0
- package/src/subagent-tools.ts +1609 -0
- package/src/thread-context.ts +25 -0
- package/src/tools/attachments.ts +28 -0
- package/src/tools/bash.ts +217 -0
- package/src/tools/model-spec.ts +22 -0
- package/src/tools/read.ts +262 -0
- package/src/tools/result-media.ts +147 -0
- package/src/tools/result-output.ts +44 -0
- package/src/tools/tool-files.ts +204 -0
- package/src/tools/view-file.ts +155 -0
- package/src/turn-runtime.ts +33 -0
- package/src/types.ts +79 -0
- package/src/workflow-input-attachments.ts +140 -0
package/src/run-fate.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgentExecutionLostError,
|
|
3
|
+
type AgentRunSession,
|
|
4
|
+
AgentTurnInterruptedError,
|
|
5
|
+
} from "@sixb/core/internal/agents"
|
|
6
|
+
import type { AgentRunRecord, AppendAgentMessageInput } from "@sixb/core/storage"
|
|
7
|
+
import { type AgentRunFailure, toAgentExecutionFailure } from "./failure"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* End a run that failed or stopped, with what it did kept in the same transaction. Returns
|
|
11
|
+
* nothing when the run is no longer this session's; throws {@link AgentFinalizationError} when
|
|
12
|
+
* storage stays unavailable, so the job is redelivered rather than acknowledged.
|
|
13
|
+
*/
|
|
14
|
+
export async function recordFate(
|
|
15
|
+
session: AgentRunSession,
|
|
16
|
+
status: "failed" | "cancelled",
|
|
17
|
+
error: unknown,
|
|
18
|
+
messages?: readonly AppendAgentMessageInput[]
|
|
19
|
+
): Promise<{ readonly run: AgentRunRecord; readonly failure: AgentRunFailure } | undefined> {
|
|
20
|
+
const { run } = session
|
|
21
|
+
const completedAt = new Date()
|
|
22
|
+
const failure = toAgentExecutionFailure(error, {
|
|
23
|
+
status,
|
|
24
|
+
at: completedAt,
|
|
25
|
+
details: agentRunFailureDetails(run),
|
|
26
|
+
})
|
|
27
|
+
try {
|
|
28
|
+
const finalized = await session.finish(
|
|
29
|
+
{
|
|
30
|
+
status,
|
|
31
|
+
error: failure,
|
|
32
|
+
completedAt,
|
|
33
|
+
...(error instanceof AgentTurnInterruptedError
|
|
34
|
+
? { finishReason: "interrupted" as const }
|
|
35
|
+
: {}),
|
|
36
|
+
},
|
|
37
|
+
messages
|
|
38
|
+
)
|
|
39
|
+
return { run: finalized, failure }
|
|
40
|
+
} catch (finishError) {
|
|
41
|
+
if (finishError instanceof AgentExecutionLostError) return undefined
|
|
42
|
+
throw finishError
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What a run's failure names: its run, and its thread or its parent. */
|
|
47
|
+
export function agentRunFailureDetails(run: AgentRunRecord): Readonly<Record<string, string>> {
|
|
48
|
+
return run.kind === "conversation"
|
|
49
|
+
? { runId: run.id, threadId: run.threadId }
|
|
50
|
+
: { parentRunId: run.parentRunId, runId: run.id }
|
|
51
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Sandbox } from "@sixb/core"
|
|
2
|
+
import type { AgentWorkspacePromptContext } from "./agent-prompt"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where a provider's sandbox keeps a run's files: in its working directory, which a Git checkout
|
|
6
|
+
* excludes (see `prepareWorkspaceRunFiles`).
|
|
7
|
+
*/
|
|
8
|
+
export { SANDBOX_CONTEXT_DIRECTORY } from "@sixb/core/internal/agents"
|
|
9
|
+
|
|
10
|
+
/** The sandbox a run works in, with what the system prompt may say about it. */
|
|
11
|
+
export interface AcquiredRunSandbox {
|
|
12
|
+
readonly sandbox: Sandbox
|
|
13
|
+
/** The workspace's environment; provisioning adds the run's. */
|
|
14
|
+
readonly env?: Readonly<Record<string, string>>
|
|
15
|
+
/** Facts about a prepared workspace; absent for a blank sandbox. */
|
|
16
|
+
readonly promptContext?: AgentWorkspacePromptContext
|
|
17
|
+
/** When the workspace was recreated and its earlier files were lost. */
|
|
18
|
+
readonly resetAt?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How one run obtains its sandbox and gives it back. Every run kind goes through this lifecycle,
|
|
23
|
+
* with one of two behaviors:
|
|
24
|
+
*
|
|
25
|
+
* - ephemeral (`ephemeralRunSandbox`): created for this run, blank or prepared from a recipe, and
|
|
26
|
+
* destroyed when the run releases it;
|
|
27
|
+
* - persistent (`persistentRunSandbox`): a thread's workspace, resumed for this run and saved by
|
|
28
|
+
* `beforeFinalize`. It has no `release`: only a sub-agent run that ends without pausing
|
|
29
|
+
* destroys it.
|
|
30
|
+
*
|
|
31
|
+
* Acquisition may happen before `acquire()`: a thread's workspace is acquired before compaction,
|
|
32
|
+
* so `acquire()` only hands it over. Preservation belongs to `beforeFinalize`, never `release`.
|
|
33
|
+
*/
|
|
34
|
+
export interface RunSandbox {
|
|
35
|
+
/**
|
|
36
|
+
* Whether the first model call waits for acquisition. A workspace the prompt describes must
|
|
37
|
+
* exist first; a blank sandbox boots concurrently and tools wait for it only when used.
|
|
38
|
+
*/
|
|
39
|
+
readonly waitBeforeModel: boolean
|
|
40
|
+
/**
|
|
41
|
+
* Where the run's files go, known before acquisition so the prompt can name them. As the agent
|
|
42
|
+
* names it: relative to the working directory, or absolute.
|
|
43
|
+
*/
|
|
44
|
+
readonly contextDirectory: string
|
|
45
|
+
/** Called once, when the run environment starts. */
|
|
46
|
+
acquire(): Promise<AcquiredRunSandbox>
|
|
47
|
+
/**
|
|
48
|
+
* Give back an acquired sandbox, after the run or a failed provisioning. Absent when the run
|
|
49
|
+
* has nothing to give back, so teardown never waits on it.
|
|
50
|
+
*/
|
|
51
|
+
release?(acquired: AcquiredRunSandbox): Promise<void>
|
|
52
|
+
/** Confirm preservation before the run's terminal write. Idempotent: the first call decides. */
|
|
53
|
+
beforeFinalize?(options?: BeforeFinalizeOptions): Promise<void>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** How a run's end leaves its thread's workspace. */
|
|
57
|
+
export interface BeforeFinalizeOptions {
|
|
58
|
+
/**
|
|
59
|
+
* The run leaves nothing its thread goes on from, as a sub-agent run that answered, failed or
|
|
60
|
+
* was stopped: destroy its workspace instead of saving it.
|
|
61
|
+
*/
|
|
62
|
+
readonly discard?: boolean
|
|
63
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import type { AgentMessagePart } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
type AgentRunSession,
|
|
4
|
+
createAgentMessageId,
|
|
5
|
+
type PausableModelLoopResult,
|
|
6
|
+
runModelLoop,
|
|
7
|
+
toModelMessages,
|
|
8
|
+
} from "@sixb/core/internal/agents"
|
|
9
|
+
import { createSixbError, withFailureMessage } from "@sixb/core/internal/errors"
|
|
10
|
+
import type { ModelMessage, ModelStep } from "@sixb/core/models"
|
|
11
|
+
import {
|
|
12
|
+
type AgentRunFinishReason,
|
|
13
|
+
type AgentThreadRecord,
|
|
14
|
+
type AppendAgentMessageInput,
|
|
15
|
+
coerceAgentRunFinishReason,
|
|
16
|
+
type SubagentRunRecord,
|
|
17
|
+
} from "@sixb/core/storage"
|
|
18
|
+
import { renderCurrentTime } from "./agent-prompt"
|
|
19
|
+
import { unansweredQuestion } from "./ask-parent-tool"
|
|
20
|
+
import { attachmentKey, toolResultAttachmentKey } from "./attachments"
|
|
21
|
+
import { storeChildOutputs } from "./child-files"
|
|
22
|
+
import { throwIfExecutionLost } from "./errors"
|
|
23
|
+
import type { ResolvedAgentExecutionPlan } from "./execution-plan"
|
|
24
|
+
import { agentTraceFromModelSteps, agentTraceFromPartialModelLoop } from "./model-adapters"
|
|
25
|
+
import { monitorSandboxReadiness } from "./sandbox-readiness"
|
|
26
|
+
import type { SubagentHistory } from "./subagent-continuation"
|
|
27
|
+
import type { SubagentDelivery, SubagentInbox } from "./subagent-inbox"
|
|
28
|
+
import type { AgentTurnRuntime } from "./turn-runtime"
|
|
29
|
+
import type { AgentRunFiles, AgentTurnContext } from "./types"
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What a child did so far, kept in its thread however its run ends: a failed or stopped child
|
|
33
|
+
* keeps it as an interrupted turn does. Its steps, and each message its parent sent it where it
|
|
34
|
+
* read it.
|
|
35
|
+
*/
|
|
36
|
+
export interface SubagentWork {
|
|
37
|
+
messages?: readonly AppendAgentMessageInput[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Run one child in its thread. Its request is the thread's messages, the task dated at the thread's
|
|
42
|
+
* creation; what it did is the thread's next message, written with its finish. A held call stops it
|
|
43
|
+
* after that step: it pauses, and its thread's next run continues it once the call is decided.
|
|
44
|
+
*/
|
|
45
|
+
export async function runSubagent(input: {
|
|
46
|
+
readonly context: AgentTurnContext
|
|
47
|
+
readonly plan: ResolvedAgentExecutionPlan
|
|
48
|
+
readonly run: SubagentRunRecord
|
|
49
|
+
/** The child's session: its finish is fenced by the run's execution token. */
|
|
50
|
+
readonly session: AgentRunSession
|
|
51
|
+
/** The child's own thread, and what its runs before this one left there. */
|
|
52
|
+
readonly thread: AgentThreadRecord
|
|
53
|
+
readonly history: SubagentHistory
|
|
54
|
+
/** Created by the worker before the run's tools, which share its signal and accounting. */
|
|
55
|
+
readonly runtime: AgentTurnRuntime
|
|
56
|
+
/** Filled once the model loop returns, for the worker to keep when the run fails afterwards. */
|
|
57
|
+
readonly work?: SubagentWork
|
|
58
|
+
/** Where it finds the files its parent handed over, and leaves the ones it returns. */
|
|
59
|
+
readonly runFiles: AgentRunFiles
|
|
60
|
+
/**
|
|
61
|
+
* What its parent sends it while it runs, read before its next step, and the questions it asks,
|
|
62
|
+
* through the inbox's `ask_parent` among its tools.
|
|
63
|
+
*/
|
|
64
|
+
readonly inbox: SubagentInbox
|
|
65
|
+
}): Promise<SubagentRunRecord> {
|
|
66
|
+
const { context, plan, run, thread, runtime, inbox } = input
|
|
67
|
+
const messages = subagentModelMessages({
|
|
68
|
+
context,
|
|
69
|
+
thread,
|
|
70
|
+
history: input.history,
|
|
71
|
+
timeZone: plan.timeZone ?? context.projectTimeZone,
|
|
72
|
+
})
|
|
73
|
+
const sandboxReadiness = monitorSandboxReadiness(context.sandboxReady)
|
|
74
|
+
const abortSignal = AbortSignal.any([runtime.signal, sandboxReadiness.signal])
|
|
75
|
+
let chunkIndex = 0
|
|
76
|
+
const generation = await runModelLoop({
|
|
77
|
+
model: plan.model,
|
|
78
|
+
messages: [{ role: "system", content: context.systemPrompt }, ...messages],
|
|
79
|
+
tools: context.tools,
|
|
80
|
+
...(plan.reasoning === undefined ? {} : { reasoning: plan.reasoning }),
|
|
81
|
+
// A message from the parent comes after the step that was running when it arrived, and stays
|
|
82
|
+
// in the history of every later step.
|
|
83
|
+
prepareStep: async (step) => {
|
|
84
|
+
const prepared = await context.prepareStep?.(step)
|
|
85
|
+
const mail = await inbox.take(step.stepIndex)
|
|
86
|
+
if (!mail) return prepared
|
|
87
|
+
|
|
88
|
+
return { messages: [...(prepared?.messages ?? step.messages), ...toModelMessages([mail])] }
|
|
89
|
+
},
|
|
90
|
+
// One that arrives as the child answers is read too: it writes its result again, after it.
|
|
91
|
+
beforeComplete: async (_step, stepIndex) => {
|
|
92
|
+
const mail = await inbox.take(stepIndex + 1, { afterAnswer: true })
|
|
93
|
+
return mail ? toModelMessages([mail]) : undefined
|
|
94
|
+
},
|
|
95
|
+
signal: abortSignal,
|
|
96
|
+
onEvent: (chunk) =>
|
|
97
|
+
input.session.events.publishUiChunk({ run, chunkIndex: chunkIndex++, chunk }),
|
|
98
|
+
// The approval records decide, never the prompt or the tool output: a held call stops the
|
|
99
|
+
// child after that step, whatever its model would do next. So does a question its parent's
|
|
100
|
+
// turn ended without answering.
|
|
101
|
+
pauseAfterStep: async () =>
|
|
102
|
+
inbox.deferred !== undefined || (await input.session.heldCalls.holdsRun()),
|
|
103
|
+
})
|
|
104
|
+
const work = subagentWorkMessages(generation, run, inbox.deliveries)
|
|
105
|
+
if (input.work) input.work.messages = work
|
|
106
|
+
|
|
107
|
+
return settleSubagent({
|
|
108
|
+
...input,
|
|
109
|
+
generation,
|
|
110
|
+
work,
|
|
111
|
+
abortSignal,
|
|
112
|
+
sandboxReadiness,
|
|
113
|
+
lastMessageAt: inbox.deliveries.at(-1)?.afterSteps ?? 0,
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** End a child whose model loop returned: paused on its held calls, answered, or failed. */
|
|
118
|
+
async function settleSubagent(
|
|
119
|
+
input: Omit<Parameters<typeof finishSubagent>[0], "finishReason" | "text"> & {
|
|
120
|
+
readonly generation: PausableModelLoopResult
|
|
121
|
+
readonly sandboxReadiness: ReturnType<typeof monitorSandboxReadiness>
|
|
122
|
+
/** After how many steps it read its parent's last message. */
|
|
123
|
+
readonly lastMessageAt: number
|
|
124
|
+
}
|
|
125
|
+
): Promise<SubagentRunRecord> {
|
|
126
|
+
const { generation, run } = input
|
|
127
|
+
assertSubagentCanFinalize(input)
|
|
128
|
+
if (generation.status === "aborted")
|
|
129
|
+
throw new DOMException("Subagent stream was aborted.", "AbortError")
|
|
130
|
+
|
|
131
|
+
// A call held after its last step, by a command left running in the sandbox, pauses it too. A
|
|
132
|
+
// held call wins over a question: only a decision continues it, and it can ask again then.
|
|
133
|
+
const asked = input.inbox.deferred !== undefined
|
|
134
|
+
const held =
|
|
135
|
+
(generation.status === "paused" && !asked) || (await input.session.heldCalls.holdsRun())
|
|
136
|
+
if (held) {
|
|
137
|
+
// Its question then goes unanswered: the decision continues it.
|
|
138
|
+
const work = asked ? unansweredQuestion(input.work) : input.work
|
|
139
|
+
return finishSubagent({ ...input, work, finishReason: "awaiting_approval" })
|
|
140
|
+
}
|
|
141
|
+
if (generation.status === "paused" || asked) {
|
|
142
|
+
return finishSubagent({ ...input, finishReason: "awaiting_answer" })
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// The parent would read a cut-short answer as complete: it gets the cause instead.
|
|
146
|
+
if (generation.finishReason === "length") {
|
|
147
|
+
const remedy = "Spawn it again with lower reasoning or a narrower task."
|
|
148
|
+
throw withFailureMessage(
|
|
149
|
+
createSixbError(
|
|
150
|
+
"agent.execution_failed",
|
|
151
|
+
`[SixbAgent] Subagent run '${run.id}' reached the model's output limit before finishing its answer. ${remedy}`,
|
|
152
|
+
{ details: { parentRunId: run.parentRunId, runId: run.id } }
|
|
153
|
+
),
|
|
154
|
+
`The subagent's model reached its output limit before finishing its answer. ${remedy}`
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
return finishSubagent({
|
|
158
|
+
...input,
|
|
159
|
+
text: subagentResult(generation.steps, run, input.lastMessageAt),
|
|
160
|
+
finishReason: coerceAgentRunFinishReason(generation.finishReason) ?? "unknown",
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Keep the child's outcome: its answer, what it wrote under `out/`, and what it did as its thread's
|
|
166
|
+
* next message. A paused child keeps its files too, for the run that continues it; `wait_agent`
|
|
167
|
+
* hands over only a finished child's, as an unfinished one's would pass for complete. Its `out/`
|
|
168
|
+
* is stored even when its workspace is kept: a continuation that finds it lost gets it back.
|
|
169
|
+
*/
|
|
170
|
+
async function finishSubagent(input: {
|
|
171
|
+
readonly context: AgentTurnContext
|
|
172
|
+
readonly plan: ResolvedAgentExecutionPlan
|
|
173
|
+
readonly run: SubagentRunRecord
|
|
174
|
+
readonly runtime: AgentTurnRuntime
|
|
175
|
+
readonly runFiles: AgentRunFiles
|
|
176
|
+
readonly session: AgentRunSession
|
|
177
|
+
readonly abortSignal: AbortSignal
|
|
178
|
+
readonly finishReason: AgentRunFinishReason
|
|
179
|
+
readonly work: readonly AppendAgentMessageInput[]
|
|
180
|
+
readonly inbox: SubagentInbox
|
|
181
|
+
/** None yet for a paused child. */
|
|
182
|
+
readonly text?: string
|
|
183
|
+
}): Promise<SubagentRunRecord> {
|
|
184
|
+
const { context, run } = input
|
|
185
|
+
const outputs = await storeChildOutputs({
|
|
186
|
+
files: input.runFiles,
|
|
187
|
+
handed: run.spec.files ?? [],
|
|
188
|
+
signal: input.abortSignal,
|
|
189
|
+
})
|
|
190
|
+
input.runtime.assertCanContinue()
|
|
191
|
+
|
|
192
|
+
// Before the terminal write, and after out/ was read: a paused child's workspace is kept for
|
|
193
|
+
// the run that continues it, and one that answered goes.
|
|
194
|
+
const paused =
|
|
195
|
+
input.finishReason === "awaiting_approval" || input.finishReason === "awaiting_answer"
|
|
196
|
+
// A paused child stops reading here: what came and it never read goes on with the run that
|
|
197
|
+
// continues it, as that run's first message.
|
|
198
|
+
const unread = paused ? input.inbox.close() : []
|
|
199
|
+
await context.beforeFinalize?.({ discard: !paused })
|
|
200
|
+
input.runtime.assertCanContinue()
|
|
201
|
+
|
|
202
|
+
const finalized = await input.session.finish(
|
|
203
|
+
{
|
|
204
|
+
status: "succeeded",
|
|
205
|
+
modelId: input.plan.model.modelId,
|
|
206
|
+
finishReason: input.finishReason,
|
|
207
|
+
result: {
|
|
208
|
+
text: input.text ?? "",
|
|
209
|
+
...(outputs.files.length > 0 ? { files: outputs.files } : {}),
|
|
210
|
+
...(outputs.error === undefined ? {} : { filesError: outputs.error }),
|
|
211
|
+
...(unread.length > 0 ? { unread } : {}),
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
input.work
|
|
215
|
+
)
|
|
216
|
+
if (finalized.kind !== "subagent") {
|
|
217
|
+
throw new Error(`[SixbAgent] Subagent run '${run.id}' changed kind during finalization.`)
|
|
218
|
+
}
|
|
219
|
+
await input.session.events.publishRunFinished(finalized)
|
|
220
|
+
return finalized
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* What a child run did, as the messages its thread keeps: its steps, the one cut short included,
|
|
225
|
+
* and each message its parent sent it, where it read it. Each has the run's id.
|
|
226
|
+
*/
|
|
227
|
+
function subagentWorkMessages(
|
|
228
|
+
generation: PausableModelLoopResult,
|
|
229
|
+
run: SubagentRunRecord,
|
|
230
|
+
deliveries: readonly SubagentDelivery[]
|
|
231
|
+
): readonly AppendAgentMessageInput[] {
|
|
232
|
+
const details = { threadId: run.threadId, runId: run.id }
|
|
233
|
+
const messages: AppendAgentMessageInput[] = []
|
|
234
|
+
const push = (role: "user" | "assistant", parts: readonly AgentMessagePart[]) => {
|
|
235
|
+
messages.push({
|
|
236
|
+
id: createAgentMessageId(),
|
|
237
|
+
projectId: run.projectId,
|
|
238
|
+
threadId: run.threadId,
|
|
239
|
+
runId: run.id,
|
|
240
|
+
role,
|
|
241
|
+
parts,
|
|
242
|
+
})
|
|
243
|
+
}
|
|
244
|
+
const pushSteps = (parts: readonly AgentMessagePart[]) => {
|
|
245
|
+
if (parts.some((part) => part.type !== "step-start")) push("assistant", parts)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let from = 0
|
|
249
|
+
for (const delivery of deliveries) {
|
|
250
|
+
pushSteps(agentTraceFromModelSteps(generation.steps.slice(from, delivery.afterSteps), details))
|
|
251
|
+
push("user", delivery.parts)
|
|
252
|
+
from = delivery.afterSteps
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const rest = generation.steps.slice(from)
|
|
256
|
+
pushSteps(
|
|
257
|
+
generation.status === "aborted"
|
|
258
|
+
? agentTraceFromPartialModelLoop(rest, generation.partialContent, details)
|
|
259
|
+
: agentTraceFromModelSteps(rest, details)
|
|
260
|
+
)
|
|
261
|
+
return messages
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The child's thread as its model reads it. The task, its first message, closes with the time the
|
|
266
|
+
* thread was created, so each run of the child replays the same bytes. Files a tool showed it are
|
|
267
|
+
* projected again as a turn projects its own.
|
|
268
|
+
*/
|
|
269
|
+
function subagentModelMessages(input: {
|
|
270
|
+
readonly context: AgentTurnContext
|
|
271
|
+
readonly thread: AgentThreadRecord
|
|
272
|
+
readonly history: SubagentHistory
|
|
273
|
+
readonly timeZone: string
|
|
274
|
+
}): ModelMessage[] {
|
|
275
|
+
const { messages } = input.history
|
|
276
|
+
const projections = input.context.attachmentContext
|
|
277
|
+
const taskId = messages[0]?.id
|
|
278
|
+
const createdAt = renderCurrentTime(input.thread.createdAt, input.timeZone)
|
|
279
|
+
|
|
280
|
+
return toModelMessages(messages, {
|
|
281
|
+
userMessageSuffix: (message) => (message.id === taskId ? createdAt : undefined),
|
|
282
|
+
fileText: ({ message, partIndex }) =>
|
|
283
|
+
projections?.promptTextByPartKey.get(attachmentKey(message.id, partIndex)),
|
|
284
|
+
fileData: ({ message, partIndex }) =>
|
|
285
|
+
projections?.modelFileDataByPartKey.get(attachmentKey(message.id, partIndex)),
|
|
286
|
+
toolResultFileText: ({ message, partIndex, contentIndex }) =>
|
|
287
|
+
projections?.promptTextByPartKey.get(
|
|
288
|
+
toolResultAttachmentKey(message.id, partIndex, contentIndex)
|
|
289
|
+
),
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function assertSubagentCanFinalize(input: {
|
|
294
|
+
readonly runtime: AgentTurnRuntime
|
|
295
|
+
readonly sandboxReadiness: ReturnType<typeof monitorSandboxReadiness>
|
|
296
|
+
}): void {
|
|
297
|
+
throwIfExecutionLost(input.runtime.sourceSignal)
|
|
298
|
+
input.sandboxReadiness.throwIfFailed()
|
|
299
|
+
input.runtime.assertCanContinue()
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** The child's answer, or a failure that tells the parent what to do about none. */
|
|
303
|
+
function subagentResult(
|
|
304
|
+
steps: readonly ModelStep[],
|
|
305
|
+
run: SubagentRunRecord,
|
|
306
|
+
lastMessageAt: number
|
|
307
|
+
): string {
|
|
308
|
+
const text = subagentAnswer(steps, lastMessageAt)
|
|
309
|
+
if (!text) {
|
|
310
|
+
const remedy = "Spawn it again with a task that says what its answer must contain."
|
|
311
|
+
throw withFailureMessage(
|
|
312
|
+
createSixbError(
|
|
313
|
+
"agent.execution_failed",
|
|
314
|
+
`[SixbAgent] Subagent run '${run.id}' finished without an answer. ${remedy}`,
|
|
315
|
+
{ details: { parentRunId: run.parentRunId, runId: run.id } }
|
|
316
|
+
),
|
|
317
|
+
`The subagent finished without an answer. ${remedy}`
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
return text
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* What the child answers: the text it wrote once its last local tool returned, through any provider
|
|
325
|
+
* continuation, and after the last message its parent sent it (read after `lastMessageAt` steps),
|
|
326
|
+
* which an answer written before it no longer answers. Without any, the last text it wrote, such
|
|
327
|
+
* as an answer it gave before a final tool call. Narration written before earlier tool calls never
|
|
328
|
+
* reaches the parent.
|
|
329
|
+
*/
|
|
330
|
+
export function subagentAnswer(steps: readonly ModelStep[], lastMessageAt = 0): string {
|
|
331
|
+
const ranLocalTools = steps.map((step) =>
|
|
332
|
+
step.content.some((part) => part.type === "tool-call" && part.providerExecuted !== true)
|
|
333
|
+
)
|
|
334
|
+
const answer = textOf(steps.slice(Math.max(ranLocalTools.lastIndexOf(true) + 1, lastMessageAt)))
|
|
335
|
+
if (answer) return answer
|
|
336
|
+
|
|
337
|
+
const written = steps.map((step) => textOf([step])).filter(Boolean)
|
|
338
|
+
return written.at(-1) ?? ""
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function textOf(steps: readonly ModelStep[]): string {
|
|
342
|
+
return steps
|
|
343
|
+
.flatMap((step) => step.content)
|
|
344
|
+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
345
|
+
.join("\n")
|
|
346
|
+
.trim()
|
|
347
|
+
}
|