@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
@@ -502,6 +502,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
502
502
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
503
503
  import { join as join2 } from "path";
504
504
 
505
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
506
+ var DEFERRED_MIGRATION_TAGS = [
507
+ "0031_audit_capture_by_time_index",
508
+ "0032_audit_capture_by_id_index",
509
+ "0033_audit_capture_location_index",
510
+ "0034_findings_read_indexes"
511
+ ];
512
+
505
513
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
506
514
  var SQLITE_MIGRATIONS = [
507
515
  {
@@ -619,6 +627,30 @@ var SQLITE_MIGRATIONS = [
619
627
  {
620
628
  tag: "0028_activity_session_probe_indexes",
621
629
  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"
630
+ },
631
+ {
632
+ tag: "0029_audit_capture_rollup_index",
633
+ 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');"
634
+ },
635
+ {
636
+ tag: "0030_audit_content_expiry",
637
+ 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;"
638
+ },
639
+ {
640
+ tag: "0031_audit_capture_by_time_index",
641
+ 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');"
642
+ },
643
+ {
644
+ tag: "0032_audit_capture_by_id_index",
645
+ 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');"
646
+ },
647
+ {
648
+ tag: "0033_audit_capture_location_index",
649
+ 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');"
650
+ },
651
+ {
652
+ tag: "0034_findings_read_indexes",
653
+ 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`);"
622
654
  }
623
655
  ];
624
656
 
@@ -20666,6 +20698,15 @@ var FindingCategory = external_exports.enum([
20666
20698
  ]).meta({ id: "FindingCategory" });
20667
20699
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20668
20700
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20701
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20702
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20703
+ var FindingDelivery = external_exports.object({
20704
+ state: FindingDeliveryState,
20705
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20706
+ at: external_exports.iso.datetime().optional(),
20707
+ // Only on `not_sent`, and only when a known reason was recorded.
20708
+ reason: SyncFailureReason.optional()
20709
+ }).meta({ id: "FindingDelivery" });
20669
20710
  var ResolutionMethod = external_exports.enum([
20670
20711
  "enforced-in-flight",
20671
20712
  "fixed-at-source",
@@ -20722,7 +20763,10 @@ var FindingInstance = external_exports.object({
20722
20763
  // The session that event belongs to, when it has one — the seam a
20723
20764
  // per-instance "view session" link needs. Absent for events captured
20724
20765
  // outside a session.
20725
- sessionId: external_exports.string().optional()
20766
+ sessionId: external_exports.string().optional(),
20767
+ // The delivery state of the event above (see FindingDelivery). Optional so
20768
+ // readers that do not project it stay valid.
20769
+ delivery: FindingDelivery.optional()
20726
20770
  }).meta({ id: "FindingInstance" });
20727
20771
  var FindingGroup = external_exports.object({
20728
20772
  id: external_exports.string(),
@@ -20774,7 +20818,10 @@ var FindingFacets = external_exports.object({
20774
20818
  // Host tool (attributes.tool_name). Present only on the instance-level
20775
20819
  // reads, which can filter by it; the type-level read omits the dimension
20776
20820
  // because a group spans tools.
20777
- tool: external_exports.array(FindingFacetItem).optional()
20821
+ tool: external_exports.array(FindingFacetItem).optional(),
20822
+ // Delivery states (FindingDeliveryState). Present only on the
20823
+ // instance-level reads, like `tool`.
20824
+ deployment: external_exports.array(FindingFacetItem).optional()
20778
20825
  }).meta({ id: "FindingFacets" });
20779
20826
  var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20780
20827
  id: "FindingTypeSummary"
@@ -20885,6 +20932,8 @@ var ListFindingInstancesQuery = external_exports.object({
20885
20932
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20886
20933
  // where the free-text `q` can only match the rendered "via Bash" label.
20887
20934
  tool: external_exports.array(external_exports.string()).optional(),
20935
+ // The delivery state of each finding's event (see FindingDelivery).
20936
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20888
20937
  // Exact repository / file-path matches, for the drill-down out of the
20889
20938
  // locations view. A row whose event carries no repo/file matches neither.
20890
20939
  repo: external_exports.string().optional(),
@@ -20905,6 +20954,10 @@ var ListFindingInstancesResponse = external_exports.object({
20905
20954
  items: external_exports.array(FindingInstanceDetail),
20906
20955
  nextCursor: external_exports.string().nullable()
20907
20956
  }).meta({ id: "ListFindingInstancesResponse" });
20957
+ var ListFindingInstancesPage = external_exports.object({
20958
+ items: external_exports.array(FindingInstanceDetail),
20959
+ nextCursor: external_exports.string().nullable()
20960
+ }).meta({ id: "ListFindingInstancesPage" });
20908
20961
  var FindingLocationSummary = external_exports.object({
20909
20962
  // Opaque, stable, minted from the pair by encodeLocationId. It exists
20910
20963
  // because a location's identity is two values and a URL param carries one:
@@ -20947,6 +21000,8 @@ var ListFindingLocationsQuery = external_exports.object({
20947
21000
  // instances that match, and folds its status from those.
20948
21001
  status: external_exports.array(FindingStatus).optional(),
20949
21002
  tool: external_exports.array(external_exports.string()).optional(),
21003
+ // The delivery state of each finding's event (see FindingDelivery).
21004
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20950
21005
  q: external_exports.string().optional(),
20951
21006
  sessionId: external_exports.string().optional(),
20952
21007
  from: external_exports.iso.datetime().optional(),
@@ -21149,6 +21204,10 @@ var CaptureAttributes = external_exports.object({
21149
21204
  // to 'allow' — the enforcement audit trail's link back to the grant that
21150
21205
  // authorized the bypass.
21151
21206
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21207
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21208
+ // join back to the `llm_call` leaf for the same assistant turn.
21209
+ message_id: external_exports.string().optional(),
21210
+ conversation_id: external_exports.string().optional(),
21152
21211
  // Whole milliseconds this capture's inspection blocked its caller — the
21153
21212
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21154
21213
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21157,7 +21216,19 @@ var CaptureAttributes = external_exports.object({
21157
21216
  // inline json_extract and is not itself an optimization.
21158
21217
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21159
21218
  // before the measurement shipped — never present as a placeholder 0.
21160
- inspection_ms: external_exports.number().int().nonnegative().optional()
21219
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21220
+ // What a `redact` this capture could not carry out became instead (see
21221
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21222
+ // degrade actually happened, so absence is the ordinary case rather than a
21223
+ // reader having to distinguish it from a zero.
21224
+ //
21225
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21226
+ // so on a multi-finding row this does not say which finding degraded, and
21227
+ // its presence does not mean the fallback decided the capture's action. A
21228
+ // capture denied by another finding's own Block policy carries `block`
21229
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21230
+ // repeated rather than referenced because a store reader opens this file.
21231
+ redact_degraded_to: ActionTaken.optional()
21161
21232
  }).catchall(external_exports.unknown());
21162
21233
  var ToolCallInspection = external_exports.object({
21163
21234
  ruleId: external_exports.string().min(1),
@@ -21356,7 +21427,17 @@ var AuditEvent = external_exports.object({
21356
21427
  /** `share` to a first-party/internal destination. */
21357
21428
  internal: external_exports.boolean(),
21358
21429
  /** Event needs review (e.g. unverified egress). */
21359
- flagged: external_exports.boolean()
21430
+ flagged: external_exports.boolean(),
21431
+ /**
21432
+ * The body this event's `title` is drawn from was cleared by local body
21433
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21434
+ *
21435
+ * A separate flag rather than a sentinel written into `title`: the title is
21436
+ * rendered text, and a store-layer module that invented display copy for it
21437
+ * would be choosing words the view is supposed to choose. Additive and
21438
+ * defaulted, so an older producer still validates.
21439
+ */
21440
+ bodyExpired: external_exports.boolean().default(false)
21360
21441
  }).meta({ id: "ActivityAuditEvent" });
21361
21442
  var ActivitySessionSummary = external_exports.object({
21362
21443
  id: external_exports.string(),
@@ -22697,6 +22778,12 @@ var EventMetadata = external_exports.object({
22697
22778
  // to 'allow' — the enforcement audit trail's link back to the grant that
22698
22779
  // authorized the bypass. Absent on captures where no exception applied.
22699
22780
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22781
+ // The assistant message this capture belongs to, and the conversation it sits
22782
+ // in — set by the browser extension's network capture so a stored `response`
22783
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22784
+ // on every other capture path, which has no such id.
22785
+ messageId: external_exports.string().optional(),
22786
+ conversationId: external_exports.string().optional(),
22700
22787
  // How long THIS capture's inspection blocked its caller, in whole
22701
22788
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22702
22789
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22709,7 +22796,37 @@ var EventMetadata = external_exports.object({
22709
22796
  // Absent is also what every pre-measurement client writes, and what a
22710
22797
  // clock failure degrades to — a reader must treat absence as "not measured"
22711
22798
  // and never as a zero, which would read as "inspection is free".
22712
- inspectionMs: external_exports.number().int().nonnegative().optional()
22799
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22800
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22801
+ // workspace's `redactFallback`, applied because the field could not be
22802
+ // masked in place (a shell command, a URL, or any argument on a host whose
22803
+ // hook contract offers no rewrite channel).
22804
+ //
22805
+ // It exists because the action alone cannot say why. A finding recorded as
22806
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22807
+ // assigned Redact on a field that could not take one — and those are
22808
+ // different facts about the same row: the first is a policy the user chose,
22809
+ // the second is a masking the host could not perform. Absent means no
22810
+ // degrade happened, which is every ordinary capture.
22811
+ //
22812
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22813
+ // is the CAPTURE while `actionTaken` is per FINDING:
22814
+ //
22815
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22816
+ // `redact` alongside a finding ASSIGNED the same action stores both
22817
+ // identically and one reason for the pair; attributing it to both
22818
+ // describes the assigned one wrongly, and to neither loses the degrade.
22819
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22820
+ // became, not the reason the capture ended as it did — a capture denied
22821
+ // by some other finding's own Block policy still carries `block` here,
22822
+ // and clearing the workspace's fallback would not have let it through.
22823
+ // Gate on the value against what a fallback can produce; never read the
22824
+ // field's presence as "this was the fallback's doing".
22825
+ //
22826
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22827
+ // Closing either means moving the reason onto the finding row, which
22828
+ // already carries its own action.
22829
+ redactDegradedTo: ActionTaken.optional()
22713
22830
  }).meta({ id: "EventMetadata" });
22714
22831
  var Event = external_exports.object({
22715
22832
  id: external_exports.guid(),
@@ -22819,7 +22936,32 @@ var RotateKeyInput = external_exports.object({
22819
22936
  confirmation: external_exports.string()
22820
22937
  });
22821
22938
 
22939
+ // ../../packages/schema/src/zod/finding-delivery.ts
22940
+ var KNOWN_REASONS = SyncFailureReason.options;
22941
+ function knownReason(value) {
22942
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22943
+ }
22944
+ function deriveFindingDelivery(row) {
22945
+ if (row.kind === "code_change") return { state: "local_scan" };
22946
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22947
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22948
+ }
22949
+ if (row.syncedAt !== null) {
22950
+ const reason = knownReason(row.syncFailure);
22951
+ return {
22952
+ state: "not_sent",
22953
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22954
+ ...reason === void 0 ? {} : { reason }
22955
+ };
22956
+ }
22957
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22958
+ return { state: "never_offered" };
22959
+ }
22960
+
22822
22961
  // ../../packages/schema/src/zod/findings-group-build.ts
22962
+ function lookupOwn(map2, key) {
22963
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22964
+ }
22823
22965
  function toApiAction(dbVal) {
22824
22966
  const map2 = {
22825
22967
  log: "monitored",
@@ -22828,7 +22970,7 @@ function toApiAction(dbVal) {
22828
22970
  warn: "warned",
22829
22971
  allow: "allowed"
22830
22972
  };
22831
- return map2[dbVal] ?? "allowed";
22973
+ return lookupOwn(map2, dbVal) ?? "allowed";
22832
22974
  }
22833
22975
  function toApiCategory(dbVal) {
22834
22976
  if (dbVal === "code_context") return "source_code";
@@ -22836,13 +22978,18 @@ function toApiCategory(dbVal) {
22836
22978
  return parsed2.success ? parsed2.data : "custom";
22837
22979
  }
22838
22980
  function toApiProvider(sourceTool) {
22839
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22981
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22840
22982
  }
22841
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22983
+ var FINDING_STATUS_PRECEDENCE = [
22984
+ "open",
22985
+ "handled",
22986
+ "dismissed",
22987
+ "resolved"
22988
+ ];
22842
22989
  function foldGroupStatus(instanceStatuses) {
22843
22990
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22844
22991
  if (statuses.size === 0) return void 0;
22845
- for (const candidate of STATUS_PRECEDENCE) {
22992
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22846
22993
  if (statuses.has(candidate)) return candidate;
22847
22994
  }
22848
22995
  return void 0;
@@ -22949,11 +23096,16 @@ function applyFindingFilters(types, opts) {
22949
23096
  }
22950
23097
  return filtered;
22951
23098
  }
22952
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22953
- var SEVERITY_RANK = SEVERITY_ORDER;
23099
+ function rankByOrder(members2) {
23100
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23101
+ }
23102
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23103
+ function severityRank(severity) {
23104
+ return lookupOwn(SEVERITY_RANK, severity);
23105
+ }
22954
23106
  function compareFindingGroupOrder(a, b) {
22955
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22956
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23107
+ const rankA = severityRank(a.severity) ?? -1;
23108
+ const rankB = severityRank(b.severity) ?? -1;
22957
23109
  const severityDiff = rankA - rankB;
22958
23110
  if (severityDiff !== 0) return severityDiff;
22959
23111
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
@@ -23028,6 +23180,20 @@ function computeFindingFacets(allTypes, opts) {
23028
23180
  }
23029
23181
 
23030
23182
  // ../../packages/schema/src/zod/findings-flat-build.ts
23183
+ function compareCodePoints(a, b) {
23184
+ const aIter = a[Symbol.iterator]();
23185
+ const bIter = b[Symbol.iterator]();
23186
+ for (; ; ) {
23187
+ const aNext = aIter.next();
23188
+ const bNext = bIter.next();
23189
+ if (aNext.done && bNext.done) return 0;
23190
+ if (aNext.done) return -1;
23191
+ if (bNext.done) return 1;
23192
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23193
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23194
+ if (aPoint !== bPoint) return aPoint - bPoint;
23195
+ }
23196
+ }
23031
23197
  function rowHaystack(row) {
23032
23198
  return [
23033
23199
  row.ruleId,
@@ -23052,6 +23218,8 @@ function matchesDimension(row, opts, dimension) {
23052
23218
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23053
23219
  case "statuses":
23054
23220
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23221
+ case "deliveries":
23222
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23055
23223
  case "tools":
23056
23224
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23057
23225
  // An EMPTY value is a real filter here, not an absent one. The location
@@ -23078,6 +23246,7 @@ var DIMENSIONS = [
23078
23246
  "providers",
23079
23247
  "actions",
23080
23248
  "statuses",
23249
+ "deliveries",
23081
23250
  "tools",
23082
23251
  "repo",
23083
23252
  "file",
@@ -23091,10 +23260,19 @@ function matchesInstanceFilters(row, opts, except) {
23091
23260
  return true;
23092
23261
  }
23093
23262
  function toItems(counts) {
23094
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23263
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23264
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23265
+ // NFD spelling of the same text) as equal, so a count tie between
23266
+ // them would otherwise be ordered by whichever the Map iteration
23267
+ // produced. compareCodePoints breaks that tie deterministically, which
23268
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23269
+ // which it need not: foldFacetTuples runs this same sort over grouped
23270
+ // tuples, so both paths order facets identically by construction.
23271
+ compareCodePoints(a.value, b.value)
23272
+ );
23095
23273
  }
23096
- function bump(counts, value) {
23097
- counts.set(value, (counts.get(value) ?? 0) + 1);
23274
+ function bump(counts, value, by = 1) {
23275
+ counts.set(value, (counts.get(value) ?? 0) + by);
23098
23276
  }
23099
23277
  function createInstanceFacetAccumulator(opts) {
23100
23278
  const severity = /* @__PURE__ */ new Map();
@@ -23103,6 +23281,7 @@ function createInstanceFacetAccumulator(opts) {
23103
23281
  const action = /* @__PURE__ */ new Map();
23104
23282
  const status = /* @__PURE__ */ new Map();
23105
23283
  const tool = /* @__PURE__ */ new Map();
23284
+ const deployment = /* @__PURE__ */ new Map();
23106
23285
  return {
23107
23286
  add(row) {
23108
23287
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23117,6 +23296,9 @@ function createInstanceFacetAccumulator(opts) {
23117
23296
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23118
23297
  bump(tool, row.toolName);
23119
23298
  }
23299
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23300
+ bump(deployment, row.delivery.state);
23301
+ }
23120
23302
  },
23121
23303
  facets: () => ({
23122
23304
  severity: toItems(severity),
@@ -23124,7 +23306,8 @@ function createInstanceFacetAccumulator(opts) {
23124
23306
  provider: toItems(provider),
23125
23307
  action: toItems(action),
23126
23308
  status: toItems(status),
23127
- tool: toItems(tool)
23309
+ tool: toItems(tool),
23310
+ deployment: toItems(deployment)
23128
23311
  })
23129
23312
  };
23130
23313
  }
@@ -23138,6 +23321,7 @@ function toInstanceDetail(row) {
23138
23321
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23139
23322
  eventId: row.eventId,
23140
23323
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23324
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23141
23325
  ...row.user === void 0 ? {} : { user: row.user },
23142
23326
  action: toApiAction(row.actionTaken),
23143
23327
  detectedAt: row.occurredAt,
@@ -23152,12 +23336,6 @@ function toInstanceDetail(row) {
23152
23336
  policy: { id: `category:${category}`, name: category }
23153
23337
  };
23154
23338
  }
23155
- var SEVERITY_ORDER2 = {
23156
- critical: 0,
23157
- high: 1,
23158
- medium: 2,
23159
- low: 3
23160
- };
23161
23339
  function newLocationAccumulator() {
23162
23340
  return {
23163
23341
  instanceCount: 0,
@@ -23172,7 +23350,7 @@ function newLocationAccumulator() {
23172
23350
  }
23173
23351
  function addToLocation(acc, row) {
23174
23352
  acc.instanceCount += 1;
23175
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23353
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23176
23354
  if (rank < acc.maxSeverityRank) {
23177
23355
  acc.maxSeverityRank = rank;
23178
23356
  acc.maxSeverity = row.severity;
@@ -23182,15 +23360,15 @@ function addToLocation(acc, row) {
23182
23360
  acc.ruleIds.add(row.ruleId);
23183
23361
  }
23184
23362
  function compareLocationOrder(a, b) {
23185
- const rankA = SEVERITY_ORDER2[a.maxSeverity] ?? -1;
23186
- const rankB = SEVERITY_ORDER2[b.maxSeverity] ?? -1;
23363
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23364
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23187
23365
  if (rankA !== rankB) return rankA - rankB;
23188
23366
  if (a.latestDetectedAt !== b.latestDetectedAt) {
23189
23367
  return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23190
23368
  }
23191
- if (a.repo !== b.repo) return a.repo < b.repo ? -1 : 1;
23192
- if (a.file !== b.file) return a.file < b.file ? -1 : 1;
23193
- return 0;
23369
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23370
+ if (repoDiff !== 0) return repoDiff;
23371
+ return compareCodePoints(a.file, b.file);
23194
23372
  }
23195
23373
  function encodeLocationId(repo, file2) {
23196
23374
  return `${encodePart(repo)}/${encodePart(file2)}`;
@@ -23265,6 +23443,11 @@ var Policy = external_exports.object({
23265
23443
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23266
23444
  provenance: PolicyProvenance.optional()
23267
23445
  }).meta({ id: "Policy" });
23446
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23447
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23448
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23449
+ id: "RedactFallback"
23450
+ });
23268
23451
  var PolicyBundle = external_exports.object({
23269
23452
  version: external_exports.string(),
23270
23453
  policies: external_exports.array(Policy),
@@ -23312,6 +23495,16 @@ var PolicyBundle = external_exports.object({
23312
23495
  // control plane), so no name resolution stands between the decision and the
23313
23496
  // comparison.
23314
23497
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23498
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23499
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23500
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23501
+ // a control plane can tighten a machine and never loosen one — the same
23502
+ // direction `mergeRaiseOnly` enforces for policies.
23503
+ //
23504
+ // Optional so an older backend, and an older on-disk cache, still parses;
23505
+ // absent leaves the device's own setting in force, which is the behaviour
23506
+ // that predates the field and the safe direction to default.
23507
+ redactFallback: RedactFallback.optional(),
23315
23508
  customKeywords: external_exports.array(external_exports.string()),
23316
23509
  fetchedAt: external_exports.iso.datetime()
23317
23510
  }).meta({ id: "PolicyBundle" });
@@ -23341,11 +23534,6 @@ function severityFloorPolicy(category) {
23341
23534
  const peak = CATEGORY_PEAK_SEVERITY[category];
23342
23535
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23343
23536
  }
23344
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23345
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23346
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23347
- id: "RedactFallback"
23348
- });
23349
23537
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23350
23538
  var BUILTIN_POLICY_SPECS = {
23351
23539
  monitor: {
@@ -23401,6 +23589,11 @@ function isActionAtLeast(action, floor) {
23401
23589
  function strongerAction(a, b) {
23402
23590
  return actionRank(a) >= actionRank(b) ? a : b;
23403
23591
  }
23592
+ function strongerRedactFallback(local, remote) {
23593
+ if (remote === void 0) return local;
23594
+ const localAction = builtinPolicyToAction(local);
23595
+ return localAction === strongerAction(localAction, builtinPolicyToAction(remote)) ? local : remote;
23596
+ }
23404
23597
  function weakestBuiltinAtLeast(floor) {
23405
23598
  return KNOWN_BUILTIN_IDS.find((id) => isActionAtLeast(builtinPolicyToAction(id), floor)) ?? "block";
23406
23599
  }
@@ -23641,7 +23834,7 @@ var VaultConsent = external_exports.object({
23641
23834
  });
23642
23835
 
23643
23836
  // ../../packages/schema/src/zod/local.ts
23644
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23837
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23645
23838
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23646
23839
  var RunMode = external_exports.enum(["standalone", "attached"]);
23647
23840
  var ControlPlaneConnection = external_exports.object({
@@ -23661,6 +23854,15 @@ var HistorySyncConsent = external_exports.object({
23661
23854
  payloadVersion: external_exports.number().int().positive(),
23662
23855
  endpoint: external_exports.string()
23663
23856
  });
23857
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23858
+ var BodyRetention = external_exports.object({
23859
+ enabled: external_exports.boolean().default(false),
23860
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23861
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23862
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23863
+ // candidate set that is already bounded by "delivered, or never owed".
23864
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23865
+ }).meta({ id: "BodyRetention" });
23664
23866
  var WorkspaceSettings = external_exports.object({
23665
23867
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23666
23868
  runMode: RunMode.default("standalone"),
@@ -23709,7 +23911,13 @@ var WorkspaceSettings = external_exports.object({
23709
23911
  // carry prompt/reply/tool-output text in `content`; the key name predates
23710
23912
  // both widenings. Absent until granted, and a grant for a different endpoint
23711
23913
  // or an older payload no longer counts.
23712
- historySyncConsent: HistorySyncConsent.optional()
23914
+ historySyncConsent: HistorySyncConsent.optional(),
23915
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23916
+ // body never removes the row or its findings.
23917
+ bodyRetention: BodyRetention.default({
23918
+ enabled: false,
23919
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23920
+ })
23713
23921
  });
23714
23922
  function defaultWorkspaceSettings() {
23715
23923
  return WorkspaceSettings.parse({});
@@ -23804,12 +24012,15 @@ function toCaptureAttributes(event) {
23804
24012
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23805
24013
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23806
24014
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24015
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23807
24016
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23808
24017
  // has ever populated either), but every legacy metadata key still rides
23809
24018
  // the bag rather than being silently dropped — CaptureAttributes'
23810
24019
  // `.catchall(z.unknown())` carries the long tail.
23811
24020
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23812
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24021
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24022
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24023
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23813
24024
  };
23814
24025
  }
23815
24026
  function captureDefinitionVersion(finding) {
@@ -23837,13 +24048,22 @@ var ManagedSettingKey = external_exports.enum([
23837
24048
  "vaultInlineReveal",
23838
24049
  "modelJudgeConsent",
23839
24050
  "dataSharesInPlace",
23840
- "redactFallback"
24051
+ "redactFallback",
24052
+ // Pins the toggle and the day count together — see BodyRetention on why the
24053
+ // two are one unit. An administrator mandating a window wants the count
24054
+ // enforced with it, not one a user can widen while the toggle stays on.
24055
+ "bodyRetention"
23841
24056
  ]).meta({ id: "ManagedSettingKey" });
23842
24057
  function isManagedSettingKey(value) {
23843
24058
  return ManagedSettingKey.safeParse(value).success;
23844
24059
  }
23845
24060
  var ManagedSettingsValues = external_exports.object({
23846
24061
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24062
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24063
+ // plain, non-strict objects: a key under either that this build does not know
24064
+ // is stripped and nothing reports it. The unknown-value split in
24065
+ // ManagedSettings below classifies top-level names only, so it stops at
24066
+ // these boundaries.
23847
24067
  controlPlane: external_exports.object({
23848
24068
  endpoint: external_exports.string().min(1),
23849
24069
  label: external_exports.string().min(1).optional()
@@ -23854,7 +24074,8 @@ var ManagedSettingsValues = external_exports.object({
23854
24074
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23855
24075
  modelJudgeConsent: external_exports.boolean().optional(),
23856
24076
  dataSharesInPlace: external_exports.boolean().optional(),
23857
- redactFallback: RedactFallback.optional()
24077
+ redactFallback: RedactFallback.optional(),
24078
+ bodyRetention: BodyRetention.optional()
23858
24079
  }).meta({ id: "ManagedSettingsValues" });
23859
24080
  var ManagedSettings = external_exports.object({
23860
24081
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23862,7 +24083,21 @@ var ManagedSettings = external_exports.object({
23862
24083
  // decision from a bug. Absent renders as a generic "your organization".
23863
24084
  organization: external_exports.string().min(1).optional(),
23864
24085
  // What the administrator pinned.
23865
- values: ManagedSettingsValues.default({}),
24086
+ //
24087
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24088
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24089
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24090
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24091
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24092
+ // exactly the file an administrator is most likely to write while a fleet
24093
+ // is mid-upgrade.
24094
+ //
24095
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24096
+ // file, which is the outcome the lock half already rejected — an older
24097
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24098
+ // value still fails, because the nested schema is re-run over the known
24099
+ // subset and its issues are re-raised on this parse.
24100
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23866
24101
  // Which of those the user may not change. A key here with no matching value
23867
24102
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23868
24103
  // the user may still override. The two are separable on purpose.
@@ -23875,17 +24110,31 @@ var ManagedSettings = external_exports.object({
23875
24110
  // the fleets most likely to carry a version skew. A name outside the enum
23876
24111
  // is still never HONOURED: the lockable set stays explicit above.
23877
24112
  lockedFields: external_exports.array(external_exports.string()).default([])
23878
- }).transform(({ lockedFields, ...rest }) => {
24113
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
23879
24114
  const known = [];
23880
24115
  const unknown2 = [];
23881
24116
  for (const name of lockedFields) {
23882
24117
  if (isManagedSettingKey(name)) known.push(name);
23883
24118
  else unknown2.push(name);
23884
24119
  }
24120
+ const knownValues = /* @__PURE__ */ Object.create(null);
24121
+ const unknownValues = [];
24122
+ for (const [name, value] of Object.entries(values)) {
24123
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24124
+ else unknownValues.push(name);
24125
+ }
24126
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24127
+ if (!pinned.success) {
24128
+ for (const issue2 of pinned.error.issues)
24129
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24130
+ return external_exports.NEVER;
24131
+ }
23885
24132
  return {
23886
24133
  ...rest,
24134
+ values: pinned.data,
23887
24135
  lockedFields: known,
23888
- ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {}
24136
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24137
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
23889
24138
  };
23890
24139
  }).meta({ id: "ManagedSettings" });
23891
24140
 
@@ -24149,7 +24398,23 @@ var SaveSettingsInput = external_exports.object({
24149
24398
  modelJudgeConsent: ModelJudgeConsentChoice,
24150
24399
  historySyncConsent: HistorySyncConsentChoice,
24151
24400
  vaultConsent: external_exports.string(),
24152
- vaultInlineReveal: external_exports.string()
24401
+ vaultInlineReveal: external_exports.string(),
24402
+ // Widened to `string` like its neighbours rather than typed as
24403
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24404
+ // the call site, so the domain check receives the type it was written for.
24405
+ //
24406
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24407
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24408
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24409
+ // trade against. The real cost runs the other way and is the part worth
24410
+ // knowing: a value this schema admits and the domain enum then rejects lands
24411
+ // on the action's shared refusal, which names NO field, where a shape
24412
+ // rejection reaches `malformedInput` and names the schema key.
24413
+ redactFallback: external_exports.string(),
24414
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24415
+ // `BodyRetention`'s and the action checks it there, so there is one place
24416
+ // that decides what a legal horizon is rather than two that can drift.
24417
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24153
24418
  });
24154
24419
  var AttachInput = external_exports.object({
24155
24420
  endpoint: external_exports.string(),
@@ -24321,6 +24586,52 @@ function reviewSeverityRank(reasons) {
24321
24586
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24322
24587
  }
24323
24588
 
24589
+ // ../../packages/schema/src/zod/web-capture.ts
24590
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24591
+ var WebUsage = external_exports.object({
24592
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24593
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24594
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24595
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24596
+ });
24597
+ var WebToolCall = external_exports.object({
24598
+ toolUseId: external_exports.string().min(1),
24599
+ toolName: external_exports.string().min(1),
24600
+ target: external_exports.string().optional(),
24601
+ isError: external_exports.boolean().optional(),
24602
+ inputSize: external_exports.number().int().nonnegative().optional(),
24603
+ outputSize: external_exports.number().int().nonnegative().optional()
24604
+ });
24605
+ var WebExchange = external_exports.object({
24606
+ messageId: external_exports.string().min(1),
24607
+ startedAt: external_exports.iso.datetime(),
24608
+ model: external_exports.string().optional(),
24609
+ usage: WebUsage.optional(),
24610
+ usageSource: WebUsageSource,
24611
+ stopReason: external_exports.string().optional(),
24612
+ conversationId: external_exports.string().optional(),
24613
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24614
+ toolCalls: external_exports.array(WebToolCall).default([]),
24615
+ // Absent when the adapter recovered no text. Capped by the caller at
24616
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24617
+ // short capture is never mistaken for a short reply.
24618
+ responseText: external_exports.string().optional(),
24619
+ truncated: external_exports.boolean().default(false)
24620
+ });
24621
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24622
+ var WebCaptureStatus = external_exports.object({
24623
+ patched: external_exports.boolean(),
24624
+ live: external_exports.boolean(),
24625
+ blind: external_exports.boolean(),
24626
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24627
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24628
+ parseFailures: external_exports.number().int().nonnegative(),
24629
+ unparsedBodies: external_exports.number().int().nonnegative(),
24630
+ // The adapter-declared JSON key paths that were absent from a real payload —
24631
+ // the earliest signal that a site's contract moved.
24632
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24633
+ });
24634
+
24324
24635
  // ../../packages/persistence/src/paths.ts
24325
24636
  import {
24326
24637
  chmodSync,
@@ -24681,6 +24992,22 @@ function discardStore(file2, backup) {
24681
24992
  }
24682
24993
  }
24683
24994
 
24995
+ // ../../packages/persistence/src/internal/sql-functions.ts
24996
+ var utf8 = new TextDecoder();
24997
+ function akaLower(value) {
24998
+ if (value === null) return null;
24999
+ if (typeof value === "string") return value.toLowerCase();
25000
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
25001
+ return utf8.decode(value).toLowerCase();
25002
+ }
25003
+ function registerSqlFunctions(db) {
25004
+ db.function(
25005
+ "aka_lower",
25006
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
25007
+ akaLower
25008
+ );
25009
+ }
25010
+
24684
25011
  // ../../packages/persistence/src/internal/sql-text.ts
24685
25012
  function escapeLikePattern(s) {
24686
25013
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24765,6 +25092,11 @@ function schemaObjectExists(db, kind, name) {
24765
25092
  function indexExists(db, name) {
24766
25093
  return schemaObjectExists(db, "index", name);
24767
25094
  }
25095
+ function indexColumns(db, name) {
25096
+ if (!indexExists(db, name)) return [];
25097
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25098
+ return columns.map((c) => c.name).filter((c) => c !== null);
25099
+ }
24768
25100
  function columnNames(db, table, opts) {
24769
25101
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24770
25102
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).all();
@@ -24826,177 +25158,819 @@ function mapRowsTolerant(rows, map2) {
24826
25158
  return out;
24827
25159
  }
24828
25160
 
24829
- // ../../packages/persistence/src/migrations.ts
24830
- function describeObject(object2) {
24831
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24832
- }
24833
- function splitStatements(sql) {
24834
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24835
- }
24836
- function createdIndexName(statement) {
24837
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24838
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25161
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25162
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25163
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25164
+
25165
+ // ../../packages/persistence/src/sync-failure.ts
25166
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25167
+ function syncFailureRejectCondition(column = "sync_failure") {
25168
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25169
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24839
25170
  }
24840
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24841
- function applyMigrations(db, file2) {
24842
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24843
- db.exec(
24844
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24845
- );
24846
- const applied = new Set(
24847
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24848
- );
24849
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24850
- const record2 = db.prepare(
24851
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24852
- );
24853
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24854
- if (applied.has(migration.tag)) continue;
24855
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24856
- const evidence = evidenceObjects(migration.sql);
24857
- const present = evidence.filter((o) => evidenceExists(db, o));
24858
- if (present.length > 0 && present.length < evidence.length) {
24859
- const missing = evidence.filter((o) => !present.includes(o));
24860
- 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.`;
24861
- akaWarn(message);
24862
- throw new Error(`[aka] ${message}`);
24863
- }
24864
- const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
24865
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24866
- const statements = splitStatements(migration.sql);
24867
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24868
- try {
24869
- withTransaction(
24870
- db,
24871
- () => {
24872
- for (const statement of statements) {
24873
- const indexName = createdIndexName(statement);
24874
- if (indexName === void 0) {
24875
- if (alreadyApplied) continue;
24876
- } else if (indexExists(db, indexName)) {
24877
- continue;
24878
- }
24879
- db.exec(statement);
24880
- }
24881
- if (wantsFkOff && !alreadyApplied) {
24882
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24883
- if (violations.length > 0) {
24884
- throw new Error(
24885
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24886
- );
24887
- }
24888
- }
24889
- record2.run(migration.tag, Date.now());
24890
- },
24891
- "IMMEDIATE"
24892
- );
24893
- } finally {
24894
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24895
- }
25171
+
25172
+ // ../../packages/persistence/src/repositories/history-sync.ts
25173
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25174
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25175
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25176
+ var COUNTED_EVENT_TYPES = [
25177
+ ...STRUCTURAL_EVENT_TYPES,
25178
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25179
+ ];
25180
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25181
+ var PARTITION_BUCKETS = `
25182
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25183
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25184
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25185
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25186
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25187
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25188
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25189
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25190
+ -- added later lands in no bucket and fails the sum assertion, instead
25191
+ -- of silently joining this one.
25192
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25193
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25194
+ THEN 1 ELSE 0 END) AS failed,
25195
+ COUNT(*) AS total`;
25196
+ var COUNTED_SCOPE = `
25197
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25198
+ AND (
25199
+ event_type IN (${TYPE_LIST})
25200
+ OR synced_at IS NOT NULL
25201
+ OR outbox_owed = 1
25202
+ )`;
25203
+ var SKIPPED = -1;
25204
+ var ROW_COLUMNS = `id,
25205
+ parent_id AS parentId,
25206
+ root_session_id AS rootSessionId,
25207
+ event_type AS eventType,
25208
+ host_id AS hostId,
25209
+ harness_id AS harnessId,
25210
+ source_project_id AS sourceProjectId,
25211
+ started_at AS startedAt,
25212
+ ended_at AS endedAt,
25213
+ severity,
25214
+ priority,
25215
+ content,
25216
+ content_hash AS contentHash,
25217
+ attributes`;
25218
+ var SqliteHistorySyncRepository = class {
25219
+ constructor(db) {
25220
+ this.db = db;
25221
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25222
+ this.sessionsStmt = db.prepare(
25223
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25224
+ FROM audit_events
25225
+ WHERE synced_at IS NULL
25226
+ AND event_type IN (${TYPE_LIST})
25227
+ AND started_at < :before
25228
+ GROUP BY sessionId
25229
+ ORDER BY earliest
25230
+ LIMIT :limit`
25231
+ );
25232
+ this.rowsStmt = db.prepare(
25233
+ `SELECT ${ROW_COLUMNS}
25234
+ FROM audit_events
25235
+ WHERE synced_at IS NULL
25236
+ AND event_type IN (${TYPE_LIST})
25237
+ AND started_at < :before
25238
+ AND COALESCE(root_session_id, id) = :sessionId
25239
+ ORDER BY (event_type = 'session') DESC, started_at
25240
+ LIMIT :limit`
25241
+ );
25242
+ this.captureRowsStmt = db.prepare(
25243
+ `SELECT ${ROW_COLUMNS}
25244
+ FROM audit_events
25245
+ WHERE synced_at IS NULL
25246
+ AND sync_claimed_at IS NULL
25247
+ AND outbox_owed = 1
25248
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25249
+ AND started_at < :before
25250
+ ORDER BY started_at
25251
+ LIMIT :limit`
25252
+ );
25253
+ this.markOwedStmt = db.prepare(
25254
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25255
+ );
25256
+ this.markCaptureBacklogOwedStmt = db.prepare(
25257
+ `UPDATE audit_events SET outbox_owed = 1
25258
+ WHERE synced_at IS NULL
25259
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25260
+ AND started_at < :before`
25261
+ );
25262
+ this.stampStmt = db.prepare(
25263
+ `UPDATE audit_events
25264
+ SET synced_at = :at,
25265
+ sync_claimed_at = NULL,
25266
+ sync_failed_at = :failedAt,
25267
+ sync_failure = :failure
25268
+ WHERE id = :id`
25269
+ );
25270
+ this.claimRowStmt = db.prepare(
25271
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25272
+ );
25273
+ this.releaseRowStmt = db.prepare(
25274
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25275
+ );
25276
+ this.releaseStaleClaimsStmt = db.prepare(
25277
+ `UPDATE audit_events SET sync_claimed_at = NULL
25278
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25279
+ );
25280
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25281
+ FROM audit_events${COUNTED_SCOPE}`);
25282
+ this.partitionByKindStmt = db.prepare(
25283
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25284
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25285
+ GROUP BY event_type`
25286
+ );
25287
+ this.countsStmt = db.prepare(
25288
+ `SELECT
25289
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25290
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25291
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25292
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25293
+ THEN 1 ELSE 0 END) AS skipped,
25294
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25295
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25296
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25297
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25298
+ FROM audit_events
25299
+ WHERE event_type IN (${TYPE_LIST})`
25300
+ );
25301
+ this.captureSkipCountStmt = db.prepare(
25302
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25303
+ // way the structural totals are. The split exists because a refusal is
25304
+ // terminal only against the deployment that gave it, and the structural
25305
+ // re-arm frees it on a change of deployment. The capture lane has no such
25306
+ // escape: re-arming a capture would offer one deployment's undelivered
25307
+ // prompts, with their text, to a deployment that never saw them, which is
25308
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25309
+ // reasons mean the same thing — this row will not be sent — and splitting
25310
+ // them would put refused captures in a bucket nothing reads and nothing
25311
+ // frees.
25312
+ `SELECT COUNT(*) AS skipped
25313
+ FROM audit_events
25314
+ WHERE synced_at = ${String(SKIPPED)}
25315
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25316
+ );
25317
+ this.fingerprintStmt = db.prepare(
25318
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25319
+ FROM history_sync WHERE id = 1`
25320
+ );
25321
+ this.setFingerprintStmt = db.prepare(
25322
+ `UPDATE history_sync
25323
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25324
+ WHERE id = 1`
25325
+ );
25326
+ this.disownCapturesStmt = db.prepare(
25327
+ `UPDATE audit_events SET outbox_owed = NULL
25328
+ WHERE outbox_owed IS NOT NULL
25329
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25330
+ AND started_at < :attachedAt`
25331
+ );
25332
+ this.rearmStmt = db.prepare(
25333
+ `UPDATE audit_events
25334
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25335
+ WHERE (synced_at > 0
25336
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25337
+ AND event_type IN (${TYPE_LIST})`
25338
+ );
25339
+ this.claimStmt = db.prepare(
25340
+ `UPDATE history_sync
25341
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25342
+ WHERE id = 1
25343
+ AND (owner_pid IS NULL
25344
+ OR heartbeat_at IS NULL
25345
+ OR heartbeat_at < :staleBefore
25346
+ OR heartbeat_at > :now)`
25347
+ );
25348
+ this.heartbeatStmt = db.prepare(
25349
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25350
+ );
25351
+ this.releaseStmt = db.prepare(
25352
+ `UPDATE history_sync
25353
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25354
+ WHERE id = 1 AND owner_pid = :pid`
25355
+ );
25356
+ this.closeWindowStmt = db.prepare(
25357
+ `UPDATE audit_events
25358
+ SET synced_at = ${String(SKIPPED)},
25359
+ sync_failed_at = :at,
25360
+ sync_failure = 'detached_undelivered'
25361
+ WHERE synced_at IS NULL
25362
+ AND event_type IN (${TYPE_LIST})
25363
+ AND started_at >= :attachedAt`
25364
+ );
25365
+ this.releaseBoundaryStmt = db.prepare(
25366
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25367
+ );
25368
+ this.freezeBoundaryStmt = db.prepare(
25369
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25370
+ );
25371
+ this.leaseStmt = db.prepare(
25372
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25373
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25374
+ FROM history_sync WHERE id = 1`
25375
+ );
25376
+ this.inspectionsStmt = db.prepare(
25377
+ `SELECT d.rule_id AS ruleId,
25378
+ d.name AS ruleName,
25379
+ d.version AS ruleVersion,
25380
+ d.category AS category,
25381
+ d.severity AS severity,
25382
+ f.span_start AS spanStart,
25383
+ f.span_end AS spanEnd,
25384
+ f.masked_match AS maskedMatch,
25385
+ f.action_taken AS actionTaken,
25386
+ f.confidence AS confidence
25387
+ FROM inspection_findings f
25388
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25389
+ WHERE f.audit_event_id = :auditEventId
25390
+ ORDER BY f.span_start, f.id`
25391
+ );
24896
25392
  }
24897
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24898
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25393
+ db;
25394
+ ensureRowStmt;
25395
+ sessionsStmt;
25396
+ rowsStmt;
25397
+ stampStmt;
25398
+ countsStmt;
25399
+ fingerprintStmt;
25400
+ setFingerprintStmt;
25401
+ rearmStmt;
25402
+ claimStmt;
25403
+ heartbeatStmt;
25404
+ releaseStmt;
25405
+ leaseStmt;
25406
+ inspectionsStmt;
25407
+ closeWindowStmt;
25408
+ releaseBoundaryStmt;
25409
+ freezeBoundaryStmt;
25410
+ captureRowsStmt;
25411
+ markOwedStmt;
25412
+ markCaptureBacklogOwedStmt;
25413
+ captureSkipCountStmt;
25414
+ disownCapturesStmt;
25415
+ partitionStmt;
25416
+ partitionByKindStmt;
25417
+ claimRowStmt;
25418
+ releaseRowStmt;
25419
+ releaseStaleClaimsStmt;
25420
+ /**
25421
+ * The masked detections recorded against one tool call.
25422
+ *
25423
+ * These travel with the event because a tool call's target is not
25424
+ * re-inspectable from the event alone — unlike a capture, where the text
25425
+ * itself is re-scannable. What crosses is the masked match and the rule that
25426
+ * produced it, never the value.
25427
+ */
25428
+ inspectionsFor(auditEventId) {
25429
+ return allRows(this.inspectionsStmt, { auditEventId });
24899
25430
  }
24900
- ensureSyncedAtColumn(db, "audit_events");
24901
- ensureScanLedgerTable(db);
24902
- ensureHistorySyncTable(db);
24903
- ensureBlockedDetectionsTable(db);
24904
- ensureRuleProbeCacheTable(db);
24905
- ensureWriteGateTrigger(db);
24906
- ensureTokenUsageColumns(db);
24907
- reconcileSourceProjectIds(db);
24908
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24909
- const drained = runLegacyHistoryBackfill(db);
24910
- if (drained) applyLegacyDropMigration(db, file2);
25431
+ /**
25432
+ * Sessions with structural rows still to send, oldest first.
25433
+ *
25434
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25435
+ * read. Anything recorded after the machine attached is the live forward
25436
+ * path's to deliver; this drain exists for what was recorded before it, and a
25437
+ * row both paths send is at best a duplicate request and at worst — for a
25438
+ * session root — an overwrite of the inventory ids the live path resolved.
25439
+ */
25440
+ pendingSessions(limit, before) {
25441
+ return allRows(this.sessionsStmt, { limit, before }).map(
25442
+ (r) => r.sessionId
25443
+ );
24911
25444
  }
24912
- }
24913
- function readLegacyTables(db) {
24914
- let holdsRows = false;
24915
- const marks = [];
24916
- for (const table of ["events", "findings"]) {
24917
- try {
24918
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
24919
- if (row === void 0) {
24920
- holdsRows = true;
24921
- marks.push(`${table}:unreadable`);
24922
- continue;
24923
- }
24924
- if (row.n > 0) holdsRows = true;
24925
- marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
24926
- } catch {
24927
- holdsRows = true;
24928
- marks.push(`${table}:unreadable`);
24929
- }
25445
+ /** One session's undelivered structural rows within the backlog, root first. */
25446
+ pendingRows(sessionId, limit, before) {
25447
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24930
25448
  }
24931
- return { holdsRows, mark: marks.join("|") };
24932
- }
24933
- function applyLegacyDropMigration(db, file2) {
24934
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24935
- if (!migration) return;
24936
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24937
- if (file2 !== void 0 && before?.holdsRows === true) {
24938
- try {
24939
- backupBeforeLegacyDrop(db, file2);
24940
- } catch (error61) {
24941
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24942
- return;
24943
- }
25449
+ /**
25450
+ * Captures this machine still owes the deployment, oldest first.
25451
+ *
25452
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25453
+ * by a time window — see captureRowsStmt for why a window could not express
25454
+ * this. `before` is the grace window that leaves a just-recorded capture to
25455
+ * the live path.
25456
+ */
25457
+ pendingCaptureRows(limit, before) {
25458
+ return allRows(this.captureRowsStmt, { limit, before });
24944
25459
  }
24945
- try {
25460
+ /**
25461
+ * Record that a capture is OWED to the deployment.
25462
+ *
25463
+ * Written by the attached forward path when a live send did not confirm
25464
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25465
+ * a fact rather than an inference: the machine was attached, the send did not
25466
+ * land, so the row is owed — which no time window can state, because the same
25467
+ * window that holds the rows a past attachment left owed also holds every
25468
+ * capture recorded while the machine was DETACHED, and those were never
25469
+ * offered to anyone.
25470
+ *
25471
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25472
+ * out of the drain's read.
25473
+ */
25474
+ markCaptureOwed(id) {
25475
+ this.markOwedStmt.run({ id });
25476
+ }
25477
+ /**
25478
+ * Mark every capture already on disk as owed, as of `before`.
25479
+ *
25480
+ * The consent-time backfill, called once from `aka attach` when a human
25481
+ * grants existing-history consent — never from an ongoing drain pass, and
25482
+ * never inferred from a boundary that could later move. `before` is the
25483
+ * caller's own "now" at the moment consent was granted, so what this marks
25484
+ * is exactly the backlog the consent prompt already counted, not whatever a
25485
+ * later re-attach or key rotation might widen it to.
25486
+ *
25487
+ * Returns how many rows matched, for the caller to log or test against. Not a
25488
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25489
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25490
+ */
25491
+ markCaptureBacklogOwed(before) {
25492
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25493
+ }
25494
+ /**
25495
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25496
+ *
25497
+ * CLEARS any failure reason in the same statement. A row that failed against
25498
+ * one deployment and then landed is delivered, and leaving the reason behind
25499
+ * would leave the store holding two contradictory answers about one row —
25500
+ * with the surface free to render either.
25501
+ */
25502
+ markSynced(ids, atMs) {
25503
+ this.stampAll(ids, atMs, null);
25504
+ }
25505
+ /**
25506
+ * Record that THIS MACHINE cannot express the row on the wire.
25507
+ *
25508
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25509
+ * payload, or a body the client itself refused to send. It fails identically
25510
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25511
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25512
+ * is retried; marking those would turn one outage into permanent data loss.
25513
+ */
25514
+ markSkipped(ids, atMs) {
25515
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25516
+ }
25517
+ /**
25518
+ * Record that THIS DEPLOYMENT refused the row.
25519
+ *
25520
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25521
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25522
+ * row is outstanding rather than why. What separates them is the reason, and
25523
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25524
+ * on one body, so it is terminal only for as long as this machine points at
25525
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25526
+ *
25527
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25528
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25529
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25530
+ */
25531
+ markRefused(ids, atMs) {
25532
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25533
+ }
25534
+ eachInTransaction(ids, run) {
25535
+ if (ids.length === 0) return;
24946
25536
  withTransaction(
24947
- db,
25537
+ this.db,
24948
25538
  () => {
24949
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24950
- if (alreadyDropped) return;
24951
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24952
- akaWarn(
24953
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24954
- );
24955
- return;
24956
- }
24957
- for (const statement of splitStatements(migration.sql)) {
24958
- db.exec(statement);
24959
- }
24960
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24961
- migration.tag,
24962
- Date.now()
24963
- );
25539
+ for (const id of ids) run(id);
24964
25540
  },
24965
25541
  "IMMEDIATE"
24966
25542
  );
24967
- } catch (error61) {
24968
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24969
25543
  }
24970
- }
24971
- function backupBeforeLegacyDrop(db, file2) {
24972
- reapStalePartials(file2);
24973
- const backup = backupPath(file2, "pre-drop");
24974
- snapshotStore(db, backup);
24975
- return backup;
24976
- }
24977
- var TOKEN_USAGE_COLUMNS = [
24978
- {
24979
- name: "input_tokens",
24980
- ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
24981
- },
24982
- {
24983
- name: "output_tokens",
24984
- ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
24985
- },
24986
- {
24987
- name: "cache_creation_input_tokens",
24988
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
24989
- },
24990
- {
24991
- name: "cache_read_input_tokens",
24992
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
24993
- },
24994
- {
24995
- name: "model",
24996
- ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
24997
- },
24998
- {
24999
- name: "provider",
25544
+ stampAll(ids, value, failure, failedAtMs) {
25545
+ if (ids.length === 0) return;
25546
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25547
+ withTransaction(
25548
+ this.db,
25549
+ () => {
25550
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25551
+ },
25552
+ "IMMEDIATE"
25553
+ );
25554
+ }
25555
+ /**
25556
+ * Claim rows as in-flight.
25557
+ *
25558
+ * Advisory in exactly the sense the lease is: it records that a send is in
25559
+ * progress so a surface can say so, and a lost claim costs a row showing as
25560
+ * queued while it is actually being sent. It is not exclusion — the far side
25561
+ * settles a duplicate on the row id.
25562
+ */
25563
+ claimRows(ids, atMs) {
25564
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25565
+ }
25566
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25567
+ releaseRows(ids) {
25568
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25569
+ }
25570
+ /**
25571
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25572
+ *
25573
+ * A process killed between claiming and settling leaves rows claimed with
25574
+ * nothing left to settle them. Without this they read as "sending" for ever.
25575
+ */
25576
+ releaseStaleClaims(staleBefore) {
25577
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25578
+ }
25579
+ /**
25580
+ * Every tracked row in exactly one delivery state.
25581
+ *
25582
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25583
+ * pick up now", which is a different question from "what state is this row
25584
+ * in" — and a machine that has never attached has no boundary to pass, so
25585
+ * requiring one would force a caller to invent one and report the whole store
25586
+ * as queued.
25587
+ */
25588
+ /**
25589
+ * The same partition, one row per kind that a lane carries.
25590
+ *
25591
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25592
+ * scope decides which rows exist at all, so a kind that has never been
25593
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25594
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25595
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25596
+ * different things.
25597
+ */
25598
+ partitionByKind() {
25599
+ return allRows(
25600
+ this.partitionByKindStmt,
25601
+ {}
25602
+ ).map((row) => ({
25603
+ kind: row.kind,
25604
+ queued: row.queued ?? 0,
25605
+ inProgress: row.inProgress ?? 0,
25606
+ synced: row.synced ?? 0,
25607
+ failed: row.failed ?? 0,
25608
+ refused: row.refused ?? 0,
25609
+ detached: row.detached ?? 0,
25610
+ total: row.total ?? 0
25611
+ }));
25612
+ }
25613
+ partition() {
25614
+ const row = getRow(this.partitionStmt, {});
25615
+ return {
25616
+ queued: row?.queued ?? 0,
25617
+ inProgress: row?.inProgress ?? 0,
25618
+ synced: row?.synced ?? 0,
25619
+ failed: row?.failed ?? 0,
25620
+ refused: row?.refused ?? 0,
25621
+ detached: row?.detached ?? 0,
25622
+ total: row?.total ?? 0
25623
+ };
25624
+ }
25625
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25626
+ counts(before) {
25627
+ const row = getRow(this.countsStmt, { before });
25628
+ const captures = getRow(this.captureSkipCountStmt);
25629
+ return {
25630
+ pending: row?.pending ?? 0,
25631
+ sent: row?.sent ?? 0,
25632
+ skipped: row?.skipped ?? 0,
25633
+ refused: row?.refused ?? 0,
25634
+ detached: row?.detached ?? 0,
25635
+ capturesSkipped: captures?.skipped ?? 0
25636
+ };
25637
+ }
25638
+ /**
25639
+ * The deployment the current stamps were made against, and where its backlog
25640
+ * ends.
25641
+ *
25642
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25643
+ * machine that has never drained is — and every writer below seeds the row
25644
+ * before it needs one, so nothing depends on this creating it. Keeping the
25645
+ * write off the gate path matters because the gate runs on every pass while a
25646
+ * write has to take the database's write lock.
25647
+ */
25648
+ deployment() {
25649
+ const row = getRow(
25650
+ this.fingerprintStmt
25651
+ );
25652
+ return {
25653
+ fingerprint: row?.fingerprint ?? void 0,
25654
+ backlogBefore: row?.backlogBefore ?? void 0
25655
+ };
25656
+ }
25657
+ /**
25658
+ * Point the ledger at a different deployment, discarding what it recorded
25659
+ * about the previous one.
25660
+ *
25661
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25662
+ * machine has just left are undelivered as far as the new one is concerned.
25663
+ * All four in one transaction, so a crash between them cannot leave stamps
25664
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25665
+ * a disown with no re-mark to follow it.
25666
+ *
25667
+ * The boundary is written HERE and only here, which is what freezes it: a
25668
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25669
+ * unchanged, so this never runs and the backlog does not widen back over rows
25670
+ * the live path has since delivered.
25671
+ *
25672
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25673
+ * granted existing-history consent for the deployment this call is arming —
25674
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25675
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25676
+ * apart. Passed only when that grant is valid, since this method has no way
25677
+ * to check consent itself and must not mark a row owed for a machine that
25678
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25679
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25680
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25681
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25682
+ * on the cleared side of that bound — and the re-mark in the same
25683
+ * transaction is what puts those rows back. A crash between the two cannot
25684
+ * strand the ledger disowned with nothing re-marked — the transaction either
25685
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25686
+ * committed re-enters this method on the very next pass. Omit it (the
25687
+ * structural-only tests do) to exercise the disown in isolation.
25688
+ *
25689
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25690
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25691
+ * live path can mark a capture owed from the moment `aka attach` writes the
25692
+ * descriptor, before the drain's first pass ever reaches this method, and
25693
+ * such a row sits at or after the bound rather than below it. What keeps the
25694
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25695
+ * bound — disown runs first, re-mark second, both inside the one
25696
+ * transaction above.
25697
+ */
25698
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25699
+ this.ensureRowStmt.run();
25700
+ withTransaction(
25701
+ this.db,
25702
+ () => {
25703
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25704
+ this.rearmStmt.run();
25705
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25706
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25707
+ }
25708
+ if (backfillCapturesBefore !== void 0) {
25709
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25710
+ }
25711
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25712
+ },
25713
+ "IMMEDIATE"
25714
+ );
25715
+ }
25716
+ /**
25717
+ * End the attached period: hand its rows to the live path, and release the
25718
+ * boundary so the next attachment can freeze a new one.
25719
+ *
25720
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25721
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25722
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25723
+ * during the detached period, because the machine is not attached. Rows
25724
+ * recorded in that window sit after the boundary and before the re-attach, so
25725
+ * neither path takes them, and the pending count reports none outstanding.
25726
+ *
25727
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25728
+ * closing attachment's to deliver and are no longer outstanding — that is what
25729
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25730
+ * distinction is not academic: this used to write a delivery TIME, which every
25731
+ * read treats as delivery, so one detach turned a window of undelivered rows
25732
+ * into a window of delivered ones and no surface could tell. It writes the
25733
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25734
+ * "received" stop being the same fact.
25735
+ *
25736
+ * A change of deployment still frees them (see the re-arm), because the next
25737
+ * deployment has seen none of this machine's history — so the rows reach it
25738
+ * exactly as they did when this wrote a delivery time.
25739
+ *
25740
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25741
+ * window unstamped — that half-state would re-send the whole attached period
25742
+ * on the next attach, which is the failure the boundary exists to prevent.
25743
+ */
25744
+ closeAttachedWindow(attachedAtMs, atMs) {
25745
+ this.ensureRowStmt.run();
25746
+ withTransaction(
25747
+ this.db,
25748
+ () => {
25749
+ const row = getRow(this.fingerprintStmt);
25750
+ const from = row?.backlogBefore ?? attachedAtMs;
25751
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25752
+ this.releaseBoundaryStmt.run();
25753
+ },
25754
+ "IMMEDIATE"
25755
+ );
25756
+ }
25757
+ /**
25758
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25759
+ *
25760
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25761
+ * different deployment and therefore discards what was delivered to the old
25762
+ * one: here the recipient is the same, so everything already sent to it stays
25763
+ * sent.
25764
+ */
25765
+ freezeBoundary(backlogBefore) {
25766
+ this.ensureRowStmt.run();
25767
+ this.freezeBoundaryStmt.run({ backlogBefore });
25768
+ }
25769
+ /** Take the claim, or report that someone live already holds it. */
25770
+ claim(pid, host, nowMs, staleAfterMs) {
25771
+ this.ensureRowStmt.run();
25772
+ let taken = false;
25773
+ withTransaction(
25774
+ this.db,
25775
+ () => {
25776
+ const result = this.claimStmt.run({
25777
+ pid,
25778
+ host,
25779
+ now: nowMs,
25780
+ staleBefore: nowMs - staleAfterMs
25781
+ });
25782
+ taken = result.changes === 1;
25783
+ },
25784
+ "IMMEDIATE"
25785
+ );
25786
+ return taken;
25787
+ }
25788
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25789
+ heartbeat(pid, nowMs) {
25790
+ this.heartbeatStmt.run({ now: nowMs, pid });
25791
+ }
25792
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25793
+ release(pid) {
25794
+ this.releaseStmt.run({ pid });
25795
+ }
25796
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25797
+ lease() {
25798
+ return getRow(this.leaseStmt);
25799
+ }
25800
+ };
25801
+
25802
+ // ../../packages/persistence/src/migrations.ts
25803
+ function describeObject(object2) {
25804
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25805
+ }
25806
+ function splitStatements(sql) {
25807
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25808
+ }
25809
+ function createdIndexName(statement) {
25810
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25811
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25812
+ }
25813
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25814
+ function applyMigrations(db, file2, options = {}) {
25815
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25816
+ db.exec(
25817
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25818
+ );
25819
+ const applied = new Set(
25820
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25821
+ );
25822
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25823
+ const record2 = db.prepare(
25824
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25825
+ );
25826
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25827
+ if (applied.has(migration.tag)) continue;
25828
+ if (options.skipTags?.has(migration.tag) === true) continue;
25829
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25830
+ const evidence = evidenceObjects(migration.sql);
25831
+ const present = evidence.filter((o) => evidenceExists(db, o));
25832
+ if (present.length > 0 && present.length < evidence.length) {
25833
+ const missing = evidence.filter((o) => !present.includes(o));
25834
+ 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.`;
25835
+ akaWarn(message);
25836
+ throw new Error(`[aka] ${message}`);
25837
+ }
25838
+ const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
25839
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25840
+ const statements = splitStatements(migration.sql);
25841
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25842
+ try {
25843
+ withTransaction(
25844
+ db,
25845
+ () => {
25846
+ for (const statement of statements) {
25847
+ const indexName = createdIndexName(statement);
25848
+ if (indexName === void 0) {
25849
+ if (alreadyApplied) continue;
25850
+ } else if (indexExists(db, indexName)) {
25851
+ continue;
25852
+ }
25853
+ db.exec(statement);
25854
+ }
25855
+ if (wantsFkOff && !alreadyApplied) {
25856
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25857
+ if (violations.length > 0) {
25858
+ throw new Error(
25859
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25860
+ );
25861
+ }
25862
+ }
25863
+ record2.run(migration.tag, Date.now());
25864
+ },
25865
+ "IMMEDIATE"
25866
+ );
25867
+ } finally {
25868
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25869
+ }
25870
+ }
25871
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25872
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25873
+ }
25874
+ ensureSyncedAtColumn(db, "audit_events");
25875
+ ensureScanLedgerTable(db);
25876
+ ensureHistorySyncTable(db);
25877
+ ensureBlockedDetectionsTable(db);
25878
+ ensureRuleProbeCacheTable(db);
25879
+ ensureWriteGateTrigger(db);
25880
+ ensureTokenUsageColumns(db);
25881
+ reconcileSourceProjectIds(db);
25882
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25883
+ const drained = runLegacyHistoryBackfill(db);
25884
+ if (drained) applyLegacyDropMigration(db, file2);
25885
+ }
25886
+ }
25887
+ function readLegacyTables(db) {
25888
+ let holdsRows = false;
25889
+ const marks = [];
25890
+ for (const table of ["events", "findings"]) {
25891
+ try {
25892
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
25893
+ if (row === void 0) {
25894
+ holdsRows = true;
25895
+ marks.push(`${table}:unreadable`);
25896
+ continue;
25897
+ }
25898
+ if (row.n > 0) holdsRows = true;
25899
+ marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
25900
+ } catch {
25901
+ holdsRows = true;
25902
+ marks.push(`${table}:unreadable`);
25903
+ }
25904
+ }
25905
+ return { holdsRows, mark: marks.join("|") };
25906
+ }
25907
+ function applyLegacyDropMigration(db, file2) {
25908
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25909
+ if (!migration) return;
25910
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25911
+ if (file2 !== void 0 && before?.holdsRows === true) {
25912
+ try {
25913
+ backupBeforeLegacyDrop(db, file2);
25914
+ } catch (error61) {
25915
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25916
+ return;
25917
+ }
25918
+ }
25919
+ try {
25920
+ withTransaction(
25921
+ db,
25922
+ () => {
25923
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25924
+ if (alreadyDropped) return;
25925
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25926
+ akaWarn(
25927
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25928
+ );
25929
+ return;
25930
+ }
25931
+ for (const statement of splitStatements(migration.sql)) {
25932
+ db.exec(statement);
25933
+ }
25934
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25935
+ migration.tag,
25936
+ Date.now()
25937
+ );
25938
+ },
25939
+ "IMMEDIATE"
25940
+ );
25941
+ } catch (error61) {
25942
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25943
+ }
25944
+ }
25945
+ function backupBeforeLegacyDrop(db, file2) {
25946
+ reapStalePartials(file2);
25947
+ const backup = backupPath(file2, "pre-drop");
25948
+ snapshotStore(db, backup);
25949
+ return backup;
25950
+ }
25951
+ var TOKEN_USAGE_COLUMNS = [
25952
+ {
25953
+ name: "input_tokens",
25954
+ ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
25955
+ },
25956
+ {
25957
+ name: "output_tokens",
25958
+ ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
25959
+ },
25960
+ {
25961
+ name: "cache_creation_input_tokens",
25962
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
25963
+ },
25964
+ {
25965
+ name: "cache_read_input_tokens",
25966
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25967
+ },
25968
+ {
25969
+ name: "model",
25970
+ ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25971
+ },
25972
+ {
25973
+ name: "provider",
25000
25974
  ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
25001
25975
  }
25002
25976
  ];
@@ -25258,10 +26232,62 @@ function ensureSyncedAtColumn(db, table) {
25258
26232
  if (!columns.includes("outbox_owed")) {
25259
26233
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25260
26234
  }
26235
+ if (!columns.includes("sync_failed_at")) {
26236
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26237
+ }
26238
+ if (!columns.includes("sync_failure")) {
26239
+ withTransaction(
26240
+ db,
26241
+ () => {
26242
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26243
+ db.exec(
26244
+ `UPDATE ${table} SET synced_at = NULL
26245
+ WHERE synced_at = -1
26246
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26247
+ );
26248
+ },
26249
+ "IMMEDIATE"
26250
+ );
26251
+ }
25261
26252
  db.exec(
25262
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25263
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26253
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26254
+ BEFORE UPDATE OF sync_failure ON ${table}
26255
+ WHEN ${syncFailureRejectCondition()}
26256
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25264
26257
  );
26258
+ const syncIndexColumns = [
26259
+ "event_type",
26260
+ "synced_at",
26261
+ "sync_claimed_at",
26262
+ "started_at",
26263
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26264
+ // has to be in the index for the read to stay covered — but putting it
26265
+ // ahead of `started_at` would reorder the prefix the structural drain's
26266
+ // reads match on.
26267
+ "sync_failure"
26268
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26269
+ //
26270
+ // The delivery-state read tests it — a capture's state depends on whether a
26271
+ // live forward marked it owed — so carrying it here makes that read covering
26272
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26273
+ // But a sixth column changes what the planner charges for this index, and
26274
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26275
+ // then stops choosing the per-session index for the token rollup and walks
26276
+ // every `llm_call` in the store through the event-type index instead. That
26277
+ // read grows with the store; this one does not.
26278
+ //
26279
+ // 40 ms on the largest store measured, once per render, is a cost worth
26280
+ // paying to leave every other read's plan where it was.
26281
+ ];
26282
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26283
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26284
+ if (!syncIndexMatches) {
26285
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26286
+ db.exec(
26287
+ `CREATE INDEX idx_audit_events_sync
26288
+ ON audit_events (${syncIndexColumns.join(", ")})`
26289
+ );
26290
+ }
25265
26291
  db.exec(
25266
26292
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25267
26293
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25483,7 +26509,11 @@ function buildAuditEvent(row) {
25483
26509
  link: linkParsed?.success ? linkParsed.data : null,
25484
26510
  targetId: row.target_id,
25485
26511
  internal: intToBool(row.internal),
25486
- flagged: intToBool(row.flagged)
26512
+ flagged: intToBool(row.flagged),
26513
+ // Only meaningful when the title came out empty — a row whose body was
26514
+ // expired but whose title fell back to `tool_name` still has something to
26515
+ // render, and flagging it would make the view apologise for nothing.
26516
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25487
26517
  };
25488
26518
  }
25489
26519
  var TIMELINE_COLUMNS = `
@@ -25491,6 +26521,7 @@ var TIMELINE_COLUMNS = `
25491
26521
  event_type,
25492
26522
  started_at,
25493
26523
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26524
+ content_expired_at,
25494
26525
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25495
26526
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25496
26527
  json_extract(attributes, '$.severity') AS severity,
@@ -26156,6 +27187,88 @@ var SqliteAuditEventsRepository = class {
26156
27187
  }
26157
27188
  };
26158
27189
 
27190
+ // ../../packages/persistence/src/repositories/body-retention.ts
27191
+ var DEFAULT_BATCH_SIZE = 500;
27192
+ var DEFAULT_MAX_ROWS = 5e4;
27193
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27194
+ var SqliteBodyRetentionRepository = class {
27195
+ constructor(db) {
27196
+ this.db = db;
27197
+ const select = (laneClause) => `
27198
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27199
+ FROM audit_events
27200
+ WHERE content IS NOT NULL
27201
+ AND started_at < :cutoff
27202
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27203
+ ${laneClause}
27204
+ ORDER BY started_at
27205
+ LIMIT :limit`;
27206
+ this.candidatesStmt = this.db.prepare(select(""));
27207
+ this.candidatesSyncSafeStmt = this.db.prepare(
27208
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27209
+ );
27210
+ this.heldBySyncStmt = this.db.prepare(`
27211
+ SELECT COUNT(*) AS n
27212
+ FROM audit_events
27213
+ WHERE content IS NOT NULL
27214
+ AND started_at < :cutoff
27215
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27216
+ AND synced_at IS NULL`);
27217
+ this.expireStmt = this.db.prepare(
27218
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27219
+ );
27220
+ }
27221
+ db;
27222
+ candidatesStmt;
27223
+ candidatesSyncSafeStmt;
27224
+ heldBySyncStmt;
27225
+ expireStmt;
27226
+ /** How many bytes a pass with these options would free, changing nothing. */
27227
+ preview(opts) {
27228
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27229
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27230
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27231
+ return {
27232
+ rowsExpired: rows.length,
27233
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27234
+ rowsHeldBySync: this.countHeldBySync(opts)
27235
+ };
27236
+ }
27237
+ /** Clear eligible bodies, in bounded batches. */
27238
+ expire(opts) {
27239
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27240
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27241
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27242
+ let rowsExpired = 0;
27243
+ let bytesFreed = 0;
27244
+ let done = true;
27245
+ while (rowsExpired < maxRows) {
27246
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27247
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27248
+ if (batch.length === 0) break;
27249
+ withTransaction(
27250
+ this.db,
27251
+ () => {
27252
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27253
+ },
27254
+ "IMMEDIATE"
27255
+ );
27256
+ rowsExpired += batch.length;
27257
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27258
+ if (batch.length < remaining) break;
27259
+ if (rowsExpired >= maxRows) {
27260
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27261
+ }
27262
+ }
27263
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27264
+ }
27265
+ countHeldBySync(opts) {
27266
+ if (opts.sweepSyncLane) return 0;
27267
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27268
+ return row.n;
27269
+ }
27270
+ };
27271
+
26159
27272
  // ../../packages/persistence/src/repositories/classified-data.ts
26160
27273
  var SqliteClassifiedDataRepository = class {
26161
27274
  constructor(db) {
@@ -26984,7 +28097,15 @@ function toFlatFindingRow(r) {
26984
28097
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26985
28098
  eventId: r.event_id,
26986
28099
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26987
- status: deriveInstanceStatus(r)
28100
+ status: deriveInstanceStatus(r),
28101
+ delivery: deriveFindingDelivery({
28102
+ kind: r.kind,
28103
+ syncedAt: r.synced_at,
28104
+ syncClaimedAt: r.sync_claimed_at,
28105
+ syncFailedAt: r.sync_failed_at,
28106
+ syncFailure: r.sync_failure,
28107
+ outboxOwed: r.outbox_owed
28108
+ })
26988
28109
  };
26989
28110
  }
26990
28111
  function encodeGroupCursor(group) {
@@ -27048,7 +28169,10 @@ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS c
27048
28169
  e.tool_name AS tool_name,
27049
28170
  f.audit_event_id AS event_id, e.root_session_id AS session_id,
27050
28171
  e.event_type AS kind, f.finding_key AS finding_key,
27051
- ${latestResolutionStatusSql("f")} AS latest_status`;
28172
+ ${latestResolutionStatusSql("f")} AS latest_status,
28173
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28174
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28175
+ e.outbox_owed AS outbox_owed`;
27052
28176
  var DAY_MS3 = 864e5;
27053
28177
  var SqliteFindingsRepository = class {
27054
28178
  constructor(db) {
@@ -27293,6 +28417,7 @@ var SqliteFindingsRepository = class {
27293
28417
  providers: query.provider,
27294
28418
  actions: query.action,
27295
28419
  statuses: query.status,
28420
+ deliveries: query.deployment,
27296
28421
  tools: query.tool,
27297
28422
  repo: query.repo,
27298
28423
  file: query.file,
@@ -27360,6 +28485,7 @@ var SqliteFindingsRepository = class {
27360
28485
  providers: query.provider,
27361
28486
  actions: query.action,
27362
28487
  statuses: query.status,
28488
+ deliveries: query.deployment,
27363
28489
  tools: query.tool,
27364
28490
  q: query.q
27365
28491
  };
@@ -27623,7 +28749,9 @@ var SqliteFindingsRepository = class {
27623
28749
  )
27624
28750
  );
27625
28751
  for (const row of grouped) {
27626
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28752
+ if (Object.hasOwn(byAction, row.action_taken)) {
28753
+ byAction[row.action_taken] = row.c;
28754
+ }
27627
28755
  }
27628
28756
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27629
28757
  const sevRows = allRows(
@@ -27640,7 +28768,9 @@ var SqliteFindingsRepository = class {
27640
28768
  )
27641
28769
  );
27642
28770
  for (const row of sevRows) {
27643
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28771
+ if (Object.hasOwn(bySeverity, row.severity)) {
28772
+ bySeverity[row.severity] = row.c;
28773
+ }
27644
28774
  }
27645
28775
  const categories = ENFORCEABLE_CATEGORIES;
27646
28776
  const enabledRows = allRows(
@@ -27689,525 +28819,6 @@ function isoDay(ms) {
27689
28819
  return new Date(ms).toISOString().slice(0, 10);
27690
28820
  }
27691
28821
 
27692
- // ../../packages/persistence/src/repositories/history-sync.ts
27693
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27694
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27695
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27696
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27697
- var SKIPPED = -1;
27698
- var ROW_COLUMNS = `id,
27699
- parent_id AS parentId,
27700
- root_session_id AS rootSessionId,
27701
- event_type AS eventType,
27702
- host_id AS hostId,
27703
- harness_id AS harnessId,
27704
- source_project_id AS sourceProjectId,
27705
- started_at AS startedAt,
27706
- ended_at AS endedAt,
27707
- severity,
27708
- priority,
27709
- content,
27710
- content_hash AS contentHash,
27711
- attributes`;
27712
- var SqliteHistorySyncRepository = class {
27713
- constructor(db) {
27714
- this.db = db;
27715
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27716
- this.sessionsStmt = db.prepare(
27717
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27718
- FROM audit_events
27719
- WHERE synced_at IS NULL
27720
- AND event_type IN (${TYPE_LIST})
27721
- AND started_at < :before
27722
- GROUP BY sessionId
27723
- ORDER BY earliest
27724
- LIMIT :limit`
27725
- );
27726
- this.rowsStmt = db.prepare(
27727
- `SELECT ${ROW_COLUMNS}
27728
- FROM audit_events
27729
- WHERE synced_at IS NULL
27730
- AND event_type IN (${TYPE_LIST})
27731
- AND started_at < :before
27732
- AND COALESCE(root_session_id, id) = :sessionId
27733
- ORDER BY (event_type = 'session') DESC, started_at
27734
- LIMIT :limit`
27735
- );
27736
- this.captureRowsStmt = db.prepare(
27737
- `SELECT ${ROW_COLUMNS}
27738
- FROM audit_events
27739
- WHERE synced_at IS NULL
27740
- AND sync_claimed_at IS NULL
27741
- AND outbox_owed = 1
27742
- AND event_type IN (${CAPTURE_TYPE_LIST})
27743
- AND started_at < :before
27744
- ORDER BY started_at
27745
- LIMIT :limit`
27746
- );
27747
- this.markOwedStmt = db.prepare(
27748
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27749
- );
27750
- this.markCaptureBacklogOwedStmt = db.prepare(
27751
- `UPDATE audit_events SET outbox_owed = 1
27752
- WHERE synced_at IS NULL
27753
- AND event_type IN (${CAPTURE_TYPE_LIST})
27754
- AND started_at < :before`
27755
- );
27756
- this.stampStmt = db.prepare(
27757
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27758
- );
27759
- this.claimRowStmt = db.prepare(
27760
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27761
- );
27762
- this.releaseRowStmt = db.prepare(
27763
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27764
- );
27765
- this.releaseStaleClaimsStmt = db.prepare(
27766
- `UPDATE audit_events SET sync_claimed_at = NULL
27767
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27768
- );
27769
- this.partitionStmt = db.prepare(
27770
- `SELECT
27771
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27772
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27773
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27774
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27775
- COUNT(*) AS total
27776
- FROM audit_events
27777
- WHERE event_type IN (${TYPE_LIST})`
27778
- );
27779
- this.countsStmt = db.prepare(
27780
- `SELECT
27781
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27782
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27783
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27784
- FROM audit_events
27785
- WHERE event_type IN (${TYPE_LIST})`
27786
- );
27787
- this.captureSkipCountStmt = db.prepare(
27788
- `SELECT COUNT(*) AS skipped
27789
- FROM audit_events
27790
- WHERE synced_at = ${String(SKIPPED)}
27791
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27792
- );
27793
- this.fingerprintStmt = db.prepare(
27794
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27795
- FROM history_sync WHERE id = 1`
27796
- );
27797
- this.setFingerprintStmt = db.prepare(
27798
- `UPDATE history_sync
27799
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27800
- WHERE id = 1`
27801
- );
27802
- this.disownCapturesStmt = db.prepare(
27803
- `UPDATE audit_events SET outbox_owed = NULL
27804
- WHERE outbox_owed IS NOT NULL
27805
- AND event_type IN (${CAPTURE_TYPE_LIST})
27806
- AND started_at < :attachedAt`
27807
- );
27808
- this.rearmStmt = db.prepare(
27809
- `UPDATE audit_events SET synced_at = NULL
27810
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27811
- );
27812
- this.claimStmt = db.prepare(
27813
- `UPDATE history_sync
27814
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27815
- WHERE id = 1
27816
- AND (owner_pid IS NULL
27817
- OR heartbeat_at IS NULL
27818
- OR heartbeat_at < :staleBefore
27819
- OR heartbeat_at > :now)`
27820
- );
27821
- this.heartbeatStmt = db.prepare(
27822
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27823
- );
27824
- this.releaseStmt = db.prepare(
27825
- `UPDATE history_sync
27826
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27827
- WHERE id = 1 AND owner_pid = :pid`
27828
- );
27829
- this.closeWindowStmt = db.prepare(
27830
- `UPDATE audit_events SET synced_at = :at
27831
- WHERE synced_at IS NULL
27832
- AND event_type IN (${TYPE_LIST})
27833
- AND started_at >= :attachedAt`
27834
- );
27835
- this.releaseBoundaryStmt = db.prepare(
27836
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27837
- );
27838
- this.freezeBoundaryStmt = db.prepare(
27839
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27840
- );
27841
- this.leaseStmt = db.prepare(
27842
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27843
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27844
- FROM history_sync WHERE id = 1`
27845
- );
27846
- this.inspectionsStmt = db.prepare(
27847
- `SELECT d.rule_id AS ruleId,
27848
- d.name AS ruleName,
27849
- d.version AS ruleVersion,
27850
- d.category AS category,
27851
- d.severity AS severity,
27852
- f.span_start AS spanStart,
27853
- f.span_end AS spanEnd,
27854
- f.masked_match AS maskedMatch,
27855
- f.action_taken AS actionTaken,
27856
- f.confidence AS confidence
27857
- FROM inspection_findings f
27858
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27859
- WHERE f.audit_event_id = :auditEventId
27860
- ORDER BY f.span_start, f.id`
27861
- );
27862
- }
27863
- db;
27864
- ensureRowStmt;
27865
- sessionsStmt;
27866
- rowsStmt;
27867
- stampStmt;
27868
- countsStmt;
27869
- fingerprintStmt;
27870
- setFingerprintStmt;
27871
- rearmStmt;
27872
- claimStmt;
27873
- heartbeatStmt;
27874
- releaseStmt;
27875
- leaseStmt;
27876
- inspectionsStmt;
27877
- closeWindowStmt;
27878
- releaseBoundaryStmt;
27879
- freezeBoundaryStmt;
27880
- captureRowsStmt;
27881
- markOwedStmt;
27882
- markCaptureBacklogOwedStmt;
27883
- captureSkipCountStmt;
27884
- disownCapturesStmt;
27885
- partitionStmt;
27886
- claimRowStmt;
27887
- releaseRowStmt;
27888
- releaseStaleClaimsStmt;
27889
- /**
27890
- * The masked detections recorded against one tool call.
27891
- *
27892
- * These travel with the event because a tool call's target is not
27893
- * re-inspectable from the event alone — unlike a capture, where the text
27894
- * itself is re-scannable. What crosses is the masked match and the rule that
27895
- * produced it, never the value.
27896
- */
27897
- inspectionsFor(auditEventId) {
27898
- return allRows(this.inspectionsStmt, { auditEventId });
27899
- }
27900
- /**
27901
- * Sessions with structural rows still to send, oldest first.
27902
- *
27903
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27904
- * read. Anything recorded after the machine attached is the live forward
27905
- * path's to deliver; this drain exists for what was recorded before it, and a
27906
- * row both paths send is at best a duplicate request and at worst — for a
27907
- * session root — an overwrite of the inventory ids the live path resolved.
27908
- */
27909
- pendingSessions(limit, before) {
27910
- return allRows(this.sessionsStmt, { limit, before }).map(
27911
- (r) => r.sessionId
27912
- );
27913
- }
27914
- /** One session's undelivered structural rows within the backlog, root first. */
27915
- pendingRows(sessionId, limit, before) {
27916
- return allRows(this.rowsStmt, { sessionId, limit, before });
27917
- }
27918
- /**
27919
- * Captures this machine still owes the deployment, oldest first.
27920
- *
27921
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27922
- * by a time window — see captureRowsStmt for why a window could not express
27923
- * this. `before` is the grace window that leaves a just-recorded capture to
27924
- * the live path.
27925
- */
27926
- pendingCaptureRows(limit, before) {
27927
- return allRows(this.captureRowsStmt, { limit, before });
27928
- }
27929
- /**
27930
- * Record that a capture is OWED to the deployment.
27931
- *
27932
- * Written by the attached forward path when a live send did not confirm
27933
- * delivery, and read by the drain as the whole of its eligibility test. It is
27934
- * a fact rather than an inference: the machine was attached, the send did not
27935
- * land, so the row is owed — which no time window can state, because the same
27936
- * window that holds the rows a past attachment left owed also holds every
27937
- * capture recorded while the machine was DETACHED, and those were never
27938
- * offered to anyone.
27939
- *
27940
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27941
- * out of the drain's read.
27942
- */
27943
- markCaptureOwed(id) {
27944
- this.markOwedStmt.run({ id });
27945
- }
27946
- /**
27947
- * Mark every capture already on disk as owed, as of `before`.
27948
- *
27949
- * The consent-time backfill, called once from `aka attach` when a human
27950
- * grants existing-history consent — never from an ongoing drain pass, and
27951
- * never inferred from a boundary that could later move. `before` is the
27952
- * caller's own "now" at the moment consent was granted, so what this marks
27953
- * is exactly the backlog the consent prompt already counted, not whatever a
27954
- * later re-attach or key rotation might widen it to.
27955
- *
27956
- * Returns how many rows matched, for the caller to log or test against. Not a
27957
- * count of NEWLY marked rows — a row still unsynced from an earlier call
27958
- * matches again and is counted again, the same as `UPDATE`'s own `changes`.
27959
- */
27960
- markCaptureBacklogOwed(before) {
27961
- return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
27962
- }
27963
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27964
- markSynced(ids, atMs) {
27965
- this.stampAll(ids, atMs);
27966
- }
27967
- /**
27968
- * Record that a row will never be sent.
27969
- *
27970
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27971
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27972
- * is retried; marking those would turn one outage into permanent data loss.
27973
- */
27974
- markSkipped(ids) {
27975
- this.stampAll(ids, SKIPPED);
27976
- }
27977
- eachInTransaction(ids, run) {
27978
- if (ids.length === 0) return;
27979
- withTransaction(
27980
- this.db,
27981
- () => {
27982
- for (const id of ids) run(id);
27983
- },
27984
- "IMMEDIATE"
27985
- );
27986
- }
27987
- stampAll(ids, value) {
27988
- if (ids.length === 0) return;
27989
- withTransaction(
27990
- this.db,
27991
- () => {
27992
- for (const id of ids) this.stampStmt.run({ at: value, id });
27993
- },
27994
- "IMMEDIATE"
27995
- );
27996
- }
27997
- /**
27998
- * Claim rows as in-flight.
27999
- *
28000
- * Advisory in exactly the sense the lease is: it records that a send is in
28001
- * progress so a surface can say so, and a lost claim costs a row showing as
28002
- * queued while it is actually being sent. It is not exclusion — the far side
28003
- * settles a duplicate on the row id.
28004
- */
28005
- claimRows(ids, atMs) {
28006
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
28007
- }
28008
- /** Give back a claim without settling — the send failed, the row is queued again. */
28009
- releaseRows(ids) {
28010
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
28011
- }
28012
- /**
28013
- * Clear claims older than `staleBefore`, and report how many were cleared.
28014
- *
28015
- * A process killed between claiming and settling leaves rows claimed with
28016
- * nothing left to settle them. Without this they read as "sending" for ever.
28017
- */
28018
- releaseStaleClaims(staleBefore) {
28019
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
28020
- }
28021
- /**
28022
- * Every tracked row in exactly one delivery state.
28023
- *
28024
- * Takes no boundary on purpose. The boundary answers "what should the drain
28025
- * pick up now", which is a different question from "what state is this row
28026
- * in" — and a machine that has never attached has no boundary to pass, so
28027
- * requiring one would force a caller to invent one and report the whole store
28028
- * as queued.
28029
- */
28030
- partition() {
28031
- const row = getRow(this.partitionStmt, {});
28032
- return {
28033
- queued: row?.queued ?? 0,
28034
- inProgress: row?.inProgress ?? 0,
28035
- synced: row?.synced ?? 0,
28036
- failed: row?.failed ?? 0,
28037
- total: row?.total ?? 0
28038
- };
28039
- }
28040
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
28041
- counts(before) {
28042
- const row = getRow(
28043
- this.countsStmt,
28044
- { before }
28045
- );
28046
- const captures = getRow(this.captureSkipCountStmt);
28047
- return {
28048
- pending: row?.pending ?? 0,
28049
- sent: row?.sent ?? 0,
28050
- skipped: row?.skipped ?? 0,
28051
- capturesSkipped: captures?.skipped ?? 0
28052
- };
28053
- }
28054
- /**
28055
- * The deployment the current stamps were made against, and where its backlog
28056
- * ends.
28057
- *
28058
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
28059
- * machine that has never drained is — and every writer below seeds the row
28060
- * before it needs one, so nothing depends on this creating it. Keeping the
28061
- * write off the gate path matters because the gate runs on every pass while a
28062
- * write has to take the database's write lock.
28063
- */
28064
- deployment() {
28065
- const row = getRow(
28066
- this.fingerprintStmt
28067
- );
28068
- return {
28069
- fingerprint: row?.fingerprint ?? void 0,
28070
- backlogBefore: row?.backlogBefore ?? void 0
28071
- };
28072
- }
28073
- /**
28074
- * Point the ledger at a different deployment, discarding what it recorded
28075
- * about the previous one.
28076
- *
28077
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
28078
- * machine has just left are undelivered as far as the new one is concerned.
28079
- * All four in one transaction, so a crash between them cannot leave stamps
28080
- * attributed to the wrong deployment, a boundary that belongs to another, or
28081
- * a disown with no re-mark to follow it.
28082
- *
28083
- * The boundary is written HERE and only here, which is what freezes it: a
28084
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28085
- * unchanged, so this never runs and the backlog does not widen back over rows
28086
- * the live path has since delivered.
28087
- *
28088
- * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
28089
- * granted existing-history consent for the deployment this call is arming —
28090
- * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
28091
- * instant, `backlogBefore` is the ATTACH instant, and the two can be far
28092
- * apart. Passed only when that grant is valid, since this method has no way
28093
- * to check consent itself and must not mark a row owed for a machine that
28094
- * never agreed to it. Applied AFTER the disown above, in the SAME
28095
- * transaction: what the disown clears is every marker below `backlogBefore`,
28096
- * which includes this deployment's OWN pre-attach rows — `aka attach` calls
28097
- * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
28098
- * on the cleared side of that bound — and the re-mark in the same
28099
- * transaction is what puts those rows back. A crash between the two cannot
28100
- * strand the ledger disowned with nothing re-marked — the transaction either
28101
- * lands whole or not at all, and a fingerprint mismatch that has not yet
28102
- * committed re-enters this method on the very next pass. Omit it (the
28103
- * structural-only tests do) to exercise the disown in isolation.
28104
- *
28105
- * The disown is bounded by `backlogBefore`, which is what keeps it from
28106
- * touching a marker the NEW deployment's OWN live path has already set: B's
28107
- * live path can mark a capture owed from the moment `aka attach` writes the
28108
- * descriptor, before the drain's first pass ever reaches this method, and
28109
- * such a row sits at or after the bound rather than below it. What keeps the
28110
- * disown from eating THIS SAME CALL's own re-mark is the order, not the
28111
- * bound — disown runs first, re-mark second, both inside the one
28112
- * transaction above.
28113
- */
28114
- rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
28115
- this.ensureRowStmt.run();
28116
- withTransaction(
28117
- this.db,
28118
- () => {
28119
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28120
- this.rearmStmt.run();
28121
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28122
- this.disownCapturesStmt.run({ attachedAt: backlogBefore });
28123
- }
28124
- if (backfillCapturesBefore !== void 0) {
28125
- this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
28126
- }
28127
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28128
- },
28129
- "IMMEDIATE"
28130
- );
28131
- }
28132
- /**
28133
- * End the attached period: hand its rows to the live path, and release the
28134
- * boundary so the next attachment can freeze a new one.
28135
- *
28136
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28137
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28138
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28139
- * during the detached period, because the machine is not attached. Rows
28140
- * recorded in that window sit after the boundary and before the re-attach, so
28141
- * neither path takes them, and the pending count reports none outstanding.
28142
- *
28143
- * Stamping the attached window is not a claim that every one of those rows
28144
- * reached the deployment — the live path drops on failure and says so
28145
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28146
- * status quo: they sit outside the frozen boundary today and are equally never
28147
- * re-sent. Making it explicit is what lets the boundary move.
28148
- *
28149
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28150
- * window unstamped — that half-state would re-send the whole attached period
28151
- * on the next attach, which is the failure the boundary exists to prevent.
28152
- */
28153
- closeAttachedWindow(attachedAtMs, atMs) {
28154
- this.ensureRowStmt.run();
28155
- withTransaction(
28156
- this.db,
28157
- () => {
28158
- const row = getRow(this.fingerprintStmt);
28159
- const from = row?.backlogBefore ?? attachedAtMs;
28160
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28161
- this.releaseBoundaryStmt.run();
28162
- },
28163
- "IMMEDIATE"
28164
- );
28165
- }
28166
- /**
28167
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28168
- *
28169
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28170
- * different deployment and therefore discards what was delivered to the old
28171
- * one: here the recipient is the same, so everything already sent to it stays
28172
- * sent.
28173
- */
28174
- freezeBoundary(backlogBefore) {
28175
- this.ensureRowStmt.run();
28176
- this.freezeBoundaryStmt.run({ backlogBefore });
28177
- }
28178
- /** Take the claim, or report that someone live already holds it. */
28179
- claim(pid, host, nowMs, staleAfterMs) {
28180
- this.ensureRowStmt.run();
28181
- let taken = false;
28182
- withTransaction(
28183
- this.db,
28184
- () => {
28185
- const result = this.claimStmt.run({
28186
- pid,
28187
- host,
28188
- now: nowMs,
28189
- staleBefore: nowMs - staleAfterMs
28190
- });
28191
- taken = result.changes === 1;
28192
- },
28193
- "IMMEDIATE"
28194
- );
28195
- return taken;
28196
- }
28197
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28198
- heartbeat(pid, nowMs) {
28199
- this.heartbeatStmt.run({ now: nowMs, pid });
28200
- }
28201
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28202
- release(pid) {
28203
- this.releaseStmt.run({ pid });
28204
- }
28205
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28206
- lease() {
28207
- return getRow(this.leaseStmt);
28208
- }
28209
- };
28210
-
28211
28822
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28212
28823
  var SqliteInspectionDefinitionsRepository = class {
28213
28824
  constructor(db) {
@@ -28438,6 +29049,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28438
29049
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28439
29050
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28440
29051
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29052
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28441
29053
  if (values.vaultConsent !== void 0) {
28442
29054
  merged.vaultConsent = values.vaultConsent ? (
28443
29055
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30945,7 +31557,7 @@ var SqliteSecurityRepository = class {
30945
31557
  ELSE 0
30946
31558
  END) AS open_at_rest
30947
31559
  FROM inspection_findings f
30948
- JOIN audit_events e ON e.id = f.audit_event_id
31560
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30949
31561
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30950
31562
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30951
31563
  ON latest.finding_key = f.finding_key
@@ -31171,7 +31783,7 @@ var SqliteSecurityRepository = class {
31171
31783
  this.db.prepare(
31172
31784
  `SELECT e.repo AS repo, count(*) AS c
31173
31785
  FROM inspection_findings f
31174
- JOIN audit_events e ON e.id = f.audit_event_id
31786
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31175
31787
  WHERE e.started_at >= :from AND e.started_at < :to
31176
31788
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31177
31789
  AND e.repo IS NOT NULL
@@ -31295,7 +31907,7 @@ var SqliteSecurityRepository = class {
31295
31907
  d.severity AS severity,
31296
31908
  COUNT(*) AS count
31297
31909
  FROM inspection_findings f
31298
- JOIN audit_events e ON e.id = f.audit_event_id
31910
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31299
31911
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31300
31912
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31301
31913
  ON latest.finding_key = f.finding_key
@@ -31330,7 +31942,7 @@ var SqliteSecurityRepository = class {
31330
31942
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31331
31943
  d.rule_id AS rule_id, d.category AS category
31332
31944
  FROM inspection_findings f
31333
- JOIN audit_events e ON e.id = f.audit_event_id
31945
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31334
31946
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31335
31947
  WHERE e.started_at >= :from AND e.started_at < :to
31336
31948
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32171,6 +32783,7 @@ function openWithPragmas(file2) {
32171
32783
  db.exec("PRAGMA journal_mode = WAL");
32172
32784
  db.exec("PRAGMA busy_timeout = 2000");
32173
32785
  db.exec("PRAGMA foreign_keys = ON");
32786
+ registerSqlFunctions(db);
32174
32787
  } catch (err) {
32175
32788
  closeQuietly(db);
32176
32789
  throw err;
@@ -32200,7 +32813,7 @@ function backupLegacyStore(db, file2) {
32200
32813
  discardStore(file2, backup);
32201
32814
  return backup;
32202
32815
  }
32203
- function openAndInitialize(file2, base) {
32816
+ function openAndInitialize(file2, base, skipTags) {
32204
32817
  let db = openWithPragmas(file2);
32205
32818
  try {
32206
32819
  if (isForeignSqliteLineage(db)) {
@@ -32210,7 +32823,7 @@ function openAndInitialize(file2, base) {
32210
32823
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32211
32824
  );
32212
32825
  }
32213
- applyMigrations(db, file2);
32826
+ applyMigrations(db, file2, { skipTags });
32214
32827
  tightenPerms(file2);
32215
32828
  const policies = new SqlitePoliciesRepository(db);
32216
32829
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32225,6 +32838,7 @@ function openAndInitialize(file2, base) {
32225
32838
  exceptions: new SqliteExceptionsRepository(db),
32226
32839
  resolutions: new SqliteResolutionsRepository(db),
32227
32840
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32841
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32228
32842
  security: new SqliteSecurityRepository(db),
32229
32843
  detections: new SqliteDetectionsRepository(db),
32230
32844
  shares: new SqliteSharesRepository(db),
@@ -32247,7 +32861,8 @@ function openAndInitialize(file2, base) {
32247
32861
  throw err;
32248
32862
  }
32249
32863
  }
32250
- function openLocalDatabase(dir) {
32864
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32865
+ function openLocalDatabase(dir, options = {}) {
32251
32866
  ensureDataDirSync(dir);
32252
32867
  const file2 = join7(dir, DB_FILENAME);
32253
32868
  reapStalePartials(file2);
@@ -32259,6 +32874,7 @@ function openLocalDatabase(dir) {
32259
32874
  installedPacks,
32260
32875
  scanLedger,
32261
32876
  historySync,
32877
+ bodyRetention,
32262
32878
  secretVault,
32263
32879
  exceptions,
32264
32880
  resolutions,
@@ -32282,7 +32898,8 @@ function openLocalDatabase(dir) {
32282
32898
  // `dir` is always `<base>/data` — every caller resolves it through
32283
32899
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32284
32900
  // settings/ and data/, and the pack-policy floor needs both halves.
32285
- dirname2(dir)
32901
+ dirname2(dir),
32902
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32286
32903
  );
32287
32904
  function captureRowId(event) {
32288
32905
  return captureId(
@@ -32475,6 +33092,7 @@ function openLocalDatabase(dir) {
32475
33092
  installedPacks,
32476
33093
  scanLedger,
32477
33094
  historySync,
33095
+ bodyRetention,
32478
33096
  secretVault,
32479
33097
  exceptions,
32480
33098
  resolutions,
@@ -32515,8 +33133,35 @@ function openLocalDatabase(dir) {
32515
33133
 
32516
33134
  // ../../packages/persistence/src/egress-wire.ts
32517
33135
  import { createHash as createHash3 } from "crypto";
33136
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33137
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33138
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33139
+ var FILE_URL = /^file:\/\//i;
33140
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33141
+ var SLASH = "/".charCodeAt(0);
33142
+ var GIT_SUFFIX = ".git";
33143
+ function trimSlashes(path) {
33144
+ let start = 0;
33145
+ let end = path.length;
33146
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33147
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33148
+ return path.slice(start, end);
33149
+ }
33150
+ function canonicalGitUrl(url2) {
33151
+ const trimmed = url2.trim();
33152
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33153
+ const scheme = SCHEME_FORM.exec(trimmed);
33154
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33155
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33156
+ if (host === void 0) return trimmed;
33157
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33158
+ const bare = trimSlashes(path);
33159
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33160
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33161
+ }
32518
33162
  function hashProjectKey(projectKey) {
32519
- return createHash3("sha256").update(projectKey, "utf8").digest("hex");
33163
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33164
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
32520
33165
  }
32521
33166
  function toIngestHit(hit) {
32522
33167
  return {
@@ -32683,18 +33328,50 @@ function fingerprintValue(key, raw) {
32683
33328
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32684
33329
  }
32685
33330
 
33331
+ // ../../packages/persistence/src/forward-health.ts
33332
+ import { readFileSync as readFileSync7 } from "fs";
33333
+ import { join as join9 } from "path";
33334
+ var FAILURES = /* @__PURE__ */ new Set([
33335
+ "unauthorized",
33336
+ "forbidden",
33337
+ "unreachable"
33338
+ ]);
33339
+ var BREAKER_COOLDOWN_MS = 3e4;
33340
+ function parseForwardHealth(raw, nowMs) {
33341
+ try {
33342
+ const parsed2 = JSON.parse(raw);
33343
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33344
+ const record2 = parsed2;
33345
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33346
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33347
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33348
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33349
+ } catch {
33350
+ return null;
33351
+ }
33352
+ }
33353
+ function isForwardPaused(health, nowMs) {
33354
+ const openedAtMs = health?.openedAtMs ?? null;
33355
+ if (openedAtMs === null) return false;
33356
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33357
+ }
33358
+
32686
33359
  // ../../packages/persistence/src/history-backfill.ts
32687
33360
  import { existsSync as existsSync4 } from "fs";
32688
- import { join as join9 } from "path";
33361
+ import { join as join10 } from "path";
32689
33362
 
32690
33363
  // ../../packages/persistence/src/history-preview.ts
32691
33364
  import { existsSync as existsSync5 } from "fs";
32692
- import { join as join10 } from "path";
33365
+ import { join as join11 } from "path";
32693
33366
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32694
33367
 
33368
+ // ../../packages/persistence/src/history-sync-state.ts
33369
+ import { readFileSync as readFileSync8 } from "fs";
33370
+ import { join as join12 } from "path";
33371
+
32695
33372
  // ../../packages/persistence/src/store-symlinks.ts
32696
33373
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32697
- import { dirname as dirname3, join as join11, resolve } from "path";
33374
+ import { dirname as dirname3, join as join13, resolve } from "path";
32698
33375
  var STORE_DB = "the store database (including the prompt corpus)";
32699
33376
  var STORE_SETTINGS = "your settings file";
32700
33377
  function storeContents(home) {
@@ -32703,7 +33380,7 @@ function storeContents(home) {
32703
33380
  [settingsDir(home), STORE_SETTINGS],
32704
33381
  [dataDir(home), STORE_DB],
32705
33382
  [keysDir(home), "the vault key"],
32706
- [join11(settingsDir(home), "settings.json"), STORE_SETTINGS],
33383
+ [join13(settingsDir(home), "settings.json"), STORE_SETTINGS],
32707
33384
  [dbPath(home), STORE_DB]
32708
33385
  ]);
32709
33386
  }
@@ -32755,19 +33432,19 @@ import {
32755
33432
  // ../../packages/persistence/src/vault/key-provider.ts
32756
33433
  import { execFileSync } from "child_process";
32757
33434
  import { randomBytes as randomBytes2 } from "crypto";
32758
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32759
- import { join as join12 } from "path";
33435
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33436
+ import { join as join14 } from "path";
32760
33437
 
32761
33438
  // ../../packages/persistence/src/vault/vault.ts
32762
33439
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32763
33440
 
32764
33441
  // ../../packages/persistence/src/warn-era-cap.ts
32765
33442
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
32766
- import { join as join13 } from "path";
33443
+ import { join as join15 } from "path";
32767
33444
  var MARKER = "warn-era-capped";
32768
33445
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32769
33446
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32770
- const marker = join13(dataDir2, MARKER);
33447
+ const marker = join15(dataDir2, MARKER);
32771
33448
  if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32772
33449
  const capped = db.policies.capCategoryActions();
32773
33450
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
@@ -32958,10 +33635,10 @@ function parsed(schema, body, route) {
32958
33635
  }
32959
33636
  function withoutTrailingSlashes(endpoint) {
32960
33637
  let end = endpoint.length;
32961
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
33638
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
32962
33639
  return endpoint.slice(0, end);
32963
33640
  }
32964
- var SLASH = "/".charCodeAt(0);
33641
+ var SLASH2 = "/".charCodeAt(0);
32965
33642
  function createRemoteClient(options) {
32966
33643
  const base = withoutTrailingSlashes(options.endpoint);
32967
33644
  const url2 = (route) => `${base}${route}`;
@@ -33143,11 +33820,11 @@ function withTimeout(promise2, ms) {
33143
33820
  }
33144
33821
 
33145
33822
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
33146
- import { readFileSync as readFileSync8 } from "fs";
33147
- import { join as join14 } from "path";
33823
+ import { readFileSync as readFileSync10 } from "fs";
33824
+ import { join as join16 } from "path";
33148
33825
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
33149
33826
  function forwardDropsPath(dataDir2) {
33150
- return join14(dataDir2, FORWARD_DROPS_FILENAME);
33827
+ return join16(dataDir2, FORWARD_DROPS_FILENAME);
33151
33828
  }
33152
33829
  function recordForwardDrops(dataDir2, count, nowMs) {
33153
33830
  if (count <= 0) return;
@@ -33165,7 +33842,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
33165
33842
  }
33166
33843
  function readForwardDrops(dataDir2) {
33167
33844
  try {
33168
- const parsed2 = JSON.parse(readFileSync8(forwardDropsPath(dataDir2), "utf8"));
33845
+ const parsed2 = JSON.parse(readFileSync10(forwardDropsPath(dataDir2), "utf8"));
33169
33846
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
33170
33847
  const record2 = parsed2;
33171
33848
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -33183,13 +33860,12 @@ function readForwardDrops(dataDir2) {
33183
33860
 
33184
33861
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
33185
33862
  import { randomUUID as randomUUID15 } from "crypto";
33186
- import { readFileSync as readFileSync15 } from "fs";
33187
33863
  import { readFile, rename, writeFile } from "fs/promises";
33188
- import { join as join24 } from "path";
33864
+ import { join as join26 } from "path";
33189
33865
 
33190
33866
  // ../../packages/plugin-sdk/src/config.ts
33191
33867
  import { existsSync as existsSync8 } from "fs";
33192
- import { join as join15 } from "path";
33868
+ import { join as join17 } from "path";
33193
33869
 
33194
33870
  // ../../packages/plugin-sdk/src/provider-env.ts
33195
33871
  var DEFAULT_ANTHROPIC_HOST = "api.anthropic.com";
@@ -33243,7 +33919,7 @@ function resolveProvider() {
33243
33919
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
33244
33920
  try {
33245
33921
  ensureLayoutDirSync(base);
33246
- const settingsFile = join15(settingsDir(base), "settings.json");
33922
+ const settingsFile = join17(settingsDir(base), "settings.json");
33247
33923
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
33248
33924
  } catch {
33249
33925
  }
@@ -33267,9 +33943,9 @@ function resolveProviderSafe(resolveProviderFn) {
33267
33943
  }
33268
33944
 
33269
33945
  // ../../packages/plugin-sdk/src/config-inventory.ts
33270
- import { readdirSync as readdirSync2, readFileSync as readFileSync10, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33946
+ import { readdirSync as readdirSync2, readFileSync as readFileSync12, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33271
33947
  import { homedir as homedir2 } from "os";
33272
- import { basename as basename3, join as join17 } from "path";
33948
+ import { basename as basename3, join as join19 } from "path";
33273
33949
 
33274
33950
  // ../../packages/detections/src/egress/registry.ts
33275
33951
  var EXTRACTOR_VERSION = "1";
@@ -36361,8 +37037,8 @@ function uniqueRuleIds(findings) {
36361
37037
  }
36362
37038
 
36363
37039
  // ../../packages/plugin-sdk/src/repo.ts
36364
- import { existsSync as existsSync9, readFileSync as readFileSync9, statSync as statSync6 } from "fs";
36365
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join16, sep as sep2 } from "path";
37040
+ import { existsSync as existsSync9, readFileSync as readFileSync11, statSync as statSync6 } from "fs";
37041
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join18, sep as sep2 } from "path";
36366
37042
  function resolveRepo(cwd) {
36367
37043
  try {
36368
37044
  const root = findGitRoot(cwd);
@@ -36377,36 +37053,36 @@ function resolveRepo(cwd) {
36377
37053
  function findGitRoot(start) {
36378
37054
  let dir = start;
36379
37055
  for (; ; ) {
36380
- if (existsSync9(join16(dir, ".git"))) return dir;
37056
+ if (existsSync9(join18(dir, ".git"))) return dir;
36381
37057
  const parent = dirname4(dir);
36382
37058
  if (parent === dir) return void 0;
36383
37059
  dir = parent;
36384
37060
  }
36385
37061
  }
36386
37062
  function resolveGitContext(root) {
36387
- const dotGit = join16(root, ".git");
37063
+ const dotGit = join18(root, ".git");
36388
37064
  try {
36389
37065
  if (statSync6(dotGit).isDirectory()) {
36390
- return { configPath: join16(dotGit, "config"), headRoot: root };
37066
+ return { configPath: join18(dotGit, "config"), headRoot: root };
36391
37067
  }
36392
37068
  } catch {
36393
37069
  return void 0;
36394
37070
  }
36395
37071
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
36396
37072
  if (!target) return void 0;
36397
- const gitdir = isAbsolute(target) ? target : join16(root, target);
36398
- if (existsSync9(join16(gitdir, "config"))) {
36399
- return { configPath: join16(gitdir, "config"), headRoot: root };
37073
+ const gitdir = isAbsolute(target) ? target : join18(root, target);
37074
+ if (existsSync9(join18(gitdir, "config"))) {
37075
+ return { configPath: join18(gitdir, "config"), headRoot: root };
36400
37076
  }
36401
- const commonRaw = safeRead(join16(gitdir, "commondir"))?.trim();
37077
+ const commonRaw = safeRead(join18(gitdir, "commondir"))?.trim();
36402
37078
  if (!commonRaw) return void 0;
36403
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join16(gitdir, commonRaw);
37079
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join18(gitdir, commonRaw);
36404
37080
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
36405
- return { configPath: join16(commonGitDir, "config"), headRoot };
37081
+ return { configPath: join18(commonGitDir, "config"), headRoot };
36406
37082
  }
36407
37083
  function safeRead(path) {
36408
37084
  try {
36409
- return readFileSync9(path, "utf8");
37085
+ return readFileSync11(path, "utf8");
36410
37086
  } catch {
36411
37087
  return void 0;
36412
37088
  }
@@ -36950,8 +37626,8 @@ function createGuardedScanner(partition, gateway, opts) {
36950
37626
  }
36951
37627
 
36952
37628
  // ../../packages/plugin-sdk/src/host-floor.ts
36953
- import { readFileSync as readFileSync12 } from "fs";
36954
- import { join as join19 } from "path";
37629
+ import { readFileSync as readFileSync14 } from "fs";
37630
+ import { join as join21 } from "path";
36955
37631
 
36956
37632
  // ../../packages/plugin-sdk/src/model-governance.ts
36957
37633
  import {
@@ -36959,11 +37635,11 @@ import {
36959
37635
  fstatSync,
36960
37636
  mkdirSync as mkdirSync2,
36961
37637
  openSync as openSync2,
36962
- readFileSync as readFileSync11,
37638
+ readFileSync as readFileSync13,
36963
37639
  readSync,
36964
37640
  writeFileSync as writeFileSync5
36965
37641
  } from "fs";
36966
- import { join as join18 } from "path";
37642
+ import { join as join20 } from "path";
36967
37643
  var TAIL_BYTES = 256 * 1024;
36968
37644
 
36969
37645
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -36986,15 +37662,15 @@ var HOST_FLOORS = {
36986
37662
 
36987
37663
  // ../../packages/plugin-sdk/src/ignore-layers.ts
36988
37664
  var import_ignore = __toESM(require_ignore(), 1);
36989
- import { readFileSync as readFileSync13 } from "fs";
36990
- import { join as join20 } from "path";
37665
+ import { readFileSync as readFileSync15 } from "fs";
37666
+ import { join as join22 } from "path";
36991
37667
 
36992
37668
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
36993
37669
  import { arch, hostname as hostname4, platform, release } from "os";
36994
37670
 
36995
37671
  // ../../packages/plugin-sdk/src/nudge.ts
36996
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync14, writeFileSync as writeFileSync6 } from "fs";
36997
- import { join as join21 } from "path";
37672
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync16, writeFileSync as writeFileSync6 } from "fs";
37673
+ import { join as join23 } from "path";
36998
37674
 
36999
37675
  // ../../packages/plugin-sdk/src/paths.ts
37000
37676
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -37037,7 +37713,7 @@ function createPolicyResolver(bundle) {
37037
37713
 
37038
37714
  // ../../packages/plugin-sdk/src/project-files.ts
37039
37715
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
37040
- import { basename as basename5, join as join22 } from "path";
37716
+ import { basename as basename5, join as join24 } from "path";
37041
37717
 
37042
37718
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
37043
37719
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -37112,7 +37788,7 @@ function createPluginRuntime(gateway, settings, opts) {
37112
37788
  bundlesPacked = true;
37113
37789
  }
37114
37790
  const policyMode = settings.policy;
37115
- const redactFallback = settings.redactFallback;
37791
+ let redactFallback = settings.redactFallback;
37116
37792
  const dataDir2 = opts?.dataDir;
37117
37793
  let rules = [];
37118
37794
  let scanner;
@@ -37156,6 +37832,7 @@ function createPluginRuntime(gateway, settings, opts) {
37156
37832
  rules = [...verified, ...unverified];
37157
37833
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
37158
37834
  bundleExceptions = bundle.exceptions ?? [];
37835
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
37159
37836
  initialized = true;
37160
37837
  }
37161
37838
  let cachedKey;
@@ -37194,11 +37871,13 @@ function createPluginRuntime(gateway, settings, opts) {
37194
37871
  function decide(findings, text, excepted, rewritable = true) {
37195
37872
  if (findings.length === 0) return { action: "log", text, findings: [] };
37196
37873
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
37874
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
37875
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
37197
37876
  let worst = "log";
37198
37877
  for (const finding of findings) {
37199
37878
  worst = strongerAction(worst, actionFor(finding));
37200
37879
  }
37201
- if (worst === "block") return { action: "block", text: null, findings };
37880
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
37202
37881
  if (worst === "redact") {
37203
37882
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
37204
37883
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -37208,9 +37887,13 @@ function createPluginRuntime(gateway, settings, opts) {
37208
37887
  findings,
37209
37888
  enforcedFindings: redactFindings,
37210
37889
  reversibleFindings
37890
+ // No `degraded` here, and it is not an omission: `rewritable` is per
37891
+ // CAPTURE, so on an unrewritable field every redact has already become
37892
+ // the fallback and this branch is unreachable. Spreading it would read
37893
+ // as a case that can happen.
37211
37894
  };
37212
37895
  }
37213
- return { action: worst, text, findings };
37896
+ return { action: worst, text, findings, ...degraded };
37214
37897
  }
37215
37898
  function fingerprintOf(key, finding, cache) {
37216
37899
  let fp = cache.get(finding);
@@ -37339,8 +38022,8 @@ function createPluginRuntime(gateway, settings, opts) {
37339
38022
  };
37340
38023
  }
37341
38024
  }
37342
- async function processText(text, context) {
37343
- return (await evaluate(text, context, {})).decision;
38025
+ async function processText(text, context, opts2 = {}) {
38026
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
37344
38027
  }
37345
38028
  async function capture(input2, opts2 = {}) {
37346
38029
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -37363,10 +38046,12 @@ function createPluginRuntime(gateway, settings, opts) {
37363
38046
  );
37364
38047
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
37365
38048
  const inspectionMs = elapsedMs(timingStartedAt);
37366
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
38049
+ const redactDegradedTo = decision.redactDegradedTo;
38050
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
37367
38051
  ...input2.metadata,
37368
38052
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
37369
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
38053
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
38054
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
37370
38055
  } : input2.metadata;
37371
38056
  const event = buildIngestEvent({
37372
38057
  kind: input2.kind,
@@ -37438,7 +38123,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
37438
38123
 
37439
38124
  // ../../packages/plugin-sdk/src/throttle.ts
37440
38125
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
37441
- import { join as join23 } from "path";
38126
+ import { join as join25 } from "path";
37442
38127
 
37443
38128
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
37444
38129
  function isInvalidRequest(err) {
@@ -37454,31 +38139,12 @@ function isServerRejection(err) {
37454
38139
  var FORWARD_BUDGET_MS = 1500;
37455
38140
  var DECISION_PATH_BUDGET_MS = 800;
37456
38141
  var BREAKER_FAILURE_THRESHOLD = 3;
37457
- var BREAKER_COOLDOWN_MS = 3e4;
37458
38142
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
37459
- var FAILURES = /* @__PURE__ */ new Set([
37460
- "unauthorized",
37461
- "forbidden",
37462
- "unreachable"
37463
- ]);
37464
38143
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
37465
38144
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
37466
- function parseBreakerState(raw, nowMs) {
37467
- try {
37468
- const parsed2 = JSON.parse(raw);
37469
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
37470
- const record2 = parsed2;
37471
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
37472
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
37473
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
37474
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
37475
- } catch {
37476
- return null;
37477
- }
37478
- }
37479
38145
  function createForwardPolicy(deps) {
37480
38146
  const now = deps.now ?? (() => Date.now());
37481
- const file2 = join24(deps.dir, STATE_FILENAME);
38147
+ const file2 = join26(deps.dir, STATE_FILENAME);
37482
38148
  let state = null;
37483
38149
  let loading = null;
37484
38150
  async function readState() {
@@ -37488,7 +38154,7 @@ function createForwardPolicy(deps) {
37488
38154
  } catch {
37489
38155
  return { ...CLOSED };
37490
38156
  }
37491
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
38157
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
37492
38158
  }
37493
38159
  async function load() {
37494
38160
  if (state !== null) return state;
@@ -37534,7 +38200,7 @@ function createForwardPolicy(deps) {
37534
38200
  };
37535
38201
  const at = now();
37536
38202
  if (current.openedAtMs !== null) {
37537
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
38203
+ if (isForwardPaused(current, at)) {
37538
38204
  return { ok: false, reason: "breaker-open" };
37539
38205
  }
37540
38206
  await persist({
@@ -38071,7 +38737,18 @@ var AttachedDataGateway = class {
38071
38737
  // and the spread above would otherwise drop the field silently — which is
38072
38738
  // exactly what it did, leaving the whole control inert on every device
38073
38739
  // while every test around it stayed green.
38074
- prohibitedModels: cached2.prohibitedModels
38740
+ prohibitedModels: cached2.prohibitedModels,
38741
+ // NAMED for the same reason as the line above, and it is the same defect
38742
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
38743
+ // only the cache carries is dropped in silence. That is what left
38744
+ // `prohibitedModels` inert on every attached device with every test
38745
+ // around it green.
38746
+ //
38747
+ // Taken from the cache rather than merged here, because merging it needs
38748
+ // the device's own SETTING — which is not a bundle field and is not in
38749
+ // scope at this seam. The runtime does that merge, raise-only, where both
38750
+ // values are in hand (createPluginRuntime's ensureInitialized).
38751
+ redactFallback: cached2.redactFallback
38075
38752
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
38076
38753
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
38077
38754
  // it emits, so an 'authored' policy arriving from the control plane
@@ -38199,10 +38876,6 @@ function toolAuditEvent(input2) {
38199
38876
  };
38200
38877
  }
38201
38878
 
38202
- // ../../packages/plugin-runtime/src/attached/history-state.ts
38203
- import { readFileSync as readFileSync16 } from "fs";
38204
- import { join as join25 } from "path";
38205
-
38206
38879
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
38207
38880
  import { createHash as createHash6 } from "crypto";
38208
38881
  import { hostname as hostname5 } from "os";
@@ -38211,6 +38884,10 @@ import { hostname as hostname5 } from "os";
38211
38884
  var CORRELATION_ID = EventMetadata.shape.correlationId;
38212
38885
  var TRACE_ID = EventMetadata.shape.traceId;
38213
38886
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
38887
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
38888
+
38889
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
38890
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
38214
38891
 
38215
38892
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
38216
38893
  import { spawn } from "child_process";
@@ -38237,7 +38914,7 @@ function createPluginBlock(build, policyStore) {
38237
38914
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38238
38915
  import { randomUUID as randomUUID16 } from "crypto";
38239
38916
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
38240
- import { join as join26 } from "path";
38917
+ import { join as join27 } from "path";
38241
38918
 
38242
38919
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
38243
38920
  import { rename as rename2 } from "fs/promises";
@@ -38261,7 +38938,7 @@ async function publishByRename(tmp, file2, move = rename2) {
38261
38938
 
38262
38939
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38263
38940
  function createPolicyStore(dir = dataDir()) {
38264
- const file2 = join26(dir, "policy-cache.json");
38941
+ const file2 = join27(dir, "policy-cache.json");
38265
38942
  async function read() {
38266
38943
  try {
38267
38944
  const raw = await readFile2(file2, "utf8");
@@ -38492,11 +39169,11 @@ function readStorePosture(dbPath2) {
38492
39169
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
38493
39170
  import { randomUUID as randomUUID17 } from "crypto";
38494
39171
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
38495
- import { join as join27 } from "path";
39172
+ import { join as join28 } from "path";
38496
39173
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
38497
39174
  function createPostureStore(dir = settingsDir(), legacyDir) {
38498
- const file2 = join27(dir, "posture-state.json");
38499
- const legacyFile = legacyDir === void 0 ? null : join27(legacyDir, "posture-state.json");
39175
+ const file2 = join28(dir, "posture-state.json");
39176
+ const legacyFile = legacyDir === void 0 ? null : join28(legacyDir, "posture-state.json");
38500
39177
  async function persist(state) {
38501
39178
  await ensureDataDir(dir);
38502
39179
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -38565,7 +39242,7 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
38565
39242
 
38566
39243
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
38567
39244
  import { readFileSync as readFileSync18 } from "fs";
38568
- import { join as join28 } from "path";
39245
+ import { join as join29 } from "path";
38569
39246
 
38570
39247
  // ../../packages/plugin-runtime/src/attached/status.ts
38571
39248
  var REFUSAL_LINES = {
@@ -38586,6 +39263,14 @@ import { spawn as spawn2 } from "child_process";
38586
39263
  import { fileURLToPath as fileURLToPath3 } from "url";
38587
39264
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
38588
39265
 
39266
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
39267
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
39268
+
39269
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
39270
+ import { spawn as spawn3 } from "child_process";
39271
+ import { fileURLToPath as fileURLToPath4 } from "url";
39272
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
39273
+
38589
39274
  // ../../packages/plugin-runtime/src/attached/factory.ts
38590
39275
  import { hostname as hostname6 } from "os";
38591
39276
 
@@ -39198,7 +39883,7 @@ function baseMetadata(input2) {
39198
39883
 
39199
39884
  // src/hooks/store-health.ts
39200
39885
  import { mkdirSync as mkdirSync5, readFileSync as readFileSync19, writeFileSync as writeFileSync8 } from "fs";
39201
- import { dirname as dirname6, join as join29 } from "path";
39886
+ import { dirname as dirname6, join as join30 } from "path";
39202
39887
  var STORE_REDIRECT_MARKER = "store-redirect-last-session";
39203
39888
  function markerDirs(dataDir2) {
39204
39889
  return [dataDir2, dirname6(dataDir2)];
@@ -39206,7 +39891,7 @@ function markerDirs(dataDir2) {
39206
39891
  function alreadyClaimed(dirs, marker, sessionId) {
39207
39892
  return dirs.some((dir) => {
39208
39893
  try {
39209
- return readFileSync19(join29(dir, marker), "utf8") === sessionId;
39894
+ return readFileSync19(join30(dir, marker), "utf8") === sessionId;
39210
39895
  } catch {
39211
39896
  return false;
39212
39897
  }
@@ -39216,7 +39901,7 @@ function recordClaim(dirs, marker, sessionId) {
39216
39901
  for (const dir of dirs) {
39217
39902
  try {
39218
39903
  mkdirSync5(dir, { recursive: true, mode: DATA_DIR_MODE });
39219
- writeFileSync8(join29(dir, marker), sessionId, { mode: DATA_FILE_MODE });
39904
+ writeFileSync8(join30(dir, marker), sessionId, { mode: DATA_FILE_MODE });
39220
39905
  return;
39221
39906
  } catch {
39222
39907
  }