agl 43.3.2 → 43.5.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 (110) hide show
  1. package/changelog.md +33 -0
  2. package/dist_serve/bundle.js +2248 -2236
  3. package/dist_serve/bundle.js.map +2 -2
  4. package/dist_serve/bundle.js.third-party-notices.json +29 -29
  5. package/dist_serve/bundle.js.third-party-notices.txt +29 -29
  6. package/dist_ts/00_commitinfo_data.js +1 -1
  7. package/dist_ts/aglapi/interfaces.d.ts +5 -0
  8. package/dist_ts/aglapi/interfaces.js +2 -1
  9. package/dist_ts/classes.authstore.js +5 -2
  10. package/dist_ts/classes.claudeharness.js +4 -3
  11. package/dist_ts/classes.codexclientadapter.d.ts +5 -0
  12. package/dist_ts/classes.codexclientadapter.js +12 -6
  13. package/dist_ts/classes.controller.d.ts +32 -6
  14. package/dist_ts/classes.controller.js +165 -62
  15. package/dist_ts/classes.inferencegateway.js +14 -2
  16. package/dist_ts/classes.inferencerouter.d.ts +9 -1
  17. package/dist_ts/classes.inferencerouter.js +68 -14
  18. package/dist_ts/classes.mcpclient.d.ts +2 -2
  19. package/dist_ts/classes.mcpclient.js +14 -9
  20. package/dist_ts/classes.mcptools.d.ts +1 -1
  21. package/dist_ts/classes.mcptools.js +2 -1
  22. package/dist_ts/classes.opencodeclient.js +20 -4
  23. package/dist_ts/driver/functions.driverinstructions.d.ts +1 -1
  24. package/dist_ts/driver/functions.driverinstructions.js +2 -1
  25. package/dist_ts/fleet/classes.claudeworkers.d.ts +8 -5
  26. package/dist_ts/fleet/classes.claudeworkers.js +13 -8
  27. package/dist_ts/fleet/classes.deploymodels.d.ts +58 -1
  28. package/dist_ts/fleet/classes.deploymodels.js +121 -2
  29. package/dist_ts/fleet/classes.deploymonitor.d.ts +193 -47
  30. package/dist_ts/fleet/classes.deploymonitor.js +548 -180
  31. package/dist_ts/fleet/classes.gitfacts.d.ts +4 -1
  32. package/dist_ts/fleet/classes.gitfacts.js +13 -10
  33. package/dist_ts/fleet/classes.service.d.ts +2 -0
  34. package/dist_ts/fleet/classes.service.js +6 -3
  35. package/dist_ts/fleet/functions.deployprobes.js +21 -2
  36. package/dist_ts/fleet/functions.deploystatus.d.ts +26 -0
  37. package/dist_ts/fleet/functions.deploystatus.js +33 -0
  38. package/dist_ts/fleet/functions.deployvalidation.d.ts +1 -0
  39. package/dist_ts/fleet/functions.deployvalidation.js +5 -2
  40. package/dist_ts/functions.claudecoderelease.d.ts +1 -1
  41. package/dist_ts/functions.claudecoderelease.js +1 -1
  42. package/dist_ts/functions.deploycli.js +3 -2
  43. package/dist_ts/functions.deviceapi.d.ts +7 -6
  44. package/dist_ts/functions.deviceapi.js +13 -5
  45. package/dist_ts/functions.inferencecli.js +10 -2
  46. package/dist_ts/functions.inferencelaunch.d.ts +19 -8
  47. package/dist_ts/functions.inferencelaunch.js +23 -12
  48. package/dist_ts/functions.mcpdeploytools.js +3 -1
  49. package/dist_ts/functions.promptreply.d.ts +13 -0
  50. package/dist_ts/functions.promptreply.js +17 -0
  51. package/dist_ts/functions.proxyharnessconfig.d.ts +18 -0
  52. package/dist_ts/functions.proxyharnessconfig.js +30 -1
  53. package/dist_ts_authswitch/authority-renewal.d.ts +12 -3
  54. package/dist_ts_authswitch/authority-renewal.js +14 -5
  55. package/dist_ts_authswitch/classes.claudenative.d.ts +8 -8
  56. package/dist_ts_authswitch/classes.claudenative.js +13 -13
  57. package/dist_ts_interfaces/deployments.d.ts +38 -6
  58. package/dist_ts_interfaces/deployments.js +7 -3
  59. package/dist_ts_interfaces/devices.d.ts +2 -1
  60. package/dist_ts_interfaces/devices.js +1 -1
  61. package/dist_ts_interfaces/inbox.d.ts +10 -1
  62. package/dist_ts_interfaces/inbox.js +1 -1
  63. package/dist_ts_interfaces/mcprequests.d.ts +1 -1
  64. package/dist_ts_interfaces/requests.accounts.d.ts +5 -0
  65. package/dist_ts_interfaces/requests.accounts.js +3 -1
  66. package/docs/authswitch.md +5 -4
  67. package/package.json +2 -2
  68. package/readme.md +49 -21
  69. package/ts/00_commitinfo_data.ts +1 -1
  70. package/ts/aglapi/interfaces.ts +1 -0
  71. package/ts/classes.authstore.ts +4 -1
  72. package/ts/classes.claudeharness.ts +3 -2
  73. package/ts/classes.codexclientadapter.ts +11 -5
  74. package/ts/classes.controller.ts +177 -73
  75. package/ts/classes.inferencegateway.ts +15 -1
  76. package/ts/classes.inferencerouter.ts +72 -13
  77. package/ts/classes.mcpclient.ts +14 -5
  78. package/ts/classes.mcptools.ts +1 -0
  79. package/ts/classes.opencodeclient.ts +23 -3
  80. package/ts/driver/functions.driverinstructions.ts +1 -0
  81. package/ts/fleet/classes.claudeworkers.ts +12 -7
  82. package/ts/fleet/classes.deploymodels.ts +113 -1
  83. package/ts/fleet/classes.deploymonitor.ts +565 -179
  84. package/ts/fleet/classes.gitfacts.ts +10 -8
  85. package/ts/fleet/classes.service.ts +7 -1
  86. package/ts/fleet/functions.deployprobes.ts +16 -1
  87. package/ts/fleet/functions.deploystatus.ts +56 -0
  88. package/ts/fleet/functions.deployvalidation.ts +4 -1
  89. package/ts/functions.claudecoderelease.ts +1 -1
  90. package/ts/functions.deploycli.ts +2 -1
  91. package/ts/functions.deviceapi.ts +19 -9
  92. package/ts/functions.inferencecli.ts +7 -1
  93. package/ts/functions.inferencelaunch.ts +35 -14
  94. package/ts/functions.mcpdeploytools.ts +2 -0
  95. package/ts/functions.promptreply.ts +20 -0
  96. package/ts/functions.proxyharnessconfig.ts +32 -0
  97. package/ts_authswitch/authority-renewal.ts +14 -4
  98. package/ts_authswitch/classes.claudenative.ts +12 -12
  99. package/ts_authswitch/native-contracts.md +250 -28
  100. package/ts_interfaces/deployments.ts +39 -6
  101. package/ts_interfaces/devices.schema.json +1 -1
  102. package/ts_interfaces/devices.ts +2 -1
  103. package/ts_interfaces/inbox.ts +8 -1
  104. package/ts_interfaces/requests.accounts.ts +2 -0
  105. package/ts_web/00_commitinfo_data.ts +1 -1
  106. package/ts_web/classes.accountsreadmodel.ts +19 -7
  107. package/ts_web/elements.accounts.ts +31 -18
  108. package/ts_web/elements.deployments.ts +20 -4
  109. package/ts_web/elements.harnesscontrollerapp.ts +43 -13
  110. package/ts_web/elements.inbox.ts +3 -3
@@ -2,12 +2,18 @@ import * as plugins from '../plugins.js';
2
2
  import type { IFleetConfiguration, IFleetDiscoverySkip, IFleetRepository } from '../../ts_interfaces/fleet.js';
3
3
  import type { IFleetEvent } from '../../ts_interfaces/fleetworkers.js';
4
4
  import { ticketDeployPlanOptions, ticketDoneColumns, ticketWindowOptions, type ITicket, type ITicketActor, type ITicketQuestion } from '../../ts_interfaces/tickets.js';
5
- import type {
6
- IFleetDeployArtifact, IFleetDeployBug, IFleetDeployObservation, IFleetDeployPlan, IFleetDeployProbeState, IFleetDeployRelease, IFleetDeployStatus,
7
- IFleetDeployTarget, IFleetDeployTargetStatus, IFleetDeployTicket, IFleetDeployTicketPlacement, IFleetDeployWindow, IFleetReleasePlanTicket,
8
- IFleetReleaseStatus, IFleetRepositoryReleaseStatus, TFleetDeployCondition, TFleetDeployGo, TFleetDeployWindowGo,
5
+ import {
6
+ fleetDeployRefreshWaitMs, type IFleetDeployArtifact, type IFleetDeployBug, type IFleetDeployObservation, type IFleetDeployProbeState,
7
+ type IFleetDeployRefreshResult, type IFleetDeployRelease, type IFleetDeployStatus, type IFleetDeployStatusFailure, type IFleetDeployTarget,
8
+ type IFleetDeployTargetStatus, type IFleetDeployTicket, type IFleetDeployTicketPlacement, type IFleetReleasePlanTicket, type IFleetReleaseStatus,
9
+ type IFleetRepositoryReleaseStatus, type TFleetDeployCondition, type TFleetDeployGo, type TFleetDeployWindowGo,
9
10
  } from '../../ts_interfaces/deployments.js';
10
- import { DeployObservationModel } from './classes.deploymodels.js';
11
+ import {
12
+ DeployObservationModel, DeployStatusModel, deployStatusContentSchema, deployStatusFailureSchema, deployStatusId, type IDeployStatusDocument,
13
+ type TDeployStatusContent, type TDeployStoredPlan, type TDeployStoredTarget, type TDeployStoredWindow,
14
+ } from './classes.deploymodels.js';
15
+ import { deployPlansAt, deployStatusAt, deployTargetWindows, deployWindowAt, deployWindowState, type IDeployStatusModel } from './functions.deploystatus.js';
16
+ import { controllerFailureJournal } from '../classes.failurejournal.js';
11
17
  import { probeFleetHttpTarget, type TFleetDeployProbeResult } from './functions.deployprobes.js';
12
18
  import {
13
19
  compareFleetVersions, findFleetRepository, fleetReleaseLinkMatches, fleetRepositoryMatches, fleetRepositorySlug, fleetVersion,
@@ -24,8 +30,14 @@ export const deployMonitorDefaults = {
24
30
  intervalMinutes: 5, timeoutMs: 10_000, discoveryTtlMs: 60_000, gitTimeoutMs: 120_000, fetchConcurrency: 4,
25
31
  /** Probes in a row that must fail before a target is down, so one lost answer is no outage. */
26
32
  downAfterFailures: 2,
27
- /** How long ticket changes gather before the undeployed set is computed again. */
33
+ /** How long ticket changes gather before the status is computed again: this long after the last change… */
28
34
  ticketsSettleMs: 2_000,
35
+ /** …and at most this long after the first change that waits, so a steady stream of board changes holds it off no longer. */
36
+ ticketsMaxWaitMs: 10_000,
37
+ /** How long a read waits for the first computation while no status is stored (AGL-1704); then it is refused. */
38
+ firstStatusWaitMs: 20_000,
39
+ /** The stored status holds at most this much JSON: a computation over it fails, and nothing is cut. */
40
+ contentLimitBytes: 4 * 1024 * 1024,
29
41
  } as const;
30
42
  /** The built-in target: the AGL controller this monitor runs in. */
31
43
  export const deployControllerTargetId = 'agl-controller';
@@ -57,6 +69,8 @@ const maximumListedTickets = 100;
57
69
  export const deployHistoryMaximum = 200;
58
70
 
59
71
  export interface IDeployMonitorHost {
72
+ /** The controller whose status the monitor stores: its one document is `<controllerId>:deployments`. */
73
+ controllerId(): string;
60
74
  /** The fleet owner and configuration; undefined while the configuration authority is unavailable. */
61
75
  scope(): Promise<{ owner: string; configuration: IFleetConfiguration } | undefined>;
62
76
  /** The repositories fleet discovery finds; a target's repository is one of them. */
@@ -80,7 +94,11 @@ export interface IDeployMonitorHost {
80
94
  closeDeployPlan(planArg: { key: string; questionId: string }, textArg: string): Promise<void>;
81
95
  /** The tickets whose `undeployed` AutoLabel changed; the board reads them again. */
82
96
  undeployedChanged?(ticketKeysArg: string[]): void;
83
- /** What a target runs or whether its probe answers changed; the Deployments view reads again. */
97
+ /**
98
+ * The stored status's revision moved (what a target runs, its probe, its releases, plans or windows changed), a
99
+ * computation failed or the first success after a failure cleared it, or the window timer saw a maintenance window
100
+ * open or end; the Deployments view reads again.
101
+ */
84
102
  changed?(): void;
85
103
  }
86
104
 
@@ -89,18 +107,60 @@ export interface IDeployMonitorOptions {
89
107
  fetch?: typeof fetch;
90
108
  git?: TFleetGitRunner;
91
109
  timeoutMs?: number;
110
+ /** How long a read waits for the first computation; `deployMonitorDefaults.firstStatusWaitMs` unless set. */
111
+ firstStatusWaitMs?: number;
112
+ /** How long `refresh` waits for its check; `fleetDeployRefreshWaitMs` unless set. */
113
+ refreshWaitMs?: number;
92
114
  }
93
115
 
94
116
  /** The refusals of the deploy views; their messages name only what the caller asked for, so the private channel passes them on. */
95
117
  export const deployErrorCodes = ['deploy_target_unknown', 'deploy_repository_unknown', 'deploy_ticket_unknown', 'deploy_unavailable'] as const;
96
118
 
119
+ /**
120
+ * Why the deploy views are unavailable (`deploy_unavailable`): the configuration authority, the controller or the monitor
121
+ * is not running; or no status is stored yet because the first computation still runs (`computing`) or failed (`failed`).
122
+ * Only `not_running` is a state of the host a device reads as such.
123
+ */
124
+ export type TDeployUnavailableReason = 'not_running' | 'computing' | 'failed';
125
+
97
126
  export class DeployError extends Error {
98
- constructor(public readonly code: (typeof deployErrorCodes)[number], messageArg: string) {
127
+ /** For `deploy_unavailable`: why. */
128
+ public readonly unavailable?: TDeployUnavailableReason;
129
+ /** For `deploy_unavailable` `failed`: the failure journal reference the failed computation was recorded under. */
130
+ public readonly reference?: string;
131
+ constructor(public readonly code: (typeof deployErrorCodes)[number], messageArg: string,
132
+ unavailableArg?: Exclude<TDeployUnavailableReason, 'not_running'>, referenceArg?: string) {
99
133
  super(messageArg);
100
134
  this.name = 'DeployError';
135
+ if (code === 'deploy_unavailable') this.unavailable = unavailableArg ?? 'not_running';
136
+ if (this.unavailable === 'failed' && referenceArg) this.reference = referenceArg;
101
137
  }
102
138
  }
103
139
 
140
+ /** A computation that failed, said in AGL's own words that name what could not be read; its cause stays beside it. */
141
+ class DeployStatusError extends Error {
142
+ constructor(messageArg: string, optionsArg?: ErrorOptions) {
143
+ super(messageArg, optionsArg);
144
+ this.name = 'DeployStatusError';
145
+ }
146
+ }
147
+
148
+ /** What runs the monitor's work: a pass fetches, releases and probes, then computes; a computation reads only what is held. */
149
+ export type TDeployRun = 'pass' | 'compute';
150
+
151
+ /** The longest delay Node holds a timer for; a longer one fires at once. */
152
+ const maximumTimerMs = 2 ** 31 - 1;
153
+ /** An unref'd timer, its delay clamped to what Node holds, so no timer keeps the process up or fires early. */
154
+ const deployTimer = (delayMsArg: number, runArg: () => void): ReturnType<typeof setTimeout> => {
155
+ const timer = setTimeout(runArg, Math.min(Math.max(0, delayMsArg), maximumTimerMs));
156
+ timer.unref();
157
+ return timer;
158
+ };
159
+ /** A status whose probes answered at other times shows nothing new: the views hear of it only when something else changed. */
160
+ const withoutProbeTimes = (contentArg: TDeployStatusContent): TDeployStatusContent => ({ ...contentArg, status: { ...contentArg.status,
161
+ targets: contentArg.status.targets.map((targetArg) => { const { checkedAt: _checkedAt, ...probe } = targetArg.probe; return { ...targetArg, probe }; }) } });
162
+ const byKey = (leftArg: string, rightArg: string): number => leftArg.localeCompare(rightArg, 'en', { numeric: true });
163
+
104
164
  /** The built-in controller target, with what a configured controller target does not set. */
105
165
  const controllerTarget = (hostArg: string, repoArg: string | undefined, configuredArg?: IFleetDeployTarget): (IFleetDeployTarget & { builtIn?: true }) | undefined => {
106
166
  const repo = configuredArg?.repo ?? repoArg;
@@ -152,18 +212,30 @@ interface IDeployReadContext {
152
212
  fresh: boolean;
153
213
  }
154
214
 
215
+ /** A walk of the workspace roots: when, under which workspace generation, and what it found. */
216
+ interface IDeployDiscovery {
217
+ at: number;
218
+ workspace: number;
219
+ repositories: IFleetRepository[];
220
+ skipped: IFleetDiscoverySkip[];
221
+ }
222
+
155
223
  /** What the monitor holds about a target's probe, beyond what the views show: the failures in a row. */
156
224
  interface IDeployProbeRecord extends IFleetDeployProbeState {
157
225
  failures: number;
158
226
  }
159
227
 
160
228
  /**
161
- * Observes what each deploy target runs (AGL-911). Every interval it fetches the remotes of the repositories the board
162
- * and the targets name (remote main and the remote's release tags, which are the releases), lets the release reconciler
163
- * link and release what each tag holds, probes the configured targets and the built-in AGL controller, records an
164
- * observation when a target answers another version, commit or artifact set than its newest row, and publishes
165
- * `deploy.observed`, `deploy.probe.failed` (after two failures in a row) and `deploy.probe.recovered`. Status, history,
166
- * release state and where a ticket runs are computed when read. Git runs here, never inside a ticket transaction.
229
+ * Observes what each deploy target runs (AGL-911) and keeps the deploy status stored (AGL-1704). Every interval a pass
230
+ * fetches the remotes of the repositories the board and the targets name (remote main and the remote's release tags,
231
+ * which are the releases), lets the release reconciler link and release what each tag holds, probes the configured
232
+ * targets and the built-in AGL controller, records an observation when a target answers another version, commit or
233
+ * artifact set than its newest row, and publishes `deploy.observed`, `deploy.probe.failed` (after two failures in a row)
234
+ * and `deploy.probe.recovered`; then it computes. A computation reads the board, the last walk and the mirrored refs,
235
+ * and stores what it found as the controller's one `DeployStatusModel` document; status and release state answer from
236
+ * it, with the clock and the busy sessions applied when read (`deployStatusAt`). One runner (`request`) runs passes and
237
+ * computations one at a time; `refresh` asks it for a pass and waits for it. History and where a ticket runs are read when asked. Git runs here, never inside a ticket
238
+ * transaction.
167
239
  */
168
240
  export class DeployMonitor {
169
241
  private readonly now: () => number;
@@ -172,20 +244,41 @@ export class DeployMonitor {
172
244
  private readonly reconciler: ReleaseReconciler;
173
245
  private readonly probes = new Map<string, IDeployProbeRecord>();
174
246
  private readonly abort = new AbortController();
175
- private timer?: ReturnType<typeof setTimeout>;
176
- private running?: Promise<void>;
177
- private changed = false;
247
+ private started = false;
248
+ /** The next pass of the loop, armed when a pass ends. */
249
+ private passTimer?: ReturnType<typeof setTimeout>;
250
+ private settleTimer?: ReturnType<typeof setTimeout>;
251
+ /** When the first board change the settle timer waits for came, so the computation waits at most `ticketsMaxWaitMs`. */
252
+ private settleSince?: number;
253
+ /** The next boundary of a maintenance window of the status served, where the views hear of it (AGL-1704). */
254
+ private windowTimer?: ReturnType<typeof setTimeout>;
255
+ /** The run in progress: its outcome, and when the runner is done with it. */
256
+ private active?: { run: Promise<void>; ended: Promise<void> };
257
+ /**
258
+ * The one run that follows the active one, for every request made while it runs; a pass if any of them asked for one,
259
+ * and told to the views when it ends if any of them asked for that (`refresh`).
260
+ */
261
+ private next?: { kind: TDeployRun; announce: boolean; settled: PromiseWithResolvers<void> };
262
+ /** Reads waiting for the first status, woken whenever a run ends. */
263
+ private readonly runEnded = new Set<() => void>();
264
+ /** The status served, as last stored or loaded; absent until one is (a cold start). */
265
+ private model?: IDeployStatusModel & { revision: number };
266
+ /** The stored document's revision, also when its content did not parse, so the computation that overwrites it moves it on. */
267
+ private storedRevision?: number;
268
+ /** The last computation that failed while no later one succeeded. */
269
+ private failure?: IFleetDeployStatusFailure;
178
270
  /** One probe of a target at a time, so two never record the same change twice; a probe waits for no other target. */
179
271
  private readonly probing = new Map<string, Promise<unknown>>();
180
- private discovered?: { at: number; repositories: IFleetRepository[]; skipped: IFleetDiscoverySkip[] };
272
+ private discovered?: IDeployDiscovery;
273
+ /** The walk in progress and the workspace generation it walks: one at a time, and a read that needs no fresh one takes it while that is current. */
274
+ private walking?: { walk: Promise<IDeployDiscovery>; workspace: number };
275
+ /** Moves when the workspace roots may have changed (a standard directory, a project, the configuration): a walk before it is stale. */
276
+ private workspace = 0;
181
277
  private board?: { revision: number; tickets: ITicket[] };
182
- /** The tickets released in a version some observed target of their repository does not run, as last computed. */
278
+ /** The tickets released in a version some observed target of their repository does not run, as last stored. */
183
279
  private undeployedKeys: ReadonlySet<string> = new Set();
184
- /** Where each maintenance window stood at the last computation, so the views hear when one opens or ends. */
280
+ /** Where each maintenance window stood when the views last heard of the status, so the window timer tells when one opens or ends. */
185
281
  private windowStates = '';
186
- private refreshing: Promise<void> = Promise.resolve();
187
- private settleTimer?: ReturnType<typeof setTimeout>;
188
- private settling?: Promise<void>;
189
282
  /** Deploy plan questions by ticket, question and ticket revision. */
190
283
  private readonly questions = new Map<string, ITicketQuestion | undefined>();
191
284
  /** Fetch failures already logged, by repository, so a host offline for a day logs each failure once. */
@@ -200,56 +293,173 @@ export class DeployMonitor {
200
293
  this.reconciler = new ReleaseReconciler(this.facts, host.effects);
201
294
  }
202
295
 
296
+ /**
297
+ * Loads the stored status, before anything reads the board (AGL-1704): the `undeployed` AutoLabel shows the stored set
298
+ * from the first read on, and the views are answered at once. Content that does not parse is a cold start: nothing
299
+ * is served until the next computation, which overwrites it.
300
+ */
301
+ public async load(): Promise<void> {
302
+ let document: IDeployStatusDocument;
303
+ try {
304
+ const stored = await DeployStatusModel.exact.findStoredOne({ id: deployStatusId(this.host.controllerId()) });
305
+ if (!stored) return;
306
+ document = DeployStatusModel.exact.toPersisted(stored);
307
+ } catch (errorArg) {
308
+ this.recordFailure(new DeployStatusError('The stored deploy status could not be read.', { cause: errorArg }));
309
+ return;
310
+ }
311
+ this.storedRevision = document.revision;
312
+ const failure = document.failure === undefined ? undefined : deployStatusFailureSchema.safeParse(document.failure);
313
+ if (failure?.success) this.failure = failure.data;
314
+ const content = deployStatusContentSchema.safeParse(document.content);
315
+ if (!content.success) {
316
+ console.error('The stored deploy status does not parse; it is not served, and the next computation stores it anew.', content.error);
317
+ return;
318
+ }
319
+ this.model = { revision: document.revision, computedAt: document.computedAt, content: content.data };
320
+ this.undeployedKeys = new Set(content.data.undeployed);
321
+ this.windowStates = this.windowSignature(this.model);
322
+ this.armWindowTimer();
323
+ }
324
+
325
+ /** Starts the loop, once: a pass now (after the run in progress, if one runs), and one every interval after each pass. */
203
326
  public start(): void {
204
- if (this.abort.signal.aborted || this.timer || this.running) return;
205
- this.run();
327
+ if (this.abort.signal.aborted || this.started) return;
328
+ this.started = true;
329
+ void this.request('pass').catch(() => undefined);
206
330
  }
207
331
 
208
- /** A saved Configuration master may change the targets: probe now and schedule from there. */
332
+ /** A saved Configuration master may change the targets: walk again, probe now and schedule from there. */
209
333
  public configurationChanged(): void {
210
334
  if (this.abort.signal.aborted) return;
211
- this.discovered = undefined;
212
- this.passSoon();
335
+ this.workspace += 1;
336
+ void this.request('pass').catch(() => undefined);
213
337
  }
214
338
 
215
- /** A release went out: fetch its tag and release what it holds now, not at the next pass. */
339
+ /**
340
+ * A fleet release or landing pushed: fetch the remote now, not at the next pass. A release's tag releases what it
341
+ * holds; a landing moves remote main, which says what the next release ships.
342
+ */
216
343
  public reconcileSoon(): void {
217
344
  if (this.abort.signal.aborted) return;
218
- this.passSoon();
345
+ void this.request('pass').catch(() => undefined);
346
+ }
347
+
348
+ /**
349
+ * A standard project directory or a project was added or removed: the repositories the last walk found may not be
350
+ * the fleet's any more, so the next run walks again, also when a walk is in progress now. Nothing runs for it.
351
+ */
352
+ public workspaceChanged(): void {
353
+ this.workspace += 1;
219
354
  }
220
355
 
221
- /** The board changed: the undeployed set is computed again once the changes settle; nothing is fetched or probed. */
356
+ /**
357
+ * The board changed: the status is computed again once the changes settle, `ticketsSettleMs` after the last change
358
+ * and at most `ticketsMaxWaitMs` after the first one that waits; nothing is fetched or probed.
359
+ */
222
360
  public ticketsChanged(): void {
223
361
  if (this.abort.signal.aborted) return;
362
+ const now = this.now();
363
+ this.settleSince ??= now;
224
364
  clearTimeout(this.settleTimer);
225
- this.settleTimer = setTimeout(() => {
365
+ this.settleTimer = deployTimer(Math.min(deployMonitorDefaults.ticketsSettleMs, this.settleSince + deployMonitorDefaults.ticketsMaxWaitMs - now), () => {
226
366
  this.settleTimer = undefined;
227
- this.settling = (this.settling ?? Promise.resolve()).then(async () => {
228
- const scope = await this.host.scope();
229
- if (scope) await this.refreshUndeployed(scope.configuration);
230
- }).catch((errorArg: unknown) => {
231
- if (!this.abort.signal.aborted) console.error('Computing the undeployed tickets failed.', errorArg);
367
+ this.settleSince = undefined;
368
+ void this.request('compute').catch(() => undefined);
369
+ });
370
+ }
371
+
372
+ /**
373
+ * The one runner (AGL-1704): a pass or a computation now when none runs; while one runs, every request shares the one
374
+ * run that follows it (a pass when any of them asked for one). Each caller resolves with the first run that started
375
+ * after its request, and is refused with its failure. A stopped monitor runs nothing. `announceArg` tells the views
376
+ * when that run stored its status, also when nothing moved (`refresh`, so they show when it was checked).
377
+ */
378
+ public request(kindArg: TDeployRun, announceArg = false): Promise<void> {
379
+ if (this.abort.signal.aborted) return Promise.reject(new DeployError('deploy_unavailable', 'The deploy monitor is stopped.'));
380
+ if (!this.active) return this.begin(kindArg, announceArg);
381
+ if (this.next) {
382
+ if (kindArg === 'pass') this.next.kind = 'pass';
383
+ if (announceArg) this.next.announce = true;
384
+ return this.next.settled.promise;
385
+ }
386
+ this.next = { kind: kindArg, announce: announceArg, settled: Promise.withResolvers<void>() };
387
+ return this.next.settled.promise;
388
+ }
389
+
390
+ /**
391
+ * One full check now (AGL-1704): a pass, the one after the run in progress when one runs, which every request until
392
+ * it starts shares. It waits for that pass at most `refreshWaitMs` and the caller's signal: `refreshed` once it ended,
393
+ * `refreshing` while it runs on. Its end tells the views (`deployments.changed`) either way, also when nothing moved.
394
+ * A failed pass is refused with the failure it recorded beside the status (`deploy_unavailable` `failed`).
395
+ */
396
+ public async refresh(signalArg?: AbortSignal): Promise<IFleetDeployRefreshResult> {
397
+ await this.requireScope();
398
+ signalArg?.throwIfAborted();
399
+ let outcome: { failed: false } | { failed: true; error: unknown } | undefined;
400
+ const check = this.request('pass', true).then(() => { outcome = { failed: false }; }, (errorArg: unknown) => { outcome = { failed: true, error: errorArg }; });
401
+ const bound = AbortSignal.timeout(this.options.refreshWaitMs ?? fleetDeployRefreshWaitMs);
402
+ const signal = signalArg ? AbortSignal.any([signalArg, bound]) : bound;
403
+ if (!signal.aborted) {
404
+ await new Promise<void>((resolve) => {
405
+ const done = () => { signal.removeEventListener('abort', done); resolve(); };
406
+ signal.addEventListener('abort', done, { once: true });
407
+ void check.then(done);
232
408
  });
233
- }, deployMonitorDefaults.ticketsSettleMs);
234
- this.settleTimer.unref();
409
+ }
410
+ const checkedAt = this.model ? { checkedAt: this.model.computedAt } : {};
411
+ if (!outcome) {
412
+ signalArg?.throwIfAborted();
413
+ return { state: 'refreshing', ...checkedAt };
414
+ }
415
+ if (!outcome.failed) return { state: 'refreshed', ...checkedAt };
416
+ if (outcome.error instanceof DeployError) throw outcome.error;
417
+ if (this.abort.signal.aborted) throw new DeployError('deploy_unavailable', 'The deploy monitor stopped.');
418
+ const failure = this.failure;
419
+ throw new DeployError('deploy_unavailable', failure ? `The check failed: ${failure.message} (ref ${failure.reference})` : 'The check failed.',
420
+ 'failed', failure?.reference);
235
421
  }
236
422
 
237
- private passSoon(): void {
238
- if (this.running) { this.changed = true; return; }
239
- clearTimeout(this.timer); this.timer = undefined;
240
- this.run();
423
+ private begin(kindArg: TDeployRun, announceArg: boolean): Promise<void> {
424
+ if (kindArg === 'pass') { clearTimeout(this.passTimer); this.passTimer = undefined; }
425
+ const run = this.run(kindArg, announceArg);
426
+ this.active = { run, ended: run.catch(() => undefined).then(() => this.ended(kindArg)) };
427
+ return run;
241
428
  }
242
429
 
243
- private run(): void {
244
- this.running = this.pass().catch((errorArg: unknown) => {
245
- if (!this.abort.signal.aborted) console.error('The deploy monitor pass failed.', errorArg);
246
- }).then(() => {
247
- this.running = undefined;
248
- if (this.abort.signal.aborted) return;
249
- if (this.changed) { this.changed = false; this.run(); return; }
250
- this.timer = setTimeout(() => { this.timer = undefined; this.run(); }, deployMonitorDefaults.intervalMinutes * 60_000);
251
- this.timer.unref();
252
- });
430
+ /** A run ended: the one that waited starts, and the loop's next pass is armed after a pass. */
431
+ private ended(kindArg: TDeployRun): void {
432
+ this.active = undefined;
433
+ const next = this.next;
434
+ this.next = undefined;
435
+ if (this.abort.signal.aborted) next?.settled.reject(new DeployError('deploy_unavailable', 'The deploy monitor stopped.'));
436
+ else {
437
+ if (kindArg === 'pass' && next?.kind !== 'pass') {
438
+ this.passTimer = deployTimer(deployMonitorDefaults.intervalMinutes * 60_000, () => {
439
+ this.passTimer = undefined;
440
+ void this.request('pass').catch(() => undefined);
441
+ });
442
+ }
443
+ if (next) this.begin(next.kind, next.announce).then(next.settled.resolve, next.settled.reject);
444
+ }
445
+ for (const waiter of [...this.runEnded]) waiter();
446
+ }
447
+
448
+ /** One run. Without a configuration authority nothing is read; a failure keeps the stored status and is recorded beside it. */
449
+ private async run(kindArg: TDeployRun, announceArg: boolean): Promise<void> {
450
+ try {
451
+ const scope = await this.host.scope();
452
+ if (!scope) return;
453
+ const remotes = kindArg === 'pass' ? await this.passFetchAndProbe(scope) : undefined;
454
+ await this.compute(scope, kindArg, remotes, announceArg);
455
+ } catch (errorArg) {
456
+ if (this.abort.signal.aborted) throw errorArg;
457
+ this.recordFailure(errorArg);
458
+ await this.storeFailure();
459
+ // The views read again: the failure shows beside the status that stands, or a cold read names it.
460
+ if (!this.abort.signal.aborted) this.host.changed?.();
461
+ throw errorArg;
462
+ }
253
463
  }
254
464
 
255
465
  /** The configured targets and the built-in controller, which a configured controller target replaces. */
@@ -263,13 +473,11 @@ export class DeployMonitor {
263
473
  }
264
474
 
265
475
  /**
266
- * One pass: fetch the remotes the board and the targets name, release what their tags hold, probe every target, then
267
- * compute the tickets that do not run everywhere yet.
476
+ * A pass's network work: fetch the remotes the board and the targets name, release what their tags hold and probe
477
+ * every target. Answers the remotes as fetched, which the computation that follows reads.
268
478
  */
269
- public async pass(): Promise<void> {
270
- const scope = await this.host.scope();
271
- if (!scope) return;
272
- const targets = this.targets(scope.configuration);
479
+ private async passFetchAndProbe(scopeArg: { owner: string; configuration: IFleetConfiguration }): Promise<Map<string, IFleetRemoteState>> {
480
+ const targets = this.targets(scopeArg.configuration);
273
481
  const repositories = await this.repositories();
274
482
  const tickets = await this.tickets();
275
483
  const candidates = ReleaseReconciler.candidates(tickets, repositories);
@@ -278,22 +486,21 @@ export class DeployMonitor {
278
486
  ...candidates.flatMap((candidateArg) => candidateArg.repos), ...this.namedRepositories(tickets, repositories)]) repos.set(repo.path, repo);
279
487
  const remotes = await this.fetchRemotes([...repos.values()]);
280
488
  try {
281
- const released = await this.reconciler.reconcile(scope.owner, candidates.filter((candidateArg) => candidateArg.repos.some((repoArg) => remotes.has(repoArg.path))),
489
+ await this.reconciler.reconcile(scopeArg.owner, candidates.filter((candidateArg) => candidateArg.repos.some((repoArg) => remotes.has(repoArg.path))),
282
490
  remotes, this.abort.signal);
283
- if (released.length) this.host.changed?.();
284
491
  } catch (errorArg) {
285
492
  if (this.abort.signal.aborted) throw errorArg;
286
493
  console.error('Releasing the landed tickets the remote tags hold failed.', errorArg);
287
494
  }
288
495
  for (const target of targets) {
289
496
  this.abort.signal.throwIfAborted();
290
- try { await this.probeSerialized(scope.owner, target); }
497
+ try { await this.probeSerialized(scopeArg.owner, target); }
291
498
  catch (errorArg) {
292
499
  if (this.abort.signal.aborted) throw errorArg;
293
500
  console.error(`The deploy probe of ${target.id} failed.`, errorArg);
294
501
  }
295
502
  }
296
- await this.refreshUndeployed(scope.configuration);
503
+ return remotes;
297
504
  }
298
505
 
299
506
  /** The repositories the board names besides its landed tickets: release plans and `affects` links, whose releases matter. */
@@ -307,25 +514,39 @@ export class DeployMonitor {
307
514
  return [...names].flatMap((nameArg) => findFleetRepository(repositoriesArg, nameArg) ?? []);
308
515
  }
309
516
 
310
- /** Fetches each repository's remote, a few at a time; a failure keeps the last mirrored refs and is logged once. */
517
+ /**
518
+ * Fetches each repository's remote, a few at a time; a failed fetch keeps the last mirrored refs and is logged once.
519
+ * A remote whose mirrored refs cannot be read fails the pass, naming the repository, and starts no further fetch.
520
+ */
311
521
  private async fetchRemotes(reposArg: IFleetRepository[]): Promise<Map<string, IFleetRemoteState>> {
312
522
  const remotes = new Map<string, IFleetRemoteState>();
313
523
  const pending = [...reposArg];
524
+ let unread: DeployStatusError | undefined;
314
525
  const worker = async () => {
315
- for (let repo = pending.shift(); repo; repo = pending.shift()) {
526
+ for (let repo = pending.shift(); repo && !unread; repo = pending.shift()) {
316
527
  this.abort.signal.throwIfAborted();
317
528
  const failure = await this.facts.fetch(repo).catch((errorArg: unknown) => errorArg instanceof Error ? errorArg.message : 'git fetch failed');
318
529
  if (failure) {
319
530
  if (this.fetchFailures.get(repo.path) !== failure) console.error(`Fetching the remote of ${repo.id} failed; its last fetched tags stand: ${failure}`);
320
531
  this.fetchFailures.set(repo.path, failure);
321
532
  } else this.fetchFailures.delete(repo.path);
322
- remotes.set(repo.path, await this.facts.remote(repo));
533
+ try { remotes.set(repo.path, await this.facts.remote(repo)); }
534
+ catch (errorArg) {
535
+ this.abort.signal.throwIfAborted();
536
+ unread ??= this.unreadRemote(repo, errorArg);
537
+ }
323
538
  }
324
539
  };
325
540
  await Promise.all(Array.from({ length: Math.min(deployMonitorDefaults.fetchConcurrency, pending.length) }, worker));
541
+ if (unread) throw unread;
326
542
  return remotes;
327
543
  }
328
544
 
545
+ /** A remote whose mirrored refs could not be read: the computation fails, naming the repository, and never takes it as one without releases. */
546
+ private unreadRemote(repoArg: IFleetRepository, errorArg: unknown): DeployStatusError {
547
+ return new DeployStatusError(`Reading the fetched remote of ${fleetRepositorySlug(repoArg)} failed.`, { cause: errorArg });
548
+ }
549
+
329
550
  /**
330
551
  * The tickets the AutoLabel `undeployed` marks: released in a version that some observed target of the repository
331
552
  * does not run yet. A ticket holds the label until every target of its repository runs its release. The ticket store
@@ -336,39 +557,149 @@ export class DeployMonitor {
336
557
  }
337
558
 
338
559
  /**
339
- * After each pass, each probe and each settled board change: computes the undeployed set, closes the fulfilled deploy
340
- * plans, and tells the views when the set changed or a maintenance window opened, ended or got the owner's go. One
341
- * computation runs at a time, each after the last, so an older set never replaces a newer one and a plan is closed once.
560
+ * One computation, after each pass, each probe now and each settled board change: every target's status, every
561
+ * maintenance window, the release state and the undeployed set, from the board, the last walk and the mirrored refs.
562
+ * It closes the fulfilled deploy plans and stores what it found; it touches no network.
342
563
  */
343
- private refreshUndeployed(configurationArg: IFleetConfiguration): Promise<void> {
344
- const next = this.refreshing.then(() => this.computeUndeployed(configurationArg));
345
- this.refreshing = next.catch(() => undefined);
346
- return next;
347
- }
348
-
349
- private async computeUndeployed(configurationArg: IFleetConfiguration): Promise<void> {
564
+ private async compute(scopeArg: { owner: string; configuration: IFleetConfiguration }, kindArg: TDeployRun,
565
+ remotesArg?: Map<string, IFleetRemoteState>, announceArg = false): Promise<void> {
350
566
  const context = await this.context();
351
- const targets = this.targets(configurationArg);
352
- const next = new Set<string>();
567
+ for (const [path, remote] of remotesArg ?? []) context.remotes.set(path, Promise.resolve(remote));
568
+ const targets = this.targets(scopeArg.configuration);
569
+ const statuses: TDeployStoredTarget[] = [];
570
+ const undeployed = new Set<string>();
353
571
  for (const target of targets) {
354
572
  const { status, notRunning } = await this.targetState(target, context);
355
- for (const key of notRunning) next.add(key);
573
+ statuses.push(status);
574
+ for (const key of notRunning) undeployed.add(key);
356
575
  await this.closeFulfilledPlans(status, context);
357
576
  }
358
- const changed = [...new Set([...next, ...this.undeployedKeys])].filter((keyArg) => next.has(keyArg) !== this.undeployedKeys.has(keyArg));
359
- this.undeployedKeys = next;
360
- const windowStates = (await this.windows(context)).map((windowArg) => `${windowArg.key}:${windowArg.state}:${windowArg.go}`).join(',');
361
- const windowsMoved = windowStates !== this.windowStates;
362
- this.windowStates = windowStates;
363
- if (changed.length) this.host.undeployedChanged?.(changed.sort((leftArg, rightArg) => leftArg.localeCompare(rightArg, 'en', { numeric: true })));
364
- if (changed.length || windowsMoved) this.host.changed?.();
577
+ await this.store({ status: { targets: statuses, windows: await this.windows(context) },
578
+ release: { repositories: await this.releaseRepositories(context, targets) }, undeployed: [...undeployed].sort(byKey) }, kindArg, announceArg);
579
+ }
580
+
581
+ /**
582
+ * Stores a computation's content and serves it from then on. Content over `contentLimitBytes` of JSON fails the
583
+ * computation, and so does content its own schema refuses, since a stored status must load again. The document is
584
+ * written when the content changed and at least once per pass, so its `computedAt` says when the targets were last
585
+ * checked; its revision moves only when more changed than when a probe last answered, and the views hear of it then,
586
+ * when the failure they show clears and when a `refresh` asked (`announceArg`). A window's boundary changes no
587
+ * content: the window timer, armed again after every computation, tells of it.
588
+ */
589
+ private async store(contentArg: TDeployStatusContent, kindArg: TDeployRun, announceArg: boolean): Promise<void> {
590
+ const json = JSON.stringify(contentArg);
591
+ const bytes = Buffer.byteLength(json);
592
+ if (bytes > deployMonitorDefaults.contentLimitBytes) {
593
+ throw new DeployStatusError(`The deploy status is ${bytes} bytes of JSON, more than its bound of ${deployMonitorDefaults.contentLimitBytes}; nothing was stored.`);
594
+ }
595
+ const content = deployStatusContentSchema.parse(JSON.parse(json));
596
+ const previous = this.model;
597
+ const moved = !previous || !plugins.util.isDeepStrictEqual(withoutProbeTimes(previous.content), withoutProbeTimes(content));
598
+ if (previous && !moved && kindArg !== 'pass' && !this.failure && plugins.util.isDeepStrictEqual(previous.content, content)) {
599
+ this.armWindowTimer();
600
+ return;
601
+ }
602
+ const revision = moved ? (this.storedRevision ?? 0) + 1 : previous.revision;
603
+ const computedAt = new Date(this.now()).toISOString();
604
+ await this.persist({ revision, computedAt, content });
605
+ this.model = { revision, computedAt, content };
606
+ this.storedRevision = revision;
607
+ const hadFailure = this.failure !== undefined;
608
+ this.failure = undefined;
609
+ const before = this.undeployedKeys;
610
+ this.undeployedKeys = new Set(content.undeployed);
611
+ const changed = [...new Set([...content.undeployed, ...before])].filter((keyArg) => this.undeployedKeys.has(keyArg) !== before.has(keyArg));
612
+ if (changed.length) this.host.undeployedChanged?.(changed.sort(byKey));
613
+ if (moved || hadFailure || announceArg) {
614
+ // The views read the whole status again, the windows where they stand now included, and a failure they show is gone.
615
+ this.windowStates = this.windowSignature(this.model);
616
+ this.host.changed?.();
617
+ }
618
+ this.armWindowTimer();
619
+ }
620
+
621
+ /** Writes the controller's one status document; nothing is written once the monitor stopped. */
622
+ private async persist(fieldsArg: Pick<IDeployStatusDocument, 'revision' | 'computedAt' | 'content' | 'failure'>): Promise<void> {
623
+ this.abort.signal.throwIfAborted();
624
+ const controllerId = this.host.controllerId();
625
+ const id = deployStatusId(controllerId);
626
+ const current = await DeployStatusModel.exact.findStoredOne({ id });
627
+ this.abort.signal.throwIfAborted();
628
+ if (!current) {
629
+ const inserted = await DeployStatusModel.exact.insert({ id, controllerId, ...fieldsArg });
630
+ if (inserted.status === 'conflict') throw new Error('Another writer stored this controller\'s deploy status first.');
631
+ return;
632
+ }
633
+ const written = await DeployStatusModel.exact.transition({ current, change: (modelArg) => {
634
+ modelArg.revision = fieldsArg.revision;
635
+ modelArg.computedAt = fieldsArg.computedAt;
636
+ modelArg.content = fieldsArg.content;
637
+ modelArg.failure = fieldsArg.failure;
638
+ } });
639
+ if (written.status === 'concurrent_change') throw new Error('The stored deploy status changed while it was written.');
640
+ }
641
+
642
+ /** A failed computation: logged and journaled under the reference the failure keeps, its words AGL's own. */
643
+ private recordFailure(errorArg: unknown): void {
644
+ const message = errorArg instanceof DeployStatusError ? errorArg.message : 'Computing the deploy status failed.';
645
+ const reference = controllerFailureJournal.record({ operation: 'deploy status', cause: errorArg });
646
+ console.error(`${message} The stored deploy status stands (ref ${reference}).`, errorArg);
647
+ this.failure = { at: new Date(this.now()).toISOString(), message, reference };
648
+ }
649
+
650
+ /** Records the last failure beside the stored status, which stands; without a stored status, a cold read names it. */
651
+ private async storeFailure(): Promise<void> {
652
+ if (!this.model || !this.failure) return;
653
+ try {
654
+ await this.persist({ revision: this.model.revision, computedAt: this.model.computedAt, content: this.model.content, failure: { ...this.failure } });
655
+ } catch (errorArg) {
656
+ if (!this.abort.signal.aborted) console.error('Recording the failed deploy status computation failed.', errorArg);
657
+ }
658
+ }
659
+
660
+ /** Where each maintenance window of a status stands now and the owner's go, to tell when one opens or ends. */
661
+ private windowSignature(modelArg: IDeployStatusModel): string {
662
+ const now = this.now();
663
+ return modelArg.content.status.windows.map((windowArg) => `${windowArg.key}:${deployWindowState(windowArg, now)}:${windowArg.go}`).join(',');
664
+ }
665
+
666
+ /**
667
+ * Arms the window timer for the next time a maintenance window of the status served opens or ends (AGL-1704), at
668
+ * once when one did since the views last heard of the status. The content does not change at a boundary, so no
669
+ * computation tells of it: the timer does. A boundary further away than a timer holds wakes it early, and it arms
670
+ * itself again.
671
+ */
672
+ private armWindowTimer(): void {
673
+ clearTimeout(this.windowTimer);
674
+ this.windowTimer = undefined;
675
+ if (this.abort.signal.aborted || !this.model) return;
676
+ const now = this.now();
677
+ const boundaries = this.model.content.status.windows.flatMap((windowArg) => [Date.parse(windowArg.from), Date.parse(windowArg.to)])
678
+ .filter((timeArg) => timeArg > now);
679
+ const due = this.windowSignature(this.model) !== this.windowStates ? now : boundaries.length ? Math.min(...boundaries) : undefined;
680
+ if (due === undefined) return;
681
+ this.windowTimer = deployTimer(due - now, () => {
682
+ this.windowTimer = undefined;
683
+ this.windowBoundary();
684
+ });
685
+ }
686
+
687
+ /** The window timer fired: tells the views when a window opened or ended, and arms itself for the next boundary. */
688
+ private windowBoundary(): void {
689
+ if (this.abort.signal.aborted || !this.model) return;
690
+ const windowStates = this.windowSignature(this.model);
691
+ if (windowStates !== this.windowStates) {
692
+ this.windowStates = windowStates;
693
+ this.host.changed?.();
694
+ }
695
+ this.armWindowTimer();
365
696
  }
366
697
 
367
698
  /**
368
699
  * A deploy plan is done once its target is observed running its release or a newer one: AGL closes its ticket and
369
700
  * says so, whoever deployed it. A failed close is tried again at the next computation.
370
701
  */
371
- private async closeFulfilledPlans(statusArg: IFleetDeployTargetStatus, contextArg: IDeployReadContext): Promise<void> {
702
+ private async closeFulfilledPlans(statusArg: TDeployStoredTarget, contextArg: IDeployReadContext): Promise<void> {
372
703
  const running = statusArg.running;
373
704
  if (!running) return;
374
705
  const repo = findFleetRepository(contextArg.repositories, statusArg.target.repo);
@@ -386,50 +717,89 @@ export class DeployMonitor {
386
717
  }
387
718
  }
388
719
 
389
- /** Probes one target now, as a deploy path asks after a deploy; it claims nothing, waits for no pass, and answers the target's status. */
390
- public async probe(targetIdArg: string): Promise<IFleetDeployTargetStatus> {
720
+ /**
721
+ * Probes one target now, as a deploy path asks after a deploy, and computes after it: it claims nothing, and answers
722
+ * the target's status as stored then.
723
+ */
724
+ public async probe(targetIdArg: string, signalArg?: AbortSignal): Promise<IFleetDeployTargetStatus> {
391
725
  const scope = await this.requireScope();
392
726
  const target = this.targets(scope.configuration).find((targetArg) => targetArg.id === targetIdArg);
393
727
  if (!target) throw new DeployError('deploy_target_unknown', `No deploy target is named ${targetIdArg}.`);
394
728
  await this.probeSerialized(scope.owner, target);
395
- await this.refreshUndeployed(scope.configuration);
396
- return (await this.status(targetIdArg)).targets[0]!;
729
+ await this.request('compute');
730
+ return (await this.status(targetIdArg, signalArg)).targets[0]!;
397
731
  }
398
732
 
399
- public async status(targetIdArg?: string): Promise<IFleetDeployStatus> {
400
- const scope = await this.requireScope();
401
- const targets = this.targets(scope.configuration).filter((targetArg) => targetIdArg === undefined || targetArg.id === targetIdArg);
402
- if (targetIdArg !== undefined && !targets.length) throw new DeployError('deploy_target_unknown', `No deploy target is named ${targetIdArg}.`);
403
- const context = await this.context();
404
- const statuses: IFleetDeployTargetStatus[] = [];
405
- for (const target of targets) statuses.push(await this.targetStatus(target, context));
406
- return { checkedAt: new Date(this.now()).toISOString(), targets: statuses, windows: await this.windows(context) };
733
+ /**
734
+ * The deploy status as stored, at this moment (`deployStatusAt`): it walks nothing and reads no board. While the newest
735
+ * computation failed, its failure goes with it. Without a stored status it waits for the first computation, at most
736
+ * `firstStatusWaitMs`.
737
+ */
738
+ public async status(targetIdArg?: string, signalArg?: AbortSignal): Promise<IFleetDeployStatus> {
739
+ const model = await this.served(signalArg);
740
+ const status: IFleetDeployStatus = { ...deployStatusAt(model, this.now(), this.host.busySessions()),
741
+ ...(this.failure ? { failure: { ...this.failure } } : {}) };
742
+ if (targetIdArg === undefined) return status;
743
+ const targets = status.targets.filter((targetArg) => targetArg.target.id === targetIdArg);
744
+ if (!targets.length) throw new DeployError('deploy_target_unknown', `No deploy target is named ${targetIdArg}.`);
745
+ return { ...status, targets };
407
746
  }
408
747
 
409
748
  /**
410
- * Every maintenance window on a ticket that is not done, with where it stands now and the owner's go, the earliest
411
- * first. The owner's own window counts at once; one the driver proposed counts once the owner approves it.
749
+ * The status served. A cold start (nothing stored, or content that did not parse) waits for the run in progress and
750
+ * the one after it, bounded by the caller's signal and `firstStatusWaitMs`; then it is refused, saying whether the
751
+ * first computation still runs or failed.
412
752
  */
413
- private async windows(contextArg: IDeployReadContext): Promise<IFleetDeployWindow[]> {
414
- const now = this.now();
415
- const windows: IFleetDeployWindow[] = [];
753
+ private async served(signalArg?: AbortSignal): Promise<IDeployStatusModel> {
754
+ if (this.model) return this.model;
755
+ const bound = AbortSignal.timeout(this.options.firstStatusWaitMs ?? deployMonitorDefaults.firstStatusWaitMs);
756
+ const signal = signalArg ? AbortSignal.any([signalArg, bound]) : bound;
757
+ while (!this.model && this.active && !signal.aborted) {
758
+ await new Promise<void>((resolve) => {
759
+ const wake = () => { this.runEnded.delete(wake); signal.removeEventListener('abort', wake); resolve(); };
760
+ this.runEnded.add(wake);
761
+ signal.addEventListener('abort', wake, { once: true });
762
+ });
763
+ }
764
+ if (this.model) return this.model;
765
+ signalArg?.throwIfAborted();
766
+ if (this.active) {
767
+ throw new DeployError('deploy_unavailable', 'The deploy status is still being computed for the first time; ask again in a moment.', 'computing');
768
+ }
769
+ if (this.abort.signal.aborted) throw new DeployError('deploy_unavailable', 'The deploy monitor is stopped.');
770
+ if (this.failure) {
771
+ throw new DeployError('deploy_unavailable', `No deploy status is stored yet: ${this.failure.message} (ref ${this.failure.reference})`, 'failed',
772
+ this.failure.reference);
773
+ }
774
+ if (!await this.host.scope()) throw new DeployError('deploy_unavailable', 'The configuration authority is unavailable.');
775
+ throw new DeployError('deploy_unavailable', 'The deploy monitor has not run yet.');
776
+ }
777
+
778
+ /**
779
+ * Every maintenance window on a ticket that is not done, with the owner's go, the earliest first; where it stands is
780
+ * the clock's, derived when read (`deployWindowAt`). The owner's own window counts at once; one the driver proposed
781
+ * counts once the owner approves it.
782
+ */
783
+ private async windows(contextArg: IDeployReadContext): Promise<TDeployStoredWindow[]> {
784
+ const windows: TDeployStoredWindow[] = [];
416
785
  for (const ticket of contextArg.tickets) {
417
786
  const window = ticket.maintenanceWindow;
418
787
  if (!window || ticketDoneColumns.includes(ticket.columnId)) continue;
419
- const state = now < Date.parse(window.from) ? 'scheduled' : now < Date.parse(window.to) ? 'active' : 'done';
420
788
  const answer = window.questionId ? ownerAnswer(await this.deployQuestion(ticket, window.questionId)) : undefined;
421
789
  const go: TFleetDeployWindowGo = !window.questionId ? 'given' : !answer ? 'waiting'
422
790
  : answer.choices[0] === ticketWindowOptions.approve ? 'given' : 'declined';
423
- windows.push({ ...deployTicket(ticket), from: window.from, to: window.to, targets: [...window.targets], state, go,
791
+ windows.push({ ...deployTicket(ticket), from: window.from, to: window.to, targets: [...window.targets], go,
424
792
  ...(window.questionId ? { questionId: window.questionId } : {}) });
425
793
  }
426
794
  return windows.sort((leftArg, rightArg) => leftArg.from.localeCompare(rightArg.from) || leftArg.key.localeCompare(rightArg.key, 'en', { numeric: true }));
427
795
  }
428
796
 
429
- /** A target's deploy plans whose ticket is not done, with the owner's go from the answer to each plan's question. */
430
- private async deployPlans(contextArg: IDeployReadContext, targetArg: IFleetDeployTarget, conditionArg: TFleetDeployCondition,
431
- windowsArg: IFleetDeployWindow[]): Promise<IFleetDeployPlan[]> {
432
- const plans: IFleetDeployPlan[] = [];
797
+ /**
798
+ * A target's deploy plans whose ticket is not done, with the owner's go from the answer to each plan's question; the
799
+ * window a `window` go is queued into is the clock's, derived when read (`deployPlansAt`).
800
+ */
801
+ private async deployPlans(contextArg: IDeployReadContext, targetArg: IFleetDeployTarget, conditionArg: TFleetDeployCondition): Promise<TDeployStoredPlan[]> {
802
+ const plans: TDeployStoredPlan[] = [];
433
803
  for (const ticket of contextArg.tickets) {
434
804
  const plan = ticket.deployPlan;
435
805
  if (!plan || plan.target !== targetArg.id || ticketDoneColumns.includes(ticket.columnId)) continue;
@@ -437,10 +807,8 @@ export class DeployMonitor {
437
807
  const choice = answer?.choices[0];
438
808
  const go: TFleetDeployGo = !answer ? 'waiting' : choice === ticketDeployPlanOptions.now ? 'now'
439
809
  : choice === ticketDeployPlanOptions.later ? (conditionArg === 'idle' ? 'idle' : 'window') : 'hold';
440
- const next = go === 'window' ? windowsArg.find((windowArg) => windowArg.state !== 'done' && windowArg.go === 'given') : undefined;
441
810
  plans.push({ ...deployTicket(ticket), target: plan.target, version: plan.version, questionId: plan.questionId, go,
442
- ...(answer ? { answeredAt: answer.answeredAt } : {}), ...(next ? { window: next } : {}),
443
- ...(ticket.assignee ? { assignee: ticket.assignee.name } : {}) });
811
+ ...(answer ? { answeredAt: answer.answeredAt } : {}), ...(ticket.assignee ? { assignee: ticket.assignee.name } : {}) });
444
812
  }
445
813
  return plans.sort((leftArg, rightArg) => compareFleetVersions(rightArg.version, leftArg.version) || byPriority(leftArg, rightArg));
446
814
  }
@@ -467,44 +835,53 @@ export class DeployMonitor {
467
835
  }
468
836
 
469
837
  /**
470
- * The release state of one repository, or of every repository a target runs, a ticket plans or a landed ticket names:
471
- * the newest tag on the remote, the plans and the landed tickets the newest release does not hold.
838
+ * The release state of every repository a target runs, a ticket plans or a landed ticket names, as stored; or of one
839
+ * repository, read now: the newest tag on the remote, the plans and the landed tickets the newest release does not hold.
472
840
  */
473
- public async releaseStatus(repoArg?: string): Promise<IFleetReleaseStatus> {
841
+ public async releaseStatus(repoArg?: string, signalArg?: AbortSignal): Promise<IFleetReleaseStatus> {
842
+ if (repoArg === undefined) {
843
+ const model = await this.served(signalArg);
844
+ return { checkedAt: model.computedAt, repositories: structuredClone(model.content.release.repositories) };
845
+ }
474
846
  const scope = await this.requireScope();
475
847
  const context = await this.context();
476
- const targets = this.targets(scope.configuration);
477
- let repositories: IFleetRepository[];
478
- if (repoArg !== undefined) {
479
- const repo = await this.findRepository(context, repoArg);
480
- if (!repo) throw new DeployError('deploy_repository_unknown', `No single fleet repository is named ${repoArg}.`);
481
- repositories = [repo];
482
- } else {
483
- const named = new Map<string, IFleetRepository>();
484
- for (const repo of [...targets.flatMap((targetArg) => findFleetRepository(context.repositories, targetArg.repo) ?? []),
485
- ...ReleaseReconciler.candidates(context.tickets.filter((ticketArg) => ticketArg.columnId === 'landed'), context.repositories)
486
- .flatMap((candidateArg) => candidateArg.repos),
487
- ...context.tickets.flatMap((ticketArg) => ticketArg.autoLabels?.includes('planned') && ticketArg.releasePlan
488
- ? findFleetRepository(context.repositories, ticketArg.releasePlan.repo) ?? [] : [])]) named.set(repo.path, repo);
489
- repositories = [...named.values()];
490
- }
848
+ const repo = await this.findRepository(context, repoArg);
849
+ if (!repo) throw new DeployError('deploy_repository_unknown', `No single fleet repository is named ${repoArg}.`);
850
+ return { checkedAt: new Date(this.now()).toISOString(), repositories: [await this.repositoryRelease(context, repo, this.targets(scope.configuration))] };
851
+ }
852
+
853
+ /** The release state of the repositories the targets run, landed tickets name and planned tickets plan, by `org/name`. */
854
+ private async releaseRepositories(contextArg: IDeployReadContext, targetsArg: IFleetDeployTarget[]): Promise<IFleetRepositoryReleaseStatus[]> {
855
+ const named = new Map<string, IFleetRepository>();
856
+ for (const repo of [...targetsArg.flatMap((targetArg) => findFleetRepository(contextArg.repositories, targetArg.repo) ?? []),
857
+ ...ReleaseReconciler.candidates(contextArg.tickets.filter((ticketArg) => ticketArg.columnId === 'landed'), contextArg.repositories)
858
+ .flatMap((candidateArg) => candidateArg.repos),
859
+ ...contextArg.tickets.flatMap((ticketArg) => ticketArg.autoLabels?.includes('planned') && ticketArg.releasePlan
860
+ ? findFleetRepository(contextArg.repositories, ticketArg.releasePlan.repo) ?? [] : [])]) named.set(repo.path, repo);
491
861
  const statuses: IFleetRepositoryReleaseStatus[] = [];
492
- for (const repo of repositories.sort((leftArg, rightArg) => fleetRepositorySlug(leftArg).localeCompare(fleetRepositorySlug(rightArg)))) {
493
- const remote = await this.remote(context, repo);
494
- statuses.push({ repo: fleetRepositorySlug(repo), id: repo.id, ...(remote.releases[0] ? { latest: remote.releases[0] } : {}),
495
- plans: this.plans(context, repo), landedAfterLatest: await this.landedAfter(context, repo, remote),
496
- targets: targets.filter((targetArg) => fleetRepositoryMatches(repo, targetArg.repo)).map((targetArg) => targetArg.id) });
862
+ for (const repo of [...named.values()].sort((leftArg, rightArg) => fleetRepositorySlug(leftArg).localeCompare(fleetRepositorySlug(rightArg)))) {
863
+ statuses.push(await this.repositoryRelease(contextArg, repo, targetsArg));
497
864
  }
498
- return { checkedAt: new Date(this.now()).toISOString(), repositories: statuses };
865
+ return statuses;
499
866
  }
500
867
 
501
- /** Stops the loop and waits for a pass and the probes in progress. */
868
+ private async repositoryRelease(contextArg: IDeployReadContext, repoArg: IFleetRepository, targetsArg: IFleetDeployTarget[]): Promise<IFleetRepositoryReleaseStatus> {
869
+ const remote = await this.remote(contextArg, repoArg);
870
+ return { repo: fleetRepositorySlug(repoArg), id: repoArg.id, ...(remote.releases[0] ? { latest: remote.releases[0] } : {}),
871
+ plans: this.plans(contextArg, repoArg), landedAfterLatest: await this.landedAfter(contextArg, repoArg, remote),
872
+ targets: targetsArg.filter((targetArg) => fleetRepositoryMatches(repoArg, targetArg.repo)).map((targetArg) => targetArg.id) };
873
+ }
874
+
875
+ /**
876
+ * Stops the monitor: its timers are cleared, nothing runs or is written after it, and it waits for the run and the
877
+ * probes in progress; a run that waited is refused.
878
+ */
502
879
  public async close(): Promise<void> {
503
880
  this.abort.abort();
504
- clearTimeout(this.timer); this.timer = undefined;
505
- clearTimeout(this.settleTimer); this.settleTimer = undefined;
506
- await this.running;
507
- await this.settling;
881
+ clearTimeout(this.passTimer); this.passTimer = undefined;
882
+ clearTimeout(this.settleTimer); this.settleTimer = undefined; this.settleSince = undefined;
883
+ clearTimeout(this.windowTimer); this.windowTimer = undefined;
884
+ await this.active?.ended;
508
885
  await Promise.allSettled([...this.probing.values()]);
509
886
  }
510
887
 
@@ -528,11 +905,32 @@ export class DeployMonitor {
528
905
  return (await this.discovery(freshArg)).repositories;
529
906
  }
530
907
 
531
- private async discovery(freshArg = false): Promise<{ repositories: IFleetRepository[]; skipped: IFleetDiscoverySkip[] }> {
532
- if (!freshArg && this.discovered && this.now() - this.discovered.at < deployMonitorDefaults.discoveryTtlMs) return this.discovered;
533
- const { repositories, skipped } = await this.host.discover();
534
- this.discovered = { at: this.now(), repositories, skipped: skipped ?? [] };
535
- return this.discovered;
908
+ /**
909
+ * One walk at a time. A read that needs no fresh walk takes the last walk within the minute or the walk in progress,
910
+ * either only while no workspace change came after it began; any other read walks after the walk in progress, and
911
+ * joins the walk another such read began after it while that one is current.
912
+ */
913
+ private async discovery(freshArg = false): Promise<IDeployDiscovery> {
914
+ const discovered = this.discovered;
915
+ if (!freshArg && discovered?.workspace === this.workspace && this.now() - discovered.at < deployMonitorDefaults.discoveryTtlMs) return discovered;
916
+ let walking = this.walking;
917
+ if (walking && !freshArg && walking.workspace === this.workspace) return await walking.walk;
918
+ while (walking) {
919
+ await walking.walk.catch(() => undefined);
920
+ const next = this.walking;
921
+ if (!next || next === walking) break;
922
+ if (next.workspace === this.workspace) return await next.walk;
923
+ walking = next;
924
+ }
925
+ const workspace = this.workspace;
926
+ const walk = this.host.discover().then(({ repositories, skipped }) => {
927
+ this.discovered = { at: this.now(), workspace, repositories, skipped: skipped ?? [] };
928
+ return this.discovered;
929
+ });
930
+ const entry = { walk, workspace };
931
+ this.walking = entry;
932
+ try { return await walk; }
933
+ finally { if (this.walking === entry) this.walking = undefined; }
536
934
  }
537
935
 
538
936
  /**
@@ -577,14 +975,11 @@ export class DeployMonitor {
577
975
  return findFleetRepository(contextArg.repositories, nameArg);
578
976
  }
579
977
 
580
- /** A repository's remote as the last fetch mirrored it, read once per read. */
978
+ /** A repository's remote as the last fetch mirrored it, read once per read; one that cannot be read fails the read, naming it. */
581
979
  private remote(contextArg: IDeployReadContext, repoArg: IFleetRepository): Promise<IFleetRemoteState> {
582
980
  let remote = contextArg.remotes.get(repoArg.path);
583
981
  if (!remote) {
584
- remote = this.facts.remote(repoArg).catch((errorArg: unknown) => {
585
- console.error(`Reading the fetched remote of ${repoArg.id} failed.`, errorArg);
586
- return { releases: [] };
587
- });
982
+ remote = this.facts.remote(repoArg).catch((errorArg: unknown) => { throw this.unreadRemote(repoArg, errorArg); });
588
983
  contextArg.remotes.set(repoArg.path, remote);
589
984
  }
590
985
  return remote;
@@ -630,7 +1025,6 @@ export class DeployMonitor {
630
1025
  const downSince = previous?.state === 'down' ? previous.downSince ?? checkedAt : checkedAt;
631
1026
  this.probes.set(targetArg.id, { state: 'down', checkedAt, downSince, error: result.error, failures });
632
1027
  if (previous?.state !== 'down') {
633
- this.host.changed?.();
634
1028
  this.host.publish({ kind: 'deploy.probe.failed', target: targetArg.id, repo: targetArg.repo, error: result.error,
635
1029
  text: `${name} did not answer its probe at ${checkedAt}, ${failures} times in a row: ${result.error}` }, ownerArg);
636
1030
  }
@@ -641,7 +1035,6 @@ export class DeployMonitor {
641
1035
  this.host.publish({ kind: 'deploy.probe.recovered', target: targetArg.id, repo: targetArg.repo, version: result.version,
642
1036
  text: `${name} answers its probe again at ${checkedAt}; it was down since ${previous.downSince ?? previous.checkedAt}.` }, ownerArg);
643
1037
  }
644
- if (previous?.state !== 'up') this.host.changed?.();
645
1038
  await this.record(ownerArg, targetArg, result, checkedAt);
646
1039
  }
647
1040
 
@@ -660,7 +1053,6 @@ export class DeployMonitor {
660
1053
  version: resultArg.version, ...(resultArg.sha ? { sha: resultArg.sha } : {}), observedAt,
661
1054
  ...(newest ? { previousVersion: newest.version } : {}), artifacts: resultArg.artifacts };
662
1055
  if ((await DeployObservationModel.exact.insert(observation)).status === 'conflict') return;
663
- this.host.changed?.();
664
1056
  const name = targetArg.name ?? targetArg.id;
665
1057
  const rollback = newest !== undefined && compareFleetVersions(newest.version, resultArg.version) > 0;
666
1058
  const rebuilt = newest !== undefined && newest.version === resultArg.version;
@@ -698,13 +1090,9 @@ export class DeployMonitor {
698
1090
  return after;
699
1091
  }
700
1092
 
701
- private async targetStatus(targetArg: IFleetDeployTarget & { builtIn?: true }, contextArg: IDeployReadContext): Promise<IFleetDeployTargetStatus> {
702
- return (await this.targetState(targetArg, contextArg)).status;
703
- }
704
-
705
- /** A target's status, and the key of every ticket that does not run there; the status lists the first hundred. */
1093
+ /** A target's status as stored, and the key of every ticket that does not run there; the status lists the first hundred. */
706
1094
  private async targetState(targetArg: IFleetDeployTarget & { builtIn?: true }, contextArg: IDeployReadContext):
707
- Promise<{ status: IFleetDeployTargetStatus; notRunning: string[] }> {
1095
+ Promise<{ status: TDeployStoredTarget; notRunning: string[] }> {
708
1096
  const repo = findFleetRepository(contextArg.repositories, targetArg.repo);
709
1097
  const newest = await this.newestObservation(targetArg.id);
710
1098
  const remote: IFleetRemoteState = repo ? await this.remote(contextArg, repo) : { releases: [] };
@@ -727,7 +1115,8 @@ export class DeployMonitor {
727
1115
  }
728
1116
  }
729
1117
  notRunningTickets.sort((leftArg, rightArg) => compareFleetVersions(rightArg.version!, leftArg.version!));
730
- const status: IFleetDeployTargetStatus = {
1118
+ const deployWhen = this.deployWhen(targetArg);
1119
+ const status: TDeployStoredTarget = {
731
1120
  target: publicTarget(targetArg),
732
1121
  ...(repo ? { repository: fleetRepositorySlug(repo) } : {}),
733
1122
  ...(running ? { running } : {}),
@@ -737,24 +1126,18 @@ export class DeployMonitor {
737
1126
  landedAfterLatest: repo ? await this.landedAfter(contextArg, repo, remote) : [],
738
1127
  openBugs: running ? await this.openBugs(contextArg, repo, targetArg.repo, running.version, remote) : [],
739
1128
  plans: this.plans(contextArg, repo, targetArg.repo),
740
- ...await this.deployCondition(targetArg, contextArg),
1129
+ deployWhen,
1130
+ deployPlans: await this.deployPlans(contextArg, targetArg, deployWhen),
741
1131
  };
742
1132
  return { status, notRunning: notRunningTickets.map((ticketArg) => ticketArg.key) };
743
1133
  }
744
1134
 
745
1135
  /**
746
- * When a target's deploys may run, and whether they may now (AGL-909): any time; inside a maintenance window for a
747
- * target that needs one; for the AGL controller when no session is busy. With its windows and deploy plans.
1136
+ * When a target's deploys may run (AGL-909): any time; inside a maintenance window for a target that needs one; for
1137
+ * the AGL controller when no session is busy. Whether they may now is the clock's and the sessions', derived when read.
748
1138
  */
749
- private async deployCondition(targetArg: IFleetDeployTarget, contextArg: IDeployReadContext): Promise<Pick<IFleetDeployTargetStatus,
750
- 'deployWhen' | 'busySessions' | 'deployOpen' | 'windows' | 'deployPlans'>> {
751
- const deployWhen: TFleetDeployCondition = targetArg.probe.kind === 'controller' ? 'idle' : targetArg.deployWhen ?? 'anytime';
752
- const windows = (await this.windows(contextArg)).filter((windowArg) => windowArg.state !== 'done' && windowArg.targets.includes(targetArg.id));
753
- const busySessions = deployWhen === 'idle' ? this.host.busySessions() : undefined;
754
- const deployOpen = deployWhen === 'anytime'
755
- || (deployWhen === 'window' ? windows.some((windowArg) => windowArg.state === 'active' && windowArg.go === 'given') : busySessions === 0);
756
- return { deployWhen, ...(busySessions === undefined ? {} : { busySessions }), deployOpen, windows,
757
- deployPlans: await this.deployPlans(contextArg, targetArg, deployWhen, windows) };
1139
+ private deployWhen(targetArg: IFleetDeployTarget): TFleetDeployCondition {
1140
+ return targetArg.probe.kind === 'controller' ? 'idle' : targetArg.deployWhen ?? 'anytime';
758
1141
  }
759
1142
 
760
1143
  /** What the views show of a target's probe; the failures in a row stay with the monitor. */
@@ -840,15 +1223,18 @@ export class DeployMonitor {
840
1223
  ...(shippedIn ? { shippedIn } : {}) });
841
1224
  }
842
1225
  }
843
- const maintenanceWindow = (await this.windows(context)).find((windowArg) => windowArg.key === ticket.key);
1226
+ const now = this.now();
1227
+ const windows = (await this.windows(context)).map((windowArg) => deployWindowAt(windowArg, now));
1228
+ const maintenanceWindow = windows.find((windowArg) => windowArg.key === ticket.key);
844
1229
  const planTarget = ticket.deployPlan ? targets.find((targetArg) => targetArg.id === ticket.deployPlan!.target) : undefined;
845
- const condition = planTarget ? await this.deployCondition(planTarget, context) : undefined;
846
- const deployPlan = condition?.deployPlans.find((planArg) => planArg.key === ticket.key);
1230
+ const deployWhen = planTarget ? this.deployWhen(planTarget) : undefined;
1231
+ const stored = planTarget && deployWhen ? (await this.deployPlans(context, planTarget, deployWhen)).filter((planArg) => planArg.key === ticket.key) : [];
1232
+ const [deployPlan] = planTarget ? deployPlansAt(stored, deployTargetWindows(windows, planTarget.id)) : [];
847
1233
  return { ticketKey: ticket.key, landed, runs,
848
1234
  foundIn: ticket.links.flatMap((linkArg) => linkArg.kind === 'affects' ? [{ repo: linkArg.repo, version: linkArg.version }] : []),
849
1235
  ...(ticket.releasePlan ? { releasePlan: structuredClone(ticket.releasePlan) } : {}),
850
1236
  ...(maintenanceWindow ? { maintenanceWindow } : {}),
851
- ...(deployPlan && planTarget && condition ? { deployPlan: { plan: deployPlan, targetName: planTarget.name ?? planTarget.id,
852
- ...(planTarget.environment ? { environment: planTarget.environment } : {}), deployWhen: condition.deployWhen } } : {}) };
1237
+ ...(deployPlan && planTarget && deployWhen ? { deployPlan: { plan: deployPlan, targetName: planTarget.name ?? planTarget.id,
1238
+ ...(planTarget.environment ? { environment: planTarget.environment } : {}), deployWhen } } : {}) };
853
1239
  }
854
1240
  }