@akagilnc/pi-workflow-roles 0.1.4802 → 0.1.4870

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/dist/acp-host/production-host.js +19434 -18218
  2. package/dist/analyst-book-key.js +63 -0
  3. package/dist/analyst-cohort.js +167 -0
  4. package/dist/analyst-entry.js +320 -0
  5. package/dist/analyst-index.js +247 -0
  6. package/dist/analyst-ledger.js +561 -0
  7. package/dist/analyst-median.js +16 -0
  8. package/dist/analyst-metric-families/acceptance-success-rework.js +249 -0
  9. package/dist/analyst-metric-families/b2-frame-buckets-actions.js +189 -0
  10. package/dist/analyst-metric-families/gate-cycles.js +77 -0
  11. package/dist/analyst-metric-families/leg-wall-clock.js +56 -0
  12. package/dist/analyst-metric-families/round-timeline.js +126 -0
  13. package/dist/analyst-metric-families.js +23 -0
  14. package/dist/analyst-metric-family.js +11 -0
  15. package/dist/analyst-model-groups.js +135 -0
  16. package/dist/analyst-page.js +182 -0
  17. package/dist/atomic-write.js +23 -0
  18. package/dist/auditor-soul.js +1 -1
  19. package/dist/countersign-contracts.js +0 -2
  20. package/dist/diarist-contracts.js +0 -1
  21. package/dist/doctor-contracts.js +2 -2
  22. package/dist/gatekeeper-role.js +15 -16
  23. package/dist/gleaner-left-contracts.js +0 -2
  24. package/dist/headless-host/production-host.js +16967 -15749
  25. package/dist/host-contracts.js +0 -4
  26. package/dist/inspector-contracts.js +0 -2
  27. package/dist/institutional-resolution.js +4 -2
  28. package/dist/merger-contracts.js +0 -2
  29. package/dist/migrate-book-topology.js +3908 -3089
  30. package/dist/navigator-public-session.js +4 -3
  31. package/dist/notary-contracts.js +0 -2
  32. package/dist/package-contracts/auditor-output.js +0 -1
  33. package/dist/package-contracts/collector-output.js +0 -2
  34. package/dist/package-contracts/fixer-output.js +0 -2
  35. package/dist/package-contracts/gatekeeper-output.js +0 -2
  36. package/dist/package-contracts/judge-output.js +0 -2
  37. package/dist/package-contracts/navigator-output.js +0 -2
  38. package/dist/package-contracts/reviewer-output.js +0 -2
  39. package/dist/package-contracts/terminating-tools.js +32 -92
  40. package/dist/package-contracts/worker-output.js +0 -4
  41. package/dist/packaged-role-registry.js +518 -36
  42. package/dist/pi/role-turn-host.js +2 -1
  43. package/dist/public-cli/analyst-run.js +185 -0
  44. package/dist/public-cli/cli.js +885 -0
  45. package/dist/public-cli/config.js +12 -9
  46. package/dist/public-cli/countersign-run.js +4 -311
  47. package/dist/public-cli/instruction-seat-run.js +628 -158
  48. package/dist/public-cli/invocation.js +806 -1550
  49. package/dist/public-cli/main.js +6962 -9882
  50. package/dist/public-cli/option-definitions.js +51 -99
  51. package/dist/public-cli/post-admission.js +30 -2
  52. package/dist/public-cli/run-lifecycle.js +234 -353
  53. package/dist/public-cli/seat-ticket-binding.js +6 -0
  54. package/dist/public-cli/settlement.js +261 -675
  55. package/dist/public-cli/turn-request.js +76 -0
  56. package/dist/public-role-summons.js +38 -112
  57. package/dist/role-activation-flags.js +23 -45
  58. package/dist/secretariat-contracts.js +0 -2
  59. package/dist/session-identity.js +17 -8
  60. package/dist/session-opening-materials.js +3 -2
  61. package/extensions/role-runtime.ts +6 -80
  62. package/package.json +1 -1
  63. package/src/acp-host/role-turn-host.ts +4 -1
  64. package/src/analyst-metric-families/acceptance-success-rework.ts +3 -2
  65. package/src/auditor-role.ts +1 -2
  66. package/src/auditor-soul.ts +1 -1
  67. package/src/collector-role.ts +2 -2
  68. package/src/countersign-contracts.ts +0 -1
  69. package/src/countersign-role.ts +1 -1
  70. package/src/diarist-contracts.ts +0 -1
  71. package/src/diarist-role.ts +0 -1
  72. package/src/doctor-auditor.ts +1 -2
  73. package/src/doctor-contracts.ts +1 -1
  74. package/src/doctor-role.ts +3 -2
  75. package/src/external-host-turn-loop.ts +1 -1
  76. package/src/factory-board.ts +10 -7
  77. package/src/gatekeeper-role.ts +16 -10
  78. package/src/gleaner-left-contracts.ts +0 -1
  79. package/src/gleaner-left-role.ts +0 -1
  80. package/src/headless-host/role-turn-host.ts +7 -1
  81. package/src/host-contracts.ts +10 -6
  82. package/src/inspector-contracts.ts +0 -1
  83. package/src/inspector-role.ts +0 -2
  84. package/src/institutional-resolution.ts +6 -4
  85. package/src/judge-role.ts +3 -3
  86. package/src/merger-contracts.ts +0 -1
  87. package/src/merger-role.ts +2 -2
  88. package/src/navigator-public-session.ts +7 -6
  89. package/src/navigator-work-context.ts +6 -8
  90. package/src/notary-contracts.ts +0 -1
  91. package/src/notary-role.ts +2 -3
  92. package/src/package-contracts/auditor-output.ts +0 -1
  93. package/src/package-contracts/collector-output.ts +0 -1
  94. package/src/package-contracts/fixer-output.ts +0 -1
  95. package/src/package-contracts/gatekeeper-output.ts +0 -1
  96. package/src/package-contracts/judge-output.ts +0 -1
  97. package/src/package-contracts/navigator-output.ts +0 -1
  98. package/src/package-contracts/reviewer-output.ts +0 -1
  99. package/src/package-contracts/terminating-tools.ts +27 -99
  100. package/src/package-contracts/worker-output.ts +0 -2
  101. package/src/packaged-role-registry.ts +604 -35
  102. package/src/pi/role-turn-host.ts +2 -1
  103. package/src/public-cli/cli.ts +64 -259
  104. package/src/public-cli/config.ts +18 -10
  105. package/src/public-cli/countersign-run.ts +14 -449
  106. package/src/public-cli/instruction-seat-run.ts +758 -277
  107. package/src/public-cli/invocation.ts +1097 -2051
  108. package/src/public-cli/option-definitions.ts +62 -127
  109. package/src/public-cli/post-admission.ts +47 -2
  110. package/src/public-cli/run-lifecycle.ts +303 -613
  111. package/src/public-cli/seat-ticket-binding.ts +6 -0
  112. package/src/public-cli/settlement.ts +399 -1185
  113. package/src/public-cli/turn-request.ts +92 -1
  114. package/src/public-role-summons.ts +44 -128
  115. package/src/reviewer-role.ts +3 -2
  116. package/src/role-activation-flags.ts +20 -41
  117. package/src/role-runtime-dependencies.ts +25 -26
  118. package/src/role-runtime.ts +121 -232
  119. package/src/secretariat-contracts.ts +0 -1
  120. package/src/secretariat-role.ts +0 -2
  121. package/src/session-identity.ts +23 -8
  122. package/src/session-opening-materials.ts +2 -1
  123. package/src/worker-role.ts +3 -4
  124. package/dist/doctor-auditor.js +0 -25
  125. package/dist/public-cli/diarist-run.js +0 -130
  126. package/dist/public-cli/doctor-run.js +0 -87
  127. package/dist/public-cli/inspector-run.js +0 -151
  128. package/dist/public-cli/judge-run.js +0 -114
  129. package/dist/public-cli/notary-run.js +0 -174
  130. package/dist/public-cli/reviewer-run.js +0 -339
  131. package/src/public-cli/coder-run.ts +0 -255
  132. package/src/public-cli/collector-run.ts +0 -189
  133. package/src/public-cli/diarist-run.ts +0 -233
  134. package/src/public-cli/doctor-run.ts +0 -167
  135. package/src/public-cli/fixer-run.ts +0 -265
  136. package/src/public-cli/gleaner-left-run.ts +0 -167
  137. package/src/public-cli/inspector-run.ts +0 -261
  138. package/src/public-cli/judge-run.ts +0 -196
  139. package/src/public-cli/merger-run.ts +0 -294
  140. package/src/public-cli/notary-run.ts +0 -279
  141. package/src/public-cli/reviewer-run.ts +0 -480
  142. package/src/public-cli/secretariat-run.ts +0 -265
@@ -1,80 +1,28 @@
1
1
  /**
2
- * Public Countersign Role run: admit ticket materials → court-pipeline prior
3
- * station (起居郎) → shared post-admission coordinator → settle Terminal result
4
- * (#572 / ADR 0074 / ADR 0075 / #742 / #771). #599: manual resume continues the
5
- * exact session via explicit package runId. #987 Result 7: public entry no longer
6
- * selects a prior run by ticket number; gate same-parent re-summons keep
7
- * parentRunPath resume (#747 / ADR 0079 gate face). Explicit `ak-role new` mints
8
- * fresh; explicit `ak-role resume <runId>` continues a named run.
2
+ * Court diarist station for the shared countersign entry (#572 / ADR 0074 /
3
+ * ADR 0075 / #742 / #771). The public seat itself admits and settles through
4
+ * instruction-seat-run. This module only summons 起居郎 and refreshes a bound
5
+ * court diary.
9
6
  *
10
7
  * Court admission auto-runs 起居郎 so the 起居郎 LLM asserts the court target;
11
8
  * mechanical layer only verifies; countersign reuses that typed identity for bind
12
9
  * (ADR 0075 / 0081). Code never matches instruction text against book-known
13
10
  * numbers. Who may call 起居郎 and in what order is not written into law
14
- * (ADR 0075 不规定谁调用起居郎、顺序归调用者); the present admission effect is what this
15
- * seat currently does. 起居录 path delivery is owned once by post-admission
16
- * (#709 / ADR 0081). Court refresh may run on resume; it is not a resume
17
- * precondition and does not rewrite or reject host resume (#987).
18
- *
19
- * Wiring (#771 / #863 / #987): gate parentRunPath resume (when present) runs
20
- * before identity mint; public path without parentRunPath always materializes a
21
- * new run after identity. 起居郎 escalate (认不出) and typed failure terminals
22
- * (incl. verification failure) settle as countersign controlled failure — never
23
- * wash into 真无票. Only a true missing lawful typed terminal stays unbound and
24
- * continues the body (r5 unbound-continue). Bound refresh hands the typed key to
25
- * 起居郎 so freeze loads issue face (ADR 0075: 每次过庭都跑是调用者用法 / typed handoff).
11
+ * (ADR 0075 不规定谁调用起居郎、顺序归调用者). 起居录 path delivery is owned once by
12
+ * post-admission (#709 / ADR 0081). Bound refresh hands the typed key to 起居郎
13
+ * so freeze loads issue face (ADR 0075: 每次过庭都跑是调用者用法 / typed handoff).
26
14
  */
27
- import { resolve } from "node:path";
28
- import type {
29
- DurablePrincipalAuthority,
30
- RoleTurnRequest,
31
- } from "../host-contracts.ts";
32
- import {
33
- engineSessionMaterialFromOptions,
34
- pickEngineAxis,
35
- } from "../package-resources/engine-material.ts";
36
- import { CliUsageError } from "./cli-errors.ts";
15
+ import type { DurablePrincipalAuthority } from "../host-contracts.ts";
37
16
  import { projectCourtTicketNumbers } from "../diarist-contracts.ts";
38
17
  import { readableGateItem } from "../readable-gate-item.ts";
39
18
  import { isSafePositiveTicketNumber } from "../run-ticket-number.ts";
19
+ import type { AdmittedCountersignInvocation } from "./invocation.ts";
40
20
  import {
41
- admitCountersignInvocation,
42
- bindAdmittedTicketNumber,
43
- bindCourtTicketNumbersOnAdmitted,
44
- buildCountersignTransportPrompt,
45
- materializeCountersignInvocation,
46
- persistAdmittedSourceRunPath,
47
- relocateAdmittedRunToTicket,
48
- withPreparedAttachments,
49
- type AdmittedCountersignInvocation,
50
- type ParseCountersignArgvResult,
51
- } from "./invocation.ts";
52
- import {
53
- prepareSummonsResumeMaterials,
54
- presentControlledFailure,
55
- runPostAdmissionOneShot,
56
21
  type PostAdmissionEnv,
57
- runPostAdmissionSeatResume,
58
- resumeTurnRequestProjectionOptions,
59
22
  StationChildExhaustedError,
60
23
  } from "./post-admission.ts";
61
- import {
62
- loadResumableCountersignRun,
63
- markRunAdmitted,
64
- type PublicResumeRequest,
65
- type RunWriterLease,
66
- } from "./run-lifecycle.ts";
67
- import { tryResumeSameTicketSeatRun } from "./seat-ticket-binding.ts";
68
- import {
69
- presentStructuralRejection,
70
- trySettleCountersignTerminalResult,
71
- } from "./settlement.ts";
72
24
  import type { CliIo } from "./cli-io.ts";
73
- import type { TerminalResult, TerminalRoleOutcome } from "./terminal.ts";
74
- import {
75
- projectRoleTurnRequest,
76
- type RoleTurnRequestProjectionOptions,
77
- } from "./turn-request.ts";
25
+ import type { TerminalRoleOutcome } from "./terminal.ts";
78
26
 
79
27
  export type CountersignRunEnv = PostAdmissionEnv & {
80
28
  principalAuthority: DurablePrincipalAuthority;
@@ -104,30 +52,11 @@ export type CountersignRunEnv = PostAdmissionEnv & {
104
52
  /**
105
53
  * #747 / #987 gate same-parent resume key (Secretariat source run directory).
106
54
  * When set, re-summons resume the prior 给事中 under this parent before mint.
55
+ * Absent on an ordinary public summons, which always mints.
107
56
  */
108
57
  parentRunPath?: string;
109
58
  };
110
59
 
111
- /** Project admitted invocation onto the host-neutral turn request. */
112
- export function buildCountersignTurnRequest(
113
- admitted: AdmittedCountersignInvocation,
114
- options: RoleTurnRequestProjectionOptions,
115
- ): RoleTurnRequest {
116
- return projectRoleTurnRequest(
117
- admitted,
118
- {
119
- activation: {
120
- role: "countersign" as const,
121
- // Admitted typed binding rides the turn activation seam to the Notary gate.
122
- ...(admitted.ticketNumber === undefined
123
- ? {}
124
- : { ticketNumber: admitted.ticketNumber }),
125
- },
126
- },
127
- options,
128
- );
129
- }
130
-
131
60
  /** 起居郎 identity outcome — escalate stays distinct from missing terminal. */
132
61
  export type CourtDiaristIdentity =
133
62
  | {
@@ -393,8 +322,8 @@ export async function invokeCourtDiarist(
393
322
  *
394
323
  * Missing ticketNumber (true-unbound / identity deferred) skips the refresh
395
324
  * station — no diary is minted for a true-unbound run. First-entry identity
396
- * lives on `runPublicCountersign` (typed 起居郎 key for bind + same-ticket
397
- * resume). Bound refresh: 起居郎 failure propagates (失败诚实).
325
+ * lives on the shared countersign entry (typed 起居郎 key for bind). Bound refresh:
326
+ * 起居郎 failure propagates (失败诚实).
398
327
  * Path delivery onto materials is not this station's job — post-admission owns it.
399
328
  */
400
329
  export async function runCountersignCourtDiaristStation(
@@ -407,7 +336,7 @@ export async function runCountersignCourtDiaristStation(
407
336
  return;
408
337
  }
409
338
  // Production court refresh (ADR 0075: 每次过庭都跑是调用者用法) only under a known ticket identity.
410
- // First-entry unbound identity is owned by runPublicCountersign.
339
+ // First-entry unbound identity is owned by the shared countersign entry.
411
340
  if (admitted.ticketNumber === undefined) return;
412
341
 
413
342
  // #871: refresh every member of the typed co-review set; single-ticket face
@@ -452,367 +381,3 @@ export async function runCountersignCourtDiaristStation(
452
381
  }
453
382
  }
454
383
  }
455
-
456
- export async function runPublicCountersign(
457
- argv: readonly string[],
458
- env: CountersignRunEnv,
459
- io: CliIo,
460
- parseCountersignArgv: (args: readonly string[]) => ParseCountersignArgvResult,
461
- ): Promise<{
462
- exitCode: number;
463
- admitted?: AdmittedCountersignInvocation;
464
- terminal?: TerminalResult;
465
- }> {
466
- let parsed: ParseCountersignArgvResult;
467
- try {
468
- parsed = parseCountersignArgv(argv);
469
- } catch (error) {
470
- if (error instanceof CliUsageError) {
471
- presentStructuralRejection(error, io);
472
- return { exitCode: 2 };
473
- }
474
- throw error;
475
- }
476
-
477
- let admitted: AdmittedCountersignInvocation;
478
- const gateParentRunPath =
479
- typeof env.parentRunPath === "string" && env.parentRunPath.trim() !== ""
480
- ? env.parentRunPath
481
- : undefined;
482
- if (gateParentRunPath !== undefined) {
483
- const resumeInstruction = env.reviewReask ?? env.gateReviewInstruction ?? parsed.instruction;
484
- const resumed = await tryResumeSameTicketSeatRun({
485
- home: env.home,
486
- projectRoot: resolve(parsed.project ?? env.cwd),
487
- role: "countersign",
488
- parentRunPath: gateParentRunPath,
489
- freshSummons: env.freshSummons,
490
- summons: {
491
- sourceRunPath: gateParentRunPath,
492
- instruction: resumeInstruction,
493
- instructionEmpty: resumeInstruction.trim() === "",
494
- },
495
- resume: (runId, materials) =>
496
- runPublicCountersignResume(
497
- { runId, ...(materials === undefined ? {} : { summons: materials }) },
498
- env,
499
- io,
500
- ),
501
- });
502
- if (resumed !== undefined) return resumed;
503
- }
504
-
505
- try {
506
- admitted = await admitCountersignInvocation({
507
- home: env.home,
508
- principalAuthority: env.principalAuthority,
509
- cwd: env.cwd,
510
- instruction: parsed.instruction,
511
- attachmentPaths: parsed.attachmentPaths,
512
- ...(parsed.project === undefined ? {} : { project: parsed.project }),
513
- ...(env.createRunId === undefined
514
- ? {}
515
- : { createRunId: env.createRunId }),
516
- ...(env.model === undefined ? {} : { model: env.model }),
517
- ...(env.correlationId === undefined
518
- ? {}
519
- : { correlationId: env.correlationId }),
520
- deferPersistence: true,
521
- });
522
- } catch (error) {
523
- if (error instanceof CliUsageError) {
524
- presentStructuralRejection(error, io);
525
- return { exitCode: 2 };
526
- }
527
- throw error;
528
- }
529
-
530
- // One owner spans prepare, lookup/load/resume or new-run materialization.
531
- // It preserves both causes when its final cleanup also fails.
532
- try {
533
- return await withPreparedAttachments(
534
- parsed.attachmentPaths,
535
- async (preparedAttachments) => {
536
- const materializeAdmission = async (): Promise<void> => {
537
- await materializeCountersignInvocation(admitted, {
538
- home: env.home,
539
- principalAuthority: env.principalAuthority,
540
- preparedAttachments,
541
- ...(env.model === undefined ? {} : { model: env.model }),
542
- });
543
- };
544
-
545
- // #747 / #987: gate same-parent resume before identity mint. Public entry
546
- // without parentRunPath never selects a prior run by ticket number.
547
- // #637 / #771: ticket identity is the 起居郎 LLM typed assertion (never
548
- // mechanical matching of summons text). Resolve that typed key before
549
- // materializing a first-mint run. Controlled failures materialize below so
550
- // they still have a durable page. The test seam `runCourtDiaristStation`
551
- // defers identity to beforeDispatch; generic hook failures stay on the
552
- // parent call-local budget, exhausted nested station children still skip
553
- // parent auto-resume (#840 父子不层叠).
554
- let typedTicket: number | undefined;
555
- let typedCourtTicketNumbers: readonly number[] | undefined;
556
- let identityDiaristRan = false;
557
-
558
- if (env.runCourtDiaristStation === undefined) {
559
- let outcome: CourtDiaristInvocationResult;
560
- try {
561
- outcome = await invokeCourtDiarist(
562
- {
563
- instruction: parsed.instruction,
564
- projectRoot: admitted.projectRoot,
565
- failureLabel: "unbound summons",
566
- // #969: parent durable ticket is the bind key (not a resume lookup).
567
- ...(env.boundTicketNumber === undefined
568
- ? {}
569
- : { boundTicketNumber: env.boundTicketNumber }),
570
- },
571
- env,
572
- io,
573
- );
574
- } catch (error) {
575
- await materializeAdmission();
576
- await markRunAdmitted(admitted, env.principalAuthority);
577
- return await presentControlledFailure(
578
- admitted,
579
- { timedOut: false, code: null, stderr: "", thrown: error },
580
- countersignAdapters(),
581
- env.principalAuthority,
582
- io,
583
- );
584
- }
585
- identityDiaristRan = true;
586
-
587
- if (outcome.identity.kind === "escalate") {
588
- // 御批: 识别不了就上抛 — materialize and settle this countersign run.
589
- // Diagnostic relays diarist escalate payload facts (#953).
590
- await materializeAdmission();
591
- await markRunAdmitted(admitted, env.principalAuthority);
592
- return await presentControlledFailure(
593
- admitted,
594
- {
595
- timedOut: false,
596
- code: null,
597
- stderr: "",
598
- thrown: new Error(outcome.identity.diagnostic),
599
- },
600
- countersignAdapters(),
601
- env.principalAuthority,
602
- io,
603
- );
604
- }
605
-
606
- // Typed failure terminal (verification / infra / non-zero without escalate)
607
- // is not 真无票 — settle controlled failure on the admitted run (失败诚实).
608
- // Only a true missing lawful typed terminal keeps the r5 unbound-continue.
609
- if (outcome.failedWithoutEscalate !== undefined) {
610
- await materializeAdmission();
611
- await markRunAdmitted(admitted, env.principalAuthority);
612
- return await presentControlledFailure(
613
- admitted,
614
- {
615
- timedOut: false,
616
- code: null,
617
- stderr: "",
618
- thrown: new Error(outcome.failedWithoutEscalate.diagnostic),
619
- },
620
- countersignAdapters(),
621
- env.principalAuthority,
622
- io,
623
- );
624
- }
625
-
626
- // Missing lawful 起居郎 terminal is not countersign body failure: leave
627
- // unbound and continue (true-unbound face) — unless parent gate handoff
628
- // already carries the durable board ticket (#969). Escalate / typed
629
- // failure above; bound refresh still fails honest via station.
630
- if (outcome.identity.kind === "ticket") {
631
- // #969: parent durable handoff wins over 起居郎 re-assert when both
632
- // present (receipt/assert mismatch → parent bind key).
633
- typedTicket = isSafePositiveTicketNumber(env.boundTicketNumber)
634
- ? env.boundTicketNumber
635
- : outcome.identity.ticketNumber;
636
- // #871: identity may hand a co-review set; absent field defaults to [main].
637
- typedCourtTicketNumbers = outcome.identity.courtTicketNumbers;
638
- } else if (isSafePositiveTicketNumber(env.boundTicketNumber)) {
639
- // Parent board handoff present; 起居郎 returned true-unbound — still
640
- // bind under the parent key (omitted receipt ticketNumber path).
641
- typedTicket = env.boundTicketNumber;
642
- }
643
- }
644
-
645
- // No prior same-parent run was selected. Materialize this invocation now;
646
- // true-unbound, first-ticket and deferred test-seam paths all retain their
647
- // own durable page. Public re-summons without parentRunPath always mint.
648
- await materializeAdmission();
649
- await markRunAdmitted(admitted, env.principalAuthority);
650
-
651
- if (gateParentRunPath !== undefined) {
652
- await persistAdmittedSourceRunPath(admitted, gateParentRunPath);
653
- admitted = { ...admitted, sourceRunPath: gateParentRunPath };
654
- }
655
-
656
- if (identityDiaristRan && typedTicket !== undefined) {
657
- try {
658
- await bindAdmittedTicketNumber(admitted, typedTicket);
659
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
660
- // First court: explicit set wins; omitted field → single-ticket face [main].
661
- // Set persistence is outside beforeDispatch — route write failures into the
662
- // same controlled-failure settlement as station children (#871 B7).
663
- await bindCourtTicketNumbersOnAdmitted(
664
- admitted,
665
- typedCourtTicketNumbers ?? [typedTicket],
666
- );
667
- } catch (error) {
668
- return await presentControlledFailure(
669
- admitted,
670
- {
671
- timedOut: false,
672
- code: null,
673
- stderr: "",
674
- thrown: error,
675
- },
676
- countersignAdapters(),
677
- env.principalAuthority,
678
- io,
679
- );
680
- }
681
- }
682
-
683
- const turnProjection: RoleTurnRequestProjectionOptions = {
684
- packageRoot: env.packageRoot,
685
- home: env.home,
686
- agentDir: env.agentDir,
687
- ...(env.model === undefined ? {} : { model: env.model }),
688
- ...pickEngineAxis(env),
689
- ...(env.timeoutMs === undefined ? {} : { timeoutMs: env.timeoutMs }),
690
- ...(env.correlationId === undefined || env.correlationId.trim() === ""
691
- ? {}
692
- : { correlationId: env.correlationId }),
693
- continuation: {
694
- kind: "initial",
695
- // #969/#879: gate path first mint carries parent payload as dialogue content;
696
- // ordinary public entry keeps package transport prompt.
697
- prompt: (env.reviewReask ?? env.gateReviewInstruction)
698
- ?? buildCountersignTransportPrompt(
699
- admitted,
700
- engineSessionMaterialFromOptions({
701
- ...pickEngineAxis(env),
702
- packageRoot: env.packageRoot,
703
- }),
704
- ),
705
- },
706
- };
707
- // Mutable shell: ticket bind re-projects activation before executeTurn.
708
- const turnRequest = buildCountersignTurnRequest(
709
- admitted,
710
- turnProjection,
711
- );
712
-
713
- const result = await runPostAdmissionOneShot({
714
- admitted,
715
- env,
716
- io,
717
- request: turnRequest,
718
- adapters: countersignAdapters({
719
- beforeDispatch: async (admittedSeat, lease) => {
720
- // Dossier pointer delivery rides post-admission after this hook (#709).
721
- if (!identityDiaristRan) {
722
- // Test seam (or any deferred identity): station owns assert + bind.
723
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
724
- } else if (typedTicket !== undefined) {
725
- await runCountersignCourtDiaristStation(admittedSeat, env, io);
726
- }
727
- await relocateAdmittedRunToTicket(
728
- admittedSeat,
729
- env.principalAuthority,
730
- lease,
731
- );
732
- Object.assign(
733
- turnRequest,
734
- buildCountersignTurnRequest(admittedSeat, turnProjection),
735
- );
736
- },
737
- }),
738
- ...(env.engine === undefined ? {} : { effectiveEngine: env.engine }),
739
- });
740
- await relocateAdmittedRunToTicket(admitted, env.principalAuthority);
741
- return result;
742
- },
743
- );
744
- } catch (error) {
745
- if (error instanceof CliUsageError) {
746
- presentStructuralRejection(error, io);
747
- return { exitCode: 2 };
748
- }
749
- throw error;
750
- }
751
- }
752
-
753
- function countersignAdapters(options?: {
754
- beforeDispatch?: (
755
- admitted: AdmittedCountersignInvocation,
756
- lease?: RunWriterLease,
757
- ) => void | Promise<void>;
758
- }) {
759
- return {
760
- trySettle: (
761
- admitted: AdmittedCountersignInvocation,
762
- authority: DurablePrincipalAuthority,
763
- scope?: { readonly courtAttemptId?: string },
764
- ) => trySettleCountersignTerminalResult(admitted, authority, scope),
765
- // Accepted receipts and failure terminals both present via shared path.
766
- shouldPresentSettled: () => true,
767
- ...(options?.beforeDispatch === undefined
768
- ? {}
769
- : { beforeDispatch: options.beforeDispatch }),
770
- };
771
- }
772
-
773
- /**
774
- * Resume a previously admitted Countersign run (#599 / DK-3 / #637 / #987).
775
- * Restores role/ticket/session identity. Gate
776
- * same-parent and explicit `ak-role resume <runId>` share this entry; summons
777
- * may carry this turn's instruction. Manual resume forwards only the caller's
778
- * message bytes. 起居录 path delivery remains
779
- * post-admission's single mount (#709).
780
- */
781
- export async function runPublicCountersignResume(
782
- request: PublicResumeRequest,
783
- env: CountersignRunEnv,
784
- io: CliIo,
785
- ): Promise<{
786
- exitCode: number;
787
- admitted?: AdmittedCountersignInvocation;
788
- terminal?: TerminalResult;
789
- }> {
790
- return await runPostAdmissionSeatResume({
791
- request,
792
- env,
793
- io,
794
- load: (effective) =>
795
- loadResumableCountersignRun(
796
- env.home,
797
- effective.runId,
798
- env.principalAuthority,
799
- ),
800
- buildTurnRequest: async (admitted, effective) => {
801
- const summonsPrepared = await prepareSummonsResumeMaterials(
802
- admitted.runDirectory,
803
- effective.summons,
804
- );
805
- return buildCountersignTurnRequest(
806
- admitted,
807
- resumeTurnRequestProjectionOptions(
808
- admitted,
809
- effective,
810
- env,
811
- summonsPrepared,
812
- ),
813
- );
814
- },
815
- adapters: countersignAdapters(),
816
- ...(env.engine === undefined ? {} : { effectiveEngine: env.engine }),
817
- });
818
- }