@testsprite/testsprite-cli 0.16.0 → 0.18.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 (206) hide show
  1. package/README.md +52 -147
  2. package/dist/commands/agent.d.ts +12 -4
  3. package/dist/commands/agent.js +48 -63
  4. package/dist/commands/auth.d.ts +6 -10
  5. package/dist/commands/auth.js +40 -56
  6. package/dist/commands/ci.d.ts +23 -100
  7. package/dist/commands/ci.js +293 -487
  8. package/dist/commands/conversation.d.ts +27 -0
  9. package/dist/commands/conversation.js +741 -0
  10. package/dist/commands/doctor.d.ts +5 -1
  11. package/dist/commands/doctor.js +129 -34
  12. package/dist/commands/init.d.ts +4 -45
  13. package/dist/commands/init.js +108 -66
  14. package/dist/commands/project-env.d.ts +62 -2
  15. package/dist/commands/project-env.js +434 -43
  16. package/dist/commands/project-sign-in.d.ts +0 -48
  17. package/dist/commands/project-sign-in.js +1 -229
  18. package/dist/commands/project.d.ts +19 -186
  19. package/dist/commands/project.js +108 -926
  20. package/dist/commands/result.d.ts +10 -0
  21. package/dist/commands/result.js +216 -0
  22. package/dist/commands/schedule.d.ts +0 -29
  23. package/dist/commands/schedule.js +28 -87
  24. package/dist/commands/test.d.ts +301 -1838
  25. package/dist/commands/test.js +1880 -13917
  26. package/dist/commands/testlist.d.ts +130 -5
  27. package/dist/commands/testlist.js +300 -342
  28. package/dist/commands/tunnel.d.ts +3 -3
  29. package/dist/commands/tunnel.js +23 -20
  30. package/dist/commands/usage.d.ts +12 -45
  31. package/dist/commands/usage.js +50 -131
  32. package/dist/index.js +191 -110
  33. package/dist/lib/agent-detect.js +9 -5
  34. package/dist/lib/agent-targets.d.ts +21 -2
  35. package/dist/lib/agent-targets.js +88 -11
  36. package/dist/lib/args.d.ts +13 -0
  37. package/dist/lib/args.js +49 -0
  38. package/dist/lib/basic-auth.d.ts +3 -0
  39. package/dist/lib/basic-auth.js +15 -0
  40. package/dist/lib/billing-refusal.js +4 -3
  41. package/dist/lib/ci-linkage.d.ts +4 -82
  42. package/dist/lib/ci-linkage.js +4 -157
  43. package/dist/lib/ci-run.js +1 -1
  44. package/dist/lib/client-factory.d.ts +12 -4
  45. package/dist/lib/client-factory.js +41 -30
  46. package/dist/lib/commit-signal.d.ts +65 -0
  47. package/dist/lib/commit-signal.js +305 -0
  48. package/dist/lib/concurrency-limiter.d.ts +6 -0
  49. package/dist/lib/concurrency-limiter.js +29 -0
  50. package/dist/lib/config.js +10 -5
  51. package/dist/lib/conflict-reason.d.ts +9 -3
  52. package/dist/lib/conflict-reason.js +6 -3
  53. package/dist/lib/conversation/answers.d.ts +9 -0
  54. package/dist/lib/conversation/answers.js +49 -0
  55. package/dist/lib/conversation/api.d.ts +75 -0
  56. package/dist/lib/conversation/api.js +151 -0
  57. package/dist/lib/conversation/change.d.ts +31 -0
  58. package/dist/lib/conversation/change.js +124 -0
  59. package/dist/lib/conversation/context.d.ts +29 -0
  60. package/dist/lib/conversation/context.js +82 -0
  61. package/dist/lib/conversation/environment.d.ts +16 -0
  62. package/dist/lib/conversation/environment.js +21 -0
  63. package/dist/lib/conversation/follow.d.ts +24 -0
  64. package/dist/lib/conversation/follow.js +42 -0
  65. package/dist/lib/conversation/render.d.ts +33 -0
  66. package/dist/lib/conversation/render.js +318 -0
  67. package/dist/lib/conversation/title.d.ts +2 -0
  68. package/dist/lib/conversation/title.js +18 -0
  69. package/dist/lib/conversation/types.d.ts +158 -0
  70. package/dist/lib/conversation/types.js +6 -0
  71. package/dist/lib/credentials.js +67 -11
  72. package/dist/lib/cron.d.ts +1 -1
  73. package/dist/lib/cron.js +2 -2
  74. package/dist/lib/dispatch-stop.d.ts +20 -0
  75. package/dist/lib/dispatch-stop.js +30 -0
  76. package/dist/lib/download.d.ts +100 -0
  77. package/dist/lib/download.js +408 -0
  78. package/dist/lib/dry-run/fetch.d.ts +10 -0
  79. package/dist/lib/dry-run/fetch.js +20 -0
  80. package/dist/lib/dry-run/samples.d.ts +6 -34
  81. package/dist/lib/dry-run/samples.js +375 -747
  82. package/dist/lib/environment-tunnel.d.ts +19 -30
  83. package/dist/lib/environment-tunnel.js +168 -202
  84. package/dist/lib/errors.d.ts +20 -4
  85. package/dist/lib/errors.js +36 -16
  86. package/dist/lib/facade.d.ts +7 -29
  87. package/dist/lib/facade.js +7 -36
  88. package/dist/lib/failing-fe-resolver.d.ts +13 -58
  89. package/dist/lib/failing-fe-resolver.js +47 -132
  90. package/dist/lib/first-suite-hint.d.ts +20 -30
  91. package/dist/lib/first-suite-hint.js +57 -72
  92. package/dist/lib/gh-output.d.ts +76 -21
  93. package/dist/lib/gh-output.js +171 -90
  94. package/dist/lib/git-utils.d.ts +56 -0
  95. package/dist/lib/git-utils.js +212 -0
  96. package/dist/lib/github-context.d.ts +2 -0
  97. package/dist/lib/github-context.js +40 -8
  98. package/dist/lib/history-retention.d.ts +6 -2
  99. package/dist/lib/history-retention.js +10 -7
  100. package/dist/lib/http.d.ts +58 -134
  101. package/dist/lib/http.js +354 -291
  102. package/dist/lib/interrupt.d.ts +14 -8
  103. package/dist/lib/interrupt.js +28 -10
  104. package/dist/lib/junit-report.d.ts +57 -4
  105. package/dist/lib/junit-report.js +202 -32
  106. package/dist/lib/local-target.d.ts +8 -7
  107. package/dist/lib/local-target.js +14 -13
  108. package/dist/lib/output.d.ts +2 -41
  109. package/dist/lib/output.js +13 -51
  110. package/dist/lib/pagination.d.ts +2 -5
  111. package/dist/lib/pagination.js +2 -5
  112. package/dist/lib/presigned-url.d.ts +22 -0
  113. package/dist/lib/presigned-url.js +90 -0
  114. package/dist/lib/prompt.js +36 -2
  115. package/dist/lib/proxy.js +2 -1
  116. package/dist/lib/redact.d.ts +2 -2
  117. package/dist/lib/redact.js +3 -4
  118. package/dist/lib/removed-commands.d.ts +18 -0
  119. package/dist/lib/removed-commands.js +170 -0
  120. package/dist/lib/render-error.d.ts +31 -6
  121. package/dist/lib/render-error.js +88 -25
  122. package/dist/lib/response-schemas.d.ts +38 -30
  123. package/dist/lib/response-schemas.js +207 -207
  124. package/dist/lib/result/api.d.ts +24 -0
  125. package/dist/lib/result/api.js +28 -0
  126. package/dist/lib/result/evidence.d.ts +73 -0
  127. package/dist/lib/result/evidence.js +422 -0
  128. package/dist/lib/result/render.d.ts +20 -0
  129. package/dist/lib/result/render.js +194 -0
  130. package/dist/lib/result/types.d.ts +122 -0
  131. package/dist/lib/result/types.js +19 -0
  132. package/dist/lib/run-output.d.ts +275 -0
  133. package/dist/lib/run-output.js +227 -0
  134. package/dist/lib/run-render.d.ts +101 -0
  135. package/dist/lib/run-render.js +183 -0
  136. package/dist/lib/run-wait.d.ts +192 -0
  137. package/dist/lib/run-wait.js +452 -0
  138. package/dist/lib/runs.types.d.ts +118 -367
  139. package/dist/lib/runs.types.js +0 -14
  140. package/dist/lib/secret-file.d.ts +1 -1
  141. package/dist/lib/secret-file.js +11 -2
  142. package/dist/lib/skill-nudge.d.ts +7 -27
  143. package/dist/lib/skill-nudge.js +9 -37
  144. package/dist/lib/target-path.d.ts +15 -0
  145. package/dist/lib/target-path.js +35 -0
  146. package/dist/lib/target-url-advisory.d.ts +19 -0
  147. package/dist/lib/target-url-advisory.js +31 -0
  148. package/dist/lib/target-url-preflight.d.ts +9 -0
  149. package/dist/lib/target-url-preflight.js +214 -106
  150. package/dist/lib/target-url.d.ts +8 -8
  151. package/dist/lib/target-url.js +14 -24
  152. package/dist/lib/telemetry.d.ts +35 -15
  153. package/dist/lib/telemetry.js +38 -9
  154. package/dist/lib/terminal-text.d.ts +41 -0
  155. package/dist/lib/terminal-text.js +127 -0
  156. package/dist/lib/testlist.types.d.ts +16 -8
  157. package/dist/lib/testlist.types.js +1 -1
  158. package/dist/lib/text-equality.d.ts +2 -0
  159. package/dist/lib/text-equality.js +5 -0
  160. package/dist/lib/text-file.d.ts +16 -0
  161. package/dist/lib/text-file.js +41 -0
  162. package/dist/lib/text-table.d.ts +46 -0
  163. package/dist/lib/text-table.js +72 -4
  164. package/dist/lib/ticker.d.ts +9 -2
  165. package/dist/lib/ticker.js +16 -3
  166. package/dist/lib/transport-failure.d.ts +22 -0
  167. package/dist/lib/transport-failure.js +126 -0
  168. package/dist/lib/tunnel-session.d.ts +8 -2
  169. package/dist/lib/tunnel-session.js +24 -11
  170. package/dist/lib/update-check.js +3 -2
  171. package/dist/lib/url-template.d.ts +43 -0
  172. package/dist/lib/url-template.js +151 -0
  173. package/dist/lib/validate.d.ts +0 -64
  174. package/dist/lib/validate.js +2 -131
  175. package/dist/lib/version-notice.js +4 -3
  176. package/dist/lib/wait-exit.d.ts +37 -2
  177. package/dist/lib/wait-exit.js +88 -30
  178. package/dist/lib/workspace-status.js +10 -2
  179. package/dist/version.d.ts +1 -1
  180. package/dist/version.js +1 -1
  181. package/package.json +2 -3
  182. package/skills/testsprite-ci.skill.md +52 -23
  183. package/skills/testsprite-onboard.skill.md +70 -153
  184. package/skills/testsprite-verify.codex.md +54 -58
  185. package/skills/testsprite-verify.skill.md +189 -358
  186. package/dist/lib/browser.d.ts +0 -6
  187. package/dist/lib/browser.js +0 -54
  188. package/dist/lib/bundle.d.ts +0 -234
  189. package/dist/lib/bundle.js +0 -781
  190. package/dist/lib/deprecate.d.ts +0 -32
  191. package/dist/lib/deprecate.js +0 -39
  192. package/dist/lib/flaky.d.ts +0 -91
  193. package/dist/lib/flaky.js +0 -71
  194. package/dist/lib/plan-poll.d.ts +0 -171
  195. package/dist/lib/plan-poll.js +0 -441
  196. package/dist/lib/plans.types.d.ts +0 -130
  197. package/dist/lib/plans.types.js +0 -31
  198. package/dist/lib/poll.d.ts +0 -100
  199. package/dist/lib/poll.js +0 -290
  200. package/dist/lib/rate-throttle.d.ts +0 -49
  201. package/dist/lib/rate-throttle.js +0 -75
  202. package/dist/lib/run-progress.d.ts +0 -41
  203. package/dist/lib/run-progress.js +0 -56
  204. package/dist/lib/v3-advisory.d.ts +0 -55
  205. package/dist/lib/v3-advisory.js +0 -74
  206. package/schemas/plan.schema.json +0 -61
@@ -1,32 +0,0 @@
1
- /**
2
- * Shared helper for the hidden, deprecated command aliases introduced by the
3
- * setup consolidation (`init`/`auth configure`/`auth whoami`/`auth logout`).
4
- *
5
- * The aliases are hidden from `--help` (so a coding agent never sees the old
6
- * names) but still work (so existing scripts and any agent trained on the old
7
- * commands keep running). When invoked, they print this one-line notice to
8
- * stderr — stdout stays clean so JSON pipelines are unaffected.
9
- */
10
- export declare function emitDeprecationNotice(oldCmd: string, newCmd: string, stderr?: (line: string) => void): void;
11
- /** Release that deletes the commands deprecated in the 0.16 line. */
12
- export declare const REMOVED_IN = "0.17.0";
13
- /**
14
- * A deprecated command and the invocation that replaces it. `command` is the
15
- * full name (`test rerun`) so the same entry feeds the stderr notice and the
16
- * JSON `deprecations` key. `replacement` is a template, not a command to run
17
- * as is: the caller's ids and flags carry over (see the notice's advice).
18
- */
19
- export interface DeprecationEntry {
20
- command: string;
21
- replacement: string;
22
- removedIn: string;
23
- }
24
- export declare const TEST_RERUN_DEPRECATION: DeprecationEntry;
25
- export declare const TEST_FLAKY_DEPRECATION: DeprecationEntry;
26
- /**
27
- * `advice` replaces the default "Use `<replacement>`." sentence with command-specific
28
- * migration steps. `noReplacement` says so in the lead sentence for a command that is
29
- * deleted outright.
30
- */
31
- export declare function deprecationNoticeLine(entry: DeprecationEntry, advice?: string, noReplacement?: boolean): string;
32
- export declare function emitDeprecationEntry(entry: DeprecationEntry, stderr?: (line: string) => void, advice?: string, noReplacement?: boolean): void;
@@ -1,39 +0,0 @@
1
- /**
2
- * Shared helper for the hidden, deprecated command aliases introduced by the
3
- * setup consolidation (`init`/`auth configure`/`auth whoami`/`auth logout`).
4
- *
5
- * The aliases are hidden from `--help` (so a coding agent never sees the old
6
- * names) but still work (so existing scripts and any agent trained on the old
7
- * commands keep running). When invoked, they print this one-line notice to
8
- * stderr — stdout stays clean so JSON pipelines are unaffected.
9
- */
10
- export function emitDeprecationNotice(oldCmd, newCmd, stderr = (line) => process.stderr.write(`${line}\n`)) {
11
- stderr(`[deprecated] \`testsprite ${oldCmd}\` is deprecated — use \`testsprite ${newCmd}\`.`);
12
- }
13
- /** Release that deletes the commands deprecated in the 0.16 line. */
14
- export const REMOVED_IN = '0.17.0';
15
- export const TEST_RERUN_DEPRECATION = {
16
- command: 'test rerun',
17
- replacement: 'test run',
18
- removedIn: REMOVED_IN,
19
- };
20
- // `test flaky` has no replacement; `replacement` names the command that samples a test again.
21
- export const TEST_FLAKY_DEPRECATION = {
22
- command: 'test flaky',
23
- replacement: 'test run',
24
- removedIn: REMOVED_IN,
25
- };
26
- /**
27
- * `advice` replaces the default "Use `<replacement>`." sentence with command-specific
28
- * migration steps. `noReplacement` says so in the lead sentence for a command that is
29
- * deleted outright.
30
- */
31
- export function deprecationNoticeLine(entry, advice, noReplacement = false) {
32
- return (`[deprecated] \`testsprite ${entry.command}\` is deprecated and will be removed in ${entry.removedIn}` +
33
- (noReplacement ? ', with no replacement. ' : '. ') +
34
- (advice ?? `Use \`testsprite ${entry.replacement}\`.`));
35
- }
36
- export function emitDeprecationEntry(entry, stderr = (line) => process.stderr.write(`${line}\n`), advice, noReplacement = false) {
37
- stderr(deprecationNoticeLine(entry, advice, noReplacement));
38
- }
39
- //# sourceMappingURL=deprecate.js.map
@@ -1,91 +0,0 @@
1
- /**
2
- * Pure aggregation + rendering for `test flaky` — the repeat-run flaky-test
3
- * detector. This module performs no I/O: the orchestrator in
4
- * `commands/test.ts` feeds it the per-attempt outcomes, and it returns a
5
- * machine-readable stability report plus the text rendering and exit code.
6
- *
7
- * Keeping the scoring logic pure makes it trivially unit-testable in isolation
8
- * (deterministic, no network / credentials), matching the repo's mock-based
9
- * test convention.
10
- */
11
- import type { RerunAdvisory } from './runs.types.js';
12
- /**
13
- * Outcome of a single flaky-detector attempt. The first four mirror the
14
- * terminal `RunStatus` values; `timeout` and `error` are orchestration
15
- * outcomes (per-attempt deadline exceeded, or a non-fatal trigger/transport
16
- * error that was recorded rather than aborting the whole probe).
17
- */
18
- export type FlakyOutcome = 'passed' | 'failed' | 'blocked' | 'cancelled' | 'timeout' | 'error';
19
- /** Overall stability verdict across all observed attempts. */
20
- export type FlakyVerdict = 'stable' | 'flaky' | 'failing';
21
- /** One recorded attempt in a flaky run. */
22
- export interface FlakyAttempt {
23
- /** 1-based attempt index. */
24
- attempt: number;
25
- /** The runId of this attempt, or `null` when the trigger never returned one. */
26
- runId: string | null;
27
- outcome: FlakyOutcome;
28
- /**
29
- * Server `failureKind` for non-passing runs (or the error code for `error`
30
- * outcomes). `null` / omitted when the attempt passed or the kind is unknown.
31
- */
32
- failureKind?: string | null;
33
- }
34
- /** A single non-passing attempt as surfaced in the report. */
35
- export interface FlakyFailure {
36
- attempt: number;
37
- runId: string | null;
38
- outcome: FlakyOutcome;
39
- failureKind: string | null;
40
- }
41
- /**
42
- * Machine-readable stability report. This is also the exact `--output json`
43
- * shape, so dashboards / agents / CI can consume it directly.
44
- */
45
- export interface FlakyReport {
46
- testId: string;
47
- /**
48
- * Attempts actually observed. May be fewer than requested when
49
- * `--until-fail` short-circuits on the first non-passing attempt.
50
- */
51
- runs: number;
52
- passed: number;
53
- failed: number;
54
- /** `passed / runs`, rounded to 4 decimal places. `0` when `runs === 0`. */
55
- stableRatio: number;
56
- verdict: FlakyVerdict;
57
- /** Non-passing attempts, in attempt order. */
58
- failures: FlakyFailure[];
59
- /**
60
- * Server-side advisories collected across all replay attempts and deduped
61
- * (by `feature`+`message`) — surfaced ONCE per probe, not once per attempt.
62
- * Empty when the server sent none, which is the common case: absent on
63
- * every V2 response and every V3 response that isn't a V3-routed rerun
64
- * with an explicit `autoHeal:false` opt-out (see `RerunAdvisory`). `test
65
- * flaky` always sends `autoHeal:false`, so this is populated whenever the
66
- * probe's reruns are routed to V3.
67
- */
68
- advisories: RerunAdvisory[];
69
- }
70
- /**
71
- * Aggregate per-attempt outcomes into a stability report.
72
- *
73
- * Verdict rules:
74
- * - every attempt passed → `stable`
75
- * - no attempt passed → `failing`
76
- * - a mix of pass and non-pass → `flaky`
77
- * An empty attempt list (no runs observed) is reported as `failing` with a
78
- * `0` ratio — there is no evidence the test is stable.
79
- */
80
- export declare function summarizeFlaky(testId: string, attempts: FlakyAttempt[], advisories?: RerunAdvisory[]): FlakyReport;
81
- /**
82
- * Exit code for the command: `0` only when the verdict is `stable` (every
83
- * observed attempt passed). Anything else is non-zero so CI can gate a merge
84
- * on flakiness (`testsprite test flaky <id> --runs 5 || exit 1`).
85
- */
86
- export declare function flakyExitCode(report: FlakyReport): number;
87
- /**
88
- * Render a report to human-readable text. JSON-mode callers ship the report
89
- * verbatim via `out.print`; this is the text-mode rendering.
90
- */
91
- export declare function renderFlakyText(report: FlakyReport): string;
package/dist/lib/flaky.js DELETED
@@ -1,71 +0,0 @@
1
- /**
2
- * Aggregate per-attempt outcomes into a stability report.
3
- *
4
- * Verdict rules:
5
- * - every attempt passed → `stable`
6
- * - no attempt passed → `failing`
7
- * - a mix of pass and non-pass → `flaky`
8
- * An empty attempt list (no runs observed) is reported as `failing` with a
9
- * `0` ratio — there is no evidence the test is stable.
10
- */
11
- export function summarizeFlaky(testId, attempts, advisories = []) {
12
- const runs = attempts.length;
13
- const passed = attempts.filter(a => a.outcome === 'passed').length;
14
- const failed = runs - passed;
15
- const stableRatio = runs === 0 ? 0 : Math.round((passed / runs) * 10000) / 10000;
16
- const verdict = runs > 0 && passed === runs ? 'stable' : passed === 0 ? 'failing' : 'flaky';
17
- const failures = attempts
18
- .filter(a => a.outcome !== 'passed')
19
- .map(a => ({
20
- attempt: a.attempt,
21
- runId: a.runId,
22
- outcome: a.outcome,
23
- failureKind: a.failureKind ?? null,
24
- }));
25
- return { testId, runs, passed, failed, stableRatio, verdict, failures, advisories };
26
- }
27
- /**
28
- * Exit code for the command: `0` only when the verdict is `stable` (every
29
- * observed attempt passed). Anything else is non-zero so CI can gate a merge
30
- * on flakiness (`testsprite test flaky <id> --runs 5 || exit 1`).
31
- */
32
- export function flakyExitCode(report) {
33
- return report.verdict === 'stable' ? 0 : 1;
34
- }
35
- /** Human-readable label for a verdict. */
36
- function verdictLabel(verdict) {
37
- switch (verdict) {
38
- case 'stable':
39
- return 'STABLE';
40
- case 'flaky':
41
- return 'FLAKY';
42
- case 'failing':
43
- return 'FAILING';
44
- }
45
- }
46
- /**
47
- * Render a report to human-readable text. JSON-mode callers ship the report
48
- * verbatim via `out.print`; this is the text-mode rendering.
49
- */
50
- export function renderFlakyText(report) {
51
- const pct = Math.round(report.stableRatio * 100);
52
- const lines = [
53
- `Ran ${report.testId} ${report.runs}x — ${report.passed} passed, ${report.failed} failed → ` +
54
- `${verdictLabel(report.verdict)} (${pct}% stable)`,
55
- ];
56
- if (report.failures.length > 0) {
57
- lines.push(' failed attempts:');
58
- for (const f of report.failures) {
59
- const kind = f.failureKind ? ` failureKind=${f.failureKind}` : '';
60
- const rid = f.runId ?? '(no runId)';
61
- lines.push(` #${f.attempt} ${rid} ${f.outcome}${kind}`);
62
- }
63
- }
64
- if (report.advisories.length > 0) {
65
- for (const advisory of report.advisories) {
66
- lines.push(` [advisory] ${advisory.message}`);
67
- }
68
- }
69
- return lines.join('\n');
70
- }
71
- //# sourceMappingURL=flaky.js.map
@@ -1,171 +0,0 @@
1
- /**
2
- * Generation-ladder driver for `test plan generate`.
3
- *
4
- * This is a NEW loop, deliberately not a reuse of `poll.ts`: the run poller
5
- * is hard-typed to runs (its `RunClient` contract, `isTerminalStatus` check,
6
- * and `TimeoutError.runId` are all run-specific), while generation settles on
7
- * a completely different state machine — the facade-synthesized
8
- * `CliGenerationStatus` — and additionally has to RE-TRIGGER between stages
9
- * (writes happen on POST; the read stays pure, so when a stage finishes while
10
- * a later one is still missing the client must POST again to start it).
11
- *
12
- * What it shares with the run poller, by design:
13
- *
14
- * - **The shutdown handle is armed here** for the whole ladder (first POST
15
- * included). Arm/disarm lives inside the run poller, not for free in the
16
- * harness — without arming, Ctrl-C during a generate wait would hard-kill
17
- * the process instead of running the honest-detach UX.
18
- * - Long-poll preferred (`?waitSeconds = min(remaining, 25)`), with a
19
- * plain-GET + fixed-sleep fallback when the server rejects `waitSeconds`
20
- * with VALIDATION_ERROR (older deployment).
21
- * - ONE session-scoped `AbortController` firing at the deadline plus a 2s
22
- * transport cushion (a fixed absolute instant, so per-request minting
23
- * would be pure churn), composed with the shutdown signal via explicit
24
- * add/removeEventListener — NOT `AbortSignal.any`, whose dependent-signal
25
- * tracking against the long-lived shutdown signal is the known
26
- * FinalizationRegistry backlog. poll.ts CONTAINS that backlog by hoisting
27
- * a single `.any()` per session (it still calls it); only http.ts
28
- * eliminated it outright (`composeAbortSignals`). This module composes
29
- * explicitly, like http.ts — don't go hunting for a poll.ts replacement
30
- * that isn't there.
31
- * A hung fetch can never overrun `--timeout` and an interrupt aborts the
32
- * in-flight request instantly.
33
- * - Every sleep is clamped to the remaining deadline and bails on interrupt.
34
- *
35
- * The deadline throws {@link PlanGenerationTimeoutError} — which, exactly
36
- * like poll.ts's `TimeoutError`, is NOT a typed CLI error. The command layer
37
- * must convert it into the typed exit-7 envelope (`code: 'UNSUPPORTED'`) the
38
- * way every existing wait site does; letting it propagate raw would surface
39
- * as a generic exit 1.
40
- *
41
- * ── Billing safety (why re-POSTs are guarded) ──────────────────────────────
42
- *
43
- * The proposals stage has NO server-side duplicate guard: a second append
44
- * request wipes the staged batch, regenerates, and bills another 2 credits.
45
- * The facade refuses to restart when it can SEE the stage (staged proposals,
46
- * or in-flight per-feature flags) — but those flags are set asynchronously
47
- * by the append listener, so a client that re-POSTs in the gap between
48
- * "append accepted" and "flags visible" would still double-bill. Two ladder
49
- * rules make that structurally impossible:
50
- *
51
- * 1. **Never re-POST after the proposals rung.** The trigger response names
52
- * the remaining stages; when the last accepted trigger reported
53
- * `stagesRemaining: []` there is no later stage a re-POST could start,
54
- * so an idle-and-empty read just keeps polling.
55
- * 2. **Dwell before any other re-POST.** A re-POST fires only once the
56
- * started stage was actually observed running (it has genuinely
57
- * finished), or after two consecutive idle reads (covering a stage that
58
- * finished faster than our first poll). Exploration and strategy are
59
- * server-idempotent (per-project ledger key / row lock), so a duplicate
60
- * POST there wastes nothing even if the dwell heuristic misjudges.
61
- */
62
- import { ApiError } from './errors.js';
63
- import type { ShutdownHandle } from './interrupt.js';
64
- import type { CliGeneratePlansResponse, CliGetPlansResponse } from './plans.types.js';
65
- /**
66
- * Minimal structural interface the ladder needs from the HTTP client —
67
- * mirrors `RunClient` in poll.ts so tests supply a lightweight fake instead
68
- * of the full `HttpClient`.
69
- */
70
- export interface PlanLadderClient {
71
- generatePlans(projectId: string, options: {
72
- idempotencyKey: string;
73
- signal?: AbortSignal;
74
- }): Promise<CliGeneratePlansResponse>;
75
- getPlans(projectId: string, options?: {
76
- waitSeconds?: number;
77
- signal?: AbortSignal;
78
- }): Promise<CliGetPlansResponse>;
79
- }
80
- /**
81
- * Thrown when the ladder's deadline elapses. NOT a typed CLI error — the
82
- * command layer converts it to the exit-7 `UNSUPPORTED` envelope (see the
83
- * module doc). Carries the last observed plans read so the caller can print
84
- * an honest partial before exiting.
85
- */
86
- export declare class PlanGenerationTimeoutError extends Error {
87
- readonly projectId: string;
88
- readonly timeoutSeconds: number;
89
- readonly lastPlans: CliGetPlansResponse | null;
90
- constructor(projectId: string, timeoutSeconds: number, lastPlans: CliGetPlansResponse | null);
91
- }
92
- export interface PlanLadderOptions {
93
- /** Total wall-clock budget for the WHOLE ladder (all stages), in seconds. */
94
- timeoutSeconds: number;
95
- /**
96
- * Idempotency key for the first trigger POST. Every later POST attempt —
97
- * 409-attaches included — suffixes it (`<key>:post2`, `<key>:post3`, …):
98
- * each POST is fresh work, so replaying an earlier POST's cached 202
99
- * would silently never start the next stage.
100
- */
101
- idempotencyKey: string;
102
- /** Injectable sleep (tests use an instant fake). */
103
- sleep?: (ms: number) => Promise<void>;
104
- /**
105
- * Graceful-detach coordinator. The ladder arms it for its whole
106
- * duration; a SIGINT/SIGTERM surfaces as `InterruptError` out of this
107
- * function and the caller renders the honest detach.
108
- */
109
- shutdown?: ShutdownHandle;
110
- /**
111
- * Fired after every trigger POST outcome: an accepted stage start
112
- * (`acceptedPosts` counts it) or the `nothing_to_start` answer. The
113
- * command layer prints the first-run hint / credentials warning from the
114
- * FIRST accepted response.
115
- */
116
- onTrigger?: (response: CliGeneratePlansResponse, acceptedPosts: number) => void;
117
- /** Fired when a trigger answered 409 `stage_in_flight` (attach + poll). */
118
- onAttach?: (err: ApiError) => void;
119
- /** Fired after every successful plans read (ticker updates). */
120
- onTick?: (plans: CliGetPlansResponse, elapsedMs: number) => void;
121
- /** Low-volume mode-switch messages (wired to stderr at `--verbose`). */
122
- onTransition?: (msg: string) => void;
123
- }
124
- export interface PlanLadderResult {
125
- /** The settled read: staged proposals (success), empty-idle, or `failed`. */
126
- plans: CliGetPlansResponse;
127
- /**
128
- * The first ACCEPTED trigger response, or `null` when the ladder only ever
129
- * attached to stages started elsewhere (409) or found the batch already
130
- * staged (`nothing_to_start`).
131
- */
132
- firstTrigger: CliGeneratePlansResponse | null;
133
- /** How many trigger POSTs were accepted (stages this invocation started). */
134
- acceptedPosts: number;
135
- /**
136
- * `skippedCategories` from the accepted trigger that carried it (the
137
- * proposals rung), or `null` when no accepted trigger did. Read from the
138
- * LAST such trigger, not `firstTrigger`: on a fresh backend project the
139
- * first accepted POST starts strategy, and the field rides only on the
140
- * proposals POST that follows.
141
- */
142
- skippedCategories: number | null;
143
- }
144
- /**
145
- * Hard cap on accepted trigger POSTs per invocation. The ladder has one rung
146
- * per pipeline stage, so one more accepted POST than stages means the server
147
- * keeps reporting "idle and nothing staged" after every stage was started — a
148
- * contract violation worth failing loudly on instead of re-POSTing forever.
149
- */
150
- export declare const MAX_ACCEPTED_TRIGGER_POSTS: 3;
151
- /**
152
- * Companion cap on TOTAL trigger POSTs (attaches included), so an
153
- * attach-dominated pathological sequence also fails fast instead of
154
- * silently burning the whole `--timeout`: the accepted rungs + headroom for
155
- * an attach per rung.
156
- */
157
- export declare const MAX_TRIGGER_POSTS_TOTAL: number;
158
- /**
159
- * Drive the generate ladder to a settled state: trigger the next missing
160
- * stage, poll, re-trigger between stages, and return the settled plans read.
161
- *
162
- * @returns the settled read — the caller maps it to exit codes (staged
163
- * proposals → 0, `failed` → 1) and renders it.
164
- * @throws `PlanGenerationTimeoutError` when `timeoutSeconds` elapses (the
165
- * caller converts to the typed exit-7 envelope).
166
- * @throws `InterruptError` on SIGINT/SIGTERM while armed (the caller renders
167
- * the honest detach and index.ts exits 128+signum).
168
- * @throws `ApiError` for trigger/read failures (412 preconditions, 402
169
- * credits, 404, 429 — already typed with their exit codes).
170
- */
171
- export declare function runGenerationLadder(client: PlanLadderClient, projectId: string, options: PlanLadderOptions): Promise<PlanLadderResult>;