@parcha/agentrun-serve 0.1.0-beta.44 → 0.1.0-beta.45
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 +131 -5
- package/README.md +371 -300
- 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 +52 -16
- 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-3O2Yl6hZ.js +701 -0
- package/dist/console/assets/index-DaFsZuzQ.css +1 -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/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 +87 -102
- 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 +13 -1
- package/dist/learn/promote.js +9 -0
- 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 +4 -4
- package/dist/learning-setting.js +6 -6
- 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 +268 -316
- 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 +16 -8
- 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 +16 -5
- 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 +63 -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 +8 -6
- package/dist/runner/promotion.js +19 -12
- 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 +1222 -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 +685 -320
- 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 +2 -1
- package/dist/templates.js +6 -3
- 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 +60 -5
- package/dist/triggers.js +123 -18
- package/dist/validate.js +1 -1
- package/dist/webhooks.d.ts +4 -1
- package/dist/webhooks.js +11 -4
- 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 +7 -7
- package/templates/inbound-lead/agent.md +3 -3
- 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/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
|
@@ -1,45 +1,42 @@
|
|
|
1
|
-
// A run's learning end, from the one store: what its end-of-run turn kept
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// notes, its answer's time and costs) is the run's, not this.
|
|
8
|
-
import { posix } from "node:path";
|
|
1
|
+
// A run's learning end, from the one store: what its end-of-run turn kept beside the run's end (learning_proposals:
|
|
2
|
+
// merged with the commit it wrote, or dropped with its reason), as runner/reflect-hook.ts writes it in the run's end
|
|
3
|
+
// commit. It gives the run's learning (learning.ts Learning) to the run's view, its whole learning, /v2/usage and /v2/stats:
|
|
4
|
+
// how it ended and why, in a person's words, what it merged and held for the owner's OK, what the checks left out, and
|
|
5
|
+
// what its compile turn cost. What the server keeps on a run before its end (its seed, its answer's time and costs) is
|
|
6
|
+
// the run's launch's and journal's, not this.
|
|
9
7
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
10
8
|
import { currentLearning, holdsForOk, LEARNING_REASONS } from "../learning.js";
|
|
11
9
|
import { runLearningBody } from "../learning-routes.js";
|
|
12
10
|
import { learningStartOf, learningStartOfStored, readLaunchBody, seedFailed } from "../run-launch.js";
|
|
13
11
|
import { said, STOPPED } from "../learning-words.js";
|
|
14
|
-
import { learningImpact
|
|
15
|
-
import { amortize, billOf, costSummary, JOB_PATHS, noiseOff, RUN_PATHS, runCostOf, SERVED_PATHS } from "../learning-cost.js";
|
|
12
|
+
import { learningImpact } from "../learning-run.js";
|
|
13
|
+
import { amortize, billOf, costSummary, JOB_PATHS, noiseOff, RUN_PATHS, runCostOf, SERVED_PATHS, tookOverOf } from "../learning-cost.js";
|
|
16
14
|
import { COMPILE_AFTER_DEFAULT } from "../learning-setting.js";
|
|
17
15
|
import { modelsOwn, runPathOf, workflowNameOf } from "../runner/learning-end.js";
|
|
18
16
|
import { feedbackStatus } from "../runner/feedback-round.js";
|
|
19
|
-
import { PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
|
|
17
|
+
import { costStopOf, PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
|
|
20
18
|
import { reflects } from "../reflect-range.js";
|
|
21
|
-
import {
|
|
19
|
+
import { correctionCount } from "../learn/corrections.js";
|
|
22
20
|
import { runEntries, runReflect, runTimeline, withoutFeedbackRounds } from "./journal.js";
|
|
23
21
|
import { FINAL_STATES, StoreLaunchMissing } from "../store/store.js";
|
|
24
|
-
import { journalEntries } from "../runner/journal-reader.js";
|
|
25
22
|
import { KINDS } from "../runner/journal-kinds.js";
|
|
26
|
-
import { keptOf, learnedOf, lostOf } from "../runner/learning-loop.js";
|
|
27
23
|
import { promotedOf } from "../learn/compile.js";
|
|
28
24
|
import { isShadowRun, shadowRunId } from "../learn/audit.js";
|
|
29
25
|
import { championOf, checkShares, gradesOf, gradeView, promotionsOf } from "../learn/promote.js";
|
|
30
26
|
const timeOf = (ms) => new Date(ms).toISOString();
|
|
31
27
|
const usdOrNull = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
32
|
-
|
|
28
|
+
const textOrNull = (value) => (typeof value === "string" ? value : null);
|
|
29
|
+
/** What a kept row says of a run that looked back (its compile turn's figures): none for one that did not. */
|
|
33
30
|
function answeredOf(row) {
|
|
34
31
|
if (typeof row.answered_at !== "string")
|
|
35
32
|
return {};
|
|
36
33
|
return { answered_at: row.answered_at, job_cost_usd: usdOrNull(row.job_cost_usd), look_back_usd: usdOrNull(row.look_back_usd) };
|
|
37
34
|
}
|
|
38
|
-
/** Agent `agentId`'s runs' records read at once, whatever their number: its runs' launches (Store.runs.
|
|
39
|
-
* round its runs' learning kept
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
35
|
+
/** Agent `agentId`'s runs' records read at once, whatever their number: its runs' launches (Store.runs.learningStarts)
|
|
36
|
+
* and every round its runs' learning kept (Store.learning.kept), two reads beside `listed` (its runs, as runs.list read
|
|
37
|
+
* them). Each answers as the store's per-run read does: a run of `listed` with no launch row is StoreLaunchMissing, one
|
|
38
|
+
* that kept no learning has no round. A run not of `listed` is read from the store, but for an audit's shadow run:
|
|
39
|
+
* `listed` is every run of the agent, its shadows among them. */
|
|
43
40
|
export async function agentRecordsOf(store, agentId, listed) {
|
|
44
41
|
const [starts, kept] = await Promise.all([store.runs.learningStarts(agentId), store.learning.kept(agentId)]);
|
|
45
42
|
const ours = new Set(listed.map((run) => run.id));
|
|
@@ -48,7 +45,7 @@ export async function agentRecordsOf(store, agentId, listed) {
|
|
|
48
45
|
const rounds = new Map();
|
|
49
46
|
for (const one of kept.proposals)
|
|
50
47
|
rounds.set(one.runId, [...(rounds.get(one.runId) ?? []), one]);
|
|
51
|
-
const rows = new Map(
|
|
48
|
+
const rows = new Map(listed.map((run) => [run.id, run]));
|
|
52
49
|
return {
|
|
53
50
|
runs: {
|
|
54
51
|
launch: (runId) => store.runs.launch(runId),
|
|
@@ -73,79 +70,46 @@ export async function agentRecordsOf(store, agentId, listed) {
|
|
|
73
70
|
},
|
|
74
71
|
};
|
|
75
72
|
}
|
|
76
|
-
/** Run `runId`'s learning end (its newest round's, or round `round`'s); null when the run kept no learning.
|
|
73
|
+
/** Run `runId`'s learning end (its newest round's, or round `round`'s); null when the run kept no learning. `records`:
|
|
74
|
+
* where its rows are read (the store's per-run reads by default). */
|
|
77
75
|
export async function runLearningEnd(store, runId, round, records = store) {
|
|
78
76
|
const kept = await records.learning.proposal(runId, round);
|
|
79
77
|
if (!kept)
|
|
80
78
|
return null;
|
|
81
79
|
const row = (kept.proposal ?? {});
|
|
82
|
-
return { ...
|
|
80
|
+
return { ...endOfKept(kept.createdAt, row), ...answeredOf(row), ...compiledOf(row) };
|
|
83
81
|
}
|
|
84
82
|
/** What a kept row says of its compile turn (runner/reflect-hook.ts): the record, and a failed draft's errors; read as
|
|
85
83
|
* untrusted, an entry out of shape left out. */
|
|
86
84
|
function compiledOf(row) {
|
|
87
85
|
const texts = (list) => (Array.isArray(list) ? list.filter((one) => typeof one === "string") : []);
|
|
88
|
-
|
|
89
|
-
const { compiled: record, draft_errors } = row;
|
|
90
|
-
const errors = texts(draft_errors);
|
|
86
|
+
const errors = texts(row.draft_errors);
|
|
91
87
|
const out = errors.length ? { draft_errors: errors } : {};
|
|
88
|
+
const record = row.compiled;
|
|
92
89
|
if (record && typeof record === "object" && typeof record.workflow === "string") {
|
|
93
|
-
const { workflow,
|
|
90
|
+
const { workflow, attempts, digest } = record;
|
|
94
91
|
const tries = Array.isArray(attempts) ? attempts.filter((one) => one && typeof one === "object" && typeof one.ok === "boolean").map((one) => ({ ok: one.ok, errors: texts(one.errors) })) : [];
|
|
95
92
|
// `promoted` is never kept: runLearningOverStore reads it from the agent's promotion records (promotedOf).
|
|
96
|
-
out.compiled = { workflow,
|
|
93
|
+
out.compiled = { workflow, attempts: tries, ...(typeof digest === "string" && /^[0-9a-f]{16}$/.test(digest) ? { digest } : {}) };
|
|
97
94
|
}
|
|
98
95
|
return out;
|
|
99
96
|
}
|
|
100
|
-
/** The learning end a kept row
|
|
101
|
-
|
|
102
|
-
const at = timeOf(
|
|
103
|
-
|
|
104
|
-
if (row.state
|
|
105
|
-
|
|
97
|
+
/** The learning end a kept row says, at the time it was kept: merged with its commit, else dropped with its reason. */
|
|
98
|
+
function endOfKept(createdAt, row) {
|
|
99
|
+
const at = timeOf(createdAt);
|
|
100
|
+
const problems = Array.isArray(row.problems) && row.problems.length ? { problems: row.problems } : {};
|
|
101
|
+
if (row.state === "merged" && typeof row.merged === "string") {
|
|
102
|
+
const corrections = typeof row.corrections === "number" && row.corrections > 0 ? { corrections: row.corrections } : {};
|
|
103
|
+
const workflows = Array.isArray(row.workflows) && row.workflows.length ? { workflows: row.workflows } : {};
|
|
104
|
+
return { state: "merged", reason: null, detail: textOrNull(row.detail), at, merged: row.merged, ...workflows, ...corrections, ...problems };
|
|
106
105
|
}
|
|
107
|
-
|
|
108
|
-
const left = row.left?.length ? { problems: row.left } : {};
|
|
109
|
-
const proposed = { state: "proposed", reason: null, detail: row.detail ?? null, at, ...left };
|
|
110
|
-
if (!kept.learningRunId)
|
|
111
|
-
return proposed;
|
|
112
|
-
let learningRun = await records.runs.get(kept.learningRunId);
|
|
113
|
-
const { merged, dropped, graded } = await learningEndEntries(store, kept.learningRunId, learningRun);
|
|
114
|
-
if (!merged && !dropped)
|
|
115
|
-
return proposed;
|
|
116
|
-
// The loop saves its end entry and the learning run's end (state, ended_at) in one transaction, and the row was read
|
|
117
|
-
// before the journal: an end the row had not seen landed between the two reads. The row read again from the store (past
|
|
118
|
-
// any list read before) is the ended one, so the end is at its ended_at, never once at the proposal's time.
|
|
119
|
-
if (!(learningRun && FINAL_STATES.includes(learningRun.state)))
|
|
120
|
-
learningRun = await store.runs.get(kept.learningRunId);
|
|
121
|
-
const endedAt = learningRun?.endedAt ? timeOf(learningRun.endedAt) : at;
|
|
122
|
-
// What the grade spent: the graded entry's, or what a grade that failed spent before it did.
|
|
123
|
-
const spent = graded ?? (dropped && "usd" in dropped ? { usd: dropped.usd ?? null, tokens: dropped.tokens ?? null } : undefined);
|
|
124
|
-
const cost = spent ? { cost_usd: { grade: spent.usd }, tokens: { grade: spent.tokens ?? null } } : {};
|
|
125
|
-
if (merged && graded) {
|
|
126
|
-
// Each item as the merge settled it: its record's verdict (a note it left out is no longer kept, and says why). A merge
|
|
127
|
-
// recorded with none (before the record kept it) reads no item decisions.
|
|
128
|
-
const verdict = merged.verdict;
|
|
129
|
-
const what = verdict ? learnedOf(keptOf(row.proposal, verdict), verdict) : { learned: [], pending: [], skills: [], workflows: [] };
|
|
130
|
-
return {
|
|
131
|
-
state: "merged", reason: null, detail: merged.detail, at: endedAt, ...left, ...(verdict ? { verdict } : {}), graded: { ...graded.graded }, ...cost, merged: merged.commit,
|
|
132
|
-
learned: what.learned, ...(what.pending.length ? { pending: what.pending } : {}), ...(what.skills.length ? { skills: what.skills } : {}), ...(what.workflows.length ? { workflows: what.workflows } : {}),
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const reason = (dropped?.reason ?? null);
|
|
136
|
-
const lost = reason === "grader" ? lostOf(row.proposal) : [];
|
|
137
|
-
return {
|
|
138
|
-
state: "dropped", reason, detail: dropped?.detail ?? null, at: endedAt,
|
|
139
|
-
...(dropped?.problems?.length ? { problems: [...dropped.problems] } : left),
|
|
140
|
-
// The verdict, when one was given; the blobs it read only go with a merge (the server kept them for its merge alone).
|
|
141
|
-
...(graded ? { verdict: graded.verdict } : {}), ...cost, ...(lost.length ? { lost } : {}),
|
|
142
|
-
};
|
|
106
|
+
return { state: "dropped", reason: textOrNull(row.reason), detail: textOrNull(row.detail), at, ...problems };
|
|
143
107
|
}
|
|
144
108
|
/** GET /stats's learning counts from the one store, over the runs `filter` picks as the analytics pick them (not archived,
|
|
145
109
|
* their own or their agent's; started in [from, to); of `agent`): an agentic run with a learning end counts, its end
|
|
146
110
|
* read as the run's view reads it (runLearningEnd). A workflow's run, and one with no learning, is not counted; nor is
|
|
147
|
-
* one whose reason this server does not give
|
|
148
|
-
*
|
|
111
|
+
* one whose reason this server does not give. An imported run keeps no learning in the store. Each agent's runs'
|
|
112
|
+
* records are read at once (agentRecordsOf; `records`, already read for `filter.agent`), never a read a run. */
|
|
149
113
|
export async function learningCountsOverStore(store, filter, records) {
|
|
150
114
|
const from = filter.from === undefined ? -Infinity : Date.parse(filter.from);
|
|
151
115
|
const to = filter.to === undefined ? Infinity : Date.parse(filter.to);
|
|
@@ -184,7 +148,7 @@ export async function learningCountsOverStore(store, filter, records) {
|
|
|
184
148
|
if (workflow.get(run.revisionId))
|
|
185
149
|
continue;
|
|
186
150
|
const learning = await runLearningEnd(store, run.id, undefined, await recordsOf(run.agentId));
|
|
187
|
-
// A reason this server does not give
|
|
151
|
+
// A reason this server does not give is not counted.
|
|
188
152
|
if (!learning || (learning.reason !== null && !LEARNING_REASONS.includes(learning.reason)))
|
|
189
153
|
continue;
|
|
190
154
|
out.runs += 1;
|
|
@@ -198,27 +162,22 @@ export async function learningCountsOverStore(store, filter, records) {
|
|
|
198
162
|
return out;
|
|
199
163
|
}
|
|
200
164
|
/** What learning spent outside any run (GET /usage's `learning` meter) in [from, to) (ms), one entry per run whose
|
|
201
|
-
* learning ended then
|
|
202
|
-
*
|
|
203
|
-
* of the store (Learning.spend), whatever the number of runs. */
|
|
165
|
+
* learning ended then: its agent, when its learning ended, and what it spent outside the run. One read of the store
|
|
166
|
+
* (Learning.spend), whatever the number of runs. */
|
|
204
167
|
export async function learningSpendOverStore(store, window) {
|
|
205
168
|
return (await store.learning.spend(window)).map((one) => ({ agent: one.agentId, at: timeOf(one.at), cost: one.costUsd }));
|
|
206
169
|
}
|
|
207
|
-
/** An agent's learning counts as its learning view gives them (GET /agents/{id}/learning
|
|
208
|
-
*
|
|
209
|
-
* agentLearningOverStore's. */
|
|
170
|
+
/** An agent's learning counts as its learning view gives them (GET /agents/{id}/learning): how its runs that tried to
|
|
171
|
+
* learn fared, less `runs`, from the one store. */
|
|
210
172
|
export async function agentLearningCountsOverStore(store, agentId, records) {
|
|
211
173
|
const { runs: _, ...counts } = await learningCountsOverStore(store, { agent: agentId }, records);
|
|
212
174
|
return counts;
|
|
213
175
|
}
|
|
214
|
-
/** Agent `agentId`'s learning view from the one store (GET /agents/{id}/learning
|
|
215
|
-
* (
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* launch's seedNotes: every note in its seed, before the budget picked the given ones (main's seed_notes). Its runs'
|
|
220
|
-
* launches and learning are read at once (agentRecordsOf), and a settled run's journal once a server (settledCache):
|
|
221
|
-
* a read a run only the first time it is seen. */
|
|
176
|
+
/** Agent `agentId`'s learning view from the one store (GET /agents/{id}/learning): its counts (agentLearningCountsOverStore)
|
|
177
|
+
* and what learning changed (learningImpact), never a run's files. Each run's part is read from its launch (the
|
|
178
|
+
* corrections and workflows it started from), its journal (the workflows its claims laid) and its learning end (when its
|
|
179
|
+
* job answered and what it and looking back cost). Its runs' launches and learning are read at once (agentRecordsOf),
|
|
180
|
+
* and a settled run's journal once a server (settledCache). */
|
|
222
181
|
export async function agentLearningOverStore(store, agentId) {
|
|
223
182
|
const listed = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId);
|
|
224
183
|
const records = await agentRecordsOf(store, agentId, listed);
|
|
@@ -226,7 +185,6 @@ export async function agentLearningOverStore(store, agentId) {
|
|
|
226
185
|
const kinds = new Map();
|
|
227
186
|
const rows = [];
|
|
228
187
|
const learnings = new Map();
|
|
229
|
-
const files = [];
|
|
230
188
|
for (const run of listed) {
|
|
231
189
|
if (!kinds.has(run.revisionId))
|
|
232
190
|
kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind);
|
|
@@ -234,30 +192,28 @@ export async function agentLearningOverStore(store, agentId) {
|
|
|
234
192
|
const started = await learningStartOfRun(records, run.id);
|
|
235
193
|
// A run that only uses what its agent learned keeps no proposal: its end is its start's (usedOnly), as its run reads give it.
|
|
236
194
|
const end = (await runLearningEnd(store, run.id, undefined, records)) ?? (started?.use ? usedOnly(started, final, timeOf(run.endedAt ?? run.startedAt)) : null);
|
|
237
|
-
const { read, laid } = await journalReadsOf(store, run, started);
|
|
238
|
-
if (started || end)
|
|
239
|
-
files.push(notesOf({ given: started?.given ?? [], read }));
|
|
240
195
|
// The row learningImpact reads (the file path's RunRow): an ended run is a completed one.
|
|
241
196
|
rows.push({
|
|
242
197
|
id: run.id, kind: kinds.get(run.revisionId) === "workflow" ? "workflow" : "agentic", state: run.state === "ended" ? "completed" : run.state,
|
|
243
198
|
startedAt: new Date(run.startedAt).toISOString(), endedAt: run.endedAt === null ? null : new Date(run.endedAt).toISOString(),
|
|
244
199
|
costUsd: run.costUsd, importedFrom: run.importedFrom, archivedAt: run.archivedAt === null ? null : new Date(run.archivedAt).toISOString(),
|
|
245
200
|
});
|
|
246
|
-
//
|
|
247
|
-
|
|
201
|
+
// The workflows its claims laid of those it was offered (every learningLaid entry, laidOf): the run started from those alone.
|
|
202
|
+
const laid = end && started?.workflows?.length ? await journalLaidOf(store, run) : undefined;
|
|
248
203
|
learnings.set(run.id, end ? impactLearningOf(end, started, laid, await roundsSpendOf(records, run.id), run.costUsd) : null);
|
|
249
204
|
}
|
|
250
|
-
return { counts, impact: learningImpact(rows, (row) => learnings.get(row.id) ?? null)
|
|
205
|
+
return { counts, impact: learningImpact(rows, (row) => learnings.get(row.id) ?? null) };
|
|
251
206
|
}
|
|
252
207
|
/** Agent `agentId`'s cost per run from the one store (GET /agents/{id}/learning/cost, learning-cost.ts): each of its
|
|
253
|
-
* agentic runs that ended (ended, stopped or failed), oldest first,
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
208
|
+
* agentic runs that ended (ended, stopped or failed), oldest first, each run that does a job numbered from 1 (a replay
|
|
209
|
+
* none: the bill's numbering), with its own cost and what learning added for it (runCostOf over its row's cost and its
|
|
210
|
+
* learning's spend, learningSpendOf), the path it took and the learned workflow it ran (its run record's, pathOfRow), the
|
|
211
|
+
* revision of that workflow (metrics.workflow_digest) and whether an audit's shadow ran for it; and what
|
|
212
|
+
* the series shows (costSummary). A run still at work, an imported or archived one and a workflow revision's run are left
|
|
213
|
+
* out. A run that learns whose record names no path (its end's patch was late or failed, or no runner wrote its end) is
|
|
214
|
+
* left out too and counted (`unknown_path_runs`): its path and cost are not known, never `agent` at $0. `limit`: the
|
|
215
|
+
* latest runs shown, each keeping its number; the summary is over every run; `bill` is the owner's bill over all of
|
|
216
|
+
* them (billOf, with the revision ledger: agentLedgerOf). */
|
|
261
217
|
export async function agentCostOverStore(store, agentId, limit = Infinity) {
|
|
262
218
|
const kinds = new Map();
|
|
263
219
|
const listed = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId);
|
|
@@ -265,32 +221,36 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
|
|
|
265
221
|
const records = await agentRecordsOf(store, agentId, listed);
|
|
266
222
|
// An audit's shadow run is no run of the series: its cost is its served run's learning (allInOf).
|
|
267
223
|
const ended = listed.filter((run) => FINAL_STATES.includes(run.state) && !run.importedFrom && run.archivedAt === null && !isShadowRun(run.id)).sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
|
|
268
|
-
const
|
|
224
|
+
const rows = [];
|
|
269
225
|
let unknownPath = 0;
|
|
226
|
+
// One numbering, the bill's: the runs that do a job, 1 the first; a replay checks a revision and has none.
|
|
227
|
+
let jobs = 0;
|
|
270
228
|
for (const run of ended) {
|
|
271
229
|
if (!kinds.has(run.revisionId))
|
|
272
230
|
kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind);
|
|
273
231
|
if (kinds.get(run.revisionId) === "workflow")
|
|
274
232
|
continue;
|
|
275
233
|
// A run that learns whose record names no path is left out and counted, never `agent` at $0.
|
|
276
|
-
|
|
234
|
+
const placed = pathOfRow(run.metrics, await learningStartOfRun(records, run.id));
|
|
235
|
+
if (!placed) {
|
|
277
236
|
unknownPath += 1;
|
|
278
237
|
continue;
|
|
279
238
|
}
|
|
280
|
-
|
|
239
|
+
rows.push(await costRunOf(records, run, JOB_PATHS.includes(placed.path) ? (jobs += 1) : null));
|
|
281
240
|
}
|
|
241
|
+
// Where each revision took over, over every run before the limit cuts the read (the first is the summary's takeover).
|
|
242
|
+
const runs = tookOverOf(rows);
|
|
282
243
|
// Each learned workflow that ran first, where it stands against the money rule (workflow-first.ts workflowCostsOf), over
|
|
283
244
|
// the runs the run's launch reads (pastRunsOverStore) at the all-in cost this series shows: a workflow over the line
|
|
284
245
|
// shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
|
|
285
246
|
const workflows = workflowCostsOf(await pastRunsOverStore(store, agentId, new Map(runs.map((run) => [run.run_id, run.total_usd])), records));
|
|
286
247
|
const promotions = await promotionsOf(store.settings, agentId);
|
|
287
248
|
const replay = servingReplayOf(promotions, runs);
|
|
288
|
-
// The owner's bill: booked $, every run of the series (billOf), from the first run on a served path (
|
|
289
|
-
// run rows
|
|
290
|
-
// compile and check over the served runs that carry it.
|
|
249
|
+
// The owner's bill: booked $, every run of the series (billOf), from the series' first run on a served path (the
|
|
250
|
+
// summary's takeover: read from the run rows by their place, as the promotion feed is trimmed and its oldest record need
|
|
251
|
+
// not be the first promotion), and each revision's compile and check over the served runs that carry it.
|
|
291
252
|
const ledger = await agentLedgerOf(records, promotions, listed);
|
|
292
|
-
const
|
|
293
|
-
const booked = billOf(runs, servedFrom === undefined ? null : new Date(servedFrom).toISOString());
|
|
253
|
+
const booked = billOf(runs);
|
|
294
254
|
// What no run carries is the booked total less every run's all-in (A8), so a revision whose records aged out of the
|
|
295
255
|
// feed lands here, never nowhere: unknown while any run's figures are.
|
|
296
256
|
let carried = 0;
|
|
@@ -302,8 +262,6 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
|
|
|
302
262
|
const bill = { ...booked, revisions: ledger.revisions, unattributed_usd: unattributed, holds: holdsOf(promotions, runs) };
|
|
303
263
|
return { runs: runs.slice(Math.max(0, runs.length - limit)), summary: costSummary(runs, replay), total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: unknownPath, workflows, bill };
|
|
304
264
|
}
|
|
305
|
-
/** The paths the bill's baseline ends at: the first run a learned workflow served, or an audit audited. */
|
|
306
|
-
const FROM_PATHS = ["workflow", "handed_back", "audit"];
|
|
307
265
|
/** Each hold of the agent's learned workflows (its promotion records that say hold), placed on the series: at the first
|
|
308
266
|
* run that does a job from it on, by its number among them (the points' `n`), null when none has run since. */
|
|
309
267
|
function holdsOf(records, runs) {
|
|
@@ -320,22 +278,25 @@ const revisionOfRow = (row) => {
|
|
|
320
278
|
const { workflow, workflow_digest: digest } = metricOf(row);
|
|
321
279
|
return typeof workflow === "string" && typeof digest === "string" ? { workflow, digest } : undefined;
|
|
322
280
|
};
|
|
323
|
-
/** The compile turns run `runId`'s rounds asked, oldest first: none for a run that compiled nothing
|
|
281
|
+
/** The compile turns run `runId`'s rounds asked, oldest first: none for a run that compiled nothing (a round whose step
|
|
282
|
+
* asked no model turn keeps `look_back_usd` 0, or none at all). */
|
|
324
283
|
async function compilesOf(store, runId) {
|
|
325
284
|
return (await roundsOf(store, runId)).flatMap((kept) => {
|
|
326
285
|
const row = (kept.proposal ?? {});
|
|
327
286
|
const { compiled, draft_errors } = compiledOf(row);
|
|
328
|
-
|
|
329
|
-
//
|
|
287
|
+
const usd = "look_back_usd" in row ? usdOrNull(row.look_back_usd) : 0;
|
|
288
|
+
// A compile turn that kept no draft (the cap stopped it, or a stop, a fault or an unreadable refuse list ended the step
|
|
289
|
+
// after an earlier claim asked it: runner/reflect-hook.ts `unasked`) spent with no revision to serve: its spend is
|
|
290
|
+
// unattributed, never its run's.
|
|
330
291
|
if (!compiled)
|
|
331
|
-
return
|
|
332
|
-
const usd = "compile_usd" in row ? usdOrNull(row.compile_usd) : 0;
|
|
292
|
+
return usd === 0 ? [] : [{ workflow: null, digest: null, usd }];
|
|
333
293
|
return [{ workflow: compiled.workflow, digest: compiled.digest && !draft_errors?.length ? compiled.digest : null, usd }];
|
|
334
294
|
});
|
|
335
295
|
}
|
|
336
|
-
/** A run's v2 learning (A8): what learning cost for it as the cost series counts it (its
|
|
337
|
-
* shadow: `run.learning_usd`) less its compile turns (each its revision's), with its share of
|
|
338
|
-
* and check (`shares`: a missing entry is none, an unknown share stays unknown). Null while any
|
|
296
|
+
/** A run's v2 learning (A8): what learning cost for it as the cost series counts it (its compile turns, any follow-up
|
|
297
|
+
* round's turns and its audit shadow: `run.learning_usd`) less its compile turns (each its revision's), with its share of
|
|
298
|
+
* its revision's compile and check (`shares`: a missing entry is none, an unknown share stays unknown). Null while any
|
|
299
|
+
* part is unknown. */
|
|
339
300
|
function learningV2Of(run, compiles, shares) {
|
|
340
301
|
const compiled = compiles.reduce((sum, one) => (sum === null || one.usd === null ? null : sum + one.usd), 0);
|
|
341
302
|
const share = shares.has(run.run_id) ? shares.get(run.run_id) : 0;
|
|
@@ -392,12 +353,12 @@ export function revisionLedgerOf(records, rows, compiles = []) {
|
|
|
392
353
|
/** Run `runId`'s path and its costs as GET /runs/{id} shows them (A8): `path` its record's (metrics.path, RUN_PATHS:
|
|
393
354
|
* `agent` for a run that does not learn; null while it has not ended, for a workflow revision's run, and for a run that
|
|
394
355
|
* learns whose end named none); `usd` its own spend, the job's (runCostOf's `cost_usd`: its row's cost, `rowUsd` when
|
|
395
|
-
* given, less its
|
|
396
|
-
*
|
|
397
|
-
* check (revisionLedgerOf); `all_in_usd` the two. A replay or a shadow run carries none: its
|
|
398
|
-
* serves, so its `learning_usd` and `all_in_usd` are 0, and the sum of every run's `all_in_usd`
|
|
399
|
-
* `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null;
|
|
400
|
-
* unknown (its shadow still at work, a
|
|
356
|
+
* given, less its compile turns; null while unknown); `learning_usd` what learning spent for it: its follow-up rounds'
|
|
357
|
+
* turns (learningSpendOf less its compile turns, each its revision's), its audit shadow's cost, and its share of its
|
|
358
|
+
* revision's compile and check (revisionLedgerOf); `all_in_usd` the two. A replay or a shadow run carries none: its
|
|
359
|
+
* spend is on the runs it serves, so its `learning_usd` and `all_in_usd` are 0, and the sum of every run's `all_in_usd`
|
|
360
|
+
* and the bill's `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null;
|
|
361
|
+
* null while unknown (its shadow still at work, a compile's or a check's cost unknown), never $0. Undefined for an
|
|
401
362
|
* unknown run. */
|
|
402
363
|
export async function runAllInOverStore(store, runId, rowUsd) {
|
|
403
364
|
const run = await store.runs.get(runId);
|
|
@@ -410,9 +371,7 @@ export async function runAllInOverStore(store, runId, rowUsd) {
|
|
|
410
371
|
const path = !ended || kind === "workflow" ? null : (pathOfRow(run.metrics, started)?.path ?? null);
|
|
411
372
|
if (!ended)
|
|
412
373
|
return { path, usd: row, learning_usd: null, all_in_usd: null };
|
|
413
|
-
const
|
|
414
|
-
const shadow = (await shadowSpendOf(store, run.id)).usd;
|
|
415
|
-
const cost = runCostOf(row, looked, grade === null || shadow === null ? null : grade + shadow);
|
|
374
|
+
const cost = runCostOf(row, await learningSpendOf(store, run.id, row), (await shadowSpendOf(store, run.id)).usd);
|
|
416
375
|
if (path === "replay" || path === "audit" || isShadowRun(run.id))
|
|
417
376
|
return { path, usd: cost.cost_usd, learning_usd: 0, all_in_usd: 0 };
|
|
418
377
|
// Its share of its revision's compile and check: a served run's, over the agent's ledger.
|
|
@@ -439,19 +398,16 @@ export function servingReplayOf(records, runs) {
|
|
|
439
398
|
return { n: check.n, workflow_pct: Math.round(checkShares({ n: check.n, agreed, alone }).agreement * 1000) / 10, agent_pct: 100, cleared: true, agreed, alone, agree_min: check.agree_min };
|
|
440
399
|
}
|
|
441
400
|
/** What run `runId`'s learning spent, as its cost per run counts it, over every round a follow-up opened (roundsOf): what
|
|
442
|
-
* it kept as it answered (
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
* later round's follow-up turn and every round's look-back: feedback the run was given is learning, never the job) is
|
|
447
|
-
* its look-back here, `rowUsd` (the run's row cost) less that job (roundsLooked). */
|
|
401
|
+
* it kept as it answered (answeredOf). A run a follow-up opened again (roundsSpendOf's figures): its job is what it had
|
|
402
|
+
* cost when its first round answered, and everything after (each later round's follow-up turn and every round's
|
|
403
|
+
* look-back: feedback the run was given is learning, never the job) is its look-back here, `rowUsd` (the run's row cost)
|
|
404
|
+
* less that job (roundsLooked). */
|
|
448
405
|
export async function learningSpendOf(store, runId, rowUsd) {
|
|
449
406
|
const rounds = await roundsOf(store, runId);
|
|
450
407
|
if (!rounds.length)
|
|
451
|
-
return
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
return { looked: more ? roundsLooked(more, rowUsd) : answeredOf((rounds.at(-1).proposal ?? {})), grade: spent.grade };
|
|
408
|
+
return null;
|
|
409
|
+
const more = roundsSpendIn(rounds, roundsSpentOf(rounds));
|
|
410
|
+
return more ? roundsLooked(more, rowUsd) : answeredOf((rounds.at(-1).proposal ?? {}));
|
|
455
411
|
}
|
|
456
412
|
/** Every round run `runId` kept (one a follow-up opened again is a round past its first), oldest first: none when it kept
|
|
457
413
|
* no learning. The store's proposal(runId) is the newest round alone; a read of what the run's learning did or spent
|
|
@@ -469,35 +425,27 @@ export async function roundsOf(store, runId) {
|
|
|
469
425
|
out.push(newest);
|
|
470
426
|
return out;
|
|
471
427
|
}
|
|
472
|
-
/** Over a run's rounds (roundsOf): the first round that answered (its job's own figures)
|
|
473
|
-
* up
|
|
474
|
-
|
|
428
|
+
/** Over a run's rounds (roundsOf): the first round that answered (its job's own figures) and every round's look-back
|
|
429
|
+
* added up (null once one round's is unknown). */
|
|
430
|
+
function roundsSpentOf(rounds) {
|
|
475
431
|
let first;
|
|
476
432
|
let look = 0;
|
|
477
|
-
let grade = 0;
|
|
478
433
|
for (const kept of rounds) {
|
|
479
434
|
const answered = answeredOf((kept.proposal ?? {}));
|
|
480
435
|
if (!first && answered.answered_at !== undefined)
|
|
481
436
|
first = answered;
|
|
482
437
|
// A round that never looked back (no figures kept) added none.
|
|
483
438
|
look = look === null || answered.look_back_usd === null ? null : look + (answered.look_back_usd ?? 0);
|
|
484
|
-
const learningRun = kept.learningRunId ? await store.runs.get(kept.learningRunId) : undefined;
|
|
485
|
-
const spent = learningRun && FINAL_STATES.includes(learningRun.state) ? (learningRun.costUsd ?? null) : 0;
|
|
486
|
-
grade = grade === null || spent === null ? null : grade + spent;
|
|
487
439
|
}
|
|
488
|
-
return { first, look
|
|
440
|
+
return { first, look };
|
|
489
441
|
}
|
|
490
|
-
/** A run's own cost and what learning added for it (runCostOf): its row's cost, every round of its learning
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* the cost page shows and the money rule reads (pastRunsOverStore); null when any part is unknown. */
|
|
442
|
+
/** A run's own cost and what learning added for it (runCostOf): its row's cost, every round of its learning
|
|
443
|
+
* (learningSpendOf) and its audit's shadow run (shadowSpendOf). `total_usd` is the run's all-in cost, the one the cost page shows and the money rule reads
|
|
444
|
+
* (pastRunsOverStore); null when any part is unknown. */
|
|
494
445
|
export async function allInOf(store, run) {
|
|
495
|
-
// Every round's (learningSpendOf over the rounds: what its row spent after its job answered is learning, roundsLooked).
|
|
496
|
-
const { looked, grade } = await learningSpendOf(store, run.id, run.costUsd);
|
|
497
446
|
// An audit's shadow run of it (runner/audit.ts) is its learning too (shadowSpendOf): unknown while it is queued or
|
|
498
447
|
// running, never $0.
|
|
499
|
-
|
|
500
|
-
return runCostOf(run.costUsd, looked, grade === null || audited === null ? null : grade + audited);
|
|
448
|
+
return runCostOf(run.costUsd, await learningSpendOf(store, run.id, run.costUsd), (await shadowSpendOf(store, run.id)).usd);
|
|
501
449
|
}
|
|
502
450
|
/** What run `runId`'s audit shadow run (runner/audit.ts) adds to its all-in cost: `open` while it is queued or running
|
|
503
451
|
* (its cost not known yet: `usd` null, never $0), else its cost once it ended, stopped or failed (null: unknown); 0 when
|
|
@@ -510,16 +458,20 @@ export async function shadowSpendOf(store, runId) {
|
|
|
510
458
|
return { open: false, usd: 0 };
|
|
511
459
|
return FINAL_STATES.includes(shadow.state) ? { open: false, usd: shadow.costUsd ?? null } : { open: true, usd: null };
|
|
512
460
|
}
|
|
513
|
-
/** One run of the cost series, as number `n` of it: its figures (allInOf)
|
|
514
|
-
* and
|
|
461
|
+
/** One run of the cost series, as number `n` of it (null for a replay): its figures (allInOf), its path, the workflow
|
|
462
|
+
* revision that ran it and whether an audit's shadow ran for it (agentCostOverStore's loop body); `took_over` is the
|
|
463
|
+
* series' (tookOverOf). */
|
|
515
464
|
export async function costRunOf(store, run, n) {
|
|
516
465
|
const started = await learningStartOfRun(store, run.id);
|
|
517
|
-
//
|
|
466
|
+
// A run with no path named is placed as the agent's here, and left out by the series.
|
|
518
467
|
const { path, workflow } = pathOfRow(run.metrics, started) ?? { path: "agent", workflow: null };
|
|
519
468
|
const { cost_usd, learning_usd, total_usd } = await allInOf(store, run);
|
|
520
469
|
// The run row's stored bound (RunRow.costAtMostUsd, written with a null cost_usd): its cost counts a request cut at what its cap held.
|
|
521
470
|
const cost_upper_bound = typeof run.costAtMostUsd === "number";
|
|
522
|
-
|
|
471
|
+
// The revision its metrics name (pathMetrics): the bill's revisions are read the same way, offered or not.
|
|
472
|
+
const digest = path === "agent" ? null : revisionOfRow(run)?.digest ?? null;
|
|
473
|
+
const audited = (await store.runs.get(shadowRunId(run.id))) !== undefined;
|
|
474
|
+
return { n, run_id: run.id, started_at: new Date(run.startedAt).toISOString(), path, workflow, digest, audited, cost_usd, cost_upper_bound, learning_usd, total_usd };
|
|
523
475
|
}
|
|
524
476
|
/** The runs the money rule reads for agent `agentId` (runner/workflow-first.ts costLineOf), one set for the run's launch
|
|
525
477
|
* and the cost page: of its newest PAST_RUNS runs, each that ended (ended, stopped or failed), is neither imported nor
|
|
@@ -556,6 +508,10 @@ export async function pastRunsOverStore(store, agentId, known, records = store)
|
|
|
556
508
|
}
|
|
557
509
|
return past;
|
|
558
510
|
}
|
|
511
|
+
/** The money rule's decision for agent `agentId` (runner/workflow-first.ts costStopOf) over the runs a run's launch reads
|
|
512
|
+
* (pastRunsOverStore), as the launch reads them: runs that can't be read now stop nothing (workflowFirstOf serves). What
|
|
513
|
+
* the promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
|
|
514
|
+
export const costStopOverStore = async (store, agentId) => costStopOf(await pastRunsOverStore(store, agentId).catch(() => []));
|
|
559
515
|
/** The path run `runId` took and the learned workflow it ran, the one classifier its run record keeps (metrics.path,
|
|
560
516
|
* metrics.workflow, metrics.workflow_digest: workflow-first.ts pathMetrics, written at the run's end) and its cost per run
|
|
561
517
|
* reads (pathOfRow). A run its learned workflow served first (its WORKFLOW_FIRST entry, servedOf): `workflow` when it
|
|
@@ -597,27 +553,18 @@ function pathOfRow(metrics, started) {
|
|
|
597
553
|
const path = named ? m.path : "agent";
|
|
598
554
|
return { path, workflow: path === "agent" || typeof m.workflow !== "string" ? null : offeredName(m.workflow, started?.workflows) };
|
|
599
555
|
}
|
|
600
|
-
/** What a run's
|
|
601
|
-
*
|
|
602
|
-
|
|
603
|
-
async
|
|
604
|
-
|
|
605
|
-
const offered = !!(started?.skills?.length || started?.workflows?.length);
|
|
606
|
-
const journal = async () => {
|
|
607
|
-
const rows = final && started?.given.length ? await runTimeline(store, run.id, BACKGROUND_CONTEXT, { settled: final }) : [];
|
|
608
|
-
const entries = offered ? await runEntries(store, run.id, BACKGROUND_CONTEXT) : [];
|
|
609
|
-
return {
|
|
610
|
-
read: final && started?.given.length ? notesReadOf(started.given, await runReflect(store, run.id, BACKGROUND_CONTEXT), rows) : [],
|
|
611
|
-
laid: offered ? laidOf(entries) : undefined,
|
|
612
|
-
};
|
|
613
|
-
};
|
|
614
|
-
return final ? settledJournalReads(store, run.id, run.epoch, journal) : journal();
|
|
556
|
+
/** What a run's claims laid of the workflows it was offered (laidOf over its journal): read once for a settled run and kept
|
|
557
|
+
* by its epoch (settledLaid), else read now. */
|
|
558
|
+
async function journalLaidOf(store, run) {
|
|
559
|
+
const read = async () => laidOf(await runEntries(store, run.id, BACKGROUND_CONTEXT));
|
|
560
|
+
return FINAL_STATES.includes(run.state) ? settledLaid(store, run.id, run.epoch, read) : read();
|
|
615
561
|
}
|
|
616
562
|
/** `name` when it is one of the learned workflows the run's launch offered, else null. */
|
|
617
563
|
const offeredName = (name, offered) => (name !== null && offered?.includes(name) ? name : null);
|
|
618
564
|
const SETTLED_READS_MAX = 4096;
|
|
619
|
-
/** A cache of what a settled run's journal says, by store and run id with the run's epoch
|
|
620
|
-
*
|
|
565
|
+
/** A cache of what a settled run's journal says, by store and run id with the run's epoch: a claim and a run's end each
|
|
566
|
+
* move the epoch, and only a claim writes to its journal, so a settled run's reads hold until the next claim of it. At
|
|
567
|
+
* most SETTLED_READS_MAX runs a store, the oldest read dropped first. A read in flight is kept too, so two reads of the
|
|
621
568
|
* agent at once (its page and its tab) walk a run's journal once; one that fails is dropped, for the next to read. */
|
|
622
569
|
function settledCache() {
|
|
623
570
|
const byStore = new WeakMap();
|
|
@@ -638,35 +585,19 @@ function settledCache() {
|
|
|
638
585
|
return value;
|
|
639
586
|
};
|
|
640
587
|
}
|
|
641
|
-
const
|
|
642
|
-
|
|
643
|
-
/** Learning run `id`'s end entries (`run`: its row): read once it is final and kept (settledCache), so an agent's
|
|
644
|
-
* learning view reads each of its learning runs' journals once, not at every read; read now while it runs. */
|
|
645
|
-
async function learningEndEntries(store, id, run) {
|
|
646
|
-
const read = async () => {
|
|
647
|
-
const entries = await journalEntries(store.readJournal(id), BACKGROUND_CONTEXT);
|
|
648
|
-
const dataOf = (kind) => entries.find((entry) => entry.kind === kind)?.data;
|
|
649
|
-
return { merged: dataOf(KINDS.learningMerged), dropped: dataOf(KINDS.learningDropped), graded: dataOf(KINDS.learningGraded) };
|
|
650
|
-
};
|
|
651
|
-
return run && FINAL_STATES.includes(run.state) ? settledLearningEnds(store, id, run.epoch, read) : read();
|
|
652
|
-
}
|
|
653
|
-
/** What every claim of a run laid, from its learningLaid entries, each name once: a resumed claim finds an earlier
|
|
654
|
-
* claim's skill folders there already and lays none of them again, though the run still has them. None laid when it has
|
|
655
|
-
* no entry. */
|
|
588
|
+
const settledLaid = settledCache();
|
|
589
|
+
/** What every claim of a run laid, from its learningLaid entries, each name once. None laid when it has no entry. */
|
|
656
590
|
export function laidOf(entries) {
|
|
657
|
-
const names = (list) => (Array.isArray(list) ? list.filter((name) => typeof name === "string") : []);
|
|
658
|
-
const skills = new Set();
|
|
659
591
|
const workflows = new Set();
|
|
660
592
|
for (const entry of entries) {
|
|
661
593
|
if (entry.kind !== KINDS.learningLaid)
|
|
662
594
|
continue;
|
|
663
|
-
const
|
|
664
|
-
for (const name of
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
workflows.add(name);
|
|
595
|
+
const list = (entry.data ?? {}).workflows;
|
|
596
|
+
for (const name of Array.isArray(list) ? list : [])
|
|
597
|
+
if (typeof name === "string")
|
|
598
|
+
workflows.add(name);
|
|
668
599
|
}
|
|
669
|
-
return {
|
|
600
|
+
return { workflows: [...workflows] };
|
|
670
601
|
}
|
|
671
602
|
/** What a run with rounds (`rounds`) kept as it answered, as its cost per run and its impact both read it: its job is
|
|
672
603
|
* what it had cost when its first round answered; everything its row (`rowUsd`) spent after that (each later round's
|
|
@@ -680,40 +611,29 @@ export function roundsLooked(rounds, rowUsd) {
|
|
|
680
611
|
}
|
|
681
612
|
export async function roundsSpendOf(store, runId) {
|
|
682
613
|
const rounds = await roundsOf(store, runId);
|
|
683
|
-
return rounds.length < 2 ? undefined : roundsSpendIn(rounds,
|
|
614
|
+
return rounds.length < 2 ? undefined : roundsSpendIn(rounds, roundsSpentOf(rounds));
|
|
684
615
|
}
|
|
685
616
|
/** The one reader of a run's rounds' spend (roundsSpendOf, learningSpendOf): undefined for fewer than two rounds, or when
|
|
686
617
|
* the first one's answer time is unknown. */
|
|
687
|
-
const roundsSpendIn = (rounds, { first, look
|
|
688
|
-
/** What a run's learning end tells its impact
|
|
689
|
-
*
|
|
690
|
-
* not); its grade's cost once it ended (0 when nothing was graded). */
|
|
618
|
+
const roundsSpendIn = (rounds, { first, look }) => rounds.length > 1 && first && typeof first.answered_at === "string" ? { answered_at: first.answered_at, job_cost_usd: first.job_cost_usd ?? null, look_back_usd: look } : undefined;
|
|
619
|
+
/** What a run's learning end tells its impact: how it ended and when, the corrections and workflows it started from, when
|
|
620
|
+
* its job answered and its costs once it looked back (a look-back of 0 for an ended one that did not). */
|
|
691
621
|
export function impactLearningOf(end, started, laid, rounds, rowUsd = null) {
|
|
692
622
|
const over = end.state === "merged" || end.state === "dropped";
|
|
693
623
|
// A run a follow-up opened again: its job answered at its first round's end, and all its row spent after (every later
|
|
694
|
-
// round's follow-up turn and look-back)
|
|
695
|
-
// (roundsLooked; the newest round's `job_cost_usd` also holds the earlier rounds' look-backs, never the job's own).
|
|
624
|
+
// round's follow-up turn and look-back) is its learning, as its cost per run reads it (roundsLooked).
|
|
696
625
|
const looked = rounds ? roundsLooked(rounds, rowUsd) : end.answered_at !== undefined ? { answered_at: end.answered_at, job_cost_usd: end.job_cost_usd ?? null, look_back_usd: end.look_back_usd ?? null } : over ? { look_back_usd: 0 } : {};
|
|
626
|
+
const corrections = started ? correctionCount(started.section) : 0;
|
|
697
627
|
return {
|
|
698
|
-
state: end.state, reason: end.reason, at: end.at,
|
|
699
|
-
...(
|
|
700
|
-
...(over ? { grade_usd: rounds ? rounds.grade_usd : end.cost_usd ? end.cost_usd.grade : 0 } : {}),
|
|
628
|
+
state: end.state, reason: end.reason, at: end.at,
|
|
629
|
+
...(corrections ? { seed_corrections: corrections } : {}), ...(laid?.workflows.length ? { seed_workflows: laid.workflows.length } : {}), ...looked,
|
|
701
630
|
};
|
|
702
631
|
}
|
|
703
|
-
/** The notes a run's job read (learning-run.ts noteRead's rule, over the runner's journal rows): its own successful
|
|
704
|
-
* `read` calls on a file of its copy of the folder, the end-of-run turn's (`reflect`) left out, keyed on the notes it was
|
|
705
|
-
* `given` and named as given. A file it was not given (MEMORY.md, one it wrote, one not in its seed) is not one of them. */
|
|
706
|
-
export function notesReadOf(given, reflect, rows) {
|
|
707
|
-
if (!given?.length)
|
|
708
|
-
return [];
|
|
709
|
-
const names = new Map(given.map((note) => [note.file, note.name]));
|
|
710
|
-
return notesRead(reflect, rows).flatMap((file) => (names.has(file) ? [{ file, name: names.get(file) }] : []));
|
|
711
|
-
}
|
|
712
632
|
/** learned_workflow by every name a harness calls it (runner/learning-end.ts LEARNED_NAMES). */
|
|
713
633
|
const WORKFLOW_CALLS = new Set(["learned_workflow", "mcp__hands__learned_workflow"]);
|
|
714
634
|
/** Each tool call of a run's journal, by its call's id: its tool and the arguments it was called with, as recorded (pi's
|
|
715
635
|
* call in its assistant entry, a CLI harness's in its record's call start). A timeline row's input preview is cut and
|
|
716
|
-
* for some tools absent, so what a call
|
|
636
|
+
* for some tools absent, so what a call ran is read from these. */
|
|
717
637
|
export function callArgsOf(entries) {
|
|
718
638
|
const out = new Map();
|
|
719
639
|
const argsOf = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
|
|
@@ -732,108 +652,29 @@ export function callArgsOf(entries) {
|
|
|
732
652
|
}
|
|
733
653
|
return out;
|
|
734
654
|
}
|
|
735
|
-
/** The
|
|
736
|
-
|
|
737
|
-
const CLAUDE_SKILLS_PLUGIN = "agentrun";
|
|
738
|
-
/** The id ranges of a run's end-of-run turns, each from its prompt (an endOfRunTurn entry, `role: "person"`) to its
|
|
739
|
-
* answer (`role: "model"`); one with no answer runs to the person's next message (a reopened run's follow-up), else to
|
|
740
|
-
* the end. A reopened run has one per time it looked back. */
|
|
741
|
-
function endOfRunTurns(entries) {
|
|
742
|
-
const turns = [];
|
|
743
|
-
let open;
|
|
744
|
-
for (const entry of [...entries].filter((one) => one.id !== undefined).sort((a, b) => a.id - b.id)) {
|
|
745
|
-
const role = entry.data?.role;
|
|
746
|
-
if (entry.kind === KINDS.endOfRunTurn && role === "person")
|
|
747
|
-
open ??= entry.id;
|
|
748
|
-
else if (open !== undefined && ((entry.kind === KINDS.endOfRunTurn && role === "model") || (entry.kind === KINDS.message && role === "person"))) {
|
|
749
|
-
turns.push([open, entry.id]);
|
|
750
|
-
open = undefined;
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
if (open !== undefined)
|
|
754
|
-
turns.push([open, Number.POSITIVE_INFINITY]);
|
|
755
|
-
return turns;
|
|
756
|
-
}
|
|
757
|
-
/** The skills a run's job loaded with a harness's own Skill tool (claude-agent's): a native call, which the CLI's session
|
|
758
|
-
* records as a kernel step of its own (`kernel.start` named `tool.Skill`, keyed `tool:<call id>`, then its `kernel.end`)
|
|
759
|
-
* and never as a host tool's call record, so no timeline row or callArgsOf entry has it. Each by the name the model
|
|
760
|
-
* gave (`agentrun:<name>` or the bare one), only when its step completed, and only outside every end-of-run turn (a
|
|
761
|
-
* reopened run's follow-up's loads are the job's). */
|
|
762
|
-
export function nativeSkillLoads(entries) {
|
|
763
|
-
const turns = endOfRunTurns(entries);
|
|
764
|
-
const inTurn = (id) => id !== undefined && turns.some(([from, to]) => id >= from && id <= to);
|
|
765
|
-
const asked = new Map();
|
|
766
|
-
const loaded = [];
|
|
767
|
-
for (const entry of entries) {
|
|
768
|
-
if (inTurn(entry.id))
|
|
769
|
-
continue;
|
|
770
|
-
const data = (entry.data ?? {});
|
|
771
|
-
if (entry.kind === "kernel.start" && data.name === "tool.Skill" && typeof data.stepId === "string") {
|
|
772
|
-
const skill = data.input?.skill;
|
|
773
|
-
if (typeof skill === "string")
|
|
774
|
-
asked.set(data.stepId, skill);
|
|
775
|
-
}
|
|
776
|
-
else if (entry.kind === "kernel.end" && typeof data.stepId === "string" && data.status === "completed" && asked.has(data.stepId)) {
|
|
777
|
-
loaded.push(asked.get(data.stepId));
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
return loaded;
|
|
781
|
-
}
|
|
782
|
-
/** Whether a skill loaded by the name `asked` is skill `name`: by its plugin-qualified name or its bare one. */
|
|
783
|
-
const loadsSkill = (asked, name) => asked === name || asked === `${CLAUDE_SKILLS_PLUGIN}:${name}`;
|
|
784
|
-
/** Where a claim lays a learned skill's SKILL.md in the run's work (learning.ts RUN_SKILLS). */
|
|
785
|
-
const laidSkillPath = (name) => `.agents/skills/${name}/SKILL.md`;
|
|
786
|
-
/** Whether call `call` read skill `name`'s SKILL.md: the read hand on the path its claim laid it at (`.agents/skills/
|
|
787
|
-
* <name>/SKILL.md` in the run's work, relative or absolute under it, never another file whose path ends the same way), a
|
|
788
|
-
* CLI adapter's read_skill_file of the skill's SKILL.md, or a Skill tool call on the skill. */
|
|
789
|
-
function readsSkill(call, name) {
|
|
790
|
-
const text = (key) => (typeof call.args[key] === "string" ? call.args[key] : "");
|
|
791
|
-
if (call.tool === "read" || call.tool === "mcp__hands__read") {
|
|
792
|
-
const path = posix.normalize(text("path"));
|
|
793
|
-
return path === laidSkillPath(name) || (path.startsWith("/") && path.endsWith(`/${laidSkillPath(name)}`));
|
|
794
|
-
}
|
|
795
|
-
if (call.tool === "read_skill_file")
|
|
796
|
-
return text("skill") === name && posix.normalize(text("path")) === "SKILL.md";
|
|
797
|
-
if (call.tool === "Skill")
|
|
798
|
-
return [text("skill"), text("command"), text("name")].some((asked) => loadsSkill(asked, name));
|
|
799
|
-
return false;
|
|
800
|
-
}
|
|
801
|
-
/** `laid`: what the run's claims laid (laidOf): a skill its launch offered but no claim laid (over the budget, or a name
|
|
802
|
-
* the revision's own skill holds, whose copy is the one installed) is not the run's learned skill, whatever it read.
|
|
803
|
-
* `loads`: the skills its job loaded with a harness's own Skill tool (nativeSkillLoads), each a read of its SKILL.md. */
|
|
804
|
-
export function learnedUseOf(started, reflect, rows, args = new Map(), laid = { skills: [] }, loads = []) {
|
|
655
|
+
/** The learned workflows a run's job called (LearnedUse), over its timeline rows less the end-of-run turn's (`reflect`). */
|
|
656
|
+
export function learnedUseOf(reflect, rows, args = new Map()) {
|
|
805
657
|
const job = rows.filter((row) => !reflects(reflect, row.id));
|
|
806
658
|
const results = new Map(job.flatMap((row) => (row.kind === "tool_result" && row.tool_use_id ? [[row.tool_use_id, row.tool_status]] : [])));
|
|
807
659
|
const models = modelsOwn(job);
|
|
808
|
-
const calls = job.filter((row) => row.kind === "tool_use" && models(row) && row.tool_use_id).flatMap((row) => {
|
|
809
|
-
const call = args.get(row.tool_use_id);
|
|
810
|
-
return call ? [{ id: row.tool_use_id, ...call }] : [];
|
|
811
|
-
});
|
|
812
|
-
const installed = new Set(laid.skills);
|
|
813
|
-
const skills = (started?.skills ?? []).filter((name) => installed.has(name)).map((name) => ({ name, read: calls.some((call) => results.get(call.id) === "success" && readsSkill(call, name)) || loads.some((asked) => loadsSkill(asked, name)) }));
|
|
814
660
|
const workflows = new Map();
|
|
815
|
-
for (const
|
|
816
|
-
if (!
|
|
661
|
+
for (const row of job) {
|
|
662
|
+
if (row.kind !== "tool_use" || !models(row) || !row.tool_use_id)
|
|
663
|
+
continue;
|
|
664
|
+
const call = args.get(row.tool_use_id);
|
|
665
|
+
if (!call || !WORKFLOW_CALLS.has(call.tool) || typeof call.args.name !== "string")
|
|
817
666
|
continue;
|
|
818
667
|
const seen = workflows.get(call.args.name) ?? { calls: 0, failed: 0 };
|
|
819
668
|
seen.calls += 1;
|
|
820
669
|
// Only a result that says so: a call with no result yet (one still running) is no failure.
|
|
821
|
-
if (results.get(
|
|
670
|
+
if (results.get(row.tool_use_id) === "failed")
|
|
822
671
|
seen.failed += 1;
|
|
823
672
|
workflows.set(call.args.name, seen);
|
|
824
673
|
}
|
|
825
|
-
return {
|
|
826
|
-
}
|
|
827
|
-
/** `end` with its run's given notes (from its launch's learning start) and the notes its job read. */
|
|
828
|
-
export function withNotes(end, started, read) {
|
|
829
|
-
return {
|
|
830
|
-
...end,
|
|
831
|
-
...(started ? { given: started.given.map((note) => ({ file: note.file, name: note.name })) } : {}),
|
|
832
|
-
...(read.length ? { read: [...read] } : {}),
|
|
833
|
-
};
|
|
674
|
+
return { workflows: [...workflows].map(([name, seen]) => ({ name, ...seen })) };
|
|
834
675
|
}
|
|
835
|
-
/** Run `runId`'s learning start as its launch records it (run-launch.ts learningStartOf, the one reader): its
|
|
836
|
-
* its
|
|
676
|
+
/** Run `runId`'s learning start as its launch records it (run-launch.ts learningStartOf, the one reader): its seed sha,
|
|
677
|
+
* its # Learned section and its workflows; undefined when the run does not learn or has no launch. */
|
|
837
678
|
async function learningStartOfRun(store, runId) {
|
|
838
679
|
// The agent's narrow read (agentRecordsOf) answers when the run is in it; a missing launch reads as a run that does not learn.
|
|
839
680
|
if (store.runs.learningStart)
|
|
@@ -845,19 +686,18 @@ async function learningStartOfRun(store, runId) {
|
|
|
845
686
|
/** The learning of a run that only uses what its agent learned (`learning: "use"`): seeded while it runs, as a run that
|
|
846
687
|
* learns is; dropped `use` once it ended, since it is never asked what to keep. */
|
|
847
688
|
export function usedOnly(started, terminal, at) {
|
|
848
|
-
// A start that could not be made (a folder unreadable
|
|
849
|
-
//
|
|
689
|
+
// A start that could not be made (a folder unreadable) handed the run nothing: said so from its start, as the end hook
|
|
690
|
+
// says it for a run that learns, never "seeded" and then `use`. No proposal is written for it.
|
|
850
691
|
if (seedFailed(started)) {
|
|
851
692
|
const why = started.seedFailed === "unreadable" ? STOPPED.unreadable : started.seedFailed === "not_copied" ? STOPPED.notCopied : STOPPED.noSeed;
|
|
852
693
|
return { state: "dropped", reason: "seed_failed", detail: said.stopped(why), at };
|
|
853
694
|
}
|
|
854
|
-
return terminal ? { state: "dropped", reason: "use", detail: said.use, at } : { state: "seeded", reason: null, detail: said.seeded(started.
|
|
695
|
+
return terminal ? { state: "dropped", reason: "use", detail: said.use, at } : { state: "seeded", reason: null, detail: said.seeded(correctionCount(started.section), started.workflows?.length ?? 0), at };
|
|
855
696
|
}
|
|
856
697
|
/** GET /runs/{id}/learning from the one store, in the shape the route gives (learning-routes.ts runLearningBody): the
|
|
857
|
-
* run's learning end (runLearningEnd)
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
* no learning. */
|
|
698
|
+
* run's learning end (runLearningEnd) and the seed it started from as its launch records it, what it held for its
|
|
699
|
+
* owner's OK as it stands on its agent's folder's main (`statuses`, learning-routes.ts statusesOf, read only when it held
|
|
700
|
+
* something; none: as the merge wrote it), settled once the run is final. Null when the run kept no learning. */
|
|
861
701
|
export async function runLearningOverStore(store, runId, shown, secrets, statuses) {
|
|
862
702
|
const started = await learningStartOfRun(store, runId);
|
|
863
703
|
const row = await store.runs.get(runId);
|
|
@@ -873,12 +713,10 @@ export async function runLearningOverStore(store, runId, shown, secrets, statuse
|
|
|
873
713
|
if (promoted !== undefined)
|
|
874
714
|
end.compiled = { ...end.compiled, promoted };
|
|
875
715
|
}
|
|
876
|
-
const used = started?.
|
|
716
|
+
const used = !!started?.workflows?.length;
|
|
877
717
|
const [reflect, rows, entries] = used ? await Promise.all([runReflect(store, runId, BACKGROUND_CONTEXT), runTimeline(store, runId, BACKGROUND_CONTEXT, { settled }), runEntries(store, runId, BACKGROUND_CONTEXT)]) : [undefined, [], []];
|
|
878
|
-
const
|
|
879
|
-
const
|
|
880
|
-
const current = holdsForOk(learning) && row && statuses ? currentLearning(learning, await statuses(row.agentId)) : learning;
|
|
881
|
-
const body = runLearningBody({ id: runId, learning: current, seed: started?.seed ?? null, settled, used: learnedUseOf(started, reflect, rows, callArgsOf(entries), laidOf(entries), nativeSkillLoads(entries)) }, shown, secrets);
|
|
718
|
+
const current = holdsForOk(end) && row && statuses ? currentLearning(end, await statuses(row.agentId)) : end;
|
|
719
|
+
const body = runLearningBody({ id: runId, learning: current, seed: started?.seed ?? null, settled, used: learnedUseOf(reflect, rows, callArgsOf(entries)) }, shown, secrets);
|
|
882
720
|
// The run's own grade (PUT /runs/{id}/grade), as its agent keeps it and the PUT answered it, with the round it opened.
|
|
883
721
|
// Never its `expected` (a person's words, uncut), nor its label (not kept). Its workflow's name is cut as every other
|
|
884
722
|
// learning name is (`shown.name`): a secret registered after the grade never shows here.
|