@parcha/agentrun-serve 0.1.0-beta.39 → 0.1.0-beta.40

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 (81) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +51 -1
  3. package/dist/console/assets/index-B8Zn1Iqn.css +1 -0
  4. package/dist/console/assets/index-Jbxjcmdp.js +705 -0
  5. package/dist/console/index.html +2 -2
  6. package/dist/email/account.d.ts +0 -1
  7. package/dist/email/account.js +3 -3
  8. package/dist/email/answer.d.ts +5 -0
  9. package/dist/email/answer.js +35 -0
  10. package/dist/email/mail.js +19 -26
  11. package/dist/email/names.d.ts +30 -0
  12. package/dist/email/names.js +34 -0
  13. package/dist/email/routes.d.ts +1 -0
  14. package/dist/email/routes.js +1 -1
  15. package/dist/email/tools.d.ts +11 -4
  16. package/dist/email/tools.js +91 -46
  17. package/dist/eval-mailbox.d.ts +1 -1
  18. package/dist/eval-mailbox.js +3 -3
  19. package/dist/learn/compile.d.ts +3 -2
  20. package/dist/learn/compile.js +6 -5
  21. package/dist/learn/promote.d.ts +25 -14
  22. package/dist/learn/promote.js +32 -14
  23. package/dist/learning-cost.d.ts +17 -8
  24. package/dist/learning-cost.js +12 -4
  25. package/dist/local-models.d.ts +7 -1
  26. package/dist/local-models.js +11 -5
  27. package/dist/openapi.d.ts +1 -1
  28. package/dist/openapi.js +17 -15
  29. package/dist/port-models.d.ts +4 -1
  30. package/dist/port-models.js +1 -0
  31. package/dist/refusal-words.d.ts +3 -0
  32. package/dist/refusal-words.js +3 -0
  33. package/dist/run-launch.d.ts +2 -0
  34. package/dist/run-launch.js +2 -0
  35. package/dist/run-view.d.ts +3 -4
  36. package/dist/run-view.js +26 -24
  37. package/dist/runner/adapter-loop.d.ts +6 -2
  38. package/dist/runner/adapter-loop.js +45 -12
  39. package/dist/runner/adapter-run-spec.d.ts +1 -0
  40. package/dist/runner/adapter-run-spec.js +37 -1
  41. package/dist/runner/cli-run-spec.js +1 -0
  42. package/dist/runner/journal-kinds.d.ts +22 -0
  43. package/dist/runner/journal-kinds.js +39 -0
  44. package/dist/runner/model-route.d.ts +3 -1
  45. package/dist/runner/model-route.js +18 -3
  46. package/dist/runner/promotion.d.ts +8 -8
  47. package/dist/runner/promotion.js +135 -54
  48. package/dist/server.js +15 -8
  49. package/dist/store/dialect.d.ts +3 -0
  50. package/dist/store/dialect.js +1 -0
  51. package/dist/store/postgres/connection.d.ts +11 -5
  52. package/dist/store/postgres/connection.js +44 -17
  53. package/dist/store/postgres/dialect.js +5 -0
  54. package/dist/store/postgres/store.d.ts +7 -3
  55. package/dist/store/postgres/store.js +1 -0
  56. package/dist/store/sqlite/connection.js +72 -8
  57. package/dist/store/sqlite/run-launch.d.ts +11 -0
  58. package/dist/store/sqlite/run-launch.js +8 -0
  59. package/dist/store/sqlite/store.js +37 -16
  60. package/dist/store/sqlite/worker.d.ts +2 -0
  61. package/dist/store/sqlite/worker.js +13 -10
  62. package/dist/store/store.d.ts +15 -0
  63. package/dist/store-reads/learning.d.ts +30 -13
  64. package/dist/store-reads/learning.js +125 -51
  65. package/dist/template-eval.js +1 -1
  66. package/dist/templates.d.ts +14 -1
  67. package/dist/templates.js +67 -17
  68. package/dist/tool-gate.d.ts +5 -3
  69. package/dist/tool-gate.js +5 -2
  70. package/dist/tools.d.ts +13 -0
  71. package/dist/tools.js +15 -10
  72. package/dist/trigger-adapter.d.ts +3 -32
  73. package/dist/trigger-email.js +30 -77
  74. package/dist/triggers.d.ts +26 -14
  75. package/dist/triggers.js +99 -76
  76. package/dist/workflow/decision-recovery.js +5 -2
  77. package/dist/workflow/decisions.d.ts +7 -0
  78. package/package.json +9 -9
  79. package/templates/README.md +2 -2
  80. package/dist/console/assets/index-BNhZs-jp.css +0 -1
  81. package/dist/console/assets/index-BQv5aOGB.js +0 -700
@@ -14,7 +14,7 @@ import { createHash } from "node:crypto";
14
14
  import { childNodes, LEARNED_MAX_TURNS, frontMatter, WORKFLOW_NAME, WORKFLOWS_DIR } from "./skills.js";
15
15
  import { NOTE_BLOCK, parseReflection, SKILL_BLOCK, WORKFLOW_BLOCK } from "./reflect.js";
16
16
  import { NOTE_FILE } from "./notes.js";
17
- import { answersAlone, championOf, LABEL_MAX, PROMOTE_BAR, promotionOf } from "./promote.js";
17
+ import { answersAlone, championOf, LABEL_MAX, PROMOTE_BAR, promotionOf, replayingOf } from "./promote.js";
18
18
  import { redactSecrets } from "./secrets.js";
19
19
  /** How many active notes an agent keeps before its look-back compiles its job. */
20
20
  export const COMPILE_AFTER_NOTES = 3;
@@ -39,22 +39,23 @@ const ERRORS_MAX = 10;
39
39
  const ERROR_MAX = 400;
40
40
  /** Whether the draft a compile kept at `digest` serves (learn/promote.ts records, newest wins): true once promoted, or
41
41
  * once a threshold refit of it was (its new revision serves in its place); false while held, or once a newer revision
42
- * superseded it; undefined while it is replayed or before its first claim decides it. */
42
+ * was skipped for a newer one; undefined while it is queued or replayed, or before its first claim decides it. */
43
43
  export function promotedOf(records, workflow, digest) {
44
44
  const own = promotionOf(records, workflow, digest);
45
45
  if (own?.decided === "promote")
46
46
  return true;
47
47
  if (records.some((one) => one.workflow === workflow && one.decided === "promote" && one.source === "refit" && one.refit?.from === digest))
48
48
  return true;
49
- return own?.decided === "hold" || own?.decided === "superseded" ? false : undefined;
49
+ return own?.decided === "hold" || own?.decided === "skipped" ? false : undefined;
50
50
  }
51
51
  /** The learned workflows whose newest revision other than their champion (learn/promote.ts championOf, which serves while
52
52
  * that revision is held) is held: each one's compile turn is asked for a revision (once per hold). A champion's own
53
- * graded hold counts: with it held there is no champion. */
53
+ * graded hold counts: with it held there is no champion. One with a revision queued is not: that newer revision replays
54
+ * next (a replay ahead of it held meanwhile asks for nothing). */
54
55
  export function heldNamesOf(records) {
55
56
  return new Set([...new Set(records.map(({ workflow }) => workflow))].filter((name) => {
56
57
  const champion = championOf(records, name)?.digest;
57
- return records.findLast((one) => one.workflow === name && one.digest !== champion)?.decided === "hold";
58
+ return records.findLast((one) => one.workflow === name && one.digest !== champion)?.decided === "hold" && !replayingOf(records, name, null, "queued").length;
58
59
  }));
59
60
  }
60
61
  /** Whether a run's look-back compiles its agent's job: the run completed, the agent has no learned workflow (active or
@@ -66,8 +66,10 @@ export declare const scoreOf: (x: ReplayScore) => ReplayScore;
66
66
  export declare const answersAlone: (score: Pick<ReplayScore, "n" | "handed_back">) => boolean;
67
67
  /** A learned workflow's promotion record at one digest. `decided`: `promote` (it serves first), `hold` (it does not: the
68
68
  * champion, championOf, takes its runs, else the agent, until a new revision), `replaying` (its replay runs, `runs`,
69
- * are at work; the champion serves meanwhile, else the agent), or `superseded` (a newer revision, `superseded_by`, came
70
- * while it replayed: one challenger at a time, so its replay runs were cancelled and it never serves). `source`: `replay` (decided on its replay), `dry_run` (fewer than REPLAY_MIN graded inputs: promoted on
69
+ * are at work; the champion serves meanwhile, else the agent), `queued` (it came while another revision replayed: one
70
+ * replays at a time, and a running replay finishes; it replays next once that one is decided, unless a newer revision
71
+ * comes first), or `skipped` (a newer revision, `skipped_by`, came while it was queued: only the newest queued one
72
+ * replays next, so it never replayed and never serves). `source`: `replay` (decided on its replay), `dry_run` (fewer than REPLAY_MIN graded inputs: promoted on
71
73
  * its passing dry run), `refit` (a threshold refit wrote this revision, decided on the rows it was fit on), `graded` (a
72
74
  * serving revision's graded runs fell below the bar: held). `replay`: the numbers it was decided on, with the graded runs
73
75
  * replayed (`of`), what its replay runs cost (`cost_usd`, null when one's is unknown), its runs' outcomes (`outcomes`,
@@ -83,7 +85,7 @@ export interface PromotionRecord {
83
85
  readonly workflow: string;
84
86
  readonly digest: string;
85
87
  readonly at: string;
86
- readonly decided: "promote" | "hold" | "replaying" | "superseded";
88
+ readonly decided: "promote" | "hold" | "replaying" | "queued" | "skipped";
87
89
  readonly source: "replay" | "dry_run" | "refit" | "graded";
88
90
  readonly reason: string;
89
91
  readonly replay?: ReplayScore & {
@@ -123,8 +125,8 @@ export interface PromotionRecord {
123
125
  * inputs), and on a replay's hold: a replay of the revision promotes it only once it answered (or handed back) on every
124
126
  * one (runner/promotion.ts heldOn). */
125
127
  readonly held_on?: readonly string[];
126
- /** A superseded revision's newer one: the digest whose claim cancelled its replay. */
127
- readonly superseded_by?: string;
128
+ /** A skipped revision's newer one: the digest that came while it was queued, and replays in its place. */
129
+ readonly skipped_by?: string;
128
130
  /** The learning commit whose workflows/<workflow>/workflow.json is this digest's text: the seed of the claim that
129
131
  * recorded it, or a refit's own commit. What the digest serves from as the champion (championOf) once a newer revision
130
132
  * is on the folder's main. */
@@ -171,7 +173,11 @@ export declare const putGrade: (settings: Pick<Settings, "get" | "putIfAbsent" |
171
173
  * that round's steer, in place: its `seq` stays, so a serving digest's check never counts it twice. A newer grade of the
172
174
  * run is left as it is, one given in the same millisecond too (its `seq` is another). The grade, marked. */
173
175
  export declare const markGradeRound: (settings: Pick<Settings, "get" | "putIfAbsent" | "replaceIf">, agentId: string, grade: Grade, opened: OpenedRound) => Promise<Grade>;
174
- /** The agent's promotion records, oldest first. */
176
+ /** The agent's promotion records, oldest first. A beta.39 store may hold `decided: "superseded"` (a replay a newer draft
177
+ * cancelled; beta.40 writes `skipped` for an older draft instead): read as `skipped`, `skipped_by` the draft that
178
+ * superseded it, with a reason that says when. Only the read maps (readAs, also what a guarded write compares): the stored
179
+ * record is never rewritten. */
180
+ export declare const readAs: (one: PromotionRecord) => PromotionRecord;
175
181
  export declare const promotionsOf: (settings: Pick<Settings, "get">, agentId: string) => Promise<PromotionRecord[]>;
176
182
  /** The newest record of `workflow` at `digest`, or undefined. */
177
183
  export declare const promotionOf: (records: readonly PromotionRecord[], workflow: string, digest: string) => PromotionRecord | undefined;
@@ -182,12 +188,13 @@ export declare const promotionOf: (records: readonly PromotionRecord[], workflow
182
188
  * revision its owner replays (runner/promotion.ts askReplay) and the replay promotes wins again: the champion from then.
183
189
  * The money rule stops it on its own (runner/workflow-first.ts). Undefined: none. */
184
190
  export declare function championOf(records: readonly PromotionRecord[], workflow: string): PromotionRecord | undefined;
185
- /** The revisions of `workflow` other than `digest` whose newest record still says replaying, newest first: at most one
186
- * once each new revision supersedes the one replaying (runner/promotion.ts). */
187
- export declare function replayingOf(records: readonly PromotionRecord[], workflow: string, digest: string | null): PromotionRecord[];
191
+ /** The revisions of `workflow` other than `digest` whose newest record still says `decided` (replaying, else queued),
192
+ * newest first: at most one replaying (one replay at a time, the reservation's guarded write), and at most one queued
193
+ * once each newer one skips the one queued before it (runner/promotion.ts). */
194
+ export declare function replayingOf(records: readonly PromotionRecord[], workflow: string, digest: string | null, decided?: "replaying" | "queued"): PromotionRecord[];
188
195
  /** What each learned workflow with a record serves, by name: `digest`, its champion's (championOf; null: none serves,
189
- * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a
190
- * superseded one's replay was cancelled, never at work). What the promotions list says beside the records
196
+ * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a queued
197
+ * one is not at work yet). What the promotions list says beside the records
191
198
  * (GET /agents/{id}/learning/promotions). */
192
199
  export declare function servingOf(records: readonly PromotionRecord[]): {
193
200
  workflow: string;
@@ -216,15 +223,19 @@ export type Recorded = {
216
223
  };
217
224
  /** Keeps `record` as the newest of its workflow at its digest, when `still` holds of the one there now (none: undefined)
218
225
  * and of every record kept (`items`: a check across revisions, one challenger at a time, in the same guarded write): so
219
- * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). */
220
- export declare function recordPromotion(settings: Pick<Settings, "get" | "putIfAbsent" | "replaceIf">, agentId: string, record: PromotionRecord, still: (now: PromotionRecord | undefined, items: readonly PromotionRecord[]) => boolean): Promise<Recorded>;
226
+ * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). `also`:
227
+ * records kept after it in the same write, all or none (a queue change and the skips it makes, runner/promotion.ts). */
228
+ export declare function recordPromotion(settings: Pick<Settings, "get" | "putIfAbsent" | "replaceIf">, agentId: string, record: PromotionRecord, still: (now: PromotionRecord | undefined, items: readonly PromotionRecord[]) => boolean, also?: readonly PromotionRecord[]): Promise<Recorded>;
221
229
  /** The newest records, at most PROMOTIONS_KEPT, in order, with each workflow's champion (championOf) the same after as
222
230
  * before, whatever was dropped. championOf reads two things, and the trim keeps both: the place of every digest it can
223
231
  * still see promoted (a promote record kept keeps its win, winsOf, beside it, so an older revision's late graded check
224
232
  * never reads as a newer promotion), and the digest promoted last, its latest win and its newest record (so a held last
225
233
  * revision is never trimmed into handing the champion back). The kept records are a run of the newest plus those wins,
226
234
  * and a win's own run reaches no further back than it: a record that ended an older run of the same digest is newer
227
- * than every record of that run, so a run is never joined to an older one by the trim. */
235
+ * than every record of that run, so a run is never joined to an older one by the trim. The queue's state is kept too,
236
+ * whatever its age: a replaying revision's newest record (what its settle, its runs' claims and the one-replay-at-a-time
237
+ * check read: dropped, a newer revision would start a second replay while its runs run, and its verdict would never
238
+ * land) and a queued one's (what replays next). Neither is a promote record, so championOf reads the same. */
228
239
  export declare function trimPromotions(items: readonly PromotionRecord[]): PromotionRecord[];
229
240
  /** One replayed input: the graded run it replays (`of`), its recorded answer, its grade and the path it took (whose
230
241
  * answer it is: agentRight), and what the workflow
@@ -70,8 +70,17 @@ export const markGradeRound = async (settings, agentId, grade, opened) => {
70
70
  await updateKept(settings, gradesKey(agentId), (items) => items.map((one) => (one.run === grade.run && one.at === grade.at && one.seq === grade.seq ? { ...one, ...mark } : one)));
71
71
  return { ...grade, ...mark };
72
72
  };
73
- /** The agent's promotion records, oldest first. */
74
- export const promotionsOf = async (settings, agentId) => itemsOf(await settings.get(promotionsKey(agentId)));
73
+ /** The agent's promotion records, oldest first. A beta.39 store may hold `decided: "superseded"` (a replay a newer draft
74
+ * cancelled; beta.40 writes `skipped` for an older draft instead): read as `skipped`, `skipped_by` the draft that
75
+ * superseded it, with a reason that says when. Only the read maps (readAs, also what a guarded write compares): the stored
76
+ * record is never rewritten. */
77
+ export const readAs = (one) => {
78
+ if (one.decided !== "superseded")
79
+ return one;
80
+ const { superseded_by: by, ...rest } = one;
81
+ return { ...rest, decided: "skipped", reason: "a newer draft arrived while it replayed (before beta.40)", ...(by ? { skipped_by: by } : {}) };
82
+ };
83
+ export const promotionsOf = async (settings, agentId) => itemsOf(await settings.get(promotionsKey(agentId))).map(readAs);
75
84
  /** The newest record of `workflow` at `digest`, or undefined. */
76
85
  export const promotionOf = (records, workflow, digest) => records.findLast((one) => one.workflow === workflow && one.digest === digest);
77
86
  /** The champion of `workflow`: the newest record of the digest promoted last (a digest's place is its latest win's,
@@ -109,21 +118,22 @@ function lastPromotedOf(records, workflow) {
109
118
  last = records[i].digest;
110
119
  return last;
111
120
  }
112
- /** The revisions of `workflow` other than `digest` whose newest record still says replaying, newest first: at most one
113
- * once each new revision supersedes the one replaying (runner/promotion.ts). */
114
- export function replayingOf(records, workflow, digest) {
121
+ /** The revisions of `workflow` other than `digest` whose newest record still says `decided` (replaying, else queued),
122
+ * newest first: at most one replaying (one replay at a time, the reservation's guarded write), and at most one queued
123
+ * once each newer one skips the one queued before it (runner/promotion.ts). */
124
+ export function replayingOf(records, workflow, digest, decided = "replaying") {
115
125
  const out = [];
116
126
  for (const one of [...records].reverse()) {
117
127
  if (one.workflow !== workflow || one.digest === digest || out.some((seen) => seen.digest === one.digest))
118
128
  continue;
119
- if (promotionOf(records, workflow, one.digest) === one && one.decided === "replaying")
129
+ if (promotionOf(records, workflow, one.digest) === one && one.decided === decided)
120
130
  out.push(one);
121
131
  }
122
132
  return out;
123
133
  }
124
134
  /** What each learned workflow with a record serves, by name: `digest`, its champion's (championOf; null: none serves,
125
- * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a
126
- * superseded one's replay was cancelled, never at work). What the promotions list says beside the records
135
+ * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a queued
136
+ * one is not at work yet). What the promotions list says beside the records
127
137
  * (GET /agents/{id}/learning/promotions). */
128
138
  export function servingOf(records) {
129
139
  const names = [...new Set(records.map((one) => one.workflow))].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
@@ -146,17 +156,20 @@ export function promotionsShown(records, shown) {
146
156
  }
147
157
  /** Keeps `record` as the newest of its workflow at its digest, when `still` holds of the one there now (none: undefined)
148
158
  * and of every record kept (`items`: a check across revisions, one challenger at a time, in the same guarded write): so
149
- * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). */
150
- export async function recordPromotion(settings, agentId, record, still) {
159
+ * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). `also`:
160
+ * records kept after it in the same write, all or none (a queue change and the skips it makes, runner/promotion.ts). */
161
+ export async function recordPromotion(settings, agentId, record, still, also = []) {
151
162
  let out = { record, won: true };
152
163
  await updateKept(settings, promotionsKey(agentId), (items) => {
153
- const now = promotionOf(items, record.workflow, record.digest);
154
- if (!still(now, items)) {
164
+ // `still` compares what a read shows (readAs); the write keeps the stored items as they are.
165
+ const read = items.map(readAs);
166
+ const now = promotionOf(read, record.workflow, record.digest);
167
+ if (!still(now, read)) {
155
168
  out = { record: now, won: false };
156
169
  return items;
157
170
  }
158
171
  out = { record, won: true };
159
- return trimPromotions([...items, record]);
172
+ return trimPromotions([...items, record, ...also]);
160
173
  });
161
174
  return out;
162
175
  }
@@ -166,7 +179,10 @@ export async function recordPromotion(settings, agentId, record, still) {
166
179
  * never reads as a newer promotion), and the digest promoted last, its latest win and its newest record (so a held last
167
180
  * revision is never trimmed into handing the champion back). The kept records are a run of the newest plus those wins,
168
181
  * and a win's own run reaches no further back than it: a record that ended an older run of the same digest is newer
169
- * than every record of that run, so a run is never joined to an older one by the trim. */
182
+ * than every record of that run, so a run is never joined to an older one by the trim. The queue's state is kept too,
183
+ * whatever its age: a replaying revision's newest record (what its settle, its runs' claims and the one-replay-at-a-time
184
+ * check read: dropped, a newer revision would start a second replay while its runs run, and its verdict would never
185
+ * land) and a queued one's (what replays next). Neither is a promote record, so championOf reads the same. */
170
186
  export function trimPromotions(items) {
171
187
  if (items.length <= PROMOTIONS_KEPT)
172
188
  return [...items];
@@ -174,6 +190,8 @@ export function trimPromotions(items) {
174
190
  const placeOf = (i) => wins.get(i);
175
191
  const keep = new Set();
176
192
  for (const workflow of new Set(items.map((one) => one.workflow))) {
193
+ for (const one of [...replayingOf(items, workflow, null), ...replayingOf(items, workflow, null, "queued")])
194
+ keep.add(items.lastIndexOf(one));
177
195
  const last = lastPromotedOf(items, workflow);
178
196
  if (last === undefined)
179
197
  continue;
@@ -61,16 +61,24 @@ export type CostReplay = {
61
61
  agent_pct: number;
62
62
  cleared: boolean;
63
63
  };
64
+ /** The paths a learned workflow served a run on: it ran first and answered alone (`workflow`) or handed back
65
+ * (`handed_back`). A `replay` checks a new revision on a graded input and serves no one; an `agent` run is by hand. */
66
+ export declare const SERVED_PATHS: readonly RunPath[];
67
+ /** Why the after side has a figure or not: `priced`, it has; `no_served_runs`, no learned workflow has taken over (a
68
+ * replay is no served run); `cost_unknown`, runs were served but none's all-in cost is known exactly. */
69
+ export declare const AFTER_STATES: readonly ["priced", "no_served_runs", "cost_unknown"];
70
+ export type AfterState = (typeof AFTER_STATES)[number];
64
71
  /** What the series shows: `takeover`, its first `workflow` run, null before one; `before`, the mean of the job's own $
65
72
  * (learning left out) over its `agent` runs before the takeover (every one when there is none yet), whose cost is known
66
- * exactly; `after`, the mean all-in $ (learning included) of every run from the takeover on whose total is known exactly;
67
- * and `times_cheaper`, before over after to a tenth, once `after` counts CHEAPER_AFTER_RUNS runs or more and every run
68
- * either side reads is known exactly, else null. The before side leaves learning out and the after side keeps it in, so
69
- * the figure never flatters learning. `withheld`: after a takeover, how many runs either side reads have an unknown cost
70
- * and how many one known only as at most (`cost_upper_bound`); while any does there is no ratio, null when none does.
71
- * `hand_backs`: of the runs a learned workflow ran first on from the takeover on (`workflow` or `handed_back`), how many
72
- * handed back; null before a takeover. `replay`: the serving promotion's, as given. Every one is over every run, never
73
- * only the latest a read lists. */
73
+ * exactly; `after`, the mean all-in $ (learning included) of the runs a learned workflow served from the takeover on
74
+ * (SERVED_PATHS: never a replay, which checks a revision and serves no one, nor a run by hand) whose total is known
75
+ * exactly, and `after_state` why it is null when it is; and `times_cheaper`, before over after to a tenth, once `after`
76
+ * counts CHEAPER_AFTER_RUNS runs or more and every run either side reads is known exactly, else null. The before side
77
+ * leaves learning out and the after side keeps it in, so the figure never flatters learning. `withheld`: after a
78
+ * takeover, how many runs either side reads have an unknown cost and how many one known only as at most
79
+ * (`cost_upper_bound`); while any does there is no ratio, null when none does. `hand_backs`: of the served runs from
80
+ * the takeover on, how many handed back; null before a takeover. `replay`: the serving promotion's, as given. Every one
81
+ * is over every run, never only the latest a read lists. */
74
82
  export type CostSummary = {
75
83
  takeover: {
76
84
  n: number;
@@ -84,6 +92,7 @@ export type CostSummary = {
84
92
  runs: number;
85
93
  mean_usd: number;
86
94
  } | null;
95
+ after_state: AfterState;
87
96
  times_cheaper: number | null;
88
97
  hand_backs: {
89
98
  ran: number;
@@ -41,10 +41,18 @@ export const COST_WINDOW = 10;
41
41
  /** The latest runs the route answers by default, and at most. */
42
42
  export const COST_RUNS_DEFAULT = 1000;
43
43
  export const COST_RUNS_MAX = 10_000;
44
+ /** The paths a learned workflow served a run on: it ran first and answered alone (`workflow`) or handed back
45
+ * (`handed_back`). A `replay` checks a new revision on a graded input and serves no one; an `agent` run is by hand. */
46
+ export const SERVED_PATHS = ["workflow", "handed_back"];
47
+ /** Why the after side has a figure or not: `priced`, it has; `no_served_runs`, no learned workflow has taken over (a
48
+ * replay is no served run); `cost_unknown`, runs were served but none's all-in cost is known exactly. */
49
+ export const AFTER_STATES = ["priced", "no_served_runs", "cost_unknown"];
44
50
  export function costSummary(runs, replay = null) {
45
51
  const first = runs.findIndex((run) => run.path === "workflow");
46
52
  const beforeSide = (first < 0 ? runs : runs.slice(0, first)).filter((run) => run.path === "agent").map((run) => ({ value: run.cost_usd, bound: run.cost_upper_bound }));
47
- const afterSide = first < 0 ? [] : runs.slice(first).map((run) => ({ value: run.total_usd, bound: run.cost_upper_bound }));
53
+ // Served runs alone: a replay (a check of a new revision, at workflow cost) and a run by hand are never the after side.
54
+ const served = first < 0 ? [] : runs.slice(first).filter((run) => SERVED_PATHS.includes(run.path));
55
+ const afterSide = served.map((run) => ({ value: run.total_usd, bound: run.cost_upper_bound }));
48
56
  const mean = (side) => {
49
57
  const values = side.flatMap(({ value, bound }) => (value === null || bound ? [] : [value]));
50
58
  return values.length ? { runs: values.length, mean_usd: usd(values.reduce((a, b) => a + b, 0) / values.length) } : null;
@@ -57,9 +65,9 @@ export function costSummary(runs, replay = null) {
57
65
  const atMost = read.filter(({ bound }) => bound).length;
58
66
  const withheld = first >= 0 && unknown + atMost > 0 ? { unknown, at_most: atMost } : null;
59
67
  const times = !withheld && before && after && after.runs >= CHEAPER_AFTER_RUNS && after.mean_usd > 0 ? Math.round((before.mean_usd / after.mean_usd) * 10) / 10 : null;
60
- const ranFirst = first < 0 ? [] : runs.slice(first).filter((run) => run.path === "workflow" || run.path === "handed_back");
61
- const handBacks = first < 0 ? null : { ran: ranFirst.length, back: ranFirst.filter((run) => run.path === "handed_back").length };
62
- return { takeover: first < 0 ? null : { n: runs[first].n, run_id: runs[first].run_id }, before, after, times_cheaper: times, hand_backs: handBacks, withheld, replay };
68
+ const handBacks = first < 0 ? null : { ran: served.length, back: served.filter((run) => run.path === "handed_back").length };
69
+ const afterState = after ? "priced" : first < 0 ? "no_served_runs" : "cost_unknown";
70
+ return { takeover: first < 0 ? null : { n: runs[first].n, run_id: runs[first].run_id }, before, after, after_state: afterState, times_cheaper: times, hand_backs: handBacks, withheld, replay };
63
71
  }
64
72
  /** The series as CSV (RFC 4180, a header line, `\r\n` line ends): one row a run, an unknown figure empty. A text cell that
65
73
  * a spreadsheet would read as a formula (one starting with =, +, -, @, a tab or a carriage return) starts with a `'`, so
@@ -30,7 +30,12 @@ export declare const PRICE_OVERRIDES: readonly {
30
30
  * id, name, input, context window and output ceiling, at $0, taking its tools natively (the `like` entry's `toolsAs` is
31
31
  * not copied). Context and output ceiling are OpenRouter's (GET https://openrouter.ai/api/v1/models, read 2026-11-15:
32
32
  * context_length and top_provider.max_completion_tokens; pricing "0" prompt and completion). Each one answered a request
33
- * that defined a tool with a call to that tool, through OpenRouter, on 2026-11-15, billed $0. */
33
+ * that defined a tool with a call to that tool, through OpenRouter, on 2026-11-15, billed $0. `mustReason` is that list's
34
+ * `reasoning.mandatory` (read 2026-11-16): a model that must reason has thinking off mapped to no effort (thinkingLevelMap
35
+ * `off: null`, as pi-ai's catalog maps Claude Sonnet 5.5's), so pi-ai never sends it `reasoning: {effort: "none"}`, which
36
+ * OpenRouter refuses with "Reasoning is mandatory for this endpoint and cannot be disabled", and the model reasons at
37
+ * its default. A model that lists `reasoning` among its supported parameters need not reason (Ling 3.2 Flash): only
38
+ * `mandatory` says so. */
34
39
  export declare const ADDED_MODELS: readonly {
35
40
  readonly provider: string;
36
41
  readonly id: string;
@@ -39,6 +44,7 @@ export declare const ADDED_MODELS: readonly {
39
44
  readonly input: readonly ("text" | "image")[];
40
45
  readonly contextWindow: number;
41
46
  readonly maxTokens: number;
47
+ readonly mustReason: boolean;
42
48
  }[];
43
49
  /** Whether `provider`/`id` is one of OpenRouter's routers, each of which picks a model per request (the kernel cap's
44
50
  * ROUTERS, the one list). OpenRouter lists the price of `auto`, `openrouter/auto*`, `openrouter/fusion`,
@@ -29,21 +29,27 @@ const ZERO = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
29
29
  * id, name, input, context window and output ceiling, at $0, taking its tools natively (the `like` entry's `toolsAs` is
30
30
  * not copied). Context and output ceiling are OpenRouter's (GET https://openrouter.ai/api/v1/models, read 2026-11-15:
31
31
  * context_length and top_provider.max_completion_tokens; pricing "0" prompt and completion). Each one answered a request
32
- * that defined a tool with a call to that tool, through OpenRouter, on 2026-11-15, billed $0. */
32
+ * that defined a tool with a call to that tool, through OpenRouter, on 2026-11-15, billed $0. `mustReason` is that list's
33
+ * `reasoning.mandatory` (read 2026-11-16): a model that must reason has thinking off mapped to no effort (thinkingLevelMap
34
+ * `off: null`, as pi-ai's catalog maps Claude Sonnet 5.5's), so pi-ai never sends it `reasoning: {effort: "none"}`, which
35
+ * OpenRouter refuses with "Reasoning is mandatory for this endpoint and cannot be disabled", and the model reasons at
36
+ * its default. A model that lists `reasoning` among its supported parameters need not reason (Ling 3.2 Flash): only
37
+ * `mandatory` says so. */
33
38
  export const ADDED_MODELS = [
34
- { provider: "openrouter", id: "baidu/ernie-5.5-flash-preview:free", like: "inclusionai/ling-3.1-flash", name: "Baidu: ERNIE 5.5 Flash Preview (free)", input: ["text"], contextWindow: 1_024_000, maxTokens: 921_600 },
35
- { provider: "openrouter", id: "inclusionai/ling-3.2-flash:free", like: "inclusionai/ling-3.1-flash", name: "inclusionAI: Ling 3.2 Flash (free)", input: ["text"], contextWindow: 1_048_576, maxTokens: 262_144 },
39
+ { provider: "openrouter", id: "baidu/ernie-5.5-flash-preview:free", like: "inclusionai/ling-3.1-flash", name: "Baidu: ERNIE 5.5 Flash Preview (free)", input: ["text"], contextWindow: 1_024_000, maxTokens: 921_600, mustReason: true },
40
+ { provider: "openrouter", id: "inclusionai/ling-3.2-flash:free", like: "inclusionai/ling-3.1-flash", name: "inclusionAI: Ling 3.2 Flash (free)", input: ["text"], contextWindow: 1_048_576, maxTokens: 262_144, mustReason: false },
36
41
  ];
37
42
  /** `list` with ADDED_MODELS appended, each built on its `like` entry in `list` (one whose `like` the list lacks is left
38
43
  * out) and never twice: once pi-ai lists an id itself, its own entry stands. */
39
44
  const added = (list, adds) => [
40
45
  ...list,
41
- ...adds.flatMap(({ id, like, name, input, contextWindow, maxTokens }) => {
46
+ ...adds.flatMap(({ id, like, name, input, contextWindow, maxTokens, mustReason }) => {
42
47
  const base = list.find((model) => model.id === like);
43
48
  if (!base || list.some((model) => model.id === id))
44
49
  return [];
45
50
  const { toolsAs: _base, ...rest } = base;
46
- return [{ ...rest, id, name, input: [...input], contextWindow, maxTokens, cost: { ...ZERO } }];
51
+ const levels = rest.thinkingLevelMap;
52
+ return [{ ...rest, id, name, input: [...input], contextWindow, maxTokens, cost: { ...ZERO }, ...(mustReason ? { reasoning: true, thinkingLevelMap: { ...levels, off: null } } : {}) }];
47
53
  }),
48
54
  ];
49
55
  /** Whether `provider`/`id` is one of OpenRouter's routers, each of which picks a model per request (the kernel cap's
package/dist/openapi.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TriggerAdapter } from "./trigger-adapter.js";
1
+ import { type TriggerAdapter } from "./trigger-adapter.js";
2
2
  import "./mcp-secret-writes.js";
3
3
  import "./email/routes.js";
4
4
  import "./computers.js";
package/dist/openapi.js CHANGED
@@ -8,7 +8,8 @@ import { NATIVE_BINDING, NATIVE_RUN, NATIVE_PATHS } from "./native/schema.js";
8
8
  // `x-agentrun-serve` (true, or the addition that made it, as `stats/1`), listed in `info.x-agentrun-serve`.
9
9
  import { readFileSync } from "node:fs";
10
10
  import { CALL_RESULT_CHARS, CAPS, TRIGGER_SOURCES } from "./timeline/index.js";
11
- import { EMAIL_SCOPES, SCOPED_TOOLS } from "./email/names.js";
11
+ import { EMAIL_SCOPES, PASSWORD_MAX, SCOPED_TOOLS, SEND_CAP } from "./email/names.js";
12
+ import { PAYLOAD_KEPT_BYTES } from "./trigger-adapter.js";
12
13
  import { TRIGGER_ADAPTERS } from "./trigger-adapters.js";
13
14
  import { NOTE_ORIGINS, NOTE_STATUSES, NOTE_TYPES } from "./learn/index.js";
14
15
  import { ERROR_STATUS } from "./errors.js";
@@ -19,7 +20,7 @@ import { LAUNCH_LOST, SETUP_LOST } from "./files.js";
19
20
  import { HARNESSES } from "./harness.js";
20
21
  import { LEARNING_LABEL, LEARNING_MODES, LEARNING_REASONS, LEARNING_STATES, RUN_LEARNING_USE } from "./learning.js";
21
22
  import { BATCH_CELL_MAX, BATCH_ROWS_MAX } from "./learning-batch.js";
22
- import { CHEAPER_AFTER_RUNS, COST_CSV_COLUMNS, COST_WINDOW, COST_RUNS_DEFAULT, COST_RUNS_MAX, RUN_PATHS } from "./learning-cost.js";
23
+ import { AFTER_STATES, CHEAPER_AFTER_RUNS, COST_CSV_COLUMNS, COST_WINDOW, COST_RUNS_DEFAULT, COST_RUNS_MAX, RUN_PATHS } from "./learning-cost.js";
23
24
  import { FEEDBACK_ROUNDS, FEEDBACK_STATUSES } from "./runner/feedback-round.js";
24
25
  import { EVAL_JOBS_MAX, EVAL_TEXT_MAX, EVALS_KEPT } from "./learning-evals.js";
25
26
  import { LOOP_COMPILES_MAX, LOOP_POINTS_MAX } from "./store-reads/loop.js";
@@ -40,7 +41,7 @@ import { MCP_SETTINGS_PAGE } from "./mcp-oauth.js";
40
41
  import { ATTEMPTS_KEPT, MESSAGE_TYPES, WEBHOOK_EVENTS } from "./webhooks.js";
41
42
  import { DEFAULT_SCHEME, SCHEME_NAMES, SIGNATURE_SCHEMES } from "./trigger-signatures.js";
42
43
  import { HOOK_BODY_MS } from "./push-handler.js";
43
- import { CATCHUP_WINDOW_S, DUE_KEPT, MAX_ACTIVE_RUNS, SEEN_KEPT, FAILURES_TO_PAUSE, FIRING_STATUSES, PAYLOAD_KEPT_BYTES, HISTORY_KEPT, MAX_EVERY_S, SOURCE_KINDS, TARGET_KINDS } from "./triggers.js";
44
+ import { CATCHUP_WINDOW_S, DUE_KEPT, MAX_ACTIVE_RUNS, SEEN_KEPT, FAILURES_TO_PAUSE, FIRING_STATUSES, HISTORY_KEPT, MAX_EVERY_S, SOURCE_KINDS, TARGET_KINDS } from "./triggers.js";
44
45
  import { DEFINITION_FIELDS, EXTERNAL_NAME, MODEL_REF } from "./validate.js";
45
46
  import { CATEGORIES, COMPUTER_ENTRIES, GRADER_TYPES, MODEL_TIERS } from "./templates.js";
46
47
  import { MCP_SOURCES } from "./mcp-sources.js";
@@ -208,7 +209,7 @@ const EMAIL_ACCOUNT_BODY = {
208
209
  preset: { enum: ["gmail", "fastmail", "icloud", "other", "outlook"], default: "other", description: "gmail, fastmail and icloud fill imap and smtp; other takes both. outlook is refused in words: Outlook needs Microsoft sign-in, which this server doesn't do yet." },
209
210
  address: { type: "string", minLength: 1, maxLength: 320, description: "The address it sends as." },
210
211
  user: { type: "string", minLength: 1, maxLength: 320, description: "Who signs in. Default: the address." },
211
- password: { type: "string", minLength: 1, maxLength: 320, "x-agentrun-secret": true, description: "The account's app password (Gmail, iCloud and Fastmail each make one for an app): sealed, never answered or shown again. A test of the saved account may leave it out." },
212
+ password: { type: "string", minLength: 1, maxLength: PASSWORD_MAX, "x-agentrun-secret": true, description: "The account's app password (Gmail, iCloud and Fastmail each make one for an app): sealed, never answered or shown again. A test of the saved account may leave it out." },
212
213
  imap: MAIL_ENDPOINT,
213
214
  smtp: MAIL_ENDPOINT,
214
215
  },
@@ -1231,15 +1232,16 @@ const LEARNING_COST = closed({
1231
1232
  workflows: { type: "array", items: closed({
1232
1233
  workflow: { type: "string", description: "The learned workflow, by name." },
1233
1234
  digest: { type: "string", description: "The version of it its newest run served first ran: the first 16 hex characters of its workflow.json's sha256. A new version starts its count from zero." },
1234
- state: { enum: ["serving", "needs_revision"], description: `\`serving\`: it runs first on every run it fits. \`needs_revision\`: over its last ${COST_WINDOW} runs served at this version, its mean all-in cost (\`served\`) is at or over the agent's cost by hand (\`by_hand\`), so it no longer runs first; each run it would have served asks the agent for a new version, which runs first again.` },
1235
- served: COST_MEAN(`The mean all-in cost (\`total_usd\`: the run's own cost, the workflow's nodes and, after a handback, the agent's work, with every learning round booked for it) of its newest ${COST_WINDOW} runs served first at this version whose all-in cost is known; null when none is.`),
1235
+ state: { enum: ["serving", "needs_revision"], description: `\`serving\`: it runs first on every run it fits. \`needs_revision\`: over its newest ${COST_WINDOW} priced runs served at this version, its mean all-in cost (\`served\`) is at or over the agent's cost by hand (\`by_hand\`), so it no longer runs first; each run it would have served asks the agent for a new version, which runs first again.` },
1236
+ served: COST_MEAN(`The mean all-in cost (\`total_usd\`: the run's own cost, the workflow's nodes and, after a handback, the agent's work, with every learning round booked for it) of its newest ${COST_WINDOW} runs served first at this version whose all-in cost is known (a window, never how many runs it served: \`runs\` stops at ${COST_WINDOW}); null when none is.`),
1236
1237
  by_hand: COST_MEAN("The agent's cost by hand: the mean all-in cost (`total_usd`) of its runs on the `agent` path whose all-in cost is known or, with none, of its runs before the workflow first ran; null when there are none: the workflow then keeps serving."),
1237
1238
  unknown_cost_runs: { ...COUNT, description: "How many runs `served` and `by_hand` would have read have no known all-in cost: left out of their means, never read as 0." },
1238
1239
  }), description: "Each learned workflow that has run first on one of these runs, at its newest version, and where it stands against the money rule: it runs first while its cost a run stays under the agent's by hand. It does not catch a workflow that answers wrong with confidence." },
1239
1240
  summary: closed({
1240
1241
  takeover: { oneOf: [closed({ n: { type: "integer", minimum: 1 }, run_id: { type: "string" } }), { type: "null" }], description: "The first run a learned workflow finished alone (`path` `workflow`); null before one." },
1241
1242
  before: COST_MEAN("The mean of the job's own cost (`cost_usd`, learning left out) over the `agent` runs before the takeover (all of them when there is none yet) whose cost is known exactly (never `cost_upper_bound`); null when there is none."),
1242
- after: COST_MEAN("The mean all-in cost (`total_usd`, learning included) of every run from the takeover on whose total is known exactly (never `cost_upper_bound`); null before a takeover."),
1243
+ after: COST_MEAN("The mean all-in cost (`total_usd`, learning included) of the runs a learned workflow served from the takeover on (`path` `workflow` or `handed_back`; never a `replay`, which checks a revision and serves no one, nor an `agent` run) whose total is known exactly (never `cost_upper_bound`); null when there is none, and `after_state` says why."),
1244
+ after_state: { enum: [...AFTER_STATES], description: "Why `after` is what it is: `priced`, it is the mean of the served runs whose cost is known; `no_served_runs`, no learned workflow has taken over yet (replays are not served runs), so there is no after side; `cost_unknown`, runs were served but no served run's all-in cost is known exactly. A reader says these in words, never as a figure." },
1243
1245
  times_cheaper: { type: ["number", "null"], minimum: 0, description: `\`before\` over \`after\`, to a tenth, once \`after\` counts ${CHEAPER_AFTER_RUNS} runs or more and \`withheld\` is null; else null. Learning is left out of the before side and kept in the after side, so it never flatters learning.` },
1244
1246
  hand_backs: { oneOf: [closed({ ran: COUNT, back: COUNT }), { type: "null" }], description: "Of the runs a learned workflow ran first on from the takeover on (`path` `workflow` or `handed_back`), how many (`ran`) and how many it handed back to the agent (`back`); null before a takeover." },
1245
1247
  withheld: { oneOf: [closed({ unknown: { ...COUNT, description: "Runs whose cost is unknown." }, at_most: { ...COUNT, description: "Runs whose cost is known only as at most (`cost_upper_bound`)." } }), { type: "null" }], description: "After a takeover, the runs `before` and `after` would read whose cost is not known exactly: while any is, `times_cheaper` is null, so the ratio is never over a partial set. Null when every one is known, and before a takeover." },
@@ -1292,7 +1294,7 @@ const PROMOTION = closedWith({
1292
1294
  workflow: { type: "string" },
1293
1295
  digest: { type: "string", description: "The revision the record decides: the first 16 hex of its workflow.json's sha256." },
1294
1296
  at: DATE_TIME,
1295
- decided: { enum: ["promote", "hold", "replaying", "superseded"], description: "`promote`: it serves first on the runs it fits. `hold`: it does not; the champion (the revision promoted last) takes them, else the agent, until a new revision. `replaying`: its replay runs are at work; the champion serves meanwhile, else the agent. `superseded`: a newer revision (`superseded_by`) came while it replayed; one revision replays at a time, so its replay runs were cancelled (what they spent stays on them) and it never serves." },
1297
+ decided: { enum: ["promote", "hold", "replaying", "queued", "skipped"], description: "`promote`: it serves first on the runs it fits. `hold`: it does not; the champion (the revision promoted last) takes them, else the agent, until a new revision. `replaying`: its replay runs are at work; the champion serves meanwhile, else the agent. `queued`: it came while another revision replayed; one revision replays at a time and a running replay finishes, so it replays next, at the first run's claim once that replay is decided (the finished replay's verdict stands). `skipped`: a newer revision (`skipped_by`) came while it was queued; only the newest queued revision replays next, so it never replayed and never serves." },
1296
1298
  source: { enum: ["replay", "dry_run", "refit", "graded"], description: `\`replay\`: decided on a replay of it on the agent's last ${REPLAY_INPUTS} graded inputs it fits, or those its owner asked for (POST .../revisions/{digest}/replays). \`dry_run\`: fewer than ${REPLAY_MIN} graded inputs, promoted on its passing dry run. \`refit\`: a threshold refit wrote this revision, decided on the rows it was fit on. \`graded\`: the graded runs it served fell more than ${PROMOTE_BAR * 100} points under the agent's accuracy by hand.` },
1297
1299
  reason: { type: "string", description: "Why, in words, with the numbers." },
1298
1300
  }, {
@@ -1305,7 +1307,7 @@ const PROMOTION = closedWith({
1305
1307
  since: { ...COUNT, description: "On a replay's promote: the place among the agent's grades of the newest grade when this promotion began, which names it. Its checks count only the graded runs this promotion served, so a revision promoted again after a hold is never held again on the grades it was held on, nor on a run served before it and graded after." },
1306
1308
  held_on: { type: "array", items: { type: "string" }, description: `The runs it served that were graded wrong when its graded runs held it (newest first, at most ${REPLAY_INPUTS}), kept on that hold, on its next replay (those still graded that it fits, each among its inputs) and on that replay's hold. A replay promotes it only once it answered or handed back on each of them, and is within the bar on them alone (gains on other inputs never pay for repeating their wrong answers).` },
1307
1309
  commit: { type: "string", description: "The learning commit whose workflow.json is this revision's text: what it serves from as the champion while a newer revision replays." },
1308
- superseded_by: { type: "string", description: "A superseded revision's newer one: the digest whose replay replaced its own." },
1310
+ skipped_by: { type: "string", description: "A skipped revision's newer one: the digest that came while it was queued, and replays in its place." },
1309
1311
  });
1310
1312
  const SERVING = closed({
1311
1313
  workflow: { type: "string" },
@@ -1372,7 +1374,7 @@ const LEARNING_EXTENSIONS = {
1372
1374
  from_notes: { type: "array", items: { type: "string" }, description: "The notes it absorbed, by file name (each marked \`compiled_into\` once it merged)." },
1373
1375
  attempts: { type: "array", items: closed({ ok: { type: "boolean" }, errors: { type: "array", items: { type: "string" } } }), description: "Each attempt in order: whether its checks and dry run passed, and what they refused." },
1374
1376
  digest: { ...NULL_STRING, description: "The kept draft's digest; null when none passed." },
1375
- promoted: { type: ["boolean", "null"], description: "Whether that revision was promoted, as its records say now: true promoted (or a refit of it was), false held or superseded, null undecided or none passed. It may no longer serve: the promotions read's \`serving\` says which revision does." },
1377
+ promoted: { type: ["boolean", "null"], description: "Whether that revision was promoted, as its records say now: true promoted (or a refit of it was), false held or skipped, null queued, undecided or none passed. It may no longer serve: the promotions read's \`serving\` says which revision does." },
1376
1378
  draft_errors: { type: "array", items: { type: "string" }, description: "A draft that never passed: its last errors. Empty when one passed." },
1377
1379
  withheld: { type: "boolean", description: "The run's handed values can't be read here: its error texts are left out (the counts stand)." },
1378
1380
  }), description: "Newest first." },
@@ -1397,7 +1399,7 @@ const LEARNING_EXTENSIONS = {
1397
1399
  "/agents/{agent_id}/learning/promotions": {
1398
1400
  get: {
1399
1401
  operationId: "listLearningPromotions", tags: ["agents"], summary: "Each learned workflow revision's promotion: its replay's numbers and whether it serves",
1400
- description: "Every promotion record of the agent's learned workflows, oldest first: a new revision's replay on the agent's graded inputs (replaying, then promoted or held, with its numbers), one promoted on its dry run, a threshold refit's new revision, a serving revision held on its graded runs. A revision serves only once its replay promotes it; the champion (the revision promoted last) keeps serving while a newer one replays or after it is held, and `serving` says which digest serves and which replays. Aggregates only: no input or answer. What the console's Learning tab marks on its cost chart.",
1402
+ description: "Every promotion record of the agent's learned workflows, oldest first: a new revision's replay on the agent's graded inputs (replaying, then promoted or held, with its numbers), one promoted on its dry run, a threshold refit's new revision, a serving revision held on its graded runs. A revision serves only once its replay promotes it; the champion (the revision promoted last) keeps serving while a newer one replays or after it is held, and `serving` says which digest serves and which replays. One revision replays at a time and a running replay finishes: a newer one that comes meanwhile is queued, and replays next; one queued before it is skipped, its record naming the newer one. Aggregates only: no input or answer. What the console's Learning tab marks on its cost chart.",
1401
1403
  parameters: [AGENT_ID],
1402
1404
  responses: { "200": json(closed({ agent: { type: "string" }, promotions: { type: "array", items: PROMOTION }, serving: { type: "array", items: SERVING, description: "Each learned workflow with a record: the revision that serves and the one replaying." } }), "The agent's promotion records, and what serves."), ...errors(401, 403, 404, 429, 500) },
1403
1405
  "x-agentrun-serve": "promotion/1",
@@ -1562,7 +1564,7 @@ const LEARNING_EXTENSIONS = {
1562
1564
  "/agents/{agent_id}/learning/workflows/{name}/revisions/{digest}/replays": {
1563
1565
  post: {
1564
1566
  operationId: "startLearnedWorkflowReplay", tags: ["agents"], summary: "Replay a learned workflow's revision on the agent's graded inputs: re-check a held revision, or an edited one",
1565
- description: `Starts the replay a run's claim starts for a new revision (GET /agents/{agent_id}/learning/promotions explains them): one replay run of the agent per graded input, each the workflow alone at workflow cost, its reads live and its writes answered only from the graded run's recording, never sent. \`inputs\`: \`held\` (the default), the graded runs the revision's last replay or hold was decided on, or, with none, the agent's newest ${REPLAY_INPUTS}; a number from 1 to ${GRADES_KEPT}, that many of the agent's newest graded runs. Either way, a revision held on runs it served that were graded wrong is replayed on those too (its hold's \`held_on\`), and promoted only once it answered or handed back on each and is within the bar on them alone. Each is a graded run the agent answered (path \`agent\` or \`handed_back\`) or the workflow answered alone (\`workflow\`), with no uploaded file, whose input the revision takes. Its record is a replay's (\`source: replay\`, \`replaying\` then promoted or held, with its numbers, as a claim-started one is), decided once every replay run ended: at a run's claim that lays the revision, or at its read (GET .../replays/{replay_id}). A promoted revision serves first, and a held one may be refit, as after any replay. Its replay runs are handed no per-run MCP credentials. The revision is \`digest\`, the first 16 hex of its workflow.json's sha256: one a promotion record names (read at its commit), or the one on the learning folder's main. 404 for an unknown agent, workflow or revision. 409 \`replay_conflict\` when it is replaying already, another revision of the workflow replays (one at a time), it is promoted (it serves already), fewer than ${REPLAY_MIN} of the graded inputs asked for fit it (a replay on fewer could only hold it, so nothing is reserved and no run starts), or the agent's runs take no learned workflow here. 409 \`agent_not_runnable\` for an archived agent, as a run's start is: nothing is reserved. 400 for any other \`inputs\`. Only the owner, signed in with their password, starts one: the console's password session alone, from this server's own origin with a JSON body; every token is 403 \`forbidden\`.`,
1567
+ description: `Starts the replay a run's claim starts for a new revision (GET /agents/{agent_id}/learning/promotions explains them): one replay run of the agent per graded input, each the workflow alone at workflow cost, its reads live and its writes answered only from the graded run's recording, never sent. \`inputs\`: \`held\` (the default), the graded runs the revision's last replay or hold was decided on, or, with none, the agent's newest ${REPLAY_INPUTS}; a number from 1 to ${GRADES_KEPT}, that many of the agent's newest graded runs. Either way, a revision held on runs it served that were graded wrong is replayed on those too (its hold's \`held_on\`), and promoted only once it answered or handed back on each and is within the bar on them alone. Each is a graded run the agent answered (path \`agent\` or \`handed_back\`) or the workflow answered alone (\`workflow\`), with no uploaded file, whose input the revision takes. Its record is a replay's (\`source: replay\`, \`replaying\` then promoted or held, with its numbers, as a claim-started one is), decided once every replay run ended: at a run's claim that lays the revision, or at its read (GET .../replays/{replay_id}). A promoted revision serves first, and a held one may be refit, as after any replay. Its replay runs are handed no per-run MCP credentials. The revision is \`digest\`, the first 16 hex of its workflow.json's sha256: one a promotion record names (read at its commit), or the one on the learning folder's main. 404 for an unknown agent, workflow or revision. 409 \`replay_conflict\` when it is replaying already, another revision of the workflow replays (one at a time, and a running replay finishes: a revision queued behind it, \`queued\` in the promotions, replays at the first run's claim once it ends, no POST needed), it is promoted (it serves already), fewer than ${REPLAY_MIN} of the graded inputs asked for fit it (a replay on fewer could only hold it, so nothing is reserved and no run starts), or the agent's runs take no learned workflow here. 409 \`agent_not_runnable\` for an archived agent, as a run's start is: nothing is reserved. 400 for any other \`inputs\`. Only the owner, signed in with their password, starts one: the console's password session alone, from this server's own origin with a JSON body; every token is 403 \`forbidden\`.`,
1566
1568
  security: [{ ownerSession: [] }],
1567
1569
  parameters: [AGENT_ID, { name: "name", in: "path", required: true, description: "The learned workflow's folder name, `workflows/<name>/`.", schema: { type: "string" } }, { name: "digest", in: "path", required: true, description: "The revision: the first 16 hex of its workflow.json's sha256.", schema: { type: "string" } }],
1568
1570
  requestBody: { required: true, content: { "application/json": { schema: { type: "object", additionalProperties: false, properties: { inputs: { oneOf: [{ const: "held" }, { type: "integer", minimum: 1, maximum: GRADES_KEPT }], default: "held", description: "`held`: the graded inputs it was last decided on; a number: the agent's newest that many." } } } } } },
@@ -1572,7 +1574,7 @@ const LEARNING_EXTENSIONS = {
1572
1574
  "/agents/{agent_id}/learning/workflows/{name}/revisions/{digest}/replays/{replay_id}": {
1573
1575
  get: {
1574
1576
  operationId: "getLearnedWorkflowReplay", tags: ["agents"], summary: "Read a replay's result: running, done or failed, its counts and its score (start one from the console's Learning tab)",
1575
- description: "A replay a run's claim or the agent's owner started. This read decides it as a claim would, once every replay run ended or its deadline passed (promoted within 2 points of the agent, else held, then one refit), so a replay of a revision no run lays is decided too. It never starts a run: a replay whose launch was interrupted (the server stopped before creating its runs) reads `running` until its deadline. `status`: `running` while it replays, `done` once promoted or held (`decided`, with its `reason`), `failed` when a newer revision superseded it and its runs were cancelled, or when none of its runs started by its deadline (held, nothing replayed; its `reason` says so and to start it again with POST). `outcomes` counts its replay runs so far (once decided, those it was decided on, as its promotion record keeps them): `answered` alone, `handed_back` (the agent's recorded answer), `failed` (no evidence either way), `skipped_write` (a write its recording does not have). `score` is the decided record's, else the runs ended so far at the revision's serving thresholds. Aggregates only, its words cut as the promotions list's are.",
1577
+ description: "A replay a run's claim or the agent's owner started. This read decides it as a claim would, once every replay run ended or its deadline passed (promoted within 2 points of the agent, else held, then one refit), so a replay of a revision no run lays is decided too. It never starts a run: a replay whose launch was interrupted (the server stopped before creating its runs) reads `running` until its deadline. `status`: `running` while it replays, `done` once promoted or held (`decided`, with its `reason`; a newer revision that came meanwhile queued behind it, never cancelling it), `failed` when none of its runs started by its deadline (held, nothing replayed; its `reason` says so and to start it again with POST). `outcomes` counts its replay runs so far (once decided, those it was decided on, as its promotion record keeps them): `answered` alone, `handed_back` (the agent's recorded answer), `failed` (no evidence either way), `skipped_write` (a write its recording does not have). `score` is the decided record's, else the runs ended so far at the revision's serving thresholds. Aggregates only, its words cut as the promotions list's are.",
1576
1578
  parameters: [AGENT_ID, { name: "name", in: "path", required: true, description: "The learned workflow's folder name.", schema: { type: "string" } }, { name: "digest", in: "path", required: true, description: "The revision's digest.", schema: { type: "string" } }, { name: "replay_id", in: "path", required: true, description: "The replay's id: POST .../replays answered it, and its promotion records carry it (`replay_id`).", schema: { type: "string" } }],
1577
1579
  responses: { "200": json(closedWith({
1578
1580
  replay: { type: "string" }, workflow: { type: "string" }, digest: { type: "string" },
@@ -1582,7 +1584,7 @@ const LEARNING_EXTENSIONS = {
1582
1584
  score: closed(REPLAY_SCORE),
1583
1585
  cost_usd: { type: ["number", "null"], minimum: 0, description: "What its replay runs cost together; null while one's cost is unknown (one not created or not ended yet), never a part of it." },
1584
1586
  }, {
1585
- decided: { enum: ["promote", "hold", "superseded"], description: "Once it is not running: what it decided." },
1587
+ decided: { enum: ["promote", "hold"], description: "Once it is not running: what it decided." },
1586
1588
  reason: { type: "string", description: "Why, in words, with the numbers." },
1587
1589
  }), "The replay."), ...errors(401, 403, 404, 429, 500) }, ...MARK,
1588
1590
  },
@@ -2699,7 +2701,7 @@ const PENDING = {
2699
2701
  tool: { type: "string" },
2700
2702
  call_id: { type: "string", description: "The call's id, as its tool_result row's tool_use_id." },
2701
2703
  args_preview: { type: "string", maxLength: ARGS_PREVIEW_BYTES, description: `The call's arguments as JSON, cut at ${ARGS_PREVIEW_BYTES} bytes.` },
2702
- summary: { type: "string", description: "What the call does, in words, where its tool says: email_send's \"Send email to <to>, cc <cc>: <subject>\", a blank line, then the text's first 300 characters; email_file's \"File <message_id> under <to>\". Redacted as args_preview is.", "x-agentrun-serve": "email/1" },
2704
+ summary: { type: "string", description: `What the call does, in words, where its tool says: email_send's "Send email to <to>, cc <cc>: <subject>", a blank line, then the text's first 300 characters; email_file's "File <message_id> under <to>". When the call asks though its setting allows it (its tool's own ask: email_send past ${SEND_CAP} sends in a run, or to anyone outside the thread of the email that started the run), it ends "Asks because <why>.", and args_preview carries the why as asked_because. Redacted as args_preview is.`, "x-agentrun-serve": "email/1" },
2703
2705
  requested_at: { type: "string", format: "date-time" },
2704
2706
  price: { oneOf: [{ type: "number", minimum: 0 }, { const: "not metered" }, { const: "priced by usage" }], description: "What the call costs, in USD, where its tool declares a price; \"not metered\" for an unpriced MCP call; \"priced by usage\" for a monid endpoint priced by usage: monid estimates its cost only when the call runs, after the allow (the call is held at that estimate, and a charge above it stops the run), so nothing is sent to monid while it waits. Absent when the tool names none." },
2705
2707
  destructive: { const: true, description: `Present on an MCP tool's call that is destructive by the annotations the run pinned (destructiveHint true, readOnlyHint not true), and on a call whose allow lets such a tool run (a learned workflow's, its card naming the tool in \`mcp_calls\`): its allow needs \`confirm_destructive: true\`. Absent on every other call.`, "x-agentrun-serve": "allowreads/1" },
@@ -15,11 +15,14 @@ export type ThinkingLevelMap = NonNullable<Model<Api>["thinkingLevelMap"]>;
15
15
  /** The catalog's entry for a session's model, as its route carries it (model-route.ts `like`): its reasoning, price,
16
16
  * window and ceiling, the effort each thinking level asks for, and what it takes as input with its input limits. */
17
17
  /** `developerRole`: on Responses, the instructions go as a developer message, not a system one (a ChatGPT plan's route,
18
- * which takes no system message). */
18
+ * which takes no system message). `adaptiveThinking`: on Messages, the session asks its thinking level as an adaptive
19
+ * effort (pi-ai's `forceAdaptiveThinking`), never a token budget: the route's own translation reads that level back
20
+ * (model-route.ts LEVELS_BY_NAME). */
19
21
  export type SessionLike = Partial<Pick<Model<Api>, "reasoning" | "cost" | "contextWindow" | "maxTokens" | "thinkingLevelMap">> & {
20
22
  readonly input?: readonly string[];
21
23
  readonly inputLimits?: unknown;
22
24
  readonly developerRole?: boolean;
25
+ readonly adaptiveThinking?: boolean;
23
26
  };
24
27
  /** The one model of a session's route as pi-ai lists it, for the port's requests and for pi's own (runner adapters/pi.ts):
25
28
  * `model` in `api` at the proxy `url` (sessionBaseUrl), under `provider`, with `headers` (the session key as the proxy
@@ -32,6 +32,7 @@ export function sessionModel(api, url, model, options) {
32
32
  cost: like?.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
33
33
  contextWindow: like?.contextWindow ?? 200_000, maxTokens: like?.maxTokens ?? 32_000,
34
34
  ...(api === "openai-responses" ? { compat: { supportsDeveloperRole: like?.developerRole === true } } : {}),
35
+ ...(api === "anthropic-messages" && like?.adaptiveThinking ? { compat: { forceAdaptiveThinking: true } } : {}),
35
36
  };
36
37
  }
37
38
  /** pi-ai's provider for the one model of a session's proxy on its wire: pi-ai's client of `api` (Anthropic's Messages or
@@ -9,6 +9,9 @@ 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";
12
15
  /** How a person's deny opens (refusedText). */
13
16
  export declare const USER_DENIED_OPENING = "The user denied this ";
14
17
  /** What a read-only bash policy's refusal says after the tool's name (bash-read-only.ts readOnlyText). */
@@ -11,6 +11,9 @@ 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";
14
17
  /** How a person's deny opens (refusedText). */
15
18
  export const USER_DENIED_OPENING = "The user denied this ";
16
19
  /** What a read-only bash policy's refusal says after the tool's name (bash-read-only.ts readOnlyText). */