@akasecurity/ai-tc-codex 0.9.11 → 0.9.12

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.
@@ -50,7 +50,7 @@ var require_ignore = __commonJS({
50
50
  var REGEX_SPLITALL_CRLF = /\r?\n/g;
51
51
  var REGEX_TEST_INVALID_PATH = /^\.{0,2}\/|^\.{1,2}$/;
52
52
  var REGEX_TEST_TRAILING_SLASH = /\/$/;
53
- var SLASH2 = "/";
53
+ var SLASH3 = "/";
54
54
  var TMP_KEY_IGNORE = "node-ignore";
55
55
  if (typeof Symbol !== "undefined") {
56
56
  TMP_KEY_IGNORE = /* @__PURE__ */ Symbol.for("node-ignore");
@@ -422,11 +422,11 @@ var require_ignore = __commonJS({
422
422
  if (!REGEX_TEST_TRAILING_SLASH.test(path)) {
423
423
  return this.test(path);
424
424
  }
425
- const slices = path.split(SLASH2).filter(Boolean);
425
+ const slices = path.split(SLASH3).filter(Boolean);
426
426
  slices.pop();
427
427
  if (slices.length) {
428
428
  const parent = this._t(
429
- slices.join(SLASH2) + SLASH2,
429
+ slices.join(SLASH3) + SLASH3,
430
430
  this._testCache,
431
431
  true,
432
432
  slices
@@ -442,14 +442,14 @@ var require_ignore = __commonJS({
442
442
  return cache[path];
443
443
  }
444
444
  if (!slices) {
445
- slices = path.split(SLASH2).filter(Boolean);
445
+ slices = path.split(SLASH3).filter(Boolean);
446
446
  }
447
447
  slices.pop();
448
448
  if (!slices.length) {
449
449
  return cache[path] = this._rules.test(path, checkUnignored, MODE_IGNORE);
450
450
  }
451
451
  const parent = this._t(
452
- slices.join(SLASH2) + SLASH2,
452
+ slices.join(SLASH3) + SLASH3,
453
453
  cache,
454
454
  checkUnignored,
455
455
  slices
@@ -492,11 +492,11 @@ var require_ignore = __commonJS({
492
492
  });
493
493
 
494
494
  // src/backfill.ts
495
- import { fileURLToPath as fileURLToPath4 } from "url";
495
+ import { fileURLToPath as fileURLToPath5 } from "url";
496
496
 
497
497
  // ../../packages/plugin-sdk/src/config.ts
498
498
  import { existsSync as existsSync8 } from "fs";
499
- import { join as join14 } from "path";
499
+ import { join as join16 } from "path";
500
500
 
501
501
  // ../../packages/persistence/src/attached-derived.ts
502
502
  import { rmSync } from "fs";
@@ -509,6 +509,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
509
509
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
510
510
  import { join as join2 } from "path";
511
511
 
512
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
513
+ var DEFERRED_MIGRATION_TAGS = [
514
+ "0031_audit_capture_by_time_index",
515
+ "0032_audit_capture_by_id_index",
516
+ "0033_audit_capture_location_index",
517
+ "0034_findings_read_indexes"
518
+ ];
519
+
512
520
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
513
521
  var SQLITE_MIGRATIONS = [
514
522
  {
@@ -626,6 +634,30 @@ var SQLITE_MIGRATIONS = [
626
634
  {
627
635
  tag: "0028_activity_session_probe_indexes",
628
636
  sql: "CREATE INDEX `idx_audit_session_prompt` ON `audit_events` (`root_session_id`) WHERE event_type = 'prompt';--> statement-breakpoint\nCREATE INDEX `idx_audit_session_share` ON `audit_events` (`root_session_id`) WHERE event_type = 'share';--> statement-breakpoint\nCREATE INDEX `idx_audit_ended_at` ON `audit_events` (`ended_at`,`root_session_id`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\nCREATE INDEX `idx_audit_session_ended` ON `audit_events` (`root_session_id`,`ended_at`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\n-- Expression index for the activity list's turns rollup: a live-captured\n-- session's turns are the DISTINCT `run_key` across its `llm_call` leaves, and\n-- carrying the extracted key in the index answers that count from the index\n-- alone instead of parsing every leaf's attribute bag. Written by hand, as\n-- 0013's `idx_audit_code_change_path` was: drizzle-kit cannot emit an\n-- expression containing a comma, so this index is not declared in sqlite.ts.\nCREATE INDEX `idx_audit_session_run_key` ON `audit_events` (`root_session_id`, json_extract(`attributes`, '$.run_key')) WHERE `event_type` = 'llm_call';\n"
637
+ },
638
+ {
639
+ tag: "0029_audit_capture_rollup_index",
640
+ sql: "CREATE INDEX `idx_audit_capture_rollup` ON `audit_events` (`event_type`,`started_at`,`repo`,`id`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
641
+ },
642
+ {
643
+ tag: "0030_audit_content_expiry",
644
+ sql: "ALTER TABLE `audit_events` ADD `content_expired_at` integer;--> statement-breakpoint\nCREATE INDEX `idx_audit_expirable_body` ON `audit_events` (`started_at`) WHERE content IS NOT NULL;"
645
+ },
646
+ {
647
+ tag: "0031_audit_capture_by_time_index",
648
+ sql: "CREATE INDEX `idx_audit_capture_by_time` ON `audit_events` (`started_at`,`id`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
649
+ },
650
+ {
651
+ tag: "0032_audit_capture_by_id_index",
652
+ sql: "CREATE INDEX `idx_audit_capture_by_id` ON `audit_events` (`id`,`started_at`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
653
+ },
654
+ {
655
+ tag: "0033_audit_capture_location_index",
656
+ sql: "CREATE INDEX `idx_audit_capture_location` ON `audit_events` (`repo`,`file_path`,`started_at`,`id`,`event_type`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
657
+ },
658
+ {
659
+ tag: "0034_findings_read_indexes",
660
+ sql: "CREATE INDEX `idx_inspection_definitions_rule` ON `inspection_definitions` (`rule_id`,`severity`,`category`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_def` ON `inspection_findings` (`inspection_definition_id`,`audit_event_id`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_event_cover` ON `inspection_findings` (`audit_event_id`,`inspection_definition_id`,`action_taken`,`finding_key`,`id`);"
629
661
  }
630
662
  ];
631
663
 
@@ -20626,7 +20658,7 @@ var TOOL_TO_HARNESS = {
20626
20658
  [SOURCE_TOOL.ClaudeAi]: HARNESS.ClaudeAi
20627
20659
  };
20628
20660
  function harnessFromTool(tool) {
20629
- return TOOL_TO_HARNESS[tool] ?? tool;
20661
+ return (Object.hasOwn(TOOL_TO_HARNESS, tool) ? TOOL_TO_HARNESS[tool] : void 0) ?? tool;
20630
20662
  }
20631
20663
 
20632
20664
  // ../../packages/schema/src/zod/finding.ts
@@ -20676,6 +20708,15 @@ var FindingCategory = external_exports.enum([
20676
20708
  ]).meta({ id: "FindingCategory" });
20677
20709
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20678
20710
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20711
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20712
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20713
+ var FindingDelivery = external_exports.object({
20714
+ state: FindingDeliveryState,
20715
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20716
+ at: external_exports.iso.datetime().optional(),
20717
+ // Only on `not_sent`, and only when a known reason was recorded.
20718
+ reason: SyncFailureReason.optional()
20719
+ }).meta({ id: "FindingDelivery" });
20679
20720
  var ResolutionMethod = external_exports.enum([
20680
20721
  "enforced-in-flight",
20681
20722
  "fixed-at-source",
@@ -20732,7 +20773,10 @@ var FindingInstance = external_exports.object({
20732
20773
  // The session that event belongs to, when it has one — the seam a
20733
20774
  // per-instance "view session" link needs. Absent for events captured
20734
20775
  // outside a session.
20735
- sessionId: external_exports.string().optional()
20776
+ sessionId: external_exports.string().optional(),
20777
+ // The delivery state of the event above (see FindingDelivery). Optional so
20778
+ // readers that do not project it stay valid.
20779
+ delivery: FindingDelivery.optional()
20736
20780
  }).meta({ id: "FindingInstance" });
20737
20781
  var FindingGroup = external_exports.object({
20738
20782
  id: external_exports.string(),
@@ -20784,7 +20828,10 @@ var FindingFacets = external_exports.object({
20784
20828
  // Host tool (attributes.tool_name). Present only on the instance-level
20785
20829
  // reads, which can filter by it; the type-level read omits the dimension
20786
20830
  // because a group spans tools.
20787
- tool: external_exports.array(FindingFacetItem).optional()
20831
+ tool: external_exports.array(FindingFacetItem).optional(),
20832
+ // Delivery states (FindingDeliveryState). Present only on the
20833
+ // instance-level reads, like `tool`.
20834
+ deployment: external_exports.array(FindingFacetItem).optional()
20788
20835
  }).meta({ id: "FindingFacets" });
20789
20836
  var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20790
20837
  id: "FindingTypeSummary"
@@ -20895,6 +20942,8 @@ var ListFindingInstancesQuery = external_exports.object({
20895
20942
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20896
20943
  // where the free-text `q` can only match the rendered "via Bash" label.
20897
20944
  tool: external_exports.array(external_exports.string()).optional(),
20945
+ // The delivery state of each finding's event (see FindingDelivery).
20946
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20898
20947
  // Exact repository / file-path matches, for the drill-down out of the
20899
20948
  // locations view. A row whose event carries no repo/file matches neither.
20900
20949
  repo: external_exports.string().optional(),
@@ -20915,6 +20964,10 @@ var ListFindingInstancesResponse = external_exports.object({
20915
20964
  items: external_exports.array(FindingInstanceDetail),
20916
20965
  nextCursor: external_exports.string().nullable()
20917
20966
  }).meta({ id: "ListFindingInstancesResponse" });
20967
+ var ListFindingInstancesPage = external_exports.object({
20968
+ items: external_exports.array(FindingInstanceDetail),
20969
+ nextCursor: external_exports.string().nullable()
20970
+ }).meta({ id: "ListFindingInstancesPage" });
20918
20971
  var FindingLocationSummary = external_exports.object({
20919
20972
  // Opaque, stable, minted from the pair by encodeLocationId. It exists
20920
20973
  // because a location's identity is two values and a URL param carries one:
@@ -20957,6 +21010,8 @@ var ListFindingLocationsQuery = external_exports.object({
20957
21010
  // instances that match, and folds its status from those.
20958
21011
  status: external_exports.array(FindingStatus).optional(),
20959
21012
  tool: external_exports.array(external_exports.string()).optional(),
21013
+ // The delivery state of each finding's event (see FindingDelivery).
21014
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20960
21015
  q: external_exports.string().optional(),
20961
21016
  sessionId: external_exports.string().optional(),
20962
21017
  from: external_exports.iso.datetime().optional(),
@@ -21159,6 +21214,10 @@ var CaptureAttributes = external_exports.object({
21159
21214
  // to 'allow' — the enforcement audit trail's link back to the grant that
21160
21215
  // authorized the bypass.
21161
21216
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21217
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21218
+ // join back to the `llm_call` leaf for the same assistant turn.
21219
+ message_id: external_exports.string().optional(),
21220
+ conversation_id: external_exports.string().optional(),
21162
21221
  // Whole milliseconds this capture's inspection blocked its caller — the
21163
21222
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21164
21223
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21167,7 +21226,19 @@ var CaptureAttributes = external_exports.object({
21167
21226
  // inline json_extract and is not itself an optimization.
21168
21227
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21169
21228
  // before the measurement shipped — never present as a placeholder 0.
21170
- inspection_ms: external_exports.number().int().nonnegative().optional()
21229
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21230
+ // What a `redact` this capture could not carry out became instead (see
21231
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21232
+ // degrade actually happened, so absence is the ordinary case rather than a
21233
+ // reader having to distinguish it from a zero.
21234
+ //
21235
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21236
+ // so on a multi-finding row this does not say which finding degraded, and
21237
+ // its presence does not mean the fallback decided the capture's action. A
21238
+ // capture denied by another finding's own Block policy carries `block`
21239
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21240
+ // repeated rather than referenced because a store reader opens this file.
21241
+ redact_degraded_to: ActionTaken.optional()
21171
21242
  }).catchall(external_exports.unknown());
21172
21243
  var ToolCallInspection = external_exports.object({
21173
21244
  ruleId: external_exports.string().min(1),
@@ -21366,7 +21437,17 @@ var AuditEvent = external_exports.object({
21366
21437
  /** `share` to a first-party/internal destination. */
21367
21438
  internal: external_exports.boolean(),
21368
21439
  /** Event needs review (e.g. unverified egress). */
21369
- flagged: external_exports.boolean()
21440
+ flagged: external_exports.boolean(),
21441
+ /**
21442
+ * The body this event's `title` is drawn from was cleared by local body
21443
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21444
+ *
21445
+ * A separate flag rather than a sentinel written into `title`: the title is
21446
+ * rendered text, and a store-layer module that invented display copy for it
21447
+ * would be choosing words the view is supposed to choose. Additive and
21448
+ * defaulted, so an older producer still validates.
21449
+ */
21450
+ bodyExpired: external_exports.boolean().default(false)
21370
21451
  }).meta({ id: "ActivityAuditEvent" });
21371
21452
  var ActivitySessionSummary = external_exports.object({
21372
21453
  id: external_exports.string(),
@@ -22707,6 +22788,12 @@ var EventMetadata = external_exports.object({
22707
22788
  // to 'allow' — the enforcement audit trail's link back to the grant that
22708
22789
  // authorized the bypass. Absent on captures where no exception applied.
22709
22790
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22791
+ // The assistant message this capture belongs to, and the conversation it sits
22792
+ // in — set by the browser extension's network capture so a stored `response`
22793
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22794
+ // on every other capture path, which has no such id.
22795
+ messageId: external_exports.string().optional(),
22796
+ conversationId: external_exports.string().optional(),
22710
22797
  // How long THIS capture's inspection blocked its caller, in whole
22711
22798
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22712
22799
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22719,7 +22806,37 @@ var EventMetadata = external_exports.object({
22719
22806
  // Absent is also what every pre-measurement client writes, and what a
22720
22807
  // clock failure degrades to — a reader must treat absence as "not measured"
22721
22808
  // and never as a zero, which would read as "inspection is free".
22722
- inspectionMs: external_exports.number().int().nonnegative().optional()
22809
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22810
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22811
+ // workspace's `redactFallback`, applied because the field could not be
22812
+ // masked in place (a shell command, a URL, or any argument on a host whose
22813
+ // hook contract offers no rewrite channel).
22814
+ //
22815
+ // It exists because the action alone cannot say why. A finding recorded as
22816
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22817
+ // assigned Redact on a field that could not take one — and those are
22818
+ // different facts about the same row: the first is a policy the user chose,
22819
+ // the second is a masking the host could not perform. Absent means no
22820
+ // degrade happened, which is every ordinary capture.
22821
+ //
22822
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22823
+ // is the CAPTURE while `actionTaken` is per FINDING:
22824
+ //
22825
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22826
+ // `redact` alongside a finding ASSIGNED the same action stores both
22827
+ // identically and one reason for the pair; attributing it to both
22828
+ // describes the assigned one wrongly, and to neither loses the degrade.
22829
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22830
+ // became, not the reason the capture ended as it did — a capture denied
22831
+ // by some other finding's own Block policy still carries `block` here,
22832
+ // and clearing the workspace's fallback would not have let it through.
22833
+ // Gate on the value against what a fallback can produce; never read the
22834
+ // field's presence as "this was the fallback's doing".
22835
+ //
22836
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22837
+ // Closing either means moving the reason onto the finding row, which
22838
+ // already carries its own action.
22839
+ redactDegradedTo: ActionTaken.optional()
22723
22840
  }).meta({ id: "EventMetadata" });
22724
22841
  var Event = external_exports.object({
22725
22842
  id: external_exports.guid(),
@@ -22829,7 +22946,32 @@ var RotateKeyInput = external_exports.object({
22829
22946
  confirmation: external_exports.string()
22830
22947
  });
22831
22948
 
22949
+ // ../../packages/schema/src/zod/finding-delivery.ts
22950
+ var KNOWN_REASONS = SyncFailureReason.options;
22951
+ function knownReason(value) {
22952
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22953
+ }
22954
+ function deriveFindingDelivery(row) {
22955
+ if (row.kind === "code_change") return { state: "local_scan" };
22956
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22957
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22958
+ }
22959
+ if (row.syncedAt !== null) {
22960
+ const reason = knownReason(row.syncFailure);
22961
+ return {
22962
+ state: "not_sent",
22963
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22964
+ ...reason === void 0 ? {} : { reason }
22965
+ };
22966
+ }
22967
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22968
+ return { state: "never_offered" };
22969
+ }
22970
+
22832
22971
  // ../../packages/schema/src/zod/findings-group-build.ts
22972
+ function lookupOwn(map2, key) {
22973
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22974
+ }
22833
22975
  function toApiAction(dbVal) {
22834
22976
  const map2 = {
22835
22977
  log: "monitored",
@@ -22838,7 +22980,7 @@ function toApiAction(dbVal) {
22838
22980
  warn: "warned",
22839
22981
  allow: "allowed"
22840
22982
  };
22841
- return map2[dbVal] ?? "allowed";
22983
+ return lookupOwn(map2, dbVal) ?? "allowed";
22842
22984
  }
22843
22985
  function toApiCategory(dbVal) {
22844
22986
  if (dbVal === "code_context") return "source_code";
@@ -22846,13 +22988,18 @@ function toApiCategory(dbVal) {
22846
22988
  return parsed2.success ? parsed2.data : "custom";
22847
22989
  }
22848
22990
  function toApiProvider(sourceTool) {
22849
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22991
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22850
22992
  }
22851
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22993
+ var FINDING_STATUS_PRECEDENCE = [
22994
+ "open",
22995
+ "handled",
22996
+ "dismissed",
22997
+ "resolved"
22998
+ ];
22852
22999
  function foldGroupStatus(instanceStatuses) {
22853
23000
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22854
23001
  if (statuses.size === 0) return void 0;
22855
- for (const candidate of STATUS_PRECEDENCE) {
23002
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22856
23003
  if (statuses.has(candidate)) return candidate;
22857
23004
  }
22858
23005
  return void 0;
@@ -22959,11 +23106,16 @@ function applyFindingFilters(types, opts) {
22959
23106
  }
22960
23107
  return filtered;
22961
23108
  }
22962
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22963
- var SEVERITY_RANK = SEVERITY_ORDER;
23109
+ function rankByOrder(members2) {
23110
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23111
+ }
23112
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23113
+ function severityRank(severity) {
23114
+ return lookupOwn(SEVERITY_RANK, severity);
23115
+ }
22964
23116
  function compareFindingGroupOrder(a, b) {
22965
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22966
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23117
+ const rankA = severityRank(a.severity) ?? -1;
23118
+ const rankB = severityRank(b.severity) ?? -1;
22967
23119
  const severityDiff = rankA - rankB;
22968
23120
  if (severityDiff !== 0) return severityDiff;
22969
23121
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
@@ -23038,6 +23190,20 @@ function computeFindingFacets(allTypes, opts) {
23038
23190
  }
23039
23191
 
23040
23192
  // ../../packages/schema/src/zod/findings-flat-build.ts
23193
+ function compareCodePoints(a, b) {
23194
+ const aIter = a[Symbol.iterator]();
23195
+ const bIter = b[Symbol.iterator]();
23196
+ for (; ; ) {
23197
+ const aNext = aIter.next();
23198
+ const bNext = bIter.next();
23199
+ if (aNext.done && bNext.done) return 0;
23200
+ if (aNext.done) return -1;
23201
+ if (bNext.done) return 1;
23202
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23203
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23204
+ if (aPoint !== bPoint) return aPoint - bPoint;
23205
+ }
23206
+ }
23041
23207
  function rowHaystack(row) {
23042
23208
  return [
23043
23209
  row.ruleId,
@@ -23062,6 +23228,8 @@ function matchesDimension(row, opts, dimension) {
23062
23228
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23063
23229
  case "statuses":
23064
23230
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23231
+ case "deliveries":
23232
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23065
23233
  case "tools":
23066
23234
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23067
23235
  // An EMPTY value is a real filter here, not an absent one. The location
@@ -23088,6 +23256,7 @@ var DIMENSIONS = [
23088
23256
  "providers",
23089
23257
  "actions",
23090
23258
  "statuses",
23259
+ "deliveries",
23091
23260
  "tools",
23092
23261
  "repo",
23093
23262
  "file",
@@ -23101,10 +23270,19 @@ function matchesInstanceFilters(row, opts, except) {
23101
23270
  return true;
23102
23271
  }
23103
23272
  function toItems(counts) {
23104
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23273
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23274
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23275
+ // NFD spelling of the same text) as equal, so a count tie between
23276
+ // them would otherwise be ordered by whichever the Map iteration
23277
+ // produced. compareCodePoints breaks that tie deterministically, which
23278
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23279
+ // which it need not: foldFacetTuples runs this same sort over grouped
23280
+ // tuples, so both paths order facets identically by construction.
23281
+ compareCodePoints(a.value, b.value)
23282
+ );
23105
23283
  }
23106
- function bump(counts, value) {
23107
- counts.set(value, (counts.get(value) ?? 0) + 1);
23284
+ function bump(counts, value, by = 1) {
23285
+ counts.set(value, (counts.get(value) ?? 0) + by);
23108
23286
  }
23109
23287
  function createInstanceFacetAccumulator(opts) {
23110
23288
  const severity = /* @__PURE__ */ new Map();
@@ -23113,6 +23291,7 @@ function createInstanceFacetAccumulator(opts) {
23113
23291
  const action = /* @__PURE__ */ new Map();
23114
23292
  const status = /* @__PURE__ */ new Map();
23115
23293
  const tool = /* @__PURE__ */ new Map();
23294
+ const deployment = /* @__PURE__ */ new Map();
23116
23295
  return {
23117
23296
  add(row) {
23118
23297
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23127,6 +23306,9 @@ function createInstanceFacetAccumulator(opts) {
23127
23306
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23128
23307
  bump(tool, row.toolName);
23129
23308
  }
23309
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23310
+ bump(deployment, row.delivery.state);
23311
+ }
23130
23312
  },
23131
23313
  facets: () => ({
23132
23314
  severity: toItems(severity),
@@ -23134,7 +23316,8 @@ function createInstanceFacetAccumulator(opts) {
23134
23316
  provider: toItems(provider),
23135
23317
  action: toItems(action),
23136
23318
  status: toItems(status),
23137
- tool: toItems(tool)
23319
+ tool: toItems(tool),
23320
+ deployment: toItems(deployment)
23138
23321
  })
23139
23322
  };
23140
23323
  }
@@ -23148,6 +23331,7 @@ function toInstanceDetail(row) {
23148
23331
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23149
23332
  eventId: row.eventId,
23150
23333
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23334
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23151
23335
  ...row.user === void 0 ? {} : { user: row.user },
23152
23336
  action: toApiAction(row.actionTaken),
23153
23337
  detectedAt: row.occurredAt,
@@ -23162,12 +23346,6 @@ function toInstanceDetail(row) {
23162
23346
  policy: { id: `category:${category}`, name: category }
23163
23347
  };
23164
23348
  }
23165
- var SEVERITY_ORDER2 = {
23166
- critical: 0,
23167
- high: 1,
23168
- medium: 2,
23169
- low: 3
23170
- };
23171
23349
  function newLocationAccumulator() {
23172
23350
  return {
23173
23351
  instanceCount: 0,
@@ -23182,7 +23360,7 @@ function newLocationAccumulator() {
23182
23360
  }
23183
23361
  function addToLocation(acc, row) {
23184
23362
  acc.instanceCount += 1;
23185
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23363
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23186
23364
  if (rank < acc.maxSeverityRank) {
23187
23365
  acc.maxSeverityRank = rank;
23188
23366
  acc.maxSeverity = row.severity;
@@ -23192,15 +23370,15 @@ function addToLocation(acc, row) {
23192
23370
  acc.ruleIds.add(row.ruleId);
23193
23371
  }
23194
23372
  function compareLocationOrder(a, b) {
23195
- const rankA = SEVERITY_ORDER2[a.maxSeverity] ?? -1;
23196
- const rankB = SEVERITY_ORDER2[b.maxSeverity] ?? -1;
23373
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23374
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23197
23375
  if (rankA !== rankB) return rankA - rankB;
23198
23376
  if (a.latestDetectedAt !== b.latestDetectedAt) {
23199
23377
  return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23200
23378
  }
23201
- if (a.repo !== b.repo) return a.repo < b.repo ? -1 : 1;
23202
- if (a.file !== b.file) return a.file < b.file ? -1 : 1;
23203
- return 0;
23379
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23380
+ if (repoDiff !== 0) return repoDiff;
23381
+ return compareCodePoints(a.file, b.file);
23204
23382
  }
23205
23383
  function encodeLocationId(repo, file2) {
23206
23384
  return `${encodePart(repo)}/${encodePart(file2)}`;
@@ -23275,6 +23453,11 @@ var Policy = external_exports.object({
23275
23453
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23276
23454
  provenance: PolicyProvenance.optional()
23277
23455
  }).meta({ id: "Policy" });
23456
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23457
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23458
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23459
+ id: "RedactFallback"
23460
+ });
23278
23461
  var PolicyBundle = external_exports.object({
23279
23462
  version: external_exports.string(),
23280
23463
  policies: external_exports.array(Policy),
@@ -23322,6 +23505,16 @@ var PolicyBundle = external_exports.object({
23322
23505
  // control plane), so no name resolution stands between the decision and the
23323
23506
  // comparison.
23324
23507
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23508
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23509
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23510
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23511
+ // a control plane can tighten a machine and never loosen one — the same
23512
+ // direction `mergeRaiseOnly` enforces for policies.
23513
+ //
23514
+ // Optional so an older backend, and an older on-disk cache, still parses;
23515
+ // absent leaves the device's own setting in force, which is the behaviour
23516
+ // that predates the field and the safe direction to default.
23517
+ redactFallback: RedactFallback.optional(),
23325
23518
  customKeywords: external_exports.array(external_exports.string()),
23326
23519
  fetchedAt: external_exports.iso.datetime()
23327
23520
  }).meta({ id: "PolicyBundle" });
@@ -23351,11 +23544,6 @@ function severityFloorPolicy(category) {
23351
23544
  const peak = CATEGORY_PEAK_SEVERITY[category];
23352
23545
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23353
23546
  }
23354
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23355
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23356
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23357
- id: "RedactFallback"
23358
- });
23359
23547
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23360
23548
  var BUILTIN_POLICY_SPECS = {
23361
23549
  monitor: {
@@ -23411,6 +23599,11 @@ function isActionAtLeast(action, floor) {
23411
23599
  function strongerAction(a, b) {
23412
23600
  return actionRank(a) >= actionRank(b) ? a : b;
23413
23601
  }
23602
+ function strongerRedactFallback(local, remote) {
23603
+ if (remote === void 0) return local;
23604
+ const localAction = builtinPolicyToAction(local);
23605
+ return localAction === strongerAction(localAction, builtinPolicyToAction(remote)) ? local : remote;
23606
+ }
23414
23607
  function weakestBuiltinAtLeast(floor) {
23415
23608
  return KNOWN_BUILTIN_IDS.find((id) => isActionAtLeast(builtinPolicyToAction(id), floor)) ?? "block";
23416
23609
  }
@@ -23651,7 +23844,7 @@ var VaultConsent = external_exports.object({
23651
23844
  });
23652
23845
 
23653
23846
  // ../../packages/schema/src/zod/local.ts
23654
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23847
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23655
23848
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23656
23849
  var RunMode = external_exports.enum(["standalone", "attached"]);
23657
23850
  var ControlPlaneConnection = external_exports.object({
@@ -23671,6 +23864,15 @@ var HistorySyncConsent = external_exports.object({
23671
23864
  payloadVersion: external_exports.number().int().positive(),
23672
23865
  endpoint: external_exports.string()
23673
23866
  });
23867
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23868
+ var BodyRetention = external_exports.object({
23869
+ enabled: external_exports.boolean().default(false),
23870
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23871
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23872
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23873
+ // candidate set that is already bounded by "delivered, or never owed".
23874
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23875
+ }).meta({ id: "BodyRetention" });
23674
23876
  var WorkspaceSettings = external_exports.object({
23675
23877
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23676
23878
  runMode: RunMode.default("standalone"),
@@ -23719,7 +23921,13 @@ var WorkspaceSettings = external_exports.object({
23719
23921
  // carry prompt/reply/tool-output text in `content`; the key name predates
23720
23922
  // both widenings. Absent until granted, and a grant for a different endpoint
23721
23923
  // or an older payload no longer counts.
23722
- historySyncConsent: HistorySyncConsent.optional()
23924
+ historySyncConsent: HistorySyncConsent.optional(),
23925
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23926
+ // body never removes the row or its findings.
23927
+ bodyRetention: BodyRetention.default({
23928
+ enabled: false,
23929
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23930
+ })
23723
23931
  });
23724
23932
  function defaultWorkspaceSettings() {
23725
23933
  return WorkspaceSettings.parse({});
@@ -23814,12 +24022,15 @@ function toCaptureAttributes(event) {
23814
24022
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23815
24023
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23816
24024
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24025
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23817
24026
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23818
24027
  // has ever populated either), but every legacy metadata key still rides
23819
24028
  // the bag rather than being silently dropped — CaptureAttributes'
23820
24029
  // `.catchall(z.unknown())` carries the long tail.
23821
24030
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23822
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24031
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24032
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24033
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23823
24034
  };
23824
24035
  }
23825
24036
  function captureDefinitionVersion(finding) {
@@ -23847,13 +24058,22 @@ var ManagedSettingKey = external_exports.enum([
23847
24058
  "vaultInlineReveal",
23848
24059
  "modelJudgeConsent",
23849
24060
  "dataSharesInPlace",
23850
- "redactFallback"
24061
+ "redactFallback",
24062
+ // Pins the toggle and the day count together — see BodyRetention on why the
24063
+ // two are one unit. An administrator mandating a window wants the count
24064
+ // enforced with it, not one a user can widen while the toggle stays on.
24065
+ "bodyRetention"
23851
24066
  ]).meta({ id: "ManagedSettingKey" });
23852
24067
  function isManagedSettingKey(value) {
23853
24068
  return ManagedSettingKey.safeParse(value).success;
23854
24069
  }
23855
24070
  var ManagedSettingsValues = external_exports.object({
23856
24071
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24072
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24073
+ // plain, non-strict objects: a key under either that this build does not know
24074
+ // is stripped and nothing reports it. The unknown-value split in
24075
+ // ManagedSettings below classifies top-level names only, so it stops at
24076
+ // these boundaries.
23857
24077
  controlPlane: external_exports.object({
23858
24078
  endpoint: external_exports.string().min(1),
23859
24079
  label: external_exports.string().min(1).optional()
@@ -23864,7 +24084,8 @@ var ManagedSettingsValues = external_exports.object({
23864
24084
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23865
24085
  modelJudgeConsent: external_exports.boolean().optional(),
23866
24086
  dataSharesInPlace: external_exports.boolean().optional(),
23867
- redactFallback: RedactFallback.optional()
24087
+ redactFallback: RedactFallback.optional(),
24088
+ bodyRetention: BodyRetention.optional()
23868
24089
  }).meta({ id: "ManagedSettingsValues" });
23869
24090
  var ManagedSettings = external_exports.object({
23870
24091
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23872,7 +24093,21 @@ var ManagedSettings = external_exports.object({
23872
24093
  // decision from a bug. Absent renders as a generic "your organization".
23873
24094
  organization: external_exports.string().min(1).optional(),
23874
24095
  // What the administrator pinned.
23875
- values: ManagedSettingsValues.default({}),
24096
+ //
24097
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24098
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24099
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24100
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24101
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24102
+ // exactly the file an administrator is most likely to write while a fleet
24103
+ // is mid-upgrade.
24104
+ //
24105
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24106
+ // file, which is the outcome the lock half already rejected — an older
24107
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24108
+ // value still fails, because the nested schema is re-run over the known
24109
+ // subset and its issues are re-raised on this parse.
24110
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23876
24111
  // Which of those the user may not change. A key here with no matching value
23877
24112
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23878
24113
  // the user may still override. The two are separable on purpose.
@@ -23885,17 +24120,31 @@ var ManagedSettings = external_exports.object({
23885
24120
  // the fleets most likely to carry a version skew. A name outside the enum
23886
24121
  // is still never HONOURED: the lockable set stays explicit above.
23887
24122
  lockedFields: external_exports.array(external_exports.string()).default([])
23888
- }).transform(({ lockedFields, ...rest }) => {
24123
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
23889
24124
  const known = [];
23890
24125
  const unknown2 = [];
23891
24126
  for (const name of lockedFields) {
23892
24127
  if (isManagedSettingKey(name)) known.push(name);
23893
24128
  else unknown2.push(name);
23894
24129
  }
24130
+ const knownValues = /* @__PURE__ */ Object.create(null);
24131
+ const unknownValues = [];
24132
+ for (const [name, value] of Object.entries(values)) {
24133
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24134
+ else unknownValues.push(name);
24135
+ }
24136
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24137
+ if (!pinned.success) {
24138
+ for (const issue2 of pinned.error.issues)
24139
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24140
+ return external_exports.NEVER;
24141
+ }
23895
24142
  return {
23896
24143
  ...rest,
24144
+ values: pinned.data,
23897
24145
  lockedFields: known,
23898
- ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {}
24146
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24147
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
23899
24148
  };
23900
24149
  }).meta({ id: "ManagedSettings" });
23901
24150
 
@@ -24159,7 +24408,23 @@ var SaveSettingsInput = external_exports.object({
24159
24408
  modelJudgeConsent: ModelJudgeConsentChoice,
24160
24409
  historySyncConsent: HistorySyncConsentChoice,
24161
24410
  vaultConsent: external_exports.string(),
24162
- vaultInlineReveal: external_exports.string()
24411
+ vaultInlineReveal: external_exports.string(),
24412
+ // Widened to `string` like its neighbours rather than typed as
24413
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24414
+ // the call site, so the domain check receives the type it was written for.
24415
+ //
24416
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24417
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24418
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24419
+ // trade against. The real cost runs the other way and is the part worth
24420
+ // knowing: a value this schema admits and the domain enum then rejects lands
24421
+ // on the action's shared refusal, which names NO field, where a shape
24422
+ // rejection reaches `malformedInput` and names the schema key.
24423
+ redactFallback: external_exports.string(),
24424
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24425
+ // `BodyRetention`'s and the action checks it there, so there is one place
24426
+ // that decides what a legal horizon is rather than two that can drift.
24427
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24163
24428
  });
24164
24429
  var AttachInput = external_exports.object({
24165
24430
  endpoint: external_exports.string(),
@@ -24331,6 +24596,52 @@ function reviewSeverityRank(reasons) {
24331
24596
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24332
24597
  }
24333
24598
 
24599
+ // ../../packages/schema/src/zod/web-capture.ts
24600
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24601
+ var WebUsage = external_exports.object({
24602
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24603
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24604
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24605
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24606
+ });
24607
+ var WebToolCall = external_exports.object({
24608
+ toolUseId: external_exports.string().min(1),
24609
+ toolName: external_exports.string().min(1),
24610
+ target: external_exports.string().optional(),
24611
+ isError: external_exports.boolean().optional(),
24612
+ inputSize: external_exports.number().int().nonnegative().optional(),
24613
+ outputSize: external_exports.number().int().nonnegative().optional()
24614
+ });
24615
+ var WebExchange = external_exports.object({
24616
+ messageId: external_exports.string().min(1),
24617
+ startedAt: external_exports.iso.datetime(),
24618
+ model: external_exports.string().optional(),
24619
+ usage: WebUsage.optional(),
24620
+ usageSource: WebUsageSource,
24621
+ stopReason: external_exports.string().optional(),
24622
+ conversationId: external_exports.string().optional(),
24623
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24624
+ toolCalls: external_exports.array(WebToolCall).default([]),
24625
+ // Absent when the adapter recovered no text. Capped by the caller at
24626
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24627
+ // short capture is never mistaken for a short reply.
24628
+ responseText: external_exports.string().optional(),
24629
+ truncated: external_exports.boolean().default(false)
24630
+ });
24631
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24632
+ var WebCaptureStatus = external_exports.object({
24633
+ patched: external_exports.boolean(),
24634
+ live: external_exports.boolean(),
24635
+ blind: external_exports.boolean(),
24636
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24637
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24638
+ parseFailures: external_exports.number().int().nonnegative(),
24639
+ unparsedBodies: external_exports.number().int().nonnegative(),
24640
+ // The adapter-declared JSON key paths that were absent from a real payload —
24641
+ // the earliest signal that a site's contract moved.
24642
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24643
+ });
24644
+
24334
24645
  // ../../packages/persistence/src/paths.ts
24335
24646
  import {
24336
24647
  chmodSync,
@@ -24691,6 +25002,22 @@ function discardStore(file2, backup) {
24691
25002
  }
24692
25003
  }
24693
25004
 
25005
+ // ../../packages/persistence/src/internal/sql-functions.ts
25006
+ var utf8 = new TextDecoder();
25007
+ function akaLower(value) {
25008
+ if (value === null) return null;
25009
+ if (typeof value === "string") return value.toLowerCase();
25010
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
25011
+ return utf8.decode(value).toLowerCase();
25012
+ }
25013
+ function registerSqlFunctions(db) {
25014
+ db.function(
25015
+ "aka_lower",
25016
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
25017
+ akaLower
25018
+ );
25019
+ }
25020
+
24694
25021
  // ../../packages/persistence/src/internal/sql-text.ts
24695
25022
  function escapeLikePattern(s) {
24696
25023
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24775,6 +25102,11 @@ function schemaObjectExists(db, kind, name) {
24775
25102
  function indexExists(db, name) {
24776
25103
  return schemaObjectExists(db, "index", name);
24777
25104
  }
25105
+ function indexColumns(db, name) {
25106
+ if (!indexExists(db, name)) return [];
25107
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25108
+ return columns.map((c) => c.name).filter((c) => c !== null);
25109
+ }
24778
25110
  function columnNames(db, table, opts) {
24779
25111
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24780
25112
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).all();
@@ -24836,175 +25168,817 @@ function mapRowsTolerant(rows, map2) {
24836
25168
  return out;
24837
25169
  }
24838
25170
 
24839
- // ../../packages/persistence/src/migrations.ts
24840
- function describeObject(object2) {
24841
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24842
- }
24843
- function splitStatements(sql) {
24844
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24845
- }
24846
- function createdIndexName(statement) {
24847
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24848
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25171
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25172
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25173
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25174
+
25175
+ // ../../packages/persistence/src/sync-failure.ts
25176
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25177
+ function syncFailureRejectCondition(column = "sync_failure") {
25178
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25179
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24849
25180
  }
24850
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24851
- function applyMigrations(db, file2) {
24852
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24853
- db.exec(
24854
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24855
- );
24856
- const applied = new Set(
24857
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24858
- );
24859
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24860
- const record2 = db.prepare(
24861
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24862
- );
24863
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24864
- if (applied.has(migration.tag)) continue;
24865
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24866
- const evidence = evidenceObjects(migration.sql);
24867
- const present = evidence.filter((o) => evidenceExists(db, o));
24868
- if (present.length > 0 && present.length < evidence.length) {
24869
- const missing = evidence.filter((o) => !present.includes(o));
24870
- const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
24871
- akaWarn(message);
24872
- throw new Error(`[aka] ${message}`);
24873
- }
24874
- const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
24875
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24876
- const statements = splitStatements(migration.sql);
24877
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24878
- try {
24879
- withTransaction(
24880
- db,
24881
- () => {
24882
- for (const statement of statements) {
24883
- const indexName = createdIndexName(statement);
24884
- if (indexName === void 0) {
24885
- if (alreadyApplied) continue;
24886
- } else if (indexExists(db, indexName)) {
24887
- continue;
24888
- }
24889
- db.exec(statement);
24890
- }
24891
- if (wantsFkOff && !alreadyApplied) {
24892
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24893
- if (violations.length > 0) {
24894
- throw new Error(
24895
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24896
- );
24897
- }
24898
- }
24899
- record2.run(migration.tag, Date.now());
24900
- },
24901
- "IMMEDIATE"
24902
- );
24903
- } finally {
24904
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24905
- }
25181
+
25182
+ // ../../packages/persistence/src/repositories/history-sync.ts
25183
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25184
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25185
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25186
+ var COUNTED_EVENT_TYPES = [
25187
+ ...STRUCTURAL_EVENT_TYPES,
25188
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25189
+ ];
25190
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25191
+ var PARTITION_BUCKETS = `
25192
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25193
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25194
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25195
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25196
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25197
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25198
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25199
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25200
+ -- added later lands in no bucket and fails the sum assertion, instead
25201
+ -- of silently joining this one.
25202
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25203
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25204
+ THEN 1 ELSE 0 END) AS failed,
25205
+ COUNT(*) AS total`;
25206
+ var COUNTED_SCOPE = `
25207
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25208
+ AND (
25209
+ event_type IN (${TYPE_LIST})
25210
+ OR synced_at IS NOT NULL
25211
+ OR outbox_owed = 1
25212
+ )`;
25213
+ var SKIPPED = -1;
25214
+ var ROW_COLUMNS = `id,
25215
+ parent_id AS parentId,
25216
+ root_session_id AS rootSessionId,
25217
+ event_type AS eventType,
25218
+ host_id AS hostId,
25219
+ harness_id AS harnessId,
25220
+ source_project_id AS sourceProjectId,
25221
+ started_at AS startedAt,
25222
+ ended_at AS endedAt,
25223
+ severity,
25224
+ priority,
25225
+ content,
25226
+ content_hash AS contentHash,
25227
+ attributes`;
25228
+ var SqliteHistorySyncRepository = class {
25229
+ constructor(db) {
25230
+ this.db = db;
25231
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25232
+ this.sessionsStmt = db.prepare(
25233
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25234
+ FROM audit_events
25235
+ WHERE synced_at IS NULL
25236
+ AND event_type IN (${TYPE_LIST})
25237
+ AND started_at < :before
25238
+ GROUP BY sessionId
25239
+ ORDER BY earliest
25240
+ LIMIT :limit`
25241
+ );
25242
+ this.rowsStmt = db.prepare(
25243
+ `SELECT ${ROW_COLUMNS}
25244
+ FROM audit_events
25245
+ WHERE synced_at IS NULL
25246
+ AND event_type IN (${TYPE_LIST})
25247
+ AND started_at < :before
25248
+ AND COALESCE(root_session_id, id) = :sessionId
25249
+ ORDER BY (event_type = 'session') DESC, started_at
25250
+ LIMIT :limit`
25251
+ );
25252
+ this.captureRowsStmt = db.prepare(
25253
+ `SELECT ${ROW_COLUMNS}
25254
+ FROM audit_events
25255
+ WHERE synced_at IS NULL
25256
+ AND sync_claimed_at IS NULL
25257
+ AND outbox_owed = 1
25258
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25259
+ AND started_at < :before
25260
+ ORDER BY started_at
25261
+ LIMIT :limit`
25262
+ );
25263
+ this.markOwedStmt = db.prepare(
25264
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25265
+ );
25266
+ this.markCaptureBacklogOwedStmt = db.prepare(
25267
+ `UPDATE audit_events SET outbox_owed = 1
25268
+ WHERE synced_at IS NULL
25269
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25270
+ AND started_at < :before`
25271
+ );
25272
+ this.stampStmt = db.prepare(
25273
+ `UPDATE audit_events
25274
+ SET synced_at = :at,
25275
+ sync_claimed_at = NULL,
25276
+ sync_failed_at = :failedAt,
25277
+ sync_failure = :failure
25278
+ WHERE id = :id`
25279
+ );
25280
+ this.claimRowStmt = db.prepare(
25281
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25282
+ );
25283
+ this.releaseRowStmt = db.prepare(
25284
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25285
+ );
25286
+ this.releaseStaleClaimsStmt = db.prepare(
25287
+ `UPDATE audit_events SET sync_claimed_at = NULL
25288
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25289
+ );
25290
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25291
+ FROM audit_events${COUNTED_SCOPE}`);
25292
+ this.partitionByKindStmt = db.prepare(
25293
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25294
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25295
+ GROUP BY event_type`
25296
+ );
25297
+ this.countsStmt = db.prepare(
25298
+ `SELECT
25299
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25300
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25301
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25302
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25303
+ THEN 1 ELSE 0 END) AS skipped,
25304
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25305
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25306
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25307
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25308
+ FROM audit_events
25309
+ WHERE event_type IN (${TYPE_LIST})`
25310
+ );
25311
+ this.captureSkipCountStmt = db.prepare(
25312
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25313
+ // way the structural totals are. The split exists because a refusal is
25314
+ // terminal only against the deployment that gave it, and the structural
25315
+ // re-arm frees it on a change of deployment. The capture lane has no such
25316
+ // escape: re-arming a capture would offer one deployment's undelivered
25317
+ // prompts, with their text, to a deployment that never saw them, which is
25318
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25319
+ // reasons mean the same thing — this row will not be sent — and splitting
25320
+ // them would put refused captures in a bucket nothing reads and nothing
25321
+ // frees.
25322
+ `SELECT COUNT(*) AS skipped
25323
+ FROM audit_events
25324
+ WHERE synced_at = ${String(SKIPPED)}
25325
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25326
+ );
25327
+ this.fingerprintStmt = db.prepare(
25328
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25329
+ FROM history_sync WHERE id = 1`
25330
+ );
25331
+ this.setFingerprintStmt = db.prepare(
25332
+ `UPDATE history_sync
25333
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25334
+ WHERE id = 1`
25335
+ );
25336
+ this.disownCapturesStmt = db.prepare(
25337
+ `UPDATE audit_events SET outbox_owed = NULL
25338
+ WHERE outbox_owed IS NOT NULL
25339
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25340
+ AND started_at < :attachedAt`
25341
+ );
25342
+ this.rearmStmt = db.prepare(
25343
+ `UPDATE audit_events
25344
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25345
+ WHERE (synced_at > 0
25346
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25347
+ AND event_type IN (${TYPE_LIST})`
25348
+ );
25349
+ this.claimStmt = db.prepare(
25350
+ `UPDATE history_sync
25351
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25352
+ WHERE id = 1
25353
+ AND (owner_pid IS NULL
25354
+ OR heartbeat_at IS NULL
25355
+ OR heartbeat_at < :staleBefore
25356
+ OR heartbeat_at > :now)`
25357
+ );
25358
+ this.heartbeatStmt = db.prepare(
25359
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25360
+ );
25361
+ this.releaseStmt = db.prepare(
25362
+ `UPDATE history_sync
25363
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25364
+ WHERE id = 1 AND owner_pid = :pid`
25365
+ );
25366
+ this.closeWindowStmt = db.prepare(
25367
+ `UPDATE audit_events
25368
+ SET synced_at = ${String(SKIPPED)},
25369
+ sync_failed_at = :at,
25370
+ sync_failure = 'detached_undelivered'
25371
+ WHERE synced_at IS NULL
25372
+ AND event_type IN (${TYPE_LIST})
25373
+ AND started_at >= :attachedAt`
25374
+ );
25375
+ this.releaseBoundaryStmt = db.prepare(
25376
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25377
+ );
25378
+ this.freezeBoundaryStmt = db.prepare(
25379
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25380
+ );
25381
+ this.leaseStmt = db.prepare(
25382
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25383
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25384
+ FROM history_sync WHERE id = 1`
25385
+ );
25386
+ this.inspectionsStmt = db.prepare(
25387
+ `SELECT d.rule_id AS ruleId,
25388
+ d.name AS ruleName,
25389
+ d.version AS ruleVersion,
25390
+ d.category AS category,
25391
+ d.severity AS severity,
25392
+ f.span_start AS spanStart,
25393
+ f.span_end AS spanEnd,
25394
+ f.masked_match AS maskedMatch,
25395
+ f.action_taken AS actionTaken,
25396
+ f.confidence AS confidence
25397
+ FROM inspection_findings f
25398
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25399
+ WHERE f.audit_event_id = :auditEventId
25400
+ ORDER BY f.span_start, f.id`
25401
+ );
24906
25402
  }
24907
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24908
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25403
+ db;
25404
+ ensureRowStmt;
25405
+ sessionsStmt;
25406
+ rowsStmt;
25407
+ stampStmt;
25408
+ countsStmt;
25409
+ fingerprintStmt;
25410
+ setFingerprintStmt;
25411
+ rearmStmt;
25412
+ claimStmt;
25413
+ heartbeatStmt;
25414
+ releaseStmt;
25415
+ leaseStmt;
25416
+ inspectionsStmt;
25417
+ closeWindowStmt;
25418
+ releaseBoundaryStmt;
25419
+ freezeBoundaryStmt;
25420
+ captureRowsStmt;
25421
+ markOwedStmt;
25422
+ markCaptureBacklogOwedStmt;
25423
+ captureSkipCountStmt;
25424
+ disownCapturesStmt;
25425
+ partitionStmt;
25426
+ partitionByKindStmt;
25427
+ claimRowStmt;
25428
+ releaseRowStmt;
25429
+ releaseStaleClaimsStmt;
25430
+ /**
25431
+ * The masked detections recorded against one tool call.
25432
+ *
25433
+ * These travel with the event because a tool call's target is not
25434
+ * re-inspectable from the event alone — unlike a capture, where the text
25435
+ * itself is re-scannable. What crosses is the masked match and the rule that
25436
+ * produced it, never the value.
25437
+ */
25438
+ inspectionsFor(auditEventId) {
25439
+ return allRows(this.inspectionsStmt, { auditEventId });
24909
25440
  }
24910
- ensureSyncedAtColumn(db, "audit_events");
24911
- ensureScanLedgerTable(db);
24912
- ensureHistorySyncTable(db);
24913
- ensureBlockedDetectionsTable(db);
24914
- ensureRuleProbeCacheTable(db);
24915
- ensureWriteGateTrigger(db);
24916
- ensureTokenUsageColumns(db);
24917
- reconcileSourceProjectIds(db);
24918
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24919
- const drained = runLegacyHistoryBackfill(db);
24920
- if (drained) applyLegacyDropMigration(db, file2);
25441
+ /**
25442
+ * Sessions with structural rows still to send, oldest first.
25443
+ *
25444
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25445
+ * read. Anything recorded after the machine attached is the live forward
25446
+ * path's to deliver; this drain exists for what was recorded before it, and a
25447
+ * row both paths send is at best a duplicate request and at worst — for a
25448
+ * session root — an overwrite of the inventory ids the live path resolved.
25449
+ */
25450
+ pendingSessions(limit, before) {
25451
+ return allRows(this.sessionsStmt, { limit, before }).map(
25452
+ (r) => r.sessionId
25453
+ );
24921
25454
  }
24922
- }
24923
- function readLegacyTables(db) {
24924
- let holdsRows = false;
24925
- const marks = [];
24926
- for (const table of ["events", "findings"]) {
24927
- try {
24928
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
24929
- if (row === void 0) {
24930
- holdsRows = true;
24931
- marks.push(`${table}:unreadable`);
24932
- continue;
24933
- }
24934
- if (row.n > 0) holdsRows = true;
24935
- marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
24936
- } catch {
24937
- holdsRows = true;
24938
- marks.push(`${table}:unreadable`);
24939
- }
25455
+ /** One session's undelivered structural rows within the backlog, root first. */
25456
+ pendingRows(sessionId, limit, before) {
25457
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24940
25458
  }
24941
- return { holdsRows, mark: marks.join("|") };
24942
- }
24943
- function applyLegacyDropMigration(db, file2) {
24944
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24945
- if (!migration) return;
24946
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24947
- if (file2 !== void 0 && before?.holdsRows === true) {
24948
- try {
24949
- backupBeforeLegacyDrop(db, file2);
24950
- } catch (error61) {
24951
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24952
- return;
24953
- }
25459
+ /**
25460
+ * Captures this machine still owes the deployment, oldest first.
25461
+ *
25462
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25463
+ * by a time window — see captureRowsStmt for why a window could not express
25464
+ * this. `before` is the grace window that leaves a just-recorded capture to
25465
+ * the live path.
25466
+ */
25467
+ pendingCaptureRows(limit, before) {
25468
+ return allRows(this.captureRowsStmt, { limit, before });
24954
25469
  }
24955
- try {
25470
+ /**
25471
+ * Record that a capture is OWED to the deployment.
25472
+ *
25473
+ * Written by the attached forward path when a live send did not confirm
25474
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25475
+ * a fact rather than an inference: the machine was attached, the send did not
25476
+ * land, so the row is owed — which no time window can state, because the same
25477
+ * window that holds the rows a past attachment left owed also holds every
25478
+ * capture recorded while the machine was DETACHED, and those were never
25479
+ * offered to anyone.
25480
+ *
25481
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25482
+ * out of the drain's read.
25483
+ */
25484
+ markCaptureOwed(id) {
25485
+ this.markOwedStmt.run({ id });
25486
+ }
25487
+ /**
25488
+ * Mark every capture already on disk as owed, as of `before`.
25489
+ *
25490
+ * The consent-time backfill, called once from `aka attach` when a human
25491
+ * grants existing-history consent — never from an ongoing drain pass, and
25492
+ * never inferred from a boundary that could later move. `before` is the
25493
+ * caller's own "now" at the moment consent was granted, so what this marks
25494
+ * is exactly the backlog the consent prompt already counted, not whatever a
25495
+ * later re-attach or key rotation might widen it to.
25496
+ *
25497
+ * Returns how many rows matched, for the caller to log or test against. Not a
25498
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25499
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25500
+ */
25501
+ markCaptureBacklogOwed(before) {
25502
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25503
+ }
25504
+ /**
25505
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25506
+ *
25507
+ * CLEARS any failure reason in the same statement. A row that failed against
25508
+ * one deployment and then landed is delivered, and leaving the reason behind
25509
+ * would leave the store holding two contradictory answers about one row —
25510
+ * with the surface free to render either.
25511
+ */
25512
+ markSynced(ids, atMs) {
25513
+ this.stampAll(ids, atMs, null);
25514
+ }
25515
+ /**
25516
+ * Record that THIS MACHINE cannot express the row on the wire.
25517
+ *
25518
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25519
+ * payload, or a body the client itself refused to send. It fails identically
25520
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25521
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25522
+ * is retried; marking those would turn one outage into permanent data loss.
25523
+ */
25524
+ markSkipped(ids, atMs) {
25525
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25526
+ }
25527
+ /**
25528
+ * Record that THIS DEPLOYMENT refused the row.
25529
+ *
25530
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25531
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25532
+ * row is outstanding rather than why. What separates them is the reason, and
25533
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25534
+ * on one body, so it is terminal only for as long as this machine points at
25535
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25536
+ *
25537
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25538
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25539
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25540
+ */
25541
+ markRefused(ids, atMs) {
25542
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25543
+ }
25544
+ eachInTransaction(ids, run) {
25545
+ if (ids.length === 0) return;
24956
25546
  withTransaction(
24957
- db,
25547
+ this.db,
24958
25548
  () => {
24959
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24960
- if (alreadyDropped) return;
24961
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24962
- akaWarn(
24963
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24964
- );
24965
- return;
24966
- }
24967
- for (const statement of splitStatements(migration.sql)) {
24968
- db.exec(statement);
24969
- }
24970
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24971
- migration.tag,
24972
- Date.now()
24973
- );
25549
+ for (const id of ids) run(id);
24974
25550
  },
24975
25551
  "IMMEDIATE"
24976
25552
  );
24977
- } catch (error61) {
24978
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24979
25553
  }
24980
- }
24981
- function backupBeforeLegacyDrop(db, file2) {
24982
- reapStalePartials(file2);
24983
- const backup = backupPath(file2, "pre-drop");
24984
- snapshotStore(db, backup);
24985
- return backup;
24986
- }
24987
- var TOKEN_USAGE_COLUMNS = [
24988
- {
24989
- name: "input_tokens",
24990
- ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
24991
- },
24992
- {
24993
- name: "output_tokens",
24994
- ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
24995
- },
24996
- {
24997
- name: "cache_creation_input_tokens",
24998
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
24999
- },
25000
- {
25001
- name: "cache_read_input_tokens",
25002
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25003
- },
25004
- {
25005
- name: "model",
25006
- ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25007
- },
25554
+ stampAll(ids, value, failure, failedAtMs) {
25555
+ if (ids.length === 0) return;
25556
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25557
+ withTransaction(
25558
+ this.db,
25559
+ () => {
25560
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25561
+ },
25562
+ "IMMEDIATE"
25563
+ );
25564
+ }
25565
+ /**
25566
+ * Claim rows as in-flight.
25567
+ *
25568
+ * Advisory in exactly the sense the lease is: it records that a send is in
25569
+ * progress so a surface can say so, and a lost claim costs a row showing as
25570
+ * queued while it is actually being sent. It is not exclusion — the far side
25571
+ * settles a duplicate on the row id.
25572
+ */
25573
+ claimRows(ids, atMs) {
25574
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25575
+ }
25576
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25577
+ releaseRows(ids) {
25578
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25579
+ }
25580
+ /**
25581
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25582
+ *
25583
+ * A process killed between claiming and settling leaves rows claimed with
25584
+ * nothing left to settle them. Without this they read as "sending" for ever.
25585
+ */
25586
+ releaseStaleClaims(staleBefore) {
25587
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25588
+ }
25589
+ /**
25590
+ * Every tracked row in exactly one delivery state.
25591
+ *
25592
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25593
+ * pick up now", which is a different question from "what state is this row
25594
+ * in" — and a machine that has never attached has no boundary to pass, so
25595
+ * requiring one would force a caller to invent one and report the whole store
25596
+ * as queued.
25597
+ */
25598
+ /**
25599
+ * The same partition, one row per kind that a lane carries.
25600
+ *
25601
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25602
+ * scope decides which rows exist at all, so a kind that has never been
25603
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25604
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25605
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25606
+ * different things.
25607
+ */
25608
+ partitionByKind() {
25609
+ return allRows(
25610
+ this.partitionByKindStmt,
25611
+ {}
25612
+ ).map((row) => ({
25613
+ kind: row.kind,
25614
+ queued: row.queued ?? 0,
25615
+ inProgress: row.inProgress ?? 0,
25616
+ synced: row.synced ?? 0,
25617
+ failed: row.failed ?? 0,
25618
+ refused: row.refused ?? 0,
25619
+ detached: row.detached ?? 0,
25620
+ total: row.total ?? 0
25621
+ }));
25622
+ }
25623
+ partition() {
25624
+ const row = getRow(this.partitionStmt, {});
25625
+ return {
25626
+ queued: row?.queued ?? 0,
25627
+ inProgress: row?.inProgress ?? 0,
25628
+ synced: row?.synced ?? 0,
25629
+ failed: row?.failed ?? 0,
25630
+ refused: row?.refused ?? 0,
25631
+ detached: row?.detached ?? 0,
25632
+ total: row?.total ?? 0
25633
+ };
25634
+ }
25635
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25636
+ counts(before) {
25637
+ const row = getRow(this.countsStmt, { before });
25638
+ const captures = getRow(this.captureSkipCountStmt);
25639
+ return {
25640
+ pending: row?.pending ?? 0,
25641
+ sent: row?.sent ?? 0,
25642
+ skipped: row?.skipped ?? 0,
25643
+ refused: row?.refused ?? 0,
25644
+ detached: row?.detached ?? 0,
25645
+ capturesSkipped: captures?.skipped ?? 0
25646
+ };
25647
+ }
25648
+ /**
25649
+ * The deployment the current stamps were made against, and where its backlog
25650
+ * ends.
25651
+ *
25652
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25653
+ * machine that has never drained is — and every writer below seeds the row
25654
+ * before it needs one, so nothing depends on this creating it. Keeping the
25655
+ * write off the gate path matters because the gate runs on every pass while a
25656
+ * write has to take the database's write lock.
25657
+ */
25658
+ deployment() {
25659
+ const row = getRow(
25660
+ this.fingerprintStmt
25661
+ );
25662
+ return {
25663
+ fingerprint: row?.fingerprint ?? void 0,
25664
+ backlogBefore: row?.backlogBefore ?? void 0
25665
+ };
25666
+ }
25667
+ /**
25668
+ * Point the ledger at a different deployment, discarding what it recorded
25669
+ * about the previous one.
25670
+ *
25671
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25672
+ * machine has just left are undelivered as far as the new one is concerned.
25673
+ * All four in one transaction, so a crash between them cannot leave stamps
25674
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25675
+ * a disown with no re-mark to follow it.
25676
+ *
25677
+ * The boundary is written HERE and only here, which is what freezes it: a
25678
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25679
+ * unchanged, so this never runs and the backlog does not widen back over rows
25680
+ * the live path has since delivered.
25681
+ *
25682
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25683
+ * granted existing-history consent for the deployment this call is arming —
25684
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25685
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25686
+ * apart. Passed only when that grant is valid, since this method has no way
25687
+ * to check consent itself and must not mark a row owed for a machine that
25688
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25689
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25690
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25691
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25692
+ * on the cleared side of that bound — and the re-mark in the same
25693
+ * transaction is what puts those rows back. A crash between the two cannot
25694
+ * strand the ledger disowned with nothing re-marked — the transaction either
25695
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25696
+ * committed re-enters this method on the very next pass. Omit it (the
25697
+ * structural-only tests do) to exercise the disown in isolation.
25698
+ *
25699
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25700
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25701
+ * live path can mark a capture owed from the moment `aka attach` writes the
25702
+ * descriptor, before the drain's first pass ever reaches this method, and
25703
+ * such a row sits at or after the bound rather than below it. What keeps the
25704
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25705
+ * bound — disown runs first, re-mark second, both inside the one
25706
+ * transaction above.
25707
+ */
25708
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25709
+ this.ensureRowStmt.run();
25710
+ withTransaction(
25711
+ this.db,
25712
+ () => {
25713
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25714
+ this.rearmStmt.run();
25715
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25716
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25717
+ }
25718
+ if (backfillCapturesBefore !== void 0) {
25719
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25720
+ }
25721
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25722
+ },
25723
+ "IMMEDIATE"
25724
+ );
25725
+ }
25726
+ /**
25727
+ * End the attached period: hand its rows to the live path, and release the
25728
+ * boundary so the next attachment can freeze a new one.
25729
+ *
25730
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25731
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25732
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25733
+ * during the detached period, because the machine is not attached. Rows
25734
+ * recorded in that window sit after the boundary and before the re-attach, so
25735
+ * neither path takes them, and the pending count reports none outstanding.
25736
+ *
25737
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25738
+ * closing attachment's to deliver and are no longer outstanding — that is what
25739
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25740
+ * distinction is not academic: this used to write a delivery TIME, which every
25741
+ * read treats as delivery, so one detach turned a window of undelivered rows
25742
+ * into a window of delivered ones and no surface could tell. It writes the
25743
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25744
+ * "received" stop being the same fact.
25745
+ *
25746
+ * A change of deployment still frees them (see the re-arm), because the next
25747
+ * deployment has seen none of this machine's history — so the rows reach it
25748
+ * exactly as they did when this wrote a delivery time.
25749
+ *
25750
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25751
+ * window unstamped — that half-state would re-send the whole attached period
25752
+ * on the next attach, which is the failure the boundary exists to prevent.
25753
+ */
25754
+ closeAttachedWindow(attachedAtMs, atMs) {
25755
+ this.ensureRowStmt.run();
25756
+ withTransaction(
25757
+ this.db,
25758
+ () => {
25759
+ const row = getRow(this.fingerprintStmt);
25760
+ const from = row?.backlogBefore ?? attachedAtMs;
25761
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25762
+ this.releaseBoundaryStmt.run();
25763
+ },
25764
+ "IMMEDIATE"
25765
+ );
25766
+ }
25767
+ /**
25768
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25769
+ *
25770
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25771
+ * different deployment and therefore discards what was delivered to the old
25772
+ * one: here the recipient is the same, so everything already sent to it stays
25773
+ * sent.
25774
+ */
25775
+ freezeBoundary(backlogBefore) {
25776
+ this.ensureRowStmt.run();
25777
+ this.freezeBoundaryStmt.run({ backlogBefore });
25778
+ }
25779
+ /** Take the claim, or report that someone live already holds it. */
25780
+ claim(pid, host, nowMs, staleAfterMs) {
25781
+ this.ensureRowStmt.run();
25782
+ let taken = false;
25783
+ withTransaction(
25784
+ this.db,
25785
+ () => {
25786
+ const result = this.claimStmt.run({
25787
+ pid,
25788
+ host,
25789
+ now: nowMs,
25790
+ staleBefore: nowMs - staleAfterMs
25791
+ });
25792
+ taken = result.changes === 1;
25793
+ },
25794
+ "IMMEDIATE"
25795
+ );
25796
+ return taken;
25797
+ }
25798
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25799
+ heartbeat(pid, nowMs) {
25800
+ this.heartbeatStmt.run({ now: nowMs, pid });
25801
+ }
25802
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25803
+ release(pid) {
25804
+ this.releaseStmt.run({ pid });
25805
+ }
25806
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25807
+ lease() {
25808
+ return getRow(this.leaseStmt);
25809
+ }
25810
+ };
25811
+
25812
+ // ../../packages/persistence/src/migrations.ts
25813
+ function describeObject(object2) {
25814
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25815
+ }
25816
+ function splitStatements(sql) {
25817
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25818
+ }
25819
+ function createdIndexName(statement) {
25820
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25821
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25822
+ }
25823
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25824
+ function applyMigrations(db, file2, options = {}) {
25825
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25826
+ db.exec(
25827
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25828
+ );
25829
+ const applied = new Set(
25830
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25831
+ );
25832
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25833
+ const record2 = db.prepare(
25834
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25835
+ );
25836
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25837
+ if (applied.has(migration.tag)) continue;
25838
+ if (options.skipTags?.has(migration.tag) === true) continue;
25839
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25840
+ const evidence = evidenceObjects(migration.sql);
25841
+ const present = evidence.filter((o) => evidenceExists(db, o));
25842
+ if (present.length > 0 && present.length < evidence.length) {
25843
+ const missing = evidence.filter((o) => !present.includes(o));
25844
+ const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
25845
+ akaWarn(message);
25846
+ throw new Error(`[aka] ${message}`);
25847
+ }
25848
+ const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
25849
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25850
+ const statements = splitStatements(migration.sql);
25851
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25852
+ try {
25853
+ withTransaction(
25854
+ db,
25855
+ () => {
25856
+ for (const statement of statements) {
25857
+ const indexName = createdIndexName(statement);
25858
+ if (indexName === void 0) {
25859
+ if (alreadyApplied) continue;
25860
+ } else if (indexExists(db, indexName)) {
25861
+ continue;
25862
+ }
25863
+ db.exec(statement);
25864
+ }
25865
+ if (wantsFkOff && !alreadyApplied) {
25866
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25867
+ if (violations.length > 0) {
25868
+ throw new Error(
25869
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25870
+ );
25871
+ }
25872
+ }
25873
+ record2.run(migration.tag, Date.now());
25874
+ },
25875
+ "IMMEDIATE"
25876
+ );
25877
+ } finally {
25878
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25879
+ }
25880
+ }
25881
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25882
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25883
+ }
25884
+ ensureSyncedAtColumn(db, "audit_events");
25885
+ ensureScanLedgerTable(db);
25886
+ ensureHistorySyncTable(db);
25887
+ ensureBlockedDetectionsTable(db);
25888
+ ensureRuleProbeCacheTable(db);
25889
+ ensureWriteGateTrigger(db);
25890
+ ensureTokenUsageColumns(db);
25891
+ reconcileSourceProjectIds(db);
25892
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25893
+ const drained = runLegacyHistoryBackfill(db);
25894
+ if (drained) applyLegacyDropMigration(db, file2);
25895
+ }
25896
+ }
25897
+ function readLegacyTables(db) {
25898
+ let holdsRows = false;
25899
+ const marks = [];
25900
+ for (const table of ["events", "findings"]) {
25901
+ try {
25902
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
25903
+ if (row === void 0) {
25904
+ holdsRows = true;
25905
+ marks.push(`${table}:unreadable`);
25906
+ continue;
25907
+ }
25908
+ if (row.n > 0) holdsRows = true;
25909
+ marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
25910
+ } catch {
25911
+ holdsRows = true;
25912
+ marks.push(`${table}:unreadable`);
25913
+ }
25914
+ }
25915
+ return { holdsRows, mark: marks.join("|") };
25916
+ }
25917
+ function applyLegacyDropMigration(db, file2) {
25918
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25919
+ if (!migration) return;
25920
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25921
+ if (file2 !== void 0 && before?.holdsRows === true) {
25922
+ try {
25923
+ backupBeforeLegacyDrop(db, file2);
25924
+ } catch (error61) {
25925
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25926
+ return;
25927
+ }
25928
+ }
25929
+ try {
25930
+ withTransaction(
25931
+ db,
25932
+ () => {
25933
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25934
+ if (alreadyDropped) return;
25935
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25936
+ akaWarn(
25937
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25938
+ );
25939
+ return;
25940
+ }
25941
+ for (const statement of splitStatements(migration.sql)) {
25942
+ db.exec(statement);
25943
+ }
25944
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25945
+ migration.tag,
25946
+ Date.now()
25947
+ );
25948
+ },
25949
+ "IMMEDIATE"
25950
+ );
25951
+ } catch (error61) {
25952
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25953
+ }
25954
+ }
25955
+ function backupBeforeLegacyDrop(db, file2) {
25956
+ reapStalePartials(file2);
25957
+ const backup = backupPath(file2, "pre-drop");
25958
+ snapshotStore(db, backup);
25959
+ return backup;
25960
+ }
25961
+ var TOKEN_USAGE_COLUMNS = [
25962
+ {
25963
+ name: "input_tokens",
25964
+ ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
25965
+ },
25966
+ {
25967
+ name: "output_tokens",
25968
+ ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
25969
+ },
25970
+ {
25971
+ name: "cache_creation_input_tokens",
25972
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
25973
+ },
25974
+ {
25975
+ name: "cache_read_input_tokens",
25976
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25977
+ },
25978
+ {
25979
+ name: "model",
25980
+ ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25981
+ },
25008
25982
  {
25009
25983
  name: "provider",
25010
25984
  ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
@@ -25268,10 +26242,62 @@ function ensureSyncedAtColumn(db, table) {
25268
26242
  if (!columns.includes("outbox_owed")) {
25269
26243
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25270
26244
  }
26245
+ if (!columns.includes("sync_failed_at")) {
26246
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26247
+ }
26248
+ if (!columns.includes("sync_failure")) {
26249
+ withTransaction(
26250
+ db,
26251
+ () => {
26252
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26253
+ db.exec(
26254
+ `UPDATE ${table} SET synced_at = NULL
26255
+ WHERE synced_at = -1
26256
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26257
+ );
26258
+ },
26259
+ "IMMEDIATE"
26260
+ );
26261
+ }
25271
26262
  db.exec(
25272
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25273
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26263
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26264
+ BEFORE UPDATE OF sync_failure ON ${table}
26265
+ WHEN ${syncFailureRejectCondition()}
26266
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25274
26267
  );
26268
+ const syncIndexColumns = [
26269
+ "event_type",
26270
+ "synced_at",
26271
+ "sync_claimed_at",
26272
+ "started_at",
26273
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26274
+ // has to be in the index for the read to stay covered — but putting it
26275
+ // ahead of `started_at` would reorder the prefix the structural drain's
26276
+ // reads match on.
26277
+ "sync_failure"
26278
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26279
+ //
26280
+ // The delivery-state read tests it — a capture's state depends on whether a
26281
+ // live forward marked it owed — so carrying it here makes that read covering
26282
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26283
+ // But a sixth column changes what the planner charges for this index, and
26284
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26285
+ // then stops choosing the per-session index for the token rollup and walks
26286
+ // every `llm_call` in the store through the event-type index instead. That
26287
+ // read grows with the store; this one does not.
26288
+ //
26289
+ // 40 ms on the largest store measured, once per render, is a cost worth
26290
+ // paying to leave every other read's plan where it was.
26291
+ ];
26292
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26293
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26294
+ if (!syncIndexMatches) {
26295
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26296
+ db.exec(
26297
+ `CREATE INDEX idx_audit_events_sync
26298
+ ON audit_events (${syncIndexColumns.join(", ")})`
26299
+ );
26300
+ }
25275
26301
  db.exec(
25276
26302
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25277
26303
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25493,7 +26519,11 @@ function buildAuditEvent(row) {
25493
26519
  link: linkParsed?.success ? linkParsed.data : null,
25494
26520
  targetId: row.target_id,
25495
26521
  internal: intToBool(row.internal),
25496
- flagged: intToBool(row.flagged)
26522
+ flagged: intToBool(row.flagged),
26523
+ // Only meaningful when the title came out empty — a row whose body was
26524
+ // expired but whose title fell back to `tool_name` still has something to
26525
+ // render, and flagging it would make the view apologise for nothing.
26526
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25497
26527
  };
25498
26528
  }
25499
26529
  var TIMELINE_COLUMNS = `
@@ -25501,6 +26531,7 @@ var TIMELINE_COLUMNS = `
25501
26531
  event_type,
25502
26532
  started_at,
25503
26533
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26534
+ content_expired_at,
25504
26535
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25505
26536
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25506
26537
  json_extract(attributes, '$.severity') AS severity,
@@ -26166,6 +27197,88 @@ var SqliteAuditEventsRepository = class {
26166
27197
  }
26167
27198
  };
26168
27199
 
27200
+ // ../../packages/persistence/src/repositories/body-retention.ts
27201
+ var DEFAULT_BATCH_SIZE = 500;
27202
+ var DEFAULT_MAX_ROWS = 5e4;
27203
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27204
+ var SqliteBodyRetentionRepository = class {
27205
+ constructor(db) {
27206
+ this.db = db;
27207
+ const select = (laneClause) => `
27208
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27209
+ FROM audit_events
27210
+ WHERE content IS NOT NULL
27211
+ AND started_at < :cutoff
27212
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27213
+ ${laneClause}
27214
+ ORDER BY started_at
27215
+ LIMIT :limit`;
27216
+ this.candidatesStmt = this.db.prepare(select(""));
27217
+ this.candidatesSyncSafeStmt = this.db.prepare(
27218
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27219
+ );
27220
+ this.heldBySyncStmt = this.db.prepare(`
27221
+ SELECT COUNT(*) AS n
27222
+ FROM audit_events
27223
+ WHERE content IS NOT NULL
27224
+ AND started_at < :cutoff
27225
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27226
+ AND synced_at IS NULL`);
27227
+ this.expireStmt = this.db.prepare(
27228
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27229
+ );
27230
+ }
27231
+ db;
27232
+ candidatesStmt;
27233
+ candidatesSyncSafeStmt;
27234
+ heldBySyncStmt;
27235
+ expireStmt;
27236
+ /** How many bytes a pass with these options would free, changing nothing. */
27237
+ preview(opts) {
27238
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27239
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27240
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27241
+ return {
27242
+ rowsExpired: rows.length,
27243
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27244
+ rowsHeldBySync: this.countHeldBySync(opts)
27245
+ };
27246
+ }
27247
+ /** Clear eligible bodies, in bounded batches. */
27248
+ expire(opts) {
27249
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27250
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27251
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27252
+ let rowsExpired = 0;
27253
+ let bytesFreed = 0;
27254
+ let done = true;
27255
+ while (rowsExpired < maxRows) {
27256
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27257
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27258
+ if (batch.length === 0) break;
27259
+ withTransaction(
27260
+ this.db,
27261
+ () => {
27262
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27263
+ },
27264
+ "IMMEDIATE"
27265
+ );
27266
+ rowsExpired += batch.length;
27267
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27268
+ if (batch.length < remaining) break;
27269
+ if (rowsExpired >= maxRows) {
27270
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27271
+ }
27272
+ }
27273
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27274
+ }
27275
+ countHeldBySync(opts) {
27276
+ if (opts.sweepSyncLane) return 0;
27277
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27278
+ return row.n;
27279
+ }
27280
+ };
27281
+
26169
27282
  // ../../packages/persistence/src/repositories/classified-data.ts
26170
27283
  var SqliteClassifiedDataRepository = class {
26171
27284
  constructor(db) {
@@ -26994,7 +28107,15 @@ function toFlatFindingRow(r) {
26994
28107
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26995
28108
  eventId: r.event_id,
26996
28109
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26997
- status: deriveInstanceStatus(r)
28110
+ status: deriveInstanceStatus(r),
28111
+ delivery: deriveFindingDelivery({
28112
+ kind: r.kind,
28113
+ syncedAt: r.synced_at,
28114
+ syncClaimedAt: r.sync_claimed_at,
28115
+ syncFailedAt: r.sync_failed_at,
28116
+ syncFailure: r.sync_failure,
28117
+ outboxOwed: r.outbox_owed
28118
+ })
26998
28119
  };
26999
28120
  }
27000
28121
  function encodeGroupCursor(group) {
@@ -27058,7 +28179,10 @@ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS c
27058
28179
  e.tool_name AS tool_name,
27059
28180
  f.audit_event_id AS event_id, e.root_session_id AS session_id,
27060
28181
  e.event_type AS kind, f.finding_key AS finding_key,
27061
- ${latestResolutionStatusSql("f")} AS latest_status`;
28182
+ ${latestResolutionStatusSql("f")} AS latest_status,
28183
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28184
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28185
+ e.outbox_owed AS outbox_owed`;
27062
28186
  var DAY_MS3 = 864e5;
27063
28187
  var SqliteFindingsRepository = class {
27064
28188
  constructor(db) {
@@ -27303,6 +28427,7 @@ var SqliteFindingsRepository = class {
27303
28427
  providers: query.provider,
27304
28428
  actions: query.action,
27305
28429
  statuses: query.status,
28430
+ deliveries: query.deployment,
27306
28431
  tools: query.tool,
27307
28432
  repo: query.repo,
27308
28433
  file: query.file,
@@ -27370,6 +28495,7 @@ var SqliteFindingsRepository = class {
27370
28495
  providers: query.provider,
27371
28496
  actions: query.action,
27372
28497
  statuses: query.status,
28498
+ deliveries: query.deployment,
27373
28499
  tools: query.tool,
27374
28500
  q: query.q
27375
28501
  };
@@ -27633,7 +28759,9 @@ var SqliteFindingsRepository = class {
27633
28759
  )
27634
28760
  );
27635
28761
  for (const row of grouped) {
27636
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28762
+ if (Object.hasOwn(byAction, row.action_taken)) {
28763
+ byAction[row.action_taken] = row.c;
28764
+ }
27637
28765
  }
27638
28766
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27639
28767
  const sevRows = allRows(
@@ -27650,7 +28778,9 @@ var SqliteFindingsRepository = class {
27650
28778
  )
27651
28779
  );
27652
28780
  for (const row of sevRows) {
27653
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28781
+ if (Object.hasOwn(bySeverity, row.severity)) {
28782
+ bySeverity[row.severity] = row.c;
28783
+ }
27654
28784
  }
27655
28785
  const categories = ENFORCEABLE_CATEGORIES;
27656
28786
  const enabledRows = allRows(
@@ -27699,525 +28829,6 @@ function isoDay(ms) {
27699
28829
  return new Date(ms).toISOString().slice(0, 10);
27700
28830
  }
27701
28831
 
27702
- // ../../packages/persistence/src/repositories/history-sync.ts
27703
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27704
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27705
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27706
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27707
- var SKIPPED = -1;
27708
- var ROW_COLUMNS = `id,
27709
- parent_id AS parentId,
27710
- root_session_id AS rootSessionId,
27711
- event_type AS eventType,
27712
- host_id AS hostId,
27713
- harness_id AS harnessId,
27714
- source_project_id AS sourceProjectId,
27715
- started_at AS startedAt,
27716
- ended_at AS endedAt,
27717
- severity,
27718
- priority,
27719
- content,
27720
- content_hash AS contentHash,
27721
- attributes`;
27722
- var SqliteHistorySyncRepository = class {
27723
- constructor(db) {
27724
- this.db = db;
27725
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27726
- this.sessionsStmt = db.prepare(
27727
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27728
- FROM audit_events
27729
- WHERE synced_at IS NULL
27730
- AND event_type IN (${TYPE_LIST})
27731
- AND started_at < :before
27732
- GROUP BY sessionId
27733
- ORDER BY earliest
27734
- LIMIT :limit`
27735
- );
27736
- this.rowsStmt = db.prepare(
27737
- `SELECT ${ROW_COLUMNS}
27738
- FROM audit_events
27739
- WHERE synced_at IS NULL
27740
- AND event_type IN (${TYPE_LIST})
27741
- AND started_at < :before
27742
- AND COALESCE(root_session_id, id) = :sessionId
27743
- ORDER BY (event_type = 'session') DESC, started_at
27744
- LIMIT :limit`
27745
- );
27746
- this.captureRowsStmt = db.prepare(
27747
- `SELECT ${ROW_COLUMNS}
27748
- FROM audit_events
27749
- WHERE synced_at IS NULL
27750
- AND sync_claimed_at IS NULL
27751
- AND outbox_owed = 1
27752
- AND event_type IN (${CAPTURE_TYPE_LIST})
27753
- AND started_at < :before
27754
- ORDER BY started_at
27755
- LIMIT :limit`
27756
- );
27757
- this.markOwedStmt = db.prepare(
27758
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27759
- );
27760
- this.markCaptureBacklogOwedStmt = db.prepare(
27761
- `UPDATE audit_events SET outbox_owed = 1
27762
- WHERE synced_at IS NULL
27763
- AND event_type IN (${CAPTURE_TYPE_LIST})
27764
- AND started_at < :before`
27765
- );
27766
- this.stampStmt = db.prepare(
27767
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27768
- );
27769
- this.claimRowStmt = db.prepare(
27770
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27771
- );
27772
- this.releaseRowStmt = db.prepare(
27773
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27774
- );
27775
- this.releaseStaleClaimsStmt = db.prepare(
27776
- `UPDATE audit_events SET sync_claimed_at = NULL
27777
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27778
- );
27779
- this.partitionStmt = db.prepare(
27780
- `SELECT
27781
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27782
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27783
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27784
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27785
- COUNT(*) AS total
27786
- FROM audit_events
27787
- WHERE event_type IN (${TYPE_LIST})`
27788
- );
27789
- this.countsStmt = db.prepare(
27790
- `SELECT
27791
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27792
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27793
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27794
- FROM audit_events
27795
- WHERE event_type IN (${TYPE_LIST})`
27796
- );
27797
- this.captureSkipCountStmt = db.prepare(
27798
- `SELECT COUNT(*) AS skipped
27799
- FROM audit_events
27800
- WHERE synced_at = ${String(SKIPPED)}
27801
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27802
- );
27803
- this.fingerprintStmt = db.prepare(
27804
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27805
- FROM history_sync WHERE id = 1`
27806
- );
27807
- this.setFingerprintStmt = db.prepare(
27808
- `UPDATE history_sync
27809
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27810
- WHERE id = 1`
27811
- );
27812
- this.disownCapturesStmt = db.prepare(
27813
- `UPDATE audit_events SET outbox_owed = NULL
27814
- WHERE outbox_owed IS NOT NULL
27815
- AND event_type IN (${CAPTURE_TYPE_LIST})
27816
- AND started_at < :attachedAt`
27817
- );
27818
- this.rearmStmt = db.prepare(
27819
- `UPDATE audit_events SET synced_at = NULL
27820
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27821
- );
27822
- this.claimStmt = db.prepare(
27823
- `UPDATE history_sync
27824
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27825
- WHERE id = 1
27826
- AND (owner_pid IS NULL
27827
- OR heartbeat_at IS NULL
27828
- OR heartbeat_at < :staleBefore
27829
- OR heartbeat_at > :now)`
27830
- );
27831
- this.heartbeatStmt = db.prepare(
27832
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27833
- );
27834
- this.releaseStmt = db.prepare(
27835
- `UPDATE history_sync
27836
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27837
- WHERE id = 1 AND owner_pid = :pid`
27838
- );
27839
- this.closeWindowStmt = db.prepare(
27840
- `UPDATE audit_events SET synced_at = :at
27841
- WHERE synced_at IS NULL
27842
- AND event_type IN (${TYPE_LIST})
27843
- AND started_at >= :attachedAt`
27844
- );
27845
- this.releaseBoundaryStmt = db.prepare(
27846
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27847
- );
27848
- this.freezeBoundaryStmt = db.prepare(
27849
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27850
- );
27851
- this.leaseStmt = db.prepare(
27852
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27853
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27854
- FROM history_sync WHERE id = 1`
27855
- );
27856
- this.inspectionsStmt = db.prepare(
27857
- `SELECT d.rule_id AS ruleId,
27858
- d.name AS ruleName,
27859
- d.version AS ruleVersion,
27860
- d.category AS category,
27861
- d.severity AS severity,
27862
- f.span_start AS spanStart,
27863
- f.span_end AS spanEnd,
27864
- f.masked_match AS maskedMatch,
27865
- f.action_taken AS actionTaken,
27866
- f.confidence AS confidence
27867
- FROM inspection_findings f
27868
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27869
- WHERE f.audit_event_id = :auditEventId
27870
- ORDER BY f.span_start, f.id`
27871
- );
27872
- }
27873
- db;
27874
- ensureRowStmt;
27875
- sessionsStmt;
27876
- rowsStmt;
27877
- stampStmt;
27878
- countsStmt;
27879
- fingerprintStmt;
27880
- setFingerprintStmt;
27881
- rearmStmt;
27882
- claimStmt;
27883
- heartbeatStmt;
27884
- releaseStmt;
27885
- leaseStmt;
27886
- inspectionsStmt;
27887
- closeWindowStmt;
27888
- releaseBoundaryStmt;
27889
- freezeBoundaryStmt;
27890
- captureRowsStmt;
27891
- markOwedStmt;
27892
- markCaptureBacklogOwedStmt;
27893
- captureSkipCountStmt;
27894
- disownCapturesStmt;
27895
- partitionStmt;
27896
- claimRowStmt;
27897
- releaseRowStmt;
27898
- releaseStaleClaimsStmt;
27899
- /**
27900
- * The masked detections recorded against one tool call.
27901
- *
27902
- * These travel with the event because a tool call's target is not
27903
- * re-inspectable from the event alone — unlike a capture, where the text
27904
- * itself is re-scannable. What crosses is the masked match and the rule that
27905
- * produced it, never the value.
27906
- */
27907
- inspectionsFor(auditEventId) {
27908
- return allRows(this.inspectionsStmt, { auditEventId });
27909
- }
27910
- /**
27911
- * Sessions with structural rows still to send, oldest first.
27912
- *
27913
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27914
- * read. Anything recorded after the machine attached is the live forward
27915
- * path's to deliver; this drain exists for what was recorded before it, and a
27916
- * row both paths send is at best a duplicate request and at worst — for a
27917
- * session root — an overwrite of the inventory ids the live path resolved.
27918
- */
27919
- pendingSessions(limit, before) {
27920
- return allRows(this.sessionsStmt, { limit, before }).map(
27921
- (r) => r.sessionId
27922
- );
27923
- }
27924
- /** One session's undelivered structural rows within the backlog, root first. */
27925
- pendingRows(sessionId, limit, before) {
27926
- return allRows(this.rowsStmt, { sessionId, limit, before });
27927
- }
27928
- /**
27929
- * Captures this machine still owes the deployment, oldest first.
27930
- *
27931
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27932
- * by a time window — see captureRowsStmt for why a window could not express
27933
- * this. `before` is the grace window that leaves a just-recorded capture to
27934
- * the live path.
27935
- */
27936
- pendingCaptureRows(limit, before) {
27937
- return allRows(this.captureRowsStmt, { limit, before });
27938
- }
27939
- /**
27940
- * Record that a capture is OWED to the deployment.
27941
- *
27942
- * Written by the attached forward path when a live send did not confirm
27943
- * delivery, and read by the drain as the whole of its eligibility test. It is
27944
- * a fact rather than an inference: the machine was attached, the send did not
27945
- * land, so the row is owed — which no time window can state, because the same
27946
- * window that holds the rows a past attachment left owed also holds every
27947
- * capture recorded while the machine was DETACHED, and those were never
27948
- * offered to anyone.
27949
- *
27950
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27951
- * out of the drain's read.
27952
- */
27953
- markCaptureOwed(id) {
27954
- this.markOwedStmt.run({ id });
27955
- }
27956
- /**
27957
- * Mark every capture already on disk as owed, as of `before`.
27958
- *
27959
- * The consent-time backfill, called once from `aka attach` when a human
27960
- * grants existing-history consent — never from an ongoing drain pass, and
27961
- * never inferred from a boundary that could later move. `before` is the
27962
- * caller's own "now" at the moment consent was granted, so what this marks
27963
- * is exactly the backlog the consent prompt already counted, not whatever a
27964
- * later re-attach or key rotation might widen it to.
27965
- *
27966
- * Returns how many rows matched, for the caller to log or test against. Not a
27967
- * count of NEWLY marked rows — a row still unsynced from an earlier call
27968
- * matches again and is counted again, the same as `UPDATE`'s own `changes`.
27969
- */
27970
- markCaptureBacklogOwed(before) {
27971
- return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
27972
- }
27973
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27974
- markSynced(ids, atMs) {
27975
- this.stampAll(ids, atMs);
27976
- }
27977
- /**
27978
- * Record that a row will never be sent.
27979
- *
27980
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27981
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27982
- * is retried; marking those would turn one outage into permanent data loss.
27983
- */
27984
- markSkipped(ids) {
27985
- this.stampAll(ids, SKIPPED);
27986
- }
27987
- eachInTransaction(ids, run) {
27988
- if (ids.length === 0) return;
27989
- withTransaction(
27990
- this.db,
27991
- () => {
27992
- for (const id of ids) run(id);
27993
- },
27994
- "IMMEDIATE"
27995
- );
27996
- }
27997
- stampAll(ids, value) {
27998
- if (ids.length === 0) return;
27999
- withTransaction(
28000
- this.db,
28001
- () => {
28002
- for (const id of ids) this.stampStmt.run({ at: value, id });
28003
- },
28004
- "IMMEDIATE"
28005
- );
28006
- }
28007
- /**
28008
- * Claim rows as in-flight.
28009
- *
28010
- * Advisory in exactly the sense the lease is: it records that a send is in
28011
- * progress so a surface can say so, and a lost claim costs a row showing as
28012
- * queued while it is actually being sent. It is not exclusion — the far side
28013
- * settles a duplicate on the row id.
28014
- */
28015
- claimRows(ids, atMs) {
28016
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
28017
- }
28018
- /** Give back a claim without settling — the send failed, the row is queued again. */
28019
- releaseRows(ids) {
28020
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
28021
- }
28022
- /**
28023
- * Clear claims older than `staleBefore`, and report how many were cleared.
28024
- *
28025
- * A process killed between claiming and settling leaves rows claimed with
28026
- * nothing left to settle them. Without this they read as "sending" for ever.
28027
- */
28028
- releaseStaleClaims(staleBefore) {
28029
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
28030
- }
28031
- /**
28032
- * Every tracked row in exactly one delivery state.
28033
- *
28034
- * Takes no boundary on purpose. The boundary answers "what should the drain
28035
- * pick up now", which is a different question from "what state is this row
28036
- * in" — and a machine that has never attached has no boundary to pass, so
28037
- * requiring one would force a caller to invent one and report the whole store
28038
- * as queued.
28039
- */
28040
- partition() {
28041
- const row = getRow(this.partitionStmt, {});
28042
- return {
28043
- queued: row?.queued ?? 0,
28044
- inProgress: row?.inProgress ?? 0,
28045
- synced: row?.synced ?? 0,
28046
- failed: row?.failed ?? 0,
28047
- total: row?.total ?? 0
28048
- };
28049
- }
28050
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
28051
- counts(before) {
28052
- const row = getRow(
28053
- this.countsStmt,
28054
- { before }
28055
- );
28056
- const captures = getRow(this.captureSkipCountStmt);
28057
- return {
28058
- pending: row?.pending ?? 0,
28059
- sent: row?.sent ?? 0,
28060
- skipped: row?.skipped ?? 0,
28061
- capturesSkipped: captures?.skipped ?? 0
28062
- };
28063
- }
28064
- /**
28065
- * The deployment the current stamps were made against, and where its backlog
28066
- * ends.
28067
- *
28068
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
28069
- * machine that has never drained is — and every writer below seeds the row
28070
- * before it needs one, so nothing depends on this creating it. Keeping the
28071
- * write off the gate path matters because the gate runs on every pass while a
28072
- * write has to take the database's write lock.
28073
- */
28074
- deployment() {
28075
- const row = getRow(
28076
- this.fingerprintStmt
28077
- );
28078
- return {
28079
- fingerprint: row?.fingerprint ?? void 0,
28080
- backlogBefore: row?.backlogBefore ?? void 0
28081
- };
28082
- }
28083
- /**
28084
- * Point the ledger at a different deployment, discarding what it recorded
28085
- * about the previous one.
28086
- *
28087
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
28088
- * machine has just left are undelivered as far as the new one is concerned.
28089
- * All four in one transaction, so a crash between them cannot leave stamps
28090
- * attributed to the wrong deployment, a boundary that belongs to another, or
28091
- * a disown with no re-mark to follow it.
28092
- *
28093
- * The boundary is written HERE and only here, which is what freezes it: a
28094
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28095
- * unchanged, so this never runs and the backlog does not widen back over rows
28096
- * the live path has since delivered.
28097
- *
28098
- * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
28099
- * granted existing-history consent for the deployment this call is arming —
28100
- * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
28101
- * instant, `backlogBefore` is the ATTACH instant, and the two can be far
28102
- * apart. Passed only when that grant is valid, since this method has no way
28103
- * to check consent itself and must not mark a row owed for a machine that
28104
- * never agreed to it. Applied AFTER the disown above, in the SAME
28105
- * transaction: what the disown clears is every marker below `backlogBefore`,
28106
- * which includes this deployment's OWN pre-attach rows — `aka attach` calls
28107
- * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
28108
- * on the cleared side of that bound — and the re-mark in the same
28109
- * transaction is what puts those rows back. A crash between the two cannot
28110
- * strand the ledger disowned with nothing re-marked — the transaction either
28111
- * lands whole or not at all, and a fingerprint mismatch that has not yet
28112
- * committed re-enters this method on the very next pass. Omit it (the
28113
- * structural-only tests do) to exercise the disown in isolation.
28114
- *
28115
- * The disown is bounded by `backlogBefore`, which is what keeps it from
28116
- * touching a marker the NEW deployment's OWN live path has already set: B's
28117
- * live path can mark a capture owed from the moment `aka attach` writes the
28118
- * descriptor, before the drain's first pass ever reaches this method, and
28119
- * such a row sits at or after the bound rather than below it. What keeps the
28120
- * disown from eating THIS SAME CALL's own re-mark is the order, not the
28121
- * bound — disown runs first, re-mark second, both inside the one
28122
- * transaction above.
28123
- */
28124
- rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
28125
- this.ensureRowStmt.run();
28126
- withTransaction(
28127
- this.db,
28128
- () => {
28129
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28130
- this.rearmStmt.run();
28131
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28132
- this.disownCapturesStmt.run({ attachedAt: backlogBefore });
28133
- }
28134
- if (backfillCapturesBefore !== void 0) {
28135
- this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
28136
- }
28137
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28138
- },
28139
- "IMMEDIATE"
28140
- );
28141
- }
28142
- /**
28143
- * End the attached period: hand its rows to the live path, and release the
28144
- * boundary so the next attachment can freeze a new one.
28145
- *
28146
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28147
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28148
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28149
- * during the detached period, because the machine is not attached. Rows
28150
- * recorded in that window sit after the boundary and before the re-attach, so
28151
- * neither path takes them, and the pending count reports none outstanding.
28152
- *
28153
- * Stamping the attached window is not a claim that every one of those rows
28154
- * reached the deployment — the live path drops on failure and says so
28155
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28156
- * status quo: they sit outside the frozen boundary today and are equally never
28157
- * re-sent. Making it explicit is what lets the boundary move.
28158
- *
28159
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28160
- * window unstamped — that half-state would re-send the whole attached period
28161
- * on the next attach, which is the failure the boundary exists to prevent.
28162
- */
28163
- closeAttachedWindow(attachedAtMs, atMs) {
28164
- this.ensureRowStmt.run();
28165
- withTransaction(
28166
- this.db,
28167
- () => {
28168
- const row = getRow(this.fingerprintStmt);
28169
- const from = row?.backlogBefore ?? attachedAtMs;
28170
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28171
- this.releaseBoundaryStmt.run();
28172
- },
28173
- "IMMEDIATE"
28174
- );
28175
- }
28176
- /**
28177
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28178
- *
28179
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28180
- * different deployment and therefore discards what was delivered to the old
28181
- * one: here the recipient is the same, so everything already sent to it stays
28182
- * sent.
28183
- */
28184
- freezeBoundary(backlogBefore) {
28185
- this.ensureRowStmt.run();
28186
- this.freezeBoundaryStmt.run({ backlogBefore });
28187
- }
28188
- /** Take the claim, or report that someone live already holds it. */
28189
- claim(pid, host, nowMs, staleAfterMs) {
28190
- this.ensureRowStmt.run();
28191
- let taken = false;
28192
- withTransaction(
28193
- this.db,
28194
- () => {
28195
- const result = this.claimStmt.run({
28196
- pid,
28197
- host,
28198
- now: nowMs,
28199
- staleBefore: nowMs - staleAfterMs
28200
- });
28201
- taken = result.changes === 1;
28202
- },
28203
- "IMMEDIATE"
28204
- );
28205
- return taken;
28206
- }
28207
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28208
- heartbeat(pid, nowMs) {
28209
- this.heartbeatStmt.run({ now: nowMs, pid });
28210
- }
28211
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28212
- release(pid) {
28213
- this.releaseStmt.run({ pid });
28214
- }
28215
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28216
- lease() {
28217
- return getRow(this.leaseStmt);
28218
- }
28219
- };
28220
-
28221
28832
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28222
28833
  var SqliteInspectionDefinitionsRepository = class {
28223
28834
  constructor(db) {
@@ -28445,6 +29056,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28445
29056
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28446
29057
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28447
29058
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29059
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28448
29060
  if (values.vaultConsent !== void 0) {
28449
29061
  merged.vaultConsent = values.vaultConsent ? (
28450
29062
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30952,7 +31564,7 @@ var SqliteSecurityRepository = class {
30952
31564
  ELSE 0
30953
31565
  END) AS open_at_rest
30954
31566
  FROM inspection_findings f
30955
- JOIN audit_events e ON e.id = f.audit_event_id
31567
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30956
31568
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30957
31569
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30958
31570
  ON latest.finding_key = f.finding_key
@@ -31178,7 +31790,7 @@ var SqliteSecurityRepository = class {
31178
31790
  this.db.prepare(
31179
31791
  `SELECT e.repo AS repo, count(*) AS c
31180
31792
  FROM inspection_findings f
31181
- JOIN audit_events e ON e.id = f.audit_event_id
31793
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31182
31794
  WHERE e.started_at >= :from AND e.started_at < :to
31183
31795
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31184
31796
  AND e.repo IS NOT NULL
@@ -31302,7 +31914,7 @@ var SqliteSecurityRepository = class {
31302
31914
  d.severity AS severity,
31303
31915
  COUNT(*) AS count
31304
31916
  FROM inspection_findings f
31305
- JOIN audit_events e ON e.id = f.audit_event_id
31917
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31306
31918
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31307
31919
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31308
31920
  ON latest.finding_key = f.finding_key
@@ -31337,7 +31949,7 @@ var SqliteSecurityRepository = class {
31337
31949
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31338
31950
  d.rule_id AS rule_id, d.category AS category
31339
31951
  FROM inspection_findings f
31340
- JOIN audit_events e ON e.id = f.audit_event_id
31952
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31341
31953
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31342
31954
  WHERE e.started_at >= :from AND e.started_at < :to
31343
31955
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32178,6 +32790,7 @@ function openWithPragmas(file2) {
32178
32790
  db.exec("PRAGMA journal_mode = WAL");
32179
32791
  db.exec("PRAGMA busy_timeout = 2000");
32180
32792
  db.exec("PRAGMA foreign_keys = ON");
32793
+ registerSqlFunctions(db);
32181
32794
  } catch (err) {
32182
32795
  closeQuietly(db);
32183
32796
  throw err;
@@ -32207,7 +32820,7 @@ function backupLegacyStore(db, file2) {
32207
32820
  discardStore(file2, backup);
32208
32821
  return backup;
32209
32822
  }
32210
- function openAndInitialize(file2, base) {
32823
+ function openAndInitialize(file2, base, skipTags) {
32211
32824
  let db = openWithPragmas(file2);
32212
32825
  try {
32213
32826
  if (isForeignSqliteLineage(db)) {
@@ -32217,7 +32830,7 @@ function openAndInitialize(file2, base) {
32217
32830
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32218
32831
  );
32219
32832
  }
32220
- applyMigrations(db, file2);
32833
+ applyMigrations(db, file2, { skipTags });
32221
32834
  tightenPerms(file2);
32222
32835
  const policies = new SqlitePoliciesRepository(db);
32223
32836
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32232,6 +32845,7 @@ function openAndInitialize(file2, base) {
32232
32845
  exceptions: new SqliteExceptionsRepository(db),
32233
32846
  resolutions: new SqliteResolutionsRepository(db),
32234
32847
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32848
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32235
32849
  security: new SqliteSecurityRepository(db),
32236
32850
  detections: new SqliteDetectionsRepository(db),
32237
32851
  shares: new SqliteSharesRepository(db),
@@ -32254,7 +32868,8 @@ function openAndInitialize(file2, base) {
32254
32868
  throw err;
32255
32869
  }
32256
32870
  }
32257
- function openLocalDatabase(dir) {
32871
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32872
+ function openLocalDatabase(dir, options = {}) {
32258
32873
  ensureDataDirSync(dir);
32259
32874
  const file2 = join7(dir, DB_FILENAME);
32260
32875
  reapStalePartials(file2);
@@ -32266,6 +32881,7 @@ function openLocalDatabase(dir) {
32266
32881
  installedPacks,
32267
32882
  scanLedger,
32268
32883
  historySync,
32884
+ bodyRetention,
32269
32885
  secretVault,
32270
32886
  exceptions,
32271
32887
  resolutions,
@@ -32289,7 +32905,8 @@ function openLocalDatabase(dir) {
32289
32905
  // `dir` is always `<base>/data` — every caller resolves it through
32290
32906
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32291
32907
  // settings/ and data/, and the pack-policy floor needs both halves.
32292
- dirname2(dir)
32908
+ dirname2(dir),
32909
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32293
32910
  );
32294
32911
  function captureRowId(event) {
32295
32912
  return captureId(
@@ -32482,6 +33099,7 @@ function openLocalDatabase(dir) {
32482
33099
  installedPacks,
32483
33100
  scanLedger,
32484
33101
  historySync,
33102
+ bodyRetention,
32485
33103
  secretVault,
32486
33104
  exceptions,
32487
33105
  resolutions,
@@ -32522,8 +33140,35 @@ function openLocalDatabase(dir) {
32522
33140
 
32523
33141
  // ../../packages/persistence/src/egress-wire.ts
32524
33142
  import { createHash as createHash3 } from "crypto";
33143
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33144
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33145
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33146
+ var FILE_URL = /^file:\/\//i;
33147
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33148
+ var SLASH = "/".charCodeAt(0);
33149
+ var GIT_SUFFIX = ".git";
33150
+ function trimSlashes(path) {
33151
+ let start = 0;
33152
+ let end = path.length;
33153
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33154
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33155
+ return path.slice(start, end);
33156
+ }
33157
+ function canonicalGitUrl(url2) {
33158
+ const trimmed = url2.trim();
33159
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33160
+ const scheme = SCHEME_FORM.exec(trimmed);
33161
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33162
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33163
+ if (host === void 0) return trimmed;
33164
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33165
+ const bare = trimSlashes(path);
33166
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33167
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33168
+ }
32525
33169
  function hashProjectKey(projectKey) {
32526
- return createHash3("sha256").update(projectKey, "utf8").digest("hex");
33170
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33171
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
32527
33172
  }
32528
33173
  function toIngestHit(hit) {
32529
33174
  return {
@@ -32690,18 +33335,50 @@ function fingerprintValue(key, raw) {
32690
33335
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32691
33336
  }
32692
33337
 
33338
+ // ../../packages/persistence/src/forward-health.ts
33339
+ import { readFileSync as readFileSync7 } from "fs";
33340
+ import { join as join9 } from "path";
33341
+ var FAILURES = /* @__PURE__ */ new Set([
33342
+ "unauthorized",
33343
+ "forbidden",
33344
+ "unreachable"
33345
+ ]);
33346
+ var BREAKER_COOLDOWN_MS = 3e4;
33347
+ function parseForwardHealth(raw, nowMs) {
33348
+ try {
33349
+ const parsed2 = JSON.parse(raw);
33350
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33351
+ const record2 = parsed2;
33352
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33353
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33354
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33355
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33356
+ } catch {
33357
+ return null;
33358
+ }
33359
+ }
33360
+ function isForwardPaused(health, nowMs) {
33361
+ const openedAtMs = health?.openedAtMs ?? null;
33362
+ if (openedAtMs === null) return false;
33363
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33364
+ }
33365
+
32693
33366
  // ../../packages/persistence/src/history-backfill.ts
32694
33367
  import { existsSync as existsSync4 } from "fs";
32695
- import { join as join9 } from "path";
33368
+ import { join as join10 } from "path";
32696
33369
 
32697
33370
  // ../../packages/persistence/src/history-preview.ts
32698
33371
  import { existsSync as existsSync5 } from "fs";
32699
- import { join as join10 } from "path";
33372
+ import { join as join11 } from "path";
32700
33373
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32701
33374
 
33375
+ // ../../packages/persistence/src/history-sync-state.ts
33376
+ import { readFileSync as readFileSync8 } from "fs";
33377
+ import { join as join12 } from "path";
33378
+
32702
33379
  // ../../packages/persistence/src/store-symlinks.ts
32703
33380
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32704
- import { dirname as dirname3, join as join11, resolve } from "path";
33381
+ import { dirname as dirname3, join as join13, resolve } from "path";
32705
33382
 
32706
33383
  // ../../packages/persistence/src/vault/crypto.ts
32707
33384
  import {
@@ -32715,19 +33392,19 @@ import {
32715
33392
  // ../../packages/persistence/src/vault/key-provider.ts
32716
33393
  import { execFileSync } from "child_process";
32717
33394
  import { randomBytes as randomBytes2 } from "crypto";
32718
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32719
- import { join as join12 } from "path";
33395
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33396
+ import { join as join14 } from "path";
32720
33397
 
32721
33398
  // ../../packages/persistence/src/vault/vault.ts
32722
33399
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32723
33400
 
32724
33401
  // ../../packages/persistence/src/warn-era-cap.ts
32725
33402
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
32726
- import { join as join13 } from "path";
33403
+ import { join as join15 } from "path";
32727
33404
  var MARKER = "warn-era-capped";
32728
33405
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32729
33406
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32730
- const marker = join13(dataDir2, MARKER);
33407
+ const marker = join15(dataDir2, MARKER);
32731
33408
  if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32732
33409
  const capped = db.policies.capCategoryActions();
32733
33410
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
@@ -32787,7 +33464,7 @@ function resolveProvider() {
32787
33464
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32788
33465
  try {
32789
33466
  ensureLayoutDirSync(base);
32790
- const settingsFile = join14(settingsDir(base), "settings.json");
33467
+ const settingsFile = join16(settingsDir(base), "settings.json");
32791
33468
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
32792
33469
  } catch {
32793
33470
  }
@@ -32811,9 +33488,9 @@ function resolveProviderSafe(resolveProviderFn) {
32811
33488
  }
32812
33489
 
32813
33490
  // ../../packages/plugin-sdk/src/config-inventory.ts
32814
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33491
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32815
33492
  import { homedir as homedir2 } from "os";
32816
- import { basename as basename3, join as join16 } from "path";
33493
+ import { basename as basename3, join as join18 } from "path";
32817
33494
 
32818
33495
  // ../../packages/detections/src/egress/registry.ts
32819
33496
  var EXTRACTOR_VERSION = "1";
@@ -35944,8 +36621,8 @@ function scanText(text, ruleVersions) {
35944
36621
  }
35945
36622
 
35946
36623
  // ../../packages/plugin-sdk/src/repo.ts
35947
- import { existsSync as existsSync9, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
35948
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
36624
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
36625
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
35949
36626
  function resolveRepoIdentity(cwd) {
35950
36627
  try {
35951
36628
  const root = findGitRoot(cwd);
@@ -35967,36 +36644,36 @@ function resolveRepoIdentity(cwd) {
35967
36644
  function findGitRoot(start) {
35968
36645
  let dir = start;
35969
36646
  for (; ; ) {
35970
- if (existsSync9(join15(dir, ".git"))) return dir;
36647
+ if (existsSync9(join17(dir, ".git"))) return dir;
35971
36648
  const parent = dirname4(dir);
35972
36649
  if (parent === dir) return void 0;
35973
36650
  dir = parent;
35974
36651
  }
35975
36652
  }
35976
36653
  function resolveGitContext(root) {
35977
- const dotGit = join15(root, ".git");
36654
+ const dotGit = join17(root, ".git");
35978
36655
  try {
35979
36656
  if (statSync6(dotGit).isDirectory()) {
35980
- return { configPath: join15(dotGit, "config"), headRoot: root };
36657
+ return { configPath: join17(dotGit, "config"), headRoot: root };
35981
36658
  }
35982
36659
  } catch {
35983
36660
  return void 0;
35984
36661
  }
35985
36662
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
35986
36663
  if (!target) return void 0;
35987
- const gitdir = isAbsolute(target) ? target : join15(root, target);
35988
- if (existsSync9(join15(gitdir, "config"))) {
35989
- return { configPath: join15(gitdir, "config"), headRoot: root };
36664
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
36665
+ if (existsSync9(join17(gitdir, "config"))) {
36666
+ return { configPath: join17(gitdir, "config"), headRoot: root };
35990
36667
  }
35991
- const commonRaw = safeRead(join15(gitdir, "commondir"))?.trim();
36668
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
35992
36669
  if (!commonRaw) return void 0;
35993
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join15(gitdir, commonRaw);
36670
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
35994
36671
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
35995
- return { configPath: join15(commonGitDir, "config"), headRoot };
36672
+ return { configPath: join17(commonGitDir, "config"), headRoot };
35996
36673
  }
35997
36674
  function safeRead(path) {
35998
36675
  try {
35999
- return readFileSync8(path, "utf8");
36676
+ return readFileSync10(path, "utf8");
36000
36677
  } catch {
36001
36678
  return void 0;
36002
36679
  }
@@ -36540,8 +37217,8 @@ function createGuardedScanner(partition, gateway, opts) {
36540
37217
  }
36541
37218
 
36542
37219
  // ../../packages/plugin-sdk/src/host-floor.ts
36543
- import { readFileSync as readFileSync11 } from "fs";
36544
- import { join as join18 } from "path";
37220
+ import { readFileSync as readFileSync13 } from "fs";
37221
+ import { join as join20 } from "path";
36545
37222
 
36546
37223
  // ../../packages/plugin-sdk/src/model-governance.ts
36547
37224
  import {
@@ -36549,11 +37226,11 @@ import {
36549
37226
  fstatSync,
36550
37227
  mkdirSync as mkdirSync2,
36551
37228
  openSync as openSync2,
36552
- readFileSync as readFileSync10,
37229
+ readFileSync as readFileSync12,
36553
37230
  readSync,
36554
37231
  writeFileSync as writeFileSync5
36555
37232
  } from "fs";
36556
- import { join as join17 } from "path";
37233
+ import { join as join19 } from "path";
36557
37234
  var TAIL_BYTES = 256 * 1024;
36558
37235
 
36559
37236
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -36576,8 +37253,8 @@ var HOST_FLOORS = {
36576
37253
 
36577
37254
  // ../../packages/plugin-sdk/src/ignore-layers.ts
36578
37255
  var import_ignore = __toESM(require_ignore(), 1);
36579
- import { readFileSync as readFileSync12 } from "fs";
36580
- import { join as join19 } from "path";
37256
+ import { readFileSync as readFileSync14 } from "fs";
37257
+ import { join as join21 } from "path";
36581
37258
 
36582
37259
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
36583
37260
  import { arch, hostname as hostname4, platform, release } from "os";
@@ -36609,8 +37286,8 @@ function resolveInventoryContext(input2) {
36609
37286
  }
36610
37287
 
36611
37288
  // ../../packages/plugin-sdk/src/nudge.ts
36612
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
36613
- import { join as join20 } from "path";
37289
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
37290
+ import { join as join22 } from "path";
36614
37291
 
36615
37292
  // ../../packages/plugin-sdk/src/paths.ts
36616
37293
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -36653,7 +37330,7 @@ function createPolicyResolver(bundle) {
36653
37330
 
36654
37331
  // ../../packages/plugin-sdk/src/project-files.ts
36655
37332
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
36656
- import { basename as basename5, join as join21 } from "path";
37333
+ import { basename as basename5, join as join23 } from "path";
36657
37334
 
36658
37335
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
36659
37336
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -36778,7 +37455,7 @@ function createPluginRuntime(gateway, settings, opts) {
36778
37455
  bundlesPacked = true;
36779
37456
  }
36780
37457
  const policyMode = settings.policy;
36781
- const redactFallback = settings.redactFallback;
37458
+ let redactFallback = settings.redactFallback;
36782
37459
  const dataDir2 = opts?.dataDir;
36783
37460
  let rules = [];
36784
37461
  let scanner;
@@ -36822,6 +37499,7 @@ function createPluginRuntime(gateway, settings, opts) {
36822
37499
  rules = [...verified, ...unverified];
36823
37500
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
36824
37501
  bundleExceptions = bundle.exceptions ?? [];
37502
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
36825
37503
  initialized = true;
36826
37504
  }
36827
37505
  let cachedKey;
@@ -36860,11 +37538,13 @@ function createPluginRuntime(gateway, settings, opts) {
36860
37538
  function decide(findings, text, excepted, rewritable = true) {
36861
37539
  if (findings.length === 0) return { action: "log", text, findings: [] };
36862
37540
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
37541
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
37542
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
36863
37543
  let worst = "log";
36864
37544
  for (const finding of findings) {
36865
37545
  worst = strongerAction(worst, actionFor(finding));
36866
37546
  }
36867
- if (worst === "block") return { action: "block", text: null, findings };
37547
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
36868
37548
  if (worst === "redact") {
36869
37549
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
36870
37550
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -36874,9 +37554,13 @@ function createPluginRuntime(gateway, settings, opts) {
36874
37554
  findings,
36875
37555
  enforcedFindings: redactFindings,
36876
37556
  reversibleFindings
37557
+ // No `degraded` here, and it is not an omission: `rewritable` is per
37558
+ // CAPTURE, so on an unrewritable field every redact has already become
37559
+ // the fallback and this branch is unreachable. Spreading it would read
37560
+ // as a case that can happen.
36877
37561
  };
36878
37562
  }
36879
- return { action: worst, text, findings };
37563
+ return { action: worst, text, findings, ...degraded };
36880
37564
  }
36881
37565
  function fingerprintOf(key, finding, cache) {
36882
37566
  let fp = cache.get(finding);
@@ -37005,8 +37689,8 @@ function createPluginRuntime(gateway, settings, opts) {
37005
37689
  };
37006
37690
  }
37007
37691
  }
37008
- async function processText(text, context) {
37009
- return (await evaluate(text, context, {})).decision;
37692
+ async function processText(text, context, opts2 = {}) {
37693
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
37010
37694
  }
37011
37695
  async function capture(input2, opts2 = {}) {
37012
37696
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -37029,10 +37713,12 @@ function createPluginRuntime(gateway, settings, opts) {
37029
37713
  );
37030
37714
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
37031
37715
  const inspectionMs = elapsedMs(timingStartedAt);
37032
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
37716
+ const redactDegradedTo = decision.redactDegradedTo;
37717
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
37033
37718
  ...input2.metadata,
37034
37719
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
37035
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
37720
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
37721
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
37036
37722
  } : input2.metadata;
37037
37723
  const event = buildIngestEvent({
37038
37724
  kind: input2.kind,
@@ -37104,11 +37790,11 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
37104
37790
 
37105
37791
  // ../../packages/plugin-sdk/src/throttle.ts
37106
37792
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
37107
- import { join as join22 } from "path";
37793
+ import { join as join24 } from "path";
37108
37794
 
37109
37795
  // ../../packages/setup-wizard/src/remediation/rotation-checklist.ts
37110
37796
  import { writeFileSync as writeFileSync8 } from "fs";
37111
- import { join as join23 } from "path";
37797
+ import { join as join25 } from "path";
37112
37798
 
37113
37799
  // ../../packages/setup-wizard/src/triage/merge.ts
37114
37800
  var RANK = Object.fromEntries(
@@ -37116,9 +37802,9 @@ var RANK = Object.fromEntries(
37116
37802
  );
37117
37803
 
37118
37804
  // ../../packages/setup-wizard/src/triage/plan-file.ts
37119
- import { mkdtempSync, readFileSync as readFileSync14, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37805
+ import { mkdtempSync, readFileSync as readFileSync16, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37120
37806
  import { tmpdir } from "os";
37121
- import { basename as basename6, dirname as dirname6, join as join24 } from "path";
37807
+ import { basename as basename6, dirname as dirname6, join as join26 } from "path";
37122
37808
  var SuppressionEntrySchema = external_exports.object({
37123
37809
  ruleId: external_exports.string(),
37124
37810
  category: DetectionCategory,
@@ -37345,10 +38031,10 @@ function parsed(schema, body, route) {
37345
38031
  }
37346
38032
  function withoutTrailingSlashes(endpoint) {
37347
38033
  let end = endpoint.length;
37348
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
38034
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
37349
38035
  return endpoint.slice(0, end);
37350
38036
  }
37351
- var SLASH = "/".charCodeAt(0);
38037
+ var SLASH2 = "/".charCodeAt(0);
37352
38038
  function createRemoteClient(options) {
37353
38039
  const base = withoutTrailingSlashes(options.endpoint);
37354
38040
  const url2 = (route) => `${base}${route}`;
@@ -37530,11 +38216,11 @@ function withTimeout(promise2, ms) {
37530
38216
  }
37531
38217
 
37532
38218
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
37533
- import { readFileSync as readFileSync15 } from "fs";
37534
- import { join as join25 } from "path";
38219
+ import { readFileSync as readFileSync17 } from "fs";
38220
+ import { join as join27 } from "path";
37535
38221
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
37536
38222
  function forwardDropsPath(dataDir2) {
37537
- return join25(dataDir2, FORWARD_DROPS_FILENAME);
38223
+ return join27(dataDir2, FORWARD_DROPS_FILENAME);
37538
38224
  }
37539
38225
  function recordForwardDrops(dataDir2, count, nowMs) {
37540
38226
  if (count <= 0) return;
@@ -37552,7 +38238,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
37552
38238
  }
37553
38239
  function readForwardDrops(dataDir2) {
37554
38240
  try {
37555
- const parsed2 = JSON.parse(readFileSync15(forwardDropsPath(dataDir2), "utf8"));
38241
+ const parsed2 = JSON.parse(readFileSync17(forwardDropsPath(dataDir2), "utf8"));
37556
38242
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
37557
38243
  const record2 = parsed2;
37558
38244
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -37570,9 +38256,8 @@ function readForwardDrops(dataDir2) {
37570
38256
 
37571
38257
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
37572
38258
  import { randomUUID as randomUUID15 } from "crypto";
37573
- import { readFileSync as readFileSync16 } from "fs";
37574
38259
  import { readFile, rename, writeFile } from "fs/promises";
37575
- import { join as join26 } from "path";
38260
+ import { join as join28 } from "path";
37576
38261
  function isInvalidRequest(err) {
37577
38262
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
37578
38263
  }
@@ -37586,31 +38271,12 @@ function isServerRejection(err) {
37586
38271
  var FORWARD_BUDGET_MS = 1500;
37587
38272
  var DECISION_PATH_BUDGET_MS = 800;
37588
38273
  var BREAKER_FAILURE_THRESHOLD = 3;
37589
- var BREAKER_COOLDOWN_MS = 3e4;
37590
38274
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
37591
- var FAILURES = /* @__PURE__ */ new Set([
37592
- "unauthorized",
37593
- "forbidden",
37594
- "unreachable"
37595
- ]);
37596
38275
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
37597
38276
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
37598
- function parseBreakerState(raw, nowMs) {
37599
- try {
37600
- const parsed2 = JSON.parse(raw);
37601
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
37602
- const record2 = parsed2;
37603
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
37604
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
37605
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
37606
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
37607
- } catch {
37608
- return null;
37609
- }
37610
- }
37611
38277
  function createForwardPolicy(deps) {
37612
38278
  const now = deps.now ?? (() => Date.now());
37613
- const file2 = join26(deps.dir, STATE_FILENAME);
38279
+ const file2 = join28(deps.dir, STATE_FILENAME);
37614
38280
  let state = null;
37615
38281
  let loading = null;
37616
38282
  async function readState() {
@@ -37620,7 +38286,7 @@ function createForwardPolicy(deps) {
37620
38286
  } catch {
37621
38287
  return { ...CLOSED };
37622
38288
  }
37623
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
38289
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
37624
38290
  }
37625
38291
  async function load() {
37626
38292
  if (state !== null) return state;
@@ -37666,7 +38332,7 @@ function createForwardPolicy(deps) {
37666
38332
  };
37667
38333
  const at = now();
37668
38334
  if (current.openedAtMs !== null) {
37669
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
38335
+ if (isForwardPaused(current, at)) {
37670
38336
  return { ok: false, reason: "breaker-open" };
37671
38337
  }
37672
38338
  await persist({
@@ -38203,7 +38869,18 @@ var AttachedDataGateway = class {
38203
38869
  // and the spread above would otherwise drop the field silently — which is
38204
38870
  // exactly what it did, leaving the whole control inert on every device
38205
38871
  // while every test around it stayed green.
38206
- prohibitedModels: cached2.prohibitedModels
38872
+ prohibitedModels: cached2.prohibitedModels,
38873
+ // NAMED for the same reason as the line above, and it is the same defect
38874
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
38875
+ // only the cache carries is dropped in silence. That is what left
38876
+ // `prohibitedModels` inert on every attached device with every test
38877
+ // around it green.
38878
+ //
38879
+ // Taken from the cache rather than merged here, because merging it needs
38880
+ // the device's own SETTING — which is not a bundle field and is not in
38881
+ // scope at this seam. The runtime does that merge, raise-only, where both
38882
+ // values are in hand (createPluginRuntime's ensureInitialized).
38883
+ redactFallback: cached2.redactFallback
38207
38884
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
38208
38885
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
38209
38886
  // it emits, so an 'authored' policy arriving from the control plane
@@ -38331,10 +39008,6 @@ function toolAuditEvent(input2) {
38331
39008
  };
38332
39009
  }
38333
39010
 
38334
- // ../../packages/plugin-runtime/src/attached/history-state.ts
38335
- import { readFileSync as readFileSync17 } from "fs";
38336
- import { join as join27 } from "path";
38337
-
38338
39011
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
38339
39012
  import { createHash as createHash6 } from "crypto";
38340
39013
  import { hostname as hostname5 } from "os";
@@ -38343,6 +39016,10 @@ import { hostname as hostname5 } from "os";
38343
39016
  var CORRELATION_ID = EventMetadata.shape.correlationId;
38344
39017
  var TRACE_ID = EventMetadata.shape.traceId;
38345
39018
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
39019
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
39020
+
39021
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
39022
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
38346
39023
 
38347
39024
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
38348
39025
  import { spawn } from "child_process";
@@ -38384,7 +39061,7 @@ function createPluginBlock(build, policyStore) {
38384
39061
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38385
39062
  import { randomUUID as randomUUID16 } from "crypto";
38386
39063
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
38387
- import { join as join28 } from "path";
39064
+ import { join as join29 } from "path";
38388
39065
 
38389
39066
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
38390
39067
  import { rename as rename2 } from "fs/promises";
@@ -38408,7 +39085,7 @@ async function publishByRename(tmp, file2, move = rename2) {
38408
39085
 
38409
39086
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38410
39087
  function createPolicyStore(dir = dataDir()) {
38411
- const file2 = join28(dir, "policy-cache.json");
39088
+ const file2 = join29(dir, "policy-cache.json");
38412
39089
  async function read() {
38413
39090
  try {
38414
39091
  const raw = await readFile2(file2, "utf8");
@@ -38639,11 +39316,11 @@ function readStorePosture(dbPath2) {
38639
39316
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
38640
39317
  import { randomUUID as randomUUID17 } from "crypto";
38641
39318
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
38642
- import { join as join29 } from "path";
39319
+ import { join as join30 } from "path";
38643
39320
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
38644
39321
  function createPostureStore(dir = settingsDir(), legacyDir) {
38645
- const file2 = join29(dir, "posture-state.json");
38646
- const legacyFile = legacyDir === void 0 ? null : join29(legacyDir, "posture-state.json");
39322
+ const file2 = join30(dir, "posture-state.json");
39323
+ const legacyFile = legacyDir === void 0 ? null : join30(legacyDir, "posture-state.json");
38647
39324
  async function persist(state) {
38648
39325
  await ensureDataDir(dir);
38649
39326
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -38712,7 +39389,7 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
38712
39389
 
38713
39390
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
38714
39391
  import { readFileSync as readFileSync19 } from "fs";
38715
- import { join as join30 } from "path";
39392
+ import { join as join31 } from "path";
38716
39393
 
38717
39394
  // ../../packages/plugin-runtime/src/attached/status.ts
38718
39395
  var REFUSAL_LINES = {
@@ -38733,6 +39410,14 @@ import { spawn as spawn2 } from "child_process";
38733
39410
  import { fileURLToPath as fileURLToPath3 } from "url";
38734
39411
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
38735
39412
 
39413
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
39414
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
39415
+
39416
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
39417
+ import { spawn as spawn3 } from "child_process";
39418
+ import { fileURLToPath as fileURLToPath4 } from "url";
39419
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
39420
+
38736
39421
  // ../../packages/plugin-runtime/src/attached/factory.ts
38737
39422
  import { hostname as hostname6 } from "os";
38738
39423
 
@@ -39185,9 +39870,9 @@ var EXCEPTION_RETENTION_MS = 90 * 24 * 60 * 60 * 1e3;
39185
39870
  // src/history/transcripts.ts
39186
39871
  import { closeSync as closeSync3, openSync as openSync3, readdirSync as readdirSync5, readFileSync as readFileSync20, readSync as readSync2, statSync as statSync10 } from "fs";
39187
39872
  import { homedir as homedir3 } from "os";
39188
- import { join as join31 } from "path";
39873
+ import { join as join32 } from "path";
39189
39874
  function transcriptsDir(home) {
39190
- return join31(home ?? homedir3(), ".codex", "sessions");
39875
+ return join32(home ?? homedir3(), ".codex", "sessions");
39191
39876
  }
39192
39877
  function isRecord(value) {
39193
39878
  return typeof value === "object" && value !== null;
@@ -39426,7 +40111,7 @@ function* walkJsonlFiles(dir, depth = 0) {
39426
40111
  return;
39427
40112
  }
39428
40113
  for (const entry of entries) {
39429
- const full = join31(dir, entry.name);
40114
+ const full = join32(dir, entry.name);
39430
40115
  if (entry.isDirectory()) {
39431
40116
  yield* walkJsonlFiles(full, depth + 1);
39432
40117
  } else if (entry.isFile() && entry.name.endsWith(".jsonl")) {
@@ -39563,7 +40248,7 @@ import {
39563
40248
  readSync as readSync3,
39564
40249
  writeFileSync as writeFileSync10
39565
40250
  } from "fs";
39566
- import { join as join32 } from "path";
40251
+ import { join as join33 } from "path";
39567
40252
 
39568
40253
  // src/history/usage.ts
39569
40254
  var NO_PROJECT_CWD = "/nonexistent/aka-reconciler/no-project";
@@ -39866,7 +40551,7 @@ async function runBackfill(deps) {
39866
40551
  }
39867
40552
  }
39868
40553
  }
39869
- if (process.argv[1] && fileURLToPath4(import.meta.url) === process.argv[1]) {
40554
+ if (process.argv[1] && fileURLToPath5(import.meta.url) === process.argv[1]) {
39870
40555
  const triage = process.argv.includes("--triage");
39871
40556
  const startedAt = Date.now();
39872
40557
  await runBackfill({