@parcha/agentrun-serve 0.1.0-beta.41 → 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 +53 -0
- package/NOTICE +2 -0
- package/README.md +77 -12
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.js +3 -1
- 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 +3 -1
- 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.js +2 -14
- package/dist/console/assets/index-DYJ_wj27.js +711 -0
- package/dist/console/index.html +1 -1
- package/dist/eval-feed.d.ts +2 -2
- package/dist/eval-feed.js +9 -5
- package/dist/feed/parse.d.ts +3 -2
- package/dist/feed/parse.js +9 -18
- 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 +14 -0
- package/dist/learn/check.js +194 -14
- 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/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 +8 -0
- package/dist/mcp-oauth.js +7 -0
- package/dist/mcp-private-hosts.d.ts +13 -0
- package/dist/mcp-private-hosts.js +16 -0
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +105 -9
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- 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/runner/adapter-loop.d.ts +1 -1
- package/dist/runner/adapter-loop.js +3 -6
- package/dist/runner/adapter-run-spec.d.ts +16 -1
- package/dist/runner/adapter-run-spec.js +20 -6
- package/dist/runner/beside.js +1 -1
- package/dist/runner/cli-run-spec.js +10 -7
- 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 +11 -0
- package/dist/runner/main.js +29 -2
- package/dist/runner/mcp-run.d.ts +2 -2
- package/dist/runner/promotion.d.ts +36 -4
- package/dist/runner/promotion.js +280 -20
- 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 +0 -6
- package/dist/runner/workflow-first.js +7 -13
- package/dist/runs-over-store.d.ts +1 -1
- package/dist/sdk/runs.js +2 -1
- package/dist/secrets.js +2 -2
- package/dist/server.d.ts +4 -3
- package/dist/server.js +72 -11
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/loop.d.ts +1 -0
- package/dist/store-reads/loop.js +14 -4
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +4 -0
- package/dist/template-eval.d.ts +7 -0
- package/dist/template-eval.js +54 -6
- package/dist/templates.d.ts +3 -1
- package/dist/templates.js +14 -1
- package/dist/timeline/computer.js +2 -1
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +11 -0
- package/dist/trigger-feed.js +67 -18
- package/dist/validate.js +7 -2
- package/dist/workspace/computer-for-claim.d.ts +7 -0
- package/dist/workspace/computer-for-claim.js +12 -2
- package/dist/workspace/paths.d.ts +4 -0
- package/dist/workspace/paths.js +9 -0
- package/dist/workspace/resolve-computer.d.ts +3 -1
- package/launcher/agentrun-serve +31 -24
- package/package.json +10 -9
- package/templates/README.md +11 -2
- package/dist/console/assets/index-COGhx_2Z.js +0 -711
package/dist/console/index.html
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<meta name="color-scheme" content="light dark" />
|
|
7
7
|
<title>AgentRun</title>
|
|
8
8
|
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' fill='%23ff8000'/%3E%3Cpath d='M12 9l11 7-11 7z' fill='%23000'/%3E%3C/svg%3E" />
|
|
9
|
-
<script type="module" crossorigin src="./assets/index-
|
|
9
|
+
<script type="module" crossorigin src="./assets/index-DYJ_wj27.js"></script>
|
|
10
10
|
<link rel="stylesheet" crossorigin href="./assets/index-C3gNPz58.css">
|
|
11
11
|
</head>
|
|
12
12
|
<body>
|
package/dist/eval-feed.d.ts
CHANGED
|
@@ -22,5 +22,5 @@ export type CaseFeeds = {
|
|
|
22
22
|
close(): Promise<void>;
|
|
23
23
|
};
|
|
24
24
|
/** The fake feeds of one run of a case on `fixture`, for a trigger that reads `urls` (its first the fixture's) and, for a
|
|
25
|
-
* JSON list, its items at `under
|
|
26
|
-
export declare function caseFeeds(fixture: FeedFixture, urls: readonly string[], under?: string): Promise<CaseFeeds>;
|
|
25
|
+
* JSON list, its items at `under` and each item's id at `idAt` (the trigger's own id pointer: /id when it names none). */
|
|
26
|
+
export declare function caseFeeds(fixture: FeedFixture, urls: readonly string[], under?: string, idAt?: string): Promise<CaseFeeds>;
|
package/dist/eval-feed.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// words keep the package's URLs. The trigger's first poll, at the apply, takes its start on the fixture's items; the
|
|
6
6
|
// fixture's `item` is then added (or, when one there has its id, put in its place: an incident updated) and the trigger's
|
|
7
7
|
// next poll fires on it.
|
|
8
|
+
import { pointed } from "./feed/pointer.js";
|
|
8
9
|
import { FEED_FORMATS, startFeedFaux } from "./feed/feed-faux.js";
|
|
9
10
|
import { isObject } from "./validate.js";
|
|
10
11
|
const FIXTURE_FIELDS = ["description", "format", "title", "items", "item", "trigger"];
|
|
@@ -37,8 +38,8 @@ export function feedFixtureOf(value, at, bad) {
|
|
|
37
38
|
};
|
|
38
39
|
}
|
|
39
40
|
/** The fake feeds of one run of a case on `fixture`, for a trigger that reads `urls` (its first the fixture's) and, for a
|
|
40
|
-
* JSON list, its items at `under
|
|
41
|
-
export async function caseFeeds(fixture, urls, under) {
|
|
41
|
+
* JSON list, its items at `under` and each item's id at `idAt` (the trigger's own id pointer: /id when it names none). */
|
|
42
|
+
export async function caseFeeds(fixture, urls, under, idAt = "/id") {
|
|
42
43
|
const fakes = new Map();
|
|
43
44
|
try {
|
|
44
45
|
for (const [i, url] of urls.entries()) {
|
|
@@ -54,9 +55,12 @@ export async function caseFeeds(fixture, urls, under) {
|
|
|
54
55
|
return {
|
|
55
56
|
route: (url) => fakes.get(url)?.url,
|
|
56
57
|
deliver() {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
// The item's id as the trigger reads it: at its id pointer in a JSON list, its `id` in any other format.
|
|
59
|
+
const idOf = (one) => (fixture.format === "json-list" ? pointed(one, idAt) : one.id);
|
|
60
|
+
const id = idOf(fixture.item);
|
|
61
|
+
const has = (one) => id !== undefined && id !== null && typeof id !== "object" && idOf(one) === id;
|
|
62
|
+
if (first.items().some(has))
|
|
63
|
+
first.set(first.items().map((one) => (has(one) ? fixture.item : one)));
|
|
60
64
|
else
|
|
61
65
|
first.add(fixture.item);
|
|
62
66
|
},
|
package/dist/feed/parse.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { pointed } from "./pointer.js";
|
|
2
|
+
export { pointed };
|
|
1
3
|
/** The most characters of an item's summary its payload holds. */
|
|
2
4
|
export declare const SUMMARY_CHARS = 4000;
|
|
3
5
|
/** One item as a feed trigger reads it. `key`: what it is known by (its id, else its link, else a hash of its title and
|
|
@@ -26,6 +28,7 @@ export type ParsedFeed = {
|
|
|
26
28
|
export type ListPointers = {
|
|
27
29
|
items?: string;
|
|
28
30
|
id?: string;
|
|
31
|
+
updated?: string;
|
|
29
32
|
};
|
|
30
33
|
/** A body that is no feed this reads: its words. */
|
|
31
34
|
export declare class FeedUnreadable extends Error {
|
|
@@ -37,5 +40,3 @@ export declare function htmlToText(html: string): string;
|
|
|
37
40
|
/** `body` read as a feed. A JSON document is a JSON Feed (its `version` says so) unless `pointers` name a list, else a
|
|
38
41
|
* JSON list; anything else is read as XML: RSS or Atom. Throws FeedUnreadable in words. */
|
|
39
42
|
export declare function parseFeed(body: string, pointers?: ListPointers): ParsedFeed;
|
|
40
|
-
/** The value at JSON Pointer `pointer` in `doc`, or undefined. */
|
|
41
|
-
export declare function pointed(doc: unknown, pointer: string): unknown;
|
package/dist/feed/parse.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// tags dropped, its entities decoded, a block a line).
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
7
7
|
import { XMLParser, XMLValidator } from "fast-xml-parser";
|
|
8
|
+
import { pointed } from "./pointer.js";
|
|
9
|
+
export { pointed };
|
|
8
10
|
/** The most characters of an item's summary its payload holds. */
|
|
9
11
|
export const SUMMARY_CHARS = 4000;
|
|
10
12
|
/** The most characters of a title. */
|
|
@@ -266,23 +268,12 @@ function jsonFeedOf(doc) {
|
|
|
266
268
|
}),
|
|
267
269
|
};
|
|
268
270
|
}
|
|
269
|
-
/** The
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const part = raw.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
276
|
-
if (Array.isArray(at) && /^(0|[1-9]\d*)$/.test(part))
|
|
277
|
-
at = at[Number(part)];
|
|
278
|
-
else if (at && typeof at === "object" && !Array.isArray(at) && Object.hasOwn(at, part))
|
|
279
|
-
at = at[part];
|
|
280
|
-
else
|
|
281
|
-
return undefined;
|
|
282
|
-
}
|
|
283
|
-
return at;
|
|
284
|
-
}
|
|
285
|
-
function jsonListOf(doc, { items: itemsAt = "", id: idAt = "/id" }) {
|
|
271
|
+
/** The instant a JSON list's `updated` pointer reads, in ms: a date string it names; anything else, none. */
|
|
272
|
+
const instantOf = (value) => {
|
|
273
|
+
const at = typeof value === "string" ? Date.parse(value) : NaN;
|
|
274
|
+
return Number.isNaN(at) ? null : at;
|
|
275
|
+
};
|
|
276
|
+
function jsonListOf(doc, { items: itemsAt = "", id: idAt = "/id", updated: updatedAt }) {
|
|
286
277
|
const items = pointed(doc, itemsAt);
|
|
287
278
|
if (!Array.isArray(items))
|
|
288
279
|
throw new FeedUnreadable(itemsAt ? `nothing at ${itemsAt} in its JSON is a list` : "its JSON is no list, and the trigger names no items pointer to one");
|
|
@@ -294,7 +285,7 @@ function jsonListOf(doc, { items: itemsAt = "", id: idAt = "/id" }) {
|
|
|
294
285
|
const id = str(pointed(object, idAt));
|
|
295
286
|
const link = str(object.html_url) ?? str(object.url) ?? str(object.link);
|
|
296
287
|
const title = str(object.title, TITLE_CHARS) ?? str(object.name, TITLE_CHARS);
|
|
297
|
-
return { key: id ?? link ?? `hash:${hash(title ?? JSON.stringify(one))}`, id, link, title, published: null, updated: null, author: null, summary: null, categories: [], raw: one };
|
|
288
|
+
return { key: id ?? link ?? `hash:${hash(title ?? JSON.stringify(one))}`, id, link, title, published: null, updated: updatedAt === undefined ? null : instantOf(pointed(object, updatedAt)), author: null, summary: null, categories: [], raw: one };
|
|
298
289
|
}),
|
|
299
290
|
};
|
|
300
291
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// A JSON Pointer read (RFC 6901), kept apart from the feed parser so the trigger reads it without loading the parser.
|
|
2
|
+
/** The value at JSON Pointer `pointer` in `doc`, or undefined. */
|
|
3
|
+
export function pointed(doc, pointer) {
|
|
4
|
+
if (pointer === "")
|
|
5
|
+
return doc;
|
|
6
|
+
let at = doc;
|
|
7
|
+
for (const raw of pointer.split("/").slice(1)) {
|
|
8
|
+
const part = raw.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
9
|
+
if (Array.isArray(at) && /^(0|[1-9]\d*)$/.test(part))
|
|
10
|
+
at = at[Number(part)];
|
|
11
|
+
else if (at && typeof at === "object" && !Array.isArray(at) && Object.hasOwn(at, part))
|
|
12
|
+
at = at[part];
|
|
13
|
+
else
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
return at;
|
|
17
|
+
}
|
package/dist/file-types.js
CHANGED
|
@@ -4,6 +4,7 @@ import { extname } from "node:path";
|
|
|
4
4
|
const TYPES = new Map(Object.entries({
|
|
5
5
|
txt: "text/plain; charset=utf-8", md: "text/markdown; charset=utf-8", csv: "text/csv; charset=utf-8", json: "application/json", jsonl: "application/x-ndjson",
|
|
6
6
|
html: "text/html; charset=utf-8", htm: "text/html; charset=utf-8", png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", pdf: "application/pdf",
|
|
7
|
+
mp4: "video/mp4",
|
|
7
8
|
}));
|
|
8
9
|
/** The types a file is served as in the page (every known type but PDF); any other is a download. */
|
|
9
10
|
export const INLINE_TYPES = new Set([...TYPES.values()].filter((type) => type !== "application/pdf"));
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Whether a folder of `path` (a PATH value) holds an executable file named `command`; on Windows, with an executable
|
|
2
|
+
* extension too (platform-tools installs adb.exe). */
|
|
3
|
+
export declare function commandOnPath(command: string, path: string | undefined, platform?: string): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Whether a program this server runs on its own machine is there: a provider's (the phone's adb, computers.ts) and the
|
|
2
|
+
// screen recorder's ffmpeg (computer-record.ts).
|
|
3
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
4
|
+
import { delimiter, join } from "node:path";
|
|
5
|
+
/** Whether a folder of `path` (a PATH value) holds an executable file named `command`; on Windows, with an executable
|
|
6
|
+
* extension too (platform-tools installs adb.exe). */
|
|
7
|
+
export function commandOnPath(command, path, platform = process.platform) {
|
|
8
|
+
const named = platform === "win32" ? [command, ...[".exe", ".cmd", ".bat"].map((extension) => `${command}${extension}`)] : [command];
|
|
9
|
+
return (path ?? "").split(delimiter).some((folder) => folder !== "" && named.some((file) => {
|
|
10
|
+
try {
|
|
11
|
+
accessSync(join(folder, file), constants.X_OK);
|
|
12
|
+
return statSync(join(folder, file)).isFile();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
}
|
package/dist/host.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { statfs } from "node:fs/promises";
|
|
2
2
|
import type { Store } from "./store/index.js";
|
|
3
|
+
/** The port a server listens on unless --port says otherwise (cli.ts), and the one a standalone runner's loopback reads
|
|
4
|
+
* never go to (runner/main.ts runnerLoopback). */
|
|
5
|
+
export declare const DEFAULT_PORT = 7340;
|
|
3
6
|
/** Memory the server keeps for itself, before any run, in MiB. Measured with the runs' (harness-table.ts): the server's anon
|
|
4
7
|
* 3 s after its start, 32 starts at limits of 512M to 2048M, was 58 to 81 MB, with no peak at the start over that;
|
|
5
8
|
* reserved for the high end, 81, plus 47 (+58%), since what it holds grows with the agents and runs it keeps. That is
|
package/dist/host.js
CHANGED
|
@@ -7,6 +7,9 @@ import { totalmem } from "node:os";
|
|
|
7
7
|
import { REQUEST_BUSY_MS, whenFree } from "./agents.js";
|
|
8
8
|
import { DEFAULT_HARNESS, HARNESSES } from "./harness.js";
|
|
9
9
|
import { builtinHarness } from "./harness-table.js";
|
|
10
|
+
/** The port a server listens on unless --port says otherwise (cli.ts), and the one a standalone runner's loopback reads
|
|
11
|
+
* never go to (runner/main.ts runnerLoopback). */
|
|
12
|
+
export const DEFAULT_PORT = 7340;
|
|
10
13
|
const MiB = 2 ** 20;
|
|
11
14
|
/** Memory the server keeps for itself, before any run, in MiB. Measured with the runs' (harness-table.ts): the server's anon
|
|
12
15
|
* 3 s after its start, 32 starts at limits of 512M to 2048M, was 58 to 81 MB, with no peak at the start over that;
|
package/dist/learn/check.d.ts
CHANGED
|
@@ -109,6 +109,20 @@ export declare const printable: (problem: Problem, values: readonly string[]) =>
|
|
|
109
109
|
* its item and are never the answer; a loop's body writes the state around it. Legwork on fast and an answer on strong
|
|
110
110
|
* pass. Out of sample (H1), the same answering step on the fast model scored 8.5-9.6 points lower than on the agent's. */
|
|
111
111
|
export declare function answerTierProblems(doc: unknown): string[];
|
|
112
|
+
/** Why a draft's model step off the fast tier reads a page (compact state): an agent, decide or extract step with no
|
|
113
|
+
* `"tier": "fast"` whose `state` map names a key that may hold a page a call step fetched (a call's result, a map's
|
|
114
|
+
* collected one, a pick or sift over pages, a composed workflow's answer when it is one, the key or anything under it),
|
|
115
|
+
* or, with no `state` map, which reads the whole state while such a key is in it (pagesRead: the engine's own lookup).
|
|
116
|
+
* Read in run order: a key holds a page from the step that fetched it until a model step writes over it; a parallel's
|
|
117
|
+
* branches each start from the same state and their writes merge, so a key one branch replaced is no page after it; a
|
|
118
|
+
* route runs one branch, so a key stays a page unless every branch replaced it; a loop's body is read pass after pass,
|
|
119
|
+
* each on every page its passes may leave, until the marks stop changing (within its maxIters); a map's body sees
|
|
120
|
+
* `item` as a page when its list is one, and its failedAs rows are short errors, never pages; a composed workflow's
|
|
121
|
+
* child sees an input key as a page when its value reads one. Pages reach a step on the agent's model only through a fast-tier step's
|
|
122
|
+
* output: chain14b's drafts fed whole pages to the answering step (4.1 requests a run at ~70k cached tokens each,
|
|
123
|
+
* $0.364 a served run), where the research shape (a fast extract's compact facts to the answering step) booked ~$0.08.
|
|
124
|
+
* One line each, in words the compile retry acts on. */
|
|
125
|
+
export declare function compactStateProblems(doc: unknown): string[];
|
|
112
126
|
/** Why a draft's quote check is not code: its answer carries quotes (a `quote` or `quotes` property in its output schema)
|
|
113
127
|
* and it fetches pages (a call step), yet some place in the answer holding quotes has no escalate on quote_missing
|
|
114
128
|
* reading it after the answer is written (quoteRule), or that escalate reads as a page a key holding no page as fetched
|
package/dist/learn/check.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// which is the grader's. The host reads the copy itself (regular files only, never a link followed) and hands each change
|
|
4
4
|
// here with its bytes; nothing the checker refuses is ever written to git. Every problem it reports is printable: its
|
|
5
5
|
// path and detail are cut of the host's secrets and of text shaped like one, and a field over PRINT_MAX is redacted whole.
|
|
6
|
+
import { placeholdersOf, placeholderValue } from "@parcha/agentrun-dsl";
|
|
6
7
|
import { hiddenCharacter } from "./invisible.js";
|
|
7
8
|
import { compiledLineBytes, INDEX_FILE, INDEX_LINES, NOTE_FILE, parseNoteFile, ruleOf } from "./notes.js";
|
|
8
9
|
import { learnedSecretShapes, redactLearned, valueMatcher } from "./secrets.js";
|
|
@@ -183,6 +184,165 @@ export function answerTierProblems(doc) {
|
|
|
183
184
|
answered(doc);
|
|
184
185
|
return problems;
|
|
185
186
|
}
|
|
187
|
+
/** The key each page mark stands for (pageStateOf). */
|
|
188
|
+
const pageKeyOf = new WeakMap();
|
|
189
|
+
/** A state holding a page at each of `pages` and nothing else, as a static read sees it: a page has every path (its
|
|
190
|
+
* fields are the run's), so any path under one reads the page itself. */
|
|
191
|
+
function pageStateOf(pages) {
|
|
192
|
+
const state = Object.create(null);
|
|
193
|
+
for (const key of pages) {
|
|
194
|
+
const mark = new Proxy(Object.create(null), { get: (_target, prop) => (typeof prop === "string" ? mark : undefined) });
|
|
195
|
+
pageKeyOf.set(mark, key);
|
|
196
|
+
state[key] = mark;
|
|
197
|
+
}
|
|
198
|
+
return state;
|
|
199
|
+
}
|
|
200
|
+
/** The mark key (pageStateOf) the placeholder `path` reads in `state`, if it reads one. */
|
|
201
|
+
const markRead = (state, path) => {
|
|
202
|
+
const read = placeholderValue(state, path);
|
|
203
|
+
return read !== null && typeof read === "object" ? pageKeyOf.get(read) : undefined;
|
|
204
|
+
};
|
|
205
|
+
/** The page keys a value reads: each placeholder in its strings, at any depth, as interpolation fills them (the DSL's
|
|
206
|
+
* placeholdersOf: `{path?}` only as a whole string, literal text inside a longer one), resolved by the engine's own
|
|
207
|
+
* lookup (the DSL's placeholderValue: the path as written, so a real `state` key first, then the path after `state.`)
|
|
208
|
+
* over pageStateOf. A key holding compact facts is absent there, so a `{state.x}` read falls back to `x` as the engine
|
|
209
|
+
* may when its `state` key lacks `x`. */
|
|
210
|
+
function pagesRead(value, state, out = new Set(), depth = 0) {
|
|
211
|
+
if (depth > 32)
|
|
212
|
+
return out;
|
|
213
|
+
if (typeof value === "string") {
|
|
214
|
+
for (const { path } of placeholdersOf(value)) {
|
|
215
|
+
const key = markRead(state, path);
|
|
216
|
+
if (key !== undefined)
|
|
217
|
+
out.add(key);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else if (Array.isArray(value))
|
|
221
|
+
value.forEach((one) => pagesRead(one, state, out, depth + 1));
|
|
222
|
+
else if (isObject(value))
|
|
223
|
+
Object.values(value).forEach((one) => pagesRead(one, state, out, depth + 1));
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
/** Why a draft's model step off the fast tier reads a page (compact state): an agent, decide or extract step with no
|
|
227
|
+
* `"tier": "fast"` whose `state` map names a key that may hold a page a call step fetched (a call's result, a map's
|
|
228
|
+
* collected one, a pick or sift over pages, a composed workflow's answer when it is one, the key or anything under it),
|
|
229
|
+
* or, with no `state` map, which reads the whole state while such a key is in it (pagesRead: the engine's own lookup).
|
|
230
|
+
* Read in run order: a key holds a page from the step that fetched it until a model step writes over it; a parallel's
|
|
231
|
+
* branches each start from the same state and their writes merge, so a key one branch replaced is no page after it; a
|
|
232
|
+
* route runs one branch, so a key stays a page unless every branch replaced it; a loop's body is read pass after pass,
|
|
233
|
+
* each on every page its passes may leave, until the marks stop changing (within its maxIters); a map's body sees
|
|
234
|
+
* `item` as a page when its list is one, and its failedAs rows are short errors, never pages; a composed workflow's
|
|
235
|
+
* child sees an input key as a page when its value reads one. Pages reach a step on the agent's model only through a fast-tier step's
|
|
236
|
+
* output: chain14b's drafts fed whole pages to the answering step (4.1 requests a run at ~70k cached tokens each,
|
|
237
|
+
* $0.364 a served run), where the research shape (a fast extract's compact facts to the answering step) booked ~$0.08.
|
|
238
|
+
* One line each, in words the compile retry acts on. */
|
|
239
|
+
export function compactStateProblems(doc) {
|
|
240
|
+
const problems = new Set();
|
|
241
|
+
const walk = (node, pages, depth) => {
|
|
242
|
+
const after = new Set(pages);
|
|
243
|
+
if (!isNode(node) || depth > 32)
|
|
244
|
+
return after;
|
|
245
|
+
const key = writeKeyOf(node);
|
|
246
|
+
const wrote = (page) => {
|
|
247
|
+
if (typeof key !== "string")
|
|
248
|
+
return;
|
|
249
|
+
if (page)
|
|
250
|
+
after.add(key);
|
|
251
|
+
else
|
|
252
|
+
after.delete(key);
|
|
253
|
+
};
|
|
254
|
+
const kind = node.node;
|
|
255
|
+
if (kind === "chain") {
|
|
256
|
+
let state = after;
|
|
257
|
+
for (const step of Array.isArray(node.steps) ? node.steps : [])
|
|
258
|
+
state = walk(step, state, depth + 1);
|
|
259
|
+
return state;
|
|
260
|
+
}
|
|
261
|
+
if (kind === "parallel") {
|
|
262
|
+
// Every branch starts from the same state and the engine merges their writes (disjoint, or the run fails): a key
|
|
263
|
+
// stays a page when no branch replaced it, and a page a branch wrote is one.
|
|
264
|
+
const outs = (Array.isArray(node.branches) ? node.branches : []).map((body) => walk(body, after, depth + 1));
|
|
265
|
+
const merged = new Set([...after].filter((one) => outs.every((out) => out.has(one))));
|
|
266
|
+
for (const out of outs)
|
|
267
|
+
for (const one of out)
|
|
268
|
+
if (!after.has(one))
|
|
269
|
+
merged.add(one);
|
|
270
|
+
return merged;
|
|
271
|
+
}
|
|
272
|
+
if (kind === "route") {
|
|
273
|
+
// One branch runs, after the choice is recorded: a page on any branch's way out is one.
|
|
274
|
+
wrote(false);
|
|
275
|
+
const bodies = childNodes(node);
|
|
276
|
+
if (!bodies.length)
|
|
277
|
+
return after;
|
|
278
|
+
const merged = new Set();
|
|
279
|
+
for (const body of bodies)
|
|
280
|
+
for (const one of walk(body, after, depth + 1))
|
|
281
|
+
merged.add(one);
|
|
282
|
+
return merged;
|
|
283
|
+
}
|
|
284
|
+
if (kind === "loop") {
|
|
285
|
+
// Any pass may be the last (do-while, up to maxIters): walk the body again on every page its passes may leave
|
|
286
|
+
// until the marks stop changing. The set read grows on each further pass, so it stops within one pass a key the
|
|
287
|
+
// workflow names, and never past maxIters; each pass's way out holds the ones before it.
|
|
288
|
+
const passes = typeof node.maxIters === "number" && node.maxIters >= 1 ? Math.floor(node.maxIters) : Number.POSITIVE_INFINITY;
|
|
289
|
+
let read = after;
|
|
290
|
+
let out = walk(node.body, read, depth + 1);
|
|
291
|
+
for (let pass = 2; pass <= passes; pass++) {
|
|
292
|
+
const next = new Set([...read, ...out]);
|
|
293
|
+
if (next.size === read.size)
|
|
294
|
+
break;
|
|
295
|
+
read = next;
|
|
296
|
+
out = walk(node.body, read, depth + 1);
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
if (kind === "map") {
|
|
301
|
+
const itemsPage = typeof node.itemsPath === "string" && after.has(node.itemsPath.split(".")[0]);
|
|
302
|
+
const inner = new Set(after);
|
|
303
|
+
if (itemsPage)
|
|
304
|
+
inner.add("item");
|
|
305
|
+
else
|
|
306
|
+
inner.delete("item");
|
|
307
|
+
const out = walk(node.body, inner, depth + 1);
|
|
308
|
+
const result = typeof node.resultPath === "string" && node.resultPath ? node.resultPath.split(".")[0] : isNode(node.body) && typeof node.body.as === "string" && node.body.as ? node.body.as : undefined;
|
|
309
|
+
// Each item's result, or its whole state (the state around the map too) when nothing picks one key.
|
|
310
|
+
wrote(result !== undefined ? out.has(result) : out.size > 0);
|
|
311
|
+
// Its failed items' rows are short error records, never the items.
|
|
312
|
+
if (typeof node.failedAs === "string" && node.failedAs)
|
|
313
|
+
after.delete(node.failedAs);
|
|
314
|
+
return after;
|
|
315
|
+
}
|
|
316
|
+
if (kind === "workflow") {
|
|
317
|
+
const child = isObject(node.workflow) ? node.workflow : {};
|
|
318
|
+
const input = isObject(node.input) ? node.input : {};
|
|
319
|
+
const state = pageStateOf(after);
|
|
320
|
+
const seen = new Set(Object.entries(input).filter(([, value]) => pagesRead(value, state).size > 0).map(([name]) => name));
|
|
321
|
+
const out = walk(child.root, seen, depth + 1);
|
|
322
|
+
const path = isObject(child.output) && typeof child.output.path === "string" ? child.output.path.split(".")[0] : "";
|
|
323
|
+
wrote(!!path && out.has(path));
|
|
324
|
+
return after;
|
|
325
|
+
}
|
|
326
|
+
if (kind === "call")
|
|
327
|
+
return wrote(true), after;
|
|
328
|
+
if (kind === "pick" || kind === "sift")
|
|
329
|
+
return wrote(typeof node.itemsPath === "string" && after.has(node.itemsPath.split(".")[0])), after;
|
|
330
|
+
if (TIERED_KINDS.has(kind) && node.tier !== "fast") {
|
|
331
|
+
const label = JSON.stringify(String(node.label ?? "(no label)").slice(0, 64));
|
|
332
|
+
const read = node.state === undefined ? [...after] : [...pagesRead(node.state, pageStateOf(after))];
|
|
333
|
+
if (read.length) {
|
|
334
|
+
const what = node.state === undefined
|
|
335
|
+
? `the whole run state (it has no "state" map), the page${read.length > 1 ? "s" : ""} ${read.map((one) => JSON.stringify(one.slice(0, 64))).join(", ")} included`
|
|
336
|
+
: `${read.map((one) => JSON.stringify(`{${one.slice(0, 64)}}`)).join(", ")}, ${read.length > 1 ? "pages" : "a page"} a call step fetched`;
|
|
337
|
+
problems.add(`the ${kind} step ${label} runs off the fast tier and reads ${what}: a page reaches a step on the agent's model only through a fast-tier step's output. Give it a "state" map naming only compact keys (the facts a fast-tier extract step wrote from the pages; in a re-pick loop its own draft too, as "{<its as>?}"), or, for legwork, "tier": "fast"`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
wrote(false);
|
|
341
|
+
return after;
|
|
342
|
+
};
|
|
343
|
+
walk(doc?.root, new Set(), 0);
|
|
344
|
+
return [...problems];
|
|
345
|
+
}
|
|
186
346
|
/** The property names that make an answer carry quotes. */
|
|
187
347
|
const QUOTE_KEYS = new Set(["quote", "quotes"]);
|
|
188
348
|
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -397,17 +557,34 @@ const repicks = (step, check) => {
|
|
|
397
557
|
const of = step.until.of;
|
|
398
558
|
return of.predicate === "quote_missing" && of.path === check.path && (of.field ?? "") === check.field && JSON.stringify(of.pages) === JSON.stringify(check.pages);
|
|
399
559
|
};
|
|
400
|
-
/**
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
|
|
560
|
+
/** Whether a step's `state` map names state key `key` as the whole marked placeholder `"{answer?}"` (a whole string):
|
|
561
|
+
* null on a re-pick loop's first pass, before any draft exists, and the failed draft on its second. No path below the
|
|
562
|
+
* key counts (`"{answer.x?}"`, `"{state.answer?}"`, unmarked `"{answer}"`): paths are not validated, so only the key
|
|
563
|
+
* itself is sure to hold the draft. */
|
|
564
|
+
function readsDraft(state, key, depth = 0) {
|
|
565
|
+
if (depth > 32)
|
|
566
|
+
return false;
|
|
567
|
+
if (typeof state === "string")
|
|
568
|
+
return placeholdersOf(state).some(({ path, optional }) => optional && path === key);
|
|
569
|
+
if (Array.isArray(state))
|
|
570
|
+
return state.some((one) => readsDraft(one, key, depth + 1));
|
|
571
|
+
return isObject(state) && Object.values(state).some((one) => readsDraft(one, key, depth + 1));
|
|
572
|
+
}
|
|
573
|
+
/** The labels of the model steps inside `node` (a loop's body) that write state key `key` without reading their own
|
|
574
|
+
* draft through their `state` map (readsDraft). The DSL sends a step with a map only the keys it names (each pass a
|
|
575
|
+
* fresh model session), so one not naming the draft never sees it on the re-pick; naming it unmarked (`"{answer}"`)
|
|
576
|
+
* fails the first pass, which has none yet; and a step with no map reads the whole state, the pages included, which no
|
|
577
|
+
* step off the fast tier may (compactStateProblems). A map's or a composed workflow's own steps write their item's or
|
|
578
|
+
* child's state, not this one. */
|
|
579
|
+
const blindRepicks = (node, key, depth = 0) => {
|
|
405
580
|
if (!isNode(node) || depth > 32)
|
|
406
581
|
return [];
|
|
407
|
-
const own = writeKeyOf(node) === key && node.state
|
|
408
|
-
const inner = node.node === "map" || node.node === "workflow" ? [] : childNodes(node).flatMap((child) =>
|
|
582
|
+
const own = writeKeyOf(node) === key && TIERED_KINDS.has(node.node) && !readsDraft(node.state, key) ? [String(node.label ?? node.node).slice(0, 64)] : [];
|
|
583
|
+
const inner = node.node === "map" || node.node === "workflow" ? [] : childNodes(node).flatMap((child) => blindRepicks(child, key, depth + 1));
|
|
409
584
|
return [...own, ...inner];
|
|
410
585
|
};
|
|
586
|
+
/** How a re-pick step reads its failed draft, as a refusal says it. */
|
|
587
|
+
const draftWords = (key) => `a "state" map naming the compact facts it answers from and its failed draft as an optional placeholder, "draft": "{${key}?}" (null on the first pass, before any draft exists; the failed draft on the second)`;
|
|
411
588
|
/** The labels of the quote_missing escalates inside `node` (a loop's body), a composed workflow's included: each hands
|
|
412
589
|
* back on the first pass's miss, so a re-pick loop around one never re-picks. */
|
|
413
590
|
const quoteEscalatesIn = (node, depth = 0) => {
|
|
@@ -496,12 +673,12 @@ function quoteRule(workflow, schemasAround, depth = 0) {
|
|
|
496
673
|
// A check inside the loop's body hands back on the first draft's miss: the loop never re-picks.
|
|
497
674
|
problems.push(`the re-pick loop ${JSON.stringify(String(writer.label ?? "(no label)").slice(0, 64))} holds the quote_missing escalate ${JSON.stringify(quoteEscalatesIn(writer.body)[0])} in its body, which hands back on the first draft's miss, so the loop never re-picks: the check after the loop, never inside it, as a step of the top-level chain`);
|
|
498
675
|
}
|
|
499
|
-
else if (!problems.length && first && isNode(writer) && writer.node === "loop" && repicks(writer, first) &&
|
|
500
|
-
// The re-pick must see the failed draft
|
|
501
|
-
problems.push(`the step ${JSON.stringify(
|
|
676
|
+
else if (!problems.length && first && isNode(writer) && writer.node === "loop" && repicks(writer, first) && blindRepicks(writer.body, answerPath.split(".")[0]).length) {
|
|
677
|
+
// The re-pick must see the failed draft, and only through a state map that names it optionally (never the pages).
|
|
678
|
+
problems.push(`the step ${JSON.stringify(blindRepicks(writer.body, answerPath.split(".")[0])[0])} in the re-pick loop ${JSON.stringify(String(writer.label ?? "(no label)").slice(0, 64))} writes the answer without reading its failed draft, so its second pass writes the same miss again: give it ${draftWords(answerPath.split(".")[0])}. Naming it unmarked ("{${answerPath.split(".")[0]}}") fails the first pass, and no "state" map reads the whole state, the pages included`);
|
|
502
679
|
}
|
|
503
680
|
else if (!problems.length && first && !checks.every((check) => repicks(writer, check))) {
|
|
504
|
-
problems.push(`the step ${JSON.stringify(String(isNode(writer) ? writer.label ?? writer.node : "(no label)").slice(0, 64))} writes the answer and the escalate ${JSON.stringify(first.label)} hands it back on its first quote miss: give it one re-pick first, the step in a loop {"node": "loop", "label": "repick", "maxIters": ${REPICK_PASSES}, "until": {"predicate": "not", "of": ${quoteMissingText(first)}}, "body": <the step>} in its place, its instructions saying that a draft
|
|
681
|
+
problems.push(`the step ${JSON.stringify(String(isNode(writer) ? writer.label ?? writer.node : "(no label)").slice(0, 64))} writes the answer and the escalate ${JSON.stringify(first.label)} hands it back on its first quote miss: give it one re-pick first, the step in a loop {"node": "loop", "label": "repick", "maxIters": ${REPICK_PASSES}, "until": {"predicate": "not", "of": ${quoteMissingText(first)}}, "body": <the step>} in its place, the step with ${draftWords(answerPath.split(".")[0])} and its instructions saying that a draft there failed the quote check, so a draft quoting text no page holds is written once more and only a second miss hands back: the check after the loop, never inside it`);
|
|
505
682
|
}
|
|
506
683
|
}
|
|
507
684
|
// A composed workflow writing the answer that applies the rule and fails it: its own check is the one to fix.
|
|
@@ -513,7 +690,8 @@ function quoteRule(workflow, schemasAround, depth = 0) {
|
|
|
513
690
|
* regular file within CAPS.file (a README within CAPS.readme), its text as textOf takes it, and in shape: a skill's
|
|
514
691
|
* front matter (skillProblems), with no `status`, which is the host's; a draft the DSL validates against `tools` with
|
|
515
692
|
* no node that runs code (draftProblems), whose answering step runs on the agent's model (answerTierProblems), whose
|
|
516
|
-
* quotes of the pages it fetched are checked in code
|
|
693
|
+
* steps there read no page (compactStateProblems), whose quotes of the pages it fetched are checked in code
|
|
694
|
+
* (quoteCheckProblems); a README that names its folder and says what
|
|
517
695
|
* the draft is for. */
|
|
518
696
|
function learnedChecked(change, place, refused, input, add) {
|
|
519
697
|
const { path } = change;
|
|
@@ -541,10 +719,12 @@ function learnedChecked(change, place, refused, input, add) {
|
|
|
541
719
|
if (!input.dsl)
|
|
542
720
|
throw new TypeError(`${path}: a workflow draft is read with the workflow DSL, which was not given`);
|
|
543
721
|
const problems = draftProblems(input.dsl, text, input.tools, input.gated, input.deniedBy, input.decisions, input.workflowAdapters);
|
|
544
|
-
// A draft the DSL reads parses: its answering step is held to the agent's model,
|
|
722
|
+
// A draft the DSL reads parses: its answering step is held to the agent's model, its steps there to compact state
|
|
723
|
+
// (the SDK bridge's runNode picks every model itself: it names no tier), and its quote check to code.
|
|
545
724
|
if (!problems.length) {
|
|
546
725
|
const doc = JSON.parse(text);
|
|
547
|
-
|
|
726
|
+
const compact = input.workflowAdapters === undefined ? compactStateProblems(doc) : [];
|
|
727
|
+
problems.push(...[...answerTierProblems(doc), ...compact, ...quoteCheckProblems(doc)].map((detail) => ({ rule: "workflow", detail })));
|
|
548
728
|
}
|
|
549
729
|
for (const { rule, detail } of problems)
|
|
550
730
|
add(rule, detail, path);
|