@parcha/agentrun-serve 0.1.0-beta.40 → 0.1.0-beta.42
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 +130 -0
- package/NOTICE +2 -0
- package/README.md +149 -21
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.js +3 -1
- package/dist/api-only.d.ts +3 -1
- package/dist/api-only.js +4 -2
- package/dist/api-token.d.ts +30 -0
- package/dist/api-token.js +89 -0
- package/dist/calendar/address.d.ts +49 -0
- package/dist/calendar/address.js +153 -0
- package/dist/calendar/fetch.d.ts +24 -0
- package/dist/calendar/fetch.js +146 -0
- package/dist/calendar/ics-faux.d.ts +39 -0
- package/dist/calendar/ics-faux.js +236 -0
- package/dist/calendar/labels.d.ts +3 -0
- package/dist/calendar/labels.js +61 -0
- package/dist/calendar/names.d.ts +52 -0
- package/dist/calendar/names.js +54 -0
- package/dist/calendar/parse.d.ts +108 -0
- package/dist/calendar/parse.js +625 -0
- package/dist/calendar/routes.d.ts +17 -0
- package/dist/calendar/routes.js +81 -0
- package/dist/calendar/tools.d.ts +23 -0
- package/dist/calendar/tools.js +124 -0
- package/dist/cli.js +22 -6
- package/dist/composio-made.d.ts +54 -0
- package/dist/composio-made.js +83 -0
- package/dist/composio-preset.d.ts +16 -8
- package/dist/composio-preset.js +106 -43
- package/dist/composio-triggers.d.ts +7 -2
- package/dist/composio-triggers.js +13 -7
- package/dist/computer-attach.d.ts +29 -11
- package/dist/computer-attach.js +141 -47
- package/dist/computer-keys.d.ts +4 -0
- package/dist/computer-keys.js +5 -0
- package/dist/computer-record.d.ts +55 -0
- package/dist/computer-record.js +315 -0
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +15 -2
- package/dist/computers.d.ts +11 -1
- package/dist/computers.js +23 -26
- package/dist/console/assets/{index-B8Zn1Iqn.css → index-C3gNPz58.css} +1 -1
- package/dist/console/assets/index-DYJ_wj27.js +711 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.js +9 -2
- package/dist/eval-feed.d.ts +26 -0
- package/dist/eval-feed.js +72 -0
- package/dist/eval-mailbox.d.ts +4 -1
- package/dist/eval-mailbox.js +11 -3
- package/dist/exa-keyless.d.ts +1 -1
- package/dist/exa-keyless.js +1 -1
- package/dist/feed/feed-faux.d.ts +62 -0
- package/dist/feed/feed-faux.js +123 -0
- package/dist/feed/fetch.d.ts +41 -0
- package/dist/feed/fetch.js +128 -0
- package/dist/feed/parse.d.ts +42 -0
- package/dist/feed/parse.js +291 -0
- package/dist/feed/pointer.d.ts +2 -0
- package/dist/feed/pointer.js +17 -0
- package/dist/file-types.js +1 -0
- package/dist/host-command.d.ts +3 -0
- package/dist/host-command.js +18 -0
- package/dist/host.d.ts +3 -0
- package/dist/host.js +3 -0
- package/dist/learn/check.d.ts +17 -1
- package/dist/learn/check.js +240 -5
- package/dist/learn/compile.d.ts +48 -27
- package/dist/learn/compile.js +134 -101
- package/dist/learn/promote.d.ts +61 -5
- package/dist/learn/promote.js +12 -2
- package/dist/learned-nodes.d.ts +21 -4
- package/dist/learned-nodes.js +21 -8
- package/dist/learned-workflow.d.ts +3 -3
- package/dist/learned-workflow.js +14 -5
- package/dist/learning-setting.d.ts +23 -0
- package/dist/learning-setting.js +51 -0
- package/dist/learning-words.d.ts +3 -0
- package/dist/learning-words.js +3 -0
- package/dist/learning.d.ts +3 -2
- package/dist/learning.js +5 -3
- package/dist/mcp-oauth.d.ts +23 -1
- package/dist/mcp-oauth.js +46 -5
- package/dist/mcp-pin-first.d.ts +4 -2
- package/dist/mcp-pin-first.js +4 -3
- package/dist/mcp-private-hosts.d.ts +13 -0
- package/dist/mcp-private-hosts.js +16 -0
- package/dist/mcp-servers-store.d.ts +58 -5
- package/dist/mcp-servers-store.js +136 -17
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +2 -2
- package/dist/observation/http.d.ts +4 -1
- package/dist/observation/http.js +5 -4
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +151 -21
- package/dist/print.d.ts +6 -3
- package/dist/print.js +4 -2
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- package/dist/run-labels.d.ts +23 -0
- package/dist/run-labels.js +79 -0
- package/dist/run-learning-served.d.ts +1 -1
- package/dist/run-reads-over-store.d.ts +5 -1
- package/dist/run-reads-over-store.js +8 -4
- package/dist/run-recordings.d.ts +20 -0
- package/dist/run-recordings.js +133 -0
- package/dist/run-secrets.js +12 -4
- package/dist/runner/adapter-loop.d.ts +1 -1
- package/dist/runner/adapter-loop.js +94 -16
- package/dist/runner/adapter-run-spec.d.ts +19 -0
- package/dist/runner/adapter-run-spec.js +57 -36
- package/dist/runner/beside.d.ts +6 -3
- package/dist/runner/beside.js +9 -7
- package/dist/runner/cli-run-spec.js +41 -32
- package/dist/runner/conformance/harness.js +69 -14
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/feedback-round.d.ts +9 -25
- package/dist/runner/feedback-round.js +22 -40
- package/dist/runner/learning-end.d.ts +17 -7
- package/dist/runner/learning-end.js +79 -53
- package/dist/runner/main.d.ts +12 -1
- package/dist/runner/main.js +31 -4
- package/dist/runner/mcp-claim.d.ts +3 -0
- package/dist/runner/mcp-claim.js +1 -1
- package/dist/runner/mcp-run.d.ts +6 -2
- package/dist/runner/mcp-run.js +5 -3
- package/dist/runner/model-route.d.ts +4 -2
- package/dist/runner/model-route.js +8 -8
- package/dist/runner/promotion.d.ts +44 -4
- package/dist/runner/promotion.js +323 -21
- package/dist/runner/reflect-hook.d.ts +5 -0
- package/dist/runner/reflect-hook.js +22 -14
- package/dist/runner/store-secrets.js +3 -1
- package/dist/runner/workflow-first.d.ts +6 -11
- package/dist/runner/workflow-first.js +16 -20
- package/dist/runs-over-store.d.ts +1 -1
- package/dist/runs-over-store.js +5 -2
- package/dist/sdk/runs.d.ts +7 -1
- package/dist/sdk/runs.js +21 -7
- package/dist/secret-cut.js +3 -1
- package/dist/secrets.js +5 -3
- package/dist/server.d.ts +31 -5
- package/dist/server.js +240 -61
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/dialect.d.ts +3 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/postgres/dialect.js +1 -0
- package/dist/store/postgres/migrations/0015_run_labels.sql +5 -0
- package/dist/store/postgres/migrations/0016_minted_tokens.sql +21 -0
- package/dist/store/schema.d.ts +9 -0
- package/dist/store/schema.js +26 -2
- package/dist/store/sqlite/connection.js +11 -3
- package/dist/store/sqlite/run-patch.d.ts +2 -1
- package/dist/store/sqlite/run-patch.js +3 -2
- package/dist/store/sqlite/store.js +64 -10
- package/dist/store/store.d.ts +38 -1
- package/dist/store-reads/journal.js +2 -1
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/learning.js +6 -1
- package/dist/store-reads/loop.d.ts +1 -0
- package/dist/store-reads/loop.js +14 -4
- package/dist/store-reads/runs.d.ts +4 -3
- package/dist/store-reads/runs.js +3 -2
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +8 -2
- package/dist/template-eval.d.ts +11 -0
- package/dist/template-eval.js +177 -52
- package/dist/templates.d.ts +13 -1
- package/dist/templates.js +89 -18
- package/dist/timeline/computer.js +2 -1
- package/dist/timeline/origin.d.ts +1 -1
- package/dist/timeline/origin.js +2 -2
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +11 -0
- package/dist/trigger-adapter.d.ts +9 -4
- package/dist/trigger-adapters.js +2 -1
- package/dist/trigger-feed.d.ts +12 -0
- package/dist/trigger-feed.js +420 -0
- package/dist/triggers.d.ts +2 -1
- package/dist/triggers.js +23 -5
- package/dist/validate.js +7 -2
- package/dist/webhooks.js +9 -5
- package/dist/workspace/computer-deps.d.ts +5 -1
- package/dist/workspace/computer-deps.js +2 -2
- package/dist/workspace/computer-for-claim.d.ts +7 -0
- package/dist/workspace/computer-for-claim.js +12 -2
- package/dist/workspace/index.d.ts +1 -0
- package/dist/workspace/index.js +2 -0
- package/dist/workspace/paths.d.ts +4 -0
- package/dist/workspace/paths.js +9 -0
- package/dist/workspace/resolve-computer.d.ts +35 -4
- package/dist/workspace/resolve-computer.js +47 -9
- package/launcher/agentrun-serve +34 -22
- package/package.json +11 -9
- package/templates/README.md +38 -4
- package/dist/console/assets/index-Jbxjcmdp.js +0 -705
package/dist/run-files.js
CHANGED
|
@@ -73,8 +73,23 @@ const fileEntry = async (workspace, rel) => {
|
|
|
73
73
|
const entry = await entryAt(workspace, rel);
|
|
74
74
|
return entry?.kind === "file" ? entry : undefined;
|
|
75
75
|
};
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
76
|
+
/** The one byte range a Range header asks of a file of `size` bytes, first and last byte; undefined for the whole file (no
|
|
77
|
+
* Range, or one this server does not take: another unit, several ranges, or a last byte before the first);
|
|
78
|
+
* "unsatisfiable" when it starts past the file's end. */
|
|
79
|
+
export function rangeOf(header, size) {
|
|
80
|
+
const asked = /^bytes=(\d*)-(\d*)$/.exec(header?.trim() ?? "");
|
|
81
|
+
if (!asked || (asked[1] === "" && asked[2] === ""))
|
|
82
|
+
return undefined;
|
|
83
|
+
if (asked[1] === "")
|
|
84
|
+
return Number(asked[2]) === 0 || size === 0 ? "unsatisfiable" : [Math.max(0, size - Number(asked[2])), size - 1];
|
|
85
|
+
const start = Number(asked[1]);
|
|
86
|
+
if (start >= size)
|
|
87
|
+
return "unsatisfiable";
|
|
88
|
+
const end = asked[2] === "" ? size - 1 : Math.min(Number(asked[2]), size - 1);
|
|
89
|
+
return end < start ? undefined : [start, end];
|
|
90
|
+
}
|
|
91
|
+
/** File `rel` of `workspace`, raw, in reads of SEND_CHUNK, whole or the one byte range its Range header asks (206): its
|
|
92
|
+
* length from the listing, so HEAD reads no byte. `who` names who makes a file private, in the words of a 403. */
|
|
78
93
|
export async function sendFile(req, res, url, workspace, rel, who) {
|
|
79
94
|
const unreadable = () => new ApiError("system_file_forbidden", `This file was created private by ${who}: this server cannot read it.`, { details: { reason: "unreadable" } });
|
|
80
95
|
const entry = await fileEntry(workspace, rel);
|
|
@@ -82,9 +97,14 @@ export async function sendFile(req, res, url, workspace, rel, who) {
|
|
|
82
97
|
throw new ApiError("not_found", "no such file");
|
|
83
98
|
if (entry.unreadable)
|
|
84
99
|
throw unreadable();
|
|
100
|
+
const range = rangeOf(req.headers?.range, entry.size);
|
|
101
|
+
if (range === "unsatisfiable")
|
|
102
|
+
throw new ApiError("range_not_satisfiable", `The range asked for starts past the file's end: it is ${entry.size} bytes.`, {}, { "content-range": `bytes */${entry.size}` });
|
|
103
|
+
const [start, last] = range ?? [0, entry.size - 1];
|
|
104
|
+
const length = last - start + 1;
|
|
85
105
|
// A GET reads its first chunk before the head: a file gone, or swapped for a link, since the listing is a 404, not a
|
|
86
106
|
// broken 200. A HEAD reads none.
|
|
87
|
-
const first = req.method === "HEAD" ? new Uint8Array(0) : await workspace.read(rel, { offset:
|
|
107
|
+
const first = req.method === "HEAD" ? new Uint8Array(0) : await workspace.read(rel, { offset: start, length: Math.min(SEND_CHUNK, length) }).catch((error) => {
|
|
88
108
|
if (isWorkspaceError(error, "REFUSED"))
|
|
89
109
|
throw unreadable();
|
|
90
110
|
if (skipped(error))
|
|
@@ -94,9 +114,11 @@ export async function sendFile(req, res, url, workspace, rel, who) {
|
|
|
94
114
|
const type = typeOf(rel) ?? "application/octet-stream";
|
|
95
115
|
const download = url.searchParams.get("download") === "1" || !INLINE_TYPES.has(type);
|
|
96
116
|
const name = rel.split("/").at(-1).replace(/[^\w.-]/g, "_");
|
|
97
|
-
res.writeHead(200, {
|
|
117
|
+
res.writeHead(range ? 206 : 200, {
|
|
98
118
|
"content-type": type,
|
|
99
|
-
"content-length":
|
|
119
|
+
"content-length": length,
|
|
120
|
+
"accept-ranges": "bytes",
|
|
121
|
+
...(range ? { "content-range": `bytes ${start}-${last}/${entry.size}` } : {}),
|
|
100
122
|
"x-content-type-options": "nosniff",
|
|
101
123
|
"cache-control": "no-store",
|
|
102
124
|
...(type.startsWith("text/html") ? { "content-security-policy": FILE_CSP } : {}),
|
|
@@ -107,13 +129,13 @@ export async function sendFile(req, res, url, workspace, rel, who) {
|
|
|
107
129
|
// The length said is the listing's: a file that changes while it is sent is cut there, or the answer ends short.
|
|
108
130
|
try {
|
|
109
131
|
let sent = 0;
|
|
110
|
-
for (let chunk = first;; chunk = await workspace.read(rel, { offset: sent, length: Math.min(SEND_CHUNK,
|
|
132
|
+
for (let chunk = first;; chunk = await workspace.read(rel, { offset: start + sent, length: Math.min(SEND_CHUNK, length - sent) })) {
|
|
111
133
|
if (chunk.length === 0 || res.destroyed)
|
|
112
134
|
break;
|
|
113
135
|
if (!res.write(chunk))
|
|
114
136
|
await new Promise((resolve) => { res.once("drain", resolve); res.once("close", resolve); });
|
|
115
137
|
sent += chunk.length;
|
|
116
|
-
if (sent >=
|
|
138
|
+
if (sent >= length)
|
|
117
139
|
break;
|
|
118
140
|
}
|
|
119
141
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type InputIssue } from "./errors.js";
|
|
2
|
+
import type { RunLabels } from "./store/store.js";
|
|
3
|
+
/** At most this many labels on a run. */
|
|
4
|
+
export declare const MAX_LABELS = 16;
|
|
5
|
+
/** A label's key: 1 to 63 characters, letters, digits and `.`, `_`, `-`, `/`, starting with a letter or a digit. No `:`, so
|
|
6
|
+
* a filter's `<key>:<value>` splits at its first one. */
|
|
7
|
+
export declare const LABEL_KEY: RegExp;
|
|
8
|
+
/** A label's value: text of 1 to this many characters, counted as Unicode code points (as JSON Schema's maxLength counts
|
|
9
|
+
* them), never UTF-16 units: an emoji is one character. */
|
|
10
|
+
export declare const MAX_LABEL_VALUE = 256;
|
|
11
|
+
/** A UTF-16 surrogate standing alone, half of no character (in `u` mode a whole pair is one code point, so only a lone half
|
|
12
|
+
* matches). JSON.stringify keeps one as a `\ud800` escape that Postgres' json check takes but json_each_text cannot
|
|
13
|
+
* decode: kept on a run, it would fail every later label search that scans that run. */
|
|
14
|
+
export declare const LONE_SURROGATE: RegExp;
|
|
15
|
+
/** A create body's `labels`, field by field: an object of at most MAX_LABELS keys, each matching LABEL_KEY, each value text
|
|
16
|
+
* of 1 to MAX_LABEL_VALUE characters with no control character. Absent is none. */
|
|
17
|
+
export declare function labelsIssues(labels: unknown): InputIssue[];
|
|
18
|
+
/** A create body's labels, once labelsIssues found none: the map as given, or undefined when absent or empty. */
|
|
19
|
+
export declare function labelsOf(labels: unknown): RunLabels | undefined;
|
|
20
|
+
/** GET /runs' `label` parameter, `<key>:<value>` (split at the first `:`, which a key never holds): the label a run must
|
|
21
|
+
* hold. One per request: the document declares a single string, and a client generated from it sends one, so a second
|
|
22
|
+
* is 400 bad_request rather than read here and dropped by a client. 400 too for one that does not read as a label. */
|
|
23
|
+
export declare function labelFilter(given: readonly string[]): RunLabels | undefined;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// A run's labels: a small map of the caller's own strings, given at its start (POST /agents/{agent_id}/runs, POST /runs),
|
|
2
|
+
// kept on the run, served on every read of it, and filtered on by GET /runs (`label=<key>:<value>`, one per request: the
|
|
3
|
+
// OpenAPI document declares it a single string, which every generated client sends once). The server never reads
|
|
4
|
+
// them otherwise: they are the caller's way to find its runs again (its own ids for a run, a job, a tenant). They are no
|
|
5
|
+
// place for a secret: every read of the run serves them as given.
|
|
6
|
+
import { ApiError } from "./errors.js";
|
|
7
|
+
/** At most this many labels on a run. */
|
|
8
|
+
export const MAX_LABELS = 16;
|
|
9
|
+
/** A label's key: 1 to 63 characters, letters, digits and `.`, `_`, `-`, `/`, starting with a letter or a digit. No `:`, so
|
|
10
|
+
* a filter's `<key>:<value>` splits at its first one. */
|
|
11
|
+
export const LABEL_KEY = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,62}$/;
|
|
12
|
+
/** A label's value: text of 1 to this many characters, counted as Unicode code points (as JSON Schema's maxLength counts
|
|
13
|
+
* them), never UTF-16 units: an emoji is one character. */
|
|
14
|
+
export const MAX_LABEL_VALUE = 256;
|
|
15
|
+
/** No control character in a value: a label is shown as one line. */
|
|
16
|
+
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
17
|
+
/** A UTF-16 surrogate standing alone, half of no character (in `u` mode a whole pair is one code point, so only a lone half
|
|
18
|
+
* matches). JSON.stringify keeps one as a `\ud800` escape that Postgres' json check takes but json_each_text cannot
|
|
19
|
+
* decode: kept on a run, it would fail every later label search that scans that run. */
|
|
20
|
+
export const LONE_SURROGATE = /[\ud800-\udfff]/u;
|
|
21
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
22
|
+
/** Why a value is not a label's, or undefined when it is one. */
|
|
23
|
+
function valueProblem(value) {
|
|
24
|
+
if (typeof value !== "string")
|
|
25
|
+
return "is text";
|
|
26
|
+
const characters = [...value].length;
|
|
27
|
+
if (characters < 1 || characters > MAX_LABEL_VALUE)
|
|
28
|
+
return `is text of 1 to ${MAX_LABEL_VALUE} characters`;
|
|
29
|
+
if (CONTROL.test(value))
|
|
30
|
+
return "holds no control character (a newline, a tab, NUL)";
|
|
31
|
+
if (LONE_SURROGATE.test(value))
|
|
32
|
+
return "is whole Unicode characters (no lone UTF-16 surrogate)";
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/** A create body's `labels`, field by field: an object of at most MAX_LABELS keys, each matching LABEL_KEY, each value text
|
|
36
|
+
* of 1 to MAX_LABEL_VALUE characters with no control character. Absent is none. */
|
|
37
|
+
export function labelsIssues(labels) {
|
|
38
|
+
if (labels === undefined)
|
|
39
|
+
return [];
|
|
40
|
+
if (!isObject(labels))
|
|
41
|
+
return [{ field: "labels", reason: "wrong_type", message: "labels is an object of text values: {\"<key>\": \"<value>\"}" }];
|
|
42
|
+
const issues = [];
|
|
43
|
+
const keys = Object.keys(labels);
|
|
44
|
+
if (keys.length > MAX_LABELS)
|
|
45
|
+
issues.push({ field: "labels", reason: "too_many", message: `labels holds at most ${MAX_LABELS} keys; it holds ${keys.length}.` });
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
if (!LABEL_KEY.test(key)) {
|
|
48
|
+
issues.push({ field: `labels.${key}`, reason: "bad_format", message: `A label's key is 1 to 63 letters, digits, '.', '_', '-' or '/', starting with a letter or a digit: ${JSON.stringify(key)} is not one.` });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const problem = valueProblem(labels[key]);
|
|
52
|
+
if (problem)
|
|
53
|
+
issues.push({ field: `labels.${key}`, reason: typeof labels[key] === "string" ? "bad_format" : "wrong_type", message: `labels.${key} ${problem}.` });
|
|
54
|
+
}
|
|
55
|
+
return issues;
|
|
56
|
+
}
|
|
57
|
+
/** A create body's labels, once labelsIssues found none: the map as given, or undefined when absent or empty. */
|
|
58
|
+
export function labelsOf(labels) {
|
|
59
|
+
if (!isObject(labels) || Object.keys(labels).length === 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
return Object.fromEntries(Object.entries(labels).map(([key, value]) => [key, String(value)]));
|
|
62
|
+
}
|
|
63
|
+
/** GET /runs' `label` parameter, `<key>:<value>` (split at the first `:`, which a key never holds): the label a run must
|
|
64
|
+
* hold. One per request: the document declares a single string, and a client generated from it sends one, so a second
|
|
65
|
+
* is 400 bad_request rather than read here and dropped by a client. 400 too for one that does not read as a label. */
|
|
66
|
+
export function labelFilter(given) {
|
|
67
|
+
if (given.length === 0)
|
|
68
|
+
return undefined;
|
|
69
|
+
if (given.length > 1)
|
|
70
|
+
throw new ApiError("bad_request", "label is given once: one <key>:<value> per request");
|
|
71
|
+
const one = given[0];
|
|
72
|
+
const at = one.indexOf(":");
|
|
73
|
+
const key = at < 0 ? one : one.slice(0, at);
|
|
74
|
+
const value = at < 0 ? undefined : one.slice(at + 1);
|
|
75
|
+
if (!LABEL_KEY.test(key) || value === undefined || valueProblem(value) !== undefined) {
|
|
76
|
+
throw new ApiError("bad_request", `label is <key>:<value>, a label's key and its value (${JSON.stringify(one)} is not one)`);
|
|
77
|
+
}
|
|
78
|
+
return { [key]: value };
|
|
79
|
+
}
|
|
@@ -76,7 +76,7 @@ export declare function servedRunLearning(deps: {
|
|
|
76
76
|
graded_at: string;
|
|
77
77
|
path: "agent" | "workflow" | "handed_back" | "replay";
|
|
78
78
|
workflow_digest: string | null;
|
|
79
|
-
feedback: "
|
|
79
|
+
feedback: "look_back" | null;
|
|
80
80
|
run_id: string;
|
|
81
81
|
correct: boolean;
|
|
82
82
|
};
|
|
@@ -17,6 +17,9 @@ export interface RunReadsDeps {
|
|
|
17
17
|
/** The run's Workspace for a server-side read (not the runner's claim-time openRunWorkspace): null when it has no folder
|
|
18
18
|
* (an imported run never has one). */
|
|
19
19
|
runWorkspace(store: RunReadsStore, runId: string): Promise<Workspace | null>;
|
|
20
|
+
/** The run's recordings folder on this machine, read-only (run-recordings.ts recordingsWorkspace): null when it recorded
|
|
21
|
+
* nothing. */
|
|
22
|
+
recordings(store: RunReadsStore, runId: string): Promise<Workspace | null>;
|
|
20
23
|
/** The environment the plane re-check reads (tool-plane.ts recheckPlane): the server's keys as it holds them. */
|
|
21
24
|
readonly env: NodeJS.ProcessEnv;
|
|
22
25
|
/** The run's open live browser viewer, when its runner serves one (its link), or undefined. The runner publishes the
|
|
@@ -47,7 +50,8 @@ export declare function listRunFiles(store: RunReadsStore, runId: string, deps:
|
|
|
47
50
|
truncated: boolean;
|
|
48
51
|
}>;
|
|
49
52
|
/** One of a run's files, through its Workspace (readRunFile): streamed to `res` as the route streams it, with the route's
|
|
50
|
-
* path check and its 404 words; a withheld run refuses (403) before anything is streamed.
|
|
53
|
+
* path check and its 404 words; a withheld run refuses (403) before anything is streamed. A path under recordings/ is
|
|
54
|
+
* one of the run's recordings (its recordings folder, beside its Workspace's), never a file the run made. */
|
|
51
55
|
export declare function readRunFile(store: RunReadsStore, runId: string, rawPath: string, req: IncomingMessage, res: ServerResponse, url: URL, deps: RunReadsDeps): Promise<void>;
|
|
52
56
|
/** A run's browser state (getRunBrowser): `live` with its viewer's link when its runner serves one; else `off` with why
|
|
53
57
|
* when the run has no browser or its browser is unavailable here now (the kept plane re-checked, as a launch would); else
|
|
@@ -10,6 +10,7 @@ import { costIsBooked } from "./runner/metrics.js";
|
|
|
10
10
|
import { runEntries, runTimeline } from "./store-reads/journal.js";
|
|
11
11
|
import { StoreLaunchMissing } from "./store/index.js";
|
|
12
12
|
import { isUnavailable, recheckPlane, whyUnavailable } from "./tool-plane.js";
|
|
13
|
+
import { RECORDINGS } from "./workspace/paths.js";
|
|
13
14
|
/** The most files a list answers with (server.ts FILE_LIST_CAP). */
|
|
14
15
|
export const FILE_LIST_CAP = 2_000;
|
|
15
16
|
/** A run's files, through its Workspace (listRunFiles): every file, path-sorted, cut at the cap. A run with no folder lists
|
|
@@ -20,20 +21,23 @@ export async function listRunFiles(store, runId, deps) {
|
|
|
20
21
|
if (await deps.withheld(store, runId))
|
|
21
22
|
throw new ApiError("forbidden", WITHHELD_SIGN_IN);
|
|
22
23
|
const workspace = await deps.runWorkspace(store, runId);
|
|
23
|
-
|
|
24
|
+
// A folder the run made named recordings is not listed: the route's recordings/ is the run's recordings.
|
|
25
|
+
const { files, deep } = workspace ? await listFiles(workspace, FILE_LIST_CAP, { owned: (name) => name === RECORDINGS }) : { files: [], deep: false };
|
|
24
26
|
files.sort((a, b) => a.path.localeCompare(b.path));
|
|
25
27
|
return { run_id: runId, files: files.slice(0, FILE_LIST_CAP), complete: true, truncated: deep || files.length > FILE_LIST_CAP };
|
|
26
28
|
}
|
|
27
29
|
/** One of a run's files, through its Workspace (readRunFile): streamed to `res` as the route streams it, with the route's
|
|
28
|
-
* path check and its 404 words; a withheld run refuses (403) before anything is streamed.
|
|
30
|
+
* path check and its 404 words; a withheld run refuses (403) before anything is streamed. A path under recordings/ is
|
|
31
|
+
* one of the run's recordings (its recordings folder, beside its Workspace's), never a file the run made. */
|
|
29
32
|
export async function readRunFile(store, runId, rawPath, req, res, url, deps) {
|
|
30
33
|
if (await deps.withheld(store, runId))
|
|
31
34
|
throw new ApiError("forbidden", WITHHELD_SIGN_IN);
|
|
32
35
|
const rel = checkedPath(decodedPath(rawPath), "the run's own, not a file it wrote");
|
|
33
|
-
const
|
|
36
|
+
const recording = rel.startsWith(`${RECORDINGS}/`) ? rel.slice(RECORDINGS.length + 1) : undefined;
|
|
37
|
+
const workspace = await (recording === undefined ? deps.runWorkspace : deps.recordings)(store, runId);
|
|
34
38
|
if (!workspace)
|
|
35
39
|
throw new ApiError("not_found", "no such file");
|
|
36
|
-
return sendFile(req, res, url, workspace, rel, "a tool in the run's box");
|
|
40
|
+
return sendFile(req, res, url, workspace, recording ?? rel, "a tool in the run's box");
|
|
37
41
|
}
|
|
38
42
|
/** Whether the run's revision grants the browser tool: an agentic revision whose tools name it. */
|
|
39
43
|
async function grantsBrowser(store, revisionId) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Workspace } from "./workspace/index.js";
|
|
2
|
+
/** One recording as the run read lists it. `finished`: ffmpeg finished the file, or the run has ended and the file has
|
|
3
|
+
* stopped changing (an encoder cut short leaves a file that plays up to its last fragment). */
|
|
4
|
+
export type RecordingView = {
|
|
5
|
+
path: string;
|
|
6
|
+
computer: string;
|
|
7
|
+
seconds: number;
|
|
8
|
+
bytes: number;
|
|
9
|
+
finished: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** The recordings `folder` holds as a read-only Workspace, or null when it holds none (the run recorded nothing). */
|
|
12
|
+
export declare function recordingsWorkspace(folder: string): Promise<Workspace | null>;
|
|
13
|
+
/** The recordings in `folder`, each computer's files in the order it recorded them: its path on the files route, its
|
|
14
|
+
* computer (its ref id, as its file is named: computer-record.ts names a computer's later files <computer>-2.mp4 and
|
|
15
|
+
* on), its length in seconds, its size, and whether it is finished: its trailer is there, or the run ended at `endedAt`
|
|
16
|
+
* and neither that end nor the file's last change is within twice the time the recorder gives ffmpeg to finish it
|
|
17
|
+
* (RECORDING_LIMITS.settleMs; the recorder stops at the computer's release, right after the run's end is saved, and a
|
|
18
|
+
* quiet screen's file may not have changed for a while before it). None when the folder isn't there. A file that can't
|
|
19
|
+
* be read is listed with 0 seconds. */
|
|
20
|
+
export declare function recordingsOf(folder: string, endedAt: number | null): Promise<RecordingView[]>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// A run's screen recordings as the API serves them: the files computer-record.ts writes into the run's recordings folder
|
|
2
|
+
// on this machine (workspace/paths.ts recordingsFolder), beside its work/ folder. The run view lists them (`recordings`),
|
|
3
|
+
// and the run files route serves them under recordings/, read-only, to the readers of the run's other files.
|
|
4
|
+
//
|
|
5
|
+
// A file's length is read from its own boxes, never by running a program: ffmpeg's fragmented MP4 says each fragment's
|
|
6
|
+
// start (tfdt) and each frame's duration (trun, else tfhd's or trex's default), at its track's timescale (mdhd). A
|
|
7
|
+
// finished file ends with ffmpeg's trailer (mfra); one still being written has none, and its last box may be cut short,
|
|
8
|
+
// so it is read up to its last whole box. A read goes on from where the one before it stopped, so a console that polls a
|
|
9
|
+
// recording being written reads only what was added.
|
|
10
|
+
import { lstat, open, readdir } from "node:fs/promises";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { RECORDING_LIMITS } from "./computer-record.js";
|
|
13
|
+
import { localWorkspace, readOnlyWorkspace } from "./workspace/index.js";
|
|
14
|
+
import { RECORDINGS } from "./workspace/paths.js";
|
|
15
|
+
/** The recordings `folder` holds as a read-only Workspace, or null when it holds none (the run recorded nothing). */
|
|
16
|
+
export async function recordingsWorkspace(folder) {
|
|
17
|
+
const there = await lstat(folder).catch(() => undefined);
|
|
18
|
+
return there?.isDirectory() ? readOnlyWorkspace(localWorkspace(folder)) : null;
|
|
19
|
+
}
|
|
20
|
+
/** The newest reads, by path: at most READS_KEPT. */
|
|
21
|
+
const reads = new Map();
|
|
22
|
+
const READS_KEPT = 256;
|
|
23
|
+
/** The boxes in `buf`, each its type and its body; a box cut short ends the walk. */
|
|
24
|
+
function* boxes(buf) {
|
|
25
|
+
for (let at = 0; at + 8 <= buf.length;) {
|
|
26
|
+
let length = buf.readUInt32BE(at);
|
|
27
|
+
let head = 8;
|
|
28
|
+
if (length === 1 && at + 16 <= buf.length)
|
|
29
|
+
[length, head] = [Number(buf.readBigUInt64BE(at + 8)), 16];
|
|
30
|
+
if (length < head || at + length > buf.length)
|
|
31
|
+
return;
|
|
32
|
+
yield [buf.toString("latin1", at + 4, at + 8), buf.subarray(at + head, at + length)];
|
|
33
|
+
at += length;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** What box `type` says into `read`: the timescale, the default frame duration, and a fragment's end. */
|
|
37
|
+
function take(read, type, body) {
|
|
38
|
+
if (type === "moov" || type === "trak" || type === "mdia" || type === "mvex" || type === "moof")
|
|
39
|
+
for (const [inner, part] of boxes(body))
|
|
40
|
+
take(read, inner, part);
|
|
41
|
+
else if (type === "mdhd")
|
|
42
|
+
read.timescale = body.readUInt32BE(body[0] === 1 ? 20 : 12);
|
|
43
|
+
else if (type === "trex")
|
|
44
|
+
read.dflt = body.readUInt32BE(12);
|
|
45
|
+
else if (type === "traf") {
|
|
46
|
+
let start = 0;
|
|
47
|
+
let dflt = read.dflt;
|
|
48
|
+
let length = 0;
|
|
49
|
+
for (const [inner, part] of boxes(body)) {
|
|
50
|
+
const flags = part.readUInt32BE(0) & 0xffffff;
|
|
51
|
+
if (inner === "tfhd" && flags & 0x8)
|
|
52
|
+
dflt = part.readUInt32BE(8 + (flags & 0x1 ? 8 : 0) + (flags & 0x2 ? 4 : 0));
|
|
53
|
+
else if (inner === "tfdt")
|
|
54
|
+
start = part[0] === 1 ? Number(part.readBigUInt64BE(4)) : part.readUInt32BE(4);
|
|
55
|
+
else if (inner === "trun") {
|
|
56
|
+
const count = part.readUInt32BE(4);
|
|
57
|
+
const stride = 4 * [0x100, 0x200, 0x400, 0x800].filter((bit) => flags & bit).length;
|
|
58
|
+
for (let i = 0, at = 8 + (flags & 0x1 ? 4 : 0) + (flags & 0x4 ? 4 : 0); i < count; i += 1, at += stride)
|
|
59
|
+
length += flags & 0x100 ? part.readUInt32BE(at) : dflt;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
read.end = Math.max(read.end, start + length);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** File `path`'s read, `size` bytes long and changed at `mtimeMs` now: the last read when the file is unchanged (it is not
|
|
66
|
+
* opened), on from it when the file only grew, else from its start. */
|
|
67
|
+
async function readOf(path, size, mtimeMs) {
|
|
68
|
+
const kept = reads.get(path);
|
|
69
|
+
if (kept && kept.size === size && kept.mtimeMs === mtimeMs)
|
|
70
|
+
return kept;
|
|
71
|
+
const read = kept && kept.size <= size ? { ...kept } : { at: 0, size: 0, mtimeMs: 0, timescale: 0, dflt: 0, end: 0, finished: false };
|
|
72
|
+
const file = await open(path, "r");
|
|
73
|
+
try {
|
|
74
|
+
const head = Buffer.alloc(16);
|
|
75
|
+
while (read.at + 8 <= size) {
|
|
76
|
+
await file.read(head, 0, 16, read.at);
|
|
77
|
+
let length = head.readUInt32BE(0);
|
|
78
|
+
let skip = 8;
|
|
79
|
+
if (length === 1)
|
|
80
|
+
[length, skip] = [Number(head.readBigUInt64BE(8)), 16];
|
|
81
|
+
if (length < skip || read.at + length > size)
|
|
82
|
+
break;
|
|
83
|
+
const type = head.toString("latin1", 4, 8);
|
|
84
|
+
if (type === "mfra")
|
|
85
|
+
read.finished = true;
|
|
86
|
+
if (type === "moov" || type === "moof") {
|
|
87
|
+
const body = Buffer.alloc(length - skip);
|
|
88
|
+
await file.read(body, 0, body.length, read.at + skip);
|
|
89
|
+
take(read, type, body);
|
|
90
|
+
}
|
|
91
|
+
read.at += length;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await file.close();
|
|
96
|
+
}
|
|
97
|
+
read.size = size;
|
|
98
|
+
read.mtimeMs = mtimeMs;
|
|
99
|
+
reads.delete(path);
|
|
100
|
+
reads.set(path, read);
|
|
101
|
+
if (reads.size > READS_KEPT)
|
|
102
|
+
reads.delete(reads.keys().next().value);
|
|
103
|
+
return read;
|
|
104
|
+
}
|
|
105
|
+
/** The recordings in `folder`, each computer's files in the order it recorded them: its path on the files route, its
|
|
106
|
+
* computer (its ref id, as its file is named: computer-record.ts names a computer's later files <computer>-2.mp4 and
|
|
107
|
+
* on), its length in seconds, its size, and whether it is finished: its trailer is there, or the run ended at `endedAt`
|
|
108
|
+
* and neither that end nor the file's last change is within twice the time the recorder gives ffmpeg to finish it
|
|
109
|
+
* (RECORDING_LIMITS.settleMs; the recorder stops at the computer's release, right after the run's end is saved, and a
|
|
110
|
+
* quiet screen's file may not have changed for a while before it). None when the folder isn't there. A file that can't
|
|
111
|
+
* be read is listed with 0 seconds. */
|
|
112
|
+
export async function recordingsOf(folder, endedAt) {
|
|
113
|
+
const names = await readdir(folder).catch(() => []);
|
|
114
|
+
const files = new Set(names.filter((name) => name.endsWith(".mp4")));
|
|
115
|
+
const series = [...files].map((name) => {
|
|
116
|
+
const base = name.slice(0, -".mp4".length);
|
|
117
|
+
const later = /^(.+)-(\d+)$/.exec(base);
|
|
118
|
+
return later && Number(later[2]) >= 2 && files.has(`${later[1]}.mp4`) ? { name, computer: later[1], n: Number(later[2]) } : { name, computer: base, n: 1 };
|
|
119
|
+
});
|
|
120
|
+
series.sort((a, b) => (a.computer === b.computer ? a.n - b.n : a.computer < b.computer ? -1 : 1));
|
|
121
|
+
const listed = [];
|
|
122
|
+
for (const { name, computer } of series) {
|
|
123
|
+
const path = join(folder, name);
|
|
124
|
+
const file = await lstat(path).catch(() => undefined);
|
|
125
|
+
if (!file?.isFile())
|
|
126
|
+
continue;
|
|
127
|
+
const read = await readOf(path, file.size, file.mtimeMs).catch(() => undefined);
|
|
128
|
+
const seconds = read?.timescale ? Math.round((read.end / read.timescale) * 10) / 10 : 0;
|
|
129
|
+
const settled = endedAt !== null && Date.now() - Math.max(endedAt, file.mtimeMs) > 2 * RECORDING_LIMITS.settleMs;
|
|
130
|
+
listed.push({ path: `${RECORDINGS}/${name}`, computer, seconds, bytes: file.size, finished: read?.finished === true || settled });
|
|
131
|
+
}
|
|
132
|
+
return listed;
|
|
133
|
+
}
|
package/dist/run-secrets.js
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import { harnessModelKeys } from "./adapters.js";
|
|
2
|
+
import { COMPUTER_KEY_NAMES } from "./computer-keys.js";
|
|
2
3
|
import { BOX_KEYS } from "./place-offers.js";
|
|
3
4
|
import { providersNamed } from "./decisions.js";
|
|
4
5
|
import { harnessOfBody } from "./harness.js";
|
|
5
6
|
import { harnessOf } from "./harness-table.js";
|
|
6
7
|
import { GATEWAY_KEY_ENV, llmTiers, modelKeys, nodeTiers, PROVIDER_KEYS, workflowNodes } from "./model.js";
|
|
7
8
|
import { DECISION_KEYS, SECRET_NAMES } from "./secrets.js";
|
|
8
|
-
import { TOOL_KEYS, unlocksTool } from "./tool-keys.js";
|
|
9
|
+
import { TOOL_FIELDS, TOOL_KEYS, unlocksTool } from "./tool-keys.js";
|
|
9
10
|
import { workflowTools } from "./tools.js";
|
|
11
|
+
import { ARCHIL_API_KEY_ENV } from "./workspace/archil-key.js";
|
|
12
|
+
/** The computer providers' account keys a run never gets: every computer key (computer-keys.ts COMPUTER_KEY_NAMES) but
|
|
13
|
+
* a tool key's field (Browserbase's and Kernel's, which a run's `browser` tool takes when its revision binds it). Only
|
|
14
|
+
* the server's and the runner's computer providers read one, at a claim or a Settings test. */
|
|
15
|
+
const COMPUTER_ACCOUNT_KEYS = COMPUTER_KEY_NAMES.filter((name) => !TOOL_FIELDS.includes(name));
|
|
10
16
|
/** The server's own secrets, which a run never gets: the API token, by name or by value, the seal key the tool keys are
|
|
11
|
-
* kept under (tool-keys.ts), the store's URL (it may hold the database's password),
|
|
12
|
-
* (place-offers.ts BOX_KEYS), which only the server's
|
|
13
|
-
|
|
17
|
+
* kept under (tool-keys.ts), the store's URL (it may hold the database's password), the remote boxes' keys
|
|
18
|
+
* (place-offers.ts BOX_KEYS) and every other computer provider's account key, which only the server's and the runner's
|
|
19
|
+
* computer providers use, and the Archil account key (archil-key.ts), from which the claim mints each run's own mount
|
|
20
|
+
* credential. */
|
|
21
|
+
const SERVER_ONLY = new Set(["AGENTRUN_TOKEN", "AGENTRUN_TOKENS", "AGENTRUN_API_KEY", "AGENTRUN_SEAL_KEY", "AGENTRUN_STORE_URL", ...BOX_KEYS, ...COMPUTER_ACCOUNT_KEYS, ARCHIL_API_KEY_ENV]);
|
|
14
22
|
/** The secrets a run may be handed: every one but the server's own and the decision keys, which go apart. */
|
|
15
23
|
export const RUN_SECRETS = SECRET_NAMES.filter((name) => !SERVER_ONLY.has(name) && !DECISION_KEYS.includes(name));
|
|
16
24
|
/** The fields a run gets only when its revision names what they unlock: a `tool` key's. Any other kind's (a model key's)
|
|
@@ -138,7 +138,7 @@ export interface AdapterRunSpec {
|
|
|
138
138
|
readonly workflowFirst?: WorkflowFirst;
|
|
139
139
|
/** The learned workflow that fits the run but that the money rule stopped from running first (workflow-first.ts
|
|
140
140
|
* costLineOf): the loop keeps why on the root (WORKFLOW_FIRST, outcome needs_revision, the two numbers) as the agent
|
|
141
|
-
* takes the run
|
|
141
|
+
* takes the run. The stop asks for no revision. */
|
|
142
142
|
readonly workflowRevise?: WorkflowRevise;
|
|
143
143
|
/** The run's end-of-run step, when its spec builds one (over the per-claim resources it holds, its Archil claim among
|
|
144
144
|
* them): it wins over LoopDeps.endOfRun and is asked where that one is, in the end turn while the claim is held, before
|
|
@@ -392,6 +392,20 @@ async function openCliRecord(harness, journal, context, now, cwd, endsItsCalls =
|
|
|
392
392
|
followUps.add(requestId);
|
|
393
393
|
personWritten = true;
|
|
394
394
|
}),
|
|
395
|
+
steered: (input, requestId, origin) => inTurn(async () => {
|
|
396
|
+
if (followUps.has(requestId))
|
|
397
|
+
return;
|
|
398
|
+
const entry = origin
|
|
399
|
+
? { entry: { kind: KINDS.input, data: { origin, requestId, at: now() }, model: [{ role: "user", content: input, timestamp: now() }] } }
|
|
400
|
+
: { kind: KINDS.message, data: { role: "steer", text: input, requestId } };
|
|
401
|
+
if (turnsOpen === 0)
|
|
402
|
+
await startsTurn(entry);
|
|
403
|
+
else if ("entry" in entry)
|
|
404
|
+
await harness.commit((tx) => tx.appendEntry(root.id, entry.entry), context);
|
|
405
|
+
else
|
|
406
|
+
await append(entry.kind, entry.data);
|
|
407
|
+
followUps.add(requestId);
|
|
408
|
+
}),
|
|
395
409
|
message: (input, origin) => inTurn(async () => {
|
|
396
410
|
if (origin)
|
|
397
411
|
await startsTurn({ entry: { kind: KINDS.input, data: { origin }, model: [{ role: "user", content: input, timestamp: now() }] } });
|
|
@@ -665,6 +679,22 @@ function adapterLoop(specOf, options, deps) {
|
|
|
665
679
|
* end waits on each one's submission (turnOfRequest) before the run may end, so the run never reads completed with a
|
|
666
680
|
* message it took still unanswered, and its session is never closed under that answer. */
|
|
667
681
|
const steeredIn = [];
|
|
682
|
+
/** A CLI's steers (a harness whose record the loop keeps, and whose session steers: claude-agent), by request id: the
|
|
683
|
+
* model took it (its adapter's message_taken, `takenIds`), then its record entry, the commit its row is taken in
|
|
684
|
+
* (`recorded`). The record says where the model read it, after the tool calls in flight, never where it was queued. */
|
|
685
|
+
const cliSteers = new Map();
|
|
686
|
+
/** The submissions a CLI's adapter said its model took (message_taken), and who waits on one of them. */
|
|
687
|
+
const takenIds = new Set();
|
|
688
|
+
const takenWaiters = new Map();
|
|
689
|
+
const modelTook = (id) => takenIds.has(id) ? Promise.resolve() : new Promise((resolve) => takenWaiters.set(id, [...(takenWaiters.get(id) ?? []), resolve]));
|
|
690
|
+
const tookNow = (ids) => {
|
|
691
|
+
for (const id of ids) {
|
|
692
|
+
takenIds.add(id);
|
|
693
|
+
for (const resolve of takenWaiters.get(id) ?? [])
|
|
694
|
+
resolve();
|
|
695
|
+
takenWaiters.delete(id);
|
|
696
|
+
}
|
|
697
|
+
};
|
|
668
698
|
/** 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
|
|
669
699
|
* during the end-of-run turn does. */
|
|
670
700
|
let turnsOver = false;
|
|
@@ -1125,15 +1155,19 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1125
1155
|
}
|
|
1126
1156
|
// Who wrote it, when the person did not (a trigger's message): carried on its input, as the Timeline shows it.
|
|
1127
1157
|
const origin = originOf(command.body?.origin);
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1158
|
+
// A harness that takes no message mid-turn, or a CLI's message that is no steer of the turn in flight (a trigger's that
|
|
1159
|
+
// answers nothing, or the person's answer to the question its last turn ended on, whose session is idle): the API
|
|
1160
|
+
// answered it `follow_up` (it waits for the turn's end), so it is the run's next turn, after any taken before it
|
|
1161
|
+
// (afterTurn). Taken now as the dispatch takes a row it buffers: the store's take is the commit of its submission, once
|
|
1162
|
+
// sent.
|
|
1163
|
+
if (!session.steer || (cliRecord && (followUp || askedLast))) {
|
|
1132
1164
|
if (turnsOver)
|
|
1133
1165
|
return { later: "the run has answered and is ending: the message waits for the run's end" };
|
|
1134
1166
|
afterTurn.push({ commandId: command.id, text, requestId, ...(origin ? { origin } : {}) });
|
|
1135
1167
|
return { taken: true };
|
|
1136
1168
|
}
|
|
1169
|
+
if (cliRecord)
|
|
1170
|
+
return steerCli(session, cliRecord, gate, command.id, text, requestId, origin);
|
|
1137
1171
|
// The claim's last turn has ended and nothing more is sent (turnsOver): a steer placed now would start a turn the run's
|
|
1138
1172
|
// end then cuts, so it waits for the run's end, as a CLI's message does.
|
|
1139
1173
|
if (turnsOver && !askedLast)
|
|
@@ -1166,6 +1200,50 @@ function adapterLoop(specOf, options, deps) {
|
|
|
1166
1200
|
await taken.catch(() => { });
|
|
1167
1201
|
return { taken: true };
|
|
1168
1202
|
};
|
|
1203
|
+
/** A person's message steered into a CLI's turn in flight (claude-agent): its session places it (its harness sends it to the
|
|
1204
|
+
* CLI at once, which hands it to the model at its next step); once the model took it, the record's steer entry is written
|
|
1205
|
+
* and the row is taken in that commit (cliSteers). A runner lost before that leaves the row untaken, and the session holds
|
|
1206
|
+
* the steer under the row's request id: the next claim's steer() of the same row places nothing again, its resumed turn
|
|
1207
|
+
* carries the steer if the CLI never had it, and the record is written once. Neither lost nor sent twice. */
|
|
1208
|
+
const steerCli = async (theSession, theRecord, theGate, commandId, text, requestId, origin) => {
|
|
1209
|
+
if (turnsOver)
|
|
1210
|
+
return { later: "the run has answered and is ending: the message waits for the run's end" };
|
|
1211
|
+
const taken = theGate.expect(writesEntry(origin ? KINDS.input : KINDS.message, (data) => data.requestId === requestId), { take: [commandId] });
|
|
1212
|
+
taken.catch(() => undefined);
|
|
1213
|
+
// Waited on before it is placed: the model may take it before steer() resolves.
|
|
1214
|
+
const took = modelTook(requestId);
|
|
1215
|
+
try {
|
|
1216
|
+
await theSession.steer(text, requestId, "steer", origin ? { origin } : undefined);
|
|
1217
|
+
}
|
|
1218
|
+
catch (error) {
|
|
1219
|
+
if (theGate.sealed)
|
|
1220
|
+
return { later: "the run is parking: the steer waits for its resume" };
|
|
1221
|
+
return { refused: `the steer was not placed: ${error.message}` };
|
|
1222
|
+
}
|
|
1223
|
+
await specNow?.hooks?.afterSteerPlaced?.();
|
|
1224
|
+
const recorded = took.then(() => theRecord.steered(text, requestId, origin));
|
|
1225
|
+
recorded.catch(() => undefined);
|
|
1226
|
+
cliSteers.set(requestId, { text, ...(origin ? { origin } : {}), recorded });
|
|
1227
|
+
// Placed in the turn in flight: the turn's end waits for it to be answered (steeredIn), never ending the run first.
|
|
1228
|
+
steeredIn.push(requestId);
|
|
1229
|
+
return { taken: true };
|
|
1230
|
+
};
|
|
1231
|
+
/** The end of the turn that answers a CLI's steer (steerCli): the turn in flight, or the next one its session started for
|
|
1232
|
+
* it. One the turn that ended never took (it ended as the steer was placed) is placed again, which writes nothing twice
|
|
1233
|
+
* and starts the session's next turn. A turn that ends without the model taking it is that end: its row stays untaken. */
|
|
1234
|
+
const cliSteerAnswered = async (theSession, requestId) => {
|
|
1235
|
+
const steer = cliSteers.get(requestId);
|
|
1236
|
+
if (!steer)
|
|
1237
|
+
return theSession.turnEnd();
|
|
1238
|
+
if (!takenIds.has(requestId))
|
|
1239
|
+
await theSession.steer(steer.text, requestId, "steer", steer.origin ? { origin: steer.origin } : undefined);
|
|
1240
|
+
const end = await Promise.race([steer.recorded.then(() => undefined), theSession.turnEnd()]);
|
|
1241
|
+
if (end !== undefined && !takenIds.has(requestId))
|
|
1242
|
+
return end;
|
|
1243
|
+
await steer.recorded;
|
|
1244
|
+
cliSteers.delete(requestId);
|
|
1245
|
+
return theSession.turnEnd();
|
|
1246
|
+
};
|
|
1169
1247
|
/** A message the turn in flight could not take (afterTurn), sent as a turn of its own. Its row is taken only once the
|
|
1170
1248
|
* session holds the prompt (send resolves once it is placed): in the commit that writes it on the run's record, right
|
|
1171
1249
|
* after the send (a CLI's session writes no submission to the run's journal), or, for an adapter keeping its own record,
|
|
@@ -2162,6 +2240,9 @@ function adapterLoop(specOf, options, deps) {
|
|
|
2162
2240
|
// After a CLI's question to the person (TurnCut armed): what its model says is not on the record, as pi's turn says
|
|
2163
2241
|
// nothing after the question; a call of another tool lets the turn go on.
|
|
2164
2242
|
const onEvent = (theRecord) => (event) => {
|
|
2243
|
+
// The model took a steer: its record entry, and its row, follow (steerCli).
|
|
2244
|
+
if (event.type === "message_taken")
|
|
2245
|
+
return tookNow(event.ids);
|
|
2165
2246
|
if (askCut.armed && event.type === "text")
|
|
2166
2247
|
return;
|
|
2167
2248
|
if (askCut.armed && event.type === "tool_start")
|
|
@@ -2292,11 +2373,11 @@ function adapterLoop(specOf, options, deps) {
|
|
|
2292
2373
|
firstInputPlaced = new Promise((resolve) => (mine.placed = resolve));
|
|
2293
2374
|
reopen = mine;
|
|
2294
2375
|
}
|
|
2295
|
-
// A CLI run parked on its question (its turn cut there, TurnCut):
|
|
2296
|
-
// answer is this claim's turn, sent as a reopened run's follow-up
|
|
2297
|
-
// summary going in its take. A turn the session still reads open is
|
|
2298
|
-
// never recovered (recovered, the CLI would go on past its question).
|
|
2299
|
-
if (!endsRoot && !inFlight && spec.record === "loop" &&
|
|
2376
|
+
// A CLI run parked on its question (its turn cut there, TurnCut): no turn is in flight for a steer to reach (a CLI
|
|
2377
|
+
// steers only the turn in flight), so the person's answer is this claim's turn, sent as a reopened run's follow-up
|
|
2378
|
+
// is (steer, through `reopen`), the row's waiting summary going in its take. A turn the session still reads open is
|
|
2379
|
+
// the one the cut stopped: settled first, never recovered (recovered, the CLI would go on past its question).
|
|
2380
|
+
if (!endsRoot && !inFlight && spec.record === "loop" && turnEndedOnAsk(foundEntries) !== undefined) {
|
|
2300
2381
|
if (session.state === "open_turn")
|
|
2301
2382
|
await session.settleOpenTurn();
|
|
2302
2383
|
if (session.state === "idle")
|
|
@@ -2793,7 +2874,7 @@ function adapterLoop(specOf, options, deps) {
|
|
|
2793
2874
|
// the run's end. One already answered settles at once.
|
|
2794
2875
|
const steered = cut ? undefined : steeredIn.shift();
|
|
2795
2876
|
if (steered !== undefined) {
|
|
2796
|
-
turn = turnOfRequest(harness, root, steered);
|
|
2877
|
+
turn = record ? cliSteerAnswered(session, steered) : turnOfRequest(harness, root, steered);
|
|
2797
2878
|
continue;
|
|
2798
2879
|
}
|
|
2799
2880
|
// The output contract: an answer whose last json block misses the agent's output schema is asked again, once,
|
|
@@ -3499,14 +3580,11 @@ function adapterLoop(specOf, options, deps) {
|
|
|
3499
3580
|
const theSession = session;
|
|
3500
3581
|
const requestId = reflectRequestId(claim.runId, again, round, step);
|
|
3501
3582
|
const theRecord = cliRecord;
|
|
3502
|
-
// A grade's feedback round (feedbackNow): its first ask is told the grade first (feedback-round.ts feedbackAsk),
|
|
3503
|
-
//
|
|
3504
|
-
// workflow's answer before it, cut of every secret the runner holds, as a follow-up's are.
|
|
3583
|
+
// A grade's feedback round (feedbackNow): its first ask is told the grade first (feedback-round.ts feedbackAsk), cut
|
|
3584
|
+
// of every secret the runner holds. Only a run the agent finished opens one, so its session saw the job.
|
|
3505
3585
|
if (feedbackNow && !again && step === undefined) {
|
|
3506
3586
|
const cut = (text) => (deps.redact ? deps.redact(text) : text);
|
|
3507
|
-
|
|
3508
|
-
const seen = servedNow && specNow ? { job: specNow.input, served: { workflow: servedNow.workflow, answer: cut(servedNow.answer) } } : undefined;
|
|
3509
|
-
prompt = `${feedbackAsk({ ...feedbackNow, text: cut(feedbackNow.text) }, seen)}\n\n${prompt}`;
|
|
3587
|
+
prompt = `${feedbackAsk({ ...feedbackNow, text: cut(feedbackNow.text) })}\n\n${prompt}`;
|
|
3510
3588
|
}
|
|
3511
3589
|
let timer;
|
|
3512
3590
|
try {
|