@parcha/agentrun-serve 0.1.0-beta.50 → 0.1.0-beta.51

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.
Files changed (146) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +142 -17
  3. package/dist/agents-store.js +7 -3
  4. package/dist/api-only.d.ts +2 -1
  5. package/dist/api-only.js +3 -2
  6. package/dist/capacity.d.ts +50 -0
  7. package/dist/capacity.js +49 -0
  8. package/dist/cli-learning/claude-wire.d.ts +41 -0
  9. package/dist/cli-learning/claude-wire.js +258 -0
  10. package/dist/cli-learning/cli.d.ts +11 -0
  11. package/dist/cli-learning/cli.js +241 -0
  12. package/dist/cli-learning/codex-worktree.d.ts +10 -0
  13. package/dist/cli-learning/codex-worktree.js +119 -0
  14. package/dist/cli-learning/connections.d.ts +62 -0
  15. package/dist/cli-learning/connections.js +87 -0
  16. package/dist/cli-learning/entry.d.ts +1 -0
  17. package/dist/cli-learning/entry.js +4 -0
  18. package/dist/cli-learning/evidence.d.ts +78 -0
  19. package/dist/cli-learning/evidence.js +492 -0
  20. package/dist/cli-learning/hook.d.ts +33 -0
  21. package/dist/cli-learning/hook.js +544 -0
  22. package/dist/cli-learning/install.d.ts +33 -0
  23. package/dist/cli-learning/install.js +335 -0
  24. package/dist/cli-learning/observation-proof.d.ts +14 -0
  25. package/dist/cli-learning/observation-proof.js +10 -0
  26. package/dist/cli-learning/policy.d.ts +48 -0
  27. package/dist/cli-learning/policy.js +363 -0
  28. package/dist/cli-learning/schema.d.ts +16 -0
  29. package/dist/cli-learning/schema.js +27 -0
  30. package/dist/cli-learning/state.d.ts +23 -0
  31. package/dist/cli-learning/state.js +61 -0
  32. package/dist/cli.js +24 -2
  33. package/dist/console/assets/index-Bt1B51b7.js +733 -0
  34. package/dist/console/assets/index-sVftE9Ht.css +1 -0
  35. package/dist/console/index.html +2 -2
  36. package/dist/email/email-faux.d.ts +3 -1
  37. package/dist/email/email-faux.js +3 -2
  38. package/dist/email/routes.js +22 -12
  39. package/dist/harness-install.d.ts +6 -2
  40. package/dist/harness-install.js +40 -8
  41. package/dist/harness.d.ts +1 -1
  42. package/dist/harness.js +2 -2
  43. package/dist/learn/secrets.js +14 -3
  44. package/dist/learning-words.d.ts +1 -1
  45. package/dist/learning-words.js +1 -1
  46. package/dist/mcp-pin-first.js +2 -1
  47. package/dist/mcp-private-hosts.d.ts +24 -3
  48. package/dist/mcp-private-hosts.js +53 -3
  49. package/dist/model-gateway.d.ts +2 -0
  50. package/dist/model-gateway.js +3 -1
  51. package/dist/native/runtimes.d.ts +2 -0
  52. package/dist/native/runtimes.js +2 -0
  53. package/dist/observation/learning.d.ts +32 -0
  54. package/dist/observation/learning.js +66 -0
  55. package/dist/observation/observer.d.ts +8 -1
  56. package/dist/observation/observer.js +89 -9
  57. package/dist/observation/schema.js +12 -4
  58. package/dist/observation/usage-identity.d.ts +9 -0
  59. package/dist/observation/usage-identity.js +11 -0
  60. package/dist/openapi.js +89 -13
  61. package/dist/run-launch.d.ts +14 -0
  62. package/dist/run-launch.js +4 -0
  63. package/dist/run-view.d.ts +2 -0
  64. package/dist/run-view.js +12 -7
  65. package/dist/runner/adapter-loop.js +29 -26
  66. package/dist/runner/adapter-run-spec.js +2 -2
  67. package/dist/runner/beside.d.ts +13 -0
  68. package/dist/runner/beside.js +27 -8
  69. package/dist/runner/cli-run-spec.js +2 -2
  70. package/dist/runner/disk-free.d.ts +44 -0
  71. package/dist/runner/disk-free.js +96 -0
  72. package/dist/runner/feedback-round.d.ts +12 -8
  73. package/dist/runner/feedback-round.js +16 -30
  74. package/dist/runner/harness-loops.js +3 -1
  75. package/dist/runner/journal-kinds.d.ts +1 -1
  76. package/dist/runner/learning-end.js +4 -0
  77. package/dist/runner/main.d.ts +17 -6
  78. package/dist/runner/main.js +187 -45
  79. package/dist/runner/presence.d.ts +11 -2
  80. package/dist/runner/presence.js +31 -3
  81. package/dist/runner/run-child-host.d.ts +12 -15
  82. package/dist/runner/run-child-host.js +46 -108
  83. package/dist/runner/run-errors.js +2 -0
  84. package/dist/runner/runner-check.d.ts +51 -0
  85. package/dist/runner/runner-check.js +206 -0
  86. package/dist/runner/runner-metrics.d.ts +19 -0
  87. package/dist/runner/runner-metrics.js +49 -0
  88. package/dist/runner/runner.d.ts +37 -0
  89. package/dist/runner/runner.js +43 -3
  90. package/dist/runner/sdk-loop.js +34 -22
  91. package/dist/runs-over-store.d.ts +8 -1
  92. package/dist/runs-over-store.js +57 -9
  93. package/dist/sdk/runs.d.ts +22 -2
  94. package/dist/sdk/runs.js +103 -34
  95. package/dist/server.d.ts +3 -0
  96. package/dist/server.js +149 -26
  97. package/dist/skills/agentrun-serve/SKILL.md +52 -7
  98. package/dist/start-switches.js +2 -0
  99. package/dist/stats.d.ts +4 -0
  100. package/dist/stats.js +1 -1
  101. package/dist/store/agent-body.d.ts +12 -0
  102. package/dist/store/agent-body.js +16 -2
  103. package/dist/store/dialect.d.ts +2 -0
  104. package/dist/store/dialect.js +1 -0
  105. package/dist/store/index.d.ts +3 -3
  106. package/dist/store/index.js +2 -2
  107. package/dist/store/postgres/connection.d.ts +5 -2
  108. package/dist/store/postgres/connection.js +19 -7
  109. package/dist/store/postgres/dialect.js +1 -0
  110. package/dist/store/postgres/migrate.d.ts +30 -0
  111. package/dist/store/postgres/migrate.js +72 -5
  112. package/dist/store/postgres/migration-lint.d.ts +8 -0
  113. package/dist/store/postgres/migration-lint.js +119 -0
  114. package/dist/store/postgres/migrations/0024_runner_capacity.sql +28 -0
  115. package/dist/store/postgres/store.d.ts +8 -3
  116. package/dist/store/postgres/store.js +31 -11
  117. package/dist/store/schema.d.ts +18 -1
  118. package/dist/store/schema.js +33 -2
  119. package/dist/store/sqlite/run-launch.d.ts +9 -1
  120. package/dist/store/sqlite/run-launch.js +44 -0
  121. package/dist/store/sqlite/store.d.ts +17 -0
  122. package/dist/store/sqlite/store.js +87 -10
  123. package/dist/store/store-schema.d.ts +34 -0
  124. package/dist/store/store-schema.js +125 -0
  125. package/dist/store/store.d.ts +53 -1
  126. package/dist/store/store.js +13 -0
  127. package/dist/store-reads/analytics.d.ts +3 -2
  128. package/dist/store-reads/analytics.js +20 -8
  129. package/dist/store-view.d.ts +4 -3
  130. package/dist/store-view.js +11 -7
  131. package/dist/templates.d.ts +3 -2
  132. package/dist/tool-gate.d.ts +4 -2
  133. package/dist/unified-usage.d.ts +24 -0
  134. package/dist/unified-usage.js +400 -0
  135. package/dist/uploads-archil.d.ts +100 -0
  136. package/dist/uploads-archil.js +430 -0
  137. package/dist/uploads.d.ts +55 -1
  138. package/dist/uploads.js +185 -98
  139. package/dist/validate.js +1 -1
  140. package/dist/workspace/archil-disk.d.ts +6 -5
  141. package/dist/workspace/archil-disk.js +27 -12
  142. package/dist/workspace/archil.js +3 -2
  143. package/package.json +10 -10
  144. package/skills/agentrun-serve/SKILL.md +52 -7
  145. package/dist/console/assets/index-D0mr9wfZ.css +0 -1
  146. package/dist/console/assets/index-D7G6o-1E.js +0 -720
@@ -947,6 +947,17 @@ export interface Runs {
947
947
  /** The run's launch row (the runner reads it at its claim; the server for views). StoreLaunchMissing when the run has
948
948
  * none (it was not started with one), never a default. */
949
949
  launch(runId: string): Promise<RunLaunchRow>;
950
+ /** Only the immutable CLI accounting identity/turn and (when requested) SDK completion usage. Projects these JSON
951
+ * members in the database: no launch input, policy, sealed bytes, completion answer or replay events are returned.
952
+ * Undefined for an absent run; absent members stay undefined. Historical identity may be read without completion.
953
+ * An ordinary-store analytics read, not for Store.snapshot: PostgreSQL's specific NUL/surrogate extraction refusal falls back
954
+ * to JS parsing that row's text at the prior full-row cost (never sealed bytes); other database failures propagate. */
955
+ usageMetadata(runId: string, options: {
956
+ completion: boolean;
957
+ }): Promise<{
958
+ cli: Readonly<Record<string, unknown>>;
959
+ completionUsage?: unknown;
960
+ } | undefined>;
950
961
  /** The learning start (the launch body's `learning` member, as stored: undefined when the body names none) of every run of
951
962
  * agent `agentId` (no built-in learning run's) that has a launch row, in one read: what an agent-wide read (its cost per
952
963
  * run, its learning view) needs of its runs at once, never a read a run. Only that member is read: no run's input, no
@@ -1585,6 +1596,23 @@ export interface RunnerRow {
1585
1596
  readonly host: string | null;
1586
1597
  readonly port: number | null;
1587
1598
  readonly seenAt: number;
1599
+ /** The runner's build (the serve package's version), and the most runs it holds at once (its loops): each null on a
1600
+ * row a runner of an earlier build wrote, or one that said neither (a refusing runner says no slots). */
1601
+ readonly version: string | null;
1602
+ readonly slots: number | null;
1603
+ }
1604
+ /** The store's runs and hand-backs as the capacity read counts them (GET /v2/capacity, capacity.ts), on the deadlines'
1605
+ * clock (fence.ts storeNow), read once for the whole answer. Counts and runner names only. */
1606
+ export interface RunCapacity {
1607
+ /** The deadlines' clock when it was read. */
1608
+ readonly at: number;
1609
+ /** Runs a runner may claim now, by the claim's own rule (queued; handed back or its claim run out, past any backoff; a
1610
+ * parked run something wakes). */
1611
+ readonly queued: number;
1612
+ /** Runs a claim holds now (its lease not run out), by the runner that holds them. */
1613
+ readonly held: ReadonlyMap<string, number>;
1614
+ /** The hand-backs (hand_backs) within the window asked for. */
1615
+ readonly handBacks: number;
1588
1616
  }
1589
1617
  /** How long a runner's row is kept once it stopped saying it is there (a runner that died without its close): its next
1590
1618
  * put removes every row older than this. */
@@ -1596,9 +1624,11 @@ export interface Runners {
1596
1624
  * `unlessLiveMs`, not over another incarnation's row of the same name seen within that: a process's put again of a row
1597
1625
  * that went (pruned, a store restored) never takes back a later process's live one. False when such a row is there and
1598
1626
  * nothing was written. */
1599
- put(row: Omit<RunnerRow, "seenAt" | "host" | "port"> & {
1627
+ put(row: Omit<RunnerRow, "seenAt" | "host" | "port" | "version" | "slots"> & {
1600
1628
  readonly host?: string | null;
1601
1629
  readonly port?: number | null;
1630
+ readonly version?: string | null;
1631
+ readonly slots?: number | null;
1602
1632
  }, options?: {
1603
1633
  readonly unlessLiveMs?: number;
1604
1634
  }): Promise<boolean>;
@@ -1615,6 +1645,8 @@ export interface Runners {
1615
1645
  live(staleMs: number): Promise<RunnerRow[]>;
1616
1646
  /** The runner's row gone, while it is still this incarnation's. */
1617
1647
  remove(runner: string, incarnation: string): Promise<void>;
1648
+ /** The runs waiting for a runner and held by one, and the hand-backs within the last `windowMs` (RunCapacity). */
1649
+ capacity(windowMs: number): Promise<RunCapacity>;
1618
1650
  }
1619
1651
  /** A template package imported from GitHub (store schema `imported_templates`, POST /v2/templates/import): its id
1620
1652
  * (`github:<owner>/<repo>[/<path>]`), its source as given, the commit SHA read, its publisher (the GitHub account), and
@@ -1894,6 +1926,9 @@ export interface Store {
1894
1926
  * done; `left` says why it was kept, for the next start to finish. Postgres: `work` alone (a replaced row version is
1895
1927
  * the database's until its vacuum). */
1896
1928
  secureRewrite?(work: () => Promise<number>, options?: SecureRewriteOptions): Promise<SecureRewrite>;
1929
+ /** Reads the store's schema version and its runner floor now, without a lock or a write: a runner reads it at each
1930
+ * renewal (runner/main.ts), and stops once the floor is above the version its build knows. */
1931
+ schemaState?(): Promise<StoreSchemaState>;
1897
1932
  /** Claims the run for `runner` until now + `leaseMs`: the run's epoch + 1 when it is claimable (runs.claimable); null when
1898
1933
  * another claim holds it, or it is final, or parked with nothing to wake it. A queued or parked run becomes running. */
1899
1934
  claim(runId: string, runner: string, options?: {
@@ -2038,6 +2073,23 @@ export declare class SchemaVersionUnknown extends Error {
2038
2073
  readonly known: number;
2039
2074
  constructor(file: string, found: number | null, known: number);
2040
2075
  }
2076
+ /** A store a later build migrated past what this runner can run beside: its schema is ahead of the version this build
2077
+ * knows, and its runner floor (a breaking migration's version) is above it. Run a runner of the server's release. A
2078
+ * SchemaVersionUnknown, so every caller that refuses an unknown schema refuses this one the same way (a runner's exit 3). */
2079
+ export declare class RunnerBelowFloor extends SchemaVersionUnknown {
2080
+ readonly floor: number;
2081
+ constructor(place: string, found: number, known: number, floor: number);
2082
+ }
2083
+ /** A store's schema version and its runner floor (Store.schemaState). */
2084
+ export interface StoreSchemaState {
2085
+ /** The store's schema version; null when none is set up. */
2086
+ readonly version: number | null;
2087
+ /** The oldest schema version a runner's build must know to run on this store: a runner whose build knows less stops
2088
+ * claiming, hands its runs back and exits. On SQLite the version itself (a SQLite runner opens only its own version). */
2089
+ readonly runnerFloor: number | null;
2090
+ /** The version this build knows (its newest migration on Postgres, SCHEMA_VERSION on SQLite). */
2091
+ readonly known: number;
2092
+ }
2041
2093
  /** A command for a run that is final, or that does not exist. */
2042
2094
  export declare class RunFinal extends Error {
2043
2095
  readonly runId: string;
@@ -224,6 +224,19 @@ export class SchemaVersionUnknown extends Error {
224
224
  this.known = known;
225
225
  }
226
226
  }
227
+ /** A store a later build migrated past what this runner can run beside: its schema is ahead of the version this build
228
+ * knows, and its runner floor (a breaking migration's version) is above it. Run a runner of the server's release. A
229
+ * SchemaVersionUnknown, so every caller that refuses an unknown schema refuses this one the same way (a runner's exit 3). */
230
+ export class RunnerBelowFloor extends SchemaVersionUnknown {
231
+ floor;
232
+ constructor(place, found, known, floor) {
233
+ super(place, found, known);
234
+ const store = place.startsWith("the ") ? place : `the store at ${place}`;
235
+ this.message = `${store} is schema version ${found}, and a runner on it must know version ${floor} or later (its runner floor: a later release changed the schema in a way an earlier runner cannot run beside); this runner knows ${known}. Run a runner of the server's release.`;
236
+ this.name = "RunnerBelowFloor";
237
+ this.floor = floor;
238
+ }
239
+ }
227
240
  /** A command for a run that is final, or that does not exist. */
228
241
  export class RunFinal extends Error {
229
242
  runId;
@@ -8,14 +8,15 @@ export type AnalyticsFilter = {
8
8
  agent?: string;
9
9
  pending?: Readonly<Record<string, PendingEnd>>;
10
10
  };
11
- /** Runs counted together: how many, how many completed (escalated included) and failed, how many have metrics, their
12
- * tokens, and their cost as stats sums it (null when none ended with one; unknownCostRuns the rest). */
11
+ /** Runs counted together: how many completed (escalated included) and failed, how many have metrics and how many
12
+ * report every token counter, their known token total, and their cost (null when none ended with one). */
13
13
  export type AnalyticsGroup = {
14
14
  runs: number;
15
15
  completed: number;
16
16
  needsReview: number;
17
17
  failed: number;
18
18
  runsWithMetrics: number;
19
+ runsWithTokenMetrics: number;
19
20
  tokens: number | null;
20
21
  costUsd: number | null;
21
22
  unknownCostRuns: number;
@@ -4,13 +4,21 @@
4
4
  // given. A pending run (its end on the caller's way, not yet in the store) is counted at the state its end will have, in
5
5
  // the totals alone; its metrics, cost and effort wait for its end. No journal is read: the figures are the row's.
6
6
  import { sdkOf } from "../sdk/runs.js";
7
+ import { cliLearningOf } from "../store/agent-body.js";
8
+ import { NATIVE_RUNTIME_FACTS } from "../native/runtimes.js";
7
9
  import { DEFAULT_HARNESS } from "../harness.js";
8
10
  import { costOfRun, runReadState } from "./runs.js";
9
11
  import { isShadowRun, shadowRunId } from "../learn/audit.js";
10
12
  const PENDING_ENDS = new Set(["completed", "escalated", "failed", "stopped"]);
11
13
  const num = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
12
14
  const metricsOf = (run) => (run.metrics !== null && typeof run.metrics === "object" && !Array.isArray(run.metrics) ? run.metrics : null);
13
- const tokensOf = (m) => num(m.input) + num(m.cacheRead) + num(m.cacheWrite) + num(m.output);
15
+ const TOKEN_KEYS = ["input", "cacheRead", "cacheWrite", "output"];
16
+ /** A metrics object may hold only tools or null native counters. Count coverage only when its whole total is known. */
17
+ const tokensOf = (m) => {
18
+ if (!TOKEN_KEYS.every((key) => typeof m[key] === "number" && Number.isSafeInteger(m[key]) && m[key] >= 0))
19
+ return null;
20
+ return num(m.input) + num(m.cacheRead) + num(m.cacheWrite) + num(m.output);
21
+ };
14
22
  /** The runs and their metrics summed (the old AgentStoreReader.analytics), from A's run rows. */
15
23
  export async function analytics(store, filter = {}) {
16
24
  const pending = {};
@@ -41,7 +49,10 @@ export async function analytics(store, filter = {}) {
41
49
  continue;
42
50
  if (!harnessOf.has(run.revisionId)) {
43
51
  const revision = await store.agents.revision(run.revisionId);
44
- harnessOf.set(run.revisionId, revision?.kind === "workflow" ? "workflow" : (sdkOf(revision?.body)?.runtime ?? revision?.body?.harness ?? DEFAULT_HARNESS));
52
+ const cli = cliLearningOf(revision?.body);
53
+ harnessOf.set(run.revisionId, revision?.kind === "workflow" ? "workflow" : cli
54
+ ? NATIVE_RUNTIME_FACTS[cli.runtime].captureRuntime
55
+ : sdkOf(revision?.body)?.runtime ?? revision?.body?.harness ?? DEFAULT_HARNESS);
45
56
  }
46
57
  const read = runReadState(run, harnessOf.get(run.revisionId) === "workflow" ? "workflow" : "agentic");
47
58
  const running = read === "running";
@@ -67,8 +78,7 @@ export async function analytics(store, filter = {}) {
67
78
  });
68
79
  }
69
80
  const counted = (group) => {
70
- let unknownTokens = false;
71
- let runsN = 0, completed = 0, needsReview = 0, failed = 0, metered = 0, tokens = 0, costUsd = 0, known = 0, pend = 0;
81
+ let runsN = 0, completed = 0, needsReview = 0, failed = 0, metered = 0, tokenMetered = 0, tokens = 0, costUsd = 0, known = 0, pend = 0;
72
82
  let shadowUsd = 0;
73
83
  for (const f of group) {
74
84
  shadowUsd += f.shadowUsd;
@@ -81,16 +91,18 @@ export async function analytics(store, filter = {}) {
81
91
  failed += 1;
82
92
  if (f.metered) {
83
93
  metered += 1;
84
- tokens += tokensOf(f.metrics);
85
- if (['input', 'cacheRead', 'cacheWrite', 'output'].some(key => f.metrics?.[key] === null))
86
- unknownTokens = true;
94
+ const reported = tokensOf(f.metrics);
95
+ if (reported !== null) {
96
+ tokenMetered += 1;
97
+ tokens += reported;
98
+ }
87
99
  }
88
100
  if (!f.running && !f.pending && f.costUsd !== null)
89
101
  (costUsd += f.costUsd, (known += 1));
90
102
  if (f.pending)
91
103
  pend += 1;
92
104
  }
93
- return { runs: runsN, completed, needsReview, failed, runsWithMetrics: metered, tokens: unknownTokens ? null : tokens, costUsd: known > 0 || shadowUsd > 0 ? costUsd + shadowUsd : null, unknownCostRuns: runsN - known - pend };
105
+ return { runs: runsN, completed, needsReview, failed, runsWithMetrics: metered, runsWithTokenMetrics: tokenMetered, tokens: tokenMetered > 0 || runsN === 0 ? tokens : null, costUsd: known > 0 || shadowUsd > 0 ? costUsd + shadowUsd : null, unknownCostRuns: runsN - known - pend };
94
106
  };
95
107
  // Totals over the whole scope: the group counts, the token sums, and the time figures.
96
108
  let input = 0, cacheRead = 0, cacheWrite = 0, output = 0, requests = 0, unmetered = 0;
@@ -54,6 +54,7 @@ export declare const answerOf: (timeline: readonly TimelineEvent[]) => string |
54
54
  interface StoreRun {
55
55
  readonly native?: Record<string, unknown>;
56
56
  readonly sdk?: Record<string, unknown>;
57
+ readonly cli_learning?: Record<string, unknown>;
57
58
  readonly row: NonNullable<Awaited<ReturnType<typeof readRun>>>;
58
59
  readonly runRow: RunRow;
59
60
  readonly launch: RunLaunch | undefined;
@@ -147,9 +148,9 @@ export declare function standsOnPerson(approvals: ReadonlyMap<string, {
147
148
  * True when written; false when another write got there first, or when the write failed (said, never thrown: a read
148
149
  * never fails on its heal, and the next read or start sweep writes it again). */
149
150
  export declare function healStoredCost(store: Store, runId: string, heal: NonNullable<StoreRun["heal"]>): Promise<boolean>;
150
- /** The end run `runId` keeps while it looks back after its job ended, as every reader serves it (lookingBackOf), else
151
- * undefined: a run that reads ended while it is still learning from that end. */
152
- export declare function lookingBackEnd(store: Store, runId: string): Promise<KeptEnd | undefined>;
151
+ /** The end run `stored` (its row as the caller read it) keeps while it looks back after its job ended, as every reader
152
+ * serves it (lookingBackOf), else undefined: a run that reads ended while it is still learning from that end. */
153
+ export declare function lookingBackEnd(store: Store, stored: RunRow): Promise<KeptEnd | undefined>;
153
154
  /** What run `runId`'s paid tool calls cost, the run read's `cost.tools_usd` (server.ts allInViewOf): the charges its
154
155
  * paid-tools session booked (a monid endpoint's, a priced MCP tool's, Parallel's behind the web tools, the browser's
155
156
  * time) and the decisions Parallel charged on its own API (store-reads/decisions.ts chargedByParallel), joined and rounded by the one rule its `usd` is (costWordsOf), less those in its end-of-run turn, which are
@@ -1,3 +1,4 @@
1
+ import { cliPolicyOf } from "./sdk/runs.js";
1
2
  // A run's served view, built from the one store (design 2.1): the same object GET /runs/{id} answered from the per-run
2
3
  // files, now composed from the store's row (store-reads/runs.ts), its journal (store-reads/journal.ts) and its learning end
3
4
  // (store-reads/learning.ts), fed through the run view's pure functions (run-view.ts) so the figures and words a person or a
@@ -221,13 +222,12 @@ async function lookingBackOf(stored, entries, commands) {
221
222
  const kept = keptEndOf(entries, rows);
222
223
  return kept !== undefined && !rows.some(waitingMessage) ? kept : undefined;
223
224
  }
224
- /** The end run `runId` keeps while it looks back after its job ended, as every reader serves it (lookingBackOf), else
225
- * undefined: a run that reads ended while it is still learning from that end. */
226
- export async function lookingBackEnd(store, runId) {
227
- const stored = await store.runs.get(runId);
228
- if (stored?.state !== "running")
225
+ /** The end run `stored` (its row as the caller read it) keeps while it looks back after its job ended, as every reader
226
+ * serves it (lookingBackOf), else undefined: a run that reads ended while it is still learning from that end. */
227
+ export async function lookingBackEnd(store, stored) {
228
+ if (stored.state !== "running")
229
229
  return undefined;
230
- return lookingBackOf(stored, (await journalRecords(store.readJournal(runId), BACKGROUND_CONTEXT)).entries, () => store.commands(runId, 0));
230
+ return lookingBackOf(stored, (await journalRecords(store.readJournal(stored.id), BACKGROUND_CONTEXT)).entries, () => store.commands(stored.id, 0));
231
231
  }
232
232
  /** `storeRunOf`'s reads, over `store` (a snapshot) and the run's cut, begun at `since` (servedSince). */
233
233
  async function storeRunIn(store, runId, deps, cut, since) {
@@ -283,6 +283,8 @@ async function storeRunIn(store, runId, deps, cut, since) {
283
283
  const sdkCompletion = records.entries.findLast(entry => entry.kind === "agentrun.sdk.completion")?.data;
284
284
  const sdkSession = records.entries.findLast(entry => entry.kind === "agentrun.sdk.session")?.data;
285
285
  const sdk = launch?.sdk ? { runtime: launch.sdk.runtime, ...(launch.sdk.runtime === "external" ? { platform: launch.sdk.platform } : {}), model: sdkCompletion?.model ?? launch.sdk.model ?? null, session_id: sdkCompletion?.session_id ?? sdkSession?.session_id ?? null, learning_revision: launch.learning?.seed ?? null, usage: sdkCompletion?.usage ?? null } : undefined;
286
+ const cliCompletion = records.entries.findLast(entry => entry.kind === "agentrun.cli.completion")?.data;
287
+ const cli_learning = launch?.cli_learning ? { runtime: launch.cli_learning.runtime, session_id: launch.cli_learning.session_id, turn_id: launch.cli_learning.turn_id, model: cliCompletion?.model ?? launch.cli_learning.model ?? null, learning_revision: launch.learning?.seed ?? null, seed_observed: cliCompletion?.seed_observed ?? false, policy_complete: cliPolicyOf(launch, cliCompletion)?.complete ?? false, evidence_complete: cliCompletion?.evidence_complete ?? false, usage: cliCompletion?.usage ?? null } : undefined;
286
288
  const nativeConfiguration = records.entries.findLast(e => e.kind === "agentrun.native.configuration")?.data;
287
289
  const nativeEvents = records.entries.filter(e => e.kind === "agentrun.native.usage").map(e => e.data);
288
290
  const reflectRange = (await runReflect(store, runId, context));
@@ -416,7 +418,7 @@ async function storeRunIn(store, runId, deps, cut, since) {
416
418
  const endSaidCost = endCost && !lookingBack ? endSaid !== undefined : false;
417
419
  const endUnread = !lookingBack && bookedCost !== undefined && !endCost && endSaid === null;
418
420
  const heal = lookingBack ? undefined : healOf(endCost, runRow);
419
- return { ...(sdk ? { sdk } : {}), ...(native ? { native } : {}), row, runRow, launch, rows, version, harness, learningEnd: shownEnd, shownEnd, view, unshown, values, live, updatedAtMs, lastProgressAt, budget, paused, browserAsk, computerAsk, approvalsMode, output, ...(review ? { review } : {}), structured, decisions, plan, model, ...(endCost ? { endCost, endSaidCost } : {}), ...(endUnread ? { endUnread } : {}), ...(heal ? { heal } : {}), ...(lookingBack ? { lookingBack: true } : {}) };
421
+ return { ...(cli_learning ? { cli_learning } : {}), ...(sdk ? { sdk } : {}), ...(native ? { native } : {}), row, runRow, launch, rows, version, harness, learningEnd: shownEnd, shownEnd, view, unshown, values, live, updatedAtMs, lastProgressAt, budget, paused, browserAsk, computerAsk, approvalsMode, output, ...(review ? { review } : {}), structured, decisions, plan, model, ...(endCost ? { endCost, endSaidCost } : {}), ...(endUnread ? { endUnread } : {}), ...(heal ? { heal } : {}), ...(lookingBack ? { lookingBack: true } : {}) };
420
422
  }
421
423
  /** Run read `read` (a run still going, as the store holds it) as the end `ended` keeps (its state, summary and end time:
422
424
  * the end a run looking back keeps), its cost as read. */
@@ -638,6 +640,7 @@ async function viewOfRun(store, runId, deps, run, read) {
638
640
  // A run looking back reads its job's end, and is settled only once its look-back's end is in.
639
641
  const settled = terminal && ended && !run.lookingBack;
640
642
  return {
643
+ ...(run.cli_learning && !unshown ? { cli_learning: redactedOutput(run.cli_learning, values) } : {}),
641
644
  ...(run.sdk && !unshown ? { sdk: redactedOutput(run.sdk, values) } : {}),
642
645
  ...(run.native && !unshown ? { native: redactedOutput(run.native, values) } : {}),
643
646
  run_id: row.id,
@@ -705,6 +708,7 @@ export function summaryOfView(view) {
705
708
  const { run_id, agent_id, agent_version, console_url, revision, kind, sha, status, state, title, effort, created_at, ended_at, error, review, pending, cost, archived_at, imported_from, trigger, labels, learning } = view;
706
709
  return {
707
710
  run_id, agent_id, agent_version, console_url, revision, kind, sha, status, state, title,
711
+ ...(view.cli_learning ? { cli_learning: view.cli_learning } : {}),
708
712
  ...(view.sdk ? { sdk: view.sdk } : {}),
709
713
  ...(view.native ? { native: view.native } : {}),
710
714
  ...(effort ? { effort } : {}),
@@ -42,8 +42,9 @@ export declare const MAX_PACKAGE_FILES = 256;
42
42
  * from the agent's files' limits above. The answer's schema is given to the agent, so under evals/ too it keeps those. */
43
43
  export declare const MAX_EVAL_FILE_BYTES: number;
44
44
  /** An MCP server a package uses, by where it comes from (`source`, `toolkit`), never by the name an owner saves it under:
45
- * inside the package its tools are `mcp:<toolkit>/<tool>`. `tools`, those the agent may be granted, and `allow`, those of
46
- * them the package asks the owner to grant (a consent screen shows it, with `reason`). Neither the package nor an apply
45
+ * inside the package its tools are `mcp:<toolkit>/<tool>`. `tools`, those the agent uses and the package asks the owner to
46
+ * grant (a consent screen shows each, with `reason`), and `allow`, those of them expected to run without asking (reads);
47
+ * the rest (writes) wait for the owner's OK on each call (packageView's `asks`). Neither the package nor an apply
47
48
  * sets a permission: an apply's `grants`, the owner's ticks, grant the tools, and each call follows this server's tool settings
48
49
  * (a write asks unless the owner allowed it there, a read follows the server's Allow reads, approvals never lifts an ask). `pin`, by tool: the arguments each of its calls must carry (a value, or
49
50
  * `${parameters.<name>}`), which apply writes as the tool's pin in the agent's tool settings, so a send goes only to the
@@ -143,9 +143,11 @@ export type PendingApproval = {
143
143
  /** The call's arguments as text, at most ARGS_PREVIEW_BYTES. */
144
144
  args_preview: string;
145
145
  /** What the call does in words, where its tool says (email_send: "Send email to <to>, cc <cc>: <subject>" and the
146
- * text's first 300 characters), then why it asks when its setting allows it (ToolEntry.asks), redacted as its
147
- * arguments are. */
146
+ * text's first 300 characters), redacted as its arguments are. */
148
147
  summary?: string;
148
+ /** Why the call asks though its setting allows it (its tool's own ask, ToolEntry.asks), redacted as its arguments are;
149
+ * absent for a call that asks by its setting. Never among its arguments. */
150
+ asks_because?: string;
149
151
  requested_at: string;
150
152
  price?: ApprovalPrice;
151
153
  /** Present, true, when the run's pin marks the tool destructive (toolEffect): an allow of it needs confirm_destructive. */
@@ -0,0 +1,24 @@
1
+ import type { Store } from './store/store.js';
2
+ import type { AnalyticsFilter } from './store-reads/analytics.js';
3
+ declare const fields: readonly ["input", "output", "cache_read", "cache_write"];
4
+ type Counts = Record<typeof fields[number], number | null> & {
5
+ tokens: number | null;
6
+ };
7
+ export type UnifiedUsage = Counts & {
8
+ known_runs: number;
9
+ unknown_runs: number;
10
+ observed_sessions: number;
11
+ unknown_sessions: number;
12
+ partial_days: number;
13
+ retention_limited: boolean;
14
+ by_day: {
15
+ day: string;
16
+ tokens: number | null;
17
+ }[];
18
+ by_harness: {
19
+ harness: string;
20
+ tokens: number | null;
21
+ }[];
22
+ };
23
+ export declare function unifiedUsage(store: Store, filter?: AnalyticsFilter, now?: number): Promise<UnifiedUsage>;
24
+ export {};