@buildanagent/harness 0.0.0-stage → 0.1.0-alpha.28
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 +54 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +716 -2
- package/dist/ablation.d.ts +2 -0
- package/dist/ablation.js +3 -0
- package/dist/ablation.js.map +1 -0
- package/dist/completion/check.d.ts +64 -0
- package/dist/completion/check.js +152 -0
- package/dist/completion/check.js.map +1 -0
- package/dist/completion/checks.d.ts +45 -0
- package/dist/completion/checks.js +173 -0
- package/dist/completion/checks.js.map +1 -0
- package/dist/completion/flags.d.ts +25 -0
- package/dist/completion/flags.js +87 -0
- package/dist/completion/flags.js.map +1 -0
- package/dist/completion/index.d.ts +8 -0
- package/dist/completion/index.js +9 -0
- package/dist/completion/index.js.map +1 -0
- package/dist/completion/snapshot.d.ts +33 -0
- package/dist/completion/snapshot.js +83 -0
- package/dist/completion/snapshot.js.map +1 -0
- package/dist/context/cap.d.ts +32 -0
- package/dist/context/cap.js +50 -0
- package/dist/context/cap.js.map +1 -0
- package/dist/context/estimate.d.ts +18 -0
- package/dist/context/estimate.js +20 -0
- package/dist/context/estimate.js.map +1 -0
- package/dist/context/file-spill.d.ts +5 -0
- package/dist/context/file-spill.js +25 -0
- package/dist/context/file-spill.js.map +1 -0
- package/dist/context/index.d.ts +10 -0
- package/dist/context/index.js +10 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/mask.d.ts +27 -0
- package/dist/context/mask.js +71 -0
- package/dist/context/mask.js.map +1 -0
- package/dist/context/mid-turn.d.ts +40 -0
- package/dist/context/mid-turn.js +82 -0
- package/dist/context/mid-turn.js.map +1 -0
- package/dist/context/policy.d.ts +9 -0
- package/dist/context/policy.js +140 -0
- package/dist/context/policy.js.map +1 -0
- package/dist/context/spill-ledger.d.ts +25 -0
- package/dist/context/spill-ledger.js +54 -0
- package/dist/context/spill-ledger.js.map +1 -0
- package/dist/context/spill.d.ts +6 -0
- package/dist/context/spill.js +14 -0
- package/dist/context/spill.js.map +1 -0
- package/dist/context/summarize.d.ts +53 -0
- package/dist/context/summarize.js +204 -0
- package/dist/context/summarize.js.map +1 -0
- package/dist/context/summary-template.d.ts +29 -0
- package/dist/context/summary-template.js +69 -0
- package/dist/context/summary-template.js.map +1 -0
- package/dist/context/types.d.ts +150 -0
- package/dist/context/types.js +2 -0
- package/dist/context/types.js.map +1 -0
- package/dist/context/write-cap.d.ts +19 -0
- package/dist/context/write-cap.js +41 -0
- package/dist/context/write-cap.js.map +1 -0
- package/dist/create-harness.d.ts +7 -0
- package/dist/create-harness.js +21 -0
- package/dist/create-harness.js.map +1 -0
- package/dist/credentials/addresses.d.ts +14 -0
- package/dist/credentials/addresses.js +144 -0
- package/dist/credentials/addresses.js.map +1 -0
- package/dist/credentials/allowlist.d.ts +34 -0
- package/dist/credentials/allowlist.js +77 -0
- package/dist/credentials/allowlist.js.map +1 -0
- package/dist/credentials/egress-proxy.d.ts +70 -0
- package/dist/credentials/egress-proxy.js +482 -0
- package/dist/credentials/egress-proxy.js.map +1 -0
- package/dist/credentials/index.d.ts +9 -0
- package/dist/credentials/index.js +10 -0
- package/dist/credentials/index.js.map +1 -0
- package/dist/credentials/scrub.d.ts +16 -0
- package/dist/credentials/scrub.js +93 -0
- package/dist/credentials/scrub.js.map +1 -0
- package/dist/errors.d.ts +56 -0
- package/dist/errors.js +63 -0
- package/dist/errors.js.map +1 -0
- package/dist/extensions/compose.d.ts +27 -0
- package/dist/extensions/compose.js +83 -0
- package/dist/extensions/compose.js.map +1 -0
- package/dist/extensions/context-files-node.d.ts +28 -0
- package/dist/extensions/context-files-node.js +74 -0
- package/dist/extensions/context-files-node.js.map +1 -0
- package/dist/extensions/context-files.d.ts +68 -0
- package/dist/extensions/context-files.js +214 -0
- package/dist/extensions/context-files.js.map +1 -0
- package/dist/extensions/digest.d.ts +6 -0
- package/dist/extensions/digest.js +68 -0
- package/dist/extensions/digest.js.map +1 -0
- package/dist/extensions/index.d.ts +10 -0
- package/dist/extensions/index.js +10 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/load.d.ts +42 -0
- package/dist/extensions/load.js +211 -0
- package/dist/extensions/load.js.map +1 -0
- package/dist/extensions/lock.d.ts +8 -0
- package/dist/extensions/lock.js +95 -0
- package/dist/extensions/lock.js.map +1 -0
- package/dist/extensions/node.d.ts +10 -0
- package/dist/extensions/node.js +11 -0
- package/dist/extensions/node.js.map +1 -0
- package/dist/extensions/project-dirs.d.ts +24 -0
- package/dist/extensions/project-dirs.js +97 -0
- package/dist/extensions/project-dirs.js.map +1 -0
- package/dist/extensions/skills-node.d.ts +34 -0
- package/dist/extensions/skills-node.js +123 -0
- package/dist/extensions/skills-node.js.map +1 -0
- package/dist/extensions/skills.d.ts +51 -0
- package/dist/extensions/skills.js +175 -0
- package/dist/extensions/skills.js.map +1 -0
- package/dist/extensions/trust-file.d.ts +3 -0
- package/dist/extensions/trust-file.js +62 -0
- package/dist/extensions/trust-file.js.map +1 -0
- package/dist/extensions/trust-store-node.d.ts +10 -0
- package/dist/extensions/trust-store-node.js +33 -0
- package/dist/extensions/trust-store-node.js.map +1 -0
- package/dist/extensions/types.d.ts +42 -0
- package/dist/extensions/types.js +2 -0
- package/dist/extensions/types.js.map +1 -0
- package/dist/harness.d.ts +156 -0
- package/dist/harness.js +2 -0
- package/dist/harness.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/connection.d.ts +22 -0
- package/dist/mcp/connection.js +267 -0
- package/dist/mcp/connection.js.map +1 -0
- package/dist/mcp/content.d.ts +27 -0
- package/dist/mcp/content.js +177 -0
- package/dist/mcp/content.js.map +1 -0
- package/dist/mcp/errors.d.ts +19 -0
- package/dist/mcp/errors.js +21 -0
- package/dist/mcp/errors.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +10 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manager.d.ts +7 -0
- package/dist/mcp/manager.js +173 -0
- package/dist/mcp/manager.js.map +1 -0
- package/dist/mcp/record.d.ts +7 -0
- package/dist/mcp/record.js +20 -0
- package/dist/mcp/record.js.map +1 -0
- package/dist/mcp/stdio.d.ts +39 -0
- package/dist/mcp/stdio.js +382 -0
- package/dist/mcp/stdio.js.map +1 -0
- package/dist/mcp/types.d.ts +172 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +3 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/permissions/default-rules.d.ts +7 -0
- package/dist/permissions/default-rules.js +42 -0
- package/dist/permissions/default-rules.js.map +1 -0
- package/dist/permissions/errors.d.ts +11 -0
- package/dist/permissions/errors.js +14 -0
- package/dist/permissions/errors.js.map +1 -0
- package/dist/permissions/index.d.ts +9 -0
- package/dist/permissions/index.js +9 -0
- package/dist/permissions/index.js.map +1 -0
- package/dist/permissions/match.d.ts +33 -0
- package/dist/permissions/match.js +287 -0
- package/dist/permissions/match.js.map +1 -0
- package/dist/permissions/pipeline.d.ts +9 -0
- package/dist/permissions/pipeline.js +235 -0
- package/dist/permissions/pipeline.js.map +1 -0
- package/dist/permissions/rules.d.ts +22 -0
- package/dist/permissions/rules.js +74 -0
- package/dist/permissions/rules.js.map +1 -0
- package/dist/permissions/shadow.d.ts +5 -0
- package/dist/permissions/shadow.js +22 -0
- package/dist/permissions/shadow.js.map +1 -0
- package/dist/permissions/types.d.ts +104 -0
- package/dist/permissions/types.js +2 -0
- package/dist/permissions/types.js.map +1 -0
- package/dist/sandbox/base.d.ts +35 -0
- package/dist/sandbox/base.js +60 -0
- package/dist/sandbox/base.js.map +1 -0
- package/dist/sandbox/docker/index.d.ts +35 -0
- package/dist/sandbox/docker/index.js +448 -0
- package/dist/sandbox/docker/index.js.map +1 -0
- package/dist/sandbox/errors.d.ts +16 -0
- package/dist/sandbox/errors.js +22 -0
- package/dist/sandbox/errors.js.map +1 -0
- package/dist/sandbox/host-files.d.ts +63 -0
- package/dist/sandbox/host-files.js +248 -0
- package/dist/sandbox/host-files.js.map +1 -0
- package/dist/sandbox/index.d.ts +8 -0
- package/dist/sandbox/index.js +8 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/none.d.ts +17 -0
- package/dist/sandbox/none.js +108 -0
- package/dist/sandbox/none.js.map +1 -0
- package/dist/sandbox/process.d.ts +61 -0
- package/dist/sandbox/process.js +253 -0
- package/dist/sandbox/process.js.map +1 -0
- package/dist/sandbox/spawn-env.d.ts +20 -0
- package/dist/sandbox/spawn-env.js +128 -0
- package/dist/sandbox/spawn-env.js.map +1 -0
- package/dist/sandbox/srt/index.d.ts +35 -0
- package/dist/sandbox/srt/index.js +264 -0
- package/dist/sandbox/srt/index.js.map +1 -0
- package/dist/sandbox/srt/settings.d.ts +48 -0
- package/dist/sandbox/srt/settings.js +31 -0
- package/dist/sandbox/srt/settings.js.map +1 -0
- package/dist/sandbox/types.d.ts +88 -0
- package/dist/sandbox/types.js +6 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/server/answers.d.ts +16 -0
- package/dist/server/answers.js +43 -0
- package/dist/server/answers.js.map +1 -0
- package/dist/server/connection.d.ts +99 -0
- package/dist/server/connection.js +309 -0
- package/dist/server/connection.js.map +1 -0
- package/dist/server/dispatcher.d.ts +86 -0
- package/dist/server/dispatcher.js +800 -0
- package/dist/server/dispatcher.js.map +1 -0
- package/dist/server/index.d.ts +13 -0
- package/dist/server/index.js +12 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/local-transport.d.ts +44 -0
- package/dist/server/local-transport.js +103 -0
- package/dist/server/local-transport.js.map +1 -0
- package/dist/server/outbox.d.ts +31 -0
- package/dist/server/outbox.js +130 -0
- package/dist/server/outbox.js.map +1 -0
- package/dist/server/params.d.ts +25 -0
- package/dist/server/params.js +112 -0
- package/dist/server/params.js.map +1 -0
- package/dist/server/protocol.schema.json +2448 -0
- package/dist/server/rpc-session.d.ts +43 -0
- package/dist/server/rpc-session.js +167 -0
- package/dist/server/rpc-session.js.map +1 -0
- package/dist/server/stdio-streams.d.ts +67 -0
- package/dist/server/stdio-streams.js +218 -0
- package/dist/server/stdio-streams.js.map +1 -0
- package/dist/server/stdio.d.ts +59 -0
- package/dist/server/stdio.js +189 -0
- package/dist/server/stdio.js.map +1 -0
- package/dist/server/websocket.d.ts +106 -0
- package/dist/server/websocket.js +327 -0
- package/dist/server/websocket.js.map +1 -0
- package/dist/session/approvals.d.ts +81 -0
- package/dist/session/approvals.js +217 -0
- package/dist/session/approvals.js.map +1 -0
- package/dist/session/code-call-log.d.ts +42 -0
- package/dist/session/code-call-log.js +63 -0
- package/dist/session/code-call-log.js.map +1 -0
- package/dist/session/code-call-result.d.ts +19 -0
- package/dist/session/code-call-result.js +95 -0
- package/dist/session/code-call-result.js.map +1 -0
- package/dist/session/code-call.d.ts +49 -0
- package/dist/session/code-call.js +345 -0
- package/dist/session/code-call.js.map +1 -0
- package/dist/session/effect-scope.d.ts +11 -0
- package/dist/session/effect-scope.js +10 -0
- package/dist/session/effect-scope.js.map +1 -0
- package/dist/session/errors.d.ts +19 -0
- package/dist/session/errors.js +27 -0
- package/dist/session/errors.js.map +1 -0
- package/dist/session/file-index.d.ts +8 -0
- package/dist/session/file-index.js +70 -0
- package/dist/session/file-index.js.map +1 -0
- package/dist/session/file-key-store.d.ts +16 -0
- package/dist/session/file-key-store.js +169 -0
- package/dist/session/file-key-store.js.map +1 -0
- package/dist/session/file-lock.d.ts +52 -0
- package/dist/session/file-lock.js +324 -0
- package/dist/session/file-lock.js.map +1 -0
- package/dist/session/index.d.ts +22 -0
- package/dist/session/index.js +20 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/jsonl-store.d.ts +37 -0
- package/dist/session/jsonl-store.js +386 -0
- package/dist/session/jsonl-store.js.map +1 -0
- package/dist/session/jsonl.d.ts +9 -0
- package/dist/session/jsonl.js +10 -0
- package/dist/session/jsonl.js.map +1 -0
- package/dist/session/lock.d.ts +19 -0
- package/dist/session/lock.js +2 -0
- package/dist/session/lock.js.map +1 -0
- package/dist/session/memory-store.d.ts +12 -0
- package/dist/session/memory-store.js +60 -0
- package/dist/session/memory-store.js.map +1 -0
- package/dist/session/migrate.d.ts +41 -0
- package/dist/session/migrate.js +86 -0
- package/dist/session/migrate.js.map +1 -0
- package/dist/session/pointer.d.ts +15 -0
- package/dist/session/pointer.js +22 -0
- package/dist/session/pointer.js.map +1 -0
- package/dist/session/rebuild.d.ts +53 -0
- package/dist/session/rebuild.js +230 -0
- package/dist/session/rebuild.js.map +1 -0
- package/dist/session/resume.d.ts +26 -0
- package/dist/session/resume.js +128 -0
- package/dist/session/resume.js.map +1 -0
- package/dist/session/retention.d.ts +97 -0
- package/dist/session/retention.js +702 -0
- package/dist/session/retention.js.map +1 -0
- package/dist/session/runner.d.ts +156 -0
- package/dist/session/runner.js +656 -0
- package/dist/session/runner.js.map +1 -0
- package/dist/session/scheduler.d.ts +47 -0
- package/dist/session/scheduler.js +85 -0
- package/dist/session/scheduler.js.map +1 -0
- package/dist/session/secret-key-store.d.ts +47 -0
- package/dist/session/secret-key-store.js +79 -0
- package/dist/session/secret-key-store.js.map +1 -0
- package/dist/session/session.d.ts +106 -0
- package/dist/session/session.js +353 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/sticky.d.ts +48 -0
- package/dist/session/sticky.js +232 -0
- package/dist/session/sticky.js.map +1 -0
- package/dist/session/store.d.ts +108 -0
- package/dist/session/store.js +163 -0
- package/dist/session/store.js.map +1 -0
- package/dist/telemetry/index.d.ts +2 -0
- package/dist/telemetry/index.js +3 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/testing/fake-sandbox.d.ts +34 -0
- package/dist/testing/fake-sandbox.js +221 -0
- package/dist/testing/fake-sandbox.js.map +1 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/key-store-conformance.d.ts +22 -0
- package/dist/testing/key-store-conformance.js +97 -0
- package/dist/testing/key-store-conformance.js.map +1 -0
- package/dist/testing/run-cases.d.ts +21 -0
- package/dist/testing/run-cases.js +20 -0
- package/dist/testing/run-cases.js.map +1 -0
- package/dist/testing/sandbox-conformance.d.ts +64 -0
- package/dist/testing/sandbox-conformance.js +361 -0
- package/dist/testing/sandbox-conformance.js.map +1 -0
- package/dist/testing/scheduler-conformance.d.ts +32 -0
- package/dist/testing/scheduler-conformance.js +72 -0
- package/dist/testing/scheduler-conformance.js.map +1 -0
- package/dist/testing/store-conformance.d.ts +21 -0
- package/dist/testing/store-conformance.js +320 -0
- package/dist/testing/store-conformance.js.map +1 -0
- package/dist/tools/errors.d.ts +13 -0
- package/dist/tools/errors.js +17 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +3 -0
- package/dist/tools/registry.js +234 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/tool-search.d.ts +30 -0
- package/dist/tools/tool-search.js +118 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +106 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/llms.txt +435 -0
- package/package.json +118 -3
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `runTurn()`: one SDK run, written into a session's log. The user entries are durable before the run starts, so a turn
|
|
3
|
+
* that dies before its first model call leaves the question in the file. The run's own events then become entries
|
|
4
|
+
* through one observer: a message's blocks and end when its call ends, a tool's result when the call ends, usage, and the
|
|
5
|
+
* turn's end. Every write goes through one queue, so the permission pipeline's entries and the observer's never race for
|
|
6
|
+
* the same parent.
|
|
7
|
+
*/
|
|
8
|
+
import { addCost, addUsage } from "@buildanagent/gateway";
|
|
9
|
+
import { FORMAT_VERSION } from "@buildanagent/protocol";
|
|
10
|
+
import { CAPABILITIES, composeInterceptors, continueRun, DEFAULTS_HASH, run, SDK_VERSION, } from "@buildanagent/sdk";
|
|
11
|
+
import { completionFailure, runCompletionCheck, snapshotStart, } from "../completion/check.js";
|
|
12
|
+
import { HarnessError } from "../errors.js";
|
|
13
|
+
import { skillOfRead } from "../extensions/skills.js";
|
|
14
|
+
import { contextOf } from "./rebuild.js";
|
|
15
|
+
import { settleOpenCalls, unansweredCalls } from "./resume.js";
|
|
16
|
+
import { findSticky, stickyDecision } from "./sticky.js";
|
|
17
|
+
/** Writes entries one at a time, each after the one before, so every entry's parent is the entry before it. */
|
|
18
|
+
class EntryWriter {
|
|
19
|
+
#session;
|
|
20
|
+
#newId;
|
|
21
|
+
#now;
|
|
22
|
+
#onFailure;
|
|
23
|
+
#chain = Promise.resolve();
|
|
24
|
+
#failure;
|
|
25
|
+
#failed = false;
|
|
26
|
+
constructor(session, newId, now, onFailure) {
|
|
27
|
+
this.#session = session;
|
|
28
|
+
this.#newId = newId;
|
|
29
|
+
this.#now = now;
|
|
30
|
+
this.#onFailure = onFailure;
|
|
31
|
+
}
|
|
32
|
+
/** The first write that failed, once one has. */
|
|
33
|
+
failure() {
|
|
34
|
+
return this.#failed ? { error: this.#failure } : undefined;
|
|
35
|
+
}
|
|
36
|
+
write(origin, body) {
|
|
37
|
+
const next = this.#chain.then(async () => {
|
|
38
|
+
if (this.#failed)
|
|
39
|
+
throw this.#failure;
|
|
40
|
+
const entry = {
|
|
41
|
+
...body,
|
|
42
|
+
id: this.#newId(),
|
|
43
|
+
parentId: this.#session.head(),
|
|
44
|
+
timestamp: this.#now().toISOString(),
|
|
45
|
+
origin,
|
|
46
|
+
};
|
|
47
|
+
const [stored] = await this.#session.append([entry]);
|
|
48
|
+
return stored;
|
|
49
|
+
});
|
|
50
|
+
this.#chain = next.catch((error) => {
|
|
51
|
+
if (!this.#failed) {
|
|
52
|
+
this.#failed = true;
|
|
53
|
+
this.#failure = error;
|
|
54
|
+
// A log that can't be written can't be trusted to hold the turn, so the run stops.
|
|
55
|
+
this.#onFailure();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return next;
|
|
59
|
+
}
|
|
60
|
+
/** Waits for every write queued so far. */
|
|
61
|
+
settled() {
|
|
62
|
+
return this.#chain;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const refKey = (ref) => `${ref.api}\u0000${ref.provider}\u0000${ref.id}`;
|
|
66
|
+
/**
|
|
67
|
+
* The harness's wrapper, with a `client_request` tool's `execute` sent to `clientTool` first, so a write cap or shadow
|
|
68
|
+
* mode still wraps the call the client runs.
|
|
69
|
+
*/
|
|
70
|
+
function wrapperOf(harness, clientTool) {
|
|
71
|
+
if (clientTool === undefined)
|
|
72
|
+
return harness.wrap;
|
|
73
|
+
const inner = harness.wrap ?? ((tool) => tool.definition);
|
|
74
|
+
return (tool, hostContext) => {
|
|
75
|
+
if (tool.pattern !== "client_request")
|
|
76
|
+
return inner(tool, hostContext);
|
|
77
|
+
const definition = {
|
|
78
|
+
...tool.definition,
|
|
79
|
+
execute: (input, ctx) => clientTool({ callId: ctx.callId, tool: tool.canonicalName, input, signal: ctx.signal }),
|
|
80
|
+
};
|
|
81
|
+
return inner({ ...tool, definition }, hostContext);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** The turn's tools: the registry's definitions for this host context, cut to the allowlist. */
|
|
85
|
+
function toolsOf(harness, options) {
|
|
86
|
+
const all = harness.registry.definitions(wrapperOf(harness, options.clientTool), options.hostContext);
|
|
87
|
+
if (options.tools === undefined)
|
|
88
|
+
return all;
|
|
89
|
+
const allowed = new Set();
|
|
90
|
+
for (const name of options.tools) {
|
|
91
|
+
const tool = harness.registry.get(name);
|
|
92
|
+
if (tool === undefined)
|
|
93
|
+
throw new HarnessError("invalid_turn_options", `the tool allowlist names "${name}", which no tool is registered under`);
|
|
94
|
+
allowed.add(tool.canonicalName);
|
|
95
|
+
}
|
|
96
|
+
return all.filter((definition) => allowed.has(definition.name));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Writes what a new session starts with: its header when the path is empty, and its `env_stamp` when the path has
|
|
100
|
+
* none. `write` appends one entry after the ones before it.
|
|
101
|
+
*/
|
|
102
|
+
export async function recordSessionStart(harness, write, model, started) {
|
|
103
|
+
if (started.length === 0) {
|
|
104
|
+
await write("harness", {
|
|
105
|
+
type: "session_header",
|
|
106
|
+
formatVersion: FORMAT_VERSION,
|
|
107
|
+
harnessVersion: harness.harnessVersion,
|
|
108
|
+
sdkVersion: SDK_VERSION,
|
|
109
|
+
defaultsHash: DEFAULTS_HASH,
|
|
110
|
+
capabilities: [...CAPABILITIES],
|
|
111
|
+
ablationHash: harness.ablationHash,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (!started.some((entry) => entry.type === "env_stamp")) {
|
|
115
|
+
await write("harness", { type: "env_stamp", ...harness.env, model: model.ref });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Writes what a new session starts with, and a `loadout` when the tools or the model differ from the last one recorded. */
|
|
119
|
+
async function recordLoadout(harness, writer, model, tools, started) {
|
|
120
|
+
await recordSessionStart(harness, (origin, body) => writer.write(origin, body), model, started);
|
|
121
|
+
const names = tools.map((tool) => tool.name);
|
|
122
|
+
const last = started.findLast((entry) => entry.type === "loadout");
|
|
123
|
+
const same = last?.type === "loadout" &&
|
|
124
|
+
last.model !== undefined &&
|
|
125
|
+
refKey(last.model) === refKey(model.ref) &&
|
|
126
|
+
[...last.tools].sort().join() === [...names].sort().join();
|
|
127
|
+
if (!same)
|
|
128
|
+
await writer.write("harness", { type: "loadout", tools: names, model: model.ref });
|
|
129
|
+
const written = new Set();
|
|
130
|
+
for (const record of harness.extensionRecords ?? []) {
|
|
131
|
+
const key = `${record.name}\0${record.contentHash}`;
|
|
132
|
+
const known = written.has(key) ||
|
|
133
|
+
started.some((entry) => entry.type === "extension_loaded" && entry.name === record.name && entry.contentHash === record.contentHash);
|
|
134
|
+
if (known)
|
|
135
|
+
continue;
|
|
136
|
+
written.add(key);
|
|
137
|
+
await writer.write("harness", { ...record, type: "extension_loaded" });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async function writeInput(writer, options, newId) {
|
|
141
|
+
const blocks = typeof options.input === "string" ? [{ type: "text", text: options.input }] : options.input;
|
|
142
|
+
const messageId = newId();
|
|
143
|
+
for (const [index, block] of blocks.entries()) {
|
|
144
|
+
await writer.write(options.inputOrigin ?? "user", {
|
|
145
|
+
type: "message_block",
|
|
146
|
+
messageId,
|
|
147
|
+
role: "user",
|
|
148
|
+
index,
|
|
149
|
+
block,
|
|
150
|
+
delivery: "input",
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Turns the run's events into entries. It keeps the last model message back until the next event shows it was kept. */
|
|
155
|
+
class EventRecorder {
|
|
156
|
+
#writer;
|
|
157
|
+
#harness;
|
|
158
|
+
#system;
|
|
159
|
+
#newId;
|
|
160
|
+
#tools = new Map();
|
|
161
|
+
#inputs = new Map();
|
|
162
|
+
#pending;
|
|
163
|
+
#mode;
|
|
164
|
+
#spillOf;
|
|
165
|
+
constructor(writer, harness, system, newId, mode = "run", spillOf) {
|
|
166
|
+
this.#spillOf = spillOf;
|
|
167
|
+
this.#mode = mode;
|
|
168
|
+
this.#writer = writer;
|
|
169
|
+
this.#harness = harness;
|
|
170
|
+
this.#system = system;
|
|
171
|
+
this.#newId = newId;
|
|
172
|
+
}
|
|
173
|
+
/** Queues a write and returns at once. A failure is recorded by the writer and rethrown by `runTurn()`. */
|
|
174
|
+
#queue(origin, body) {
|
|
175
|
+
this.#writer.write(origin, body).catch(() => undefined);
|
|
176
|
+
}
|
|
177
|
+
#usage(message) {
|
|
178
|
+
this.#queue("harness", {
|
|
179
|
+
type: "usage",
|
|
180
|
+
source: "model_call",
|
|
181
|
+
usage: message.usage,
|
|
182
|
+
...(message.lastAttemptUsage === undefined ? {} : { lastAttemptUsage: message.lastAttemptUsage }),
|
|
183
|
+
cost: message.cost,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Queues the held-back message: its blocks, its end and its usage. The gate calls it before it writes, so a call's
|
|
188
|
+
* decision never lands ahead of the message that made the call.
|
|
189
|
+
*/
|
|
190
|
+
flush() {
|
|
191
|
+
const message = this.#pending;
|
|
192
|
+
if (message === undefined)
|
|
193
|
+
return;
|
|
194
|
+
this.#pending = undefined;
|
|
195
|
+
const messageId = this.#newId();
|
|
196
|
+
for (const [index, block] of message.content.entries()) {
|
|
197
|
+
this.#queue("model", { type: "message_block", messageId, role: "assistant", index, block });
|
|
198
|
+
}
|
|
199
|
+
this.#queue("model", {
|
|
200
|
+
type: "message_end",
|
|
201
|
+
messageId,
|
|
202
|
+
model: message.model,
|
|
203
|
+
stopReason: message.stopReason,
|
|
204
|
+
...(message.error === undefined ? {} : { error: message.error }),
|
|
205
|
+
});
|
|
206
|
+
this.#usage(message);
|
|
207
|
+
}
|
|
208
|
+
/** Handles an event without waiting for the store, so the observer is never behind and the SDK never drops it. */
|
|
209
|
+
handle(event) {
|
|
210
|
+
if (event.type === "retry" && event.layer === "turn" && this.#pending !== undefined) {
|
|
211
|
+
// The call's message is superseded and not in the transcript, but the provider billed it.
|
|
212
|
+
const superseded = this.#pending;
|
|
213
|
+
this.#pending = undefined;
|
|
214
|
+
this.#usage(superseded);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
this.flush();
|
|
218
|
+
this.#record(event);
|
|
219
|
+
}
|
|
220
|
+
#record(event) {
|
|
221
|
+
switch (event.type) {
|
|
222
|
+
case "run_start":
|
|
223
|
+
this.#queue("harness", {
|
|
224
|
+
type: "turn_start",
|
|
225
|
+
runId: event.runId,
|
|
226
|
+
mode: this.#mode,
|
|
227
|
+
system: this.#system,
|
|
228
|
+
});
|
|
229
|
+
return;
|
|
230
|
+
case "approval_request":
|
|
231
|
+
// The request is in the log before any client hears of it: the harness's own observers wait for the writes.
|
|
232
|
+
this.#queue("harness", { type: "approval_requested", request: event.request, input: event.input });
|
|
233
|
+
return;
|
|
234
|
+
case "model_call_end":
|
|
235
|
+
this.#pending = event.message;
|
|
236
|
+
// A call the SDK refuses to start (unknown tool, bad input) gets no tool_call_start, so its name comes from here.
|
|
237
|
+
for (const block of event.message.content) {
|
|
238
|
+
if (block.type === "tool_call")
|
|
239
|
+
this.#tools.set(block.callId, block.name);
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
case "tool_call_start":
|
|
243
|
+
this.#tools.set(event.callId, event.tool);
|
|
244
|
+
this.#inputs.set(event.callId, event.input);
|
|
245
|
+
return;
|
|
246
|
+
case "tool_call_end":
|
|
247
|
+
this.#toolResult(event);
|
|
248
|
+
return;
|
|
249
|
+
case "steer_applied":
|
|
250
|
+
for (const [index, block] of event.receipt.message.entries()) {
|
|
251
|
+
this.#queue("user", {
|
|
252
|
+
type: "message_block",
|
|
253
|
+
messageId: event.receipt.id,
|
|
254
|
+
role: "user",
|
|
255
|
+
index,
|
|
256
|
+
block,
|
|
257
|
+
delivery: event.receipt.kind,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
case "run_end":
|
|
262
|
+
this.#queue("harness", {
|
|
263
|
+
type: "turn_end",
|
|
264
|
+
runId: event.runId,
|
|
265
|
+
stopReason: event.result.stopReason,
|
|
266
|
+
steps: event.result.steps,
|
|
267
|
+
});
|
|
268
|
+
return;
|
|
269
|
+
default:
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
#toolResult(event) {
|
|
273
|
+
const tool = this.#tools.get(event.callId) ?? "";
|
|
274
|
+
const trust = this.#harness.registry.get(tool)?.outputTrust ?? "untrusted";
|
|
275
|
+
const { result } = event;
|
|
276
|
+
const spill = this.#spillOf?.(event.callId);
|
|
277
|
+
this.#queue("tool", {
|
|
278
|
+
type: "tool_result",
|
|
279
|
+
callId: event.callId,
|
|
280
|
+
tool,
|
|
281
|
+
content: [...result.content],
|
|
282
|
+
isError: result.isError === true,
|
|
283
|
+
...(result.details === undefined ? {} : { details: result.details }),
|
|
284
|
+
...(spill === undefined ? {} : { spill }),
|
|
285
|
+
trust,
|
|
286
|
+
});
|
|
287
|
+
this.#skillLoaded(tool, event);
|
|
288
|
+
}
|
|
289
|
+
/** A `read` that opened a listed skill's `SKILL.md` and succeeded put the skill's instructions in the context. */
|
|
290
|
+
#skillLoaded(tool, event) {
|
|
291
|
+
const skills = this.#harness.skills;
|
|
292
|
+
if (skills === undefined || tool !== "read" || event.result.isError === true)
|
|
293
|
+
return;
|
|
294
|
+
const input = event.input ?? this.#inputs.get(event.callId);
|
|
295
|
+
const skill = skillOfRead(skills, this.#harness.env.cwd, input);
|
|
296
|
+
if (skill !== undefined)
|
|
297
|
+
this.#queue("harness", { type: "skill_loaded", name: skill.name, path: skill.path });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/** What the layers that write inside a run are given: the turn's writer, and a way to wait for the run's entries. */
|
|
301
|
+
function attachment(writer, recorder) {
|
|
302
|
+
return {
|
|
303
|
+
append: (origin, body) => writer.write(origin, body),
|
|
304
|
+
settle: async () => {
|
|
305
|
+
recorder.flush();
|
|
306
|
+
await writer.settled();
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/** The turn's context layers, when the harness has a policy. */
|
|
311
|
+
function contextFor(harness, session, model, options, retry = false) {
|
|
312
|
+
return harness.context?.forTurn({
|
|
313
|
+
session,
|
|
314
|
+
model,
|
|
315
|
+
ownHistory: options.history !== undefined,
|
|
316
|
+
...(retry ? { midTurn: false } : {}),
|
|
317
|
+
...(options.onContextFill === undefined ? {} : { onFill: options.onContextFill }),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/** The interceptors of a turn: the harness's, the pipeline's gate, and a guard that refuses a model change inside a turn. */
|
|
321
|
+
function interceptorsOf(harness, session, writer, recorder, hostContext, turnModel, turnContext) {
|
|
322
|
+
const policySession = { append: (entry) => writer.write("harness", entry) };
|
|
323
|
+
const pipelineGate = harness.pipeline?.gate(async () => {
|
|
324
|
+
// The model's message and the earlier results are queued first, so the gate's entries follow them in the log.
|
|
325
|
+
recorder.flush();
|
|
326
|
+
await writer.settled();
|
|
327
|
+
return { session: policySession, hostContext, entries: await session.path() };
|
|
328
|
+
});
|
|
329
|
+
const gate = pipelineGate === undefined ? undefined : withStickyAllowances(pipelineGate, harness, session, writer, recorder);
|
|
330
|
+
// The context policy runs first, so it reads and indexes the run's own transcript, and the harness's hooks see its view.
|
|
331
|
+
const composed = composeInterceptors(turnContext?.interceptors ?? {}, harness.interceptors ?? {}, gate ?? {});
|
|
332
|
+
const inner = composed.beforeModelCall;
|
|
333
|
+
return {
|
|
334
|
+
...composed,
|
|
335
|
+
async beforeModelCall(event) {
|
|
336
|
+
const patch = await inner?.(event);
|
|
337
|
+
if (patch !== undefined &&
|
|
338
|
+
"model" in patch &&
|
|
339
|
+
patch.model !== undefined &&
|
|
340
|
+
refKey(patch.model.ref) !== refKey(turnModel.ref)) {
|
|
341
|
+
throw new HarnessError("invalid_turn_options", "a hook changed the model inside a turn; set TurnOptions.model for the next turn so it is recorded");
|
|
342
|
+
}
|
|
343
|
+
return patch;
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The pipeline's gate with the sticky allowances applied: a call the pipeline would ask about, and that a person's
|
|
349
|
+
* earlier sticky `allow` covers, runs. A deny stays a deny, and the pipeline's own entry for the ask stays in the log
|
|
350
|
+
* beside the entry that records the allowance.
|
|
351
|
+
*/
|
|
352
|
+
function withStickyAllowances(gate, harness, session, writer, recorder) {
|
|
353
|
+
return {
|
|
354
|
+
async beforeToolCall(event) {
|
|
355
|
+
// The pipeline's gate always has a `beforeToolCall`; a gate without one has nothing to ask.
|
|
356
|
+
const decision = (await gate.beforeToolCall?.(event)) ?? { decision: "allow" };
|
|
357
|
+
if (decision.decision !== "ask")
|
|
358
|
+
return decision;
|
|
359
|
+
recorder.flush();
|
|
360
|
+
await writer.settled();
|
|
361
|
+
const covered = await findSticky(await session.path(), { tool: event.tool, input: event.input, policyVersion: decision.policyVersion }, harness.registry);
|
|
362
|
+
if (covered === undefined)
|
|
363
|
+
return decision;
|
|
364
|
+
const tool = harness.registry.get(event.tool)?.canonicalName ?? event.tool;
|
|
365
|
+
await writer.write("harness", await stickyDecision({ callId: event.callId, tool, input: event.input }, covered, decision.policyVersion));
|
|
366
|
+
return { decision: "allow", reason: "a sticky allowance a person gave earlier covers this call" };
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The harness's own observers, each held back on an approval request until the entries written so far are durable. An
|
|
372
|
+
* observer that hears of a request can therefore always find its `approval_requested` entry in the log.
|
|
373
|
+
*/
|
|
374
|
+
function afterWrites(observers, writer) {
|
|
375
|
+
return (observers ?? []).map((observer) => async (event) => {
|
|
376
|
+
// Only a request needs its entry first, and `settled()` never rejects: a failed write is the writer's to report.
|
|
377
|
+
if (event.type === "approval_request")
|
|
378
|
+
await writer.settled();
|
|
379
|
+
await observer(event);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
function optionalRunOptions(options, harness, history) {
|
|
383
|
+
return {
|
|
384
|
+
...(history.length === 0 ? {} : { history }),
|
|
385
|
+
...(options.clock === undefined ? {} : { clock: options.clock }),
|
|
386
|
+
...(options.newId === undefined ? {} : { idGenerator: options.newId }),
|
|
387
|
+
...(options.hostContext === undefined ? {} : { app: options.hostContext }),
|
|
388
|
+
...(options.output === undefined ? {} : { output: options.output }),
|
|
389
|
+
...(options.maxSteps === undefined ? {} : { maxSteps: options.maxSteps }),
|
|
390
|
+
...(options.turnRetries === undefined ? {} : { turnRetries: options.turnRetries }),
|
|
391
|
+
...(options.modelOptions === undefined ? {} : { modelOptions: options.modelOptions }),
|
|
392
|
+
...(options.budgetUsd === undefined ? {} : { budgetUsd: options.budgetUsd }),
|
|
393
|
+
...(harness.signer === undefined ? {} : { signer: harness.signer }),
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/** Waits for the run and for every queued write, and returns the result with where the log ends. */
|
|
397
|
+
async function settleTurn(handle, writer, session) {
|
|
398
|
+
let result;
|
|
399
|
+
let runError;
|
|
400
|
+
try {
|
|
401
|
+
result = (await handle.result);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
runError = error;
|
|
405
|
+
}
|
|
406
|
+
await writer.settled();
|
|
407
|
+
// A store failure is the cause of an aborted run, so it comes before the run's own error.
|
|
408
|
+
const failed = writer.failure();
|
|
409
|
+
if (failed !== undefined)
|
|
410
|
+
throw failed.error;
|
|
411
|
+
if (result === undefined)
|
|
412
|
+
throw runError;
|
|
413
|
+
return { ...result, sessionKey: session.key.sessionId, lastEntryId: session.head() };
|
|
414
|
+
}
|
|
415
|
+
/** What every turn and every resume needs: a writer whose failure aborts the run, the run's signal and its id source. */
|
|
416
|
+
function turnPlumbing(session, options) {
|
|
417
|
+
const stop = new AbortController();
|
|
418
|
+
const signal = options.signal === undefined ? stop.signal : AbortSignal.any([options.signal, stop.signal]);
|
|
419
|
+
const newId = options.newId ?? (() => globalThis.crypto.randomUUID());
|
|
420
|
+
const writer = new EntryWriter(session, newId, options.clock === undefined ? () => new Date() : () => new Date(options.clock.now()), () => stop.abort());
|
|
421
|
+
return { signal, newId, writer };
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Runs one turn on a session and writes it to the log. The session must be open with a lock. The first turn of a new
|
|
425
|
+
* session writes its header and `env_stamp`; a turn whose model or tools differ from the last recorded ones writes a
|
|
426
|
+
* `loadout` first; the input blocks follow, and the run's events after them. The returned result is the SDK's, with the
|
|
427
|
+
* session id and the id of the last entry the turn wrote. When the harness has a context policy and the run ends because
|
|
428
|
+
* the provider said the request was too large, the policy compacts once and the turn goes on from the rebuilt context;
|
|
429
|
+
* a second overflow ends the turn with that error.
|
|
430
|
+
*/
|
|
431
|
+
export async function runTurn(session, harness, options) {
|
|
432
|
+
const settings = options.completion;
|
|
433
|
+
// The start state is taken before the turn writes anything, and with the turn's own signal, so an abort stops it.
|
|
434
|
+
// It is taken for every turn, because whether the turn ends `done` is known only after the model has run.
|
|
435
|
+
const start = settings === undefined ? undefined : await snapshotStart(settings, options.signal ?? new AbortController().signal);
|
|
436
|
+
const result = await retryAfterOverflow(session, harness, options, await runTurnOnce(session, harness, options));
|
|
437
|
+
if (settings === undefined || result.stopReason !== "done")
|
|
438
|
+
return result;
|
|
439
|
+
return recordCompletion(session, settings, start, options, result);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Runs the completion check after a `done` turn and writes its entry. The result's stop reason is left as it is, and a
|
|
443
|
+
* check that throws (a sandbox error, a disposed sandbox) is recorded as a failed entry instead of losing the result.
|
|
444
|
+
*/
|
|
445
|
+
async function recordCompletion(session, settings, start, options, result) {
|
|
446
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
447
|
+
let outcome;
|
|
448
|
+
try {
|
|
449
|
+
outcome = await runCompletionCheck(settings, start, signal);
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
outcome = { checks: [], passed: false, flagged: [...(start?.failures ?? []), completionFailure(error)] };
|
|
453
|
+
}
|
|
454
|
+
const { writer } = turnPlumbing(session, options);
|
|
455
|
+
let entry;
|
|
456
|
+
try {
|
|
457
|
+
entry = await writer.write("harness", { type: "completion_check", ...outcome });
|
|
458
|
+
}
|
|
459
|
+
catch {
|
|
460
|
+
// A failed append (a lost lock, a store conflict) must not turn a `done` turn into a rejection: the turn's own
|
|
461
|
+
// entries are written, and the result stands without the check's entry.
|
|
462
|
+
return result;
|
|
463
|
+
}
|
|
464
|
+
options.onCompletionCheck?.(entry);
|
|
465
|
+
return { ...result, lastEntryId: entry.id };
|
|
466
|
+
}
|
|
467
|
+
async function runTurnOnce(session, harness, options) {
|
|
468
|
+
const model = options.model ?? harness.model;
|
|
469
|
+
const system = options.system ?? harness.system;
|
|
470
|
+
const tools = toolsOf(harness, options);
|
|
471
|
+
const { signal, newId, writer } = turnPlumbing(session, options);
|
|
472
|
+
// What a crash left open is settled first, so the log reads as a whole and the context has no call without a result.
|
|
473
|
+
let path = await session.path();
|
|
474
|
+
const wrote = await settleOpenCalls(path, harness, (origin, body) => writer.write(origin, body), !session.headMoved());
|
|
475
|
+
if (wrote)
|
|
476
|
+
path = await session.path();
|
|
477
|
+
await recordLoadout(harness, writer, model, tools, path);
|
|
478
|
+
const turnContext = contextFor(harness, session, model, options);
|
|
479
|
+
const waiting = options.history === undefined ? unansweredCalls(path, true) : [];
|
|
480
|
+
if (waiting.length > 0) {
|
|
481
|
+
throw new HarnessError("invalid_turn_options", `the session waits on tool calls with no result (${waiting.map((call) => call.callId).join(", ")}); resolve them before another turn, or pass history`);
|
|
482
|
+
}
|
|
483
|
+
// Between turns, before the input is written, the summary layer may replace an outgrown span with a summary. It
|
|
484
|
+
// runs only once no call waits for a result, so a paused turn's own message is never in the span.
|
|
485
|
+
if (options.history === undefined &&
|
|
486
|
+
(await turnContext?.compact({ path, signal, append: (origin, body) => writer.write(origin, body) }))) {
|
|
487
|
+
path = await session.path();
|
|
488
|
+
}
|
|
489
|
+
const history = options.history ?? contextOf(path).messages;
|
|
490
|
+
await writeInput(writer, options, newId);
|
|
491
|
+
const recorder = new EventRecorder(writer, harness, system, newId, "run", turnContext?.spillOf);
|
|
492
|
+
turnContext?.attach(attachment(writer, recorder));
|
|
493
|
+
const observer = (event) => recorder.handle(event);
|
|
494
|
+
const handle = run({
|
|
495
|
+
model,
|
|
496
|
+
system,
|
|
497
|
+
input: options.input,
|
|
498
|
+
tools,
|
|
499
|
+
signal,
|
|
500
|
+
interceptors: interceptorsOf(harness, session, writer, recorder, options.hostContext, model, turnContext),
|
|
501
|
+
observers: [observer, ...afterWrites(harness.observers, writer), ...afterWrites(options.observers, writer)],
|
|
502
|
+
...optionalRunOptions(options, harness, history),
|
|
503
|
+
});
|
|
504
|
+
options.onRun?.(handle);
|
|
505
|
+
return settleTurn(handle, writer, session);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Re-runs from an entry: HEAD moves to `entryId` with a `checkout` entry, and a new turn follows it as a new branch of
|
|
509
|
+
* the same log. The entries of the branch it left stay as they were. The entry must not leave a tool call without its
|
|
510
|
+
* result, because that call never ran on the new branch.
|
|
511
|
+
*/
|
|
512
|
+
export async function rerunFrom(session, harness, entryId, options) {
|
|
513
|
+
const waiting = unansweredCalls(await session.path(entryId), true).map((call) => call.callId);
|
|
514
|
+
if (waiting.length > 0) {
|
|
515
|
+
throw new HarnessError("invalid_turn_options", `entry ${entryId} follows tool calls with no result (${waiting.join(", ")}), so a re-run from it would send calls that never ran`);
|
|
516
|
+
}
|
|
517
|
+
await session.checkout(entryId);
|
|
518
|
+
return runTurn(session, harness, options);
|
|
519
|
+
}
|
|
520
|
+
/** The resume's policy check: the pipeline decides the call again, so a deny written after the pause wins. */
|
|
521
|
+
function recheckThrough(session, harness, writer, recorder, hostContext) {
|
|
522
|
+
return async (pending, input) => {
|
|
523
|
+
// The earlier results and decisions are queued first, so the pipeline reads a log that holds them.
|
|
524
|
+
recorder.flush();
|
|
525
|
+
await writer.settled();
|
|
526
|
+
const decision = await harness.pipeline?.decide({ callId: pending.callId, tool: pending.tool, input, initiator: "model" }, { session: { append: (entry) => writer.write("harness", entry) }, hostContext, entries: await session.path() });
|
|
527
|
+
// A person approved the call, so a question the pipeline would ask again doesn't stop it; a deny does.
|
|
528
|
+
return decision?.decision === "deny" ? { deny: decision.reason } : "allow";
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Resumes a paused turn from the log alone. The context, with its waiting requests, is rebuilt before anything is
|
|
533
|
+
* written; each resolution is then written as an `approval_resolved` entry, and `continueRun()` verifies the requests
|
|
534
|
+
* against the harness's signer. Each approved call goes through the permission pipeline again first, and a deny now
|
|
535
|
+
* turns the approval into a denial.
|
|
536
|
+
*/
|
|
537
|
+
export async function resumeTurn(session, harness, options) {
|
|
538
|
+
if (harness.signer === undefined) {
|
|
539
|
+
throw new HarnessError("approval_unavailable", "the harness has no signer, so no approval can be checked");
|
|
540
|
+
}
|
|
541
|
+
const { signal, newId, writer } = turnPlumbing(session, options);
|
|
542
|
+
const path = await session.path();
|
|
543
|
+
const context = contextOf(path);
|
|
544
|
+
const waiting = new Set((context.pending ?? []).map((request) => request.id));
|
|
545
|
+
const answered = new Set();
|
|
546
|
+
for (const resolution of options.resolutions) {
|
|
547
|
+
if (answered.has(resolution.requestId)) {
|
|
548
|
+
throw new HarnessError("approval_not_pending", `request ${resolution.requestId} is answered twice in one resume`);
|
|
549
|
+
}
|
|
550
|
+
answered.add(resolution.requestId);
|
|
551
|
+
if (!waiting.has(resolution.requestId)) {
|
|
552
|
+
throw new HarnessError("approval_not_pending", `no request ${resolution.requestId} is waiting in this session`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
// The paused turn's tools: the latest `loadout` names them, and one the registry has lost stops the resume.
|
|
556
|
+
const names = path.findLast((entry) => entry.type === "loadout");
|
|
557
|
+
const tools = toolsOf(harness, {
|
|
558
|
+
...(names?.type === "loadout" ? { tools: names.tools } : {}),
|
|
559
|
+
...(options.hostContext === undefined ? {} : { hostContext: options.hostContext }),
|
|
560
|
+
...(options.clientTool === undefined ? {} : { clientTool: options.clientTool }),
|
|
561
|
+
});
|
|
562
|
+
const records = options.records ?? options.resolutions.map((resolution) => ({ resolution }));
|
|
563
|
+
for (const record of records)
|
|
564
|
+
await writer.write("user", { type: "approval_resolved", ...record });
|
|
565
|
+
const model = options.model ?? harness.model;
|
|
566
|
+
await recordLoadout(harness, writer, model, tools, path);
|
|
567
|
+
const turnContext = contextFor(harness, session, model, options);
|
|
568
|
+
const recorder = new EventRecorder(writer, harness, context.system, newId, "continue", turnContext?.spillOf);
|
|
569
|
+
turnContext?.attach(attachment(writer, recorder));
|
|
570
|
+
const handle = continueRun(context, {
|
|
571
|
+
model,
|
|
572
|
+
tools,
|
|
573
|
+
resolutions: [...options.resolutions],
|
|
574
|
+
recheck: recheckThrough(session, harness, writer, recorder, options.hostContext),
|
|
575
|
+
signer: harness.signer,
|
|
576
|
+
signal,
|
|
577
|
+
interceptors: interceptorsOf(harness, session, writer, recorder, options.hostContext, model, turnContext),
|
|
578
|
+
observers: [
|
|
579
|
+
(event) => recorder.handle(event),
|
|
580
|
+
...afterWrites(harness.observers, writer),
|
|
581
|
+
...afterWrites(options.observers, writer),
|
|
582
|
+
],
|
|
583
|
+
...optionalRunOptions(options, harness, []),
|
|
584
|
+
});
|
|
585
|
+
options.onRun?.(handle);
|
|
586
|
+
return settleTurn(handle, writer, session);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Layer 5: after a run that ended because the request was too large, asks the context policy to compact once and
|
|
590
|
+
* goes on from the rebuilt context with `continueRun()`, which asks the model again because the gateway leaves the
|
|
591
|
+
* failed response out of the request. Only one retry is made, so a second overflow is the turn's result.
|
|
592
|
+
*/
|
|
593
|
+
async function retryAfterOverflow(session, harness, options, first) {
|
|
594
|
+
if (first.stopReason !== "error" || first.error.kind !== "overflow" || options.history !== undefined)
|
|
595
|
+
return first;
|
|
596
|
+
const model = options.model ?? harness.model;
|
|
597
|
+
const turnContext = contextFor(harness, session, model, options, true);
|
|
598
|
+
if (turnContext === undefined)
|
|
599
|
+
return first;
|
|
600
|
+
// The retry has what the first attempt left of `maxSteps`; with none left there is nothing to retry into.
|
|
601
|
+
const stepsLeft = options.maxSteps === undefined ? undefined : options.maxSteps - first.steps;
|
|
602
|
+
if (stepsLeft !== undefined && stepsLeft < 1)
|
|
603
|
+
return first;
|
|
604
|
+
const { signal, newId, writer } = turnPlumbing(session, options);
|
|
605
|
+
// The first run has ended: a steer sent now waits for the retry's run.
|
|
606
|
+
options.onOverflowRetry?.();
|
|
607
|
+
let compacted;
|
|
608
|
+
try {
|
|
609
|
+
compacted = await turnContext.onOverflow({
|
|
610
|
+
path: await session.path(),
|
|
611
|
+
signal,
|
|
612
|
+
append: (origin, body) => writer.write(origin, body),
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
catch (error) {
|
|
616
|
+
// An abort during the summary call ends the turn as aborted, not as the overflow it was trying to cure.
|
|
617
|
+
if (signal.aborted)
|
|
618
|
+
return { ...withoutError(first), stopReason: "aborted", lastEntryId: session.head() };
|
|
619
|
+
throw error;
|
|
620
|
+
}
|
|
621
|
+
await writer.settled();
|
|
622
|
+
// A summary call that was made and rejected wrote its usage entry, so the log ends later than the first result says.
|
|
623
|
+
if (!compacted)
|
|
624
|
+
return { ...first, lastEntryId: session.head() };
|
|
625
|
+
const context = contextOf(await session.path());
|
|
626
|
+
const recorder = new EventRecorder(writer, harness, context.system, newId, "continue", turnContext.spillOf);
|
|
627
|
+
turnContext.attach(attachment(writer, recorder));
|
|
628
|
+
const handle = continueRun(context, {
|
|
629
|
+
model,
|
|
630
|
+
tools: toolsOf(harness, options),
|
|
631
|
+
resolutions: [],
|
|
632
|
+
signal,
|
|
633
|
+
interceptors: interceptorsOf(harness, session, writer, recorder, options.hostContext, model, turnContext),
|
|
634
|
+
observers: [
|
|
635
|
+
(event) => recorder.handle(event),
|
|
636
|
+
...afterWrites(harness.observers, writer),
|
|
637
|
+
...afterWrites(options.observers, writer),
|
|
638
|
+
],
|
|
639
|
+
...optionalRunOptions(stepsLeft === undefined ? options : { ...options, maxSteps: stepsLeft }, harness, []),
|
|
640
|
+
});
|
|
641
|
+
options.onRun?.(handle);
|
|
642
|
+
const retried = await settleTurn(handle, writer, session);
|
|
643
|
+
// The turn's result counts both attempts: what the first one spent and the steps it took.
|
|
644
|
+
return {
|
|
645
|
+
...retried,
|
|
646
|
+
steps: first.steps + retried.steps,
|
|
647
|
+
usage: addUsage(first.usage, retried.usage),
|
|
648
|
+
cost: addCost(first.cost, retried.cost),
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
/** A result without its `error`, for a turn that was cut short before the error mattered. */
|
|
652
|
+
function withoutError(result) {
|
|
653
|
+
const { error: _error, ...rest } = result;
|
|
654
|
+
return rest;
|
|
655
|
+
}
|
|
656
|
+
//# sourceMappingURL=runner.js.map
|