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

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 (135) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +94 -13
  3. package/dist/adapters.js +1 -1
  4. package/dist/agent-delete.d.ts +13 -2
  5. package/dist/agent-delete.js +14 -5
  6. package/dist/agents-store.d.ts +5 -1
  7. package/dist/agents-store.js +10 -4
  8. package/dist/api-only.d.ts +4 -2
  9. package/dist/api-only.js +5 -3
  10. package/dist/archil-routes.d.ts +1 -0
  11. package/dist/archil-routes.js +28 -2
  12. package/dist/computer-setting.d.ts +8 -0
  13. package/dist/computer-setting.js +11 -0
  14. package/dist/computers.d.ts +6 -2
  15. package/dist/computers.js +2 -2
  16. package/dist/console/assets/index-B-D0EM3j.js +733 -0
  17. package/dist/console/assets/index-DlQepP2J.css +1 -0
  18. package/dist/console/index.html +2 -2
  19. package/dist/fal-price.d.ts +76 -0
  20. package/dist/fal-price.js +368 -0
  21. package/dist/learn/commit.d.ts +5 -0
  22. package/dist/learn/commit.js +4 -1
  23. package/dist/learn/compile.d.ts +13 -5
  24. package/dist/learn/compile.js +24 -7
  25. package/dist/learn/delete.d.ts +12 -6
  26. package/dist/learn/delete.js +17 -11
  27. package/dist/learn/folder.d.ts +11 -0
  28. package/dist/learn/folder.js +20 -3
  29. package/dist/learn/learning-of.d.ts +8 -0
  30. package/dist/learn/learning-of.js +8 -0
  31. package/dist/learn/learning.sh +14 -1
  32. package/dist/learn/promote.d.ts +51 -7
  33. package/dist/learn/promote.js +51 -8
  34. package/dist/learning-reset.d.ts +18 -0
  35. package/dist/learning-reset.js +115 -0
  36. package/dist/learning-routes.d.ts +1 -1
  37. package/dist/learning-words.d.ts +5 -0
  38. package/dist/learning-words.js +5 -0
  39. package/dist/learning.d.ts +8 -6
  40. package/dist/learning.js +5 -4
  41. package/dist/llms.txt +1 -0
  42. package/dist/mcp-gateway.d.ts +39 -2
  43. package/dist/mcp-gateway.js +41 -10
  44. package/dist/mcp-pin-first.js +1 -1
  45. package/dist/mcp-servers-store.d.ts +1 -0
  46. package/dist/mcp-servers-store.js +2 -1
  47. package/dist/media-duration.d.ts +2 -0
  48. package/dist/media-duration.js +136 -0
  49. package/dist/openapi.js +45 -12
  50. package/dist/provider.d.ts +8 -0
  51. package/dist/provider.js +14 -2
  52. package/dist/refusal-words.d.ts +0 -3
  53. package/dist/refusal-words.js +0 -3
  54. package/dist/request-metadata.d.ts +20 -0
  55. package/dist/request-metadata.js +24 -0
  56. package/dist/run-launch.d.ts +13 -4
  57. package/dist/run-launch.js +14 -3
  58. package/dist/run-learning-served.d.ts +2 -2
  59. package/dist/run-usage.d.ts +48 -4
  60. package/dist/run-usage.js +156 -39
  61. package/dist/run-view.d.ts +1 -1
  62. package/dist/run-view.js +3 -7
  63. package/dist/runner/adapter-loop.d.ts +3 -1
  64. package/dist/runner/adapter-loop.js +12 -9
  65. package/dist/runner/adapter-run-spec.d.ts +1 -1
  66. package/dist/runner/adapter-run-spec.js +13 -13
  67. package/dist/runner/audit.d.ts +8 -3
  68. package/dist/runner/audit.js +21 -6
  69. package/dist/runner/claim-secrets.d.ts +2 -1
  70. package/dist/runner/claim-secrets.js +5 -3
  71. package/dist/runner/cli-run-spec.d.ts +1 -1
  72. package/dist/runner/cli-run-spec.js +12 -11
  73. package/dist/runner/conformance/harness.js +4 -2
  74. package/dist/runner/dispatch.js +4 -2
  75. package/dist/runner/harness-loops.js +5 -2
  76. package/dist/runner/journal-kinds.d.ts +3 -0
  77. package/dist/runner/learning-end.d.ts +2 -1
  78. package/dist/runner/learning-end.js +47 -22
  79. package/dist/runner/mcp-claim.js +1 -1
  80. package/dist/runner/mcp-run.d.ts +9 -0
  81. package/dist/runner/mcp-run.js +16 -1
  82. package/dist/runner/model-route.js +7 -4
  83. package/dist/runner/promotion.d.ts +40 -21
  84. package/dist/runner/promotion.js +130 -59
  85. package/dist/runner/run-child-host.js +6 -4
  86. package/dist/runner/runner.js +15 -3
  87. package/dist/runner/sdk-loop.d.ts +2 -2
  88. package/dist/runner/sdk-loop.js +3 -3
  89. package/dist/runner/store-port.d.ts +3 -0
  90. package/dist/runner/workflow-spec.js +3 -3
  91. package/dist/runs-over-store.d.ts +5 -2
  92. package/dist/runs-over-store.js +14 -7
  93. package/dist/sdk/check.d.ts +6 -5
  94. package/dist/sdk/check.js +17 -16
  95. package/dist/sdk/runs.js +81 -65
  96. package/dist/server.js +84 -20
  97. package/dist/store/agent-body.d.ts +4 -0
  98. package/dist/store/agent-body.js +4 -1
  99. package/dist/store/index.d.ts +1 -1
  100. package/dist/store/journal/storage.d.ts +2 -0
  101. package/dist/store/journal/storage.js +2 -1
  102. package/dist/store/postgres/migrations/0025_run_attempts.sql +27 -0
  103. package/dist/store/postgres/migrations/0026_run_disk.sql +23 -0
  104. package/dist/store/schema.d.ts +14 -1
  105. package/dist/store/schema.js +42 -4
  106. package/dist/store/sqlite/run-launch.d.ts +22 -0
  107. package/dist/store/sqlite/run-launch.js +72 -7
  108. package/dist/store/sqlite/run-patch.d.ts +2 -1
  109. package/dist/store/sqlite/run-patch.js +4 -3
  110. package/dist/store/sqlite/store.js +141 -29
  111. package/dist/store/store-schema.js +2 -2
  112. package/dist/store/store.d.ts +67 -4
  113. package/dist/store-reads/learning.d.ts +12 -8
  114. package/dist/store-reads/learning.js +33 -24
  115. package/dist/store-reads/loop.d.ts +4 -3
  116. package/dist/store-reads/loop.js +7 -5
  117. package/dist/template-eval.js +4 -4
  118. package/dist/templates.d.ts +3 -0
  119. package/dist/templates.js +7 -5
  120. package/dist/timeline/labels.js +1 -1
  121. package/dist/tools.js +1 -1
  122. package/dist/uploads-archil.js +2 -1
  123. package/dist/uploads.d.ts +0 -3
  124. package/dist/uploads.js +2 -11
  125. package/dist/validate.js +5 -1
  126. package/dist/web-save.d.ts +3 -0
  127. package/dist/web-save.js +17 -2
  128. package/dist/workspace/archil-exec.js +1 -1
  129. package/dist/workspace/archil.js +1 -1
  130. package/dist/workspace/revision-skills.js +24 -11
  131. package/dist/workspace/synced-folder.d.ts +3 -3
  132. package/dist/workspace/synced-folder.js +7 -6
  133. package/package.json +10 -10
  134. package/dist/console/assets/index-Bt1B51b7.js +0 -733
  135. package/dist/console/assets/index-sVftE9Ht.css +0 -1
@@ -32,6 +32,11 @@ export declare function gatewayBase(setting: string, value: string): string;
32
32
  /** The generic gateway must speak HTTP Responses (or Messages). Particle exposes Chat Completions
33
33
  * and a WebSocket-only Responses subset, so a direct API base is not a compatible gateway. */
34
34
  export declare function modelGatewayBase(value: string): string;
35
+ /** Whether `base` (a gatewayBase answer) is on the origin of serve's own gateway (AGENT_MODEL_BASE_URL): there a run's own
36
+ * model key (run-model-key.ts) is sent, so the endpoint needs no standing key of the server's. */
37
+ /** The headers a request to `origin` carries for serve's gateway (gatewayHeadersOf), when `origin` is that gateway's own
38
+ * (AGENT_MODEL_BASE_URL's origin): none to any other origin, a vendor's own included. */
39
+ export declare function gatewayHeadersAt(env: NodeJS.ProcessEnv, origin: string): Readonly<Record<string, string>> | undefined;
35
40
  /**
36
41
  * Whether `wire` reaches its vendor's models here only through OpenRouter: OPENROUTER_API_KEY is set and none of the
37
42
  * wire's own settings is. Those win, as on a host with no OpenRouter key: the endpoint the host names (ANTHROPIC_BASE_URL
@@ -59,6 +64,8 @@ export declare function anthropicModelId(model: ModelRef): string;
59
64
  * the plan's token, asked at each request, and `refused` tells the plan OpenAI answered 401 to one it sent. */
60
65
  /** `login`: claude-agent on Claude Code's login on this machine (claude-login.ts): billed to the person's plan, its CLI
61
66
  * calling Anthropic itself; `origin` and `credential` are never read (no proxy, no key). */
67
+ /** `headers`: what each request on serve's own gateway carries beside its key (request-metadata.ts: the run, turn and
68
+ * attempt it was made for, as LiteLLM's spend-logs metadata); none on any other endpoint. */
62
69
  export type Provider = {
63
70
  origin: string;
64
71
  credential: () => string | Promise<string>;
@@ -68,6 +75,7 @@ export type Provider = {
68
75
  billing?: "plan";
69
76
  refused?: (credential: string) => Promise<void>;
70
77
  login?: ClaudeAgentLogin;
78
+ headers?: Readonly<Record<string, string>>;
71
79
  };
72
80
  export declare function providerFor(wire: Wire, env: NodeJS.ProcessEnv, model: ModelRef, who: string): Provider;
73
81
  /** The catalog entry a run on `wire` of `model` is capped and priced by: providerFor's, or the model's own (normalized)
package/dist/provider.js CHANGED
@@ -2,6 +2,7 @@ import { CLAUDE_LOGIN_PROVIDER, onClaudeLogin, claudeLoginOf } from "./claude-lo
2
2
  import { chooseModel, FAUX_ENV, hostModel, MODEL_PROVIDER } from "./model.js";
3
3
  import { modelWire, WIRES, wireOf } from "./wires.js";
4
4
  import { modelFormWhy } from "./model-form.js";
5
+ import { gatewayHeadersOf } from "./request-metadata.js";
5
6
  import { EXPIRED, PLAN_ORIGIN, PLAN_PROVIDER, planOf, SIGNED_OUT } from "./chatgpt-plan.js";
6
7
  // Each wire's own settings live in a leaf module (wires.ts), which the harness table reads as it loads.
7
8
  export { modelWire, WIRES, wireOf };
@@ -71,6 +72,11 @@ export function modelGatewayBase(value) {
71
72
  }
72
73
  /** Whether `base` (a gatewayBase answer) is on the origin of serve's own gateway (AGENT_MODEL_BASE_URL): there a run's own
73
74
  * model key (run-model-key.ts) is sent, so the endpoint needs no standing key of the server's. */
75
+ /** The headers a request to `origin` carries for serve's gateway (gatewayHeadersOf), when `origin` is that gateway's own
76
+ * (AGENT_MODEL_BASE_URL's origin): none to any other origin, a vendor's own included. */
77
+ export function gatewayHeadersAt(env, origin) {
78
+ return onServeGateway(env, origin) ? gatewayHeadersOf(env) : undefined;
79
+ }
74
80
  function onServeGateway(env, base) {
75
81
  if (!env.AGENT_MODEL_BASE_URL)
76
82
  return false;
@@ -111,6 +117,11 @@ export const openRouterName = (id) => id.replace(/-(\d+)-(\d+)$/, "-$1.$2");
111
117
  export function anthropicModelId(model) {
112
118
  return model.provider === WIRES["anthropic-messages"].vendor ? model.modelId.replaceAll(".", "-") : model.modelId;
113
119
  }
120
+ /** `env`'s gateway headers (gatewayHeadersOf), as a Provider's optional field. */
121
+ const onGateway = (env) => {
122
+ const headers = gatewayHeadersOf(env);
123
+ return headers ? { headers } : {};
124
+ };
114
125
  /** OpenRouter's key, read per request, refused by name where it is not set. */
115
126
  function openRouterKey(env, who, named) {
116
127
  if (!env.OPENROUTER_API_KEY)
@@ -119,7 +130,7 @@ function openRouterKey(env, who, named) {
119
130
  }
120
131
  /** Serve's gateway (AGENT_MODEL_BASE_URL) on either wire: its base, its key (AGENT_MODEL_API_KEY, or none) as a bearer
121
132
  * token, the model id as model.ts chose it, priced as runModels prices it. */
122
- const gatewayProvider = (env, model) => ({ origin: modelGatewayBase(env.AGENT_MODEL_BASE_URL), credential: () => env.AGENT_MODEL_API_KEY || "none", auth: "bearer", modelId: model.modelId, priceModel: model });
133
+ const gatewayProvider = (env, model) => ({ origin: modelGatewayBase(env.AGENT_MODEL_BASE_URL), credential: () => env.AGENT_MODEL_API_KEY || "none", auth: "bearer", modelId: model.modelId, priceModel: model, ...onGateway(env) });
123
134
  export function providerFor(wire, env, model, who) {
124
135
  const named = `${model.provider}/${model.modelId}`;
125
136
  // A harness on its own plan (Cursor's) calls its vendor itself, on its owner's key: no route of ours is its.
@@ -150,7 +161,8 @@ export function providerFor(wire, env, model, who) {
150
161
  // the harness runs here with no standing key, and a run with no key to spend is refused at its start.
151
162
  if (!env.ANTHROPIC_API_KEY && !onServeGateway(env, origin))
152
163
  throw env.ANTHROPIC_BASE_URL ? new HarnessUnavailable(`${who} needs ANTHROPIC_API_KEY.`, undefined, ["ANTHROPIC_API_KEY"]) : new HarnessUnavailable(`${who} needs ANTHROPIC_API_KEY or OPENROUTER_API_KEY.`, undefined, ["ANTHROPIC_API_KEY", "OPENROUTER_API_KEY"]);
153
- return { origin, credential: () => env.ANTHROPIC_API_KEY ?? "", auth: "x-api-key", modelId: own, priceModel: { provider: "anthropic", modelId: own } };
164
+ // On serve's own gateway's origin, the claim's request metadata goes with each request (request-metadata.ts).
165
+ return { origin, credential: () => env.ANTHROPIC_API_KEY ?? "", auth: "x-api-key", modelId: own, priceModel: { provider: "anthropic", modelId: own }, ...(onServeGateway(env, origin) ? onGateway(env) : {}) };
154
166
  }
155
167
  // A ChatGPT plan's model: OpenAI's Responses API on the plan's token, whatever else is set.
156
168
  if (model.provider === PLAN_PROVIDER) {
@@ -9,9 +9,6 @@ export declare const STOPPED_WHILE_WAITING = "Not run: the run was stopped while
9
9
  export declare const ENDED_WHILE_WAITING = "Not run: the run ended while it waited";
10
10
  /** How a refusal of a setting's deny, or of an ask no one can approve, opens (deniedText, noApproverText). */
11
11
  export declare const DENIED_OPENING = "Permission denied: ";
12
- /** The field of an asked call's card that says why a call its setting allows asks a person anyway (a tool's own ask,
13
- * tools.ts ToolEntry.asks): written by the gate alone, so the model's own arguments never carry it onto the card. */
14
- export declare const ASKED_BECAUSE = "asked_because";
15
12
  /** How a person's deny opens (refusedText). */
16
13
  export declare const USER_DENIED_OPENING = "The user denied this ";
17
14
  /** What a read-only bash policy's refusal says after the tool's name (bash-read-only.ts readOnlyText). */
@@ -11,9 +11,6 @@ export const STOPPED_WHILE_WAITING = "Not run: the run was stopped while it wait
11
11
  export const ENDED_WHILE_WAITING = "Not run: the run ended while it waited";
12
12
  /** How a refusal of a setting's deny, or of an ask no one can approve, opens (deniedText, noApproverText). */
13
13
  export const DENIED_OPENING = "Permission denied: ";
14
- /** The field of an asked call's card that says why a call its setting allows asks a person anyway (a tool's own ask,
15
- * tools.ts ToolEntry.asks): written by the gate alone, so the model's own arguments never carry it onto the card. */
16
- export const ASKED_BECAUSE = "asked_because";
17
14
  /** How a person's deny opens (refusedText). */
18
15
  export const USER_DENIED_OPENING = "The user denied this ";
19
16
  /** What a read-only bash policy's refusal says after the tool's name (bash-read-only.ts readOnlyText). */
@@ -0,0 +1,20 @@
1
+ import type { Claim } from "./store/index.js";
2
+ /** The env name a claim's model routes read the metadata from: the JSON the header carries. */
3
+ export declare const REQUEST_METADATA_ENV = "AGENTRUN_REQUEST_METADATA";
4
+ /** LiteLLM's header for metadata it keeps on a call's spend-log row (LiteLLM v1.110.0 and later). */
5
+ export declare const SPEND_LOGS_METADATA_HEADER = "x-litellm-spend-logs-metadata";
6
+ /** What one claim's requests say of themselves: the run, the turn the claim works and its attempt at it (store
7
+ * run_attempts). Keys are prefixed, as a spend log row holds the metadata of every caller of the gateway. */
8
+ export type RequestMetadata = {
9
+ agentrun_run_id: string;
10
+ agentrun_turn?: number;
11
+ agentrun_attempt?: number;
12
+ };
13
+ /** The metadata of `claim`'s requests: its run, and its turn and attempt where its store keeps them. */
14
+ export declare function requestMetadataOf(claim: Pick<Claim, "runId" | "turn" | "attempt">): RequestMetadata;
15
+ /** `env` with `claim`'s request metadata on it, by name and never enumerable (as a model key is); `env` itself when there
16
+ * is no claim. The same object is changed and returned: a claim's model env is its own (claimModelEnv makes it). */
17
+ export declare function withRequestMetadata(env: NodeJS.ProcessEnv, claim: Pick<Claim, "runId" | "turn" | "attempt"> | undefined): NodeJS.ProcessEnv;
18
+ /** The headers a request to serve's gateway carries from `env`: the spend-logs metadata, when the claim set it and it is
19
+ * one line of JSON an HTTP header can carry (the proxy's own bound on a host's header, 1024 characters). None else. */
20
+ export declare function gatewayHeadersOf(env: NodeJS.ProcessEnv): Readonly<Record<string, string>> | undefined;
@@ -0,0 +1,24 @@
1
+ /** The env name a claim's model routes read the metadata from: the JSON the header carries. */
2
+ export const REQUEST_METADATA_ENV = "AGENTRUN_REQUEST_METADATA";
3
+ /** LiteLLM's header for metadata it keeps on a call's spend-log row (LiteLLM v1.110.0 and later). */
4
+ export const SPEND_LOGS_METADATA_HEADER = "x-litellm-spend-logs-metadata";
5
+ /** The metadata of `claim`'s requests: its run, and its turn and attempt where its store keeps them. */
6
+ export function requestMetadataOf(claim) {
7
+ return { agentrun_run_id: claim.runId, ...(claim.turn !== undefined ? { agentrun_turn: claim.turn } : {}), ...(claim.attempt !== undefined ? { agentrun_attempt: claim.attempt } : {}) };
8
+ }
9
+ /** `env` with `claim`'s request metadata on it, by name and never enumerable (as a model key is); `env` itself when there
10
+ * is no claim. The same object is changed and returned: a claim's model env is its own (claimModelEnv makes it). */
11
+ export function withRequestMetadata(env, claim) {
12
+ if (!claim)
13
+ return env;
14
+ Object.defineProperty(env, REQUEST_METADATA_ENV, { value: JSON.stringify(requestMetadataOf(claim)), enumerable: false, writable: true, configurable: true });
15
+ return env;
16
+ }
17
+ /** The headers a request to serve's gateway carries from `env`: the spend-logs metadata, when the claim set it and it is
18
+ * one line of JSON an HTTP header can carry (the proxy's own bound on a host's header, 1024 characters). None else. */
19
+ export function gatewayHeadersOf(env) {
20
+ const value = env[REQUEST_METADATA_ENV];
21
+ if (typeof value !== "string" || value.length === 0 || value.length > 1024 || /[^\x20-\x7e]/.test(value))
22
+ return undefined;
23
+ return { [SPEND_LOGS_METADATA_HEADER]: value };
24
+ }
@@ -71,7 +71,8 @@ export interface RunLaunch {
71
71
  * never answered by a learned workflow and asked for no workflow draft at its end (a native run, an SDK run with no
72
72
  * workflow binding); `model`, the model an SDK run's compile turn is asked on (its agent's own); `seedFailed`, why it
73
73
  * starts with nothing learned when it does (SeedFailed); `use`, a run started with `learning: "use"` (learning.ts
74
- * RUN_LEARNING_USE): it starts from all of the above and keeps nothing. Absent when the run does not learn. */
74
+ * RUN_LEARNING_USE): it starts from all of the above and keeps nothing; `seededAt`, when its seed was read (learning.ts
75
+ * Seed.at), which the agent's reset watermark is read against. Absent when the run does not learn. */
75
76
  readonly learning?: {
76
77
  readonly seed: string | null;
77
78
  readonly section: string;
@@ -80,6 +81,7 @@ export interface RunLaunch {
80
81
  readonly model?: string;
81
82
  readonly seedFailed?: SeedFailed;
82
83
  readonly use?: true;
84
+ readonly seededAt?: number;
83
85
  };
84
86
  /** `false`: the run never starts on a learned workflow, even one that fits its input (RunCreate `workflow_first`,
85
87
  * runner/workflow-first.ts); its agent takes it from its first turn. Absent: a fitting one serves first. */
@@ -126,6 +128,12 @@ export declare function keepInputOrigin(stored: unknown, trigger: RunTrigger | n
126
128
  /** The JSON body the server stores for a run's launch. `harness` is a top-level field, so the loop factory reads it from the
127
129
  * stored body without parsing the rest. */
128
130
  export declare function launchBody(launch: RunLaunch): Json;
131
+ /** The launch a runner runs (runner/dispatch.ts, run-child-host.ts): on a disk the launch names as its own (kind
132
+ * "launch"), its learning start holds no seed, # Learned section or learned workflow, whatever the body says (a start
133
+ * made before its run's disk was read, or written by hand), as the server's seed is the default disk's, never that
134
+ * disk's (learn/learning-of.ts): the run starts with nothing learned (`other_disk`), its flags kept. Any other launch as
135
+ * read. readLaunchBody keeps the body as written: a finished run's views show what it ran. */
136
+ export declare function launchOnItsDisk(launch: RunLaunch): RunLaunch;
129
137
  /** A launch body read back into a `RunLaunch`, with the input-origin fail-safe applied through the body's own trigger, or
130
138
  * undefined when the body is not one (not an object, or it names no place: a damaged or hand-edited body). */
131
139
  export declare function readLaunchBody(stored: unknown): RunLaunch | undefined;
@@ -135,9 +143,10 @@ export declare const learningStartOf: (launch: RunLaunch) => RunLaunch["learning
135
143
  /** The same start read from a launch body's `learning` member alone (Store.runs.learningStarts), as readLaunchBody reads it. */
136
144
  export declare const learningStartOfStored: (stored: unknown) => RunLaunch["learning"];
137
145
  /** Why a learning run starts with nothing learned, as main says it: its agent's learning folder could not be read
138
- * (`unreadable`, STOPPED.unreadable), or what was read could not be made into its start (`not_copied`,
139
- * STOPPED.notCopied). */
140
- export type SeedFailed = "unreadable" | "not_copied";
146
+ * (`unreadable`, STOPPED.unreadable), what was read could not be made into its start (`not_copied`,
147
+ * STOPPED.notCopied), or its launch names a disk of its own, whose learning folder the server does not read
148
+ * (`other_disk`, STOPPED.otherDisk): a run on a named disk learns nothing (learn/learning-of.ts). */
149
+ export type SeedFailed = "unreadable" | "not_copied" | "other_disk";
141
150
  /** A learning start with no seed (no commit of main, runs-over-store.ts learningStartFor): the run learns nothing, is
142
151
  * never asked what to keep, and so is never told it learns. Why is its `seedFailed`, when its start records one. */
143
152
  export declare const seedFailed: (start: NonNullable<RunLaunch["learning"]>) => boolean;
@@ -70,7 +70,7 @@ export function launchBody(launch) {
70
70
  ...(launch.boxAllowSet ? { box_allow_set: true } : {}),
71
71
  ...(launch.files?.length ? { files: launch.files } : {}),
72
72
  ...(launch.filesPlaced ? { files_placed: true } : {}),
73
- ...(launch.learning ? { learning: { seed: launch.learning.seed, section: launch.learning.section, ...(launch.learning.workflows?.length ? { workflows: [...launch.learning.workflows] } : {}), ...(launch.learning.noWorkflows ? { noWorkflows: true } : {}), ...(launch.learning.model ? { model: launch.learning.model } : {}), ...(launch.learning.seedFailed ? { seedFailed: launch.learning.seedFailed } : {}), ...(launch.learning.use ? { use: true } : {}) } } : {}),
73
+ ...(launch.learning ? { learning: { seed: launch.learning.seed, section: launch.learning.section, ...(launch.learning.workflows?.length ? { workflows: [...launch.learning.workflows] } : {}), ...(launch.learning.noWorkflows ? { noWorkflows: true } : {}), ...(launch.learning.model ? { model: launch.learning.model } : {}), ...(launch.learning.seedFailed ? { seedFailed: launch.learning.seedFailed } : {}), ...(launch.learning.use ? { use: true } : {}), ...(launch.learning.seededAt !== undefined ? { seededAt: launch.learning.seededAt } : {}) } } : {}),
74
74
  ...(launch.workflowFirst === false ? { workflow_first: false } : {}),
75
75
  ...(launch.replay ? { replay: { of: launch.replay.of, workflow: launch.replay.workflow, digest: launch.replay.digest } } : {}),
76
76
  ...(launch.audit ? { audit: { of: launch.audit.of, workflow: launch.audit.workflow, digest: launch.audit.digest } } : {}),
@@ -90,7 +90,18 @@ function readLearning(value) {
90
90
  const seed = typeof value.seed === "string" ? value.seed : null;
91
91
  const section = typeof value.section === "string" ? value.section : "";
92
92
  const workflows = Array.isArray(value.workflows) ? value.workflows.filter((name) => typeof name === "string") : [];
93
- return { seed, section, ...(workflows.length ? { workflows } : {}), ...(value.noWorkflows === true ? { noWorkflows: true } : {}), ...(typeof value.model === "string" && value.model !== "" ? { model: value.model } : {}), ...(typeof value.seedFailed === "string" && SEED_FAILURES.includes(value.seedFailed) ? { seedFailed: value.seedFailed } : {}), ...(value.use === true ? { use: true } : {}) };
93
+ return { seed, section, ...(workflows.length ? { workflows } : {}), ...(value.noWorkflows === true ? { noWorkflows: true } : {}), ...(typeof value.model === "string" && value.model !== "" ? { model: value.model } : {}), ...(typeof value.seedFailed === "string" && SEED_FAILURES.includes(value.seedFailed) ? { seedFailed: value.seedFailed } : {}), ...(value.use === true ? { use: true } : {}), ...(typeof value.seededAt === "number" && Number.isFinite(value.seededAt) ? { seededAt: value.seededAt } : {}) };
94
+ }
95
+ /** The launch a runner runs (runner/dispatch.ts, run-child-host.ts): on a disk the launch names as its own (kind
96
+ * "launch"), its learning start holds no seed, # Learned section or learned workflow, whatever the body says (a start
97
+ * made before its run's disk was read, or written by hand), as the server's seed is the default disk's, never that
98
+ * disk's (learn/learning-of.ts): the run starts with nothing learned (`other_disk`), its flags kept. Any other launch as
99
+ * read. readLaunchBody keeps the body as written: a finished run's views show what it ran. */
100
+ export function launchOnItsDisk(launch) {
101
+ if (!launch.learning || launch.archilDisk?.kind !== "launch")
102
+ return launch;
103
+ const { noWorkflows, model, use } = launch.learning;
104
+ return { ...launch, learning: { seed: null, section: "", seedFailed: "other_disk", ...(noWorkflows ? { noWorkflows } : {}), ...(model ? { model } : {}), ...(use ? { use } : {}) } };
94
105
  }
95
106
  /** A launch body read back into a `RunLaunch`, with the input-origin fail-safe applied through the body's own trigger, or
96
107
  * undefined when the body is not one (not an object, or it names no place: a damaged or hand-edited body). */
@@ -140,7 +151,7 @@ export function readLaunchBody(stored) {
140
151
  export const learningStartOf = (launch) => launch.learning;
141
152
  /** The same start read from a launch body's `learning` member alone (Store.runs.learningStarts), as readLaunchBody reads it. */
142
153
  export const learningStartOfStored = (stored) => (stored === undefined ? undefined : readLearning(stored) ?? { seed: null, section: "" });
143
- const SEED_FAILURES = ["unreadable", "not_copied"];
154
+ const SEED_FAILURES = ["unreadable", "not_copied", "other_disk"];
144
155
  /** A learning start with no seed (no commit of main, runs-over-store.ts learningStartFor): the run learns nothing, is
145
156
  * never asked what to keep, and so is never told it learns. Why is its `seedFailed`, when its start records one. */
146
157
  export const seedFailed = (start) => start.seed === null;
@@ -9,7 +9,7 @@ export declare function servedRunLearning(deps: {
9
9
  }, store: Store, runId: string): Promise<{
10
10
  run: string;
11
11
  state: "dropped" | "merged" | "seeded" | "reflecting";
12
- reason: "cancelled" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
12
+ reason: "cancelled" | "reset" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
13
13
  detail: string | null;
14
14
  at: string;
15
15
  workflows: import("./learning.js").LearnedFile[];
@@ -52,7 +52,7 @@ export declare function servedRunLearning(deps: {
52
52
  };
53
53
  run: string;
54
54
  state: "dropped" | "merged" | "seeded" | "reflecting";
55
- reason: "cancelled" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
55
+ reason: "cancelled" | "reset" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
56
56
  detail: string | null;
57
57
  at: string;
58
58
  workflows: import("./learning.js").LearnedFile[];
@@ -2,7 +2,7 @@ import { type BrowserSessionLine } from "./browser-sessions.js";
2
2
  import { type CostBasis, type CostWords } from "./runner/metrics.js";
3
3
  import type { RunLaunch } from "./run-launch.js";
4
4
  import { type SignInReader } from "./run-view.js";
5
- import { type ComputerIntervalRow, type Store } from "./store/index.js";
5
+ import { type ComputerIntervalRow, type RunAttemptRow, type Store } from "./store/index.js";
6
6
  import { type ComputeResource, type ComputeUnit } from "./compute-cost.js";
7
7
  import { type EntryLike } from "./timeline/index.js";
8
8
  /** The meters a run's lines are on: the contract's `tokens` and `browser_time`, and this server's `tools` and
@@ -26,6 +26,12 @@ export type RunUsageLine = {
26
26
  quantity: number | null;
27
27
  unit: "token" | "call" | "second" | ComputeUnit;
28
28
  basis: CostBasis;
29
+ /** The attempt at its turn the line's requests were made in (1 the first, 2 its first redo, …); null for a run whose
30
+ * claims kept no attempt. */
31
+ attempt: number | null;
32
+ /** False for an attempt that was cut and whose turn another attempt worked again: its requests were made again, the
33
+ * infrastructure's cost (the run's `infra_usd`), not the task's. Charging does not read it yet. */
34
+ billable: boolean;
29
35
  /** On `compute_time`: the resource (vcpu, memory, gpu, disk), a GPU's type, the part of `quantity` no claim held (in
30
36
  * no cost), and how many of its computers are still open, their time counted up to `measured_to`. */
31
37
  resource?: ComputeResource;
@@ -61,10 +67,14 @@ export type RunUsage = {
61
67
  lines: RunUsageLine[] | null;
62
68
  /** One record per browser session the run opened (browser-sessions.ts), for matching `browser_time` to the provider's
63
69
  * own records by its session id; null exactly when `lines` is. */
64
- browser_sessions: BrowserSessionLine[] | null;
70
+ browser_sessions: RunBrowserSession[] | null;
65
71
  /** One record per computer the run made (computerIntervalsOf), by when each was made; null exactly when `lines` is.
66
72
  * Computer time is not priced: its cost is in no line and not in `total`. */
67
73
  computer_intervals: ComputerIntervalLine[] | null;
74
+ /** The sum of the lines that are not billable (`billable: false`): what the infrastructure's redo of cut attempts cost.
75
+ * 0 for a run no attempt of which was cut; null with no lines, and when one such line's cost is unknown. Inside
76
+ * `total`, never beside it. */
77
+ infra_usd: number | null;
68
78
  reason: UsageReason | null;
69
79
  };
70
80
  /** One computer the run made through its provider (a sandbox, a desktop, a phone), as GET /runs/{run_id}/usage serves
@@ -88,9 +98,38 @@ export type ComputerIntervalLine = {
88
98
  ended_at: string | null;
89
99
  kept: boolean;
90
100
  booked_seconds: number;
101
+ /** The attempt whose claim made the computer, and whether that attempt is billable (RunUsageLine). */
102
+ attempt: number | null;
103
+ billable: boolean;
91
104
  };
92
105
  /** The run's computer rows as served (ComputerIntervalLine), in the order the store reads them (by when each was made). */
93
- export declare function computerIntervalsOf(rows: readonly ComputerIntervalRow[]): ComputerIntervalLine[];
106
+ export declare function computerIntervalsOf(rows: readonly ComputerIntervalRow[], attempts?: readonly RunAttemptRow[]): ComputerIntervalLine[];
107
+ /** Which attempt a part of a run's usage was made in, and whether it is billable. */
108
+ export type AttemptTag = {
109
+ attempt: number | null;
110
+ billable: boolean;
111
+ };
112
+ /** A run's attempts (`rows`, by epoch) as tags: the attempt a journal entry was written in (the last claim whose first
113
+ * entry id is at or below the entry's), and the one a time falls in (the last claim made at or before it, on the
114
+ * store's clock: a computer is made a moment after its claim). `spans` splits a stretch of time across the claims whose
115
+ * windows it falls in (each claim's window runs from its claim to the next claim's). Every part is billable: a claim
116
+ * that takes over a cut turn resumes from what the cut one saved (its answered requests, its finished calls), so that
117
+ * work is the task's, done once. `redoneAt` is the one exception: the attempt an entry was written in, billable unless
118
+ * another claim worked its turn again after it (the same turn, one attempt more), for a model request that attempt sent
119
+ * and never finished (usageLinesOf), which the claim after it sends again. With no rows (a run from before
120
+ * them), every part is NO_ATTEMPT: attempt null, billable. A part before the first row is NO_ATTEMPT too, unless that
121
+ * row is the run's first claim (taken from queued): then nothing came before it, and a part read a moment early (a
122
+ * runner's clock behind the store's) is that claim's. A run that took its first row after the change that writes them
123
+ * (an older run's follow-up) never counts what it did before as any attempt's. */
124
+ export declare function attemptTags(rows: readonly RunAttemptRow[]): {
125
+ atEntry(id: unknown): AttemptTag;
126
+ redoneAt(id: unknown): AttemptTag;
127
+ atTime(ms: number): AttemptTag;
128
+ spans(from: number, to: number): {
129
+ at: AttemptTag;
130
+ ms: number;
131
+ }[];
132
+ };
94
133
  /** A workflow decision's receipt as the lines read it (store-reads/decisions.ts): by its `decision_id`, which its
95
134
  * request's booking names (`decision:<id>`), the model and provider that answered it, and how its cost was reached. */
96
135
  export type DecisionReceiptLike = {
@@ -107,10 +146,15 @@ export type DecisionReceiptLike = {
107
146
  export declare function usageLinesOf(entries: readonly EntryLike[], launch?: Pick<RunLaunch, "model">, receipts?: ReadonlyMap<string, DecisionReceiptLike>, computers?: {
108
147
  readonly rows: readonly ComputerIntervalRow[];
109
148
  readonly at: number;
110
- }): {
149
+ }, attempts?: readonly RunAttemptRow[]): {
111
150
  lines: RunUsageLine[];
112
151
  total: CostWords;
113
152
  };
153
+ /** A browser session as the usage read serves it: its record (BrowserSessionLine), and the attempt whose claim opened it
154
+ * and whether that attempt is billable (RunUsageLine). */
155
+ export type RunBrowserSession = BrowserSessionLine & AttemptTag;
156
+ /** The sum of the lines no customer is charged for (`billable: false`): null when one of them has no known figure. */
157
+ export declare function infraUsdOf(lines: readonly RunUsageLine[]): number | null;
114
158
  /** Run `runId`'s usage lines (GET /runs/{run_id}/usage); undefined for an unknown run. No lines (`reason`) while it is at
115
159
  * work, for a run whose figure is not its booking's, and for a learning run a follow-up opened again: the cases where
116
160
  * `cost.tools_usd` is null for the same reasons. Its state, launch, learning rounds and journal are read in one snapshot