@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.
@@ -20675,10 +20675,12 @@ __export(invocation_exports, {
20675
20675
  deriveMergerEnvelopeFromActiveMerge: () => deriveMergerEnvelopeFromActiveMerge,
20676
20676
  ensureRunArtifactsDir: () => ensureRunArtifactsDir,
20677
20677
  freezeAttachmentsIntoRun: () => freezeAttachmentsIntoRun,
20678
+ freezePreparedAttachmentsIntoRun: () => freezePreparedAttachmentsIntoRun,
20678
20679
  homeFromRunDirectory: () => homeFromRunDirectory,
20679
20680
  isReviewerLens: () => isReviewerLens,
20680
20681
  issueAdmissionPlacement: () => issueAdmissionPlacement,
20681
20682
  loadAdmittedJudgeRequest: () => loadAdmittedJudgeRequest,
20683
+ materializeCountersignInvocation: () => materializeCountersignInvocation,
20682
20684
  observeLaunchedRolePackageIdentity: () => observeLaunchedRolePackageIdentity,
20683
20685
  parseAnalystArgv: () => parseAnalystArgv,
20684
20686
  parseAnalystCohortIssueToken: () => parseAnalystCohortIssueToken,
@@ -20702,6 +20704,7 @@ __export(invocation_exports, {
20702
20704
  parseReviewerArgv: () => parseReviewerArgv,
20703
20705
  parseSecretariatArgv: () => parseSecretariatArgv,
20704
20706
  persistAdmittedSourceRunPath: () => persistAdmittedSourceRunPath,
20707
+ recordAdmittedCorrelation: () => recordAdmittedCorrelation,
20705
20708
  recordEffectiveInvocationModel: () => recordEffectiveInvocationModel,
20706
20709
  recordLaunchedPiIdentity: () => recordLaunchedPiIdentity,
20707
20710
  recordLaunchedRolePackageIdentity: () => recordLaunchedRolePackageIdentity,
@@ -20710,17 +20713,21 @@ __export(invocation_exports, {
20710
20713
  requireReviewerBaseRevision: () => requireReviewerBaseRevision,
20711
20714
  requireReviewerLens: () => requireReviewerLens,
20712
20715
  resolveDoctorCaseRunsPath: () => resolveDoctorCaseRunsPath,
20713
- resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository
20716
+ resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository,
20717
+ withPreparedAttachments: () => withPreparedAttachments
20714
20718
  });
20715
20719
  import { execFileSync as execFileSync3 } from "node:child_process";
20716
20720
  import { existsSync as existsSync6 } from "node:fs";
20717
20721
  import {
20718
20722
  lstat as lstat4,
20723
+ mkdtemp,
20719
20724
  readFile as readFile11,
20720
20725
  realpath as realpath4,
20721
20726
  rename,
20727
+ rm,
20722
20728
  writeFile as writeFile5
20723
20729
  } from "node:fs/promises";
20730
+ import { tmpdir } from "node:os";
20724
20731
  import { basename as basename6, dirname as dirname9, isAbsolute as isAbsolute7, join as join15, resolve as resolve7, sep as sep4 } from "node:path";
20725
20732
  function issueAdmissionPlacement(authority, request) {
20726
20733
  const ledgerHome = resolveActivationLedgerHome(request.home);
@@ -20731,7 +20738,7 @@ function issueAdmissionPlacement(authority, request) {
20731
20738
  runId: request.runId,
20732
20739
  role: request.role
20733
20740
  });
20734
- ensureRoleRunPlacement(ledgerHome, placement);
20741
+ if (request.materialize !== false) ensureRoleRunPlacement(ledgerHome, placement);
20735
20742
  return {
20736
20743
  principal: authority.seal(placement),
20737
20744
  ...placement,
@@ -20856,6 +20863,31 @@ async function bindAdmittedTicketNumber(admitted, ticketNumber) {
20856
20863
  await bindTicketNumberOnRunDirectory(admitted.runDirectory, ticketNumber);
20857
20864
  admitted.ticketNumber = ticketNumber;
20858
20865
  }
20866
+ async function recordAdmittedCorrelation(admitted, correlationId) {
20867
+ const current = JSON.parse(
20868
+ await readFile11(admitted.admittedRequestPath, "utf8")
20869
+ );
20870
+ const prior = [
20871
+ ...Array.isArray(current.correlationIds) ? current.correlationIds.filter(
20872
+ (value) => typeof value === "string" && value.trim() !== ""
20873
+ ) : [],
20874
+ ...typeof current.correlationId === "string" && current.correlationId.trim() !== "" ? [current.correlationId] : []
20875
+ ];
20876
+ const correlationIds = [.../* @__PURE__ */ new Set([...prior, correlationId])];
20877
+ await writeFile5(
20878
+ admitted.admittedRequestPath,
20879
+ `${JSON.stringify({ ...current, correlationId, correlationIds }, null, 2)}
20880
+ `,
20881
+ "utf8"
20882
+ );
20883
+ await mergeInvocationIdentityPage(admitted.runDirectory, {
20884
+ correlationId,
20885
+ correlationIds
20886
+ });
20887
+ const mutable = admitted;
20888
+ mutable.correlationId = correlationId;
20889
+ mutable.correlationIds = correlationIds;
20890
+ }
20859
20891
  async function bindCourtTicketNumbersOnAdmitted(admitted, courtTicketNumbers) {
20860
20892
  if (admitted.ticketNumber === void 0) {
20861
20893
  throw new Error(
@@ -21246,7 +21278,7 @@ function parseFixerArgv(args) {
21246
21278
  ...project === void 0 ? {} : { project }
21247
21279
  };
21248
21280
  }
21249
- async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
21281
+ async function readRegularFileAttachment(sourcePath) {
21250
21282
  const absolute = isAbsolute7(sourcePath) ? sourcePath : resolve7(sourcePath);
21251
21283
  let st;
21252
21284
  try {
@@ -21262,18 +21294,72 @@ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
21262
21294
  `attachment must be a regular file (not a directory or symlink): ${sourcePath}`
21263
21295
  );
21264
21296
  }
21265
- const bytes = await readFile11(absolute);
21266
- const name = `${String(index).padStart(2, "0")}-${basename6(absolute)}`;
21267
- const frozenPath = join15(destinationDir, name);
21297
+ try {
21298
+ return { absolute, bytes: await readFile11(absolute) };
21299
+ } catch (error) {
21300
+ throw new CliUsageError(
21301
+ `attachment is not a readable regular file: ${sourcePath}`,
21302
+ { cause: error }
21303
+ );
21304
+ }
21305
+ }
21306
+ async function removePreparedAttachmentDirectory(stagingDirectory) {
21307
+ await rm(stagingDirectory, { recursive: true, force: true });
21308
+ }
21309
+ async function withPreparedAttachments(attachmentPaths, use) {
21310
+ if (attachmentPaths.length === 0) return await use([]);
21311
+ const stagingDirectory = await mkdtemp(join15(tmpdir(), "ak-role-attachments-"));
21312
+ const prepared = [];
21313
+ let result2;
21314
+ try {
21315
+ for (let index = 0; index < attachmentPaths.length; index += 1) {
21316
+ const { absolute, bytes } = await readRegularFileAttachment(attachmentPaths[index]);
21317
+ const snapshotPath = join15(stagingDirectory, String(index).padStart(6, "0"));
21318
+ await writeFile5(snapshotPath, bytes);
21319
+ prepared.push({ absolute, snapshotPath });
21320
+ }
21321
+ result2 = await use(prepared);
21322
+ } catch (primary) {
21323
+ try {
21324
+ await removePreparedAttachmentDirectory(stagingDirectory);
21325
+ } catch (cleanup) {
21326
+ throw new AggregateError(
21327
+ [primary, cleanup],
21328
+ "attachment snapshot operation failed and cleanup also failed",
21329
+ { cause: primary }
21330
+ );
21331
+ }
21332
+ throw primary;
21333
+ }
21334
+ await removePreparedAttachmentDirectory(stagingDirectory);
21335
+ return result2;
21336
+ }
21337
+ async function freezeAttachmentBytes(provenancePath, bytes, destinationDir, index) {
21338
+ const frozenPath = join15(
21339
+ destinationDir,
21340
+ `${String(index).padStart(2, "0")}-${basename6(provenancePath)}`
21341
+ );
21268
21342
  await writeFile5(frozenPath, bytes);
21269
21343
  return {
21270
- attachment: {
21271
- provenancePath: absolute,
21272
- frozenPath,
21273
- byteLength: bytes.byteLength,
21274
- sha256: sha256Hex(bytes),
21275
- mediaKind: "regular-file"
21276
- },
21344
+ provenancePath,
21345
+ frozenPath,
21346
+ byteLength: bytes.byteLength,
21347
+ sha256: sha256Hex(bytes),
21348
+ mediaKind: "regular-file"
21349
+ };
21350
+ }
21351
+ async function freezePreparedAttachment(prepared, destinationDir, index) {
21352
+ return freezeAttachmentBytes(
21353
+ prepared.absolute,
21354
+ await readFile11(prepared.snapshotPath),
21355
+ destinationDir,
21356
+ index
21357
+ );
21358
+ }
21359
+ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
21360
+ const { absolute, bytes } = await readRegularFileAttachment(sourcePath);
21361
+ return {
21362
+ attachment: await freezeAttachmentBytes(absolute, bytes, destinationDir, index),
21277
21363
  body: bytes
21278
21364
  };
21279
21365
  }
@@ -21296,6 +21382,17 @@ async function freezeAttachmentsIntoRun(attachmentPaths, runDirectory, summonsKe
21296
21382
  ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
21297
21383
  return freezeAttachments(attachmentPaths, attachmentsDirectory);
21298
21384
  }
21385
+ async function freezePreparedAttachmentsIntoRun(prepared, runDirectory, summonsKey) {
21386
+ if (prepared.length === 0) return [];
21387
+ const ledgerHome = resolveActivationLedgerHome(homeFromRunDirectory(runDirectory));
21388
+ const attachmentsDirectory = summonsKey === void 0 ? join15(runDirectory, "attachments") : join15(runDirectory, "attachments", summonsKey);
21389
+ ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
21390
+ const attachments = [];
21391
+ for (let index = 0; index < prepared.length; index += 1) {
21392
+ attachments.push(await freezePreparedAttachment(prepared[index], attachmentsDirectory, index));
21393
+ }
21394
+ return attachments;
21395
+ }
21299
21396
  function ticketAdmissionFields(ticketNumber) {
21300
21397
  if (ticketNumber === void 0) return {};
21301
21398
  return {
@@ -21435,9 +21532,10 @@ async function admitCountersignInvocation(options) {
21435
21532
  runId,
21436
21533
  role: "countersign",
21437
21534
  subject: { unbound: true },
21438
- home: options.home
21535
+ home: options.home,
21536
+ materialize: options.deferPersistence !== true
21439
21537
  });
21440
- const attachments = await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
21538
+ const attachments = options.deferPersistence === true ? [] : await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
21441
21539
  const instruction = options.instruction;
21442
21540
  const instructionEmpty = instruction.trim() === "";
21443
21541
  const admitted = {
@@ -21459,11 +21557,13 @@ async function admitCountersignInvocation(options) {
21459
21557
  }))
21460
21558
  };
21461
21559
  const admittedRequestPath = join15(runDirectory, "admitted-request.json");
21462
- await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
21463
- sessionDirectory,
21464
- sessionFile
21465
- });
21466
- await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
21560
+ if (options.deferPersistence !== true) {
21561
+ await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
21562
+ sessionDirectory,
21563
+ sessionFile
21564
+ });
21565
+ await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
21566
+ }
21467
21567
  return {
21468
21568
  role: "countersign",
21469
21569
  runId,
@@ -21478,6 +21578,29 @@ async function admitCountersignInvocation(options) {
21478
21578
  ...options.correlationId === void 0 ? {} : { correlationId: options.correlationId }
21479
21579
  };
21480
21580
  }
21581
+ async function materializeCountersignInvocation(admitted, options) {
21582
+ const placement = issueAdmissionPlacement(options.principalAuthority, {
21583
+ cwd: admitted.projectRoot,
21584
+ runId: admitted.runId,
21585
+ role: "countersign",
21586
+ subject: { unbound: true },
21587
+ home: options.home
21588
+ });
21589
+ const attachments = await freezePreparedAttachmentsIntoRun(
21590
+ options.preparedAttachments,
21591
+ admitted.runDirectory
21592
+ );
21593
+ admitted.attachments = attachments;
21594
+ await writeAdmittedRequestPersistence(admitted.admittedRequestPath, admitted, {
21595
+ sessionDirectory: placement.sessionDirectory,
21596
+ sessionFile: placement.sessionFile
21597
+ });
21598
+ await writeRoleInvocationLedger(
21599
+ { ...admitted, sessionDirectory: placement.sessionDirectory, sessionFile: placement.sessionFile },
21600
+ admitted.role,
21601
+ options.model
21602
+ );
21603
+ }
21481
21604
  function buildCountersignTransportPrompt(admitted, engineMaterial) {
21482
21605
  return buildInstructionTransportPrompt(admitted, engineMaterial);
21483
21606
  }
@@ -27545,8 +27668,8 @@ var init_ticket_provenance = __esm({
27545
27668
  });
27546
27669
 
27547
27670
  // src/public-cli/case-dossier-delivery.ts
27548
- import { mkdtemp, readFile as readFile18, rm, writeFile as writeFile8 } from "node:fs/promises";
27549
- import { tmpdir } from "node:os";
27671
+ import { mkdtemp as mkdtemp2, readFile as readFile18, rm as rm2, writeFile as writeFile8 } from "node:fs/promises";
27672
+ import { tmpdir as tmpdir2 } from "node:os";
27550
27673
  import { join as join26 } from "node:path";
27551
27674
  async function projectCaseDossierPointerSection(input) {
27552
27675
  if (input.ticketNumber === void 0) {
@@ -27574,7 +27697,7 @@ async function deliverCaseDossierAsAttachment(input) {
27574
27697
  projectRoot: input.projectRoot,
27575
27698
  home: input.home
27576
27699
  });
27577
- const stagingDir = await mkdtemp(join26(tmpdir(), "ak-case-dossier-"));
27700
+ const stagingDir = await mkdtemp2(join26(tmpdir2(), "ak-case-dossier-"));
27578
27701
  try {
27579
27702
  const stagingPath = join26(stagingDir, CASE_DOSSIER_ATTACH_FILE);
27580
27703
  await writeFile8(stagingPath, `${section}
@@ -27585,7 +27708,7 @@ async function deliverCaseDossierAsAttachment(input) {
27585
27708
  CASE_DOSSIER_ATTACH_KEY
27586
27709
  );
27587
27710
  } finally {
27588
- await rm(stagingDir, { recursive: true, force: true });
27711
+ await rm2(stagingDir, { recursive: true, force: true });
27589
27712
  }
27590
27713
  }
27591
27714
  var CASE_DOSSIER_SECTION_HEADING, UNBOUND_DOSSIER_PATH_HEADING, CASE_DOSSIER_ATTACH_KEY, CASE_DOSSIER_ATTACH_FILE;
@@ -30163,6 +30286,9 @@ function diaristAdapters(env) {
30163
30286
  shouldPresentSettled: () => true,
30164
30287
  // Resume interrupted state: board ticket already on pages, run still under unbound/.
30165
30288
  beforeDispatch: async (admitted, lease) => {
30289
+ if (env.correlationId !== void 0 && env.correlationId.trim() !== "") {
30290
+ await recordAdmittedCorrelation(admitted, env.correlationId);
30291
+ }
30166
30292
  await bindAndRelocateDiaristIfBoardBound(admitted, env.principalAuthority, lease);
30167
30293
  },
30168
30294
  // First post-turn assert: accept hook wrote board pages; relocate before lease release.
@@ -30714,7 +30840,8 @@ function courtDiaristEscalated(roleOutcome) {
30714
30840
  if (roleOutcome.kind === "audit_escalation") return true;
30715
30841
  if (roleOutcome.kind !== "accepted") return false;
30716
30842
  return (roleOutcome.payloads ?? []).some((payload) => {
30717
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return false;
30843
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload))
30844
+ return false;
30718
30845
  const record4 = payload;
30719
30846
  return record4.status === "escalate" || record4.countersignStatus === "escalate";
30720
30847
  });
@@ -30767,7 +30894,10 @@ async function invokeCourtDiarist(input, env, io) {
30767
30894
  }
30768
30895
  const asserted = result2.admitted?.ticketNumber;
30769
30896
  if (isSafePositiveTicketNumber(asserted)) {
30770
- const courtTicketNumbers = courtTicketNumbersFromOutcome(roleOutcome, asserted);
30897
+ const courtTicketNumbers = courtTicketNumbersFromOutcome(
30898
+ roleOutcome,
30899
+ asserted
30900
+ );
30771
30901
  return {
30772
30902
  identity: {
30773
30903
  kind: "ticket",
@@ -30816,7 +30946,9 @@ async function runCountersignCourtDiaristStation(admitted, env, io) {
30816
30946
  );
30817
30947
  }
30818
30948
  if (outcome.failedWithoutEscalate !== void 0) {
30819
- throw new StationChildExhaustedError(outcome.failedWithoutEscalate.diagnostic);
30949
+ throw new StationChildExhaustedError(
30950
+ outcome.failedWithoutEscalate.diagnostic
30951
+ );
30820
30952
  }
30821
30953
  }
30822
30954
  }
@@ -30842,7 +30974,8 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
30842
30974
  ...parsed.project === void 0 ? {} : { project: parsed.project },
30843
30975
  ...env.createRunId === void 0 ? {} : { createRunId: env.createRunId },
30844
30976
  ...env.model === void 0 ? {} : { model: env.model },
30845
- ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId }
30977
+ ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId },
30978
+ deferPersistence: true
30846
30979
  });
30847
30980
  } catch (error) {
30848
30981
  if (error instanceof CliUsageError) {
@@ -30851,160 +30984,216 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
30851
30984
  }
30852
30985
  throw error;
30853
30986
  }
30854
- await markRunAdmitted(admitted, env.principalAuthority);
30855
- let typedTicket;
30856
- let typedCourtTicketNumbers;
30857
- let identityDiaristRan = false;
30858
- if (env.runCourtDiaristStation === void 0) {
30859
- const outcome = await invokeCourtDiarist(
30860
- {
30861
- instruction: parsed.instruction,
30862
- projectRoot: admitted.projectRoot,
30863
- failureLabel: "unbound summons",
30864
- correlationId: admitted.runId
30865
- },
30866
- env,
30867
- io
30868
- );
30869
- identityDiaristRan = true;
30870
- if (outcome.identity.kind === "escalate") {
30871
- return await presentControlledFailure2(
30872
- admitted,
30873
- {
30874
- timedOut: false,
30875
- code: null,
30876
- stderr: "",
30877
- thrown: new Error(
30878
- "court diarist station escalated (cannot identify court target)"
30879
- )
30880
- },
30881
- countersignAdapters(),
30882
- env.principalAuthority,
30883
- io
30884
- );
30885
- }
30886
- if (outcome.failedWithoutEscalate !== void 0) {
30887
- return await presentControlledFailure2(
30888
- admitted,
30889
- {
30890
- timedOut: false,
30891
- code: null,
30892
- stderr: "",
30893
- thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
30894
- },
30895
- countersignAdapters(),
30896
- env.principalAuthority,
30897
- io
30898
- );
30899
- }
30900
- if (outcome.identity.kind === "ticket") {
30901
- const assertedTicket = outcome.identity.ticketNumber;
30902
- typedTicket = assertedTicket;
30903
- typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
30904
- const summons = {
30905
- instruction: parsed.instruction,
30906
- instructionEmpty: parsed.instruction.trim() === "",
30907
- attachmentPaths: parsed.attachmentPaths
30908
- };
30909
- const resumed = await tryResumeSameTicketSeatRun({
30910
- home: env.home,
30911
- projectRoot: admitted.projectRoot,
30912
- role: "countersign",
30913
- ticketNumber: typedTicket,
30914
- freshSummons: env.freshSummons,
30915
- summons,
30916
- resume: async (runId, materials) => {
30917
- await bindAdmittedTicketNumber(admitted, assertedTicket);
30918
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
30919
- await markRunTerminal(admitted.runDirectory);
30920
- return await runPublicCountersignResume(
30921
- {
30922
- runId,
30923
- ...materials === void 0 ? {} : { summons: materials }
30924
- },
30925
- {
30926
- ...env,
30927
- ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
30928
- },
30929
- io
30930
- );
30987
+ try {
30988
+ return await withPreparedAttachments(
30989
+ parsed.attachmentPaths,
30990
+ async (preparedAttachments) => {
30991
+ const materializeAdmission = async () => {
30992
+ await materializeCountersignInvocation(admitted, {
30993
+ home: env.home,
30994
+ principalAuthority: env.principalAuthority,
30995
+ preparedAttachments,
30996
+ ...env.model === void 0 ? {} : { model: env.model }
30997
+ });
30998
+ };
30999
+ let typedTicket;
31000
+ let typedCourtTicketNumbers;
31001
+ let identityDiaristRan = false;
31002
+ if (env.runCourtDiaristStation === void 0) {
31003
+ let outcome;
31004
+ try {
31005
+ outcome = await invokeCourtDiarist(
31006
+ {
31007
+ instruction: parsed.instruction,
31008
+ projectRoot: admitted.projectRoot,
31009
+ failureLabel: "unbound summons"
31010
+ },
31011
+ env,
31012
+ io
31013
+ );
31014
+ } catch (error) {
31015
+ await materializeAdmission();
31016
+ await markRunAdmitted(admitted, env.principalAuthority);
31017
+ return await presentControlledFailure2(
31018
+ admitted,
31019
+ { timedOut: false, code: null, stderr: "", thrown: error },
31020
+ countersignAdapters(),
31021
+ env.principalAuthority,
31022
+ io
31023
+ );
31024
+ }
31025
+ identityDiaristRan = true;
31026
+ if (outcome.identity.kind === "escalate") {
31027
+ await materializeAdmission();
31028
+ await markRunAdmitted(admitted, env.principalAuthority);
31029
+ return await presentControlledFailure2(
31030
+ admitted,
31031
+ {
31032
+ timedOut: false,
31033
+ code: null,
31034
+ stderr: "",
31035
+ thrown: new Error(
31036
+ "court diarist station escalated (cannot identify court target)"
31037
+ )
31038
+ },
31039
+ countersignAdapters(),
31040
+ env.principalAuthority,
31041
+ io
31042
+ );
31043
+ }
31044
+ if (outcome.failedWithoutEscalate !== void 0) {
31045
+ await materializeAdmission();
31046
+ await markRunAdmitted(admitted, env.principalAuthority);
31047
+ return await presentControlledFailure2(
31048
+ admitted,
31049
+ {
31050
+ timedOut: false,
31051
+ code: null,
31052
+ stderr: "",
31053
+ thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
31054
+ },
31055
+ countersignAdapters(),
31056
+ env.principalAuthority,
31057
+ io
31058
+ );
31059
+ }
31060
+ if (outcome.identity.kind === "ticket") {
31061
+ const assertedTicket = outcome.identity.ticketNumber;
31062
+ typedTicket = assertedTicket;
31063
+ typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
31064
+ const summons = {
31065
+ instruction: parsed.instruction,
31066
+ instructionEmpty: parsed.instruction.trim() === ""
31067
+ };
31068
+ const resumed = await tryResumeSameTicketSeatRun({
31069
+ home: env.home,
31070
+ projectRoot: admitted.projectRoot,
31071
+ role: "countersign",
31072
+ ticketNumber: typedTicket,
31073
+ freshSummons: env.freshSummons,
31074
+ summons,
31075
+ resume: async (runId, materials) => {
31076
+ const retained = await loadResumableCountersignRun(
31077
+ env.home,
31078
+ runId,
31079
+ env.principalAuthority
31080
+ );
31081
+ if (retained.admitted === void 0) {
31082
+ throw new Error(
31083
+ `retained countersign run disappeared before resume: ${runId}`
31084
+ );
31085
+ }
31086
+ const frozenPaths = (await freezePreparedAttachmentsIntoRun(
31087
+ preparedAttachments,
31088
+ retained.admitted.runDirectory,
31089
+ `s-${Date.now().toString(36)}`
31090
+ )).map((attachment) => attachment.frozenPath);
31091
+ const preparedMaterials = {
31092
+ ...materials ?? {},
31093
+ ...frozenPaths.length === 0 ? {} : { attachmentPaths: frozenPaths }
31094
+ };
31095
+ return await runPublicCountersignResume(
31096
+ {
31097
+ runId,
31098
+ summons: preparedMaterials
31099
+ },
31100
+ {
31101
+ ...env,
31102
+ ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
31103
+ },
31104
+ io
31105
+ );
31106
+ }
31107
+ });
31108
+ if (resumed !== void 0) {
31109
+ return resumed;
31110
+ }
31111
+ }
30931
31112
  }
30932
- });
30933
- if (resumed !== void 0) {
30934
- return resumed;
30935
- }
30936
- }
30937
- }
30938
- if (identityDiaristRan && typedTicket !== void 0) {
30939
- try {
30940
- await bindAdmittedTicketNumber(admitted, typedTicket);
30941
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
30942
- await bindCourtTicketNumbersOnAdmitted(
30943
- admitted,
30944
- typedCourtTicketNumbers ?? [typedTicket]
30945
- );
30946
- } catch (error) {
30947
- return await presentControlledFailure2(
30948
- admitted,
30949
- {
30950
- timedOut: false,
30951
- code: null,
30952
- stderr: "",
30953
- thrown: error
30954
- },
30955
- countersignAdapters(),
30956
- env.principalAuthority,
30957
- io
30958
- );
30959
- }
30960
- }
30961
- const turnProjection = {
30962
- packageRoot: env.packageRoot,
30963
- home: env.home,
30964
- agentDir: env.agentDir,
30965
- ...env.model === void 0 ? {} : { model: env.model },
30966
- ...pickEngineAxis(env),
30967
- ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
30968
- ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
30969
- continuation: {
30970
- kind: "initial",
30971
- prompt: buildCountersignTransportPrompt(
30972
- admitted,
30973
- engineSessionMaterialFromOptions({
30974
- ...pickEngineAxis(env),
30975
- packageRoot: env.packageRoot
30976
- })
30977
- )
30978
- }
30979
- };
30980
- const turnRequest = buildCountersignTurnRequest(admitted, turnProjection);
30981
- const result2 = await runPostAdmissionOneShot({
30982
- admitted,
30983
- env,
30984
- io,
30985
- request: turnRequest,
30986
- adapters: countersignAdapters({
30987
- beforeDispatch: async (admittedSeat, lease) => {
30988
- if (!identityDiaristRan) {
30989
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
30990
- } else if (typedTicket !== void 0) {
30991
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
31113
+ await materializeAdmission();
31114
+ await markRunAdmitted(admitted, env.principalAuthority);
31115
+ if (identityDiaristRan && typedTicket !== void 0) {
31116
+ try {
31117
+ await bindAdmittedTicketNumber(admitted, typedTicket);
31118
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
31119
+ await bindCourtTicketNumbersOnAdmitted(
31120
+ admitted,
31121
+ typedCourtTicketNumbers ?? [typedTicket]
31122
+ );
31123
+ } catch (error) {
31124
+ return await presentControlledFailure2(
31125
+ admitted,
31126
+ {
31127
+ timedOut: false,
31128
+ code: null,
31129
+ stderr: "",
31130
+ thrown: error
31131
+ },
31132
+ countersignAdapters(),
31133
+ env.principalAuthority,
31134
+ io
31135
+ );
31136
+ }
30992
31137
  }
30993
- await relocateAdmittedRunToTicket(
30994
- admittedSeat,
30995
- env.principalAuthority,
30996
- lease
30997
- );
30998
- Object.assign(
30999
- turnRequest,
31000
- buildCountersignTurnRequest(admittedSeat, turnProjection)
31138
+ const turnProjection = {
31139
+ packageRoot: env.packageRoot,
31140
+ home: env.home,
31141
+ agentDir: env.agentDir,
31142
+ ...env.model === void 0 ? {} : { model: env.model },
31143
+ ...pickEngineAxis(env),
31144
+ ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
31145
+ ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
31146
+ continuation: {
31147
+ kind: "initial",
31148
+ prompt: buildCountersignTransportPrompt(
31149
+ admitted,
31150
+ engineSessionMaterialFromOptions({
31151
+ ...pickEngineAxis(env),
31152
+ packageRoot: env.packageRoot
31153
+ })
31154
+ )
31155
+ }
31156
+ };
31157
+ const turnRequest = buildCountersignTurnRequest(
31158
+ admitted,
31159
+ turnProjection
31001
31160
  );
31161
+ const result2 = await runPostAdmissionOneShot({
31162
+ admitted,
31163
+ env,
31164
+ io,
31165
+ request: turnRequest,
31166
+ adapters: countersignAdapters({
31167
+ beforeDispatch: async (admittedSeat, lease) => {
31168
+ if (!identityDiaristRan) {
31169
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
31170
+ } else if (typedTicket !== void 0) {
31171
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
31172
+ }
31173
+ await relocateAdmittedRunToTicket(
31174
+ admittedSeat,
31175
+ env.principalAuthority,
31176
+ lease
31177
+ );
31178
+ Object.assign(
31179
+ turnRequest,
31180
+ buildCountersignTurnRequest(admittedSeat, turnProjection)
31181
+ );
31182
+ }
31183
+ }),
31184
+ ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
31185
+ });
31186
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
31187
+ return result2;
31002
31188
  }
31003
- }),
31004
- ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
31005
- });
31006
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
31007
- return result2;
31189
+ );
31190
+ } catch (error) {
31191
+ if (error instanceof CliUsageError) {
31192
+ presentStructuralRejection(error, io);
31193
+ return { exitCode: 2 };
31194
+ }
31195
+ throw error;
31196
+ }
31008
31197
  }
31009
31198
  function countersignAdapters(options) {
31010
31199
  return {
@@ -31042,7 +31231,9 @@ async function runPublicCountersignResume(request, env, io) {
31042
31231
  adapters: countersignAdapters({
31043
31232
  beforeDispatch: async (admitted) => {
31044
31233
  if (admitted.courtTicketNumbersDamage !== void 0) {
31045
- throw new StationChildExhaustedError(admitted.courtTicketNumbersDamage);
31234
+ throw new StationChildExhaustedError(
31235
+ admitted.courtTicketNumbersDamage
31236
+ );
31046
31237
  }
31047
31238
  if (env.pendingCourtTicketNumbers !== void 0) {
31048
31239
  await bindCourtTicketNumbersOnAdmitted(
@@ -31916,7 +32107,7 @@ var init_analyst_book_key = __esm({
31916
32107
 
31917
32108
  // src/atomic-write.ts
31918
32109
  import { randomUUID as randomUUID7 } from "node:crypto";
31919
- import { rename as rename3, rm as rm2, writeFile as writeFile10 } from "node:fs/promises";
32110
+ import { rename as rename3, rm as rm3, writeFile as writeFile10 } from "node:fs/promises";
31920
32111
  import { dirname as dirname16, join as join31 } from "node:path";
31921
32112
  async function writeFileAtomically(destination, contents) {
31922
32113
  const parent = dirname16(destination);
@@ -31925,7 +32116,7 @@ async function writeFileAtomically(destination, contents) {
31925
32116
  await writeFile10(temporary, contents);
31926
32117
  await rename3(temporary, destination);
31927
32118
  } catch (error) {
31928
- await rm2(temporary, { force: true }).catch(() => void 0);
32119
+ await rm3(temporary, { force: true }).catch(() => void 0);
31929
32120
  throw error;
31930
32121
  }
31931
32122
  }