@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
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Tools as text, for a model whose provider caches no prefix of a request that carries a `tools` parameter (a catalog
|
|
2
|
+
// entry's 'toolsAs: "text"', local-models.ts; the translated route, model-route.ts, reads it). Measured
|
|
3
|
+
// 2026-11-11 on OpenRouter's particle-1 (one provider, Particle AI): the same request with `tools` renders to a prompt
|
|
4
|
+
// whose token count varies by request (58,791 vs 58,793) and is cached 0-30% of the time; with its tools described in
|
|
5
|
+
// the system prompt and its calls and results written as text it is cached whole from the second send on (57,088 of
|
|
6
|
+
// 57,188 tokens). A tool-using review sends its whole history every turn, so that is the difference between paying
|
|
7
|
+
// $0.25/M (list) and $0.03/M for nearly all of its input. The text protocol is DeepSeek's DSML without its marker
|
|
8
|
+
// ("<function_calls><invoke name><parameter name string>"), the shape particle-1 writes on its own when given tools as
|
|
9
|
+
// text (DeepSeek V3.2's chat template words it so; the provider drops or breaks the `|DSML|` marker in what it streams);
|
|
10
|
+
// a reply with the marker, or a `<tool_call>{json}</tool_call>` block, is read too. Calls and results are rebuilt from
|
|
11
|
+
// history the same way every turn, so each request's prefix is byte for byte the last one's.
|
|
12
|
+
const OPEN = "<tool_call>";
|
|
13
|
+
const CLOSE = "</tool_call>";
|
|
14
|
+
/** The system prompt's tools section, in DeepSeek V3.2's own words: how to call, then each tool's schema. */
|
|
15
|
+
export function toolsSection(tools) {
|
|
16
|
+
return [
|
|
17
|
+
"## Tools",
|
|
18
|
+
"You have access to a set of tools you can use to answer the user's question.",
|
|
19
|
+
'You can invoke functions by writing a "<function_calls>" block like the following as part of your reply to the user:',
|
|
20
|
+
"<function_calls>",
|
|
21
|
+
'<invoke name="$FUNCTION_NAME">',
|
|
22
|
+
'<parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</parameter>',
|
|
23
|
+
"...",
|
|
24
|
+
"</invoke>",
|
|
25
|
+
'<invoke name="$FUNCTION_NAME2">',
|
|
26
|
+
"...",
|
|
27
|
+
"</invoke>",
|
|
28
|
+
"</function_calls>",
|
|
29
|
+
"",
|
|
30
|
+
'String and scalar parameters should be specified as is without any escaping or quotes, while lists and objects should use JSON format. The "string" attribute should be set to "true" for string type parameters and "false" for other types (numbers, booleans, arrays, objects).',
|
|
31
|
+
"Several invokes in one block run together, in order. Write nothing after the block; the results come back to you in a <function_results> block. A reply with no block is your final answer.",
|
|
32
|
+
"",
|
|
33
|
+
"### Available Tool Schemas",
|
|
34
|
+
"",
|
|
35
|
+
"<functions>",
|
|
36
|
+
...tools.map((tool) => JSON.stringify({ name: tool.name, description: (tool.description ?? "").trim(), parameters: tool.parameters ?? { type: "object", properties: {} } })),
|
|
37
|
+
"</functions>",
|
|
38
|
+
"",
|
|
39
|
+
"You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.",
|
|
40
|
+
].join("\n");
|
|
41
|
+
}
|
|
42
|
+
/** One call as a DSML invoke, as `toolsSection` asks for it. */
|
|
43
|
+
const invokeOf = (name, args) => {
|
|
44
|
+
const params = Object.entries((args && typeof args === "object" ? args : {})).map(([key, value]) => `<parameter name="${key}" string="${typeof value === "string"}">${typeof value === "string" ? value : JSON.stringify(value)}</parameter>`);
|
|
45
|
+
return [`<invoke name="${name}">`, ...params, "</invoke>"].join("\n");
|
|
46
|
+
};
|
|
47
|
+
/** `context` with its tools in its system prompt and every tool call and result in its history written as text. */
|
|
48
|
+
export function toolsAsText(context) {
|
|
49
|
+
const tools = context.tools ?? [];
|
|
50
|
+
const messages = [];
|
|
51
|
+
for (const message of context.messages) {
|
|
52
|
+
if (message.role === "assistant" && Array.isArray(message.content)) {
|
|
53
|
+
const said = message.content.filter((part) => part.type === "text").map((part) => part.text ?? "").join("").trim();
|
|
54
|
+
const calls = message.content.filter((part) => part.type === "toolCall").map((part) => invokeOf(part.name ?? "", part.arguments));
|
|
55
|
+
const text = [said, calls.length ? ["<function_calls>", ...calls, "</function_calls>"].join("\n") : ""].filter(Boolean).join("\n\n");
|
|
56
|
+
messages.push({ ...message, content: [{ type: "text", text: text || "(no text)" }] });
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (message.role === "toolResult") {
|
|
60
|
+
const parts = Array.isArray(message.content) ? message.content : [{ type: "text", text: String(message.content) }];
|
|
61
|
+
const body = parts.filter((part) => part.type === "text").map((part) => part.text ?? "").join("\n");
|
|
62
|
+
const images = parts.filter((part) => part.type === "image");
|
|
63
|
+
const text = `<result tool="${message.toolName ?? ""}"${message.isError ? ' error="true"' : ""}>\n${body}\n</result>`;
|
|
64
|
+
// Results of one turn's calls go together, as one user message, in the order called.
|
|
65
|
+
const last = messages[messages.length - 1];
|
|
66
|
+
if (last?.role === "user" && last.fromTools === true && Array.isArray(last.content))
|
|
67
|
+
last.content.push({ type: "text", text: `\n${text}` }, ...images);
|
|
68
|
+
else
|
|
69
|
+
messages.push({ role: "user", content: [{ type: "text", text: `<function_results>\n${text}` }, ...images], timestamp: message.timestamp, fromTools: true });
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
messages.push(message);
|
|
73
|
+
}
|
|
74
|
+
for (const message of messages) {
|
|
75
|
+
if (message.fromTools === true && Array.isArray(message.content))
|
|
76
|
+
message.content.push({ type: "text", text: "\n</function_results>" });
|
|
77
|
+
delete message.fromTools;
|
|
78
|
+
}
|
|
79
|
+
const system = [context.systemPrompt ?? "", tools.length ? toolsSection(tools) : ""].filter(Boolean).join("\n\n");
|
|
80
|
+
return { ...(system ? { systemPrompt: system } : {}), messages };
|
|
81
|
+
}
|
|
82
|
+
/** A reply's text read as text and calls: each `<tool_call>` block whose JSON names a tool, in order; the rest is
|
|
83
|
+
* text. A block whose JSON does not parse stays text. */
|
|
84
|
+
export function callsOfText(said, tools = []) {
|
|
85
|
+
const calls = [];
|
|
86
|
+
// DSML with its marker or without (the provider may drop it, or put zero-width spaces in its tags), as the model
|
|
87
|
+
// writes it: `<function_calls>` (or "<calls>"), each `<invoke name="...">` with `<parameter name="..." string="...">`
|
|
88
|
+
// values, or `<invoke name="...", "arguments": {...}}`. The slips particle-1 makes are read too: the tool named by a
|
|
89
|
+
// '<parameter name="invoke">', a `<name>value</parameter>` parameter, and an invoke with no name (the one tool whose
|
|
90
|
+
// schema takes every parameter given and all its required ones).
|
|
91
|
+
const text = said.replace(/\u200b/g, "").replace(/|DSML|\s*/g, "");
|
|
92
|
+
const first = text.search(/<(?:function_calls|calls|invoke\b|parameter\b)/);
|
|
93
|
+
const groups = [];
|
|
94
|
+
const current = () => groups[groups.length - 1] ?? (groups.push({ args: {} }), groups[0]);
|
|
95
|
+
// A string is written unescaped, so a value may hold "</parameter>" itself (a file that writes this protocol, an
|
|
96
|
+
// example in a comment): a value ends at the closing tag that the call's next tag, or the reply's end, follows.
|
|
97
|
+
const token = /<invoke\s+name="([^"]+)"(\s*,\s*"arguments"\s*:\s*)?|<\/invoke>|<parameter\s+name="([^"]+)"(?:\s+string="(true|false)")?\s*>([\s\S]*?)<\/(?:parameter|invoke)>(?=\s*(?:<parameter\s+name=|<\/?invoke\b|<\/function_calls>|<\/calls>|<[A-Za-z_][\w-]*>[^<]*<\/parameter>|$))|<([A-Za-z_][\w-]*)>([^<]*)<\/parameter>/g;
|
|
98
|
+
const block = first < 0 ? "" : text.slice(first);
|
|
99
|
+
let closed = false;
|
|
100
|
+
// A call the reply did not finish (cut off, or a tag opened and never closed) is never run on what it holds so far:
|
|
101
|
+
// the reply is broken, and asked again.
|
|
102
|
+
let unfinished = false;
|
|
103
|
+
let end = 0;
|
|
104
|
+
for (const match of block.matchAll(token)) {
|
|
105
|
+
const [whole, invoked, jsonArgs, key, isString, value, slipKey, slipValue] = match;
|
|
106
|
+
if (/<(?:invoke|parameter)\b/.test(block.slice(end, match.index)))
|
|
107
|
+
unfinished = true;
|
|
108
|
+
end = match.index + whole.length;
|
|
109
|
+
if (invoked !== undefined) {
|
|
110
|
+
groups.push({ name: invoked, args: {} });
|
|
111
|
+
closed = false;
|
|
112
|
+
if (jsonArgs) {
|
|
113
|
+
const object = objectAt(block, end);
|
|
114
|
+
if (object === undefined)
|
|
115
|
+
unfinished = true;
|
|
116
|
+
else {
|
|
117
|
+
end += object.length;
|
|
118
|
+
try {
|
|
119
|
+
Object.assign(groups[groups.length - 1].args, JSON.parse(object));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
unfinished = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (whole === "</invoke>")
|
|
128
|
+
closed = true;
|
|
129
|
+
else if (key === "invoke" && value.includes("<parameter")) {
|
|
130
|
+
// `<parameter name="invoke">` opening a call whose name never came: its first parameter is inside.
|
|
131
|
+
groups.push({ args: {} });
|
|
132
|
+
closed = false;
|
|
133
|
+
const inner = /<parameter\s+name="([^"]+)"(?:\s+string="(true|false)")?\s*>([\s\S]*)$/.exec(value);
|
|
134
|
+
if (inner)
|
|
135
|
+
current().args[inner[1]] = valueOf(inner[3], inner[2]);
|
|
136
|
+
}
|
|
137
|
+
else if (key === "invoke" || key === "name" && current().name === undefined && tools.some((tool) => tool.name === value)) {
|
|
138
|
+
groups.push({ name: value.trim(), args: {} });
|
|
139
|
+
closed = false;
|
|
140
|
+
}
|
|
141
|
+
else if (key !== undefined) {
|
|
142
|
+
if (closed)
|
|
143
|
+
groups.push({ args: {} });
|
|
144
|
+
closed = false;
|
|
145
|
+
current().args[key] = valueOf(value, isString);
|
|
146
|
+
}
|
|
147
|
+
else if (slipKey !== undefined && !["function_calls", "calls", "invoke", "parameter"].includes(slipKey) && !(slipKey in current().args))
|
|
148
|
+
current().args[slipKey] = valueOf(slipValue, undefined);
|
|
149
|
+
}
|
|
150
|
+
if (first >= 0 && /<(?:invoke|parameter)\b/.test(block.slice(end)))
|
|
151
|
+
unfinished = true;
|
|
152
|
+
if (unfinished)
|
|
153
|
+
return { text: text.trim(), calls: [], broken: true };
|
|
154
|
+
for (const group of groups) {
|
|
155
|
+
const name = group.name ?? nameOf(group.args, tools);
|
|
156
|
+
if (name !== undefined)
|
|
157
|
+
calls.push({ name, arguments: group.args });
|
|
158
|
+
}
|
|
159
|
+
if (calls.length)
|
|
160
|
+
return { text: text.slice(0, first).trim(), calls };
|
|
161
|
+
if (first >= 0 && /<(?:function_calls|calls|invoke|parameter)\b/.test(text))
|
|
162
|
+
return { text: text.trim(), calls, broken: true };
|
|
163
|
+
// A `<tool_call>{"name": ..., "arguments": ...}</tool_call>` block.
|
|
164
|
+
let rest = "";
|
|
165
|
+
let at = 0;
|
|
166
|
+
for (;;) {
|
|
167
|
+
const open = text.indexOf(OPEN, at);
|
|
168
|
+
if (open < 0)
|
|
169
|
+
break;
|
|
170
|
+
const close = text.indexOf(CLOSE, open + OPEN.length);
|
|
171
|
+
const raw = text.slice(open + OPEN.length, close < 0 ? undefined : close).trim();
|
|
172
|
+
let call;
|
|
173
|
+
try {
|
|
174
|
+
call = JSON.parse(raw);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
call = undefined;
|
|
178
|
+
}
|
|
179
|
+
rest += text.slice(at, open);
|
|
180
|
+
if (call && typeof call.name === "string") {
|
|
181
|
+
const args = typeof call.arguments === "string" ? valueOf(call.arguments, "false") : call.arguments;
|
|
182
|
+
calls.push({ name: call.name, arguments: (args && typeof args === "object" && !Array.isArray(args) ? args : {}) });
|
|
183
|
+
}
|
|
184
|
+
else
|
|
185
|
+
rest += text.slice(open, close < 0 ? undefined : close + CLOSE.length);
|
|
186
|
+
if (close < 0) {
|
|
187
|
+
at = text.length;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
at = close + CLOSE.length;
|
|
191
|
+
}
|
|
192
|
+
rest += text.slice(at);
|
|
193
|
+
return { text: rest.trim(), calls };
|
|
194
|
+
}
|
|
195
|
+
/** The one tool whose schema takes every argument given and all its required ones, or undefined. */
|
|
196
|
+
function nameOf(args, tools) {
|
|
197
|
+
const keys = Object.keys(args);
|
|
198
|
+
if (!keys.length)
|
|
199
|
+
return undefined;
|
|
200
|
+
const fits = tools.filter((tool) => {
|
|
201
|
+
const schema = (tool.parameters ?? {});
|
|
202
|
+
const known = Object.keys(schema.properties ?? {});
|
|
203
|
+
const props = (schema.properties ?? {});
|
|
204
|
+
return keys.every((key) => known.includes(key) && (!props[key]?.enum || props[key].enum.includes(args[key]))) && (schema.required ?? []).every((key) => keys.includes(key));
|
|
205
|
+
});
|
|
206
|
+
return fits.length === 1 ? fits[0].name : undefined;
|
|
207
|
+
}
|
|
208
|
+
/** A parameter's value: as written when it is a string, else its JSON (a number, a list, an object), else as written. */
|
|
209
|
+
function valueOf(value, isString) {
|
|
210
|
+
if (isString === "true")
|
|
211
|
+
return value;
|
|
212
|
+
try {
|
|
213
|
+
return JSON.parse(value.trim());
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** The JSON object that starts at `at` in `text` (its braces matched, strings skipped), or undefined. */
|
|
220
|
+
function objectAt(text, at) {
|
|
221
|
+
if (text[at] !== "{")
|
|
222
|
+
return undefined;
|
|
223
|
+
let depth = 0;
|
|
224
|
+
for (let i = at; i < text.length; i++) {
|
|
225
|
+
const c = text[i];
|
|
226
|
+
if (c === '"') {
|
|
227
|
+
for (i++; i < text.length && text[i] !== '"'; i++)
|
|
228
|
+
if (text[i] === "\\")
|
|
229
|
+
i++;
|
|
230
|
+
}
|
|
231
|
+
else if (c === "{")
|
|
232
|
+
depth++;
|
|
233
|
+
else if (c === "}" && --depth === 0)
|
|
234
|
+
return text.slice(at, i + 1);
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
@@ -94,6 +94,12 @@ export interface WorkflowRunSpec {
|
|
|
94
94
|
* once, however the run ended (ended, stopped, failed, parked or interrupted), so a parked or resumed run starts fresh
|
|
95
95
|
* proxies on its next claim. Never throws out. Absent: nothing to let go. */
|
|
96
96
|
readonly release?: () => Promise<void>;
|
|
97
|
+
/** Resolves once every model request the spec admitted has been booked, or a bound has passed (then a request left
|
|
98
|
+
* unbooked reads at its hold and bound). A request its node let go of (a stop) is booked only once the provider's side
|
|
99
|
+
* ends, which can be after the node ended: once the workflow's task is over, the loop waits for this before it closes
|
|
100
|
+
* the Harness the bookings are written through. Not on a park or a lost claim: the next claim books a request left
|
|
101
|
+
* open there as cut. Absent: nothing to wait for. */
|
|
102
|
+
readonly settle?: () => Promise<void>;
|
|
97
103
|
}
|
|
98
104
|
/** What the interpreter's host may ask of the loop while it runs. */
|
|
99
105
|
export interface WorkflowHostHooks {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The workflow loop on the runner (design: "Runners carry many loops"; the lead's ruling, workflow agents keep running
|
|
2
2
|
// after the cutover). A run of a workflow revision, which the runner claims like any run: this loop is its driver, so the
|
|
3
|
-
// claim, the fence, a cancel and the end commit are its. The DSL's durable runner (
|
|
3
|
+
// claim, the fence, a cancel and the end commit are its. The DSL's durable runner (workflow-run.ts
|
|
4
4
|
// startWorkflowRevision over agentrun-pi's workflow task) runs the revision's workflow UNCHANGED — the same nodes,
|
|
5
5
|
// decisions, call tools and cap it ran with in the per-run process — on the run's journal in the one store (A,
|
|
6
6
|
// deps.journal(claim)) in place of a per-run store file. The interpreter's host (model nodes on the run's models, call
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
|
|
21
21
|
import { createRegistry, Harness, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
22
22
|
import { createWorkflowExtension } from "@parcha/agentrun-pi/durable";
|
|
23
|
-
import { readWorkflowRun, startWorkflowRevision, workflowCarry } from "
|
|
23
|
+
import { readWorkflowRun, startWorkflowRevision, workflowCarry } from "../workflow-run.js";
|
|
24
24
|
import { KINDS } from "./journal-kinds.js";
|
|
25
25
|
import { endedBy } from "./loop-contract.js";
|
|
26
26
|
/** The most the end's patch (cost and metrics) may take before the end is written without it (the pi loop's CUT_STEP_MS). */
|
|
@@ -263,8 +263,10 @@ function workflowLoop(specOf, deps) {
|
|
|
263
263
|
void error;
|
|
264
264
|
return { state: "interrupted" };
|
|
265
265
|
}
|
|
266
|
-
// The task is terminal.
|
|
267
|
-
//
|
|
266
|
+
// The task is terminal. Its model requests booked (a stopped one books once its provider's side ends, after the node
|
|
267
|
+
// let go of it), close the Harness, then read the run's view from its journal and write the end, both on a fresh
|
|
268
|
+
// journal of the same claim (the Harness closed its own).
|
|
269
|
+
await spec.settle?.().catch(() => undefined);
|
|
268
270
|
await closeHarness();
|
|
269
271
|
const endJournal = deps.journal(claim);
|
|
270
272
|
// A person stopped or cancelled: the run ends stopped whatever the workflow's own outcome (its last node may have
|
|
@@ -4,6 +4,7 @@ import { type BeforeRequest, type Wire } from "@parcha/agentrun-kernel";
|
|
|
4
4
|
import { type ProviderPolicy, type ProxyUsage } from "@parcha/agentrun-kernel/host";
|
|
5
5
|
import { type CapModel } from "@parcha/agentrun-kernel/kernel/cap";
|
|
6
6
|
import { type Tier } from "../model.js";
|
|
7
|
+
import { deniedText } from "../refusal-words.js";
|
|
7
8
|
import { type PaidBooking } from "../paid-calls.js";
|
|
8
9
|
import { type RunTools } from "@parcha/agentrun-kernel/kernel/cap";
|
|
9
10
|
import { type ModelRoute } from "./model-route.js";
|
|
@@ -138,6 +139,16 @@ export declare const heldBooking: () => {
|
|
|
138
139
|
readonly paid: PaidBooking;
|
|
139
140
|
} | undefined;
|
|
140
141
|
export declare const releaseCallHold: () => Promise<void>;
|
|
142
|
+
/** A call tool's permission now, as main's per-run process read it at each call node (app.ts:382-384, the gate's
|
|
143
|
+
* policy): the revision's tool settings and approvals over the server's, the server's read again from the store at each
|
|
144
|
+
* call, so a deny or an ask set while the run goes reaches its next call. A read that fails is unread, as the agentic
|
|
145
|
+
* gate's is (adapter-run-spec.ts storeSettings): with no good read before it every tool asks, so a call node is refused
|
|
146
|
+
* (the revision's own allow never stands alone over settings no read has seen); after one, the gate holds to that read
|
|
147
|
+
* marked unread (RunGate.serverSettings), so its denies still refuse. */
|
|
148
|
+
type CallPolicy = (tool: string) => Promise<{
|
|
149
|
+
permission: "allow" | "ask" | "deny";
|
|
150
|
+
denyFrom: Parameters<typeof deniedText>[2];
|
|
151
|
+
}>;
|
|
141
152
|
/** `cap` with a call node's recorded tool failure thrown as the node's failure, as main's `failing` (app.ts:417-420):
|
|
142
153
|
* the driver records the effect's outcome ({ TOOL_FAILED: words }) as its receipt, and this, past the driver, throws it
|
|
143
154
|
* as an EffectFailure in those words, so the call node fails and the run with it in the tool's words. Applied outside
|
|
@@ -151,6 +162,13 @@ export declare const RUNNER_NODE_TOOLS: readonly string[];
|
|
|
151
162
|
/** A node naming a tool the runner does not bind for a model node yet: the run is refused at its claim in these words,
|
|
152
163
|
* the tool never offered and never silently dropped. */
|
|
153
164
|
export declare const nodeToolUnboundText: (tool: string) => string;
|
|
165
|
+
/** A model node's tool call as its pi-durable task runs it: its args, the task's api and the call's context. */
|
|
166
|
+
type GatedExecute = (args: never, api: never, context: never) => Promise<unknown>;
|
|
167
|
+
/** `execute` behind the gate, for the tool `name`. A call it lets through is marked so in its task first, so when
|
|
168
|
+
* pi-durable's recovery runs the same call again on a later claim (the first attempt cut with its claim, maybe after it
|
|
169
|
+
* was sent) and the tool is now denied or set to ask, the refusal says that whether it took effect is not known
|
|
170
|
+
* (RAN_UNKNOWN), never that it was not run or that nothing was sent and nothing was charged. */
|
|
171
|
+
export declare function gatedNodeCall(name: string, policy: CallPolicy, execute: GatedExecute): GatedExecute;
|
|
154
172
|
/** The tool names a workflow's model node offers its model: the node's `submit` (pi's SUBMIT_TOOL) and every tool the
|
|
155
173
|
* workflow's model nodes name. */
|
|
156
174
|
export declare function nodeOffer(workflow: Workflow): string[];
|
|
@@ -39,7 +39,8 @@ import { PI_WIRES, storeSettings } from "./adapter-run-spec.js";
|
|
|
39
39
|
import { policyFor } from "./adapters/pi.js";
|
|
40
40
|
import { sessionProvider } from "../port-models.js";
|
|
41
41
|
import { journalSessions } from "./sessions.js";
|
|
42
|
-
import { KINDS } from "./journal-kinds.js";
|
|
42
|
+
import { KINDS, WORKFLOW_SESSION } from "./journal-kinds.js";
|
|
43
|
+
import { CLOSE_MS } from "./loop.js";
|
|
43
44
|
import { PAUSED_WORDS } from "./workflow-loop.js";
|
|
44
45
|
import { heldRedactor, holdClaimed } from "./redact.js";
|
|
45
46
|
import { holdStored } from "./store-secrets.js";
|
|
@@ -49,7 +50,7 @@ import { workflowHost } from "./workflow-host.js";
|
|
|
49
50
|
* does not have: importing it here stopped the runner from starting. workflow-submit-name.test.mjs holds the two equal. */
|
|
50
51
|
export const SUBMIT_TOOL = "submit";
|
|
51
52
|
/** The one session key the workflow's model requests book under (the run's one booking sums it, runBooking). */
|
|
52
|
-
const SESSION =
|
|
53
|
+
const SESSION = WORKFLOW_SESSION;
|
|
53
54
|
/** A call node's tool failure and a cap-held effect, as app.ts's runEffect completed them (the loop's `failing` reads them). */
|
|
54
55
|
const TOOL_FAILED = "agentrun_serve_tool_failed";
|
|
55
56
|
const CAP_HELD = "agentrun_serve_cap_held";
|
|
@@ -143,7 +144,7 @@ export function workflowSpecOf(deps) {
|
|
|
143
144
|
if (unbound !== undefined)
|
|
144
145
|
throw new Error(nodeToolUnboundText(unbound));
|
|
145
146
|
// Every secret the server keeps sealed (store-secrets.ts) held by the runner's one redactor before the workflow runs.
|
|
146
|
-
if (run?.redact)
|
|
147
|
+
if (run?.redact && deps.sealKey)
|
|
147
148
|
await holdStored(heldRedactor(run.redact), deps.store, deps.sealKey, deps.data);
|
|
148
149
|
const tiers = tiersOf(claimed.workflow);
|
|
149
150
|
// The run's own keys, as the server's runnable check reads them: each by its name from the runner's environment first,
|
|
@@ -218,12 +219,10 @@ export function workflowSpecOf(deps) {
|
|
|
218
219
|
const nodeHost = {
|
|
219
220
|
where: { root: claimed.workRoot, work: claimed.workRoot, ref: { id: claim.runId } },
|
|
220
221
|
env: callEnv,
|
|
221
|
-
coding: { tools: [], extension: { name: "coding", tools: [] } },
|
|
222
222
|
install: (extension) => void installed.push(extension),
|
|
223
223
|
onConversation: () => { },
|
|
224
224
|
beforeResume: () => { },
|
|
225
225
|
beforeRelease: () => { },
|
|
226
|
-
handoverInJournal: true,
|
|
227
226
|
};
|
|
228
227
|
const nodeBindings = await bindNodeTools(nodeNames, nodeHost);
|
|
229
228
|
// Each node tool as the node gets it and the registry holds it: booked on the run's one ledger (bookedNodeTools), in an
|
|
@@ -307,6 +306,12 @@ export function workflowSpecOf(deps) {
|
|
|
307
306
|
paidTools = await run.tools(await sessions.open(TOOL_SESSION), { flush: () => deps.store.flush() });
|
|
308
307
|
hooks.setPort(booking.models);
|
|
309
308
|
},
|
|
309
|
+
settle: async () => {
|
|
310
|
+
const left = await hooks.bookingsSettled();
|
|
311
|
+
// Past the bound: the end is written, and each request still unbooked counts at its hold and bound (never less).
|
|
312
|
+
if (left > 0)
|
|
313
|
+
console.warn(`[runner] ${claim.runId}: ${left} model request(s) still unbooked ${CLOSE_MS} ms after the workflow ended; each counts at its hold and bound`);
|
|
314
|
+
},
|
|
310
315
|
release: async () => {
|
|
311
316
|
for (const proxy of proxies)
|
|
312
317
|
await proxy.close().catch(() => undefined);
|
|
@@ -372,18 +377,46 @@ export function pauseAtEffects(cap, hooks, flight) {
|
|
|
372
377
|
}
|
|
373
378
|
/** The proxies start before the booking is wired; their hooks forward to the booking set at harnessReady. A request before
|
|
374
379
|
* then would be a bug (no model request runs before the workflow does), so it refuses loudly. `park` is the loop's hook,
|
|
375
|
-
* set at build, which a cap refusal calls.
|
|
380
|
+
* set at build, which a cap refusal calls. Each request admitted is counted until it is booked: a request a person's
|
|
381
|
+
* stop cut short is booked only once its proxy sees the provider's side end, which can be after its node ended, and the
|
|
382
|
+
* run's end waits for it (bookingsSettled), as the pi adapter's loop does. */
|
|
376
383
|
function deferredHooks() {
|
|
377
384
|
let port;
|
|
378
385
|
let loop;
|
|
386
|
+
let unbooked = 0;
|
|
387
|
+
const bookedAll = [];
|
|
379
388
|
return {
|
|
380
389
|
port: {
|
|
381
|
-
beforeRequest: (request) =>
|
|
382
|
-
|
|
390
|
+
beforeRequest: async (request) => {
|
|
391
|
+
const ticket = await (port ?? missing()).beforeRequest(request);
|
|
392
|
+
unbooked += 1;
|
|
393
|
+
return ticket;
|
|
394
|
+
},
|
|
395
|
+
afterRequest: async (usage, ticket, ended) => {
|
|
396
|
+
try {
|
|
397
|
+
await (port ?? missing()).afterRequest(usage, ticket, ended);
|
|
398
|
+
}
|
|
399
|
+
finally {
|
|
400
|
+
unbooked = Math.max(0, unbooked - 1);
|
|
401
|
+
if (unbooked === 0)
|
|
402
|
+
for (const wake of bookedAll.splice(0))
|
|
403
|
+
wake();
|
|
404
|
+
}
|
|
405
|
+
},
|
|
383
406
|
responded: (ids, ticket) => (port ?? missing()).responded?.(ids, ticket) ?? Promise.resolve(),
|
|
384
407
|
note: (model, request) => (port ?? missing()).note?.(model, request),
|
|
385
408
|
},
|
|
386
409
|
setPort: (p) => { port = p; },
|
|
410
|
+
/** Waits, at most CLOSE_MS, until every request admitted is booked, and says how many are still not. Past the bound
|
|
411
|
+
* the run's end is written anyway, and a request left unbooked reads at its hold and bound, the worst it can have cost. */
|
|
412
|
+
bookingsSettled: async () => {
|
|
413
|
+
if (unbooked === 0)
|
|
414
|
+
return 0;
|
|
415
|
+
let timer;
|
|
416
|
+
await Promise.race([new Promise((resolve) => bookedAll.push(resolve)), new Promise((resolve) => (timer = setTimeout(resolve, CLOSE_MS)))]);
|
|
417
|
+
clearTimeout(timer);
|
|
418
|
+
return unbooked;
|
|
419
|
+
},
|
|
387
420
|
park: (refusal) => loop?.park(refusal),
|
|
388
421
|
// A person's pause, asked where the cap is (the loop's hooks): none before the loop hands its hooks over.
|
|
389
422
|
paused: () => loop?.paused() ?? false,
|
|
@@ -532,7 +565,7 @@ function callPolicyOf(store, runId, claimed) {
|
|
|
532
565
|
// Nothing read yet: unread, so a policy asked before the first read fails closed too.
|
|
533
566
|
let current = { settings: {}, problems: [], unreadable: true };
|
|
534
567
|
// Only the permission is read here: a workflow never parks, so whether anyone can answer an ask plays no part.
|
|
535
|
-
const gate = runGate({ root: claimed.workRoot, work: claimed.workRoot }, runId, claimed.revision, () => current,
|
|
568
|
+
const gate = runGate({ root: claimed.workRoot, work: claimed.workRoot }, runId, claimed.revision, () => current, true);
|
|
536
569
|
return async (tool) => {
|
|
537
570
|
current = await storeSettings(store);
|
|
538
571
|
return { permission: gate.policy(tool), denyFrom: gate.denyFrom(tool) };
|
|
@@ -741,17 +774,30 @@ function nodeToolGate(names, policy) {
|
|
|
741
774
|
name: "tool-gate",
|
|
742
775
|
wraps: [...names].map((name) => wrapTool({ name }, (inner) => ({
|
|
743
776
|
...inner,
|
|
744
|
-
|
|
745
|
-
const { permission, denyFrom } = await policy(inner.name);
|
|
746
|
-
if (permission === "deny")
|
|
747
|
-
return forModel(deniedText(inner.name, undefined, denyFrom), { isError: true });
|
|
748
|
-
if (permission === "ask")
|
|
749
|
-
return forModel(modelNodeAskText(inner.name), { isError: true });
|
|
750
|
-
return inner.execute(args, api, context);
|
|
751
|
-
},
|
|
777
|
+
execute: gatedNodeCall(inner.name, policy, inner.execute),
|
|
752
778
|
}))),
|
|
753
779
|
});
|
|
754
780
|
}
|
|
781
|
+
/** The task memo the gate keeps that it let a call through, before the call runs. */
|
|
782
|
+
const LET_THROUGH = "agentrun.gate.let-through";
|
|
783
|
+
/** `execute` behind the gate, for the tool `name`. A call it lets through is marked so in its task first, so when
|
|
784
|
+
* pi-durable's recovery runs the same call again on a later claim (the first attempt cut with its claim, maybe after it
|
|
785
|
+
* was sent) and the tool is now denied or set to ask, the refusal says that whether it took effect is not known
|
|
786
|
+
* (RAN_UNKNOWN), never that it was not run or that nothing was sent and nothing was charged. */
|
|
787
|
+
export function gatedNodeCall(name, policy, execute) {
|
|
788
|
+
return async (args, api, context) => {
|
|
789
|
+
const task = api;
|
|
790
|
+
const { permission, denyFrom } = await policy(name);
|
|
791
|
+
if (permission !== "allow") {
|
|
792
|
+
// A call an earlier attempt of which this gate let through may have reached its host: whatever refuses it now, its
|
|
793
|
+
// effect is not known, never "not run".
|
|
794
|
+
const ran = (await task.memo(LET_THROUGH, context)) === true ? "unknown" : "no";
|
|
795
|
+
return forModel(permission === "deny" ? deniedText(name, undefined, denyFrom, ran) : modelNodeAskText(name, ran), { isError: true });
|
|
796
|
+
}
|
|
797
|
+
await task.memo(LET_THROUGH, true, context);
|
|
798
|
+
return execute(args, api, context);
|
|
799
|
+
};
|
|
800
|
+
}
|
|
755
801
|
/** `binding`'s tools with each call's paid requests booked on the run's one ledger (Ledger.paid, the paid tool calls on
|
|
756
802
|
* the run's cap, as an agentic run's are), a refusal for room parking the run (Ledger.pause, as main's paidCall pauses):
|
|
757
803
|
* a node's tool call is the node's own turn, so the run parks and resumes it from its journal. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Revision } from "
|
|
2
|
-
import type { MessageOrigin } from "
|
|
1
|
+
import type { Revision } from "./agent-rows.js";
|
|
2
|
+
import type { MessageOrigin } from "./timeline/index.js";
|
|
3
3
|
import { type AgentEntry, type ExternalRef } from "./agents-store.js";
|
|
4
4
|
import { ApiError, type InputIssue } from "./errors.js";
|
|
5
5
|
import { type LearningMode, type Seed, type Shown } from "./learning.js";
|
|
@@ -128,6 +128,8 @@ export interface StartDeps {
|
|
|
128
128
|
readonly documents?: string;
|
|
129
129
|
/** What a run's box may reach, as the server resolved it, or "none". */
|
|
130
130
|
readonly boxAllow?: readonly string[] | "none";
|
|
131
|
+
/** The operator set that (run-launch.ts boxAllowSet). */
|
|
132
|
+
readonly boxAllowSet?: true;
|
|
131
133
|
/** The run's model, pinned at start: resolved exactly as the old per-run process resolved it from the revision and agent
|
|
132
134
|
* (app.ts's runModels / wireModels over revision.body.model on its harness). The runner reads launch.model alone, so one
|
|
133
135
|
* resolution happens, here. Required. */
|
|
@@ -147,6 +149,9 @@ export interface StartDeps {
|
|
|
147
149
|
}>;
|
|
148
150
|
/** The learning budget in bytes (learning.ts LEARNING_BUDGET), for which notes fit a run's # Learned section. */
|
|
149
151
|
readonly learningBudget?: number;
|
|
152
|
+
/** The model the server names to grade what a run proposes (learning.ts gradeModelOf), put on each learning start; none,
|
|
153
|
+
* the agent's own grades it. */
|
|
154
|
+
readonly gradeModel?: string;
|
|
150
155
|
/** The host's secrets as they are shown now (learning-run.ts shown), for cutting the notes a run is given. */
|
|
151
156
|
shown?(): Shown;
|
|
152
157
|
}
|