@opengeni/worker-bundle 0.24.2-canary.0 → 0.26.0-canary.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.
Files changed (83) hide show
  1. package/dist/activities/agent-turn/agent-build.d.ts +3 -10
  2. package/dist/activities/agent-turn/claim.d.ts +7 -0
  3. package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
  4. package/dist/activities/agent-turn/errors.d.ts +6 -5
  5. package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
  6. package/dist/activities/agent-turn/history-prefix.d.ts +8 -0
  7. package/dist/activities/agent-turn/history-sink.d.ts +6 -2
  8. package/dist/activities/agent-turn/quiescence.d.ts +13 -8
  9. package/dist/activities/agent-turn/tool-environment.d.ts +2 -13
  10. package/dist/activities/agent-turn/tool-policy.d.ts +4 -0
  11. package/dist/activities/agent-turn/turn-context.d.ts +9 -1
  12. package/dist/activities/codemode-dispatcher.d.ts +10 -1
  13. package/dist/activities/codex-capacity.d.ts +32 -3
  14. package/dist/activities/codex-fleet-shadow.d.ts +1 -1
  15. package/dist/activities/codex-image-generation.d.ts +1 -1
  16. package/dist/activities/codex-rotation.d.ts +8 -67
  17. package/dist/activities/goals.d.ts +1 -1
  18. package/dist/activities/google-drive-publication.d.ts +2 -13
  19. package/dist/activities/image-generation-operation.d.ts +13 -4
  20. package/dist/activities/sandbox-lease.d.ts +7 -2
  21. package/dist/activities/session-state.d.ts +4 -2
  22. package/dist/activities/types.d.ts +11 -1
  23. package/dist/activities/xai-quota.d.ts +3 -0
  24. package/dist/{activities-control-ZWH7J4KZ.js → activities-control-KS3ZPQVY.js} +233 -88
  25. package/dist/activities-control-KS3ZPQVY.js.map +1 -0
  26. package/dist/activities-control.d.ts +2 -0
  27. package/dist/{activities-turn-I4OPLQLS.js → activities-turn-GHSFZB64.js} +1109 -1865
  28. package/dist/activities-turn-GHSFZB64.js.map +1 -0
  29. package/dist/activities.d.ts +2 -0
  30. package/dist/{chunk-65JEWP2Q.js → chunk-D4ZEFOYH.js} +1 -1
  31. package/dist/chunk-D4ZEFOYH.js.map +1 -0
  32. package/dist/{chunk-RQNH6CGR.js → chunk-O2LQWJIK.js} +3250 -2061
  33. package/dist/chunk-O2LQWJIK.js.map +1 -0
  34. package/dist/github-rest-mcp.d.ts +0 -2
  35. package/dist/index.d.ts +1 -0
  36. package/dist/index.js +2 -2
  37. package/dist/index.js.map +1 -1
  38. package/dist/observability-metrics.d.ts +1 -1
  39. package/dist/sandbox-routing.d.ts +6 -0
  40. package/dist/workflow-bundle.js +94 -9
  41. package/dist/workflows/activities.d.ts +1 -1
  42. package/dist/workflows/session.d.ts +3 -0
  43. package/package.json +17 -17
  44. package/src/activities/agent-turn/agent-build.ts +21 -26
  45. package/src/activities/agent-turn/claim.ts +26 -1
  46. package/src/activities/agent-turn/codex-capacity.ts +311 -234
  47. package/src/activities/agent-turn/credential-leases.ts +36 -0
  48. package/src/activities/agent-turn/errors.ts +73 -12
  49. package/src/activities/agent-turn/failure-settlement.ts +472 -331
  50. package/src/activities/agent-turn/history-prefix.ts +51 -0
  51. package/src/activities/agent-turn/history-sink.ts +30 -6
  52. package/src/activities/agent-turn/quiescence.ts +29 -17
  53. package/src/activities/agent-turn/run.ts +22 -5
  54. package/src/activities/agent-turn/stream-attempt.ts +35 -8
  55. package/src/activities/agent-turn/tool-environment.ts +42 -32
  56. package/src/activities/agent-turn/tool-policy.ts +14 -8
  57. package/src/activities/agent-turn/turn-context.ts +13 -0
  58. package/src/activities/agent-turn/xai-capacity.ts +11 -0
  59. package/src/activities/codemode-dispatcher.ts +235 -59
  60. package/src/activities/codex-capacity.ts +107 -10
  61. package/src/activities/codex-fleet-shadow.ts +8 -1
  62. package/src/activities/codex-image-generation.ts +19 -2
  63. package/src/activities/codex-rotation.ts +45 -142
  64. package/src/activities/goals.ts +9 -9
  65. package/src/activities/google-drive-publication.ts +4 -81
  66. package/src/activities/image-generation-operation.ts +28 -3
  67. package/src/activities/run-input.ts +6 -7
  68. package/src/activities/sandbox-lease.ts +246 -87
  69. package/src/activities/session-state.ts +20 -1
  70. package/src/activities/types.ts +16 -8
  71. package/src/activities/xai-quota.ts +35 -0
  72. package/src/activities.ts +1 -0
  73. package/src/github-rest-mcp.ts +0 -82
  74. package/src/observability-metrics.ts +4 -0
  75. package/src/sandbox-routing.ts +186 -16
  76. package/src/workflows/activities.ts +1 -0
  77. package/src/workflows/session.ts +114 -8
  78. package/dist/activities/api-integrations.d.ts +0 -21
  79. package/dist/activities-control-ZWH7J4KZ.js.map +0 -1
  80. package/dist/activities-turn-I4OPLQLS.js.map +0 -1
  81. package/dist/chunk-65JEWP2Q.js.map +0 -1
  82. package/dist/chunk-RQNH6CGR.js.map +0 -1
  83. package/src/activities/api-integrations.ts +0 -151
@@ -113,9 +113,17 @@ export type RotationDecision =
113
113
  // No connected accounts at all (preserves today's relogin-fail path).
114
114
  | { kind: "none" };
115
115
 
116
+ export type CodexTurnLeaseDecision =
117
+ | RotationDecision
118
+ // The user's manual pin or rotation-off active pointer names a connected row
119
+ // that is disabled for NEW allocations. This is policy-constrained capacity,
120
+ // not a disconnected-account relogin failure: the accepted turn must wait
121
+ // until the row is re-enabled or the governing policy changes.
122
+ | { kind: "allocatorDisabled"; credentialId: string };
123
+
116
124
  export type CodexTurnLeaseSelection = {
117
125
  credentialId: string | null;
118
- decision: RotationDecision;
126
+ decision: CodexTurnLeaseDecision;
119
127
  /** Policy/manual homes never move the workspace-global legacy pointer. */
120
128
  advanceActivePointer?: boolean;
121
129
  };
@@ -391,74 +399,6 @@ export function computeIdleDelayMs(earliestResetAt: Date, now: Date, maxMs: numb
391
399
  return Math.min(Math.max(delta, MIN_IDLE_MS), maxMs);
392
400
  }
393
401
 
394
- // --- P3 reactive-rotation boundedness (Finding 1). The reactive 429 catch, on a
395
- // LIVE failover candidate, normally returns continueDelayMs:0 (skip the hold and
396
- // re-dispatch NOW — the just-served account is cooling so the next rank cannot
397
- // re-pick it). Two independent second-order faults can break that "cannot
398
- // re-pick it" premise and turn the 0-delay re-dispatch into a hot loop, so this
399
- // pure decision applies two backstops before allowing the 0.
400
-
401
- /**
402
- * Slow-retry floor (Finding 1a) for the double-fault where the just-served
403
- * account's cooldown write could NOT be confirmed persisted: the next proactive
404
- * rank might re-select the same capped account (stale-low cached usedPercent, not
405
- * cooling). A positive floor degrades that to a SLOW retry instead of a
406
- * model-paced hot loop. A few seconds — long enough to break the loop, short
407
- * enough to stay responsive once the transient write fault clears.
408
- */
409
- export const REACTIVE_PERSISTENCE_FAULT_FLOOR_MS = 5_000; // 5s
410
- /**
411
- * Circuit-breaker slack (Finding 1b) added to the connected-account count to bound
412
- * consecutive reactive failovers. Each legitimate failover cools one account, so
413
- * after at most N failovers every account is capped and the reactive path takes the
414
- * all-capped idle instead; the +margin absorbs benign capacity re-ranks without
415
- * tripping the breaker in normal use.
416
- */
417
- export const REACTIVE_ROTATION_MARGIN = 2;
418
- /**
419
- * The FIXED positive idle the reactive path falls to once consecutive reactive
420
- * failovers exceed the bound (a double-fault where cooldown writes are not sticking
421
- * AND the 429s carry no usage headers, so the same account keeps getting re-picked).
422
- * Mirrors the all-capped floor — a mandatory, bounded hold, never another 0-delay
423
- * re-dispatch — so the loop can never hammer the capped backend + DB (invariant 4).
424
- */
425
- export const REACTIVE_CIRCUIT_BREAKER_IDLE_MS = MIN_IDLE_MS; // 60s
426
-
427
- /** The reactive-resume shape returned to the goal-continuation caller. */
428
- export type ReactiveRotationResume = { continueDelayMs: number; idleUntilReset: boolean };
429
-
430
- /**
431
- * Decide the continueDelayMs for a reactive 429 failover onto a LIVE candidate,
432
- * bounding the (otherwise 0-delay) re-dispatch against two second-order faults:
433
- *
434
- * 1. Circuit breaker (1b): if the consecutive reactive-failover streak (prior
435
- * rotated 429 failovers since the last successful turn, PLUS this one) exceeds
436
- * `connectedAccountCount + REACTIVE_ROTATION_MARGIN`, fall to a FIXED positive,
437
- * MANDATORY idle. Covers the double-fault (cooldown write not persisted + a
438
- * header-less cap 429) where the same capped account is re-picked every turn.
439
- * 2. Persistence-fault floor (1a): if the just-served account's cooldown could NOT
440
- * be confirmed persisted, use a positive floor (slow retry) instead of 0.
441
- *
442
- * Otherwise (a confirmed cooldown + an in-bounds streak) returns the unchanged
443
- * 0-delay fast re-dispatch — the happy single-rotation path is byte-identical.
444
- * Pure so the boundedness contract is unit-testable without a worker/db env.
445
- */
446
- export function computeReactiveRotationResume(args: {
447
- cooldownPersisted: boolean;
448
- priorConsecutiveRotations: number; // reactive rotated failovers since last success (excludes this one)
449
- connectedAccountCount: number;
450
- }): ReactiveRotationResume {
451
- const streak = args.priorConsecutiveRotations + 1; // include the failover about to be published
452
- const bound = args.connectedAccountCount + REACTIVE_ROTATION_MARGIN;
453
- if (streak > bound) {
454
- return { continueDelayMs: REACTIVE_CIRCUIT_BREAKER_IDLE_MS, idleUntilReset: true };
455
- }
456
- if (!args.cooldownPersisted) {
457
- return { continueDelayMs: REACTIVE_PERSISTENCE_FAULT_FLOOR_MS, idleUntilReset: false };
458
- }
459
- return { continueDelayMs: 0, idleUntilReset: false };
460
- }
461
-
462
402
  /**
463
403
  * THE pure rotation ranker. `accounts` arrives in stable created_at order
464
404
  * (listCodexAccountStatuses), which deterministically breaks ranking ties.
@@ -567,69 +507,37 @@ export function chooseRotationActive(args: {
567
507
  return decide(chosen);
568
508
  }
569
509
 
570
- /**
571
- * Exact pre-0053 selector used while the lease cutover is off.
572
- *
573
- * Old workers keep a healthy `most_remaining` active pointer sticky and know
574
- * nothing about lease counts or fairness cursors. A compatible worker must do
575
- * the same until BOTH the deployment flag and workspace cutover are enabled;
576
- * otherwise a rolling fleet would run two allocators at once. When the active
577
- * pointer is no longer eligible, zeroing the additive metadata delegates to the
578
- * same capacity ranking that the old selector used.
579
- */
580
- export function chooseLegacyRotationActive(
581
- args: Parameters<typeof chooseRotationActive>[0],
582
- ): RotationDecision {
583
- if (args.rotationStrategy === "most_remaining") {
584
- const active = args.activeCredentialId
585
- ? args.accounts.find((account) => account.id === args.activeCredentialId)
586
- : undefined;
587
- if (active && isCodexCredentialEligible(active, args.now)) {
588
- return { kind: "active", credentialId: active.id, moved: false };
589
- }
590
- }
591
- return chooseRotationActive({
592
- ...args,
593
- accounts: args.accounts.map((account) => ({
594
- ...account,
595
- activeLeaseCount: 0,
596
- selectionCount: 0,
597
- lastSelectedAt: null,
598
- })),
599
- });
600
- }
601
-
602
510
  /**
603
511
  * Full pure policy at the transaction boundary. A live same-turn lease is
604
- * idempotent; the rollout-off/manual path preserves pin>pointer behavior; a
512
+ * idempotent; manual and rotation-off policy preserve pin>pointer behavior; a
605
513
  * healthy pin wins only while eligible, so quota/auth quarantine cannot trap a
606
514
  * session on one subscription.
607
515
  */
608
- export function selectCodexCredentialLeaseForTurn(args: {
609
- context: CodexCredentialLeaseSelectionContext;
610
- leasingEnabled: boolean;
516
+ export function selectCodexCredentialLeaseForTurn<
517
+ TPolicyScope = never,
518
+ TUnavailableDiagnostic = never,
519
+ >(args: {
520
+ context: CodexCredentialLeaseSelectionContext<TPolicyScope, TUnavailableDiagnostic>;
611
521
  sessionId: string;
612
522
  sessionPinnedCredentialId: string | null;
613
523
  sessionPinSource: CodexPinSource | null;
614
524
  sessionLastCredentialId: string | null;
615
525
  now: Date;
616
526
  }): CodexTurnLeaseSelection {
617
- const {
618
- accounts,
619
- activeCredentialId,
620
- rotationEnabled,
621
- leaseRotationEnabled,
622
- rotationStrategy,
623
- existingCredentialId,
624
- } = args.context;
625
- const leasingEnabled = args.leasingEnabled && leaseRotationEnabled;
527
+ const { accounts, activeCredentialId, rotationEnabled, rotationStrategy, existingCredentialId } =
528
+ args.context;
529
+ const failedCredentialIds = new Set(args.context.failedCredentialIds ?? []);
626
530
  // Normalized: rotation-enabled always behaves as sharded (see
627
531
  // effectiveRotationStrategy); the stored value is only ever legacy residue.
628
532
  const strategy = effectiveRotationStrategy(rotationStrategy);
629
533
  const existing = existingCredentialId
630
534
  ? accounts.find((account) => account.id === existingCredentialId)
631
535
  : undefined;
632
- if (existing && isCodexCredentialHealthy(existing, args.now)) {
536
+ if (
537
+ existing &&
538
+ !failedCredentialIds.has(existing.id) &&
539
+ isCodexCredentialHealthy(existing, args.now)
540
+ ) {
633
541
  return {
634
542
  credentialId: existing.id,
635
543
  advanceActivePointer: false,
@@ -641,10 +549,6 @@ export function selectCodexCredentialLeaseForTurn(args: {
641
549
  };
642
550
  }
643
551
 
644
- const connectedIds = new Set(
645
- accounts.filter((account) => account.allocatorEnabled).map((account) => account.id),
646
- );
647
-
648
552
  const pinDisposition = classifyCodexPin({
649
553
  pinnedCredentialId: args.sessionPinnedCredentialId,
650
554
  pinSource: args.sessionPinSource,
@@ -657,9 +561,16 @@ export function selectCodexCredentialLeaseForTurn(args: {
657
561
  // same-turn lease already returned above before this admission filter.
658
562
  if (pinDisposition === "manual" && args.sessionPinnedCredentialId) {
659
563
  const pinned = accounts.find((account) => account.id === args.sessionPinnedCredentialId);
660
- if (!pinned?.allocatorEnabled) {
564
+ if (!pinned) {
661
565
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
662
566
  }
567
+ if (!pinned.allocatorEnabled) {
568
+ return {
569
+ credentialId: null,
570
+ decision: { kind: "allocatorDisabled", credentialId: pinned.id },
571
+ advanceActivePointer: false,
572
+ };
573
+ }
663
574
  if (isCodexCredentialHealthy(pinned, args.now)) {
664
575
  return {
665
576
  credentialId: pinned.id,
@@ -682,13 +593,14 @@ export function selectCodexCredentialLeaseForTurn(args: {
682
593
  // primary/fallback pool before this ranker runs; accounts from different pools
683
594
  // are never union-ranked here.
684
595
  if (pinDisposition === "sharded") {
685
- if (accounts.length === 0) {
596
+ const unattemptedAccounts = accounts.filter((account) => !failedCredentialIds.has(account.id));
597
+ if (unattemptedAccounts.length === 0) {
686
598
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
687
599
  }
688
600
  const shard = chooseShardedHome({
689
601
  sessionId: args.sessionId,
690
602
  currentPolicyPin: args.sessionPinSource === "policy" ? args.sessionPinnedCredentialId : null,
691
- accounts,
603
+ accounts: unattemptedAccounts,
692
604
  now: args.now,
693
605
  });
694
606
  if (shard.kind === "allCapped") {
@@ -718,9 +630,16 @@ export function selectCodexCredentialLeaseForTurn(args: {
718
630
  const active = activeCredentialId
719
631
  ? accounts.find((account) => account.id === activeCredentialId)
720
632
  : undefined;
721
- if (!active?.allocatorEnabled) {
633
+ if (!active) {
722
634
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
723
635
  }
636
+ if (!active.allocatorEnabled) {
637
+ return {
638
+ credentialId: null,
639
+ decision: { kind: "allocatorDisabled", credentialId: active.id },
640
+ advanceActivePointer: false,
641
+ };
642
+ }
724
643
  if (!isCodexCredentialHealthy(active, args.now)) {
725
644
  return {
726
645
  credentialId: null,
@@ -735,31 +654,15 @@ export function selectCodexCredentialLeaseForTurn(args: {
735
654
  };
736
655
  }
737
656
 
738
- // Before the workspace cutover bit is enabled (or after the deployment kill
739
- // switch is turned off), preserve the exact legacy policy used by old workers:
740
- // a pin stays sticky; otherwise an enabled workspace runs the pure legacy
741
- // rotation ranker without touching lease/cursor state.
742
- if (!leasingEnabled && pinDisposition !== "clearStale" && args.sessionPinnedCredentialId) {
743
- const credentialId = connectedIds.has(args.sessionPinnedCredentialId)
744
- ? args.sessionPinnedCredentialId
745
- : activeCredentialId && connectedIds.has(activeCredentialId)
746
- ? activeCredentialId
747
- : null;
748
- return {
749
- credentialId,
750
- decision: credentialId ? { kind: "active", credentialId, moved: false } : { kind: "none" },
751
- };
752
- }
753
-
754
657
  const priorId = args.sessionLastCredentialId ?? activeCredentialId;
755
- const choose = leasingEnabled ? chooseRotationActive : chooseLegacyRotationActive;
756
- const decision = choose({
658
+ const unattemptedAccounts = accounts.filter((account) => !failedCredentialIds.has(account.id));
659
+ const decision = chooseRotationActive({
757
660
  rotationStrategy: strategy,
758
661
  activeCredentialId,
759
662
  // Per-session continuity is the round-robin/drain cursor. Falling back to
760
663
  // the workspace pointer is only correct when this session has never run.
761
664
  priorCredentialId: priorId,
762
- accounts,
665
+ accounts: unattemptedAccounts,
763
666
  now: args.now,
764
667
  });
765
668
  return {
@@ -174,7 +174,7 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
174
174
  tools: withFirstPartyTools(settings, session.tools),
175
175
  sandboxBackend: session.sandboxBackend,
176
176
  },
177
- // The hold guidance is only given when `goal_wait` is actually in this
177
+ // Long-wait guidance is only given when `wait_for_input` is actually in this
178
178
  // session's effective first-party selection (the same source the worker
179
179
  // signs into the delegated token and the API uses to register tools), so
180
180
  // a pre-existing narrowed selection is never told to call a missing tool.
@@ -184,7 +184,7 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
184
184
  session.firstPartyMcpTools,
185
185
  );
186
186
  return goalContinuationPrompt(goal, autoContinuation, cap, {
187
- goalWaitAvailable: effectiveFirstPartyTools.includes("goal_wait"),
187
+ inputWaitAvailable: effectiveFirstPartyTools.includes("wait_for_input"),
188
188
  humanInputRespondAvailable: effectiveFirstPartyTools.includes(
189
189
  "session_human_input_respond",
190
190
  ),
@@ -254,15 +254,15 @@ export function goalContinuationPrompt(
254
254
  _goal: SessionGoal,
255
255
  _autoContinuation: number,
256
256
  _cap: number | null,
257
- options: { goalWaitAvailable?: boolean; humanInputRespondAvailable?: boolean } = {},
257
+ options: { inputWaitAvailable?: boolean; humanInputRespondAvailable?: boolean } = {},
258
258
  ): string {
259
- const waitingGuidance = options.goalWaitAvailable
259
+ const waitingGuidance = options.inputWaitAvailable
260
260
  ? [
261
- "Waiting on child sessions or external events:",
262
- "- When the next progress depends on child sessions you spawned or on an external event, do not sleep, loop, or poll sessions_list/session_get/session_events to wait for it.",
263
- "- Re-check sessions_list or session_get once; if the work is still in flight, call opengeni__goal_wait with a concrete reason and a deadline (untilSeconds), then end your turn immediately. You will be woken by a child result, a message, a human prompt, or at the deadline, and this goal stays active.",
264
- "- If the immediately preceding user-facing update already reported this same unchanged wait, do not restate it or produce another equivalent final answer. Call opengeni__goal_wait and end the turn. Report only material new state or a newly discovered blocker.",
265
- "- A hold is for child/external progress only. If you are blocked on a human decision, use opengeni__goal_pause under the blocked audit below instead.",
261
+ "Waiting on child sessions, background commands, or external events:",
262
+ "- When further progress depends on work already in flight, do not sleep, loop, or poll session or command state repeatedly.",
263
+ "- Re-check once; if the work is still in flight and the wait is long or uncertain, call opengeni__wait_for_input with a concrete reason and timeoutSeconds, then end your turn immediately. Relevant session input or the safety deadline will start a new turn, and this goal stays active.",
264
+ "- If the immediately preceding user-facing update already reported this same unchanged wait, do not restate it or produce another equivalent final answer. Call opengeni__wait_for_input and end the turn. Report only material new state or a newly discovered blocker.",
265
+ "- If you are blocked on a human decision, use opengeni__goal_pause under the blocked audit below instead.",
266
266
  "",
267
267
  ]
268
268
  : [];
@@ -1,5 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
- import { AttemptToolApprovalRequiredError, type AttemptToolDefinition } from "@opengeni/codemode";
2
+ import type { AttemptToolDefinition } from "@opengeni/codemode";
3
3
  import type { McpPersonalConnectionDelegation } from "@opengeni/contracts";
4
4
  import {
5
5
  GOOGLE_DRIVE_FILE_SCOPE,
@@ -17,10 +17,7 @@ import {
17
17
  type GoogleDrivePublicationToolInput as GoogleDrivePublicationToolInputValue,
18
18
  } from "@opengeni/contracts/google-drive";
19
19
  import {
20
- beginConnectorActionExecution,
21
- completeConnectorActionExecution,
22
20
  getConnectionMetadata,
23
- prepareConnectorActionApproval,
24
21
  requireFile,
25
22
  namedSubjectHasLiveWorkspaceAuthority,
26
23
  type ConnectorActionAttemptIdentity,
@@ -34,6 +31,7 @@ import {
34
31
  type PersistedEditableArtifactMaterializationJob,
35
32
  } from "@opengeni/db/editable-artifact-durable-export";
36
33
  import { readResponseJsonBounded, undiciFetch, type FetchLike } from "@opengeni/network";
34
+ import { ConnectorActionExecutionError } from "@opengeni/runtime";
37
35
  import { retryWhileMissing, type ObjectStorage } from "@opengeni/storage";
38
36
 
39
37
  const DRIVE_API = "https://www.googleapis.com/drive/v3";
@@ -66,9 +64,6 @@ export type GoogleDrivePublicationPorts = {
66
64
  getMembership: typeof namedSubjectHasLiveWorkspaceAuthority;
67
65
  readMaterialization: typeof readAuthorizedEditableArtifactMaterialization;
68
66
  requireFile: typeof requireFile;
69
- prepare: typeof prepareConnectorActionApproval;
70
- begin: typeof beginConnectorActionExecution;
71
- complete: typeof completeConnectorActionExecution;
72
67
  fetch: FetchLike;
73
68
  };
74
69
 
@@ -77,9 +72,6 @@ const defaultPorts: GoogleDrivePublicationPorts = {
77
72
  getMembership: namedSubjectHasLiveWorkspaceAuthority,
78
73
  readMaterialization: readAuthorizedEditableArtifactMaterialization,
79
74
  requireFile,
80
- prepare: prepareConnectorActionApproval,
81
- begin: beginConnectorActionExecution,
82
- complete: completeConnectorActionExecution,
83
75
  fetch: undiciFetch,
84
76
  };
85
77
 
@@ -303,35 +295,6 @@ export function createGoogleDrivePublicationAttemptTool(input: {
303
295
  approval: "policy",
304
296
  execute: async (raw, context) => {
305
297
  const request = GoogleDrivePublicationToolInput.parse(raw);
306
- const connectorCall = googleDrivePublicationConnectorCall(
307
- input.target,
308
- request,
309
- context.operationId,
310
- );
311
- // Every caller is behind exactly one durable execute-once fence. Model
312
- // callers arrive through the attempt connector-action wrapper, which has
313
- // already registered this call under its durable SDK call id and moved
314
- // the approved row to executing - a second inner begin here would mint a
315
- // second row under an unrelated operation id and deadlock the default
316
- // ask policy. Codemode callers bypass that wrapper, so the tool
317
- // registers the fence itself with the shared Codemode operation id.
318
- let requestId: string | null = null;
319
- if (context.caller.kind === "codemode") {
320
- const admission = await ports.begin(input.db, input.identity, connectorCall);
321
- if (!admission.allowed) {
322
- throw new Error(
323
- admission.reason === "uncertain_retry" || admission.reason === "already_executed"
324
- ? "Google Drive publication outcome is unknown: a previous attempt may have already sent this publication to Google Drive. Verify in Drive before retrying with a new idempotency key."
325
- : admission.reason === "not_executed"
326
- ? "Google Drive publication was not executed: the previous attempt failed before any request reached Google Drive. Retrying with a new call is safe."
327
- : `Google Drive publication was not executed: ${admission.reason}. No request reached Google Drive.`,
328
- );
329
- }
330
- if (!admission.managed) {
331
- throw new Error("Google Drive publication has no explicit connector action policy");
332
- }
333
- requestId = admission.requestId;
334
- }
335
298
  let providerRequestStarted = false;
336
299
  try {
337
300
  const receipt = await executeGoogleDrivePublication(
@@ -350,34 +313,17 @@ export function createGoogleDrivePublicationAttemptTool(input: {
350
313
  },
351
314
  ports,
352
315
  );
353
- if (requestId) {
354
- await ports.complete(input.db, {
355
- accountId: input.identity.accountId,
356
- workspaceId: input.identity.workspaceId,
357
- requestId,
358
- attemptId: input.identity.attemptId,
359
- outcome: "completed",
360
- });
361
- }
362
316
  return {
363
317
  content: [{ type: "text", text: JSON.stringify(receipt) }],
364
318
  structuredContent: receipt,
365
319
  };
366
320
  } catch (error) {
367
- if (requestId) {
368
- await ports.complete(input.db, {
369
- accountId: input.identity.accountId,
370
- workspaceId: input.identity.workspaceId,
371
- requestId,
372
- attemptId: input.identity.attemptId,
373
- outcome: providerRequestStarted ? "uncertain" : "not_executed",
374
- });
375
- }
376
321
  const detail = error instanceof Error ? error.message : String(error);
377
- throw new Error(
322
+ throw new ConnectorActionExecutionError(
378
323
  providerRequestStarted
379
324
  ? `Google Drive publication outcome is unknown: a provider request may have been sent before the failure. Verify in Drive before retrying. Cause: ${detail}`
380
325
  : `Google Drive publication was not executed: no request reached Google Drive. Cause: ${detail}`,
326
+ providerRequestStarted ? "uncertain" : "not_executed",
381
327
  { cause: error },
382
328
  );
383
329
  }
@@ -385,29 +331,6 @@ export function createGoogleDrivePublicationAttemptTool(input: {
385
331
  };
386
332
  }
387
333
 
388
- export async function authorizeGoogleDrivePublicationAttempt(input: {
389
- db: Database;
390
- identity: ConnectorActionAttemptIdentity;
391
- target: GoogleDrivePublicationTarget;
392
- approvalId: string;
393
- arguments: unknown;
394
- ports?: Pick<GoogleDrivePublicationPorts, "prepare">;
395
- }): Promise<void> {
396
- const request = GoogleDrivePublicationToolInput.parse(input.arguments);
397
- const preparation = await (input.ports ?? defaultPorts).prepare(
398
- input.db,
399
- input.identity,
400
- googleDrivePublicationConnectorCall(input.target, request, input.approvalId),
401
- );
402
- if (!preparation.managed) {
403
- throw new Error("Google Drive publication has no explicit connector action policy");
404
- }
405
- if (preparation.decision === "ask") throw new AttemptToolApprovalRequiredError();
406
- if (preparation.decision === "block") {
407
- throw new Error("Google Drive publication is blocked by connector action policy");
408
- }
409
- }
410
-
411
334
  export async function executeGoogleDrivePublication(
412
335
  input: {
413
336
  db: Database;
@@ -5,6 +5,7 @@ import {
5
5
  markImageGenerationOperationOutcomeUnknown,
6
6
  markImageGenerationOperationRetentionFailed,
7
7
  prepareImageGenerationOperation,
8
+ resetImageGenerationOperationBeforeProviderDispatch,
8
9
  type Database,
9
10
  type ImageGenerationOperationStatus,
10
11
  } from "@opengeni/db";
@@ -45,6 +46,7 @@ export class ImageGenerationRetentionFailedError extends Error {
45
46
  export type ImageGenerationOperationPorts = {
46
47
  prepare: typeof prepareImageGenerationOperation;
47
48
  begin: typeof beginImageGenerationOperation;
49
+ resetBeforeProviderDispatch: typeof resetImageGenerationOperationBeforeProviderDispatch;
48
50
  retain: typeof retainGeneratedImage;
49
51
  complete: typeof completeImageGenerationOperation;
50
52
  markOutcomeUnknown: typeof markImageGenerationOperationOutcomeUnknown;
@@ -55,6 +57,7 @@ export type ImageGenerationOperationPorts = {
55
57
  const imageGenerationOperationPorts: ImageGenerationOperationPorts = {
56
58
  prepare: prepareImageGenerationOperation,
57
59
  begin: beginImageGenerationOperation,
60
+ resetBeforeProviderDispatch: resetImageGenerationOperationBeforeProviderDispatch,
58
61
  retain: retainGeneratedImage,
59
62
  complete: completeImageGenerationOperation,
60
63
  markOutcomeUnknown: markImageGenerationOperationOutcomeUnknown,
@@ -65,9 +68,10 @@ const imageGenerationOperationPorts: ImageGenerationOperationPorts = {
65
68
  /**
66
69
  * Durable admission fence shared by every client-executed image provider.
67
70
  * Provider execution is permitted at most once after the prepared ->
68
- * provider_started transition. A crash at that boundary may conservatively
69
- * leave the outcome unknown; a retry may recover a ready artifact, but never
70
- * blindly replays the paid operation.
71
+ * provider_started transition. A verified pre-dispatch fence rejection returns
72
+ * the row to `prepared`; a crash or any error after dispatch admission may
73
+ * conservatively leave the outcome unknown. A retry may recover a ready
74
+ * artifact, but never blindly replays an admitted paid operation.
71
75
  */
72
76
  export type ExecuteImageGenerationOperationInput = {
73
77
  db: Database;
@@ -83,6 +87,13 @@ export type ExecuteImageGenerationOperationInput = {
83
87
  modelId: string;
84
88
  prompt: string;
85
89
  referenceDigests?: readonly Readonly<{ mediaType: string; sha256: string }>[];
90
+ /**
91
+ * Provider-specific proof that a thrown error occurred before the paid
92
+ * provider boundary was crossed. Such a failure may return the operation to
93
+ * `prepared`; every other generation error remains outcome-ambiguous after
94
+ * `provider_started` and is never blindly replayed.
95
+ */
96
+ isProviderDispatchRejected?: (error: unknown) => boolean;
86
97
  generate: () => Promise<GeneratedImageOutput>;
87
98
  };
88
99
 
@@ -132,6 +143,8 @@ export async function executeImageGenerationOperation(
132
143
  workspaceId: input.workspaceId,
133
144
  operationId: identity.operationId,
134
145
  operationKey: identity.operationKey,
146
+ providerBindingHash: input.providerBindingHash,
147
+ expectedArtifactId: identity.artifactId,
135
148
  });
136
149
  if (!begun.started || begun.operation.status !== "provider_started") {
137
150
  const recovered = await ports.recover(input, {
@@ -147,6 +160,18 @@ export async function executeImageGenerationOperation(
147
160
  try {
148
161
  output = await input.generate();
149
162
  } catch (error) {
163
+ if (input.isProviderDispatchRejected?.(error) === true) {
164
+ await ports
165
+ .resetBeforeProviderDispatch(input.db, {
166
+ accountId: input.accountId,
167
+ workspaceId: input.workspaceId,
168
+ operationId: identity.operationId,
169
+ operationKey: identity.operationKey,
170
+ error: error instanceof Error ? error.message : String(error),
171
+ })
172
+ .catch(() => undefined);
173
+ throw error;
174
+ }
150
175
  await ports
151
176
  .markOutcomeUnknown(input.db, {
152
177
  accountId: input.accountId,
@@ -416,13 +416,12 @@ export async function turnInput(
416
416
  options.turnId,
417
417
  ),
418
418
  );
419
- if (updates.length > 0) {
420
- const historyItemIds = new Set(
421
- updates.map((update) => update.deliveredHistoryItemId).filter(Boolean),
422
- );
423
- if (historyItemIds.size !== 1 || updates.some((update) => !update.deliveredHistoryItemId)) {
424
- throw new Error("Delivered internal updates have no single durable model-memory batch");
425
- }
419
+ // A logical turn can receive another atomic batch when an interrupted
420
+ // attempt resumes. Every update must retain its durable receipt, but the
421
+ // turn-wide query legitimately spans multiple history items. Canonical
422
+ // history owns their ordering and exactly-once inclusion below.
423
+ if (updates.some((update) => !update.deliveredHistoryItemId)) {
424
+ throw new Error("Delivered internal update has no durable model-memory batch");
426
425
  }
427
426
  const internalContext = joinInternalContext(
428
427
  options.recovering