@parcha/agentrun-serve 0.1.0-beta.57 → 0.1.0-beta.59
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 +376 -0
- package/README.md +393 -118
- package/dist/acp-package.d.ts +5 -1
- package/dist/acp-package.js +19 -2
- package/dist/adapters.d.ts +23 -2
- package/dist/adapters.js +106 -33
- package/dist/agents-store.js +1 -1
- package/dist/answer-text.d.ts +3 -0
- package/dist/answer-text.js +22 -0
- package/dist/api-only.d.ts +2 -4
- package/dist/api-only.js +2 -4
- package/dist/attach-conformance.js +5 -4
- package/dist/box-paths.d.ts +7 -5
- package/dist/box-paths.js +12 -8
- package/dist/browser-computer.d.ts +3 -8
- package/dist/browser-computer.js +12 -35
- package/dist/browser-cost.js +94 -20
- package/dist/capacity.d.ts +1 -1
- package/dist/capacity.js +3 -3
- package/dist/chatgpt-plan.d.ts +5 -0
- package/dist/chatgpt-plan.js +25 -11
- package/dist/claude-login-token.d.ts +121 -0
- package/dist/claude-login-token.js +797 -0
- package/dist/claude-login.d.ts +119 -34
- package/dist/claude-login.js +551 -79
- package/dist/claude-sign-in.d.ts +3 -0
- package/dist/claude-sign-in.js +10 -1
- package/dist/cli.js +325 -328
- package/dist/composio-made.d.ts +1 -6
- package/dist/composio-made.js +1 -6
- package/dist/composio-owners.d.ts +26 -0
- package/dist/composio-owners.js +36 -0
- package/dist/composio-preset.d.ts +56 -23
- package/dist/composio-preset.js +173 -77
- package/dist/composio-triggers.d.ts +4 -4
- package/dist/composio-triggers.js +6 -7
- package/dist/compute-cost.js +11 -2
- package/dist/computer-attach.d.ts +40 -19
- package/dist/computer-attach.js +326 -62
- package/dist/computer-held-routes.d.ts +1 -1
- package/dist/computer-held-routes.js +9 -4
- package/dist/computer-keys.d.ts +11 -1
- package/dist/computer-keys.js +26 -1
- package/dist/computer-record.js +3 -1
- package/dist/computer-view.d.ts +5 -3
- package/dist/computer-view.js +10 -6
- package/dist/computer-viewer.js +36 -7
- package/dist/computers.d.ts +12 -11
- package/dist/computers.js +11 -8
- package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
- package/dist/console/assets/backend-BztxEAvb.js +166 -0
- package/dist/console/assets/index-LwNL4L9q.js +619 -0
- package/dist/console/assets/share-L84tlUDW.js +6 -0
- package/dist/console/assets/styles-CaRKlHke.js +291 -0
- package/dist/console/assets/styles-DQo8rzY-.css +1 -0
- package/dist/console/index.html +4 -3
- package/dist/console/share.html +17 -0
- package/dist/decisions.d.ts +17 -7
- package/dist/decisions.js +41 -15
- package/dist/dial-relay.d.ts +25 -4
- package/dist/dial-relay.js +139 -5
- package/dist/docker-daemon.js +4 -5
- package/dist/docker-host-addresses.js +29 -11
- package/dist/email/account.d.ts +6 -0
- package/dist/email/account.js +6 -0
- package/dist/email/authentication.d.ts +21 -0
- package/dist/email/authentication.js +106 -0
- package/dist/email/email-faux.d.ts +11 -2
- package/dist/email/email-faux.js +21 -5
- package/dist/email/mail.d.ts +25 -0
- package/dist/email/mail.js +58 -2
- package/dist/email/names.js +2 -2
- package/dist/email/tools.d.ts +9 -4
- package/dist/email/tools.js +95 -13
- package/dist/env-switch.d.ts +13 -5
- package/dist/env-switch.js +24 -6
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +12 -4
- package/dist/fal-price.js +110 -26
- package/dist/faux-computers.js +1 -1
- package/dist/filed-answer.d.ts +2 -2
- package/dist/filed-answer.js +2 -2
- package/dist/graph-served.d.ts +10 -5
- package/dist/graph-served.js +40 -10
- package/dist/handed-values.js +1 -2
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +45 -12
- package/dist/harness-keys.js +9 -6
- package/dist/harness-opencode/facts.js +2 -0
- package/dist/harness-recipe-entry.d.ts +9 -3
- package/dist/harness-recipe-entry.js +18 -9
- package/dist/harness-recipe.d.ts +21 -0
- package/dist/harness-recipe.js +25 -3
- package/dist/harness-table.d.ts +8 -0
- package/dist/harness-table.js +17 -6
- package/dist/helper-env.d.ts +4 -1
- package/dist/helper-env.js +4 -1
- package/dist/host-check.d.ts +2 -3
- package/dist/host-check.js +13 -7
- package/dist/hosted-search.js +1 -1
- package/dist/import.d.ts +11 -8
- package/dist/import.js +437 -186
- package/dist/key-refusal.d.ts +9 -2
- package/dist/key-refusal.js +86 -32
- package/dist/learn/audit.d.ts +8 -5
- package/dist/learn/audit.js +15 -7
- package/dist/learn/compile.d.ts +5 -2
- package/dist/learn/compile.js +45 -21
- package/dist/learn/field-check.d.ts +111 -0
- package/dist/learn/field-check.js +174 -0
- package/dist/learn/ledger.d.ts +173 -0
- package/dist/learn/ledger.js +225 -0
- package/dist/learn/promote.d.ts +82 -39
- package/dist/learn/promote.js +61 -66
- package/dist/learn/secrets.d.ts +4 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learned-nodes.d.ts +8 -5
- package/dist/learned-nodes.js +8 -5
- package/dist/learned-workflow.d.ts +2 -1
- package/dist/learned-workflow.js +13 -10
- package/dist/learning-cost.d.ts +40 -21
- package/dist/learning-cost.js +30 -21
- package/dist/learning-setting.d.ts +6 -0
- package/dist/learning-setting.js +11 -1
- package/dist/listen-first.d.ts +5 -4
- package/dist/listen-first.js +4 -5
- package/dist/live-reach.d.ts +9 -0
- package/dist/live-reach.js +9 -0
- package/dist/mcp-api-only-origins.d.ts +23 -3
- package/dist/mcp-api-only-origins.js +57 -17
- package/dist/mcp-gateway.d.ts +16 -2
- package/dist/mcp-gateway.js +41 -3
- package/dist/mcp-oauth.d.ts +1 -1
- package/dist/mcp-oauth.js +3 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +3 -0
- package/dist/mcp-secret-writes.d.ts +24 -2
- package/dist/mcp-secret-writes.js +30 -2
- package/dist/mcp-servers-store.d.ts +52 -14
- package/dist/mcp-servers-store.js +165 -61
- package/dist/mcp-sources.d.ts +1 -2
- package/dist/mcp-sources.js +1 -2
- package/dist/mcp-store.d.ts +5 -5
- package/dist/mcp-store.js +3 -3
- package/dist/mcp.js +1 -1
- package/dist/micro.d.ts +2 -0
- package/dist/micro.js +2 -0
- package/dist/model-gateway.d.ts +1 -1
- package/dist/model-gateway.js +20 -36
- package/dist/model.d.ts +5 -4
- package/dist/model.js +7 -6
- package/dist/monid.d.ts +14 -1
- package/dist/monid.js +103 -26
- package/dist/ollama-provider.d.ts +23 -9
- package/dist/ollama-provider.js +49 -20
- package/dist/openapi.js +234 -61
- package/dist/otel/genai.d.ts +83 -7
- package/dist/otel/genai.js +69 -9
- package/dist/otel/tracer.d.ts +42 -13
- package/dist/otel/tracer.js +241 -56
- package/dist/page.d.ts +7 -1
- package/dist/page.js +76 -22
- package/dist/paid-calls.d.ts +6 -4
- package/dist/paid-calls.js +15 -7
- package/dist/place-offers.d.ts +59 -34
- package/dist/place-offers.js +100 -58
- package/dist/place.d.ts +13 -14
- package/dist/place.js +56 -41
- package/dist/print.js +1 -1
- package/dist/provider.d.ts +5 -4
- package/dist/provider.js +26 -13
- package/dist/redact/match.d.ts +27 -9
- package/dist/redact/match.js +266 -57
- package/dist/run-census.d.ts +1 -1
- package/dist/run-census.js +4 -5
- package/dist/run-cut.js +4 -5
- package/dist/run-export-store.d.ts +15 -0
- package/dist/run-export-store.js +30 -0
- package/dist/run-export.d.ts +139 -0
- package/dist/run-export.js +379 -0
- package/dist/run-file-cut.d.ts +5 -0
- package/dist/run-file-cut.js +13 -0
- package/dist/run-launch.js +16 -3
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-model-key.d.ts +15 -6
- package/dist/run-model-key.js +30 -31
- package/dist/run-reads-over-store.js +4 -4
- package/dist/run-secrets.js +3 -5
- package/dist/run-timeline-served.js +3 -1
- package/dist/run-view.d.ts +1 -2
- package/dist/run-view.js +2 -1
- package/dist/runnable.js +2 -2
- package/dist/runner/adapter-loop.d.ts +35 -18
- package/dist/runner/adapter-loop.js +97 -63
- package/dist/runner/adapter-run-spec.d.ts +34 -14
- package/dist/runner/adapter-run-spec.js +120 -75
- package/dist/runner/adapters/pi.js +4 -2
- package/dist/runner/audit.d.ts +8 -5
- package/dist/runner/audit.js +37 -17
- package/dist/runner/beside.d.ts +9 -2
- package/dist/runner/beside.js +29 -13
- package/dist/runner/claim-secrets.d.ts +27 -18
- package/dist/runner/claim-secrets.js +61 -47
- package/dist/runner/cli-run-spec.d.ts +18 -0
- package/dist/runner/cli-run-spec.js +93 -46
- package/dist/runner/compare-end.d.ts +2 -1
- package/dist/runner/compare-end.js +25 -10
- package/dist/runner/conformance/harness.js +2 -3
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/harness-loops.js +3 -3
- package/dist/runner/held.d.ts +19 -4
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/journal-kinds.js +22 -4
- package/dist/runner/learned-first.js +20 -4
- package/dist/runner/learning-end.d.ts +2 -8
- package/dist/runner/learning-end.js +11 -19
- package/dist/runner/live-ticker.js +7 -5
- package/dist/runner/loop-contract.d.ts +0 -2
- package/dist/runner/loop.d.ts +7 -0
- package/dist/runner/loop.js +1 -0
- package/dist/runner/main.d.ts +11 -6
- package/dist/runner/main.js +24 -18
- package/dist/runner/mcp-claim.js +24 -7
- package/dist/runner/mcp-run.d.ts +3 -6
- package/dist/runner/mcp-run.js +3 -4
- package/dist/runner/meta-tools.js +3 -2
- package/dist/runner/metrics.d.ts +7 -0
- package/dist/runner/metrics.js +19 -0
- package/dist/runner/model-route.d.ts +10 -7
- package/dist/runner/model-route.js +79 -15
- package/dist/runner/native-loop.js +10 -8
- package/dist/runner/presence.d.ts +11 -4
- package/dist/runner/presence.js +11 -6
- package/dist/runner/promotion.d.ts +25 -13
- package/dist/runner/promotion.js +125 -50
- package/dist/runner/redact.d.ts +3 -1
- package/dist/runner/redact.js +5 -3
- package/dist/runner/reflect-hook.js +1 -1
- package/dist/runner/responses-fetch.d.ts +36 -0
- package/dist/runner/responses-fetch.js +511 -0
- package/dist/runner/run-child-host.d.ts +38 -12
- package/dist/runner/run-child-host.js +231 -83
- package/dist/runner/run-child.js +11 -4
- package/dist/runner/runner-check.js +5 -1
- package/dist/runner/runner.d.ts +5 -3
- package/dist/runner/runner.js +14 -10
- package/dist/runner/save-slots.js +2 -1
- package/dist/runner/sdk-loop.js +8 -8
- package/dist/runner/spec-holds.d.ts +8 -1
- package/dist/runner/spec-holds.js +9 -4
- package/dist/runner/subagent.d.ts +11 -0
- package/dist/runner/subagent.js +17 -3
- package/dist/runner/workflow-calls.d.ts +9 -0
- package/dist/runner/workflow-calls.js +150 -0
- package/dist/runner/workflow-first.d.ts +138 -37
- package/dist/runner/workflow-first.js +230 -99
- package/dist/runner/workflow-spec.js +2 -2
- package/dist/runs-over-store.js +8 -7
- package/dist/save-once.d.ts +14 -0
- package/dist/save-once.js +31 -0
- package/dist/scrub-command.d.ts +34 -31
- package/dist/scrub-command.js +116 -147
- package/dist/sdk/runs.d.ts +6 -0
- package/dist/sdk/runs.js +89 -10
- package/dist/seal-key.js +4 -4
- package/dist/secret-cut.d.ts +3 -0
- package/dist/secret-cut.js +7 -1
- package/dist/secret-rules.d.ts +8 -1
- package/dist/secret-rules.js +482 -53
- package/dist/secrets.d.ts +51 -9
- package/dist/secrets.js +311 -21
- package/dist/server-keys.d.ts +4 -3
- package/dist/server-keys.js +4 -3
- package/dist/server-secrets-store.js +2 -4
- package/dist/server.d.ts +33 -10
- package/dist/server.js +454 -136
- package/dist/share-context.d.ts +22 -0
- package/dist/share-context.js +76 -0
- package/dist/shell-lex.d.ts +105 -0
- package/dist/shell-lex.js +646 -0
- package/dist/sign-ins-store.d.ts +5 -7
- package/dist/sign-ins-store.js +12 -16
- package/dist/sign-ins.d.ts +7 -11
- package/dist/sign-ins.js +40 -41
- package/dist/skills/agentrun-serve/SKILL.md +5 -2
- package/dist/slots.d.ts +9 -0
- package/dist/slots.js +45 -0
- package/dist/start-switches.d.ts +6 -5
- package/dist/start-switches.js +8 -4
- package/dist/stats.d.ts +5 -1
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.js +2 -2
- package/dist/store/dialect.d.ts +4 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/mcp-record-keys.d.ts +10 -0
- package/dist/store/mcp-record-keys.js +13 -0
- package/dist/store/postgres/dialect.js +2 -0
- package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
- package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
- package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
- package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
- package/dist/store/postgres/store.js +4 -0
- package/dist/store/schema.d.ts +44 -4
- package/dist/store/schema.js +111 -11
- package/dist/store/seal.d.ts +4 -1
- package/dist/store/sqlite/run-launch.d.ts +3 -10
- package/dist/store/sqlite/run-launch.js +12 -23
- package/dist/store/sqlite/run-patch.d.ts +19 -2
- package/dist/store/sqlite/run-patch.js +67 -2
- package/dist/store/sqlite/store.d.ts +3 -0
- package/dist/store/sqlite/store.js +177 -32
- package/dist/store/store-schema.js +3 -4
- package/dist/store/store.d.ts +74 -25
- package/dist/store/store.js +14 -11
- package/dist/store-reads/analytics.d.ts +2 -1
- package/dist/store-reads/analytics.js +7 -5
- package/dist/store-reads/learning.d.ts +102 -136
- package/dist/store-reads/learning.js +251 -302
- package/dist/store-reads/stats.d.ts +2 -2
- package/dist/store-reads/stats.js +13 -7
- package/dist/store-view.js +10 -7
- package/dist/suggestions.d.ts +1 -1
- package/dist/suggestions.js +29 -13
- package/dist/template-eval.d.ts +91 -9
- package/dist/template-eval.js +448 -105
- package/dist/template-import.d.ts +0 -10
- package/dist/template-import.js +5 -10
- package/dist/timeline/labels.d.ts +1 -1
- package/dist/timeline/labels.js +4 -3
- package/dist/timeline/timeline.d.ts +23 -11
- package/dist/timeline/timeline.js +115 -31
- package/dist/timeline/web-calls.d.ts +14 -14
- package/dist/timeline/web-calls.js +18 -30
- package/dist/tool-gate.d.ts +13 -1
- package/dist/tool-gate.js +43 -3
- package/dist/tool-keys-store.js +2 -4
- package/dist/tool-keys.d.ts +11 -0
- package/dist/tool-keys.js +16 -0
- package/dist/tool-plane.d.ts +50 -11
- package/dist/tool-plane.js +94 -30
- package/dist/tools.d.ts +5 -1
- package/dist/tools.js +7 -7
- package/dist/trigger-email.js +12 -6
- package/dist/triggers.d.ts +15 -34
- package/dist/triggers.js +119 -126
- package/dist/validate.d.ts +8 -1
- package/dist/validate.js +163 -23
- package/dist/web-fake.d.ts +2 -1
- package/dist/web-fake.js +8 -6
- package/dist/web-fetch.d.ts +8 -6
- package/dist/web-fetch.js +13 -11
- package/dist/web-holds.d.ts +32 -0
- package/dist/web-holds.js +86 -0
- package/dist/web-run.d.ts +32 -5
- package/dist/web-run.js +75 -28
- package/dist/web-save.d.ts +4 -9
- package/dist/web-save.js +27 -55
- package/dist/web-search.js +14 -25
- package/dist/wires.d.ts +4 -3
- package/dist/wires.js +5 -4
- package/dist/workspace/archil-claim.d.ts +12 -5
- package/dist/workspace/archil-claim.js +45 -5
- package/dist/workspace/attach.js +52 -19
- package/dist/workspace/attached-computers.d.ts +38 -11
- package/dist/workspace/attached-computers.js +81 -16
- package/dist/workspace/computer-deps.d.ts +29 -7
- package/dist/workspace/computer-deps.js +36 -18
- package/dist/workspace/computer-for-claim.d.ts +21 -4
- package/dist/workspace/computer-for-claim.js +166 -57
- package/dist/workspace/computer-intervals.d.ts +2 -0
- package/dist/workspace/computer-intervals.js +1 -1
- package/dist/workspace/computer-janitor.d.ts +48 -14
- package/dist/workspace/computer-janitor.js +153 -44
- package/dist/workspace/computer-kept.d.ts +158 -32
- package/dist/workspace/computer-kept.js +288 -63
- package/dist/workspace/computer-providers.d.ts +8 -3
- package/dist/workspace/computer-providers.js +12 -5
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/local.js +41 -1
- package/dist/workspace/mount-revoke-left.d.ts +31 -0
- package/dist/workspace/mount-revoke-left.js +72 -0
- package/dist/workspace/paired-route.d.ts +91 -0
- package/dist/workspace/paired-route.js +152 -0
- package/dist/workspace/resolve-computer.d.ts +5 -2
- package/dist/workspace/resolve-computer.js +9 -3
- package/dist/workspace/run-end-computers.d.ts +11 -5
- package/dist/workspace/run-end-computers.js +153 -28
- package/dist/workspace/run-tools.d.ts +22 -3
- package/dist/workspace/run-tools.js +17 -5
- package/dist/workspace/run-workspace.d.ts +14 -1
- package/dist/workspace/run-workspace.js +34 -1
- package/dist/workspace/workspace.d.ts +6 -0
- package/dist/wrapped.d.ts +8 -2
- package/dist/wrapped.js +21 -6
- package/package.json +12 -13
- package/skills/agentrun-serve/SKILL.md +5 -2
- package/templates/README.md +25 -9
- package/dist/console/assets/backend-DVhHzJX2.js +0 -166
- package/dist/console/assets/index-Bwn1oTem.css +0 -1
- package/dist/console/assets/index-okP-y8PK.js +0 -747
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Checking a learned workflow, field by field, against the best answer each field has: a grade's expected answer, else
|
|
2
|
+
// the agent's own answer. Pure: rows and a bar in, a verdict out; no store, no settings, no secrets, no prose beyond
|
|
3
|
+
// each field's numbers in words. The caller turns its replays (or native turns) into rows, and stores and words what
|
|
4
|
+
// comes back (runner/promotion.ts decideCheck).
|
|
5
|
+
//
|
|
6
|
+
// A field passes when, on the inputs the workflow answered alone, its answers are right against the known answer, over
|
|
7
|
+
// all of its known inputs at once (pooled across where each input's known answer came from):
|
|
8
|
+
// - at least `agree_min` of them right;
|
|
9
|
+
// - and, on the inputs a grade gave the answer of, at least `agree_min` of them right and at most MARGIN under the
|
|
10
|
+
// agent's own accuracy on them, one miss always allowed in each (a correction, where the agent was wrong, gives no
|
|
11
|
+
// free pass; one input missed is never a hold on its own).
|
|
12
|
+
// Each row's field is judged against its own best known answer, so a field with grades on some inputs is judged against
|
|
13
|
+
// the grades there and against the agent elsewhere; one graded input missed among many the agent knows is one input of
|
|
14
|
+
// the field's floor, not a source of its own that fails. An input with no known answer on a field (the
|
|
15
|
+
// agent's answer graded wrong with none given, and the workflow said another) is unjudged there: never right, so never
|
|
16
|
+
// right on every field at once, and a field judged on fewer than `alone_min` of the inputs that carried it fails. The
|
|
17
|
+
// workflow serves only when it answered at least `alone_min` of the inputs alone, at least `agree_min` of those were
|
|
18
|
+
// right on every field at once (the joint floor: misses spread over several fields add up, and an unjudged field is not
|
|
19
|
+
// right), and every field passes; a hold names each failing field and its misses, an unjudged input among them (known
|
|
20
|
+
// "unknown": marked wrong, with no right answer to show). Field names are the caller's (agree_on paths, which never hold
|
|
21
|
+
// `__proto__`, else the whole answer): they key Maps here, never a plain object's lookup, so a name Object.prototype
|
|
22
|
+
// holds (`constructor`, `toString`) is a field like any other.
|
|
23
|
+
/** Where a field's known answer came from, strongest first. */
|
|
24
|
+
export const KNOWN = ["grade", "agent"];
|
|
25
|
+
/** How far under the agent's own accuracy a field's graded inputs may be. */
|
|
26
|
+
const MARGIN = 0.02;
|
|
27
|
+
/** JSON values equal as values: keys in any order, arrays in order. */
|
|
28
|
+
export function sameValue(a, b) {
|
|
29
|
+
if (a === b)
|
|
30
|
+
return true;
|
|
31
|
+
if (typeof a !== typeof b || a === null || b === null || typeof a !== "object")
|
|
32
|
+
return false;
|
|
33
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
34
|
+
return false;
|
|
35
|
+
if (Array.isArray(a))
|
|
36
|
+
return a.length === b.length && a.every((one, i) => sameValue(one, b[i]));
|
|
37
|
+
const [ka, kb] = [Object.keys(a), Object.keys(b)];
|
|
38
|
+
return ka.length === kb.length && ka.every((key) => Object.hasOwn(b, key) && sameValue(a[key], b[key]));
|
|
39
|
+
}
|
|
40
|
+
/** A value is right against a known one when both are there and equal; a missing value is never right. */
|
|
41
|
+
const right = (value, known) => value !== undefined && known !== undefined && sameValue(value, known);
|
|
42
|
+
/** A field's best known answer on one input, and where it came from. */
|
|
43
|
+
function knownOf(one) {
|
|
44
|
+
return one.grade !== undefined ? { value: one.grade, known: "grade" } : { value: one.agent, known: "agent" };
|
|
45
|
+
}
|
|
46
|
+
/** Whether the workflow's value on one field is right, and the agent's, against that field's best known answer;
|
|
47
|
+
* undefined when it has none (the agent's answer graded wrong with no expected one, and the workflow said another).
|
|
48
|
+
* `marked`: judged by a grade that gave no right answer (the workflow repeated the answer it marked wrong). A value equal
|
|
49
|
+
* to an answer a grade marked wrong is never right, whatever `agrees` says; where the agent gave no value, the agent is
|
|
50
|
+
* never right either. */
|
|
51
|
+
function judged(one) {
|
|
52
|
+
if (one.no_known)
|
|
53
|
+
return undefined;
|
|
54
|
+
const { value, known } = knownOf(one);
|
|
55
|
+
const repeatsWrong = one.agent_correct === false && right(one.workflow, one.agent);
|
|
56
|
+
if (known === "grade") {
|
|
57
|
+
const agent = one.agent_correct ?? right(one.agent, value);
|
|
58
|
+
return { known, expected: value, workflow: !repeatsWrong && (one.agrees ?? right(one.workflow, value)), agent };
|
|
59
|
+
}
|
|
60
|
+
const same = one.agrees ?? right(one.workflow, one.agent);
|
|
61
|
+
if (one.agent_correct === false)
|
|
62
|
+
return same || repeatsWrong ? { known: "grade", expected: undefined, workflow: false, agent: false, marked: true } : undefined;
|
|
63
|
+
return { known, expected: value, workflow: same, agent: one.agent !== undefined };
|
|
64
|
+
}
|
|
65
|
+
const NO_TALLY = { n: 0, alone: 0, agreed: 0, agent_agreed: 0, fields: {} };
|
|
66
|
+
/** `rows` counted, added to `before` (a check's inputs counted so far). */
|
|
67
|
+
export function tallyOf(rows, before = NO_TALLY) {
|
|
68
|
+
let { n, alone, agreed, agent_agreed: agentAgreed } = before;
|
|
69
|
+
const fields = new Map(Object.entries(before.fields).map(([name, by]) => [name, countsOf(by)]));
|
|
70
|
+
for (const row of rows) {
|
|
71
|
+
if (row.outcome === "skipped")
|
|
72
|
+
continue;
|
|
73
|
+
n += 1;
|
|
74
|
+
if (row.outcome !== "answered")
|
|
75
|
+
continue;
|
|
76
|
+
alone += 1;
|
|
77
|
+
let all = true;
|
|
78
|
+
let agentAll = true;
|
|
79
|
+
for (const [name, one] of Object.entries(row.fields ?? {})) {
|
|
80
|
+
const j = judged(one);
|
|
81
|
+
const by = fields.get(name) ?? {};
|
|
82
|
+
fields.set(name, by);
|
|
83
|
+
if (!j) {
|
|
84
|
+
// No known answer: not right, and the agent's answer was graded wrong.
|
|
85
|
+
by.unjudged = (by.unjudged ?? 0) + 1;
|
|
86
|
+
all = false;
|
|
87
|
+
agentAll = false;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const was = by[j.known] ?? { n: 0, workflow_right: 0, agent_right: 0 };
|
|
91
|
+
by[j.known] = { n: was.n + 1, workflow_right: was.workflow_right + (j.workflow ? 1 : 0), agent_right: was.agent_right + (j.agent ? 1 : 0) };
|
|
92
|
+
if (!j.workflow)
|
|
93
|
+
all = false;
|
|
94
|
+
if (!j.agent)
|
|
95
|
+
agentAll = false;
|
|
96
|
+
}
|
|
97
|
+
if (all)
|
|
98
|
+
agreed += 1;
|
|
99
|
+
if (agentAll)
|
|
100
|
+
agentAgreed += 1;
|
|
101
|
+
}
|
|
102
|
+
return { n, alone, agreed, agent_agreed: agentAgreed, fields: Object.fromEntries(fields) };
|
|
103
|
+
}
|
|
104
|
+
/** A field's counts, copied: only the sources KNOWN names and its unjudged count, whatever else the object holds. */
|
|
105
|
+
const countsOf = (by) => ({
|
|
106
|
+
...Object.fromEntries(KNOWN.flatMap((known) => (Object.hasOwn(by, known) && by[known] ? [[known, { ...by[known] }]] : []))),
|
|
107
|
+
...(Object.hasOwn(by, "unjudged") && typeof by.unjudged === "number" && by.unjudged > 0 ? { unjudged: by.unjudged } : {}),
|
|
108
|
+
});
|
|
109
|
+
/** The misses in `rows`: each field the workflow had wrong on an input it answered alone, inputs in order, and each it
|
|
110
|
+
* had no known answer on (never right), no value: "contradicts" where its grade contradicts itself, else "unknown". */
|
|
111
|
+
export function missesOf(rows) {
|
|
112
|
+
return rows.flatMap((row) => row.outcome !== "answered" ? [] : Object.entries(row.fields ?? {}).flatMap(([field, one]) => {
|
|
113
|
+
const j = judged(one);
|
|
114
|
+
if (j?.workflow)
|
|
115
|
+
return [];
|
|
116
|
+
return [{ field, input_id: row.input_id, workflow: one.workflow, expected: j?.expected, known: one.no_known ? "contradicts" : !j || j.marked ? "unknown" : j.known }];
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
/** At least `share` of `n`, counted in whole inputs. Bars are decimals (0.8), so the product is rounded off its float
|
|
120
|
+
* noise before it is rounded up: 0.9 of 20 needs 18, never 19. */
|
|
121
|
+
const atLeast = (count, share, n) => count >= Math.ceil(Math.round(share * n * 1e6) / 1e6);
|
|
122
|
+
const pct = (value) => `${(value * 100).toFixed(1)}%`;
|
|
123
|
+
const share = (count, n) => pct(n ? count / n : 0);
|
|
124
|
+
/** The misses `n` inputs may have under a share `share` of them, one at least: one input missed is never a hold alone. */
|
|
125
|
+
const missesAllowed = (share, n) => Math.max(1, Math.floor(Math.round(share * n * 1e6) / 1e6));
|
|
126
|
+
/** Whether a field's graded inputs keep their bar: at least agree_min of them right and workflow_right / n >=
|
|
127
|
+
* agent_right / n - MARGIN (the workflow may be MARGIN * n inputs under the agent), one miss always allowed in each. */
|
|
128
|
+
const keepsGraded = (count, bar) => count.n - count.workflow_right <= missesAllowed(1 - bar.agree_min, count.n) && count.agent_right - count.workflow_right <= missesAllowed(MARGIN, count.n);
|
|
129
|
+
/** One source's counts in words. */
|
|
130
|
+
const sourceWords = (known, count, bar) => known === "agent"
|
|
131
|
+
? `${count.workflow_right} of ${count.n} said what the agent said (${share(count.workflow_right, count.n)}, the bar ${pct(bar.agree_min)})`
|
|
132
|
+
: `${count.workflow_right} of ${count.n} right against the grades' expected answers (${share(count.workflow_right, count.n)}, the bar ${pct(bar.agree_min)}), the agent ${count.agent_right} (${share(count.agent_right, count.n)}; at most ${pct(MARGIN)} under it), one miss allowed in each`;
|
|
133
|
+
/** `tally` judged on `bar`. A field passes on three tests: over all its known inputs pooled, at least agree_min of them
|
|
134
|
+
* right (the floor); its graded inputs keep their bar (keepsGraded: agree_min and MARGIN, one miss allowed in each);
|
|
135
|
+
* and it was judged on at least `alone_min` of the inputs that carried it. It fails with no input. So agent-known
|
|
136
|
+
* inputs never carry graded ones the draft got wrong past one miss, and one graded input missed is never a hold alone. */
|
|
137
|
+
export function verdictOf(tally, bar) {
|
|
138
|
+
const fields = new Map();
|
|
139
|
+
for (const [name, stored] of Object.entries(tally.fields)) {
|
|
140
|
+
const by = countsOf(stored);
|
|
141
|
+
const sources = KNOWN.filter((known) => (by[known]?.n ?? 0) > 0);
|
|
142
|
+
const sum = (key) => sources.reduce((total, known) => total + by[known][key], 0);
|
|
143
|
+
const unjudged = by.unjudged ?? 0;
|
|
144
|
+
const carried = sum("n") + unjudged;
|
|
145
|
+
// Judged on enough of the inputs that carried it: unknown answers never stand in for known ones.
|
|
146
|
+
const enough = atLeast(sum("n"), bar.alone_min, carried);
|
|
147
|
+
const floor = atLeast(sum("workflow_right"), bar.agree_min, sum("n"));
|
|
148
|
+
const graded = by.grade;
|
|
149
|
+
const pass = sources.length > 0 && floor && (!graded?.n || keepsGraded(graded, bar)) && enough;
|
|
150
|
+
const words = [
|
|
151
|
+
...sources.map((known) => sourceWords(known, by[known], bar)),
|
|
152
|
+
...(sources.length > 1 ? [`in all: ${sum("workflow_right")} of ${sum("n")} right (${share(sum("workflow_right"), sum("n"))}, the bar ${pct(bar.agree_min)})`] : []),
|
|
153
|
+
...(unjudged ? [`judged on ${sum("n")} of ${carried} (${share(sum("n"), carried)}, the bar ${pct(bar.alone_min)}): ${unjudged} had no known answer (graded wrong with none given and the workflow said another, or a grade that contradicts itself)`] : []),
|
|
154
|
+
];
|
|
155
|
+
fields.set(name, {
|
|
156
|
+
n: sum("n"), workflow_right: sum("workflow_right"), agent_right: graded?.agent_right ?? 0, ...(unjudged ? { unjudged } : {}), known: sources[0] ?? "agent", pass,
|
|
157
|
+
reason: words.length ? `${name}: ${words.join("; ")}` : `${name}: no input answered alone`,
|
|
158
|
+
by: Object.fromEntries(sources.map((known) => [known, { ...by[known] }])),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
const alone_pass = tally.alone > 0 && atLeast(tally.alone, bar.alone_min, tally.n);
|
|
162
|
+
const joint = { agreed: tally.agreed, alone: tally.alone, pass: tally.alone > 0 && atLeast(tally.agreed, bar.agree_min, tally.alone) };
|
|
163
|
+
const failing = [...fields].filter(([, one]) => !one.pass).map(([name]) => name);
|
|
164
|
+
const serve = alone_pass && joint.pass && fields.size > 0 && failing.length === 0;
|
|
165
|
+
return { verdict: serve ? "serve" : "hold", tally, alone_pass, joint, fields: Object.fromEntries(fields), failing };
|
|
166
|
+
}
|
|
167
|
+
/** Checks a workflow on `rows`, with the inputs a check counted before them (`before`), against `bar`. */
|
|
168
|
+
export function check(rows, bar, before) {
|
|
169
|
+
return { ...verdictOf(tallyOf(rows, before), bar), misses: missesOf(rows) };
|
|
170
|
+
}
|
|
171
|
+
/** Whether a verdict's words say the joint floor: when it says more than one field's own count, with two or more fields,
|
|
172
|
+
* or with one that passed while the joint fell short (inputs with no known answer kept it from being right on all).
|
|
173
|
+
* `fields`: each field's pass; `joint`: the joint floor's. */
|
|
174
|
+
export const jointSaid = (fields, joint) => fields.length > 1 || (fields.length === 1 && !joint && fields[0]);
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type Bill, type CostRun, type CostSummary, type NotServed, type RevisionSpend, type RunPath, type WorkflowCall } from "../learning-cost.js";
|
|
2
|
+
import { type RunRow } from "../store/store.js";
|
|
3
|
+
/** A run's own cost as its row stores it: `known`, an exact figure; `unknown`, none; `at_most`, only a bound (a request
|
|
4
|
+
* cut at its cap hold), never shown as a cost. */
|
|
5
|
+
export declare const COST_STATES: readonly ["known", "unknown", "at_most"];
|
|
6
|
+
export type CostState = (typeof COST_STATES)[number];
|
|
7
|
+
/** One ended run: who answered it (`path`, null when its end named none), the learned workflow revision that ran it
|
|
8
|
+
* (`workflow` at `digest`; for an audit's shadow, the revision it audits), why that workflow did not serve it alone,
|
|
9
|
+
* the job's own $ (`jobUsd`: what it had cost when its answer was final, its look-back left out) and everything its
|
|
10
|
+
* rounds looked back for (`lookBackUsd`: its compile turns and its follow-up rounds' turns); each null when unknown; and
|
|
11
|
+
* every learned workflow call it made (`workflowCalls`: its metrics' `workflow_calls`, the platform's and the agent's,
|
|
12
|
+
* each with its digest, caller, outcome and booked $), none for a run that made none. */
|
|
13
|
+
export type LedgerRow = {
|
|
14
|
+
readonly runId: string;
|
|
15
|
+
readonly agentId: string;
|
|
16
|
+
readonly startedAt: number;
|
|
17
|
+
readonly path: RunPath | null;
|
|
18
|
+
readonly workflow: string | null;
|
|
19
|
+
readonly digest: string | null;
|
|
20
|
+
readonly notServed: NotServed | null;
|
|
21
|
+
readonly jobUsd: number | null;
|
|
22
|
+
readonly lookBackUsd: number | null;
|
|
23
|
+
readonly cost: CostState;
|
|
24
|
+
readonly workflowCalls: readonly WorkflowCall[];
|
|
25
|
+
};
|
|
26
|
+
/** What a spend row paid for: a compile turn that drafted a revision (`compile`), a replay run checking one (`check`), or
|
|
27
|
+
* an audit's shadow run of a served run (`audit`). */
|
|
28
|
+
export declare const SPEND_KINDS: readonly ["compile", "check", "audit"];
|
|
29
|
+
export type SpendKind = (typeof SPEND_KINDS)[number];
|
|
30
|
+
/** One learning cost: the run whose end booked it (`runId`, with its `round` for a compile turn, 0 otherwise), the run
|
|
31
|
+
* an audit's shadow audited (`ofRun`), the revision it is for (`workflow` at `digest`; a compile that kept no draft drafted
|
|
32
|
+
* none: both null; one whose drafts failed their checks, the workflow alone) and its $ (null: unknown). */
|
|
33
|
+
export type SpendRow = {
|
|
34
|
+
readonly runId: string;
|
|
35
|
+
readonly round: number;
|
|
36
|
+
readonly agentId: string;
|
|
37
|
+
readonly kind: SpendKind;
|
|
38
|
+
readonly ofRun: string | null;
|
|
39
|
+
readonly workflow: string | null;
|
|
40
|
+
readonly digest: string | null;
|
|
41
|
+
readonly usd: number | null;
|
|
42
|
+
};
|
|
43
|
+
/** What record() reads of an ended run: its row (its cost, the bound beside it, its metrics: the run-end classifier's
|
|
44
|
+
* path, workflow and digest, runner/workflow-first.ts pathMetrics), every round its learning kept (learning_proposals)
|
|
45
|
+
* and, for an audit's shadow run, the run its launch audits (`auditOf`: its end may name no audit, when it failed first).
|
|
46
|
+
* `learns`: whether its launch learns (learnsOfLaunch); false, an end that named no path is the agent's. */
|
|
47
|
+
export type RunEnd = {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly agentId: string;
|
|
50
|
+
readonly startedAt: number;
|
|
51
|
+
readonly costUsd: number | null;
|
|
52
|
+
readonly costAtMostUsd: number | null;
|
|
53
|
+
readonly metrics: unknown;
|
|
54
|
+
readonly rounds: readonly {
|
|
55
|
+
readonly round: number;
|
|
56
|
+
readonly proposal: unknown;
|
|
57
|
+
}[];
|
|
58
|
+
readonly auditOf?: string | null;
|
|
59
|
+
readonly learns?: boolean;
|
|
60
|
+
};
|
|
61
|
+
/** RunEnd.auditOf of run `runId` from its stored launch body (`body`, undefined with none): the run its launch audits
|
|
62
|
+
* (null when it names none) for an audit's shadow run (learn/audit.ts SHADOW_PREFIX "audit."), undefined for any other. */
|
|
63
|
+
export declare const auditOfLaunch: (runId: string, body: unknown) => string | null | undefined;
|
|
64
|
+
/** RunEnd.learns from a run's stored launch body (`body`, undefined with none): whether it holds a learning start, as
|
|
65
|
+
* run-launch.ts learningStartOfStored reads one. No launch reads as a run that does not learn. */
|
|
66
|
+
export declare const learnsOfLaunch: (body: unknown) => boolean;
|
|
67
|
+
/** What a kept round says of its job as it answered (runner/reflect-hook.ts): when, what the job had cost by then, and
|
|
68
|
+
* what looking back cost after it. */
|
|
69
|
+
export type Answered = {
|
|
70
|
+
readonly answered_at: string;
|
|
71
|
+
readonly job_cost_usd: number | null;
|
|
72
|
+
readonly look_back_usd: number | null;
|
|
73
|
+
};
|
|
74
|
+
/** A kept round's Answered; undefined for one that never answered. */
|
|
75
|
+
export declare const answeredOf: (proposal: unknown) => Answered | undefined;
|
|
76
|
+
/** A run a follow-up opened again: its job is what it had cost when its first round answered, and everything its row
|
|
77
|
+
* (`rowUsd`) spent after that (each later round's follow-up turn and every round's look-back: feedback the run was given
|
|
78
|
+
* is learning, never the job) is its look-back; unknown when the row's cost is and the job's is known (a later round
|
|
79
|
+
* stopped mid-request keeps no figures, so the rounds' sum would read short as if settled); the rounds' look-backs added
|
|
80
|
+
* up when the job's cost is unknown. The impact read (store-reads/learning.ts impactLearningOf) takes the same. */
|
|
81
|
+
export declare function roundsLooked(rounds: Answered, rowUsd: number | null): Answered;
|
|
82
|
+
/** Over a run's kept rounds, oldest first: with two or more and one that answered, what they kept together (its first
|
|
83
|
+
* answer's job, every round's look-back added up, null once one is unknown); else undefined. */
|
|
84
|
+
export declare function roundsSpentIn(proposals: readonly unknown[]): Answered | undefined;
|
|
85
|
+
/** What a run's rounds kept as it answered, as its cost reads it: over every round (roundsLooked) for one a follow-up
|
|
86
|
+
* opened again, else its one round's; undefined for one that never answered. */
|
|
87
|
+
export declare function lookedOf(proposals: readonly unknown[], rowUsd: number | null): Answered | undefined;
|
|
88
|
+
/** The ledger row and spend rows a run's end writes, from its row and its kept rounds (RunEnd). The job's own $ is its
|
|
89
|
+
* row's cost less every look-back, never under what it had cost when it answered (a later turn's spend is the job's
|
|
90
|
+
* too); its row's cost for a run that did not look back. Its compile turns are spend rows of the revision each drafted.
|
|
91
|
+
* A replay run's cost is its revision's check, and an audit's shadow run's the run it audits: a spend row each, beside
|
|
92
|
+
* its own ledger row. */
|
|
93
|
+
export declare function record(end: RunEnd): {
|
|
94
|
+
row: LedgerRow;
|
|
95
|
+
spend: SpendRow[];
|
|
96
|
+
};
|
|
97
|
+
/** An audit's shadow run of a run, as the reader finds it: `open` while it is queued or running (its cost not known yet),
|
|
98
|
+
* `ended` once it ended, stopped or failed. A run with none has no entry. */
|
|
99
|
+
export type Shadow = "open" | "ended";
|
|
100
|
+
/** A run's figures (figuresOf), null when unknown: `series_*` are the cost series', the rest GET /runs/{id}'s. */
|
|
101
|
+
export type RunFigures = {
|
|
102
|
+
readonly usd: number | null;
|
|
103
|
+
readonly compile_usd: number | null;
|
|
104
|
+
readonly followup_usd: number | null;
|
|
105
|
+
readonly shadow_usd: number | null;
|
|
106
|
+
readonly share_usd: number | null;
|
|
107
|
+
readonly series_learning_usd: number | null;
|
|
108
|
+
readonly series_total_usd: number | null;
|
|
109
|
+
readonly learning_usd: number | null;
|
|
110
|
+
readonly all_in_usd: number | null;
|
|
111
|
+
};
|
|
112
|
+
/** One run's figures (RunFigures) from its row and the parts the spend rows give it (its compile turns, its shadow's
|
|
113
|
+
* cost, its share): the one formula every reader of a run's cost takes. */
|
|
114
|
+
export declare function figuresOf(row: Pick<LedgerRow, "path" | "jobUsd" | "lookBackUsd">, parts: {
|
|
115
|
+
readonly compile: number | null;
|
|
116
|
+
readonly shadow: number | null;
|
|
117
|
+
readonly share: number | null;
|
|
118
|
+
}): RunFigures;
|
|
119
|
+
/** The spend rows read by run: each run's compile turns (null once one is unknown) and each audited run's shadow cost. */
|
|
120
|
+
export declare function spendByRun(spend: readonly SpendRow[]): {
|
|
121
|
+
compile: Map<string, number | null>;
|
|
122
|
+
audit: Map<string, number | null>;
|
|
123
|
+
};
|
|
124
|
+
/** An audit's shadow run as its cost reads: `ended` once it is final (FINAL_STATES), `usd` its cost (null while unknown
|
|
125
|
+
* or only a bound). */
|
|
126
|
+
export type ShadowCost = {
|
|
127
|
+
readonly ended: boolean;
|
|
128
|
+
readonly usd: number | null;
|
|
129
|
+
};
|
|
130
|
+
/** A shadow run's row as a ShadowCost. */
|
|
131
|
+
export declare const shadowOfRun: (run: Pick<RunRow, "state" | "costUsd" | "costAtMostUsd">) => ShadowCost;
|
|
132
|
+
/** A run's shadow cost, the one rule the ledger, GET /stats and analytics read: 0 with no shadow (undefined); unknown
|
|
133
|
+
* (null) while it is queued or running, or once it ended at an unknown cost, never $0; its cost once known. */
|
|
134
|
+
export declare const shadowUsdOf: (shadow: ShadowCost | undefined) => number | null;
|
|
135
|
+
/** A run's shadow cost (shadowUsdOf) from the reader's shadows and the audit spend rows: an ended shadow costs its
|
|
136
|
+
* spend row's, unknown with none. */
|
|
137
|
+
export declare function shadowCostOf(runId: string, audit: ReadonlyMap<string, number | null>, shadows: ReadonlyMap<string, Shadow>): number | null;
|
|
138
|
+
/** A revision of a learned workflow as its records say it now (store-reads/learning.ts revisionsOf): the served runs its
|
|
139
|
+
* spend is spread over (`k`, its check's compile_after) and where it stands. Order is the records'. */
|
|
140
|
+
export type RevisionAt = {
|
|
141
|
+
readonly workflow: string;
|
|
142
|
+
readonly digest: string;
|
|
143
|
+
readonly k: number;
|
|
144
|
+
readonly state: RevisionSpend["state"];
|
|
145
|
+
};
|
|
146
|
+
/** What summarize reads: the series' rows and spend, shadows by audited run, learning runs (absent: all), revisions. */
|
|
147
|
+
export type LedgerRead = {
|
|
148
|
+
readonly rows: readonly LedgerRow[];
|
|
149
|
+
readonly spend: readonly SpendRow[];
|
|
150
|
+
readonly shadows: ReadonlyMap<string, Shadow>;
|
|
151
|
+
readonly learning?: ReadonlySet<string>;
|
|
152
|
+
readonly revisions: readonly RevisionAt[];
|
|
153
|
+
readonly compileAfter: number;
|
|
154
|
+
readonly named?: (row: LedgerRow) => string | null;
|
|
155
|
+
};
|
|
156
|
+
export type Summarized = {
|
|
157
|
+
runs: CostRun[];
|
|
158
|
+
summary: CostSummary;
|
|
159
|
+
bill: Omit<Bill, "holds">;
|
|
160
|
+
figures: Map<string, RunFigures>;
|
|
161
|
+
unknown_path_runs: number;
|
|
162
|
+
};
|
|
163
|
+
/** What the ledger says over an agent's series: each run of it (`runs`, oldest first, every one), what they show
|
|
164
|
+
* (`summary`), the owner's bill (`bill`, its holds aside: they are the records'), each run's figures by id (`figures`)
|
|
165
|
+
* and the rows whose end named no path (`unknown_path_runs`, left out of all of it). Every sum is over every run.
|
|
166
|
+
*
|
|
167
|
+
* A revision is its workflow and its digest, never the digest alone: two workflows of the same text share a digest, and
|
|
168
|
+
* each pays its own way. Its spend is its compile turns and its check's replay runs (spend rows of its workflow at its
|
|
169
|
+
* digest), whether or not a record names it or the records were trimmed: one no record names follows the ones that do,
|
|
170
|
+
* at `compileAfter`, `checking` while it is its workflow's newest spend, else `stopped`. It is spread over its first K
|
|
171
|
+
* served runs (amortize). What no run carries is the booked total less every run's all-in, unknown while any run's
|
|
172
|
+
* figures are. */
|
|
173
|
+
export declare function summarize(read: LedgerRead): Summarized;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// The ledger: one row a run, written by its end, and one spend row a learning cost (a compile turn, a check's replay run,
|
|
2
|
+
// an audit's shadow run), keyed to the workflow revision it is for. record() is what a run's end writes, in the end's own
|
|
3
|
+
// transaction (store/sqlite/run-patch.ts); summarize() is the one reading of them, which GET /agents/{id}/learning/cost,
|
|
4
|
+
// GET /runs/{id}'s cost and the money rule (runner/workflow-first.ts costLineOf, over RunFigures) all take. Pure: no
|
|
5
|
+
// store, no clock.
|
|
6
|
+
import { amortize, billOf, costSummary, JOB_PATHS, NOT_SERVED, noiseOff, RUN_PATHS, SERVED_PATHS, tookOverOf, workflowCallsIn } from "../learning-cost.js";
|
|
7
|
+
import { FINAL_STATES } from "../store/store.js";
|
|
8
|
+
/** A run's own cost as its row stores it: `known`, an exact figure; `unknown`, none; `at_most`, only a bound (a request
|
|
9
|
+
* cut at its cap hold), never shown as a cost. */
|
|
10
|
+
export const COST_STATES = ["known", "unknown", "at_most"];
|
|
11
|
+
/** What a spend row paid for: a compile turn that drafted a revision (`compile`), a replay run checking one (`check`), or
|
|
12
|
+
* an audit's shadow run of a served run (`audit`). */
|
|
13
|
+
export const SPEND_KINDS = ["compile", "check", "audit"];
|
|
14
|
+
const usdOrNull = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
15
|
+
const textOrNull = (value) => (typeof value === "string" ? value : null);
|
|
16
|
+
const objectOf = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
|
|
17
|
+
/** RunEnd.auditOf of run `runId` from its stored launch body (`body`, undefined with none): the run its launch audits
|
|
18
|
+
* (null when it names none) for an audit's shadow run (learn/audit.ts SHADOW_PREFIX "audit."), undefined for any other. */
|
|
19
|
+
export const auditOfLaunch = (runId, body) => runId.startsWith("audit.") ? textOrNull(objectOf(objectOf(body).audit).of) : undefined;
|
|
20
|
+
/** RunEnd.learns from a run's stored launch body (`body`, undefined with none): whether it holds a learning start, as
|
|
21
|
+
* run-launch.ts learningStartOfStored reads one. No launch reads as a run that does not learn. */
|
|
22
|
+
export const learnsOfLaunch = (body) => objectOf(body).learning !== undefined;
|
|
23
|
+
/** A sum of parts, null once one is unknown; 0 for none. */
|
|
24
|
+
const sumOf = (parts) => (parts.some((one) => one === null) ? null : noiseOff(parts.reduce((total, one) => total + one, 0)));
|
|
25
|
+
/** A kept round's Answered; undefined for one that never answered. */
|
|
26
|
+
export const answeredOf = (proposal) => {
|
|
27
|
+
const kept = objectOf(proposal);
|
|
28
|
+
return typeof kept.answered_at === "string" ? { answered_at: kept.answered_at, job_cost_usd: usdOrNull(kept.job_cost_usd), look_back_usd: usdOrNull(kept.look_back_usd) } : undefined;
|
|
29
|
+
};
|
|
30
|
+
/** A run a follow-up opened again: its job is what it had cost when its first round answered, and everything its row
|
|
31
|
+
* (`rowUsd`) spent after that (each later round's follow-up turn and every round's look-back: feedback the run was given
|
|
32
|
+
* is learning, never the job) is its look-back; unknown when the row's cost is and the job's is known (a later round
|
|
33
|
+
* stopped mid-request keeps no figures, so the rounds' sum would read short as if settled); the rounds' look-backs added
|
|
34
|
+
* up when the job's cost is unknown. The impact read (store-reads/learning.ts impactLearningOf) takes the same. */
|
|
35
|
+
export function roundsLooked(rounds, rowUsd) {
|
|
36
|
+
const after = rounds.job_cost_usd === null ? rounds.look_back_usd : rowUsd === null ? null : Math.max(0, Math.round((rowUsd - rounds.job_cost_usd) * 1e9) / 1e9);
|
|
37
|
+
return { answered_at: rounds.answered_at, job_cost_usd: rounds.job_cost_usd, look_back_usd: after };
|
|
38
|
+
}
|
|
39
|
+
/** Over a run's kept rounds, oldest first: with two or more and one that answered, what they kept together (its first
|
|
40
|
+
* answer's job, every round's look-back added up, null once one is unknown); else undefined. */
|
|
41
|
+
export function roundsSpentIn(proposals) {
|
|
42
|
+
if (proposals.length < 2)
|
|
43
|
+
return undefined;
|
|
44
|
+
let first;
|
|
45
|
+
let look = 0;
|
|
46
|
+
for (const proposal of proposals) {
|
|
47
|
+
const answered = answeredOf(proposal);
|
|
48
|
+
if (!first && answered)
|
|
49
|
+
first = answered;
|
|
50
|
+
// A round that never looked back (no figures kept) added none.
|
|
51
|
+
if (answered)
|
|
52
|
+
look = look === null || answered.look_back_usd === null ? null : look + answered.look_back_usd;
|
|
53
|
+
}
|
|
54
|
+
return first ? { answered_at: first.answered_at, job_cost_usd: first.job_cost_usd, look_back_usd: look } : undefined;
|
|
55
|
+
}
|
|
56
|
+
/** What a run's rounds kept as it answered, as its cost reads it: over every round (roundsLooked) for one a follow-up
|
|
57
|
+
* opened again, else its one round's; undefined for one that never answered. */
|
|
58
|
+
export function lookedOf(proposals, rowUsd) {
|
|
59
|
+
const together = roundsSpentIn(proposals);
|
|
60
|
+
return together ? roundsLooked(together, rowUsd) : proposals.length ? answeredOf(proposals.at(-1)) : undefined;
|
|
61
|
+
}
|
|
62
|
+
function compilesIn(rounds) {
|
|
63
|
+
return rounds.flatMap(({ round, proposal }) => {
|
|
64
|
+
const kept = objectOf(proposal);
|
|
65
|
+
const usd = "look_back_usd" in kept ? usdOrNull(kept.look_back_usd) : 0;
|
|
66
|
+
const compiled = objectOf(kept.compiled);
|
|
67
|
+
if (typeof compiled.workflow !== "string")
|
|
68
|
+
return usd === 0 ? [] : [{ round, workflow: null, digest: null, usd }];
|
|
69
|
+
const failed = Array.isArray(kept.draft_errors) && kept.draft_errors.some((one) => typeof one === "string");
|
|
70
|
+
const digest = typeof compiled.digest === "string" && /^[0-9a-f]{16}$/.test(compiled.digest) && !failed ? compiled.digest : null;
|
|
71
|
+
return [{ round, workflow: compiled.workflow, digest, usd }];
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** The ledger row and spend rows a run's end writes, from its row and its kept rounds (RunEnd). The job's own $ is its
|
|
75
|
+
* row's cost less every look-back, never under what it had cost when it answered (a later turn's spend is the job's
|
|
76
|
+
* too); its row's cost for a run that did not look back. Its compile turns are spend rows of the revision each drafted.
|
|
77
|
+
* A replay run's cost is its revision's check, and an audit's shadow run's the run it audits: a spend row each, beside
|
|
78
|
+
* its own ledger row. */
|
|
79
|
+
export function record(end) {
|
|
80
|
+
const metrics = objectOf(end.metrics);
|
|
81
|
+
const cost = typeof end.costAtMostUsd === "number" ? "at_most" : end.costUsd === null ? "unknown" : "known";
|
|
82
|
+
const rowUsd = cost === "known" ? end.costUsd : null;
|
|
83
|
+
const rounds = [...end.rounds].sort((a, b) => a.round - b.round);
|
|
84
|
+
const looked = lookedOf(rounds.map((one) => one.proposal), rowUsd);
|
|
85
|
+
const asAnswered = looked ? looked.job_cost_usd : rowUsd;
|
|
86
|
+
const back = looked ? looked.look_back_usd : 0;
|
|
87
|
+
// What the row holds past the answer and its look-back is a later turn's spend, unknown once the row's cost is; with the
|
|
88
|
+
// look-back's cost unknown the two can't be told apart, and the job is what it had cost when it answered.
|
|
89
|
+
const job = !looked || asAnswered === null || back === null ? asAnswered : rowUsd === null ? null : Math.max(asAnswered, rowUsd - back);
|
|
90
|
+
const audited = metrics.path === "audit" || end.auditOf !== undefined;
|
|
91
|
+
// The one by-hand rule (the window's read and Learning.byHandBefore read the row's path, each leaving a workflow
|
|
92
|
+
// revision's runs out): a run whose end named no path is the agent's when its launch does not learn; one that learns
|
|
93
|
+
// and named none is unknown (null).
|
|
94
|
+
const named = RUN_PATHS.includes(metrics.path) ? metrics.path : null;
|
|
95
|
+
const path = audited ? "audit" : named ?? (end.learns === false ? "agent" : null);
|
|
96
|
+
const audit = objectOf(metrics.audit);
|
|
97
|
+
const workflow = textOrNull(audited ? audit.workflow : metrics.workflow);
|
|
98
|
+
const digest = textOrNull(audited ? audit.digest : metrics.workflow_digest);
|
|
99
|
+
// A hand-back a restart cut (pathMetrics workflow_cut) is `cut`, never the workflow's own `back`.
|
|
100
|
+
const cut = path === "handed_back" && metrics.workflow_cut === true;
|
|
101
|
+
const notServed = cut ? "cut" : NOT_SERVED.includes(metrics.not_served) ? metrics.not_served : null;
|
|
102
|
+
const row = {
|
|
103
|
+
runId: end.id, agentId: end.agentId, startedAt: end.startedAt, path, workflow, digest, notServed,
|
|
104
|
+
jobUsd: job === null ? null : noiseOff(job), lookBackUsd: back === null ? null : noiseOff(back), cost, workflowCalls: workflowCallsIn(metrics),
|
|
105
|
+
};
|
|
106
|
+
const base = { runId: end.id, agentId: end.agentId };
|
|
107
|
+
const spend = compilesIn(rounds).map((one) => ({ ...base, round: one.round, kind: "compile", ofRun: null, workflow: one.workflow, digest: one.digest, usd: one.usd }));
|
|
108
|
+
if (path === "replay")
|
|
109
|
+
spend.push({ ...base, round: 0, kind: "check", ofRun: null, workflow, digest, usd: rowUsd });
|
|
110
|
+
if (audited)
|
|
111
|
+
spend.push({ ...base, round: 0, kind: "audit", ofRun: textOrNull(audit.of) ?? end.auditOf ?? null, workflow, digest, usd: rowUsd });
|
|
112
|
+
return { row, spend };
|
|
113
|
+
}
|
|
114
|
+
/** One run's figures (RunFigures) from its row and the parts the spend rows give it (its compile turns, its shadow's
|
|
115
|
+
* cost, its share): the one formula every reader of a run's cost takes. */
|
|
116
|
+
export function figuresOf(row, parts) {
|
|
117
|
+
const { compile, shadow, share } = parts;
|
|
118
|
+
const back = row.lookBackUsd;
|
|
119
|
+
const followup = back === null || compile === null ? null : noiseOff(Math.max(0, back - compile));
|
|
120
|
+
const seriesLearning = sumOf([back, shadow]);
|
|
121
|
+
const learns = row.path !== "replay" && row.path !== "audit";
|
|
122
|
+
const learning = learns ? sumOf([followup, shadow, share]) : 0;
|
|
123
|
+
return {
|
|
124
|
+
usd: row.jobUsd, compile_usd: compile, followup_usd: followup, shadow_usd: shadow, share_usd: learns ? share : 0,
|
|
125
|
+
series_learning_usd: seriesLearning, series_total_usd: sumOf([row.jobUsd, seriesLearning]),
|
|
126
|
+
learning_usd: learning, all_in_usd: learns ? sumOf([row.jobUsd, learning]) : 0,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** The spend rows read by run: each run's compile turns (null once one is unknown) and each audited run's shadow cost. */
|
|
130
|
+
export function spendByRun(spend) {
|
|
131
|
+
const compile = new Map();
|
|
132
|
+
const audit = new Map();
|
|
133
|
+
const add = (map, key, usd) => map.set(key, map.has(key) ? sumOf([map.get(key), usd]) : usd);
|
|
134
|
+
for (const one of spend) {
|
|
135
|
+
if (one.kind === "compile")
|
|
136
|
+
add(compile, one.runId, one.usd);
|
|
137
|
+
else if (one.kind === "audit" && one.ofRun !== null)
|
|
138
|
+
add(audit, one.ofRun, one.usd);
|
|
139
|
+
}
|
|
140
|
+
return { compile, audit };
|
|
141
|
+
}
|
|
142
|
+
/** A shadow run's row as a ShadowCost. */
|
|
143
|
+
export const shadowOfRun = (run) => ({ ended: FINAL_STATES.includes(run.state), usd: typeof run.costAtMostUsd === "number" ? null : run.costUsd });
|
|
144
|
+
/** A run's shadow cost, the one rule the ledger, GET /stats and analytics read: 0 with no shadow (undefined); unknown
|
|
145
|
+
* (null) while it is queued or running, or once it ended at an unknown cost, never $0; its cost once known. */
|
|
146
|
+
export const shadowUsdOf = (shadow) => (shadow === undefined ? 0 : shadow.ended ? shadow.usd : null);
|
|
147
|
+
/** A run's shadow cost (shadowUsdOf) from the reader's shadows and the audit spend rows: an ended shadow costs its
|
|
148
|
+
* spend row's, unknown with none. */
|
|
149
|
+
export function shadowCostOf(runId, audit, shadows) {
|
|
150
|
+
const state = shadows.get(runId);
|
|
151
|
+
if (state === undefined && !audit.has(runId))
|
|
152
|
+
return 0;
|
|
153
|
+
return shadowUsdOf({ ended: state !== "open", usd: audit.has(runId) ? audit.get(runId) : null });
|
|
154
|
+
}
|
|
155
|
+
/** What the ledger says over an agent's series: each run of it (`runs`, oldest first, every one), what they show
|
|
156
|
+
* (`summary`), the owner's bill (`bill`, its holds aside: they are the records'), each run's figures by id (`figures`)
|
|
157
|
+
* and the rows whose end named no path (`unknown_path_runs`, left out of all of it). Every sum is over every run.
|
|
158
|
+
*
|
|
159
|
+
* A revision is its workflow and its digest, never the digest alone: two workflows of the same text share a digest, and
|
|
160
|
+
* each pays its own way. Its spend is its compile turns and its check's replay runs (spend rows of its workflow at its
|
|
161
|
+
* digest), whether or not a record names it or the records were trimmed: one no record names follows the ones that do,
|
|
162
|
+
* at `compileAfter`, `checking` while it is its workflow's newest spend, else `stopped`. It is spread over its first K
|
|
163
|
+
* served runs (amortize). What no run carries is the booked total less every run's all-in, unknown while any run's
|
|
164
|
+
* figures are. */
|
|
165
|
+
export function summarize(read) {
|
|
166
|
+
const ordered = read.rows.filter((row) => row.path !== null && row.path !== "audit").sort((a, b) => a.startedAt - b.startedAt || (a.runId < b.runId ? -1 : a.runId > b.runId ? 1 : 0));
|
|
167
|
+
const unknownPath = read.rows.filter((row) => row.path === null).length;
|
|
168
|
+
const place = new Map(ordered.map((row, at) => [row.runId, at]));
|
|
169
|
+
const learns = (runId) => place.has(runId) && (read.learning?.has(runId) ?? true);
|
|
170
|
+
const spend = read.spend.filter((one) => (one.kind === "audit" ? place.has(one.ofRun ?? "") : learns(one.runId)));
|
|
171
|
+
const { compile, audit } = spendByRun(spend);
|
|
172
|
+
const partsOf = (row, share) => ({ compile: compile.has(row.runId) ? compile.get(row.runId) : 0, shadow: shadowCostOf(row.runId, audit, read.shadows), share });
|
|
173
|
+
// One numbering, the bill's: the runs that do a job, 1 the first; a replay checks a revision and has none.
|
|
174
|
+
let jobs = 0;
|
|
175
|
+
const series = ordered.map((row) => {
|
|
176
|
+
const figures = figuresOf(row, partsOf(row, 0));
|
|
177
|
+
const path = row.path;
|
|
178
|
+
return {
|
|
179
|
+
n: JOB_PATHS.includes(path) ? (jobs += 1) : null, run_id: row.runId, started_at: new Date(row.startedAt).toISOString(), path,
|
|
180
|
+
workflow: path === "agent" ? null : read.named ? read.named(row) : row.workflow,
|
|
181
|
+
digest: path === "agent" || row.workflow === null ? null : row.digest,
|
|
182
|
+
audited: read.shadows.has(row.runId) || audit.has(row.runId),
|
|
183
|
+
cost_usd: figures.usd, cost_upper_bound: row.cost === "at_most", learning_usd: figures.series_learning_usd, total_usd: figures.series_total_usd,
|
|
184
|
+
compile_usd: figures.compile_usd, followup_usd: figures.followup_usd, shadow_usd: figures.shadow_usd, share_usd: 0, not_served: row.notServed,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
const summary = costSummary(tookOverOf(series), null, new Set(series.flatMap((run) => (run.not_served === "cut" ? [run.run_id] : []))));
|
|
188
|
+
const booked = billOf(tookOverOf(series), summary);
|
|
189
|
+
// Each revision's compile and check, in spend order (the runs' order, then the rounds').
|
|
190
|
+
const key = (workflow, digest) => `${workflow}\0${digest}`;
|
|
191
|
+
const parts = new Map();
|
|
192
|
+
const newest = new Map();
|
|
193
|
+
// An audit's row is the run it audits' (its shadow is no run of the series): it is no revision's spend.
|
|
194
|
+
for (const one of spend.filter((each) => each.kind !== "audit").sort((a, b) => place.get(a.runId) - place.get(b.runId) || a.round - b.round)) {
|
|
195
|
+
if (one.workflow === null || one.digest === null)
|
|
196
|
+
continue;
|
|
197
|
+
const at = key(one.workflow, one.digest);
|
|
198
|
+
const entry = parts.get(at) ?? { workflow: one.workflow, digest: one.digest, compile: [], check: [] };
|
|
199
|
+
(one.kind === "compile" ? entry.compile : entry.check).push(one.usd);
|
|
200
|
+
parts.set(at, entry);
|
|
201
|
+
newest.set(one.workflow, at);
|
|
202
|
+
}
|
|
203
|
+
const spendOf = (workflow, digest, k, state) => {
|
|
204
|
+
const entry = parts.get(key(workflow, digest));
|
|
205
|
+
const compileUsd = sumOf(entry?.compile ?? []);
|
|
206
|
+
const checkUsd = sumOf(entry?.check ?? []);
|
|
207
|
+
return { workflow, digest, compile_usd: compileUsd, check_usd: checkUsd, spend_usd: sumOf([compileUsd, checkUsd]), k, state };
|
|
208
|
+
};
|
|
209
|
+
const named = new Set(read.revisions.map((one) => key(one.workflow, one.digest)));
|
|
210
|
+
const revisions = [
|
|
211
|
+
...read.revisions.map((one) => spendOf(one.workflow, one.digest, one.k, one.state)),
|
|
212
|
+
...[...parts].filter(([at]) => !named.has(at)).map(([at, entry]) => spendOf(entry.workflow, entry.digest, read.compileAfter, newest.get(entry.workflow) === at ? "checking" : "stopped")),
|
|
213
|
+
];
|
|
214
|
+
const served = ordered.flatMap((row) => (learns(row.runId) && SERVED_PATHS.includes(row.path) && row.workflow !== null && row.digest !== null ? [{ run_id: row.runId, workflow: row.workflow, digest: row.digest }] : []));
|
|
215
|
+
const split = amortize(revisions, served);
|
|
216
|
+
const figures = new Map(ordered.map((row) => [row.runId, figuresOf(row, partsOf(row, split.shares.has(row.runId) ? split.shares.get(row.runId) : 0))]));
|
|
217
|
+
const runs = tookOverOf(series.map((run) => ({ ...run, share_usd: figures.get(run.run_id).share_usd })));
|
|
218
|
+
let carried = 0;
|
|
219
|
+
for (const row of ordered) {
|
|
220
|
+
const all = row.path === "replay" ? 0 : row.cost === "at_most" ? null : figures.get(row.runId).all_in_usd;
|
|
221
|
+
carried = carried === null || all === null ? null : noiseOff(carried + all);
|
|
222
|
+
}
|
|
223
|
+
const unattributed = booked.withheld > 0 || carried === null ? null : noiseOff(booked.booked_usd - carried);
|
|
224
|
+
return { runs, summary, bill: { ...booked, revisions: split.revisions, unattributed_usd: unattributed }, figures, unknown_path_runs: unknownPath };
|
|
225
|
+
}
|