@parcha/agentrun-serve 0.1.0-beta.48 → 0.1.0-beta.50
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 +37 -0
- package/README.md +236 -26
- package/dist/agent-delete.d.ts +36 -12
- package/dist/agent-delete.js +213 -30
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +18 -5
- package/dist/agents.d.ts +8 -1
- package/dist/agents.js +14 -4
- package/dist/answer-json.d.ts +2 -2
- package/dist/answer-json.js +3 -4
- package/dist/api-only.d.ts +7 -10
- package/dist/api-only.js +9 -11
- package/dist/archil-routes.d.ts +1 -1
- package/dist/archil-routes.js +4 -3
- package/dist/browser-sessions.d.ts +56 -0
- package/dist/browser-sessions.js +117 -0
- package/dist/compute-cost.d.ts +101 -0
- package/dist/compute-cost.js +250 -0
- package/dist/computer-routes.d.ts +4 -15
- package/dist/computer-routes.js +6 -10
- package/dist/console/assets/index-D0mr9wfZ.css +1 -0
- package/dist/console/assets/index-D7G6o-1E.js +720 -0
- package/dist/console/index.html +2 -2
- package/dist/errors.d.ts +4 -1
- package/dist/errors.js +15 -5
- package/dist/eval-previous.d.ts +52 -0
- package/dist/eval-previous.js +164 -0
- package/dist/file-types.d.ts +2 -0
- package/dist/file-types.js +3 -1
- package/dist/harness.js +2 -1
- package/dist/import.js +4 -3
- package/dist/learn/check.d.ts +16 -4
- package/dist/learn/check.js +30 -26
- package/dist/learn/promote.d.ts +7 -1
- package/dist/learn/promote.js +47 -0
- package/dist/learn/repick.d.ts +43 -0
- package/dist/learn/repick.js +202 -0
- package/dist/learn/workflow-draft.d.ts +4 -0
- package/dist/learn/workflow-draft.js +10 -7
- package/dist/learned-workflow.d.ts +2 -0
- package/dist/learned-workflow.js +19 -2
- package/dist/learning-setting.d.ts +13 -0
- package/dist/learning-setting.js +23 -1
- package/dist/llms.txt +1 -1
- package/dist/openapi.js +180 -14
- package/dist/paid-calls.d.ts +17 -7
- package/dist/paid-calls.js +15 -9
- package/dist/run-launch.d.ts +2 -2
- package/dist/run-launch.js +2 -1
- package/dist/run-learning-served.d.ts +1 -1
- package/dist/run-timeline-served.js +5 -3
- package/dist/run-usage.d.ts +125 -0
- package/dist/run-usage.js +394 -0
- package/dist/runner/adapter-loop.d.ts +4 -0
- package/dist/runner/adapter-loop.js +66 -21
- package/dist/runner/adapter-run-spec.d.ts +8 -3
- package/dist/runner/adapter-run-spec.js +13 -6
- package/dist/runner/big-writes.d.ts +33 -0
- package/dist/runner/big-writes.js +105 -0
- package/dist/runner/cli-run-spec.js +14 -6
- package/dist/runner/conformance/harness.d.ts +2 -0
- package/dist/runner/conformance/harness.js +18 -4
- package/dist/runner/feedback-round.d.ts +29 -1
- package/dist/runner/feedback-round.js +54 -3
- package/dist/runner/held.d.ts +4 -1
- package/dist/runner/journal-kinds.d.ts +3 -1
- package/dist/runner/learning-end.js +54 -18
- package/dist/runner/loop-contract.d.ts +3 -0
- package/dist/runner/metrics.d.ts +4 -0
- package/dist/runner/metrics.js +9 -0
- package/dist/runner/monid-run.js +2 -2
- package/dist/runner/promotion.js +8 -2
- package/dist/runner/reflect-hook.js +3 -2
- package/dist/runner/run-child-host.d.ts +15 -2
- package/dist/runner/run-child-host.js +24 -8
- package/dist/runner/run-child.js +3 -0
- package/dist/runner/save-slots.d.ts +9 -0
- package/dist/runner/save-slots.js +91 -0
- package/dist/runs-over-store.d.ts +3 -2
- package/dist/runs-over-store.js +9 -7
- package/dist/sdk/runs.d.ts +3 -4
- package/dist/sdk/runs.js +10 -7
- package/dist/sdk/runtimes.d.ts +19 -0
- package/dist/sdk/runtimes.js +23 -0
- package/dist/sdk/schema.d.ts +14 -8
- package/dist/sdk/schema.js +18 -6
- package/dist/server.d.ts +7 -3
- package/dist/server.js +223 -100
- package/dist/store/agent-body.d.ts +2 -3
- package/dist/store/agent-body.js +3 -2
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +1 -1
- package/dist/store/open.d.ts +1 -1
- package/dist/store/open.js +1 -1
- package/dist/store/postgres/migrations/0021_purged.sql +13 -0
- package/dist/store/postgres/migrations/0022_computer_intervals.sql +27 -0
- package/dist/store/postgres/migrations/0023_computer_interval_resources.sql +14 -0
- package/dist/store/schema.d.ts +20 -1
- package/dist/store/schema.js +60 -5
- package/dist/store/sqlite/connection.d.ts +4 -1
- package/dist/store/sqlite/connection.js +5 -0
- package/dist/store/sqlite/run-patch.d.ts +8 -3
- package/dist/store/sqlite/run-patch.js +15 -7
- package/dist/store/sqlite/store.d.ts +2 -1
- package/dist/store/sqlite/store.js +279 -68
- package/dist/store/sqlite/worker.d.ts +9 -0
- package/dist/store/sqlite/worker.js +18 -1
- package/dist/store/store.d.ts +149 -9
- package/dist/store/store.js +63 -0
- package/dist/store-reads/analytics.js +2 -1
- package/dist/store-reads/cap.d.ts +17 -7
- package/dist/store-reads/cap.js +54 -4
- package/dist/store-reads/decisions.d.ts +21 -0
- package/dist/store-reads/decisions.js +22 -0
- package/dist/store-reads/journal.d.ts +8 -2
- package/dist/store-reads/journal.js +27 -8
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/learning.js +5 -2
- package/dist/store-reads/runs.js +2 -0
- package/dist/store-reads/stats.js +4 -2
- package/dist/store-view.d.ts +12 -1
- package/dist/store-view.js +98 -38
- package/dist/template-eval.d.ts +15 -1
- package/dist/template-eval.js +92 -13
- package/dist/templates.d.ts +1 -1
- package/dist/templates.js +4 -3
- package/dist/timeline/labels.js +17 -0
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +6 -2
- package/dist/tools.js +22 -1
- package/dist/trigger-targets.d.ts +28 -9
- package/dist/trigger-targets.js +19 -16
- package/dist/triggers.d.ts +2 -1
- package/dist/triggers.js +5 -2
- package/dist/validate.d.ts +4 -2
- package/dist/validate.js +13 -3
- package/dist/viewer-credential.d.ts +3 -4
- package/dist/viewer-credential.js +6 -4
- package/dist/web-fake.d.ts +25 -0
- package/dist/web-fake.js +38 -1
- package/dist/web-save.d.ts +75 -0
- package/dist/web-save.js +235 -0
- package/dist/workspace/archil-claim.d.ts +5 -2
- package/dist/workspace/archil-claim.js +5 -2
- package/dist/workspace/archil-disk.d.ts +10 -1
- package/dist/workspace/archil-disk.js +19 -1
- package/dist/workspace/archil.d.ts +3 -0
- package/dist/workspace/archil.js +101 -16
- package/dist/workspace/computer-for-claim.d.ts +19 -1
- package/dist/workspace/computer-for-claim.js +88 -20
- package/dist/workspace/computer-intervals.d.ts +26 -0
- package/dist/workspace/computer-intervals.js +58 -0
- package/dist/workspace/computer-janitor.d.ts +7 -3
- package/dist/workspace/computer-janitor.js +14 -6
- package/dist/workspace/computer-kept.d.ts +7 -2
- package/dist/workspace/computer-kept.js +9 -2
- package/dist/workspace/pieced.d.ts +14 -0
- package/dist/workspace/pieced.js +23 -0
- package/dist/workspace/resolve-computer.d.ts +7 -4
- package/dist/workspace/resolve-computer.js +21 -32
- package/dist/workspace/run-disk.js +32 -12
- package/dist/workspace/run-end-computers.d.ts +1 -0
- package/dist/workspace/run-end-computers.js +4 -1
- package/dist/workspace/run-workspace.d.ts +1 -1
- package/dist/workspace/run-workspace.js +11 -6
- package/package.json +10 -10
- package/templates/README.md +36 -7
- package/dist/console/assets/index-CTGegcKt.css +0 -1
- package/dist/console/assets/index-D1Se6mgG.js +0 -715
|
@@ -50,9 +50,18 @@ export type Request = {
|
|
|
50
50
|
op: "commit";
|
|
51
51
|
} | {
|
|
52
52
|
op: "rollback";
|
|
53
|
+
} | {
|
|
54
|
+
op: "checkpoint";
|
|
55
|
+
busyMs: number;
|
|
53
56
|
} | {
|
|
54
57
|
op: "close";
|
|
55
58
|
};
|
|
59
|
+
/** A WAL checkpoint's result: whether another connection held it back (`busy` 1), the log's frames, and those copied. */
|
|
60
|
+
export type CheckpointResult = {
|
|
61
|
+
busy: number;
|
|
62
|
+
log: number;
|
|
63
|
+
checkpointed: number;
|
|
64
|
+
};
|
|
56
65
|
/** An error as the worker reports it: busy (the lock outlasted busy_timeout), constraint, miss (an expectation), owned
|
|
57
66
|
* (another server holds the store), closed, or sql (anything else). */
|
|
58
67
|
export type WorkerError = {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// the disk (a flush at FULL, or a write behind another process's lock) waits here, never on the event loop the runner's
|
|
3
3
|
// loops share. WAL with synchronous=NORMAL: a commit is durable against a process crash at once, and against a lost box
|
|
4
4
|
// only after the next FULL commit (flush). Every write starts BEGIN IMMEDIATE, so a write never upgrades a read lock and
|
|
5
|
-
// deadlocks another writer; busy_timeout covers another process's write in flight. Foreign keys are on.
|
|
5
|
+
// deadlocks another writer; busy_timeout covers another process's write in flight. Foreign keys and secure_delete are on.
|
|
6
6
|
//
|
|
7
7
|
// The messages (connection.ts sends them one at a time, in order):
|
|
8
8
|
// open {file, busyTimeoutMs, owner} the connection and its pragmas; `owner` also takes the store's owner lock (one
|
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
// an interactive transaction (connection.ts transaction()), its statements sent
|
|
19
19
|
// as stmt, or as step (one statement, its changes and rows, its `expect` checked)
|
|
20
20
|
// in between
|
|
21
|
+
// checkpoint {busyMs} a WAL checkpoint (TRUNCATE) that waits `busyMs` at most for another connection's
|
|
22
|
+
// reader or writer, not the connection's own busy wait: the requests queued behind
|
|
23
|
+
// it never wait on it longer. Its result row (busy, log, checkpointed)
|
|
21
24
|
// close
|
|
22
25
|
import { chmodSync, closeSync, openSync } from "node:fs";
|
|
23
26
|
import { parentPort } from "node:worker_threads";
|
|
@@ -207,6 +210,10 @@ function handle(request) {
|
|
|
207
210
|
keepPrivate(request.file, false);
|
|
208
211
|
db.exec(NORMAL);
|
|
209
212
|
db.exec("PRAGMA foreign_keys = ON");
|
|
213
|
+
// Every byte a write of this connection frees (a deleted row, an overwritten value, a freed page) is zeroed in the
|
|
214
|
+
// page it writes, so a purge (or any delete) leaves no copy in the file's free space once the WAL is checkpointed
|
|
215
|
+
// (sqlite/store.ts #purged). Per connection: the server's and each runner's, every one that writes, set here.
|
|
216
|
+
db.exec("PRAGMA secure_delete = ON");
|
|
210
217
|
return null;
|
|
211
218
|
}
|
|
212
219
|
case "exec":
|
|
@@ -247,6 +254,16 @@ function handle(request) {
|
|
|
247
254
|
if (inTx)
|
|
248
255
|
end(false);
|
|
249
256
|
return null;
|
|
257
|
+
case "checkpoint": {
|
|
258
|
+
const conn = opened();
|
|
259
|
+
conn.exec(`PRAGMA busy_timeout = ${Math.max(0, Math.floor(request.busyMs))}`);
|
|
260
|
+
try {
|
|
261
|
+
return conn.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get() ?? null;
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
conn.exec(`PRAGMA busy_timeout = ${busyTimeout}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
250
267
|
case "close": {
|
|
251
268
|
if (inTx)
|
|
252
269
|
end(false);
|
package/dist/store/store.d.ts
CHANGED
|
@@ -50,6 +50,9 @@ export interface AgentRow {
|
|
|
50
50
|
/** The reference of the agent this one mirrors in another system ("<source>:<kind>:<id>"), or null for one made here. An
|
|
51
51
|
* agent with a reference is read-only from here: it is written on the first run that names it and never edited. */
|
|
52
52
|
readonly externalRef: string | null;
|
|
53
|
+
/** When the agent was purged (Agents.purge): its row is then a tombstone, its name and description blanked; null when it
|
|
54
|
+
* was not. */
|
|
55
|
+
readonly purgedAt: number | null;
|
|
53
56
|
}
|
|
54
57
|
/** The template package an agent was applied from, kept on the revision the apply made (its first): the package's id
|
|
55
58
|
* (a bundled one's name, an imported one's `github:<owner>/<repo>[/<path>]`), its SemVer version, and the commit SHA it
|
|
@@ -215,7 +218,32 @@ export interface RunRow {
|
|
|
215
218
|
readonly labels: RunLabels;
|
|
216
219
|
readonly startedAt: number;
|
|
217
220
|
readonly endedAt: number | null;
|
|
221
|
+
/** When the run was purged (Runs.purge): its row is then a tombstone, its cost and times kept, everything else of it
|
|
222
|
+
* gone; null when it was not. */
|
|
223
|
+
readonly purgedAt: number | null;
|
|
224
|
+
}
|
|
225
|
+
/** What a purge keeps of a run on its tombstone's metrics (`purged`), read from its journal before that goes: the reads of
|
|
226
|
+
* its spend that read the journal answer from it after (store-reads/cap.ts). `meters`: /usage's meter figures
|
|
227
|
+
* (metersOverStore); `spend`: what it spent as a run that reads running reads it (spendOverStore); `learning`: how its
|
|
228
|
+
* learning ended as GET /stats counts it (store-reads/learning.ts learningCountsOverStore), its state and reason alone,
|
|
229
|
+
* null for none. */
|
|
230
|
+
export interface PurgeKept {
|
|
231
|
+
readonly meters: unknown;
|
|
232
|
+
readonly spend: {
|
|
233
|
+
readonly usd: number | null;
|
|
234
|
+
readonly basis?: string;
|
|
235
|
+
};
|
|
236
|
+
readonly learning: {
|
|
237
|
+
readonly state: string;
|
|
238
|
+
readonly reason: string | null;
|
|
239
|
+
} | null;
|
|
218
240
|
}
|
|
241
|
+
/** A run's metrics as its tombstone keeps them (Runs.purge): what the totals and the analytics read of them, never a
|
|
242
|
+
* call's arguments or results, a prompt, a URL or a grader's words. Each number (or an unknown one's null) at the top;
|
|
243
|
+
* each tool's calls and errors by its name (GET /stats' top tools); the effort (its analytics group) and the cost basis
|
|
244
|
+
* (metrics.ts storedBasisOf), each a code; and `purged`, `kept`. Everything else goes: its models, a grader's verdict, an
|
|
245
|
+
* audit's or a replay's link, a learned loop's nodes, the path it took (no reader of a tombstone reads it). */
|
|
246
|
+
export declare function tombstoneMetrics(metrics: unknown, kept: PurgeKept): Record<string, unknown>;
|
|
219
247
|
/** What a runner may change on its run's row, under its claim. A final state with no `endedAt` is stamped now. */
|
|
220
248
|
export interface RunPatch {
|
|
221
249
|
readonly state?: RunState;
|
|
@@ -231,6 +259,10 @@ export interface RunPatch {
|
|
|
231
259
|
* fence); null clears them. While the run runs, a sign-in hashing to one of these cannot be forgotten (sign-ins-store.ts). */
|
|
232
260
|
readonly signInHanded?: readonly string[] | null;
|
|
233
261
|
readonly endedAt?: number | null;
|
|
262
|
+
/** A final patch that writes the end the run's job kept before its look-back (feedback-round.ts KeptEnd): a stop or a
|
|
263
|
+
* cancel waiting at its commit cut the look-back, never the job's end, and is refused (refuseLeftovers), unless a
|
|
264
|
+
* person's message waits too: that message's run is the one they stop or cancel. */
|
|
265
|
+
readonly keepsEnd?: boolean;
|
|
234
266
|
}
|
|
235
267
|
/** The row runs.update answers. When the patch wrote `signInHanded` (the runner's bind at its claim), `save` rides with it:
|
|
236
268
|
* whether this run holds the sign-in's save lease — true unless another run already holds one of the handed pointers,
|
|
@@ -657,6 +689,17 @@ export declare class AgentDeleting extends Error {
|
|
|
657
689
|
name: string;
|
|
658
690
|
constructor(agentId: string);
|
|
659
691
|
}
|
|
692
|
+
/** A write to a purged agent (Agents.purge): a revision, its current revision moved, its name or description, its archive,
|
|
693
|
+
* a run or a trigger of it. Refused, nothing written: its purge time is read under the agent row's lock, in the
|
|
694
|
+
* transaction that would write, so of a purge and such a write one commits first and the other sees it (the purge blanks
|
|
695
|
+
* what the write wrote, or the write is refused). A purged agent's row is its tombstone: nothing of it is written again. */
|
|
696
|
+
export declare class AgentPurged extends Error {
|
|
697
|
+
readonly agentId: string;
|
|
698
|
+
readonly archivedAt: number | null;
|
|
699
|
+
readonly purgedAt: number;
|
|
700
|
+
name: string;
|
|
701
|
+
constructor(agentId: string, archivedAt: number | null, purgedAt: number);
|
|
702
|
+
}
|
|
660
703
|
/** An agent's removal refused while a trigger of it is left (Agents.remove): a trigger keeps things outside its row (its
|
|
661
704
|
* registration upstream, its adapter settings) that only the trigger engine removes, so its row never goes with the
|
|
662
705
|
* agent's. Nothing written. */
|
|
@@ -674,6 +717,8 @@ export declare class ExternalBodyConflict extends Error {
|
|
|
674
717
|
name: string;
|
|
675
718
|
constructor(ref: string, version: string);
|
|
676
719
|
}
|
|
720
|
+
/** An agent's rows. Every write to an agent that exists (put, archive, addRevision, setCurrent; templates.upgrade; a run
|
|
721
|
+
* or a trigger of it) is refused once it is purged (AgentPurged), nothing written, and pinOnce writes no pin then. */
|
|
677
722
|
export interface Agents {
|
|
678
723
|
/** Creates the agent, or renames it (name, description) when it exists. */
|
|
679
724
|
put(agent: {
|
|
@@ -695,8 +740,10 @@ export interface Agents {
|
|
|
695
740
|
hasExternalVersion(agentId: string, version: string): Promise<boolean>;
|
|
696
741
|
/** The revision that maps `version` of its owning system, or undefined before that version's first run. */
|
|
697
742
|
externalRevision(agentId: string, version: string): Promise<RevisionRow | undefined>;
|
|
743
|
+
/** The agents, by `archived` (default false). A purged agent (Agents.purge) only with `purged`: what a total counts. */
|
|
698
744
|
list(options?: {
|
|
699
745
|
archived?: boolean;
|
|
746
|
+
purged?: boolean;
|
|
700
747
|
}): Promise<AgentRow[]>;
|
|
701
748
|
/** Archives the agent, or unarchives it. Archived, every trigger of it that is on is turned off in the same transaction
|
|
702
749
|
* (`disabled` set to `why`, JSON; its scope rows dropped), and those are the answer: from its commit no firing starts a
|
|
@@ -722,13 +769,21 @@ export interface Agents {
|
|
|
722
769
|
revisions(agentId: string): Promise<RevisionRow[]>;
|
|
723
770
|
/** Pins a revision saved with no MCP pin (before pins were made at save) at its first run: `pin` is written only while
|
|
724
771
|
* the revision still has none, and the pin it has then is the answer, so two first runs at once agree on one. Null when
|
|
725
|
-
* there is no such revision. */
|
|
772
|
+
* there is no such revision, or its agent is purged (nothing written). */
|
|
726
773
|
pinOnce(revisionId: string, pin: unknown): Promise<unknown>;
|
|
727
774
|
/** The agent's own rows gone, in one transaction (the last write of DELETE /agents/{id}, agent-delete.ts): its learning
|
|
728
775
|
* rows and kept proposals, its sign-ins and their tombstones, its revisions and the agent. Only once it has no run and
|
|
729
776
|
* no trigger left (runs.remove and the trigger engine's remove each first): refused while a run is (StoreConstraint)
|
|
730
777
|
* or a trigger is (TriggersLeft), read under the agent row's lock, nothing written. An agent with no row is no write. */
|
|
731
778
|
remove(agentId: string): Promise<void>;
|
|
779
|
+
/** The last write of DELETE /agents/{id} for an archived agent made here (agent-delete.ts purgeAgent), in one
|
|
780
|
+
* transaction under the agent row's lock: its learning rows, its sign-ins and their tombstones, the answer a template
|
|
781
|
+
* apply kept naming it, its revisions' content (body, skills, package, note, pins) and its own name and description
|
|
782
|
+
* blanked, and its purge time set last. Only once each of its runs is purged (Runs.purge) and no trigger is left:
|
|
783
|
+
* refused while a run is not (StoreConstraint) or a trigger is (TriggersLeft), nothing written. Its row and its
|
|
784
|
+
* revisions' ids stay, so its runs' tombstones and every total over them read as before. An agent with no row, or one
|
|
785
|
+
* purged already, is no write. On SQLite, once it commits, a WAL checkpoint (Runs.purge). */
|
|
786
|
+
purge(agentId: string): Promise<void>;
|
|
732
787
|
}
|
|
733
788
|
export interface Templates {
|
|
734
789
|
/** A template applied: the agent, its first revision (with the package) and its triggers, in one transaction, all or
|
|
@@ -849,7 +904,8 @@ export interface Runs {
|
|
|
849
904
|
/** A queued run of `revisionId`, its row's only writer until a runner claims it. With `origin`, the origin row is written
|
|
850
905
|
* in the same transaction, so a retry under the same key finds this run (runOfOrigin); a key already used is refused
|
|
851
906
|
* (StoreConstraint), and nothing is written. With `launch`, its launch row is written in the same transaction too (all
|
|
852
|
-
* or nothing).
|
|
907
|
+
* or nothing). A launch made from another run (its body's `replay.of`, `audit.of`) takes that run's row lock and
|
|
908
|
+
* reads it in the same transaction: made from a purged run, it is refused (RunPurged), nothing written. */
|
|
853
909
|
create(run: {
|
|
854
910
|
id: string;
|
|
855
911
|
agentId: string;
|
|
@@ -900,7 +956,9 @@ export interface Runs {
|
|
|
900
956
|
learning: unknown;
|
|
901
957
|
}[]>;
|
|
902
958
|
/** Newest first, at most `limit` (no limit: all). `archived` filters on the run's own archive time (true: archived,
|
|
903
|
-
* false: not); its agent's archive is read apart. A built-in learning run (one a kept learning names) is never listed. `labels`: only runs holding every one of these labels, key and value.
|
|
959
|
+
* false: not); its agent's archive is read apart. A built-in learning run (one a kept learning names) is never listed. `labels`: only runs holding every one of these labels, key and value.
|
|
960
|
+
* A purged run (Runs.purge) only with `purged`: the readers of money and counts (usage, stats, analytics) and an
|
|
961
|
+
* import's read of the sessions it holds, never a reader of a run's content. */
|
|
904
962
|
list(filter: {
|
|
905
963
|
agentId?: string;
|
|
906
964
|
revisionId?: string;
|
|
@@ -908,6 +966,7 @@ export interface Runs {
|
|
|
908
966
|
archived?: boolean;
|
|
909
967
|
labels?: RunLabels;
|
|
910
968
|
limit?: number;
|
|
969
|
+
purged?: boolean;
|
|
911
970
|
}): Promise<RunRow[]>;
|
|
912
971
|
/** A page of a runs list, in one read: newest first, `offset` rows on, at most `limit`, and whether more follow (`more`).
|
|
913
972
|
* Each with its listing as its row keeps it (RUN_LISTING_COLUMNS: null while none has been built), whether that is
|
|
@@ -975,11 +1034,32 @@ export interface Runs {
|
|
|
975
1034
|
/** Archives the run (its own archive time), or unarchives it: idempotent. */
|
|
976
1035
|
archive(runId: string, archived: boolean): Promise<void>;
|
|
977
1036
|
/** Every run of agent `agentId` with its state, oldest first, its built-in learning runs included (which list leaves
|
|
978
|
-
* out): what deleting the agent stops and removes. */
|
|
1037
|
+
* out): what deleting the agent stops and removes, with the runner that holds each (null: none) and its purge time. */
|
|
979
1038
|
every(agentId: string): Promise<{
|
|
980
1039
|
id: string;
|
|
981
1040
|
state: RunState;
|
|
1041
|
+
runner: string | null;
|
|
1042
|
+
purgedAt: number | null;
|
|
982
1043
|
}[]>;
|
|
1044
|
+
/** The runs made from run `runId` that hold what it was given or did, by id: each run whose launch replays its input
|
|
1045
|
+
* (the launch body's `replay.of`) or audits it (`audit.of`: its audit's shadow run), and each learning run its kept
|
|
1046
|
+
* learning names (learning_proposals' learning_run_id). What purging the run purges with it. */
|
|
1047
|
+
madeFrom(runId: string): Promise<string[]>;
|
|
1048
|
+
/** A run purged, in one transaction under its row's lock (DELETE /runs/{id}, agent-delete.ts): its journal, commands,
|
|
1049
|
+
* origin, launch, live view and viewers, its channel posts with their tries and what each channel was told of it, a
|
|
1050
|
+
* trigger's conversation that names it and its changes in the run-change feed gone; a trigger's last run, a firing's run
|
|
1051
|
+
* or wait and a machine's token that name it let go (null); its kept learning's content blanked, each row kept with its
|
|
1052
|
+
* proposal's state alone, as the learning meter and the list of learning runs read it; its row's content blanked
|
|
1053
|
+
* (summary, labels, handed sign-ins, listing) with `kept` folded into its metrics (`purged`), and its purge time set
|
|
1054
|
+
* last. Its id, agent, revision, state, outcome, times, cost and metrics stay: a tombstone, which every total sums as
|
|
1055
|
+
* before. Only a run that is final and that no runner holds: one that is not is refused (StoreConstraint), nothing
|
|
1056
|
+
* written; so is one a run made from it (madeFrom's replays and shadow) not purged names (RunsMadeFromLeft): each is
|
|
1057
|
+
* purged first, and Runs.create, under the same row lock, makes none once it is purged. A run with no row, or one
|
|
1058
|
+
* purged already, is no write. On SQLite, once it commits, a WAL checkpoint (TRUNCATE): with secure_delete on in every
|
|
1059
|
+
* connection, neither the file nor its WAL keeps the purged bytes after it. A checkpoint another connection holds back
|
|
1060
|
+
* is retried once, then told to the store's `warn`; it never fails the purge. Postgres: the old row versions are the
|
|
1061
|
+
* database's until its VACUUM. */
|
|
1062
|
+
purge(runId: string, kept: PurgeKept): Promise<void>;
|
|
983
1063
|
/** The run's rows gone, in one transaction: its journal, commands, origin, launch, live view and viewers, its channel
|
|
984
1064
|
* posts with their tries and what each channel was told of it, its kept learning, its changes in the run-change feed,
|
|
985
1065
|
* a trigger's conversation that names it, and the run (a firing or trigger that names it keeps no run: ON DELETE SET
|
|
@@ -993,11 +1073,14 @@ export interface Runs {
|
|
|
993
1073
|
* so its next turn is booked on top of what it spent), and `command` (the follow-up's steer) is written untaken for it.
|
|
994
1074
|
* Only an agentic run that ended completed (state 'ended') or that a person stopped (state 'stopped', outcome
|
|
995
1075
|
* 'stopped'), not imported and not a learning run, reopens: a workflow, a failed or cancelled run, any other, or none
|
|
996
|
-
* is RunFinal, and nothing is written.
|
|
1076
|
+
* is RunFinal, and nothing is written. With `endedAt`, only the run whose end is at that time (a grade's round opens
|
|
1077
|
+
* the end it read, never a later one): any other is RunFinal too. */
|
|
997
1078
|
reopen(runId: string, command: {
|
|
998
1079
|
kind: string;
|
|
999
1080
|
body?: unknown;
|
|
1000
|
-
}, options?: LaunchSealedWrite
|
|
1081
|
+
}, options?: LaunchSealedWrite & {
|
|
1082
|
+
readonly endedAt?: number;
|
|
1083
|
+
}): Promise<Command>;
|
|
1001
1084
|
/** Wakes a parked run: its wake_at set to now on the deadlines' clock, so a runner claims it at its next poll (the
|
|
1002
1085
|
* claimable rule's wake branch) and its loop reads what it waits on again. True when the run was parked; a run in any
|
|
1003
1086
|
* other state (or none) is left as it is, false. Its claim, if one holds it while it parks, is let go as before: the
|
|
@@ -1334,6 +1417,46 @@ export interface ComputerLedgerRow {
|
|
|
1334
1417
|
readonly detail: Readonly<Record<string, unknown>>;
|
|
1335
1418
|
readonly updatedAt: number;
|
|
1336
1419
|
}
|
|
1420
|
+
/** One computer a run's claim made through its provider (store schema `computer_intervals`): by the provider's own id
|
|
1421
|
+
* for it (`providerId`), what its create asked for (null where it asked none), when it was made, whether the run's park
|
|
1422
|
+
* kept it past its claim, and when the provider's delete of it answered ok (`endedAt`; null while none did). */
|
|
1423
|
+
export interface ComputerIntervalRow {
|
|
1424
|
+
readonly runId: string;
|
|
1425
|
+
/** The provider's id in this server's registry (its setting's provider: "docker", "e2b", ...). */
|
|
1426
|
+
readonly provider: string;
|
|
1427
|
+
/** The provider's own id for the computer: what its invoice and console name it by. */
|
|
1428
|
+
readonly providerId: string;
|
|
1429
|
+
/** sandbox, desktop or phone; null when not known. */
|
|
1430
|
+
readonly kind: string | null;
|
|
1431
|
+
readonly gpuType: string | null;
|
|
1432
|
+
readonly gpuCount: number | null;
|
|
1433
|
+
readonly cpus: number | null;
|
|
1434
|
+
readonly memoryMb: number | null;
|
|
1435
|
+
/** Its disk in GB, where its provider said it; null else. */
|
|
1436
|
+
readonly diskGb: number | null;
|
|
1437
|
+
readonly startedAt: number;
|
|
1438
|
+
readonly kept: boolean;
|
|
1439
|
+
readonly endedAt: number | null;
|
|
1440
|
+
/** How much of its time a claim of its run booked on the run's ledger (compute-cost.ts), in ms; 0 for none. */
|
|
1441
|
+
readonly bookedMs: number;
|
|
1442
|
+
}
|
|
1443
|
+
/** Each computer a run made, from its create to its proven delete: how long it lived for its run, and one left running
|
|
1444
|
+
* after its run (no `endedAt`). Every write is idempotent. */
|
|
1445
|
+
export interface ComputerIntervals {
|
|
1446
|
+
/** The computer's row, made: a second call for the same provider and id changes nothing (a retried create answers the
|
|
1447
|
+
* computer it made). */
|
|
1448
|
+
opened(row: Omit<ComputerIntervalRow, "kept" | "endedAt" | "bookedMs">): Promise<void>;
|
|
1449
|
+
/** The computer was kept past its claim (the run's park). */
|
|
1450
|
+
kept(provider: string, providerId: string): Promise<void>;
|
|
1451
|
+
/** The provider's delete of the computer answered ok at `at`: its end, written only while none is (a later delete of
|
|
1452
|
+
* one already ended never moves it). A computer with no row is left without one. */
|
|
1453
|
+
ended(provider: string, providerId: string, at: number): Promise<void>;
|
|
1454
|
+
/** A claim booked `ms` more of the computer's time on its run's ledger: added to its booked time (a figure that is not
|
|
1455
|
+
* a whole number of ms above 0 adds nothing). A computer with no row is left without one. */
|
|
1456
|
+
booked(provider: string, providerId: string, ms: number): Promise<void>;
|
|
1457
|
+
/** The run's computers, by when each was made. */
|
|
1458
|
+
forRun(runId: string): Promise<ComputerIntervalRow[]>;
|
|
1459
|
+
}
|
|
1337
1460
|
/** The labels of the sessions a provider runs with none of its own (a phone provider's), so its list, an attach by
|
|
1338
1461
|
* labels and the janitor outlive a restart. */
|
|
1339
1462
|
export interface ComputerLedger {
|
|
@@ -1742,6 +1865,8 @@ export interface Store {
|
|
|
1742
1865
|
readonly sealedKeys: SealedKeys;
|
|
1743
1866
|
/** The labels of the sessions a provider runs with none of its own (a phone provider's). */
|
|
1744
1867
|
readonly computerLedger: ComputerLedger;
|
|
1868
|
+
/** Each computer a run made, from its create to its proven delete. */
|
|
1869
|
+
readonly computerIntervals: ComputerIntervals;
|
|
1745
1870
|
/** The people's own machines paired with this server, and the codes that pair them (spec/computer/attach.md). */
|
|
1746
1871
|
readonly pairedComputers: PairedComputers;
|
|
1747
1872
|
/** The tokens minted for hosted machines that dial this server, each kept as its SHA-256. */
|
|
@@ -1763,9 +1888,9 @@ export interface Store {
|
|
|
1763
1888
|
readonly sealedSecrets: SealedSecrets;
|
|
1764
1889
|
/** Runs `work`, writes that replace a secret the store kept in the clear (it answers how many rows it rewrote), so the
|
|
1765
1890
|
* bytes they replace are not left in the store's files. SQLite: a marker file beside the database is written first;
|
|
1766
|
-
* secure_delete is on
|
|
1767
|
-
* or an earlier start left the marker, a VACUUM (no free page or free block keeps a copy an
|
|
1768
|
-
* left) and a WAL checkpoint (TRUNCATE: the log keeps no older frame). The marker is removed once the checkpoint is
|
|
1891
|
+
* secure_delete is on in every connection from its open, so a rewritten row's old bytes are zeroed in its page; then,
|
|
1892
|
+
* when a row was rewritten or an earlier start left the marker, a VACUUM (no free page or free block keeps a copy an
|
|
1893
|
+
* update left before secure_delete was on) and a WAL checkpoint (TRUNCATE: the log keeps no older frame). The marker is removed once the checkpoint is
|
|
1769
1894
|
* done; `left` says why it was kept, for the next start to finish. Postgres: `work` alone (a replaced row version is
|
|
1770
1895
|
* the database's until its vacuum). */
|
|
1771
1896
|
secureRewrite?(work: () => Promise<number>, options?: SecureRewriteOptions): Promise<SecureRewrite>;
|
|
@@ -1954,6 +2079,21 @@ export declare class StoreConstraint extends Error {
|
|
|
1954
2079
|
export declare class AnswerTaken extends StoreConstraint {
|
|
1955
2080
|
constructor(message: string, index: string);
|
|
1956
2081
|
}
|
|
2082
|
+
/** A run's purge refused while a run made from it (a replay of its input, its audit's shadow run: Runs.madeFrom) is not
|
|
2083
|
+
* purged: one was made after the purge read them (a claim that read the run's launch before). `runs`: those runs. A
|
|
2084
|
+
* StoreConstraint, so the caller answers it as a run not final; nothing written. */
|
|
2085
|
+
export declare class RunsMadeFromLeft extends StoreConstraint {
|
|
2086
|
+
readonly runId: string;
|
|
2087
|
+
readonly runs: readonly string[];
|
|
2088
|
+
constructor(runId: string, runs: readonly string[]);
|
|
2089
|
+
}
|
|
2090
|
+
/** A run made from a purged run (its launch's `replay.of` or `audit.of` names it): refused, nothing written. Its source's
|
|
2091
|
+
* input is gone, and a copy of it is never made again (Runs.create). `runId`: the purged run. */
|
|
2092
|
+
export declare class RunPurged extends Error {
|
|
2093
|
+
readonly runId: string;
|
|
2094
|
+
readonly made: string;
|
|
2095
|
+
constructor(runId: string, made: string);
|
|
2096
|
+
}
|
|
1957
2097
|
/** An agent's current revision is not the one a conditional write expected (addRevision's `ifCurrent`): another write
|
|
1958
2098
|
* moved it first. `current` is the agent's current revision now. Nothing was written. */
|
|
1959
2099
|
export declare class RevisionMoved extends Error {
|
package/dist/store/store.js
CHANGED
|
@@ -8,6 +8,28 @@ export function endedReason(state) {
|
|
|
8
8
|
export function stoppedByPerson(run, kind) {
|
|
9
9
|
return run.state === "stopped" && run.outcome === "stopped" && kind === "agentic" && run.importedFrom === null;
|
|
10
10
|
}
|
|
11
|
+
/** A run's metrics as its tombstone keeps them (Runs.purge): what the totals and the analytics read of them, never a
|
|
12
|
+
* call's arguments or results, a prompt, a URL or a grader's words. Each number (or an unknown one's null) at the top;
|
|
13
|
+
* each tool's calls and errors by its name (GET /stats' top tools); the effort (its analytics group) and the cost basis
|
|
14
|
+
* (metrics.ts storedBasisOf), each a code; and `purged`, `kept`. Everything else goes: its models, a grader's verdict, an
|
|
15
|
+
* audit's or a replay's link, a learned loop's nodes, the path it took (no reader of a tombstone reads it). */
|
|
16
|
+
export function tombstoneMetrics(metrics, kept) {
|
|
17
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
18
|
+
const count = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
|
|
19
|
+
const from = isObject(metrics) ? metrics : {};
|
|
20
|
+
const out = {};
|
|
21
|
+
for (const [key, value] of Object.entries(from))
|
|
22
|
+
if (value === null || (typeof value === "number" && Number.isFinite(value)))
|
|
23
|
+
out[key] = value;
|
|
24
|
+
if (isObject(from.tools))
|
|
25
|
+
out.tools = Object.fromEntries(Object.entries(from.tools).map(([name, each]) => [name, { calls: count(isObject(each) ? each.calls : 0), errors: count(isObject(each) ? each.errors : 0) }]));
|
|
26
|
+
// A code is a word with no space (an effort level, a cost basis).
|
|
27
|
+
for (const key of ["effort", "costBasis"])
|
|
28
|
+
if (typeof from[key] === "string" && /^[\w-]{1,64}$/.test(from[key]))
|
|
29
|
+
out[key] = from[key];
|
|
30
|
+
out.purged = kept;
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
11
33
|
/** A firing a trigger has no room for: `due` firings are DUE_KEPT already. Nothing was written. */
|
|
12
34
|
export class TriggerFull extends Error {
|
|
13
35
|
triggerId;
|
|
@@ -40,6 +62,22 @@ export class AgentDeleting extends Error {
|
|
|
40
62
|
this.agentId = agentId;
|
|
41
63
|
}
|
|
42
64
|
}
|
|
65
|
+
/** A write to a purged agent (Agents.purge): a revision, its current revision moved, its name or description, its archive,
|
|
66
|
+
* a run or a trigger of it. Refused, nothing written: its purge time is read under the agent row's lock, in the
|
|
67
|
+
* transaction that would write, so of a purge and such a write one commits first and the other sees it (the purge blanks
|
|
68
|
+
* what the write wrote, or the write is refused). A purged agent's row is its tombstone: nothing of it is written again. */
|
|
69
|
+
export class AgentPurged extends Error {
|
|
70
|
+
agentId;
|
|
71
|
+
archivedAt;
|
|
72
|
+
purgedAt;
|
|
73
|
+
name = "AgentPurged";
|
|
74
|
+
constructor(agentId, archivedAt, purgedAt) {
|
|
75
|
+
super(`the agent ${agentId} is purged`);
|
|
76
|
+
this.agentId = agentId;
|
|
77
|
+
this.archivedAt = archivedAt;
|
|
78
|
+
this.purgedAt = purgedAt;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
43
81
|
/** An agent's removal refused while a trigger of it is left (Agents.remove): a trigger keeps things outside its row (its
|
|
44
82
|
* registration upstream, its adapter settings) that only the trigger engine removes, so its row never goes with the
|
|
45
83
|
* agent's. Nothing written. */
|
|
@@ -235,6 +273,31 @@ export class AnswerTaken extends StoreConstraint {
|
|
|
235
273
|
this.name = "AnswerTaken";
|
|
236
274
|
}
|
|
237
275
|
}
|
|
276
|
+
/** A run's purge refused while a run made from it (a replay of its input, its audit's shadow run: Runs.madeFrom) is not
|
|
277
|
+
* purged: one was made after the purge read them (a claim that read the run's launch before). `runs`: those runs. A
|
|
278
|
+
* StoreConstraint, so the caller answers it as a run not final; nothing written. */
|
|
279
|
+
export class RunsMadeFromLeft extends StoreConstraint {
|
|
280
|
+
runId;
|
|
281
|
+
runs;
|
|
282
|
+
constructor(runId, runs) {
|
|
283
|
+
super(`run ${runId} has runs made from it not purged (${runs.join(", ")}): each is purged first`);
|
|
284
|
+
this.runId = runId;
|
|
285
|
+
this.runs = runs;
|
|
286
|
+
this.name = "RunsMadeFromLeft";
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/** A run made from a purged run (its launch's `replay.of` or `audit.of` names it): refused, nothing written. Its source's
|
|
290
|
+
* input is gone, and a copy of it is never made again (Runs.create). `runId`: the purged run. */
|
|
291
|
+
export class RunPurged extends Error {
|
|
292
|
+
runId;
|
|
293
|
+
made;
|
|
294
|
+
constructor(runId, made) {
|
|
295
|
+
super(`run ${made} is made from run ${runId}, which is purged: it is not started`);
|
|
296
|
+
this.runId = runId;
|
|
297
|
+
this.made = made;
|
|
298
|
+
this.name = "RunPurged";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
238
301
|
/** An agent's current revision is not the one a conditional write expected (addRevision's `ifCurrent`): another write
|
|
239
302
|
* moved it first. `current` is the agent's current revision now. Nothing was written. */
|
|
240
303
|
export class RevisionMoved extends Error {
|
|
@@ -21,7 +21,8 @@ export async function analytics(store, filter = {}) {
|
|
|
21
21
|
}
|
|
22
22
|
const fromMs = filter.from === undefined ? null : Date.parse(filter.from);
|
|
23
23
|
const toMs = filter.to === undefined ? null : Date.parse(filter.to);
|
|
24
|
-
|
|
24
|
+
// Purged runs too: a tombstone keeps its state, times, cost and metrics, which these figures read.
|
|
25
|
+
const runs = await store.runs.list({ purged: true, ...(filter.agent !== undefined ? { agentId: filter.agent } : {}) });
|
|
25
26
|
const agentArchived = new Map();
|
|
26
27
|
const harnessOf = new Map();
|
|
27
28
|
const facts = [];
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { Context } from "@earendil-works/chord/context";
|
|
2
2
|
import { type Meters } from "../cap.js";
|
|
3
|
-
import type { Store } from "../store/store.js";
|
|
4
|
-
import { type CostBasis } from "../runner/metrics.js";
|
|
3
|
+
import type { PurgeKept, Store } from "../store/store.js";
|
|
4
|
+
import { cliModelWorked, type CostBasis } from "../runner/metrics.js";
|
|
5
|
+
/** A CLI run whose model answered or called a tool with no model request booked (metrics.ts cliModelWorked): what it
|
|
6
|
+
* spent no booking read, so its spend is unknown. */
|
|
7
|
+
export declare const unbookedCli: (entries: Parameters<typeof cliModelWorked>[0]) => boolean;
|
|
8
|
+
/** What a purged run's tombstone keeps of its journal's figures (Runs.purge folds them into its metrics), read only for a
|
|
9
|
+
* run whose journal reads empty; undefined for any other run. */
|
|
10
|
+
export declare function purgeKept(store: Store, runId: string): Promise<PurgeKept | undefined>;
|
|
5
11
|
/** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
|
|
6
12
|
export declare function spentOverStore(store: Store, runId: string, context: Context): Promise<number | null>;
|
|
7
13
|
/** What a run has spent (spentOverStore) and how its model requests were priced so far (metrics.ts costBasisOf), from one
|
|
@@ -10,13 +16,17 @@ export declare function spendOverStore(store: Store, runId: string, context: Con
|
|
|
10
16
|
usd: number | null;
|
|
11
17
|
basis: CostBasis | undefined;
|
|
12
18
|
}>;
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
* file-backed run held each on its root. A run with no journal reads as a store that was never there: every figure 0, tokens unknown. */
|
|
16
|
-
export declare function metersOverStore(store: Store, runId: string, context: Context): Promise<Meters & {
|
|
19
|
+
/** A run's /usage meters, with each meter's known dollars. */
|
|
20
|
+
type KnownMeters = Meters & {
|
|
17
21
|
known: {
|
|
18
22
|
tokens: number;
|
|
19
23
|
browser: number;
|
|
20
24
|
others: number;
|
|
21
25
|
};
|
|
22
|
-
}
|
|
26
|
+
};
|
|
27
|
+
/** /usage's meters for a run that ended, over the one store's journal (design 2.1): cap.ts meterFiguresFrom over A's
|
|
28
|
+
* entries, with the run's paid tool charges folded back from its paid-tools session (journal.ts withPaidCosts) as the
|
|
29
|
+
* file-backed run held each on its root, and the decisions Parallel charged on its own API (parallelDecisionsOn) on its
|
|
30
|
+
* tools meter beside them. A run with no journal reads as a store that was never there: every figure 0, tokens unknown. */
|
|
31
|
+
export declare function metersOverStore(store: Store, runId: string, context: Context): Promise<KnownMeters>;
|
|
32
|
+
export {};
|
package/dist/store-reads/cap.js
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
import { meterFiguresFrom, spentFrom } from "../cap.js";
|
|
2
|
-
import { bookedListSpend, bookedSpend, cliModelWorked, costBasisOf, costIsBooked } from "../runner/metrics.js";
|
|
2
|
+
import { bookedDecisionsOf, bookedListSpend, bookedSpend, cliModelWorked, costBasisOf, costIsBooked } from "../runner/metrics.js";
|
|
3
|
+
import { booksDecisions, decisionsOverStore, parallelChargedDecisions } from "./decisions.js";
|
|
3
4
|
import { isCliRecord, runEntries, withPaidCosts } from "./journal.js";
|
|
4
5
|
/** A CLI run whose model answered or called a tool with no model request booked (metrics.ts cliModelWorked): what it
|
|
5
6
|
* spent no booking read, so its spend is unknown. */
|
|
6
|
-
const unbookedCli = (entries) => isCliRecord(entries) && bookedSpend(entries, "model").requests === 0 && cliModelWorked(entries);
|
|
7
|
+
export const unbookedCli = (entries) => isCliRecord(entries) && bookedSpend(entries, "model").requests === 0 && cliModelWorked(entries);
|
|
8
|
+
/** What a purged run's tombstone keeps of its journal's figures (Runs.purge folds them into its metrics), read only for a
|
|
9
|
+
* run whose journal reads empty; undefined for any other run. */
|
|
10
|
+
export async function purgeKept(store, runId) {
|
|
11
|
+
const row = await store.runs.get(runId);
|
|
12
|
+
if (!row || row.purgedAt === null)
|
|
13
|
+
return undefined;
|
|
14
|
+
return row.metrics?.purged;
|
|
15
|
+
}
|
|
7
16
|
/** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
|
|
8
17
|
export async function spentOverStore(store, runId, context) {
|
|
9
|
-
|
|
18
|
+
const entries = await runEntries(store, runId, context);
|
|
19
|
+
if (!entries.length) {
|
|
20
|
+
const kept = await purgeKept(store, runId);
|
|
21
|
+
if (kept)
|
|
22
|
+
return kept.spend.usd;
|
|
23
|
+
}
|
|
24
|
+
return spentOf(entries);
|
|
10
25
|
}
|
|
11
26
|
/** What a run has spent (spentOverStore) and how its model requests were priced so far (metrics.ts costBasisOf), from one
|
|
12
27
|
* read of its journal; `basis` undefined while it booked no model request. */
|
|
13
28
|
export async function spendOverStore(store, runId, context) {
|
|
14
29
|
const entries = await runEntries(store, runId, context);
|
|
30
|
+
if (!entries.length) {
|
|
31
|
+
const kept = await purgeKept(store, runId);
|
|
32
|
+
if (kept)
|
|
33
|
+
return { usd: kept.spend.usd, basis: kept.spend.basis };
|
|
34
|
+
}
|
|
15
35
|
const model = bookedSpend(entries, "model");
|
|
16
36
|
return { usd: spentOf(entries), basis: model.requests > 0 ? costBasisOf(model) : undefined };
|
|
17
37
|
}
|
|
@@ -37,11 +57,41 @@ function spentOf(entries) {
|
|
|
37
57
|
}
|
|
38
58
|
/** /usage's meters for a run that ended, over the one store's journal (design 2.1): cap.ts meterFiguresFrom over A's
|
|
39
59
|
* entries, with the run's paid tool charges folded back from its paid-tools session (journal.ts withPaidCosts) as the
|
|
40
|
-
* file-backed run held each on its root
|
|
60
|
+
* file-backed run held each on its root, and the decisions Parallel charged on its own API (parallelDecisionsOn) on its
|
|
61
|
+
* tools meter beside them. A run with no journal reads as a store that was never there: every figure 0, tokens unknown. */
|
|
41
62
|
export async function metersOverStore(store, runId, context) {
|
|
42
63
|
const raw = await runEntries(store, runId, context);
|
|
43
64
|
if (!raw.length)
|
|
65
|
+
return metersOfEntries(store, runId, raw);
|
|
66
|
+
return parallelDecisionsOn(store, runId, raw, context, await metersOfEntries(store, runId, raw));
|
|
67
|
+
}
|
|
68
|
+
/** A run's decisions Parallel charged on its own API (store-reads/decisions.ts chargedByParallel) on its tools meter, as
|
|
69
|
+
* GET /runs/{run_id}/usage puts them on Parallel's tools line: each a paid call, at its list price. They leave the rest
|
|
70
|
+
* of the run's cost (its tokens meter, which /usage reads as its cost less its tools) as they join its tools, so no
|
|
71
|
+
* total moves. As for a paid call, /usage says no "at most": one booked at its hold, or of no known cost, makes the
|
|
72
|
+
* meter unknown, its exact charges known beside it. */
|
|
73
|
+
async function parallelDecisionsOn(store, runId, raw, context, meters) {
|
|
74
|
+
if (!booksDecisions(raw))
|
|
75
|
+
return meters;
|
|
76
|
+
const ids = parallelChargedDecisions(await decisionsOverStore(store, runId, context));
|
|
77
|
+
if (!ids.size)
|
|
78
|
+
return meters;
|
|
79
|
+
const charged = bookedDecisionsOf(raw, ids);
|
|
80
|
+
if (!charged.requests)
|
|
81
|
+
return meters;
|
|
82
|
+
const usd = charged.unpriced > 0 || charged.bounded > 0 ? null : charged.known;
|
|
83
|
+
const plus = (figure) => (figure === null || usd === null ? null : figure + usd);
|
|
84
|
+
return { ...meters, tools: plus(meters.tools), others: plus(meters.others), known: { ...meters.known, others: meters.known.others + charged.known } };
|
|
85
|
+
}
|
|
86
|
+
/** metersOverStore's figures over `raw`, the run's entries, before its Parallel decisions join its tools meter. */
|
|
87
|
+
async function metersOfEntries(store, runId, raw) {
|
|
88
|
+
if (!raw.length) {
|
|
89
|
+
// A purged run's journal is gone: its figures are the ones its purge read from it (Runs.purge, PurgeKept).
|
|
90
|
+
const kept = await purgeKept(store, runId);
|
|
91
|
+
if (kept)
|
|
92
|
+
return kept.meters;
|
|
44
93
|
return { tools: 0, tokens: null, browser: { usd: 0, seconds: 0 }, others: 0, known: { tokens: 0, browser: 0, others: 0 } };
|
|
94
|
+
}
|
|
45
95
|
const figures = meterFiguresFrom(withPaidCosts(raw).map((entry) => ({ conversation: Number(entry.conversationId ?? 0), entry })));
|
|
46
96
|
// A run whose record carries no cost of its own (a CLI harness's, a workflow's): its tokens meter is its model requests
|
|
47
97
|
// at what they were booked at (metrics.ts bookedSpend, the one reader), never the $0 its record reads.
|
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
import type { Context } from "@earendil-works/chord/context";
|
|
2
2
|
import type { Store } from "../store/store.js";
|
|
3
|
+
/** Who charges for a decision Parallel answered on its own API, on this server's key: Parallel, the same name its web
|
|
4
|
+
* tools' charges carry (paid-calls.ts). */
|
|
5
|
+
export declare const PARALLEL_CHARGER = "parallel";
|
|
6
|
+
/** Whether a decision's receipt says Parallel charged for it as a paid call: answered by the `parallel` provider on
|
|
7
|
+
* Parallel's own API, booked at Parallel's list price (`cost_basis` "estimate", @parcha/agentrun-decisions
|
|
8
|
+
* PARALLEL_LIST_PRICE_USD). One the same provider sent through OpenRouter (Parallel's model there) is booked at the cost
|
|
9
|
+
* OpenRouter reported ("reported", or "unknown"): OpenRouter charged it, a model's cost, and it stays one. */
|
|
10
|
+
export declare const chargedByParallel: (receipt: {
|
|
11
|
+
readonly provider?: unknown;
|
|
12
|
+
readonly cost_basis?: unknown;
|
|
13
|
+
}) => boolean;
|
|
14
|
+
/** A receipt's decision id, the one its booking names (`decision:<id>`). */
|
|
15
|
+
export declare const receiptIdOf: (receipt: Record<string, unknown>) => string;
|
|
16
|
+
/** The ids of the decisions among `receipts` Parallel charged as a paid call (chargedByParallel). */
|
|
17
|
+
export declare function parallelChargedDecisions(receipts: Iterable<Record<string, unknown>>): Set<string>;
|
|
18
|
+
/** Whether a run's journal `entries` book a workflow decision (a request named `decision:<id>`), so its receipts are
|
|
19
|
+
* worth reading. */
|
|
20
|
+
export declare const booksDecisions: (entries: readonly {
|
|
21
|
+
readonly kind: string;
|
|
22
|
+
readonly data?: unknown;
|
|
23
|
+
}[]) => boolean;
|
|
3
24
|
/** A run's decision receipts: the keyed `.count` and family rows in order, read through
|
|
4
25
|
* the one store's journal. */
|
|
5
26
|
export declare function decisionsOverStore(store: Store, runId: string, context: Context): Promise<Array<Record<string, unknown>>>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RUNNER_RECORDS } from "@parcha/agentrun-kernel";
|
|
1
2
|
import { decisionKey } from "../workflow/decisions.js";
|
|
2
3
|
/** A session-scoped document's current value, by kind and (for a family) key: findDocument, then its stored value. */
|
|
3
4
|
async function sessionDocValue(reads, kind, key, context) {
|
|
@@ -7,6 +8,27 @@ async function sessionDocValue(reads, kind, key, context) {
|
|
|
7
8
|
const stored = await reads.document(record.id, "current", context);
|
|
8
9
|
return stored ? stored.value : undefined;
|
|
9
10
|
}
|
|
11
|
+
/** Who charges for a decision Parallel answered on its own API, on this server's key: Parallel, the same name its web
|
|
12
|
+
* tools' charges carry (paid-calls.ts). */
|
|
13
|
+
export const PARALLEL_CHARGER = "parallel";
|
|
14
|
+
/** Whether a decision's receipt says Parallel charged for it as a paid call: answered by the `parallel` provider on
|
|
15
|
+
* Parallel's own API, booked at Parallel's list price (`cost_basis` "estimate", @parcha/agentrun-decisions
|
|
16
|
+
* PARALLEL_LIST_PRICE_USD). One the same provider sent through OpenRouter (Parallel's model there) is booked at the cost
|
|
17
|
+
* OpenRouter reported ("reported", or "unknown"): OpenRouter charged it, a model's cost, and it stays one. */
|
|
18
|
+
export const chargedByParallel = (receipt) => receipt.provider === "parallel" && receipt.cost_basis === "estimate";
|
|
19
|
+
/** A receipt's decision id, the one its booking names (`decision:<id>`). */
|
|
20
|
+
export const receiptIdOf = (receipt) => String(receipt.decision_id ?? receipt.id);
|
|
21
|
+
/** The ids of the decisions among `receipts` Parallel charged as a paid call (chargedByParallel). */
|
|
22
|
+
export function parallelChargedDecisions(receipts) {
|
|
23
|
+
const ids = new Set();
|
|
24
|
+
for (const receipt of receipts)
|
|
25
|
+
if (chargedByParallel(receipt))
|
|
26
|
+
ids.add(receiptIdOf(receipt));
|
|
27
|
+
return ids;
|
|
28
|
+
}
|
|
29
|
+
/** Whether a run's journal `entries` book a workflow decision (a request named `decision:<id>`), so its receipts are
|
|
30
|
+
* worth reading. */
|
|
31
|
+
export const booksDecisions = (entries) => entries.some((e) => e.kind === RUNNER_RECORDS.request && String(e.data?.model ?? "").startsWith("decision:"));
|
|
10
32
|
/** A run's decision receipts: the keyed `.count` and family rows in order, read through
|
|
11
33
|
* the one store's journal. */
|
|
12
34
|
export async function decisionsOverStore(store, runId, context) {
|