@akasecurity/ai-tc-claude-code 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 SLASH = "/";
53
+ var SLASH2 = "/";
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(SLASH).filter(Boolean);
425
+ const slices = path.split(SLASH2).filter(Boolean);
426
426
  slices.pop();
427
427
  if (slices.length) {
428
428
  const parent = this._t(
429
- slices.join(SLASH) + SLASH,
429
+ slices.join(SLASH2) + SLASH2,
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(SLASH).filter(Boolean);
445
+ slices = path.split(SLASH2).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(SLASH) + SLASH,
452
+ slices.join(SLASH2) + SLASH2,
453
453
  cache,
454
454
  checkUnignored,
455
455
  slices
@@ -493,7 +493,7 @@ var require_ignore = __commonJS({
493
493
 
494
494
  // ../../packages/plugin-sdk/src/config.ts
495
495
  import { existsSync as existsSync8 } from "fs";
496
- import { join as join14 } from "path";
496
+ import { join as join16 } from "path";
497
497
 
498
498
  // ../../packages/persistence/src/attached-derived.ts
499
499
  import { rmSync } from "fs";
@@ -504,6 +504,14 @@ var POLICY_CACHE_FILENAME = "policy-cache.json";
504
504
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
505
505
  import { join as join2 } from "path";
506
506
 
507
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
508
+ var DEFERRED_MIGRATION_TAGS = [
509
+ "0031_audit_capture_by_time_index",
510
+ "0032_audit_capture_by_id_index",
511
+ "0033_audit_capture_location_index",
512
+ "0034_findings_read_indexes"
513
+ ];
514
+
507
515
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
508
516
  var SQLITE_MIGRATIONS = [
509
517
  {
@@ -621,6 +629,30 @@ var SQLITE_MIGRATIONS = [
621
629
  {
622
630
  tag: "0028_activity_session_probe_indexes",
623
631
  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"
632
+ },
633
+ {
634
+ tag: "0029_audit_capture_rollup_index",
635
+ 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');"
636
+ },
637
+ {
638
+ tag: "0030_audit_content_expiry",
639
+ 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;"
640
+ },
641
+ {
642
+ tag: "0031_audit_capture_by_time_index",
643
+ 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');"
644
+ },
645
+ {
646
+ tag: "0032_audit_capture_by_id_index",
647
+ 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');"
648
+ },
649
+ {
650
+ tag: "0033_audit_capture_location_index",
651
+ 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');"
652
+ },
653
+ {
654
+ tag: "0034_findings_read_indexes",
655
+ 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`);"
624
656
  }
625
657
  ];
626
658
 
@@ -20668,6 +20700,15 @@ var FindingCategory = external_exports.enum([
20668
20700
  ]).meta({ id: "FindingCategory" });
20669
20701
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20670
20702
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20703
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20704
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20705
+ var FindingDelivery = external_exports.object({
20706
+ state: FindingDeliveryState,
20707
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20708
+ at: external_exports.iso.datetime().optional(),
20709
+ // Only on `not_sent`, and only when a known reason was recorded.
20710
+ reason: SyncFailureReason.optional()
20711
+ }).meta({ id: "FindingDelivery" });
20671
20712
  var ResolutionMethod = external_exports.enum([
20672
20713
  "enforced-in-flight",
20673
20714
  "fixed-at-source",
@@ -20724,7 +20765,10 @@ var FindingInstance = external_exports.object({
20724
20765
  // The session that event belongs to, when it has one — the seam a
20725
20766
  // per-instance "view session" link needs. Absent for events captured
20726
20767
  // outside a session.
20727
- sessionId: external_exports.string().optional()
20768
+ sessionId: external_exports.string().optional(),
20769
+ // The delivery state of the event above (see FindingDelivery). Optional so
20770
+ // readers that do not project it stay valid.
20771
+ delivery: FindingDelivery.optional()
20728
20772
  }).meta({ id: "FindingInstance" });
20729
20773
  var FindingGroup = external_exports.object({
20730
20774
  id: external_exports.string(),
@@ -20776,7 +20820,10 @@ var FindingFacets = external_exports.object({
20776
20820
  // Host tool (attributes.tool_name). Present only on the instance-level
20777
20821
  // reads, which can filter by it; the type-level read omits the dimension
20778
20822
  // because a group spans tools.
20779
- tool: external_exports.array(FindingFacetItem).optional()
20823
+ tool: external_exports.array(FindingFacetItem).optional(),
20824
+ // Delivery states (FindingDeliveryState). Present only on the
20825
+ // instance-level reads, like `tool`.
20826
+ deployment: external_exports.array(FindingFacetItem).optional()
20780
20827
  }).meta({ id: "FindingFacets" });
20781
20828
  var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20782
20829
  id: "FindingTypeSummary"
@@ -20887,6 +20934,8 @@ var ListFindingInstancesQuery = external_exports.object({
20887
20934
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20888
20935
  // where the free-text `q` can only match the rendered "via Bash" label.
20889
20936
  tool: external_exports.array(external_exports.string()).optional(),
20937
+ // The delivery state of each finding's event (see FindingDelivery).
20938
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20890
20939
  // Exact repository / file-path matches, for the drill-down out of the
20891
20940
  // locations view. A row whose event carries no repo/file matches neither.
20892
20941
  repo: external_exports.string().optional(),
@@ -20907,6 +20956,10 @@ var ListFindingInstancesResponse = external_exports.object({
20907
20956
  items: external_exports.array(FindingInstanceDetail),
20908
20957
  nextCursor: external_exports.string().nullable()
20909
20958
  }).meta({ id: "ListFindingInstancesResponse" });
20959
+ var ListFindingInstancesPage = external_exports.object({
20960
+ items: external_exports.array(FindingInstanceDetail),
20961
+ nextCursor: external_exports.string().nullable()
20962
+ }).meta({ id: "ListFindingInstancesPage" });
20910
20963
  var FindingLocationSummary = external_exports.object({
20911
20964
  // Opaque, stable, minted from the pair by encodeLocationId. It exists
20912
20965
  // because a location's identity is two values and a URL param carries one:
@@ -20949,6 +21002,8 @@ var ListFindingLocationsQuery = external_exports.object({
20949
21002
  // instances that match, and folds its status from those.
20950
21003
  status: external_exports.array(FindingStatus).optional(),
20951
21004
  tool: external_exports.array(external_exports.string()).optional(),
21005
+ // The delivery state of each finding's event (see FindingDelivery).
21006
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20952
21007
  q: external_exports.string().optional(),
20953
21008
  sessionId: external_exports.string().optional(),
20954
21009
  from: external_exports.iso.datetime().optional(),
@@ -21151,6 +21206,10 @@ var CaptureAttributes = external_exports.object({
21151
21206
  // to 'allow' — the enforcement audit trail's link back to the grant that
21152
21207
  // authorized the bypass.
21153
21208
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21209
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21210
+ // join back to the `llm_call` leaf for the same assistant turn.
21211
+ message_id: external_exports.string().optional(),
21212
+ conversation_id: external_exports.string().optional(),
21154
21213
  // Whole milliseconds this capture's inspection blocked its caller — the
21155
21214
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21156
21215
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21159,7 +21218,19 @@ var CaptureAttributes = external_exports.object({
21159
21218
  // inline json_extract and is not itself an optimization.
21160
21219
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21161
21220
  // before the measurement shipped — never present as a placeholder 0.
21162
- inspection_ms: external_exports.number().int().nonnegative().optional()
21221
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21222
+ // What a `redact` this capture could not carry out became instead (see
21223
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21224
+ // degrade actually happened, so absence is the ordinary case rather than a
21225
+ // reader having to distinguish it from a zero.
21226
+ //
21227
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21228
+ // so on a multi-finding row this does not say which finding degraded, and
21229
+ // its presence does not mean the fallback decided the capture's action. A
21230
+ // capture denied by another finding's own Block policy carries `block`
21231
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21232
+ // repeated rather than referenced because a store reader opens this file.
21233
+ redact_degraded_to: ActionTaken.optional()
21163
21234
  }).catchall(external_exports.unknown());
21164
21235
  var ToolCallInspection = external_exports.object({
21165
21236
  ruleId: external_exports.string().min(1),
@@ -21358,7 +21429,17 @@ var AuditEvent = external_exports.object({
21358
21429
  /** `share` to a first-party/internal destination. */
21359
21430
  internal: external_exports.boolean(),
21360
21431
  /** Event needs review (e.g. unverified egress). */
21361
- flagged: external_exports.boolean()
21432
+ flagged: external_exports.boolean(),
21433
+ /**
21434
+ * The body this event's `title` is drawn from was cleared by local body
21435
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21436
+ *
21437
+ * A separate flag rather than a sentinel written into `title`: the title is
21438
+ * rendered text, and a store-layer module that invented display copy for it
21439
+ * would be choosing words the view is supposed to choose. Additive and
21440
+ * defaulted, so an older producer still validates.
21441
+ */
21442
+ bodyExpired: external_exports.boolean().default(false)
21362
21443
  }).meta({ id: "ActivityAuditEvent" });
21363
21444
  var ActivitySessionSummary = external_exports.object({
21364
21445
  id: external_exports.string(),
@@ -22698,6 +22779,12 @@ var EventMetadata = external_exports.object({
22698
22779
  // to 'allow' — the enforcement audit trail's link back to the grant that
22699
22780
  // authorized the bypass. Absent on captures where no exception applied.
22700
22781
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22782
+ // The assistant message this capture belongs to, and the conversation it sits
22783
+ // in — set by the browser extension's network capture so a stored `response`
22784
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22785
+ // on every other capture path, which has no such id.
22786
+ messageId: external_exports.string().optional(),
22787
+ conversationId: external_exports.string().optional(),
22701
22788
  // How long THIS capture's inspection blocked its caller, in whole
22702
22789
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22703
22790
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22710,7 +22797,37 @@ var EventMetadata = external_exports.object({
22710
22797
  // Absent is also what every pre-measurement client writes, and what a
22711
22798
  // clock failure degrades to — a reader must treat absence as "not measured"
22712
22799
  // and never as a zero, which would read as "inspection is free".
22713
- inspectionMs: external_exports.number().int().nonnegative().optional()
22800
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22801
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22802
+ // workspace's `redactFallback`, applied because the field could not be
22803
+ // masked in place (a shell command, a URL, or any argument on a host whose
22804
+ // hook contract offers no rewrite channel).
22805
+ //
22806
+ // It exists because the action alone cannot say why. A finding recorded as
22807
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22808
+ // assigned Redact on a field that could not take one — and those are
22809
+ // different facts about the same row: the first is a policy the user chose,
22810
+ // the second is a masking the host could not perform. Absent means no
22811
+ // degrade happened, which is every ordinary capture.
22812
+ //
22813
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22814
+ // is the CAPTURE while `actionTaken` is per FINDING:
22815
+ //
22816
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22817
+ // `redact` alongside a finding ASSIGNED the same action stores both
22818
+ // identically and one reason for the pair; attributing it to both
22819
+ // describes the assigned one wrongly, and to neither loses the degrade.
22820
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22821
+ // became, not the reason the capture ended as it did — a capture denied
22822
+ // by some other finding's own Block policy still carries `block` here,
22823
+ // and clearing the workspace's fallback would not have let it through.
22824
+ // Gate on the value against what a fallback can produce; never read the
22825
+ // field's presence as "this was the fallback's doing".
22826
+ //
22827
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22828
+ // Closing either means moving the reason onto the finding row, which
22829
+ // already carries its own action.
22830
+ redactDegradedTo: ActionTaken.optional()
22714
22831
  }).meta({ id: "EventMetadata" });
22715
22832
  var Event = external_exports.object({
22716
22833
  id: external_exports.guid(),
@@ -22820,7 +22937,32 @@ var RotateKeyInput = external_exports.object({
22820
22937
  confirmation: external_exports.string()
22821
22938
  });
22822
22939
 
22940
+ // ../../packages/schema/src/zod/finding-delivery.ts
22941
+ var KNOWN_REASONS = SyncFailureReason.options;
22942
+ function knownReason(value) {
22943
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22944
+ }
22945
+ function deriveFindingDelivery(row) {
22946
+ if (row.kind === "code_change") return { state: "local_scan" };
22947
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22948
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22949
+ }
22950
+ if (row.syncedAt !== null) {
22951
+ const reason = knownReason(row.syncFailure);
22952
+ return {
22953
+ state: "not_sent",
22954
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22955
+ ...reason === void 0 ? {} : { reason }
22956
+ };
22957
+ }
22958
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22959
+ return { state: "never_offered" };
22960
+ }
22961
+
22823
22962
  // ../../packages/schema/src/zod/findings-group-build.ts
22963
+ function lookupOwn(map2, key) {
22964
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22965
+ }
22824
22966
  function toApiAction(dbVal) {
22825
22967
  const map2 = {
22826
22968
  log: "monitored",
@@ -22829,7 +22971,7 @@ function toApiAction(dbVal) {
22829
22971
  warn: "warned",
22830
22972
  allow: "allowed"
22831
22973
  };
22832
- return map2[dbVal] ?? "allowed";
22974
+ return lookupOwn(map2, dbVal) ?? "allowed";
22833
22975
  }
22834
22976
  function toApiCategory(dbVal) {
22835
22977
  if (dbVal === "code_context") return "source_code";
@@ -22837,13 +22979,18 @@ function toApiCategory(dbVal) {
22837
22979
  return parsed.success ? parsed.data : "custom";
22838
22980
  }
22839
22981
  function toApiProvider(sourceTool) {
22840
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22982
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22841
22983
  }
22842
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22984
+ var FINDING_STATUS_PRECEDENCE = [
22985
+ "open",
22986
+ "handled",
22987
+ "dismissed",
22988
+ "resolved"
22989
+ ];
22843
22990
  function foldGroupStatus(instanceStatuses) {
22844
22991
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22845
22992
  if (statuses.size === 0) return void 0;
22846
- for (const candidate of STATUS_PRECEDENCE) {
22993
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22847
22994
  if (statuses.has(candidate)) return candidate;
22848
22995
  }
22849
22996
  return void 0;
@@ -22950,11 +23097,16 @@ function applyFindingFilters(types, opts) {
22950
23097
  }
22951
23098
  return filtered;
22952
23099
  }
22953
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22954
- var SEVERITY_RANK = SEVERITY_ORDER;
23100
+ function rankByOrder(members2) {
23101
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23102
+ }
23103
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23104
+ function severityRank(severity) {
23105
+ return lookupOwn(SEVERITY_RANK, severity);
23106
+ }
22955
23107
  function compareFindingGroupOrder(a, b) {
22956
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22957
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23108
+ const rankA = severityRank(a.severity) ?? -1;
23109
+ const rankB = severityRank(b.severity) ?? -1;
22958
23110
  const severityDiff = rankA - rankB;
22959
23111
  if (severityDiff !== 0) return severityDiff;
22960
23112
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
@@ -23029,6 +23181,20 @@ function computeFindingFacets(allTypes, opts) {
23029
23181
  }
23030
23182
 
23031
23183
  // ../../packages/schema/src/zod/findings-flat-build.ts
23184
+ function compareCodePoints(a, b) {
23185
+ const aIter = a[Symbol.iterator]();
23186
+ const bIter = b[Symbol.iterator]();
23187
+ for (; ; ) {
23188
+ const aNext = aIter.next();
23189
+ const bNext = bIter.next();
23190
+ if (aNext.done && bNext.done) return 0;
23191
+ if (aNext.done) return -1;
23192
+ if (bNext.done) return 1;
23193
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23194
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23195
+ if (aPoint !== bPoint) return aPoint - bPoint;
23196
+ }
23197
+ }
23032
23198
  function rowHaystack(row) {
23033
23199
  return [
23034
23200
  row.ruleId,
@@ -23053,6 +23219,8 @@ function matchesDimension(row, opts, dimension) {
23053
23219
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23054
23220
  case "statuses":
23055
23221
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23222
+ case "deliveries":
23223
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23056
23224
  case "tools":
23057
23225
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23058
23226
  // An EMPTY value is a real filter here, not an absent one. The location
@@ -23079,6 +23247,7 @@ var DIMENSIONS = [
23079
23247
  "providers",
23080
23248
  "actions",
23081
23249
  "statuses",
23250
+ "deliveries",
23082
23251
  "tools",
23083
23252
  "repo",
23084
23253
  "file",
@@ -23092,10 +23261,19 @@ function matchesInstanceFilters(row, opts, except) {
23092
23261
  return true;
23093
23262
  }
23094
23263
  function toItems(counts) {
23095
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23264
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23265
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23266
+ // NFD spelling of the same text) as equal, so a count tie between
23267
+ // them would otherwise be ordered by whichever the Map iteration
23268
+ // produced. compareCodePoints breaks that tie deterministically, which
23269
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23270
+ // which it need not: foldFacetTuples runs this same sort over grouped
23271
+ // tuples, so both paths order facets identically by construction.
23272
+ compareCodePoints(a.value, b.value)
23273
+ );
23096
23274
  }
23097
- function bump(counts, value) {
23098
- counts.set(value, (counts.get(value) ?? 0) + 1);
23275
+ function bump(counts, value, by = 1) {
23276
+ counts.set(value, (counts.get(value) ?? 0) + by);
23099
23277
  }
23100
23278
  function createInstanceFacetAccumulator(opts) {
23101
23279
  const severity = /* @__PURE__ */ new Map();
@@ -23104,6 +23282,7 @@ function createInstanceFacetAccumulator(opts) {
23104
23282
  const action = /* @__PURE__ */ new Map();
23105
23283
  const status = /* @__PURE__ */ new Map();
23106
23284
  const tool = /* @__PURE__ */ new Map();
23285
+ const deployment = /* @__PURE__ */ new Map();
23107
23286
  return {
23108
23287
  add(row) {
23109
23288
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23118,6 +23297,9 @@ function createInstanceFacetAccumulator(opts) {
23118
23297
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23119
23298
  bump(tool, row.toolName);
23120
23299
  }
23300
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23301
+ bump(deployment, row.delivery.state);
23302
+ }
23121
23303
  },
23122
23304
  facets: () => ({
23123
23305
  severity: toItems(severity),
@@ -23125,7 +23307,8 @@ function createInstanceFacetAccumulator(opts) {
23125
23307
  provider: toItems(provider),
23126
23308
  action: toItems(action),
23127
23309
  status: toItems(status),
23128
- tool: toItems(tool)
23310
+ tool: toItems(tool),
23311
+ deployment: toItems(deployment)
23129
23312
  })
23130
23313
  };
23131
23314
  }
@@ -23139,6 +23322,7 @@ function toInstanceDetail(row) {
23139
23322
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23140
23323
  eventId: row.eventId,
23141
23324
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23325
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23142
23326
  ...row.user === void 0 ? {} : { user: row.user },
23143
23327
  action: toApiAction(row.actionTaken),
23144
23328
  detectedAt: row.occurredAt,
@@ -23153,12 +23337,6 @@ function toInstanceDetail(row) {
23153
23337
  policy: { id: `category:${category}`, name: category }
23154
23338
  };
23155
23339
  }
23156
- var SEVERITY_ORDER2 = {
23157
- critical: 0,
23158
- high: 1,
23159
- medium: 2,
23160
- low: 3
23161
- };
23162
23340
  function newLocationAccumulator() {
23163
23341
  return {
23164
23342
  instanceCount: 0,
@@ -23173,7 +23351,7 @@ function newLocationAccumulator() {
23173
23351
  }
23174
23352
  function addToLocation(acc, row) {
23175
23353
  acc.instanceCount += 1;
23176
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23354
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23177
23355
  if (rank < acc.maxSeverityRank) {
23178
23356
  acc.maxSeverityRank = rank;
23179
23357
  acc.maxSeverity = row.severity;
@@ -23183,15 +23361,15 @@ function addToLocation(acc, row) {
23183
23361
  acc.ruleIds.add(row.ruleId);
23184
23362
  }
23185
23363
  function compareLocationOrder(a, b) {
23186
- const rankA = SEVERITY_ORDER2[a.maxSeverity] ?? -1;
23187
- const rankB = SEVERITY_ORDER2[b.maxSeverity] ?? -1;
23364
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23365
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23188
23366
  if (rankA !== rankB) return rankA - rankB;
23189
23367
  if (a.latestDetectedAt !== b.latestDetectedAt) {
23190
23368
  return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23191
23369
  }
23192
- if (a.repo !== b.repo) return a.repo < b.repo ? -1 : 1;
23193
- if (a.file !== b.file) return a.file < b.file ? -1 : 1;
23194
- return 0;
23370
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23371
+ if (repoDiff !== 0) return repoDiff;
23372
+ return compareCodePoints(a.file, b.file);
23195
23373
  }
23196
23374
  function encodeLocationId(repo, file2) {
23197
23375
  return `${encodePart(repo)}/${encodePart(file2)}`;
@@ -23266,6 +23444,11 @@ var Policy = external_exports.object({
23266
23444
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23267
23445
  provenance: PolicyProvenance.optional()
23268
23446
  }).meta({ id: "Policy" });
23447
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23448
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23449
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23450
+ id: "RedactFallback"
23451
+ });
23269
23452
  var PolicyBundle = external_exports.object({
23270
23453
  version: external_exports.string(),
23271
23454
  policies: external_exports.array(Policy),
@@ -23313,6 +23496,16 @@ var PolicyBundle = external_exports.object({
23313
23496
  // control plane), so no name resolution stands between the decision and the
23314
23497
  // comparison.
23315
23498
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23499
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23500
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23501
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23502
+ // a control plane can tighten a machine and never loosen one — the same
23503
+ // direction `mergeRaiseOnly` enforces for policies.
23504
+ //
23505
+ // Optional so an older backend, and an older on-disk cache, still parses;
23506
+ // absent leaves the device's own setting in force, which is the behaviour
23507
+ // that predates the field and the safe direction to default.
23508
+ redactFallback: RedactFallback.optional(),
23316
23509
  customKeywords: external_exports.array(external_exports.string()),
23317
23510
  fetchedAt: external_exports.iso.datetime()
23318
23511
  }).meta({ id: "PolicyBundle" });
@@ -23342,11 +23535,6 @@ function severityFloorPolicy(category) {
23342
23535
  const peak = CATEGORY_PEAK_SEVERITY[category];
23343
23536
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23344
23537
  }
23345
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23346
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23347
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23348
- id: "RedactFallback"
23349
- });
23350
23538
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23351
23539
  var BUILTIN_POLICY_SPECS = {
23352
23540
  monitor: {
@@ -23650,7 +23838,7 @@ function isVaultConsentValid(consent) {
23650
23838
  }
23651
23839
 
23652
23840
  // ../../packages/schema/src/zod/local.ts
23653
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23841
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23654
23842
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23655
23843
  var RunMode = external_exports.enum(["standalone", "attached"]);
23656
23844
  var ControlPlaneConnection = external_exports.object({
@@ -23670,6 +23858,15 @@ var HistorySyncConsent = external_exports.object({
23670
23858
  payloadVersion: external_exports.number().int().positive(),
23671
23859
  endpoint: external_exports.string()
23672
23860
  });
23861
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23862
+ var BodyRetention = external_exports.object({
23863
+ enabled: external_exports.boolean().default(false),
23864
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23865
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23866
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23867
+ // candidate set that is already bounded by "delivered, or never owed".
23868
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23869
+ }).meta({ id: "BodyRetention" });
23673
23870
  var WorkspaceSettings = external_exports.object({
23674
23871
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23675
23872
  runMode: RunMode.default("standalone"),
@@ -23718,7 +23915,13 @@ var WorkspaceSettings = external_exports.object({
23718
23915
  // carry prompt/reply/tool-output text in `content`; the key name predates
23719
23916
  // both widenings. Absent until granted, and a grant for a different endpoint
23720
23917
  // or an older payload no longer counts.
23721
- historySyncConsent: HistorySyncConsent.optional()
23918
+ historySyncConsent: HistorySyncConsent.optional(),
23919
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23920
+ // body never removes the row or its findings.
23921
+ bodyRetention: BodyRetention.default({
23922
+ enabled: false,
23923
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23924
+ })
23722
23925
  });
23723
23926
  function defaultWorkspaceSettings() {
23724
23927
  return WorkspaceSettings.parse({});
@@ -23813,12 +24016,15 @@ function toCaptureAttributes(event) {
23813
24016
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23814
24017
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23815
24018
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24019
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23816
24020
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23817
24021
  // has ever populated either), but every legacy metadata key still rides
23818
24022
  // the bag rather than being silently dropped — CaptureAttributes'
23819
24023
  // `.catchall(z.unknown())` carries the long tail.
23820
24024
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23821
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24025
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24026
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24027
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23822
24028
  };
23823
24029
  }
23824
24030
  function captureDefinitionVersion(finding) {
@@ -23846,13 +24052,22 @@ var ManagedSettingKey = external_exports.enum([
23846
24052
  "vaultInlineReveal",
23847
24053
  "modelJudgeConsent",
23848
24054
  "dataSharesInPlace",
23849
- "redactFallback"
24055
+ "redactFallback",
24056
+ // Pins the toggle and the day count together — see BodyRetention on why the
24057
+ // two are one unit. An administrator mandating a window wants the count
24058
+ // enforced with it, not one a user can widen while the toggle stays on.
24059
+ "bodyRetention"
23850
24060
  ]).meta({ id: "ManagedSettingKey" });
23851
24061
  function isManagedSettingKey(value) {
23852
24062
  return ManagedSettingKey.safeParse(value).success;
23853
24063
  }
23854
24064
  var ManagedSettingsValues = external_exports.object({
23855
24065
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24066
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24067
+ // plain, non-strict objects: a key under either that this build does not know
24068
+ // is stripped and nothing reports it. The unknown-value split in
24069
+ // ManagedSettings below classifies top-level names only, so it stops at
24070
+ // these boundaries.
23856
24071
  controlPlane: external_exports.object({
23857
24072
  endpoint: external_exports.string().min(1),
23858
24073
  label: external_exports.string().min(1).optional()
@@ -23863,7 +24078,8 @@ var ManagedSettingsValues = external_exports.object({
23863
24078
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23864
24079
  modelJudgeConsent: external_exports.boolean().optional(),
23865
24080
  dataSharesInPlace: external_exports.boolean().optional(),
23866
- redactFallback: RedactFallback.optional()
24081
+ redactFallback: RedactFallback.optional(),
24082
+ bodyRetention: BodyRetention.optional()
23867
24083
  }).meta({ id: "ManagedSettingsValues" });
23868
24084
  var ManagedSettings = external_exports.object({
23869
24085
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23871,7 +24087,21 @@ var ManagedSettings = external_exports.object({
23871
24087
  // decision from a bug. Absent renders as a generic "your organization".
23872
24088
  organization: external_exports.string().min(1).optional(),
23873
24089
  // What the administrator pinned.
23874
- values: ManagedSettingsValues.default({}),
24090
+ //
24091
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24092
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24093
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24094
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24095
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24096
+ // exactly the file an administrator is most likely to write while a fleet
24097
+ // is mid-upgrade.
24098
+ //
24099
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24100
+ // file, which is the outcome the lock half already rejected — an older
24101
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24102
+ // value still fails, because the nested schema is re-run over the known
24103
+ // subset and its issues are re-raised on this parse.
24104
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23875
24105
  // Which of those the user may not change. A key here with no matching value
23876
24106
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23877
24107
  // the user may still override. The two are separable on purpose.
@@ -23884,17 +24114,31 @@ var ManagedSettings = external_exports.object({
23884
24114
  // the fleets most likely to carry a version skew. A name outside the enum
23885
24115
  // is still never HONOURED: the lockable set stays explicit above.
23886
24116
  lockedFields: external_exports.array(external_exports.string()).default([])
23887
- }).transform(({ lockedFields, ...rest }) => {
24117
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
23888
24118
  const known = [];
23889
24119
  const unknown2 = [];
23890
24120
  for (const name of lockedFields) {
23891
24121
  if (isManagedSettingKey(name)) known.push(name);
23892
24122
  else unknown2.push(name);
23893
24123
  }
24124
+ const knownValues = /* @__PURE__ */ Object.create(null);
24125
+ const unknownValues = [];
24126
+ for (const [name, value] of Object.entries(values)) {
24127
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24128
+ else unknownValues.push(name);
24129
+ }
24130
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24131
+ if (!pinned.success) {
24132
+ for (const issue2 of pinned.error.issues)
24133
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24134
+ return external_exports.NEVER;
24135
+ }
23894
24136
  return {
23895
24137
  ...rest,
24138
+ values: pinned.data,
23896
24139
  lockedFields: known,
23897
- ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {}
24140
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24141
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
23898
24142
  };
23899
24143
  }).meta({ id: "ManagedSettings" });
23900
24144
 
@@ -24158,7 +24402,23 @@ var SaveSettingsInput = external_exports.object({
24158
24402
  modelJudgeConsent: ModelJudgeConsentChoice,
24159
24403
  historySyncConsent: HistorySyncConsentChoice,
24160
24404
  vaultConsent: external_exports.string(),
24161
- vaultInlineReveal: external_exports.string()
24405
+ vaultInlineReveal: external_exports.string(),
24406
+ // Widened to `string` like its neighbours rather than typed as
24407
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24408
+ // the call site, so the domain check receives the type it was written for.
24409
+ //
24410
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24411
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24412
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24413
+ // trade against. The real cost runs the other way and is the part worth
24414
+ // knowing: a value this schema admits and the domain enum then rejects lands
24415
+ // on the action's shared refusal, which names NO field, where a shape
24416
+ // rejection reaches `malformedInput` and names the schema key.
24417
+ redactFallback: external_exports.string(),
24418
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24419
+ // `BodyRetention`'s and the action checks it there, so there is one place
24420
+ // that decides what a legal horizon is rather than two that can drift.
24421
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24162
24422
  });
24163
24423
  var AttachInput = external_exports.object({
24164
24424
  endpoint: external_exports.string(),
@@ -24330,6 +24590,52 @@ function reviewSeverityRank(reasons) {
24330
24590
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24331
24591
  }
24332
24592
 
24593
+ // ../../packages/schema/src/zod/web-capture.ts
24594
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24595
+ var WebUsage = external_exports.object({
24596
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24597
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24598
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24599
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24600
+ });
24601
+ var WebToolCall = external_exports.object({
24602
+ toolUseId: external_exports.string().min(1),
24603
+ toolName: external_exports.string().min(1),
24604
+ target: external_exports.string().optional(),
24605
+ isError: external_exports.boolean().optional(),
24606
+ inputSize: external_exports.number().int().nonnegative().optional(),
24607
+ outputSize: external_exports.number().int().nonnegative().optional()
24608
+ });
24609
+ var WebExchange = external_exports.object({
24610
+ messageId: external_exports.string().min(1),
24611
+ startedAt: external_exports.iso.datetime(),
24612
+ model: external_exports.string().optional(),
24613
+ usage: WebUsage.optional(),
24614
+ usageSource: WebUsageSource,
24615
+ stopReason: external_exports.string().optional(),
24616
+ conversationId: external_exports.string().optional(),
24617
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24618
+ toolCalls: external_exports.array(WebToolCall).default([]),
24619
+ // Absent when the adapter recovered no text. Capped by the caller at
24620
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24621
+ // short capture is never mistaken for a short reply.
24622
+ responseText: external_exports.string().optional(),
24623
+ truncated: external_exports.boolean().default(false)
24624
+ });
24625
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24626
+ var WebCaptureStatus = external_exports.object({
24627
+ patched: external_exports.boolean(),
24628
+ live: external_exports.boolean(),
24629
+ blind: external_exports.boolean(),
24630
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24631
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24632
+ parseFailures: external_exports.number().int().nonnegative(),
24633
+ unparsedBodies: external_exports.number().int().nonnegative(),
24634
+ // The adapter-declared JSON key paths that were absent from a real payload —
24635
+ // the earliest signal that a site's contract moved.
24636
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24637
+ });
24638
+
24333
24639
  // ../../packages/persistence/src/paths.ts
24334
24640
  import {
24335
24641
  chmodSync,
@@ -24599,6 +24905,22 @@ function discardStore(file2, backup) {
24599
24905
  }
24600
24906
  }
24601
24907
 
24908
+ // ../../packages/persistence/src/internal/sql-functions.ts
24909
+ var utf8 = new TextDecoder();
24910
+ function akaLower(value) {
24911
+ if (value === null) return null;
24912
+ if (typeof value === "string") return value.toLowerCase();
24913
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
24914
+ return utf8.decode(value).toLowerCase();
24915
+ }
24916
+ function registerSqlFunctions(db) {
24917
+ db.function(
24918
+ "aka_lower",
24919
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
24920
+ akaLower
24921
+ );
24922
+ }
24923
+
24602
24924
  // ../../packages/persistence/src/internal/sql-text.ts
24603
24925
  function escapeLikePattern(s) {
24604
24926
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24683,6 +25005,11 @@ function schemaObjectExists(db, kind, name) {
24683
25005
  function indexExists(db, name) {
24684
25006
  return schemaObjectExists(db, "index", name);
24685
25007
  }
25008
+ function indexColumns(db, name) {
25009
+ if (!indexExists(db, name)) return [];
25010
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25011
+ return columns.map((c) => c.name).filter((c) => c !== null);
25012
+ }
24686
25013
  function columnNames(db, table, opts) {
24687
25014
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24688
25015
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).all();
@@ -24744,178 +25071,820 @@ function mapRowsTolerant(rows, map2) {
24744
25071
  return out;
24745
25072
  }
24746
25073
 
24747
- // ../../packages/persistence/src/migrations.ts
24748
- function describeObject(object2) {
24749
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24750
- }
24751
- function splitStatements(sql) {
24752
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24753
- }
24754
- function createdIndexName(statement) {
24755
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24756
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25074
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25075
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25076
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25077
+
25078
+ // ../../packages/persistence/src/sync-failure.ts
25079
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25080
+ function syncFailureRejectCondition(column = "sync_failure") {
25081
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25082
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24757
25083
  }
24758
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24759
- function applyMigrations(db, file2) {
24760
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24761
- db.exec(
24762
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24763
- );
24764
- const applied = new Set(
24765
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24766
- );
24767
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24768
- const record2 = db.prepare(
24769
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24770
- );
24771
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24772
- if (applied.has(migration.tag)) continue;
24773
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24774
- const evidence = evidenceObjects(migration.sql);
24775
- const present = evidence.filter((o) => evidenceExists(db, o));
24776
- if (present.length > 0 && present.length < evidence.length) {
24777
- const missing = evidence.filter((o) => !present.includes(o));
24778
- 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.`;
24779
- akaWarn(message);
24780
- throw new Error(`[aka] ${message}`);
24781
- }
24782
- const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
24783
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24784
- const statements = splitStatements(migration.sql);
24785
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24786
- try {
24787
- withTransaction(
24788
- db,
24789
- () => {
24790
- for (const statement of statements) {
24791
- const indexName = createdIndexName(statement);
24792
- if (indexName === void 0) {
24793
- if (alreadyApplied) continue;
24794
- } else if (indexExists(db, indexName)) {
24795
- continue;
24796
- }
24797
- db.exec(statement);
24798
- }
24799
- if (wantsFkOff && !alreadyApplied) {
24800
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24801
- if (violations.length > 0) {
24802
- throw new Error(
24803
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24804
- );
24805
- }
24806
- }
24807
- record2.run(migration.tag, Date.now());
24808
- },
24809
- "IMMEDIATE"
24810
- );
24811
- } finally {
24812
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24813
- }
25084
+
25085
+ // ../../packages/persistence/src/repositories/history-sync.ts
25086
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25087
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25088
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25089
+ var COUNTED_EVENT_TYPES = [
25090
+ ...STRUCTURAL_EVENT_TYPES,
25091
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25092
+ ];
25093
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25094
+ var PARTITION_BUCKETS = `
25095
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25096
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25097
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25098
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25099
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25100
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25101
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25102
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25103
+ -- added later lands in no bucket and fails the sum assertion, instead
25104
+ -- of silently joining this one.
25105
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25106
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25107
+ THEN 1 ELSE 0 END) AS failed,
25108
+ COUNT(*) AS total`;
25109
+ var COUNTED_SCOPE = `
25110
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25111
+ AND (
25112
+ event_type IN (${TYPE_LIST})
25113
+ OR synced_at IS NOT NULL
25114
+ OR outbox_owed = 1
25115
+ )`;
25116
+ var SKIPPED = -1;
25117
+ var ROW_COLUMNS = `id,
25118
+ parent_id AS parentId,
25119
+ root_session_id AS rootSessionId,
25120
+ event_type AS eventType,
25121
+ host_id AS hostId,
25122
+ harness_id AS harnessId,
25123
+ source_project_id AS sourceProjectId,
25124
+ started_at AS startedAt,
25125
+ ended_at AS endedAt,
25126
+ severity,
25127
+ priority,
25128
+ content,
25129
+ content_hash AS contentHash,
25130
+ attributes`;
25131
+ var SqliteHistorySyncRepository = class {
25132
+ constructor(db) {
25133
+ this.db = db;
25134
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25135
+ this.sessionsStmt = db.prepare(
25136
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25137
+ FROM audit_events
25138
+ WHERE synced_at IS NULL
25139
+ AND event_type IN (${TYPE_LIST})
25140
+ AND started_at < :before
25141
+ GROUP BY sessionId
25142
+ ORDER BY earliest
25143
+ LIMIT :limit`
25144
+ );
25145
+ this.rowsStmt = db.prepare(
25146
+ `SELECT ${ROW_COLUMNS}
25147
+ FROM audit_events
25148
+ WHERE synced_at IS NULL
25149
+ AND event_type IN (${TYPE_LIST})
25150
+ AND started_at < :before
25151
+ AND COALESCE(root_session_id, id) = :sessionId
25152
+ ORDER BY (event_type = 'session') DESC, started_at
25153
+ LIMIT :limit`
25154
+ );
25155
+ this.captureRowsStmt = db.prepare(
25156
+ `SELECT ${ROW_COLUMNS}
25157
+ FROM audit_events
25158
+ WHERE synced_at IS NULL
25159
+ AND sync_claimed_at IS NULL
25160
+ AND outbox_owed = 1
25161
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25162
+ AND started_at < :before
25163
+ ORDER BY started_at
25164
+ LIMIT :limit`
25165
+ );
25166
+ this.markOwedStmt = db.prepare(
25167
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25168
+ );
25169
+ this.markCaptureBacklogOwedStmt = db.prepare(
25170
+ `UPDATE audit_events SET outbox_owed = 1
25171
+ WHERE synced_at IS NULL
25172
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25173
+ AND started_at < :before`
25174
+ );
25175
+ this.stampStmt = db.prepare(
25176
+ `UPDATE audit_events
25177
+ SET synced_at = :at,
25178
+ sync_claimed_at = NULL,
25179
+ sync_failed_at = :failedAt,
25180
+ sync_failure = :failure
25181
+ WHERE id = :id`
25182
+ );
25183
+ this.claimRowStmt = db.prepare(
25184
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25185
+ );
25186
+ this.releaseRowStmt = db.prepare(
25187
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25188
+ );
25189
+ this.releaseStaleClaimsStmt = db.prepare(
25190
+ `UPDATE audit_events SET sync_claimed_at = NULL
25191
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25192
+ );
25193
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25194
+ FROM audit_events${COUNTED_SCOPE}`);
25195
+ this.partitionByKindStmt = db.prepare(
25196
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25197
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25198
+ GROUP BY event_type`
25199
+ );
25200
+ this.countsStmt = db.prepare(
25201
+ `SELECT
25202
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25203
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25204
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25205
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25206
+ THEN 1 ELSE 0 END) AS skipped,
25207
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25208
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25209
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25210
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25211
+ FROM audit_events
25212
+ WHERE event_type IN (${TYPE_LIST})`
25213
+ );
25214
+ this.captureSkipCountStmt = db.prepare(
25215
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25216
+ // way the structural totals are. The split exists because a refusal is
25217
+ // terminal only against the deployment that gave it, and the structural
25218
+ // re-arm frees it on a change of deployment. The capture lane has no such
25219
+ // escape: re-arming a capture would offer one deployment's undelivered
25220
+ // prompts, with their text, to a deployment that never saw them, which is
25221
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25222
+ // reasons mean the same thing — this row will not be sent — and splitting
25223
+ // them would put refused captures in a bucket nothing reads and nothing
25224
+ // frees.
25225
+ `SELECT COUNT(*) AS skipped
25226
+ FROM audit_events
25227
+ WHERE synced_at = ${String(SKIPPED)}
25228
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25229
+ );
25230
+ this.fingerprintStmt = db.prepare(
25231
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25232
+ FROM history_sync WHERE id = 1`
25233
+ );
25234
+ this.setFingerprintStmt = db.prepare(
25235
+ `UPDATE history_sync
25236
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25237
+ WHERE id = 1`
25238
+ );
25239
+ this.disownCapturesStmt = db.prepare(
25240
+ `UPDATE audit_events SET outbox_owed = NULL
25241
+ WHERE outbox_owed IS NOT NULL
25242
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25243
+ AND started_at < :attachedAt`
25244
+ );
25245
+ this.rearmStmt = db.prepare(
25246
+ `UPDATE audit_events
25247
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25248
+ WHERE (synced_at > 0
25249
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25250
+ AND event_type IN (${TYPE_LIST})`
25251
+ );
25252
+ this.claimStmt = db.prepare(
25253
+ `UPDATE history_sync
25254
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25255
+ WHERE id = 1
25256
+ AND (owner_pid IS NULL
25257
+ OR heartbeat_at IS NULL
25258
+ OR heartbeat_at < :staleBefore
25259
+ OR heartbeat_at > :now)`
25260
+ );
25261
+ this.heartbeatStmt = db.prepare(
25262
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25263
+ );
25264
+ this.releaseStmt = db.prepare(
25265
+ `UPDATE history_sync
25266
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25267
+ WHERE id = 1 AND owner_pid = :pid`
25268
+ );
25269
+ this.closeWindowStmt = db.prepare(
25270
+ `UPDATE audit_events
25271
+ SET synced_at = ${String(SKIPPED)},
25272
+ sync_failed_at = :at,
25273
+ sync_failure = 'detached_undelivered'
25274
+ WHERE synced_at IS NULL
25275
+ AND event_type IN (${TYPE_LIST})
25276
+ AND started_at >= :attachedAt`
25277
+ );
25278
+ this.releaseBoundaryStmt = db.prepare(
25279
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25280
+ );
25281
+ this.freezeBoundaryStmt = db.prepare(
25282
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25283
+ );
25284
+ this.leaseStmt = db.prepare(
25285
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25286
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25287
+ FROM history_sync WHERE id = 1`
25288
+ );
25289
+ this.inspectionsStmt = db.prepare(
25290
+ `SELECT d.rule_id AS ruleId,
25291
+ d.name AS ruleName,
25292
+ d.version AS ruleVersion,
25293
+ d.category AS category,
25294
+ d.severity AS severity,
25295
+ f.span_start AS spanStart,
25296
+ f.span_end AS spanEnd,
25297
+ f.masked_match AS maskedMatch,
25298
+ f.action_taken AS actionTaken,
25299
+ f.confidence AS confidence
25300
+ FROM inspection_findings f
25301
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25302
+ WHERE f.audit_event_id = :auditEventId
25303
+ ORDER BY f.span_start, f.id`
25304
+ );
24814
25305
  }
24815
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24816
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25306
+ db;
25307
+ ensureRowStmt;
25308
+ sessionsStmt;
25309
+ rowsStmt;
25310
+ stampStmt;
25311
+ countsStmt;
25312
+ fingerprintStmt;
25313
+ setFingerprintStmt;
25314
+ rearmStmt;
25315
+ claimStmt;
25316
+ heartbeatStmt;
25317
+ releaseStmt;
25318
+ leaseStmt;
25319
+ inspectionsStmt;
25320
+ closeWindowStmt;
25321
+ releaseBoundaryStmt;
25322
+ freezeBoundaryStmt;
25323
+ captureRowsStmt;
25324
+ markOwedStmt;
25325
+ markCaptureBacklogOwedStmt;
25326
+ captureSkipCountStmt;
25327
+ disownCapturesStmt;
25328
+ partitionStmt;
25329
+ partitionByKindStmt;
25330
+ claimRowStmt;
25331
+ releaseRowStmt;
25332
+ releaseStaleClaimsStmt;
25333
+ /**
25334
+ * The masked detections recorded against one tool call.
25335
+ *
25336
+ * These travel with the event because a tool call's target is not
25337
+ * re-inspectable from the event alone — unlike a capture, where the text
25338
+ * itself is re-scannable. What crosses is the masked match and the rule that
25339
+ * produced it, never the value.
25340
+ */
25341
+ inspectionsFor(auditEventId) {
25342
+ return allRows(this.inspectionsStmt, { auditEventId });
24817
25343
  }
24818
- ensureSyncedAtColumn(db, "audit_events");
24819
- ensureScanLedgerTable(db);
24820
- ensureHistorySyncTable(db);
24821
- ensureBlockedDetectionsTable(db);
24822
- ensureRuleProbeCacheTable(db);
24823
- ensureWriteGateTrigger(db);
24824
- ensureTokenUsageColumns(db);
24825
- reconcileSourceProjectIds(db);
24826
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24827
- const drained = runLegacyHistoryBackfill(db);
24828
- if (drained) applyLegacyDropMigration(db, file2);
25344
+ /**
25345
+ * Sessions with structural rows still to send, oldest first.
25346
+ *
25347
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25348
+ * read. Anything recorded after the machine attached is the live forward
25349
+ * path's to deliver; this drain exists for what was recorded before it, and a
25350
+ * row both paths send is at best a duplicate request and at worst — for a
25351
+ * session root — an overwrite of the inventory ids the live path resolved.
25352
+ */
25353
+ pendingSessions(limit, before) {
25354
+ return allRows(this.sessionsStmt, { limit, before }).map(
25355
+ (r) => r.sessionId
25356
+ );
24829
25357
  }
24830
- }
24831
- function readLegacyTables(db) {
24832
- let holdsRows = false;
24833
- const marks = [];
24834
- for (const table of ["events", "findings"]) {
24835
- try {
24836
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
24837
- if (row === void 0) {
24838
- holdsRows = true;
24839
- marks.push(`${table}:unreadable`);
24840
- continue;
24841
- }
24842
- if (row.n > 0) holdsRows = true;
24843
- marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
24844
- } catch {
24845
- holdsRows = true;
24846
- marks.push(`${table}:unreadable`);
24847
- }
25358
+ /** One session's undelivered structural rows within the backlog, root first. */
25359
+ pendingRows(sessionId, limit, before) {
25360
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24848
25361
  }
24849
- return { holdsRows, mark: marks.join("|") };
24850
- }
24851
- function applyLegacyDropMigration(db, file2) {
24852
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24853
- if (!migration) return;
24854
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24855
- if (file2 !== void 0 && before?.holdsRows === true) {
24856
- try {
24857
- backupBeforeLegacyDrop(db, file2);
24858
- } catch (error61) {
24859
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24860
- return;
24861
- }
25362
+ /**
25363
+ * Captures this machine still owes the deployment, oldest first.
25364
+ *
25365
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25366
+ * by a time window — see captureRowsStmt for why a window could not express
25367
+ * this. `before` is the grace window that leaves a just-recorded capture to
25368
+ * the live path.
25369
+ */
25370
+ pendingCaptureRows(limit, before) {
25371
+ return allRows(this.captureRowsStmt, { limit, before });
24862
25372
  }
24863
- try {
25373
+ /**
25374
+ * Record that a capture is OWED to the deployment.
25375
+ *
25376
+ * Written by the attached forward path when a live send did not confirm
25377
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25378
+ * a fact rather than an inference: the machine was attached, the send did not
25379
+ * land, so the row is owed — which no time window can state, because the same
25380
+ * window that holds the rows a past attachment left owed also holds every
25381
+ * capture recorded while the machine was DETACHED, and those were never
25382
+ * offered to anyone.
25383
+ *
25384
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25385
+ * out of the drain's read.
25386
+ */
25387
+ markCaptureOwed(id) {
25388
+ this.markOwedStmt.run({ id });
25389
+ }
25390
+ /**
25391
+ * Mark every capture already on disk as owed, as of `before`.
25392
+ *
25393
+ * The consent-time backfill, called once from `aka attach` when a human
25394
+ * grants existing-history consent — never from an ongoing drain pass, and
25395
+ * never inferred from a boundary that could later move. `before` is the
25396
+ * caller's own "now" at the moment consent was granted, so what this marks
25397
+ * is exactly the backlog the consent prompt already counted, not whatever a
25398
+ * later re-attach or key rotation might widen it to.
25399
+ *
25400
+ * Returns how many rows matched, for the caller to log or test against. Not a
25401
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25402
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25403
+ */
25404
+ markCaptureBacklogOwed(before) {
25405
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25406
+ }
25407
+ /**
25408
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25409
+ *
25410
+ * CLEARS any failure reason in the same statement. A row that failed against
25411
+ * one deployment and then landed is delivered, and leaving the reason behind
25412
+ * would leave the store holding two contradictory answers about one row —
25413
+ * with the surface free to render either.
25414
+ */
25415
+ markSynced(ids, atMs) {
25416
+ this.stampAll(ids, atMs, null);
25417
+ }
25418
+ /**
25419
+ * Record that THIS MACHINE cannot express the row on the wire.
25420
+ *
25421
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25422
+ * payload, or a body the client itself refused to send. It fails identically
25423
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25424
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25425
+ * is retried; marking those would turn one outage into permanent data loss.
25426
+ */
25427
+ markSkipped(ids, atMs) {
25428
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25429
+ }
25430
+ /**
25431
+ * Record that THIS DEPLOYMENT refused the row.
25432
+ *
25433
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25434
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25435
+ * row is outstanding rather than why. What separates them is the reason, and
25436
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25437
+ * on one body, so it is terminal only for as long as this machine points at
25438
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25439
+ *
25440
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25441
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25442
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25443
+ */
25444
+ markRefused(ids, atMs) {
25445
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25446
+ }
25447
+ eachInTransaction(ids, run) {
25448
+ if (ids.length === 0) return;
24864
25449
  withTransaction(
24865
- db,
25450
+ this.db,
24866
25451
  () => {
24867
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24868
- if (alreadyDropped) return;
24869
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24870
- akaWarn(
24871
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24872
- );
24873
- return;
24874
- }
24875
- for (const statement of splitStatements(migration.sql)) {
24876
- db.exec(statement);
24877
- }
24878
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24879
- migration.tag,
24880
- Date.now()
24881
- );
25452
+ for (const id of ids) run(id);
24882
25453
  },
24883
25454
  "IMMEDIATE"
24884
25455
  );
24885
- } catch (error61) {
24886
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24887
25456
  }
24888
- }
24889
- function backupBeforeLegacyDrop(db, file2) {
24890
- reapStalePartials(file2);
24891
- const backup = backupPath(file2, "pre-drop");
24892
- snapshotStore(db, backup);
24893
- return backup;
24894
- }
24895
- var TOKEN_USAGE_COLUMNS = [
24896
- {
24897
- name: "input_tokens",
24898
- ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
24899
- },
24900
- {
24901
- name: "output_tokens",
24902
- ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
24903
- },
24904
- {
24905
- name: "cache_creation_input_tokens",
24906
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
24907
- },
24908
- {
24909
- name: "cache_read_input_tokens",
24910
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
24911
- },
24912
- {
24913
- name: "model",
24914
- ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
24915
- },
24916
- {
24917
- name: "provider",
24918
- ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
25457
+ stampAll(ids, value, failure, failedAtMs) {
25458
+ if (ids.length === 0) return;
25459
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25460
+ withTransaction(
25461
+ this.db,
25462
+ () => {
25463
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25464
+ },
25465
+ "IMMEDIATE"
25466
+ );
25467
+ }
25468
+ /**
25469
+ * Claim rows as in-flight.
25470
+ *
25471
+ * Advisory in exactly the sense the lease is: it records that a send is in
25472
+ * progress so a surface can say so, and a lost claim costs a row showing as
25473
+ * queued while it is actually being sent. It is not exclusion — the far side
25474
+ * settles a duplicate on the row id.
25475
+ */
25476
+ claimRows(ids, atMs) {
25477
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25478
+ }
25479
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25480
+ releaseRows(ids) {
25481
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25482
+ }
25483
+ /**
25484
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25485
+ *
25486
+ * A process killed between claiming and settling leaves rows claimed with
25487
+ * nothing left to settle them. Without this they read as "sending" for ever.
25488
+ */
25489
+ releaseStaleClaims(staleBefore) {
25490
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25491
+ }
25492
+ /**
25493
+ * Every tracked row in exactly one delivery state.
25494
+ *
25495
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25496
+ * pick up now", which is a different question from "what state is this row
25497
+ * in" — and a machine that has never attached has no boundary to pass, so
25498
+ * requiring one would force a caller to invent one and report the whole store
25499
+ * as queued.
25500
+ */
25501
+ /**
25502
+ * The same partition, one row per kind that a lane carries.
25503
+ *
25504
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25505
+ * scope decides which rows exist at all, so a kind that has never been
25506
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25507
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25508
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25509
+ * different things.
25510
+ */
25511
+ partitionByKind() {
25512
+ return allRows(
25513
+ this.partitionByKindStmt,
25514
+ {}
25515
+ ).map((row) => ({
25516
+ kind: row.kind,
25517
+ queued: row.queued ?? 0,
25518
+ inProgress: row.inProgress ?? 0,
25519
+ synced: row.synced ?? 0,
25520
+ failed: row.failed ?? 0,
25521
+ refused: row.refused ?? 0,
25522
+ detached: row.detached ?? 0,
25523
+ total: row.total ?? 0
25524
+ }));
25525
+ }
25526
+ partition() {
25527
+ const row = getRow(this.partitionStmt, {});
25528
+ return {
25529
+ queued: row?.queued ?? 0,
25530
+ inProgress: row?.inProgress ?? 0,
25531
+ synced: row?.synced ?? 0,
25532
+ failed: row?.failed ?? 0,
25533
+ refused: row?.refused ?? 0,
25534
+ detached: row?.detached ?? 0,
25535
+ total: row?.total ?? 0
25536
+ };
25537
+ }
25538
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25539
+ counts(before) {
25540
+ const row = getRow(this.countsStmt, { before });
25541
+ const captures = getRow(this.captureSkipCountStmt);
25542
+ return {
25543
+ pending: row?.pending ?? 0,
25544
+ sent: row?.sent ?? 0,
25545
+ skipped: row?.skipped ?? 0,
25546
+ refused: row?.refused ?? 0,
25547
+ detached: row?.detached ?? 0,
25548
+ capturesSkipped: captures?.skipped ?? 0
25549
+ };
25550
+ }
25551
+ /**
25552
+ * The deployment the current stamps were made against, and where its backlog
25553
+ * ends.
25554
+ *
25555
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25556
+ * machine that has never drained is — and every writer below seeds the row
25557
+ * before it needs one, so nothing depends on this creating it. Keeping the
25558
+ * write off the gate path matters because the gate runs on every pass while a
25559
+ * write has to take the database's write lock.
25560
+ */
25561
+ deployment() {
25562
+ const row = getRow(
25563
+ this.fingerprintStmt
25564
+ );
25565
+ return {
25566
+ fingerprint: row?.fingerprint ?? void 0,
25567
+ backlogBefore: row?.backlogBefore ?? void 0
25568
+ };
25569
+ }
25570
+ /**
25571
+ * Point the ledger at a different deployment, discarding what it recorded
25572
+ * about the previous one.
25573
+ *
25574
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25575
+ * machine has just left are undelivered as far as the new one is concerned.
25576
+ * All four in one transaction, so a crash between them cannot leave stamps
25577
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25578
+ * a disown with no re-mark to follow it.
25579
+ *
25580
+ * The boundary is written HERE and only here, which is what freezes it: a
25581
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25582
+ * unchanged, so this never runs and the backlog does not widen back over rows
25583
+ * the live path has since delivered.
25584
+ *
25585
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25586
+ * granted existing-history consent for the deployment this call is arming —
25587
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25588
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25589
+ * apart. Passed only when that grant is valid, since this method has no way
25590
+ * to check consent itself and must not mark a row owed for a machine that
25591
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25592
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25593
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25594
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25595
+ * on the cleared side of that bound — and the re-mark in the same
25596
+ * transaction is what puts those rows back. A crash between the two cannot
25597
+ * strand the ledger disowned with nothing re-marked — the transaction either
25598
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25599
+ * committed re-enters this method on the very next pass. Omit it (the
25600
+ * structural-only tests do) to exercise the disown in isolation.
25601
+ *
25602
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25603
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25604
+ * live path can mark a capture owed from the moment `aka attach` writes the
25605
+ * descriptor, before the drain's first pass ever reaches this method, and
25606
+ * such a row sits at or after the bound rather than below it. What keeps the
25607
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25608
+ * bound — disown runs first, re-mark second, both inside the one
25609
+ * transaction above.
25610
+ */
25611
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25612
+ this.ensureRowStmt.run();
25613
+ withTransaction(
25614
+ this.db,
25615
+ () => {
25616
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25617
+ this.rearmStmt.run();
25618
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25619
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25620
+ }
25621
+ if (backfillCapturesBefore !== void 0) {
25622
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25623
+ }
25624
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25625
+ },
25626
+ "IMMEDIATE"
25627
+ );
25628
+ }
25629
+ /**
25630
+ * End the attached period: hand its rows to the live path, and release the
25631
+ * boundary so the next attachment can freeze a new one.
25632
+ *
25633
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25634
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25635
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25636
+ * during the detached period, because the machine is not attached. Rows
25637
+ * recorded in that window sit after the boundary and before the re-attach, so
25638
+ * neither path takes them, and the pending count reports none outstanding.
25639
+ *
25640
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25641
+ * closing attachment's to deliver and are no longer outstanding — that is what
25642
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25643
+ * distinction is not academic: this used to write a delivery TIME, which every
25644
+ * read treats as delivery, so one detach turned a window of undelivered rows
25645
+ * into a window of delivered ones and no surface could tell. It writes the
25646
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25647
+ * "received" stop being the same fact.
25648
+ *
25649
+ * A change of deployment still frees them (see the re-arm), because the next
25650
+ * deployment has seen none of this machine's history — so the rows reach it
25651
+ * exactly as they did when this wrote a delivery time.
25652
+ *
25653
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25654
+ * window unstamped — that half-state would re-send the whole attached period
25655
+ * on the next attach, which is the failure the boundary exists to prevent.
25656
+ */
25657
+ closeAttachedWindow(attachedAtMs, atMs) {
25658
+ this.ensureRowStmt.run();
25659
+ withTransaction(
25660
+ this.db,
25661
+ () => {
25662
+ const row = getRow(this.fingerprintStmt);
25663
+ const from = row?.backlogBefore ?? attachedAtMs;
25664
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25665
+ this.releaseBoundaryStmt.run();
25666
+ },
25667
+ "IMMEDIATE"
25668
+ );
25669
+ }
25670
+ /**
25671
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25672
+ *
25673
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25674
+ * different deployment and therefore discards what was delivered to the old
25675
+ * one: here the recipient is the same, so everything already sent to it stays
25676
+ * sent.
25677
+ */
25678
+ freezeBoundary(backlogBefore) {
25679
+ this.ensureRowStmt.run();
25680
+ this.freezeBoundaryStmt.run({ backlogBefore });
25681
+ }
25682
+ /** Take the claim, or report that someone live already holds it. */
25683
+ claim(pid, host, nowMs, staleAfterMs) {
25684
+ this.ensureRowStmt.run();
25685
+ let taken = false;
25686
+ withTransaction(
25687
+ this.db,
25688
+ () => {
25689
+ const result = this.claimStmt.run({
25690
+ pid,
25691
+ host,
25692
+ now: nowMs,
25693
+ staleBefore: nowMs - staleAfterMs
25694
+ });
25695
+ taken = result.changes === 1;
25696
+ },
25697
+ "IMMEDIATE"
25698
+ );
25699
+ return taken;
25700
+ }
25701
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25702
+ heartbeat(pid, nowMs) {
25703
+ this.heartbeatStmt.run({ now: nowMs, pid });
25704
+ }
25705
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25706
+ release(pid) {
25707
+ this.releaseStmt.run({ pid });
25708
+ }
25709
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25710
+ lease() {
25711
+ return getRow(this.leaseStmt);
25712
+ }
25713
+ };
25714
+
25715
+ // ../../packages/persistence/src/migrations.ts
25716
+ function describeObject(object2) {
25717
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25718
+ }
25719
+ function splitStatements(sql) {
25720
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25721
+ }
25722
+ function createdIndexName(statement) {
25723
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25724
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25725
+ }
25726
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25727
+ function applyMigrations(db, file2, options = {}) {
25728
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25729
+ db.exec(
25730
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25731
+ );
25732
+ const applied = new Set(
25733
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25734
+ );
25735
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25736
+ const record2 = db.prepare(
25737
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25738
+ );
25739
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25740
+ if (applied.has(migration.tag)) continue;
25741
+ if (options.skipTags?.has(migration.tag) === true) continue;
25742
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25743
+ const evidence = evidenceObjects(migration.sql);
25744
+ const present = evidence.filter((o) => evidenceExists(db, o));
25745
+ if (present.length > 0 && present.length < evidence.length) {
25746
+ const missing = evidence.filter((o) => !present.includes(o));
25747
+ 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.`;
25748
+ akaWarn(message);
25749
+ throw new Error(`[aka] ${message}`);
25750
+ }
25751
+ const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
25752
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25753
+ const statements = splitStatements(migration.sql);
25754
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25755
+ try {
25756
+ withTransaction(
25757
+ db,
25758
+ () => {
25759
+ for (const statement of statements) {
25760
+ const indexName = createdIndexName(statement);
25761
+ if (indexName === void 0) {
25762
+ if (alreadyApplied) continue;
25763
+ } else if (indexExists(db, indexName)) {
25764
+ continue;
25765
+ }
25766
+ db.exec(statement);
25767
+ }
25768
+ if (wantsFkOff && !alreadyApplied) {
25769
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25770
+ if (violations.length > 0) {
25771
+ throw new Error(
25772
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25773
+ );
25774
+ }
25775
+ }
25776
+ record2.run(migration.tag, Date.now());
25777
+ },
25778
+ "IMMEDIATE"
25779
+ );
25780
+ } finally {
25781
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25782
+ }
25783
+ }
25784
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25785
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25786
+ }
25787
+ ensureSyncedAtColumn(db, "audit_events");
25788
+ ensureScanLedgerTable(db);
25789
+ ensureHistorySyncTable(db);
25790
+ ensureBlockedDetectionsTable(db);
25791
+ ensureRuleProbeCacheTable(db);
25792
+ ensureWriteGateTrigger(db);
25793
+ ensureTokenUsageColumns(db);
25794
+ reconcileSourceProjectIds(db);
25795
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25796
+ const drained = runLegacyHistoryBackfill(db);
25797
+ if (drained) applyLegacyDropMigration(db, file2);
25798
+ }
25799
+ }
25800
+ function readLegacyTables(db) {
25801
+ let holdsRows = false;
25802
+ const marks = [];
25803
+ for (const table of ["events", "findings"]) {
25804
+ try {
25805
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
25806
+ if (row === void 0) {
25807
+ holdsRows = true;
25808
+ marks.push(`${table}:unreadable`);
25809
+ continue;
25810
+ }
25811
+ if (row.n > 0) holdsRows = true;
25812
+ marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
25813
+ } catch {
25814
+ holdsRows = true;
25815
+ marks.push(`${table}:unreadable`);
25816
+ }
25817
+ }
25818
+ return { holdsRows, mark: marks.join("|") };
25819
+ }
25820
+ function applyLegacyDropMigration(db, file2) {
25821
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25822
+ if (!migration) return;
25823
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25824
+ if (file2 !== void 0 && before?.holdsRows === true) {
25825
+ try {
25826
+ backupBeforeLegacyDrop(db, file2);
25827
+ } catch (error61) {
25828
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25829
+ return;
25830
+ }
25831
+ }
25832
+ try {
25833
+ withTransaction(
25834
+ db,
25835
+ () => {
25836
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25837
+ if (alreadyDropped) return;
25838
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25839
+ akaWarn(
25840
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25841
+ );
25842
+ return;
25843
+ }
25844
+ for (const statement of splitStatements(migration.sql)) {
25845
+ db.exec(statement);
25846
+ }
25847
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25848
+ migration.tag,
25849
+ Date.now()
25850
+ );
25851
+ },
25852
+ "IMMEDIATE"
25853
+ );
25854
+ } catch (error61) {
25855
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25856
+ }
25857
+ }
25858
+ function backupBeforeLegacyDrop(db, file2) {
25859
+ reapStalePartials(file2);
25860
+ const backup = backupPath(file2, "pre-drop");
25861
+ snapshotStore(db, backup);
25862
+ return backup;
25863
+ }
25864
+ var TOKEN_USAGE_COLUMNS = [
25865
+ {
25866
+ name: "input_tokens",
25867
+ ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
25868
+ },
25869
+ {
25870
+ name: "output_tokens",
25871
+ ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
25872
+ },
25873
+ {
25874
+ name: "cache_creation_input_tokens",
25875
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
25876
+ },
25877
+ {
25878
+ name: "cache_read_input_tokens",
25879
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25880
+ },
25881
+ {
25882
+ name: "model",
25883
+ ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25884
+ },
25885
+ {
25886
+ name: "provider",
25887
+ ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
24919
25888
  }
24920
25889
  ];
24921
25890
  function ensureTokenUsageColumns(db) {
@@ -25176,10 +26145,62 @@ function ensureSyncedAtColumn(db, table) {
25176
26145
  if (!columns.includes("outbox_owed")) {
25177
26146
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25178
26147
  }
26148
+ if (!columns.includes("sync_failed_at")) {
26149
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26150
+ }
26151
+ if (!columns.includes("sync_failure")) {
26152
+ withTransaction(
26153
+ db,
26154
+ () => {
26155
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26156
+ db.exec(
26157
+ `UPDATE ${table} SET synced_at = NULL
26158
+ WHERE synced_at = -1
26159
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26160
+ );
26161
+ },
26162
+ "IMMEDIATE"
26163
+ );
26164
+ }
25179
26165
  db.exec(
25180
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25181
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26166
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26167
+ BEFORE UPDATE OF sync_failure ON ${table}
26168
+ WHEN ${syncFailureRejectCondition()}
26169
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25182
26170
  );
26171
+ const syncIndexColumns = [
26172
+ "event_type",
26173
+ "synced_at",
26174
+ "sync_claimed_at",
26175
+ "started_at",
26176
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26177
+ // has to be in the index for the read to stay covered — but putting it
26178
+ // ahead of `started_at` would reorder the prefix the structural drain's
26179
+ // reads match on.
26180
+ "sync_failure"
26181
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26182
+ //
26183
+ // The delivery-state read tests it — a capture's state depends on whether a
26184
+ // live forward marked it owed — so carrying it here makes that read covering
26185
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26186
+ // But a sixth column changes what the planner charges for this index, and
26187
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26188
+ // then stops choosing the per-session index for the token rollup and walks
26189
+ // every `llm_call` in the store through the event-type index instead. That
26190
+ // read grows with the store; this one does not.
26191
+ //
26192
+ // 40 ms on the largest store measured, once per render, is a cost worth
26193
+ // paying to leave every other read's plan where it was.
26194
+ ];
26195
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26196
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26197
+ if (!syncIndexMatches) {
26198
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26199
+ db.exec(
26200
+ `CREATE INDEX idx_audit_events_sync
26201
+ ON audit_events (${syncIndexColumns.join(", ")})`
26202
+ );
26203
+ }
25183
26204
  db.exec(
25184
26205
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25185
26206
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25401,7 +26422,11 @@ function buildAuditEvent(row) {
25401
26422
  link: linkParsed?.success ? linkParsed.data : null,
25402
26423
  targetId: row.target_id,
25403
26424
  internal: intToBool(row.internal),
25404
- flagged: intToBool(row.flagged)
26425
+ flagged: intToBool(row.flagged),
26426
+ // Only meaningful when the title came out empty — a row whose body was
26427
+ // expired but whose title fell back to `tool_name` still has something to
26428
+ // render, and flagging it would make the view apologise for nothing.
26429
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25405
26430
  };
25406
26431
  }
25407
26432
  var TIMELINE_COLUMNS = `
@@ -25409,6 +26434,7 @@ var TIMELINE_COLUMNS = `
25409
26434
  event_type,
25410
26435
  started_at,
25411
26436
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26437
+ content_expired_at,
25412
26438
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25413
26439
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25414
26440
  json_extract(attributes, '$.severity') AS severity,
@@ -26074,6 +27100,88 @@ var SqliteAuditEventsRepository = class {
26074
27100
  }
26075
27101
  };
26076
27102
 
27103
+ // ../../packages/persistence/src/repositories/body-retention.ts
27104
+ var DEFAULT_BATCH_SIZE = 500;
27105
+ var DEFAULT_MAX_ROWS = 5e4;
27106
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27107
+ var SqliteBodyRetentionRepository = class {
27108
+ constructor(db) {
27109
+ this.db = db;
27110
+ const select = (laneClause) => `
27111
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27112
+ FROM audit_events
27113
+ WHERE content IS NOT NULL
27114
+ AND started_at < :cutoff
27115
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27116
+ ${laneClause}
27117
+ ORDER BY started_at
27118
+ LIMIT :limit`;
27119
+ this.candidatesStmt = this.db.prepare(select(""));
27120
+ this.candidatesSyncSafeStmt = this.db.prepare(
27121
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27122
+ );
27123
+ this.heldBySyncStmt = this.db.prepare(`
27124
+ SELECT COUNT(*) AS n
27125
+ FROM audit_events
27126
+ WHERE content IS NOT NULL
27127
+ AND started_at < :cutoff
27128
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27129
+ AND synced_at IS NULL`);
27130
+ this.expireStmt = this.db.prepare(
27131
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27132
+ );
27133
+ }
27134
+ db;
27135
+ candidatesStmt;
27136
+ candidatesSyncSafeStmt;
27137
+ heldBySyncStmt;
27138
+ expireStmt;
27139
+ /** How many bytes a pass with these options would free, changing nothing. */
27140
+ preview(opts) {
27141
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27142
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27143
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27144
+ return {
27145
+ rowsExpired: rows.length,
27146
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27147
+ rowsHeldBySync: this.countHeldBySync(opts)
27148
+ };
27149
+ }
27150
+ /** Clear eligible bodies, in bounded batches. */
27151
+ expire(opts) {
27152
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27153
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27154
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27155
+ let rowsExpired = 0;
27156
+ let bytesFreed = 0;
27157
+ let done = true;
27158
+ while (rowsExpired < maxRows) {
27159
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27160
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27161
+ if (batch.length === 0) break;
27162
+ withTransaction(
27163
+ this.db,
27164
+ () => {
27165
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27166
+ },
27167
+ "IMMEDIATE"
27168
+ );
27169
+ rowsExpired += batch.length;
27170
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27171
+ if (batch.length < remaining) break;
27172
+ if (rowsExpired >= maxRows) {
27173
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27174
+ }
27175
+ }
27176
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27177
+ }
27178
+ countHeldBySync(opts) {
27179
+ if (opts.sweepSyncLane) return 0;
27180
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27181
+ return row.n;
27182
+ }
27183
+ };
27184
+
26077
27185
  // ../../packages/persistence/src/repositories/classified-data.ts
26078
27186
  var SqliteClassifiedDataRepository = class {
26079
27187
  constructor(db) {
@@ -26902,7 +28010,15 @@ function toFlatFindingRow(r) {
26902
28010
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26903
28011
  eventId: r.event_id,
26904
28012
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26905
- status: deriveInstanceStatus(r)
28013
+ status: deriveInstanceStatus(r),
28014
+ delivery: deriveFindingDelivery({
28015
+ kind: r.kind,
28016
+ syncedAt: r.synced_at,
28017
+ syncClaimedAt: r.sync_claimed_at,
28018
+ syncFailedAt: r.sync_failed_at,
28019
+ syncFailure: r.sync_failure,
28020
+ outboxOwed: r.outbox_owed
28021
+ })
26906
28022
  };
26907
28023
  }
26908
28024
  function encodeGroupCursor(group) {
@@ -26966,7 +28082,10 @@ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS c
26966
28082
  e.tool_name AS tool_name,
26967
28083
  f.audit_event_id AS event_id, e.root_session_id AS session_id,
26968
28084
  e.event_type AS kind, f.finding_key AS finding_key,
26969
- ${latestResolutionStatusSql("f")} AS latest_status`;
28085
+ ${latestResolutionStatusSql("f")} AS latest_status,
28086
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28087
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28088
+ e.outbox_owed AS outbox_owed`;
26970
28089
  var DAY_MS3 = 864e5;
26971
28090
  var SqliteFindingsRepository = class {
26972
28091
  constructor(db) {
@@ -27211,6 +28330,7 @@ var SqliteFindingsRepository = class {
27211
28330
  providers: query.provider,
27212
28331
  actions: query.action,
27213
28332
  statuses: query.status,
28333
+ deliveries: query.deployment,
27214
28334
  tools: query.tool,
27215
28335
  repo: query.repo,
27216
28336
  file: query.file,
@@ -27278,6 +28398,7 @@ var SqliteFindingsRepository = class {
27278
28398
  providers: query.provider,
27279
28399
  actions: query.action,
27280
28400
  statuses: query.status,
28401
+ deliveries: query.deployment,
27281
28402
  tools: query.tool,
27282
28403
  q: query.q
27283
28404
  };
@@ -27541,7 +28662,9 @@ var SqliteFindingsRepository = class {
27541
28662
  )
27542
28663
  );
27543
28664
  for (const row of grouped) {
27544
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28665
+ if (Object.hasOwn(byAction, row.action_taken)) {
28666
+ byAction[row.action_taken] = row.c;
28667
+ }
27545
28668
  }
27546
28669
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27547
28670
  const sevRows = allRows(
@@ -27558,7 +28681,9 @@ var SqliteFindingsRepository = class {
27558
28681
  )
27559
28682
  );
27560
28683
  for (const row of sevRows) {
27561
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28684
+ if (Object.hasOwn(bySeverity, row.severity)) {
28685
+ bySeverity[row.severity] = row.c;
28686
+ }
27562
28687
  }
27563
28688
  const categories = ENFORCEABLE_CATEGORIES;
27564
28689
  const enabledRows = allRows(
@@ -27607,525 +28732,6 @@ function isoDay(ms) {
27607
28732
  return new Date(ms).toISOString().slice(0, 10);
27608
28733
  }
27609
28734
 
27610
- // ../../packages/persistence/src/repositories/history-sync.ts
27611
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27612
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27613
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27614
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27615
- var SKIPPED = -1;
27616
- var ROW_COLUMNS = `id,
27617
- parent_id AS parentId,
27618
- root_session_id AS rootSessionId,
27619
- event_type AS eventType,
27620
- host_id AS hostId,
27621
- harness_id AS harnessId,
27622
- source_project_id AS sourceProjectId,
27623
- started_at AS startedAt,
27624
- ended_at AS endedAt,
27625
- severity,
27626
- priority,
27627
- content,
27628
- content_hash AS contentHash,
27629
- attributes`;
27630
- var SqliteHistorySyncRepository = class {
27631
- constructor(db) {
27632
- this.db = db;
27633
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27634
- this.sessionsStmt = db.prepare(
27635
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27636
- FROM audit_events
27637
- WHERE synced_at IS NULL
27638
- AND event_type IN (${TYPE_LIST})
27639
- AND started_at < :before
27640
- GROUP BY sessionId
27641
- ORDER BY earliest
27642
- LIMIT :limit`
27643
- );
27644
- this.rowsStmt = db.prepare(
27645
- `SELECT ${ROW_COLUMNS}
27646
- FROM audit_events
27647
- WHERE synced_at IS NULL
27648
- AND event_type IN (${TYPE_LIST})
27649
- AND started_at < :before
27650
- AND COALESCE(root_session_id, id) = :sessionId
27651
- ORDER BY (event_type = 'session') DESC, started_at
27652
- LIMIT :limit`
27653
- );
27654
- this.captureRowsStmt = db.prepare(
27655
- `SELECT ${ROW_COLUMNS}
27656
- FROM audit_events
27657
- WHERE synced_at IS NULL
27658
- AND sync_claimed_at IS NULL
27659
- AND outbox_owed = 1
27660
- AND event_type IN (${CAPTURE_TYPE_LIST})
27661
- AND started_at < :before
27662
- ORDER BY started_at
27663
- LIMIT :limit`
27664
- );
27665
- this.markOwedStmt = db.prepare(
27666
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27667
- );
27668
- this.markCaptureBacklogOwedStmt = db.prepare(
27669
- `UPDATE audit_events SET outbox_owed = 1
27670
- WHERE synced_at IS NULL
27671
- AND event_type IN (${CAPTURE_TYPE_LIST})
27672
- AND started_at < :before`
27673
- );
27674
- this.stampStmt = db.prepare(
27675
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27676
- );
27677
- this.claimRowStmt = db.prepare(
27678
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27679
- );
27680
- this.releaseRowStmt = db.prepare(
27681
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27682
- );
27683
- this.releaseStaleClaimsStmt = db.prepare(
27684
- `UPDATE audit_events SET sync_claimed_at = NULL
27685
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27686
- );
27687
- this.partitionStmt = db.prepare(
27688
- `SELECT
27689
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27690
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27691
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27692
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27693
- COUNT(*) AS total
27694
- FROM audit_events
27695
- WHERE event_type IN (${TYPE_LIST})`
27696
- );
27697
- this.countsStmt = db.prepare(
27698
- `SELECT
27699
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27700
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27701
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27702
- FROM audit_events
27703
- WHERE event_type IN (${TYPE_LIST})`
27704
- );
27705
- this.captureSkipCountStmt = db.prepare(
27706
- `SELECT COUNT(*) AS skipped
27707
- FROM audit_events
27708
- WHERE synced_at = ${String(SKIPPED)}
27709
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27710
- );
27711
- this.fingerprintStmt = db.prepare(
27712
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27713
- FROM history_sync WHERE id = 1`
27714
- );
27715
- this.setFingerprintStmt = db.prepare(
27716
- `UPDATE history_sync
27717
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27718
- WHERE id = 1`
27719
- );
27720
- this.disownCapturesStmt = db.prepare(
27721
- `UPDATE audit_events SET outbox_owed = NULL
27722
- WHERE outbox_owed IS NOT NULL
27723
- AND event_type IN (${CAPTURE_TYPE_LIST})
27724
- AND started_at < :attachedAt`
27725
- );
27726
- this.rearmStmt = db.prepare(
27727
- `UPDATE audit_events SET synced_at = NULL
27728
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27729
- );
27730
- this.claimStmt = db.prepare(
27731
- `UPDATE history_sync
27732
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27733
- WHERE id = 1
27734
- AND (owner_pid IS NULL
27735
- OR heartbeat_at IS NULL
27736
- OR heartbeat_at < :staleBefore
27737
- OR heartbeat_at > :now)`
27738
- );
27739
- this.heartbeatStmt = db.prepare(
27740
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27741
- );
27742
- this.releaseStmt = db.prepare(
27743
- `UPDATE history_sync
27744
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27745
- WHERE id = 1 AND owner_pid = :pid`
27746
- );
27747
- this.closeWindowStmt = db.prepare(
27748
- `UPDATE audit_events SET synced_at = :at
27749
- WHERE synced_at IS NULL
27750
- AND event_type IN (${TYPE_LIST})
27751
- AND started_at >= :attachedAt`
27752
- );
27753
- this.releaseBoundaryStmt = db.prepare(
27754
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27755
- );
27756
- this.freezeBoundaryStmt = db.prepare(
27757
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27758
- );
27759
- this.leaseStmt = db.prepare(
27760
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27761
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27762
- FROM history_sync WHERE id = 1`
27763
- );
27764
- this.inspectionsStmt = db.prepare(
27765
- `SELECT d.rule_id AS ruleId,
27766
- d.name AS ruleName,
27767
- d.version AS ruleVersion,
27768
- d.category AS category,
27769
- d.severity AS severity,
27770
- f.span_start AS spanStart,
27771
- f.span_end AS spanEnd,
27772
- f.masked_match AS maskedMatch,
27773
- f.action_taken AS actionTaken,
27774
- f.confidence AS confidence
27775
- FROM inspection_findings f
27776
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27777
- WHERE f.audit_event_id = :auditEventId
27778
- ORDER BY f.span_start, f.id`
27779
- );
27780
- }
27781
- db;
27782
- ensureRowStmt;
27783
- sessionsStmt;
27784
- rowsStmt;
27785
- stampStmt;
27786
- countsStmt;
27787
- fingerprintStmt;
27788
- setFingerprintStmt;
27789
- rearmStmt;
27790
- claimStmt;
27791
- heartbeatStmt;
27792
- releaseStmt;
27793
- leaseStmt;
27794
- inspectionsStmt;
27795
- closeWindowStmt;
27796
- releaseBoundaryStmt;
27797
- freezeBoundaryStmt;
27798
- captureRowsStmt;
27799
- markOwedStmt;
27800
- markCaptureBacklogOwedStmt;
27801
- captureSkipCountStmt;
27802
- disownCapturesStmt;
27803
- partitionStmt;
27804
- claimRowStmt;
27805
- releaseRowStmt;
27806
- releaseStaleClaimsStmt;
27807
- /**
27808
- * The masked detections recorded against one tool call.
27809
- *
27810
- * These travel with the event because a tool call's target is not
27811
- * re-inspectable from the event alone — unlike a capture, where the text
27812
- * itself is re-scannable. What crosses is the masked match and the rule that
27813
- * produced it, never the value.
27814
- */
27815
- inspectionsFor(auditEventId) {
27816
- return allRows(this.inspectionsStmt, { auditEventId });
27817
- }
27818
- /**
27819
- * Sessions with structural rows still to send, oldest first.
27820
- *
27821
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27822
- * read. Anything recorded after the machine attached is the live forward
27823
- * path's to deliver; this drain exists for what was recorded before it, and a
27824
- * row both paths send is at best a duplicate request and at worst — for a
27825
- * session root — an overwrite of the inventory ids the live path resolved.
27826
- */
27827
- pendingSessions(limit, before) {
27828
- return allRows(this.sessionsStmt, { limit, before }).map(
27829
- (r) => r.sessionId
27830
- );
27831
- }
27832
- /** One session's undelivered structural rows within the backlog, root first. */
27833
- pendingRows(sessionId, limit, before) {
27834
- return allRows(this.rowsStmt, { sessionId, limit, before });
27835
- }
27836
- /**
27837
- * Captures this machine still owes the deployment, oldest first.
27838
- *
27839
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27840
- * by a time window — see captureRowsStmt for why a window could not express
27841
- * this. `before` is the grace window that leaves a just-recorded capture to
27842
- * the live path.
27843
- */
27844
- pendingCaptureRows(limit, before) {
27845
- return allRows(this.captureRowsStmt, { limit, before });
27846
- }
27847
- /**
27848
- * Record that a capture is OWED to the deployment.
27849
- *
27850
- * Written by the attached forward path when a live send did not confirm
27851
- * delivery, and read by the drain as the whole of its eligibility test. It is
27852
- * a fact rather than an inference: the machine was attached, the send did not
27853
- * land, so the row is owed — which no time window can state, because the same
27854
- * window that holds the rows a past attachment left owed also holds every
27855
- * capture recorded while the machine was DETACHED, and those were never
27856
- * offered to anyone.
27857
- *
27858
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27859
- * out of the drain's read.
27860
- */
27861
- markCaptureOwed(id) {
27862
- this.markOwedStmt.run({ id });
27863
- }
27864
- /**
27865
- * Mark every capture already on disk as owed, as of `before`.
27866
- *
27867
- * The consent-time backfill, called once from `aka attach` when a human
27868
- * grants existing-history consent — never from an ongoing drain pass, and
27869
- * never inferred from a boundary that could later move. `before` is the
27870
- * caller's own "now" at the moment consent was granted, so what this marks
27871
- * is exactly the backlog the consent prompt already counted, not whatever a
27872
- * later re-attach or key rotation might widen it to.
27873
- *
27874
- * Returns how many rows matched, for the caller to log or test against. Not a
27875
- * count of NEWLY marked rows — a row still unsynced from an earlier call
27876
- * matches again and is counted again, the same as `UPDATE`'s own `changes`.
27877
- */
27878
- markCaptureBacklogOwed(before) {
27879
- return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
27880
- }
27881
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27882
- markSynced(ids, atMs) {
27883
- this.stampAll(ids, atMs);
27884
- }
27885
- /**
27886
- * Record that a row will never be sent.
27887
- *
27888
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27889
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27890
- * is retried; marking those would turn one outage into permanent data loss.
27891
- */
27892
- markSkipped(ids) {
27893
- this.stampAll(ids, SKIPPED);
27894
- }
27895
- eachInTransaction(ids, run) {
27896
- if (ids.length === 0) return;
27897
- withTransaction(
27898
- this.db,
27899
- () => {
27900
- for (const id of ids) run(id);
27901
- },
27902
- "IMMEDIATE"
27903
- );
27904
- }
27905
- stampAll(ids, value) {
27906
- if (ids.length === 0) return;
27907
- withTransaction(
27908
- this.db,
27909
- () => {
27910
- for (const id of ids) this.stampStmt.run({ at: value, id });
27911
- },
27912
- "IMMEDIATE"
27913
- );
27914
- }
27915
- /**
27916
- * Claim rows as in-flight.
27917
- *
27918
- * Advisory in exactly the sense the lease is: it records that a send is in
27919
- * progress so a surface can say so, and a lost claim costs a row showing as
27920
- * queued while it is actually being sent. It is not exclusion — the far side
27921
- * settles a duplicate on the row id.
27922
- */
27923
- claimRows(ids, atMs) {
27924
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
27925
- }
27926
- /** Give back a claim without settling — the send failed, the row is queued again. */
27927
- releaseRows(ids) {
27928
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
27929
- }
27930
- /**
27931
- * Clear claims older than `staleBefore`, and report how many were cleared.
27932
- *
27933
- * A process killed between claiming and settling leaves rows claimed with
27934
- * nothing left to settle them. Without this they read as "sending" for ever.
27935
- */
27936
- releaseStaleClaims(staleBefore) {
27937
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
27938
- }
27939
- /**
27940
- * Every tracked row in exactly one delivery state.
27941
- *
27942
- * Takes no boundary on purpose. The boundary answers "what should the drain
27943
- * pick up now", which is a different question from "what state is this row
27944
- * in" — and a machine that has never attached has no boundary to pass, so
27945
- * requiring one would force a caller to invent one and report the whole store
27946
- * as queued.
27947
- */
27948
- partition() {
27949
- const row = getRow(this.partitionStmt, {});
27950
- return {
27951
- queued: row?.queued ?? 0,
27952
- inProgress: row?.inProgress ?? 0,
27953
- synced: row?.synced ?? 0,
27954
- failed: row?.failed ?? 0,
27955
- total: row?.total ?? 0
27956
- };
27957
- }
27958
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
27959
- counts(before) {
27960
- const row = getRow(
27961
- this.countsStmt,
27962
- { before }
27963
- );
27964
- const captures = getRow(this.captureSkipCountStmt);
27965
- return {
27966
- pending: row?.pending ?? 0,
27967
- sent: row?.sent ?? 0,
27968
- skipped: row?.skipped ?? 0,
27969
- capturesSkipped: captures?.skipped ?? 0
27970
- };
27971
- }
27972
- /**
27973
- * The deployment the current stamps were made against, and where its backlog
27974
- * ends.
27975
- *
27976
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
27977
- * machine that has never drained is — and every writer below seeds the row
27978
- * before it needs one, so nothing depends on this creating it. Keeping the
27979
- * write off the gate path matters because the gate runs on every pass while a
27980
- * write has to take the database's write lock.
27981
- */
27982
- deployment() {
27983
- const row = getRow(
27984
- this.fingerprintStmt
27985
- );
27986
- return {
27987
- fingerprint: row?.fingerprint ?? void 0,
27988
- backlogBefore: row?.backlogBefore ?? void 0
27989
- };
27990
- }
27991
- /**
27992
- * Point the ledger at a different deployment, discarding what it recorded
27993
- * about the previous one.
27994
- *
27995
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
27996
- * machine has just left are undelivered as far as the new one is concerned.
27997
- * All four in one transaction, so a crash between them cannot leave stamps
27998
- * attributed to the wrong deployment, a boundary that belongs to another, or
27999
- * a disown with no re-mark to follow it.
28000
- *
28001
- * The boundary is written HERE and only here, which is what freezes it: a
28002
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28003
- * unchanged, so this never runs and the backlog does not widen back over rows
28004
- * the live path has since delivered.
28005
- *
28006
- * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
28007
- * granted existing-history consent for the deployment this call is arming —
28008
- * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
28009
- * instant, `backlogBefore` is the ATTACH instant, and the two can be far
28010
- * apart. Passed only when that grant is valid, since this method has no way
28011
- * to check consent itself and must not mark a row owed for a machine that
28012
- * never agreed to it. Applied AFTER the disown above, in the SAME
28013
- * transaction: what the disown clears is every marker below `backlogBefore`,
28014
- * which includes this deployment's OWN pre-attach rows — `aka attach` calls
28015
- * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
28016
- * on the cleared side of that bound — and the re-mark in the same
28017
- * transaction is what puts those rows back. A crash between the two cannot
28018
- * strand the ledger disowned with nothing re-marked — the transaction either
28019
- * lands whole or not at all, and a fingerprint mismatch that has not yet
28020
- * committed re-enters this method on the very next pass. Omit it (the
28021
- * structural-only tests do) to exercise the disown in isolation.
28022
- *
28023
- * The disown is bounded by `backlogBefore`, which is what keeps it from
28024
- * touching a marker the NEW deployment's OWN live path has already set: B's
28025
- * live path can mark a capture owed from the moment `aka attach` writes the
28026
- * descriptor, before the drain's first pass ever reaches this method, and
28027
- * such a row sits at or after the bound rather than below it. What keeps the
28028
- * disown from eating THIS SAME CALL's own re-mark is the order, not the
28029
- * bound — disown runs first, re-mark second, both inside the one
28030
- * transaction above.
28031
- */
28032
- rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
28033
- this.ensureRowStmt.run();
28034
- withTransaction(
28035
- this.db,
28036
- () => {
28037
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28038
- this.rearmStmt.run();
28039
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28040
- this.disownCapturesStmt.run({ attachedAt: backlogBefore });
28041
- }
28042
- if (backfillCapturesBefore !== void 0) {
28043
- this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
28044
- }
28045
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28046
- },
28047
- "IMMEDIATE"
28048
- );
28049
- }
28050
- /**
28051
- * End the attached period: hand its rows to the live path, and release the
28052
- * boundary so the next attachment can freeze a new one.
28053
- *
28054
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28055
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28056
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28057
- * during the detached period, because the machine is not attached. Rows
28058
- * recorded in that window sit after the boundary and before the re-attach, so
28059
- * neither path takes them, and the pending count reports none outstanding.
28060
- *
28061
- * Stamping the attached window is not a claim that every one of those rows
28062
- * reached the deployment — the live path drops on failure and says so
28063
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28064
- * status quo: they sit outside the frozen boundary today and are equally never
28065
- * re-sent. Making it explicit is what lets the boundary move.
28066
- *
28067
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28068
- * window unstamped — that half-state would re-send the whole attached period
28069
- * on the next attach, which is the failure the boundary exists to prevent.
28070
- */
28071
- closeAttachedWindow(attachedAtMs, atMs) {
28072
- this.ensureRowStmt.run();
28073
- withTransaction(
28074
- this.db,
28075
- () => {
28076
- const row = getRow(this.fingerprintStmt);
28077
- const from = row?.backlogBefore ?? attachedAtMs;
28078
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28079
- this.releaseBoundaryStmt.run();
28080
- },
28081
- "IMMEDIATE"
28082
- );
28083
- }
28084
- /**
28085
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28086
- *
28087
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28088
- * different deployment and therefore discards what was delivered to the old
28089
- * one: here the recipient is the same, so everything already sent to it stays
28090
- * sent.
28091
- */
28092
- freezeBoundary(backlogBefore) {
28093
- this.ensureRowStmt.run();
28094
- this.freezeBoundaryStmt.run({ backlogBefore });
28095
- }
28096
- /** Take the claim, or report that someone live already holds it. */
28097
- claim(pid, host, nowMs, staleAfterMs) {
28098
- this.ensureRowStmt.run();
28099
- let taken = false;
28100
- withTransaction(
28101
- this.db,
28102
- () => {
28103
- const result = this.claimStmt.run({
28104
- pid,
28105
- host,
28106
- now: nowMs,
28107
- staleBefore: nowMs - staleAfterMs
28108
- });
28109
- taken = result.changes === 1;
28110
- },
28111
- "IMMEDIATE"
28112
- );
28113
- return taken;
28114
- }
28115
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28116
- heartbeat(pid, nowMs) {
28117
- this.heartbeatStmt.run({ now: nowMs, pid });
28118
- }
28119
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28120
- release(pid) {
28121
- this.releaseStmt.run({ pid });
28122
- }
28123
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28124
- lease() {
28125
- return getRow(this.leaseStmt);
28126
- }
28127
- };
28128
-
28129
28735
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28130
28736
  var SqliteInspectionDefinitionsRepository = class {
28131
28737
  constructor(db) {
@@ -28352,6 +28958,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28352
28958
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28353
28959
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28354
28960
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
28961
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28355
28962
  if (values.vaultConsent !== void 0) {
28356
28963
  merged.vaultConsent = values.vaultConsent ? (
28357
28964
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30859,7 +31466,7 @@ var SqliteSecurityRepository = class {
30859
31466
  ELSE 0
30860
31467
  END) AS open_at_rest
30861
31468
  FROM inspection_findings f
30862
- JOIN audit_events e ON e.id = f.audit_event_id
31469
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30863
31470
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30864
31471
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30865
31472
  ON latest.finding_key = f.finding_key
@@ -31085,7 +31692,7 @@ var SqliteSecurityRepository = class {
31085
31692
  this.db.prepare(
31086
31693
  `SELECT e.repo AS repo, count(*) AS c
31087
31694
  FROM inspection_findings f
31088
- JOIN audit_events e ON e.id = f.audit_event_id
31695
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31089
31696
  WHERE e.started_at >= :from AND e.started_at < :to
31090
31697
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31091
31698
  AND e.repo IS NOT NULL
@@ -31209,7 +31816,7 @@ var SqliteSecurityRepository = class {
31209
31816
  d.severity AS severity,
31210
31817
  COUNT(*) AS count
31211
31818
  FROM inspection_findings f
31212
- JOIN audit_events e ON e.id = f.audit_event_id
31819
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31213
31820
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31214
31821
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31215
31822
  ON latest.finding_key = f.finding_key
@@ -31244,7 +31851,7 @@ var SqliteSecurityRepository = class {
31244
31851
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31245
31852
  d.rule_id AS rule_id, d.category AS category
31246
31853
  FROM inspection_findings f
31247
- JOIN audit_events e ON e.id = f.audit_event_id
31854
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31248
31855
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31249
31856
  WHERE e.started_at >= :from AND e.started_at < :to
31250
31857
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32085,6 +32692,7 @@ function openWithPragmas(file2) {
32085
32692
  db.exec("PRAGMA journal_mode = WAL");
32086
32693
  db.exec("PRAGMA busy_timeout = 2000");
32087
32694
  db.exec("PRAGMA foreign_keys = ON");
32695
+ registerSqlFunctions(db);
32088
32696
  } catch (err) {
32089
32697
  closeQuietly(db);
32090
32698
  throw err;
@@ -32114,7 +32722,7 @@ function backupLegacyStore(db, file2) {
32114
32722
  discardStore(file2, backup);
32115
32723
  return backup;
32116
32724
  }
32117
- function openAndInitialize(file2, base) {
32725
+ function openAndInitialize(file2, base, skipTags) {
32118
32726
  let db = openWithPragmas(file2);
32119
32727
  try {
32120
32728
  if (isForeignSqliteLineage(db)) {
@@ -32124,7 +32732,7 @@ function openAndInitialize(file2, base) {
32124
32732
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32125
32733
  );
32126
32734
  }
32127
- applyMigrations(db, file2);
32735
+ applyMigrations(db, file2, { skipTags });
32128
32736
  tightenPerms(file2);
32129
32737
  const policies = new SqlitePoliciesRepository(db);
32130
32738
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32139,6 +32747,7 @@ function openAndInitialize(file2, base) {
32139
32747
  exceptions: new SqliteExceptionsRepository(db),
32140
32748
  resolutions: new SqliteResolutionsRepository(db),
32141
32749
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32750
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32142
32751
  security: new SqliteSecurityRepository(db),
32143
32752
  detections: new SqliteDetectionsRepository(db),
32144
32753
  shares: new SqliteSharesRepository(db),
@@ -32161,7 +32770,8 @@ function openAndInitialize(file2, base) {
32161
32770
  throw err;
32162
32771
  }
32163
32772
  }
32164
- function openLocalDatabase(dir) {
32773
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32774
+ function openLocalDatabase(dir, options = {}) {
32165
32775
  ensureDataDirSync(dir);
32166
32776
  const file2 = join7(dir, DB_FILENAME);
32167
32777
  reapStalePartials(file2);
@@ -32173,6 +32783,7 @@ function openLocalDatabase(dir) {
32173
32783
  installedPacks,
32174
32784
  scanLedger,
32175
32785
  historySync,
32786
+ bodyRetention,
32176
32787
  secretVault,
32177
32788
  exceptions,
32178
32789
  resolutions,
@@ -32196,7 +32807,8 @@ function openLocalDatabase(dir) {
32196
32807
  // `dir` is always `<base>/data` — every caller resolves it through
32197
32808
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32198
32809
  // settings/ and data/, and the pack-policy floor needs both halves.
32199
- dirname2(dir)
32810
+ dirname2(dir),
32811
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32200
32812
  );
32201
32813
  function captureRowId(event) {
32202
32814
  return captureId(
@@ -32389,6 +33001,7 @@ function openLocalDatabase(dir) {
32389
33001
  installedPacks,
32390
33002
  scanLedger,
32391
33003
  historySync,
33004
+ bodyRetention,
32392
33005
  secretVault,
32393
33006
  exceptions,
32394
33007
  resolutions,
@@ -32429,6 +33042,7 @@ function openLocalDatabase(dir) {
32429
33042
 
32430
33043
  // ../../packages/persistence/src/egress-wire.ts
32431
33044
  import { createHash as createHash3 } from "crypto";
33045
+ var SLASH = "/".charCodeAt(0);
32432
33046
 
32433
33047
  // ../../packages/persistence/src/exception-policy.ts
32434
33048
  var UserGrantPolicyProvider = class {
@@ -32576,18 +33190,26 @@ function fingerprintValue(key, raw) {
32576
33190
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32577
33191
  }
32578
33192
 
33193
+ // ../../packages/persistence/src/forward-health.ts
33194
+ import { readFileSync as readFileSync7 } from "fs";
33195
+ import { join as join9 } from "path";
33196
+
32579
33197
  // ../../packages/persistence/src/history-backfill.ts
32580
33198
  import { existsSync as existsSync4 } from "fs";
32581
- import { join as join9 } from "path";
33199
+ import { join as join10 } from "path";
32582
33200
 
32583
33201
  // ../../packages/persistence/src/history-preview.ts
32584
33202
  import { existsSync as existsSync5 } from "fs";
32585
- import { join as join10 } from "path";
33203
+ import { join as join11 } from "path";
32586
33204
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32587
33205
 
33206
+ // ../../packages/persistence/src/history-sync-state.ts
33207
+ import { readFileSync as readFileSync8 } from "fs";
33208
+ import { join as join12 } from "path";
33209
+
32588
33210
  // ../../packages/persistence/src/store-symlinks.ts
32589
33211
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32590
- import { dirname as dirname3, join as join11, resolve } from "path";
33212
+ import { dirname as dirname3, join as join13, resolve } from "path";
32591
33213
 
32592
33214
  // ../../packages/persistence/src/vault/crypto.ts
32593
33215
  import {
@@ -32700,8 +33322,8 @@ function formatPointer(category, keyVersion, pointerId, tag) {
32700
33322
  // ../../packages/persistence/src/vault/key-provider.ts
32701
33323
  import { execFileSync } from "child_process";
32702
33324
  import { randomBytes as randomBytes2 } from "crypto";
32703
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32704
- import { join as join12 } from "path";
33325
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33326
+ import { join as join14 } from "path";
32705
33327
  var VAULT_OCCUPANT_REASON = {
32706
33328
  symlink: "the path is a symlink; remove it so a keyring can be created",
32707
33329
  gone: "the path was occupied but holds no keyring (removed while it was being created)",
@@ -32800,7 +33422,7 @@ function claimRotationLock(lock, owner) {
32800
33422
  throw asError(err);
32801
33423
  }
32802
33424
  try {
32803
- writeFileSync3(join12(lock, LOCK_OWNER_FILE), `${owner}
33425
+ writeFileSync3(join14(lock, LOCK_OWNER_FILE), `${owner}
32804
33426
  `, { mode: DATA_FILE_MODE });
32805
33427
  return true;
32806
33428
  } catch (err) {
@@ -32809,7 +33431,7 @@ function claimRotationLock(lock, owner) {
32809
33431
  }
32810
33432
  }
32811
33433
  function acquireRotationLock(keysDir2) {
32812
- const lock = join12(keysDir2, `${VAULT_KEY_FILENAME}.lock`);
33434
+ const lock = join14(keysDir2, `${VAULT_KEY_FILENAME}.lock`);
32813
33435
  const owner = randomBytes2(16).toString("hex");
32814
33436
  if (claimRotationLock(lock, owner)) return { lock, owner };
32815
33437
  let held;
@@ -32836,7 +33458,7 @@ function acquireRotationLock(keysDir2) {
32836
33458
  }
32837
33459
  function releaseRotationLock(lease) {
32838
33460
  try {
32839
- if (readFileSync7(join12(lease.lock, LOCK_OWNER_FILE), "utf8").trim() !== lease.owner) return;
33461
+ if (readFileSync9(join14(lease.lock, LOCK_OWNER_FILE), "utf8").trim() !== lease.owner) return;
32840
33462
  } catch {
32841
33463
  return;
32842
33464
  }
@@ -32857,7 +33479,7 @@ var FileKeyProvider = class {
32857
33479
  this.#keysDir = keysDir2;
32858
33480
  }
32859
33481
  get filePath() {
32860
- return join12(this.#keysDir, VAULT_KEY_FILENAME);
33482
+ return join14(this.#keysDir, VAULT_KEY_FILENAME);
32861
33483
  }
32862
33484
  loadOrCreate() {
32863
33485
  return asAsync(() => {
@@ -32887,7 +33509,7 @@ var FileKeyProvider = class {
32887
33509
  #read() {
32888
33510
  let raw;
32889
33511
  try {
32890
- raw = readFileSync7(this.filePath, "utf8");
33512
+ raw = readFileSync9(this.filePath, "utf8");
32891
33513
  } catch (err) {
32892
33514
  if (err.code === "ENOENT") return null;
32893
33515
  throw err instanceof Error ? err : new Error(String(err));
@@ -33523,7 +34145,7 @@ var SecretVault = class {
33523
34145
 
33524
34146
  // ../../packages/persistence/src/warn-era-cap.ts
33525
34147
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
33526
- import { join as join13 } from "path";
34148
+ import { join as join15 } from "path";
33527
34149
 
33528
34150
  // ../../packages/plugin-sdk/src/provider-env.ts
33529
34151
  var DEFAULT_ANTHROPIC_HOST = "api.anthropic.com";
@@ -33577,7 +34199,7 @@ function resolveProvider() {
33577
34199
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
33578
34200
  try {
33579
34201
  ensureLayoutDirSync(base);
33580
- const settingsFile = join14(settingsDir(base), "settings.json");
34202
+ const settingsFile = join16(settingsDir(base), "settings.json");
33581
34203
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
33582
34204
  } catch {
33583
34205
  }
@@ -33601,9 +34223,9 @@ function resolveProviderSafe(resolveProviderFn) {
33601
34223
  }
33602
34224
 
33603
34225
  // ../../packages/plugin-sdk/src/config-inventory.ts
33604
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
34226
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33605
34227
  import { homedir as homedir2 } from "os";
33606
- import { basename as basename3, join as join16 } from "path";
34228
+ import { basename as basename3, join as join18 } from "path";
33607
34229
 
33608
34230
  // ../../packages/detections/src/egress/registry.ts
33609
34231
  var EXTRACTOR_VERSION = "1";
@@ -36561,8 +37183,8 @@ function registerBundledPacks() {
36561
37183
  }
36562
37184
 
36563
37185
  // ../../packages/plugin-sdk/src/repo.ts
36564
- import { existsSync as existsSync9, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
36565
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
37186
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
37187
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
36566
37188
 
36567
37189
  // ../../packages/plugin-sdk/src/events.ts
36568
37190
  import { createHash as createHash5, randomUUID as randomUUID13 } from "crypto";
@@ -36573,8 +37195,8 @@ import { fileURLToPath } from "url";
36573
37195
  import { Worker } from "worker_threads";
36574
37196
 
36575
37197
  // ../../packages/plugin-sdk/src/host-floor.ts
36576
- import { readFileSync as readFileSync11 } from "fs";
36577
- import { join as join18 } from "path";
37198
+ import { readFileSync as readFileSync13 } from "fs";
37199
+ import { join as join20 } from "path";
36578
37200
 
36579
37201
  // ../../packages/plugin-sdk/src/model-governance.ts
36580
37202
  import {
@@ -36582,11 +37204,11 @@ import {
36582
37204
  fstatSync,
36583
37205
  mkdirSync as mkdirSync2,
36584
37206
  openSync as openSync2,
36585
- readFileSync as readFileSync10,
37207
+ readFileSync as readFileSync12,
36586
37208
  readSync,
36587
37209
  writeFileSync as writeFileSync5
36588
37210
  } from "fs";
36589
- import { join as join17 } from "path";
37211
+ import { join as join19 } from "path";
36590
37212
  var TAIL_BYTES = 256 * 1024;
36591
37213
 
36592
37214
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -36609,15 +37231,15 @@ var HOST_FLOORS = {
36609
37231
 
36610
37232
  // ../../packages/plugin-sdk/src/ignore-layers.ts
36611
37233
  var import_ignore = __toESM(require_ignore(), 1);
36612
- import { readFileSync as readFileSync12 } from "fs";
36613
- import { join as join19 } from "path";
37234
+ import { readFileSync as readFileSync14 } from "fs";
37235
+ import { join as join21 } from "path";
36614
37236
 
36615
37237
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
36616
37238
  import { arch, hostname as hostname4, platform, release } from "os";
36617
37239
 
36618
37240
  // ../../packages/plugin-sdk/src/nudge.ts
36619
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
36620
- import { join as join20 } from "path";
37241
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
37242
+ import { join as join22 } from "path";
36621
37243
 
36622
37244
  // ../../packages/plugin-sdk/src/paths.ts
36623
37245
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -36625,7 +37247,7 @@ import { basename as basename4, dirname as dirname5, sep as sep3 } from "path";
36625
37247
 
36626
37248
  // ../../packages/plugin-sdk/src/project-files.ts
36627
37249
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
36628
- import { basename as basename5, join as join21 } from "path";
37250
+ import { basename as basename5, join as join23 } from "path";
36629
37251
 
36630
37252
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
36631
37253
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -36661,7 +37283,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
36661
37283
 
36662
37284
  // ../../packages/plugin-sdk/src/throttle.ts
36663
37285
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
36664
- import { join as join22 } from "path";
37286
+ import { join as join24 } from "path";
36665
37287
 
36666
37288
  // ../../packages/plugin-sdk/src/tokenize.ts
36667
37289
  function redactedPlaceholder(category) {
@@ -36991,13 +37613,13 @@ function describePointerSafe(token) {
36991
37613
  import {
36992
37614
  mkdirSync as mkdirSync5,
36993
37615
  readdirSync as readdirSync5,
36994
- readFileSync as readFileSync14,
37616
+ readFileSync as readFileSync16,
36995
37617
  renameSync as renameSync5,
36996
37618
  rmSync as rmSync7,
36997
37619
  statSync as statSync9,
36998
37620
  writeFileSync as writeFileSync8
36999
37621
  } from "fs";
37000
- import { dirname as dirname6, join as join23 } from "path";
37622
+ import { dirname as dirname6, join as join25 } from "path";
37001
37623
  var EMPTY_CARRY = Object.freeze({
37002
37624
  tail: "",
37003
37625
  fence: null,
@@ -37194,7 +37816,7 @@ var CARRY_FILE_PREFIX = "display-carry";
37194
37816
  var STALE_CARRY_MS = 15 * 60 * 1e3;
37195
37817
  function carryFilePath(dataDir2, sessionId) {
37196
37818
  const safe = sessionId.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 80);
37197
- return join23(dataDir2, `${CARRY_FILE_PREFIX}-${safe === "" ? "session" : safe}.json`);
37819
+ return join25(dataDir2, `${CARRY_FILE_PREFIX}-${safe === "" ? "session" : safe}.json`);
37198
37820
  }
37199
37821
  function parseFence(value) {
37200
37822
  if (typeof value !== "object" || value === null) return null;
@@ -37205,7 +37827,7 @@ function parseFence(value) {
37205
37827
  }
37206
37828
  function loadCarry(file2, keys) {
37207
37829
  try {
37208
- const parsed = JSON.parse(readFileSync14(file2, "utf8"));
37830
+ const parsed = JSON.parse(readFileSync16(file2, "utf8"));
37209
37831
  if (typeof parsed !== "object" || parsed === null) return EMPTY_CARRY;
37210
37832
  const record2 = parsed;
37211
37833
  const revealedCount = record2.messageKey === keys.messageKey && typeof record2.revealedCount === "number" && Number.isFinite(record2.revealedCount) ? record2.revealedCount : 0;
@@ -37258,7 +37880,7 @@ function removeStaleCarryFiles(dir, keep) {
37258
37880
  const cutoff = Date.now() - STALE_CARRY_MS;
37259
37881
  for (const name of readdirSync5(dir)) {
37260
37882
  if (!name.startsWith(CARRY_FILE_PREFIX) || !name.endsWith(".json")) continue;
37261
- const path = join23(dir, name);
37883
+ const path = join25(dir, name);
37262
37884
  if (path === keep) continue;
37263
37885
  try {
37264
37886
  if (statSync9(path).mtimeMs < cutoff) rmSync7(path, { force: true });
@@ -37289,7 +37911,7 @@ function finalizeCarry(file2, keys, carry) {
37289
37911
  }
37290
37912
  let owned = true;
37291
37913
  try {
37292
- const parsed = JSON.parse(readFileSync14(file2, "utf8"));
37914
+ const parsed = JSON.parse(readFileSync16(file2, "utf8"));
37293
37915
  if (typeof parsed === "object" && parsed !== null) {
37294
37916
  const record2 = parsed;
37295
37917
  owned = record2.blockKey === keys.blockKey || record2.messageKey === keys.messageKey;