@fro.bot/systematic 3.21.8 → 3.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -754,10 +754,12 @@ import { createHash } from "crypto";
754
754
  import fs6 from "fs";
755
755
  import path5 from "path";
756
756
  var DEFAULT_LIMITS = {
757
- maxCommandOutputBytes: 1048576,
757
+ maxCommandOutputBytes: 8388608,
758
758
  maxCommandTimeoutMs: 5000,
759
- maxFiles: 4096,
760
- maxTotalFileBytes: 16777216,
759
+ maxFiles: 20000,
760
+ maxRemoteCommandOutputBytes: 1048576,
761
+ maxRemoteItems: 4096,
762
+ maxTotalFileBytes: 134217728,
761
763
  maxPathBytes: 4096
762
764
  };
763
765
  var UNTRACKED_FILES_ARGS = [
@@ -836,6 +838,8 @@ function mergeLimits(input) {
836
838
  maxCommandOutputBytes: input?.maxCommandOutputBytes ?? DEFAULT_LIMITS.maxCommandOutputBytes,
837
839
  maxCommandTimeoutMs: input?.maxCommandTimeoutMs ?? DEFAULT_LIMITS.maxCommandTimeoutMs,
838
840
  maxFiles: input?.maxFiles ?? DEFAULT_LIMITS.maxFiles,
841
+ maxRemoteCommandOutputBytes: input?.maxRemoteCommandOutputBytes ?? DEFAULT_LIMITS.maxRemoteCommandOutputBytes,
842
+ maxRemoteItems: input?.maxRemoteItems ?? DEFAULT_LIMITS.maxRemoteItems,
839
843
  maxTotalFileBytes: input?.maxTotalFileBytes ?? DEFAULT_LIMITS.maxTotalFileBytes,
840
844
  maxPathBytes: input?.maxPathBytes ?? DEFAULT_LIMITS.maxPathBytes
841
845
  };
@@ -1198,14 +1202,14 @@ function readCommitClosure(runner, context, limits) {
1198
1202
  function runRemoteCommand(runner, executable, args2, root, limits) {
1199
1203
  let result;
1200
1204
  try {
1201
- result = runner(executable, args2, root, limits.maxCommandOutputBytes, limits.maxCommandTimeoutMs);
1205
+ result = runner(executable, args2, root, limits.maxRemoteCommandOutputBytes, limits.maxCommandTimeoutMs);
1202
1206
  } catch {
1203
1207
  return { status: "error", reasonCode: "remote-command-failed" };
1204
1208
  }
1205
1209
  if (result.timedOut) {
1206
1210
  return { status: "error", reasonCode: "command-timeout" };
1207
1211
  }
1208
- if (outputBytes(result.stdout) > limits.maxCommandOutputBytes || outputBytes(result.stderr) > limits.maxCommandOutputBytes) {
1212
+ if (outputBytes(result.stdout) > limits.maxRemoteCommandOutputBytes || outputBytes(result.stderr) > limits.maxRemoteCommandOutputBytes) {
1209
1213
  return { status: "error", reasonCode: "remote-command-output-limit" };
1210
1214
  }
1211
1215
  if (result.status !== 0) {
@@ -1250,7 +1254,7 @@ function parsePullRequest(value, limits) {
1250
1254
  const number = record.number;
1251
1255
  const state = remoteState(record.state);
1252
1256
  const headRefOid = record.headRefOid;
1253
- if (typeof number !== "number" || !Number.isSafeInteger(number) || number <= 0 || state === undefined || typeof headRefOid !== "string" || !isHexRevision(headRefOid) || Object.keys(record).length > limits.maxFiles) {
1257
+ if (typeof number !== "number" || !Number.isSafeInteger(number) || number <= 0 || state === undefined || typeof headRefOid !== "string" || !isHexRevision(headRefOid) || Object.keys(record).length > limits.maxRemoteItems) {
1254
1258
  return { status: "error", reasonCode: "remote-missing-field" };
1255
1259
  }
1256
1260
  return {
@@ -1273,7 +1277,7 @@ function reviewState(value) {
1273
1277
  return;
1274
1278
  }
1275
1279
  function checkState(value, limits) {
1276
- if (!Array.isArray(value) || value.length === 0 || value.length > limits.maxFiles)
1280
+ if (!Array.isArray(value) || value.length === 0 || value.length > limits.maxRemoteItems)
1277
1281
  return;
1278
1282
  let pending = false;
1279
1283
  let failed = false;
@@ -3997,7 +4001,7 @@ import { randomBytes as randomBytes3 } from "crypto";
3997
4001
  import fs7 from "fs";
3998
4002
  import { fileURLToPath } from "url";
3999
4003
 
4000
- // node_modules/.bun/web-tree-sitter@0.27.0/node_modules/web-tree-sitter/web-tree-sitter.js
4004
+ // node_modules/.bun/web-tree-sitter@0.27.1/node_modules/web-tree-sitter/web-tree-sitter.js
4001
4005
  var __defProp = Object.defineProperty;
4002
4006
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4003
4007
  var Edit = class {
@@ -10377,6 +10381,23 @@ function observerReadbackFailedCause(reasonCode, site = "refresh-readback") {
10377
10381
  timedOut: reasonCode === "command-timeout"
10378
10382
  };
10379
10383
  }
10384
+ function hookCause(site, kind, reasonCode) {
10385
+ return {
10386
+ site,
10387
+ kind,
10388
+ ...reasonCode !== undefined ? { reasonCode } : {},
10389
+ timedOut: reasonCode === "command-timeout"
10390
+ };
10391
+ }
10392
+ function beforeCause(kind, reasonCode) {
10393
+ return hookCause("tool-before", kind, reasonCode);
10394
+ }
10395
+ function afterCause(kind, reasonCode) {
10396
+ return hookCause("tool-after", kind, reasonCode);
10397
+ }
10398
+ function afterFailure(kind, reasonCode) {
10399
+ return { status: "unavailable", cause: afterCause(kind, reasonCode) };
10400
+ }
10380
10401
  function remoteFailureReason(result) {
10381
10402
  if (result?.status === "missing-resource")
10382
10403
  return "missing-resource";
@@ -11088,10 +11109,11 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11088
11109
  const blockedQuestionCalls = new Map;
11089
11110
  const consumedQuestionTargets = new Set;
11090
11111
  let firstUnavailableCause;
11112
+ let pendingUnavailableCause;
11091
11113
  let seedDeferralRecord;
11092
11114
  let unitStartGeneration = 0;
11093
11115
  function startFreshUnit(input, policy) {
11094
- const result = guard.startUnit(input, policy);
11116
+ const result = requireGuard().startUnit(input, policy);
11095
11117
  if (result.status === "started") {
11096
11118
  unitStartGeneration += 1;
11097
11119
  firstUnavailableCause = undefined;
@@ -11101,7 +11123,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11101
11123
  }
11102
11124
  function recordUnavailable(target, cause) {
11103
11125
  const result = target.setMode({ mode: "unavailable" });
11104
- if (cause && result.status === "changed" && !firstUnavailableCause) {
11126
+ if (result.status === "changed" && !firstUnavailableCause) {
11105
11127
  firstUnavailableCause = cause;
11106
11128
  }
11107
11129
  }
@@ -11154,7 +11176,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11154
11176
  const parentObserver = options.observer;
11155
11177
  if (!parentObserver)
11156
11178
  return { status: "fatal" };
11157
- const pinnedTargetIdentity = guard.status().unit?.pinnedOperationTargetIdentity;
11179
+ const pinnedTargetIdentity = requireGuard().status().unit?.pinnedOperationTargetIdentity;
11158
11180
  if (pinnedTargetIdentity === undefined || pinnedTargetIdentity === options.workspaceIdentity) {
11159
11181
  return {
11160
11182
  status: "ok",
@@ -11195,7 +11217,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11195
11217
  };
11196
11218
  }
11197
11219
  function questionResource(target) {
11198
- const status = guard.status();
11220
+ const status = requireGuard().status();
11199
11221
  return `workflow/${target}/${status.unit?.unitId ?? status.epoch?.epochId ?? "unknown"}`;
11200
11222
  }
11201
11223
  function questionProjection() {
@@ -11446,16 +11468,26 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11446
11468
  }
11447
11469
  function publishUnavailable(cause, reusableLedger) {
11448
11470
  ledger = reusableLedger ?? mintLedger();
11449
- guard = createGuard(ledger);
11450
- if (options.config.mode !== "disabled") {
11451
- recordUnavailable(guard, cause);
11452
- }
11471
+ const published = createGuard(ledger);
11472
+ guard = published;
11473
+ if (options.config.mode !== "disabled")
11474
+ recordUnavailable(published, cause);
11475
+ pendingUnavailableCause = undefined;
11453
11476
  initialized = true;
11454
11477
  }
11478
+ function commitPendingUnavailable() {
11479
+ const cause = pendingUnavailableCause;
11480
+ pendingUnavailableCause = undefined;
11481
+ if (cause && options.config.mode !== "disabled") {
11482
+ recordUnavailable(requireGuard(), cause);
11483
+ retryableEmptyHistory = false;
11484
+ }
11485
+ }
11455
11486
  function publishFresh(reusableLedger) {
11456
11487
  ledger = reusableLedger ?? mintLedger();
11457
11488
  guard = createGuard(ledger);
11458
11489
  firstUnavailableCause = undefined;
11490
+ commitPendingUnavailable();
11459
11491
  initialized = true;
11460
11492
  }
11461
11493
  function ensureSynchronousRuntime() {
@@ -11542,6 +11574,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11542
11574
  ledger = recoveredLedger;
11543
11575
  guard = recoveredGuard;
11544
11576
  firstUnavailableCause = undefined;
11577
+ commitPendingUnavailable();
11545
11578
  initialized = true;
11546
11579
  return;
11547
11580
  }
@@ -11585,6 +11618,14 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11585
11618
  publishFresh(reusableLedger);
11586
11619
  }
11587
11620
  async function initializeSession(sessionID, allowFresh) {
11621
+ try {
11622
+ await initializeSessionSteps(sessionID, allowFresh);
11623
+ } catch {
11624
+ retryableEmptyHistory = false;
11625
+ publishUnavailable(initializationCause("initialization-threw"));
11626
+ }
11627
+ }
11628
+ async function initializeSessionSteps(sessionID, allowFresh) {
11588
11629
  const reader = options.hostReadback?.readSessionPartsStrict ?? options.hostReadback?.readSessionParts;
11589
11630
  if (!reader) {
11590
11631
  publishFresh();
@@ -11640,30 +11681,30 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11640
11681
  const rollupKey = `${host.sessionID}:${host.callID}:${childSessionID}`;
11641
11682
  if (rolledUpChildren.has(rollupKey))
11642
11683
  return;
11643
- const parentBefore = guard.currentOperationContext();
11684
+ const parentBefore = requireGuard().currentOperationContext();
11644
11685
  const children = await options.hostReadback.listChildren(host.sessionID);
11645
11686
  if (!children.some((child) => child.sessionId === childSessionID && child.parentID === host.sessionID)) {
11646
- markUnavailable();
11687
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
11647
11688
  return;
11648
11689
  }
11649
11690
  let parts2;
11650
11691
  try {
11651
11692
  parts2 = await options.hostReadback.readSessionParts(childSessionID);
11652
11693
  } catch {
11653
- markUnavailable();
11694
+ markUnavailable(afterCause("history-read-failed"));
11654
11695
  return;
11655
11696
  }
11656
11697
  const classification = classifyMarkersFromParts(parts2);
11657
11698
  if (classification.kind === "foreign-empty")
11658
11699
  return;
11659
11700
  if (classification.kind === "ambiguous") {
11660
- markUnavailable();
11701
+ markUnavailable(afterCause("markers-ambiguous"));
11661
11702
  return;
11662
11703
  }
11663
11704
  const ownMarkers = classification.markers;
11664
11705
  const seed = extractReceiptReadbackSeed(ownMarkers);
11665
11706
  if (seed.status !== "ready") {
11666
- markUnavailable();
11707
+ markUnavailable(afterCause("recovery-rejected"));
11667
11708
  return;
11668
11709
  }
11669
11710
  const childLedger = createReceiptLedger({
@@ -11672,18 +11713,18 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11672
11713
  sessionSalt: seed.sessionSalt
11673
11714
  });
11674
11715
  if (!seedAgrees(childLedger, seed)) {
11675
- markUnavailable();
11716
+ markUnavailable(afterCause("recovery-rejected"));
11676
11717
  return;
11677
11718
  }
11678
11719
  const recovered = childLedger.recoverReadback(ownMarkers);
11679
11720
  if (recovered.status === "rejected") {
11680
- markUnavailable();
11721
+ markUnavailable(afterCause("recovery-rejected"));
11681
11722
  return;
11682
11723
  }
11683
11724
  try {
11684
11725
  await options.observer?.snapshot();
11685
11726
  } catch {}
11686
- const currentStatus = guard.status();
11727
+ const currentStatus = requireGuard().status();
11687
11728
  if (!currentStatus.epoch || !currentStatus.unit)
11688
11729
  return;
11689
11730
  const expectedWorkspace = childLedger.digestIdentity("workspace", parentBefore.workspaceIdentity);
@@ -11694,49 +11735,53 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11694
11735
  if (!localOperation(operation))
11695
11736
  continue;
11696
11737
  if (childReceipt.canonical.workspaceDigest !== expectedWorkspace) {
11697
- markUnavailable();
11738
+ markUnavailable(afterCause("operation-observation-rejected"));
11698
11739
  return;
11699
11740
  }
11700
11741
  const targetIdentity = childReceipt.canonical.operationTargetIdentity;
11701
11742
  if (!targetIdentity) {
11702
- markUnavailable();
11743
+ markUnavailable(afterCause("operation-input-invalid"));
11703
11744
  return;
11704
11745
  }
11705
11746
  if (batchTargetIdentity !== undefined && targetIdentity !== batchTargetIdentity) {
11706
- markUnavailable();
11747
+ markUnavailable(afterCause("operation-target-mismatch"));
11707
11748
  return;
11708
11749
  }
11709
11750
  batchTargetIdentity = targetIdentity;
11710
11751
  const registration = operationObserverRegistration(targetIdentity);
11711
11752
  if (!registration || !options.observer) {
11712
- markUnavailable();
11753
+ markUnavailable(afterCause("operation-input-invalid"));
11713
11754
  return;
11714
11755
  }
11715
11756
  let validation;
11716
11757
  try {
11717
11758
  validation = options.observer.validateRegisteredWorktree(registration.targetRoot);
11718
11759
  } catch {
11719
- markUnavailable();
11760
+ markUnavailable(afterCause("operation-input-invalid"));
11720
11761
  return;
11721
11762
  }
11722
11763
  if (validation.status === "error" || validation.targetRoot !== registration.targetRoot || registeredWorktreeIdentity(validation) !== registration.registeredWorktreeIdentity || registration.observer.targetDigest !== targetIdentity) {
11723
- markUnavailable();
11764
+ markUnavailable(afterCause("operation-input-invalid", validation.status === "error" ? validation.reasonCode : undefined));
11724
11765
  return;
11725
11766
  }
11726
11767
  let targetResult;
11727
11768
  try {
11728
11769
  targetResult = await registration.observer.snapshot();
11729
11770
  } catch {
11730
- markUnavailable();
11771
+ markUnavailable(afterCause("local-snapshot-error"));
11772
+ return;
11773
+ }
11774
+ if (targetResult.status === "unavailable") {
11775
+ markUnavailable(afterCause("local-snapshot-unavailable", targetResult.reasonCode));
11731
11776
  return;
11732
11777
  }
11733
- if (targetResult.status === "unavailable" || targetResult.snapshot.targetDigest !== targetIdentity) {
11734
- markUnavailable();
11778
+ if (targetResult.snapshot.targetDigest !== targetIdentity) {
11779
+ markUnavailable(afterCause("operation-target-mismatch"));
11735
11780
  return;
11736
11781
  }
11737
11782
  const recoveredContext = recoveredOperationContexts.get(childReceipt.canonical.receiptId);
11738
11783
  if (recoveredContext && recoveredContext.targetIdentity !== targetIdentity || targetIdentity !== options.observer.targetDigest && !recoveredContext) {
11739
- markUnavailable();
11784
+ markUnavailable(afterCause("operation-target-mismatch"));
11740
11785
  return;
11741
11786
  }
11742
11787
  const expectedRepository = childLedger.digestIdentity("repository", targetResult.snapshot.repositoryRevisionDigest);
@@ -11755,7 +11800,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11755
11800
  const childReceipt = candidate.receipt;
11756
11801
  const targetSnapshot = candidate.snapshot;
11757
11802
  const operation = childReceipt.canonical.operation;
11758
- const parentContext = guard.currentOperationContext();
11803
+ const parentContext = requireGuard().currentOperationContext();
11759
11804
  const beforeSnapshot = candidate.before;
11760
11805
  const callID = `task-${host.callID}-${childReceipt.canonical.receiptId}`;
11761
11806
  const observation = {
@@ -11782,7 +11827,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11782
11827
  noOp: false
11783
11828
  }
11784
11829
  };
11785
- const result = await guard.observeTrustedRecoveredOperation(observation);
11830
+ const result = await requireGuard().observeTrustedRecoveredOperation(observation);
11786
11831
  if (result.status === "accepted") {
11787
11832
  const receipt = receiptForOperation(callID, operation);
11788
11833
  if (receipt)
@@ -11797,7 +11842,20 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11797
11842
  function markUnavailable(cause) {
11798
11843
  if (options.config.mode === "disabled")
11799
11844
  return;
11800
- recordUnavailable(guard, cause);
11845
+ const published = guard;
11846
+ if (!published) {
11847
+ pendingUnavailableCause ??= cause;
11848
+ return;
11849
+ }
11850
+ recordUnavailable(published, cause);
11851
+ }
11852
+ function guardPublished() {
11853
+ return guard !== undefined;
11854
+ }
11855
+ function requireGuard() {
11856
+ if (!guard)
11857
+ throw new Error("workflow guard used before it was published");
11858
+ return guard;
11801
11859
  }
11802
11860
  function bindCall(callDigest, kind, fingerprint) {
11803
11861
  const existing = callBindings.get(callDigest);
@@ -11808,12 +11866,12 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11808
11866
  if (existing.kind === kind && existing.fingerprint === fingerprint) {
11809
11867
  return "duplicate";
11810
11868
  }
11811
- markUnavailable();
11869
+ markUnavailable(beforeCause("call-lifecycle-mismatch"));
11812
11870
  return "conflict";
11813
11871
  }
11814
11872
  function abandonPending() {
11815
11873
  for (const [callDigest, pending] of pendingCompletes) {
11816
- guard.abandonTransition({
11874
+ requireGuard().abandonTransition({
11817
11875
  callId: pending.callID,
11818
11876
  transitionId: pending.transitionId
11819
11877
  });
@@ -11831,7 +11889,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11831
11889
  }
11832
11890
  }
11833
11891
  function readStatus() {
11834
- return guard.status();
11892
+ return guard ? guard.status() : unavailableWorkflowStatus();
11835
11893
  }
11836
11894
  function control(sessionID, input) {
11837
11895
  if (!isRecord7(input) || input.mode !== "disabled") {
@@ -11854,7 +11912,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11854
11912
  return questionUnavailableResult();
11855
11913
  if (pending.requestID && questionProjection().status === "attested") {
11856
11914
  if (consumeQuestion(pending)) {
11857
- guard.setMode({ mode: "disabled" });
11915
+ requireGuard().setMode({ mode: "disabled" });
11858
11916
  return {
11859
11917
  title: "Workflow guard disabled",
11860
11918
  output: JSON.stringify({ status: "disabled" })
@@ -11873,12 +11931,12 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11873
11931
  };
11874
11932
  }
11875
11933
  function observerReadbackApplies() {
11876
- return options.observer !== undefined && guard.status().epoch !== null;
11934
+ return options.observer !== undefined && requireGuard().status().epoch !== null;
11877
11935
  }
11878
11936
  function skippedWithRemoteTail(head) {
11879
11937
  return [
11880
11938
  unconfirmedReadback(head),
11881
- ...guard.status().satisfiedOperations.filter(remoteOperation).map((operation) => unconfirmedReadback(operation))
11939
+ ...requireGuard().status().satisfiedOperations.filter(remoteOperation).map((operation) => unconfirmedReadback(operation))
11882
11940
  ];
11883
11941
  }
11884
11942
  async function refreshReadback() {
@@ -11903,7 +11961,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11903
11961
  return applyReadbackAndProbeRemote(effective, read.value);
11904
11962
  }
11905
11963
  async function applyReadbackAndProbeRemote(effective, snapshot) {
11906
- const observed = guard.observeReadback({
11964
+ const observed = requireGuard().observeReadback({
11907
11965
  workspaceIdentity: options.workspaceIdentity,
11908
11966
  repositoryIdentity: snapshot.repositoryRevisionDigest,
11909
11967
  worktreeIdentity: snapshot.worktreeRevisionDigest,
@@ -11920,7 +11978,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11920
11978
  return remote.unconfirmed;
11921
11979
  }
11922
11980
  async function refreshRemoteReadbacks(observer, local) {
11923
- const remoteOperations = guard.status().satisfiedOperations.filter(remoteOperation);
11981
+ const remoteOperations = requireGuard().status().satisfiedOperations.filter(remoteOperation);
11924
11982
  if (remoteOperations.length === 0)
11925
11983
  return { kind: "ok", unconfirmed: [] };
11926
11984
  const remoteSnapshot = observer.remoteSnapshot;
@@ -11940,14 +11998,14 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
11940
11998
  reasonCode: remoteFailureReason(result)
11941
11999
  };
11942
12000
  }
11943
- const observed = guard.observeReadback(remoteReadbackInput(operation, local, options.workspaceIdentity, result.snapshot));
12001
+ const observed = requireGuard().observeReadback(remoteReadbackInput(operation, local, options.workspaceIdentity, result.snapshot));
11944
12002
  if (observed.status === "rejected")
11945
12003
  return { kind: "fatal", unconfirmed };
11946
12004
  }
11947
12005
  return { kind: "ok", unconfirmed };
11948
12006
  }
11949
12007
  function currentMarkerSource() {
11950
- const status = guard.status();
12008
+ const status = requireGuard().status();
11951
12009
  const statusDigest = ledger.digestIdentity("resource", JSON.stringify({
11952
12010
  state: status.state,
11953
12011
  reasonCode: status.reasonCode,
@@ -12084,7 +12142,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12084
12142
  };
12085
12143
  }
12086
12144
  function writeNoEpochBlockedResult(callDigest, output, target) {
12087
- const current = guard.status();
12145
+ const current = requireGuard().status();
12088
12146
  const terminal = !current.epoch ? {
12089
12147
  target,
12090
12148
  status: "rejected",
@@ -12150,7 +12208,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12150
12208
  }
12151
12209
  const challenge = challengeForTransition(host.sessionID, target);
12152
12210
  if (!challenge) {
12153
- markUnavailable();
12211
+ markUnavailable(hookCause("tool-before", "call-lifecycle-mismatch"));
12154
12212
  return true;
12155
12213
  }
12156
12214
  if (options.config.mode === "protected") {
@@ -12174,10 +12232,13 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12174
12232
  }
12175
12233
  return;
12176
12234
  }
12177
- const status = guard.status();
12235
+ const status = requireGuard().status();
12178
12236
  if (gateCompleteWithQuestion(host, target, callDigest, status))
12179
12237
  return;
12180
- const prepared = guard.prepareTransition({ callId: host.callID, target });
12238
+ const prepared = requireGuard().prepareTransition({
12239
+ callId: host.callID,
12240
+ target
12241
+ });
12181
12242
  if (prepared.status === "allowed") {
12182
12243
  pendingCompletes.set(callDigest, {
12183
12244
  callID: host.callID,
@@ -12223,7 +12284,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12223
12284
  const { parentTargetRoot, sessionLocation } = effectiveTargetContext();
12224
12285
  const fingerprint = argsFingerprint(ledger, host.tool, args2, sessionLocation);
12225
12286
  if (!fingerprint) {
12226
- markUnavailable();
12287
+ markUnavailable(beforeCause("operation-input-invalid"));
12227
12288
  return;
12228
12289
  }
12229
12290
  const callDigest = digestCall(ledger, host.callID);
@@ -12236,24 +12297,24 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12236
12297
  validateRegisteredWorktree: options.observer.validateRegisteredWorktree
12237
12298
  });
12238
12299
  if (target.status === "unavailable") {
12239
- markUnavailable();
12300
+ markUnavailable(beforeCause("operation-input-invalid", target.reasonCode));
12240
12301
  return;
12241
12302
  }
12242
12303
  let registeredTarget;
12243
12304
  try {
12244
12305
  registeredTarget = options.observer.validateRegisteredWorktree(target.targetRoot);
12245
12306
  } catch {
12246
- markUnavailable();
12307
+ markUnavailable(beforeCause("operation-input-invalid"));
12247
12308
  return;
12248
12309
  }
12249
12310
  const registeredIdentity = registeredWorktreeIdentity(registeredTarget);
12250
12311
  if (registeredTarget.status !== "ok" || registeredIdentity === undefined || registeredTarget.targetRoot !== target.targetRoot) {
12251
- markUnavailable();
12312
+ markUnavailable(beforeCause("operation-input-invalid", registeredTarget.status === "error" ? registeredTarget.reasonCode : undefined));
12252
12313
  return;
12253
12314
  }
12254
12315
  const canonicalParentTargetRoot = canonicalExistingPath(parentTargetRoot, fs8.realpathSync);
12255
12316
  if (!canonicalParentTargetRoot) {
12256
- markUnavailable();
12317
+ markUnavailable(beforeCause("operation-input-invalid"));
12257
12318
  return;
12258
12319
  }
12259
12320
  const operationObserver = target.targetRoot === canonicalParentTargetRoot ? options.observer : [...operationObservers.values()].find((registration) => registration.targetRoot === target.targetRoot)?.observer ?? (options.createTargetObserver ?? defaultTargetObserver)(target.targetRoot);
@@ -12261,15 +12322,15 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12261
12322
  try {
12262
12323
  result = await operationObserver.snapshot();
12263
12324
  } catch {
12264
- markUnavailable();
12325
+ markUnavailable(beforeCause("local-snapshot-error"));
12265
12326
  return;
12266
12327
  }
12267
12328
  if (result.status === "unavailable") {
12268
- markUnavailable();
12329
+ markUnavailable(beforeCause("local-snapshot-unavailable", result.reasonCode));
12269
12330
  return;
12270
12331
  }
12271
12332
  if (result.snapshot.targetDigest !== operationObserver.targetDigest) {
12272
- markUnavailable();
12333
+ markUnavailable(beforeCause("operation-target-mismatch"));
12273
12334
  return;
12274
12335
  }
12275
12336
  rememberOperationObserver({
@@ -12387,7 +12448,10 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12387
12448
  } catch (error) {
12388
12449
  if (isWorkflowGuardBlockedError(error))
12389
12450
  throw error;
12390
- markUnavailable();
12451
+ markUnavailable(hookCause("tool-before", "hook-threw"));
12452
+ if (options.config.mode === "protected" && !guardPublished()) {
12453
+ throw createWorkflowGuardBlockedError("guard-unavailable");
12454
+ }
12391
12455
  }
12392
12456
  }
12393
12457
  async function seedRemoteScopes(operations) {
@@ -12434,7 +12498,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12434
12498
  return;
12435
12499
  }
12436
12500
  function recordSeedDeferral(operation, reasonCode) {
12437
- if (guard.hasResourceBaseline(operation))
12501
+ if (requireGuard().hasResourceBaseline(operation))
12438
12502
  return;
12439
12503
  const observer = options.observer;
12440
12504
  if (!observer)
@@ -12447,7 +12511,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12447
12511
  });
12448
12512
  }
12449
12513
  function seedDeferralEntries(reset = false) {
12450
- const unitId = guard.status().unit?.unitId;
12514
+ const unitId = requireGuard().status().unit?.unitId;
12451
12515
  if (!unitId)
12452
12516
  return [];
12453
12517
  if (seedDeferralRecord?.unitId === unitId)
@@ -12473,7 +12537,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12473
12537
  const deferred = deferredSeedFor(operation);
12474
12538
  if (!deferred)
12475
12539
  return;
12476
- if (guard.hasResourceBaseline(operation)) {
12540
+ if (requireGuard().hasResourceBaseline(operation)) {
12477
12541
  clearSeedDeferral(operation);
12478
12542
  return;
12479
12543
  }
@@ -12489,12 +12553,12 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12489
12553
  }
12490
12554
  if (before.status !== "available")
12491
12555
  return;
12492
- const observed = guard.observeReadback(deferredBaselineReadback(operation, before.snapshot));
12556
+ const observed = requireGuard().observeReadback(deferredBaselineReadback(operation, before.snapshot));
12493
12557
  if (observed.status !== "rejected")
12494
12558
  clearSeedDeferral(operation);
12495
12559
  }
12496
12560
  function currentInitiator() {
12497
- const current = guard.status();
12561
+ const current = requireGuard().status();
12498
12562
  return current.epoch && current.unit ? {
12499
12563
  epochId: current.epoch.epochId,
12500
12564
  unitId: current.unit.unitId,
@@ -12554,7 +12618,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12554
12618
  timedOut: false
12555
12619
  });
12556
12620
  }
12557
- const observed = guard.observeReadback(remoteReadbackInput(operation, local, options.workspaceIdentity, result.snapshot, operation !== "pr-creation"));
12621
+ const observed = requireGuard().observeReadback(remoteReadbackInput(operation, local, options.workspaceIdentity, result.snapshot, operation !== "pr-creation"));
12558
12622
  if (observed.status === "rejected") {
12559
12623
  return fatalSeed({
12560
12624
  site: "seed-remote-scope",
@@ -12571,8 +12635,9 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12571
12635
  const finalSkill = normalizeSkill(host.tool, host.args);
12572
12636
  const completedSkill = completedSkillCalls.get(callDigest);
12573
12637
  if (completedSkill) {
12574
- if (completedSkill !== finalSkill)
12575
- markUnavailable();
12638
+ if (completedSkill !== finalSkill) {
12639
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12640
+ }
12576
12641
  return;
12577
12642
  }
12578
12643
  const pending = pendingSkills.get(callDigest);
@@ -12580,14 +12645,14 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12580
12645
  return;
12581
12646
  pendingSkills.delete(callDigest);
12582
12647
  if (!finalSkill || finalSkill !== pending.skill) {
12583
- markUnavailable();
12648
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12584
12649
  return;
12585
12650
  }
12586
12651
  if (!isSuccessfulSkillAfter(output))
12587
12652
  return;
12588
12653
  completedSkillCalls.set(callDigest, pending.skill);
12589
12654
  await activateCompletedSkill(pending.skill);
12590
- const status = guard.status();
12655
+ const status = requireGuard().status();
12591
12656
  if (status.epoch) {
12592
12657
  mergeProgressionMarker(output, projectReceiptProgressionMarker(ledger, {
12593
12658
  target: "epoch",
@@ -12611,14 +12676,14 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12611
12676
  }
12612
12677
  }
12613
12678
  async function activateCompletedSkill(skill) {
12614
- const activation = guard.activate({
12679
+ const activation = requireGuard().activate({
12615
12680
  event: "guarded-skill",
12616
12681
  skill,
12617
12682
  outcome: "success"
12618
12683
  });
12619
12684
  if (activation.status === "activated" || activation.status === "reused" || activation.status === "attached") {
12620
12685
  runtimePolicy = trustedSkillPolicy(skill, options.runtimeRequiredOperations ?? []);
12621
- const status = guard.status();
12686
+ const status = requireGuard().status();
12622
12687
  if (!status.unit || status.unit.status === "completed") {
12623
12688
  startFreshUnit({}, runtimePolicy);
12624
12689
  const cause = await seedRemoteScopes(runtimePolicy.requiredOperations ?? []);
@@ -12632,19 +12697,19 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12632
12697
  const pending = pendingStarts.get(callDigest);
12633
12698
  pendingStarts.delete(callDigest);
12634
12699
  if (!pending) {
12635
- markUnavailable();
12700
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12636
12701
  return;
12637
12702
  }
12638
12703
  const normalized = normalizeStartInput(host.args);
12639
12704
  if (normalized === undefined || JSON.stringify(normalized) !== pending.fingerprint) {
12640
- markUnavailable();
12705
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12641
12706
  return;
12642
12707
  }
12643
12708
  if (!isSuccessfulAfter(output))
12644
12709
  return;
12645
12710
  const result = startFreshUnit(pending.input, runtimePolicy);
12646
12711
  if (result.status === "rejected" && (result.reasonCode === "guard-unavailable" || result.reasonCode === "invalid-configuration")) {
12647
- markUnavailable();
12712
+ markUnavailable(afterCause("unit-start-rejected", result.reasonCode));
12648
12713
  }
12649
12714
  if (result.status === "started") {
12650
12715
  const cause = await seedRemoteScopes(runtimePolicy.requiredOperations ?? []);
@@ -12652,7 +12717,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12652
12717
  markUnavailable(cause);
12653
12718
  }
12654
12719
  if (result.status === "started") {
12655
- const startedStatus = guard.status();
12720
+ const startedStatus = requireGuard().status();
12656
12721
  const epoch = startedStatus.epoch;
12657
12722
  const unit = startedStatus.unit;
12658
12723
  if (!epoch || !unit)
@@ -12674,7 +12739,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12674
12739
  const replay = finalizedCompletes.get(callDigest);
12675
12740
  if (!replay)
12676
12741
  return false;
12677
- guard.finalizeTransition({
12742
+ requireGuard().finalizeTransition({
12678
12743
  callId: replay.callID,
12679
12744
  transitionId: replay.transitionId
12680
12745
  });
@@ -12683,7 +12748,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12683
12748
  function finalizeComplete(callDigest, pending, output, readbacks) {
12684
12749
  pendingCompletes.delete(callDigest);
12685
12750
  if (!isSuccessfulAfter(output)) {
12686
- guard.abandonTransition({
12751
+ requireGuard().abandonTransition({
12687
12752
  callId: pending.callID,
12688
12753
  transitionId: pending.transitionId
12689
12754
  });
@@ -12697,7 +12762,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12697
12762
  });
12698
12763
  return;
12699
12764
  }
12700
- const result = guard.finalizeTransition({
12765
+ const result = requireGuard().finalizeTransition({
12701
12766
  callId: pending.callID,
12702
12767
  transitionId: pending.transitionId,
12703
12768
  ...readbacks ? { readbacks } : {}
@@ -12707,8 +12772,8 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12707
12772
  writeSuccessfulCompletionResult(output, pending.target);
12708
12773
  return;
12709
12774
  }
12710
- markUnavailable();
12711
12775
  const reasonCode = "reasonCode" in result ? result.reasonCode : "guard-unavailable";
12776
+ markUnavailable(afterCause("transition-finalize-rejected", reasonCode));
12712
12777
  const terminal = {
12713
12778
  target: pending.target,
12714
12779
  status: result.status === "waiting" ? "unavailable" : result.status,
@@ -12747,7 +12812,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12747
12812
  if (!expectedTarget)
12748
12813
  return false;
12749
12814
  if (finalTarget !== expectedTarget) {
12750
- markUnavailable();
12815
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12751
12816
  return true;
12752
12817
  }
12753
12818
  if (replay)
@@ -12758,7 +12823,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12758
12823
  }
12759
12824
  function abandonComplete(callDigest, pending, remember) {
12760
12825
  pendingCompletes.delete(callDigest);
12761
- guard.abandonTransition({
12826
+ requireGuard().abandonTransition({
12762
12827
  callId: pending.callID,
12763
12828
  transitionId: pending.transitionId
12764
12829
  });
@@ -12832,7 +12897,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12832
12897
  };
12833
12898
  }
12834
12899
  async function completionRemoteReadbacks(observer) {
12835
- const remoteOperations = guard.status().satisfiedOperations.filter(remoteOperation);
12900
+ const remoteOperations = requireGuard().status().satisfiedOperations.filter(remoteOperation);
12836
12901
  if (remoteOperations.length === 0)
12837
12902
  return { status: "ready", readbacks: [] };
12838
12903
  const remoteSnapshot = observer.remoteSnapshot;
@@ -12857,11 +12922,11 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12857
12922
  status: "unavailable",
12858
12923
  reasonCode: "invalid-transition"
12859
12924
  });
12860
- markUnavailable();
12925
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12861
12926
  writeAbandonedCompletionResult(output, pending.target, "invalid-transition");
12862
12927
  }
12863
12928
  function finishUnreplayableComplete(finalTarget, output) {
12864
- markUnavailable();
12929
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12865
12930
  writeAbandonedCompletionResult(output, finalTarget, "guard-unavailable");
12866
12931
  }
12867
12932
  function finishUnavailableReadback(callDigest, pending, output, reasonCode) {
@@ -12875,7 +12940,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12875
12940
  writeAbandonedCompletionResult(output, pending.target, "finalization-failed");
12876
12941
  }
12877
12942
  function finishReadbackTimeout(callDigest, pending, output) {
12878
- if (guard.status().state === "unavailable") {
12943
+ if (requireGuard().status().state === "unavailable") {
12879
12944
  finishUnavailableReadback(callDigest, pending, output, "command-timeout");
12880
12945
  } else {
12881
12946
  finishRetryableReadback(callDigest, pending, output);
@@ -12936,48 +13001,57 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
12936
13001
  function takePendingOperation(callDigest, host, fingerprint) {
12937
13002
  const pending = pendingOperations.get(callDigest);
12938
13003
  if (!pending) {
12939
- markUnavailable();
13004
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12940
13005
  return;
12941
13006
  }
12942
13007
  pendingOperations.delete(callDigest);
12943
13008
  if (pending.tool !== host.tool || fingerprint === undefined || fingerprint !== pending.argsFingerprint) {
12944
13009
  sealOperation(callDigest, pending, true);
12945
- markUnavailable();
13010
+ markUnavailable(afterCause("call-lifecycle-mismatch"));
12946
13011
  return;
12947
13012
  }
12948
13013
  return pending;
12949
13014
  }
12950
- async function captureAfterOperation(pending) {
12951
- if (!options.observer)
12952
- return;
13015
+ function afterTargetFailure(observer, pending) {
12953
13016
  let validation;
12954
13017
  try {
12955
- validation = options.observer.validateRegisteredWorktree(pending.targetRoot);
13018
+ validation = observer.validateRegisteredWorktree(pending.targetRoot);
12956
13019
  } catch {
12957
- return;
13020
+ return afterFailure("operation-input-invalid");
12958
13021
  }
12959
13022
  if (validation.status === "error" || validation.targetRoot !== pending.targetRoot || registeredWorktreeIdentity(validation) !== pending.registeredWorktreeIdentity) {
12960
- return;
13023
+ return afterFailure("operation-input-invalid", validation.status === "error" ? validation.reasonCode : undefined);
12961
13024
  }
13025
+ return;
13026
+ }
13027
+ async function captureAfterOperation(pending) {
13028
+ if (!options.observer)
13029
+ return afterFailure("operation-input-invalid");
13030
+ const targetFailure = afterTargetFailure(options.observer, pending);
13031
+ if (targetFailure)
13032
+ return targetFailure;
12962
13033
  let result;
12963
13034
  try {
12964
13035
  result = await pending.observer.snapshot();
12965
13036
  } catch {
12966
- return;
13037
+ return afterFailure("local-snapshot-error");
12967
13038
  }
12968
- return result.status === "available" && result.snapshot.targetDigest === pending.targetIdentity ? result.snapshot : undefined;
13039
+ if (result.status === "unavailable") {
13040
+ return afterFailure("local-snapshot-unavailable", result.reasonCode);
13041
+ }
13042
+ return result.snapshot.targetDigest === pending.targetIdentity ? { status: "available", snapshot: result.snapshot } : afterFailure("operation-target-mismatch");
12969
13043
  }
12970
13044
  async function captureRemoteAfter(operation, observer) {
12971
13045
  const remoteSnapshot = observer.remoteSnapshot;
12972
13046
  if (!remoteSnapshot)
12973
- return;
13047
+ return afterFailure("operation-observation-rejected");
12974
13048
  let result;
12975
13049
  try {
12976
13050
  result = await remoteSnapshot(operation, "after");
12977
13051
  } catch {
12978
- return;
13052
+ return afterFailure("operation-observation-rejected");
12979
13053
  }
12980
- return result.status === "available" ? result.snapshot : undefined;
13054
+ return result.status === "available" ? { status: "available", snapshot: result.snapshot } : afterFailure("operation-observation-rejected", remoteFailureReason(result));
12981
13055
  }
12982
13056
  async function readRemoteScope(reader, operation, phase = "after") {
12983
13057
  try {
@@ -13028,12 +13102,12 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13028
13102
  async function classifyLocalObservation(observation) {
13029
13103
  const classify = options.classifier?.classifyOperation;
13030
13104
  if (!classify)
13031
- return { status: "unavailable" };
13105
+ return afterFailure("operation-classifier-unavailable");
13032
13106
  let classification;
13033
13107
  try {
13034
13108
  classification = await classify(observation);
13035
13109
  } catch {
13036
- return { status: "unavailable" };
13110
+ return afterFailure("operation-classifier-unavailable");
13037
13111
  }
13038
13112
  if (!localOperation(classification.category)) {
13039
13113
  if (remoteOperation(classification.category)) {
@@ -13045,7 +13119,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13045
13119
  }
13046
13120
  };
13047
13121
  }
13048
- return classification.category === null ? { status: "deferred" } : { status: "unavailable" };
13122
+ return classification.category === null ? { status: "deferred" } : afterFailure("operation-classifier-unavailable");
13049
13123
  }
13050
13124
  return {
13051
13125
  status: "ready",
@@ -13054,7 +13128,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13054
13128
  }
13055
13129
  async function observeClassifiedOperation(observation) {
13056
13130
  try {
13057
- return await guard.observeOperation(observation);
13131
+ return await requireGuard().observeOperation(observation);
13058
13132
  } catch {
13059
13133
  return;
13060
13134
  }
@@ -13072,16 +13146,17 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13072
13146
  const parsedOutput = parseHostOutput(output);
13073
13147
  if (!parsedOutput) {
13074
13148
  sealOperation(callDigest, pending, true);
13075
- return { status: "unavailable" };
13149
+ return afterFailure("operation-input-invalid");
13076
13150
  }
13077
- const afterSnapshot = await captureAfterOperation(pending);
13151
+ const capture = await captureAfterOperation(pending);
13078
13152
  if (!ownsOperation(pending))
13079
13153
  return sealLostOperation(callDigest, pending);
13080
- if (!afterSnapshot) {
13154
+ if (capture.status === "unavailable") {
13081
13155
  sealOperation(callDigest, pending, true);
13082
- return { status: "unavailable" };
13156
+ return capture;
13083
13157
  }
13084
- const observation = buildOperationObservation(host, pending, afterSnapshot, guard.status(), parsedOutput);
13158
+ const afterSnapshot = capture.snapshot;
13159
+ const observation = buildOperationObservation(host, pending, afterSnapshot, requireGuard().status(), parsedOutput);
13085
13160
  if (!observation) {
13086
13161
  sealOperation(callDigest, pending, true);
13087
13162
  return { status: "ignored" };
@@ -13091,26 +13166,27 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13091
13166
  return sealLostOperation(callDigest, pending);
13092
13167
  if (classification.status !== "ready") {
13093
13168
  sealOperation(callDigest, pending, true);
13094
- return { status: classification.status };
13169
+ return classification;
13095
13170
  }
13096
13171
  let finalObservation = classification.observation;
13097
13172
  if (remoteOperation(finalObservation.operation)) {
13098
- const remoteAfter = await captureRemoteAfter(finalObservation.operation, pending.observer);
13173
+ const remoteCapture = await captureRemoteAfter(finalObservation.operation, pending.observer);
13099
13174
  if (!ownsOperation(pending)) {
13100
13175
  return sealLostOperation(callDigest, pending);
13101
13176
  }
13102
- if (!remoteAfter) {
13177
+ if (remoteCapture.status === "unavailable") {
13103
13178
  sealOperation(callDigest, pending, true);
13104
- return { status: "unavailable" };
13179
+ return remoteCapture;
13105
13180
  }
13106
- const rebuilt = buildOperationObservation(host, pending, afterSnapshot, guard.status(), parsedOutput, finalObservation.operation, remoteAfter);
13181
+ const remoteAfter = remoteCapture.snapshot;
13182
+ const rebuilt = buildOperationObservation(host, pending, afterSnapshot, requireGuard().status(), parsedOutput, finalObservation.operation, remoteAfter);
13107
13183
  if (!rebuilt) {
13108
13184
  sealOperation(callDigest, pending, true);
13109
13185
  return { status: "ignored" };
13110
13186
  }
13111
13187
  finalObservation = rebuilt;
13112
13188
  if (finalObservation.operation === "pr-creation" && pending.remoteBefore?.status === "missing-resource") {
13113
- const seeded = guard.observeReadback({
13189
+ const seeded = requireGuard().observeReadback({
13114
13190
  operation: "pr-creation",
13115
13191
  workspaceIdentity: options.workspaceIdentity,
13116
13192
  repositoryIdentity: afterSnapshot.repositoryRevisionDigest,
@@ -13120,7 +13196,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13120
13196
  });
13121
13197
  if (seeded.status === "rejected") {
13122
13198
  sealOperation(callDigest, pending, true);
13123
- return { status: "unavailable" };
13199
+ return afterFailure("operation-observation-rejected", seeded.reasonCode);
13124
13200
  }
13125
13201
  }
13126
13202
  }
@@ -13129,11 +13205,11 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13129
13205
  const observed = await observeClassifiedOperation(finalObservation);
13130
13206
  if (!observed) {
13131
13207
  sealOperation(callDigest, pending, true);
13132
- return { status: "unavailable" };
13208
+ return afterFailure("operation-observation-rejected");
13133
13209
  }
13134
13210
  sealOperation(callDigest, pending, observed.status !== "accepted");
13135
13211
  if (observed.status === "unavailable") {
13136
- return { status: "unavailable" };
13212
+ return afterFailure("operation-observation-rejected", observed.reasonCode);
13137
13213
  }
13138
13214
  if (observed.status !== "accepted")
13139
13215
  return { status: "ignored" };
@@ -13181,7 +13257,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13181
13257
  return;
13182
13258
  const result = await completeOperationObservation(callDigest, host, pending, output);
13183
13259
  if (result.status === "unavailable") {
13184
- markUnavailable();
13260
+ markUnavailable(result.cause);
13185
13261
  return;
13186
13262
  }
13187
13263
  if (result.status !== "accepted")
@@ -13216,7 +13292,7 @@ function createSessionRuntime(options, operationObservers, recoveredOperationCon
13216
13292
  await finishOperation(host, output);
13217
13293
  }
13218
13294
  } catch {
13219
- markUnavailable();
13295
+ markUnavailable(hookCause("tool-after", "hook-threw"));
13220
13296
  }
13221
13297
  }
13222
13298
  function isInternalSystem(system) {
@@ -13268,19 +13344,19 @@ ${marker}` : marker;
13268
13344
  },
13269
13345
  status: () => {
13270
13346
  ensureSynchronousRuntime();
13271
- return initialized ? guard.status() : unavailableWorkflowStatus();
13347
+ return initialized ? requireGuard().status() : unavailableWorkflowStatus();
13272
13348
  },
13273
13349
  readStatus,
13274
- unavailableCause: () => firstUnavailableCause,
13350
+ unavailableCause: () => firstUnavailableCause ?? pendingUnavailableCause,
13275
13351
  seedDeferrals: currentSeedDeferrals,
13276
13352
  refreshReadback,
13277
13353
  observeReceipt: (input) => {
13278
13354
  ensureSynchronousRuntime();
13279
- return initialized ? guard.observeReceipt(input) : { status: "unavailable", reasonCode: "guard-unavailable" };
13355
+ return initialized ? requireGuard().observeReceipt(input) : { status: "unavailable", reasonCode: "guard-unavailable" };
13280
13356
  },
13281
13357
  prepareTransition: (input) => {
13282
13358
  ensureSynchronousRuntime();
13283
- return initialized ? guard.prepareTransition(input) : { status: "unavailable", reasonCode: "guard-unavailable" };
13359
+ return initialized ? requireGuard().prepareTransition(input) : { status: "unavailable", reasonCode: "guard-unavailable" };
13284
13360
  },
13285
13361
  startUnit: (input, policy) => {
13286
13362
  ensureSynchronousRuntime();
@@ -13342,13 +13418,13 @@ function makeWorkflowTool(description, args2, getRuntime, execute) {
13342
13418
  metadata: runtime.metadata()
13343
13419
  });
13344
13420
  } catch {
13345
- runtime.markUnavailable();
13421
+ runtime.markUnavailable(hookCause("tool-execute", "hook-threw"));
13346
13422
  }
13347
13423
  }
13348
13424
  try {
13349
13425
  metadata2 = runtime.metadata();
13350
13426
  } catch {
13351
- runtime.markUnavailable();
13427
+ runtime.markUnavailable(hookCause("tool-execute", "hook-threw"));
13352
13428
  metadata2 = {
13353
13429
  protocolVersion: MARKER_PROTOCOL_VERSION,
13354
13430
  state: "unavailable",
@@ -13414,25 +13490,13 @@ function createOpencodeWorkflowGuard(options) {
13414
13490
  const host = parseHostBefore(input);
13415
13491
  if (!host)
13416
13492
  return;
13417
- const runtime = sessionRuntimeFor(host.sessionID);
13418
- try {
13419
- await runtime.hooks["tool.execute.before"](input, output);
13420
- } catch (error) {
13421
- if (isWorkflowGuardBlockedError(error))
13422
- throw error;
13423
- runtime.markUnavailable();
13424
- }
13493
+ await sessionRuntimeFor(host.sessionID).hooks["tool.execute.before"](input, output);
13425
13494
  }
13426
13495
  async function after(input, output) {
13427
13496
  const host = parseHostAfter(input);
13428
13497
  if (!host)
13429
13498
  return;
13430
- const runtime = sessionRuntimeFor(host.sessionID);
13431
- try {
13432
- await runtime.hooks["tool.execute.after"](input, output);
13433
- } catch {
13434
- runtime.markUnavailable();
13435
- }
13499
+ await sessionRuntimeFor(host.sessionID).hooks["tool.execute.after"](input, output);
13436
13500
  }
13437
13501
  async function event(input) {
13438
13502
  if (!isRecord7(input) || !isRecord7(input.event))
@@ -13445,7 +13509,7 @@ function createOpencodeWorkflowGuard(options) {
13445
13509
  try {
13446
13510
  await runtime.hooks.event(input);
13447
13511
  } catch {
13448
- runtime.markUnavailable();
13512
+ runtime.markUnavailable(hookCause("event", "hook-threw"));
13449
13513
  }
13450
13514
  }
13451
13515
  async function transform(input, output) {
@@ -13455,7 +13519,7 @@ function createOpencodeWorkflowGuard(options) {
13455
13519
  try {
13456
13520
  await runtime.hooks["experimental.chat.system.transform"](input, output);
13457
13521
  } catch {
13458
- runtime.markUnavailable();
13522
+ runtime.markUnavailable(hookCause("transform", "hook-threw"));
13459
13523
  }
13460
13524
  }
13461
13525
  return {