@stigmer/cli 0.1.2 → 0.2.0

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 (230) hide show
  1. package/auth/browser.d.ts +7 -0
  2. package/auth/browser.d.ts.map +1 -0
  3. package/auth/browser.js +49 -0
  4. package/auth/browser.js.map +1 -0
  5. package/auth/callback-server.d.ts +8 -3
  6. package/auth/callback-server.d.ts.map +1 -1
  7. package/auth/callback-server.js +53 -19
  8. package/auth/callback-server.js.map +1 -1
  9. package/auth/device.d.ts +23 -0
  10. package/auth/device.d.ts.map +1 -0
  11. package/auth/device.js +119 -0
  12. package/auth/device.js.map +1 -0
  13. package/auth/index.d.ts +4 -3
  14. package/auth/index.d.ts.map +1 -1
  15. package/auth/index.js +4 -3
  16. package/auth/index.js.map +1 -1
  17. package/auth/login.d.ts +49 -1
  18. package/auth/login.d.ts.map +1 -1
  19. package/auth/login.js +161 -51
  20. package/auth/login.js.map +1 -1
  21. package/auth/oidc.d.ts +67 -0
  22. package/auth/oidc.d.ts.map +1 -0
  23. package/auth/oidc.js +213 -0
  24. package/auth/oidc.js.map +1 -0
  25. package/auth/token.d.ts +17 -23
  26. package/auth/token.d.ts.map +1 -1
  27. package/auth/token.js +51 -60
  28. package/auth/token.js.map +1 -1
  29. package/client/client.d.ts.map +1 -1
  30. package/client/client.js +4 -1
  31. package/client/client.js.map +1 -1
  32. package/client/current-organization.d.ts +5 -0
  33. package/client/current-organization.d.ts.map +1 -0
  34. package/client/current-organization.js +15 -0
  35. package/client/current-organization.js.map +1 -0
  36. package/commands/apikey/index.d.ts +16 -1
  37. package/commands/apikey/index.d.ts.map +1 -1
  38. package/commands/apikey/index.js +81 -0
  39. package/commands/apikey/index.js.map +1 -1
  40. package/commands/app-handover.d.ts +17 -0
  41. package/commands/app-handover.d.ts.map +1 -0
  42. package/commands/app-handover.js +155 -0
  43. package/commands/app-handover.js.map +1 -0
  44. package/commands/auth/index.d.ts.map +1 -1
  45. package/commands/auth/index.js +15 -9
  46. package/commands/auth/index.js.map +1 -1
  47. package/commands/auth/login.d.ts +7 -2
  48. package/commands/auth/login.d.ts.map +1 -1
  49. package/commands/auth/login.js +26 -23
  50. package/commands/auth/login.js.map +1 -1
  51. package/commands/auth/whoami.d.ts +5 -0
  52. package/commands/auth/whoami.d.ts.map +1 -1
  53. package/commands/auth/whoami.js +13 -1
  54. package/commands/auth/whoami.js.map +1 -1
  55. package/commands/coding-agent.d.ts +5 -0
  56. package/commands/coding-agent.d.ts.map +1 -0
  57. package/commands/coding-agent.js +30 -0
  58. package/commands/coding-agent.js.map +1 -0
  59. package/commands/config/backend.js +20 -6
  60. package/commands/config/backend.js.map +1 -1
  61. package/commands/embedding.d.ts +28 -0
  62. package/commands/embedding.d.ts.map +1 -0
  63. package/commands/embedding.js +170 -0
  64. package/commands/embedding.js.map +1 -0
  65. package/commands/example.d.ts +13 -0
  66. package/commands/example.d.ts.map +1 -0
  67. package/commands/example.js +126 -0
  68. package/commands/example.js.map +1 -0
  69. package/commands/help.d.ts.map +1 -1
  70. package/commands/help.js +16 -3
  71. package/commands/help.js.map +1 -1
  72. package/commands/load-test/burst.d.ts +64 -0
  73. package/commands/load-test/burst.d.ts.map +1 -0
  74. package/commands/load-test/burst.js +202 -0
  75. package/commands/load-test/burst.js.map +1 -0
  76. package/commands/load-test/index.d.ts +37 -0
  77. package/commands/load-test/index.d.ts.map +1 -0
  78. package/commands/load-test/index.js +154 -0
  79. package/commands/load-test/index.js.map +1 -0
  80. package/commands/load-test/report.d.ts +98 -0
  81. package/commands/load-test/report.d.ts.map +1 -0
  82. package/commands/load-test/report.js +210 -0
  83. package/commands/load-test/report.js.map +1 -0
  84. package/commands/mcp-server.d.ts +3 -3
  85. package/commands/mcp-server.d.ts.map +1 -1
  86. package/commands/mcp-server.js +4 -4
  87. package/commands/mcp-server.js.map +1 -1
  88. package/commands/setup.d.ts +17 -0
  89. package/commands/setup.d.ts.map +1 -1
  90. package/commands/setup.js +71 -1
  91. package/commands/setup.js.map +1 -1
  92. package/commands/up.d.ts.map +1 -1
  93. package/commands/up.js +67 -9
  94. package/commands/up.js.map +1 -1
  95. package/commands/usage.d.ts +15 -0
  96. package/commands/usage.d.ts.map +1 -1
  97. package/commands/usage.js +52 -2
  98. package/commands/usage.js.map +1 -1
  99. package/config/config.d.ts +14 -7
  100. package/config/config.d.ts.map +1 -1
  101. package/config/config.js +5 -1
  102. package/config/config.js.map +1 -1
  103. package/config/index.d.ts +1 -1
  104. package/config/index.d.ts.map +1 -1
  105. package/config/index.js +1 -1
  106. package/config/index.js.map +1 -1
  107. package/config/resolve.d.ts +1 -1
  108. package/config/resolve.d.ts.map +1 -1
  109. package/config/resolve.js +6 -3
  110. package/config/resolve.js.map +1 -1
  111. package/errors/classify.d.ts.map +1 -1
  112. package/errors/classify.js +10 -1
  113. package/errors/classify.js.map +1 -1
  114. package/errors/cli-exit-error.d.ts +3 -1
  115. package/errors/cli-exit-error.d.ts.map +1 -1
  116. package/errors/cli-exit-error.js +6 -2
  117. package/errors/cli-exit-error.js.map +1 -1
  118. package/errors/handle.d.ts +32 -5
  119. package/errors/handle.d.ts.map +1 -1
  120. package/errors/handle.js +67 -8
  121. package/errors/handle.js.map +1 -1
  122. package/local/daemon/components.d.ts +28 -1
  123. package/local/daemon/components.d.ts.map +1 -1
  124. package/local/daemon/components.js +64 -2
  125. package/local/daemon/components.js.map +1 -1
  126. package/local/daemon/host.d.ts.map +1 -1
  127. package/local/daemon/host.js +6 -2
  128. package/local/daemon/host.js.map +1 -1
  129. package/local/daemon/launch.d.ts +12 -4
  130. package/local/daemon/launch.d.ts.map +1 -1
  131. package/local/daemon/launch.js +49 -16
  132. package/local/daemon/launch.js.map +1 -1
  133. package/local/daemon/types.d.ts +9 -1
  134. package/local/daemon/types.d.ts.map +1 -1
  135. package/local/key-check.d.ts +20 -0
  136. package/local/key-check.d.ts.map +1 -0
  137. package/local/key-check.js +45 -0
  138. package/local/key-check.js.map +1 -0
  139. package/local/model-key-step.d.ts +63 -0
  140. package/local/model-key-step.d.ts.map +1 -0
  141. package/local/model-key-step.js +133 -0
  142. package/local/model-key-step.js.map +1 -0
  143. package/local/setup/prompt.d.ts.map +1 -1
  144. package/local/setup/prompt.js +22 -18
  145. package/local/setup/prompt.js.map +1 -1
  146. package/local/setup/wizard.d.ts +10 -0
  147. package/local/setup/wizard.d.ts.map +1 -1
  148. package/local/setup/wizard.js +40 -17
  149. package/local/setup/wizard.js.map +1 -1
  150. package/local/state/lock.d.ts.map +1 -1
  151. package/local/state/lock.js +123 -36
  152. package/local/state/lock.js.map +1 -1
  153. package/local/state/pidfile.d.ts +4 -0
  154. package/local/state/pidfile.d.ts.map +1 -1
  155. package/local/state/pidfile.js +12 -3
  156. package/local/state/pidfile.js.map +1 -1
  157. package/package.json +12 -9
  158. package/program.d.ts.map +1 -1
  159. package/program.js +10 -0
  160. package/program.js.map +1 -1
  161. package/resources/get.js +1 -1
  162. package/resources/get.js.map +1 -1
  163. package/resources/run/stream.d.ts.map +1 -1
  164. package/resources/run/stream.js +2 -1
  165. package/resources/run/stream.js.map +1 -1
  166. package/resources/stream/render-plaintext.d.ts +3 -0
  167. package/resources/stream/render-plaintext.d.ts.map +1 -1
  168. package/resources/stream/render-plaintext.js +20 -2
  169. package/resources/stream/render-plaintext.js.map +1 -1
  170. package/resources/usage.d.ts +8 -0
  171. package/resources/usage.d.ts.map +1 -1
  172. package/resources/usage.js +37 -0
  173. package/resources/usage.js.map +1 -1
  174. package/runtime.d.ts +9 -0
  175. package/runtime.d.ts.map +1 -1
  176. package/runtime.js +12 -1
  177. package/runtime.js.map +1 -1
  178. package/src/auth/browser.ts +57 -0
  179. package/src/auth/callback-server.ts +58 -22
  180. package/src/auth/device.ts +151 -0
  181. package/src/auth/index.ts +4 -3
  182. package/src/auth/login.ts +243 -67
  183. package/src/auth/oidc.ts +270 -0
  184. package/src/auth/token.ts +53 -90
  185. package/src/client/client.ts +4 -1
  186. package/src/client/current-organization.ts +27 -0
  187. package/src/commands/apikey/index.ts +85 -0
  188. package/src/commands/app-handover.ts +188 -0
  189. package/src/commands/auth/index.ts +16 -10
  190. package/src/commands/auth/login.ts +30 -23
  191. package/src/commands/auth/whoami.ts +16 -1
  192. package/src/commands/coding-agent.ts +33 -0
  193. package/src/commands/config/backend.ts +20 -6
  194. package/src/commands/embedding.ts +209 -0
  195. package/src/commands/example.ts +146 -0
  196. package/src/commands/help.ts +16 -3
  197. package/src/commands/load-test/burst.ts +291 -0
  198. package/src/commands/load-test/index.ts +183 -0
  199. package/src/commands/load-test/report.ts +302 -0
  200. package/src/commands/mcp-server.ts +5 -5
  201. package/src/commands/setup.ts +76 -2
  202. package/src/commands/up.ts +76 -10
  203. package/src/commands/usage.ts +55 -2
  204. package/src/config/config.ts +18 -8
  205. package/src/config/index.ts +1 -0
  206. package/src/config/resolve.ts +5 -3
  207. package/src/errors/classify.ts +10 -1
  208. package/src/errors/cli-exit-error.ts +6 -2
  209. package/src/errors/handle.ts +74 -8
  210. package/src/local/daemon/components.ts +66 -2
  211. package/src/local/daemon/host.ts +6 -2
  212. package/src/local/daemon/launch.ts +51 -16
  213. package/src/local/daemon/types.ts +9 -1
  214. package/src/local/key-check.ts +61 -0
  215. package/src/local/model-key-step.ts +164 -0
  216. package/src/local/setup/prompt.ts +22 -18
  217. package/src/local/setup/wizard.ts +45 -18
  218. package/src/local/state/lock.ts +116 -35
  219. package/src/local/state/pidfile.ts +14 -3
  220. package/src/program.ts +10 -0
  221. package/src/resources/get.ts +1 -1
  222. package/src/resources/run/stream.ts +2 -1
  223. package/src/resources/stream/render-plaintext.ts +20 -2
  224. package/src/resources/usage.ts +42 -0
  225. package/src/runtime.ts +17 -1
  226. package/auth/auth0.d.ts +0 -19
  227. package/auth/auth0.d.ts.map +0 -1
  228. package/auth/auth0.js +0 -39
  229. package/auth/auth0.js.map +0 -1
  230. package/src/auth/auth0.ts +0 -43
@@ -0,0 +1,302 @@
1
+ // What `stigmer load-test` reports: each run's start and ending, read apart,
2
+ // and the burst's summary, as a table with sentences or as one JSON (or
3
+ // YAML) document. Pure: the engine (burst.ts) hands in what it saw, and this
4
+ // module decides the words.
5
+ //
6
+ // A run's start is one of five words. `started` once the runner stamped
7
+ // `status.started_at`; `refused` when the create itself was answered with an
8
+ // error (its code and the server's own sentence, verbatim, so a plan's limit
9
+ // reads in the server's words without this tool knowing them); `wait ran out`
10
+ // when the run ended `no_sandbox_room`; `never started` for any other ending
11
+ // before a start; `still waiting` when the watch ended first.
12
+ //
13
+ // Two times per run, both from the server's side, never this machine's clock:
14
+ // - to start: `status.started_at` (the runner) minus
15
+ // `status.audit.spec_audit.created_at` (the server, when it accepted the
16
+ // message). A `started_at` written without milliseconds (it is a
17
+ // string; the create's stamp is a Timestamp), or a negative difference,
18
+ // is "unclear" and left out of the figures.
19
+ // - waited for a sandbox: the longest `awaiting_sandbox_for` the run's
20
+ // liveness carried. The server's own number counts from its first "no
21
+ // room" to the answer that found room, and liveness is sent on each "no
22
+ // room" answer, at most one backoff (15 s) apart, so the server's figure
23
+ // is at least this one and at most "to start": the report writes "≥".
24
+ // Liveness is the serving process's memory, so the report says a run
25
+ // "reported" a wait, and when none did it says so in its own line: a
26
+ // fleet that cannot run out of room never reads as one that held.
27
+ //
28
+ // Spend is the sum of the runs' own estimates
29
+ // (`status.streaming_usage.estimated_cost_usd`), the number each run's
30
+ // budget share is enforced against.
31
+
32
+ import { timestampMs } from "@bufbuild/protobuf/wkt";
33
+ import type { Run } from "@stigmer/apis/ai/stigmer/agentic/run/v1/api_pb";
34
+ import { formatElapsed, turnPhaseOf } from "@stigmer/client";
35
+ import { stringify } from "yaml";
36
+ import type { OutputFormat } from "../../output/index.js";
37
+ import { renderTable } from "../../output/index.js";
38
+
39
+ /** How a run's start went. */
40
+ export type StartOutcome = "started" | "refused" | "wait ran out" | "never started" | "still waiting";
41
+
42
+ /** The server's answer to a create it refused: its code word and its own sentence. */
43
+ export interface Refusal {
44
+ readonly code: string;
45
+ readonly message: string;
46
+ }
47
+
48
+ /** What the engine saw of one run. */
49
+ export interface RunSighting {
50
+ /** 1-based, in the order the creates were sent. */
51
+ readonly index: number;
52
+ /** The create's refusal; the run was never made. */
53
+ readonly refusal?: Refusal;
54
+ /** The run as read after the watch; absent when it was refused, or could not be read. */
55
+ readonly run?: Run;
56
+ readonly runId: string;
57
+ readonly sessionId: string;
58
+ /** The longest wait for a sandbox any liveness frame carried, in ms; undefined when none was reported. */
59
+ readonly waitedMs?: number;
60
+ }
61
+
62
+ /** One run's row of the report. */
63
+ export interface RunOutcome {
64
+ readonly index: number;
65
+ readonly runId: string;
66
+ readonly sessionId: string;
67
+ readonly start: StartOutcome;
68
+ /** Ms from the message accepted to the agent starting; null when not started or unclear. */
69
+ readonly toStartMs: number | null;
70
+ /** Whether a started run's stamps were too coarse, or out of order, to time it. */
71
+ readonly toStartUnclear: boolean;
72
+ /** The longest wait for a sandbox the run reported, in ms (at least); null when it reported none. */
73
+ readonly waitedForRoomMs: number | null;
74
+ /** How a started run ended, or what ended one that never started; "" while it works or when refused. */
75
+ readonly ending: string;
76
+ readonly stopCause: string;
77
+ readonly stopKind: string;
78
+ /** The run's own spend estimate in USD; null when it has none. */
79
+ readonly costUsd: number | null;
80
+ readonly refusal: Refusal | null;
81
+ }
82
+
83
+ /** Median, 90th percentile (nearest rank) and longest of a set of times, in ms. */
84
+ export interface Figures {
85
+ readonly median: number | null;
86
+ readonly p90: number | null;
87
+ readonly longest: number | null;
88
+ }
89
+
90
+ /** The whole report, as `-o json` prints it. */
91
+ export interface LoadTestReport {
92
+ readonly server: string;
93
+ readonly org: string;
94
+ readonly agent: string;
95
+ readonly requested: number;
96
+ readonly budgetUsd: number;
97
+ readonly shareUsd: number;
98
+ readonly counts: {
99
+ readonly started: number;
100
+ readonly refused: number;
101
+ readonly waitRanOut: number;
102
+ readonly neverStarted: number;
103
+ readonly stillWaiting: number;
104
+ };
105
+ readonly toStartMs: Figures & { readonly unclear: number };
106
+ readonly waitedForRoomMs: Figures & { readonly runs: number };
107
+ readonly anyWaitReported: boolean;
108
+ readonly refusals: ReadonlyArray<Refusal & { readonly count: number }>;
109
+ readonly spentUsd: number;
110
+ readonly runs: readonly RunOutcome[];
111
+ /** What cleanup could not delete, as "<kind> <id>". */
112
+ readonly leftBehind: readonly string[];
113
+ }
114
+
115
+ /** The burst's frame: where it ran and what it was allowed to spend. */
116
+ export interface BurstFrame {
117
+ readonly server: string;
118
+ readonly org: string;
119
+ readonly agent: string;
120
+ readonly requested: number;
121
+ readonly budgetUsd: number;
122
+ readonly shareUsd: number;
123
+ }
124
+
125
+ const NO_SANDBOX_ROOM = "no_sandbox_room";
126
+ const COST_CAP = "cost_cap";
127
+
128
+ // A stamp's fraction of a second: three digits or more is millisecond-precise.
129
+ const FRACTION = /\.(\d+)/;
130
+
131
+ /** One run's row, from what the engine saw. */
132
+ export function outcomeOf(sighting: RunSighting): RunOutcome {
133
+ const base = {
134
+ index: sighting.index,
135
+ runId: sighting.runId,
136
+ sessionId: sighting.sessionId,
137
+ waitedForRoomMs: sighting.waitedMs ?? null,
138
+ };
139
+ if (sighting.refusal !== undefined) {
140
+ return { ...base, start: "refused", toStartMs: null, toStartUnclear: false, ending: "", stopCause: "", stopKind: "", costUsd: null, refusal: sighting.refusal };
141
+ }
142
+ const status = sighting.run?.status;
143
+ const stopCause = status?.stopCause ?? "";
144
+ const stopKind = status?.stopDetail?.kind ?? "";
145
+ const phase = turnPhaseOf(status?.phase ?? 0);
146
+ const ended = phase === "completed" || phase === "failed" || phase === "cancelled" || phase === "terminated";
147
+ const cost = status?.streamingUsage?.estimatedCostUsd;
148
+ const common = { ...base, stopCause, stopKind, costUsd: cost === undefined ? null : cost, refusal: null };
149
+ const startedAt = status?.startedAt ?? "";
150
+ if (startedAt !== "") {
151
+ const toStart = toStartOf(sighting.run, startedAt);
152
+ return {
153
+ ...common,
154
+ start: "started",
155
+ toStartMs: toStart,
156
+ toStartUnclear: toStart === null,
157
+ ending: ended ? endingOf(phase, stopCause, stopKind) : "",
158
+ };
159
+ }
160
+ const notStarted = { ...common, toStartMs: null, toStartUnclear: false };
161
+ if (stopKind === NO_SANDBOX_ROOM) return { ...notStarted, start: "wait ran out", ending: "" };
162
+ if (ended) return { ...notStarted, start: "never started", ending: endingOf(phase, stopCause, stopKind) };
163
+ return { ...notStarted, start: "still waiting", ending: "" };
164
+ }
165
+
166
+ /** The report of a burst: every run's row, the counts, the figures and the spend. */
167
+ export function buildReport(frame: BurstFrame, sightings: readonly RunSighting[], leftBehind: readonly string[]): LoadTestReport {
168
+ const runs = sightings.map(outcomeOf);
169
+ const count = (start: StartOutcome): number => runs.filter((run) => run.start === start).length;
170
+ const started = runs.filter((run) => run.start === "started");
171
+ const waits = runs.flatMap((run) => (run.waitedForRoomMs === null ? [] : [run.waitedForRoomMs]));
172
+ return {
173
+ ...frame,
174
+ counts: {
175
+ started: started.length,
176
+ refused: count("refused"),
177
+ waitRanOut: count("wait ran out"),
178
+ neverStarted: count("never started"),
179
+ stillWaiting: count("still waiting"),
180
+ },
181
+ toStartMs: {
182
+ ...figuresOf(started.flatMap((run) => (run.toStartMs === null ? [] : [run.toStartMs]))),
183
+ unclear: started.filter((run) => run.toStartUnclear).length,
184
+ },
185
+ waitedForRoomMs: { ...figuresOf(waits), runs: waits.length },
186
+ anyWaitReported: waits.length > 0,
187
+ refusals: groupRefusals(runs),
188
+ spentUsd: runs.reduce((sum, run) => sum + (run.costUsd ?? 0), 0),
189
+ runs,
190
+ leftBehind,
191
+ };
192
+ }
193
+
194
+ /** The report in the asked format: table and summary, or the JSON or YAML document. */
195
+ export function renderReport(report: LoadTestReport, format: OutputFormat): string {
196
+ if (format === "json") return `${JSON.stringify(report, null, 2)}\n`;
197
+ if (format === "yaml") return stringify(report);
198
+ return `${renderRuns(report.runs)}\n${summaryLines(report).join("\n")}\n`;
199
+ }
200
+
201
+ /** The burst's summary sentences, in the order the report prints them. */
202
+ export function summaryLines(report: LoadTestReport): string[] {
203
+ const lines = [countsLine(report)];
204
+ const toStart = report.toStartMs;
205
+ if (toStart.median !== null) {
206
+ lines.push(`To start: ${figuresText(toStart)}.`);
207
+ }
208
+ if (toStart.unclear > 0) {
209
+ lines.push(`To start was unclear for ${plural(toStart.unclear, "run")}: a stamp was too coarse to time it.`);
210
+ }
211
+ if (report.anyWaitReported) {
212
+ const waited = report.waitedForRoomMs;
213
+ lines.push(`Waited for a sandbox, ${plural(waited.runs, "run")}: ${figuresText(waited)} (at least).`);
214
+ } else {
215
+ lines.push("No run reported waiting for a sandbox during the burst.");
216
+ }
217
+ for (const refusal of report.refusals) {
218
+ lines.push(`Refused ${refusal.count}: ${refusal.code}, "${refusal.message}"`);
219
+ }
220
+ lines.push(`Spent about ${usd(report.spentUsd)}, by the runs' own estimates.`);
221
+ return lines;
222
+ }
223
+
224
+ /** A dollar amount: cents, or four places below a cent so a tiny spend is not "$0.00". */
225
+ export function usd(amount: number): string {
226
+ return amount > 0 && amount < 0.01 ? `$${amount.toFixed(4)}` : `$${amount.toFixed(2)}`;
227
+ }
228
+
229
+ function renderRuns(runs: readonly RunOutcome[]): string {
230
+ const rows = runs.map((run) => [
231
+ String(run.index),
232
+ run.start,
233
+ run.toStartUnclear ? "unclear" : duration(run.toStartMs),
234
+ run.waitedForRoomMs === null ? "-" : `≥ ${formatElapsed(run.waitedForRoomMs)}`,
235
+ run.ending === "" ? "-" : run.ending,
236
+ run.costUsd === null ? "-" : usd(run.costUsd),
237
+ ]);
238
+ return renderTable(["#", "START", "TO START", "WAITED FOR A SANDBOX", "ENDING", "COST"], rows);
239
+ }
240
+
241
+ function countsLine(report: LoadTestReport): string {
242
+ const { counts } = report;
243
+ const parts = [
244
+ `${counts.started} started`,
245
+ counts.refused > 0 ? `${counts.refused} refused` : "",
246
+ counts.waitRanOut > 0 ? `${counts.waitRanOut} wait ran out` : "",
247
+ counts.neverStarted > 0 ? `${counts.neverStarted} never started` : "",
248
+ counts.stillWaiting > 0 ? `${counts.stillWaiting} still waiting` : "",
249
+ ].filter((part) => part !== "");
250
+ return `${plural(report.requested, "agent")}: ${parts.join(", ")}.`;
251
+ }
252
+
253
+ function figuresText(figures: Figures): string {
254
+ return `median ${duration(figures.median)}, 90th percentile ${duration(figures.p90)}, longest ${duration(figures.longest)}`;
255
+ }
256
+
257
+ function duration(ms: number | null): string {
258
+ return ms === null ? "-" : formatElapsed(ms);
259
+ }
260
+
261
+ function plural(count: number, noun: string): string {
262
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
263
+ }
264
+
265
+ // How an ended run ended, in the report's words: a completion, its budget,
266
+ // or the stop's cause and kind as the run names them.
267
+ function endingOf(phase: string | undefined, stopCause: string, stopKind: string): string {
268
+ if (stopKind === COST_CAP) return "stopped at its budget";
269
+ if (stopCause === "" && phase === "completed") return "completed";
270
+ const cause = stopCause === "" ? (phase ?? "ended") : stopCause;
271
+ return stopKind === "" ? cause : `${cause} (${stopKind})`;
272
+ }
273
+
274
+ // `started_at` minus the create's stamp, when `started_at` is
275
+ // millisecond-precise and the pair is in order; null otherwise.
276
+ function toStartOf(run: Run | undefined, startedAt: string): number | null {
277
+ const created = run?.status?.audit?.specAudit?.createdAt;
278
+ const fraction = FRACTION.exec(startedAt);
279
+ if (created === undefined || fraction === null || fraction[1].length < 3) return null;
280
+ const started = Date.parse(startedAt);
281
+ const ms = started - timestampMs(created);
282
+ return Number.isNaN(started) || ms < 0 ? null : ms;
283
+ }
284
+
285
+ function figuresOf(values: readonly number[]): Figures {
286
+ if (values.length === 0) return { median: null, p90: null, longest: null };
287
+ const sorted = [...values].sort((a, b) => a - b);
288
+ const rank = (p: number): number => sorted[Math.max(0, Math.ceil(p * sorted.length) - 1)];
289
+ return { median: rank(0.5), p90: rank(0.9), longest: sorted[sorted.length - 1] };
290
+ }
291
+
292
+ function groupRefusals(runs: readonly RunOutcome[]): Array<Refusal & { count: number }> {
293
+ const groups = new Map<string, Refusal & { count: number }>();
294
+ for (const run of runs) {
295
+ if (run.refusal === null) continue;
296
+ const key = `${run.refusal.code}\u0000${run.refusal.message}`;
297
+ const group = groups.get(key);
298
+ if (group === undefined) groups.set(key, { ...run.refusal, count: 1 });
299
+ else group.count += 1;
300
+ }
301
+ return [...groups.values()];
302
+ }
@@ -24,10 +24,10 @@
24
24
  // environment) still wins and is never refreshed.
25
25
 
26
26
  import type { Command } from "commander";
27
- import { type Config, activeBackend } from "../config/config.js";
27
+ import { type Config, activeBackend, hasSignIn } from "../config/config.js";
28
28
  import { DEFAULT_CLOUD_ENDPOINT } from "../config/resolve.js";
29
29
 
30
- /** The cloud backend whose sign-in the server's credential comes from. */
30
+ /** The signed-in backend the server's credential comes from. */
31
31
  export interface CloudSignIn {
32
32
  readonly name: string;
33
33
  /** The address the server dials, the entry's own or Stigmer Cloud's. */
@@ -102,8 +102,8 @@ async function runMcpServer(flags: McpServerFlags): Promise<void> {
102
102
  * Bridge the active backend into the server's environment, but only where
103
103
  * unset, so a pre-existing environment variable (or a flag bridged before this)
104
104
  * wins. The local daemon is reached without auth; a named backend contributes
105
- * its endpoint, and a self-hosted entry its API key. A cloud entry's sign-in is
106
- * returned instead of bridged, for the caller to hand the server as a token
105
+ * its endpoint, and a self-hosted entry with no sign-in its API key. A sign-in
106
+ * (any cloud entry, or a selfhost one signed in to) is returned instead of bridged, for the caller to hand the server as a token
107
107
  * provider. The credential goes only to its own backend: when the environment
108
108
  * already names another address (a runner spawning this command for its
109
109
  * server), none applies, and none does when a key was already given.
@@ -122,7 +122,7 @@ export function applyConfigEnv(config: Config, env: NodeJS.ProcessEnv): CloudSig
122
122
  if (endpoint === undefined) return undefined;
123
123
  setIfEmpty(env, "STIGMER_SERVER_ADDRESS", endpoint);
124
124
  if (env.STIGMER_SERVER_ADDRESS !== endpoint || isSet(env.STIGMER_API_KEY)) return undefined;
125
- if (entry.type === "cloud") return { name, endpoint };
125
+ if (entry.type === "cloud" || hasSignIn(entry)) return { name, endpoint };
126
126
  setIfEmpty(env, "STIGMER_API_KEY", entry.api_key);
127
127
  return undefined;
128
128
  }
@@ -11,6 +11,17 @@
11
11
  // (Anthropic + skip: the key setup saves; the runner reaches other providers
12
12
  // by keys exported in the shell, local/provider-reach.ts).
13
13
  //
14
+ // `--api-key-stdin` reads the key from stdin instead of the command line,
15
+ // where a shell's history and a coding agent's transcript would keep it
16
+ // (`printenv ANTHROPIC_API_KEY | stigmer setup --provider anthropic
17
+ // --api-key-stdin`); it implies `--provider anthropic` and refuses a terminal,
18
+ // where the interactive `stigmer setup` asks at a hidden prompt instead.
19
+ //
20
+ // A key given to setup is checked with Anthropic before it is saved, on both
21
+ // paths (local/model-key-step.ts): the flag path refuses a key Anthropic
22
+ // rejects with exit code 1 and saves nothing, and saves a key Anthropic did not
23
+ // answer for with a line saying so; the interactive path asks again.
24
+ //
14
25
  // Setup deliberately has NO model concept: the platform model registry owns
15
26
  // the execution default, and per-run overrides belong to `stigmer run
16
27
  // --model`. A setup-level pin would look authoritative and reach no
@@ -21,9 +32,10 @@ import { homedir } from "node:os";
21
32
  import { type OutputFlags, CommandResult, renderResult } from "../output/index.js";
22
33
  import { addResultFlags, resultFormat } from "./shared.js";
23
34
 
24
- interface SetupFlags extends OutputFlags {
35
+ export interface SetupFlags extends OutputFlags {
25
36
  provider?: string;
26
37
  apiKey?: string;
38
+ apiKeyStdin?: boolean;
27
39
  operatorEmail?: string;
28
40
  operatorName?: string;
29
41
  }
@@ -34,13 +46,15 @@ export function registerSetup(program: Command): void {
34
46
  .description("configure the LLM provider and operator identity for local execution")
35
47
  .option("--provider <name>", "provider: anthropic, or skip to clear (non-interactive)")
36
48
  .option("--api-key <key>", "Anthropic API key")
49
+ .option("--api-key-stdin", "read the Anthropic API key from stdin (keeps it out of history and transcripts)")
37
50
  .option("--operator-email <email>", "operator identity for local caller attribution (non-interactive)")
38
51
  .option("--operator-name <name>", "operator display name (requires --operator-email)")
39
52
  .action((options: SetupFlags) => runSetup(options));
40
53
  addResultFlags(setup);
41
54
  }
42
55
 
43
- async function runSetup(options: SetupFlags): Promise<void> {
56
+ async function runSetup(flags: SetupFlags): Promise<void> {
57
+ const options = await withStdinKey(flags);
44
58
  const { load, save } = await import("../config/config.js");
45
59
  const { PROVIDER_CHOICES, applyChoice, applyOperatorIdentity, runInteractiveWizard } =
46
60
  await import("../local/setup/wizard.js");
@@ -55,6 +69,7 @@ async function runSetup(options: SetupFlags): Promise<void> {
55
69
  options.operatorName !== undefined;
56
70
 
57
71
  let updated: typeof config;
72
+ let unchecked = false;
58
73
  if (nonInteractive) {
59
74
  updated = config;
60
75
  if (options.provider !== undefined) {
@@ -64,6 +79,9 @@ async function runSetup(options: SetupFlags): Promise<void> {
64
79
  `Valid providers: ${PROVIDER_CHOICES.join(", ")}`,
65
80
  ]);
66
81
  }
82
+ if (provider === "anthropic" && (options.apiKey ?? "").trim() !== "") {
83
+ unchecked = await refuseRejectedKey(options.apiKey!.trim());
84
+ }
67
85
  updated = applyChoice(updated, provider as (typeof PROVIDER_CHOICES)[number], {
68
86
  apiKey: options.apiKey,
69
87
  });
@@ -90,8 +108,64 @@ async function runSetup(options: SetupFlags): Promise<void> {
90
108
  save(updated);
91
109
 
92
110
  const result = CommandResult.success("Configuration saved");
111
+ if (unchecked) {
112
+ const { KEY_UNCHECKED } = await import("../local/model-key-step.js");
113
+ result.hint(KEY_UNCHECKED);
114
+ }
93
115
  if (await isRunning(homedir())) {
94
116
  result.hint("Restart to apply: stigmer down && stigmer up");
95
117
  }
96
118
  renderResult(result, resultFormat(options));
97
119
  }
120
+
121
+ /**
122
+ * The flags with `--api-key-stdin` read into `apiKey`: refused beside
123
+ * `--api-key`, on a terminal, and when stdin is empty; the provider is
124
+ * Anthropic unless the flags said it.
125
+ */
126
+ export async function withStdinKey(
127
+ flags: SetupFlags,
128
+ io: { readonly stdinIsTty: boolean; readonly readStdin: () => Promise<string> } = {
129
+ stdinIsTty: process.stdin.isTTY === true,
130
+ readStdin: async () => {
131
+ const chunks: Buffer[] = [];
132
+ for await (const chunk of process.stdin) chunks.push(chunk as Buffer);
133
+ return Buffer.concat(chunks).toString("utf8");
134
+ },
135
+ },
136
+ ): Promise<SetupFlags> {
137
+ if (flags.apiKeyStdin !== true) return flags;
138
+ const { UsageError } = await import("../errors/usage-error.js");
139
+ if (flags.apiKey !== undefined) {
140
+ throw new UsageError("--api-key and --api-key-stdin both give the key\n\nUse one.");
141
+ }
142
+ if (io.stdinIsTty) {
143
+ throw new UsageError(
144
+ "--api-key-stdin reads a key piped in, and stdin is a terminal\n\n" +
145
+ "Pipe it: printenv ANTHROPIC_API_KEY | stigmer setup --api-key-stdin\nor type it at a hidden prompt: stigmer setup",
146
+ );
147
+ }
148
+ const key = (await io.readStdin()).trim();
149
+ if (key === "") throw new UsageError("--api-key-stdin read nothing from stdin");
150
+ return { ...flags, provider: flags.provider ?? "anthropic", apiKey: key };
151
+ }
152
+
153
+ /**
154
+ * Check a key given by flag with Anthropic, where the runner would send it:
155
+ * a refused key ends the command with exit code 1 before anything is saved.
156
+ * Returns whether Anthropic did not answer (the key is saved, and the card
157
+ * says so).
158
+ */
159
+ async function refuseRejectedKey(key: string): Promise<boolean> {
160
+ const { ANTHROPIC_KEYS_URL, anthropicKeyTarget, KEY_REJECTED } = await import("../local/model-key-step.js");
161
+ const { checkAnthropicKey } = await import("../local/key-check.js");
162
+ const { CliExitError } = await import("../errors/cli-exit-error.js");
163
+ const { ExitCode } = await import("@stigmer/client/exit-codes");
164
+ const target = anthropicKeyTarget({ ...process.env, ANTHROPIC_API_KEY: key });
165
+ if (target === null) return false;
166
+ const check = await checkAnthropicKey(key, target.baseUrl);
167
+ if (check === "rejected") {
168
+ throw new CliExitError(`${KEY_REJECTED}.`, ExitCode.General, [`Nothing was saved. Get a key at ${ANTHROPIC_KEYS_URL}`]);
169
+ }
170
+ return check === "unchecked";
171
+ }
@@ -13,6 +13,12 @@
13
13
  // started runner reaches, the same answer the server was handed (stigmer#1084).
14
14
  // A stack that does not start ends with why, in either shape: the failed
15
15
  // component, how it ended and its last stderr lines (stigmer#1845).
16
+ //
17
+ // The agents line also says what Anthropic made of the runner's key
18
+ // (stigmer#3036): the key is checked before the card calls it ready, and on a
19
+ // terminal a missing or refused key is asked for before the daemon starts, so
20
+ // the key entered reaches the runner in this start (local/model-key-step.ts).
21
+ // Off a terminal nothing is asked, and the check runs beside the start.
16
22
 
17
23
  import { homedir } from "node:os";
18
24
  import type { Command } from "commander";
@@ -21,7 +27,8 @@ import { ExitCode } from "@stigmer/client/exit-codes";
21
27
  import { CommandResult, type OutputFlags, renderResult } from "../output/index.js";
22
28
  import { SERVER_PORT } from "../local/constants.js";
23
29
  import type { UpReport } from "../local/daemon/launch.js";
24
- import { describeReach } from "../local/provider-reach.js";
30
+ import type { KeyStepOutcome } from "../local/model-key-step.js";
31
+ import { describeReach, type ProviderReach } from "../local/provider-reach.js";
25
32
  import { addResultFlags, resultFormat } from "./shared.js";
26
33
 
27
34
  interface UpFlags extends OutputFlags {
@@ -73,12 +80,26 @@ export function registerUp(program: Command): void {
73
80
  }
74
81
 
75
82
  async function runUp(run: UpRun, flags: OutputFlags): Promise<void> {
76
- process.stderr.write("Starting Stigmer local stack… (first run may take a moment)\n");
77
83
  const launch = await import("../local/daemon/launch.js");
84
+ const interactive =
85
+ process.stdin.isTTY === true && process.stderr.isTTY === true && resultFormat(flags) === "human";
86
+ let keyStep: Promise<KeyStepOutcome | undefined> = Promise.resolve(undefined);
87
+ if (!run.serverOnly) {
88
+ if (interactive) {
89
+ // Never ask for a key and then refuse to start.
90
+ const { dataDir } = await import("../local/paths.js");
91
+ launch.refuseRunningStack(dataDir(homedir()));
92
+ keyStep = Promise.resolve(await runKeyStep(true));
93
+ } else {
94
+ // A fault here (an unreadable config) is the launch's to report.
95
+ keyStep = runKeyStep(false).catch(() => undefined);
96
+ }
97
+ }
98
+ process.stderr.write("Starting Stigmer local stack… (first run may take a moment)\n");
78
99
 
79
100
  if (!run.foreground) {
80
101
  const report = await launch.up(run);
81
- await renderUpCard(run, flags, report);
102
+ await renderUpCard(run, flags, report, await keyStep);
82
103
  return;
83
104
  }
84
105
 
@@ -86,7 +107,7 @@ async function runUp(run: UpRun, flags: OutputFlags): Promise<void> {
86
107
  // detached shape would have returned — and the process then stays up,
87
108
  // mirroring the server's and runner's output, until a shutdown signal.
88
109
  const code = await launch.upForeground(run, homedir(), {
89
- onReady: (report) => renderUpCard(run, flags, report),
110
+ onReady: async (report) => renderUpCard(run, flags, report, await keyStep),
90
111
  });
91
112
  if (code !== 0) {
92
113
  // The daemon body ran as this process, so its record carries our PID; the
@@ -101,7 +122,31 @@ async function runUp(run: UpRun, flags: OutputFlags): Promise<void> {
101
122
  }
102
123
  }
103
124
 
104
- async function renderUpCard(run: UpRun, flags: OutputFlags, report: UpReport): Promise<void> {
125
+ /** The model key step over the real terminal, config file and Anthropic. */
126
+ async function runKeyStep(interactive: boolean): Promise<KeyStepOutcome | undefined> {
127
+ const [{ modelKeyStep }, { checkAnthropicKey }, { load, save }, { promptSecret }] = await Promise.all([
128
+ import("../local/model-key-step.js"),
129
+ import("../local/key-check.js"),
130
+ import("../config/config.js"),
131
+ import("../local/setup/prompt.js"),
132
+ ]);
133
+ return modelKeyStep({
134
+ env: process.env,
135
+ interactive,
136
+ loadConfig: () => load(),
137
+ saveConfig: (config) => save(config),
138
+ promptSecret,
139
+ write: (text) => process.stderr.write(text),
140
+ check: (key, baseUrl) => checkAnthropicKey(key, baseUrl),
141
+ });
142
+ }
143
+
144
+ async function renderUpCard(
145
+ run: UpRun,
146
+ flags: OutputFlags,
147
+ report: UpReport,
148
+ key: KeyStepOutcome | undefined,
149
+ ): Promise<void> {
105
150
  const result = CommandResult.success(run.serverOnly ? "Stigmer control plane is up" : "Stigmer local stack is up");
106
151
  const section = result.addSection("Endpoints");
107
152
  section.field("server", `http://localhost:${SERVER_PORT}`);
@@ -111,7 +156,7 @@ async function renderUpCard(run: UpRun, flags: OutputFlags, report: UpReport): P
111
156
  // server without a bundled export must not advertise a dead URL.
112
157
  section.field("console", `http://localhost:${SERVER_PORT}`);
113
158
  }
114
- addAgentsSection(result, report);
159
+ addAgentsSection(result, report, key);
115
160
  result.hint("Check status with: stigmer status");
116
161
  result.hint(run.foreground ? "Stop it with: Ctrl-C (or stigmer down from another shell)" : "Stop it with: stigmer down");
117
162
  renderResult(result, resultFormat(flags));
@@ -129,19 +174,40 @@ async function consoleServed(): Promise<boolean> {
129
174
  return isWebConsoleAvailable();
130
175
  }
131
176
 
132
- /** Whether agents can run on this stack, and through which providers. */
133
- function addAgentsSection(result: CommandResult, report: UpReport): void {
177
+ /**
178
+ * Whether agents can run on this stack, through which providers, and what
179
+ * Anthropic said of the runner's key: checked, not answered, or refused (a
180
+ * refused key is not ready, whatever the runner reaches).
181
+ */
182
+ function addAgentsSection(result: CommandResult, report: UpReport, key: KeyStepOutcome | undefined): void {
134
183
  const section = result.addSection("Agents");
135
184
  if (report.runnerReach === undefined) {
136
185
  section.field("not ready", "no local runner (control plane only)");
137
186
  return;
138
187
  }
188
+ const rejected = key?.check === "rejected";
189
+ const ready = report.runnerReach.filter((entry) => !(rejected && entry.provider === "anthropic"));
190
+ if (ready.length > 0) section.field("ready", ready.map((entry) => describeEntry(entry, key)).join(", "));
191
+ if (rejected) {
192
+ section.field("not ready", "anthropic key rejected");
193
+ result.hint(
194
+ key.fromEnvironment
195
+ ? "Export a working ANTHROPIC_API_KEY, or unset it and run stigmer setup, then stigmer down && stigmer up"
196
+ : "Add a working key: stigmer setup, then stigmer down && stigmer up",
197
+ );
198
+ return;
199
+ }
139
200
  if (report.runnerReach.length === 0) {
140
201
  section.field("not ready", "no model provider");
141
202
  // The daemon hands its runner the key only at start, so a key saved
142
203
  // while the stack runs needs a restart to reach it.
143
204
  result.hint("Add a model provider: stigmer setup, then stigmer down && stigmer up");
144
- return;
145
205
  }
146
- section.field("ready", describeReach(report.runnerReach));
206
+ }
207
+
208
+ /** One provider for the card: `anthropic (key checked just now)`, `openai (key)`, `anthropic (vertex)`. */
209
+ function describeEntry(entry: ProviderReach, key: KeyStepOutcome | undefined): string {
210
+ if (entry.provider !== "anthropic" || key === undefined) return describeReach([entry]);
211
+ const said = key.check === "accepted" ? "key checked just now" : "key not checked: Anthropic didn't answer";
212
+ return entry.via === "gateway" ? `anthropic (gateway, ${said})` : `anthropic (${said})`;
147
213
  }