@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
package/dist/learn/skills.js
CHANGED
|
@@ -22,14 +22,49 @@ export function learnedPlace(path) {
|
|
|
22
22
|
}
|
|
23
23
|
/** Whether `name` is a good name for a folder of `kind`. */
|
|
24
24
|
export const learnedName = (kind, name) => (kind === "skill" ? SKILL_NAME : WORKFLOW_NAME).test(name);
|
|
25
|
-
/**
|
|
26
|
-
* `
|
|
27
|
-
|
|
25
|
+
/** Where a SKILL.md's or a draft README's front matter closes in `lf` (its text with LF line ends): the index of the
|
|
26
|
+
* `\n` before its closing `---` line, the last line or not; -1 when it has none. */
|
|
27
|
+
const closeOf = (lf) => {
|
|
28
|
+
const close = /\n---(?:\n|$)/.exec(lf.slice(3));
|
|
29
|
+
return close ? 3 + close.index : -1;
|
|
30
|
+
};
|
|
31
|
+
/** YAML's double-quoted escapes, each to what it reads as (`\\x`, `\\u` and `\\U` take hex digits apart). */
|
|
32
|
+
const ESCAPES = { "0": "\0", a: "\x07", b: "\b", t: "\t", "\t": "\t", n: "\n", v: "\v", f: "\f", r: "\r", e: "\x1b", " ": " ", '"': '"', "/": "/", "\\": "\\", N: "\u0085", _: "\u00a0", L: "\u2028", P: "\u2029" };
|
|
33
|
+
/** A scalar's value as YAML reads it: a double-quoted one with its escapes decoded (JSON's, which servedSkill writes,
|
|
34
|
+
* and YAML's own), a single-quoted one with each `''` one `'`, a plain one as it is. So reading what servedSkill wrote
|
|
35
|
+
* gives back the very value it quoted, and serving a skill again writes the same text (OpenCode serves a laid skill
|
|
36
|
+
* again): never an escape quoted twice. Undefined, never a throw, for a double-quoted value with an escape that does
|
|
37
|
+
* not decode cleanly (one YAML has not, a code point past U+10FFFF, a `\\` that ends it): what another reader makes of
|
|
38
|
+
* it is not known, so its front matter reads no way. The decoded value is the one every check reads (frontValues). */
|
|
39
|
+
function unquote(raw) {
|
|
40
|
+
const value = raw.trim();
|
|
41
|
+
if (value.length >= 2 && value.startsWith("'") && value.endsWith("'"))
|
|
42
|
+
return value.slice(1, -1).replace(/''/g, "'");
|
|
43
|
+
if (!(value.length >= 2 && value.startsWith('"') && value.endsWith('"')))
|
|
44
|
+
return value;
|
|
45
|
+
let clean = true;
|
|
46
|
+
const decoded = value.slice(1, -1).replace(/\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|[\s\S]?)/g, (whole, code) => {
|
|
47
|
+
if (code.length > 1) {
|
|
48
|
+
const point = Number.parseInt(code.slice(1), 16);
|
|
49
|
+
if (point > 0x10ffff)
|
|
50
|
+
return (clean = false), whole;
|
|
51
|
+
return String.fromCodePoint(point);
|
|
52
|
+
}
|
|
53
|
+
const one = ESCAPES[code];
|
|
54
|
+
if (one === undefined)
|
|
55
|
+
clean = false;
|
|
56
|
+
return one ?? whole;
|
|
57
|
+
});
|
|
58
|
+
return clean ? decoded : undefined;
|
|
59
|
+
}
|
|
60
|
+
/** A SKILL.md's or a draft README's front matter, as the host edits it: its lines, where it closes (closeOf, in `lf`,
|
|
61
|
+
* the text with LF line ends) and each key's value, a folded or literal block value read as one line. A string is why
|
|
62
|
+
* there is none. What it says of itself (its keys, its status) is frontMatter's to read, never this. */
|
|
28
63
|
function frontOf(text) {
|
|
29
64
|
const lf = text.replace(/\r\n/g, "\n");
|
|
30
65
|
if (!lf.startsWith("---\n"))
|
|
31
66
|
return "it does not start with front matter";
|
|
32
|
-
const end = lf
|
|
67
|
+
const end = closeOf(lf);
|
|
33
68
|
if (end < 0)
|
|
34
69
|
return "its front matter is not closed";
|
|
35
70
|
const lines = lf.slice(4, end).split("\n");
|
|
@@ -54,68 +89,213 @@ function frontOf(text) {
|
|
|
54
89
|
return words.join(" ").trim();
|
|
55
90
|
if (words.length && !/^["']/.test(own))
|
|
56
91
|
return [own, ...words].join(" ").trim();
|
|
57
|
-
return own
|
|
92
|
+
return unquote(own)?.trim();
|
|
58
93
|
};
|
|
59
94
|
return { lf, end, value };
|
|
60
95
|
}
|
|
61
|
-
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
96
|
+
/** Why a front matter does not read one way, by frontMatter: the words a refusal says. */
|
|
97
|
+
export const UNCLEAR_FRONT = "its front matter does not read one way: each line a `key: value` at its left edge, a comment, or a line of the value above it";
|
|
98
|
+
/** A SKILL.md's or a draft README's front matter, read one way, as the one reader of what it says of itself (its keys,
|
|
99
|
+
* its name, its description, its status): each key to its value in order, or undefined when it does not read one way,
|
|
100
|
+
* since another reader (a harness's YAML) could then read a key, a status among them, this one does not. It reads one
|
|
101
|
+
* way when it starts the text (`---`), closes on a `---` line (the last line or not), and each line between is blank,
|
|
102
|
+
* a comment at its left edge, `key: value` at its left edge (a key set once), or a line of the value above it: any
|
|
103
|
+
* indented line under a `key:` with nothing after it (its nested value, a mapping or a list among them), an indented
|
|
104
|
+
* line of a `>` or `|` block, or of a plain value that wraps (one that reads as no key of its own), an indented comment
|
|
105
|
+
* under a plain value being no part of it. A quoted value is read as YAML reads it (unquote: its escapes decoded); a
|
|
106
|
+
* nested, block or wrapped value's lines are joined by one space. Anything else (an indented first line, an indented
|
|
107
|
+
* `key:` under a value, a list at the left edge, a key set twice, a YAML explicit key `? key`, which every writer here
|
|
108
|
+
* would have to edit in its own form) reads no way. */
|
|
109
|
+
export function frontMatter(text) {
|
|
110
|
+
const lf = text.replace(/\r\n/g, "\n");
|
|
111
|
+
if (!lf.startsWith("---\n"))
|
|
112
|
+
return undefined;
|
|
113
|
+
const end = closeOf(lf);
|
|
114
|
+
if (end < 0)
|
|
66
115
|
return undefined;
|
|
67
|
-
|
|
116
|
+
const front = new Map();
|
|
117
|
+
const add = (key, more) => front.set(key, `${front.get(key)} ${more.trim()}`.trim());
|
|
118
|
+
// The key whose `>`/`|` block, or nested value (a `key:` with nothing after it), the next indented lines are; the key
|
|
119
|
+
// whose plain value they wrap; a quoted value still open, its lines so far (quotedValue folds them).
|
|
120
|
+
let block;
|
|
121
|
+
let plain;
|
|
122
|
+
let quoting;
|
|
123
|
+
for (const line of lf.slice(4, end).split("\n")) {
|
|
124
|
+
const indented = /^\s+\S/.test(line);
|
|
125
|
+
if (quoting) {
|
|
126
|
+
// A quoted value's next line: indented, or blank (a line break of its own); one at the left edge reads no way.
|
|
127
|
+
if (line.trim() !== "" && !indented)
|
|
128
|
+
return undefined;
|
|
129
|
+
quoting.lines.push(line);
|
|
130
|
+
if (!closedQuote(quoting.lines.map((one) => one.trim()).join(" ")))
|
|
131
|
+
continue;
|
|
132
|
+
const read = unquote(foldQuoted(quoting.lines));
|
|
133
|
+
if (read === undefined)
|
|
134
|
+
return undefined;
|
|
135
|
+
front.set(quoting.key, read);
|
|
136
|
+
quoting = undefined;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (block !== undefined && (indented || line.trim() === "")) {
|
|
140
|
+
if (line.trim())
|
|
141
|
+
add(block, line);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
block = undefined;
|
|
145
|
+
if (line.trim() === "" || line.startsWith("#"))
|
|
146
|
+
continue;
|
|
147
|
+
// An indented comment under a plain value is no part of it, and the value may wrap on after it.
|
|
148
|
+
if (indented && plain !== undefined && /^\s+#/.test(line))
|
|
149
|
+
continue;
|
|
150
|
+
if (indented) {
|
|
151
|
+
// A wrapped plain value's line, never one that reads as a key, a list item or a comment of its own.
|
|
152
|
+
if (plain === undefined || /^\s+(?:#|-\s|\?\s|[^\s]*:(?:\s|$))/.test(line) || /:\s/.test(line))
|
|
153
|
+
return undefined;
|
|
154
|
+
add(plain, line);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
plain = undefined;
|
|
158
|
+
const pair = /^([A-Za-z_][\w-]*):(?:\s+(.*))?$/.exec(line);
|
|
159
|
+
if (!pair || front.has(pair[1]))
|
|
160
|
+
return undefined;
|
|
161
|
+
const value = (pair[2] ?? "").trim();
|
|
162
|
+
if (!value || /^[>|][-+]?$/.test(value)) {
|
|
163
|
+
block = pair[1];
|
|
164
|
+
front.set(block, "");
|
|
165
|
+
}
|
|
166
|
+
else if (/^["']/.test(value) && !closedQuote(value)) {
|
|
167
|
+
// A quoted value that runs on past its line: read once it closes, its lines folded as YAML folds them.
|
|
168
|
+
quoting = { key: pair[1], lines: [value] };
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
// A quoted value whose escapes do not decode cleanly reads no way.
|
|
172
|
+
const read = unquote(value);
|
|
173
|
+
if (read === undefined)
|
|
174
|
+
return undefined;
|
|
175
|
+
front.set(pair[1], read);
|
|
176
|
+
if (!/^["']/.test(value))
|
|
177
|
+
plain = pair[1];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// A quoted value never closed reads no way.
|
|
181
|
+
return quoting ? undefined : front;
|
|
182
|
+
}
|
|
183
|
+
/** Whether `value`, a quoted scalar's text from its opening quote, closes: it ends with its quote, a double quote not
|
|
184
|
+
* escaped (an even run of backslashes before it), a single quote not one of a doubled pair (`''`, an odd run). */
|
|
185
|
+
function closedQuote(value) {
|
|
186
|
+
const quote = value[0];
|
|
187
|
+
if ((quote !== '"' && quote !== "'") || value.length < 2 || !value.endsWith(quote))
|
|
188
|
+
return false;
|
|
189
|
+
const inner = value.slice(1);
|
|
190
|
+
if (quote === '"')
|
|
191
|
+
return (/(\\*)"$/.exec(inner)[1].length % 2) === 0;
|
|
192
|
+
return (/('+)$/.exec(inner)[1].length % 2) === 1;
|
|
193
|
+
}
|
|
194
|
+
/** A quoted scalar's `lines` (its first, from the opening quote, then the next ones as written) as one, folded as YAML
|
|
195
|
+
* folds a flow scalar: each line's surrounding white space dropped, each line break one space, each blank line a line
|
|
196
|
+
* break, and in a double-quoted one a break after a `\` none at all. unquote reads the result. */
|
|
197
|
+
function foldQuoted(lines) {
|
|
198
|
+
let out = lines[0].trimEnd();
|
|
199
|
+
let blanks = 0;
|
|
200
|
+
for (const line of lines.slice(1)) {
|
|
201
|
+
const part = line.trim();
|
|
202
|
+
if (part === "") {
|
|
203
|
+
blanks += 1;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (blanks)
|
|
207
|
+
out += "\n".repeat(blanks);
|
|
208
|
+
else if (out.startsWith('"') && /(?<!\\)(\\\\)*\\$/.test(out))
|
|
209
|
+
out = out.slice(0, -1);
|
|
210
|
+
else
|
|
211
|
+
out += " ";
|
|
212
|
+
out += part;
|
|
213
|
+
blanks = 0;
|
|
214
|
+
}
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
/** Every value a SKILL.md's or a draft README's front matter holds, as YAML reads it (frontMatter: escapes decoded):
|
|
218
|
+
* what a check for secrets reads beside the text as stored, since the decoded value is the one a harness is served
|
|
219
|
+
* (servedSkill). None for a text whose front matter does not read one way. */
|
|
220
|
+
export function frontValues(text) {
|
|
221
|
+
return [...(frontMatter(text)?.values() ?? [])];
|
|
222
|
+
}
|
|
223
|
+
/** The keys a SKILL.md's or a draft README's front matter sets, in order (frontMatter's); undefined for a text whose
|
|
224
|
+
* front matter does not read one way. */
|
|
225
|
+
export function frontKeys(text) {
|
|
226
|
+
const front = frontMatter(text);
|
|
227
|
+
return front ? [...front.keys()] : undefined;
|
|
68
228
|
}
|
|
69
229
|
/** The front matter keys a learned skill or draft README may hold: what a skill is (its name and when to use it). Claude
|
|
70
230
|
* Code reads directives from more (hooks, allowed-tools, context, agent, model, ...): a hook is a shell command its CLI
|
|
71
231
|
* runs. `status` is the host's (a held one). */
|
|
72
232
|
export const LEARNED_KEYS = ["name", "description"];
|
|
73
|
-
/** `text`, a learned skill's SKILL.md, as a run is given it: front matter with its name and its description alone,
|
|
74
|
-
*
|
|
75
|
-
*
|
|
233
|
+
/** `text`, a learned skill's SKILL.md, as a run is given it: front matter with its name and its description alone, as
|
|
234
|
+
* the one reader reads them (frontMatter), each quoted as JSON writes it, which every YAML reads back as that very
|
|
235
|
+
* string (never null, a boolean, a number or a date), written again whatever else its file holds (servedSkill is the
|
|
236
|
+
* one door a learned skill reaches a harness by), then its body as it is. A text with no front matter, or one whose front
|
|
237
|
+
* matter reads no way, is as it is: never served with its fields emptied. A learned one of those is never active, so
|
|
238
|
+
* never given; a revision's own is the harness's to check, as its author wrote it. */
|
|
76
239
|
export function servedSkill(text) {
|
|
77
|
-
const
|
|
78
|
-
if (typeof
|
|
240
|
+
const opened = frontOf(text);
|
|
241
|
+
if (typeof opened === "string")
|
|
242
|
+
return text;
|
|
243
|
+
const front = frontMatter(text);
|
|
244
|
+
if (!front)
|
|
79
245
|
return text;
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
const plain = /^[^\s\-?:,[\]{}#&*!|>'"%@`][^\n]*$/.test(description) && !/: |\s#|:$/.test(description);
|
|
83
|
-
const lines = [`name: ${front.value("name") ?? ""}`, `description: ${plain ? description : JSON.stringify(description)}`];
|
|
84
|
-
return `---\n${lines.join("\n")}\n---\n${front.lf.slice(front.end + 5)}`;
|
|
246
|
+
const lines = [`name: ${JSON.stringify(front.get("name") ?? "")}`, `description: ${JSON.stringify(front.get("description") ?? "")}`];
|
|
247
|
+
return `---\n${lines.join("\n")}\n---\n${opened.lf.slice(opened.end + 5)}`;
|
|
85
248
|
}
|
|
249
|
+
/** Skills as a harness that loads them through servedSkill is handed them (OpenCode, adapters.ts opencodeOf): each
|
|
250
|
+
* SKILL.md as servedSkill writes it, its other files as they are. Its table entry's check and the learned skills' room
|
|
251
|
+
* (learning.ts chooseSkills) read this same form, so a set the check takes is never one that grew past its limit after. */
|
|
252
|
+
export const servedSkills = (skills) => skills.map((skill) => ({ ...skill, files: { ...skill.files, ...(skill.files["SKILL.md"] === undefined ? {} : { "SKILL.md": servedSkill(skill.files["SKILL.md"]) }) } }));
|
|
86
253
|
/** Why a `SKILL.md` for the skill `name`, or a draft's `README.md` for the workflow `name`, is out of shape; empty when
|
|
87
254
|
* it is not. Its front matter names the folder's name and a description. */
|
|
88
255
|
export function skillProblems(text, name) {
|
|
89
|
-
const
|
|
90
|
-
if (typeof
|
|
91
|
-
return [
|
|
256
|
+
const opened = frontOf(text);
|
|
257
|
+
if (typeof opened === "string")
|
|
258
|
+
return [opened];
|
|
259
|
+
const front = frontMatter(text);
|
|
260
|
+
if (!front)
|
|
261
|
+
return [UNCLEAR_FRONT];
|
|
92
262
|
const out = [];
|
|
93
|
-
if (front.
|
|
263
|
+
if (front.get("name") !== name)
|
|
94
264
|
out.push(`name is not the folder's name, ${name}`);
|
|
95
|
-
if (!front.
|
|
265
|
+
if (!front.get("description"))
|
|
96
266
|
out.push("description is missing or empty");
|
|
97
267
|
return out;
|
|
98
268
|
}
|
|
99
|
-
/** A SKILL.md's or a draft README's front matter as a reader shows it: its name, description and status,
|
|
100
|
-
* when it is not there; undefined for a text
|
|
269
|
+
/** A SKILL.md's or a draft README's front matter as a reader shows it (frontMatter): its name, description and status,
|
|
270
|
+
* each absent when it is not there; undefined for a text whose front matter does not read one way. */
|
|
101
271
|
export function learnedFront(text) {
|
|
102
|
-
const front =
|
|
103
|
-
if (
|
|
272
|
+
const front = frontMatter(text);
|
|
273
|
+
if (!front)
|
|
104
274
|
return undefined;
|
|
105
|
-
const [name, description, status] = ["name", "description", "status"].map((key) => front.
|
|
275
|
+
const [name, description, status] = ["name", "description", "status"].map((key) => front.get(key));
|
|
106
276
|
return { ...(name === undefined ? {} : { name }), ...(description === undefined ? {} : { description }), ...(status === undefined ? {} : { status }) };
|
|
107
277
|
}
|
|
108
|
-
/** Whether a SKILL.md or a draft's README.md sets `status`, which only the host does (`held`). */
|
|
109
|
-
export const setsStatus = (text) =>
|
|
110
|
-
const front = frontOf(text);
|
|
111
|
-
return typeof front !== "string" && front.value("status") !== undefined;
|
|
112
|
-
};
|
|
278
|
+
/** Whether a SKILL.md or a draft's README.md sets `status`, which only the host does (`held`) (frontMatter). */
|
|
279
|
+
export const setsStatus = (text) => frontMatter(text)?.has("status") ?? false;
|
|
113
280
|
/** A skill or a draft the grader held for its owner's OK: its SKILL.md, or its draft's README.md, says `status:
|
|
114
|
-
* pending
|
|
115
|
-
export const isHeld = (text) =>
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
281
|
+
* pending` (learnedStatus). No run is given a held skill or workflow until a person approves it. */
|
|
282
|
+
export const isHeld = (text) => learnedStatus(text) === "pending";
|
|
283
|
+
/** Whether `text`'s status, when it sets one, is one plain line, `status: <value>` with nothing of it on the lines after
|
|
284
|
+
* (no quoted value that runs on, no block, no wrapped line): the only status the host's writers (released,
|
|
285
|
+
* retiredLearned), which take the `status:` line out whole, can edit. One set any other way reads unclear (learnedStatus:
|
|
286
|
+
* retired), never held or active, and no writer edits it, so a write never leaves part of it behind. The host writes
|
|
287
|
+
* only one-line statuses (held, retiredLearned). */
|
|
288
|
+
export function statusOnOneLine(text) {
|
|
289
|
+
const front = frontMatter(text);
|
|
290
|
+
if (!front?.has("status"))
|
|
291
|
+
return true;
|
|
292
|
+
const lf = text.replace(/\r\n/g, "\n");
|
|
293
|
+
const own = lf.slice(4, closeOf(lf)).split("\n").find((line) => line.startsWith("status:"))?.slice("status:".length).trim() ?? "";
|
|
294
|
+
if (!own || /^[>|]/.test(own) || (/^["']/.test(own) && !closedQuote(own)))
|
|
295
|
+
return false;
|
|
296
|
+
// A plain value that wraps onto the next lines reads longer than its own line.
|
|
297
|
+
return unquote(own) === front.get("status");
|
|
298
|
+
}
|
|
119
299
|
/** `text`, a SKILL.md or a draft's README.md, as the host merges one the grader held for its owner's OK: `status:
|
|
120
300
|
* pending` last in its front matter, with LF line ends. Throws on a text with no front matter, which the checker never
|
|
121
301
|
* passes. */
|
|
@@ -131,15 +311,28 @@ export function released(text) {
|
|
|
131
311
|
const front = frontOf(text);
|
|
132
312
|
if (typeof front === "string")
|
|
133
313
|
throw new Error(`it cannot be approved: ${front}`);
|
|
314
|
+
if (!statusOnOneLine(text))
|
|
315
|
+
throw new Error("it cannot be approved: its status is not one plain line");
|
|
134
316
|
const kept = front.lf.slice(4, front.end).split("\n").filter((line) => !line.startsWith("status:"));
|
|
135
|
-
|
|
317
|
+
const out = `---\n${kept.join("\n")}${front.lf.slice(front.end)}`;
|
|
318
|
+
// What it leaves reads back active, or the approve fails: never an OK over a skill no run is given.
|
|
319
|
+
if (learnedStatus(out) !== "active")
|
|
320
|
+
throw new Error(`it cannot be approved: its status would read ${learnedStatus(out)}`);
|
|
321
|
+
return out;
|
|
136
322
|
}
|
|
137
|
-
/** What a SKILL.md or a draft's README.md says of itself
|
|
138
|
-
*
|
|
323
|
+
/** What a SKILL.md or a draft's README.md says of itself, the one reader of it (frontMatter), the server's and, through
|
|
324
|
+
* the folder's snapshot, the console's: `pending` while it waits for its owner's OK, `retired` once a person retired
|
|
325
|
+
* it, `active` when it sets no status or `active`. A front matter that does not read one way, or a status that is none
|
|
326
|
+
* of these, is `retired`: never active, since another reader could read it as held or retired; no run is given it, and
|
|
327
|
+
* a restore refuses it until it reads one way. A run is given only an active one. */
|
|
139
328
|
export function learnedStatus(text) {
|
|
140
|
-
const front =
|
|
141
|
-
|
|
142
|
-
|
|
329
|
+
const front = frontMatter(text);
|
|
330
|
+
if (!front)
|
|
331
|
+
return "retired";
|
|
332
|
+
if (!statusOnOneLine(text))
|
|
333
|
+
return "retired";
|
|
334
|
+
const status = front.get("status") ?? "active";
|
|
335
|
+
return status === "active" || status === "pending" ? status : "retired";
|
|
143
336
|
}
|
|
144
337
|
/** `text`, a SKILL.md or a draft's README.md, as a person's retire leaves it: `status: retired` last in its front matter
|
|
145
338
|
* (any status line before it dropped), so no later run is given it. Throws on a text with no front matter. */
|
|
@@ -147,20 +340,293 @@ export function retiredLearned(text) {
|
|
|
147
340
|
const front = frontOf(text);
|
|
148
341
|
if (typeof front === "string")
|
|
149
342
|
throw new Error(`it cannot be retired: ${front}`);
|
|
343
|
+
if (!statusOnOneLine(text))
|
|
344
|
+
throw new Error("it cannot be retired: its status is not one plain line");
|
|
150
345
|
const kept = front.lf.slice(4, front.end).split("\n").filter((line) => !line.startsWith("status:"));
|
|
151
|
-
|
|
346
|
+
const out = `---\n${[...kept, "status: retired"].join("\n")}${front.lf.slice(front.end)}`;
|
|
347
|
+
if (!frontMatter(out) || learnedStatus(out) !== "retired")
|
|
348
|
+
throw new Error("it cannot be retired: its front matter would not read retired");
|
|
349
|
+
return out;
|
|
152
350
|
}
|
|
153
351
|
/** A draft's `README.md` as the host writes it, from the workflow's name and the one-line description its block gives. */
|
|
154
|
-
export const readmeOf = (name, description) => `---\nname: ${name}\
|
|
352
|
+
export const readmeOf = (name, description) => `---\nname: ${name}\n${frontLine("description", description)}\n---\n`;
|
|
353
|
+
/** `key: value` as a front matter line the one reader (frontMatter) reads back as `value`: plain when it does, else
|
|
354
|
+
* quoted as JSON writes it, whose escapes frontMatter decodes. A value the block quoted (`"\"a\" plus"`) is written
|
|
355
|
+
* quoted again, never as a plain line that reads another way or no way. */
|
|
356
|
+
function frontLine(key, value) {
|
|
357
|
+
const plain = `${key}: ${value}`;
|
|
358
|
+
return frontMatter(`---\n${plain}\n---\n`)?.get(key) === value ? plain : `${key}: ${JSON.stringify(value)}`;
|
|
359
|
+
}
|
|
155
360
|
/** A workflow block's two files: its `description:` front matter written as the draft's README.md (readmeOf), and the
|
|
156
361
|
* JSON after it as its workflow.json. A block with no front matter, or none that says what the draft is for, gives a
|
|
157
|
-
* README with no description, which the checker refuses; what the JSON holds is the checker's to judge.
|
|
362
|
+
* README with no description, which the checker refuses; what the JSON holds is the checker's to judge. `example`, its
|
|
363
|
+
* `example:` front matter line as written (the run's own input to the workflow, one JSON object): what its dry run runs
|
|
364
|
+
* on (draftDryRun), and never a file's: the README holds its name and description alone, so the run's input, which can
|
|
365
|
+
* be a customer's data, is never kept in the learning folder. */
|
|
158
366
|
export function draftFiles(name, block) {
|
|
159
367
|
const front = frontOf(block);
|
|
160
368
|
if (typeof front === "string")
|
|
161
369
|
return { json: block, readme: readmeOf(name, "") };
|
|
162
370
|
const description = (front.value("description") ?? "").replace(/\s+/g, " ").trim();
|
|
163
|
-
|
|
371
|
+
const example = front.value("example")?.trim();
|
|
372
|
+
return { json: front.lf.slice(front.end + "\n---\n".length), readme: readmeOf(name, description), ...(example ? { example } : {}) };
|
|
373
|
+
}
|
|
374
|
+
/** The words a draft that failed its dry run is left out with, before why: a re-ask reads them (reflect-hook.ts). */
|
|
375
|
+
export const DRY_RUN_FAILED = "its dry run failed";
|
|
376
|
+
/** The most synthetic steps a draft's dry run takes before it stops, as not run. */
|
|
377
|
+
const DRY_RUN_STEPS = 2_000;
|
|
378
|
+
/** The longest a draft's dry run may take, by the clock, before it stops, as not run: a backstop behind the steps and the
|
|
379
|
+
* static bounds (the DSL reads its signal at each step). */
|
|
380
|
+
const DRY_RUN_MS = 10_000;
|
|
381
|
+
/** The code runner a dry run hands every code node to: it refuses each. Given a runner, the DSL validates with
|
|
382
|
+
* executeCode: false (a code node's expression is parsed, never evaluated or probed) and never compiles or calls a code
|
|
383
|
+
* body in this process; with none, it evaluates every code node's expression and runs its body here, at validation and
|
|
384
|
+
* at run time. A learned draft holds no code node (draftProblems refuses it before any dry run): this holds even when
|
|
385
|
+
* one is handed here anyway. */
|
|
386
|
+
const refuseCode = async (node) => {
|
|
387
|
+
throw new Error(`the code node "${node.label}" is never run: a learned draft holds no code node`);
|
|
388
|
+
};
|
|
389
|
+
/** The most of a value a draft's dry run may synthesize from one of its schemas (the DSL's synthesizeInstance builds
|
|
390
|
+
* each in the host, before any step it counts): an array's minItems and a string's minLength each, and the values and
|
|
391
|
+
* string characters the schema's whole synthesized instance holds. Past any of them the draft is refused statically
|
|
392
|
+
* (draftProblems), and a dry run handed one anyway stands down as not run before anything is built (draftDryRun). */
|
|
393
|
+
export const SYNTH_MIN_ITEMS = 100;
|
|
394
|
+
export const SYNTH_MIN_LENGTH = 1_000;
|
|
395
|
+
export const SYNTH_VALUES = 2_000;
|
|
396
|
+
export const SYNTH_CHARS = 64_000;
|
|
397
|
+
/** The depth past which the DSL's synthesis stops (synthesizeInstance: null past 12). */
|
|
398
|
+
const SYNTH_DEPTH = 12;
|
|
399
|
+
/** A literal's values and string characters (a const, an enum's first), as the value it is. */
|
|
400
|
+
const literalSize = (value) => value === null || typeof value !== "object"
|
|
401
|
+
? { values: 1, chars: typeof value === "string" ? value.length : 0 }
|
|
402
|
+
: Object.values(value).map(literalSize).reduce((a, b) => ({ values: a.values + b.values, chars: a.chars + b.chars }), { values: 1, chars: 0 });
|
|
403
|
+
/** What the DSL's synthesizeInstance would build of `schema` at `depth`, counted without building it: its values and its
|
|
404
|
+
* strings' characters. It reads the schema as synthesis does (a $ref to its definitions, a const or an enum's first, an
|
|
405
|
+
* anyOf's first non-null branch, a oneOf's first, an object's every property, an array of max(1, minItems) items, a
|
|
406
|
+
* string of max(1, minLength) characters, nothing past depth 12), and counts an array's items once, times its length,
|
|
407
|
+
* each (schema, depth) once (`seen`), so the count itself is bounded by the schema's size. */
|
|
408
|
+
function synthesized(schema, definitions, depth, seen) {
|
|
409
|
+
if (depth > SYNTH_DEPTH || !schema || typeof schema !== "object")
|
|
410
|
+
return { values: 1, chars: 0 };
|
|
411
|
+
const known = seen.get(schema)?.get(depth);
|
|
412
|
+
if (known)
|
|
413
|
+
return known;
|
|
414
|
+
const spec = schema;
|
|
415
|
+
const of = (sub) => synthesized(sub, definitions, depth + 1, seen);
|
|
416
|
+
let out = { values: 1, chars: 0 };
|
|
417
|
+
const type = Array.isArray(spec.type) ? spec.type[0] : spec.type;
|
|
418
|
+
if (typeof spec.$ref === "string") {
|
|
419
|
+
const match = /^#\/definitions\/(.+)$/.exec(spec.$ref);
|
|
420
|
+
if (match && definitions[match[1]])
|
|
421
|
+
out = of(definitions[match[1]]);
|
|
422
|
+
}
|
|
423
|
+
else if (spec.const !== undefined || (Array.isArray(spec.enum) && spec.enum.length)) {
|
|
424
|
+
// The literal itself, as written in the draft (so bounded by its size).
|
|
425
|
+
out = literalSize(spec.const !== undefined ? spec.const : spec.enum[0]);
|
|
426
|
+
}
|
|
427
|
+
else if (Array.isArray(spec.anyOf) && spec.anyOf.length) {
|
|
428
|
+
out = of(spec.anyOf.find((branch) => branch?.type !== "null") ?? spec.anyOf[0]);
|
|
429
|
+
}
|
|
430
|
+
else if (Array.isArray(spec.oneOf) && spec.oneOf.length) {
|
|
431
|
+
out = of(spec.oneOf[0]);
|
|
432
|
+
}
|
|
433
|
+
else if (type === "object" || (!type && spec.properties)) {
|
|
434
|
+
for (const property of Object.values(spec.properties || {})) {
|
|
435
|
+
const one = of(property);
|
|
436
|
+
out = { values: out.values + one.values, chars: out.chars + one.chars };
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else if (type === "array") {
|
|
440
|
+
if (spec.maxItems !== 0 && spec.items) {
|
|
441
|
+
const length = Math.max(1, Number.isInteger(spec.minItems) ? spec.minItems : 1);
|
|
442
|
+
const item = of(spec.items);
|
|
443
|
+
out = { values: 1 + length * item.values, chars: length * item.chars };
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else if (type === "string") {
|
|
447
|
+
if (spec.maxLength !== 0)
|
|
448
|
+
out = { values: 1, chars: Math.max(1, Number.isInteger(spec.minLength) ? spec.minLength : 1) };
|
|
449
|
+
}
|
|
450
|
+
let atDepth = seen.get(schema);
|
|
451
|
+
if (!atDepth)
|
|
452
|
+
seen.set(schema, (atDepth = new Map()));
|
|
453
|
+
atDepth.set(depth, out);
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
/** What the DSL's synthesizeInstance would build of `schema` over `definitions`, counted without building it. */
|
|
457
|
+
export const synthesizedSize = (schema, definitions = {}) => synthesized(schema, definitions, 0, new Map());
|
|
458
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
459
|
+
/** The nodes directly inside `node`, as the DSL reads them (recovery/execution-path.ts childrenOf): a chain's steps, a
|
|
460
|
+
* parallel's branches, a map's or a loop's body, a route's branch bodies. A composed workflow node's child is a
|
|
461
|
+
* workflow, walked as one (eachWorkflow). Only these are node positions: an object anywhere else (a schema, a const, a
|
|
462
|
+
* step's literal value) is data, never a node, whatever keys it holds. */
|
|
463
|
+
function childNodes(node) {
|
|
464
|
+
switch (node.node) {
|
|
465
|
+
case "chain": return Array.isArray(node.steps) ? node.steps : [];
|
|
466
|
+
case "parallel": return Array.isArray(node.branches) ? node.branches : [];
|
|
467
|
+
case "map":
|
|
468
|
+
case "loop": return [node.body];
|
|
469
|
+
case "route": return isRecord(node.branches) ? Object.values(node.branches).map((branch) => (isRecord(branch) ? branch.body : undefined)) : [];
|
|
470
|
+
default: return [];
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/** Each workflow in `doc` (the draft's own, then each composed workflow node's child, at any depth), each with the loops
|
|
474
|
+
* around it (their maxIters multiplied), and each node of it at a node position with the loops around that node. */
|
|
475
|
+
function eachWorkflow(doc, visit) {
|
|
476
|
+
const walkNode = (value, around) => {
|
|
477
|
+
if (!isRecord(value))
|
|
478
|
+
return;
|
|
479
|
+
const within = visit.node ? visit.node(value, around) : around;
|
|
480
|
+
if (value.node === "workflow")
|
|
481
|
+
return walkWorkflow(value.workflow, within);
|
|
482
|
+
for (const child of childNodes(value))
|
|
483
|
+
walkNode(child, within);
|
|
484
|
+
};
|
|
485
|
+
const walkWorkflow = (value, around) => {
|
|
486
|
+
if (!isRecord(value))
|
|
487
|
+
return;
|
|
488
|
+
visit.workflow?.(value);
|
|
489
|
+
walkNode(value.root, around);
|
|
490
|
+
};
|
|
491
|
+
walkWorkflow(doc, 1);
|
|
492
|
+
}
|
|
493
|
+
/** Every workflow in `doc` with its schemas: the draft's own and each composed workflow node's child, at any depth. */
|
|
494
|
+
function workflowsOf(doc) {
|
|
495
|
+
const out = [];
|
|
496
|
+
eachWorkflow(doc, { workflow: (workflow) => out.push(workflow) });
|
|
497
|
+
return out;
|
|
498
|
+
}
|
|
499
|
+
/** The keywords whose values are schemas, by shape: a map of them, a list of them, or one (a list too, for items). Every
|
|
500
|
+
* other keyword's value (const, enum, default, examples, a title) is data, never a schema. */
|
|
501
|
+
const SCHEMA_MAPS = ["properties", "patternProperties", "$defs", "definitions", "dependentSchemas"];
|
|
502
|
+
const SCHEMA_LISTS = ["anyOf", "oneOf", "allOf", "prefixItems"];
|
|
503
|
+
const SCHEMA_ONES = ["items", "additionalItems", "additionalProperties", "contains", "not", "if", "then", "else", "propertyNames", "unevaluatedItems", "unevaluatedProperties"];
|
|
504
|
+
/** Each schema in `schema`, itself first, at schema positions only (each once). */
|
|
505
|
+
function eachSchema(schema, visit, seen = new Set()) {
|
|
506
|
+
if (!isRecord(schema) || seen.has(schema))
|
|
507
|
+
return;
|
|
508
|
+
seen.add(schema);
|
|
509
|
+
visit(schema);
|
|
510
|
+
for (const key of SCHEMA_MAPS)
|
|
511
|
+
if (isRecord(schema[key]))
|
|
512
|
+
for (const sub of Object.values(schema[key]))
|
|
513
|
+
eachSchema(sub, visit, seen);
|
|
514
|
+
for (const key of SCHEMA_LISTS)
|
|
515
|
+
if (Array.isArray(schema[key]))
|
|
516
|
+
for (const sub of schema[key])
|
|
517
|
+
eachSchema(sub, visit, seen);
|
|
518
|
+
for (const key of SCHEMA_ONES)
|
|
519
|
+
for (const sub of Array.isArray(schema[key]) ? schema[key] : [schema[key]])
|
|
520
|
+
eachSchema(sub, visit, seen);
|
|
521
|
+
}
|
|
522
|
+
/** The most iterations one loop of a learned draft may run (its maxIters), and the most nested loops may run together
|
|
523
|
+
* (their maxIters multiplied, outermost to innermost). A loop's iterations are work the dry run does in the host whether
|
|
524
|
+
* or not its body asks a model (an empty map, a predicate), so they are bounded here, not only by the dry run's steps. */
|
|
525
|
+
export const LEARNED_MAX_ITERS = 50;
|
|
526
|
+
export const LEARNED_LOOP_ITERS = 1_000;
|
|
527
|
+
/** Why `doc`'s loops could run too long: a loop whose maxIters is past LEARNED_MAX_ITERS, or nested loops whose maxIters
|
|
528
|
+
* multiply past LEARNED_LOOP_ITERS, a composed workflow node's child's included. Read from the JSON alone, at node
|
|
529
|
+
* positions only (eachWorkflow): a literal shaped like a loop is data. */
|
|
530
|
+
export function loopProblems(doc) {
|
|
531
|
+
const problems = [];
|
|
532
|
+
eachWorkflow(doc, {
|
|
533
|
+
node: (node, around) => {
|
|
534
|
+
if (node.node !== "loop")
|
|
535
|
+
return around;
|
|
536
|
+
const iters = typeof node.maxIters === "number" ? node.maxIters : 0;
|
|
537
|
+
const label = JSON.stringify(String(node.label ?? "").slice(0, 64));
|
|
538
|
+
const within = around * iters;
|
|
539
|
+
if (iters > LEARNED_MAX_ITERS)
|
|
540
|
+
problems.push(`its loop ${label} runs up to maxIters ${iters} times (the most is ${LEARNED_MAX_ITERS})`);
|
|
541
|
+
else if (within > LEARNED_LOOP_ITERS)
|
|
542
|
+
problems.push(`its loop ${label} and the loops around it run up to ${within} times together, their maxIters multiplied (the most is ${LEARNED_LOOP_ITERS})`);
|
|
543
|
+
return within;
|
|
544
|
+
},
|
|
545
|
+
});
|
|
546
|
+
return problems;
|
|
547
|
+
}
|
|
548
|
+
/** Why a dry run of `doc` would build too much in the host: a schema whose minItems or minLength is past its cap, or
|
|
549
|
+
* whose synthesized instance holds more values or characters than SYNTH_VALUES or SYNTH_CHARS. Empty when none. Each
|
|
550
|
+
* schema is counted as the dry run reads it, after the same resolution (`dsl.resolveSchemaForWorkflow`), so a `$ref` a
|
|
551
|
+
* schema's own (even empty) definitions would hide is counted as the dry run fills it in. Building nothing. */
|
|
552
|
+
export function synthesisProblems(doc, dsl) {
|
|
553
|
+
const problems = [];
|
|
554
|
+
for (const workflow of workflowsOf(doc)) {
|
|
555
|
+
const schemas = workflow.schemas && typeof workflow.schemas === "object" ? workflow.schemas : {};
|
|
556
|
+
for (const [id, raw] of Object.entries(schemas)) {
|
|
557
|
+
// One count per schema: a `$ref` resolves against that schema's own definitions.
|
|
558
|
+
const seen = new Map();
|
|
559
|
+
let schema;
|
|
560
|
+
try {
|
|
561
|
+
schema = raw && typeof raw === "object" ? dsl.resolveSchemaForWorkflow({ schemas }, raw) : raw;
|
|
562
|
+
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
problems.push(`its schema ${JSON.stringify(id.slice(0, 64))} could not be resolved as its dry run would: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
// Each cap, on each schema in the schema (eachSchema: schema positions only; a literal that holds the keyword is
|
|
568
|
+
// data).
|
|
569
|
+
const caps = new Set();
|
|
570
|
+
eachSchema(schema, (spec) => {
|
|
571
|
+
if (typeof spec.minItems === "number" && spec.minItems > SYNTH_MIN_ITEMS)
|
|
572
|
+
caps.add(`minItems ${spec.minItems} (the most is ${SYNTH_MIN_ITEMS})`);
|
|
573
|
+
if (typeof spec.minLength === "number" && spec.minLength > SYNTH_MIN_LENGTH)
|
|
574
|
+
caps.add(`minLength ${spec.minLength} (the most is ${SYNTH_MIN_LENGTH})`);
|
|
575
|
+
});
|
|
576
|
+
const definitions = schema?.definitions ?? schemas;
|
|
577
|
+
const size = synthesized(schema, definitions, 0, seen);
|
|
578
|
+
if (size.values > SYNTH_VALUES)
|
|
579
|
+
caps.add(`${size.values > 1e12 ? "over a trillion" : size.values} values (the most is ${SYNTH_VALUES})`);
|
|
580
|
+
if (size.chars > SYNTH_CHARS)
|
|
581
|
+
caps.add(`${size.chars > 1e12 ? "over a trillion" : size.chars} characters (the most is ${SYNTH_CHARS})`);
|
|
582
|
+
if (caps.size)
|
|
583
|
+
problems.push(`its schema ${JSON.stringify(id.slice(0, 64))} would have its dry run synthesize too large a value: ${[...caps].join(", ")}`);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return problems;
|
|
587
|
+
}
|
|
588
|
+
/** A draft's workflow.json `json` dry-run by the workflow DSL (dryRunWorkflow) on `example` (its block's `example:`
|
|
589
|
+
* line, one JSON object), else on an input synthesized from its input schema. Every model, Jev and tool-call answer is
|
|
590
|
+
* synthesized from the node's output schema: no request is sent, nothing is paid, no tool runs, and no code it holds
|
|
591
|
+
* runs in this process (refuseCode). Only a draft the static checks pass is dry-run (propose.ts). It checks the wiring
|
|
592
|
+
* at run time (the state each step reads, its output's path and schema), never the answers. */
|
|
593
|
+
export async function draftDryRun(dsl, json, example) {
|
|
594
|
+
let doc;
|
|
595
|
+
let input;
|
|
596
|
+
try {
|
|
597
|
+
doc = JSON.parse(json);
|
|
598
|
+
}
|
|
599
|
+
catch {
|
|
600
|
+
return { status: "failed", detail: `${DRY_RUN_FAILED}: it does not parse as JSON` };
|
|
601
|
+
}
|
|
602
|
+
if (example !== undefined) {
|
|
603
|
+
let given;
|
|
604
|
+
try {
|
|
605
|
+
given = JSON.parse(example);
|
|
606
|
+
}
|
|
607
|
+
catch {
|
|
608
|
+
given = undefined;
|
|
609
|
+
}
|
|
610
|
+
if (typeof given !== "object" || given === null || Array.isArray(given))
|
|
611
|
+
return { status: "failed", detail: `${DRY_RUN_FAILED}: its example: line is not one JSON object` };
|
|
612
|
+
input = given;
|
|
613
|
+
}
|
|
614
|
+
// Nothing is synthesized or looped past the bounds: a draft whose schemas would build too much, or whose loops could run
|
|
615
|
+
// too long, stands down here before the DSL runs any of it (the static checks refuse it first; this holds for a draft
|
|
616
|
+
// handed here anyway).
|
|
617
|
+
const tooLarge = [...synthesisProblems(doc, dsl), ...loopProblems(doc)];
|
|
618
|
+
if (tooLarge.length)
|
|
619
|
+
return { status: "not_run", detail: tooLarge.join("; ").slice(0, 300) };
|
|
620
|
+
let result;
|
|
621
|
+
try {
|
|
622
|
+
result = await dsl.dryRunWorkflow(doc, { ...(input ? { input } : {}), maxSteps: DRY_RUN_STEPS, runCode: refuseCode, signal: AbortSignal.timeout(DRY_RUN_MS) });
|
|
623
|
+
}
|
|
624
|
+
catch (error) {
|
|
625
|
+
return { status: "failed", detail: `${DRY_RUN_FAILED}: ${String(error?.message ?? error).slice(0, 300)}` };
|
|
626
|
+
}
|
|
627
|
+
if (result.ok)
|
|
628
|
+
return result.status === "passed" ? { status: "passed" } : { status: "not_run", detail: (result.skipped ?? []).join("; ").slice(0, 300) || "a check could not run" };
|
|
629
|
+
return { status: "failed", detail: `${DRY_RUN_FAILED}${result.stage ? ` at "${result.stage.slice(0, 64)}"` : ""}: ${result.problems.join("; ").slice(0, 300)}` };
|
|
164
630
|
}
|
|
165
631
|
/** The refusal for a draft that names a tool when the host gave no tool list: the check fails closed. */
|
|
166
632
|
export const NO_TOOL_LIST = "no tool list was given, so a draft that names a tool is refused";
|
|
@@ -208,6 +674,44 @@ const gatedNodes = (dsl, doc, gated, deniedBy) => dsl.inspectWorkflow(doc).nodes
|
|
|
208
674
|
: `This workflow's call node calls ${tool}, which is set to ask, and a workflow's call node can't wait for an approval: set it to allow for this agent.`;
|
|
209
675
|
return [{ rule: "workflow", detail: `${words} (the call node at ${node.path})` }];
|
|
210
676
|
});
|
|
677
|
+
/** Every string a learned workflow's workflow.json decodes to, each object's keys too, as JSON.parse reads them (its
|
|
678
|
+
* escapes decoded: what draftProblems and a learned call's run read, and a model node is given). Text a scan of the
|
|
679
|
+
* stored JSON misses, spelled in \u escapes, is there as it will be read. None for a text that does not parse (the
|
|
680
|
+
* checks refuse it as no workflow). */
|
|
681
|
+
export function decodedStrings(json) {
|
|
682
|
+
let doc;
|
|
683
|
+
try {
|
|
684
|
+
doc = JSON.parse(json);
|
|
685
|
+
}
|
|
686
|
+
catch {
|
|
687
|
+
return [];
|
|
688
|
+
}
|
|
689
|
+
// Walked with a stack of its own, never the call stack, and no deeper than DECODED_DEPTH: a draft nested past any
|
|
690
|
+
// workflow (thousands of arrays in a few KB) is refused on its own (check.ts), never a throw that takes its siblings.
|
|
691
|
+
const out = [];
|
|
692
|
+
const stack = [[doc, 0]];
|
|
693
|
+
while (stack.length) {
|
|
694
|
+
const [value, depth] = stack.pop();
|
|
695
|
+
if (typeof value === "string") {
|
|
696
|
+
out.push(value);
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (!value || typeof value !== "object")
|
|
700
|
+
continue;
|
|
701
|
+
if (depth >= DECODED_DEPTH)
|
|
702
|
+
return "too_deep";
|
|
703
|
+
if (Array.isArray(value))
|
|
704
|
+
for (let i = value.length - 1; i >= 0; i--)
|
|
705
|
+
stack.push([value[i], depth + 1]);
|
|
706
|
+
else
|
|
707
|
+
for (const [key, child] of Object.entries(value).reverse())
|
|
708
|
+
stack.push([child, depth + 1], [key, depth + 1]);
|
|
709
|
+
}
|
|
710
|
+
return out;
|
|
711
|
+
}
|
|
712
|
+
/** How deep a learned workflow's JSON may nest (decodedStrings): far past any workflow the DSL writes, a node's schemas
|
|
713
|
+
* included, and short of what a walk of it could not bound. */
|
|
714
|
+
export const DECODED_DEPTH = 100;
|
|
211
715
|
/** The most turns a learned workflow's model node runs (learned-workflow.ts hands its runner this cap): an agent node
|
|
212
716
|
* that asks for more is refused at learn time and before a call, never when it comes up after earlier nodes spent. */
|
|
213
717
|
export const LEARNED_MAX_TURNS = 8;
|
|
@@ -262,7 +766,7 @@ export function draftProblems(dsl, text, tools, gated = {}, deniedBy = {}) {
|
|
|
262
766
|
return issues.slice(0, 5).map((issue) => ({ rule: "workflow", detail: `the workflow DSL refuses it: ${issue.code} at ${issue.path || "/"}: ${issue.message.slice(0, 200)}` }));
|
|
263
767
|
let out;
|
|
264
768
|
try {
|
|
265
|
-
out = [...toolingNodes(dsl, doc), ...executableNodes(dsl, doc), ...unrunnableNodes(dsl, doc), ...longNodes(dsl, doc), ...gatedNodes(dsl, doc, gated, deniedBy)];
|
|
769
|
+
out = [...toolingNodes(dsl, doc), ...executableNodes(dsl, doc), ...unrunnableNodes(dsl, doc), ...longNodes(dsl, doc), ...gatedNodes(dsl, doc, gated, deniedBy), ...[...synthesisProblems(doc, dsl), ...loopProblems(doc)].map((detail) => ({ rule: "workflow", detail }))];
|
|
266
770
|
// A learned workflow answers the value at its output's path: one that names none would answer its whole state, which
|
|
267
771
|
// fails its output schema when the call ends, after every node spent.
|
|
268
772
|
const path = doc?.output?.path;
|