@parcha/agentrun-serve 0.1.0-beta.33 → 0.1.0-beta.34
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/CHANGELOG.md +233 -0
- package/README.md +266 -201
- package/dist/adapters.d.ts +0 -4
- package/dist/adapters.js +11 -15
- package/dist/agent-rows.d.ts +98 -0
- package/dist/agent-rows.js +1 -0
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +60 -25
- package/dist/agents.d.ts +9 -125
- package/dist/agents.js +15 -305
- package/dist/answer-json.d.ts +23 -0
- package/dist/answer-json.js +58 -0
- package/dist/api-only.d.ts +1 -1
- package/dist/api-only.js +2 -2
- package/dist/bash-read-only.d.ts +22 -0
- package/dist/bash-read-only.js +701 -0
- package/dist/box-settings.d.ts +3 -23
- package/dist/box-settings.js +7 -51
- package/dist/boxes.d.ts +17 -0
- package/dist/boxes.js +27 -20
- package/dist/browser-commands.d.ts +5 -2
- package/dist/browser-commands.js +4 -4
- package/dist/browser-computer.d.ts +5 -0
- package/dist/browser-computer.js +21 -0
- package/dist/browser-cost.d.ts +9 -6
- package/dist/browser-cost.js +92 -50
- package/dist/browser-live.d.ts +7 -20
- package/dist/browser-live.js +15 -93
- package/dist/browser-person.d.ts +18 -51
- package/dist/browser-person.js +94 -277
- package/dist/cap.d.ts +8 -5
- package/dist/cap.js +12 -5
- package/dist/cli.js +39 -13
- package/dist/closed-pipes.d.ts +2 -0
- package/dist/closed-pipes.js +13 -0
- package/dist/computer-commands.d.ts +2 -0
- package/dist/computer-commands.js +2 -0
- package/dist/computer-keys.js +5 -2
- package/dist/computer-setting.d.ts +20 -4
- package/dist/computer-setting.js +25 -5
- package/dist/computers.d.ts +30 -2
- package/dist/computers.js +70 -11
- package/dist/confine.d.ts +120 -0
- package/dist/confine.js +436 -0
- package/dist/console/assets/index-RPHGlXmM.js +540 -0
- package/dist/console/assets/index-opTN5Naq.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/decision-rows.d.ts +1 -1
- package/dist/decision-rows.js +1 -1
- package/dist/docker-daemon.d.ts +31 -0
- package/dist/docker-daemon.js +56 -0
- package/dist/effort-guidance.js +4 -3
- package/dist/environment.d.ts +0 -3
- package/dist/environment.js +3 -5
- package/dist/errors.d.ts +0 -2
- package/dist/errors.js +6 -9
- package/dist/filed-answer.d.ts +32 -0
- package/dist/filed-answer.js +160 -0
- package/dist/files.d.ts +1 -3
- package/dist/files.js +2 -4
- package/dist/github-connection.d.ts +1 -1
- package/dist/graph-view.d.ts +3 -5
- package/dist/graph-view.js +4 -14
- package/dist/handed-values.d.ts +9 -0
- package/dist/handed-values.js +26 -0
- package/dist/harness-install.d.ts +1 -7
- package/dist/harness-install.js +1 -18
- package/dist/harness-opencode/adapter.d.ts +7 -0
- package/dist/harness-opencode/adapter.js +23 -6
- package/dist/harness-opencode/index.d.ts +1 -1
- package/dist/harness-opencode/index.js +1 -1
- package/dist/harness-opencode/verify.d.ts +17 -0
- package/dist/harness-opencode/verify.js +30 -10
- package/dist/harness-table.js +6 -1
- package/dist/heard.js +1 -1
- package/dist/held-fd.d.ts +5 -0
- package/dist/held-fd.js +14 -0
- package/dist/host-calls.d.ts +20 -0
- package/dist/host-calls.js +12 -0
- package/dist/host-tools.d.ts +4 -21
- package/dist/host-tools.js +4 -61
- package/dist/host.d.ts +2 -4
- package/dist/host.js +2 -7
- package/dist/import-summary.d.ts +1 -5
- package/dist/import-summary.js +0 -21
- package/dist/import.d.ts +6 -6
- package/dist/import.js +14 -15
- package/dist/key-refusal.d.ts +5 -1
- package/dist/key-refusal.js +19 -4
- package/dist/learn/check.d.ts +6 -2
- package/dist/learn/check.js +83 -29
- package/dist/learn/folder.d.ts +5 -1
- package/dist/learn/folder.js +10 -3
- package/dist/learn/grade.d.ts +14 -0
- package/dist/learn/grade.js +68 -12
- package/dist/learn/index.d.ts +4 -4
- package/dist/learn/index.js +4 -4
- package/dist/learn/reflect.d.ts +31 -2
- package/dist/learn/reflect.js +79 -4
- package/dist/learn/relevance.d.ts +24 -0
- package/dist/learn/relevance.js +91 -0
- package/dist/learn/secrets.d.ts +7 -1
- package/dist/learn/secrets.js +86 -19
- package/dist/learn/skills.d.ts +112 -14
- package/dist/learn/skills.js +556 -52
- package/dist/learned-workflow.d.ts +32 -1
- package/dist/learned-workflow.js +100 -9
- package/dist/learning-routes.d.ts +8 -4
- package/dist/learning-routes.js +23 -9
- package/dist/learning-run.d.ts +9 -26
- package/dist/learning-run.js +22 -32
- package/dist/learning-words.d.ts +12 -5
- package/dist/learning-words.js +18 -9
- package/dist/learning.d.ts +93 -77
- package/dist/learning.js +176 -304
- package/dist/live/stream.js +1 -1
- package/dist/live/translate.js +12 -2
- package/dist/local-models.d.ts +11 -5
- package/dist/local-models.js +13 -8
- package/dist/mcp-gateway.d.ts +11 -49
- package/dist/mcp-gateway.js +44 -135
- package/dist/mcp-oauth.d.ts +10 -14
- package/dist/mcp-oauth.js +14 -32
- package/dist/mcp-probe.d.ts +0 -7
- package/dist/mcp-probe.js +4 -10
- package/dist/mcp-servers-store.d.ts +6 -6
- package/dist/mcp-servers-store.js +12 -14
- package/dist/mcp-store.js +3 -4
- package/dist/mcp.js +4 -3
- package/dist/model.d.ts +6 -7
- package/dist/model.js +43 -27
- package/dist/monid.d.ts +0 -2
- package/dist/monid.js +3 -6
- package/dist/openapi.d.ts +8 -1
- package/dist/openapi.js +121 -47
- package/dist/page.d.ts +1 -3
- package/dist/page.js +6 -18
- package/dist/paid-calls.d.ts +21 -27
- package/dist/paid-calls.js +25 -60
- package/dist/propose.js +83 -39
- package/dist/push-handler.d.ts +1 -1
- package/dist/reflect-range.d.ts +25 -0
- package/dist/reflect-range.js +30 -0
- package/dist/refusal-words.d.ts +5 -1
- package/dist/refusal-words.js +5 -1
- package/dist/rows.d.ts +1 -1
- package/dist/run-cut.d.ts +0 -6
- package/dist/run-cut.js +0 -6
- package/dist/run-db.d.ts +2 -11
- package/dist/run-db.js +2 -37
- package/dist/run-launch.d.ts +19 -2
- package/dist/run-launch.js +13 -5
- package/dist/run-learning-served.d.ts +10 -4
- package/dist/run-learning-served.js +23 -13
- package/dist/run-reads-over-store.d.ts +1 -1
- package/dist/run-reads-over-store.js +15 -6
- package/dist/run-secrets.d.ts +1 -1
- package/dist/run-timeline-served.d.ts +5 -1
- package/dist/run-timeline-served.js +15 -4
- package/dist/run-view.d.ts +7 -8
- package/dist/run-view.js +26 -7
- package/dist/runnable.d.ts +18 -10
- package/dist/runnable.js +21 -10
- package/dist/runner/adapter-loop.d.ts +6 -1
- package/dist/runner/adapter-loop.js +423 -53
- package/dist/runner/adapter-run-spec.d.ts +21 -8
- package/dist/runner/adapter-run-spec.js +90 -32
- package/dist/runner/adapters/index.js +11 -1
- package/dist/runner/adapters/pi.d.ts +12 -2
- package/dist/runner/adapters/pi.js +16 -5
- package/dist/runner/beside.d.ts +30 -3
- package/dist/runner/beside.js +36 -5
- package/dist/runner/claim-secrets.d.ts +10 -1
- package/dist/runner/claim-secrets.js +48 -14
- package/dist/runner/cli-run-spec.d.ts +2 -0
- package/dist/runner/cli-run-spec.js +45 -16
- package/dist/runner/commands.js +1 -1
- package/dist/runner/computer-journal.d.ts +15 -2
- package/dist/runner/computer-journal.js +41 -5
- package/dist/runner/conformance/harness.js +118 -8
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/dispatch.d.ts +8 -1
- package/dist/runner/dispatch.js +81 -7
- package/dist/runner/end-of-run.d.ts +8 -2
- package/dist/runner/end-of-run.js +10 -4
- package/dist/runner/github-app.js +1 -1
- package/dist/runner/harness-loops.d.ts +9 -1
- package/dist/runner/harness-loops.js +15 -2
- package/dist/runner/journal-kinds.d.ts +24 -6
- package/dist/runner/journal-kinds.js +20 -5
- package/dist/runner/journal-reader.d.ts +1 -1
- package/dist/runner/journal-reader.js +2 -2
- package/dist/runner/learning-ask.d.ts +4 -0
- package/dist/runner/learning-ask.js +15 -1
- package/dist/runner/learning-end.d.ts +18 -3
- package/dist/runner/learning-end.js +113 -11
- package/dist/runner/learning-loop.d.ts +5 -0
- package/dist/runner/learning-loop.js +26 -6
- package/dist/runner/main.d.ts +56 -9
- package/dist/runner/main.js +127 -18
- package/dist/runner/mcp-claim.js +1 -2
- package/dist/runner/mcp-run.d.ts +2 -0
- package/dist/runner/mcp-run.js +15 -5
- package/dist/runner/memory-claim.d.ts +9 -0
- package/dist/runner/memory-claim.js +25 -0
- package/dist/runner/metrics.d.ts +33 -1
- package/dist/runner/metrics.js +85 -1
- package/dist/runner/model-route.d.ts +13 -0
- package/dist/runner/model-route.js +133 -9
- package/dist/runner/reflect-hook.d.ts +12 -1
- package/dist/runner/reflect-hook.js +33 -3
- package/dist/runner/runner.d.ts +20 -0
- package/dist/runner/runner.js +56 -10
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +3 -3
- package/dist/runner/store-secrets.js +8 -12
- package/dist/runner/text-tools.d.ts +44 -0
- package/dist/runner/text-tools.js +237 -0
- package/dist/runner/workflow-loop.d.ts +6 -0
- package/dist/runner/workflow-loop.js +6 -4
- package/dist/runner/workflow-spec.d.ts +18 -0
- package/dist/runner/workflow-spec.js +63 -17
- package/dist/runs-over-store.d.ts +7 -2
- package/dist/runs-over-store.js +60 -14
- package/dist/seal-key.js +5 -43
- package/dist/sealed-settings-store.d.ts +19 -4
- package/dist/sealed-settings-store.js +9 -9
- package/dist/sealing.d.ts +4 -0
- package/dist/sealing.js +20 -0
- package/dist/secret-cut.d.ts +1 -1
- package/dist/secret-cut.js +1 -1
- package/dist/secret-name.d.ts +9 -0
- package/dist/secret-name.js +15 -0
- package/dist/secret-writes.d.ts +0 -1
- package/dist/secret-writes.js +0 -1
- package/dist/server-fs.d.ts +4 -0
- package/dist/server-fs.js +8 -2
- package/dist/server-lock.d.ts +0 -24
- package/dist/server-lock.js +1 -80
- package/dist/server-secrets-store.d.ts +4 -4
- package/dist/server-secrets-store.js +9 -31
- package/dist/server-secrets.d.ts +4 -9
- package/dist/server-secrets.js +12 -36
- package/dist/server.d.ts +16 -38
- package/dist/server.js +145 -106
- package/dist/sign-ins-store.d.ts +34 -36
- package/dist/sign-ins-store.js +27 -38
- package/dist/sign-ins.d.ts +2 -119
- package/dist/sign-ins.js +5 -311
- package/dist/standard-webhooks.d.ts +1 -1
- package/dist/standard-webhooks.js +1 -1
- package/dist/stats.d.ts +3 -2
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.d.ts +8 -2
- package/dist/store/agent-body.js +16 -4
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +2 -2
- package/dist/store/open.d.ts +6 -3
- package/dist/store/open.js +2 -2
- package/dist/store/postgres/connection.d.ts +18 -2
- package/dist/store/postgres/connection.js +156 -32
- package/dist/store/postgres/migrate.d.ts +4 -0
- package/dist/store/postgres/migrate.js +9 -2
- package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
- package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
- package/dist/store/postgres/store.d.ts +9 -2
- package/dist/store/postgres/store.js +9 -0
- package/dist/store/schema.js +1 -1
- package/dist/store/seal.js +2 -2
- package/dist/store/sqlite/connection.js +49 -5
- package/dist/store/sqlite/run-patch.d.ts +3 -0
- package/dist/store/sqlite/run-patch.js +3 -0
- package/dist/store/sqlite/store.js +41 -12
- package/dist/store/store.d.ts +35 -6
- package/dist/store/store.js +12 -2
- package/dist/store-reads/cap.d.ts +1 -1
- package/dist/store-reads/cap.js +22 -4
- package/dist/store-reads/journal.d.ts +9 -6
- package/dist/store-reads/journal.js +93 -17
- package/dist/store-reads/learning.d.ts +26 -8
- package/dist/store-reads/learning.js +12 -4
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-reads/workflow.js +1 -1
- package/dist/store-view.d.ts +5 -1
- package/dist/store-view.js +53 -18
- package/dist/template-eval.d.ts +60 -4
- package/dist/template-eval.js +120 -54
- package/dist/templates.d.ts +24 -10
- package/dist/templates.js +69 -38
- package/dist/timeline/group.d.ts +33 -0
- package/dist/timeline/group.js +77 -0
- package/dist/timeline/index.d.ts +7 -0
- package/dist/timeline/index.js +7 -0
- package/dist/timeline/labels.d.ts +79 -0
- package/dist/timeline/labels.js +150 -0
- package/dist/timeline/origin.d.ts +56 -0
- package/dist/timeline/origin.js +92 -0
- package/dist/timeline/render-text.d.ts +11 -0
- package/dist/timeline/render-text.js +62 -0
- package/dist/timeline/timeline.d.ts +274 -0
- package/dist/timeline/timeline.js +1308 -0
- package/dist/timeline/util.d.ts +17 -0
- package/dist/timeline/util.js +49 -0
- package/dist/tool-gate.d.ts +41 -216
- package/dist/tool-gate.js +79 -540
- package/dist/tool-keys-store.d.ts +11 -12
- package/dist/tool-keys-store.js +24 -31
- package/dist/tool-keys.d.ts +5 -101
- package/dist/tool-keys.js +8 -267
- package/dist/tool-plane.d.ts +14 -42
- package/dist/tool-plane.js +23 -81
- package/dist/tool-settings-store.d.ts +1 -2
- package/dist/tool-settings-store.js +5 -8
- package/dist/tools.d.ts +30 -18
- package/dist/tools.js +97 -107
- package/dist/trigger-adapter.d.ts +2 -2
- package/dist/trigger-targets.d.ts +13 -1
- package/dist/trigger-targets.js +31 -1
- package/dist/triggers.d.ts +2 -2
- package/dist/triggers.js +3 -3
- package/dist/turn-end.d.ts +1 -1
- package/dist/turn-end.js +1 -1
- package/dist/turns.d.ts +0 -6
- package/dist/turns.js +0 -19
- package/dist/uploads.js +1 -1
- package/dist/usage.d.ts +0 -2
- package/dist/usage.js +0 -2
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +23 -14
- package/dist/webhooks.d.ts +102 -76
- package/dist/webhooks.js +266 -233
- package/dist/workflow-run.d.ts +114 -0
- package/dist/workflow-run.js +144 -0
- package/dist/workspace/beneath.d.ts +19 -0
- package/dist/workspace/beneath.js +106 -0
- package/dist/workspace/computer-deps.d.ts +24 -6
- package/dist/workspace/computer-deps.js +108 -20
- package/dist/workspace/computer-for-claim.d.ts +12 -11
- package/dist/workspace/computer-for-claim.js +76 -15
- package/dist/workspace/computer-hold.d.ts +6 -1
- package/dist/workspace/computer-hold.js +6 -1
- package/dist/workspace/computer-janitor.d.ts +47 -0
- package/dist/workspace/computer-janitor.js +182 -0
- package/dist/workspace/grep-worker.d.ts +5 -0
- package/dist/workspace/grep-worker.js +9 -6
- package/dist/workspace/index.d.ts +3 -2
- package/dist/workspace/index.js +3 -3
- package/dist/workspace/local.d.ts +11 -2
- package/dist/workspace/local.js +161 -43
- package/dist/workspace/resolve-computer.d.ts +50 -10
- package/dist/workspace/resolve-computer.js +46 -11
- package/dist/workspace/run-tools.d.ts +13 -31
- package/dist/workspace/run-tools.js +10 -27
- package/dist/workspace-paths.d.ts +0 -17
- package/dist/workspace-paths.js +1 -41
- package/dist/workspace.d.ts +0 -14
- package/dist/workspace.js +3 -96
- package/package.json +13 -13
- package/templates/README.md +9 -3
- package/templates/market-monitor/agent.md +2 -2
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/dist/bound-files.d.ts +0 -4
- package/dist/bound-files.js +0 -164
- package/dist/coding-tools.d.ts +0 -47
- package/dist/coding-tools.js +0 -189
- package/dist/computer-adb.d.ts +0 -12
- package/dist/computer-adb.js +0 -14
- package/dist/console/assets/index-Dn0ngwG9.js +0 -535
- package/dist/console/assets/index-DvW6Aled.css +0 -1
- package/dist/grade.d.ts +0 -63
- package/dist/grade.js +0 -150
- package/dist/mcp-servers.d.ts +0 -269
- package/dist/mcp-servers.js +0 -456
- package/dist/packs.d.ts +0 -71
- package/dist/packs.js +0 -180
- package/dist/sealed-settings.d.ts +0 -60
- package/dist/sealed-settings.js +0 -171
- package/dist/sessions.d.ts +0 -6
- package/dist/sessions.js +0 -109
- package/dist/workspace/computer-serves.d.ts +0 -12
- package/dist/workspace/computer-serves.js +0 -15
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
|
|
2
2
|
import { createModels } from "@earendil-works/pi-ai/models";
|
|
3
|
-
import { createRegistry, Harness, LiveDoc, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
4
|
-
import { modelPortFor, RUNNER_RECORDS, sessionSpendOf } from "@parcha/agentrun-kernel";
|
|
5
|
-
import { ASK_PERSON_WAITING, endedAnswer, KINDS, withdrawnAnswers } from "./journal-kinds.js";
|
|
6
|
-
import { endedOnTurnEndingTool, HOST_PHASE, originOf, RUN_RESUMED } from "
|
|
3
|
+
import { createRegistry, defineDoc, Harness, LiveDoc, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
4
|
+
import { answeredUnpriced, modelPortFor, RUNNER_RECORDS, sessionSpendOf } from "@parcha/agentrun-kernel";
|
|
5
|
+
import { ASK_PERSON_WAITING, endedAnswer, KINDS, MESSAGE_SENT_AGAIN, withdrawnAnswers } from "./journal-kinds.js";
|
|
6
|
+
import { endedOnTurnEndingTool, HOST_PHASE, originOf, RUN_RESUMED } from "../timeline/index.js";
|
|
7
7
|
import { ClaimRefused, deniedText, endedBy, noteOf } from "./loop-contract.js";
|
|
8
|
-
import { deniedText as setToDenyText } from "../refusal-words.js";
|
|
8
|
+
import { deniedText as setToDenyText, ENDED_WHILE_WAITING, STOPPED_WHILE_WAITING } from "../refusal-words.js";
|
|
9
9
|
import { ADAPTER_API } from "../turn-end.js";
|
|
10
10
|
import { answerText, REFLECT_NOTE_ONLY, reflectRequestId, underLearning, WRITES } from "./end-of-run.js";
|
|
11
11
|
import { approvalOf, CLOSE_MS, CUT_STEP_MS, STOP_SETTLE_MS, commandRequest, cutLast, DEFAULT_RESTART_LIMIT, entriesOf, findData, Gate, heldReporter, Interrupted, KernelFault, Parked, quiet, restartLimited, writesEntry, writesSubmission } from "./loop.js";
|
|
@@ -13,9 +13,10 @@ import { answerOnce, HAND_BACK, handBackOf } from "./hand-back.js";
|
|
|
13
13
|
import { isComputerCommand, journalHold } from "./computer-journal.js";
|
|
14
14
|
import { COMPUTER_HANDOVER_KIND, COMPUTER_HOLD_KIND } from "../computer-commands.js";
|
|
15
15
|
import { journalSessions } from "./sessions.js";
|
|
16
|
-
import { DECISION_SESSION, runCap, TOOL_SESSION } from "@parcha/agentrun-kernel/kernel/cap";
|
|
16
|
+
import { DECISION_SESSION, overCapOf, runCap, TOOL_SESSION } from "@parcha/agentrun-kernel/kernel/cap";
|
|
17
17
|
import { portModels } from "../port-models.js";
|
|
18
18
|
import { bookedIn } from "../paid-calls.js";
|
|
19
|
+
import { withHostCalls } from "../host-calls.js";
|
|
19
20
|
import { sideEffect } from "./side-effects.js";
|
|
20
21
|
import { McpClaimRefused } from "./mcp-claim.js";
|
|
21
22
|
import { Fenced } from "../store/index.js";
|
|
@@ -46,6 +47,10 @@ export function hostLifecycle() {
|
|
|
46
47
|
export function agentConversation(record) {
|
|
47
48
|
return Number(record.id) === Number(ROOT_CONVERSATION_ID) || record.owner !== undefined;
|
|
48
49
|
}
|
|
50
|
+
/** On the run's root: the spec's onConversation writes for it are owed (a phase made the root while the spec was built,
|
|
51
|
+
* before the spec's lifecycle was known). Set in the root's creating commit, cleared in the commit that makes them, so a
|
|
52
|
+
* claim lost in between leaves them to the next claim. */
|
|
53
|
+
const RootWritesOwed = defineDoc({ kind: "agentrun.root-writes-owed", version: 1, scope: "conversation", history: "latest", fork: "initial", initial: () => ({ owed: false }), checkpointWhen: () => false });
|
|
49
54
|
/** The most of a harness's own turn error a failed run's summary keeps. */
|
|
50
55
|
const TURN_ERROR_MAX = 500;
|
|
51
56
|
/** `text` cut to at most `max` UTF-16 units, never inside a character: a high surrogate the cut leaves alone goes too. */
|
|
@@ -105,7 +110,8 @@ export function bookingOfAnswer(records, given) {
|
|
|
105
110
|
turns.push(turn.turn);
|
|
106
111
|
if (side.has(turn.turn))
|
|
107
112
|
continue;
|
|
108
|
-
|
|
113
|
+
// An answer with no price of its own is unknown, never the hold it was booked at for the cap (answeredUnpriced).
|
|
114
|
+
const figure = answeredUnpriced(turn) ? null : turn.bookedUsd !== undefined ? turn.bookedUsd : turn.usd;
|
|
109
115
|
usd = usd !== null && typeof figure === "number" && Number.isFinite(figure) ? (usd ?? 0) + figure : null;
|
|
110
116
|
}
|
|
111
117
|
return { turns, usd };
|
|
@@ -124,11 +130,28 @@ async function openCliRecord(harness, journal, context, now) {
|
|
|
124
130
|
// Each call started on the record: whether it has its end; and each end, by call id.
|
|
125
131
|
const started = new Map();
|
|
126
132
|
const ends = new Map();
|
|
133
|
+
// A call parked at its gate (its start marked parked) and its sent mark: one parked and never marked never left, so it
|
|
134
|
+
// is never ended interrupted; it waits for its answer, which ends it (run once, refused, or withdrawn).
|
|
135
|
+
const parked = new Set();
|
|
136
|
+
const sent = new Set();
|
|
137
|
+
// The command rows a message was about to be sent for (their intent marks): one sent again is a retry.
|
|
138
|
+
const sending = new Set();
|
|
127
139
|
let personWritten = false;
|
|
140
|
+
// Each end-of-run turn's prompt the record wrote as it asked the turn, by its request id: a turn asked again (a later
|
|
141
|
+
// claim, after a crash in the turn) reuses it, never writes a second.
|
|
142
|
+
const askedAt = new Map();
|
|
128
143
|
for (const entry of await entriesOf(journal, root.id, context)) {
|
|
129
144
|
const data = (entry.data ?? {});
|
|
145
|
+
if (entry.kind === KINDS.messageSending && typeof data.commandId === "number")
|
|
146
|
+
sending.add(data.commandId);
|
|
147
|
+
if (entry.kind === KINDS.endOfRunTurn && data.role === "person" && typeof data.requestId === "string")
|
|
148
|
+
askedAt.set(data.requestId, entry.id);
|
|
130
149
|
if (entry.kind === KINDS.callEnd && typeof data.callId === "string")
|
|
131
150
|
ends.set(data.callId, { status: data.status ?? "done", ...(typeof data.output === "string" ? { output: data.output } : {}) });
|
|
151
|
+
if (entry.kind === KINDS.callStart && typeof data.callId === "string" && data.parked === true)
|
|
152
|
+
parked.add(data.callId);
|
|
153
|
+
if (entry.kind === KINDS.callSent && typeof data.callId === "string")
|
|
154
|
+
sent.add(data.callId);
|
|
132
155
|
if (entry.kind === KINDS.callStart && typeof data.callId === "string")
|
|
133
156
|
started.set(data.callId, started.get(data.callId) ?? false);
|
|
134
157
|
else if (entry.kind === KINDS.callEnd && typeof data.callId === "string")
|
|
@@ -137,7 +160,7 @@ async function openCliRecord(harness, journal, context, now) {
|
|
|
137
160
|
personWritten = true;
|
|
138
161
|
}
|
|
139
162
|
for (const [callId, ended] of started) {
|
|
140
|
-
if (ended)
|
|
163
|
+
if (ended || (parked.has(callId) && !sent.has(callId)))
|
|
141
164
|
continue;
|
|
142
165
|
await append(KINDS.callEnd, { callId, status: "interrupted" });
|
|
143
166
|
started.set(callId, true);
|
|
@@ -187,15 +210,71 @@ async function openCliRecord(harness, journal, context, now) {
|
|
|
187
210
|
await harness.commit((tx) => tx.appendEntry(root.id, { kind: KINDS.input, data: { origin }, model: [{ role: "user", content: input, timestamp: now() }] }), context);
|
|
188
211
|
personWritten = true;
|
|
189
212
|
}),
|
|
213
|
+
message: (input, origin) => inTurn(async () => {
|
|
214
|
+
if (origin)
|
|
215
|
+
await harness.commit((tx) => tx.appendEntry(root.id, { kind: KINDS.input, data: { origin }, model: [{ role: "user", content: input, timestamp: now() }] }), context);
|
|
216
|
+
else
|
|
217
|
+
await append(KINDS.message, { role: "person", text: input });
|
|
218
|
+
}),
|
|
219
|
+
async sending(commandId) {
|
|
220
|
+
let again = false;
|
|
221
|
+
await inTurn(async () => {
|
|
222
|
+
again = sending.has(commandId);
|
|
223
|
+
if (again)
|
|
224
|
+
await harness.commit((tx) => tx.appendEntry(root.id, { kind: HOST_PHASE, data: { summary: MESSAGE_SENT_AGAIN, at: now() } }), context);
|
|
225
|
+
else
|
|
226
|
+
await harness.commit((tx) => tx.appendEntry(root.id, { kind: KINDS.messageSending, data: { commandId, at: now() } }), context);
|
|
227
|
+
sending.add(commandId);
|
|
228
|
+
});
|
|
229
|
+
return again;
|
|
230
|
+
},
|
|
190
231
|
async start(callId, tool, input, replaySafe) {
|
|
191
232
|
await flushText();
|
|
192
233
|
await inTurn(async () => {
|
|
193
|
-
if (started.has(callId))
|
|
234
|
+
if (started.has(callId)) {
|
|
235
|
+
// Parked at its gate and now sent: its mark first, in its own commit (the flush before the send makes it last).
|
|
236
|
+
if (parked.has(callId) && !sent.has(callId) && started.get(callId) === false) {
|
|
237
|
+
await harness.commit((tx) => tx.appendEntry(root.id, { kind: KINDS.callSent, data: { callId } }), context);
|
|
238
|
+
sent.add(callId);
|
|
239
|
+
}
|
|
194
240
|
return;
|
|
241
|
+
}
|
|
195
242
|
await append(KINDS.callStart, { callId, tool, input, replaySafe });
|
|
196
243
|
started.set(callId, false);
|
|
197
244
|
});
|
|
198
245
|
},
|
|
246
|
+
async park(callId, tool, input, replaySafe) {
|
|
247
|
+
await flushText();
|
|
248
|
+
await inTurn(async () => {
|
|
249
|
+
if (started.has(callId))
|
|
250
|
+
return;
|
|
251
|
+
await append(KINDS.callStart, { callId, tool, input, replaySafe, parked: true });
|
|
252
|
+
started.set(callId, false);
|
|
253
|
+
parked.add(callId);
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
async refused(callId, tool, input, replaySafe, words) {
|
|
257
|
+
await flushText();
|
|
258
|
+
await inTurn(async () => {
|
|
259
|
+
if (started.get(callId) === true)
|
|
260
|
+
return;
|
|
261
|
+
const at = now();
|
|
262
|
+
const start = { callId, tool, input, replaySafe, at };
|
|
263
|
+
const end = { callId, status: "refused", output: words, at };
|
|
264
|
+
await harness.commit(async (tx) => {
|
|
265
|
+
if (!started.has(callId))
|
|
266
|
+
await tx.appendEntry(root.id, { kind: KINDS.callStart, data: start });
|
|
267
|
+
await tx.appendEntry(root.id, { kind: KINDS.callEnd, data: end });
|
|
268
|
+
}, context);
|
|
269
|
+
started.set(callId, true);
|
|
270
|
+
ends.set(callId, { status: "refused", output: words });
|
|
271
|
+
});
|
|
272
|
+
},
|
|
273
|
+
unsentParked: () => [...parked].filter((callId) => !sent.has(callId) && started.get(callId) === false),
|
|
274
|
+
noteEnd(callId, status, output) {
|
|
275
|
+
started.set(callId, true);
|
|
276
|
+
ends.set(callId, { status, ...(output === undefined ? {} : { output }) });
|
|
277
|
+
},
|
|
199
278
|
endOf: (callId) => ends.get(callId),
|
|
200
279
|
end: (callId, status, output) => inTurn(async () => {
|
|
201
280
|
if (started.get(callId) !== false)
|
|
@@ -204,8 +283,18 @@ async function openCliRecord(harness, journal, context, now) {
|
|
|
204
283
|
started.set(callId, true);
|
|
205
284
|
ends.set(callId, { status, ...(output === undefined ? {} : { output }) });
|
|
206
285
|
}),
|
|
207
|
-
answering() {
|
|
286
|
+
answering: (requestId, prompt) => {
|
|
208
287
|
answer = "";
|
|
288
|
+
return inTurn(async () => {
|
|
289
|
+
if (askedAt.has(requestId))
|
|
290
|
+
return;
|
|
291
|
+
await harness.commit(async (tx) => {
|
|
292
|
+
if (await tx.submissionByRequest(root.id, requestId))
|
|
293
|
+
return;
|
|
294
|
+
const asked = await tx.appendEntry(root.id, { kind: KINDS.endOfRunTurn, data: { role: "person", text: prompt, requestId }, model: [{ role: "user", content: prompt, timestamp: now() }] });
|
|
295
|
+
askedAt.set(requestId, asked.id);
|
|
296
|
+
}, context);
|
|
297
|
+
});
|
|
209
298
|
},
|
|
210
299
|
answered: (requestId, prompt, status) => inTurn(async () => {
|
|
211
300
|
const said = answer ?? "";
|
|
@@ -213,7 +302,9 @@ async function openCliRecord(harness, journal, context, now) {
|
|
|
213
302
|
await harness.commit(async (tx) => {
|
|
214
303
|
if (await tx.submissionByRequest(root.id, requestId))
|
|
215
304
|
return;
|
|
216
|
-
|
|
305
|
+
// The prompt answering wrote as the turn was asked, else written now.
|
|
306
|
+
const written = askedAt.get(requestId);
|
|
307
|
+
const asked = written !== undefined ? { id: written } : await tx.appendEntry(root.id, { kind: KINDS.endOfRunTurn, data: { role: "person", text: prompt, requestId }, model: [{ role: "user", content: prompt, timestamp: now() }] });
|
|
217
308
|
if (status !== "completed") {
|
|
218
309
|
await tx.createSubmission({ type: "input", conversationId: root.id, requestId, status: "unanswered", entry: asked.id, reason: `the end-of-run turn ended ${status}` });
|
|
219
310
|
return;
|
|
@@ -244,7 +335,9 @@ function adapterLoop(specOf, options, deps) {
|
|
|
244
335
|
};
|
|
245
336
|
let stopped = () => { };
|
|
246
337
|
const stopping = new Promise((resolve) => (stopped = resolve));
|
|
247
|
-
/** Aborted by any stop of the run (AdapterRun.stopped, with the claim's signal)
|
|
338
|
+
/** Aborted by any stop of the run (AdapterRun.stopped, with the claim's signal): a host tool call in flight (a CLI's door
|
|
339
|
+
* call among them, whose own request signal its CLI may never abort) is cut with it, as pi's own abort cuts pi's calls.
|
|
340
|
+
* stop() aborts it first, with the stop's Interrupted, so the reason a cut call reads is the stop's. */
|
|
248
341
|
const stopAbort = new AbortController();
|
|
249
342
|
void stopping.then(() => stopAbort.abort(new Error("the run was stopped")));
|
|
250
343
|
let parked = () => { };
|
|
@@ -288,6 +381,9 @@ function adapterLoop(specOf, options, deps) {
|
|
|
288
381
|
/** While the end-of-run turn runs: a steer is left for the end, and its writes and edits under work/.learning/ are
|
|
289
382
|
* refused (REFLECT_NOTE_ONLY). */
|
|
290
383
|
let reflecting = false;
|
|
384
|
+
/** While the run's end waits for a person to hand its desktop or phone back (#2134): a steer is left for the end, as
|
|
385
|
+
* in the end-of-run turn, so no turn starts that the end, written after the hand-back, would cut. */
|
|
386
|
+
let handingBack = false;
|
|
291
387
|
/** A run a person's follow-up reopened (the store's runs.reopen: its journal holds the end of its last turn, its session
|
|
292
388
|
* opens idle): the follow-up's steer is this claim's turn, sent by steer() as a tracked turn (`sent`), and the claim waits
|
|
293
389
|
* for it, so the run's next end comes after that turn's answer. Undefined once the claim has read its delivered rows.
|
|
@@ -303,6 +399,14 @@ function adapterLoop(specOf, options, deps) {
|
|
|
303
399
|
* answer to a question that landed between the question's turn end and the wait's start. The wait takes it as its
|
|
304
400
|
* answer (waitInClaim), once. */
|
|
305
401
|
let idleSteer;
|
|
402
|
+
/** The messages a harness with no mid-turn steer (a CLI's: claude-agent, codex, opencode) took while its turn ran, in the
|
|
403
|
+
* order they came: each is sent as a turn of its own once the turn in flight ends, its row taken once the session holds
|
|
404
|
+
* it (sendAfterTurn) (#2174: refused here, a message the API had accepted was lost). Kept across a pass's restart; a row still here
|
|
405
|
+
* when the claim ends is untaken, so the run's next claim takes it again, or the run's end refuses it in the end's words. */
|
|
406
|
+
const afterTurn = [];
|
|
407
|
+
/** Set once the claim's last turn has ended and nothing more is sent: a message after it waits for the run's end, as one
|
|
408
|
+
* during the end-of-run turn does. */
|
|
409
|
+
let turnsOver = false;
|
|
306
410
|
/** The wait in the claim is over (an answer placed, a lapse, the run's end, park, restart or loss): its holds go. `of`:
|
|
307
411
|
* only while it is that wait's (a wait that settles late never ends a later one). */
|
|
308
412
|
const endWait = (of) => {
|
|
@@ -341,6 +445,13 @@ function adapterLoop(specOf, options, deps) {
|
|
|
341
445
|
/** The pass's MCP (spec.mcp), until the pass ends; and whether a command row came since its credentials were last made
|
|
342
446
|
* fresh (a parked token may have expired meanwhile). */
|
|
343
447
|
let mcp;
|
|
448
|
+
/** The calls of this run a person approved (an ask answered allow, on this claim or read from the journal on a resume),
|
|
449
|
+
* each with the arguments its approval card showed (its journaled ask's): an approval covers what that card named. */
|
|
450
|
+
const approvedCalls = new Map();
|
|
451
|
+
/** Each answered call's ask as journaled (the card the person saw), set by answerOf as it reads or writes the answer. */
|
|
452
|
+
const askedArgs = new Map();
|
|
453
|
+
/** An MCP tool of the run as the claim's pass wrapped it, by its catalog name (set once its tools are wrapped). */
|
|
454
|
+
let mcpHosted = () => undefined;
|
|
344
455
|
let mcpStale = false;
|
|
345
456
|
/** The run's raise state, read from its root: the newest budget entry with no raise entry after it is the pause it
|
|
346
457
|
* waits at (its pending cap is the cap a raise is checked against); the newest raise entry is its last raise. */
|
|
@@ -388,33 +499,48 @@ function adapterLoop(specOf, options, deps) {
|
|
|
388
499
|
await takeRaises();
|
|
389
500
|
return { taken: true };
|
|
390
501
|
};
|
|
502
|
+
/** A hand-back that reaches a loop whose pass is over, or never opened, waits for the run's next claim. */
|
|
503
|
+
const HAND_BACK_LATER = "the run is parking or ending: the hand-back waits for its next claim";
|
|
391
504
|
/** Hand-backs one at a time: each decides on the journal the one before it left, so a second answer meets the first. */
|
|
392
505
|
let handing = Promise.resolve();
|
|
393
506
|
/** A person's answer to the agent's browser hand-over (hand-back.ts): the first for an ask is its answer, committed as
|
|
394
507
|
* the answered hand-over entry at the row's own time, the row taken in that commit. A row after the ask lapsed writes
|
|
395
|
-
* the expiry (at the expiry's time) and is refused; one for an ask answered already, or never made, is refused.
|
|
508
|
+
* the expiry (at the expiry's time) and is refused; one for an ask answered already, or never made, is refused. `later`
|
|
509
|
+
* when this claim can no longer say, as an approval's (waitsFor): its Harness never opened, or its pass is over. */
|
|
396
510
|
const handBack = (command) => {
|
|
397
511
|
const turn = handing.then(async () => {
|
|
398
512
|
await Promise.race([ready, (finished ?? new Promise(() => { })).then(() => undefined, () => undefined)]);
|
|
399
513
|
const [theGate, theHarness, theRoot] = [gate, harness, root];
|
|
400
|
-
if (!theGate || !theHarness || !theRoot)
|
|
401
|
-
return {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
if (write && refused === undefined)
|
|
412
|
-
await landed;
|
|
413
|
-
return refused === undefined ? { taken: true } : { refused };
|
|
514
|
+
if (!theGate || !theHarness || !theRoot || theGate.sealed)
|
|
515
|
+
return { later: HAND_BACK_LATER };
|
|
516
|
+
try {
|
|
517
|
+
return await handBackThrough(command, theGate, theHarness, theRoot);
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
// Sealed while it read or wrote: closeSession seals, then closes the Harness the answer went through.
|
|
521
|
+
if (theGate.sealed)
|
|
522
|
+
return { later: HAND_BACK_LATER };
|
|
523
|
+
throw error;
|
|
524
|
+
}
|
|
414
525
|
});
|
|
415
526
|
handing = turn.catch(() => undefined);
|
|
416
527
|
return turn;
|
|
417
528
|
};
|
|
529
|
+
/** A hand-back's answer, decided and written through the pass's open Harness. */
|
|
530
|
+
const handBackThrough = async (command, theGate, theHarness, theRoot) => {
|
|
531
|
+
// The person's answer takes its row in the commit that writes it: staged for the answer this row would write (its ask,
|
|
532
|
+
// its outcome, at its own time). Decided and written in one commit (answerOnce), so no other writer's answer (the
|
|
533
|
+
// browser host's give or expiry) lands between: a row whose commit writes nothing, or the ask's expiry, takes none,
|
|
534
|
+
// and the staging matches nothing (an answered ask is never answered again).
|
|
535
|
+
const { ask, outcome } = (command.body ?? {});
|
|
536
|
+
const at = new Date(command.createdAt).toISOString();
|
|
537
|
+
const landed = typeof ask === "string" ? theGate.expect(writesEntry(KINDS.handover, (data) => data.id === ask && data.state === outcome && data.at === at), { take: [command.id] }) : undefined;
|
|
538
|
+
landed?.catch(() => undefined);
|
|
539
|
+
const { write, refused } = await answerOnce((change) => theHarness.commit(change, context), theRoot.id, (entries) => handBackOf(entries, command.body, command.createdAt));
|
|
540
|
+
if (write && refused === undefined)
|
|
541
|
+
await landed;
|
|
542
|
+
return refused === undefined ? { taken: true } : { refused };
|
|
543
|
+
};
|
|
418
544
|
/** A person's take-over or hand-back of the run's desktop or phone (computer-journal.ts): applied to the hold, and taken
|
|
419
545
|
* by the commit that writes the change it made (its entries carry the row's id); refused in words when it changed
|
|
420
546
|
* nothing. One at a time, behind the browser's hand-backs' turn: each meets the hold the one before it left. */
|
|
@@ -471,6 +597,8 @@ function adapterLoop(specOf, options, deps) {
|
|
|
471
597
|
// to answer. Left untaken, so the run's end says it came after the answer.
|
|
472
598
|
if (reflecting)
|
|
473
599
|
return { later: "the run has answered and is ending: the steer is not placed in its end-of-run turn" };
|
|
600
|
+
if (handingBack)
|
|
601
|
+
return { later: "the run has answered and waits for its computer to be handed back before it ends: the steer is not placed" };
|
|
474
602
|
const text = typeof command.body?.text === "string" ? command.body.text : "";
|
|
475
603
|
const requestId = commandRequest(command.id);
|
|
476
604
|
// A message that answers nothing (a trigger's, answers: false) waits for the turn's end and runs as a turn of its
|
|
@@ -510,11 +638,21 @@ function adapterLoop(specOf, options, deps) {
|
|
|
510
638
|
await taken.catch(() => { });
|
|
511
639
|
return { taken: true };
|
|
512
640
|
}
|
|
513
|
-
if (!session.steer)
|
|
514
|
-
return { refused: "this harness takes no steer mid-turn" };
|
|
515
641
|
// Who wrote it, when the person did not (a trigger's message): carried on its input, as the Timeline shows it.
|
|
516
642
|
const origin = originOf(command.body?.origin);
|
|
517
|
-
|
|
643
|
+
if (!session.steer) {
|
|
644
|
+
// A harness that takes no message mid-turn: the API answered it `follow_up` (it waits for the turn's end), so it is
|
|
645
|
+
// the run's next turn, after any taken before it (afterTurn). Taken now as the dispatch takes a row it buffers: the
|
|
646
|
+
// store's take is the commit of its submission, once sent.
|
|
647
|
+
if (turnsOver)
|
|
648
|
+
return { later: "the run has answered and is ending: the message waits for the run's end" };
|
|
649
|
+
afterTurn.push({ commandId: command.id, text, requestId, ...(origin ? { origin } : {}) });
|
|
650
|
+
return { taken: true };
|
|
651
|
+
}
|
|
652
|
+
// A person's answer to the question the last turn ended on (a parked ask's, its run claimed again by it): the row's
|
|
653
|
+
// waiting summary goes in its commit, as an in-claim wait's answer clears it. Left until the end commit, the answer's
|
|
654
|
+
// turn reads running, held and "Waiting for your reply", and the server tells run.needs_input for that ask again.
|
|
655
|
+
const taken = gate.expect(writesSubmission(requestId), { take: [command.id], ...(askedLast ? { run: { summary: null } } : {}) });
|
|
518
656
|
// A seal while the steer is placed fails it before it is awaited: handled here.
|
|
519
657
|
taken.catch(() => undefined);
|
|
520
658
|
const theGate = gate;
|
|
@@ -536,6 +674,31 @@ function adapterLoop(specOf, options, deps) {
|
|
|
536
674
|
await taken.catch(() => { });
|
|
537
675
|
return { taken: true };
|
|
538
676
|
};
|
|
677
|
+
/** A message the turn in flight could not take (afterTurn), sent as a turn of its own. Its row is taken only once the
|
|
678
|
+
* session holds the prompt (send resolves once it is placed): in the commit that writes it on the run's record, right
|
|
679
|
+
* after the send (a CLI's session writes no submission to the run's journal), or, for an adapter keeping its own record,
|
|
680
|
+
* in its submission's commit. A runner lost before that leaves the row untaken, so the next claim sends it again: a
|
|
681
|
+
* message may be sent twice after a crash, never lost. Its turn's end is what this gives. */
|
|
682
|
+
const sendAfterTurn = async (theGate, next) => {
|
|
683
|
+
// Its intent mark, durable before the send (the flush): a runner lost between the send and the take leaves the mark,
|
|
684
|
+
// and the next claim's send of the same row says it is sent again (CliRecord.sending), never silently paid twice.
|
|
685
|
+
if (cliRecord) {
|
|
686
|
+
await cliRecord.sending(next.commandId);
|
|
687
|
+
await deps.flush();
|
|
688
|
+
}
|
|
689
|
+
const origin = next.origin === undefined ? undefined : JSON.stringify(next.origin);
|
|
690
|
+
const match = !cliRecord
|
|
691
|
+
? writesSubmission(next.requestId)
|
|
692
|
+
: origin !== undefined
|
|
693
|
+
? writesEntry(KINDS.input, (data) => JSON.stringify(data.origin) === origin)
|
|
694
|
+
: writesEntry(KINDS.message, (data) => data.role === "person" && data.text === next.text);
|
|
695
|
+
const taken = theGate.expect(match, { take: [next.commandId] });
|
|
696
|
+
taken.catch(() => undefined);
|
|
697
|
+
await session.send(next.text, { id: next.requestId, ...(next.origin ? { origin: next.origin } : {}) });
|
|
698
|
+
await specNow?.hooks?.afterMessageSent?.();
|
|
699
|
+
await cliRecord?.message(next.text, next.origin);
|
|
700
|
+
return session.turnEnd();
|
|
701
|
+
};
|
|
539
702
|
async function run(claim) {
|
|
540
703
|
context = withAbortSignal(deps.signal, BACKGROUND_CONTEXT);
|
|
541
704
|
// Every row the runner read for this claim is with the loop once delivered resolves (at once without a runner's).
|
|
@@ -561,6 +724,11 @@ function adapterLoop(specOf, options, deps) {
|
|
|
561
724
|
// The spec's phases said before the run's root exists (AdapterRun.phase), written in the root's creating commit.
|
|
562
725
|
const phasesBeforeRoot = [];
|
|
563
726
|
let rootMade = false;
|
|
727
|
+
// A root made by a phase while the spec was built (below), before the spec's own lifecycle was known: its
|
|
728
|
+
// onConversation writes for the root are owed (RootWritesOwed, on the root), made once the spec is (after specOf).
|
|
729
|
+
let specKnown = false;
|
|
730
|
+
/** The root a phase is making, awaited before the owed writes are read. */
|
|
731
|
+
let rootMaking;
|
|
564
732
|
harness = await Harness.open(gate.journal, {
|
|
565
733
|
models: () => models, registry, settings: { restartLimit }, onReport: () => { },
|
|
566
734
|
// A tool host's onConversation, in each agent conversation's creating commit (the spec's, once it is known: the
|
|
@@ -572,6 +740,10 @@ function adapterLoop(specOf, options, deps) {
|
|
|
572
740
|
rootMade = true;
|
|
573
741
|
for (const data of phasesBeforeRoot.splice(0))
|
|
574
742
|
await tx.appendEntry(record.id, { kind: HOST_PHASE, data });
|
|
743
|
+
if (!specKnown) {
|
|
744
|
+
(await tx.doc(RootWritesOwed, record.id)).owed = true;
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
575
747
|
}
|
|
576
748
|
for (const write of specNow?.lifecycle?.onConversation ?? [])
|
|
577
749
|
await write(tx, Number(record.id));
|
|
@@ -619,14 +791,28 @@ function adapterLoop(specOf, options, deps) {
|
|
|
619
791
|
pause: async (refusal) => {
|
|
620
792
|
await pauseFor?.(refusal);
|
|
621
793
|
},
|
|
794
|
+
flush: () => deps.flush(),
|
|
622
795
|
};
|
|
623
|
-
// A phase of the host's
|
|
796
|
+
// A phase of the host's, on the run's timeline while its step runs. Said before the root exists (the claim's computer
|
|
797
|
+
// waiting on its image build, inside specOf), it makes the root then, the row in its creating commit (above), so a
|
|
798
|
+
// person sees the row during the wait, and a failed step keeps it (run()'s catch writes the end on that root). The
|
|
799
|
+
// session opens onto that root later and sets its agent there (pi.ts configures it on every open). Else at once.
|
|
624
800
|
const phasing = harness;
|
|
625
801
|
const phase = (summary) => {
|
|
626
802
|
const data = { summary, at: Date.now() };
|
|
627
803
|
void (async () => {
|
|
628
804
|
if (!rootMade && !(await phasing.conversation(ROOT_CONVERSATION_ID, context)) && !rootMade) {
|
|
629
805
|
phasesBeforeRoot.push(data);
|
|
806
|
+
rootMaking = phasing.root(context);
|
|
807
|
+
await rootMaking;
|
|
808
|
+
// The root was made by another write meanwhile (its creating commit took none of these): written now.
|
|
809
|
+
const left = phasesBeforeRoot.splice(0);
|
|
810
|
+
if (left.length === 0)
|
|
811
|
+
return;
|
|
812
|
+
await phasing.commit(async (tx) => {
|
|
813
|
+
for (const one of left)
|
|
814
|
+
await tx.appendEntry(ROOT_CONVERSATION_ID, { kind: HOST_PHASE, data: one });
|
|
815
|
+
}, context);
|
|
630
816
|
return;
|
|
631
817
|
}
|
|
632
818
|
await phasing.commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: HOST_PHASE, data }), context);
|
|
@@ -654,14 +840,39 @@ function adapterLoop(specOf, options, deps) {
|
|
|
654
840
|
if (error instanceof ClaimRefused)
|
|
655
841
|
return await writeEnd(harness, await harness.root(context), gate, { state: "failed", summary: error.message }, []);
|
|
656
842
|
// A person's stop while the spec was built (it waited on a harness's install): the run ends stopped, in the stop's
|
|
657
|
-
// words, its row taken; the work it waited on goes on for the next run.
|
|
843
|
+
// words, its row taken; the work it waited on goes on for the next run. A stop wins over the build's own failure.
|
|
658
844
|
if (stopReason?.kind === "command")
|
|
659
845
|
return await writeEnd(harness, await harness.root(context), gate, { state: "stopped", summary: endedBy(stopReason.cancel) }, [stopReason.commandId]);
|
|
846
|
+
// A spec that failed after a phase made the run's root (its computer's image build failed): the end goes on that
|
|
847
|
+
// root with why (said: cut of every secret the runner holds), as run()'s catch writes it once the session's root
|
|
848
|
+
// is known, so the timeline keeps the phase row and reads the failure after it.
|
|
849
|
+
await rootMaking?.catch(() => undefined);
|
|
850
|
+
if (rootMade && claim.from !== "parked" && !gate.sealed && !deps.signal.aborted) {
|
|
851
|
+
const ended = await writeEnd(harness, await harness.root(context), gate, { state: "failed", summary: said(error) }, []).catch(() => undefined);
|
|
852
|
+
if (ended)
|
|
853
|
+
return ended;
|
|
854
|
+
}
|
|
660
855
|
throw error;
|
|
661
856
|
}
|
|
662
857
|
mcp = spec.mcp;
|
|
663
858
|
specNow = spec;
|
|
859
|
+
specKnown = true;
|
|
860
|
+
// The spec's resources are the pass's from here: a claim lost in any write below still lets them go (closeSession).
|
|
664
861
|
releaseNow = spec.release;
|
|
862
|
+
// A root a phase made while the spec was built (on this claim or an earlier one lost before this point) took none
|
|
863
|
+
// of the spec's onConversation writes: they are made now, in a commit of their own, before anything of the
|
|
864
|
+
// session's. A root still being made is waited for first (made from here on, its creating commit takes them itself).
|
|
865
|
+
await rootMaking?.catch(() => undefined);
|
|
866
|
+
if (await harness.conversation(ROOT_CONVERSATION_ID, context)) {
|
|
867
|
+
await harness.commit(async (tx) => {
|
|
868
|
+
const owed = await tx.doc(RootWritesOwed, ROOT_CONVERSATION_ID);
|
|
869
|
+
if (!owed.owed)
|
|
870
|
+
return;
|
|
871
|
+
owed.owed = false;
|
|
872
|
+
for (const write of spec.lifecycle?.onConversation ?? [])
|
|
873
|
+
await write(tx, Number(ROOT_CONVERSATION_ID));
|
|
874
|
+
}, context);
|
|
875
|
+
}
|
|
665
876
|
// A mark of the run's commits through its own Harness, under its fence.
|
|
666
877
|
const opened_ = harness;
|
|
667
878
|
spec.harnessReady?.((change, ctx) => opened_.commit(change, ctx));
|
|
@@ -681,8 +892,35 @@ function adapterLoop(specOf, options, deps) {
|
|
|
681
892
|
let pausedAtRefusal = false;
|
|
682
893
|
/** A paid call's refusal that pauses the run (ledger.pause): the run's next request is refused in its words. */
|
|
683
894
|
let pendingPause;
|
|
895
|
+
/** How the run ends once its booked spend passed its cap (the kernel's overCapEnd, main's run end): every session's
|
|
896
|
+
* booked spend summed, as the cap reads it; the bound, its model session's requests' since the cost last fit, so a
|
|
897
|
+
* cache miss within that bound pauses the run at its next request instead. Undefined while it fits, with no cap, or
|
|
898
|
+
* with a spend unknown. */
|
|
899
|
+
const overCap = async () => {
|
|
900
|
+
// A run whose spec gives no cap (no runCap, or no cap's rule) has none to pass.
|
|
901
|
+
if (!spec.cap || !spec.runCap)
|
|
902
|
+
return undefined;
|
|
903
|
+
const cap = capNow();
|
|
904
|
+
if (cap === undefined)
|
|
905
|
+
return undefined;
|
|
906
|
+
let others = 0;
|
|
907
|
+
for (const key of await sessions.keys()) {
|
|
908
|
+
if (key === spec.session.key)
|
|
909
|
+
continue;
|
|
910
|
+
const spent = sessionSpendOf(await sessions.read(key)).spentUsd;
|
|
911
|
+
if (spent === null)
|
|
912
|
+
return undefined;
|
|
913
|
+
others += spent;
|
|
914
|
+
}
|
|
915
|
+
// Its model session read as the cap reads it (the kernel's overCapOf: a request cut by a crash at its hold plus its
|
|
916
|
+
// bound, and that bound kept, so a possible cache miss pauses the run, never fails it).
|
|
917
|
+
return overCapOf(await sessions.read(spec.session.key), others, cap);
|
|
918
|
+
};
|
|
684
919
|
const beforeRequest = async (request) => {
|
|
685
|
-
|
|
920
|
+
// A run whose booked spend passed its cap sends nothing more: it ends in the cap's words (overCap), never paused,
|
|
921
|
+
// whatever else would pause it (a paid call's refusal, pendingPause).
|
|
922
|
+
const over = await overCap();
|
|
923
|
+
const answer = over ? { refuse: over, forGood: true } : (pendingPause ?? (await theCap().before(request)));
|
|
686
924
|
if (answer && "refuse" in answer) {
|
|
687
925
|
refused = answer;
|
|
688
926
|
await pauseAt(answer);
|
|
@@ -731,17 +969,26 @@ function adapterLoop(specOf, options, deps) {
|
|
|
731
969
|
// provider may have billed it, so it is booked once, as cut, at its hold and bound, and told as any answered request
|
|
732
970
|
// is, before this claim sends anything (the harness runner's bookCut, main's cut booking).
|
|
733
971
|
await booking.bookCut();
|
|
972
|
+
// A paid tool call an earlier claim admitted and never saw settled is booked the same way, cut, at its hold, its
|
|
973
|
+
// charge unknown: it may have run and been charged before the claim was lost. Its session opens only for a paid call
|
|
974
|
+
// of this claim, and a replay the gate refuses makes none, so it is opened here for the cut alone, never read as free.
|
|
975
|
+
const toolsAtOpen = sessionSpendOf(await sessions.read(TOOL_SESSION));
|
|
976
|
+
if (spec.runCap && toolsAtOpen.turns.some((turn) => !toolsAtOpen.answered.has(turn)))
|
|
977
|
+
await paidOf();
|
|
734
978
|
const gateOf = preIntentGate(spec, hooks);
|
|
735
979
|
// A CLI's run: the loop keeps its visible record (record: "loop"), every call a crash or a fault left open ended.
|
|
736
980
|
const record = spec.record === "loop" ? await openCliRecord(harness, gate.journal, context, now) : undefined;
|
|
737
981
|
cliRecord = record;
|
|
738
|
-
|
|
982
|
+
// Each tool the session runs on the host, the host's own and any its adapter adds (SessionIO.hostTool: a CLI's
|
|
983
|
+
// read_skill_file), the same way: a CLI's call on its record, so the run's Timeline shows it and its result.
|
|
984
|
+
const hosted = (tool) => ({
|
|
739
985
|
...tool,
|
|
740
986
|
// The rule before a side effect, on the host-tool path every adapter's calls take: after the call's intent, a
|
|
741
987
|
// flush unless the tool is replay-safe, the call, then the beforeResult point. A throw there is the kernel's own
|
|
742
988
|
// fault: the journal is sealed before the harness's result commit, so the Session fails with the call before
|
|
743
989
|
// its result, and the loop restarts it. A CLI's call reaches here only once the gate allowed it (its tool
|
|
744
|
-
// bridge asks first
|
|
990
|
+
// bridge asks first; one refused is on the record from the gate, bridgeGate), so its record's start is written
|
|
991
|
+
// here, before the flush and the call, and its end after.
|
|
745
992
|
call: async (args, toolContext) => {
|
|
746
993
|
// A call its CLI issues again under an id the record already ended (after the CLI's own resume) is answered
|
|
747
994
|
// from that end, never run again.
|
|
@@ -756,7 +1003,9 @@ function adapterLoop(specOf, options, deps) {
|
|
|
756
1003
|
flush: () => deps.flush(),
|
|
757
1004
|
// Its paid requests booked under this call, on the run's one ledger (paid-calls.ts bookedIn, Ledger.paid); one
|
|
758
1005
|
// refused for room pauses the run (Ledger.pause, the run's pause), as main's paidCall does.
|
|
759
|
-
|
|
1006
|
+
// The calls it may make of the run's other tools (a learned workflow's MCP nodes: host-calls.ts), each as this
|
|
1007
|
+
// loop wrapped it, and whether a person approved this call.
|
|
1008
|
+
route: () => bookedIn({ call: toolContext.callId, record: async () => { }, paid, pause: ledger.pause }, () => withHostCalls({ mcp: mcpHosted, approved: approvedCalls.get(toolContext.callId) }, () => tool.call(args, { ...toolContext, signal: toolContext.signal ? AbortSignal.any([toolContext.signal, stopAbort.signal]) : stopAbort.signal }))),
|
|
760
1009
|
beforeResult: async (answer) => {
|
|
761
1010
|
const call = { callId: toolContext.callId, tool: tool.name, args: (args ?? {}) };
|
|
762
1011
|
try {
|
|
@@ -769,8 +1018,9 @@ function adapterLoop(specOf, options, deps) {
|
|
|
769
1018
|
}
|
|
770
1019
|
},
|
|
771
1020
|
});
|
|
1021
|
+
// A call a stop cut answers whatever its tool said of the abort: it ends interrupted, never as its outcome.
|
|
772
1022
|
if (record)
|
|
773
|
-
await record.end(toolContext.callId, answer.isError ? "failed" : "done", answer.text);
|
|
1023
|
+
await record.end(toolContext.callId, stopAbort.signal.aborted ? "interrupted" : answer.isError ? "failed" : "done", answer.text);
|
|
774
1024
|
return answer;
|
|
775
1025
|
}
|
|
776
1026
|
catch (error) {
|
|
@@ -781,7 +1031,14 @@ function adapterLoop(specOf, options, deps) {
|
|
|
781
1031
|
throw error;
|
|
782
1032
|
}
|
|
783
1033
|
},
|
|
784
|
-
})
|
|
1034
|
+
});
|
|
1035
|
+
const tools = [...spec.session.tools, ...(mcp?.tools ?? [])].map(hosted);
|
|
1036
|
+
// An MCP tool of the run by its catalog name, as wrapped above: what a learned workflow's MCP node calls.
|
|
1037
|
+
mcpHosted = (catalog) => {
|
|
1038
|
+
const wire = mcp?.tools.find((one) => mcp?.catalogOf(one.name) === catalog);
|
|
1039
|
+
const host = wire && tools.find((one) => one.name === wire.name);
|
|
1040
|
+
return host && ((args, ctx) => host.call(args, ctx));
|
|
1041
|
+
};
|
|
785
1042
|
// The session's model port, counting what it admitted and has not booked yet (a stop's booking comes once the
|
|
786
1043
|
// session's proxy sees its upstream end, which can be after pi went idle).
|
|
787
1044
|
const port = booking.models;
|
|
@@ -824,15 +1081,41 @@ function adapterLoop(specOf, options, deps) {
|
|
|
824
1081
|
given.add(turn);
|
|
825
1082
|
return booking.usd;
|
|
826
1083
|
};
|
|
827
|
-
|
|
1084
|
+
// The gate a CLI's tool bridge asks before anything of a call: a call it refuses never reaches its host tool, so its
|
|
1085
|
+
// start and its end, refused in the gate's words, go on the record here, as pi writes a refused call and its error
|
|
1086
|
+
// result (a deny set, no one to approve, or a person's deny of its ask on the claim that resumes it).
|
|
1087
|
+
const bridgeGate = record
|
|
1088
|
+
? async (call, hookContext) => {
|
|
1089
|
+
const answer = await gateOf(call, hookContext);
|
|
1090
|
+
if (answer !== "allow" && "deny" in answer) {
|
|
1091
|
+
const tool = tools.find((one) => one.name === call.tool);
|
|
1092
|
+
await record.refused(call.callId, call.tool, call.args ?? {}, tool?.replay === "safe", answer.deny);
|
|
1093
|
+
}
|
|
1094
|
+
return answer;
|
|
1095
|
+
}
|
|
1096
|
+
: gateOf;
|
|
1097
|
+
const io = { writer, models: counted, signal: deps.signal, onEvent: record ? (event) => record.onEvent(event) : () => { }, gate: bridgeGate, hostTool: hosted, bookedCost };
|
|
828
1098
|
// The MCP tools: pi's own (host tools, through the gate above); for a harness with its own MCP client, the session's
|
|
829
1099
|
// MCP its adapter mounts on its proxy, each call asking the gate first (an ask parks the run, nothing sent).
|
|
830
1100
|
const mcpNames = new Set(mcp?.tools.map((tool) => tool.name));
|
|
831
1101
|
const ownClient = mcp !== undefined && spec.adapter.capabilities().mcpClient !== undefined;
|
|
832
1102
|
const sessionMcp = ownClient
|
|
833
1103
|
? mcp.session(new Map(tools.filter((tool) => mcpNames.has(tool.name)).map((tool) => [tool.name, tool])), async (call) => {
|
|
1104
|
+
// A call through the door is on the run's record before its gate answers (once per call id; the gate's park
|
|
1105
|
+
// seals the journal): its row, and an approval read beside it, show while it waits. A refused one ends there;
|
|
1106
|
+
// a parked one runs, or is refused, on the claim that resumes it (parkedFollowUp); an allowed one runs now.
|
|
1107
|
+
const wire = mcp.tools.find((tool) => mcp.catalogOf(tool.name) === call.tool);
|
|
1108
|
+
if (record)
|
|
1109
|
+
await record.park(call.callId, wire?.name ?? call.tool, call.args ?? {}, wire?.replay === "safe");
|
|
834
1110
|
const answer = await gateOf(call, context);
|
|
835
|
-
|
|
1111
|
+
if (answer === "allow")
|
|
1112
|
+
return undefined;
|
|
1113
|
+
if ("deny" in answer) {
|
|
1114
|
+
if (record)
|
|
1115
|
+
await record.end(call.callId, "refused", answer.deny);
|
|
1116
|
+
return { refused: answer.deny };
|
|
1117
|
+
}
|
|
1118
|
+
return { parked: true };
|
|
836
1119
|
})
|
|
837
1120
|
: undefined;
|
|
838
1121
|
session = await spec.adapter.open({ ...spec.session, tools: ownClient ? tools.filter((tool) => !mcpNames.has(tool.name)) : tools, ...(sessionMcp ? { mcp: sessionMcp } : {}) }, io);
|
|
@@ -847,7 +1130,8 @@ function adapterLoop(specOf, options, deps) {
|
|
|
847
1130
|
for (const entry of entries)
|
|
848
1131
|
await tx.appendEntry(rootOf.id, { kind: entry.kind, data: entry.data });
|
|
849
1132
|
}, context), now);
|
|
850
|
-
|
|
1133
|
+
// The run's journal so far: a take an earlier claim's computer left in force ends released here (computer-journal.ts).
|
|
1134
|
+
await computerNow.up(await entriesOf(gate.journal, root.id, context));
|
|
851
1135
|
}
|
|
852
1136
|
// The cap a raise left the run at, from its root (the newest raise entry), before any request of this claim: a
|
|
853
1137
|
// claim that takes over a raised run (a resume, another runner's) holds it to the raise, never the launch's cap.
|
|
@@ -922,8 +1206,14 @@ function adapterLoop(specOf, options, deps) {
|
|
|
922
1206
|
return now === "deny" ? setToDenyText(call.tool) : typeof now === "object" ? now.deny : undefined;
|
|
923
1207
|
},
|
|
924
1208
|
run: (call) => wrapped.get(mcp.tools.find((tool) => mcp.catalogOf(tool.name) === call.tool).name).call(call.args, { callId: call.callId, signal: deps.signal }),
|
|
1209
|
+
// A parked call refused ends on the run's record (its start was written at its park), in the refusal's words.
|
|
1210
|
+
refused: async (call, text) => cliRecord?.end(call.callId, "refused", text),
|
|
925
1211
|
});
|
|
926
1212
|
if (follow === "park") {
|
|
1213
|
+
// A person's stop (or cancel) waiting here is taken: the run ends stopped, its waiting calls withdrawn in the
|
|
1214
|
+
// end's commit. Released parked instead, its untaken stop would have it claimed again at once, and again.
|
|
1215
|
+
if (stopReason?.kind === "command")
|
|
1216
|
+
return await writeEnd(harness, root, gate, { state: "stopped", summary: endedBy(stopReason.cancel) }, [stopReason.commandId], await cutRows(claim, spec, "stopped"));
|
|
927
1217
|
gate.seal(new Parked());
|
|
928
1218
|
return { state: "parked" };
|
|
929
1219
|
}
|
|
@@ -1019,8 +1309,14 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1019
1309
|
await harness.waitForIdle(context);
|
|
1020
1310
|
const settledEntries = await entriesOf(gate.journal, root.id, context);
|
|
1021
1311
|
const cutOff = restartLimited(settledEntries);
|
|
1312
|
+
// A turn whose booked spend passed the run's cap ends the run failed in the cap's words (overCap), its crossing
|
|
1313
|
+
// answer booked: main's end of a run over its cap, before any pause (a question to the person, a cap refusal).
|
|
1314
|
+
await bookingsSettled();
|
|
1315
|
+
const over = cutOff ? undefined : await overCap();
|
|
1022
1316
|
if (cutOff)
|
|
1023
1317
|
data = { state: "failed", summary: `a tool call faulted on every restart: ${cutOff}` };
|
|
1318
|
+
else if (over)
|
|
1319
|
+
data = { state: "failed", summary: over };
|
|
1024
1320
|
else if (outcome.status !== "completed" && refused) {
|
|
1025
1321
|
// A turn the host's check refused: what that does to the run is the cap's rule (parkOf), as on main. A refusal a
|
|
1026
1322
|
// raise could fix parks the run on a raise; one no raise could fix (or a check with no cap) fails it, in the
|
|
@@ -1065,12 +1361,35 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1065
1361
|
// An answer cut at the model's output limit is unfinished: the run fails, saying so, never ended (loop.ts cutLast,
|
|
1066
1362
|
// the direct loop's rule: the words are loop-contract's cutAtLimit).
|
|
1067
1363
|
const cut = cutLast(settledEntries);
|
|
1364
|
+
// A message taken while the turn ran (afterTurn): the run's next turn, on the same session, before its end.
|
|
1365
|
+
const next = cut ? undefined : afterTurn.shift();
|
|
1366
|
+
if (next) {
|
|
1367
|
+
turn = sendAfterTurn(gate, next);
|
|
1368
|
+
continue;
|
|
1369
|
+
}
|
|
1068
1370
|
data = cut ? { state: "failed", summary: cut } : { state: "ended" };
|
|
1069
1371
|
}
|
|
1070
1372
|
break;
|
|
1071
1373
|
}
|
|
1374
|
+
// No turn is sent after this one: a message from here on waits for the run's end (steer).
|
|
1375
|
+
turnsOver = true;
|
|
1072
1376
|
if (parkOn !== undefined)
|
|
1073
1377
|
return await parkOnAsk(harness, root, gate, parkOn);
|
|
1378
|
+
// A person who holds the run's desktop or phone keeps it (#2134): the run's end, and its computer's release after
|
|
1379
|
+
// it, wait for the hand-back or the hold's idle bound, the Timeline saying why. A stop ends the run stopped at
|
|
1380
|
+
// once, and a lost claim or a closing runner leaves it for the next claim, as during the end-of-run step.
|
|
1381
|
+
if (computerNow) {
|
|
1382
|
+
const aborted = new Promise((resolve) => (deps.signal.aborted ? resolve() : deps.signal.addEventListener("abort", () => resolve(), { once: true })));
|
|
1383
|
+
// Until the run returns: a steer after the hand-back would start a turn its end then cuts, too.
|
|
1384
|
+
handingBack = true;
|
|
1385
|
+
await computerNow.heldAtEnd(Promise.race([stopping, aborted, harness.closed]));
|
|
1386
|
+
// Set by stop() during the wait: read past the narrowing TypeScript did before it.
|
|
1387
|
+
const cut = stopReason;
|
|
1388
|
+
if (cut?.kind === "command")
|
|
1389
|
+
return await writeEnd(harness, root, gate, { state: "stopped", summary: endedBy(cut.cancel) }, [cut.commandId], await cutRows(claim, spec, "stopped"));
|
|
1390
|
+
if (cut || deps.signal.aborted)
|
|
1391
|
+
return await interrupted(cut?.kind ?? "its claim's signal aborted");
|
|
1392
|
+
}
|
|
1074
1393
|
// The end-of-run step, once, before the end: its rows ride in the end commit. A stop or a lost claim during it
|
|
1075
1394
|
// writes no end of the job's (a person's stop ends the run stopped; a lost claim leaves it for the next claim).
|
|
1076
1395
|
const step = await endOfRunStep(claim, spec, data.state === "failed" ? "failed" : "completed");
|
|
@@ -1078,6 +1397,15 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1078
1397
|
return await writeEnd(harness, root, gate, { state: "stopped", summary: endedBy(stopReason?.kind === "command" ? stopReason.cancel : undefined) }, [step.commandId], await cutRows(claim, spec, "stopped-looking"));
|
|
1079
1398
|
if (step.kind === "interrupted")
|
|
1080
1399
|
return await interrupted(stopReason?.kind ?? "its claim's signal aborted");
|
|
1400
|
+
// The end-of-run turn is one more paid answer: one that passed the cap ends the run in the cap's words (overCap,
|
|
1401
|
+
// read again once its booking settled), as a job's answer that passed it does, never completed. An end already
|
|
1402
|
+
// failed keeps its own words.
|
|
1403
|
+
if (data.state !== "failed") {
|
|
1404
|
+
await bookingsSettled();
|
|
1405
|
+
const overAtEnd = await overCap();
|
|
1406
|
+
if (overAtEnd)
|
|
1407
|
+
data = { state: "failed", summary: overAtEnd };
|
|
1408
|
+
}
|
|
1081
1409
|
return await writeEnd(harness, root, gate, data, [], step.also, failedWith);
|
|
1082
1410
|
}
|
|
1083
1411
|
}
|
|
@@ -1102,6 +1430,7 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1102
1430
|
held?.detach();
|
|
1103
1431
|
computerNow?.detach();
|
|
1104
1432
|
computerNow = undefined;
|
|
1433
|
+
handingBack = false;
|
|
1105
1434
|
deps.live?.(() => undefined);
|
|
1106
1435
|
// A tool host's beforeRelease, when no end or park let it go first (a shutdown, a lost claim): the Harness still open.
|
|
1107
1436
|
// A restart within the claim is no release. What it writes after a lost claim is refused by the fence, as any write.
|
|
@@ -1130,9 +1459,22 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1130
1459
|
return { deny: setToDenyText(asked) };
|
|
1131
1460
|
if (decision === "allow")
|
|
1132
1461
|
return "allow";
|
|
1133
|
-
|
|
1462
|
+
// The card shows the call's input, and beside it what the gate says the call would do that the person approves too
|
|
1463
|
+
// (a learned workflow's MCP calls set to ask: askedFor).
|
|
1464
|
+
const shown = await spec.gate?.askedFor?.(tool, call.args);
|
|
1465
|
+
// Shown first, so a card cut at its preview's bound (ARGS_PREVIEW_BYTES) still names them whatever the input's size.
|
|
1466
|
+
// The gate's own fields win over any of the same name in the call's input, which the model wrote.
|
|
1467
|
+
const args = shown ? { ...shown, ...Object.fromEntries(Object.entries((call.args ?? {})).filter(([key]) => !Object.hasOwn(shown, key))) } : call.args;
|
|
1468
|
+
const answer = await answerOf({ ...call, tool, args }, mcp?.priceOf(tool) ?? spec.approvalPrice?.(kernelCall) ?? null, asked);
|
|
1134
1469
|
if (answer === "park")
|
|
1135
1470
|
return { park: true };
|
|
1471
|
+
// A call a person approved: what it runs inside its own call (a learned workflow's MCP nodes set to ask) runs as
|
|
1472
|
+
// approved with it (host-calls.ts), as far as the card they approved named it: its ask as journaled, the card they
|
|
1473
|
+
// saw, never one built again now.
|
|
1474
|
+
if (answer.approved) {
|
|
1475
|
+
const seen = askedArgs.get(call.callId);
|
|
1476
|
+
approvedCalls.set(call.callId, seen && typeof seen === "object" && !Array.isArray(seen) ? seen : {});
|
|
1477
|
+
}
|
|
1136
1478
|
return answer.approved ? "allow" : { deny: deniedText(asked, answer.note) };
|
|
1137
1479
|
};
|
|
1138
1480
|
}
|
|
@@ -1142,10 +1484,13 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1142
1484
|
async function answerOf(call, price, asked = call.tool) {
|
|
1143
1485
|
const conversation = await harness.root(context);
|
|
1144
1486
|
const entries = await entriesOf(gate.journal, conversation.id, context);
|
|
1487
|
+
const first = findData(entries, KINDS.ask, (data) => data.callId === call.callId);
|
|
1488
|
+
// The card the person answers is the ask as first journaled: kept for the caller, which scopes an approval to it.
|
|
1489
|
+
if (first)
|
|
1490
|
+
askedArgs.set(call.callId, first.args);
|
|
1145
1491
|
const known = findData(entries, KINDS.answer, (data) => data.callId === call.callId);
|
|
1146
1492
|
if (known)
|
|
1147
1493
|
return known;
|
|
1148
|
-
const first = findData(entries, KINDS.ask, (data) => data.callId === call.callId);
|
|
1149
1494
|
// The ask names the call as the person is asked about it (`asked`: its settings key), so every reader of the ask (the
|
|
1150
1495
|
// run's pending approval, its approval row, an "always" answer pinned by it) has the one name main gives it.
|
|
1151
1496
|
const ask = first ?? { callId: call.callId, tool: asked, args: call.args ?? null, requestedAt: isoNow(), price };
|
|
@@ -1195,17 +1540,29 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1195
1540
|
await new Promise((resolve) => (wakeInbox = resolve));
|
|
1196
1541
|
}
|
|
1197
1542
|
}
|
|
1198
|
-
/** Whether a call of this run waits for the answer `approval` names: asked, and not answered yet.
|
|
1543
|
+
/** Whether a call of this run waits for the answer `approval` names: asked, and not answered yet. `later` when this
|
|
1544
|
+
* claim can no longer say: its Harness never opened, or its pass is parking, ending or stopping (the journal sealed,
|
|
1545
|
+
* closeSession closing the Harness behind the seal). The row is left untaken and the run's next claim checks it, as a
|
|
1546
|
+
* steer that comes as the run parks is (#2292: the read went through the closed Harness and threw). */
|
|
1199
1547
|
async function waitsFor(approval) {
|
|
1200
1548
|
if (approval === undefined)
|
|
1201
1549
|
return false;
|
|
1202
1550
|
await Promise.race([ready, (finished ?? new Promise(() => { })).then(() => undefined, () => undefined)]);
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1551
|
+
const [theGate, theHarness] = [gate, harness];
|
|
1552
|
+
if (!theGate || !theHarness || theGate.sealed)
|
|
1553
|
+
return "later";
|
|
1554
|
+
try {
|
|
1555
|
+
const conversation = await theHarness.root(context);
|
|
1556
|
+
const entries = await entriesOf(theGate.journal, conversation.id, context);
|
|
1557
|
+
const asked = findData(entries, KINDS.ask, (data) => data.callId === approval) !== undefined;
|
|
1558
|
+
return asked && findData(entries, KINDS.answer, (data) => data.callId === approval) === undefined;
|
|
1559
|
+
}
|
|
1560
|
+
catch (error) {
|
|
1561
|
+
// Sealed while it read: closeSession seals, then closes the Harness the read went through.
|
|
1562
|
+
if (theGate.sealed)
|
|
1563
|
+
return "later";
|
|
1564
|
+
throw error;
|
|
1565
|
+
}
|
|
1209
1566
|
}
|
|
1210
1567
|
async function reportHeld(session, conversationId) {
|
|
1211
1568
|
held?.detach();
|
|
@@ -1468,7 +1825,7 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1468
1825
|
// How this claim's turn ended; a turn answered before reads as its submission says.
|
|
1469
1826
|
let turnStatus = "completed";
|
|
1470
1827
|
if (!known) {
|
|
1471
|
-
theRecord?.answering();
|
|
1828
|
+
await theRecord?.answering(requestId, prompt);
|
|
1472
1829
|
await theSession.send(prompt, { id: requestId });
|
|
1473
1830
|
const turn = theSession.turnEnd();
|
|
1474
1831
|
const timeout = new Promise((resolve) => (timer = setTimeout(() => resolve("timeout"), deadlineMs)));
|
|
@@ -1587,7 +1944,12 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1587
1944
|
// A person's stop or cancel withdraws every call still waiting for its answer, in the end's own commit: the approval
|
|
1588
1945
|
// card reads answered (withdrawn), never waiting for good on a final run.
|
|
1589
1946
|
const byPerson = data.state === "stopped" || stopReason?.kind === "command";
|
|
1590
|
-
|
|
1947
|
+
// A cancel's calls read cancelled with their run; a stop's, withdrawn.
|
|
1948
|
+
const withdrawn = byPerson ? withdrawnAnswers(await entriesOf(theGate.journal, conversation.id, context), isoNow(), stopReason?.kind === "command" && stopReason.cancel ? "canceled" : "withdrawn") : [];
|
|
1949
|
+
// A CLI's call on the record from its gate and never sent (parked, or its runner gone before its send): its record ends
|
|
1950
|
+
// with the run's, in the same commit, saying it was not run (never interrupted: it never left).
|
|
1951
|
+
const recordEnds = cliRecord?.unsentParked() ?? [];
|
|
1952
|
+
const notRun = data.state === "stopped" ? STOPPED_WHILE_WAITING : ENDED_WHILE_WAITING;
|
|
1591
1953
|
const ended = theGate.expect(writesEntry(KINDS.end), { run, ...(also.length ? { also } : {}), ...(take.length ? { take } : {}) });
|
|
1592
1954
|
// A seal while the end is committed fails it before it is awaited: handled here, read by the await below.
|
|
1593
1955
|
ended.catch(() => undefined);
|
|
@@ -1595,6 +1957,8 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1595
1957
|
await on.commit(async (tx) => {
|
|
1596
1958
|
for (const answer of withdrawn)
|
|
1597
1959
|
await tx.appendEntry(conversation.id, { kind: KINDS.answer, data: answer });
|
|
1960
|
+
for (const callId of recordEnds)
|
|
1961
|
+
await tx.appendEntry(conversation.id, { kind: KINDS.callEnd, data: { callId, status: "refused", output: notRun, at: now() } });
|
|
1598
1962
|
if (failedWith !== undefined)
|
|
1599
1963
|
await tx.appendEntry(conversation.id, { kind: PI_ASSISTANT, model: [cliErrorAnswer(specNow?.adapter.id ?? "cli", failedWith, now())] });
|
|
1600
1964
|
return tx.appendEntry(conversation.id, { kind: KINDS.end, data: data });
|
|
@@ -1606,6 +1970,8 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1606
1970
|
throw error;
|
|
1607
1971
|
}
|
|
1608
1972
|
await ended;
|
|
1973
|
+
for (const callId of recordEnds)
|
|
1974
|
+
cliRecord?.noteEnd(callId, "refused", notRun);
|
|
1609
1975
|
return data.state === "failed" ? { state: "failed", error: { code: "run-failed", message: data.summary ?? "the run failed" } } : { state: data.state };
|
|
1610
1976
|
}
|
|
1611
1977
|
return {
|
|
@@ -1624,7 +1990,10 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1624
1990
|
// An answer names its approval: the asked call's id. One that names no call waiting for its answer (an old
|
|
1625
1991
|
// approval, another run's) is refused, never applied to the call that waits now.
|
|
1626
1992
|
const approval = approvalOf(command);
|
|
1627
|
-
|
|
1993
|
+
const waits = await waitsFor(approval);
|
|
1994
|
+
if (waits === "later")
|
|
1995
|
+
return { later: "the run is parking or ending: the answer waits for its next claim" };
|
|
1996
|
+
if (!waits) {
|
|
1628
1997
|
refusals += 1;
|
|
1629
1998
|
wakeInbox();
|
|
1630
1999
|
return { refused: `no call of this run waits for approval ${JSON.stringify(approval ?? "")}` };
|
|
@@ -1649,6 +2018,7 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1649
2018
|
if (reason.kind !== "command")
|
|
1650
2019
|
gate?.seal(new Interrupted(reason.kind));
|
|
1651
2020
|
stopped();
|
|
2021
|
+
stopAbort.abort(new Interrupted(reason.kind));
|
|
1652
2022
|
wakeInbox();
|
|
1653
2023
|
await finished?.catch(() => undefined);
|
|
1654
2024
|
},
|