@opengeni/worker-bundle 6.7.0 → 6.8.0

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.
@@ -5,10 +5,10 @@ import {
5
5
  SandboxProvisionStageError,
6
6
  SandboxWarmingTimeoutError,
7
7
  sandboxProvisionFailureStage
8
- } from "./chunk-XTG32O4Q.js";
8
+ } from "./chunk-YJDTWP6V.js";
9
9
  import {
10
10
  observabilityEventBusOptions
11
- } from "./chunk-W2IHRF7L.js";
11
+ } from "./chunk-UQAMEFZB.js";
12
12
 
13
13
  // src/activity-services.ts
14
14
  import {
@@ -5710,4 +5710,4 @@ export {
5710
5710
  codexUsageLimitFailurePayload,
5711
5711
  classifyClaudeProviderError
5712
5712
  };
5713
- //# sourceMappingURL=chunk-Q4KXCK36.js.map
5713
+ //# sourceMappingURL=chunk-ZEGN4DC4.js.map
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ import {
27
27
  telemetryInterval,
28
28
  telemetryScheduler,
29
29
  turnTaskQueueMetricLabels
30
- } from "./chunk-W2IHRF7L.js";
30
+ } from "./chunk-UQAMEFZB.js";
31
31
  import "./chunk-PZ5AY32C.js";
32
32
 
33
33
  // src/index.ts
@@ -808,7 +808,7 @@ async function internalMaintenanceCadences(input) {
808
808
  assertSandboxReaperActivityTimeout(settings);
809
809
  const periodMs = settings.sandboxLeaseReaperPeriodMs;
810
810
  const lifecycleQueue = sandboxLifecycleTaskQueue(taskQueue);
811
- const sandbox = await import("./sandbox-lease-ILEZ6RVI.js");
811
+ const sandbox = await import("./sandbox-lease-SWS4WQWO.js");
812
812
  if (jobs.has("sandbox-lease-reaper")) {
813
813
  const { SANDBOX_CHECKPOINT_GC_CLAIM_TTL_MS } = sandbox;
814
814
  cadences.push({
@@ -1572,7 +1572,7 @@ async function createDefaultWorkerActivities(role, dependencies = {}) {
1572
1572
  prepareObjectStorageProvider(dependencies.settings ?? getSettings());
1573
1573
  }
1574
1574
  configureBillingLegacyAccounts((dependencies.settings ?? getSettings()).billingLegacyAccountIds);
1575
- return role === "control" ? (await import("./activities-control-LJVIL5FP.js")).createControlActivities(dependencies) : (await import("./activities-turn-GTZPMF6T.js")).createTurnActivities(dependencies);
1575
+ return role === "control" ? (await import("./activities-control-6HEEWPLL.js")).createControlActivities(dependencies) : (await import("./activities-turn-E6VP3XVJ.js")).createTurnActivities(dependencies);
1576
1576
  }
1577
1577
  function resolveOpenGeniWorkflowDefinition(moduleUrl = import.meta.url) {
1578
1578
  const modulePath = fileURLToPath(moduleUrl).replaceAll("\\", "/");
@@ -42,10 +42,10 @@ import {
42
42
  sandboxLeaseTelemetryKey,
43
43
  terminateProviderBox,
44
44
  verifyDockerDrainCaptureFence
45
- } from "./chunk-C5RYXGXF.js";
46
- import "./chunk-XTG32O4Q.js";
45
+ } from "./chunk-WLNVZWDL.js";
46
+ import "./chunk-YJDTWP6V.js";
47
47
  import "./chunk-BKPKTXFV.js";
48
- import "./chunk-W2IHRF7L.js";
48
+ import "./chunk-UQAMEFZB.js";
49
49
  import "./chunk-C67MOSPJ.js";
50
50
  import "./chunk-PZ5AY32C.js";
51
51
  export {
@@ -93,4 +93,4 @@ export {
93
93
  terminateProviderBox,
94
94
  verifyDockerDrainCaptureFence
95
95
  };
96
- //# sourceMappingURL=sandbox-lease-ILEZ6RVI.js.map
96
+ //# sourceMappingURL=sandbox-lease-SWS4WQWO.js.map
@@ -2,6 +2,7 @@ import { type Settings } from "@opengeni/config";
2
2
  export { agentBackgroundCommandDeps, captureConnectedCommandOutput } from "@opengeni/core";
3
3
  import { type Database, type EnrollmentRecord } from "@opengeni/db";
4
4
  import type { EventBus } from "@opengeni/events";
5
+ import type { Observability } from "@opengeni/observability";
5
6
  import { type EstablishedSandboxSession, type RoutingSandboxFirstOperationObserver, type RoutingSandboxOperationObserver, type RoutingSandboxCaptureWaitObserver, type SelfhostedOpObserver, type SelfhostedConnectionBinding, type SelfhostedRelayConfig, type OpStreamJournal } from "@opengeni/runtime";
6
7
  import type { WakeSessionWorkflowSignal } from "./activities/types.js";
7
8
  export type RoutingWiringServices = {
@@ -40,6 +41,9 @@ export type RoutingWiringServices = {
40
41
  /** Wakes a session waiting on a background command this routing settles.
41
42
  * Absent: the periodic wake dispatcher delivers the committed wake. */
42
43
  wakeSessionWorkflow?: WakeSessionWorkflowSignal | null | undefined;
44
+ /** Logs a refused Connected Machine authority check with the database's own
45
+ * reason. Absent: the refusal is still thrown unchanged, just not logged. */
46
+ observability?: Pick<Observability, "warn">;
43
47
  };
44
48
  /** Worker-turn routing may final-ack streamed command output, so it must always
45
49
  * carry the turn's durable frontier journal. Non-turn helpers may still omit it
@@ -103,16 +107,6 @@ export type RoutingWiringIds = {
103
107
  sandboxId: string;
104
108
  epoch: number;
105
109
  };
106
- /**
107
- * Whether the turn's `defaultBackend` IS the session's home (so the null pointer may
108
- * resolve to it). Defaults to TRUE (omitted). Set explicitly FALSE on a machine-primary
109
- * turn of a Modal-HOME session (pinned to a machine, no Modal group box established this
110
- * turn): the routing resolver's null branch then throws a typed `home_unavailable_this_turn`
111
- * error on a mid-turn clear-to-null instead of silently serving the pinned machine — the
112
- * detach's pointer commit stands and takes effect next turn. A genuine machine-HOME turn
113
- * (home IS the machine) passes true.
114
- */
115
- defaultIsHome?: boolean;
116
110
  };
117
111
  /** Map the deployment relay URL to the leaf's `SelfhostedRelayConfig` shape
118
112
  * (host/port/tls). M8 wires the real relay; until then a configured/placeholder
@@ -208,6 +202,10 @@ export type SelfhostedTurnSessionArgs = {
208
202
  /** Project one enrollment read into one exact runtime route. Capability and
209
203
  * process identity must be taken from the same snapshot. */
210
204
  export declare function connectionBindingFor(services: RoutingWiringServices, enrollment: EnrollmentRecord | null): SelfhostedConnectionBinding | null;
205
+ /** One line an operator can read when a Connected Machine authority check is
206
+ * refused: the SQLSTATE, the fixed reason the database raised, and the
207
+ * database functions that raised it. Never parameters or SQL text. */
208
+ export declare function machineAuthorityFailureLog(error: unknown): string;
211
209
  export declare function establishSelfhostedTurnSession(services: RoutingWiringServices, args: SelfhostedTurnSessionArgs): Promise<EstablishedSandboxSession>;
212
210
  /** Whether the routing proxy should wrap the turn box: the hot-swap feature is
213
211
  * gated by the selfhosted flag (the active pointer + swap tools are only
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "6.7.0",
3
+ "version": "6.8.0",
4
4
  "description": "Opengeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -56,33 +56,33 @@
56
56
  "@1password/sdk": "0.5.0",
57
57
  "@keeper-security/secrets-manager-core": "17.6.0",
58
58
  "@llamaindex/liteparse": "2.14.2",
59
- "@opengeni/agent-proto": "6.7.0",
60
- "@opengeni/capabilities": "6.7.0",
61
- "@opengeni/codemode": "6.7.0",
62
- "@opengeni/codex": "6.7.0",
63
- "@opengeni/config": "6.7.0",
64
- "@opengeni/contracts": "6.7.0",
65
- "@opengeni/core": "6.7.0",
66
- "@opengeni/db": "6.7.0",
67
- "@opengeni/decisions": "6.7.0",
68
- "@opengeni/documents": "6.7.0",
69
- "@opengeni/events": "6.7.0",
70
- "@opengeni/github": "6.7.0",
71
- "@opengeni/interaction": "6.7.0",
72
- "@opengeni/jev": "6.7.0",
73
- "@opengeni/network": "6.7.0",
74
- "@opengeni/observability": "6.7.0",
75
- "@opengeni/runtime": "6.7.0",
76
- "@opengeni/sdk": "6.7.0",
77
- "@opengeni/storage": "6.7.0",
78
- "@opengeni/subscriptions": "6.7.0",
79
- "@opengeni/tool-gateway": "6.7.0",
80
- "@opengeni/vaults": "6.7.0",
81
- "@opengeni/vaults-bitwarden": "6.7.0",
82
- "@opengeni/vaults-keeper": "6.7.0",
83
- "@opengeni/vaults-onepassword": "6.7.0",
84
- "@opengeni/vaults-protonpass": "6.7.0",
85
- "@opengeni/xai-subscription": "6.7.0",
59
+ "@opengeni/agent-proto": "6.8.0",
60
+ "@opengeni/capabilities": "6.8.0",
61
+ "@opengeni/codemode": "6.8.0",
62
+ "@opengeni/codex": "6.8.0",
63
+ "@opengeni/config": "6.8.0",
64
+ "@opengeni/contracts": "6.8.0",
65
+ "@opengeni/core": "6.8.0",
66
+ "@opengeni/db": "6.8.0",
67
+ "@opengeni/decisions": "6.8.0",
68
+ "@opengeni/documents": "6.8.0",
69
+ "@opengeni/events": "6.8.0",
70
+ "@opengeni/github": "6.8.0",
71
+ "@opengeni/interaction": "6.8.0",
72
+ "@opengeni/jev": "6.8.0",
73
+ "@opengeni/network": "6.8.0",
74
+ "@opengeni/observability": "6.8.0",
75
+ "@opengeni/runtime": "6.8.0",
76
+ "@opengeni/sdk": "6.8.0",
77
+ "@opengeni/storage": "6.8.0",
78
+ "@opengeni/subscriptions": "6.8.0",
79
+ "@opengeni/tool-gateway": "6.8.0",
80
+ "@opengeni/vaults": "6.8.0",
81
+ "@opengeni/vaults-bitwarden": "6.8.0",
82
+ "@opengeni/vaults-keeper": "6.8.0",
83
+ "@opengeni/vaults-onepassword": "6.8.0",
84
+ "@opengeni/vaults-protonpass": "6.8.0",
85
+ "@opengeni/xai-subscription": "6.8.0",
86
86
  "@temporalio/activity": "^1.17.0",
87
87
  "@temporalio/client": "^1.17.0",
88
88
  "@temporalio/worker": "^1.17.0",
@@ -568,6 +568,7 @@ export async function establishTurnSandbox(deps: EstablishTurnSandboxDeps): Prom
568
568
  onOp: machineOpObserver.observer,
569
569
  onSandboxOperation: sandboxOperationObserver,
570
570
  onSandboxCaptureWait: sandboxCaptureWaitMetricObserver(observability),
571
+ observability,
571
572
  opJournal,
572
573
  },
573
574
  {
@@ -644,6 +645,7 @@ export async function establishTurnSandbox(deps: EstablishTurnSandboxDeps): Prom
644
645
  onOp: machineOpObserver.observer,
645
646
  onSandboxOperation: sandboxOperationObserver,
646
647
  onSandboxCaptureWait: sandboxCaptureWaitMetricObserver(observability),
648
+ observability,
647
649
  onHomeSandboxRebound,
648
650
  ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
649
651
  },
@@ -673,16 +675,6 @@ export async function establishTurnSandbox(deps: EstablishTurnSandboxDeps): Prom
673
675
  sandboxId: activeSandboxPointer!.activeSandboxId!,
674
676
  epoch: activeSandboxPointer!.activeEpoch,
675
677
  },
676
- // HOME semantics for a mid-turn clear-to-null: only a genuine
677
- // machine-HOME session (its home IS this machine, session.sandboxBackend
678
- // === "selfhosted") resolves null back to the pinned machine. A Modal-HOME
679
- // session merely PINNED to a machine this turn never established its group
680
- // box, so defaultIsHome:false makes a clear-to-null fail typed
681
- // (`home_unavailable_this_turn`) rather than silently serving the machine.
682
- // Failure settlement checkpoints the completed detach and continues the same
683
- // logical turn in a fresh home-primary attempt; it never serves this machine
684
- // after the pointer changed and never invents a cloud lease for machine-only work.
685
- defaultIsHome: session.sandboxBackend === "selfhosted",
686
678
  },
687
679
  established,
688
680
  ),
@@ -908,6 +900,7 @@ export async function establishTurnSandbox(deps: EstablishTurnSandboxDeps): Prom
908
900
  opJournal,
909
901
  onSandboxOperation: sandboxOperationObserver,
910
902
  onSandboxCaptureWait: sandboxCaptureWaitMetricObserver(observability),
903
+ observability,
911
904
  onHomeSandboxLost: publishSandboxLost,
912
905
  onHomeSandboxRebound,
913
906
  ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
@@ -1346,6 +1339,7 @@ export async function bindLazySandboxProvisioner(
1346
1339
  onOp: machineOpObserver.observer,
1347
1340
  onSandboxOperation: sandboxOperationObserver,
1348
1341
  onSandboxCaptureWait: sandboxCaptureWaitMetricObserver(observability),
1342
+ observability,
1349
1343
  onHomeSandboxLost: publishSandboxLost,
1350
1344
  onHomeSandboxRebound,
1351
1345
  ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
@@ -19,9 +19,11 @@
19
19
  import {
20
20
  rememberingSecretResolver,
21
21
  SecretValueMemory,
22
+ credentialNameMayHoldSecret,
22
23
  secretCredentialMaskLabel,
23
24
  type SecretPrincipal,
24
25
  type SecretResolver,
26
+ type SecretValueMatchEvent,
25
27
  } from "@opengeni/contracts";
26
28
  import { createTaskVaultSecretCapturer, loadSessionTreeSecretValues } from "@opengeni/core";
27
29
  import { withSecretValueMasking, type Database } from "@opengeni/db";
@@ -29,12 +31,48 @@ import { activeAttemptSecretValues, withAttemptSecretValues } from "@opengeni/ru
29
31
 
30
32
  type DiagnosticSink = {
31
33
  warn(message: string, attributes?: Record<string, unknown>): void;
34
+ incrementCounter?(input: {
35
+ name: string;
36
+ help?: string;
37
+ labels?: Record<string, string>;
38
+ amount?: number;
39
+ }): void;
32
40
  };
33
41
 
42
+ /**
43
+ * Occurrences of known secret values, by outcome (masked, or kept because a
44
+ * weak value was outside a credential position), value strength, entropy
45
+ * bucket and position. Metrics only: never the value or the text.
46
+ */
47
+ export const SECRET_MASK_MATCHES_METRIC = {
48
+ name: "opengeni_secret_mask_matches_total",
49
+ help: "Known secret value occurrences in masked text, by outcome, value strength, entropy bucket and position.",
50
+ } as const;
51
+
52
+ function recordMaskMatch(observability: DiagnosticSink | null | undefined) {
53
+ return (event: SecretValueMatchEvent) => {
54
+ try {
55
+ observability?.incrementCounter?.({
56
+ ...SECRET_MASK_MATCHES_METRIC,
57
+ labels: {
58
+ outcome: event.outcome,
59
+ strength: event.strength,
60
+ entropy: event.entropy,
61
+ position: event.position,
62
+ },
63
+ amount: event.count,
64
+ });
65
+ } catch {
66
+ // Metrics never affect masking.
67
+ }
68
+ };
69
+ }
70
+
34
71
  export function createAttemptSecretValueMemory(
35
72
  observability?: DiagnosticSink | null,
36
73
  ): SecretValueMemory {
37
74
  return new SecretValueMemory({
75
+ ...(observability?.incrementCounter ? { onMatch: recordMaskMatch(observability) } : {}),
38
76
  onDiagnostic: (diagnostic) => {
39
77
  try {
40
78
  observability?.warn("secret value masking failed; text kept as written", {
@@ -90,16 +128,11 @@ export function rememberingAttemptSecretResolver(
90
128
  return rememberingSecretResolver(resolver, () => memory ?? currentAttemptSecretValues());
91
129
  }
92
130
 
93
- const SECRET_ENVIRONMENT_NAME =
94
- /TOKEN|SECRET|PASSW|PWD|KEY|CREDENTIAL|AUTH|SESSION|COOKIE|BEARER|PRIVATE|SIGNATURE/iu;
95
131
  /** Names of variables that hold where a credential is, not the credential. */
96
132
  const CREDENTIAL_LOCATION_NAME =
97
133
  /(?:_FILE|_PATH|_DIR|_URL|_ENDPOINT)$|^GOOGLE_APPLICATION_CREDENTIALS$/iu;
98
134
  /** Values that are file paths are not credentials either. */
99
135
  const PATH_VALUE = /^(?:\/|\.{1,2}\/|~\/|[A-Za-z]:\\)[^\s]*$/u;
100
- /** MCP headers that carry credentials; others (`Accept`, `X-Org`) are not secret. */
101
- const SECRET_HEADER_NAME =
102
- /^(?:authorization|proxy-authorization|cookie)$|token|key|secret|auth|passw|session|signature/iu;
103
136
  /** Run-credential environment values are remembered only when long enough
104
137
  * to be key material: a region or account name must not be masked. */
105
138
  const RUN_CREDENTIAL_MIN_CHARS = 12;
@@ -126,7 +159,7 @@ export function rememberRunCredentialMaterial(
126
159
  if (
127
160
  typeof value === "string" &&
128
161
  value.length >= RUN_CREDENTIAL_MIN_CHARS &&
129
- SECRET_ENVIRONMENT_NAME.test(name) &&
162
+ credentialNameMayHoldSecret(name) &&
130
163
  !CREDENTIAL_LOCATION_NAME.test(name) &&
131
164
  !PATH_VALUE.test(value)
132
165
  ) {
@@ -144,7 +177,7 @@ export function rememberRunCredentialMaterial(
144
177
  if (
145
178
  typeof value === "string" &&
146
179
  value.length >= RUN_CREDENTIAL_MIN_CHARS &&
147
- SECRET_HEADER_NAME.test(name)
180
+ credentialNameMayHoldSecret(name)
148
181
  ) {
149
182
  rememberAttemptCredential(memory, value, "mcp header");
150
183
  }
@@ -217,6 +217,20 @@ export function runtimeMetricsHooksForObservability(
217
217
  labels: { provider, path, code },
218
218
  });
219
219
  },
220
+ onModelToolNameRepair: ({ provider, api, field }) => {
221
+ observability.incrementCounter({
222
+ name: "opengeni_model_tool_name_repairs_total",
223
+ help: "Model requests in which the outbound guard aliased a tool name a provider could reject, by provider, wire API and field.",
224
+ labels: { provider, api, field },
225
+ });
226
+ },
227
+ onModelToolNameRetry: ({ provider, api, outcome }) => {
228
+ observability.incrementCounter({
229
+ name: "opengeni_model_tool_name_retries_total",
230
+ help: "Strict resends after a provider rejected a tool name, by provider, wire API and outcome.",
231
+ labels: { provider, api, outcome },
232
+ });
233
+ },
220
234
  onSandboxCreate: ({ backend, imageSource, outcome, durationSeconds }) => {
221
235
  observability.incrementCounter({
222
236
  name: "opengeni_sandbox_creates_total",
@@ -52,7 +52,10 @@ import {
52
52
  markWarmLeaseInstanceLost,
53
53
  readLease,
54
54
  readActiveSandbox,
55
+ nestedPostgresMessage,
56
+ nestedPostgresSqlState,
55
57
  resolvePersonalMachineConnectionForAttempt,
58
+ safeDatabaseErrorFacts,
56
59
  SandboxRetainedProcessPromotionFencedError,
57
60
  SandboxRetainedProcessTerminalError,
58
61
  SandboxWorkspaceMutationOutputRejectedError,
@@ -63,6 +66,7 @@ import {
63
66
  import { observeSessionBackgroundCommandCompletion } from "@opengeni/db/session-background-commands";
64
67
  import { appendSessionCommandOutput } from "@opengeni/db/session-command-output";
65
68
  import type { EventBus } from "@opengeni/events";
69
+ import type { Observability } from "@opengeni/observability";
66
70
  import { publishDurableSessionEvents } from "./session-event-fanout";
67
71
  import {
68
72
  buildSelfhostedBackendSession,
@@ -159,6 +163,9 @@ export type RoutingWiringServices = {
159
163
  /** Wakes a session waiting on a background command this routing settles.
160
164
  * Absent: the periodic wake dispatcher delivers the committed wake. */
161
165
  wakeSessionWorkflow?: WakeSessionWorkflowSignal | null | undefined;
166
+ /** Logs a refused Connected Machine authority check with the database's own
167
+ * reason. Absent: the refusal is still thrown unchanged, just not logged. */
168
+ observability?: Pick<Observability, "warn">;
162
169
  };
163
170
 
164
171
  /** Worker-turn routing may final-ack streamed command output, so it must always
@@ -221,16 +228,6 @@ export type RoutingWiringIds = {
221
228
  * swap target is built fresh).
222
229
  */
223
230
  pinnedSelfhosted?: { sandboxId: string; epoch: number };
224
- /**
225
- * Whether the turn's `defaultBackend` IS the session's home (so the null pointer may
226
- * resolve to it). Defaults to TRUE (omitted). Set explicitly FALSE on a machine-primary
227
- * turn of a Modal-HOME session (pinned to a machine, no Modal group box established this
228
- * turn): the routing resolver's null branch then throws a typed `home_unavailable_this_turn`
229
- * error on a mid-turn clear-to-null instead of silently serving the pinned machine — the
230
- * detach's pointer commit stands and takes effect next turn. A genuine machine-HOME turn
231
- * (home IS the machine) passes true.
232
- */
233
- defaultIsHome?: boolean;
234
231
  };
235
232
 
236
233
  /** Map the deployment relay URL to the leaf's `SelfhostedRelayConfig` shape
@@ -1082,7 +1079,7 @@ export function wrapTurnBoxWithRouting(
1082
1079
  controlRpcFactory: controlRpcFactory(bus),
1083
1080
  resolveSelfhostedConnection: async (sandbox) => {
1084
1081
  if (!sandbox.enrollmentId) return null;
1085
- const enrollment = await resolveRoutedMachineConnection(db, ids, sandbox);
1082
+ const enrollment = await resolveRoutedMachineConnection(services, ids, sandbox);
1086
1083
  return connectionBindingFor(services, enrollment);
1087
1084
  },
1088
1085
  relay: relayConfigFromSettings(settings),
@@ -1176,11 +1173,14 @@ export function wrapTurnBoxWithRouting(
1176
1173
  // session points at a sibling modal box the turn cannot resume here) and the
1177
1174
  // op surfaces unresolvable — never a silent wrong-box landing.
1178
1175
  //
1179
- // For a machine-primary turn of a Modal-HOME session (pinned to a machine, no
1180
- // group box established this turn), a mid-turn clear-to-null must NOT fall back to
1181
- // the pinned machine — passing defaultIsHome:false makes the null branch throw typed
1182
- // `home_unavailable_this_turn` instead. Forward the explicit boolean (including false).
1183
- ...(ids.defaultIsHome !== undefined ? { defaultIsHome: ids.defaultIsHome } : {}),
1176
+ // A pinned machine is never this turn's home. A null pointer means the chat's
1177
+ // own managed box, for machine-home chats too: turn start never resolves null to
1178
+ // a machine. So a mid-turn move home must not keep dispatching to the pinned
1179
+ // machine (whose per-operation authority now refuses it, because the machine is
1180
+ // no longer the active sandbox). The null branch throws the typed
1181
+ // `home_unavailable_this_turn` transition, and failure settlement continues the
1182
+ // same logical turn on the home box in a fresh attempt.
1183
+ ...(ids.pinnedSelfhosted ? { defaultIsHome: false } : {}),
1184
1184
  ...(ids.homeLease
1185
1185
  ? {
1186
1186
  resolveDefaultBackend: () =>
@@ -1418,7 +1418,7 @@ export function wrapLazyTurnBoxWithRouting(
1418
1418
  controlRpcFactory: controlRpcFactory(bus),
1419
1419
  resolveSelfhostedConnection: async (sandbox) => {
1420
1420
  if (!sandbox.enrollmentId) return null;
1421
- const enrollment = await resolveRoutedMachineConnection(db, ids, sandbox);
1421
+ const enrollment = await resolveRoutedMachineConnection(services, ids, sandbox);
1422
1422
  return connectionBindingFor(services, enrollment);
1423
1423
  },
1424
1424
  relay: relayConfigFromSettings(settings),
@@ -1759,6 +1759,46 @@ export function connectionBindingFor(
1759
1759
  };
1760
1760
  }
1761
1761
 
1762
+ /** One line an operator can read when a Connected Machine authority check is
1763
+ * refused: the SQLSTATE, the fixed reason the database raised, and the
1764
+ * database functions that raised it. Never parameters or SQL text. */
1765
+ export function machineAuthorityFailureLog(error: unknown): string {
1766
+ const sqlState = nestedPostgresSqlState(error);
1767
+ const reason = nestedPostgresMessage(error);
1768
+ const functions = safeDatabaseErrorFacts(error).plpgsql?.join(",");
1769
+ return (
1770
+ `Connected Machine authority check failed: sqlState=${sqlState ?? "none"}` +
1771
+ (reason ? ` reason=${reason}` : "") +
1772
+ (functions ? ` functions=${functions}` : "") +
1773
+ (sqlState ? "" : ` error=${error instanceof Error ? error.name : typeof error}`)
1774
+ );
1775
+ }
1776
+
1777
+ /** The model only sees a failed authority check as tool text, which names the
1778
+ * query but not why PostgreSQL refused it. Log the refusal before rethrowing the
1779
+ * same error unchanged; its driver error stays attached as `cause`. */
1780
+ async function resolvePersonalMachineConnectionWithDiagnostics(
1781
+ services: Pick<RoutingWiringServices, "db" | "observability">,
1782
+ input: Parameters<typeof resolvePersonalMachineConnectionForAttempt>[1],
1783
+ ): Promise<EnrollmentRecord | null> {
1784
+ try {
1785
+ return await resolvePersonalMachineConnectionForAttempt(services.db, input);
1786
+ } catch (error) {
1787
+ try {
1788
+ services.observability?.warn(machineAuthorityFailureLog(error), {
1789
+ workspaceId: input.workspaceId,
1790
+ sessionId: input.sessionId,
1791
+ turnId: input.turnId,
1792
+ attemptId: input.attemptId,
1793
+ enrollmentId: input.enrollmentId,
1794
+ });
1795
+ } catch {
1796
+ // Logging must not replace the refusal the caller is about to see.
1797
+ }
1798
+ throw error;
1799
+ }
1800
+ }
1801
+
1762
1802
  /** Personal-machine discovery is not execution authority. A machine's audience
1763
1803
  * can change in place while a routed session is cached, so the current scope is
1764
1804
  * read from the enrollment itself, never from the sandbox snapshot taken when
@@ -1767,17 +1807,18 @@ export function connectionBindingFor(
1767
1807
  * visibility-keyed grant immediately before each routed operation resolves a
1768
1808
  * live control-plane connection. */
1769
1809
  async function resolveRoutedMachineConnection(
1770
- db: Database,
1810
+ services: Pick<RoutingWiringServices, "db" | "observability">,
1771
1811
  ids: RoutingWiringIds,
1772
1812
  sandbox: RoutableSandbox,
1773
1813
  ): Promise<EnrollmentRecord | null> {
1814
+ const { db } = services;
1774
1815
  if (!sandbox.enrollmentId) return null;
1775
1816
  const current = await getLiveEnrollmentConnection(db, sandbox.workspaceId, sandbox.enrollmentId);
1776
1817
  if (!current) return null;
1777
1818
  if (current.scope === "user") {
1778
1819
  const authority = ids.personalMachineAttempt;
1779
1820
  if (!authority) return null;
1780
- return await resolvePersonalMachineConnectionForAttempt(db, {
1821
+ return await resolvePersonalMachineConnectionWithDiagnostics(services, {
1781
1822
  accountId: authority.accountId,
1782
1823
  workspaceId: ids.workspaceId,
1783
1824
  subjectId: authority.subjectId,
@@ -1847,7 +1888,7 @@ export async function establishSelfhostedTurnSession(
1847
1888
  current?.scope !== "user"
1848
1889
  ? current
1849
1890
  : args.personalMachineAttempt
1850
- ? await resolvePersonalMachineConnectionForAttempt(db, {
1891
+ ? await resolvePersonalMachineConnectionWithDiagnostics(services, {
1851
1892
  accountId: args.personalMachineAttempt.accountId,
1852
1893
  workspaceId: args.workspaceId,
1853
1894
  subjectId: args.personalMachineAttempt.subjectId,