operon-agents-core 0.1.0-alpha.2 → 0.1.0-alpha.21
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/dist/agent/agent.d.ts +8 -7
- package/dist/agent/compose-hooks.d.ts +10 -1
- package/dist/agent/compose-hooks.js +61 -6
- package/dist/agent/concurrency.js +1 -1
- package/dist/agent/context-report.d.ts +2 -2
- package/dist/agent/durable-state.d.ts +22 -0
- package/dist/agent/durable-state.js +54 -0
- package/dist/agent/index.d.ts +2 -2
- package/dist/agent/index.js +1 -1
- package/dist/agent/instruction-context.d.ts +6 -6
- package/dist/agent/instruction-context.js +23 -23
- package/dist/agent/profile-builtins.js +1 -1
- package/dist/agent/run-support.d.ts +1 -1
- package/dist/agent/run-support.js +6 -5
- package/dist/agent/runner.d.ts +25 -22
- package/dist/agent/runner.js +217 -121
- package/dist/agent/session.d.ts +262 -156
- package/dist/agent/session.js +590 -418
- package/dist/agent/subagent-tools.js +3 -3
- package/dist/agent/subagent.js +28 -13
- package/dist/agent/toolset.d.ts +1 -0
- package/dist/agent/toolset.js +13 -7
- package/dist/agent/workflow/journal.d.ts +2 -2
- package/dist/agent/workflow/journal.js +2 -2
- package/dist/agent/workflow/manager.d.ts +1 -1
- package/dist/agent/workflow/manager.js +1 -1
- package/dist/agent/workflow/prompt.js +2 -2
- package/dist/agent/workflow/sandbox.d.ts +1 -1
- package/dist/agent/workflow/sandbox.js +1 -1
- package/dist/agent/workflow/tool.js +30 -43
- package/dist/agent/worktree.d.ts +8 -8
- package/dist/agent/worktree.js +15 -15
- package/dist/capabilities/assembler.d.ts +29 -5
- package/dist/capabilities/assembler.js +85 -9
- package/dist/capabilities/background/code-task.d.ts +43 -0
- package/dist/capabilities/background/code-task.js +75 -0
- package/dist/capabilities/background/command-task.d.ts +9 -2
- package/dist/capabilities/background/command-task.js +22 -6
- package/dist/capabilities/background/index.d.ts +6 -1
- package/dist/capabilities/background/index.js +26 -20
- package/dist/capabilities/background/manager.d.ts +57 -1
- package/dist/capabilities/background/manager.js +210 -45
- package/dist/capabilities/background/persist-disk.d.ts +1 -1
- package/dist/capabilities/background/persist-disk.js +1 -1
- package/dist/capabilities/background/persist.js +4 -2
- package/dist/capabilities/background/projections.d.ts +16 -0
- package/dist/capabilities/background/projections.js +86 -0
- package/dist/capabilities/background/stall-watchdog.d.ts +41 -0
- package/dist/capabilities/background/stall-watchdog.js +85 -0
- package/dist/capabilities/background/task.d.ts +8 -7
- package/dist/capabilities/background/tools.js +33 -7
- package/dist/capabilities/capability-data.d.ts +52 -0
- package/dist/capabilities/capability-data.js +88 -0
- package/dist/capabilities/capability-state.d.ts +2 -4
- package/dist/capabilities/capability-state.js +5 -23
- package/dist/capabilities/capability.d.ts +203 -38
- package/dist/capabilities/compaction/index.js +10 -4
- package/dist/capabilities/compaction/service.d.ts +20 -5
- package/dist/capabilities/compaction/service.js +27 -11
- package/dist/capabilities/goal/goal-store.d.ts +76 -12
- package/dist/capabilities/goal/goal-store.js +137 -53
- package/dist/capabilities/goal/index.d.ts +1 -1
- package/dist/capabilities/goal/index.js +30 -7
- package/dist/capabilities/index.d.ts +9 -6
- package/dist/capabilities/index.js +4 -3
- package/dist/capabilities/plan/index.js +22 -5
- package/dist/capabilities/plan/plan-mode.d.ts +30 -9
- package/dist/capabilities/plan/plan-mode.js +57 -32
- package/dist/capabilities/plan/tools.js +1 -1
- package/dist/capabilities/skills/flow-provider.js +3 -3
- package/dist/capabilities/skills/index.d.ts +13 -1
- package/dist/capabilities/skills/index.js +30 -21
- package/dist/capabilities/skills/parser.d.ts +2 -2
- package/dist/capabilities/skills/parser.js +3 -3
- package/dist/capabilities/skills/registry.d.ts +5 -6
- package/dist/capabilities/skills/registry.js +6 -9
- package/dist/capabilities/skills/scanner.d.ts +3 -3
- package/dist/capabilities/skills/scanner.js +14 -14
- package/dist/capabilities/skills/service.d.ts +2 -1
- package/dist/capabilities/skills/service.js +3 -3
- package/dist/capabilities/skills/skill-tool.d.ts +2 -0
- package/dist/capabilities/skills/skill-tool.js +1 -1
- package/dist/capabilities/task/index.js +2 -1
- package/dist/capabilities/task/persist-disk.d.ts +1 -1
- package/dist/capabilities/task/persist-disk.js +1 -1
- package/dist/capabilities/task/persist.js +1 -1
- package/dist/capabilities/todo/index.js +11 -6
- package/dist/capabilities/todo/injector.js +2 -1
- package/dist/capabilities/todo/todo-store.d.ts +28 -7
- package/dist/capabilities/todo/todo-store.js +44 -11
- package/dist/capabilities/todo/tools.js +8 -3
- package/dist/capabilities/tool-provider.d.ts +3 -3
- package/dist/capabilities/user-hooks/engine.d.ts +3 -3
- package/dist/capabilities/user-hooks/engine.js +7 -7
- package/dist/capabilities/user-hooks/index.js +2 -1
- package/dist/capabilities/workflow/index.d.ts +5 -3
- package/dist/capabilities/workflow/index.js +3 -2
- package/dist/commands/index.js +34 -32
- package/dist/config/index.d.ts +1 -1
- package/dist/config/provider-manager.js +0 -28
- package/dist/config/schema.d.ts +53 -4
- package/dist/config/schema.js +53 -1
- package/dist/events/events.d.ts +43 -11
- package/dist/events/events.js +1 -0
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.js +1 -1
- package/dist/events/projection.d.ts +49 -1
- package/dist/events/projection.js +144 -19
- package/dist/events/publisher.js +2 -21
- package/dist/home.d.ts +1 -1
- package/dist/home.js +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/internal.d.ts +5 -2
- package/dist/internal.js +5 -2
- package/dist/llm/define-model.d.ts +4 -0
- package/dist/llm/define-model.js +63 -25
- package/dist/llm/index.d.ts +1 -0
- package/dist/llm/index.js +1 -0
- package/dist/llm/model.d.ts +2 -0
- package/dist/llm/provider.d.ts +17 -0
- package/dist/llm/provider.js +14 -0
- package/dist/llm/retry.js +1 -1
- package/dist/loop/context.d.ts +21 -1
- package/dist/loop/context.js +47 -9
- package/dist/loop/events.d.ts +5 -1
- package/dist/loop/index.d.ts +2 -2
- package/dist/loop/index.js +1 -1
- package/dist/loop/interruption.d.ts +9 -4
- package/dist/loop/interruption.js +11 -25
- package/dist/loop/run-turn.d.ts +16 -3
- package/dist/loop/run-turn.js +13 -4
- package/dist/loop/steer.d.ts +78 -12
- package/dist/loop/steer.js +101 -38
- package/dist/loop/tool-call.d.ts +13 -2
- package/dist/loop/tool-call.js +135 -5
- package/dist/loop/turn-step.d.ts +13 -2
- package/dist/loop/turn-step.js +29 -7
- package/dist/loop/types.d.ts +6 -0
- package/dist/mcp/auth-tool.d.ts +3 -1
- package/dist/mcp/auth-tool.js +2 -2
- package/dist/mcp/capability.d.ts +13 -0
- package/dist/mcp/capability.js +36 -17
- package/dist/mcp/config-loader.d.ts +5 -5
- package/dist/mcp/config-loader.js +17 -17
- package/dist/mcp/credentials.d.ts +35 -0
- package/dist/mcp/credentials.js +2 -0
- package/dist/mcp/filter.d.ts +2 -2
- package/dist/mcp/index.d.ts +4 -3
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/manager.d.ts +108 -6
- package/dist/mcp/manager.js +262 -18
- package/dist/mcp/oauth/callback-server.d.ts +10 -1
- package/dist/mcp/oauth/callback-server.js +82 -16
- package/dist/mcp/oauth/index.d.ts +2 -2
- package/dist/mcp/oauth/provider.d.ts +18 -0
- package/dist/mcp/oauth/provider.js +31 -1
- package/dist/mcp/oauth/service.d.ts +9 -3
- package/dist/mcp/oauth/service.js +29 -4
- package/dist/mcp/sdk-transport.js +1 -1
- package/dist/mcp/server.js +1 -1
- package/dist/permission/auto-approver/prompts-data.d.ts +1 -1
- package/dist/permission/auto-approver/prompts-data.js +1 -1
- package/dist/permission/manager.d.ts +5 -3
- package/dist/permission/manager.js +7 -3
- package/dist/permission/policies.d.ts +2 -2
- package/dist/permission/policies.js +4 -4
- package/dist/plugins/fields.d.ts +14 -0
- package/dist/plugins/fields.js +30 -0
- package/dist/plugins/hooks.d.ts +2 -2
- package/dist/plugins/hooks.js +12 -12
- package/dist/plugins/index.js +1 -0
- package/dist/plugins/manager.d.ts +8 -3
- package/dist/plugins/manager.js +18 -7
- package/dist/plugins/manifest.d.ts +2 -2
- package/dist/plugins/manifest.js +101 -91
- package/dist/plugins/marketplace.js +76 -57
- package/dist/plugins/store.d.ts +3 -3
- package/dist/plugins/store.js +6 -6
- package/dist/protocol/index.d.ts +1 -1
- package/dist/store/catalog-store.d.ts +4 -1
- package/dist/store/catalog-store.js +5 -1
- package/dist/store/checkpointing-store.d.ts +41 -0
- package/dist/store/checkpointing-store.js +86 -0
- package/dist/store/disk-store.d.ts +8 -0
- package/dist/store/disk-store.js +127 -5
- package/dist/store/index.d.ts +3 -2
- package/dist/store/index.js +2 -1
- package/dist/store/log-store.d.ts +9 -1
- package/dist/store/log-store.js +68 -4
- package/dist/store/migrate.d.ts +8 -2
- package/dist/store/migrate.js +9 -2
- package/dist/store/origin.d.ts +31 -6
- package/dist/store/origin.js +0 -14
- package/dist/store/pg.d.ts +1 -0
- package/dist/store/pg.js +37 -2
- package/dist/store/redis.d.ts +1 -0
- package/dist/store/redis.js +30 -0
- package/dist/store/repository.d.ts +13 -1
- package/dist/store/repository.js +45 -2
- package/dist/store/session-catalog.d.ts +4 -0
- package/dist/store/session-catalog.js +6 -0
- package/dist/store/storage.js +3 -0
- package/dist/store/store.d.ts +97 -34
- package/dist/store/store.js +64 -10
- package/dist/telemetry/appender.d.ts +43 -0
- package/dist/telemetry/appender.js +46 -0
- package/dist/telemetry/events.d.ts +142 -0
- package/dist/telemetry/events.js +141 -0
- package/dist/telemetry/index.d.ts +10 -0
- package/dist/telemetry/index.js +6 -0
- package/dist/telemetry/projection.d.ts +23 -0
- package/dist/telemetry/projection.js +185 -0
- package/dist/telemetry/redact.d.ts +20 -0
- package/dist/telemetry/redact.js +40 -0
- package/dist/telemetry/service.d.ts +49 -0
- package/dist/telemetry/service.js +137 -0
- package/dist/testing.d.ts +58 -0
- package/dist/testing.js +123 -0
- package/dist/tool/background.d.ts +9 -6
- package/dist/tool/background.js +7 -1
- package/dist/tool/builtin/ask-user-question.js +6 -5
- package/dist/tool/builtin/bash.d.ts +6 -0
- package/dist/tool/builtin/bash.js +79 -42
- package/dist/tool/builtin/edit.js +82 -109
- package/dist/tool/builtin/glob.js +13 -13
- package/dist/tool/builtin/grep.js +13 -13
- package/dist/tool/builtin/read.js +25 -15
- package/dist/tool/builtin/write.js +49 -33
- package/dist/tool/define.d.ts +14 -8
- package/dist/tool/define.js +8 -2
- package/dist/tool/{machine-base.d.ts → environment-base.d.ts} +37 -37
- package/dist/tool/{machine-base.js → environment-base.js} +45 -119
- package/dist/tool/{machine-local.d.ts → environment-local.d.ts} +9 -8
- package/dist/tool/{machine-local.js → environment-local.js} +60 -81
- package/dist/tool/{machine-null.d.ts → environment-null.d.ts} +9 -9
- package/dist/tool/{machine-null.js → environment-null.js} +9 -9
- package/dist/tool/{machine-ssh.d.ts → environment-ssh.d.ts} +33 -18
- package/dist/tool/{machine-ssh.js → environment-ssh.js} +118 -149
- package/dist/tool/{machine.d.ts → environment.d.ts} +53 -82
- package/dist/tool/environment.js +2 -0
- package/dist/tool/file-freshness.d.ts +33 -19
- package/dist/tool/file-freshness.js +39 -30
- package/dist/tool/filesystem-tools.d.ts +1 -1
- package/dist/tool/filesystem-tools.js +1 -1
- package/dist/tool/git-context.d.ts +2 -2
- package/dist/tool/git-context.js +8 -8
- package/dist/tool/index.d.ts +12 -12
- package/dist/tool/index.js +12 -10
- package/dist/tool/policies/path-access.d.ts +4 -4
- package/dist/tool/policies/path-access.js +12 -12
- package/dist/tool/questions.d.ts +8 -0
- package/dist/tool/search/catalog.d.ts +37 -0
- package/dist/tool/search/catalog.js +182 -0
- package/dist/tool/search/deferral.d.ts +3 -2
- package/dist/tool/search/loaded.d.ts +32 -0
- package/dist/tool/search/loaded.js +96 -0
- package/dist/tool/search/search-tool.d.ts +2 -6
- package/dist/tool/search/search-tool.js +16 -15
- package/dist/tool/shell-env.js +4 -0
- package/dist/tool/support/bash-paths.js +38 -7
- package/dist/tool/support/{machine-ops.d.ts → environment-ops.d.ts} +12 -14
- package/dist/tool/support/{machine-ops.js → environment-ops.js} +21 -2
- package/dist/tool/support/git-worktree.d.ts +3 -3
- package/dist/tool/support/git-worktree.js +6 -6
- package/dist/tool/support/rg-locator.d.ts +6 -6
- package/dist/tool/support/rg-locator.js +6 -6
- package/dist/tool/support/ripgrep-run.d.ts +5 -5
- package/dist/tool/support/ripgrep-run.js +3 -3
- package/dist/tool/support/tool-path.d.ts +3 -3
- package/dist/tool/support/tool-path.js +7 -7
- package/dist/tool/types.d.ts +54 -10
- package/dist/tool/web/fetch-url.js +1 -1
- package/dist/tool/web/web-search.js +1 -1
- package/dist/tool/workspace-spec.d.ts +3 -3
- package/dist/tool/workspace-spec.js +27 -27
- package/dist/tracing/bridge.d.ts +9 -1
- package/dist/tracing/bridge.js +307 -77
- package/dist/tracing/index.d.ts +2 -2
- package/dist/tracing/otel.d.ts +28 -1
- package/dist/tracing/otel.js +150 -38
- package/dist/tracing/processor.d.ts +22 -0
- package/dist/tracing/processor.js +2 -0
- package/dist/tracing/spans.d.ts +54 -0
- package/dist/tracing/spans.js +5 -0
- package/package.json +6 -2
- package/dist/tool/machine.js +0 -15
- package/dist/tool/search/activation.d.ts +0 -13
- package/dist/tool/search/activation.js +0 -31
package/dist/agent/runner.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { addUsage, emptyUsage } from "../loop/usage.js";
|
|
2
2
|
import { runTurn } from "../loop/run-turn.js";
|
|
3
3
|
import { isAbortError } from "../loop/errors.js";
|
|
4
|
-
import { ConversationContext,
|
|
4
|
+
import { ConversationContext, readShardTail } from "../loop/context.js";
|
|
5
5
|
import { PermissionManager } from "../permission/manager.js";
|
|
6
6
|
import { assembleCapabilities } from "../capabilities/assembler.js";
|
|
7
7
|
import { computeContextBreakdown } from "./context-report.js";
|
|
8
8
|
import { IterableSink, } from "../events/index.js";
|
|
9
9
|
import { DEFAULT_ADDRESS, SessionBusyError } from "../store/index.js";
|
|
10
|
-
import { SteerBus
|
|
10
|
+
import { SteerBus } from "../loop/steer.js";
|
|
11
11
|
import { INTERRUPTION_STATE_KEY, applyInterruptionAnswers, findAnchoredAssistant, newInterruptionId, parseInterruptionState, } from "../loop/interruption.js";
|
|
12
12
|
import { abortReason, Session } from "./session.js";
|
|
13
13
|
import { Agent } from "./agent.js";
|
|
@@ -17,7 +17,7 @@ import { assertGraphUnambiguous, duplicateAgentNames, findAgentByName, findAgent
|
|
|
17
17
|
import { performHandoff } from "./handoff-executor.js";
|
|
18
18
|
import { pauseRun } from "./pause.js";
|
|
19
19
|
import { buildRunTools } from "./toolset.js";
|
|
20
|
-
import {
|
|
20
|
+
import { prepareToolCatalog } from "../tool/search/catalog.js";
|
|
21
21
|
import { emitRunEvent, finalText, historyChangeEmitter, injectAtTurnBoundary, lastAssistant, mapTerminalStatus, resolveModelParams, runCtxFor, tryParseOutput, } from "./run-support.js";
|
|
22
22
|
export class Runner {
|
|
23
23
|
config;
|
|
@@ -30,7 +30,12 @@ export class Runner {
|
|
|
30
30
|
return this.withLease(opts, async (lease) => {
|
|
31
31
|
const { session, owned } = await this.acquireSession(opts, undefined, lease);
|
|
32
32
|
try {
|
|
33
|
-
return await session.withRunLock(() =>
|
|
33
|
+
return await session.withRunLock(() => {
|
|
34
|
+
// Cancelled while queued behind another run: leave without touching the session —
|
|
35
|
+
// no reconcile, no context replay, no `agent.started` on the log.
|
|
36
|
+
opts?.signal?.throwIfAborted();
|
|
37
|
+
return this.execute(agent, input, opts, session);
|
|
38
|
+
});
|
|
34
39
|
}
|
|
35
40
|
finally {
|
|
36
41
|
if (owned)
|
|
@@ -68,9 +73,18 @@ export class Runner {
|
|
|
68
73
|
// One-shot pull sessions emit straight to `pull` (the bus IS the iterable). For a
|
|
69
74
|
// shared session, subscribe `pull` to its bus so we never replace the channel (Invariant 4).
|
|
70
75
|
const { session, owned } = await this.acquireSession(opts, pull, lease);
|
|
71
|
-
|
|
76
|
+
let unsub;
|
|
72
77
|
try {
|
|
73
|
-
return await session.withRunLock(() =>
|
|
78
|
+
return await session.withRunLock(() => {
|
|
79
|
+
// Cancelled while queued behind another run: leave without touching the session —
|
|
80
|
+
// no reconcile, no context replay, no `agent.started` on the log.
|
|
81
|
+
opts?.signal?.throwIfAborted();
|
|
82
|
+
// Subscribe only now, holding the lock: subscribing while queued would hand this
|
|
83
|
+
// stream the tail of the run ahead of it (its inputs and answers) before its own.
|
|
84
|
+
if (!owned)
|
|
85
|
+
unsub = session.events.subscribe((ev) => void pull.emit(ev));
|
|
86
|
+
return this.execute(agent, input, opts, session);
|
|
87
|
+
});
|
|
74
88
|
}
|
|
75
89
|
finally {
|
|
76
90
|
unsub?.();
|
|
@@ -109,9 +123,10 @@ export class Runner {
|
|
|
109
123
|
return this.withLease(baseOpts, async (lease) => {
|
|
110
124
|
const { session, owned } = await this.acquireSession(baseOpts, undefined, lease);
|
|
111
125
|
// Reopening a session: orphaned background subagents (running from a dead process) → lost.
|
|
112
|
-
await session.reconcileSubagents();
|
|
126
|
+
await session.background?.reconcileSubagents() ?? [];
|
|
113
127
|
try {
|
|
114
128
|
return await session.withRunLock(async () => {
|
|
129
|
+
baseOpts?.signal?.throwIfAborted();
|
|
115
130
|
const store = session.store;
|
|
116
131
|
if (!store)
|
|
117
132
|
throw new Error("Cannot resume an interrupted run without a durable session store.");
|
|
@@ -124,10 +139,18 @@ export class Runner {
|
|
|
124
139
|
}
|
|
125
140
|
session.setConversationHead({ agentKey: active.name, address: rootFrame.address });
|
|
126
141
|
const state = await this.beginRun(session, baseOpts, updated);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
let context;
|
|
143
|
+
try {
|
|
144
|
+
await store.putState(INTERRUPTION_STATE_KEY, updated);
|
|
145
|
+
// History is the log's job — replay it (the active agent's shard) to rebuild context.
|
|
146
|
+
context = await session.openFrameContext(rootFrame.address);
|
|
147
|
+
session.setLiveContext(rootFrame.address, context);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
// The run's capabilities are started: stop them before the error leaves.
|
|
151
|
+
await this.settleRun(session, state, undefined);
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
131
154
|
try {
|
|
132
155
|
const resumeFrom = findAnchoredAssistant(context.messages, rootFrame.anchor);
|
|
133
156
|
const result = await this.engine.run(active, context, state, resumeFrom);
|
|
@@ -173,24 +196,18 @@ export class Runner {
|
|
|
173
196
|
}
|
|
174
197
|
return { session: opts.session, owned: false };
|
|
175
198
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// Losing the lease cancels the run: a holder that can no longer renew has, by definition,
|
|
189
|
-
// been superseded, and must stop before its writes race the node that took over. Only the
|
|
190
|
-
// session this runner owns is wired up — a caller-supplied session keeps its own signal.
|
|
191
|
-
signal: mergeSignals(opts?.signal, lease?.signal),
|
|
192
|
-
};
|
|
193
|
-
const session = await Session.open(cfg);
|
|
199
|
+
// Losing the lease cancels the run: a holder that can no longer renew has, by definition,
|
|
200
|
+
// been superseded, and must stop before its writes race the node that took over. Only the
|
|
201
|
+
// session this runner owns is wired up — a caller-supplied session keeps its own signal.
|
|
202
|
+
const signal = mergeSignals(opts?.signal, lease?.signal);
|
|
203
|
+
const fromHook = (await this.config.session?.({ sessionId: opts?.sessionId })) ?? {};
|
|
204
|
+
const session = await Session.open({
|
|
205
|
+
...fromHook,
|
|
206
|
+
// A one-shot pull stream IS the session's event bus (Invariant 4); it wins over the hook's.
|
|
207
|
+
...(eventsOverride !== undefined ? { events: eventsOverride } : {}),
|
|
208
|
+
...(opts?.sessionId !== undefined ? { sessionId: opts.sessionId } : {}),
|
|
209
|
+
...(signal !== undefined ? { signal } : {}),
|
|
210
|
+
});
|
|
194
211
|
return { session, owned: true };
|
|
195
212
|
}
|
|
196
213
|
/** Resolve the durable conversation owner by following committed handoffs from `main`. */
|
|
@@ -221,13 +238,15 @@ export class Runner {
|
|
|
221
238
|
throw new Error("Cannot continue session: handoff chain exceeds 64 transfers.");
|
|
222
239
|
}
|
|
223
240
|
visited.add(head.address);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
241
|
+
// From the shard's newest checkpoint: a committed handoff freezes the shard, so it is
|
|
242
|
+
// always after the last checkpoint, and the tail is all the live context needs.
|
|
243
|
+
const records = await readShardTail(store, head.address);
|
|
244
|
+
const accepted = (from) => from.some((record) => record.type === "agent.handoff.accepted" &&
|
|
245
|
+
record.handoffId === expectedAcceptance.handoffId &&
|
|
246
|
+
record.fromAddress === expectedAcceptance.fromAddress);
|
|
247
|
+
// The acceptance opens the target shard, so a compaction there has since moved the
|
|
248
|
+
// tail past it: only then is the rest of the shard worth reading to find it.
|
|
249
|
+
if (expectedAcceptance !== undefined && !accepted(records) && !accepted(await readWholeShard(store, head.address))) {
|
|
231
250
|
throw new Error(`Cannot continue session: handoff "${expectedAcceptance.handoffId}" points to uninitialized shard "${head.address}".`);
|
|
232
251
|
}
|
|
233
252
|
const committed = records.filter((record) => record.type === "agent.handoff" &&
|
|
@@ -237,12 +256,7 @@ export class Runner {
|
|
|
237
256
|
// Reuse the leaf records we already read to seed the live cache; otherwise the caller
|
|
238
257
|
// would immediately replay the same shard a second time before appending the prompt.
|
|
239
258
|
if (session.liveContext(head.address) === undefined) {
|
|
240
|
-
|
|
241
|
-
store,
|
|
242
|
-
address: head.address,
|
|
243
|
-
});
|
|
244
|
-
context.loadFromRecords(records);
|
|
245
|
-
session.setLiveContext(head.address, context);
|
|
259
|
+
session.setLiveContext(head.address, await session.frameContextFrom(head.address, records));
|
|
246
260
|
}
|
|
247
261
|
break;
|
|
248
262
|
}
|
|
@@ -275,42 +289,55 @@ export class Runner {
|
|
|
275
289
|
// A normal post-crash continuation is a fresh prompt (for example "continue"), not the
|
|
276
290
|
// HITL-specific Runner.resume path. Reconcile durable task ghosts before request recovery
|
|
277
291
|
// inspects them so BackgroundOutput is given the truthful terminal `lost` status.
|
|
278
|
-
await session.reconcileSubagents();
|
|
292
|
+
await session.background?.reconcileSubagents() ?? [];
|
|
279
293
|
if (session.store && (await session.store.getState(INTERRUPTION_STATE_KEY)) !== null) {
|
|
280
294
|
throw new Error("This session has an interrupted run. Resume or resolve it before starting a new prompt.");
|
|
281
295
|
}
|
|
282
296
|
const head = await this.resolveConversationHead(session, rootAgent);
|
|
283
297
|
const agent = head.agent;
|
|
284
298
|
const state = await this.beginRun(session, opts, undefined, head.address);
|
|
285
|
-
//
|
|
286
|
-
//
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
// A cold run (new session, or after a log rewrite) replays the log once and caches it. No
|
|
298
|
-
// store means the same append-only behavior, held only in memory.
|
|
299
|
-
let context = session.liveContext(state.address);
|
|
300
|
-
if (context === undefined) {
|
|
301
|
-
context = state.store
|
|
302
|
-
? await replayContext(state.store, state.address)
|
|
303
|
-
: new ConversationContext({
|
|
304
|
-
store: state.store,
|
|
305
|
-
address: state.address,
|
|
306
|
-
onHistoryChange: historyChangeEmitter(state.events, state.sessionId),
|
|
299
|
+
// From here the run holds started capabilities: whatever throws before the main `try` below
|
|
300
|
+
// must still stop them, or they never are — and the session never reaches a quiet point.
|
|
301
|
+
let context;
|
|
302
|
+
let runStart;
|
|
303
|
+
try {
|
|
304
|
+
// Live runs tolerate duplicate names (edges resolve by object), but durable name-keyed
|
|
305
|
+
// resolution (resume) fails closed on them — warn now, not mid-recovery.
|
|
306
|
+
const duplicates = duplicateAgentNames(rootAgent);
|
|
307
|
+
if (duplicates.length > 0) {
|
|
308
|
+
emitRunEvent(state, {
|
|
309
|
+
type: "warning",
|
|
310
|
+
message: `Agent graph contains duplicate agent names (${duplicates.join(", ")}). Interrupted runs referencing these names cannot be resumed — give agents unique names.`,
|
|
307
311
|
});
|
|
308
|
-
|
|
312
|
+
}
|
|
313
|
+
// Continue the conversation. A long-lived session keeps its live context, so we append to it
|
|
314
|
+
// (O(1)) instead of replaying the whole log every turn. Model-visible reminders are ordinary
|
|
315
|
+
// append-only context records, so the live prefix is exactly what a cold replay reconstructs.
|
|
316
|
+
// A cold run (new session, or after a log rewrite) replays the log once and caches it. No
|
|
317
|
+
// store means the same append-only behavior, held only in memory.
|
|
318
|
+
const live = session.liveContext(state.address);
|
|
319
|
+
if (live !== undefined) {
|
|
320
|
+
context = live;
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
context = state.store
|
|
324
|
+
? await session.openFrameContext(state.address)
|
|
325
|
+
: new ConversationContext({
|
|
326
|
+
store: state.store,
|
|
327
|
+
address: state.address,
|
|
328
|
+
onHistoryChange: historyChangeEmitter(state.events, state.sessionId),
|
|
329
|
+
});
|
|
330
|
+
session.setLiveContext(state.address, context);
|
|
331
|
+
}
|
|
332
|
+
// Capability interception of the caller's input, BEFORE guardrails and before anything is
|
|
333
|
+
// journaled: a rewrite here is what the run (and its replay) sees as the prompt. Guardrails
|
|
334
|
+
// then judge what actually enters history, not what the caller originally sent.
|
|
335
|
+
runStart = await this.applyRunStarts(state, agent.name, toMessages(input));
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
await this.settleRun(session, state, context);
|
|
339
|
+
throw error;
|
|
309
340
|
}
|
|
310
|
-
// Capability interception of the caller's input, BEFORE guardrails and before anything is
|
|
311
|
-
// journaled: a rewrite here is what the run (and its replay) sees as the prompt. Guardrails
|
|
312
|
-
// then judge what actually enters history, not what the caller originally sent.
|
|
313
|
-
const runStart = await this.applyRunStarts(state, agent.name, toMessages(input));
|
|
314
341
|
const messages = runStart.input;
|
|
315
342
|
let inputAccepted = false;
|
|
316
343
|
try {
|
|
@@ -353,6 +380,7 @@ export class Runner {
|
|
|
353
380
|
state.usage = addUsage(state.usage, error.usage);
|
|
354
381
|
const activeContext = session.liveContext(state.address) ?? context;
|
|
355
382
|
activeContext.record({ type: "usage.record", usage: error.usage, total: state.usage });
|
|
383
|
+
countSessionUsage(state, error.usage);
|
|
356
384
|
if (state.store === undefined)
|
|
357
385
|
void emitRunEvent(state, { type: "usage.updated", usage: state.usage });
|
|
358
386
|
}
|
|
@@ -381,32 +409,28 @@ export class Runner {
|
|
|
381
409
|
}
|
|
382
410
|
}
|
|
383
411
|
/**
|
|
384
|
-
* Chain the capabilities' run-tier input hooks. Each sees the previous one's rewrite
|
|
385
|
-
*
|
|
412
|
+
* Chain the capabilities' run-tier input hooks. Each sees the previous one's rewrite. A broken
|
|
413
|
+
* detachable capability does not sink the prompt — its hook is isolated at assembly — while an
|
|
414
|
+
* invariant one does, as its contract says.
|
|
386
415
|
*/
|
|
387
416
|
async applyRunStarts(state, agentName, input) {
|
|
388
417
|
let current = input;
|
|
418
|
+
// No catch here: a detachable capability's hook is already isolated by the assembler (a throw
|
|
419
|
+
// is reported and reads as "no change"), so what can still throw is an invariant one — and
|
|
420
|
+
// its failure is the run's.
|
|
389
421
|
for (const hook of state.capabilities.runStarts) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
current = result.input;
|
|
403
|
-
}
|
|
404
|
-
catch (error) {
|
|
405
|
-
emitRunEvent(state, {
|
|
406
|
-
type: "warning",
|
|
407
|
-
message: `beforeRun hook failed; input left unchanged: ${error instanceof Error ? error.message : String(error)}`,
|
|
408
|
-
});
|
|
409
|
-
}
|
|
422
|
+
const result = await hook({
|
|
423
|
+
sessionId: state.sessionId,
|
|
424
|
+
address: state.address,
|
|
425
|
+
agent: agentName,
|
|
426
|
+
signal: state.signal,
|
|
427
|
+
input: current,
|
|
428
|
+
});
|
|
429
|
+
// First claim wins: once a hook answers the prompt there is nothing left to rewrite.
|
|
430
|
+
if (result?.handled !== undefined)
|
|
431
|
+
return { input: [...current], handled: result.handled };
|
|
432
|
+
if (result?.input !== undefined)
|
|
433
|
+
current = result.input;
|
|
410
434
|
}
|
|
411
435
|
return { input: [...current] };
|
|
412
436
|
}
|
|
@@ -417,10 +441,22 @@ export class Runner {
|
|
|
417
441
|
* run's latest unflushed writes — then stop capabilities and surface their diagnostics.
|
|
418
442
|
*/
|
|
419
443
|
async settleRun(session, state, context) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
444
|
+
// Each step runs even when the one before it throws: a failed flush must still stop the
|
|
445
|
+
// capabilities, and the session must always learn the run is over — that is the quiet point
|
|
446
|
+
// a queued attach/detach waits for.
|
|
447
|
+
try {
|
|
448
|
+
await (session.liveContext(state.address) ?? context)?.flush();
|
|
449
|
+
}
|
|
450
|
+
finally {
|
|
451
|
+
try {
|
|
452
|
+
await state.capabilities.stop();
|
|
453
|
+
await session.flushEvents();
|
|
454
|
+
}
|
|
455
|
+
finally {
|
|
456
|
+
await session.exitRun();
|
|
457
|
+
this.reportDiagnostics(state);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
424
460
|
}
|
|
425
461
|
async beginRun(session, opts, interruption, initialAddress = DEFAULT_ADDRESS) {
|
|
426
462
|
// Per-run cancel handle, downstream of the caller's (or the session's) signal. Capabilities
|
|
@@ -431,14 +467,7 @@ export class Runner {
|
|
|
431
467
|
const signal = AbortSignal.any([upstream, runController.signal]);
|
|
432
468
|
const ctx = {
|
|
433
469
|
sessionId: session.id,
|
|
434
|
-
machine: session.machine,
|
|
435
|
-
store: session.store,
|
|
436
|
-
events: session.events,
|
|
437
470
|
signal,
|
|
438
|
-
steer: session.steer,
|
|
439
|
-
responder: session.responder,
|
|
440
|
-
background: session.background,
|
|
441
|
-
logger: session.logger,
|
|
442
471
|
// Same session controls, except `abort` is scoped to this run rather than the session.
|
|
443
472
|
controls: {
|
|
444
473
|
...session.controls(),
|
|
@@ -448,9 +477,30 @@ export class Runner {
|
|
|
448
477
|
},
|
|
449
478
|
},
|
|
450
479
|
};
|
|
451
|
-
const capabilities = await assembleCapabilities(session.capabilities, ctx);
|
|
452
|
-
const permission = session.permission;
|
|
453
480
|
const frame = interruption?.frames[interruption.rootFrameId];
|
|
481
|
+
const runAddress = frame?.address ?? initialAddress;
|
|
482
|
+
// Counted from before assembly, so an attach requested while this run assembles waits for it.
|
|
483
|
+
session.enterRun();
|
|
484
|
+
const assembling = assembleCapabilities(session.capabilities, ctx, {
|
|
485
|
+
// A detachable hook that fails mid-run is announced as it happens, not after the run.
|
|
486
|
+
report: (diagnostic) => {
|
|
487
|
+
void session.events.emit({
|
|
488
|
+
type: diagnostic.level === "error" ? "error" : "warning",
|
|
489
|
+
message: `[capability ${diagnostic.capability}/${diagnostic.phase}] ${diagnostic.message}`,
|
|
490
|
+
address: runAddress,
|
|
491
|
+
sessionId: session.id,
|
|
492
|
+
});
|
|
493
|
+
},
|
|
494
|
+
});
|
|
495
|
+
let capabilities;
|
|
496
|
+
try {
|
|
497
|
+
capabilities = await assembling;
|
|
498
|
+
}
|
|
499
|
+
catch (error) {
|
|
500
|
+
await session.exitRun();
|
|
501
|
+
throw error;
|
|
502
|
+
}
|
|
503
|
+
const permission = session.permission;
|
|
454
504
|
const state = {
|
|
455
505
|
runId: interruption?.runId ?? newInterruptionId("run"),
|
|
456
506
|
frameId: frame?.frameId ?? newInterruptionId("frame"),
|
|
@@ -462,9 +512,9 @@ export class Runner {
|
|
|
462
512
|
usage: frame?.execution.usage ?? emptyUsage(),
|
|
463
513
|
turns: frame?.execution.turns ?? 0,
|
|
464
514
|
maxTurns: frame?.execution.maxTurns ?? opts?.maxTurns ?? this.config.maxTurns ?? 16,
|
|
465
|
-
address:
|
|
515
|
+
address: runAddress,
|
|
466
516
|
events: session.events,
|
|
467
|
-
|
|
517
|
+
environment: session.environment,
|
|
468
518
|
store: session.store,
|
|
469
519
|
responder: session.responder,
|
|
470
520
|
background: session.background,
|
|
@@ -518,6 +568,11 @@ class Engine {
|
|
|
518
568
|
let current = initial;
|
|
519
569
|
let resume = resumeFrom;
|
|
520
570
|
let canDrainFollowUps = drainQueuedAtStart === true;
|
|
571
|
+
// A wake run has no input of its own: whatever is queued IS its prompt, on either channel.
|
|
572
|
+
// A steer that landed on an idle session interrupts nothing — it is simply the next thing
|
|
573
|
+
// said — so it is appended here, ahead of the turn's boundary injections, exactly where a
|
|
574
|
+
// prompt would go. Once only: later steers arrive mid-turn and are drained at step boundaries.
|
|
575
|
+
let drainSteeringAtStart = drainQueuedAtStart === true;
|
|
521
576
|
emitRunEvent(state, { type: "agent.started", agent: current.name, ...(state.parentToolCallId ? { parentToolCallId: state.parentToolCallId } : {}) });
|
|
522
577
|
while (true) {
|
|
523
578
|
state.signal.throwIfAborted();
|
|
@@ -534,9 +589,15 @@ class Engine {
|
|
|
534
589
|
// whether to loop; this next iteration consumes the queued prompts as its new input.
|
|
535
590
|
// Deliberately NOT reset on handoff: the pre-handoff agent's turn already ran, so the
|
|
536
591
|
// target agent's first iteration draining a queued follow-up still honors this rule.
|
|
592
|
+
if (drainSteeringAtStart) {
|
|
593
|
+
drainSteeringAtStart = false;
|
|
594
|
+
for (const { message, origin } of state.steer.drainSteering()) {
|
|
595
|
+
context.appendMessage(message, origin);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
537
598
|
if (canDrainFollowUps) {
|
|
538
|
-
for (const {
|
|
539
|
-
context.appendMessage(message,
|
|
599
|
+
for (const { message, origin } of state.steer.drainFollowUps()) {
|
|
600
|
+
context.appendMessage(message, origin);
|
|
540
601
|
}
|
|
541
602
|
}
|
|
542
603
|
// Fresh turns append durable reminders to the context tail. HITL resume continues the
|
|
@@ -548,19 +609,25 @@ class Engine {
|
|
|
548
609
|
// The engine's run loop rides into the toolset as a plain callback: the spawner the
|
|
549
610
|
// subagent/Agent/Workflow tools share calls it to run child agents.
|
|
550
611
|
const toolset = await buildRunTools(current, state, (agent, childContext, childState, childResume) => this.run(agent, childContext, childState, childResume), model);
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
//
|
|
554
|
-
|
|
555
|
-
state
|
|
612
|
+
let permissionTools = toolset.tools;
|
|
613
|
+
// The approval table is shared between the authorize hook and the tool-call layer (a
|
|
614
|
+
// nested call may add to it mid-batch), so it must exist before either captures it.
|
|
615
|
+
state.answers ??= {};
|
|
616
|
+
const hooks = buildRunHooks(current, state, context, () => permissionTools);
|
|
617
|
+
// The same projection the first step will send (announcements are that step's job).
|
|
618
|
+
const visibleTools = prepareToolCatalog(context, toolset, { announce: false }).schemas.map((schema) => ({ schema }));
|
|
619
|
+
// Stamp a context-window breakdown snapshot, so `session.getContextBreakdown()` (and the
|
|
620
|
+
// app-server RPC) can report where tokens go: here from the projection the first step will
|
|
621
|
+
// send, then again from every step's final request — an extension's `model.request` hook may
|
|
622
|
+
// rewrite it (Code Mode takes the MCP catalog out), and the report must show what was sent.
|
|
623
|
+
const stampBreakdown = (snapshot) => state.session.recordContextBreakdown(computeContextBreakdown({
|
|
556
624
|
model,
|
|
557
|
-
|
|
558
|
-
tools: visibleTools,
|
|
559
|
-
messages: context.messages,
|
|
625
|
+
...snapshot,
|
|
560
626
|
injectionTokens,
|
|
561
|
-
compactBufferTokens: state.session.
|
|
627
|
+
compactBufferTokens: state.session.compactionView?.reservedContextTokens ?? 0,
|
|
562
628
|
capturedAt: Date.now(),
|
|
563
629
|
}));
|
|
630
|
+
stampBreakdown({ system, tools: visibleTools, messages: context.messages });
|
|
564
631
|
const turnId = state.currentTurnId ?? `${state.sessionId}-t${state.turns}`;
|
|
565
632
|
const outputGuardrails = current.guardrails.output ?? [];
|
|
566
633
|
if (resume === undefined)
|
|
@@ -574,26 +641,36 @@ class Engine {
|
|
|
574
641
|
sessionId: state.sessionId,
|
|
575
642
|
signal: state.signal,
|
|
576
643
|
model,
|
|
577
|
-
|
|
644
|
+
environment: state.environment,
|
|
578
645
|
background: state.background,
|
|
579
646
|
responder: state.responder,
|
|
580
647
|
// Per-address ledger: main agent and each subagent line keep independent
|
|
581
648
|
// read-before-write state that survives across turns and run() calls.
|
|
582
649
|
fileLedger: state.session.fileLedgerFor(state.address),
|
|
650
|
+
hostContext: state.context,
|
|
583
651
|
system,
|
|
584
652
|
context,
|
|
585
653
|
tools: toolset.tools,
|
|
586
654
|
deferredToolNames: toolset.deferredToolNames,
|
|
655
|
+
deferEnabled: toolset.deferEnabled,
|
|
656
|
+
onToolsPrepared: (tools) => { permissionTools = tools; },
|
|
657
|
+
onRequestPrepared: (request) => stampBreakdown({
|
|
658
|
+
system: request.system,
|
|
659
|
+
tools: (request.tools ?? []).map((schema) => ({ schema })),
|
|
660
|
+
messages: request.messages,
|
|
661
|
+
}),
|
|
662
|
+
refreshTools: () => buildRunTools(current, state, (agent, childContext, childState, childResume) => this.run(agent, childContext, childState, childResume), model),
|
|
587
663
|
hooks,
|
|
664
|
+
answers: state.answers,
|
|
588
665
|
params: resolveModelParams(current.modelSettings, state.session.thinkingSetting),
|
|
589
666
|
createOutputGuardrailMonitor: outputGuardrails.length > 0
|
|
590
667
|
? (options) => createOutputGuardrailMonitor(outputGuardrails, current, runCtxFor(state), options)
|
|
591
668
|
: undefined,
|
|
592
669
|
maxSteps: current.maxStepsPerTurn ?? this.config.maxStepsPerTurn,
|
|
593
670
|
maxRetriesPerStep: this.config.maxRetriesPerStep,
|
|
594
|
-
drainSteering: () => state.steer.drainSteering().map((s) => ({ message: s.message, origin:
|
|
671
|
+
drainSteering: () => state.steer.drainSteering().map((s) => ({ message: s.message, origin: s.origin })),
|
|
595
672
|
// One sink: the loop dispatches real AgentEvent bodies; we only add the envelope.
|
|
596
|
-
dispatchEvent: (body) =>
|
|
673
|
+
dispatchEvent: (body) => emitRunEvent(state, body),
|
|
597
674
|
logger: state.session.logger,
|
|
598
675
|
resumeFrom: resume,
|
|
599
676
|
resume: resume !== undefined ? frameResume(state) : undefined,
|
|
@@ -628,6 +705,7 @@ class Engine {
|
|
|
628
705
|
state.usage = addUsage(state.usage, r.usage);
|
|
629
706
|
// Audit record: per-turn cost, journaled in order with the message stream (not history).
|
|
630
707
|
context.record({ type: "usage.record", model: model.id, usage: r.usage, total: state.usage });
|
|
708
|
+
countSessionUsage(state, r.usage);
|
|
631
709
|
if (state.store === undefined)
|
|
632
710
|
emitRunEvent(state, { type: "usage.updated", usage: state.usage });
|
|
633
711
|
if (r.stopReason === "interrupt") {
|
|
@@ -735,10 +813,14 @@ class Engine {
|
|
|
735
813
|
const result = await hook({
|
|
736
814
|
turnId: `${state.sessionId}-t${state.turns}`,
|
|
737
815
|
stepNumber: state.turns,
|
|
816
|
+
address: state.address,
|
|
738
817
|
usage,
|
|
739
818
|
stopReason: "end_turn",
|
|
740
819
|
signal: state.signal,
|
|
741
820
|
model,
|
|
821
|
+
// A child frame shares its parent's capabilities (`deriveChild`), so every
|
|
822
|
+
// continuation hook also sees subagent stops — this is how it tells them apart.
|
|
823
|
+
isRootAgent: state.parentFrameId === undefined,
|
|
742
824
|
});
|
|
743
825
|
if (result?.continue === true)
|
|
744
826
|
return true;
|
|
@@ -783,4 +865,18 @@ function mergeSignals(a, b) {
|
|
|
783
865
|
return a;
|
|
784
866
|
return AbortSignal.any([a, b]);
|
|
785
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* Add a turn's usage to the session's running total (`Session.usage`), for a ROOT frame only.
|
|
870
|
+
* A subagent's turns are audited in its own shard but are not the session's.
|
|
871
|
+
*/
|
|
872
|
+
function countSessionUsage(state, usage) {
|
|
873
|
+
if (state.parentFrameId === undefined)
|
|
874
|
+
state.session.addSessionUsage(usage);
|
|
875
|
+
}
|
|
876
|
+
async function readWholeShard(store, address) {
|
|
877
|
+
const records = [];
|
|
878
|
+
for await (const record of store.readRecords({ address }))
|
|
879
|
+
records.push(record);
|
|
880
|
+
return records;
|
|
881
|
+
}
|
|
786
882
|
//# sourceMappingURL=runner.js.map
|