@defrex/autobuild-hosted-dispatcher 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -49,9 +49,13 @@ Dispatcher variables (server-only, like every secret; none reach the browser):
49
49
  - `AB_DISPATCHER_BUDGET_SECONDS`: per-invocation work budget (default 240,
50
50
  clamped 10–780); pair it with the route's `maxDuration` as described in the
51
51
  operator procedure.
52
- - `AB_DISPATCHER_TOKEN_TTL_SECONDS`: the TTL of the per-tick deployment
53
- operator token minted for guests (default 604800 — 7 days; minimum 3600).
54
- It must outlive your largest guest `timeoutSeconds` (e.g. 14400).
52
+ - `AB_DISPATCHER_TOKEN_TTL_SECONDS`: an upper bound on stored cron credential
53
+ lifetime (default 604800 — 7 days; minimum 3600). Actual expiry is capped at
54
+ the invocation deadline plus 60 seconds. Successful and failed ticks delete
55
+ their exact credential; spawned builds and harvest use independent grants
56
+ retired by durable lifecycle facts. Each served repository prunes at most
57
+ 100 expired/revoked dispatcher rows per tick. Legacy unexpired rows keep
58
+ their existing expiry and drain afterward; owner credentials are unaffected.
55
59
  - `CRON_SECRET`: the cron authorization shared secret. Unset or blank disables
56
60
  the endpoint entirely. It is never a signing input and is unrelated to
57
61
  `AB_STORE_SECRET`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defrex/autobuild-hosted-dispatcher",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "engines": {
package/src/dispatcher.ts CHANGED
@@ -1,4 +1,13 @@
1
- import { openPostgresCredentialStoreFromEnv } from '@defrex/autobuild-postgres-store/credentials'
1
+ import { ProjectSecretError } from '@defrex/autobuild/project-secrets'
2
+ import {
3
+ openPostgresProjectSecretsFromEnv,
4
+ type ProjectSecretsResolver,
5
+ type SecretSnapshot,
6
+ } from '@defrex/autobuild-postgres-store/project-secrets'
7
+ import {
8
+ openPostgresCredentialStoreFromEnv,
9
+ type DispatcherCredentialStore,
10
+ } from '@defrex/autobuild-postgres-store/credentials'
2
11
  import {
3
12
  openPostgresProjectRegistryFromEnv,
4
13
  normalizeProjectRepository,
@@ -19,9 +28,9 @@ import {
19
28
  * report, run boundary, and deposited `dispatcher-effective-config` artifact,
20
29
  * so the web dashboard's repository journal shows hosted activity exactly as
21
30
  * it shows a local one. Each project invocation issues an opaque stored
22
- * credential with immutable dispatcher purpose and configured expiry. It
23
- * grants that registered project's raw store and ticket access only. It stays
24
- * valid after tick return for spawned builds. Credentials never enter logs,
31
+ * credential with immutable dispatcher purpose and a short crash-safety expiry. It
32
+ * grants that registered project's raw store and ticket access only. It is
33
+ * live only during the tick; spawned builds use independent lifecycle-bound grants. Credentials never enter logs,
25
34
  * responses or errors.
26
35
  *
27
36
  * This surface is deliberately NOT routed through `createHostedStoreService`:
@@ -60,7 +69,7 @@ export interface HostedDispatcherConfig {
60
69
  /** Tick budget per invocation in seconds (default 240, inside Vercel's
61
70
  * 300 s default function duration). */
62
71
  budgetSeconds: number
63
- /** Minted guest-token TTL in seconds (default 7 days). */
72
+ /** Upper bound on cron credential TTL (default 7 days); actual expiry is at most deadline plus 60 seconds. */
64
73
  tokenTtlSeconds: number
65
74
  /** The cron authorization shared secret; blank/unset disables the endpoint. */
66
75
  cronSecret?: string
@@ -212,8 +221,8 @@ export function parseHostedDispatcherEnv(env: HostedDispatcherEnv): HostedDispat
212
221
  )
213
222
  if (tokenTtlSeconds < MIN_TOKEN_TTL_SECONDS) {
214
223
  throw new Error(
215
- `AB_DISPATCHER_TOKEN_TTL_SECONDS must be at least ${MIN_TOKEN_TTL_SECONDS} — the minted ` +
216
- 'credential must outlive a guest build lifetime',
224
+ `AB_DISPATCHER_TOKEN_TTL_SECONDS must be at least ${MIN_TOKEN_TTL_SECONDS}; ` +
225
+ 'this is an upper bound on cron credential TTL, independent of delegated guest authority',
217
226
  )
218
227
  }
219
228
  const githubTokenOverridesMap = githubTokenOverrides(env, repositories)
@@ -249,7 +258,16 @@ export interface HostedDispatcherTickSummary {
249
258
  }
250
259
 
251
260
  export interface HostedDispatcherOptions {
252
- issueDispatcherCredential?: (repo: string, creator: string, expiresAt: Date) => Promise<string>
261
+ openSecrets?: (env: HostedDispatcherEnv) => Promise<ProjectSecretsResolver>
262
+ /** Per-repository credential lifecycle; child grants are independent of these rows. */
263
+ openDispatcherCredentials?: (
264
+ env: HostedDispatcherEnv,
265
+ ) => Promise<
266
+ Pick<
267
+ DispatcherCredentialStore,
268
+ 'issue' | 'deleteDispatcherCredential' | 'pruneDispatcherCredentials' | 'close'
269
+ >
270
+ >
253
271
  openProjectRegistry?: (env: HostedDispatcherEnv) => Promise<ProjectRegistryReader>
254
272
  /** Service environment; defaults to `process.env`. */
255
273
  env?: HostedDispatcherEnv
@@ -341,6 +359,12 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
341
359
  return projectPromise
342
360
  }
343
361
 
362
+ let secrets: Promise<ProjectSecretsResolver> | undefined
363
+ const openSecrets = () =>
364
+ (secrets ??= (options.openSecrets ?? openPostgresProjectSecretsFromEnv)(env).catch((error) => {
365
+ secrets = undefined
366
+ throw error
367
+ }))
344
368
  const clock: Clock = options.clock ?? (() => new Date())
345
369
  const dispatch = options.dispatch ?? ((opts: DispatchOpts) => abDispatch(opts))
346
370
  const log = options.log ?? ((line: string) => console.log(line))
@@ -373,57 +397,91 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
373
397
  const stderr = options.stderr ?? ((line: string) => logError(`${kernelPrefix} ${line}`))
374
398
  const startedAt = Date.now()
375
399
  log(`${LOG_PREFIX} ${invocation}: ${repository} tick run=${runId}`)
376
- // Stored project authority outlives the tick for spawned builds.
377
- // This issuer cannot request owner/operator purpose.
378
- const issue =
379
- options.issueDispatcherCredential ??
380
- (async (repo: string, creator: string, expiresAt: Date) => {
381
- const credentials = await openPostgresCredentialStoreFromEnv(env)
382
- try {
383
- return (await credentials.issue(repo, creator, { purpose: 'dispatcher', expiresAt }))
384
- .token
385
- } finally {
386
- await credentials.close()
387
- }
388
- })
389
- let token: string
400
+ let snapshot: SecretSnapshot
401
+ let resolver: ProjectSecretsResolver | undefined
402
+ let resolutionFailed = false
390
403
  try {
391
- token = await issue(
404
+ resolver = await openSecrets()
405
+ snapshot = await resolver.resolveForTick(
392
406
  repository,
393
- `system:${runId}`,
394
- new Date(clock().getTime() + config.tokenTtlSeconds * 1000),
407
+ clock().getTime() + 600_000,
408
+ deadlineAt,
395
409
  )
396
410
  } catch (error) {
397
- const message = error instanceof Error ? error.message : String(error)
411
+ const message =
412
+ error instanceof ProjectSecretError ? error.message : 'project secrets unavailable'
398
413
  repositories.push({ repository, outcome: 'failed', runId, error: message })
399
- logError(
400
- `${LOG_PREFIX} ${invocation}: ${repository} credential issuance failed: ${errorDetail(error)}`,
401
- )
402
- continue
403
- }
404
- const childEnv: Record<string, string | undefined> = {
405
- ...env,
406
- AB_STORE: config.origin,
407
- AB_TOKEN: token,
408
- }
409
- // A Vercel Function receives the deployment's OIDC token as a request
410
- // header, not as VERCEL_OIDC_TOKEN; hand it to the kernel under that
411
- // name so the Sandbox SDK authenticates as the deployment. An explicit
412
- // environment token keeps precedence.
413
- if (!childEnv.VERCEL_OIDC_TOKEN && credentials.oidcToken) {
414
- childEnv.VERCEL_OIDC_TOKEN = credentials.oidcToken
415
- }
416
- // Per-repository forge identity: both variables are set to the
417
- // override so no reader (`githubTokenFromEnv` prefers GITHUB_TOKEN,
418
- // the publication path and init-validation accept either) can straddle
419
- // the override and the shared credential within one tick. Repositories
420
- // without an override keep the shared credential untouched.
421
- const forgeOverride = config.githubTokenOverrides.get(repository)
422
- if (forgeOverride !== undefined) {
423
- childEnv.GITHUB_TOKEN = forgeOverride
424
- childEnv.GH_TOKEN = forgeOverride
414
+ logError(`${LOG_PREFIX} ${invocation}: ${repository} ${message}`)
415
+ resolutionFailed = true
416
+ // Repair active policies without admitting any new work. If even the
417
+ // ready subset cannot be read (for example a missing key), skip safely.
418
+ if (!resolver?.resolveForObservation) continue
419
+ try {
420
+ snapshot = await resolver.resolveForObservation(repository)
421
+ } catch {
422
+ continue
423
+ }
425
424
  }
425
+ const redact = (line: string) =>
426
+ Object.values(snapshot.env)
427
+ .flatMap((value) => [
428
+ value,
429
+ JSON.stringify(value).slice(1, -1),
430
+ ...value.split(/\r?\n/),
431
+ ])
432
+ .filter(Boolean)
433
+ .sort((a, b) => b.length - a.length)
434
+ .reduce((text, value) => text.replaceAll(value, '[redacted]'), line)
435
+ let lifecycle:
436
+ | Awaited<ReturnType<NonNullable<HostedDispatcherOptions['openDispatcherCredentials']>>>
437
+ | undefined
438
+ let issued: { token: string; credential: { id: string } } | undefined
439
+ let failure: string | undefined
440
+ // Even markSupplied failures retire the exact issued row. Pool closure is
441
+ // independent of deletion success and never depends on lease ownership.
426
442
  try {
443
+ lifecycle = await (
444
+ options.openDispatcherCredentials ?? openPostgresCredentialStoreFromEnv
445
+ )(env)
446
+ await lifecycle.pruneDispatcherCredentials(repository, clock())
447
+ issued = await lifecycle.issue(repository, `system:${runId}`, {
448
+ purpose: 'dispatcher',
449
+ expiresAt: new Date(
450
+ Math.min(clock().getTime() + config.tokenTtlSeconds * 1000, deadlineAt + 60_000),
451
+ ),
452
+ })
453
+ const token = issued.token
454
+ const childEnv: Record<string, string | undefined> = {
455
+ ...env,
456
+ ...snapshot.env,
457
+ AB_STORE: config.origin,
458
+ AB_TOKEN: token,
459
+ ...(resolutionFailed
460
+ ? {
461
+ AB_PROJECT_SECRETS_FAILED: '1',
462
+ AB_PROJECT_SECRETS_UNAVAILABLE: JSON.stringify(snapshot.unavailable ?? []),
463
+ }
464
+ : {}),
465
+ }
466
+ delete childEnv.AB_PROJECT_SECRETS_KEY
467
+ // A Vercel Function receives the deployment's OIDC token as a request
468
+ // header, not as VERCEL_OIDC_TOKEN; hand it to the kernel under that
469
+ // name so the Sandbox SDK authenticates as the deployment. An explicit
470
+ // environment token keeps precedence.
471
+ if (!childEnv.VERCEL_OIDC_TOKEN && credentials.oidcToken) {
472
+ childEnv.VERCEL_OIDC_TOKEN = credentials.oidcToken
473
+ }
474
+ // Per-repository forge identity: both variables are set to the
475
+ // override so no reader (`githubTokenFromEnv` prefers GITHUB_TOKEN,
476
+ // the publication path and init-validation accept either) can straddle
477
+ // the override and the shared credential within one tick. Repositories
478
+ // without an override keep the shared credential untouched.
479
+ const forgeOverride = config.githubTokenOverrides.get(repository)
480
+ if (forgeOverride !== undefined) {
481
+ childEnv.GITHUB_TOKEN = forgeOverride
482
+ childEnv.GH_TOKEN = forgeOverride
483
+ }
484
+ await resolver!.markSupplied(snapshot)
427
485
  await dispatch({
428
486
  // `targetRepo` here is a caller-facing identity token, not a
429
487
  // path: origin mode (AUT-302) has no checkout. `abDispatch`
@@ -436,22 +494,51 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
436
494
  plain: true,
437
495
  env: childEnv,
438
496
  exec: spawnExec,
439
- stdout,
440
- stderr,
497
+ stdout: (line) => stdout(redact(line).replaceAll(token, '[redacted]')),
498
+ stderr: (line) => stderr(redact(line).replaceAll(token, '[redacted]')),
441
499
  kernelRunId: runId,
442
500
  deadlineAt,
443
501
  })
444
- repositories.push({ repository, outcome: 'ticked', runId })
502
+ } catch (error) {
503
+ failure = redact(error instanceof Error ? error.message : String(error))
504
+ // Explicit token redaction also covers errors from injected dispatchers.
505
+ if (issued) failure = failure.replaceAll(issued.token, '[redacted]')
506
+ let detail = redact(errorDetail(error))
507
+ if (issued) detail = detail.replaceAll(issued.token, '[redacted]')
508
+ logError(`${LOG_PREFIX} ${invocation}: ${repository} failed run=${runId}: ${detail}`)
509
+ } finally {
510
+ try {
511
+ if (issued)
512
+ await lifecycle!.deleteDispatcherCredential(repository, issued.credential.id)
513
+ } catch {
514
+ const message = 'dispatcher credential cleanup failed'
515
+ failure = failure ? `${failure}; ${message}` : message
516
+ logError(`${LOG_PREFIX} ${invocation}: ${repository} ${message}`)
517
+ } finally {
518
+ try {
519
+ await lifecycle?.close()
520
+ } catch {
521
+ const message = 'dispatcher credential storage close failed'
522
+ failure = failure ? `${failure}; ${message}` : message
523
+ logError(`${LOG_PREFIX} ${invocation}: ${repository} ${message}`)
524
+ }
525
+ }
526
+ }
527
+ if (!resolutionFailed)
528
+ repositories.push({
529
+ repository,
530
+ outcome: failure ? 'failed' : 'ticked',
531
+ runId,
532
+ ...(failure ? { error: failure } : {}),
533
+ })
534
+ else if (failure) {
535
+ const outcome = repositories.find((entry) => entry.runId === runId)!
536
+ outcome.error = `${outcome.error}; ${failure}`
537
+ }
538
+ if (!failure)
445
539
  log(
446
540
  `${LOG_PREFIX} ${invocation}: ${repository} ticked run=${runId} ms=${Date.now() - startedAt}`,
447
541
  )
448
- } catch (error) {
449
- const message = error instanceof Error ? error.message : String(error)
450
- repositories.push({ repository, outcome: 'failed', runId, error: message })
451
- logError(
452
- `${LOG_PREFIX} ${invocation}: ${repository} failed run=${runId} ms=${Date.now() - startedAt}: ${errorDetail(error)}`,
453
- )
454
- }
455
542
  }
456
543
  const counts = { ticked: 0, failed: 0, skipped: 0 }
457
544
  for (const entry of repositories) counts[entry.outcome] += 1