deepline 0.3.200 → 0.3.202

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 (43) hide show
  1. package/dist/bundling-sources/sdk/src/client.ts +62 -1
  2. package/dist/bundling-sources/sdk/src/http.ts +55 -6
  3. package/dist/bundling-sources/sdk/src/index.ts +1 -0
  4. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  5. package/dist/bundling-sources/sdk/src/types.ts +10 -2
  6. package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +4 -2
  7. package/dist/bundling-sources/shared_libs/play-runtime/context.ts +460 -96
  8. package/dist/bundling-sources/shared_libs/play-runtime/ctx-types.ts +11 -1
  9. package/dist/bundling-sources/shared_libs/play-runtime/execution-gateway-invocation-fence.ts +6 -2
  10. package/dist/bundling-sources/shared_libs/play-runtime/fixture-behavior.ts +401 -2
  11. package/dist/bundling-sources/shared_libs/play-runtime/output-size-limits.ts +458 -0
  12. package/dist/bundling-sources/shared_libs/play-runtime/provider-no-progress.ts +40 -0
  13. package/dist/bundling-sources/shared_libs/play-runtime/receipt-sql.ts +7 -0
  14. package/dist/bundling-sources/shared_libs/play-runtime/run-ledger.ts +25 -3
  15. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +1 -0
  16. package/dist/bundling-sources/shared_libs/play-runtime/runtime-api.ts +119 -171
  17. package/dist/bundling-sources/shared_libs/play-runtime/runtime-work-counters.ts +37 -0
  18. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-deadline-reconciliation.ts +160 -58
  19. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +111 -6
  20. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd.ts +51 -46
  21. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-rate-state.ts +16 -7
  22. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +304 -33
  23. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/receipt-gateway.ts +21 -0
  24. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-receipt-value-storage.ts +64 -15
  25. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-capacity.ts +133 -5
  26. package/dist/bundling-sources/shared_libs/play-runtime/sheet-attempt-sql.ts +5 -5
  27. package/dist/bundling-sources/shared_libs/play-runtime/tool-call/index.ts +1 -0
  28. package/dist/bundling-sources/shared_libs/play-runtime/tool-call/result-receipt-policy.ts +46 -0
  29. package/dist/bundling-sources/shared_libs/play-runtime/tool-execute-retry-policy.ts +14 -5
  30. package/dist/bundling-sources/shared_libs/play-runtime/tool-http-errors.ts +5 -1
  31. package/dist/bundling-sources/shared_libs/play-runtime/work-receipt-state-machine.ts +3 -9
  32. package/dist/cli/index.js +364 -39
  33. package/dist/cli/index.mjs +367 -41
  34. package/dist/index.d.mts +131 -3
  35. package/dist/index.d.ts +131 -3
  36. package/dist/index.js +64 -7
  37. package/dist/index.mjs +64 -7
  38. package/dist/install-integrity.json +3 -0
  39. package/dist/release.d.mts +1 -1
  40. package/dist/release.d.ts +1 -1
  41. package/dist/release.js +1 -1
  42. package/dist/release.mjs +1 -1
  43. package/package.json +1 -1
@@ -88,7 +88,6 @@ import {
88
88
  activeRuntimeSheetAttemptFenceSql,
89
89
  newerTerminalRuntimeSheetRowSql,
90
90
  releasableRuntimeSheetAttemptFenceSql,
91
- sameOwnerTerminalAttemptEpochSql,
92
91
  } from './sheet-attempt-sql';
93
92
  import type { MapRowOutcome } from './durability-store';
94
93
  import { RUNTIME_CAPACITY_POLICY } from './runtime-capacity-policy';
@@ -3688,7 +3687,9 @@ async function prepareRuntimeSheetDatasetRows(
3688
3687
  ORDER BY key_values._key, key_values.ord
3689
3688
  ),
3690
3689
  preexisting_rows AS (
3691
- SELECT target._key
3690
+ -- Force is new intent across runs, not across scheduler attempts of
3691
+ -- one run. Capture the owner before versioned updates restamp it.
3692
+ SELECT target._key, target._run_id
3692
3693
  FROM ${sheetTable(session)} AS target
3693
3694
  JOIN input_rows ON input_rows._key = target._key
3694
3695
  ),
@@ -3705,7 +3706,7 @@ async function prepareRuntimeSheetDatasetRows(
3705
3706
  AND target._status = 'enriched'
3706
3707
  AND COALESCE(target._write_version, 0) < $12::bigint
3707
3708
  RETURNING target._key,
3708
- ($11::boolean OR (${targetMissingOutputSql})) AS needs_recompute
3709
+ (${targetMissingOutputSql}) AS missing_output
3709
3710
  ),
3710
3711
  versioned_failed_rows AS (
3711
3712
  UPDATE ${sheetTable(session)} AS target
@@ -3750,7 +3751,13 @@ async function prepareRuntimeSheetDatasetRows(
3750
3751
  )
3751
3752
  )
3752
3753
  AND (
3753
- $11::boolean
3754
+ (
3755
+ $11::boolean
3756
+ AND (
3757
+ target._status <> 'enriched'
3758
+ OR target._run_id IS DISTINCT FROM $4::text
3759
+ )
3760
+ )
3754
3761
  OR target._input_index IS DISTINCT FROM input_rows._input_index
3755
3762
  )
3756
3763
  RETURNING target._key
@@ -3856,13 +3863,20 @@ async function prepareRuntimeSheetDatasetRows(
3856
3863
  SELECT _key
3857
3864
  FROM claimed_existing_rows
3858
3865
  UNION
3859
- SELECT _key
3866
+ SELECT existing_rows._key
3860
3867
  FROM existing_rows
3868
+ JOIN preexisting_rows USING (_key)
3861
3869
  WHERE $11::boolean
3870
+ AND preexisting_rows._run_id IS DISTINCT FROM $4::text
3862
3871
  UNION
3863
- SELECT _key
3864
- FROM versioned_completed_rows
3865
- WHERE needs_recompute
3872
+ SELECT completed._key
3873
+ FROM versioned_completed_rows AS completed
3874
+ JOIN preexisting_rows USING (_key)
3875
+ WHERE completed.missing_output
3876
+ OR (
3877
+ $11::boolean
3878
+ AND preexisting_rows._run_id IS DISTINCT FROM $4::text
3879
+ )
3866
3880
  UNION
3867
3881
  SELECT _key FROM versioned_failed_rows
3868
3882
  UNION
@@ -3901,14 +3915,23 @@ async function prepareRuntimeSheetDatasetRows(
3901
3915
  FROM ${sheetTable(session)} AS existing
3902
3916
  JOIN input_rows ON input_rows._key = existing._key
3903
3917
  WHERE existing._status = 'enriched'
3904
- AND NOT ($11::boolean)
3918
+ AND (
3919
+ NOT ($11::boolean)
3920
+ OR existing._run_id = $4::text
3921
+ )
3905
3922
  AND NOT (${existingMissingOutputSql})
3906
3923
  AND NOT EXISTS (
3907
3924
  SELECT 1 FROM pending_rows WHERE pending_rows._key = existing._key
3908
3925
  )
3909
3926
  UNION
3910
- SELECT _key FROM versioned_completed_rows
3911
- WHERE NOT needs_recompute
3927
+ SELECT completed._key
3928
+ FROM versioned_completed_rows AS completed
3929
+ JOIN preexisting_rows USING (_key)
3930
+ WHERE NOT completed.missing_output
3931
+ AND (
3932
+ NOT ($11::boolean)
3933
+ OR preexisting_rows._run_id = $4::text
3934
+ )
3912
3935
  ),
3913
3936
  blocked_rows AS (
3914
3937
  SELECT input_rows._key
@@ -4217,12 +4240,42 @@ async function buildRuntimeSheetDatasetStartResult(
4217
4240
  });
4218
4241
  }
4219
4242
  if (input.force === true) {
4243
+ // A force flag expresses a new refresh only across runs. A scheduler
4244
+ // resume of that same forced run must return its completed siblings with
4245
+ // their durable output, while pending rows still start from fresh input.
4246
+ const completedKeys = input.rowEntries.flatMap((entry) =>
4247
+ dispositionsByKey.get(entry.key) === 'completed' ? [entry.key] : [],
4248
+ );
4249
+ const completedData = completedKeys.length
4250
+ ? await readRuntimeRowsByKey(
4251
+ client,
4252
+ session,
4253
+ completedKeys,
4254
+ input.sheetContract,
4255
+ )
4256
+ : [];
4257
+ const completedDataByKey = new Map(
4258
+ completedData.map((row) => [row.key, row.data]),
4259
+ );
4220
4260
  const pendingRows: Record<string, unknown>[] = [];
4261
+ const completedRows: Record<string, unknown>[] = [];
4221
4262
  const blockedRows: Record<string, unknown>[] = [];
4222
4263
  for (const entry of input.rowEntries) {
4223
4264
  const disposition = dispositionsByKey.get(entry.key) ?? 'blocked';
4224
4265
  if (disposition === 'pending') {
4225
4266
  pendingRows.push(buildFreshRow(entry));
4267
+ } else if (disposition === 'completed') {
4268
+ const stored = completedDataByKey.get(entry.key);
4269
+ if (!stored) {
4270
+ throw new Error(
4271
+ `Forced run resume lost completed Runtime Sheet row ${entry.key}.`,
4272
+ );
4273
+ }
4274
+ completedRows.push({
4275
+ ...entry.row,
4276
+ ...stored,
4277
+ __deeplineRowKey: entry.key,
4278
+ });
4226
4279
  } else if (disposition === 'blocked') {
4227
4280
  blockedRows.push(buildFreshRow(entry));
4228
4281
  }
@@ -4231,7 +4284,7 @@ async function buildRuntimeSheetDatasetStartResult(
4231
4284
  inserted: input.inserted,
4232
4285
  skipped: input.sourceRowsLength - input.rowEntries.length,
4233
4286
  pendingRows,
4234
- completedRows: [],
4287
+ completedRows,
4235
4288
  blockedRows,
4236
4289
  tableNamespace: input.tableNamespace,
4237
4290
  writeVersion: input.writeVersion,
@@ -7120,7 +7173,6 @@ export type RuntimeMapRowsWriteResult = {
7120
7173
  type RuntimeMapRowsMutationResult = {
7121
7174
  updated: number;
7122
7175
  fencedRows: RuntimeMapRowsStaleRow[];
7123
- conflictKeys: string[];
7124
7176
  };
7125
7177
 
7126
7178
  const RUNTIME_MAP_ROWS_STALE_REASONS: ReadonlySet<RuntimeMapRowsStaleReason> =
@@ -7310,7 +7362,6 @@ async function completeRuntimeMapRowChunks(
7310
7362
  ): Promise<RuntimeMapRowsMutationResult> {
7311
7363
  let updated = 0;
7312
7364
  const fencedRows: RuntimeMapRowsStaleRow[] = [];
7313
- const conflictKeys: string[] = [];
7314
7365
  for (const chunk of input.chunks) {
7315
7366
  const chunkKeys = chunk.map((row) => row.key);
7316
7367
  const chunkInputIndexes = chunk.map((row) => row.input_index);
@@ -7338,7 +7389,6 @@ async function completeRuntimeMapRowChunks(
7338
7389
  updated: number;
7339
7390
  matched_keys: string[];
7340
7391
  fenced_rows: unknown;
7341
- conflict_keys: string[];
7342
7392
  }>(
7343
7393
  `WITH updates AS (
7344
7394
  SELECT key_values._key,
@@ -7355,6 +7405,7 @@ async function completeRuntimeMapRowChunks(
7355
7405
  ),
7356
7406
  matched_updates AS (
7357
7407
  SELECT target._key AS matched_key,
7408
+ updates._key AS submitted_key,
7358
7409
  target._status AS prev_status,
7359
7410
  target._cell_meta AS prev_cell_meta,
7360
7411
  updates.input_index,
@@ -7364,40 +7415,6 @@ async function completeRuntimeMapRowChunks(
7364
7415
  JOIN ${sheetTable(session)} AS target
7365
7416
  ON target._key = updates._key
7366
7417
  ),
7367
- same_version_conflicts AS (
7368
- SELECT updates.matched_key AS _key
7369
- FROM matched_updates AS updates
7370
- JOIN ${sheetTable(session)} AS target
7371
- ON target._key = updates.matched_key
7372
- WHERE $13::bigint IS NOT NULL
7373
- AND target._write_version = $13::bigint
7374
- AND target._run_id = $5::text
7375
- AND target._status IN ('enriched', 'failed')
7376
- AND (
7377
- EXISTS (
7378
- SELECT 1
7379
- FROM unnest($8::text[]) AS field_values(field)
7380
- WHERE target._cell_meta -> field_values.field ->> 'runId' = $5::text
7381
- )
7382
- OR (
7383
- coalesce(target._attempt_owner_run_id, target._run_id) = $10::text
7384
- AND COALESCE(target._attempt_seq, 0) = $12::integer
7385
- )
7386
- )
7387
- AND (
7388
- target._status <> 'enriched'
7389
- OR (${targetChangedPatchedCellSql})
7390
- )
7391
- -- First terminal wins: suppress conflict for same owner+epoch already-terminal row.
7392
- -- Durable replay (omitted_terminal_replay) from same attempt epoch is idempotent.
7393
- AND NOT (
7394
- $10::text IS NOT NULL
7395
- AND $12::integer IS NOT NULL
7396
- AND target._status = 'enriched'
7397
- AND coalesce(target._attempt_owner_run_id, target._run_id) = $10::text
7398
- AND COALESCE(target._attempt_seq, 0) = $12::integer
7399
- )
7400
- ),
7401
7418
  applied_rows AS (
7402
7419
  UPDATE ${sheetTable(session)} AS target
7403
7420
  SET _status = 'enriched',
@@ -7414,7 +7431,6 @@ async function completeRuntimeMapRowChunks(
7414
7431
  _cell_meta = ${mergeRuntimeCellMetaPatchSql('target._cell_meta', 'updates.cell_meta_patch')}${input.physicalUpdateSetSql}
7415
7432
  FROM matched_updates AS updates
7416
7433
  WHERE target._key = updates.matched_key
7417
- AND NOT EXISTS (SELECT 1 FROM same_version_conflicts)
7418
7434
  AND ($13::bigint IS NULL OR target._write_version = $13::bigint)
7419
7435
  AND ($13::bigint IS NOT NULL OR target._status <> 'enriched')
7420
7436
  AND NOT (${newerTerminalRuntimeSheetRowSql('target', '$11::timestamptz', '$12::integer', '$9::text', '$10::text')})
@@ -7424,14 +7440,27 @@ async function completeRuntimeMapRowChunks(
7424
7440
  OR ($9::text IS NOT NULL AND ${activeRuntimeSheetAttemptFenceSql('target', '$9::text', '$10::text', '$11::timestamptz', '$12::integer')})
7425
7441
  OR ($9::text IS NOT NULL AND ${writableEnrichedAttemptSql})
7426
7442
  )
7427
- -- First terminal wins: skip UPDATE for already-terminal same-owner+epoch row.
7428
- -- Even though we excluded from conflicts, we still no-op here for idempotency.
7443
+ -- A same-version enriched row is already the durable result for this
7444
+ -- logical run. Provider retries may produce a different payload or
7445
+ -- timestamp; preserve the first terminal value. Input-index repair
7446
+ -- remains writable when the submitted key itself changed.
7429
7447
  AND NOT (
7430
- $10::text IS NOT NULL
7431
- AND $12::integer IS NOT NULL
7448
+ $13::bigint IS NOT NULL
7449
+ AND target._write_version = $13::bigint
7450
+ AND target._run_id = $5::text
7432
7451
  AND target._status = 'enriched'
7433
- AND coalesce(target._attempt_owner_run_id, target._run_id) = $10::text
7434
- AND COALESCE(target._attempt_seq, 0) = $12::integer
7452
+ AND updates.submitted_key = target._key
7453
+ AND (
7454
+ EXISTS (
7455
+ SELECT 1
7456
+ FROM unnest($8::text[]) AS field_values(field)
7457
+ WHERE target._cell_meta -> field_values.field ->> 'runId' = $5::text
7458
+ )
7459
+ OR (
7460
+ $10::text IS NOT NULL
7461
+ AND target._attempt_owner_run_id IS NOT DISTINCT FROM $10::text
7462
+ )
7463
+ )
7435
7464
  )
7436
7465
  AND (
7437
7466
  target._status <> 'enriched'
@@ -7564,7 +7593,6 @@ async function completeRuntimeMapRowChunks(
7564
7593
  (SELECT count(*)::int FROM applied_rows) AS updated,
7565
7594
  coalesce((SELECT array_agg(matched_key) FROM matched_updates), '{}'::text[]) AS matched_keys,
7566
7595
  coalesce((SELECT jsonb_agg(jsonb_build_object('key', _key, 'reason', reason, 'rowVersionStamp', row_write_version)) FROM fenced_rows), '[]'::jsonb) AS fenced_rows,
7567
- coalesce((SELECT array_agg(_key) FROM same_version_conflicts), '{}'::text[]) AS conflict_keys,
7568
7596
  (SELECT count(*)::int FROM summary_delta) AS summary_delta_count,
7569
7597
  (SELECT count(*)::int FROM column_delta) AS column_delta_count`,
7570
7598
  [
@@ -7585,19 +7613,12 @@ async function completeRuntimeMapRowChunks(
7585
7613
  );
7586
7614
  const matchedKeys = new Set(rows[0]?.matched_keys ?? []);
7587
7615
  const chunkFencedRows = parseRuntimeMapRowsFencedRows(rows[0]?.fenced_rows);
7588
- const chunkConflictKeys = rows[0]?.conflict_keys ?? [];
7589
7616
  // An exact-version replay that already produced the same terminal value is
7590
7617
  // accepted idempotently even when SQL has no physical mutation to apply.
7591
7618
  // Stale versions remain excluded through fenced_rows.
7592
7619
  const appliedCount = Number(rows[0]?.updated ?? 0);
7593
7620
  updated += appliedCount;
7594
7621
  fencedRows.push(...chunkFencedRows);
7595
- conflictKeys.push(...chunkConflictKeys);
7596
-
7597
- if (chunkConflictKeys.length > 0) {
7598
- continue;
7599
- }
7600
-
7601
7622
  if (matchedKeys.size === chunk.length) {
7602
7623
  continue;
7603
7624
  }
@@ -7615,9 +7636,8 @@ async function completeRuntimeMapRowChunks(
7615
7636
  );
7616
7637
  updated += repaired.updated;
7617
7638
  fencedRows.push(...repaired.fencedRows);
7618
- conflictKeys.push(...repaired.conflictKeys);
7619
7639
  }
7620
- return { updated, fencedRows, conflictKeys };
7640
+ return { updated, fencedRows };
7621
7641
  }
7622
7642
 
7623
7643
  async function completeRuntimeMapRowChunksWithInputIndexRepair(
@@ -7627,7 +7647,6 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7627
7647
  ): Promise<RuntimeMapRowsMutationResult> {
7628
7648
  let updated = 0;
7629
7649
  const fencedRows: RuntimeMapRowsStaleRow[] = [];
7630
- const conflictKeys: string[] = [];
7631
7650
  for (const chunk of input.chunks) {
7632
7651
  const chunkKeys = chunk.map((row) => row.key);
7633
7652
  const chunkInputIndexes = chunk.map((row) => row.input_index);
@@ -7647,7 +7666,6 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7647
7666
  const { rows } = await client.query<{
7648
7667
  _key?: string;
7649
7668
  fenced_rows?: unknown;
7650
- conflict_keys?: string[];
7651
7669
  updated?: number;
7652
7670
  }>(
7653
7671
  `WITH updates AS (
@@ -7679,40 +7697,6 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7679
7697
  )
7680
7698
  ORDER BY target._key, (target._key = updates._key) DESC
7681
7699
  ),
7682
- same_version_conflicts AS (
7683
- SELECT updates.submitted_key AS _key
7684
- FROM matched_updates AS updates
7685
- JOIN ${sheetTable(session)} AS target
7686
- ON target._key = updates.matched_key
7687
- WHERE $13::bigint IS NOT NULL
7688
- AND target._write_version = $13::bigint
7689
- AND target._run_id = $5::text
7690
- AND target._status IN ('enriched', 'failed')
7691
- AND (
7692
- EXISTS (
7693
- SELECT 1
7694
- FROM unnest($8::text[]) AS field_values(field)
7695
- WHERE target._cell_meta -> field_values.field ->> 'runId' = $5::text
7696
- )
7697
- OR (
7698
- coalesce(target._attempt_owner_run_id, target._run_id) = $10::text
7699
- AND COALESCE(target._attempt_seq, 0) = $12::integer
7700
- )
7701
- )
7702
- AND (
7703
- target._status <> 'enriched'
7704
- OR (${targetChangedPatchedCellSql})
7705
- )
7706
- -- First terminal wins: suppress conflict for same owner+epoch already-terminal row.
7707
- -- Durable replay (omitted_terminal_replay) from same attempt epoch is idempotent.
7708
- AND NOT (
7709
- $10::text IS NOT NULL
7710
- AND $12::integer IS NOT NULL
7711
- AND target._status = 'enriched'
7712
- AND coalesce(target._attempt_owner_run_id, target._run_id) = $10::text
7713
- AND COALESCE(target._attempt_seq, 0) = $12::integer
7714
- )
7715
- ),
7716
7700
  applied_rows AS (
7717
7701
  UPDATE ${sheetTable(session)} AS target
7718
7702
  SET _status = 'enriched',
@@ -7730,14 +7714,31 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7730
7714
  FROM matched_updates AS updates, ${sheetTable(session)} AS prev
7731
7715
  WHERE target._key = updates.matched_key
7732
7716
  AND prev._key = target._key
7733
- AND NOT EXISTS (SELECT 1 FROM same_version_conflicts)
7734
7717
  AND ($13::bigint IS NULL OR target._write_version = $13::bigint)
7735
7718
  AND ($13::bigint IS NOT NULL OR target._status <> 'enriched')
7736
7719
  AND NOT (${newerTerminalRuntimeSheetRowSql('target', '$11::timestamptz', '$12::integer', '$9::text', '$10::text')})
7737
7720
  AND (
7738
7721
  ($9::text IS NULL AND target._run_id = $5::text)
7739
7722
  OR ($9::text IS NOT NULL AND ${activeRuntimeSheetAttemptFenceSql('target', '$9::text', '$10::text', '$11::timestamptz', '$12::integer')})
7740
- OR ($9::text IS NOT NULL AND ${writableEnrichedAttemptSql})
7723
+ OR ($9::text IS NOT NULL AND ${writableEnrichedAttemptSql})
7724
+ )
7725
+ AND NOT (
7726
+ $13::bigint IS NOT NULL
7727
+ AND target._write_version = $13::bigint
7728
+ AND target._run_id = $5::text
7729
+ AND target._status = 'enriched'
7730
+ AND updates.submitted_key = target._key
7731
+ AND (
7732
+ EXISTS (
7733
+ SELECT 1
7734
+ FROM unnest($8::text[]) AS field_values(field)
7735
+ WHERE target._cell_meta -> field_values.field ->> 'runId' = $5::text
7736
+ )
7737
+ OR (
7738
+ $10::text IS NOT NULL
7739
+ AND target._attempt_owner_run_id IS NOT DISTINCT FROM $10::text
7740
+ )
7741
+ )
7741
7742
  )
7742
7743
  AND (
7743
7744
  target._status <> 'enriched'
@@ -7866,8 +7867,7 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7866
7867
  )
7867
7868
  SELECT
7868
7869
  (SELECT count(*)::int FROM applied_rows) AS updated,
7869
- coalesce((SELECT jsonb_agg(jsonb_build_object('key', _key, 'reason', reason, 'rowVersionStamp', row_write_version)) FROM fenced_rows), '[]'::jsonb) AS fenced_rows,
7870
- coalesce((SELECT array_agg(_key) FROM same_version_conflicts), '{}'::text[]) AS conflict_keys`,
7870
+ coalesce((SELECT jsonb_agg(jsonb_build_object('key', _key, 'reason', reason, 'rowVersionStamp', row_write_version)) FROM fenced_rows), '[]'::jsonb) AS fenced_rows`,
7871
7871
  [
7872
7872
  chunkKeys,
7873
7873
  chunkInputIndexes,
@@ -7886,9 +7886,8 @@ async function completeRuntimeMapRowChunksWithInputIndexRepair(
7886
7886
  );
7887
7887
  updated += Number(rows[0]?.updated ?? 0);
7888
7888
  fencedRows.push(...parseRuntimeMapRowsFencedRows(rows[0]?.fenced_rows));
7889
- conflictKeys.push(...(rows[0]?.conflict_keys ?? []));
7890
7889
  }
7891
- return { updated, fencedRows, conflictKeys };
7890
+ return { updated, fencedRows };
7892
7891
  }
7893
7892
 
7894
7893
  async function insertMissingCompletedMapRowChunks(
@@ -8084,7 +8083,6 @@ async function failRuntimeMapRowChunks(
8084
8083
  ): Promise<RuntimeMapRowsMutationResult> {
8085
8084
  let updated = 0;
8086
8085
  const fencedRows: RuntimeMapRowsStaleRow[] = [];
8087
- const conflictKeys: string[] = [];
8088
8086
  for (const chunk of input.chunks) {
8089
8087
  const chunkKeys = chunk.map((row) => row.key);
8090
8088
  const chunkInputIndexes = chunk.map((row) => row.input_index);
@@ -8117,7 +8115,6 @@ async function failRuntimeMapRowChunks(
8117
8115
  const { rows } = await client.query<{
8118
8116
  updated?: number;
8119
8117
  fenced_rows?: unknown;
8120
- conflict_keys?: string[];
8121
8118
  }>(
8122
8119
  `WITH updates AS (
8123
8120
  SELECT key_values._key,
@@ -8138,6 +8135,7 @@ async function failRuntimeMapRowChunks(
8138
8135
  matched_updates AS (
8139
8136
  SELECT DISTINCT ON (target._key)
8140
8137
  target._key AS matched_key,
8138
+ updates._key AS submitted_key,
8141
8139
  updates.data_patch,
8142
8140
  updates.cell_meta_patch,
8143
8141
  updates.error
@@ -8151,42 +8149,6 @@ async function failRuntimeMapRowChunks(
8151
8149
  )
8152
8150
  ORDER BY target._key, (target._key = updates._key) DESC
8153
8151
  ),
8154
- same_version_conflicts AS (
8155
- SELECT updates.matched_key AS _key
8156
- FROM matched_updates AS updates
8157
- JOIN ${sheetTable(session)} AS target
8158
- ON target._key = updates.matched_key
8159
- WHERE $16::bigint IS NOT NULL
8160
- AND target._write_version = $16::bigint
8161
- AND target._run_id = $6::text
8162
- AND target._status IN ('enriched', 'failed')
8163
- AND (
8164
- EXISTS (
8165
- SELECT 1
8166
- FROM unnest($9::text[]) AS field_values(field)
8167
- WHERE target._cell_meta -> field_values.field ->> 'runId' = $6::text
8168
- )
8169
- OR (
8170
- coalesce(target._attempt_owner_run_id, target._run_id) = $12::text
8171
- AND COALESCE(target._attempt_seq, 0) = $14::integer
8172
- )
8173
- )
8174
- AND (
8175
- target._status <> 'failed'
8176
- OR target._error IS DISTINCT FROM updates.error
8177
- OR (${targetChangedPatchedCellSql})
8178
- )
8179
- -- Same-owner terminal replay is not a conflict. Normal replay
8180
- -- preserves the first value below; explicit force-failure may update it.
8181
- -- Different owners and attempt epochs still conflict.
8182
- AND NOT (
8183
- $12::text IS NOT NULL
8184
- AND $14::integer IS NOT NULL
8185
- AND target._status IN ('failed', 'enriched')
8186
- AND coalesce(target._attempt_owner_run_id, target._run_id) = $12::text
8187
- AND COALESCE(target._attempt_seq, 0) = $14::integer
8188
- )
8189
- ),
8190
8152
  applied_rows AS (
8191
8153
  UPDATE ${sheetTable(session)} AS target
8192
8154
  SET _status = 'failed',
@@ -8203,7 +8165,6 @@ async function failRuntimeMapRowChunks(
8203
8165
  _cell_meta = ${mergeRuntimeCellMetaPatchSql('target._cell_meta', 'updates.cell_meta_patch')}${input.physicalUpdateSetSql}
8204
8166
  FROM matched_updates AS updates, ${sheetTable(session)} AS prev
8205
8167
  WHERE target._key = updates.matched_key
8206
- AND NOT EXISTS (SELECT 1 FROM same_version_conflicts)
8207
8168
  AND ($16::bigint IS NULL OR target._write_version = $16::bigint)
8208
8169
  AND prev._key = target._key
8209
8170
  AND ($15::boolean OR target._status <> 'enriched')
@@ -8212,15 +8173,16 @@ async function failRuntimeMapRowChunks(
8212
8173
  ($11::text IS NULL AND target._run_id = $6::text)
8213
8174
  OR ($11::text IS NOT NULL AND ${activeRuntimeSheetAttemptFenceSql('target', '$11::text', '$12::text', '$13::timestamptz', '$14::integer')})
8214
8175
  )
8215
- -- First terminal wins for failed rows too, BUT only when NOT force-failing.
8216
- -- Normal replay: no-op when already-failed OR already-enriched from same owner+epoch.
8176
+ -- Normal replay preserves the first terminal value for an exact
8177
+ -- same-version key. Force-failure is the explicit teardown escape
8178
+ -- hatch and may still overwrite it.
8217
8179
  AND NOT (
8218
8180
  $15::boolean = FALSE
8219
- AND $12::text IS NOT NULL
8220
- AND $14::integer IS NOT NULL
8181
+ AND $16::bigint IS NOT NULL
8182
+ AND target._write_version = $16::bigint
8183
+ AND target._run_id = $6::text
8221
8184
  AND target._status IN ('failed', 'enriched')
8222
- AND coalesce(target._attempt_owner_run_id, target._run_id) = $12::text
8223
- AND COALESCE(target._attempt_seq, 0) = $14::integer
8185
+ AND updates.submitted_key = target._key
8224
8186
  )
8225
8187
  RETURNING target._key, prev._status AS prev_status
8226
8188
  ),
@@ -8317,8 +8279,7 @@ async function failRuntimeMapRowChunks(
8317
8279
  )
8318
8280
  SELECT
8319
8281
  (SELECT count(*)::int FROM applied_rows) AS updated,
8320
- coalesce((SELECT jsonb_agg(jsonb_build_object('key', _key, 'reason', reason, 'rowVersionStamp', row_write_version)) FROM fenced_rows), '[]'::jsonb) AS fenced_rows,
8321
- coalesce((SELECT array_agg(_key) FROM same_version_conflicts), '{}'::text[]) AS conflict_keys`,
8282
+ coalesce((SELECT jsonb_agg(jsonb_build_object('key', _key, 'reason', reason, 'rowVersionStamp', row_write_version)) FROM fenced_rows), '[]'::jsonb) AS fenced_rows`,
8322
8283
  [
8323
8284
  chunkKeys,
8324
8285
  chunkInputIndexes,
@@ -8340,12 +8301,10 @@ async function failRuntimeMapRowChunks(
8340
8301
  );
8341
8302
  const appliedCount = Number(rows[0]?.updated ?? 0);
8342
8303
  const chunkFencedRows = parseRuntimeMapRowsFencedRows(rows[0]?.fenced_rows);
8343
- const chunkConflictKeys = rows[0]?.conflict_keys ?? [];
8344
8304
  updated += appliedCount;
8345
8305
  fencedRows.push(...chunkFencedRows);
8346
- conflictKeys.push(...chunkConflictKeys);
8347
8306
  }
8348
- return { updated, fencedRows, conflictKeys };
8307
+ return { updated, fencedRows };
8349
8308
  }
8350
8309
 
8351
8310
  /**
@@ -8556,10 +8515,9 @@ export async function completeRuntimeMapRows(
8556
8515
  return {
8557
8516
  updated: updated.updated + inserted.inserted,
8558
8517
  fencedRows: updated.fencedRows,
8559
- conflictKeys: updated.conflictKeys,
8560
8518
  };
8561
8519
  })()
8562
- : { updated: 0, fencedRows: [], conflictKeys: [] };
8520
+ : { updated: 0, fencedRows: [] };
8563
8521
  const failed =
8564
8522
  failedRows.length > 0
8565
8523
  ? await failRuntimeMapRowChunks(client, session, {
@@ -8578,17 +8536,7 @@ export async function completeRuntimeMapRows(
8578
8536
  input.tableNamespace,
8579
8537
  ),
8580
8538
  })
8581
- : { updated: 0, fencedRows: [], conflictKeys: [] };
8582
- const conflictKeys = [...completed.conflictKeys, ...failed.conflictKeys];
8583
- if (conflictKeys.length > 0) {
8584
- throw new Error(
8585
- `Runtime Sheet received the same write version with a different terminal payload for row(s): ${[
8586
- ...new Set(conflictKeys),
8587
- ]
8588
- .slice(0, 10)
8589
- .join(', ')}.`,
8590
- );
8591
- }
8539
+ : { updated: 0, fencedRows: [] };
8592
8540
  return classifyRuntimeMapRowsWrite({
8593
8541
  submittedKeys: uniqueRows.keys(),
8594
8542
  updated: completed.updated + failed.updated,
@@ -0,0 +1,37 @@
1
+ /** Fixed-size terminal accounting retained even when detailed runtime telemetry is too large. */
2
+ export type RuntimeWorkCounters = {
3
+ logicalCalls: number;
4
+ providerRequests: number;
5
+ };
6
+
7
+ function record(value: unknown): Record<string, unknown> | null {
8
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
9
+ ? (value as Record<string, unknown>)
10
+ : null;
11
+ }
12
+
13
+ function count(value: unknown): number | null {
14
+ return Number.isSafeInteger(value) && (value as number) >= 0
15
+ ? (value as number)
16
+ : null;
17
+ }
18
+
19
+ export function runtimeWorkCountersFromResources(
20
+ resources: unknown,
21
+ ): RuntimeWorkCounters | null {
22
+ const root = record(resources);
23
+ const phases = record(root?.phases);
24
+ const logicalCalls = count(record(phases?.toolCallLogicalWall)?.count);
25
+ const providers = record(root?.providers);
26
+ if (logicalCalls === null || !providers) return null;
27
+
28
+ let providerRequests = 0;
29
+ for (const [key, value] of Object.entries(providers)) {
30
+ if (!key.startsWith('tool:')) continue;
31
+ const requests = count(record(record(value)?.residence)?.count);
32
+ if (requests === null) return null;
33
+ providerRequests += requests;
34
+ if (!Number.isSafeInteger(providerRequests)) return null;
35
+ }
36
+ return { logicalCalls, providerRequests };
37
+ }