@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 SLASH2 = "/";
53
+ var SLASH3 = "/";
54
54
  var TMP_KEY_IGNORE = "node-ignore";
55
55
  if (typeof Symbol !== "undefined") {
56
56
  TMP_KEY_IGNORE = /* @__PURE__ */ Symbol.for("node-ignore");
@@ -422,11 +422,11 @@ var require_ignore = __commonJS({
422
422
  if (!REGEX_TEST_TRAILING_SLASH.test(path)) {
423
423
  return this.test(path);
424
424
  }
425
- const slices = path.split(SLASH2).filter(Boolean);
425
+ const slices = path.split(SLASH3).filter(Boolean);
426
426
  slices.pop();
427
427
  if (slices.length) {
428
428
  const parent = this._t(
429
- slices.join(SLASH2) + SLASH2,
429
+ slices.join(SLASH3) + SLASH3,
430
430
  this._testCache,
431
431
  true,
432
432
  slices
@@ -442,14 +442,14 @@ var require_ignore = __commonJS({
442
442
  return cache[path];
443
443
  }
444
444
  if (!slices) {
445
- slices = path.split(SLASH2).filter(Boolean);
445
+ slices = path.split(SLASH3).filter(Boolean);
446
446
  }
447
447
  slices.pop();
448
448
  if (!slices.length) {
449
449
  return cache[path] = this._rules.test(path, checkUnignored, MODE_IGNORE);
450
450
  }
451
451
  const parent = this._t(
452
- slices.join(SLASH2) + SLASH2,
452
+ slices.join(SLASH3) + SLASH3,
453
453
  cache,
454
454
  checkUnignored,
455
455
  slices
@@ -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";
@@ -506,6 +506,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
506
506
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
507
507
  import { join as join2 } from "path";
508
508
 
509
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
510
+ var DEFERRED_MIGRATION_TAGS = [
511
+ "0031_audit_capture_by_time_index",
512
+ "0032_audit_capture_by_id_index",
513
+ "0033_audit_capture_location_index",
514
+ "0034_findings_read_indexes"
515
+ ];
516
+
509
517
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
510
518
  var SQLITE_MIGRATIONS = [
511
519
  {
@@ -623,6 +631,30 @@ var SQLITE_MIGRATIONS = [
623
631
  {
624
632
  tag: "0028_activity_session_probe_indexes",
625
633
  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"
634
+ },
635
+ {
636
+ tag: "0029_audit_capture_rollup_index",
637
+ 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');"
638
+ },
639
+ {
640
+ tag: "0030_audit_content_expiry",
641
+ 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;"
642
+ },
643
+ {
644
+ tag: "0031_audit_capture_by_time_index",
645
+ 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');"
646
+ },
647
+ {
648
+ tag: "0032_audit_capture_by_id_index",
649
+ 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');"
650
+ },
651
+ {
652
+ tag: "0033_audit_capture_location_index",
653
+ 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');"
654
+ },
655
+ {
656
+ tag: "0034_findings_read_indexes",
657
+ 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`);"
626
658
  }
627
659
  ];
628
660
 
@@ -20670,6 +20702,15 @@ var FindingCategory = external_exports.enum([
20670
20702
  ]).meta({ id: "FindingCategory" });
20671
20703
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20672
20704
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20705
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20706
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20707
+ var FindingDelivery = external_exports.object({
20708
+ state: FindingDeliveryState,
20709
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20710
+ at: external_exports.iso.datetime().optional(),
20711
+ // Only on `not_sent`, and only when a known reason was recorded.
20712
+ reason: SyncFailureReason.optional()
20713
+ }).meta({ id: "FindingDelivery" });
20673
20714
  var ResolutionMethod = external_exports.enum([
20674
20715
  "enforced-in-flight",
20675
20716
  "fixed-at-source",
@@ -20726,7 +20767,10 @@ var FindingInstance = external_exports.object({
20726
20767
  // The session that event belongs to, when it has one — the seam a
20727
20768
  // per-instance "view session" link needs. Absent for events captured
20728
20769
  // outside a session.
20729
- sessionId: external_exports.string().optional()
20770
+ sessionId: external_exports.string().optional(),
20771
+ // The delivery state of the event above (see FindingDelivery). Optional so
20772
+ // readers that do not project it stay valid.
20773
+ delivery: FindingDelivery.optional()
20730
20774
  }).meta({ id: "FindingInstance" });
20731
20775
  var FindingGroup = external_exports.object({
20732
20776
  id: external_exports.string(),
@@ -20778,7 +20822,10 @@ var FindingFacets = external_exports.object({
20778
20822
  // Host tool (attributes.tool_name). Present only on the instance-level
20779
20823
  // reads, which can filter by it; the type-level read omits the dimension
20780
20824
  // because a group spans tools.
20781
- tool: external_exports.array(FindingFacetItem).optional()
20825
+ tool: external_exports.array(FindingFacetItem).optional(),
20826
+ // Delivery states (FindingDeliveryState). Present only on the
20827
+ // instance-level reads, like `tool`.
20828
+ deployment: external_exports.array(FindingFacetItem).optional()
20782
20829
  }).meta({ id: "FindingFacets" });
20783
20830
  var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20784
20831
  id: "FindingTypeSummary"
@@ -20889,6 +20936,8 @@ var ListFindingInstancesQuery = external_exports.object({
20889
20936
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20890
20937
  // where the free-text `q` can only match the rendered "via Bash" label.
20891
20938
  tool: external_exports.array(external_exports.string()).optional(),
20939
+ // The delivery state of each finding's event (see FindingDelivery).
20940
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20892
20941
  // Exact repository / file-path matches, for the drill-down out of the
20893
20942
  // locations view. A row whose event carries no repo/file matches neither.
20894
20943
  repo: external_exports.string().optional(),
@@ -20909,6 +20958,10 @@ var ListFindingInstancesResponse = external_exports.object({
20909
20958
  items: external_exports.array(FindingInstanceDetail),
20910
20959
  nextCursor: external_exports.string().nullable()
20911
20960
  }).meta({ id: "ListFindingInstancesResponse" });
20961
+ var ListFindingInstancesPage = external_exports.object({
20962
+ items: external_exports.array(FindingInstanceDetail),
20963
+ nextCursor: external_exports.string().nullable()
20964
+ }).meta({ id: "ListFindingInstancesPage" });
20912
20965
  var FindingLocationSummary = external_exports.object({
20913
20966
  // Opaque, stable, minted from the pair by encodeLocationId. It exists
20914
20967
  // because a location's identity is two values and a URL param carries one:
@@ -20951,6 +21004,8 @@ var ListFindingLocationsQuery = external_exports.object({
20951
21004
  // instances that match, and folds its status from those.
20952
21005
  status: external_exports.array(FindingStatus).optional(),
20953
21006
  tool: external_exports.array(external_exports.string()).optional(),
21007
+ // The delivery state of each finding's event (see FindingDelivery).
21008
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20954
21009
  q: external_exports.string().optional(),
20955
21010
  sessionId: external_exports.string().optional(),
20956
21011
  from: external_exports.iso.datetime().optional(),
@@ -21153,6 +21208,10 @@ var CaptureAttributes = external_exports.object({
21153
21208
  // to 'allow' — the enforcement audit trail's link back to the grant that
21154
21209
  // authorized the bypass.
21155
21210
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21211
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21212
+ // join back to the `llm_call` leaf for the same assistant turn.
21213
+ message_id: external_exports.string().optional(),
21214
+ conversation_id: external_exports.string().optional(),
21156
21215
  // Whole milliseconds this capture's inspection blocked its caller — the
21157
21216
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21158
21217
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21161,7 +21220,19 @@ var CaptureAttributes = external_exports.object({
21161
21220
  // inline json_extract and is not itself an optimization.
21162
21221
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21163
21222
  // before the measurement shipped — never present as a placeholder 0.
21164
- inspection_ms: external_exports.number().int().nonnegative().optional()
21223
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21224
+ // What a `redact` this capture could not carry out became instead (see
21225
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21226
+ // degrade actually happened, so absence is the ordinary case rather than a
21227
+ // reader having to distinguish it from a zero.
21228
+ //
21229
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21230
+ // so on a multi-finding row this does not say which finding degraded, and
21231
+ // its presence does not mean the fallback decided the capture's action. A
21232
+ // capture denied by another finding's own Block policy carries `block`
21233
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21234
+ // repeated rather than referenced because a store reader opens this file.
21235
+ redact_degraded_to: ActionTaken.optional()
21165
21236
  }).catchall(external_exports.unknown());
21166
21237
  var ToolCallInspection = external_exports.object({
21167
21238
  ruleId: external_exports.string().min(1),
@@ -21360,7 +21431,17 @@ var AuditEvent = external_exports.object({
21360
21431
  /** `share` to a first-party/internal destination. */
21361
21432
  internal: external_exports.boolean(),
21362
21433
  /** Event needs review (e.g. unverified egress). */
21363
- flagged: external_exports.boolean()
21434
+ flagged: external_exports.boolean(),
21435
+ /**
21436
+ * The body this event's `title` is drawn from was cleared by local body
21437
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21438
+ *
21439
+ * A separate flag rather than a sentinel written into `title`: the title is
21440
+ * rendered text, and a store-layer module that invented display copy for it
21441
+ * would be choosing words the view is supposed to choose. Additive and
21442
+ * defaulted, so an older producer still validates.
21443
+ */
21444
+ bodyExpired: external_exports.boolean().default(false)
21364
21445
  }).meta({ id: "ActivityAuditEvent" });
21365
21446
  var ActivitySessionSummary = external_exports.object({
21366
21447
  id: external_exports.string(),
@@ -22701,6 +22782,12 @@ var EventMetadata = external_exports.object({
22701
22782
  // to 'allow' — the enforcement audit trail's link back to the grant that
22702
22783
  // authorized the bypass. Absent on captures where no exception applied.
22703
22784
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22785
+ // The assistant message this capture belongs to, and the conversation it sits
22786
+ // in — set by the browser extension's network capture so a stored `response`
22787
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22788
+ // on every other capture path, which has no such id.
22789
+ messageId: external_exports.string().optional(),
22790
+ conversationId: external_exports.string().optional(),
22704
22791
  // How long THIS capture's inspection blocked its caller, in whole
22705
22792
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22706
22793
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22713,7 +22800,37 @@ var EventMetadata = external_exports.object({
22713
22800
  // Absent is also what every pre-measurement client writes, and what a
22714
22801
  // clock failure degrades to — a reader must treat absence as "not measured"
22715
22802
  // and never as a zero, which would read as "inspection is free".
22716
- inspectionMs: external_exports.number().int().nonnegative().optional()
22803
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22804
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22805
+ // workspace's `redactFallback`, applied because the field could not be
22806
+ // masked in place (a shell command, a URL, or any argument on a host whose
22807
+ // hook contract offers no rewrite channel).
22808
+ //
22809
+ // It exists because the action alone cannot say why. A finding recorded as
22810
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22811
+ // assigned Redact on a field that could not take one — and those are
22812
+ // different facts about the same row: the first is a policy the user chose,
22813
+ // the second is a masking the host could not perform. Absent means no
22814
+ // degrade happened, which is every ordinary capture.
22815
+ //
22816
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22817
+ // is the CAPTURE while `actionTaken` is per FINDING:
22818
+ //
22819
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22820
+ // `redact` alongside a finding ASSIGNED the same action stores both
22821
+ // identically and one reason for the pair; attributing it to both
22822
+ // describes the assigned one wrongly, and to neither loses the degrade.
22823
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22824
+ // became, not the reason the capture ended as it did — a capture denied
22825
+ // by some other finding's own Block policy still carries `block` here,
22826
+ // and clearing the workspace's fallback would not have let it through.
22827
+ // Gate on the value against what a fallback can produce; never read the
22828
+ // field's presence as "this was the fallback's doing".
22829
+ //
22830
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22831
+ // Closing either means moving the reason onto the finding row, which
22832
+ // already carries its own action.
22833
+ redactDegradedTo: ActionTaken.optional()
22717
22834
  }).meta({ id: "EventMetadata" });
22718
22835
  var Event = external_exports.object({
22719
22836
  id: external_exports.guid(),
@@ -22823,7 +22940,32 @@ var RotateKeyInput = external_exports.object({
22823
22940
  confirmation: external_exports.string()
22824
22941
  });
22825
22942
 
22943
+ // ../../packages/schema/src/zod/finding-delivery.ts
22944
+ var KNOWN_REASONS = SyncFailureReason.options;
22945
+ function knownReason(value) {
22946
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22947
+ }
22948
+ function deriveFindingDelivery(row) {
22949
+ if (row.kind === "code_change") return { state: "local_scan" };
22950
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22951
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22952
+ }
22953
+ if (row.syncedAt !== null) {
22954
+ const reason = knownReason(row.syncFailure);
22955
+ return {
22956
+ state: "not_sent",
22957
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22958
+ ...reason === void 0 ? {} : { reason }
22959
+ };
22960
+ }
22961
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22962
+ return { state: "never_offered" };
22963
+ }
22964
+
22826
22965
  // ../../packages/schema/src/zod/findings-group-build.ts
22966
+ function lookupOwn(map2, key) {
22967
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22968
+ }
22827
22969
  function toApiAction(dbVal) {
22828
22970
  const map2 = {
22829
22971
  log: "monitored",
@@ -22832,7 +22974,7 @@ function toApiAction(dbVal) {
22832
22974
  warn: "warned",
22833
22975
  allow: "allowed"
22834
22976
  };
22835
- return map2[dbVal] ?? "allowed";
22977
+ return lookupOwn(map2, dbVal) ?? "allowed";
22836
22978
  }
22837
22979
  function toApiCategory(dbVal) {
22838
22980
  if (dbVal === "code_context") return "source_code";
@@ -22840,13 +22982,18 @@ function toApiCategory(dbVal) {
22840
22982
  return parsed2.success ? parsed2.data : "custom";
22841
22983
  }
22842
22984
  function toApiProvider(sourceTool) {
22843
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22985
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22844
22986
  }
22845
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22987
+ var FINDING_STATUS_PRECEDENCE = [
22988
+ "open",
22989
+ "handled",
22990
+ "dismissed",
22991
+ "resolved"
22992
+ ];
22846
22993
  function foldGroupStatus(instanceStatuses) {
22847
22994
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22848
22995
  if (statuses.size === 0) return void 0;
22849
- for (const candidate of STATUS_PRECEDENCE) {
22996
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22850
22997
  if (statuses.has(candidate)) return candidate;
22851
22998
  }
22852
22999
  return void 0;
@@ -22953,11 +23100,16 @@ function applyFindingFilters(types, opts) {
22953
23100
  }
22954
23101
  return filtered;
22955
23102
  }
22956
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22957
- var SEVERITY_RANK = SEVERITY_ORDER;
23103
+ function rankByOrder(members2) {
23104
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23105
+ }
23106
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23107
+ function severityRank(severity) {
23108
+ return lookupOwn(SEVERITY_RANK, severity);
23109
+ }
22958
23110
  function compareFindingGroupOrder(a, b) {
22959
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22960
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23111
+ const rankA = severityRank(a.severity) ?? -1;
23112
+ const rankB = severityRank(b.severity) ?? -1;
22961
23113
  const severityDiff = rankA - rankB;
22962
23114
  if (severityDiff !== 0) return severityDiff;
22963
23115
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
@@ -23032,6 +23184,20 @@ function computeFindingFacets(allTypes, opts) {
23032
23184
  }
23033
23185
 
23034
23186
  // ../../packages/schema/src/zod/findings-flat-build.ts
23187
+ function compareCodePoints(a, b) {
23188
+ const aIter = a[Symbol.iterator]();
23189
+ const bIter = b[Symbol.iterator]();
23190
+ for (; ; ) {
23191
+ const aNext = aIter.next();
23192
+ const bNext = bIter.next();
23193
+ if (aNext.done && bNext.done) return 0;
23194
+ if (aNext.done) return -1;
23195
+ if (bNext.done) return 1;
23196
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23197
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23198
+ if (aPoint !== bPoint) return aPoint - bPoint;
23199
+ }
23200
+ }
23035
23201
  function rowHaystack(row) {
23036
23202
  return [
23037
23203
  row.ruleId,
@@ -23056,6 +23222,8 @@ function matchesDimension(row, opts, dimension) {
23056
23222
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23057
23223
  case "statuses":
23058
23224
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23225
+ case "deliveries":
23226
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23059
23227
  case "tools":
23060
23228
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23061
23229
  // An EMPTY value is a real filter here, not an absent one. The location
@@ -23082,6 +23250,7 @@ var DIMENSIONS = [
23082
23250
  "providers",
23083
23251
  "actions",
23084
23252
  "statuses",
23253
+ "deliveries",
23085
23254
  "tools",
23086
23255
  "repo",
23087
23256
  "file",
@@ -23095,10 +23264,19 @@ function matchesInstanceFilters(row, opts, except) {
23095
23264
  return true;
23096
23265
  }
23097
23266
  function toItems(counts) {
23098
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23267
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23268
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23269
+ // NFD spelling of the same text) as equal, so a count tie between
23270
+ // them would otherwise be ordered by whichever the Map iteration
23271
+ // produced. compareCodePoints breaks that tie deterministically, which
23272
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23273
+ // which it need not: foldFacetTuples runs this same sort over grouped
23274
+ // tuples, so both paths order facets identically by construction.
23275
+ compareCodePoints(a.value, b.value)
23276
+ );
23099
23277
  }
23100
- function bump(counts, value) {
23101
- counts.set(value, (counts.get(value) ?? 0) + 1);
23278
+ function bump(counts, value, by = 1) {
23279
+ counts.set(value, (counts.get(value) ?? 0) + by);
23102
23280
  }
23103
23281
  function createInstanceFacetAccumulator(opts) {
23104
23282
  const severity = /* @__PURE__ */ new Map();
@@ -23107,6 +23285,7 @@ function createInstanceFacetAccumulator(opts) {
23107
23285
  const action = /* @__PURE__ */ new Map();
23108
23286
  const status = /* @__PURE__ */ new Map();
23109
23287
  const tool = /* @__PURE__ */ new Map();
23288
+ const deployment = /* @__PURE__ */ new Map();
23110
23289
  return {
23111
23290
  add(row) {
23112
23291
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23121,6 +23300,9 @@ function createInstanceFacetAccumulator(opts) {
23121
23300
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23122
23301
  bump(tool, row.toolName);
23123
23302
  }
23303
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23304
+ bump(deployment, row.delivery.state);
23305
+ }
23124
23306
  },
23125
23307
  facets: () => ({
23126
23308
  severity: toItems(severity),
@@ -23128,7 +23310,8 @@ function createInstanceFacetAccumulator(opts) {
23128
23310
  provider: toItems(provider),
23129
23311
  action: toItems(action),
23130
23312
  status: toItems(status),
23131
- tool: toItems(tool)
23313
+ tool: toItems(tool),
23314
+ deployment: toItems(deployment)
23132
23315
  })
23133
23316
  };
23134
23317
  }
@@ -23142,6 +23325,7 @@ function toInstanceDetail(row) {
23142
23325
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23143
23326
  eventId: row.eventId,
23144
23327
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23328
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23145
23329
  ...row.user === void 0 ? {} : { user: row.user },
23146
23330
  action: toApiAction(row.actionTaken),
23147
23331
  detectedAt: row.occurredAt,
@@ -23156,12 +23340,6 @@ function toInstanceDetail(row) {
23156
23340
  policy: { id: `category:${category}`, name: category }
23157
23341
  };
23158
23342
  }
23159
- var SEVERITY_ORDER2 = {
23160
- critical: 0,
23161
- high: 1,
23162
- medium: 2,
23163
- low: 3
23164
- };
23165
23343
  function newLocationAccumulator() {
23166
23344
  return {
23167
23345
  instanceCount: 0,
@@ -23176,7 +23354,7 @@ function newLocationAccumulator() {
23176
23354
  }
23177
23355
  function addToLocation(acc, row) {
23178
23356
  acc.instanceCount += 1;
23179
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23357
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23180
23358
  if (rank < acc.maxSeverityRank) {
23181
23359
  acc.maxSeverityRank = rank;
23182
23360
  acc.maxSeverity = row.severity;
@@ -23186,15 +23364,15 @@ function addToLocation(acc, row) {
23186
23364
  acc.ruleIds.add(row.ruleId);
23187
23365
  }
23188
23366
  function compareLocationOrder(a, b) {
23189
- const rankA = SEVERITY_ORDER2[a.maxSeverity] ?? -1;
23190
- const rankB = SEVERITY_ORDER2[b.maxSeverity] ?? -1;
23367
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23368
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23191
23369
  if (rankA !== rankB) return rankA - rankB;
23192
23370
  if (a.latestDetectedAt !== b.latestDetectedAt) {
23193
23371
  return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23194
23372
  }
23195
- if (a.repo !== b.repo) return a.repo < b.repo ? -1 : 1;
23196
- if (a.file !== b.file) return a.file < b.file ? -1 : 1;
23197
- return 0;
23373
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23374
+ if (repoDiff !== 0) return repoDiff;
23375
+ return compareCodePoints(a.file, b.file);
23198
23376
  }
23199
23377
  function encodeLocationId(repo, file2) {
23200
23378
  return `${encodePart(repo)}/${encodePart(file2)}`;
@@ -23269,6 +23447,11 @@ var Policy = external_exports.object({
23269
23447
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23270
23448
  provenance: PolicyProvenance.optional()
23271
23449
  }).meta({ id: "Policy" });
23450
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23451
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23452
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23453
+ id: "RedactFallback"
23454
+ });
23272
23455
  var PolicyBundle = external_exports.object({
23273
23456
  version: external_exports.string(),
23274
23457
  policies: external_exports.array(Policy),
@@ -23316,6 +23499,16 @@ var PolicyBundle = external_exports.object({
23316
23499
  // control plane), so no name resolution stands between the decision and the
23317
23500
  // comparison.
23318
23501
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23502
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23503
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23504
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23505
+ // a control plane can tighten a machine and never loosen one — the same
23506
+ // direction `mergeRaiseOnly` enforces for policies.
23507
+ //
23508
+ // Optional so an older backend, and an older on-disk cache, still parses;
23509
+ // absent leaves the device's own setting in force, which is the behaviour
23510
+ // that predates the field and the safe direction to default.
23511
+ redactFallback: RedactFallback.optional(),
23319
23512
  customKeywords: external_exports.array(external_exports.string()),
23320
23513
  fetchedAt: external_exports.iso.datetime()
23321
23514
  }).meta({ id: "PolicyBundle" });
@@ -23345,11 +23538,6 @@ function severityFloorPolicy(category) {
23345
23538
  const peak = CATEGORY_PEAK_SEVERITY[category];
23346
23539
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23347
23540
  }
23348
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23349
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23350
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23351
- id: "RedactFallback"
23352
- });
23353
23541
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23354
23542
  var BUILTIN_POLICY_SPECS = {
23355
23543
  monitor: {
@@ -23405,6 +23593,11 @@ function isActionAtLeast(action, floor) {
23405
23593
  function strongerAction(a, b) {
23406
23594
  return actionRank(a) >= actionRank(b) ? a : b;
23407
23595
  }
23596
+ function strongerRedactFallback(local, remote) {
23597
+ if (remote === void 0) return local;
23598
+ const localAction = builtinPolicyToAction(local);
23599
+ return localAction === strongerAction(localAction, builtinPolicyToAction(remote)) ? local : remote;
23600
+ }
23408
23601
  function weakestBuiltinAtLeast(floor) {
23409
23602
  return KNOWN_BUILTIN_IDS.find((id) => isActionAtLeast(builtinPolicyToAction(id), floor)) ?? "block";
23410
23603
  }
@@ -23645,7 +23838,7 @@ var VaultConsent = external_exports.object({
23645
23838
  });
23646
23839
 
23647
23840
  // ../../packages/schema/src/zod/local.ts
23648
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23841
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23649
23842
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23650
23843
  var RunMode = external_exports.enum(["standalone", "attached"]);
23651
23844
  var ControlPlaneConnection = external_exports.object({
@@ -23665,6 +23858,15 @@ var HistorySyncConsent = external_exports.object({
23665
23858
  payloadVersion: external_exports.number().int().positive(),
23666
23859
  endpoint: external_exports.string()
23667
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" });
23668
23870
  var WorkspaceSettings = external_exports.object({
23669
23871
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23670
23872
  runMode: RunMode.default("standalone"),
@@ -23713,7 +23915,13 @@ var WorkspaceSettings = external_exports.object({
23713
23915
  // carry prompt/reply/tool-output text in `content`; the key name predates
23714
23916
  // both widenings. Absent until granted, and a grant for a different endpoint
23715
23917
  // or an older payload no longer counts.
23716
- 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
+ })
23717
23925
  });
23718
23926
  function defaultWorkspaceSettings() {
23719
23927
  return WorkspaceSettings.parse({});
@@ -23808,12 +24016,15 @@ function toCaptureAttributes(event) {
23808
24016
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23809
24017
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23810
24018
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24019
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23811
24020
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23812
24021
  // has ever populated either), but every legacy metadata key still rides
23813
24022
  // the bag rather than being silently dropped — CaptureAttributes'
23814
24023
  // `.catchall(z.unknown())` carries the long tail.
23815
24024
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23816
- ...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 } : {}
23817
24028
  };
23818
24029
  }
23819
24030
  function captureDefinitionVersion(finding) {
@@ -23841,13 +24052,22 @@ var ManagedSettingKey = external_exports.enum([
23841
24052
  "vaultInlineReveal",
23842
24053
  "modelJudgeConsent",
23843
24054
  "dataSharesInPlace",
23844
- "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"
23845
24060
  ]).meta({ id: "ManagedSettingKey" });
23846
24061
  function isManagedSettingKey(value) {
23847
24062
  return ManagedSettingKey.safeParse(value).success;
23848
24063
  }
23849
24064
  var ManagedSettingsValues = external_exports.object({
23850
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.
23851
24071
  controlPlane: external_exports.object({
23852
24072
  endpoint: external_exports.string().min(1),
23853
24073
  label: external_exports.string().min(1).optional()
@@ -23858,7 +24078,8 @@ var ManagedSettingsValues = external_exports.object({
23858
24078
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23859
24079
  modelJudgeConsent: external_exports.boolean().optional(),
23860
24080
  dataSharesInPlace: external_exports.boolean().optional(),
23861
- redactFallback: RedactFallback.optional()
24081
+ redactFallback: RedactFallback.optional(),
24082
+ bodyRetention: BodyRetention.optional()
23862
24083
  }).meta({ id: "ManagedSettingsValues" });
23863
24084
  var ManagedSettings = external_exports.object({
23864
24085
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23866,7 +24087,21 @@ var ManagedSettings = external_exports.object({
23866
24087
  // decision from a bug. Absent renders as a generic "your organization".
23867
24088
  organization: external_exports.string().min(1).optional(),
23868
24089
  // What the administrator pinned.
23869
- 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({}),
23870
24105
  // Which of those the user may not change. A key here with no matching value
23871
24106
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23872
24107
  // the user may still override. The two are separable on purpose.
@@ -23879,17 +24114,31 @@ var ManagedSettings = external_exports.object({
23879
24114
  // the fleets most likely to carry a version skew. A name outside the enum
23880
24115
  // is still never HONOURED: the lockable set stays explicit above.
23881
24116
  lockedFields: external_exports.array(external_exports.string()).default([])
23882
- }).transform(({ lockedFields, ...rest }) => {
24117
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
23883
24118
  const known = [];
23884
24119
  const unknown2 = [];
23885
24120
  for (const name of lockedFields) {
23886
24121
  if (isManagedSettingKey(name)) known.push(name);
23887
24122
  else unknown2.push(name);
23888
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
+ }
23889
24136
  return {
23890
24137
  ...rest,
24138
+ values: pinned.data,
23891
24139
  lockedFields: known,
23892
- ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {}
24140
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24141
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
23893
24142
  };
23894
24143
  }).meta({ id: "ManagedSettings" });
23895
24144
 
@@ -24153,7 +24402,23 @@ var SaveSettingsInput = external_exports.object({
24153
24402
  modelJudgeConsent: ModelJudgeConsentChoice,
24154
24403
  historySyncConsent: HistorySyncConsentChoice,
24155
24404
  vaultConsent: external_exports.string(),
24156
- 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() })
24157
24422
  });
24158
24423
  var AttachInput = external_exports.object({
24159
24424
  endpoint: external_exports.string(),
@@ -24325,6 +24590,52 @@ function reviewSeverityRank(reasons) {
24325
24590
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24326
24591
  }
24327
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
+
24328
24639
  // ../../packages/persistence/src/paths.ts
24329
24640
  import {
24330
24641
  chmodSync,
@@ -24685,6 +24996,22 @@ function discardStore(file2, backup) {
24685
24996
  }
24686
24997
  }
24687
24998
 
24999
+ // ../../packages/persistence/src/internal/sql-functions.ts
25000
+ var utf8 = new TextDecoder();
25001
+ function akaLower(value) {
25002
+ if (value === null) return null;
25003
+ if (typeof value === "string") return value.toLowerCase();
25004
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
25005
+ return utf8.decode(value).toLowerCase();
25006
+ }
25007
+ function registerSqlFunctions(db) {
25008
+ db.function(
25009
+ "aka_lower",
25010
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
25011
+ akaLower
25012
+ );
25013
+ }
25014
+
24688
25015
  // ../../packages/persistence/src/internal/sql-text.ts
24689
25016
  function escapeLikePattern(s) {
24690
25017
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24769,6 +25096,11 @@ function schemaObjectExists(db, kind, name) {
24769
25096
  function indexExists(db, name) {
24770
25097
  return schemaObjectExists(db, "index", name);
24771
25098
  }
25099
+ function indexColumns(db, name) {
25100
+ if (!indexExists(db, name)) return [];
25101
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25102
+ return columns.map((c) => c.name).filter((c) => c !== null);
25103
+ }
24772
25104
  function columnNames(db, table2, opts) {
24773
25105
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24774
25106
  const columns = db.prepare(`PRAGMA ${pragma}(${table2})`).all();
@@ -24830,176 +25162,818 @@ function mapRowsTolerant(rows, map2) {
24830
25162
  return out;
24831
25163
  }
24832
25164
 
24833
- // ../../packages/persistence/src/migrations.ts
24834
- function describeObject(object2) {
24835
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24836
- }
24837
- function splitStatements(sql) {
24838
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24839
- }
24840
- function createdIndexName(statement) {
24841
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24842
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25165
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25166
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25167
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25168
+
25169
+ // ../../packages/persistence/src/sync-failure.ts
25170
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25171
+ function syncFailureRejectCondition(column = "sync_failure") {
25172
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25173
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24843
25174
  }
24844
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24845
- function applyMigrations(db, file2) {
24846
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24847
- db.exec(
24848
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24849
- );
24850
- const applied = new Set(
24851
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24852
- );
24853
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24854
- const record2 = db.prepare(
24855
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24856
- );
24857
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24858
- if (applied.has(migration.tag)) continue;
24859
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24860
- const evidence = evidenceObjects(migration.sql);
24861
- const present = evidence.filter((o) => evidenceExists(db, o));
24862
- if (present.length > 0 && present.length < evidence.length) {
24863
- const missing = evidence.filter((o) => !present.includes(o));
24864
- 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.`;
24865
- akaWarn(message);
24866
- throw new Error(`[aka] ${message}`);
24867
- }
24868
- const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
24869
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24870
- const statements = splitStatements(migration.sql);
24871
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24872
- try {
24873
- withTransaction(
24874
- db,
24875
- () => {
24876
- for (const statement of statements) {
24877
- const indexName = createdIndexName(statement);
24878
- if (indexName === void 0) {
24879
- if (alreadyApplied) continue;
24880
- } else if (indexExists(db, indexName)) {
24881
- continue;
24882
- }
24883
- db.exec(statement);
24884
- }
24885
- if (wantsFkOff && !alreadyApplied) {
24886
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24887
- if (violations.length > 0) {
24888
- throw new Error(
24889
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24890
- );
24891
- }
24892
- }
24893
- record2.run(migration.tag, Date.now());
24894
- },
24895
- "IMMEDIATE"
24896
- );
24897
- } finally {
24898
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24899
- }
25175
+
25176
+ // ../../packages/persistence/src/repositories/history-sync.ts
25177
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25178
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25179
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25180
+ var COUNTED_EVENT_TYPES = [
25181
+ ...STRUCTURAL_EVENT_TYPES,
25182
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25183
+ ];
25184
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25185
+ var PARTITION_BUCKETS = `
25186
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25187
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25188
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25189
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25190
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25191
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25192
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25193
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25194
+ -- added later lands in no bucket and fails the sum assertion, instead
25195
+ -- of silently joining this one.
25196
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25197
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25198
+ THEN 1 ELSE 0 END) AS failed,
25199
+ COUNT(*) AS total`;
25200
+ var COUNTED_SCOPE = `
25201
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25202
+ AND (
25203
+ event_type IN (${TYPE_LIST})
25204
+ OR synced_at IS NOT NULL
25205
+ OR outbox_owed = 1
25206
+ )`;
25207
+ var SKIPPED = -1;
25208
+ var ROW_COLUMNS = `id,
25209
+ parent_id AS parentId,
25210
+ root_session_id AS rootSessionId,
25211
+ event_type AS eventType,
25212
+ host_id AS hostId,
25213
+ harness_id AS harnessId,
25214
+ source_project_id AS sourceProjectId,
25215
+ started_at AS startedAt,
25216
+ ended_at AS endedAt,
25217
+ severity,
25218
+ priority,
25219
+ content,
25220
+ content_hash AS contentHash,
25221
+ attributes`;
25222
+ var SqliteHistorySyncRepository = class {
25223
+ constructor(db) {
25224
+ this.db = db;
25225
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25226
+ this.sessionsStmt = db.prepare(
25227
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25228
+ FROM audit_events
25229
+ WHERE synced_at IS NULL
25230
+ AND event_type IN (${TYPE_LIST})
25231
+ AND started_at < :before
25232
+ GROUP BY sessionId
25233
+ ORDER BY earliest
25234
+ LIMIT :limit`
25235
+ );
25236
+ this.rowsStmt = db.prepare(
25237
+ `SELECT ${ROW_COLUMNS}
25238
+ FROM audit_events
25239
+ WHERE synced_at IS NULL
25240
+ AND event_type IN (${TYPE_LIST})
25241
+ AND started_at < :before
25242
+ AND COALESCE(root_session_id, id) = :sessionId
25243
+ ORDER BY (event_type = 'session') DESC, started_at
25244
+ LIMIT :limit`
25245
+ );
25246
+ this.captureRowsStmt = db.prepare(
25247
+ `SELECT ${ROW_COLUMNS}
25248
+ FROM audit_events
25249
+ WHERE synced_at IS NULL
25250
+ AND sync_claimed_at IS NULL
25251
+ AND outbox_owed = 1
25252
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25253
+ AND started_at < :before
25254
+ ORDER BY started_at
25255
+ LIMIT :limit`
25256
+ );
25257
+ this.markOwedStmt = db.prepare(
25258
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25259
+ );
25260
+ this.markCaptureBacklogOwedStmt = db.prepare(
25261
+ `UPDATE audit_events SET outbox_owed = 1
25262
+ WHERE synced_at IS NULL
25263
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25264
+ AND started_at < :before`
25265
+ );
25266
+ this.stampStmt = db.prepare(
25267
+ `UPDATE audit_events
25268
+ SET synced_at = :at,
25269
+ sync_claimed_at = NULL,
25270
+ sync_failed_at = :failedAt,
25271
+ sync_failure = :failure
25272
+ WHERE id = :id`
25273
+ );
25274
+ this.claimRowStmt = db.prepare(
25275
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25276
+ );
25277
+ this.releaseRowStmt = db.prepare(
25278
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25279
+ );
25280
+ this.releaseStaleClaimsStmt = db.prepare(
25281
+ `UPDATE audit_events SET sync_claimed_at = NULL
25282
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25283
+ );
25284
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25285
+ FROM audit_events${COUNTED_SCOPE}`);
25286
+ this.partitionByKindStmt = db.prepare(
25287
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25288
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25289
+ GROUP BY event_type`
25290
+ );
25291
+ this.countsStmt = db.prepare(
25292
+ `SELECT
25293
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25294
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25295
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25296
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25297
+ THEN 1 ELSE 0 END) AS skipped,
25298
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25299
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25300
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25301
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25302
+ FROM audit_events
25303
+ WHERE event_type IN (${TYPE_LIST})`
25304
+ );
25305
+ this.captureSkipCountStmt = db.prepare(
25306
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25307
+ // way the structural totals are. The split exists because a refusal is
25308
+ // terminal only against the deployment that gave it, and the structural
25309
+ // re-arm frees it on a change of deployment. The capture lane has no such
25310
+ // escape: re-arming a capture would offer one deployment's undelivered
25311
+ // prompts, with their text, to a deployment that never saw them, which is
25312
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25313
+ // reasons mean the same thing — this row will not be sent — and splitting
25314
+ // them would put refused captures in a bucket nothing reads and nothing
25315
+ // frees.
25316
+ `SELECT COUNT(*) AS skipped
25317
+ FROM audit_events
25318
+ WHERE synced_at = ${String(SKIPPED)}
25319
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25320
+ );
25321
+ this.fingerprintStmt = db.prepare(
25322
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25323
+ FROM history_sync WHERE id = 1`
25324
+ );
25325
+ this.setFingerprintStmt = db.prepare(
25326
+ `UPDATE history_sync
25327
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25328
+ WHERE id = 1`
25329
+ );
25330
+ this.disownCapturesStmt = db.prepare(
25331
+ `UPDATE audit_events SET outbox_owed = NULL
25332
+ WHERE outbox_owed IS NOT NULL
25333
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25334
+ AND started_at < :attachedAt`
25335
+ );
25336
+ this.rearmStmt = db.prepare(
25337
+ `UPDATE audit_events
25338
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25339
+ WHERE (synced_at > 0
25340
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25341
+ AND event_type IN (${TYPE_LIST})`
25342
+ );
25343
+ this.claimStmt = db.prepare(
25344
+ `UPDATE history_sync
25345
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25346
+ WHERE id = 1
25347
+ AND (owner_pid IS NULL
25348
+ OR heartbeat_at IS NULL
25349
+ OR heartbeat_at < :staleBefore
25350
+ OR heartbeat_at > :now)`
25351
+ );
25352
+ this.heartbeatStmt = db.prepare(
25353
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25354
+ );
25355
+ this.releaseStmt = db.prepare(
25356
+ `UPDATE history_sync
25357
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25358
+ WHERE id = 1 AND owner_pid = :pid`
25359
+ );
25360
+ this.closeWindowStmt = db.prepare(
25361
+ `UPDATE audit_events
25362
+ SET synced_at = ${String(SKIPPED)},
25363
+ sync_failed_at = :at,
25364
+ sync_failure = 'detached_undelivered'
25365
+ WHERE synced_at IS NULL
25366
+ AND event_type IN (${TYPE_LIST})
25367
+ AND started_at >= :attachedAt`
25368
+ );
25369
+ this.releaseBoundaryStmt = db.prepare(
25370
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25371
+ );
25372
+ this.freezeBoundaryStmt = db.prepare(
25373
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25374
+ );
25375
+ this.leaseStmt = db.prepare(
25376
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25377
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25378
+ FROM history_sync WHERE id = 1`
25379
+ );
25380
+ this.inspectionsStmt = db.prepare(
25381
+ `SELECT d.rule_id AS ruleId,
25382
+ d.name AS ruleName,
25383
+ d.version AS ruleVersion,
25384
+ d.category AS category,
25385
+ d.severity AS severity,
25386
+ f.span_start AS spanStart,
25387
+ f.span_end AS spanEnd,
25388
+ f.masked_match AS maskedMatch,
25389
+ f.action_taken AS actionTaken,
25390
+ f.confidence AS confidence
25391
+ FROM inspection_findings f
25392
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25393
+ WHERE f.audit_event_id = :auditEventId
25394
+ ORDER BY f.span_start, f.id`
25395
+ );
24900
25396
  }
24901
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24902
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25397
+ db;
25398
+ ensureRowStmt;
25399
+ sessionsStmt;
25400
+ rowsStmt;
25401
+ stampStmt;
25402
+ countsStmt;
25403
+ fingerprintStmt;
25404
+ setFingerprintStmt;
25405
+ rearmStmt;
25406
+ claimStmt;
25407
+ heartbeatStmt;
25408
+ releaseStmt;
25409
+ leaseStmt;
25410
+ inspectionsStmt;
25411
+ closeWindowStmt;
25412
+ releaseBoundaryStmt;
25413
+ freezeBoundaryStmt;
25414
+ captureRowsStmt;
25415
+ markOwedStmt;
25416
+ markCaptureBacklogOwedStmt;
25417
+ captureSkipCountStmt;
25418
+ disownCapturesStmt;
25419
+ partitionStmt;
25420
+ partitionByKindStmt;
25421
+ claimRowStmt;
25422
+ releaseRowStmt;
25423
+ releaseStaleClaimsStmt;
25424
+ /**
25425
+ * The masked detections recorded against one tool call.
25426
+ *
25427
+ * These travel with the event because a tool call's target is not
25428
+ * re-inspectable from the event alone — unlike a capture, where the text
25429
+ * itself is re-scannable. What crosses is the masked match and the rule that
25430
+ * produced it, never the value.
25431
+ */
25432
+ inspectionsFor(auditEventId) {
25433
+ return allRows(this.inspectionsStmt, { auditEventId });
24903
25434
  }
24904
- ensureSyncedAtColumn(db, "audit_events");
24905
- ensureScanLedgerTable(db);
24906
- ensureHistorySyncTable(db);
24907
- ensureBlockedDetectionsTable(db);
24908
- ensureRuleProbeCacheTable(db);
24909
- ensureWriteGateTrigger(db);
24910
- ensureTokenUsageColumns(db);
24911
- reconcileSourceProjectIds(db);
24912
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24913
- const drained = runLegacyHistoryBackfill(db);
24914
- if (drained) applyLegacyDropMigration(db, file2);
25435
+ /**
25436
+ * Sessions with structural rows still to send, oldest first.
25437
+ *
25438
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25439
+ * read. Anything recorded after the machine attached is the live forward
25440
+ * path's to deliver; this drain exists for what was recorded before it, and a
25441
+ * row both paths send is at best a duplicate request and at worst — for a
25442
+ * session root — an overwrite of the inventory ids the live path resolved.
25443
+ */
25444
+ pendingSessions(limit, before) {
25445
+ return allRows(this.sessionsStmt, { limit, before }).map(
25446
+ (r) => r.sessionId
25447
+ );
24915
25448
  }
24916
- }
24917
- function readLegacyTables(db) {
24918
- let holdsRows = false;
24919
- const marks = [];
24920
- for (const table2 of ["events", "findings"]) {
24921
- try {
24922
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
24923
- if (row === void 0) {
24924
- holdsRows = true;
24925
- marks.push(`${table2}:unreadable`);
24926
- continue;
24927
- }
24928
- if (row.n > 0) holdsRows = true;
24929
- marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
24930
- } catch {
24931
- holdsRows = true;
24932
- marks.push(`${table2}:unreadable`);
24933
- }
25449
+ /** One session's undelivered structural rows within the backlog, root first. */
25450
+ pendingRows(sessionId, limit, before) {
25451
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24934
25452
  }
24935
- return { holdsRows, mark: marks.join("|") };
24936
- }
24937
- function applyLegacyDropMigration(db, file2) {
24938
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24939
- if (!migration) return;
24940
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24941
- if (file2 !== void 0 && before?.holdsRows === true) {
24942
- try {
24943
- backupBeforeLegacyDrop(db, file2);
24944
- } catch (error61) {
24945
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24946
- return;
24947
- }
25453
+ /**
25454
+ * Captures this machine still owes the deployment, oldest first.
25455
+ *
25456
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25457
+ * by a time window — see captureRowsStmt for why a window could not express
25458
+ * this. `before` is the grace window that leaves a just-recorded capture to
25459
+ * the live path.
25460
+ */
25461
+ pendingCaptureRows(limit, before) {
25462
+ return allRows(this.captureRowsStmt, { limit, before });
24948
25463
  }
24949
- try {
25464
+ /**
25465
+ * Record that a capture is OWED to the deployment.
25466
+ *
25467
+ * Written by the attached forward path when a live send did not confirm
25468
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25469
+ * a fact rather than an inference: the machine was attached, the send did not
25470
+ * land, so the row is owed — which no time window can state, because the same
25471
+ * window that holds the rows a past attachment left owed also holds every
25472
+ * capture recorded while the machine was DETACHED, and those were never
25473
+ * offered to anyone.
25474
+ *
25475
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25476
+ * out of the drain's read.
25477
+ */
25478
+ markCaptureOwed(id) {
25479
+ this.markOwedStmt.run({ id });
25480
+ }
25481
+ /**
25482
+ * Mark every capture already on disk as owed, as of `before`.
25483
+ *
25484
+ * The consent-time backfill, called once from `aka attach` when a human
25485
+ * grants existing-history consent — never from an ongoing drain pass, and
25486
+ * never inferred from a boundary that could later move. `before` is the
25487
+ * caller's own "now" at the moment consent was granted, so what this marks
25488
+ * is exactly the backlog the consent prompt already counted, not whatever a
25489
+ * later re-attach or key rotation might widen it to.
25490
+ *
25491
+ * Returns how many rows matched, for the caller to log or test against. Not a
25492
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25493
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25494
+ */
25495
+ markCaptureBacklogOwed(before) {
25496
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25497
+ }
25498
+ /**
25499
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25500
+ *
25501
+ * CLEARS any failure reason in the same statement. A row that failed against
25502
+ * one deployment and then landed is delivered, and leaving the reason behind
25503
+ * would leave the store holding two contradictory answers about one row —
25504
+ * with the surface free to render either.
25505
+ */
25506
+ markSynced(ids, atMs) {
25507
+ this.stampAll(ids, atMs, null);
25508
+ }
25509
+ /**
25510
+ * Record that THIS MACHINE cannot express the row on the wire.
25511
+ *
25512
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25513
+ * payload, or a body the client itself refused to send. It fails identically
25514
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25515
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25516
+ * is retried; marking those would turn one outage into permanent data loss.
25517
+ */
25518
+ markSkipped(ids, atMs) {
25519
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25520
+ }
25521
+ /**
25522
+ * Record that THIS DEPLOYMENT refused the row.
25523
+ *
25524
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25525
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25526
+ * row is outstanding rather than why. What separates them is the reason, and
25527
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25528
+ * on one body, so it is terminal only for as long as this machine points at
25529
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25530
+ *
25531
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25532
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25533
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25534
+ */
25535
+ markRefused(ids, atMs) {
25536
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25537
+ }
25538
+ eachInTransaction(ids, run) {
25539
+ if (ids.length === 0) return;
24950
25540
  withTransaction(
24951
- db,
25541
+ this.db,
24952
25542
  () => {
24953
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24954
- if (alreadyDropped) return;
24955
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24956
- akaWarn(
24957
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24958
- );
24959
- return;
24960
- }
24961
- for (const statement of splitStatements(migration.sql)) {
24962
- db.exec(statement);
24963
- }
24964
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24965
- migration.tag,
24966
- Date.now()
24967
- );
25543
+ for (const id of ids) run(id);
24968
25544
  },
24969
25545
  "IMMEDIATE"
24970
25546
  );
24971
- } catch (error61) {
24972
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24973
25547
  }
24974
- }
24975
- function backupBeforeLegacyDrop(db, file2) {
24976
- reapStalePartials(file2);
24977
- const backup = backupPath(file2, "pre-drop");
24978
- snapshotStore(db, backup);
24979
- return backup;
24980
- }
24981
- var TOKEN_USAGE_COLUMNS = [
24982
- {
24983
- name: "input_tokens",
24984
- ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
24985
- },
24986
- {
24987
- name: "output_tokens",
24988
- ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
24989
- },
24990
- {
24991
- name: "cache_creation_input_tokens",
24992
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
24993
- },
24994
- {
24995
- name: "cache_read_input_tokens",
24996
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
24997
- },
24998
- {
24999
- name: "model",
25000
- ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25001
- },
25002
- {
25548
+ stampAll(ids, value, failure, failedAtMs) {
25549
+ if (ids.length === 0) return;
25550
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25551
+ withTransaction(
25552
+ this.db,
25553
+ () => {
25554
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25555
+ },
25556
+ "IMMEDIATE"
25557
+ );
25558
+ }
25559
+ /**
25560
+ * Claim rows as in-flight.
25561
+ *
25562
+ * Advisory in exactly the sense the lease is: it records that a send is in
25563
+ * progress so a surface can say so, and a lost claim costs a row showing as
25564
+ * queued while it is actually being sent. It is not exclusion — the far side
25565
+ * settles a duplicate on the row id.
25566
+ */
25567
+ claimRows(ids, atMs) {
25568
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25569
+ }
25570
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25571
+ releaseRows(ids) {
25572
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25573
+ }
25574
+ /**
25575
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25576
+ *
25577
+ * A process killed between claiming and settling leaves rows claimed with
25578
+ * nothing left to settle them. Without this they read as "sending" for ever.
25579
+ */
25580
+ releaseStaleClaims(staleBefore) {
25581
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25582
+ }
25583
+ /**
25584
+ * Every tracked row in exactly one delivery state.
25585
+ *
25586
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25587
+ * pick up now", which is a different question from "what state is this row
25588
+ * in" — and a machine that has never attached has no boundary to pass, so
25589
+ * requiring one would force a caller to invent one and report the whole store
25590
+ * as queued.
25591
+ */
25592
+ /**
25593
+ * The same partition, one row per kind that a lane carries.
25594
+ *
25595
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25596
+ * scope decides which rows exist at all, so a kind that has never been
25597
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25598
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25599
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25600
+ * different things.
25601
+ */
25602
+ partitionByKind() {
25603
+ return allRows(
25604
+ this.partitionByKindStmt,
25605
+ {}
25606
+ ).map((row) => ({
25607
+ kind: row.kind,
25608
+ queued: row.queued ?? 0,
25609
+ inProgress: row.inProgress ?? 0,
25610
+ synced: row.synced ?? 0,
25611
+ failed: row.failed ?? 0,
25612
+ refused: row.refused ?? 0,
25613
+ detached: row.detached ?? 0,
25614
+ total: row.total ?? 0
25615
+ }));
25616
+ }
25617
+ partition() {
25618
+ const row = getRow(this.partitionStmt, {});
25619
+ return {
25620
+ queued: row?.queued ?? 0,
25621
+ inProgress: row?.inProgress ?? 0,
25622
+ synced: row?.synced ?? 0,
25623
+ failed: row?.failed ?? 0,
25624
+ refused: row?.refused ?? 0,
25625
+ detached: row?.detached ?? 0,
25626
+ total: row?.total ?? 0
25627
+ };
25628
+ }
25629
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25630
+ counts(before) {
25631
+ const row = getRow(this.countsStmt, { before });
25632
+ const captures = getRow(this.captureSkipCountStmt);
25633
+ return {
25634
+ pending: row?.pending ?? 0,
25635
+ sent: row?.sent ?? 0,
25636
+ skipped: row?.skipped ?? 0,
25637
+ refused: row?.refused ?? 0,
25638
+ detached: row?.detached ?? 0,
25639
+ capturesSkipped: captures?.skipped ?? 0
25640
+ };
25641
+ }
25642
+ /**
25643
+ * The deployment the current stamps were made against, and where its backlog
25644
+ * ends.
25645
+ *
25646
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25647
+ * machine that has never drained is — and every writer below seeds the row
25648
+ * before it needs one, so nothing depends on this creating it. Keeping the
25649
+ * write off the gate path matters because the gate runs on every pass while a
25650
+ * write has to take the database's write lock.
25651
+ */
25652
+ deployment() {
25653
+ const row = getRow(
25654
+ this.fingerprintStmt
25655
+ );
25656
+ return {
25657
+ fingerprint: row?.fingerprint ?? void 0,
25658
+ backlogBefore: row?.backlogBefore ?? void 0
25659
+ };
25660
+ }
25661
+ /**
25662
+ * Point the ledger at a different deployment, discarding what it recorded
25663
+ * about the previous one.
25664
+ *
25665
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25666
+ * machine has just left are undelivered as far as the new one is concerned.
25667
+ * All four in one transaction, so a crash between them cannot leave stamps
25668
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25669
+ * a disown with no re-mark to follow it.
25670
+ *
25671
+ * The boundary is written HERE and only here, which is what freezes it: a
25672
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25673
+ * unchanged, so this never runs and the backlog does not widen back over rows
25674
+ * the live path has since delivered.
25675
+ *
25676
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25677
+ * granted existing-history consent for the deployment this call is arming —
25678
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25679
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25680
+ * apart. Passed only when that grant is valid, since this method has no way
25681
+ * to check consent itself and must not mark a row owed for a machine that
25682
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25683
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25684
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25685
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25686
+ * on the cleared side of that bound — and the re-mark in the same
25687
+ * transaction is what puts those rows back. A crash between the two cannot
25688
+ * strand the ledger disowned with nothing re-marked — the transaction either
25689
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25690
+ * committed re-enters this method on the very next pass. Omit it (the
25691
+ * structural-only tests do) to exercise the disown in isolation.
25692
+ *
25693
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25694
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25695
+ * live path can mark a capture owed from the moment `aka attach` writes the
25696
+ * descriptor, before the drain's first pass ever reaches this method, and
25697
+ * such a row sits at or after the bound rather than below it. What keeps the
25698
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25699
+ * bound — disown runs first, re-mark second, both inside the one
25700
+ * transaction above.
25701
+ */
25702
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25703
+ this.ensureRowStmt.run();
25704
+ withTransaction(
25705
+ this.db,
25706
+ () => {
25707
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25708
+ this.rearmStmt.run();
25709
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25710
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25711
+ }
25712
+ if (backfillCapturesBefore !== void 0) {
25713
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25714
+ }
25715
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25716
+ },
25717
+ "IMMEDIATE"
25718
+ );
25719
+ }
25720
+ /**
25721
+ * End the attached period: hand its rows to the live path, and release the
25722
+ * boundary so the next attachment can freeze a new one.
25723
+ *
25724
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25725
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25726
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25727
+ * during the detached period, because the machine is not attached. Rows
25728
+ * recorded in that window sit after the boundary and before the re-attach, so
25729
+ * neither path takes them, and the pending count reports none outstanding.
25730
+ *
25731
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25732
+ * closing attachment's to deliver and are no longer outstanding — that is what
25733
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25734
+ * distinction is not academic: this used to write a delivery TIME, which every
25735
+ * read treats as delivery, so one detach turned a window of undelivered rows
25736
+ * into a window of delivered ones and no surface could tell. It writes the
25737
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25738
+ * "received" stop being the same fact.
25739
+ *
25740
+ * A change of deployment still frees them (see the re-arm), because the next
25741
+ * deployment has seen none of this machine's history — so the rows reach it
25742
+ * exactly as they did when this wrote a delivery time.
25743
+ *
25744
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25745
+ * window unstamped — that half-state would re-send the whole attached period
25746
+ * on the next attach, which is the failure the boundary exists to prevent.
25747
+ */
25748
+ closeAttachedWindow(attachedAtMs, atMs) {
25749
+ this.ensureRowStmt.run();
25750
+ withTransaction(
25751
+ this.db,
25752
+ () => {
25753
+ const row = getRow(this.fingerprintStmt);
25754
+ const from = row?.backlogBefore ?? attachedAtMs;
25755
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25756
+ this.releaseBoundaryStmt.run();
25757
+ },
25758
+ "IMMEDIATE"
25759
+ );
25760
+ }
25761
+ /**
25762
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25763
+ *
25764
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25765
+ * different deployment and therefore discards what was delivered to the old
25766
+ * one: here the recipient is the same, so everything already sent to it stays
25767
+ * sent.
25768
+ */
25769
+ freezeBoundary(backlogBefore) {
25770
+ this.ensureRowStmt.run();
25771
+ this.freezeBoundaryStmt.run({ backlogBefore });
25772
+ }
25773
+ /** Take the claim, or report that someone live already holds it. */
25774
+ claim(pid, host, nowMs, staleAfterMs) {
25775
+ this.ensureRowStmt.run();
25776
+ let taken = false;
25777
+ withTransaction(
25778
+ this.db,
25779
+ () => {
25780
+ const result = this.claimStmt.run({
25781
+ pid,
25782
+ host,
25783
+ now: nowMs,
25784
+ staleBefore: nowMs - staleAfterMs
25785
+ });
25786
+ taken = result.changes === 1;
25787
+ },
25788
+ "IMMEDIATE"
25789
+ );
25790
+ return taken;
25791
+ }
25792
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25793
+ heartbeat(pid, nowMs) {
25794
+ this.heartbeatStmt.run({ now: nowMs, pid });
25795
+ }
25796
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25797
+ release(pid) {
25798
+ this.releaseStmt.run({ pid });
25799
+ }
25800
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25801
+ lease() {
25802
+ return getRow(this.leaseStmt);
25803
+ }
25804
+ };
25805
+
25806
+ // ../../packages/persistence/src/migrations.ts
25807
+ function describeObject(object2) {
25808
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25809
+ }
25810
+ function splitStatements(sql) {
25811
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25812
+ }
25813
+ function createdIndexName(statement) {
25814
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25815
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25816
+ }
25817
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25818
+ function applyMigrations(db, file2, options = {}) {
25819
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25820
+ db.exec(
25821
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25822
+ );
25823
+ const applied = new Set(
25824
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25825
+ );
25826
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25827
+ const record2 = db.prepare(
25828
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25829
+ );
25830
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25831
+ if (applied.has(migration.tag)) continue;
25832
+ if (options.skipTags?.has(migration.tag) === true) continue;
25833
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25834
+ const evidence = evidenceObjects(migration.sql);
25835
+ const present = evidence.filter((o) => evidenceExists(db, o));
25836
+ if (present.length > 0 && present.length < evidence.length) {
25837
+ const missing = evidence.filter((o) => !present.includes(o));
25838
+ 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.`;
25839
+ akaWarn(message);
25840
+ throw new Error(`[aka] ${message}`);
25841
+ }
25842
+ const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
25843
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25844
+ const statements = splitStatements(migration.sql);
25845
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25846
+ try {
25847
+ withTransaction(
25848
+ db,
25849
+ () => {
25850
+ for (const statement of statements) {
25851
+ const indexName = createdIndexName(statement);
25852
+ if (indexName === void 0) {
25853
+ if (alreadyApplied) continue;
25854
+ } else if (indexExists(db, indexName)) {
25855
+ continue;
25856
+ }
25857
+ db.exec(statement);
25858
+ }
25859
+ if (wantsFkOff && !alreadyApplied) {
25860
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25861
+ if (violations.length > 0) {
25862
+ throw new Error(
25863
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25864
+ );
25865
+ }
25866
+ }
25867
+ record2.run(migration.tag, Date.now());
25868
+ },
25869
+ "IMMEDIATE"
25870
+ );
25871
+ } finally {
25872
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25873
+ }
25874
+ }
25875
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25876
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25877
+ }
25878
+ ensureSyncedAtColumn(db, "audit_events");
25879
+ ensureScanLedgerTable(db);
25880
+ ensureHistorySyncTable(db);
25881
+ ensureBlockedDetectionsTable(db);
25882
+ ensureRuleProbeCacheTable(db);
25883
+ ensureWriteGateTrigger(db);
25884
+ ensureTokenUsageColumns(db);
25885
+ reconcileSourceProjectIds(db);
25886
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25887
+ const drained = runLegacyHistoryBackfill(db);
25888
+ if (drained) applyLegacyDropMigration(db, file2);
25889
+ }
25890
+ }
25891
+ function readLegacyTables(db) {
25892
+ let holdsRows = false;
25893
+ const marks = [];
25894
+ for (const table2 of ["events", "findings"]) {
25895
+ try {
25896
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
25897
+ if (row === void 0) {
25898
+ holdsRows = true;
25899
+ marks.push(`${table2}:unreadable`);
25900
+ continue;
25901
+ }
25902
+ if (row.n > 0) holdsRows = true;
25903
+ marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
25904
+ } catch {
25905
+ holdsRows = true;
25906
+ marks.push(`${table2}:unreadable`);
25907
+ }
25908
+ }
25909
+ return { holdsRows, mark: marks.join("|") };
25910
+ }
25911
+ function applyLegacyDropMigration(db, file2) {
25912
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25913
+ if (!migration) return;
25914
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25915
+ if (file2 !== void 0 && before?.holdsRows === true) {
25916
+ try {
25917
+ backupBeforeLegacyDrop(db, file2);
25918
+ } catch (error61) {
25919
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25920
+ return;
25921
+ }
25922
+ }
25923
+ try {
25924
+ withTransaction(
25925
+ db,
25926
+ () => {
25927
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25928
+ if (alreadyDropped) return;
25929
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25930
+ akaWarn(
25931
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25932
+ );
25933
+ return;
25934
+ }
25935
+ for (const statement of splitStatements(migration.sql)) {
25936
+ db.exec(statement);
25937
+ }
25938
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25939
+ migration.tag,
25940
+ Date.now()
25941
+ );
25942
+ },
25943
+ "IMMEDIATE"
25944
+ );
25945
+ } catch (error61) {
25946
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25947
+ }
25948
+ }
25949
+ function backupBeforeLegacyDrop(db, file2) {
25950
+ reapStalePartials(file2);
25951
+ const backup = backupPath(file2, "pre-drop");
25952
+ snapshotStore(db, backup);
25953
+ return backup;
25954
+ }
25955
+ var TOKEN_USAGE_COLUMNS = [
25956
+ {
25957
+ name: "input_tokens",
25958
+ ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
25959
+ },
25960
+ {
25961
+ name: "output_tokens",
25962
+ ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
25963
+ },
25964
+ {
25965
+ name: "cache_creation_input_tokens",
25966
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
25967
+ },
25968
+ {
25969
+ name: "cache_read_input_tokens",
25970
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25971
+ },
25972
+ {
25973
+ name: "model",
25974
+ ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25975
+ },
25976
+ {
25003
25977
  name: "provider",
25004
25978
  ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
25005
25979
  }
@@ -25262,10 +26236,62 @@ function ensureSyncedAtColumn(db, table2) {
25262
26236
  if (!columns.includes("outbox_owed")) {
25263
26237
  db.exec(`ALTER TABLE ${table2} ADD COLUMN outbox_owed integer`);
25264
26238
  }
26239
+ if (!columns.includes("sync_failed_at")) {
26240
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failed_at integer`);
26241
+ }
26242
+ if (!columns.includes("sync_failure")) {
26243
+ withTransaction(
26244
+ db,
26245
+ () => {
26246
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failure text`);
26247
+ db.exec(
26248
+ `UPDATE ${table2} SET synced_at = NULL
26249
+ WHERE synced_at = -1
26250
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26251
+ );
26252
+ },
26253
+ "IMMEDIATE"
26254
+ );
26255
+ }
25265
26256
  db.exec(
25266
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25267
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26257
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26258
+ BEFORE UPDATE OF sync_failure ON ${table2}
26259
+ WHEN ${syncFailureRejectCondition()}
26260
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25268
26261
  );
26262
+ const syncIndexColumns = [
26263
+ "event_type",
26264
+ "synced_at",
26265
+ "sync_claimed_at",
26266
+ "started_at",
26267
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26268
+ // has to be in the index for the read to stay covered — but putting it
26269
+ // ahead of `started_at` would reorder the prefix the structural drain's
26270
+ // reads match on.
26271
+ "sync_failure"
26272
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26273
+ //
26274
+ // The delivery-state read tests it — a capture's state depends on whether a
26275
+ // live forward marked it owed — so carrying it here makes that read covering
26276
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26277
+ // But a sixth column changes what the planner charges for this index, and
26278
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26279
+ // then stops choosing the per-session index for the token rollup and walks
26280
+ // every `llm_call` in the store through the event-type index instead. That
26281
+ // read grows with the store; this one does not.
26282
+ //
26283
+ // 40 ms on the largest store measured, once per render, is a cost worth
26284
+ // paying to leave every other read's plan where it was.
26285
+ ];
26286
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26287
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26288
+ if (!syncIndexMatches) {
26289
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26290
+ db.exec(
26291
+ `CREATE INDEX idx_audit_events_sync
26292
+ ON audit_events (${syncIndexColumns.join(", ")})`
26293
+ );
26294
+ }
25269
26295
  db.exec(
25270
26296
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25271
26297
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25487,7 +26513,11 @@ function buildAuditEvent(row) {
25487
26513
  link: linkParsed?.success ? linkParsed.data : null,
25488
26514
  targetId: row.target_id,
25489
26515
  internal: intToBool(row.internal),
25490
- flagged: intToBool(row.flagged)
26516
+ flagged: intToBool(row.flagged),
26517
+ // Only meaningful when the title came out empty — a row whose body was
26518
+ // expired but whose title fell back to `tool_name` still has something to
26519
+ // render, and flagging it would make the view apologise for nothing.
26520
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25491
26521
  };
25492
26522
  }
25493
26523
  var TIMELINE_COLUMNS = `
@@ -25495,6 +26525,7 @@ var TIMELINE_COLUMNS = `
25495
26525
  event_type,
25496
26526
  started_at,
25497
26527
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26528
+ content_expired_at,
25498
26529
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25499
26530
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25500
26531
  json_extract(attributes, '$.severity') AS severity,
@@ -26160,6 +27191,88 @@ var SqliteAuditEventsRepository = class {
26160
27191
  }
26161
27192
  };
26162
27193
 
27194
+ // ../../packages/persistence/src/repositories/body-retention.ts
27195
+ var DEFAULT_BATCH_SIZE = 500;
27196
+ var DEFAULT_MAX_ROWS = 5e4;
27197
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27198
+ var SqliteBodyRetentionRepository = class {
27199
+ constructor(db) {
27200
+ this.db = db;
27201
+ const select = (laneClause) => `
27202
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27203
+ FROM audit_events
27204
+ WHERE content IS NOT NULL
27205
+ AND started_at < :cutoff
27206
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27207
+ ${laneClause}
27208
+ ORDER BY started_at
27209
+ LIMIT :limit`;
27210
+ this.candidatesStmt = this.db.prepare(select(""));
27211
+ this.candidatesSyncSafeStmt = this.db.prepare(
27212
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27213
+ );
27214
+ this.heldBySyncStmt = this.db.prepare(`
27215
+ SELECT COUNT(*) AS n
27216
+ FROM audit_events
27217
+ WHERE content IS NOT NULL
27218
+ AND started_at < :cutoff
27219
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27220
+ AND synced_at IS NULL`);
27221
+ this.expireStmt = this.db.prepare(
27222
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27223
+ );
27224
+ }
27225
+ db;
27226
+ candidatesStmt;
27227
+ candidatesSyncSafeStmt;
27228
+ heldBySyncStmt;
27229
+ expireStmt;
27230
+ /** How many bytes a pass with these options would free, changing nothing. */
27231
+ preview(opts) {
27232
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27233
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27234
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27235
+ return {
27236
+ rowsExpired: rows.length,
27237
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27238
+ rowsHeldBySync: this.countHeldBySync(opts)
27239
+ };
27240
+ }
27241
+ /** Clear eligible bodies, in bounded batches. */
27242
+ expire(opts) {
27243
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27244
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27245
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27246
+ let rowsExpired = 0;
27247
+ let bytesFreed = 0;
27248
+ let done = true;
27249
+ while (rowsExpired < maxRows) {
27250
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27251
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27252
+ if (batch.length === 0) break;
27253
+ withTransaction(
27254
+ this.db,
27255
+ () => {
27256
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27257
+ },
27258
+ "IMMEDIATE"
27259
+ );
27260
+ rowsExpired += batch.length;
27261
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27262
+ if (batch.length < remaining) break;
27263
+ if (rowsExpired >= maxRows) {
27264
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27265
+ }
27266
+ }
27267
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27268
+ }
27269
+ countHeldBySync(opts) {
27270
+ if (opts.sweepSyncLane) return 0;
27271
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27272
+ return row.n;
27273
+ }
27274
+ };
27275
+
26163
27276
  // ../../packages/persistence/src/repositories/classified-data.ts
26164
27277
  var SqliteClassifiedDataRepository = class {
26165
27278
  constructor(db) {
@@ -26988,7 +28101,15 @@ function toFlatFindingRow(r) {
26988
28101
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26989
28102
  eventId: r.event_id,
26990
28103
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26991
- status: deriveInstanceStatus(r)
28104
+ status: deriveInstanceStatus(r),
28105
+ delivery: deriveFindingDelivery({
28106
+ kind: r.kind,
28107
+ syncedAt: r.synced_at,
28108
+ syncClaimedAt: r.sync_claimed_at,
28109
+ syncFailedAt: r.sync_failed_at,
28110
+ syncFailure: r.sync_failure,
28111
+ outboxOwed: r.outbox_owed
28112
+ })
26992
28113
  };
26993
28114
  }
26994
28115
  function encodeGroupCursor(group) {
@@ -27052,7 +28173,10 @@ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS c
27052
28173
  e.tool_name AS tool_name,
27053
28174
  f.audit_event_id AS event_id, e.root_session_id AS session_id,
27054
28175
  e.event_type AS kind, f.finding_key AS finding_key,
27055
- ${latestResolutionStatusSql("f")} AS latest_status`;
28176
+ ${latestResolutionStatusSql("f")} AS latest_status,
28177
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28178
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28179
+ e.outbox_owed AS outbox_owed`;
27056
28180
  var DAY_MS3 = 864e5;
27057
28181
  var SqliteFindingsRepository = class {
27058
28182
  constructor(db) {
@@ -27297,6 +28421,7 @@ var SqliteFindingsRepository = class {
27297
28421
  providers: query.provider,
27298
28422
  actions: query.action,
27299
28423
  statuses: query.status,
28424
+ deliveries: query.deployment,
27300
28425
  tools: query.tool,
27301
28426
  repo: query.repo,
27302
28427
  file: query.file,
@@ -27364,6 +28489,7 @@ var SqliteFindingsRepository = class {
27364
28489
  providers: query.provider,
27365
28490
  actions: query.action,
27366
28491
  statuses: query.status,
28492
+ deliveries: query.deployment,
27367
28493
  tools: query.tool,
27368
28494
  q: query.q
27369
28495
  };
@@ -27627,7 +28753,9 @@ var SqliteFindingsRepository = class {
27627
28753
  )
27628
28754
  );
27629
28755
  for (const row of grouped) {
27630
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28756
+ if (Object.hasOwn(byAction, row.action_taken)) {
28757
+ byAction[row.action_taken] = row.c;
28758
+ }
27631
28759
  }
27632
28760
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27633
28761
  const sevRows = allRows(
@@ -27644,7 +28772,9 @@ var SqliteFindingsRepository = class {
27644
28772
  )
27645
28773
  );
27646
28774
  for (const row of sevRows) {
27647
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28775
+ if (Object.hasOwn(bySeverity, row.severity)) {
28776
+ bySeverity[row.severity] = row.c;
28777
+ }
27648
28778
  }
27649
28779
  const categories = ENFORCEABLE_CATEGORIES;
27650
28780
  const enabledRows = allRows(
@@ -27693,525 +28823,6 @@ function isoDay(ms) {
27693
28823
  return new Date(ms).toISOString().slice(0, 10);
27694
28824
  }
27695
28825
 
27696
- // ../../packages/persistence/src/repositories/history-sync.ts
27697
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27698
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27699
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27700
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27701
- var SKIPPED = -1;
27702
- var ROW_COLUMNS = `id,
27703
- parent_id AS parentId,
27704
- root_session_id AS rootSessionId,
27705
- event_type AS eventType,
27706
- host_id AS hostId,
27707
- harness_id AS harnessId,
27708
- source_project_id AS sourceProjectId,
27709
- started_at AS startedAt,
27710
- ended_at AS endedAt,
27711
- severity,
27712
- priority,
27713
- content,
27714
- content_hash AS contentHash,
27715
- attributes`;
27716
- var SqliteHistorySyncRepository = class {
27717
- constructor(db) {
27718
- this.db = db;
27719
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27720
- this.sessionsStmt = db.prepare(
27721
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27722
- FROM audit_events
27723
- WHERE synced_at IS NULL
27724
- AND event_type IN (${TYPE_LIST})
27725
- AND started_at < :before
27726
- GROUP BY sessionId
27727
- ORDER BY earliest
27728
- LIMIT :limit`
27729
- );
27730
- this.rowsStmt = db.prepare(
27731
- `SELECT ${ROW_COLUMNS}
27732
- FROM audit_events
27733
- WHERE synced_at IS NULL
27734
- AND event_type IN (${TYPE_LIST})
27735
- AND started_at < :before
27736
- AND COALESCE(root_session_id, id) = :sessionId
27737
- ORDER BY (event_type = 'session') DESC, started_at
27738
- LIMIT :limit`
27739
- );
27740
- this.captureRowsStmt = db.prepare(
27741
- `SELECT ${ROW_COLUMNS}
27742
- FROM audit_events
27743
- WHERE synced_at IS NULL
27744
- AND sync_claimed_at IS NULL
27745
- AND outbox_owed = 1
27746
- AND event_type IN (${CAPTURE_TYPE_LIST})
27747
- AND started_at < :before
27748
- ORDER BY started_at
27749
- LIMIT :limit`
27750
- );
27751
- this.markOwedStmt = db.prepare(
27752
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27753
- );
27754
- this.markCaptureBacklogOwedStmt = db.prepare(
27755
- `UPDATE audit_events SET outbox_owed = 1
27756
- WHERE synced_at IS NULL
27757
- AND event_type IN (${CAPTURE_TYPE_LIST})
27758
- AND started_at < :before`
27759
- );
27760
- this.stampStmt = db.prepare(
27761
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27762
- );
27763
- this.claimRowStmt = db.prepare(
27764
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27765
- );
27766
- this.releaseRowStmt = db.prepare(
27767
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27768
- );
27769
- this.releaseStaleClaimsStmt = db.prepare(
27770
- `UPDATE audit_events SET sync_claimed_at = NULL
27771
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27772
- );
27773
- this.partitionStmt = db.prepare(
27774
- `SELECT
27775
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27776
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27777
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27778
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27779
- COUNT(*) AS total
27780
- FROM audit_events
27781
- WHERE event_type IN (${TYPE_LIST})`
27782
- );
27783
- this.countsStmt = db.prepare(
27784
- `SELECT
27785
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27786
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27787
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27788
- FROM audit_events
27789
- WHERE event_type IN (${TYPE_LIST})`
27790
- );
27791
- this.captureSkipCountStmt = db.prepare(
27792
- `SELECT COUNT(*) AS skipped
27793
- FROM audit_events
27794
- WHERE synced_at = ${String(SKIPPED)}
27795
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27796
- );
27797
- this.fingerprintStmt = db.prepare(
27798
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27799
- FROM history_sync WHERE id = 1`
27800
- );
27801
- this.setFingerprintStmt = db.prepare(
27802
- `UPDATE history_sync
27803
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27804
- WHERE id = 1`
27805
- );
27806
- this.disownCapturesStmt = db.prepare(
27807
- `UPDATE audit_events SET outbox_owed = NULL
27808
- WHERE outbox_owed IS NOT NULL
27809
- AND event_type IN (${CAPTURE_TYPE_LIST})
27810
- AND started_at < :attachedAt`
27811
- );
27812
- this.rearmStmt = db.prepare(
27813
- `UPDATE audit_events SET synced_at = NULL
27814
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27815
- );
27816
- this.claimStmt = db.prepare(
27817
- `UPDATE history_sync
27818
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27819
- WHERE id = 1
27820
- AND (owner_pid IS NULL
27821
- OR heartbeat_at IS NULL
27822
- OR heartbeat_at < :staleBefore
27823
- OR heartbeat_at > :now)`
27824
- );
27825
- this.heartbeatStmt = db.prepare(
27826
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27827
- );
27828
- this.releaseStmt = db.prepare(
27829
- `UPDATE history_sync
27830
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27831
- WHERE id = 1 AND owner_pid = :pid`
27832
- );
27833
- this.closeWindowStmt = db.prepare(
27834
- `UPDATE audit_events SET synced_at = :at
27835
- WHERE synced_at IS NULL
27836
- AND event_type IN (${TYPE_LIST})
27837
- AND started_at >= :attachedAt`
27838
- );
27839
- this.releaseBoundaryStmt = db.prepare(
27840
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27841
- );
27842
- this.freezeBoundaryStmt = db.prepare(
27843
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27844
- );
27845
- this.leaseStmt = db.prepare(
27846
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27847
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27848
- FROM history_sync WHERE id = 1`
27849
- );
27850
- this.inspectionsStmt = db.prepare(
27851
- `SELECT d.rule_id AS ruleId,
27852
- d.name AS ruleName,
27853
- d.version AS ruleVersion,
27854
- d.category AS category,
27855
- d.severity AS severity,
27856
- f.span_start AS spanStart,
27857
- f.span_end AS spanEnd,
27858
- f.masked_match AS maskedMatch,
27859
- f.action_taken AS actionTaken,
27860
- f.confidence AS confidence
27861
- FROM inspection_findings f
27862
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27863
- WHERE f.audit_event_id = :auditEventId
27864
- ORDER BY f.span_start, f.id`
27865
- );
27866
- }
27867
- db;
27868
- ensureRowStmt;
27869
- sessionsStmt;
27870
- rowsStmt;
27871
- stampStmt;
27872
- countsStmt;
27873
- fingerprintStmt;
27874
- setFingerprintStmt;
27875
- rearmStmt;
27876
- claimStmt;
27877
- heartbeatStmt;
27878
- releaseStmt;
27879
- leaseStmt;
27880
- inspectionsStmt;
27881
- closeWindowStmt;
27882
- releaseBoundaryStmt;
27883
- freezeBoundaryStmt;
27884
- captureRowsStmt;
27885
- markOwedStmt;
27886
- markCaptureBacklogOwedStmt;
27887
- captureSkipCountStmt;
27888
- disownCapturesStmt;
27889
- partitionStmt;
27890
- claimRowStmt;
27891
- releaseRowStmt;
27892
- releaseStaleClaimsStmt;
27893
- /**
27894
- * The masked detections recorded against one tool call.
27895
- *
27896
- * These travel with the event because a tool call's target is not
27897
- * re-inspectable from the event alone — unlike a capture, where the text
27898
- * itself is re-scannable. What crosses is the masked match and the rule that
27899
- * produced it, never the value.
27900
- */
27901
- inspectionsFor(auditEventId) {
27902
- return allRows(this.inspectionsStmt, { auditEventId });
27903
- }
27904
- /**
27905
- * Sessions with structural rows still to send, oldest first.
27906
- *
27907
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27908
- * read. Anything recorded after the machine attached is the live forward
27909
- * path's to deliver; this drain exists for what was recorded before it, and a
27910
- * row both paths send is at best a duplicate request and at worst — for a
27911
- * session root — an overwrite of the inventory ids the live path resolved.
27912
- */
27913
- pendingSessions(limit, before) {
27914
- return allRows(this.sessionsStmt, { limit, before }).map(
27915
- (r) => r.sessionId
27916
- );
27917
- }
27918
- /** One session's undelivered structural rows within the backlog, root first. */
27919
- pendingRows(sessionId, limit, before) {
27920
- return allRows(this.rowsStmt, { sessionId, limit, before });
27921
- }
27922
- /**
27923
- * Captures this machine still owes the deployment, oldest first.
27924
- *
27925
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27926
- * by a time window — see captureRowsStmt for why a window could not express
27927
- * this. `before` is the grace window that leaves a just-recorded capture to
27928
- * the live path.
27929
- */
27930
- pendingCaptureRows(limit, before) {
27931
- return allRows(this.captureRowsStmt, { limit, before });
27932
- }
27933
- /**
27934
- * Record that a capture is OWED to the deployment.
27935
- *
27936
- * Written by the attached forward path when a live send did not confirm
27937
- * delivery, and read by the drain as the whole of its eligibility test. It is
27938
- * a fact rather than an inference: the machine was attached, the send did not
27939
- * land, so the row is owed — which no time window can state, because the same
27940
- * window that holds the rows a past attachment left owed also holds every
27941
- * capture recorded while the machine was DETACHED, and those were never
27942
- * offered to anyone.
27943
- *
27944
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27945
- * out of the drain's read.
27946
- */
27947
- markCaptureOwed(id) {
27948
- this.markOwedStmt.run({ id });
27949
- }
27950
- /**
27951
- * Mark every capture already on disk as owed, as of `before`.
27952
- *
27953
- * The consent-time backfill, called once from `aka attach` when a human
27954
- * grants existing-history consent — never from an ongoing drain pass, and
27955
- * never inferred from a boundary that could later move. `before` is the
27956
- * caller's own "now" at the moment consent was granted, so what this marks
27957
- * is exactly the backlog the consent prompt already counted, not whatever a
27958
- * later re-attach or key rotation might widen it to.
27959
- *
27960
- * Returns how many rows matched, for the caller to log or test against. Not a
27961
- * count of NEWLY marked rows — a row still unsynced from an earlier call
27962
- * matches again and is counted again, the same as `UPDATE`'s own `changes`.
27963
- */
27964
- markCaptureBacklogOwed(before) {
27965
- return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
27966
- }
27967
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27968
- markSynced(ids, atMs) {
27969
- this.stampAll(ids, atMs);
27970
- }
27971
- /**
27972
- * Record that a row will never be sent.
27973
- *
27974
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27975
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27976
- * is retried; marking those would turn one outage into permanent data loss.
27977
- */
27978
- markSkipped(ids) {
27979
- this.stampAll(ids, SKIPPED);
27980
- }
27981
- eachInTransaction(ids, run) {
27982
- if (ids.length === 0) return;
27983
- withTransaction(
27984
- this.db,
27985
- () => {
27986
- for (const id of ids) run(id);
27987
- },
27988
- "IMMEDIATE"
27989
- );
27990
- }
27991
- stampAll(ids, value) {
27992
- if (ids.length === 0) return;
27993
- withTransaction(
27994
- this.db,
27995
- () => {
27996
- for (const id of ids) this.stampStmt.run({ at: value, id });
27997
- },
27998
- "IMMEDIATE"
27999
- );
28000
- }
28001
- /**
28002
- * Claim rows as in-flight.
28003
- *
28004
- * Advisory in exactly the sense the lease is: it records that a send is in
28005
- * progress so a surface can say so, and a lost claim costs a row showing as
28006
- * queued while it is actually being sent. It is not exclusion — the far side
28007
- * settles a duplicate on the row id.
28008
- */
28009
- claimRows(ids, atMs) {
28010
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
28011
- }
28012
- /** Give back a claim without settling — the send failed, the row is queued again. */
28013
- releaseRows(ids) {
28014
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
28015
- }
28016
- /**
28017
- * Clear claims older than `staleBefore`, and report how many were cleared.
28018
- *
28019
- * A process killed between claiming and settling leaves rows claimed with
28020
- * nothing left to settle them. Without this they read as "sending" for ever.
28021
- */
28022
- releaseStaleClaims(staleBefore) {
28023
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
28024
- }
28025
- /**
28026
- * Every tracked row in exactly one delivery state.
28027
- *
28028
- * Takes no boundary on purpose. The boundary answers "what should the drain
28029
- * pick up now", which is a different question from "what state is this row
28030
- * in" — and a machine that has never attached has no boundary to pass, so
28031
- * requiring one would force a caller to invent one and report the whole store
28032
- * as queued.
28033
- */
28034
- partition() {
28035
- const row = getRow(this.partitionStmt, {});
28036
- return {
28037
- queued: row?.queued ?? 0,
28038
- inProgress: row?.inProgress ?? 0,
28039
- synced: row?.synced ?? 0,
28040
- failed: row?.failed ?? 0,
28041
- total: row?.total ?? 0
28042
- };
28043
- }
28044
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
28045
- counts(before) {
28046
- const row = getRow(
28047
- this.countsStmt,
28048
- { before }
28049
- );
28050
- const captures = getRow(this.captureSkipCountStmt);
28051
- return {
28052
- pending: row?.pending ?? 0,
28053
- sent: row?.sent ?? 0,
28054
- skipped: row?.skipped ?? 0,
28055
- capturesSkipped: captures?.skipped ?? 0
28056
- };
28057
- }
28058
- /**
28059
- * The deployment the current stamps were made against, and where its backlog
28060
- * ends.
28061
- *
28062
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
28063
- * machine that has never drained is — and every writer below seeds the row
28064
- * before it needs one, so nothing depends on this creating it. Keeping the
28065
- * write off the gate path matters because the gate runs on every pass while a
28066
- * write has to take the database's write lock.
28067
- */
28068
- deployment() {
28069
- const row = getRow(
28070
- this.fingerprintStmt
28071
- );
28072
- return {
28073
- fingerprint: row?.fingerprint ?? void 0,
28074
- backlogBefore: row?.backlogBefore ?? void 0
28075
- };
28076
- }
28077
- /**
28078
- * Point the ledger at a different deployment, discarding what it recorded
28079
- * about the previous one.
28080
- *
28081
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
28082
- * machine has just left are undelivered as far as the new one is concerned.
28083
- * All four in one transaction, so a crash between them cannot leave stamps
28084
- * attributed to the wrong deployment, a boundary that belongs to another, or
28085
- * a disown with no re-mark to follow it.
28086
- *
28087
- * The boundary is written HERE and only here, which is what freezes it: a
28088
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28089
- * unchanged, so this never runs and the backlog does not widen back over rows
28090
- * the live path has since delivered.
28091
- *
28092
- * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
28093
- * granted existing-history consent for the deployment this call is arming —
28094
- * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
28095
- * instant, `backlogBefore` is the ATTACH instant, and the two can be far
28096
- * apart. Passed only when that grant is valid, since this method has no way
28097
- * to check consent itself and must not mark a row owed for a machine that
28098
- * never agreed to it. Applied AFTER the disown above, in the SAME
28099
- * transaction: what the disown clears is every marker below `backlogBefore`,
28100
- * which includes this deployment's OWN pre-attach rows — `aka attach` calls
28101
- * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
28102
- * on the cleared side of that bound — and the re-mark in the same
28103
- * transaction is what puts those rows back. A crash between the two cannot
28104
- * strand the ledger disowned with nothing re-marked — the transaction either
28105
- * lands whole or not at all, and a fingerprint mismatch that has not yet
28106
- * committed re-enters this method on the very next pass. Omit it (the
28107
- * structural-only tests do) to exercise the disown in isolation.
28108
- *
28109
- * The disown is bounded by `backlogBefore`, which is what keeps it from
28110
- * touching a marker the NEW deployment's OWN live path has already set: B's
28111
- * live path can mark a capture owed from the moment `aka attach` writes the
28112
- * descriptor, before the drain's first pass ever reaches this method, and
28113
- * such a row sits at or after the bound rather than below it. What keeps the
28114
- * disown from eating THIS SAME CALL's own re-mark is the order, not the
28115
- * bound — disown runs first, re-mark second, both inside the one
28116
- * transaction above.
28117
- */
28118
- rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
28119
- this.ensureRowStmt.run();
28120
- withTransaction(
28121
- this.db,
28122
- () => {
28123
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28124
- this.rearmStmt.run();
28125
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28126
- this.disownCapturesStmt.run({ attachedAt: backlogBefore });
28127
- }
28128
- if (backfillCapturesBefore !== void 0) {
28129
- this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
28130
- }
28131
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28132
- },
28133
- "IMMEDIATE"
28134
- );
28135
- }
28136
- /**
28137
- * End the attached period: hand its rows to the live path, and release the
28138
- * boundary so the next attachment can freeze a new one.
28139
- *
28140
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28141
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28142
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28143
- * during the detached period, because the machine is not attached. Rows
28144
- * recorded in that window sit after the boundary and before the re-attach, so
28145
- * neither path takes them, and the pending count reports none outstanding.
28146
- *
28147
- * Stamping the attached window is not a claim that every one of those rows
28148
- * reached the deployment — the live path drops on failure and says so
28149
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28150
- * status quo: they sit outside the frozen boundary today and are equally never
28151
- * re-sent. Making it explicit is what lets the boundary move.
28152
- *
28153
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28154
- * window unstamped — that half-state would re-send the whole attached period
28155
- * on the next attach, which is the failure the boundary exists to prevent.
28156
- */
28157
- closeAttachedWindow(attachedAtMs, atMs) {
28158
- this.ensureRowStmt.run();
28159
- withTransaction(
28160
- this.db,
28161
- () => {
28162
- const row = getRow(this.fingerprintStmt);
28163
- const from = row?.backlogBefore ?? attachedAtMs;
28164
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28165
- this.releaseBoundaryStmt.run();
28166
- },
28167
- "IMMEDIATE"
28168
- );
28169
- }
28170
- /**
28171
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28172
- *
28173
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28174
- * different deployment and therefore discards what was delivered to the old
28175
- * one: here the recipient is the same, so everything already sent to it stays
28176
- * sent.
28177
- */
28178
- freezeBoundary(backlogBefore) {
28179
- this.ensureRowStmt.run();
28180
- this.freezeBoundaryStmt.run({ backlogBefore });
28181
- }
28182
- /** Take the claim, or report that someone live already holds it. */
28183
- claim(pid, host, nowMs, staleAfterMs) {
28184
- this.ensureRowStmt.run();
28185
- let taken = false;
28186
- withTransaction(
28187
- this.db,
28188
- () => {
28189
- const result = this.claimStmt.run({
28190
- pid,
28191
- host,
28192
- now: nowMs,
28193
- staleBefore: nowMs - staleAfterMs
28194
- });
28195
- taken = result.changes === 1;
28196
- },
28197
- "IMMEDIATE"
28198
- );
28199
- return taken;
28200
- }
28201
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28202
- heartbeat(pid, nowMs) {
28203
- this.heartbeatStmt.run({ now: nowMs, pid });
28204
- }
28205
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28206
- release(pid) {
28207
- this.releaseStmt.run({ pid });
28208
- }
28209
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28210
- lease() {
28211
- return getRow(this.leaseStmt);
28212
- }
28213
- };
28214
-
28215
28826
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28216
28827
  var SqliteInspectionDefinitionsRepository = class {
28217
28828
  constructor(db) {
@@ -28439,6 +29050,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28439
29050
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28440
29051
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28441
29052
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29053
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28442
29054
  if (values.vaultConsent !== void 0) {
28443
29055
  merged.vaultConsent = values.vaultConsent ? (
28444
29056
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30946,7 +31558,7 @@ var SqliteSecurityRepository = class {
30946
31558
  ELSE 0
30947
31559
  END) AS open_at_rest
30948
31560
  FROM inspection_findings f
30949
- JOIN audit_events e ON e.id = f.audit_event_id
31561
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30950
31562
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30951
31563
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30952
31564
  ON latest.finding_key = f.finding_key
@@ -31172,7 +31784,7 @@ var SqliteSecurityRepository = class {
31172
31784
  this.db.prepare(
31173
31785
  `SELECT e.repo AS repo, count(*) AS c
31174
31786
  FROM inspection_findings f
31175
- JOIN audit_events e ON e.id = f.audit_event_id
31787
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31176
31788
  WHERE e.started_at >= :from AND e.started_at < :to
31177
31789
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31178
31790
  AND e.repo IS NOT NULL
@@ -31296,7 +31908,7 @@ var SqliteSecurityRepository = class {
31296
31908
  d.severity AS severity,
31297
31909
  COUNT(*) AS count
31298
31910
  FROM inspection_findings f
31299
- JOIN audit_events e ON e.id = f.audit_event_id
31911
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31300
31912
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31301
31913
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31302
31914
  ON latest.finding_key = f.finding_key
@@ -31331,7 +31943,7 @@ var SqliteSecurityRepository = class {
31331
31943
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31332
31944
  d.rule_id AS rule_id, d.category AS category
31333
31945
  FROM inspection_findings f
31334
- JOIN audit_events e ON e.id = f.audit_event_id
31946
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31335
31947
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31336
31948
  WHERE e.started_at >= :from AND e.started_at < :to
31337
31949
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32172,6 +32784,7 @@ function openWithPragmas(file2) {
32172
32784
  db.exec("PRAGMA journal_mode = WAL");
32173
32785
  db.exec("PRAGMA busy_timeout = 2000");
32174
32786
  db.exec("PRAGMA foreign_keys = ON");
32787
+ registerSqlFunctions(db);
32175
32788
  } catch (err) {
32176
32789
  closeQuietly(db);
32177
32790
  throw err;
@@ -32201,7 +32814,7 @@ function backupLegacyStore(db, file2) {
32201
32814
  discardStore(file2, backup);
32202
32815
  return backup;
32203
32816
  }
32204
- function openAndInitialize(file2, base) {
32817
+ function openAndInitialize(file2, base, skipTags) {
32205
32818
  let db = openWithPragmas(file2);
32206
32819
  try {
32207
32820
  if (isForeignSqliteLineage(db)) {
@@ -32211,7 +32824,7 @@ function openAndInitialize(file2, base) {
32211
32824
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32212
32825
  );
32213
32826
  }
32214
- applyMigrations(db, file2);
32827
+ applyMigrations(db, file2, { skipTags });
32215
32828
  tightenPerms(file2);
32216
32829
  const policies = new SqlitePoliciesRepository(db);
32217
32830
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32226,6 +32839,7 @@ function openAndInitialize(file2, base) {
32226
32839
  exceptions: new SqliteExceptionsRepository(db),
32227
32840
  resolutions: new SqliteResolutionsRepository(db),
32228
32841
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32842
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32229
32843
  security: new SqliteSecurityRepository(db),
32230
32844
  detections: new SqliteDetectionsRepository(db),
32231
32845
  shares: new SqliteSharesRepository(db),
@@ -32248,7 +32862,8 @@ function openAndInitialize(file2, base) {
32248
32862
  throw err;
32249
32863
  }
32250
32864
  }
32251
- function openLocalDatabase(dir) {
32865
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32866
+ function openLocalDatabase(dir, options = {}) {
32252
32867
  ensureDataDirSync(dir);
32253
32868
  const file2 = join7(dir, DB_FILENAME);
32254
32869
  reapStalePartials(file2);
@@ -32260,6 +32875,7 @@ function openLocalDatabase(dir) {
32260
32875
  installedPacks,
32261
32876
  scanLedger,
32262
32877
  historySync,
32878
+ bodyRetention,
32263
32879
  secretVault,
32264
32880
  exceptions,
32265
32881
  resolutions,
@@ -32283,7 +32899,8 @@ function openLocalDatabase(dir) {
32283
32899
  // `dir` is always `<base>/data` — every caller resolves it through
32284
32900
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32285
32901
  // settings/ and data/, and the pack-policy floor needs both halves.
32286
- dirname2(dir)
32902
+ dirname2(dir),
32903
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32287
32904
  );
32288
32905
  function captureRowId(event) {
32289
32906
  return captureId(
@@ -32476,6 +33093,7 @@ function openLocalDatabase(dir) {
32476
33093
  installedPacks,
32477
33094
  scanLedger,
32478
33095
  historySync,
33096
+ bodyRetention,
32479
33097
  secretVault,
32480
33098
  exceptions,
32481
33099
  resolutions,
@@ -32516,8 +33134,35 @@ function openLocalDatabase(dir) {
32516
33134
 
32517
33135
  // ../../packages/persistence/src/egress-wire.ts
32518
33136
  import { createHash as createHash3 } from "crypto";
33137
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33138
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33139
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33140
+ var FILE_URL = /^file:\/\//i;
33141
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33142
+ var SLASH = "/".charCodeAt(0);
33143
+ var GIT_SUFFIX = ".git";
33144
+ function trimSlashes(path) {
33145
+ let start = 0;
33146
+ let end = path.length;
33147
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33148
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33149
+ return path.slice(start, end);
33150
+ }
33151
+ function canonicalGitUrl(url2) {
33152
+ const trimmed = url2.trim();
33153
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33154
+ const scheme = SCHEME_FORM.exec(trimmed);
33155
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33156
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33157
+ if (host === void 0) return trimmed;
33158
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33159
+ const bare = trimSlashes(path);
33160
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33161
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33162
+ }
32519
33163
  function hashProjectKey(projectKey) {
32520
- return createHash3("sha256").update(projectKey, "utf8").digest("hex");
33164
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33165
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
32521
33166
  }
32522
33167
  function toIngestHit(hit) {
32523
33168
  return {
@@ -32684,18 +33329,50 @@ function fingerprintValue(key, raw) {
32684
33329
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32685
33330
  }
32686
33331
 
33332
+ // ../../packages/persistence/src/forward-health.ts
33333
+ import { readFileSync as readFileSync7 } from "fs";
33334
+ import { join as join9 } from "path";
33335
+ var FAILURES = /* @__PURE__ */ new Set([
33336
+ "unauthorized",
33337
+ "forbidden",
33338
+ "unreachable"
33339
+ ]);
33340
+ var BREAKER_COOLDOWN_MS = 3e4;
33341
+ function parseForwardHealth(raw, nowMs) {
33342
+ try {
33343
+ const parsed2 = JSON.parse(raw);
33344
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33345
+ const record2 = parsed2;
33346
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33347
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33348
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33349
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33350
+ } catch {
33351
+ return null;
33352
+ }
33353
+ }
33354
+ function isForwardPaused(health, nowMs) {
33355
+ const openedAtMs = health?.openedAtMs ?? null;
33356
+ if (openedAtMs === null) return false;
33357
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33358
+ }
33359
+
32687
33360
  // ../../packages/persistence/src/history-backfill.ts
32688
33361
  import { existsSync as existsSync4 } from "fs";
32689
- import { join as join9 } from "path";
33362
+ import { join as join10 } from "path";
32690
33363
 
32691
33364
  // ../../packages/persistence/src/history-preview.ts
32692
33365
  import { existsSync as existsSync5 } from "fs";
32693
- import { join as join10 } from "path";
33366
+ import { join as join11 } from "path";
32694
33367
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32695
33368
 
33369
+ // ../../packages/persistence/src/history-sync-state.ts
33370
+ import { readFileSync as readFileSync8 } from "fs";
33371
+ import { join as join12 } from "path";
33372
+
32696
33373
  // ../../packages/persistence/src/store-symlinks.ts
32697
33374
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32698
- import { dirname as dirname3, join as join11, resolve } from "path";
33375
+ import { dirname as dirname3, join as join13, resolve } from "path";
32699
33376
 
32700
33377
  // ../../packages/persistence/src/vault/crypto.ts
32701
33378
  import {
@@ -32709,19 +33386,19 @@ import {
32709
33386
  // ../../packages/persistence/src/vault/key-provider.ts
32710
33387
  import { execFileSync } from "child_process";
32711
33388
  import { randomBytes as randomBytes2 } from "crypto";
32712
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32713
- import { join as join12 } from "path";
33389
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33390
+ import { join as join14 } from "path";
32714
33391
 
32715
33392
  // ../../packages/persistence/src/vault/vault.ts
32716
33393
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32717
33394
 
32718
33395
  // ../../packages/persistence/src/warn-era-cap.ts
32719
33396
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
32720
- import { join as join13 } from "path";
33397
+ import { join as join15 } from "path";
32721
33398
  var MARKER = "warn-era-capped";
32722
33399
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32723
33400
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32724
- const marker = join13(dataDir2, MARKER);
33401
+ const marker = join15(dataDir2, MARKER);
32725
33402
  if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32726
33403
  const capped = db.policies.capCategoryActions();
32727
33404
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
@@ -32781,7 +33458,7 @@ function resolveProvider() {
32781
33458
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32782
33459
  try {
32783
33460
  ensureLayoutDirSync(base);
32784
- const settingsFile = join14(settingsDir(base), "settings.json");
33461
+ const settingsFile = join16(settingsDir(base), "settings.json");
32785
33462
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
32786
33463
  } catch {
32787
33464
  }
@@ -32805,9 +33482,9 @@ function resolveProviderSafe(resolveProviderFn) {
32805
33482
  }
32806
33483
 
32807
33484
  // ../../packages/plugin-sdk/src/config-inventory.ts
32808
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33485
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32809
33486
  import { homedir as homedir2 } from "os";
32810
- import { basename as basename3, join as join16 } from "path";
33487
+ import { basename as basename3, join as join18 } from "path";
32811
33488
 
32812
33489
  // ../../packages/detections/src/egress/registry.ts
32813
33490
  var EXTRACTOR_VERSION = "1";
@@ -33500,12 +34177,18 @@ var EGRESS_CODE_EXTENSIONS = /* @__PURE__ */ new Set([
33500
34177
  ".rs"
33501
34178
  ]);
33502
34179
  var SNIPPET_MAX = 200;
34180
+ var WHITESPACE = /\s/;
33503
34181
  var MASK = "\u2022\u2022\u2022\u2022";
33504
34182
  var URL_CANDIDATE = /(https?|wss?|sftp|grpcs?|smtp):\/\/(?:[^\s'"`<>()[\]{},;]|\$\{[^}\s]{1,64}\}|\{[A-Za-z_]\w{0,63}\}|%[sd])+/gi;
33505
34183
  var PLACEHOLDER = /\$\{[^}\s]{1,64}\}|\{[A-Za-z_]\w{0,63}\}|%[sd]/g;
33506
34184
  var VAR_TOKEN = "${var}";
33507
34185
  var VAR_SENTINEL = "akaegressvar0";
33508
- var TRAILING_PUNCTUATION = /[.,;:'"]+$/;
34186
+ var TRAILING_PUNCTUATION = `.,;:'"`;
34187
+ function stripTrailingPunctuation(value) {
34188
+ let end = value.length;
34189
+ while (end > 0 && TRAILING_PUNCTUATION.includes(value.charAt(end - 1))) end -= 1;
34190
+ return end === value.length ? value : value.slice(0, end);
34191
+ }
33509
34192
  var TRANSPORT_BY_SCHEME = {
33510
34193
  http: "http",
33511
34194
  https: "https",
@@ -33570,35 +34253,100 @@ var VENDORED_PATH = /(^|\/)(vendor|third_party|external)\//;
33570
34253
  function isVendoredPath(file2) {
33571
34254
  return VENDORED_PATH.test(file2);
33572
34255
  }
33573
- function redactLine(line) {
33574
- return line.trim().replace(USERINFO, "://").replace(WEBHOOK_URL, `$1${MASK}`).replace(SECRET_VALUE, `$1${MASK}`).replace(AUTH_SCHEME_VALUE, `$1$2 ${MASK}`).replace(BEARER_TOKEN, `Bearer ${MASK}`);
34256
+ var REDACTION_PASSES = [
34257
+ { pattern: USERINFO, replace: () => "://" },
34258
+ { pattern: WEBHOOK_URL, replace: (m) => `${m[1] ?? ""}${MASK}` },
34259
+ { pattern: SECRET_VALUE, replace: (m) => `${m[1] ?? ""}${MASK}` },
34260
+ { pattern: AUTH_SCHEME_VALUE, replace: (m) => `${m[1] ?? ""}${m[2] ?? ""} ${MASK}` },
34261
+ { pattern: BEARER_TOKEN, replace: () => `Bearer ${MASK}` }
34262
+ ];
34263
+ function runPass(input2, pass) {
34264
+ const at = [];
34265
+ const end = [];
34266
+ const before = [];
34267
+ const after = [];
34268
+ let output2 = "";
34269
+ let copied = 0;
34270
+ let shift = 0;
34271
+ for (const match of input2.matchAll(pass.pattern)) {
34272
+ const start = match.index;
34273
+ const matched = match[0];
34274
+ const replacement = pass.replace(match);
34275
+ output2 += input2.slice(copied, start) + replacement;
34276
+ at.push(start);
34277
+ end.push(start + matched.length);
34278
+ before.push(shift);
34279
+ shift += replacement.length - matched.length;
34280
+ after.push(shift);
34281
+ copied = start + matched.length;
34282
+ }
34283
+ if (at.length === 0) return { output: input2, edits: { at, end, before, after } };
34284
+ return { output: output2 + input2.slice(copied), edits: { at, end, before, after } };
34285
+ }
34286
+ function throughPass(edits, offset) {
34287
+ let low = 0;
34288
+ let high = edits.at.length - 1;
34289
+ let found = -1;
34290
+ while (low <= high) {
34291
+ const mid = low + high >> 1;
34292
+ if ((edits.at[mid] ?? 0) <= offset) {
34293
+ found = mid;
34294
+ low = mid + 1;
34295
+ } else {
34296
+ high = mid - 1;
34297
+ }
34298
+ }
34299
+ if (found === -1) return offset;
34300
+ if (offset < (edits.end[found] ?? 0)) return (edits.at[found] ?? 0) + (edits.before[found] ?? 0);
34301
+ return offset + (edits.after[found] ?? 0);
33575
34302
  }
33576
- function redactSnippet(line, anchor2 = 0) {
33577
- const redacted = redactLine(line);
33578
- if (redacted.length <= SNIPPET_MAX) return redacted;
33579
- const lead = line.length - line.trimStart().length;
34303
+ function redactedLineOf(line) {
33580
34304
  const trimmed = line.trim();
33581
- const mapped = redacted.length === trimmed.length ? anchor2 - lead : redactLine(trimmed.slice(0, Math.max(0, anchor2 - lead))).length;
34305
+ const edits = [];
34306
+ let text = trimmed;
34307
+ for (const pass of REDACTION_PASSES) {
34308
+ const result = runPass(text, pass);
34309
+ text = result.output;
34310
+ edits.push(result.edits);
34311
+ }
34312
+ if (text.length <= SNIPPET_MAX) return { redacted: text };
34313
+ return {
34314
+ redacted: text,
34315
+ window: { trimmed, edits, lead: line.length - line.trimStart().length }
34316
+ };
34317
+ }
34318
+ function snippetWindow({ redacted, window }, anchor2) {
34319
+ if (window === void 0) return redacted;
34320
+ const { trimmed, edits, lead } = window;
34321
+ let mapped = Math.max(0, anchor2 - lead);
34322
+ if (redacted.length !== trimmed.length) {
34323
+ while (mapped > 0 && WHITESPACE.test(trimmed.charAt(mapped - 1))) mapped -= 1;
34324
+ for (const pass of edits) mapped = throughPass(pass, mapped);
34325
+ }
33582
34326
  const start = Math.min(
33583
34327
  Math.max(0, mapped - Math.floor(SNIPPET_MAX / 2)),
33584
34328
  redacted.length - SNIPPET_MAX
33585
34329
  );
33586
34330
  return redacted.slice(start, start + SNIPPET_MAX);
33587
34331
  }
34332
+ function redactSnippet(line, anchor2 = 0) {
34333
+ return snippetWindow(redactedLineOf(line), anchor2);
34334
+ }
33588
34335
  function extractEgress(text) {
33589
34336
  const lineStarts = lineStartOffsets(text);
33590
34337
  const urlSpans = [];
33591
34338
  const hits = [];
33592
34339
  const lineTextOf = memoizeByLine((index) => lineTextAt(text, lineStarts, index));
33593
34340
  const ipContextOf = memoizeByLine((index) => ipLineContext(lineTextOf(index)));
33594
- const snippetAt = (index, offset) => redactSnippet(lineTextOf(index), offset - (lineStarts[index] ?? 0));
34341
+ const redactedOf = memoizeByLine((index) => redactedLineOf(lineTextOf(index)));
34342
+ const snippetAt = (index, offset) => snippetWindow(redactedOf(index), offset - (lineStarts[index] ?? 0));
33595
34343
  for (const match of text.matchAll(URL_CANDIDATE)) {
33596
34344
  const start = match.index;
33597
34345
  const matched = match[0];
33598
34346
  urlSpans.push([start, start + matched.length]);
33599
34347
  const scheme = match[1];
33600
34348
  if (scheme === void 0) continue;
33601
- const candidate = matched.replace(TRAILING_PUNCTUATION, "");
34349
+ const candidate = stripTrailingPunctuation(matched);
33602
34350
  if (candidate === "") continue;
33603
34351
  const parsed2 = parseCandidate(candidate, scheme);
33604
34352
  if (parsed2 === null) continue;
@@ -33832,33 +34580,37 @@ function extractManifestSdks(text, kind) {
33832
34580
  return [];
33833
34581
  }
33834
34582
  }
33835
- function makeHit(ecosystem, pkg, line, rawLine) {
33836
- return { ecosystem, pkg, line, snippet: redactSnippet(rawLine) };
34583
+ function makeHit(ecosystem, pkg, line, snippet) {
34584
+ return { ecosystem, pkg, line, snippet };
33837
34585
  }
33838
34586
  function extractPackageJson(text) {
33839
34587
  const parsed2 = parseJson(text);
33840
34588
  if (parsed2 === null) return [];
33841
34589
  const seen = /* @__PURE__ */ new Set();
33842
34590
  const hits = [];
34591
+ const lines = manifestLines(text);
34592
+ const tokens = quotedTokenOffsets(text);
34593
+ const dependenciesAt = sectionOffset(text, "dependencies");
34594
+ const optionalAt = sectionOffset(text, "optionalDependencies");
33843
34595
  for (const pkg of objectKeys(parsed2.dependencies)) {
33844
34596
  seen.add(pkg);
33845
- hits.push(hitAtQuotedKey("npm", pkg, text, "dependencies"));
34597
+ hits.push(hitAtQuotedKey("npm", pkg, text, dependenciesAt, lines, tokens));
33846
34598
  }
33847
34599
  for (const pkg of objectKeys(parsed2.optionalDependencies)) {
33848
34600
  if (seen.has(pkg)) continue;
33849
34601
  seen.add(pkg);
33850
- hits.push(hitAtQuotedKey("npm", pkg, text, "optionalDependencies"));
34602
+ hits.push(hitAtQuotedKey("npm", pkg, text, optionalAt, lines, tokens));
33851
34603
  }
33852
34604
  return hits;
33853
34605
  }
33854
34606
  var REQUIREMENTS_NAME = /^\s*([A-Za-z0-9][\w.-]*)/;
33855
34607
  function extractRequirementsTxt(text) {
33856
34608
  const hits = [];
33857
- eachLine(text, (rawLine, lineNumber) => {
34609
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33858
34610
  const match = REQUIREMENTS_NAME.exec(rawLine);
33859
34611
  const name = match?.[1];
33860
34612
  if (name === void 0) return;
33861
- hits.push(makeHit("pypi", normalizePypi(name), lineNumber, rawLine));
34613
+ hits.push(makeHit("pypi", normalizePypi(name), lineNumber, snippet()));
33862
34614
  });
33863
34615
  return hits;
33864
34616
  }
@@ -33870,7 +34622,7 @@ function extractPyprojectToml(text) {
33870
34622
  const hits = [];
33871
34623
  let section = "";
33872
34624
  let inDependenciesArray = false;
33873
- eachLine(text, (rawLine, lineNumber) => {
34625
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33874
34626
  const sectionMatch = TOML_SECTION.exec(rawLine);
33875
34627
  if (sectionMatch) {
33876
34628
  section = sectionMatch[1]?.trim() ?? "";
@@ -33884,7 +34636,7 @@ function extractPyprojectToml(text) {
33884
34636
  for (const spec of quotedStrings(rawLine)) {
33885
34637
  const name = PEP508_NAME.exec(spec)?.[1];
33886
34638
  if (name !== void 0)
33887
- hits.push(makeHit("pypi", normalizePypi(name), lineNumber, rawLine));
34639
+ hits.push(makeHit("pypi", normalizePypi(name), lineNumber, snippet()));
33888
34640
  }
33889
34641
  if (rawLine.includes("]")) inDependenciesArray = false;
33890
34642
  return;
@@ -33892,7 +34644,7 @@ function extractPyprojectToml(text) {
33892
34644
  if (section === "tool.poetry.dependencies") {
33893
34645
  const key = POETRY_KEY.exec(rawLine)?.[1];
33894
34646
  if (key !== void 0 && key !== "python") {
33895
- hits.push(makeHit("pypi", normalizePypi(key), lineNumber, rawLine));
34647
+ hits.push(makeHit("pypi", normalizePypi(key), lineNumber, snippet()));
33896
34648
  }
33897
34649
  }
33898
34650
  });
@@ -33908,7 +34660,7 @@ var GO_MODULE_VERSION_LINE = /^\s*([\w./-]+)\s+v\d/;
33908
34660
  function extractGoMod(text) {
33909
34661
  const hits = [];
33910
34662
  let blockKeyword = null;
33911
- eachLine(text, (rawLine, lineNumber) => {
34663
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33912
34664
  if (blockKeyword === null) {
33913
34665
  const open3 = GO_BLOCK_OPEN.exec(rawLine)?.[1];
33914
34666
  if (open3 !== void 0) {
@@ -33916,7 +34668,7 @@ function extractGoMod(text) {
33916
34668
  return;
33917
34669
  }
33918
34670
  const path = GO_REQUIRE_SINGLE_LINE.exec(rawLine)?.[1];
33919
- if (path !== void 0) hits.push(makeHit("go", path, lineNumber, rawLine));
34671
+ if (path !== void 0) hits.push(makeHit("go", path, lineNumber, snippet()));
33920
34672
  return;
33921
34673
  }
33922
34674
  if (GO_BLOCK_CLOSE.test(rawLine)) {
@@ -33925,7 +34677,7 @@ function extractGoMod(text) {
33925
34677
  }
33926
34678
  if (blockKeyword === "require") {
33927
34679
  const path = GO_MODULE_VERSION_LINE.exec(rawLine)?.[1];
33928
- if (path !== void 0) hits.push(makeHit("go", path, lineNumber, rawLine));
34680
+ if (path !== void 0) hits.push(makeHit("go", path, lineNumber, snippet()));
33929
34681
  }
33930
34682
  });
33931
34683
  return hits;
@@ -33942,13 +34694,13 @@ var POM_CONTEXT_TAGS = /* @__PURE__ */ new Set([
33942
34694
  "exclusions",
33943
34695
  "exclusion"
33944
34696
  ]);
33945
- var XML_TAG = /<(\/?)([A-Za-z][\w.-]*)([^<>]*)>/g;
34697
+ var XML_TAG = /<(\/?)([A-Za-z][\w.-]*)((?:[^<>\w.-][^<>]*)?)>/g;
33946
34698
  var LEADING_TEXT = /^([^<]*)/;
33947
34699
  function extractPomXml(text) {
33948
34700
  const hits = [];
33949
34701
  const stack = [];
33950
34702
  let inComment = false;
33951
- eachLine(text, (rawLine, lineNumber) => {
34703
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33952
34704
  const stripped = stripXmlComments(rawLine, inComment);
33953
34705
  inComment = stripped.inComment;
33954
34706
  const visible = stripped.visible;
@@ -33967,7 +34719,7 @@ function extractPomXml(text) {
33967
34719
  const after = visible.slice(match.index + match[0].length);
33968
34720
  const value = LEADING_TEXT.exec(after)?.[1]?.trim() ?? "";
33969
34721
  if (value !== "" && isProjectDependencyGroupId(stack)) {
33970
- hits.push(makeHit("maven", value, lineNumber, rawLine));
34722
+ hits.push(makeHit("maven", value, lineNumber, snippet()));
33971
34723
  }
33972
34724
  continue;
33973
34725
  }
@@ -33983,11 +34735,11 @@ var GRADLE_DEPENDENCY = /\b(?:implementation|api|compile)\b\s*[('"]*(?:platform\
33983
34735
  var LINE_COMMENT = /^\s*\/\//;
33984
34736
  function extractBuildGradle(text) {
33985
34737
  const hits = [];
33986
- eachLine(text, (rawLine, lineNumber) => {
34738
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33987
34739
  if (LINE_COMMENT.test(rawLine)) return;
33988
34740
  for (const match of rawLine.matchAll(GRADLE_DEPENDENCY)) {
33989
34741
  const groupId = match[1];
33990
- if (groupId !== void 0) hits.push(makeHit("maven", groupId, lineNumber, rawLine));
34742
+ if (groupId !== void 0) hits.push(makeHit("maven", groupId, lineNumber, snippet()));
33991
34743
  }
33992
34744
  });
33993
34745
  return hits;
@@ -33995,9 +34747,9 @@ function extractBuildGradle(text) {
33995
34747
  var GEMFILE_DEPENDENCY = /^\s*gem\s+['"]([\w-]+)['"]/;
33996
34748
  function extractGemfile(text) {
33997
34749
  const hits = [];
33998
- eachLine(text, (rawLine, lineNumber) => {
34750
+ eachLine(text, (rawLine, lineNumber, snippet) => {
33999
34751
  const name = GEMFILE_DEPENDENCY.exec(rawLine)?.[1];
34000
- if (name !== void 0) hits.push(makeHit("rubygems", name, lineNumber, rawLine));
34752
+ if (name !== void 0) hits.push(makeHit("rubygems", name, lineNumber, snippet()));
34001
34753
  });
34002
34754
  return hits;
34003
34755
  }
@@ -34005,7 +34757,7 @@ var CARGO_KEY = /^([A-Za-z0-9_-]+)\s*=/;
34005
34757
  function extractCargoToml(text) {
34006
34758
  const hits = [];
34007
34759
  let mode = "none";
34008
- eachLine(text, (rawLine, lineNumber) => {
34760
+ eachLine(text, (rawLine, lineNumber, snippet) => {
34009
34761
  const sectionMatch = TOML_SECTION.exec(rawLine);
34010
34762
  if (sectionMatch) {
34011
34763
  const name = sectionMatch[1]?.trim() ?? "";
@@ -34014,7 +34766,7 @@ function extractCargoToml(text) {
34014
34766
  } else if (name.startsWith("dependencies.")) {
34015
34767
  mode = "dotted";
34016
34768
  const crate = name.slice("dependencies.".length);
34017
- if (crate !== "") hits.push(makeHit("cargo", crate, lineNumber, rawLine));
34769
+ if (crate !== "") hits.push(makeHit("cargo", crate, lineNumber, snippet()));
34018
34770
  } else {
34019
34771
  mode = "none";
34020
34772
  }
@@ -34022,7 +34774,7 @@ function extractCargoToml(text) {
34022
34774
  }
34023
34775
  if (mode === "plain") {
34024
34776
  const crate = CARGO_KEY.exec(rawLine)?.[1];
34025
- if (crate !== void 0) hits.push(makeHit("cargo", crate, lineNumber, rawLine));
34777
+ if (crate !== void 0) hits.push(makeHit("cargo", crate, lineNumber, snippet()));
34026
34778
  }
34027
34779
  });
34028
34780
  return hits;
@@ -34031,17 +34783,20 @@ function extractComposerJson(text) {
34031
34783
  const parsed2 = parseJson(text);
34032
34784
  if (parsed2 === null) return [];
34033
34785
  const pkgs = objectKeys(parsed2.require).filter((pkg) => pkg !== "php" && !pkg.startsWith("ext-"));
34034
- return pkgs.map((pkg) => hitAtQuotedKey("composer", pkg, text, "require"));
34786
+ const lines = manifestLines(text);
34787
+ const tokens = quotedTokenOffsets(text);
34788
+ const requireAt = sectionOffset(text, "require");
34789
+ return pkgs.map((pkg) => hitAtQuotedKey("composer", pkg, text, requireAt, lines, tokens));
34035
34790
  }
34036
34791
  var CSPROJ_PACKAGE_REFERENCE = /<PackageReference\s+Include="([^"]+)"/;
34037
34792
  function extractCsproj(text) {
34038
34793
  const hits = [];
34039
34794
  let inComment = false;
34040
- eachLine(text, (rawLine, lineNumber) => {
34795
+ eachLine(text, (rawLine, lineNumber, snippet) => {
34041
34796
  const stripped = stripXmlComments(rawLine, inComment);
34042
34797
  inComment = stripped.inComment;
34043
34798
  const name = CSPROJ_PACKAGE_REFERENCE.exec(stripped.visible)?.[1];
34044
- if (name !== void 0) hits.push(makeHit("nuget", name, lineNumber, rawLine));
34799
+ if (name !== void 0) hits.push(makeHit("nuget", name, lineNumber, snippet()));
34045
34800
  });
34046
34801
  return hits;
34047
34802
  }
@@ -34049,11 +34804,11 @@ var PACKAGES_CONFIG_PACKAGE = /<package\s+id="([^"]+)"/;
34049
34804
  function extractPackagesConfig(text) {
34050
34805
  const hits = [];
34051
34806
  let inComment = false;
34052
- eachLine(text, (rawLine, lineNumber) => {
34807
+ eachLine(text, (rawLine, lineNumber, snippet) => {
34053
34808
  const stripped = stripXmlComments(rawLine, inComment);
34054
34809
  inComment = stripped.inComment;
34055
34810
  const name = PACKAGES_CONFIG_PACKAGE.exec(stripped.visible)?.[1];
34056
- if (name !== void 0) hits.push(makeHit("nuget", name, lineNumber, rawLine));
34811
+ if (name !== void 0) hits.push(makeHit("nuget", name, lineNumber, snippet()));
34057
34812
  });
34058
34813
  return hits;
34059
34814
  }
@@ -34079,7 +34834,9 @@ function stripXmlComments(line, inComment) {
34079
34834
  function eachLine(text, fn) {
34080
34835
  const lines = text.split("\n");
34081
34836
  for (let i = 0; i < lines.length; i += 1) {
34082
- fn(lines[i] ?? "", i + 1);
34837
+ const rawLine = lines[i] ?? "";
34838
+ let cached2;
34839
+ fn(rawLine, i + 1, () => cached2 ??= redactSnippet(rawLine));
34083
34840
  }
34084
34841
  }
34085
34842
  function parseJson(text) {
@@ -34101,24 +34858,76 @@ function objectKeys(value) {
34101
34858
  if (typeof value !== "object" || value === null) return [];
34102
34859
  return Object.keys(value);
34103
34860
  }
34104
- function hitAtQuotedKey(ecosystem, pkg, text, sectionKey) {
34105
- const sectionStart = text.indexOf(`"${sectionKey}"`);
34106
- const searchFrom = sectionStart === -1 ? 0 : sectionStart;
34107
- const index = text.indexOf(`"${pkg}"`, searchFrom);
34108
- if (index === -1) return makeHit(ecosystem, pkg, 1, pkg);
34109
- return makeHit(ecosystem, pkg, lineNumberAt(text, index), lineContaining(text, index));
34861
+ function manifestLines(text) {
34862
+ const starts = lineStartOffsets(text);
34863
+ const snippets = /* @__PURE__ */ new Map();
34864
+ return {
34865
+ numberAt: (index) => lineIndexAt(starts, index) + 1,
34866
+ snippetAt: (index) => {
34867
+ const line = lineIndexAt(starts, index);
34868
+ const cached2 = snippets.get(line);
34869
+ if (cached2 !== void 0) return cached2;
34870
+ const value = redactSnippet(lineTextAt(text, starts, line));
34871
+ snippets.set(line, value);
34872
+ return value;
34873
+ }
34874
+ };
34875
+ }
34876
+ function sectionOffset(text, sectionKey) {
34877
+ const at = text.indexOf(`"${sectionKey}"`);
34878
+ return at === -1 ? 0 : at;
34879
+ }
34880
+ var QUOTE = 34;
34881
+ var BACKSLASH = 92;
34882
+ function quotedTokenOffsets(text) {
34883
+ const at = /* @__PURE__ */ new Map();
34884
+ for (let i = 0; i < text.length; i += 1) {
34885
+ if (text.charCodeAt(i) !== QUOTE) continue;
34886
+ let end = i + 1;
34887
+ while (end < text.length && text.charCodeAt(end) !== QUOTE) {
34888
+ end += text.charCodeAt(end) === BACKSLASH ? 2 : 1;
34889
+ }
34890
+ if (end >= text.length) break;
34891
+ const inner = text.slice(i + 1, end);
34892
+ const name = inner.includes("\\") ? decodeJsonString(text.slice(i, end + 1)) : inner;
34893
+ if (name !== void 0) {
34894
+ const seen = at.get(name);
34895
+ if (seen === void 0) at.set(name, [i]);
34896
+ else seen.push(i);
34897
+ }
34898
+ i = end;
34899
+ }
34900
+ return at;
34901
+ }
34902
+ function decodeJsonString(quoted) {
34903
+ try {
34904
+ return JSON.parse(quoted);
34905
+ } catch {
34906
+ return void 0;
34907
+ }
34110
34908
  }
34111
- function lineNumberAt(text, index) {
34112
- let line = 1;
34113
- for (let i = 0; i < index; i += 1) {
34114
- if (text[i] === "\n") line += 1;
34909
+ function firstAtOrAfter(offsets, from) {
34910
+ let low = 0;
34911
+ let high = offsets.length - 1;
34912
+ let found;
34913
+ while (low <= high) {
34914
+ const mid = low + high >> 1;
34915
+ const at = offsets[mid] ?? 0;
34916
+ if (at >= from) {
34917
+ found = at;
34918
+ high = mid - 1;
34919
+ } else {
34920
+ low = mid + 1;
34921
+ }
34115
34922
  }
34116
- return line;
34923
+ return found;
34117
34924
  }
34118
- function lineContaining(text, index) {
34119
- const start = text.lastIndexOf("\n", index) + 1;
34120
- const end = text.indexOf("\n", index);
34121
- return text.slice(start, end === -1 ? text.length : end);
34925
+ function hitAtQuotedKey(ecosystem, pkg, text, searchFrom, lines, tokens) {
34926
+ const offsets = tokens.get(pkg);
34927
+ const known = offsets === void 0 ? void 0 : firstAtOrAfter(offsets, searchFrom);
34928
+ const index = known ?? text.indexOf(`"${pkg}"`, searchFrom);
34929
+ if (index === -1) return makeHit(ecosystem, pkg, 1, redactSnippet(pkg));
34930
+ return { ecosystem, pkg, line: lines.numberAt(index), snippet: lines.snippetAt(index) };
34122
34931
  }
34123
34932
 
34124
34933
  // ../../packages/detections/src/egress/resolve.ts
@@ -36731,8 +37540,8 @@ function bundledDetections() {
36731
37540
  }
36732
37541
 
36733
37542
  // ../../packages/plugin-sdk/src/repo.ts
36734
- import { existsSync as existsSync9, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
36735
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
37543
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
37544
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
36736
37545
  function resolveRepoIdentity(cwd) {
36737
37546
  try {
36738
37547
  const root = findGitRoot(cwd);
@@ -36761,36 +37570,36 @@ function resolveWorktreeRoot(cwd) {
36761
37570
  function findGitRoot(start) {
36762
37571
  let dir = start;
36763
37572
  for (; ; ) {
36764
- if (existsSync9(join15(dir, ".git"))) return dir;
37573
+ if (existsSync9(join17(dir, ".git"))) return dir;
36765
37574
  const parent = dirname4(dir);
36766
37575
  if (parent === dir) return void 0;
36767
37576
  dir = parent;
36768
37577
  }
36769
37578
  }
36770
37579
  function resolveGitContext(root) {
36771
- const dotGit = join15(root, ".git");
37580
+ const dotGit = join17(root, ".git");
36772
37581
  try {
36773
37582
  if (statSync6(dotGit).isDirectory()) {
36774
- return { configPath: join15(dotGit, "config"), headRoot: root };
37583
+ return { configPath: join17(dotGit, "config"), headRoot: root };
36775
37584
  }
36776
37585
  } catch {
36777
37586
  return void 0;
36778
37587
  }
36779
37588
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
36780
37589
  if (!target) return void 0;
36781
- const gitdir = isAbsolute(target) ? target : join15(root, target);
36782
- if (existsSync9(join15(gitdir, "config"))) {
36783
- return { configPath: join15(gitdir, "config"), headRoot: root };
37590
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
37591
+ if (existsSync9(join17(gitdir, "config"))) {
37592
+ return { configPath: join17(gitdir, "config"), headRoot: root };
36784
37593
  }
36785
- const commonRaw = safeRead(join15(gitdir, "commondir"))?.trim();
37594
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
36786
37595
  if (!commonRaw) return void 0;
36787
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join15(gitdir, commonRaw);
37596
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
36788
37597
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
36789
- return { configPath: join15(commonGitDir, "config"), headRoot };
37598
+ return { configPath: join17(commonGitDir, "config"), headRoot };
36790
37599
  }
36791
37600
  function safeRead(path) {
36792
37601
  try {
36793
- return readFileSync8(path, "utf8");
37602
+ return readFileSync10(path, "utf8");
36794
37603
  } catch {
36795
37604
  return void 0;
36796
37605
  }
@@ -37334,8 +38143,8 @@ function createGuardedScanner(partition, gateway, opts) {
37334
38143
  }
37335
38144
 
37336
38145
  // ../../packages/plugin-sdk/src/host-floor.ts
37337
- import { readFileSync as readFileSync11 } from "fs";
37338
- import { join as join18 } from "path";
38146
+ import { readFileSync as readFileSync13 } from "fs";
38147
+ import { join as join20 } from "path";
37339
38148
 
37340
38149
  // ../../packages/plugin-sdk/src/model-governance.ts
37341
38150
  import {
@@ -37343,11 +38152,11 @@ import {
37343
38152
  fstatSync,
37344
38153
  mkdirSync as mkdirSync2,
37345
38154
  openSync as openSync2,
37346
- readFileSync as readFileSync10,
38155
+ readFileSync as readFileSync12,
37347
38156
  readSync,
37348
38157
  writeFileSync as writeFileSync5
37349
38158
  } from "fs";
37350
- import { join as join17 } from "path";
38159
+ import { join as join19 } from "path";
37351
38160
  var TAIL_BYTES = 256 * 1024;
37352
38161
 
37353
38162
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -37370,11 +38179,11 @@ var HOST_FLOORS = {
37370
38179
 
37371
38180
  // ../../packages/plugin-sdk/src/ignore-layers.ts
37372
38181
  var import_ignore = __toESM(require_ignore(), 1);
37373
- import { readFileSync as readFileSync12 } from "fs";
37374
- import { join as join19 } from "path";
38182
+ import { readFileSync as readFileSync14 } from "fs";
38183
+ import { join as join21 } from "path";
37375
38184
  function readIgnoreLayer(dir, filename, anchorLen) {
37376
38185
  try {
37377
- return { matcher: (0, import_ignore.default)().add(readFileSync12(join19(dir, filename), "utf8")), anchorLen };
38186
+ return { matcher: (0, import_ignore.default)().add(readFileSync14(join21(dir, filename), "utf8")), anchorLen };
37378
38187
  } catch {
37379
38188
  return void 0;
37380
38189
  }
@@ -37405,8 +38214,8 @@ function withLayer(layers, layer) {
37405
38214
  import { arch, hostname as hostname4, platform, release } from "os";
37406
38215
 
37407
38216
  // ../../packages/plugin-sdk/src/nudge.ts
37408
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
37409
- import { join as join20 } from "path";
38217
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
38218
+ import { join as join22 } from "path";
37410
38219
 
37411
38220
  // ../../packages/plugin-sdk/src/paths.ts
37412
38221
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -37479,7 +38288,7 @@ function createPolicyResolver(bundle) {
37479
38288
 
37480
38289
  // ../../packages/plugin-sdk/src/project-files.ts
37481
38290
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
37482
- import { basename as basename5, join as join21 } from "path";
38291
+ import { basename as basename5, join as join23 } from "path";
37483
38292
 
37484
38293
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
37485
38294
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -37554,7 +38363,7 @@ function createPluginRuntime(gateway, settings, opts) {
37554
38363
  bundlesPacked = true;
37555
38364
  }
37556
38365
  const policyMode = settings.policy;
37557
- const redactFallback = settings.redactFallback;
38366
+ let redactFallback = settings.redactFallback;
37558
38367
  const dataDir2 = opts?.dataDir;
37559
38368
  let rules = [];
37560
38369
  let scanner;
@@ -37598,6 +38407,7 @@ function createPluginRuntime(gateway, settings, opts) {
37598
38407
  rules = [...verified, ...unverified];
37599
38408
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
37600
38409
  bundleExceptions = bundle.exceptions ?? [];
38410
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
37601
38411
  initialized = true;
37602
38412
  }
37603
38413
  let cachedKey;
@@ -37636,11 +38446,13 @@ function createPluginRuntime(gateway, settings, opts) {
37636
38446
  function decide(findings, text, excepted, rewritable = true) {
37637
38447
  if (findings.length === 0) return { action: "log", text, findings: [] };
37638
38448
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
38449
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
38450
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
37639
38451
  let worst = "log";
37640
38452
  for (const finding of findings) {
37641
38453
  worst = strongerAction(worst, actionFor(finding));
37642
38454
  }
37643
- if (worst === "block") return { action: "block", text: null, findings };
38455
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
37644
38456
  if (worst === "redact") {
37645
38457
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
37646
38458
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -37650,9 +38462,13 @@ function createPluginRuntime(gateway, settings, opts) {
37650
38462
  findings,
37651
38463
  enforcedFindings: redactFindings,
37652
38464
  reversibleFindings
38465
+ // No `degraded` here, and it is not an omission: `rewritable` is per
38466
+ // CAPTURE, so on an unrewritable field every redact has already become
38467
+ // the fallback and this branch is unreachable. Spreading it would read
38468
+ // as a case that can happen.
37653
38469
  };
37654
38470
  }
37655
- return { action: worst, text, findings };
38471
+ return { action: worst, text, findings, ...degraded };
37656
38472
  }
37657
38473
  function fingerprintOf(key, finding, cache) {
37658
38474
  let fp = cache.get(finding);
@@ -37781,8 +38597,8 @@ function createPluginRuntime(gateway, settings, opts) {
37781
38597
  };
37782
38598
  }
37783
38599
  }
37784
- async function processText(text, context) {
37785
- return (await evaluate(text, context, {})).decision;
38600
+ async function processText(text, context, opts2 = {}) {
38601
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
37786
38602
  }
37787
38603
  async function capture(input2, opts2 = {}) {
37788
38604
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -37805,10 +38621,12 @@ function createPluginRuntime(gateway, settings, opts) {
37805
38621
  );
37806
38622
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
37807
38623
  const inspectionMs = elapsedMs(timingStartedAt);
37808
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
38624
+ const redactDegradedTo = decision.redactDegradedTo;
38625
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
37809
38626
  ...input2.metadata,
37810
38627
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
37811
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
38628
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
38629
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
37812
38630
  } : input2.metadata;
37813
38631
  const event = buildIngestEvent({
37814
38632
  kind: input2.kind,
@@ -37880,11 +38698,11 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
37880
38698
 
37881
38699
  // ../../packages/plugin-sdk/src/throttle.ts
37882
38700
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
37883
- import { join as join22 } from "path";
38701
+ import { join as join24 } from "path";
37884
38702
 
37885
38703
  // ../../packages/scanner/src/discover.ts
37886
38704
  import { readdirSync as readdirSync5 } from "fs";
37887
- import { join as join23 } from "path";
38705
+ import { join as join25 } from "path";
37888
38706
 
37889
38707
  // ../../packages/scanner/src/constants.ts
37890
38708
  var COMMON_SKIP_DIRS = ["node_modules", "__pycache__", ".venv", "venv", ".cache"];
@@ -37929,7 +38747,7 @@ function discoverGitRepos(opts) {
37929
38747
  if (!entry.isDirectory()) continue;
37930
38748
  if (DISCOVER_SKIP.has(entry.name)) continue;
37931
38749
  if (entry.name.startsWith(".")) continue;
37932
- visit2(join23(dir, entry.name), depth + 1);
38750
+ visit2(join25(dir, entry.name), depth + 1);
37933
38751
  }
37934
38752
  }
37935
38753
  for (const root of searchRoots) {
@@ -37940,7 +38758,7 @@ function discoverGitRepos(opts) {
37940
38758
 
37941
38759
  // ../../packages/scanner/src/render.ts
37942
38760
  import { basename as basename6, relative } from "path";
37943
- var SEVERITY_ORDER3 = ["critical", "high", "medium", "low"];
38761
+ var SEVERITY_ORDER = ["critical", "high", "medium", "low"];
37944
38762
  var SEVERITY_GLYPH = {
37945
38763
  critical: "\u2588",
37946
38764
  high: "\u2593",
@@ -37971,7 +38789,7 @@ function findingsLabel(total, gitignored) {
37971
38789
  return `${String(total)} (${String(gitignored)} in .gitignore'd files \u2014 informational)`;
37972
38790
  }
37973
38791
  function severitySection(bySeverity) {
37974
- const rows = SEVERITY_ORDER3.filter((s) => (bySeverity[s] ?? 0) > 0).map((s) => [
38792
+ const rows = SEVERITY_ORDER.filter((s) => (bySeverity[s] ?? 0) > 0).map((s) => [
37975
38793
  `${SEVERITY_GLYPH[s] ?? ""} ${s}`,
37976
38794
  String(bySeverity[s])
37977
38795
  ]);
@@ -38218,10 +39036,10 @@ function parsed(schema, body, route) {
38218
39036
  }
38219
39037
  function withoutTrailingSlashes(endpoint) {
38220
39038
  let end = endpoint.length;
38221
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
39039
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
38222
39040
  return endpoint.slice(0, end);
38223
39041
  }
38224
- var SLASH = "/".charCodeAt(0);
39042
+ var SLASH2 = "/".charCodeAt(0);
38225
39043
  function createRemoteClient(options) {
38226
39044
  const base = withoutTrailingSlashes(options.endpoint);
38227
39045
  const url2 = (route) => `${base}${route}`;
@@ -38403,11 +39221,11 @@ function withTimeout(promise2, ms) {
38403
39221
  }
38404
39222
 
38405
39223
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
38406
- import { readFileSync as readFileSync14 } from "fs";
38407
- import { join as join24 } from "path";
39224
+ import { readFileSync as readFileSync16 } from "fs";
39225
+ import { join as join26 } from "path";
38408
39226
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
38409
39227
  function forwardDropsPath(dataDir2) {
38410
- return join24(dataDir2, FORWARD_DROPS_FILENAME);
39228
+ return join26(dataDir2, FORWARD_DROPS_FILENAME);
38411
39229
  }
38412
39230
  function recordForwardDrops(dataDir2, count, nowMs) {
38413
39231
  if (count <= 0) return;
@@ -38425,7 +39243,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
38425
39243
  }
38426
39244
  function readForwardDrops(dataDir2) {
38427
39245
  try {
38428
- const parsed2 = JSON.parse(readFileSync14(forwardDropsPath(dataDir2), "utf8"));
39246
+ const parsed2 = JSON.parse(readFileSync16(forwardDropsPath(dataDir2), "utf8"));
38429
39247
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
38430
39248
  const record2 = parsed2;
38431
39249
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -38443,9 +39261,8 @@ function readForwardDrops(dataDir2) {
38443
39261
 
38444
39262
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
38445
39263
  import { randomUUID as randomUUID15 } from "crypto";
38446
- import { readFileSync as readFileSync15 } from "fs";
38447
39264
  import { readFile, rename, writeFile } from "fs/promises";
38448
- import { join as join25 } from "path";
39265
+ import { join as join27 } from "path";
38449
39266
  function isInvalidRequest(err) {
38450
39267
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
38451
39268
  }
@@ -38459,31 +39276,12 @@ function isServerRejection(err) {
38459
39276
  var FORWARD_BUDGET_MS = 1500;
38460
39277
  var DECISION_PATH_BUDGET_MS = 800;
38461
39278
  var BREAKER_FAILURE_THRESHOLD = 3;
38462
- var BREAKER_COOLDOWN_MS = 3e4;
38463
39279
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
38464
- var FAILURES = /* @__PURE__ */ new Set([
38465
- "unauthorized",
38466
- "forbidden",
38467
- "unreachable"
38468
- ]);
38469
39280
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
38470
39281
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
38471
- function parseBreakerState(raw, nowMs) {
38472
- try {
38473
- const parsed2 = JSON.parse(raw);
38474
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
38475
- const record2 = parsed2;
38476
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
38477
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
38478
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
38479
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
38480
- } catch {
38481
- return null;
38482
- }
38483
- }
38484
39282
  function createForwardPolicy(deps) {
38485
39283
  const now = deps.now ?? (() => Date.now());
38486
- const file2 = join25(deps.dir, STATE_FILENAME);
39284
+ const file2 = join27(deps.dir, STATE_FILENAME);
38487
39285
  let state = null;
38488
39286
  let loading = null;
38489
39287
  async function readState() {
@@ -38493,7 +39291,7 @@ function createForwardPolicy(deps) {
38493
39291
  } catch {
38494
39292
  return { ...CLOSED };
38495
39293
  }
38496
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
39294
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
38497
39295
  }
38498
39296
  async function load() {
38499
39297
  if (state !== null) return state;
@@ -38539,7 +39337,7 @@ function createForwardPolicy(deps) {
38539
39337
  };
38540
39338
  const at = now();
38541
39339
  if (current.openedAtMs !== null) {
38542
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
39340
+ if (isForwardPaused(current, at)) {
38543
39341
  return { ok: false, reason: "breaker-open" };
38544
39342
  }
38545
39343
  await persist({
@@ -39076,7 +39874,18 @@ var AttachedDataGateway = class {
39076
39874
  // and the spread above would otherwise drop the field silently — which is
39077
39875
  // exactly what it did, leaving the whole control inert on every device
39078
39876
  // while every test around it stayed green.
39079
- prohibitedModels: cached2.prohibitedModels
39877
+ prohibitedModels: cached2.prohibitedModels,
39878
+ // NAMED for the same reason as the line above, and it is the same defect
39879
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
39880
+ // only the cache carries is dropped in silence. That is what left
39881
+ // `prohibitedModels` inert on every attached device with every test
39882
+ // around it green.
39883
+ //
39884
+ // Taken from the cache rather than merged here, because merging it needs
39885
+ // the device's own SETTING — which is not a bundle field and is not in
39886
+ // scope at this seam. The runtime does that merge, raise-only, where both
39887
+ // values are in hand (createPluginRuntime's ensureInitialized).
39888
+ redactFallback: cached2.redactFallback
39080
39889
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
39081
39890
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
39082
39891
  // it emits, so an 'authored' policy arriving from the control plane
@@ -39204,10 +40013,6 @@ function toolAuditEvent(input2) {
39204
40013
  };
39205
40014
  }
39206
40015
 
39207
- // ../../packages/plugin-runtime/src/attached/history-state.ts
39208
- import { readFileSync as readFileSync16 } from "fs";
39209
- import { join as join26 } from "path";
39210
-
39211
40016
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
39212
40017
  import { createHash as createHash6 } from "crypto";
39213
40018
  import { hostname as hostname5 } from "os";
@@ -39216,6 +40021,10 @@ import { hostname as hostname5 } from "os";
39216
40021
  var CORRELATION_ID = EventMetadata.shape.correlationId;
39217
40022
  var TRACE_ID = EventMetadata.shape.traceId;
39218
40023
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
40024
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
40025
+
40026
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
40027
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
39219
40028
 
39220
40029
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
39221
40030
  import { spawn } from "child_process";
@@ -39242,7 +40051,7 @@ function createPluginBlock(build, policyStore) {
39242
40051
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
39243
40052
  import { randomUUID as randomUUID16 } from "crypto";
39244
40053
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
39245
- import { join as join27 } from "path";
40054
+ import { join as join28 } from "path";
39246
40055
 
39247
40056
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
39248
40057
  import { rename as rename2 } from "fs/promises";
@@ -39266,7 +40075,7 @@ async function publishByRename(tmp, file2, move = rename2) {
39266
40075
 
39267
40076
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
39268
40077
  function createPolicyStore(dir = dataDir()) {
39269
- const file2 = join27(dir, "policy-cache.json");
40078
+ const file2 = join28(dir, "policy-cache.json");
39270
40079
  async function read() {
39271
40080
  try {
39272
40081
  const raw = await readFile2(file2, "utf8");
@@ -39497,11 +40306,11 @@ function readStorePosture(dbPath2) {
39497
40306
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
39498
40307
  import { randomUUID as randomUUID17 } from "crypto";
39499
40308
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
39500
- import { join as join28 } from "path";
40309
+ import { join as join29 } from "path";
39501
40310
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
39502
40311
  function createPostureStore(dir = settingsDir(), legacyDir) {
39503
- const file2 = join28(dir, "posture-state.json");
39504
- const legacyFile = legacyDir === void 0 ? null : join28(legacyDir, "posture-state.json");
40312
+ const file2 = join29(dir, "posture-state.json");
40313
+ const legacyFile = legacyDir === void 0 ? null : join29(legacyDir, "posture-state.json");
39505
40314
  async function persist(state) {
39506
40315
  await ensureDataDir(dir);
39507
40316
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -39570,7 +40379,7 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
39570
40379
 
39571
40380
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
39572
40381
  import { readFileSync as readFileSync18 } from "fs";
39573
- import { join as join29 } from "path";
40382
+ import { join as join30 } from "path";
39574
40383
 
39575
40384
  // ../../packages/plugin-runtime/src/attached/status.ts
39576
40385
  var REFUSAL_LINES = {
@@ -39591,6 +40400,14 @@ import { spawn as spawn2 } from "child_process";
39591
40400
  import { fileURLToPath as fileURLToPath3 } from "url";
39592
40401
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
39593
40402
 
40403
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
40404
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
40405
+
40406
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
40407
+ import { spawn as spawn3 } from "child_process";
40408
+ import { fileURLToPath as fileURLToPath4 } from "url";
40409
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
40410
+
39594
40411
  // ../../packages/plugin-runtime/src/attached/factory.ts
39595
40412
  import { hostname as hostname6 } from "os";
39596
40413
 
@@ -40045,7 +40862,7 @@ import { statSync as statSync11 } from "fs";
40045
40862
 
40046
40863
  // ../../packages/scanner/src/walk.ts
40047
40864
  import { readdirSync as readdirSync6, readFileSync as readFileSync19, statSync as statSync10 } from "fs";
40048
- import { extname, join as join30, relative as relative2, sep as sep4 } from "path";
40865
+ import { extname, join as join31, relative as relative2, sep as sep4 } from "path";
40049
40866
  var import_ignore2 = __toESM(require_ignore(), 1);
40050
40867
  var SOURCE_EXTENSIONS = /* @__PURE__ */ new Set([
40051
40868
  ".ts",
@@ -40096,7 +40913,7 @@ function* walkTree(rootDir, opts = {}) {
40096
40913
  );
40097
40914
  for (const entry of dirents) {
40098
40915
  const name = entry.name;
40099
- const fullPath = join30(dir, name);
40916
+ const fullPath = join31(dir, name);
40100
40917
  if (entry.isDirectory()) {
40101
40918
  const skipState = evaluateIgnore(dirSkipLayers, dirRel, name, true);
40102
40919
  if (skipState !== "unignored" && (SKIP_DIRS.has(name) || skipState === "ignored")) {
@@ -40520,7 +41337,7 @@ function parseFlags(argv) {
40520
41337
  depth: depth !== void 0 && Number.isFinite(depth) && depth > 0 ? depth : void 0
40521
41338
  };
40522
41339
  }
40523
- var FOLLOW_UP = "Run /findings to review details.";
41340
+ var FOLLOW_UP = "Run /aka:findings to review details.";
40524
41341
  try {
40525
41342
  const { dir, discover, root, depth } = parseFlags(process.argv.slice(2));
40526
41343
  const cfg = loadConfig();