@akagilnc/pi-workflow-roles 0.1.4489 → 0.1.4508

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.
@@ -7666,10 +7666,12 @@ __export(invocation_exports, {
7666
7666
  deriveMergerEnvelopeFromActiveMerge: () => deriveMergerEnvelopeFromActiveMerge,
7667
7667
  ensureRunArtifactsDir: () => ensureRunArtifactsDir,
7668
7668
  freezeAttachmentsIntoRun: () => freezeAttachmentsIntoRun,
7669
+ freezePreparedAttachmentsIntoRun: () => freezePreparedAttachmentsIntoRun,
7669
7670
  homeFromRunDirectory: () => homeFromRunDirectory,
7670
7671
  isReviewerLens: () => isReviewerLens,
7671
7672
  issueAdmissionPlacement: () => issueAdmissionPlacement,
7672
7673
  loadAdmittedJudgeRequest: () => loadAdmittedJudgeRequest,
7674
+ materializeCountersignInvocation: () => materializeCountersignInvocation,
7673
7675
  observeLaunchedRolePackageIdentity: () => observeLaunchedRolePackageIdentity,
7674
7676
  parseAnalystArgv: () => parseAnalystArgv,
7675
7677
  parseAnalystCohortIssueToken: () => parseAnalystCohortIssueToken,
@@ -7693,6 +7695,7 @@ __export(invocation_exports, {
7693
7695
  parseReviewerArgv: () => parseReviewerArgv,
7694
7696
  parseSecretariatArgv: () => parseSecretariatArgv,
7695
7697
  persistAdmittedSourceRunPath: () => persistAdmittedSourceRunPath,
7698
+ recordAdmittedCorrelation: () => recordAdmittedCorrelation,
7696
7699
  recordEffectiveInvocationModel: () => recordEffectiveInvocationModel,
7697
7700
  recordLaunchedPiIdentity: () => recordLaunchedPiIdentity,
7698
7701
  recordLaunchedRolePackageIdentity: () => recordLaunchedRolePackageIdentity,
@@ -7701,17 +7704,21 @@ __export(invocation_exports, {
7701
7704
  requireReviewerBaseRevision: () => requireReviewerBaseRevision,
7702
7705
  requireReviewerLens: () => requireReviewerLens,
7703
7706
  resolveDoctorCaseRunsPath: () => resolveDoctorCaseRunsPath,
7704
- resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository
7707
+ resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository,
7708
+ withPreparedAttachments: () => withPreparedAttachments
7705
7709
  });
7706
7710
  import { execFileSync as execFileSync3 } from "node:child_process";
7707
7711
  import { existsSync as existsSync6 } from "node:fs";
7708
7712
  import {
7709
7713
  lstat as lstat4,
7714
+ mkdtemp as mkdtemp2,
7710
7715
  readFile as readFile10,
7711
7716
  realpath as realpath4,
7712
7717
  rename,
7718
+ rm as rm2,
7713
7719
  writeFile as writeFile5
7714
7720
  } from "node:fs/promises";
7721
+ import { tmpdir as tmpdir2 } from "node:os";
7715
7722
  import { basename as basename6, dirname as dirname9, isAbsolute as isAbsolute7, join as join17, resolve as resolve9, sep as sep4 } from "node:path";
7716
7723
  function issueAdmissionPlacement(authority, request) {
7717
7724
  const ledgerHome = resolveActivationLedgerHome(request.home);
@@ -7722,7 +7729,7 @@ function issueAdmissionPlacement(authority, request) {
7722
7729
  runId: request.runId,
7723
7730
  role: request.role
7724
7731
  });
7725
- ensureRoleRunPlacement(ledgerHome, placement);
7732
+ if (request.materialize !== false) ensureRoleRunPlacement(ledgerHome, placement);
7726
7733
  return {
7727
7734
  principal: authority.seal(placement),
7728
7735
  ...placement,
@@ -7847,6 +7854,31 @@ async function bindAdmittedTicketNumber(admitted, ticketNumber) {
7847
7854
  await bindTicketNumberOnRunDirectory(admitted.runDirectory, ticketNumber);
7848
7855
  admitted.ticketNumber = ticketNumber;
7849
7856
  }
7857
+ async function recordAdmittedCorrelation(admitted, correlationId) {
7858
+ const current = JSON.parse(
7859
+ await readFile10(admitted.admittedRequestPath, "utf8")
7860
+ );
7861
+ const prior = [
7862
+ ...Array.isArray(current.correlationIds) ? current.correlationIds.filter(
7863
+ (value) => typeof value === "string" && value.trim() !== ""
7864
+ ) : [],
7865
+ ...typeof current.correlationId === "string" && current.correlationId.trim() !== "" ? [current.correlationId] : []
7866
+ ];
7867
+ const correlationIds = [.../* @__PURE__ */ new Set([...prior, correlationId])];
7868
+ await writeFile5(
7869
+ admitted.admittedRequestPath,
7870
+ `${JSON.stringify({ ...current, correlationId, correlationIds }, null, 2)}
7871
+ `,
7872
+ "utf8"
7873
+ );
7874
+ await mergeInvocationIdentityPage(admitted.runDirectory, {
7875
+ correlationId,
7876
+ correlationIds
7877
+ });
7878
+ const mutable = admitted;
7879
+ mutable.correlationId = correlationId;
7880
+ mutable.correlationIds = correlationIds;
7881
+ }
7850
7882
  async function bindCourtTicketNumbersOnAdmitted(admitted, courtTicketNumbers) {
7851
7883
  if (admitted.ticketNumber === void 0) {
7852
7884
  throw new Error(
@@ -8237,7 +8269,7 @@ function parseFixerArgv(args) {
8237
8269
  ...project === void 0 ? {} : { project }
8238
8270
  };
8239
8271
  }
8240
- async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
8272
+ async function readRegularFileAttachment(sourcePath) {
8241
8273
  const absolute = isAbsolute7(sourcePath) ? sourcePath : resolve9(sourcePath);
8242
8274
  let st;
8243
8275
  try {
@@ -8253,18 +8285,72 @@ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
8253
8285
  `attachment must be a regular file (not a directory or symlink): ${sourcePath}`
8254
8286
  );
8255
8287
  }
8256
- const bytes = await readFile10(absolute);
8257
- const name = `${String(index).padStart(2, "0")}-${basename6(absolute)}`;
8258
- const frozenPath = join17(destinationDir, name);
8288
+ try {
8289
+ return { absolute, bytes: await readFile10(absolute) };
8290
+ } catch (error) {
8291
+ throw new CliUsageError(
8292
+ `attachment is not a readable regular file: ${sourcePath}`,
8293
+ { cause: error }
8294
+ );
8295
+ }
8296
+ }
8297
+ async function removePreparedAttachmentDirectory(stagingDirectory) {
8298
+ await rm2(stagingDirectory, { recursive: true, force: true });
8299
+ }
8300
+ async function withPreparedAttachments(attachmentPaths, use) {
8301
+ if (attachmentPaths.length === 0) return await use([]);
8302
+ const stagingDirectory = await mkdtemp2(join17(tmpdir2(), "ak-role-attachments-"));
8303
+ const prepared = [];
8304
+ let result;
8305
+ try {
8306
+ for (let index = 0; index < attachmentPaths.length; index += 1) {
8307
+ const { absolute, bytes } = await readRegularFileAttachment(attachmentPaths[index]);
8308
+ const snapshotPath = join17(stagingDirectory, String(index).padStart(6, "0"));
8309
+ await writeFile5(snapshotPath, bytes);
8310
+ prepared.push({ absolute, snapshotPath });
8311
+ }
8312
+ result = await use(prepared);
8313
+ } catch (primary) {
8314
+ try {
8315
+ await removePreparedAttachmentDirectory(stagingDirectory);
8316
+ } catch (cleanup) {
8317
+ throw new AggregateError(
8318
+ [primary, cleanup],
8319
+ "attachment snapshot operation failed and cleanup also failed",
8320
+ { cause: primary }
8321
+ );
8322
+ }
8323
+ throw primary;
8324
+ }
8325
+ await removePreparedAttachmentDirectory(stagingDirectory);
8326
+ return result;
8327
+ }
8328
+ async function freezeAttachmentBytes(provenancePath, bytes, destinationDir, index) {
8329
+ const frozenPath = join17(
8330
+ destinationDir,
8331
+ `${String(index).padStart(2, "0")}-${basename6(provenancePath)}`
8332
+ );
8259
8333
  await writeFile5(frozenPath, bytes);
8260
8334
  return {
8261
- attachment: {
8262
- provenancePath: absolute,
8263
- frozenPath,
8264
- byteLength: bytes.byteLength,
8265
- sha256: sha256Hex(bytes),
8266
- mediaKind: "regular-file"
8267
- },
8335
+ provenancePath,
8336
+ frozenPath,
8337
+ byteLength: bytes.byteLength,
8338
+ sha256: sha256Hex(bytes),
8339
+ mediaKind: "regular-file"
8340
+ };
8341
+ }
8342
+ async function freezePreparedAttachment(prepared, destinationDir, index) {
8343
+ return freezeAttachmentBytes(
8344
+ prepared.absolute,
8345
+ await readFile10(prepared.snapshotPath),
8346
+ destinationDir,
8347
+ index
8348
+ );
8349
+ }
8350
+ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
8351
+ const { absolute, bytes } = await readRegularFileAttachment(sourcePath);
8352
+ return {
8353
+ attachment: await freezeAttachmentBytes(absolute, bytes, destinationDir, index),
8268
8354
  body: bytes
8269
8355
  };
8270
8356
  }
@@ -8287,6 +8373,17 @@ async function freezeAttachmentsIntoRun(attachmentPaths, runDirectory, summonsKe
8287
8373
  ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
8288
8374
  return freezeAttachments(attachmentPaths, attachmentsDirectory);
8289
8375
  }
8376
+ async function freezePreparedAttachmentsIntoRun(prepared, runDirectory, summonsKey) {
8377
+ if (prepared.length === 0) return [];
8378
+ const ledgerHome = resolveActivationLedgerHome(homeFromRunDirectory(runDirectory));
8379
+ const attachmentsDirectory = summonsKey === void 0 ? join17(runDirectory, "attachments") : join17(runDirectory, "attachments", summonsKey);
8380
+ ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
8381
+ const attachments = [];
8382
+ for (let index = 0; index < prepared.length; index += 1) {
8383
+ attachments.push(await freezePreparedAttachment(prepared[index], attachmentsDirectory, index));
8384
+ }
8385
+ return attachments;
8386
+ }
8290
8387
  function ticketAdmissionFields(ticketNumber) {
8291
8388
  if (ticketNumber === void 0) return {};
8292
8389
  return {
@@ -8426,9 +8523,10 @@ async function admitCountersignInvocation(options) {
8426
8523
  runId,
8427
8524
  role: "countersign",
8428
8525
  subject: { unbound: true },
8429
- home: options.home
8526
+ home: options.home,
8527
+ materialize: options.deferPersistence !== true
8430
8528
  });
8431
- const attachments = await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
8529
+ const attachments = options.deferPersistence === true ? [] : await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
8432
8530
  const instruction = options.instruction;
8433
8531
  const instructionEmpty = instruction.trim() === "";
8434
8532
  const admitted = {
@@ -8450,11 +8548,13 @@ async function admitCountersignInvocation(options) {
8450
8548
  }))
8451
8549
  };
8452
8550
  const admittedRequestPath = join17(runDirectory, "admitted-request.json");
8453
- await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
8454
- sessionDirectory,
8455
- sessionFile
8456
- });
8457
- await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
8551
+ if (options.deferPersistence !== true) {
8552
+ await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
8553
+ sessionDirectory,
8554
+ sessionFile
8555
+ });
8556
+ await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
8557
+ }
8458
8558
  return {
8459
8559
  role: "countersign",
8460
8560
  runId,
@@ -8469,6 +8569,29 @@ async function admitCountersignInvocation(options) {
8469
8569
  ...options.correlationId === void 0 ? {} : { correlationId: options.correlationId }
8470
8570
  };
8471
8571
  }
8572
+ async function materializeCountersignInvocation(admitted, options) {
8573
+ const placement = issueAdmissionPlacement(options.principalAuthority, {
8574
+ cwd: admitted.projectRoot,
8575
+ runId: admitted.runId,
8576
+ role: "countersign",
8577
+ subject: { unbound: true },
8578
+ home: options.home
8579
+ });
8580
+ const attachments = await freezePreparedAttachmentsIntoRun(
8581
+ options.preparedAttachments,
8582
+ admitted.runDirectory
8583
+ );
8584
+ admitted.attachments = attachments;
8585
+ await writeAdmittedRequestPersistence(admitted.admittedRequestPath, admitted, {
8586
+ sessionDirectory: placement.sessionDirectory,
8587
+ sessionFile: placement.sessionFile
8588
+ });
8589
+ await writeRoleInvocationLedger(
8590
+ { ...admitted, sessionDirectory: placement.sessionDirectory, sessionFile: placement.sessionFile },
8591
+ admitted.role,
8592
+ options.model
8593
+ );
8594
+ }
8472
8595
  function buildCountersignTransportPrompt(admitted, engineMaterial) {
8473
8596
  return buildInstructionTransportPrompt(admitted, engineMaterial);
8474
8597
  }
@@ -17466,8 +17589,8 @@ __export(case_dossier_delivery_exports, {
17466
17589
  loadCaseDossierReadingMaterial: () => loadCaseDossierReadingMaterial,
17467
17590
  projectCaseDossierPointerSection: () => projectCaseDossierPointerSection
17468
17591
  });
17469
- import { mkdtemp as mkdtemp2, readFile as readFile18, rm as rm2, writeFile as writeFile9 } from "node:fs/promises";
17470
- import { tmpdir as tmpdir2 } from "node:os";
17592
+ import { mkdtemp as mkdtemp3, readFile as readFile18, rm as rm3, writeFile as writeFile9 } from "node:fs/promises";
17593
+ import { tmpdir as tmpdir3 } from "node:os";
17471
17594
  import { join as join31 } from "node:path";
17472
17595
  async function projectCaseDossierPointerSection(input) {
17473
17596
  if (input.ticketNumber === void 0) {
@@ -17495,7 +17618,7 @@ async function deliverCaseDossierAsAttachment(input) {
17495
17618
  projectRoot: input.projectRoot,
17496
17619
  home: input.home
17497
17620
  });
17498
- const stagingDir = await mkdtemp2(join31(tmpdir2(), "ak-case-dossier-"));
17621
+ const stagingDir = await mkdtemp3(join31(tmpdir3(), "ak-case-dossier-"));
17499
17622
  try {
17500
17623
  const stagingPath = join31(stagingDir, CASE_DOSSIER_ATTACH_FILE);
17501
17624
  await writeFile9(stagingPath, `${section}
@@ -17506,7 +17629,7 @@ async function deliverCaseDossierAsAttachment(input) {
17506
17629
  CASE_DOSSIER_ATTACH_KEY
17507
17630
  );
17508
17631
  } finally {
17509
- await rm2(stagingDir, { recursive: true, force: true });
17632
+ await rm3(stagingDir, { recursive: true, force: true });
17510
17633
  }
17511
17634
  }
17512
17635
  async function loadCaseDossierReadingMaterial(runDirectory) {
@@ -19808,6 +19931,9 @@ function diaristAdapters(env) {
19808
19931
  shouldPresentSettled: () => true,
19809
19932
  // Resume interrupted state: board ticket already on pages, run still under unbound/.
19810
19933
  beforeDispatch: async (admitted, lease) => {
19934
+ if (env.correlationId !== void 0 && env.correlationId.trim() !== "") {
19935
+ await recordAdmittedCorrelation(admitted, env.correlationId);
19936
+ }
19811
19937
  await bindAndRelocateDiaristIfBoardBound(admitted, env.principalAuthority, lease);
19812
19938
  },
19813
19939
  // First post-turn assert: accept hook wrote board pages; relocate before lease release.
@@ -19992,7 +20118,8 @@ function courtDiaristEscalated(roleOutcome) {
19992
20118
  if (roleOutcome.kind === "audit_escalation") return true;
19993
20119
  if (roleOutcome.kind !== "accepted") return false;
19994
20120
  return (roleOutcome.payloads ?? []).some((payload) => {
19995
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return false;
20121
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload))
20122
+ return false;
19996
20123
  const record4 = payload;
19997
20124
  return record4.status === "escalate" || record4.countersignStatus === "escalate";
19998
20125
  });
@@ -20045,7 +20172,10 @@ async function invokeCourtDiarist(input, env, io) {
20045
20172
  }
20046
20173
  const asserted = result.admitted?.ticketNumber;
20047
20174
  if (isSafePositiveTicketNumber(asserted)) {
20048
- const courtTicketNumbers = courtTicketNumbersFromOutcome(roleOutcome, asserted);
20175
+ const courtTicketNumbers = courtTicketNumbersFromOutcome(
20176
+ roleOutcome,
20177
+ asserted
20178
+ );
20049
20179
  return {
20050
20180
  identity: {
20051
20181
  kind: "ticket",
@@ -20094,7 +20224,9 @@ async function runCountersignCourtDiaristStation(admitted, env, io) {
20094
20224
  );
20095
20225
  }
20096
20226
  if (outcome.failedWithoutEscalate !== void 0) {
20097
- throw new StationChildExhaustedError(outcome.failedWithoutEscalate.diagnostic);
20227
+ throw new StationChildExhaustedError(
20228
+ outcome.failedWithoutEscalate.diagnostic
20229
+ );
20098
20230
  }
20099
20231
  }
20100
20232
  }
@@ -20120,7 +20252,8 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
20120
20252
  ...parsed.project === void 0 ? {} : { project: parsed.project },
20121
20253
  ...env.createRunId === void 0 ? {} : { createRunId: env.createRunId },
20122
20254
  ...env.model === void 0 ? {} : { model: env.model },
20123
- ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId }
20255
+ ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId },
20256
+ deferPersistence: true
20124
20257
  });
20125
20258
  } catch (error) {
20126
20259
  if (error instanceof CliUsageError) {
@@ -20129,160 +20262,216 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
20129
20262
  }
20130
20263
  throw error;
20131
20264
  }
20132
- await markRunAdmitted(admitted, env.principalAuthority);
20133
- let typedTicket;
20134
- let typedCourtTicketNumbers;
20135
- let identityDiaristRan = false;
20136
- if (env.runCourtDiaristStation === void 0) {
20137
- const outcome = await invokeCourtDiarist(
20138
- {
20139
- instruction: parsed.instruction,
20140
- projectRoot: admitted.projectRoot,
20141
- failureLabel: "unbound summons",
20142
- correlationId: admitted.runId
20143
- },
20144
- env,
20145
- io
20146
- );
20147
- identityDiaristRan = true;
20148
- if (outcome.identity.kind === "escalate") {
20149
- return await presentControlledFailure2(
20150
- admitted,
20151
- {
20152
- timedOut: false,
20153
- code: null,
20154
- stderr: "",
20155
- thrown: new Error(
20156
- "court diarist station escalated (cannot identify court target)"
20157
- )
20158
- },
20159
- countersignAdapters(),
20160
- env.principalAuthority,
20161
- io
20162
- );
20163
- }
20164
- if (outcome.failedWithoutEscalate !== void 0) {
20165
- return await presentControlledFailure2(
20166
- admitted,
20167
- {
20168
- timedOut: false,
20169
- code: null,
20170
- stderr: "",
20171
- thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
20172
- },
20173
- countersignAdapters(),
20174
- env.principalAuthority,
20175
- io
20176
- );
20177
- }
20178
- if (outcome.identity.kind === "ticket") {
20179
- const assertedTicket = outcome.identity.ticketNumber;
20180
- typedTicket = assertedTicket;
20181
- typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
20182
- const summons = {
20183
- instruction: parsed.instruction,
20184
- instructionEmpty: parsed.instruction.trim() === "",
20185
- attachmentPaths: parsed.attachmentPaths
20186
- };
20187
- const resumed = await tryResumeSameTicketSeatRun({
20188
- home: env.home,
20189
- projectRoot: admitted.projectRoot,
20190
- role: "countersign",
20191
- ticketNumber: typedTicket,
20192
- freshSummons: env.freshSummons,
20193
- summons,
20194
- resume: async (runId, materials) => {
20195
- await bindAdmittedTicketNumber(admitted, assertedTicket);
20196
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
20197
- await markRunTerminal(admitted.runDirectory);
20198
- return await runPublicCountersignResume(
20199
- {
20200
- runId,
20201
- ...materials === void 0 ? {} : { summons: materials }
20202
- },
20203
- {
20204
- ...env,
20205
- ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
20206
- },
20207
- io
20208
- );
20265
+ try {
20266
+ return await withPreparedAttachments(
20267
+ parsed.attachmentPaths,
20268
+ async (preparedAttachments) => {
20269
+ const materializeAdmission = async () => {
20270
+ await materializeCountersignInvocation(admitted, {
20271
+ home: env.home,
20272
+ principalAuthority: env.principalAuthority,
20273
+ preparedAttachments,
20274
+ ...env.model === void 0 ? {} : { model: env.model }
20275
+ });
20276
+ };
20277
+ let typedTicket;
20278
+ let typedCourtTicketNumbers;
20279
+ let identityDiaristRan = false;
20280
+ if (env.runCourtDiaristStation === void 0) {
20281
+ let outcome;
20282
+ try {
20283
+ outcome = await invokeCourtDiarist(
20284
+ {
20285
+ instruction: parsed.instruction,
20286
+ projectRoot: admitted.projectRoot,
20287
+ failureLabel: "unbound summons"
20288
+ },
20289
+ env,
20290
+ io
20291
+ );
20292
+ } catch (error) {
20293
+ await materializeAdmission();
20294
+ await markRunAdmitted(admitted, env.principalAuthority);
20295
+ return await presentControlledFailure2(
20296
+ admitted,
20297
+ { timedOut: false, code: null, stderr: "", thrown: error },
20298
+ countersignAdapters(),
20299
+ env.principalAuthority,
20300
+ io
20301
+ );
20302
+ }
20303
+ identityDiaristRan = true;
20304
+ if (outcome.identity.kind === "escalate") {
20305
+ await materializeAdmission();
20306
+ await markRunAdmitted(admitted, env.principalAuthority);
20307
+ return await presentControlledFailure2(
20308
+ admitted,
20309
+ {
20310
+ timedOut: false,
20311
+ code: null,
20312
+ stderr: "",
20313
+ thrown: new Error(
20314
+ "court diarist station escalated (cannot identify court target)"
20315
+ )
20316
+ },
20317
+ countersignAdapters(),
20318
+ env.principalAuthority,
20319
+ io
20320
+ );
20321
+ }
20322
+ if (outcome.failedWithoutEscalate !== void 0) {
20323
+ await materializeAdmission();
20324
+ await markRunAdmitted(admitted, env.principalAuthority);
20325
+ return await presentControlledFailure2(
20326
+ admitted,
20327
+ {
20328
+ timedOut: false,
20329
+ code: null,
20330
+ stderr: "",
20331
+ thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
20332
+ },
20333
+ countersignAdapters(),
20334
+ env.principalAuthority,
20335
+ io
20336
+ );
20337
+ }
20338
+ if (outcome.identity.kind === "ticket") {
20339
+ const assertedTicket = outcome.identity.ticketNumber;
20340
+ typedTicket = assertedTicket;
20341
+ typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
20342
+ const summons = {
20343
+ instruction: parsed.instruction,
20344
+ instructionEmpty: parsed.instruction.trim() === ""
20345
+ };
20346
+ const resumed = await tryResumeSameTicketSeatRun({
20347
+ home: env.home,
20348
+ projectRoot: admitted.projectRoot,
20349
+ role: "countersign",
20350
+ ticketNumber: typedTicket,
20351
+ freshSummons: env.freshSummons,
20352
+ summons,
20353
+ resume: async (runId, materials) => {
20354
+ const retained = await loadResumableCountersignRun(
20355
+ env.home,
20356
+ runId,
20357
+ env.principalAuthority
20358
+ );
20359
+ if (retained.admitted === void 0) {
20360
+ throw new Error(
20361
+ `retained countersign run disappeared before resume: ${runId}`
20362
+ );
20363
+ }
20364
+ const frozenPaths = (await freezePreparedAttachmentsIntoRun(
20365
+ preparedAttachments,
20366
+ retained.admitted.runDirectory,
20367
+ `s-${Date.now().toString(36)}`
20368
+ )).map((attachment) => attachment.frozenPath);
20369
+ const preparedMaterials = {
20370
+ ...materials ?? {},
20371
+ ...frozenPaths.length === 0 ? {} : { attachmentPaths: frozenPaths }
20372
+ };
20373
+ return await runPublicCountersignResume(
20374
+ {
20375
+ runId,
20376
+ summons: preparedMaterials
20377
+ },
20378
+ {
20379
+ ...env,
20380
+ ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
20381
+ },
20382
+ io
20383
+ );
20384
+ }
20385
+ });
20386
+ if (resumed !== void 0) {
20387
+ return resumed;
20388
+ }
20389
+ }
20209
20390
  }
20210
- });
20211
- if (resumed !== void 0) {
20212
- return resumed;
20213
- }
20214
- }
20215
- }
20216
- if (identityDiaristRan && typedTicket !== void 0) {
20217
- try {
20218
- await bindAdmittedTicketNumber(admitted, typedTicket);
20219
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
20220
- await bindCourtTicketNumbersOnAdmitted(
20221
- admitted,
20222
- typedCourtTicketNumbers ?? [typedTicket]
20223
- );
20224
- } catch (error) {
20225
- return await presentControlledFailure2(
20226
- admitted,
20227
- {
20228
- timedOut: false,
20229
- code: null,
20230
- stderr: "",
20231
- thrown: error
20232
- },
20233
- countersignAdapters(),
20234
- env.principalAuthority,
20235
- io
20236
- );
20237
- }
20238
- }
20239
- const turnProjection = {
20240
- packageRoot: env.packageRoot,
20241
- home: env.home,
20242
- agentDir: env.agentDir,
20243
- ...env.model === void 0 ? {} : { model: env.model },
20244
- ...pickEngineAxis(env),
20245
- ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
20246
- ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
20247
- continuation: {
20248
- kind: "initial",
20249
- prompt: buildCountersignTransportPrompt(
20250
- admitted,
20251
- engineSessionMaterialFromOptions({
20252
- ...pickEngineAxis(env),
20253
- packageRoot: env.packageRoot
20254
- })
20255
- )
20256
- }
20257
- };
20258
- const turnRequest = buildCountersignTurnRequest(admitted, turnProjection);
20259
- const result = await runPostAdmissionOneShot({
20260
- admitted,
20261
- env,
20262
- io,
20263
- request: turnRequest,
20264
- adapters: countersignAdapters({
20265
- beforeDispatch: async (admittedSeat, lease) => {
20266
- if (!identityDiaristRan) {
20267
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
20268
- } else if (typedTicket !== void 0) {
20269
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
20391
+ await materializeAdmission();
20392
+ await markRunAdmitted(admitted, env.principalAuthority);
20393
+ if (identityDiaristRan && typedTicket !== void 0) {
20394
+ try {
20395
+ await bindAdmittedTicketNumber(admitted, typedTicket);
20396
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
20397
+ await bindCourtTicketNumbersOnAdmitted(
20398
+ admitted,
20399
+ typedCourtTicketNumbers ?? [typedTicket]
20400
+ );
20401
+ } catch (error) {
20402
+ return await presentControlledFailure2(
20403
+ admitted,
20404
+ {
20405
+ timedOut: false,
20406
+ code: null,
20407
+ stderr: "",
20408
+ thrown: error
20409
+ },
20410
+ countersignAdapters(),
20411
+ env.principalAuthority,
20412
+ io
20413
+ );
20414
+ }
20270
20415
  }
20271
- await relocateAdmittedRunToTicket(
20272
- admittedSeat,
20273
- env.principalAuthority,
20274
- lease
20275
- );
20276
- Object.assign(
20277
- turnRequest,
20278
- buildCountersignTurnRequest(admittedSeat, turnProjection)
20416
+ const turnProjection = {
20417
+ packageRoot: env.packageRoot,
20418
+ home: env.home,
20419
+ agentDir: env.agentDir,
20420
+ ...env.model === void 0 ? {} : { model: env.model },
20421
+ ...pickEngineAxis(env),
20422
+ ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
20423
+ ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
20424
+ continuation: {
20425
+ kind: "initial",
20426
+ prompt: buildCountersignTransportPrompt(
20427
+ admitted,
20428
+ engineSessionMaterialFromOptions({
20429
+ ...pickEngineAxis(env),
20430
+ packageRoot: env.packageRoot
20431
+ })
20432
+ )
20433
+ }
20434
+ };
20435
+ const turnRequest = buildCountersignTurnRequest(
20436
+ admitted,
20437
+ turnProjection
20279
20438
  );
20439
+ const result = await runPostAdmissionOneShot({
20440
+ admitted,
20441
+ env,
20442
+ io,
20443
+ request: turnRequest,
20444
+ adapters: countersignAdapters({
20445
+ beforeDispatch: async (admittedSeat, lease) => {
20446
+ if (!identityDiaristRan) {
20447
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
20448
+ } else if (typedTicket !== void 0) {
20449
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
20450
+ }
20451
+ await relocateAdmittedRunToTicket(
20452
+ admittedSeat,
20453
+ env.principalAuthority,
20454
+ lease
20455
+ );
20456
+ Object.assign(
20457
+ turnRequest,
20458
+ buildCountersignTurnRequest(admittedSeat, turnProjection)
20459
+ );
20460
+ }
20461
+ }),
20462
+ ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
20463
+ });
20464
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
20465
+ return result;
20280
20466
  }
20281
- }),
20282
- ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
20283
- });
20284
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
20285
- return result;
20467
+ );
20468
+ } catch (error) {
20469
+ if (error instanceof CliUsageError) {
20470
+ presentStructuralRejection(error, io);
20471
+ return { exitCode: 2 };
20472
+ }
20473
+ throw error;
20474
+ }
20286
20475
  }
20287
20476
  function countersignAdapters(options) {
20288
20477
  return {
@@ -20320,7 +20509,9 @@ async function runPublicCountersignResume(request, env, io) {
20320
20509
  adapters: countersignAdapters({
20321
20510
  beforeDispatch: async (admitted) => {
20322
20511
  if (admitted.courtTicketNumbersDamage !== void 0) {
20323
- throw new StationChildExhaustedError(admitted.courtTicketNumbersDamage);
20512
+ throw new StationChildExhaustedError(
20513
+ admitted.courtTicketNumbersDamage
20514
+ );
20324
20515
  }
20325
20516
  if (env.pendingCourtTicketNumbers !== void 0) {
20326
20517
  await bindCourtTicketNumbersOnAdmitted(
@@ -21417,7 +21608,7 @@ import { join as join38, sep as sep5 } from "node:path";
21417
21608
 
21418
21609
  // src/atomic-write.ts
21419
21610
  import { randomUUID as randomUUID7 } from "node:crypto";
21420
- import { rename as rename3, rm as rm3, writeFile as writeFile11 } from "node:fs/promises";
21611
+ import { rename as rename3, rm as rm4, writeFile as writeFile11 } from "node:fs/promises";
21421
21612
  import { dirname as dirname20, join as join37 } from "node:path";
21422
21613
  async function writeFileAtomically(destination, contents) {
21423
21614
  const parent = dirname20(destination);
@@ -21426,7 +21617,7 @@ async function writeFileAtomically(destination, contents) {
21426
21617
  await writeFile11(temporary, contents);
21427
21618
  await rename3(temporary, destination);
21428
21619
  } catch (error) {
21429
- await rm3(temporary, { force: true }).catch(() => void 0);
21620
+ await rm4(temporary, { force: true }).catch(() => void 0);
21430
21621
  throw error;
21431
21622
  }
21432
21623
  }