@parcha/agentrun-serve 0.1.0-beta.44 → 0.1.0-beta.46
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 +148 -5
- package/README.md +385 -305
- package/dist/acp-package.d.ts +28 -0
- package/dist/acp-package.js +23 -0
- package/dist/adapter-facts.d.ts +8 -1
- package/dist/adapter-facts.js +9 -1
- package/dist/adapters.d.ts +5 -0
- package/dist/adapters.js +36 -3
- package/dist/agent-delete.d.ts +50 -0
- package/dist/agent-delete.js +125 -0
- package/dist/agents-store.d.ts +9 -3
- package/dist/agents-store.js +22 -8
- package/dist/agents.d.ts +5 -1
- package/dist/agents.js +8 -4
- package/dist/api-only.d.ts +8 -3
- package/dist/api-only.js +10 -4
- package/dist/archil-routes.d.ts +5 -1
- package/dist/archil-routes.js +57 -2
- package/dist/browser-wait-ended.d.ts +2 -0
- package/dist/browser-wait-ended.js +2 -0
- package/dist/calendar/ics-faux.d.ts +3 -1
- package/dist/calendar/ics-faux.js +12 -2
- package/dist/calendar/tools.js +5 -2
- package/dist/chatgpt-plan.js +3 -1
- package/dist/claude-login.d.ts +135 -0
- package/dist/claude-login.js +244 -0
- package/dist/cli.js +62 -19
- package/dist/composio-preset.js +69 -7
- package/dist/composio-triggers.d.ts +14 -0
- package/dist/composio-triggers.js +79 -15
- package/dist/computer-attach.d.ts +40 -5
- package/dist/computer-attach.js +130 -15
- package/dist/computer-keys.d.ts +3 -0
- package/dist/computer-keys.js +7 -1
- package/dist/computer-live.js +3 -2
- package/dist/computers.d.ts +21 -10
- package/dist/computers.js +39 -28
- package/dist/console/assets/index-C2MYaUBa.css +1 -0
- package/dist/console/assets/index-CZZeCReB.js +701 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.d.ts +1 -1
- package/dist/dial-relay.d.ts +29 -0
- package/dist/dial-relay.js +94 -0
- package/dist/email/tools.js +10 -5
- package/dist/env-proxy.d.ts +2 -0
- package/dist/env-proxy.js +35 -0
- package/dist/errors.d.ts +3 -1
- package/dist/errors.js +7 -4
- package/dist/faux-computers.d.ts +8 -2
- package/dist/faux-computers.js +83 -6
- package/dist/harness-archive.d.ts +67 -0
- package/dist/harness-archive.js +177 -0
- package/dist/harness-goose.d.ts +34 -0
- package/dist/harness-goose.js +87 -0
- package/dist/harness-install.d.ts +21 -0
- package/dist/harness-install.js +24 -11
- package/dist/harness-opencode/adapter.js +1 -1
- package/dist/harness-table.d.ts +9 -1
- package/dist/harness-table.js +20 -3
- package/dist/harness.d.ts +1 -2
- package/dist/harness.js +3 -4
- package/dist/host.js +2 -1
- package/dist/import.d.ts +10 -0
- package/dist/import.js +28 -8
- package/dist/key-check.js +3 -0
- package/dist/learn/audit.d.ts +6 -6
- package/dist/learn/audit.js +3 -7
- package/dist/learn/check.d.ts +23 -89
- package/dist/learn/check.js +57 -335
- package/dist/learn/commit.d.ts +63 -90
- package/dist/learn/commit.js +124 -316
- package/dist/learn/compile.d.ts +53 -50
- package/dist/learn/compile.js +89 -104
- package/dist/learn/corrections.d.ts +13 -0
- package/dist/learn/corrections.js +23 -0
- package/dist/learn/delete.d.ts +5 -5
- package/dist/learn/delete.js +8 -10
- package/dist/learn/folder.d.ts +35 -47
- package/dist/learn/folder.js +118 -134
- package/dist/learn/index.d.ts +3 -5
- package/dist/learn/index.js +6 -9
- package/dist/learn/learning.sh +45 -15
- package/dist/learn/promote.d.ts +23 -5
- package/dist/learn/promote.js +40 -2
- package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +30 -38
- package/dist/learn/{skills.js → workflow-draft.js} +48 -58
- package/dist/learn/workflow-nodes.js +1 -1
- package/dist/learned-workflow.js +2 -2
- package/dist/learning-cost.d.ts +38 -24
- package/dist/learning-cost.js +41 -24
- package/dist/learning-routes.d.ts +91 -170
- package/dist/learning-routes.js +183 -421
- package/dist/learning-run.d.ts +19 -40
- package/dist/learning-run.js +23 -93
- package/dist/learning-setting.d.ts +21 -10
- package/dist/learning-setting.js +47 -11
- package/dist/learning-words.d.ts +28 -74
- package/dist/learning-words.js +64 -162
- package/dist/learning.d.ts +61 -392
- package/dist/learning.js +101 -651
- package/dist/live-reach.d.ts +25 -2
- package/dist/live-reach.js +46 -4
- package/dist/llms.txt +1 -1
- package/dist/local-models.d.ts +3 -11
- package/dist/local-models.js +5 -6
- package/dist/mcp-servers-store.d.ts +4 -4
- package/dist/model-form.js +6 -5
- package/dist/model.d.ts +4 -0
- package/dist/model.js +26 -9
- package/dist/native/binding.js +1 -1
- package/dist/native/connection.js +1 -1
- package/dist/native/discovery.js +1 -1
- package/dist/native/instructions.d.ts +3 -0
- package/dist/native/instructions.js +5 -3
- package/dist/native/remote-worker-bridge.js +9 -43
- package/dist/native/remote.d.ts +8 -15
- package/dist/native/remote.js +3 -3
- package/dist/native/schema.js +1 -1
- package/dist/native/types.d.ts +2 -2
- package/dist/openapi.js +273 -319
- package/dist/paid-calls.d.ts +8 -0
- package/dist/paid-calls.js +20 -0
- package/dist/particle-provider.d.ts +11 -0
- package/dist/particle-provider.js +135 -0
- package/dist/port-models.d.ts +12 -4
- package/dist/port-models.js +28 -11
- package/dist/print.d.ts +27 -0
- package/dist/print.js +21 -0
- package/dist/provider.d.ts +7 -0
- package/dist/provider.js +15 -1
- package/dist/reflect-range.js +2 -3
- package/dist/run-launch.d.ts +14 -28
- package/dist/run-launch.js +9 -12
- package/dist/run-learning-served.d.ts +15 -89
- package/dist/run-listing.d.ts +34 -0
- package/dist/run-listing.js +128 -0
- package/dist/run-model-key.js +1 -1
- package/dist/run-timeline-served.js +5 -3
- package/dist/run-trace.d.ts +97 -0
- package/dist/run-trace.js +253 -0
- package/dist/run-view.d.ts +15 -0
- package/dist/run-view.js +9 -0
- package/dist/runnable.js +2 -1
- package/dist/runner/adapter-loop.d.ts +8 -6
- package/dist/runner/adapter-loop.js +16 -12
- package/dist/runner/adapter-run-spec.d.ts +16 -4
- package/dist/runner/adapter-run-spec.js +64 -58
- package/dist/runner/adapters/index.js +22 -0
- package/dist/runner/adapters/pi.d.ts +4 -12
- package/dist/runner/adapters/pi.js +2 -2
- package/dist/runner/audit.d.ts +1 -1
- package/dist/runner/audit.js +30 -13
- package/dist/runner/beside.d.ts +6 -2
- package/dist/runner/beside.js +38 -9
- package/dist/runner/claim-secrets.d.ts +18 -2
- package/dist/runner/claim-secrets.js +42 -18
- package/dist/runner/cli-run-spec.js +55 -51
- package/dist/runner/conformance/harness.d.ts +6 -0
- package/dist/runner/conformance/harness.js +35 -13
- package/dist/runner/conformance/subject.d.ts +6 -0
- package/dist/runner/dispatch.d.ts +2 -2
- package/dist/runner/end-of-run.d.ts +6 -14
- package/dist/runner/end-of-run.js +8 -33
- package/dist/runner/feedback-round.d.ts +2 -2
- package/dist/runner/feedback-round.js +7 -7
- package/dist/runner/github-app.js +3 -1
- package/dist/runner/harness-loops.d.ts +0 -8
- package/dist/runner/harness-loops.js +11 -59
- package/dist/runner/held.d.ts +170 -0
- package/dist/runner/held.js +57 -0
- package/dist/runner/journal-kinds.d.ts +2 -3
- package/dist/runner/journal-kinds.js +2 -3
- package/dist/runner/learned-first.d.ts +40 -0
- package/dist/runner/learned-first.js +32 -0
- package/dist/runner/learning-end.d.ts +77 -69
- package/dist/runner/learning-end.js +250 -303
- package/dist/runner/learning-seed.d.ts +15 -33
- package/dist/runner/learning-seed.js +24 -45
- package/dist/runner/loop-contract.d.ts +2 -3
- package/dist/runner/main.d.ts +17 -9
- package/dist/runner/main.js +82 -20
- package/dist/runner/mcp-run.d.ts +11 -0
- package/dist/runner/mcp-run.js +67 -0
- package/dist/runner/memory-claim.d.ts +10 -5
- package/dist/runner/memory-claim.js +12 -6
- package/dist/runner/model-route.d.ts +7 -4
- package/dist/runner/model-route.js +22 -10
- package/dist/runner/native-loop.d.ts +0 -7
- package/dist/runner/native-loop.js +9 -101
- package/dist/runner/promotion.d.ts +12 -8
- package/dist/runner/promotion.js +37 -28
- package/dist/runner/reflect-hook.d.ts +64 -95
- package/dist/runner/reflect-hook.js +112 -225
- package/dist/runner/run-channel.d.ts +43 -0
- package/dist/runner/run-channel.js +467 -0
- package/dist/runner/run-child-host.d.ts +169 -0
- package/dist/runner/run-child-host.js +1230 -0
- package/dist/runner/run-child.d.ts +1 -0
- package/dist/runner/run-child.js +254 -0
- package/dist/runner/run-errors.d.ts +2 -0
- package/dist/runner/run-errors.js +20 -0
- package/dist/runner/runner.js +1 -5
- package/dist/runner/sdk-ask.d.ts +78 -0
- package/dist/runner/sdk-ask.js +158 -0
- package/dist/runner/sdk-loop.d.ts +2 -4
- package/dist/runner/sdk-loop.js +29 -49
- package/dist/runner/store-secrets.d.ts +5 -3
- package/dist/runner/store-secrets.js +5 -1
- package/dist/runner/workflow-first.d.ts +5 -0
- package/dist/runner/workflow-first.js +4 -0
- package/dist/runner/workflow-spec.js +7 -2
- package/dist/runs-over-store.d.ts +16 -28
- package/dist/runs-over-store.js +31 -38
- package/dist/sdk/runs.d.ts +6 -11
- package/dist/sdk/runs.js +20 -41
- package/dist/sdk/schema.js +3 -4
- package/dist/seal-key.d.ts +5 -0
- package/dist/seal-key.js +7 -0
- package/dist/secrets.d.ts +3 -0
- package/dist/secrets.js +16 -3
- package/dist/server.d.ts +55 -33
- package/dist/server.js +689 -322
- package/dist/sign-ins-store.d.ts +8 -0
- package/dist/sign-ins-store.js +36 -1
- package/dist/skills.js +1 -1
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +3 -0
- package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
- package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
- package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
- package/dist/store/schema.d.ts +53 -4
- package/dist/store/schema.js +137 -6
- package/dist/store/sqlite/connection.d.ts +2 -0
- package/dist/store/sqlite/connection.js +3 -1
- package/dist/store/sqlite/run-patch.d.ts +0 -17
- package/dist/store/sqlite/run-patch.js +4 -33
- package/dist/store/sqlite/store.js +246 -63
- package/dist/store/sqlite/worker.js +25 -0
- package/dist/store/store.d.ts +193 -36
- package/dist/store/store.js +25 -3
- package/dist/store-reads/learning.d.ts +102 -204
- package/dist/store-reads/learning.js +159 -321
- package/dist/store-reads/loop.d.ts +0 -1
- package/dist/store-reads/loop.js +1 -1
- package/dist/store-view.d.ts +29 -1
- package/dist/store-view.js +97 -31
- package/dist/template-eval.d.ts +3 -6
- package/dist/template-eval.js +22 -36
- package/dist/templates.d.ts +8 -4
- package/dist/templates.js +44 -6
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/timeline.d.ts +5 -1
- package/dist/timeline/timeline.js +16 -0
- package/dist/tool-gate.d.ts +0 -7
- package/dist/tool-gate.js +1 -15
- package/dist/tool-keys-store.d.ts +1 -1
- package/dist/tool-keys.d.ts +6 -4
- package/dist/tool-keys.js +12 -6
- package/dist/tools.d.ts +2 -2
- package/dist/tools.js +11 -4
- package/dist/triggers.d.ts +65 -6
- package/dist/triggers.js +125 -19
- package/dist/validate.js +1 -1
- package/dist/webhooks.d.ts +7 -2
- package/dist/webhooks.js +12 -5
- package/dist/workspace/agent-computer.d.ts +2 -0
- package/dist/workspace/agent-computer.js +2 -0
- package/dist/workspace/archil-claim.d.ts +7 -0
- package/dist/workspace/archil-claim.js +29 -0
- package/dist/workspace/archil.d.ts +9 -1
- package/dist/workspace/archil.js +165 -3
- package/dist/workspace/attach.d.ts +3 -1
- package/dist/workspace/attach.js +2 -0
- package/dist/workspace/attached-computers.d.ts +16 -5
- package/dist/workspace/attached-computers.js +25 -9
- package/dist/workspace/computer-deps.js +1 -1
- package/dist/workspace/computer-for-claim.d.ts +7 -3
- package/dist/workspace/computer-for-claim.js +119 -22
- package/dist/workspace/computer-hold.js +2 -1
- package/dist/workspace/computer-kept.d.ts +11 -1
- package/dist/workspace/computer-kept.js +24 -3
- package/dist/workspace/resolve-computer.d.ts +33 -19
- package/dist/workspace/resolve-computer.js +172 -64
- package/dist/workspace/revision-skills.d.ts +1 -2
- package/dist/workspace/revision-skills.js +3 -4
- package/dist/workspace/run-workspace.d.ts +1 -1
- package/dist/workspace/run-workspace.js +5 -0
- package/dist/workspace/synced-folder.d.ts +85 -0
- package/dist/workspace/synced-folder.js +230 -0
- package/dist/workspace/workspace.d.ts +4 -0
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -10
- package/templates/README.md +8 -8
- package/templates/inbound-lead/agent.md +5 -4
- package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
- package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
- package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
- package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
- package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
- package/templates/inbound-lead/lead.schema.json +6 -5
- package/templates/inbound-lead/rules/RUBRIC.md +4 -4
- package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
- package/templates/kyb/agent.md +2 -1
- package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
- package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
- package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
- package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
- package/templates/release-notes/evals/learned-style/case.yaml +4 -4
- package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
- package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
- package/templates/release-notes/evals/learned-style/prompt.md +1 -1
- package/dist/console/assets/index-BUktRvS-.css +0 -1
- package/dist/console/assets/index-nB9kTfIN.js +0 -711
- package/dist/learn/grade.d.ts +0 -138
- package/dist/learn/grade.js +0 -682
- package/dist/learn/notes.d.ts +0 -81
- package/dist/learn/notes.js +0 -178
- package/dist/learn/reflect.d.ts +0 -77
- package/dist/learn/reflect.js +0 -228
- package/dist/learn/relevance.d.ts +0 -24
- package/dist/learn/relevance.js +0 -91
- package/dist/learn/seed-check.d.ts +0 -74
- package/dist/learn/seed-check.js +0 -103
- package/dist/learning-batch.d.ts +0 -291
- package/dist/learning-batch.js +0 -600
- package/dist/learning-evals.d.ts +0 -35
- package/dist/learning-evals.js +0 -99
- package/dist/native/context.d.ts +0 -6
- package/dist/native/context.js +0 -22
- package/dist/propose.d.ts +0 -52
- package/dist/propose.js +0 -224
- package/dist/runner/learning-ask.d.ts +0 -47
- package/dist/runner/learning-ask.js +0 -128
- package/dist/runner/learning-dispatch.d.ts +0 -45
- package/dist/runner/learning-dispatch.js +0 -99
- package/dist/runner/learning-factory.d.ts +0 -16
- package/dist/runner/learning-factory.js +0 -50
- package/dist/runner/learning-loop.d.ts +0 -273
- package/dist/runner/learning-loop.js +0 -521
- package/dist/usage.d.ts +0 -11
- package/dist/usage.js +0 -11
- package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
- package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
- package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
- package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
- package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
- package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
- package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
package/dist/learning.js
CHANGED
|
@@ -1,55 +1,27 @@
|
|
|
1
1
|
// Learning: an agent whose definition says `learning: true` keeps a folder of what it learned, and each of its runs
|
|
2
|
-
// starts from it. The folder is `learning/` in the agent's folder on the disk (files.ts agentFolderOf)
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
2
|
+
// starts from it. The folder is `learning/` in the agent's folder on the disk (files.ts agentFolderOf): plain git with
|
|
3
|
+
// main checked out, reached through its Workspace (learningWorkspace). It holds two things:
|
|
4
|
+
// - corrections.md, its owner's standing corrections (learn/corrections.ts): every run of the agent is served it whole
|
|
5
|
+
// under `# Learned` (laidLearned), cut of the host's secrets;
|
|
6
|
+
// - workflows/<name>/, the workflows its runs compiled (learn/compile.ts): a run whose input one fits is answered by it
|
|
7
|
+
// (runner/workflow-first.ts).
|
|
8
|
+
// Every write to main is one exec of the learning script (learn/folder.ts), which also writes `.agentrun/snapshot.json`,
|
|
9
|
+
// what the routes and a run's seed read: no git runs in this process. When a run of such an agent is made, the server
|
|
10
|
+
// makes the folder if it is absent (the script's init: one empty host commit on main) and reads main's snapshot; the
|
|
11
|
+
// run's launch keeps the commit it started from and its # Learned text, so its runner reads no folder.
|
|
8
12
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// copy's too. The run's process reads that pin (withLearned, for app.ts) and never runs git, so it may run anywhere its
|
|
16
|
-
// directory is. When the run ends the runner asks it one more turn, which answers with the notes it would keep
|
|
17
|
-
// (runner/learning-end.ts, reflect-hook.ts); its notes are checked (propose.ts), then graded and merged into main by the
|
|
18
|
-
// runner's learning run (runner/learning-loop.ts). The copy in the run's work is only for reading: nothing the run
|
|
19
|
-
// writes there is ever kept.
|
|
20
|
-
//
|
|
21
|
-
// The skills the agent wrote are in the folder too, one folder each under `skills/` (SKILLS_DIR). A run
|
|
22
|
-
// gets those of its commit beside its revision's own, in `work/.agents/skills/`, read as its notes are: a
|
|
23
|
-
// skill named like one of the revision's is the revision's, and the learned ones fit their own budget
|
|
24
|
-
// (LEARNED_SKILLS_BUDGET) within what the revision's leave of the adapters' cap on a run's skills, so they never push the
|
|
25
|
-
// revision's out of a harness's own loading (skills.ts splitSkills). One that does not fit is left out, and the pin
|
|
26
|
-
// names it. A link at `skills/` or anywhere under it is never followed: the skill it is in is left out, with a warning.
|
|
27
|
-
//
|
|
28
|
-
// The host never follows a symbolic link into the folder (learn/folder.ts learningWorkspace, and the script): a link at
|
|
29
|
-
// the agent's home, at its `learning/` or at `learning/.git`, or a `MEMORY.md` that is one when the script writes it, is
|
|
30
|
-
// refused, and the run starts with nothing learned. Its proposal's check reads the folder the same way, and a link
|
|
31
|
-
// there drops what the run learned. A person's reads and writes (learning-routes.ts) check it the same
|
|
32
|
-
// way (learn/folder.ts learningWorkspace), and a link there is an error.
|
|
33
|
-
import { lstat, readFile, rm } from "node:fs/promises";
|
|
34
|
-
import { join, posix } from "node:path";
|
|
35
|
-
import { SKILL_BYTES } from "@parcha/agentrun-kernel";
|
|
36
|
-
import { bm25, queryOf } from "./learn/relevance.js";
|
|
37
|
-
import { decodedStrings, frontMatter, INDEX_BYTES, INDEX_FILE, INDEX_LINES, learnedStatus, loadedIndex, NOTE_FILE, parseNoteFile, learnedSecretShapes, redactLearned, servedSkill, servedSkills, SKILL_NAME, SKILLS_DIR, valueMatcher, WORKFLOW_NAME, WORKFLOWS_DIR } from "./learn/index.js";
|
|
38
|
-
import { learningInit, learningWorkspace, readSnapshot } from "./learn/folder.js";
|
|
39
|
-
import { readSeed as readFolderSeed } from "./learn/seed-check.js";
|
|
40
|
-
import { agentFolderOf, serialBy, writeDurable } from "./files.js";
|
|
13
|
+
// The host never follows a symbolic link into the folder (learn/folder.ts learningWorkspace, and the script): a link
|
|
14
|
+
// there is refused, and the run starts with nothing learned.
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { CORRECTIONS_FILE, decodedStrings, frontMatter, learnedSecretShapes, redactLearned, valueMatcher } from "./learn/index.js";
|
|
17
|
+
import { folderAt, learningInit, learningWorkspace, readLearning } from "./learn/folder.js";
|
|
18
|
+
import { agentFolderOf, serialBy } from "./files.js";
|
|
41
19
|
import { withoutLoopbackAddresses } from "./key-refusal.js";
|
|
42
20
|
import { DETAIL_MAX, LIST_DETAIL, said, short, wordsOf } from "./learning-words.js";
|
|
43
|
-
|
|
44
|
-
import { isObject } from "./validate.js";
|
|
45
|
-
import { isWorkspaceError, WorkspaceError, workspaceAt } from "./workspace/index.js";
|
|
21
|
+
export { learnsOf } from "./learning-setting.js";
|
|
46
22
|
/** The server's learning switch (`--learning`, AGENTRUN_SERVE_LEARNING; capabilities.features.learning). `on`, the
|
|
47
23
|
* default: a run learns when its agent's definition says `learning: true`. `off`: no run this server starts does. */
|
|
48
24
|
export const LEARNING_MODES = ["on", "off"];
|
|
49
|
-
/** Whether a revision's `learning` (its definition's: true, false, "notes" or the loop's numbers, learning-setting.ts) has
|
|
50
|
-
* its runs learn: true (or an object) learns notes, skills and workflows; "notes" learns notes and skills alone, never
|
|
51
|
-
* given a learned workflow nor asked to draft one. */
|
|
52
|
-
export const learnsOf = (learning) => learnsWorkflowsOf(learning) || learning === "notes";
|
|
53
25
|
export const LEARNING_ENV = "AGENTRUN_SERVE_LEARNING";
|
|
54
26
|
/** The switch: `given`, else AGENTRUN_SERVE_LEARNING, else on. Any other value is an error, and the server does not start. */
|
|
55
27
|
export function learningOf(env, given) {
|
|
@@ -58,89 +30,51 @@ export function learningOf(env, given) {
|
|
|
58
30
|
throw new Error(`learning is on or off (--learning, ${LEARNING_ENV}), not ${value}`);
|
|
59
31
|
return value;
|
|
60
32
|
}
|
|
61
|
-
/** How much of what a run learned its instructions hold under `# Learned` by default, in UTF-8 bytes (learnedParts): its
|
|
62
|
-
* notes whole while they fit, else the notes that bear most on the run whole while they fit, then a line for each of
|
|
63
|
-
* the next while they fit; the rest are left out, and the run's log names them. */
|
|
64
|
-
export const LEARNING_BUDGET = 8000;
|
|
65
|
-
export const LEARNING_BUDGET_ENV = "AGENTRUN_SERVE_LEARNING_BUDGET";
|
|
66
|
-
/** The learning budget: `given` (`--learning-budget`), else AGENTRUN_SERVE_LEARNING_BUDGET, else LEARNING_BUDGET. Any
|
|
67
|
-
* value that is not a whole number of bytes, 0 or more, is an error, and the server does not start. */
|
|
68
|
-
export function learningBudgetOf(env, given) {
|
|
69
|
-
const named = env[LEARNING_BUDGET_ENV];
|
|
70
|
-
const value = given ?? (named ? (/^\d+$/.test(named) ? Number(named) : Number.NaN) : LEARNING_BUDGET);
|
|
71
|
-
if (!Number.isSafeInteger(value) || value < 0)
|
|
72
|
-
throw new Error(`the learning budget is a whole number of bytes, 0 or more (--learning-budget, ${LEARNING_BUDGET_ENV}), not ${given ?? named}`);
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
33
|
/** Where a run's learning stands (its `learning`, runs.ts): `seeded` once it starts from what its agent learned,
|
|
76
|
-
* `reflecting` while its end-of-run
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
export const LEARNING_STATES = ["seeded", "reflecting", "
|
|
80
|
-
export const LEARNING_REASONS = ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "
|
|
34
|
+
* `reflecting` while its end-of-run step runs (its compile turn, its owner's correction), `merged` once what it learned
|
|
35
|
+
* is on main, or `dropped` with a reason from the closed list. A learning fault never refuses a run: it starts with
|
|
36
|
+
* nothing learned, `seed_failed`. */
|
|
37
|
+
export const LEARNING_STATES = ["seeded", "reflecting", "merged", "dropped"];
|
|
38
|
+
export const LEARNING_REASONS = ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "reflect_failed", "empty", "checker", "use"];
|
|
81
39
|
/** A run's own `learning` on its start (POST /agents/{agent_id}/runs): it starts from what its agent learned, as a run that
|
|
82
|
-
* learns does, and keeps nothing (no
|
|
83
|
-
*
|
|
40
|
+
* learns does, and keeps nothing (no compile, no correction, at any end), so runs of one agent read one learned state
|
|
41
|
+
* however many run at once. Its learning ends `dropped`, reason `use`. */
|
|
84
42
|
export const RUN_LEARNING_USE = "use";
|
|
85
|
-
/** The variable that names the grader's model, as `--grade-model` does. */
|
|
86
|
-
export const GRADE_MODEL_ENV = "AGENTRUN_SERVE_GRADE_MODEL";
|
|
87
|
-
/** The model this server grades on, by name: `given` (`--grade-model`), else GRADE_MODEL_ENV, else its strong tier
|
|
88
|
-
* (AGENT_MODEL_STRONG); undefined when it names none, and each run is graded on its agent's own model (runner/learning-loop.ts). A
|
|
89
|
-
* name is a model as the strong tier takes one: sent as it is to a gateway, a provider/model-id otherwise. */
|
|
90
|
-
export const gradeModelOf = (env, given) => given || env[GRADE_MODEL_ENV] || env.AGENT_MODEL_STRONG || undefined;
|
|
91
43
|
/** What a name holding a secret reads: learn/secrets.ts redactSecrets' mark, the name's whole, never a part of it. */
|
|
92
44
|
const REDACTED = "[redacted]";
|
|
93
45
|
/** The host's secrets as a reader cuts them: a text holding a value or a shape is cut of it (whole when long:
|
|
94
|
-
* learn/secrets.ts redactLearned, a bank's numbers too), and a file's name holding one reads REDACTED whole
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* workflow.json's, so their label, line ends and JSON stay. `local`: kept, for what a run itself is given (its .learning/ notes, runner/learning-seed.ts, and its
|
|
99
|
-
* # Learned section, server.ts's learning start), where an address on the run's own machine is the note's to give. */
|
|
46
|
+
* learn/secrets.ts redactLearned, a bank's numbers too), and a file's name holding one reads REDACTED whole; any other
|
|
47
|
+
* text or name is shown as it is. A text served has no loopback address (key-refusal.ts withoutLoopbackAddresses): the
|
|
48
|
+
* host's own, never a person's. `local`: kept, for what a run itself is given (its # Learned section), where an address
|
|
49
|
+
* on the run's own machine is the owner's to give. */
|
|
100
50
|
export function secretsShown(values, { local = false } = {}) {
|
|
101
51
|
const matches = valueMatcher(values);
|
|
102
|
-
// Every shape learning refuses, a bank's numbers beside their words too (learnedSecretShapes)
|
|
103
|
-
// for one never shows it here, though a run's own view may show its user an account number.
|
|
52
|
+
// Every shape learning refuses, a bank's numbers beside their words too (learnedSecretShapes).
|
|
104
53
|
const holds = (text) => matches(text) || learnedSecretShapes(text).length > 0;
|
|
105
54
|
const cut = (text) => (holds(text) ? redactLearned(text, values) : text);
|
|
106
55
|
return { holds, text: local ? cut : (text) => withoutLoopbackAddresses(cut(text)), name: (name) => (holds(name) ? REDACTED : name) };
|
|
107
56
|
}
|
|
108
57
|
const isText = (value) => typeof value === "string";
|
|
109
|
-
/** The
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const started = refs(given);
|
|
116
|
-
const read = settled ? refs(opened) : [];
|
|
117
|
-
const added = state === "merged" && Array.isArray(learned) ? learned.filter((note) => isText(note?.file) && isText(note.name) && Array.isArray(note.supersedes) && note.supersedes.every(isText)).map(({ file, name, supersedes }) => ({ file: shown.name(file), name: shown.text(name), supersedes: supersedes.map(shown.name) })) : [];
|
|
118
|
-
const waiting = state === "merged" && Array.isArray(pending) ? pending.filter((note) => isText(note?.file) && isText(note.name)).map(({ file, name }) => ({ file: shown.name(file), name: shown.text(name) })) : [];
|
|
119
|
-
const files = (list) => (state === "merged" && Array.isArray(list) ? list.filter((item) => isText(item?.file) && isText(item.name)).map(({ file, name, pending: waits }) => ({ file: shown.name(file), name: shown.name(name), ...(waits === true ? { pending: true } : {}) })) : []);
|
|
120
|
-
const [kept, drafts] = [files(skills), files(workflows)];
|
|
121
|
-
return { ...(started.length ? { given: started } : {}), ...(read.length ? { read } : {}), ...(added.length ? { learned: added } : {}), ...(waiting.length ? { pending: waiting } : {}), ...(kept.length ? { skills: kept } : {}), ...(drafts.length ? { workflows: drafts } : {}) };
|
|
58
|
+
/** The workflow draft a run merged, as a view or a list shows it (each `pending` while it waits), each name cut of the
|
|
59
|
+
* host's secrets (`shown`), only when it holds one. The stored ones are read as untrusted: an entry out of shape is
|
|
60
|
+
* left out. */
|
|
61
|
+
export function filesView({ state, workflows }, shown) {
|
|
62
|
+
const drafts = state === "merged" && Array.isArray(workflows) ? workflows.filter((item) => isText(item?.file) && isText(item.name)).map(({ file, name, pending: waits }) => ({ file: shown.name(file), name: shown.name(name), ...(waits === true ? { pending: true } : {}) })) : [];
|
|
63
|
+
return drafts.length ? { workflows: drafts } : {};
|
|
122
64
|
}
|
|
123
|
-
/** Whether a run's learning held anything for its owner's OK: a merge with a held
|
|
124
|
-
*
|
|
125
|
-
export const holdsForOk = (learning) => learning?.state === "merged" && (
|
|
126
|
-
/** A merged run's learning as it reads now: what it held for the owner's OK checked against each
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* what the merge wrote; only its reads change. No status (the folder unread): as written. */
|
|
65
|
+
/** Whether a run's learning held anything for its owner's OK: a merge with a held draft. Only such a run's reads check
|
|
66
|
+
* main (currentLearning). */
|
|
67
|
+
export const holdsForOk = (learning) => learning?.state === "merged" && (Array.isArray(learning.workflows) ? learning.workflows : []).some((item) => item?.pending);
|
|
68
|
+
/** A merged run's learning as it reads now: what it held for the owner's OK checked against each draft's status on main
|
|
69
|
+
* now (`status`, by its workflow.json). Still pending, it waits; approved (active), it is learned, as a merge that held
|
|
70
|
+
* nothing says it; retired or gone, it is no longer said. No status (the folder unread): as written. */
|
|
130
71
|
export function currentLearning(learning, status) {
|
|
131
|
-
if (!status || learning.state !== "merged")
|
|
72
|
+
if (!status || learning.state !== "merged" || !Array.isArray(learning.workflows))
|
|
132
73
|
return learning;
|
|
133
74
|
const of = (file) => (typeof file === "string" ? status.get(file)?.status : undefined);
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const files = (list) => (Array.isArray(list) ? list.flatMap((item) => (!item?.pending || of(item.file) === "pending" ? [item] : of(item.file) === "active" ? [{ file: item.file, name: item.name }] : [])) : list);
|
|
138
|
-
const { pending: _pending, learned, skills, workflows, ...rest } = learning;
|
|
139
|
-
const waits = held.filter((note) => of(note?.file) === "pending");
|
|
140
|
-
const kept = [...(Array.isArray(learned) ? learned : []), ...approved];
|
|
141
|
-
const [madeSkills, madeDrafts] = [files(skills), files(workflows)];
|
|
142
|
-
const gone = !kept.length && !waits.length && !madeSkills?.length && !madeDrafts?.length;
|
|
143
|
-
return { ...rest, ...(gone ? { detail: said.retiredSince } : {}), ...(kept.length ? { learned: kept } : learned ? { learned } : {}), ...(waits.length ? { pending: waits } : {}), ...(madeSkills ? { skills: madeSkills } : {}), ...(madeDrafts ? { workflows: madeDrafts } : {}) };
|
|
75
|
+
const workflows = learning.workflows.flatMap((item) => (!item?.pending || of(item.file) === "pending" ? [item] : of(item.file) === "active" ? [{ file: item.file, name: item.name }] : []));
|
|
76
|
+
const gone = !workflows.length && !learning.corrections;
|
|
77
|
+
return { ...learning, ...(gone ? { detail: said.retiredSince } : {}), workflows };
|
|
144
78
|
}
|
|
145
79
|
/** A run's learning in a person's words as a read shows them (learning-words.ts wordsOf): each part cut of the host's
|
|
146
80
|
* secrets as they are now (`shown`) first, then the words cut short to `max`, so a secret is never shown in part. */
|
|
@@ -148,33 +82,21 @@ export const detailShown = (learning, shown, max = DETAIL_MAX) => {
|
|
|
148
82
|
const words = wordsOf(learning, shown.text);
|
|
149
83
|
return words === null ? null : short(words, max);
|
|
150
84
|
};
|
|
151
|
-
/** A run's learning as its view shows it: the state, why in a person's words (detailShown),
|
|
152
|
-
*
|
|
153
|
-
export const learningView = (learning, shown
|
|
85
|
+
/** A run's learning as its view shows it: the state, why in a person's words (detailShown), when, and the draft it
|
|
86
|
+
* merged (filesView). */
|
|
87
|
+
export const learningView = (learning, shown) => {
|
|
154
88
|
const { state, reason, at } = learning;
|
|
155
|
-
return { state, reason: reasonShown(reason, shown), detail: detailShown(learning, shown), at, ...
|
|
89
|
+
return { state, reason: reasonShown(reason, shown), detail: detailShown(learning, shown), at, ...filesView(learning, shown) };
|
|
156
90
|
};
|
|
157
91
|
/** A learning's `reason` as it is served: one of its words as the server writes them, read back from a kept row, which is
|
|
158
92
|
* untrusted, so cut of the host's secrets as every text is. */
|
|
159
93
|
export const reasonShown = (reason, shown) => (typeof reason === "string" ? shown.text(reason) : (reason ?? null));
|
|
160
94
|
/** A run's learning as its view shows it, in short for a list: all of it but its time, its detail cut short. */
|
|
161
|
-
export function shortOf({ state, reason, detail, at: _, ...
|
|
162
|
-
return { state, reason, detail: detail === null ? null : short(detail, LIST_DETAIL), ...
|
|
95
|
+
export function shortOf({ state, reason, detail, at: _, ...rest }) {
|
|
96
|
+
return { state, reason, detail: detail === null ? null : short(detail, LIST_DETAIL), ...rest };
|
|
163
97
|
}
|
|
164
98
|
/** The label of the end-of-run turn's timeline rows, and the kind and label of a run's learning row. */
|
|
165
99
|
export const LEARNING_LABEL = "learning";
|
|
166
|
-
/** A run's proposal, in its directory (propose.ts): the seed it was checked against, each new note's file and text, its
|
|
167
|
-
* skill's and workflow draft's files by path and text (`files`: a SKILL.md, a workflow.json and its README.md; absent
|
|
168
|
-
* when it gives none), and what the user said in the run as the grader reads it (each message cut of the host's
|
|
169
|
-
* secrets, then shortened to its head and tail, shortMessage; absent in a proposal kept before the grader read them). */
|
|
170
|
-
export const PROPOSAL = "learning-proposal.json";
|
|
171
|
-
/** A run's pin, in its directory: written once, when the run is made, and never changed by a resume. */
|
|
172
|
-
export const LEARNING_PIN = "learning.json";
|
|
173
|
-
/** Where a run finds its agent's notes: in its work, a copy the run may read and nothing reads back. */
|
|
174
|
-
export const LEARNED_DIR = ".learning";
|
|
175
|
-
/** Where a run keeps the learned workflows its pin names, whole: in the run's directory, beside its pin, never in its
|
|
176
|
-
* work, so nothing the run does changes what its `learned_workflow` tool runs. */
|
|
177
|
-
export const LEARNED_WORKFLOWS_FILE = "learned-workflows.json";
|
|
178
100
|
/** The most bytes the `learned_workflow` tool's description gives its workflows, each one's line (workflowLine)
|
|
179
101
|
* together: every harness sends the description with each request, so it never grows with what an agent learned. */
|
|
180
102
|
export const LEARNED_WORKFLOWS_BUDGET = 8 * 1024;
|
|
@@ -237,447 +159,83 @@ function compactSchema(schema, schemas, depth = 0, defs = {}) {
|
|
|
237
159
|
return s.enum.map((value) => JSON.stringify(value)).join("|");
|
|
238
160
|
return typeof s.type === "string" ? s.type : Array.isArray(s.type) ? s.type.join("|") : "any";
|
|
239
161
|
}
|
|
240
|
-
/** The most bytes of learned skills one run gets, every file of each together. They get no more than what the
|
|
241
|
-
* revision's own skills leave of the adapters' cap on a run's skills in all (SKILL_BYTES): over that cap an adapter
|
|
242
|
-
* refuses the whole set, and every skill, the revision's too, would be inlined in the prompt (skills.ts splitSkills). */
|
|
243
|
-
export const LEARNED_SKILLS_BUDGET = 256 * 1024;
|
|
244
|
-
/** Where a run's harness finds its skills in its work: the revision's, and the learned ones laid beside them. */
|
|
245
|
-
const RUN_SKILLS = [".agents", "skills"];
|
|
246
|
-
/** A file's path inside a learned skill, as every adapter takes one (agentrun-kernel skillProblems). */
|
|
247
|
-
const SKILL_FILE_PATH = /^(?:[A-Za-z0-9_][A-Za-z0-9._-]{0,99}\/){0,4}[A-Za-z0-9_][A-Za-z0-9._-]{0,99}$/;
|
|
248
162
|
/** The agent's learning folder: `learning/` in its folder on the disk, which only the host writes. The folder is files.ts
|
|
249
163
|
* agentFolderOf (server-fs.ts agentDiskDir), so two ids that differ only in case never share one. */
|
|
250
164
|
export const learningRepo = (data, agentId) => join(agentFolderOf(data, agentId), "learning");
|
|
251
165
|
const queues = new Map();
|
|
252
166
|
/** Runs `work` once every call queued before it on the agent's folder has finished: one at a time per agent, in this
|
|
253
167
|
* process. Everything that touches an agent's folder goes through it, so two runs made at once never both make it. */
|
|
254
|
-
export const withAgentRepo = (data, agentId, work) =>
|
|
255
|
-
function queued(key, work) {
|
|
256
|
-
return serialBy(queues, key, work);
|
|
257
|
-
}
|
|
168
|
+
export const withAgentRepo = (data, agentId, work) => serialBy(queues, learningRepo(data, agentId), work);
|
|
258
169
|
/** Agent `agentId`'s learning folder (learn/folder.ts learningWorkspace), in this server's place: Mode 1's local folder
|
|
259
170
|
* under `data`. The one point that names the place; its `end()` is called in a finally after every use. */
|
|
260
171
|
export const learningFolderOf = (data, agentId) => learningWorkspace({ mode: "local", agentId, data });
|
|
261
|
-
/**
|
|
262
|
-
*
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
export async function seedFrom(commit, read, { order, warn } = {}) {
|
|
269
|
-
const got = await read(commit);
|
|
270
|
-
if (!got.ok)
|
|
271
|
-
throw new Error(`the learning folder's read at ${commit ?? "its start"} is ${got.why}`);
|
|
272
|
-
for (const { skill, why } of got.refused)
|
|
273
|
-
warn?.(`starts without the skill ${skill} its agent learned: ${why}`);
|
|
274
|
-
const seed = { sha: commit, notes: {}, files: [], skipped: [], recent: [], skills: [] };
|
|
275
|
-
const skills = new Map();
|
|
276
|
-
const flows = new Map();
|
|
277
|
-
for (const { path, text } of got.files) {
|
|
278
|
-
if (path === INDEX_FILE)
|
|
279
|
-
seed.index = text;
|
|
280
|
-
else if (NOTE_FILE.test(path))
|
|
281
|
-
seed.notes[path] = text;
|
|
282
|
-
else {
|
|
283
|
-
const [top, name = "", ...rest] = path.split("/");
|
|
284
|
-
const file = rest.join("/");
|
|
285
|
-
if (top === SKILLS_DIR && SKILL_NAME.test(name) && SKILL_FILE_PATH.test(file))
|
|
286
|
-
(skills.get(name) ?? skills.set(name, {}).get(name))[file] = text;
|
|
287
|
-
else if (top === WORKFLOWS_DIR && WORKFLOW_NAME.test(name) && (file === "README.md" || file === "workflow.json"))
|
|
288
|
-
(flows.get(name) ?? flows.set(name, {}).get(name))[file] = text;
|
|
289
|
-
else
|
|
290
|
-
seed.skipped.push(path);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
const noteAt = new Map((order?.notes ?? []).map(({ file, order: at }) => [file, at]));
|
|
294
|
-
const skillAt = new Map((order?.skills ?? []).map(({ name, order: at }) => [name, at]));
|
|
295
|
-
const by = (at) => (a, b) => (at.get(a) ?? Number.POSITIVE_INFINITY) - (at.get(b) ?? Number.POSITIVE_INFINITY) || (a < b ? -1 : a > b ? 1 : 0);
|
|
296
|
-
seed.recent = Object.keys(seed.notes).sort(by(noteAt));
|
|
297
|
-
// Every entry at the seed's top, as main holds it (the read's `held`, a pending, superseded or retired note's file
|
|
298
|
-
// among them, so servedIndex leaves its line out of a run's index), beside what the read gave.
|
|
299
|
-
seed.files = [...new Set([...(seed.index === undefined ? [] : [INDEX_FILE]), ...Object.keys(seed.notes), ...got.held, ...(skills.size ? [SKILLS_DIR] : [])])];
|
|
300
|
-
seed.skills = [...skills.keys()].sort(by(skillAt)).map((name) => {
|
|
301
|
-
const files = skills.get(name);
|
|
302
|
-
// What the run is given, counted as given: a SKILL.md as servedSkill writes it (its name and description alone, each
|
|
303
|
-
// quoted where YAML needs it), which can be a few bytes more than the blob, every other file as it is.
|
|
304
|
-
const bytes = Object.entries(files).reduce((sum, [path, text]) => sum + Buffer.byteLength(path === "SKILL.md" ? servedSkill(text) : text, "utf8"), 0);
|
|
305
|
-
return bytes > LEARNED_SKILLS_BUDGET ? { name, bytes } : { name, bytes, files };
|
|
306
|
-
});
|
|
307
|
-
// The active learned workflows, by name, each with its workflow.json whole. The read gives an active one's README.md
|
|
308
|
-
// and workflow.json; its front matter is read again here, by the one reader of a learned item's status
|
|
309
|
-
// (learn/skills.ts frontMatter): one that does not read one way could hold a status another reader sees, so it is
|
|
310
|
-
// never read as active, and is named in `skipped`.
|
|
311
|
-
seed.workflows = [];
|
|
312
|
-
for (const name of [...flows.keys()].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
313
|
-
const { "README.md": readme, "workflow.json": workflow } = flows.get(name);
|
|
314
|
-
if (readme === undefined || workflow === undefined)
|
|
172
|
+
/** The seed of `snapshot` (undefined: no main yet), each body read by `text`: corrections.md, and each active draft's
|
|
173
|
+
* README.md and workflow.json. A draft whose README's front matter does not read one way is never read as active. */
|
|
174
|
+
async function seedIn(sha, snapshot, text) {
|
|
175
|
+
const corrections = snapshot?.corrections ? await text(CORRECTIONS_FILE, snapshot.corrections.oid) : "";
|
|
176
|
+
const workflows = [];
|
|
177
|
+
for (const item of snapshot?.workflows ?? []) {
|
|
178
|
+
if (item.status !== "active")
|
|
315
179
|
continue;
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
180
|
+
const readme = item.files.find((file) => file.path.endsWith("/README.md"));
|
|
181
|
+
const json = item.files.find((file) => file.path.endsWith("/workflow.json"));
|
|
182
|
+
if (!readme || !json)
|
|
319
183
|
continue;
|
|
320
|
-
|
|
321
|
-
if (
|
|
184
|
+
const front = frontMatter(await text(readme.path, readme.oid));
|
|
185
|
+
if (!front)
|
|
322
186
|
continue;
|
|
323
|
-
|
|
187
|
+
workflows.push({ name: item.name, description: front.get("description") ?? "", workflow: await text(json.path, json.oid) });
|
|
324
188
|
}
|
|
325
|
-
return
|
|
189
|
+
return { sha, corrections, workflows };
|
|
326
190
|
}
|
|
327
|
-
/** What seedFrom reads through the agent's learning folder: the script's read at a commit, or the snapshot and its
|
|
328
|
-
* bodies when the commit is main's (learn/seed-check.ts readSeed). No git in this process. */
|
|
329
|
-
export const readerOf = (ws) => async (commit) => {
|
|
330
|
-
const got = await readFolderSeed(ws, commit);
|
|
331
|
-
return got.ok ? { ok: true, files: got.files, refused: got.refused, held: got.held } : { ok: false, why: got.reason };
|
|
332
|
-
};
|
|
333
|
-
/** The places a seed can't read from its files alone, from the folder's snapshot: each note's and each skill's. */
|
|
334
|
-
const orderOf = (snapshot) => snapshot ? { notes: snapshot.notes.map(({ file, order }) => ({ file, order })), skills: snapshot.skills.map(({ name, order }) => ({ name, order })) } : undefined;
|
|
335
191
|
/** The seed of a run of agent `agentId` about to be made: main's commit, the folder made first when it is absent (the
|
|
336
|
-
* learning script's init), and what main holds for a run (
|
|
337
|
-
*
|
|
338
|
-
|
|
339
|
-
export function seedOf(data, agentId, warn) {
|
|
192
|
+
* learning script's init), and what main holds for a run (seedIn). In the agent's queue. No git in this process: the
|
|
193
|
+
* folder's own script and file API. */
|
|
194
|
+
export function seedOf(data, agentId) {
|
|
340
195
|
return withAgentRepo(data, agentId, async () => {
|
|
341
196
|
const { workspace, end } = learningFolderOf(data, agentId);
|
|
342
197
|
try {
|
|
343
|
-
|
|
344
|
-
return await
|
|
198
|
+
await learningInit(workspace);
|
|
199
|
+
return await readLearning(workspace, async ({ snapshot, body }) => seedIn(snapshot?.main ?? null, snapshot, body));
|
|
345
200
|
}
|
|
346
201
|
finally {
|
|
347
202
|
await end();
|
|
348
203
|
}
|
|
349
204
|
});
|
|
350
205
|
}
|
|
351
|
-
/** What commit `
|
|
352
|
-
*
|
|
353
|
-
export
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
export const activeOf = (seed) => Object.entries(seed.notes).flatMap(([file, text]) => {
|
|
366
|
-
const note = parseNoteFile(file, text);
|
|
367
|
-
return Array.isArray(note) ? [] : [note];
|
|
368
|
-
});
|
|
369
|
-
/** Test seams, tests only: once a part of an agent's learning folder is made (before its check), before a run's
|
|
370
|
-
* learned files are written, once its pin is in place, before what its end-of-run turn wrote is checked, as a run that
|
|
371
|
-
* learns opens again for a follow-up, and as each git call starts (its arguments). */
|
|
372
|
-
export const learningTestHooks = {};
|
|
373
|
-
/** `text` cut of the host's secrets (`shown`) a line at a time: the index, which may be longer than a whole text is
|
|
374
|
-
* read (learn/secrets.ts PRINT_MAX). */
|
|
375
|
-
const linesShown = (text, shown) => text.split("\n").map(shown.text).join("\n");
|
|
376
|
-
/** Every link's target on an index line: `<…>` or up to a space or `)`. */
|
|
377
|
-
const LINKS = /\]\(\s*(?:<([^>\n]*)>|([^)\s]+))/g;
|
|
378
|
-
/** The files an index line links to, each as the folder's path to it: its #fragment dropped, percent-decoded, and
|
|
379
|
-
* normalized at the folder's top, so `./x.md`, `/x.md` and `a/../x.md` are all x.md. */
|
|
380
|
-
const linkedFiles = (entry) => [...entry.matchAll(LINKS)].map((link) => {
|
|
381
|
-
const target = (link[1] ?? link[2]).split("#")[0];
|
|
382
|
-
let decoded = target;
|
|
383
|
-
try {
|
|
384
|
-
decoded = decodeURIComponent(target);
|
|
385
|
-
}
|
|
386
|
-
catch {
|
|
387
|
-
// Not percent-encoded right: taken as written.
|
|
388
|
-
}
|
|
389
|
-
return posix.normalize(decoded).replace(/^(?:\.?\/)+/, "");
|
|
390
|
-
});
|
|
391
|
-
/** `seed`'s index as a run is given it: each line with a link, any of them in any spelling (linkedFiles), to a file main
|
|
392
|
-
* holds that is no active note (a pending, superseded or retired one, or one not read) left out, so it is never served
|
|
393
|
-
* or counted. The host's index names none; a hand edit might. A line naming only files main does not hold stays. */
|
|
394
|
-
function servedIndex(seed) {
|
|
395
|
-
if (seed.index === undefined)
|
|
396
|
-
return undefined;
|
|
397
|
-
const held = new Set(seed.files);
|
|
398
|
-
return seed.index.split("\n").filter((entry) => !linkedFiles(entry).some((file) => held.has(file) && !Object.hasOwn(seed.notes, file))).join("\n");
|
|
399
|
-
}
|
|
400
|
-
/** Lays `seed`'s index and active notes in the run's `.learning/`, through its Workspace, made fresh: whatever was there
|
|
401
|
-
* goes. The index names the active notes alone (servedIndex), and each file is cut of the host's secrets (`shown`), the
|
|
402
|
-
* index a line at a time. */
|
|
403
|
-
export async function layLearned(work, seed, shown) {
|
|
404
|
-
await work.remove(LEARNED_DIR, { recursive: true, force: true });
|
|
405
|
-
await work.mkdir(LEARNED_DIR);
|
|
406
|
-
const files = Object.entries(seed.notes).map(([file, text]) => [file, shown.text(text)]);
|
|
407
|
-
const index = servedIndex(seed);
|
|
408
|
-
if (index !== undefined)
|
|
409
|
-
files.unshift([INDEX_FILE, linesShown(index, shown)]);
|
|
410
|
-
for (const [file, text] of files)
|
|
411
|
-
await work.write(`${LEARNED_DIR}/${file}`, text);
|
|
412
|
-
}
|
|
413
|
-
/** All of `candidates` fit whole: every note within the budget, and an index that names no note main does not hold. */
|
|
414
|
-
const allFit = ({ notes, indexComplete, budget }) => indexComplete && notes.reduce((sum, { bytes }) => sum + bytes, 0) <= budget;
|
|
415
|
-
/** Past the budget: the first `bearing` notes in `ranked`'s order (those that bear on the run), each whole while it fits
|
|
416
|
-
* what is left of the budget (one that does not is passed over for the next), then one rule line for each of the rest,
|
|
417
|
-
* in the same order, while it fits what the whole notes leave and no more than a run reads of the index (INDEX_LINES,
|
|
418
|
-
* INDEX_BYTES), the section's own lines counted. Whole notes come first: lines never take the room a whole note fits in, so past ~50 notes a run still
|
|
419
|
-
* reads the ones that bear on it whole (Run B, 11-12: past 8 KB of lines every run got lines alone). The rest are left
|
|
420
|
-
* out, and learnedParts names them. */
|
|
421
|
-
const withinBudget = (ranked, budget, read = true, bearing = ranked.length) => {
|
|
422
|
-
const whole = [];
|
|
423
|
-
// The section's own lines (its intro, the read-tool line when the run has the read tool, the blank lines around the
|
|
424
|
-
// rule lines) come first.
|
|
425
|
-
let room = budget - FRAME_BYTES(read);
|
|
426
|
-
for (const { note, bytes } of ranked.slice(0, bearing))
|
|
427
|
-
if (bytes <= room)
|
|
428
|
-
(room -= bytes), whole.push(note);
|
|
429
|
-
if (whole.length === ranked.length)
|
|
430
|
-
return { whole, index: false };
|
|
431
|
-
const taken = new Set(whole);
|
|
432
|
-
const lines = [];
|
|
433
|
-
let lineBytes = 0;
|
|
434
|
-
for (const { note, lineBytes: bytes } of ranked) {
|
|
435
|
-
if (taken.has(note) || lines.length >= INDEX_LINES || bytes > room || lineBytes + bytes > INDEX_BYTES)
|
|
436
|
-
continue;
|
|
437
|
-
room -= bytes;
|
|
438
|
-
lineBytes += bytes;
|
|
439
|
-
lines.push(note);
|
|
440
|
-
}
|
|
441
|
-
return { whole, index: false, lines };
|
|
442
|
-
};
|
|
443
|
-
/** The selector by recency: every note whole while all fit the budget and the index names no note main does not hold
|
|
444
|
-
* whole; notes that fit with an index that names one it lacks, the index, then the newest notes that fit what it
|
|
445
|
-
* leaves, each whole; past the budget, the newest notes whole while they fit, then a line for each of the next while
|
|
446
|
-
* they fit (withinBudget). */
|
|
447
|
-
export const byRecency = (candidates) => {
|
|
448
|
-
const { notes, indexBytes, budget } = candidates;
|
|
449
|
-
if (allFit(candidates))
|
|
450
|
-
return { whole: notes.map(({ note }) => note), index: false };
|
|
451
|
-
if (notes.reduce((sum, { bytes }) => sum + bytes, 0) > budget)
|
|
452
|
-
return withinBudget(notes, budget, candidates.read);
|
|
453
|
-
let left = budget - indexBytes;
|
|
454
|
-
return { whole: notes.filter(({ bytes }) => bytes <= left && ((left -= bytes), true)).map(({ note }) => note), index: true };
|
|
455
|
-
};
|
|
456
|
-
/** The default selector, by relevance, past the budget: while the notes fit it, recency's choice (byRecency). Else each
|
|
457
|
-
* note is scored against the run's input (learn/relevance.ts: BM25 over its name, description and body, a code
|
|
458
|
-
* review's changed paths' words weighed PATH_WEIGHT; no model call), and the run reads the notes that bear on it most
|
|
459
|
-
* whole, best first, while they fit the budget, then one rule line for each of the next while they fit what is left
|
|
460
|
-
* (withinBudget), a note that bears on it in no word a line at most; the rest are left out. An input with no word any
|
|
461
|
-
* note holds reads by recency. */
|
|
462
|
-
export const byRelevance = (candidates, run) => {
|
|
463
|
-
// Within the budget, nothing changes: recency's choice (every note whole).
|
|
464
|
-
if (candidates.notes.reduce((sum, { bytes }) => sum + bytes, 0) <= candidates.budget)
|
|
465
|
-
return byRecency(candidates, run);
|
|
466
|
-
const { notes, budget } = candidates;
|
|
467
|
-
const scores = bm25(notes.map(({ note }) => note), queryOf(run.input));
|
|
468
|
-
if (!scores.some((score) => score > 0))
|
|
469
|
-
return byRecency(candidates, run);
|
|
470
|
-
// Best first; a tie keeps recency (the candidates' order).
|
|
471
|
-
const ranked = notes.map((candidate, i) => ({ ...candidate, score: scores[i], at: i })).sort((a, b) => b.score - a.score || a.at - b.at);
|
|
472
|
-
// Only a note that bears on the run is read whole; one that shares no word with it is a line at most.
|
|
473
|
-
return withinBudget(ranked, budget, candidates.read, ranked.filter(({ score }) => score > 0).length);
|
|
474
|
-
};
|
|
475
|
-
/** The lines before what a run learned, under `# Learned` after LEARNS, and the one that names `.learning/` to an agent
|
|
476
|
-
* that can read it. */
|
|
477
|
-
const KEPT = "Follow a note when it applies to what you are doing.";
|
|
478
|
-
const WHOLE_INTRO = `What you learned in earlier runs: each note whole, newest first. ${KEPT}`;
|
|
479
|
-
const SOME_INTRO = `What you learned in earlier runs: the index, one line per note, then the newest notes whole. ${KEPT}`;
|
|
480
|
-
const INDEX_INTRO = `What you learned in earlier runs, one line per note. ${KEPT}`;
|
|
481
|
-
const LINES_INTRO = `What you learned in earlier runs: the notes that bear most on this run whole, then one line each for more of them. ${KEPT}`;
|
|
482
|
-
const BEST_INTRO = `What you learned in earlier runs: the notes that bear most on this run, each whole. ${KEPT}`;
|
|
483
|
-
const LINES_ONLY_INTRO = `What you learned in earlier runs: one line each for the notes that bear most on this run. ${KEPT}`;
|
|
484
|
-
const READ_LINE = `The notes not given whole here are files in \`${LEARNED_DIR}/\` in your working directory: open one with the read tool when it applies.`;
|
|
485
|
-
const oneLine = (text) => text.replace(/\s+/g, " ").trim();
|
|
486
|
-
/** What a run whose notes are all past a budget that holds none of them reads in place of them: that they are there, and,
|
|
487
|
-
* with the read tool, where. Never "Nothing learned yet." while it has notes. */
|
|
488
|
-
const NONE_FIT = "You learned notes in earlier runs, but none fit this run's learning budget, so none is given here.";
|
|
489
|
-
const NONE_FIT_READ = `You learned notes in earlier runs, but none fit this run's learning budget. They are files in \`${LEARNED_DIR}/\` in your working directory: open one with the read tool when it applies.`;
|
|
490
|
-
/** The most a section past the budget takes beside its notes and lines: its longest intro, the read-tool line when the run
|
|
491
|
-
* has the read tool (`read`), and the blank lines between its parts (withinBudget). */
|
|
492
|
-
const FRAME_BYTES = (read) => Math.max(...[LINES_INTRO, BEST_INTRO, LINES_ONLY_INTRO].map((intro) => Buffer.byteLength(intro))) + (read ? Buffer.byteLength(READ_LINE) + 2 : 0) + 4;
|
|
493
|
-
/** A note as one rule line: its name and file, then its description, cut of the host's secrets. */
|
|
494
|
-
const ruleLine = (note, shown) => `- ${shown.text(oneLine(note.name))} (${shown.name(note.file)}): ${shown.text(oneLine(note.description))}`;
|
|
495
|
-
/** A note as a run reads it whole: its name and file, its description, then its body, each cut of the host's secrets. */
|
|
496
|
-
const wholeNote = (note, shown) => [`## ${shown.text(oneLine(note.name))} (${shown.name(note.file)})`, shown.text(oneLine(note.description)), shown.text(note.body.trim())].filter(Boolean).join("\n\n");
|
|
497
|
-
/** What a run of `seed` reads under `# Learned` (`section`, the same for every harness, undefined when it learned
|
|
498
|
-
* nothing) and the notes that names (`given`), from one choice of the selector, so the section and the given notes never
|
|
499
|
-
* come from two choices. Only active notes are served: a pending, superseded or retired one is never there, whole or in
|
|
500
|
-
* the index, nor counted toward the budget (readSeed reads active notes alone, and servedIndex drops their lines). Its
|
|
501
|
-
* active notes, newest first (`seed.recent`, else by name), each whole while all fit `budget`; past it, the selector's
|
|
502
|
-
* choice (byRelevance by default). An agent with the read tool is told the rest are in `.learning/`, one without is not.
|
|
503
|
-
* Notes past a budget that holds none of them (0 among them) are said to be there (NONE_FIT), never "Nothing learned yet.".
|
|
504
|
-
* Every text is cut of the host's secrets. A run "was given" `given`; of them, it "read" those whose file it opened
|
|
505
|
-
* (store-reads/learning.ts notesReadOf). Whether it applied one given whole is not measured. `left`: the active notes
|
|
506
|
-
* the section names in no way, over the budget, which the run's log names (runner/learning-seed.ts). */
|
|
507
|
-
export function learnedParts(seed, options) {
|
|
508
|
-
const { budget, read, shown, select = byRelevance, run = {} } = options;
|
|
509
|
-
const active = new Map(activeOf(seed).map((note) => [note.file, note]));
|
|
510
|
-
const notes = (seed.recent ?? [...active.keys()].sort()).flatMap((file) => active.get(file) ?? []);
|
|
511
|
-
const index = servedIndex(seed);
|
|
512
|
-
const lines = index === undefined ? [] : loadedIndex(index).split("\n").filter((entry) => entry.trim() !== "");
|
|
513
|
-
if (!notes.length && !lines.length)
|
|
514
|
-
return { section: undefined, given: [], left: [] };
|
|
515
|
-
const blocks = new Map(notes.map((note) => [note, wholeNote(note, shown)]));
|
|
516
|
-
// Each part's bytes, and the blank line before it.
|
|
517
|
-
const size = (text) => Buffer.byteLength(text) + 2;
|
|
518
|
-
const shownIndex = lines.map(shown.text).join("\n");
|
|
519
|
-
// Which notes the run reads whole, and whether it reads the index (the selector's one choice). Each line's first link,
|
|
520
|
-
// in any spelling (`./x.md` is x.md: linkedFiles, as servedIndex reads it), names an active note when indexComplete.
|
|
521
|
-
const choice = select({
|
|
522
|
-
notes: notes.map((note) => ({ note, bytes: size(blocks.get(note)), lineBytes: Buffer.byteLength(ruleLine(note, shown)) + 1 })),
|
|
523
|
-
indexBytes: shownIndex ? size(shownIndex) : 0,
|
|
524
|
-
indexComplete: lines.every((entry) => active.has(linkedFiles(entry)[0] ?? "")),
|
|
525
|
-
budget,
|
|
526
|
-
read,
|
|
527
|
-
}, run);
|
|
528
|
-
const chosen = new Set(choice.whole);
|
|
529
|
-
const whole = choice.whole.filter((note) => blocks.has(note)).map((note) => blocks.get(note));
|
|
530
|
-
// Rule lines in place of the index: the whole notes first, then a line for each of the rest the selector names.
|
|
531
|
-
if (choice.lines) {
|
|
532
|
-
// The whole section fits the budget, its intro, blank lines and read-tool line counted: while it does not, the last
|
|
533
|
-
// line goes, then the last whole note (whole notes are what a run reads past the budget, so they go last). The lines
|
|
534
|
-
// are capped as the index is read.
|
|
535
|
-
const wholeNotes = choice.whole.filter((note) => blocks.has(note));
|
|
536
|
-
// The lines as the index is read (learn/notes.ts loadedIndex): the first INDEX_LINES, within INDEX_BYTES, each line
|
|
537
|
-
// and its newline counted.
|
|
538
|
-
const capped = (list) => {
|
|
539
|
-
let bytes = 0;
|
|
540
|
-
return list.slice(0, INDEX_LINES).filter((note) => (bytes += Buffer.byteLength(ruleLine(note, shown)) + 1) <= INDEX_BYTES);
|
|
541
|
-
};
|
|
542
|
-
const lined = capped(choice.lines.filter((note) => blocks.has(note) && !chosen.has(note)));
|
|
543
|
-
const render = () => [!wholeNotes.length ? LINES_ONLY_INTRO : lined.length ? LINES_INTRO : BEST_INTRO, ...wholeNotes.map((note) => blocks.get(note)), lined.map((note) => ruleLine(note, shown)).join("\n"), ...(read && wholeNotes.length < notes.length ? [READ_LINE] : [])].filter(Boolean).join("\n\n");
|
|
544
|
-
let section = render();
|
|
545
|
-
while (Buffer.byteLength(section) > budget && (lined.length || wholeNotes.length)) {
|
|
546
|
-
if (lined.length)
|
|
547
|
-
lined.pop();
|
|
548
|
-
else
|
|
549
|
-
wholeNotes.pop();
|
|
550
|
-
section = render();
|
|
551
|
-
}
|
|
552
|
-
const served = new Set([...wholeNotes, ...lined]);
|
|
553
|
-
// Notes, none of which fit the budget (0 among them): the run is told they are there, never that it learned nothing.
|
|
554
|
-
if (!served.size)
|
|
555
|
-
return { section: notes.length ? (read ? NONE_FIT_READ : NONE_FIT) : undefined, given: [], left: notes };
|
|
556
|
-
return { section, given: notes.filter((note) => served.has(note)), left: notes.filter((note) => !served.has(note)) };
|
|
557
|
-
}
|
|
558
|
-
if (!choice.index)
|
|
559
|
-
return { section: [WHOLE_INTRO, ...whole].join("\n\n"), given: notes.filter((note) => chosen.has(note)), left: notes.filter((note) => !chosen.has(note)) };
|
|
560
|
-
// Given: each note whole here, and each the index names by its file, by any of a line's links (a hand-edited line may
|
|
561
|
-
// name more than one; linkedFiles, as servedIndex reads them).
|
|
562
|
-
const indexed = new Set(lines.flatMap((entry) => linkedFiles(entry).filter((file) => active.has(file))));
|
|
563
|
-
const given = notes.filter((note) => indexed.has(note.file) || chosen.has(note));
|
|
564
|
-
return { section: [whole.length ? SOME_INTRO : INDEX_INTRO, shownIndex, ...(read && whole.length < notes.length ? [READ_LINE] : []), ...whole].filter(Boolean).join("\n\n"), given, left: notes.filter((note) => !given.includes(note)) };
|
|
565
|
-
}
|
|
566
|
-
/** The run's skills folder, `.agents/skills`, as its Workspace names it. */
|
|
567
|
-
const SKILLS_PATH = RUN_SKILLS.join("/");
|
|
568
|
-
/** The names in the run's `.agents/skills/`: none when it is not there. A link on the way, or anything else that keeps
|
|
569
|
-
* it from being listed, throws. */
|
|
570
|
-
async function runSkillNames(work) {
|
|
571
|
-
try {
|
|
572
|
-
return new Set((await work.list(SKILLS_PATH)).map((entry) => entry.name));
|
|
573
|
-
}
|
|
574
|
-
catch (error) {
|
|
575
|
-
if (isWorkspaceError(error, "NOT_FOUND"))
|
|
576
|
-
return new Set();
|
|
577
|
-
throw error;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
/** Lays each of `skills` in the run's `.agents/skills/<name>/` through its Workspace, a folder made new for it (one
|
|
581
|
-
* already there throws EXISTS); a link on the way is ESCAPE, and nothing is written through it. Each name goes in
|
|
582
|
-
* `made` once its folder is, so a caller that fails takes back what was laid. */
|
|
583
|
-
export async function laySkills(work, skills, made) {
|
|
584
|
-
if (!skills.length)
|
|
585
|
-
return;
|
|
586
|
-
const there = await runSkillNames(work);
|
|
587
|
-
for (const { name, files = {} } of skills) {
|
|
588
|
-
if (there.has(name))
|
|
589
|
-
throw new WorkspaceError("EXISTS", `the run's ${SKILLS_PATH}/${name} is there already`, { path: `${SKILLS_PATH}/${name}` });
|
|
590
|
-
await work.mkdir(`${SKILLS_PATH}/${name}`);
|
|
591
|
-
made.push(name);
|
|
592
|
-
// A SKILL.md as a harness is given it: its name and description alone in its front matter (servedSkill).
|
|
593
|
-
for (const [path, text] of Object.entries(files))
|
|
594
|
-
await work.write(`${SKILLS_PATH}/${name}/${path}`, path === "SKILL.md" ? servedSkill(text) : text);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
/** Takes the learned skills `names` back out of the run's work: each `.agents/skills/<name>/`, unless a folder on the
|
|
598
|
-
* way is a link (or no folder), when nothing is touched. */
|
|
599
|
-
export async function takeSkills(work, names) {
|
|
600
|
-
for (const name of names) {
|
|
601
|
-
try {
|
|
602
|
-
await work.remove(`${SKILLS_PATH}/${name}`, { recursive: true, force: true });
|
|
603
|
-
}
|
|
604
|
-
catch (error) {
|
|
605
|
-
if (isWorkspaceError(error, "ESCAPE") || isWorkspaceError(error, "NOT_FOUND") || isWorkspaceError(error, "REFUSED"))
|
|
606
|
-
return;
|
|
607
|
-
throw error;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
/** The learned skills' names a pin's `skills` hold, each once. */
|
|
612
|
-
const namesIn = (files) => [...new Set((files ?? []).map((path) => path.split("/")[0]))];
|
|
613
|
-
/** Which of `seed`'s learned skills a run gets, in `work`, in the seed's order: each whole while they fit their budget,
|
|
614
|
-
* LEARNED_SKILLS_BUDGET and never more than the revision's `own` skill files leave of SKILL_BYTES, sized in each form a
|
|
615
|
-
* harness is handed them, the larger: as they are in `work` (claude-agent, codex) and as servedSkills writes them
|
|
616
|
-
* (OpenCode, whose check reads that form). One named like a folder of the revision's skills, or like anything already in `work/.agents/skills/`, is
|
|
617
|
-
* the revision's, and not the run's; one that does not fit, or holds a host secret (`shown`) as stored or as laid, is
|
|
618
|
-
* left out. */
|
|
619
|
-
export async function chooseSkills(work, seed, own, shown) {
|
|
620
|
-
if (!seed.skills?.length)
|
|
621
|
-
return { taken: [], left: [] };
|
|
622
|
-
let stored = 0;
|
|
623
|
-
const folders = new Map();
|
|
624
|
-
// Each skill's SKILL.md as stored, the one file servedSkills changes; every other file is served as it is.
|
|
625
|
-
const skillMds = [];
|
|
626
|
-
for (const path of own) {
|
|
627
|
-
const at = `${SKILLS_PATH}/${path}`;
|
|
628
|
-
const dir = posix.dirname(at);
|
|
629
|
-
if (!folders.has(dir))
|
|
630
|
-
folders.set(dir, work.list(dir));
|
|
631
|
-
const entry = (await folders.get(dir)).find((one) => one.name === posix.basename(at) && one.kind === "file");
|
|
632
|
-
if (!entry)
|
|
633
|
-
throw new WorkspaceError("NOT_FOUND", `the revision's skill file ${at} is not in the run's work`, { path: at });
|
|
634
|
-
stored += entry.size;
|
|
635
|
-
const [name, ...rest] = path.split("/");
|
|
636
|
-
if (rest.join("/") === "SKILL.md")
|
|
637
|
-
skillMds.push({ name: name, files: { "SKILL.md": Buffer.from(await work.read(at)).toString("utf8") } });
|
|
638
|
-
}
|
|
639
|
-
const mdBytes = (skills) => skills.reduce((sum, skill) => sum + Buffer.byteLength(skill.files["SKILL.md"], "utf8"), 0);
|
|
640
|
-
const served = stored - mdBytes(skillMds) + mdBytes(servedSkills(skillMds));
|
|
641
|
-
let room = Math.max(0, Math.min(LEARNED_SKILLS_BUDGET, SKILL_BYTES - Math.max(stored, served)));
|
|
642
|
-
const owned = new Set(namesIn(own));
|
|
643
|
-
const there = await runSkillNames(work);
|
|
644
|
-
const taken = [];
|
|
645
|
-
const left = [];
|
|
646
|
-
for (const skill of seed.skills) {
|
|
647
|
-
if (owned.has(skill.name) || there.has(skill.name))
|
|
648
|
-
continue;
|
|
649
|
-
// Each file as stored and as laid: a SKILL.md is laid as servedSkill writes it, its front matter's values decoded
|
|
650
|
-
// (a held secret spelled in escapes reads as the secret there), so that text is what is checked too.
|
|
651
|
-
const secret = shown.holds(skill.name) || Object.entries(skill.files ?? {}).some(([path, text]) => shown.holds(path) || shown.holds(text) || (path === "SKILL.md" && shown.holds(servedSkill(text))));
|
|
652
|
-
if (!skill.files || secret || skill.bytes > room) {
|
|
653
|
-
left.push(shown.name(skill.name));
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
room -= skill.bytes;
|
|
657
|
-
taken.push(skill);
|
|
658
|
-
}
|
|
659
|
-
return { taken, left };
|
|
206
|
+
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script). No commit
|
|
207
|
+
* (null) is the empty seed; a commit the folder no longer holds throws. */
|
|
208
|
+
export async function seedAt(ws, commit) {
|
|
209
|
+
if (commit === null)
|
|
210
|
+
return { sha: null, corrections: "", workflows: [] };
|
|
211
|
+
const at = await folderAt(ws, commit);
|
|
212
|
+
if (!at)
|
|
213
|
+
throw new Error(`the learning folder's read at ${commit} is gone`);
|
|
214
|
+
return seedIn(commit, at.snapshot, async (_path, oid) => {
|
|
215
|
+
const bytes = at.blobs.get(oid);
|
|
216
|
+
if (!bytes)
|
|
217
|
+
throw new Error(`the learning folder's read at ${commit} holds no blob ${oid}`);
|
|
218
|
+
return bytes.toString("utf8");
|
|
219
|
+
});
|
|
660
220
|
}
|
|
661
|
-
/** Whether a learned workflow's decoded strings (decodedStrings) hold a host secret, or can't be read past the bound:
|
|
662
|
-
* either way it is never chosen. */
|
|
663
|
-
const readsHeld = (decoded, shown) => decoded === "too_deep" || decoded.some(shown.holds);
|
|
664
221
|
/** Whether `text` holds a host secret (`shown`): as written, or, read as JSON, in any string it decodes to (its escapes
|
|
665
222
|
* decoded), or nested past the bound. The one check a learned workflow's text passes at a run's start (chooseWorkflows)
|
|
666
223
|
* and every text its compile turn is told (runner/learning-end.ts compileAskOf); a text that is not JSON is read as
|
|
667
224
|
* written. */
|
|
668
|
-
export const holdsSecret = (text, shown) =>
|
|
225
|
+
export const holdsSecret = (text, shown) => {
|
|
226
|
+
const decoded = decodedStrings(text);
|
|
227
|
+
return shown.holds(text) || decoded === "too_deep" || decoded.some(shown.holds);
|
|
228
|
+
};
|
|
669
229
|
/** Which of `seed`'s learned workflows a run may run (its `learned_workflow` tool), in the seed's order: each while its
|
|
670
230
|
* line (workflowLine) fits what is left of the tool's description budget (LEARNED_WORKFLOWS_BUDGET). One that does not,
|
|
671
231
|
* or holds a host secret (`shown`), is left out and named in `left`, but for one whose name is itself a secret, never
|
|
672
|
-
* named.
|
|
232
|
+
* named. */
|
|
673
233
|
export function chooseWorkflows(seed, shown) {
|
|
674
234
|
const flows = [];
|
|
675
235
|
const left = [];
|
|
676
236
|
let room = LEARNED_WORKFLOWS_BUDGET;
|
|
677
|
-
for (const flow of seed.workflows
|
|
678
|
-
// Its JSON read as JSON reads it too (decodedStrings: its escapes decoded, as its learned call's run reads it).
|
|
237
|
+
for (const flow of seed.workflows) {
|
|
679
238
|
if (holdsSecret(flow.workflow, shown) || shown.holds(flow.description) || shown.holds(flow.name)) {
|
|
680
|
-
// Named, but for a name that is itself a secret.
|
|
681
239
|
if (!shown.holds(flow.name))
|
|
682
240
|
left.push(flow.name);
|
|
683
241
|
continue;
|
|
@@ -690,124 +248,16 @@ export function chooseWorkflows(seed, shown) {
|
|
|
690
248
|
}
|
|
691
249
|
return { flows, left };
|
|
692
250
|
}
|
|
693
|
-
/**
|
|
694
|
-
*
|
|
695
|
-
|
|
696
|
-
* those skills, last, into `.agents/skills/`. A write that fails leaves no `.learning/`, no learned skill and no pin,
|
|
697
|
-
* and throws. */
|
|
698
|
-
export async function pinSeed(runDir, work, seed, options) {
|
|
699
|
-
const pinFile = join(runDir, LEARNING_PIN);
|
|
700
|
-
const made = [];
|
|
701
|
-
try {
|
|
702
|
-
await layLearned(work, seed, options.shown);
|
|
703
|
-
const { section, given } = learnedParts(seed, options);
|
|
704
|
-
const { taken, left } = await chooseSkills(work, seed, options.own ?? [], options.shown);
|
|
705
|
-
const skills = taken.flatMap(({ name, files = {} }) => Object.keys(files).sort().map((path) => `${name}/${path}`));
|
|
706
|
-
// The learned workflows the run may run (chooseWorkflows), whole, in its directory before the pin names them (a
|
|
707
|
-
// reopen keeps them); the ones left out the pin names.
|
|
708
|
-
const { flows, left: flowsLeft } = chooseWorkflows(seed, options.shown);
|
|
709
|
-
if (flows.length)
|
|
710
|
-
await writeDurable(join(runDir, LEARNED_WORKFLOWS_FILE), `${JSON.stringify(flows)}\n`);
|
|
711
|
-
const pin = { seed: seed.sha, ...(section ? { section } : {}), ...(given.length ? { given: given.map(({ file, name }) => ({ file, name })) } : {}), ...(skills.length ? { skills } : {}), ...(left.length ? { skills_left_out: left } : {}), ...(flows.length ? { workflows: flows.map(({ name }) => name) } : {}), ...(flowsLeft.length ? { workflows_left_out: flowsLeft } : {}) };
|
|
712
|
-
// The pin names the skills before they are laid: a server that goes down while they are takes them back (dropSeed).
|
|
713
|
-
await writeDurable(pinFile, `${JSON.stringify(pin)}\n`);
|
|
714
|
-
await laySkills(work, taken, made);
|
|
715
|
-
await learningTestHooks.afterPin?.(runDir);
|
|
716
|
-
return pin;
|
|
717
|
-
}
|
|
718
|
-
catch (error) {
|
|
719
|
-
// writeDurable renames the pin into place before it syncs the run's directory, so a failure there leaves the pin
|
|
720
|
-
// behind: it goes too, or the run's instructions would still say what it learned, of notes it has no copy of.
|
|
721
|
-
await rm(pinFile, { force: true }).catch(() => undefined);
|
|
722
|
-
await rm(join(runDir, LEARNED_WORKFLOWS_FILE), { force: true }).catch(() => undefined);
|
|
723
|
-
await work.remove(LEARNED_DIR, { recursive: true, force: true }).catch(() => undefined);
|
|
724
|
-
await takeSkills(work, made).catch(() => undefined);
|
|
725
|
-
throw error;
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
/** Lays the learned skills a reopened run's pin names (`pin.skills`) again, fresh, from `read` (the skills of its seed's
|
|
729
|
-
* commit: skillsAt): what the run wrote over them, or took away, is not what it learned. Each only with the files the
|
|
730
|
-
* pin names. Throws when it cannot, a link on the way among the reasons. */
|
|
731
|
-
export async function layPinnedSkills(work, pin, read) {
|
|
732
|
-
const names = namesIn(pin.skills);
|
|
733
|
-
if (!names.length)
|
|
734
|
-
return;
|
|
735
|
-
const files = new Set(pin.skills);
|
|
736
|
-
const skills = (await read()).filter(({ name }) => names.includes(name)).map(({ name, bytes, files: all = {} }) => ({ name, bytes, files: Object.fromEntries(Object.entries(all).filter(([path]) => files.has(`${name}/${path}`))) }));
|
|
737
|
-
await takeSkills(work, names);
|
|
738
|
-
await laySkills(work, skills, []);
|
|
739
|
-
}
|
|
740
|
-
/** Takes back a seed whose copy a server went down during (runs.ts load): the learned skills its pin names, the pin,
|
|
741
|
-
* then the run's `.learning/`, through its Workspace. True when the pin or the copy was there. */
|
|
742
|
-
export async function dropSeed(runDir, work) {
|
|
743
|
-
const pinFile = join(runDir, LEARNING_PIN);
|
|
744
|
-
const copied = await work.list(LEARNED_DIR).then(() => true, (error) => !isWorkspaceError(error, "NOT_FOUND"));
|
|
745
|
-
const there = Boolean(await lstat(pinFile).catch(() => null)) || copied;
|
|
746
|
-
if (!there)
|
|
747
|
-
return false;
|
|
748
|
-
await takeSkills(work, namesIn((await readPin(runDir).catch(() => undefined))?.skills));
|
|
749
|
-
await rm(pinFile, { force: true });
|
|
750
|
-
await work.remove(LEARNED_DIR, { recursive: true, force: true });
|
|
751
|
-
return there;
|
|
752
|
-
}
|
|
753
|
-
/** The run's pin, as its process reads it: undefined when the run has none (its agent does not learn, its seed failed, a
|
|
754
|
-
* workflow's run). A pin out of shape is an error, never read as none. */
|
|
755
|
-
export async function readPin(runDir) {
|
|
756
|
-
let text;
|
|
757
|
-
try {
|
|
758
|
-
text = await readFile(join(runDir, LEARNING_PIN), "utf8");
|
|
759
|
-
}
|
|
760
|
-
catch (error) {
|
|
761
|
-
if (error.code === "ENOENT")
|
|
762
|
-
return undefined;
|
|
763
|
-
throw error;
|
|
764
|
-
}
|
|
765
|
-
const pin = JSON.parse(text);
|
|
766
|
-
const names = (value, shape) => value === undefined || (Array.isArray(value) && value.every((entry) => typeof entry === "string" && shape(entry)));
|
|
767
|
-
const skillFile = (entry) => SKILL_NAME.test(entry.split("/")[0]) && SKILL_FILE_PATH.test(entry.slice(entry.indexOf("/") + 1)) && entry.includes("/");
|
|
768
|
-
if (!isObject(pin) || (typeof pin.seed !== "string" && pin.seed !== null) || (pin.section !== undefined && typeof pin.section !== "string") || (pin.given !== undefined && !(Array.isArray(pin.given) && pin.given.every((entry) => isObject(entry) && typeof entry.file === "string" && typeof entry.name === "string"))) || !names(pin.skills, skillFile) || !names(pin.skills_left_out, () => true) || !names(pin.workflows, (entry) => WORKFLOW_NAME.test(entry)) || !names(pin.workflows_left_out, (entry) => WORKFLOW_NAME.test(entry)))
|
|
769
|
-
throw new Error(`the run's ${LEARNING_PIN} is not a learning pin`);
|
|
770
|
-
return pin;
|
|
771
|
-
}
|
|
251
|
+
/** A run's # Learned text from its seed: corrections.md whole, each line cut of the host's secrets (`shown`). Empty when
|
|
252
|
+
* the owner corrected nothing yet. */
|
|
253
|
+
export const learnedSection = (seed, shown) => seed.corrections.trim() ? seed.corrections.split("\n").map(shown.text).join("\n").trim() : "";
|
|
772
254
|
/** The heading of what a run learned, in its instructions. */
|
|
773
255
|
export const LEARNED_HEADING = "# Learned";
|
|
774
|
-
/** What
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
/** What every run that learns is told first under it, notes or none: a run not told it learns saves what the person
|
|
778
|
-
* asks it to remember in a file, or says it cannot, and that is never kept. */
|
|
779
|
-
const LEARNS = "You learn from your runs. When this run ends you are asked what to keep for later runs, anything the person asked you to remember included, and only your answer then is kept: a file you write is not. So when the person asks you to remember something, don't put it in a file or say you can't: tell them in one short sentence at the start of your reply that you will keep it for later runs. When what they asked sets how your reply starts or ends, follow it from your reply's first line to its last and leave that sentence out. A secret (a password, key or token) is never kept, so say so instead.";
|
|
780
|
-
/** What a run that starts with no note is told in place of what it learned. */
|
|
256
|
+
/** What every run that learns is told first under it: what follows is its owner's word. */
|
|
257
|
+
const CORRECTIONS_INTRO = "Your owner's standing corrections from earlier runs, one per line. Follow each one that applies.";
|
|
258
|
+
/** What a run whose owner corrected nothing yet is told in place of them. */
|
|
781
259
|
const NOTHING_YET = "Nothing learned yet.";
|
|
782
|
-
/** A run's instructions with its # Learned section laid in
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
|
|
786
|
-
export const laidLearned = (instructions, section, use = false) => `${instructions.trim()}\n\n${LEARNED_HEADING}\n\n${use ? USES : LEARNS}\n\n${section || NOTHING_YET}`;
|
|
787
|
-
/** A run's instructions, with `# Learned` after them when its pin in `runDir` says it learns: that it learns, then what
|
|
788
|
-
* the pin says it learned, or that it has learned nothing yet. A run with no pin (its agent does not learn, its seed
|
|
789
|
-
* failed, a workflow's run) is never asked what to keep, so it is told nothing. And its skill files: the revision's
|
|
790
|
-
* `own`, then the learned ones its pin names, by their paths under `.agents/skills/` in `work`, but for a skill named
|
|
791
|
-
* like one of the revision's, which is the revision's. A learned skill the work no longer holds whole, as a harness reads
|
|
792
|
-
* it through the run's Workspace (no link on the way), is left out: a learning fault never fails a run. The one path for
|
|
793
|
-
* every harness (app.ts): pi's system prompt, claude-agent's and codex's instructions, and the skills each gets. */
|
|
794
|
-
export async function withLearned(runDir, given, instructions, own = []) {
|
|
795
|
-
// The run's process still hands its folder's path.
|
|
796
|
-
const work = workspaceAt(given);
|
|
797
|
-
const pin = await readPin(runDir);
|
|
798
|
-
const section = pin?.section;
|
|
799
|
-
const owned = new Set(namesIn(own));
|
|
800
|
-
const learned = (pin?.skills ?? []).filter((path) => !owned.has(path.split("/")[0]));
|
|
801
|
-
const whole = new Set();
|
|
802
|
-
for (const name of namesIn(learned)) {
|
|
803
|
-
const files = learned.filter((path) => path.startsWith(`${name}/`));
|
|
804
|
-
if (await Promise.all(files.map((path) => work.read(`${SKILLS_PATH}/${path}`))).then(() => true, () => false))
|
|
805
|
-
whole.add(name);
|
|
806
|
-
else
|
|
807
|
-
process.stderr.write(`agentrun-serve: the skill ${name} its agent learned is no longer whole in the run's work, so the run goes on without it\n`);
|
|
808
|
-
}
|
|
809
|
-
return {
|
|
810
|
-
instructions: pin ? laidLearned(instructions, section) : instructions,
|
|
811
|
-
skills: [...own, ...learned.filter((path) => whole.has(path.split("/")[0]))],
|
|
812
|
-
};
|
|
813
|
-
}
|
|
260
|
+
/** A run's instructions with its # Learned section laid in: the revision's instructions, then the heading, and its owner's
|
|
261
|
+
* corrections (CORRECTIONS_INTRO, then the section), or "Nothing learned yet." when there are none (or a seed that could
|
|
262
|
+
* not be read). Shared so every harness lays the same bytes. */
|
|
263
|
+
export const laidLearned = (instructions, section) => `${instructions.trim()}\n\n${LEARNED_HEADING}\n\n${section ? `${CORRECTIONS_INTRO}\n\n${section}` : NOTHING_YET}`;
|