@akagilnc/pi-workflow-roles 0.1.4489 → 0.1.4503

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.
@@ -7158,10 +7158,12 @@ __export(invocation_exports, {
7158
7158
  deriveMergerEnvelopeFromActiveMerge: () => deriveMergerEnvelopeFromActiveMerge,
7159
7159
  ensureRunArtifactsDir: () => ensureRunArtifactsDir,
7160
7160
  freezeAttachmentsIntoRun: () => freezeAttachmentsIntoRun,
7161
+ freezePreparedAttachmentsIntoRun: () => freezePreparedAttachmentsIntoRun,
7161
7162
  homeFromRunDirectory: () => homeFromRunDirectory,
7162
7163
  isReviewerLens: () => isReviewerLens,
7163
7164
  issueAdmissionPlacement: () => issueAdmissionPlacement,
7164
7165
  loadAdmittedJudgeRequest: () => loadAdmittedJudgeRequest,
7166
+ materializeCountersignInvocation: () => materializeCountersignInvocation,
7165
7167
  observeLaunchedRolePackageIdentity: () => observeLaunchedRolePackageIdentity,
7166
7168
  parseAnalystArgv: () => parseAnalystArgv,
7167
7169
  parseAnalystCohortIssueToken: () => parseAnalystCohortIssueToken,
@@ -7185,6 +7187,7 @@ __export(invocation_exports, {
7185
7187
  parseReviewerArgv: () => parseReviewerArgv,
7186
7188
  parseSecretariatArgv: () => parseSecretariatArgv,
7187
7189
  persistAdmittedSourceRunPath: () => persistAdmittedSourceRunPath,
7190
+ recordAdmittedCorrelation: () => recordAdmittedCorrelation,
7188
7191
  recordEffectiveInvocationModel: () => recordEffectiveInvocationModel,
7189
7192
  recordLaunchedPiIdentity: () => recordLaunchedPiIdentity,
7190
7193
  recordLaunchedRolePackageIdentity: () => recordLaunchedRolePackageIdentity,
@@ -7193,17 +7196,21 @@ __export(invocation_exports, {
7193
7196
  requireReviewerBaseRevision: () => requireReviewerBaseRevision,
7194
7197
  requireReviewerLens: () => requireReviewerLens,
7195
7198
  resolveDoctorCaseRunsPath: () => resolveDoctorCaseRunsPath,
7196
- resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository
7199
+ resolveGitHubRemoteRepository: () => resolveGitHubRemoteRepository,
7200
+ withPreparedAttachments: () => withPreparedAttachments
7197
7201
  });
7198
7202
  import { execFileSync as execFileSync3 } from "node:child_process";
7199
7203
  import { existsSync as existsSync5 } from "node:fs";
7200
7204
  import {
7201
7205
  lstat as lstat4,
7206
+ mkdtemp as mkdtemp2,
7202
7207
  readFile as readFile11,
7203
7208
  realpath as realpath5,
7204
7209
  rename,
7210
+ rm as rm2,
7205
7211
  writeFile as writeFile5
7206
7212
  } from "node:fs/promises";
7213
+ import { tmpdir as tmpdir2 } from "node:os";
7207
7214
  import { basename as basename6, dirname as dirname9, isAbsolute as isAbsolute7, join as join15, resolve as resolve9, sep as sep4 } from "node:path";
7208
7215
  function issueAdmissionPlacement(authority, request) {
7209
7216
  const ledgerHome = resolveActivationLedgerHome(request.home);
@@ -7214,7 +7221,7 @@ function issueAdmissionPlacement(authority, request) {
7214
7221
  runId: request.runId,
7215
7222
  role: request.role
7216
7223
  });
7217
- ensureRoleRunPlacement(ledgerHome, placement);
7224
+ if (request.materialize !== false) ensureRoleRunPlacement(ledgerHome, placement);
7218
7225
  return {
7219
7226
  principal: authority.seal(placement),
7220
7227
  ...placement,
@@ -7339,6 +7346,31 @@ async function bindAdmittedTicketNumber(admitted, ticketNumber) {
7339
7346
  await bindTicketNumberOnRunDirectory(admitted.runDirectory, ticketNumber);
7340
7347
  admitted.ticketNumber = ticketNumber;
7341
7348
  }
7349
+ async function recordAdmittedCorrelation(admitted, correlationId) {
7350
+ const current = JSON.parse(
7351
+ await readFile11(admitted.admittedRequestPath, "utf8")
7352
+ );
7353
+ const prior = [
7354
+ ...Array.isArray(current.correlationIds) ? current.correlationIds.filter(
7355
+ (value) => typeof value === "string" && value.trim() !== ""
7356
+ ) : [],
7357
+ ...typeof current.correlationId === "string" && current.correlationId.trim() !== "" ? [current.correlationId] : []
7358
+ ];
7359
+ const correlationIds = [.../* @__PURE__ */ new Set([...prior, correlationId])];
7360
+ await writeFile5(
7361
+ admitted.admittedRequestPath,
7362
+ `${JSON.stringify({ ...current, correlationId, correlationIds }, null, 2)}
7363
+ `,
7364
+ "utf8"
7365
+ );
7366
+ await mergeInvocationIdentityPage(admitted.runDirectory, {
7367
+ correlationId,
7368
+ correlationIds
7369
+ });
7370
+ const mutable = admitted;
7371
+ mutable.correlationId = correlationId;
7372
+ mutable.correlationIds = correlationIds;
7373
+ }
7342
7374
  async function bindCourtTicketNumbersOnAdmitted(admitted, courtTicketNumbers) {
7343
7375
  if (admitted.ticketNumber === void 0) {
7344
7376
  throw new Error(
@@ -7729,7 +7761,7 @@ function parseFixerArgv(args) {
7729
7761
  ...project === void 0 ? {} : { project }
7730
7762
  };
7731
7763
  }
7732
- async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
7764
+ async function readRegularFileAttachment(sourcePath) {
7733
7765
  const absolute = isAbsolute7(sourcePath) ? sourcePath : resolve9(sourcePath);
7734
7766
  let st;
7735
7767
  try {
@@ -7745,18 +7777,72 @@ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
7745
7777
  `attachment must be a regular file (not a directory or symlink): ${sourcePath}`
7746
7778
  );
7747
7779
  }
7748
- const bytes = await readFile11(absolute);
7749
- const name = `${String(index).padStart(2, "0")}-${basename6(absolute)}`;
7750
- const frozenPath = join15(destinationDir, name);
7780
+ try {
7781
+ return { absolute, bytes: await readFile11(absolute) };
7782
+ } catch (error) {
7783
+ throw new CliUsageError(
7784
+ `attachment is not a readable regular file: ${sourcePath}`,
7785
+ { cause: error }
7786
+ );
7787
+ }
7788
+ }
7789
+ async function removePreparedAttachmentDirectory(stagingDirectory) {
7790
+ await rm2(stagingDirectory, { recursive: true, force: true });
7791
+ }
7792
+ async function withPreparedAttachments(attachmentPaths, use) {
7793
+ if (attachmentPaths.length === 0) return await use([]);
7794
+ const stagingDirectory = await mkdtemp2(join15(tmpdir2(), "ak-role-attachments-"));
7795
+ const prepared = [];
7796
+ let result;
7797
+ try {
7798
+ for (let index = 0; index < attachmentPaths.length; index += 1) {
7799
+ const { absolute, bytes } = await readRegularFileAttachment(attachmentPaths[index]);
7800
+ const snapshotPath = join15(stagingDirectory, String(index).padStart(6, "0"));
7801
+ await writeFile5(snapshotPath, bytes);
7802
+ prepared.push({ absolute, snapshotPath });
7803
+ }
7804
+ result = await use(prepared);
7805
+ } catch (primary) {
7806
+ try {
7807
+ await removePreparedAttachmentDirectory(stagingDirectory);
7808
+ } catch (cleanup) {
7809
+ throw new AggregateError(
7810
+ [primary, cleanup],
7811
+ "attachment snapshot operation failed and cleanup also failed",
7812
+ { cause: primary }
7813
+ );
7814
+ }
7815
+ throw primary;
7816
+ }
7817
+ await removePreparedAttachmentDirectory(stagingDirectory);
7818
+ return result;
7819
+ }
7820
+ async function freezeAttachmentBytes(provenancePath, bytes, destinationDir, index) {
7821
+ const frozenPath = join15(
7822
+ destinationDir,
7823
+ `${String(index).padStart(2, "0")}-${basename6(provenancePath)}`
7824
+ );
7751
7825
  await writeFile5(frozenPath, bytes);
7752
7826
  return {
7753
- attachment: {
7754
- provenancePath: absolute,
7755
- frozenPath,
7756
- byteLength: bytes.byteLength,
7757
- sha256: sha256Hex(bytes),
7758
- mediaKind: "regular-file"
7759
- },
7827
+ provenancePath,
7828
+ frozenPath,
7829
+ byteLength: bytes.byteLength,
7830
+ sha256: sha256Hex(bytes),
7831
+ mediaKind: "regular-file"
7832
+ };
7833
+ }
7834
+ async function freezePreparedAttachment(prepared, destinationDir, index) {
7835
+ return freezeAttachmentBytes(
7836
+ prepared.absolute,
7837
+ await readFile11(prepared.snapshotPath),
7838
+ destinationDir,
7839
+ index
7840
+ );
7841
+ }
7842
+ async function freezeRegularFileAttachment(sourcePath, destinationDir, index) {
7843
+ const { absolute, bytes } = await readRegularFileAttachment(sourcePath);
7844
+ return {
7845
+ attachment: await freezeAttachmentBytes(absolute, bytes, destinationDir, index),
7760
7846
  body: bytes
7761
7847
  };
7762
7848
  }
@@ -7779,6 +7865,17 @@ async function freezeAttachmentsIntoRun(attachmentPaths, runDirectory, summonsKe
7779
7865
  ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
7780
7866
  return freezeAttachments(attachmentPaths, attachmentsDirectory);
7781
7867
  }
7868
+ async function freezePreparedAttachmentsIntoRun(prepared, runDirectory, summonsKey) {
7869
+ if (prepared.length === 0) return [];
7870
+ const ledgerHome = resolveActivationLedgerHome(homeFromRunDirectory(runDirectory));
7871
+ const attachmentsDirectory = summonsKey === void 0 ? join15(runDirectory, "attachments") : join15(runDirectory, "attachments", summonsKey);
7872
+ ensureRealDirectoryTree(ledgerHome, attachmentsDirectory);
7873
+ const attachments = [];
7874
+ for (let index = 0; index < prepared.length; index += 1) {
7875
+ attachments.push(await freezePreparedAttachment(prepared[index], attachmentsDirectory, index));
7876
+ }
7877
+ return attachments;
7878
+ }
7782
7879
  function ticketAdmissionFields(ticketNumber) {
7783
7880
  if (ticketNumber === void 0) return {};
7784
7881
  return {
@@ -7918,9 +8015,10 @@ async function admitCountersignInvocation(options) {
7918
8015
  runId,
7919
8016
  role: "countersign",
7920
8017
  subject: { unbound: true },
7921
- home: options.home
8018
+ home: options.home,
8019
+ materialize: options.deferPersistence !== true
7922
8020
  });
7923
- const attachments = await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
8021
+ const attachments = options.deferPersistence === true ? [] : await freezeAttachments(options.attachmentPaths, attachmentsDirectory);
7924
8022
  const instruction = options.instruction;
7925
8023
  const instructionEmpty = instruction.trim() === "";
7926
8024
  const admitted = {
@@ -7942,11 +8040,13 @@ async function admitCountersignInvocation(options) {
7942
8040
  }))
7943
8041
  };
7944
8042
  const admittedRequestPath = join15(runDirectory, "admitted-request.json");
7945
- await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
7946
- sessionDirectory,
7947
- sessionFile
7948
- });
7949
- await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
8043
+ if (options.deferPersistence !== true) {
8044
+ await writeAdmittedRequestPersistence(admittedRequestPath, admitted, {
8045
+ sessionDirectory,
8046
+ sessionFile
8047
+ });
8048
+ await writeRoleInvocationLedger({ ...admitted, sessionDirectory, sessionFile }, admitted.role, options.model);
8049
+ }
7950
8050
  return {
7951
8051
  role: "countersign",
7952
8052
  runId,
@@ -7961,6 +8061,29 @@ async function admitCountersignInvocation(options) {
7961
8061
  ...options.correlationId === void 0 ? {} : { correlationId: options.correlationId }
7962
8062
  };
7963
8063
  }
8064
+ async function materializeCountersignInvocation(admitted, options) {
8065
+ const placement = issueAdmissionPlacement(options.principalAuthority, {
8066
+ cwd: admitted.projectRoot,
8067
+ runId: admitted.runId,
8068
+ role: "countersign",
8069
+ subject: { unbound: true },
8070
+ home: options.home
8071
+ });
8072
+ const attachments = await freezePreparedAttachmentsIntoRun(
8073
+ options.preparedAttachments,
8074
+ admitted.runDirectory
8075
+ );
8076
+ admitted.attachments = attachments;
8077
+ await writeAdmittedRequestPersistence(admitted.admittedRequestPath, admitted, {
8078
+ sessionDirectory: placement.sessionDirectory,
8079
+ sessionFile: placement.sessionFile
8080
+ });
8081
+ await writeRoleInvocationLedger(
8082
+ { ...admitted, sessionDirectory: placement.sessionDirectory, sessionFile: placement.sessionFile },
8083
+ admitted.role,
8084
+ options.model
8085
+ );
8086
+ }
7964
8087
  function buildCountersignTransportPrompt(admitted, engineMaterial) {
7965
8088
  return buildInstructionTransportPrompt(admitted, engineMaterial);
7966
8089
  }
@@ -16933,8 +17056,8 @@ __export(case_dossier_delivery_exports, {
16933
17056
  loadCaseDossierReadingMaterial: () => loadCaseDossierReadingMaterial,
16934
17057
  projectCaseDossierPointerSection: () => projectCaseDossierPointerSection
16935
17058
  });
16936
- import { mkdtemp as mkdtemp2, readFile as readFile19, rm as rm2, writeFile as writeFile9 } from "node:fs/promises";
16937
- import { tmpdir as tmpdir2 } from "node:os";
17059
+ import { mkdtemp as mkdtemp3, readFile as readFile19, rm as rm3, writeFile as writeFile9 } from "node:fs/promises";
17060
+ import { tmpdir as tmpdir3 } from "node:os";
16938
17061
  import { join as join29 } from "node:path";
16939
17062
  async function projectCaseDossierPointerSection(input) {
16940
17063
  if (input.ticketNumber === void 0) {
@@ -16962,7 +17085,7 @@ async function deliverCaseDossierAsAttachment(input) {
16962
17085
  projectRoot: input.projectRoot,
16963
17086
  home: input.home
16964
17087
  });
16965
- const stagingDir = await mkdtemp2(join29(tmpdir2(), "ak-case-dossier-"));
17088
+ const stagingDir = await mkdtemp3(join29(tmpdir3(), "ak-case-dossier-"));
16966
17089
  try {
16967
17090
  const stagingPath = join29(stagingDir, CASE_DOSSIER_ATTACH_FILE);
16968
17091
  await writeFile9(stagingPath, `${section}
@@ -16973,7 +17096,7 @@ async function deliverCaseDossierAsAttachment(input) {
16973
17096
  CASE_DOSSIER_ATTACH_KEY
16974
17097
  );
16975
17098
  } finally {
16976
- await rm2(stagingDir, { recursive: true, force: true });
17099
+ await rm3(stagingDir, { recursive: true, force: true });
16977
17100
  }
16978
17101
  }
16979
17102
  async function loadCaseDossierReadingMaterial(runDirectory) {
@@ -19275,6 +19398,9 @@ function diaristAdapters(env) {
19275
19398
  shouldPresentSettled: () => true,
19276
19399
  // Resume interrupted state: board ticket already on pages, run still under unbound/.
19277
19400
  beforeDispatch: async (admitted, lease) => {
19401
+ if (env.correlationId !== void 0 && env.correlationId.trim() !== "") {
19402
+ await recordAdmittedCorrelation(admitted, env.correlationId);
19403
+ }
19278
19404
  await bindAndRelocateDiaristIfBoardBound(admitted, env.principalAuthority, lease);
19279
19405
  },
19280
19406
  // First post-turn assert: accept hook wrote board pages; relocate before lease release.
@@ -19459,7 +19585,8 @@ function courtDiaristEscalated(roleOutcome) {
19459
19585
  if (roleOutcome.kind === "audit_escalation") return true;
19460
19586
  if (roleOutcome.kind !== "accepted") return false;
19461
19587
  return (roleOutcome.payloads ?? []).some((payload) => {
19462
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return false;
19588
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload))
19589
+ return false;
19463
19590
  const record4 = payload;
19464
19591
  return record4.status === "escalate" || record4.countersignStatus === "escalate";
19465
19592
  });
@@ -19512,7 +19639,10 @@ async function invokeCourtDiarist(input, env, io) {
19512
19639
  }
19513
19640
  const asserted = result.admitted?.ticketNumber;
19514
19641
  if (isSafePositiveTicketNumber(asserted)) {
19515
- const courtTicketNumbers = courtTicketNumbersFromOutcome(roleOutcome, asserted);
19642
+ const courtTicketNumbers = courtTicketNumbersFromOutcome(
19643
+ roleOutcome,
19644
+ asserted
19645
+ );
19516
19646
  return {
19517
19647
  identity: {
19518
19648
  kind: "ticket",
@@ -19561,7 +19691,9 @@ async function runCountersignCourtDiaristStation(admitted, env, io) {
19561
19691
  );
19562
19692
  }
19563
19693
  if (outcome.failedWithoutEscalate !== void 0) {
19564
- throw new StationChildExhaustedError(outcome.failedWithoutEscalate.diagnostic);
19694
+ throw new StationChildExhaustedError(
19695
+ outcome.failedWithoutEscalate.diagnostic
19696
+ );
19565
19697
  }
19566
19698
  }
19567
19699
  }
@@ -19587,7 +19719,8 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
19587
19719
  ...parsed.project === void 0 ? {} : { project: parsed.project },
19588
19720
  ...env.createRunId === void 0 ? {} : { createRunId: env.createRunId },
19589
19721
  ...env.model === void 0 ? {} : { model: env.model },
19590
- ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId }
19722
+ ...env.correlationId === void 0 ? {} : { correlationId: env.correlationId },
19723
+ deferPersistence: true
19591
19724
  });
19592
19725
  } catch (error) {
19593
19726
  if (error instanceof CliUsageError) {
@@ -19596,160 +19729,216 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
19596
19729
  }
19597
19730
  throw error;
19598
19731
  }
19599
- await markRunAdmitted(admitted, env.principalAuthority);
19600
- let typedTicket;
19601
- let typedCourtTicketNumbers;
19602
- let identityDiaristRan = false;
19603
- if (env.runCourtDiaristStation === void 0) {
19604
- const outcome = await invokeCourtDiarist(
19605
- {
19606
- instruction: parsed.instruction,
19607
- projectRoot: admitted.projectRoot,
19608
- failureLabel: "unbound summons",
19609
- correlationId: admitted.runId
19610
- },
19611
- env,
19612
- io
19613
- );
19614
- identityDiaristRan = true;
19615
- if (outcome.identity.kind === "escalate") {
19616
- return await presentControlledFailure2(
19617
- admitted,
19618
- {
19619
- timedOut: false,
19620
- code: null,
19621
- stderr: "",
19622
- thrown: new Error(
19623
- "court diarist station escalated (cannot identify court target)"
19624
- )
19625
- },
19626
- countersignAdapters(),
19627
- env.principalAuthority,
19628
- io
19629
- );
19630
- }
19631
- if (outcome.failedWithoutEscalate !== void 0) {
19632
- return await presentControlledFailure2(
19633
- admitted,
19634
- {
19635
- timedOut: false,
19636
- code: null,
19637
- stderr: "",
19638
- thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
19639
- },
19640
- countersignAdapters(),
19641
- env.principalAuthority,
19642
- io
19643
- );
19644
- }
19645
- if (outcome.identity.kind === "ticket") {
19646
- const assertedTicket = outcome.identity.ticketNumber;
19647
- typedTicket = assertedTicket;
19648
- typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
19649
- const summons = {
19650
- instruction: parsed.instruction,
19651
- instructionEmpty: parsed.instruction.trim() === "",
19652
- attachmentPaths: parsed.attachmentPaths
19653
- };
19654
- const resumed = await tryResumeSameTicketSeatRun({
19655
- home: env.home,
19656
- projectRoot: admitted.projectRoot,
19657
- role: "countersign",
19658
- ticketNumber: typedTicket,
19659
- freshSummons: env.freshSummons,
19660
- summons,
19661
- resume: async (runId, materials) => {
19662
- await bindAdmittedTicketNumber(admitted, assertedTicket);
19663
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
19664
- await markRunTerminal(admitted.runDirectory);
19665
- return await runPublicCountersignResume(
19666
- {
19667
- runId,
19668
- ...materials === void 0 ? {} : { summons: materials }
19669
- },
19670
- {
19671
- ...env,
19672
- ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
19673
- },
19674
- io
19675
- );
19732
+ try {
19733
+ return await withPreparedAttachments(
19734
+ parsed.attachmentPaths,
19735
+ async (preparedAttachments) => {
19736
+ const materializeAdmission = async () => {
19737
+ await materializeCountersignInvocation(admitted, {
19738
+ home: env.home,
19739
+ principalAuthority: env.principalAuthority,
19740
+ preparedAttachments,
19741
+ ...env.model === void 0 ? {} : { model: env.model }
19742
+ });
19743
+ };
19744
+ let typedTicket;
19745
+ let typedCourtTicketNumbers;
19746
+ let identityDiaristRan = false;
19747
+ if (env.runCourtDiaristStation === void 0) {
19748
+ let outcome;
19749
+ try {
19750
+ outcome = await invokeCourtDiarist(
19751
+ {
19752
+ instruction: parsed.instruction,
19753
+ projectRoot: admitted.projectRoot,
19754
+ failureLabel: "unbound summons"
19755
+ },
19756
+ env,
19757
+ io
19758
+ );
19759
+ } catch (error) {
19760
+ await materializeAdmission();
19761
+ await markRunAdmitted(admitted, env.principalAuthority);
19762
+ return await presentControlledFailure2(
19763
+ admitted,
19764
+ { timedOut: false, code: null, stderr: "", thrown: error },
19765
+ countersignAdapters(),
19766
+ env.principalAuthority,
19767
+ io
19768
+ );
19769
+ }
19770
+ identityDiaristRan = true;
19771
+ if (outcome.identity.kind === "escalate") {
19772
+ await materializeAdmission();
19773
+ await markRunAdmitted(admitted, env.principalAuthority);
19774
+ return await presentControlledFailure2(
19775
+ admitted,
19776
+ {
19777
+ timedOut: false,
19778
+ code: null,
19779
+ stderr: "",
19780
+ thrown: new Error(
19781
+ "court diarist station escalated (cannot identify court target)"
19782
+ )
19783
+ },
19784
+ countersignAdapters(),
19785
+ env.principalAuthority,
19786
+ io
19787
+ );
19788
+ }
19789
+ if (outcome.failedWithoutEscalate !== void 0) {
19790
+ await materializeAdmission();
19791
+ await markRunAdmitted(admitted, env.principalAuthority);
19792
+ return await presentControlledFailure2(
19793
+ admitted,
19794
+ {
19795
+ timedOut: false,
19796
+ code: null,
19797
+ stderr: "",
19798
+ thrown: new Error(outcome.failedWithoutEscalate.diagnostic)
19799
+ },
19800
+ countersignAdapters(),
19801
+ env.principalAuthority,
19802
+ io
19803
+ );
19804
+ }
19805
+ if (outcome.identity.kind === "ticket") {
19806
+ const assertedTicket = outcome.identity.ticketNumber;
19807
+ typedTicket = assertedTicket;
19808
+ typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
19809
+ const summons = {
19810
+ instruction: parsed.instruction,
19811
+ instructionEmpty: parsed.instruction.trim() === ""
19812
+ };
19813
+ const resumed = await tryResumeSameTicketSeatRun({
19814
+ home: env.home,
19815
+ projectRoot: admitted.projectRoot,
19816
+ role: "countersign",
19817
+ ticketNumber: typedTicket,
19818
+ freshSummons: env.freshSummons,
19819
+ summons,
19820
+ resume: async (runId, materials) => {
19821
+ const retained = await loadResumableCountersignRun(
19822
+ env.home,
19823
+ runId,
19824
+ env.principalAuthority
19825
+ );
19826
+ if (retained.admitted === void 0) {
19827
+ throw new Error(
19828
+ `retained countersign run disappeared before resume: ${runId}`
19829
+ );
19830
+ }
19831
+ const frozenPaths = (await freezePreparedAttachmentsIntoRun(
19832
+ preparedAttachments,
19833
+ retained.admitted.runDirectory,
19834
+ `s-${Date.now().toString(36)}`
19835
+ )).map((attachment) => attachment.frozenPath);
19836
+ const preparedMaterials = {
19837
+ ...materials ?? {},
19838
+ ...frozenPaths.length === 0 ? {} : { attachmentPaths: frozenPaths }
19839
+ };
19840
+ return await runPublicCountersignResume(
19841
+ {
19842
+ runId,
19843
+ summons: preparedMaterials
19844
+ },
19845
+ {
19846
+ ...env,
19847
+ ...typedCourtTicketNumbers === void 0 ? {} : { pendingCourtTicketNumbers: typedCourtTicketNumbers }
19848
+ },
19849
+ io
19850
+ );
19851
+ }
19852
+ });
19853
+ if (resumed !== void 0) {
19854
+ return resumed;
19855
+ }
19856
+ }
19676
19857
  }
19677
- });
19678
- if (resumed !== void 0) {
19679
- return resumed;
19680
- }
19681
- }
19682
- }
19683
- if (identityDiaristRan && typedTicket !== void 0) {
19684
- try {
19685
- await bindAdmittedTicketNumber(admitted, typedTicket);
19686
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
19687
- await bindCourtTicketNumbersOnAdmitted(
19688
- admitted,
19689
- typedCourtTicketNumbers ?? [typedTicket]
19690
- );
19691
- } catch (error) {
19692
- return await presentControlledFailure2(
19693
- admitted,
19694
- {
19695
- timedOut: false,
19696
- code: null,
19697
- stderr: "",
19698
- thrown: error
19699
- },
19700
- countersignAdapters(),
19701
- env.principalAuthority,
19702
- io
19703
- );
19704
- }
19705
- }
19706
- const turnProjection = {
19707
- packageRoot: env.packageRoot,
19708
- home: env.home,
19709
- agentDir: env.agentDir,
19710
- ...env.model === void 0 ? {} : { model: env.model },
19711
- ...pickEngineAxis(env),
19712
- ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
19713
- ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
19714
- continuation: {
19715
- kind: "initial",
19716
- prompt: buildCountersignTransportPrompt(
19717
- admitted,
19718
- engineSessionMaterialFromOptions({
19719
- ...pickEngineAxis(env),
19720
- packageRoot: env.packageRoot
19721
- })
19722
- )
19723
- }
19724
- };
19725
- const turnRequest = buildCountersignTurnRequest(admitted, turnProjection);
19726
- const result = await runPostAdmissionOneShot({
19727
- admitted,
19728
- env,
19729
- io,
19730
- request: turnRequest,
19731
- adapters: countersignAdapters({
19732
- beforeDispatch: async (admittedSeat, lease) => {
19733
- if (!identityDiaristRan) {
19734
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
19735
- } else if (typedTicket !== void 0) {
19736
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
19858
+ await materializeAdmission();
19859
+ await markRunAdmitted(admitted, env.principalAuthority);
19860
+ if (identityDiaristRan && typedTicket !== void 0) {
19861
+ try {
19862
+ await bindAdmittedTicketNumber(admitted, typedTicket);
19863
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
19864
+ await bindCourtTicketNumbersOnAdmitted(
19865
+ admitted,
19866
+ typedCourtTicketNumbers ?? [typedTicket]
19867
+ );
19868
+ } catch (error) {
19869
+ return await presentControlledFailure2(
19870
+ admitted,
19871
+ {
19872
+ timedOut: false,
19873
+ code: null,
19874
+ stderr: "",
19875
+ thrown: error
19876
+ },
19877
+ countersignAdapters(),
19878
+ env.principalAuthority,
19879
+ io
19880
+ );
19881
+ }
19737
19882
  }
19738
- await relocateAdmittedRunToTicket(
19739
- admittedSeat,
19740
- env.principalAuthority,
19741
- lease
19742
- );
19743
- Object.assign(
19744
- turnRequest,
19745
- buildCountersignTurnRequest(admittedSeat, turnProjection)
19883
+ const turnProjection = {
19884
+ packageRoot: env.packageRoot,
19885
+ home: env.home,
19886
+ agentDir: env.agentDir,
19887
+ ...env.model === void 0 ? {} : { model: env.model },
19888
+ ...pickEngineAxis(env),
19889
+ ...env.timeoutMs === void 0 ? {} : { timeoutMs: env.timeoutMs },
19890
+ ...env.correlationId === void 0 || env.correlationId.trim() === "" ? {} : { correlationId: env.correlationId },
19891
+ continuation: {
19892
+ kind: "initial",
19893
+ prompt: buildCountersignTransportPrompt(
19894
+ admitted,
19895
+ engineSessionMaterialFromOptions({
19896
+ ...pickEngineAxis(env),
19897
+ packageRoot: env.packageRoot
19898
+ })
19899
+ )
19900
+ }
19901
+ };
19902
+ const turnRequest = buildCountersignTurnRequest(
19903
+ admitted,
19904
+ turnProjection
19746
19905
  );
19906
+ const result = await runPostAdmissionOneShot({
19907
+ admitted,
19908
+ env,
19909
+ io,
19910
+ request: turnRequest,
19911
+ adapters: countersignAdapters({
19912
+ beforeDispatch: async (admittedSeat, lease) => {
19913
+ if (!identityDiaristRan) {
19914
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
19915
+ } else if (typedTicket !== void 0) {
19916
+ await runCountersignCourtDiaristStation(admittedSeat, env, io);
19917
+ }
19918
+ await relocateAdmittedRunToTicket(
19919
+ admittedSeat,
19920
+ env.principalAuthority,
19921
+ lease
19922
+ );
19923
+ Object.assign(
19924
+ turnRequest,
19925
+ buildCountersignTurnRequest(admittedSeat, turnProjection)
19926
+ );
19927
+ }
19928
+ }),
19929
+ ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
19930
+ });
19931
+ await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
19932
+ return result;
19747
19933
  }
19748
- }),
19749
- ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
19750
- });
19751
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
19752
- return result;
19934
+ );
19935
+ } catch (error) {
19936
+ if (error instanceof CliUsageError) {
19937
+ presentStructuralRejection(error, io);
19938
+ return { exitCode: 2 };
19939
+ }
19940
+ throw error;
19941
+ }
19753
19942
  }
19754
19943
  function countersignAdapters(options) {
19755
19944
  return {
@@ -19787,7 +19976,9 @@ async function runPublicCountersignResume(request, env, io) {
19787
19976
  adapters: countersignAdapters({
19788
19977
  beforeDispatch: async (admitted) => {
19789
19978
  if (admitted.courtTicketNumbersDamage !== void 0) {
19790
- throw new StationChildExhaustedError(admitted.courtTicketNumbersDamage);
19979
+ throw new StationChildExhaustedError(
19980
+ admitted.courtTicketNumbersDamage
19981
+ );
19791
19982
  }
19792
19983
  if (env.pendingCourtTicketNumbers !== void 0) {
19793
19984
  await bindCourtTicketNumbersOnAdmitted(
@@ -22455,7 +22646,7 @@ import { join as join38, sep as sep5 } from "node:path";
22455
22646
 
22456
22647
  // src/atomic-write.ts
22457
22648
  import { randomUUID as randomUUID7 } from "node:crypto";
22458
- import { rename as rename3, rm as rm3, writeFile as writeFile11 } from "node:fs/promises";
22649
+ import { rename as rename3, rm as rm4, writeFile as writeFile11 } from "node:fs/promises";
22459
22650
  import { dirname as dirname22, join as join37 } from "node:path";
22460
22651
  async function writeFileAtomically(destination, contents) {
22461
22652
  const parent = dirname22(destination);
@@ -22464,7 +22655,7 @@ async function writeFileAtomically(destination, contents) {
22464
22655
  await writeFile11(temporary, contents);
22465
22656
  await rename3(temporary, destination);
22466
22657
  } catch (error) {
22467
- await rm3(temporary, { force: true }).catch(() => void 0);
22658
+ await rm4(temporary, { force: true }).catch(() => void 0);
22468
22659
  throw error;
22469
22660
  }
22470
22661
  }