@akasecurity/ai-tc-codex 0.9.11 → 0.9.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ var require_ignore = __commonJS({
50
50
  var REGEX_SPLITALL_CRLF = /\r?\n/g;
51
51
  var REGEX_TEST_INVALID_PATH = /^\.{0,2}\/|^\.{1,2}$/;
52
52
  var REGEX_TEST_TRAILING_SLASH = /\/$/;
53
- var SLASH2 = "/";
53
+ var SLASH3 = "/";
54
54
  var TMP_KEY_IGNORE = "node-ignore";
55
55
  if (typeof Symbol !== "undefined") {
56
56
  TMP_KEY_IGNORE = /* @__PURE__ */ Symbol.for("node-ignore");
@@ -422,11 +422,11 @@ var require_ignore = __commonJS({
422
422
  if (!REGEX_TEST_TRAILING_SLASH.test(path)) {
423
423
  return this.test(path);
424
424
  }
425
- const slices = path.split(SLASH2).filter(Boolean);
425
+ const slices = path.split(SLASH3).filter(Boolean);
426
426
  slices.pop();
427
427
  if (slices.length) {
428
428
  const parent = this._t(
429
- slices.join(SLASH2) + SLASH2,
429
+ slices.join(SLASH3) + SLASH3,
430
430
  this._testCache,
431
431
  true,
432
432
  slices
@@ -442,14 +442,14 @@ var require_ignore = __commonJS({
442
442
  return cache[path];
443
443
  }
444
444
  if (!slices) {
445
- slices = path.split(SLASH2).filter(Boolean);
445
+ slices = path.split(SLASH3).filter(Boolean);
446
446
  }
447
447
  slices.pop();
448
448
  if (!slices.length) {
449
449
  return cache[path] = this._rules.test(path, checkUnignored, MODE_IGNORE);
450
450
  }
451
451
  const parent = this._t(
452
- slices.join(SLASH2) + SLASH2,
452
+ slices.join(SLASH3) + SLASH3,
453
453
  cache,
454
454
  checkUnignored,
455
455
  slices
@@ -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, table, opts) {
24773
25105
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24774
25106
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).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 table of ["events", "findings"]) {
24921
- try {
24922
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
24923
- if (row === void 0) {
24924
- holdsRows = true;
24925
- marks.push(`${table}:unreadable`);
24926
- continue;
24927
- }
24928
- if (row.n > 0) holdsRows = true;
24929
- marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
24930
- } catch {
24931
- holdsRows = true;
24932
- marks.push(`${table}: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 table of ["events", "findings"]) {
25895
+ try {
25896
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
25897
+ if (row === void 0) {
25898
+ holdsRows = true;
25899
+ marks.push(`${table}:unreadable`);
25900
+ continue;
25901
+ }
25902
+ if (row.n > 0) holdsRows = true;
25903
+ marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
25904
+ } catch {
25905
+ holdsRows = true;
25906
+ marks.push(`${table}: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, table) {
25262
26236
  if (!columns.includes("outbox_owed")) {
25263
26237
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25264
26238
  }
26239
+ if (!columns.includes("sync_failed_at")) {
26240
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26241
+ }
26242
+ if (!columns.includes("sync_failure")) {
26243
+ withTransaction(
26244
+ db,
26245
+ () => {
26246
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26247
+ db.exec(
26248
+ `UPDATE ${table} 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 ${table}
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) {
@@ -28442,6 +29053,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28442
29053
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28443
29054
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28444
29055
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29056
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28445
29057
  if (values.vaultConsent !== void 0) {
28446
29058
  merged.vaultConsent = values.vaultConsent ? (
28447
29059
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30949,7 +31561,7 @@ var SqliteSecurityRepository = class {
30949
31561
  ELSE 0
30950
31562
  END) AS open_at_rest
30951
31563
  FROM inspection_findings f
30952
- JOIN audit_events e ON e.id = f.audit_event_id
31564
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30953
31565
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30954
31566
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30955
31567
  ON latest.finding_key = f.finding_key
@@ -31175,7 +31787,7 @@ var SqliteSecurityRepository = class {
31175
31787
  this.db.prepare(
31176
31788
  `SELECT e.repo AS repo, count(*) AS c
31177
31789
  FROM inspection_findings f
31178
- JOIN audit_events e ON e.id = f.audit_event_id
31790
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31179
31791
  WHERE e.started_at >= :from AND e.started_at < :to
31180
31792
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31181
31793
  AND e.repo IS NOT NULL
@@ -31299,7 +31911,7 @@ var SqliteSecurityRepository = class {
31299
31911
  d.severity AS severity,
31300
31912
  COUNT(*) AS count
31301
31913
  FROM inspection_findings f
31302
- JOIN audit_events e ON e.id = f.audit_event_id
31914
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31303
31915
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31304
31916
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31305
31917
  ON latest.finding_key = f.finding_key
@@ -31334,7 +31946,7 @@ var SqliteSecurityRepository = class {
31334
31946
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31335
31947
  d.rule_id AS rule_id, d.category AS category
31336
31948
  FROM inspection_findings f
31337
- JOIN audit_events e ON e.id = f.audit_event_id
31949
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31338
31950
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31339
31951
  WHERE e.started_at >= :from AND e.started_at < :to
31340
31952
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32175,6 +32787,7 @@ function openWithPragmas(file2) {
32175
32787
  db.exec("PRAGMA journal_mode = WAL");
32176
32788
  db.exec("PRAGMA busy_timeout = 2000");
32177
32789
  db.exec("PRAGMA foreign_keys = ON");
32790
+ registerSqlFunctions(db);
32178
32791
  } catch (err) {
32179
32792
  closeQuietly(db);
32180
32793
  throw err;
@@ -32204,7 +32817,7 @@ function backupLegacyStore(db, file2) {
32204
32817
  discardStore(file2, backup);
32205
32818
  return backup;
32206
32819
  }
32207
- function openAndInitialize(file2, base) {
32820
+ function openAndInitialize(file2, base, skipTags) {
32208
32821
  let db = openWithPragmas(file2);
32209
32822
  try {
32210
32823
  if (isForeignSqliteLineage(db)) {
@@ -32214,7 +32827,7 @@ function openAndInitialize(file2, base) {
32214
32827
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32215
32828
  );
32216
32829
  }
32217
- applyMigrations(db, file2);
32830
+ applyMigrations(db, file2, { skipTags });
32218
32831
  tightenPerms(file2);
32219
32832
  const policies = new SqlitePoliciesRepository(db);
32220
32833
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32229,6 +32842,7 @@ function openAndInitialize(file2, base) {
32229
32842
  exceptions: new SqliteExceptionsRepository(db),
32230
32843
  resolutions: new SqliteResolutionsRepository(db),
32231
32844
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32845
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32232
32846
  security: new SqliteSecurityRepository(db),
32233
32847
  detections: new SqliteDetectionsRepository(db),
32234
32848
  shares: new SqliteSharesRepository(db),
@@ -32251,7 +32865,8 @@ function openAndInitialize(file2, base) {
32251
32865
  throw err;
32252
32866
  }
32253
32867
  }
32254
- function openLocalDatabase(dir) {
32868
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32869
+ function openLocalDatabase(dir, options = {}) {
32255
32870
  ensureDataDirSync(dir);
32256
32871
  const file2 = join7(dir, DB_FILENAME);
32257
32872
  reapStalePartials(file2);
@@ -32263,6 +32878,7 @@ function openLocalDatabase(dir) {
32263
32878
  installedPacks,
32264
32879
  scanLedger,
32265
32880
  historySync,
32881
+ bodyRetention,
32266
32882
  secretVault,
32267
32883
  exceptions,
32268
32884
  resolutions,
@@ -32286,7 +32902,8 @@ function openLocalDatabase(dir) {
32286
32902
  // `dir` is always `<base>/data` — every caller resolves it through
32287
32903
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32288
32904
  // settings/ and data/, and the pack-policy floor needs both halves.
32289
- dirname2(dir)
32905
+ dirname2(dir),
32906
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32290
32907
  );
32291
32908
  function captureRowId(event) {
32292
32909
  return captureId(
@@ -32479,6 +33096,7 @@ function openLocalDatabase(dir) {
32479
33096
  installedPacks,
32480
33097
  scanLedger,
32481
33098
  historySync,
33099
+ bodyRetention,
32482
33100
  secretVault,
32483
33101
  exceptions,
32484
33102
  resolutions,
@@ -32519,8 +33137,35 @@ function openLocalDatabase(dir) {
32519
33137
 
32520
33138
  // ../../packages/persistence/src/egress-wire.ts
32521
33139
  import { createHash as createHash3 } from "crypto";
33140
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33141
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33142
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33143
+ var FILE_URL = /^file:\/\//i;
33144
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33145
+ var SLASH = "/".charCodeAt(0);
33146
+ var GIT_SUFFIX = ".git";
33147
+ function trimSlashes(path) {
33148
+ let start = 0;
33149
+ let end = path.length;
33150
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33151
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33152
+ return path.slice(start, end);
33153
+ }
33154
+ function canonicalGitUrl(url2) {
33155
+ const trimmed = url2.trim();
33156
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33157
+ const scheme = SCHEME_FORM.exec(trimmed);
33158
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33159
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33160
+ if (host === void 0) return trimmed;
33161
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33162
+ const bare = trimSlashes(path);
33163
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33164
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33165
+ }
32522
33166
  function hashProjectKey(projectKey) {
32523
- return createHash3("sha256").update(projectKey, "utf8").digest("hex");
33167
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33168
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
32524
33169
  }
32525
33170
  function toIngestHit(hit) {
32526
33171
  return {
@@ -32687,18 +33332,50 @@ function fingerprintValue(key, raw) {
32687
33332
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32688
33333
  }
32689
33334
 
33335
+ // ../../packages/persistence/src/forward-health.ts
33336
+ import { readFileSync as readFileSync7 } from "fs";
33337
+ import { join as join9 } from "path";
33338
+ var FAILURES = /* @__PURE__ */ new Set([
33339
+ "unauthorized",
33340
+ "forbidden",
33341
+ "unreachable"
33342
+ ]);
33343
+ var BREAKER_COOLDOWN_MS = 3e4;
33344
+ function parseForwardHealth(raw, nowMs) {
33345
+ try {
33346
+ const parsed2 = JSON.parse(raw);
33347
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33348
+ const record2 = parsed2;
33349
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33350
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33351
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33352
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33353
+ } catch {
33354
+ return null;
33355
+ }
33356
+ }
33357
+ function isForwardPaused(health, nowMs) {
33358
+ const openedAtMs = health?.openedAtMs ?? null;
33359
+ if (openedAtMs === null) return false;
33360
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33361
+ }
33362
+
32690
33363
  // ../../packages/persistence/src/history-backfill.ts
32691
33364
  import { existsSync as existsSync4 } from "fs";
32692
- import { join as join9 } from "path";
33365
+ import { join as join10 } from "path";
32693
33366
 
32694
33367
  // ../../packages/persistence/src/history-preview.ts
32695
33368
  import { existsSync as existsSync5 } from "fs";
32696
- import { join as join10 } from "path";
33369
+ import { join as join11 } from "path";
32697
33370
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32698
33371
 
33372
+ // ../../packages/persistence/src/history-sync-state.ts
33373
+ import { readFileSync as readFileSync8 } from "fs";
33374
+ import { join as join12 } from "path";
33375
+
32699
33376
  // ../../packages/persistence/src/store-symlinks.ts
32700
33377
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32701
- import { dirname as dirname3, join as join11, resolve } from "path";
33378
+ import { dirname as dirname3, join as join13, resolve } from "path";
32702
33379
  var STORE_DB = "the store database (including the prompt corpus)";
32703
33380
  var STORE_SETTINGS = "your settings file";
32704
33381
  function storeContents(home) {
@@ -32707,7 +33384,7 @@ function storeContents(home) {
32707
33384
  [settingsDir(home), STORE_SETTINGS],
32708
33385
  [dataDir(home), STORE_DB],
32709
33386
  [keysDir(home), "the vault key"],
32710
- [join11(settingsDir(home), "settings.json"), STORE_SETTINGS],
33387
+ [join13(settingsDir(home), "settings.json"), STORE_SETTINGS],
32711
33388
  [dbPath(home), STORE_DB]
32712
33389
  ]);
32713
33390
  }
@@ -32759,19 +33436,19 @@ import {
32759
33436
  // ../../packages/persistence/src/vault/key-provider.ts
32760
33437
  import { execFileSync } from "child_process";
32761
33438
  import { randomBytes as randomBytes2 } from "crypto";
32762
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32763
- import { join as join12 } from "path";
33439
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33440
+ import { join as join14 } from "path";
32764
33441
 
32765
33442
  // ../../packages/persistence/src/vault/vault.ts
32766
33443
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32767
33444
 
32768
33445
  // ../../packages/persistence/src/warn-era-cap.ts
32769
33446
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
32770
- import { join as join13 } from "path";
33447
+ import { join as join15 } from "path";
32771
33448
  var MARKER = "warn-era-capped";
32772
33449
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32773
33450
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32774
- const marker = join13(dataDir2, MARKER);
33451
+ const marker = join15(dataDir2, MARKER);
32775
33452
  if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32776
33453
  const capped = db.policies.capCategoryActions();
32777
33454
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
@@ -32831,7 +33508,7 @@ function resolveProvider() {
32831
33508
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32832
33509
  try {
32833
33510
  ensureLayoutDirSync(base);
32834
- const settingsFile = join14(settingsDir(base), "settings.json");
33511
+ const settingsFile = join16(settingsDir(base), "settings.json");
32835
33512
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
32836
33513
  } catch {
32837
33514
  }
@@ -32855,9 +33532,9 @@ function resolveProviderSafe(resolveProviderFn) {
32855
33532
  }
32856
33533
 
32857
33534
  // ../../packages/plugin-sdk/src/config-inventory.ts
32858
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33535
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32859
33536
  import { homedir as homedir2 } from "os";
32860
- import { basename as basename3, join as join16 } from "path";
33537
+ import { basename as basename3, join as join18 } from "path";
32861
33538
 
32862
33539
  // ../../packages/detections/src/egress/registry.ts
32863
33540
  var EXTRACTOR_VERSION = "1";
@@ -35946,8 +36623,8 @@ function bundledDetections() {
35946
36623
  }
35947
36624
 
35948
36625
  // ../../packages/plugin-sdk/src/repo.ts
35949
- import { existsSync as existsSync9, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
35950
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
36626
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
36627
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
35951
36628
  function resolveRepo(cwd) {
35952
36629
  try {
35953
36630
  const root = findGitRoot(cwd);
@@ -35962,36 +36639,36 @@ function resolveRepo(cwd) {
35962
36639
  function findGitRoot(start) {
35963
36640
  let dir = start;
35964
36641
  for (; ; ) {
35965
- if (existsSync9(join15(dir, ".git"))) return dir;
36642
+ if (existsSync9(join17(dir, ".git"))) return dir;
35966
36643
  const parent = dirname4(dir);
35967
36644
  if (parent === dir) return void 0;
35968
36645
  dir = parent;
35969
36646
  }
35970
36647
  }
35971
36648
  function resolveGitContext(root) {
35972
- const dotGit = join15(root, ".git");
36649
+ const dotGit = join17(root, ".git");
35973
36650
  try {
35974
36651
  if (statSync6(dotGit).isDirectory()) {
35975
- return { configPath: join15(dotGit, "config"), headRoot: root };
36652
+ return { configPath: join17(dotGit, "config"), headRoot: root };
35976
36653
  }
35977
36654
  } catch {
35978
36655
  return void 0;
35979
36656
  }
35980
36657
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
35981
36658
  if (!target) return void 0;
35982
- const gitdir = isAbsolute(target) ? target : join15(root, target);
35983
- if (existsSync9(join15(gitdir, "config"))) {
35984
- return { configPath: join15(gitdir, "config"), headRoot: root };
36659
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
36660
+ if (existsSync9(join17(gitdir, "config"))) {
36661
+ return { configPath: join17(gitdir, "config"), headRoot: root };
35985
36662
  }
35986
- const commonRaw = safeRead(join15(gitdir, "commondir"))?.trim();
36663
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
35987
36664
  if (!commonRaw) return void 0;
35988
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join15(gitdir, commonRaw);
36665
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
35989
36666
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
35990
- return { configPath: join15(commonGitDir, "config"), headRoot };
36667
+ return { configPath: join17(commonGitDir, "config"), headRoot };
35991
36668
  }
35992
36669
  function safeRead(path) {
35993
36670
  try {
35994
- return readFileSync8(path, "utf8");
36671
+ return readFileSync10(path, "utf8");
35995
36672
  } catch {
35996
36673
  return void 0;
35997
36674
  }
@@ -36535,8 +37212,8 @@ function createGuardedScanner(partition, gateway, opts) {
36535
37212
  }
36536
37213
 
36537
37214
  // ../../packages/plugin-sdk/src/host-floor.ts
36538
- import { readFileSync as readFileSync11 } from "fs";
36539
- import { join as join18 } from "path";
37215
+ import { readFileSync as readFileSync13 } from "fs";
37216
+ import { join as join20 } from "path";
36540
37217
 
36541
37218
  // ../../packages/plugin-sdk/src/model-governance.ts
36542
37219
  import {
@@ -36544,11 +37221,11 @@ import {
36544
37221
  fstatSync,
36545
37222
  mkdirSync as mkdirSync2,
36546
37223
  openSync as openSync2,
36547
- readFileSync as readFileSync10,
37224
+ readFileSync as readFileSync12,
36548
37225
  readSync,
36549
37226
  writeFileSync as writeFileSync5
36550
37227
  } from "fs";
36551
- import { join as join17 } from "path";
37228
+ import { join as join19 } from "path";
36552
37229
  var TAIL_BYTES = 256 * 1024;
36553
37230
 
36554
37231
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -36571,15 +37248,15 @@ var HOST_FLOORS = {
36571
37248
 
36572
37249
  // ../../packages/plugin-sdk/src/ignore-layers.ts
36573
37250
  var import_ignore = __toESM(require_ignore(), 1);
36574
- import { readFileSync as readFileSync12 } from "fs";
36575
- import { join as join19 } from "path";
37251
+ import { readFileSync as readFileSync14 } from "fs";
37252
+ import { join as join21 } from "path";
36576
37253
 
36577
37254
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
36578
37255
  import { arch, hostname as hostname4, platform, release } from "os";
36579
37256
 
36580
37257
  // ../../packages/plugin-sdk/src/nudge.ts
36581
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
36582
- import { join as join20 } from "path";
37258
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
37259
+ import { join as join22 } from "path";
36583
37260
 
36584
37261
  // ../../packages/plugin-sdk/src/paths.ts
36585
37262
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -36622,7 +37299,7 @@ function createPolicyResolver(bundle) {
36622
37299
 
36623
37300
  // ../../packages/plugin-sdk/src/project-files.ts
36624
37301
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
36625
- import { basename as basename5, join as join21 } from "path";
37302
+ import { basename as basename5, join as join23 } from "path";
36626
37303
 
36627
37304
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
36628
37305
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -36697,7 +37374,7 @@ function createPluginRuntime(gateway, settings, opts) {
36697
37374
  bundlesPacked = true;
36698
37375
  }
36699
37376
  const policyMode = settings.policy;
36700
- const redactFallback = settings.redactFallback;
37377
+ let redactFallback = settings.redactFallback;
36701
37378
  const dataDir2 = opts?.dataDir;
36702
37379
  let rules = [];
36703
37380
  let scanner;
@@ -36741,6 +37418,7 @@ function createPluginRuntime(gateway, settings, opts) {
36741
37418
  rules = [...verified, ...unverified];
36742
37419
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
36743
37420
  bundleExceptions = bundle.exceptions ?? [];
37421
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
36744
37422
  initialized = true;
36745
37423
  }
36746
37424
  let cachedKey;
@@ -36779,11 +37457,13 @@ function createPluginRuntime(gateway, settings, opts) {
36779
37457
  function decide(findings, text, excepted, rewritable = true) {
36780
37458
  if (findings.length === 0) return { action: "log", text, findings: [] };
36781
37459
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
37460
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
37461
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
36782
37462
  let worst = "log";
36783
37463
  for (const finding of findings) {
36784
37464
  worst = strongerAction(worst, actionFor(finding));
36785
37465
  }
36786
- if (worst === "block") return { action: "block", text: null, findings };
37466
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
36787
37467
  if (worst === "redact") {
36788
37468
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
36789
37469
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -36793,9 +37473,13 @@ function createPluginRuntime(gateway, settings, opts) {
36793
37473
  findings,
36794
37474
  enforcedFindings: redactFindings,
36795
37475
  reversibleFindings
37476
+ // No `degraded` here, and it is not an omission: `rewritable` is per
37477
+ // CAPTURE, so on an unrewritable field every redact has already become
37478
+ // the fallback and this branch is unreachable. Spreading it would read
37479
+ // as a case that can happen.
36796
37480
  };
36797
37481
  }
36798
- return { action: worst, text, findings };
37482
+ return { action: worst, text, findings, ...degraded };
36799
37483
  }
36800
37484
  function fingerprintOf(key, finding, cache) {
36801
37485
  let fp = cache.get(finding);
@@ -36924,8 +37608,8 @@ function createPluginRuntime(gateway, settings, opts) {
36924
37608
  };
36925
37609
  }
36926
37610
  }
36927
- async function processText(text, context) {
36928
- return (await evaluate(text, context, {})).decision;
37611
+ async function processText(text, context, opts2 = {}) {
37612
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
36929
37613
  }
36930
37614
  async function capture(input2, opts2 = {}) {
36931
37615
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -36948,10 +37632,12 @@ function createPluginRuntime(gateway, settings, opts) {
36948
37632
  );
36949
37633
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
36950
37634
  const inspectionMs = elapsedMs(timingStartedAt);
36951
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
37635
+ const redactDegradedTo = decision.redactDegradedTo;
37636
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
36952
37637
  ...input2.metadata,
36953
37638
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
36954
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
37639
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
37640
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
36955
37641
  } : input2.metadata;
36956
37642
  const event = buildIngestEvent({
36957
37643
  kind: input2.kind,
@@ -37023,7 +37709,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
37023
37709
 
37024
37710
  // ../../packages/plugin-sdk/src/throttle.ts
37025
37711
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
37026
- import { join as join22 } from "path";
37712
+ import { join as join24 } from "path";
37027
37713
 
37028
37714
  // src/present.ts
37029
37715
  var fg = (hex3) => (text) => {
@@ -37077,7 +37763,7 @@ var SCANNABLE_FIELDS = {
37077
37763
  Bash: [{ field: "command", executable: true }],
37078
37764
  apply_patch: [{ field: "input", executable: false }]
37079
37765
  };
37080
- var EXECUTABLE_REDACT_NOTE = "Masking inside an executable command would silently change what runs, so a redact policy blocks it instead.";
37766
+ var EXECUTABLE_REDACT_NOTE = "Masking inside an executable command would silently change what runs, so masking in place was not possible and this workspace\u2019s fallback for that case is to block.";
37081
37767
  var UNREDACTABLE_NOTE = "The redacted form of this input was unavailable, so the call is blocked rather than sent unmasked.";
37082
37768
  function denyPointerMessage(toolName) {
37083
37769
  return `A vault pointer in a ${toolName} command cannot execute as text, and this plugin never substitutes a pointer back to its raw value. Remove the pointer from the command, or resolve the value yourself with \`aka vault show\` or the dashboard Vault page and retype what you need.`;
@@ -37106,13 +37792,10 @@ function decidePreToolUse(toolName, toolInput, scanned) {
37106
37792
  let escalatedUnredactable = false;
37107
37793
  let updatedInput = null;
37108
37794
  for (const { spec, result } of scanned) {
37795
+ if (result.redactDegradedTo === "block") escalatedExecutable = true;
37109
37796
  const unredactable = result.action === "redact" && result.text === null;
37110
- const escalate = result.action === "redact" && (spec.executable || unredactable);
37111
- if (escalate) {
37112
- if (spec.executable) escalatedExecutable = true;
37113
- else escalatedUnredactable = true;
37114
- }
37115
- const action = escalate ? "block" : result.action;
37797
+ if (unredactable) escalatedUnredactable = true;
37798
+ const action = unredactable ? "block" : result.action;
37116
37799
  if (action === "block") {
37117
37800
  for (const finding of result.findings) blockedRules.add(finding.ruleId);
37118
37801
  if (result.blockedReferences) blockedReferences.push(...result.blockedReferences);
@@ -37215,7 +37898,7 @@ function baseMetadata(input2) {
37215
37898
 
37216
37899
  // src/hooks/store-health.ts
37217
37900
  import { mkdirSync as mkdirSync5, readFileSync as readFileSync19, writeFileSync as writeFileSync8 } from "fs";
37218
- import { dirname as dirname6, join as join29 } from "path";
37901
+ import { dirname as dirname6, join as join30 } from "path";
37219
37902
 
37220
37903
  // ../../packages/remote/src/http.ts
37221
37904
  import { request as httpRequest } from "http";
@@ -37400,10 +38083,10 @@ function parsed(schema, body, route) {
37400
38083
  }
37401
38084
  function withoutTrailingSlashes(endpoint) {
37402
38085
  let end = endpoint.length;
37403
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
38086
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
37404
38087
  return endpoint.slice(0, end);
37405
38088
  }
37406
- var SLASH = "/".charCodeAt(0);
38089
+ var SLASH2 = "/".charCodeAt(0);
37407
38090
  function createRemoteClient(options) {
37408
38091
  const base = withoutTrailingSlashes(options.endpoint);
37409
38092
  const url2 = (route) => `${base}${route}`;
@@ -37585,11 +38268,11 @@ function withTimeout(promise2, ms) {
37585
38268
  }
37586
38269
 
37587
38270
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
37588
- import { readFileSync as readFileSync14 } from "fs";
37589
- import { join as join23 } from "path";
38271
+ import { readFileSync as readFileSync16 } from "fs";
38272
+ import { join as join25 } from "path";
37590
38273
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
37591
38274
  function forwardDropsPath(dataDir2) {
37592
- return join23(dataDir2, FORWARD_DROPS_FILENAME);
38275
+ return join25(dataDir2, FORWARD_DROPS_FILENAME);
37593
38276
  }
37594
38277
  function recordForwardDrops(dataDir2, count, nowMs) {
37595
38278
  if (count <= 0) return;
@@ -37607,7 +38290,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
37607
38290
  }
37608
38291
  function readForwardDrops(dataDir2) {
37609
38292
  try {
37610
- const parsed2 = JSON.parse(readFileSync14(forwardDropsPath(dataDir2), "utf8"));
38293
+ const parsed2 = JSON.parse(readFileSync16(forwardDropsPath(dataDir2), "utf8"));
37611
38294
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
37612
38295
  const record2 = parsed2;
37613
38296
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -37625,9 +38308,8 @@ function readForwardDrops(dataDir2) {
37625
38308
 
37626
38309
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
37627
38310
  import { randomUUID as randomUUID15 } from "crypto";
37628
- import { readFileSync as readFileSync15 } from "fs";
37629
38311
  import { readFile, rename, writeFile } from "fs/promises";
37630
- import { join as join24 } from "path";
38312
+ import { join as join26 } from "path";
37631
38313
  function isInvalidRequest(err) {
37632
38314
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
37633
38315
  }
@@ -37641,31 +38323,12 @@ function isServerRejection(err) {
37641
38323
  var FORWARD_BUDGET_MS = 1500;
37642
38324
  var DECISION_PATH_BUDGET_MS = 800;
37643
38325
  var BREAKER_FAILURE_THRESHOLD = 3;
37644
- var BREAKER_COOLDOWN_MS = 3e4;
37645
38326
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
37646
- var FAILURES = /* @__PURE__ */ new Set([
37647
- "unauthorized",
37648
- "forbidden",
37649
- "unreachable"
37650
- ]);
37651
38327
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
37652
38328
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
37653
- function parseBreakerState(raw, nowMs) {
37654
- try {
37655
- const parsed2 = JSON.parse(raw);
37656
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
37657
- const record2 = parsed2;
37658
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
37659
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
37660
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
37661
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
37662
- } catch {
37663
- return null;
37664
- }
37665
- }
37666
38329
  function createForwardPolicy(deps) {
37667
38330
  const now = deps.now ?? (() => Date.now());
37668
- const file2 = join24(deps.dir, STATE_FILENAME);
38331
+ const file2 = join26(deps.dir, STATE_FILENAME);
37669
38332
  let state = null;
37670
38333
  let loading = null;
37671
38334
  async function readState() {
@@ -37675,7 +38338,7 @@ function createForwardPolicy(deps) {
37675
38338
  } catch {
37676
38339
  return { ...CLOSED };
37677
38340
  }
37678
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
38341
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
37679
38342
  }
37680
38343
  async function load() {
37681
38344
  if (state !== null) return state;
@@ -37721,7 +38384,7 @@ function createForwardPolicy(deps) {
37721
38384
  };
37722
38385
  const at = now();
37723
38386
  if (current.openedAtMs !== null) {
37724
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
38387
+ if (isForwardPaused(current, at)) {
37725
38388
  return { ok: false, reason: "breaker-open" };
37726
38389
  }
37727
38390
  await persist({
@@ -38258,7 +38921,18 @@ var AttachedDataGateway = class {
38258
38921
  // and the spread above would otherwise drop the field silently — which is
38259
38922
  // exactly what it did, leaving the whole control inert on every device
38260
38923
  // while every test around it stayed green.
38261
- prohibitedModels: cached2.prohibitedModels
38924
+ prohibitedModels: cached2.prohibitedModels,
38925
+ // NAMED for the same reason as the line above, and it is the same defect
38926
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
38927
+ // only the cache carries is dropped in silence. That is what left
38928
+ // `prohibitedModels` inert on every attached device with every test
38929
+ // around it green.
38930
+ //
38931
+ // Taken from the cache rather than merged here, because merging it needs
38932
+ // the device's own SETTING — which is not a bundle field and is not in
38933
+ // scope at this seam. The runtime does that merge, raise-only, where both
38934
+ // values are in hand (createPluginRuntime's ensureInitialized).
38935
+ redactFallback: cached2.redactFallback
38262
38936
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
38263
38937
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
38264
38938
  // it emits, so an 'authored' policy arriving from the control plane
@@ -38386,10 +39060,6 @@ function toolAuditEvent(input2) {
38386
39060
  };
38387
39061
  }
38388
39062
 
38389
- // ../../packages/plugin-runtime/src/attached/history-state.ts
38390
- import { readFileSync as readFileSync16 } from "fs";
38391
- import { join as join25 } from "path";
38392
-
38393
39063
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
38394
39064
  import { createHash as createHash6 } from "crypto";
38395
39065
  import { hostname as hostname5 } from "os";
@@ -38398,6 +39068,10 @@ import { hostname as hostname5 } from "os";
38398
39068
  var CORRELATION_ID = EventMetadata.shape.correlationId;
38399
39069
  var TRACE_ID = EventMetadata.shape.traceId;
38400
39070
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
39071
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
39072
+
39073
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
39074
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
38401
39075
 
38402
39076
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
38403
39077
  import { spawn } from "child_process";
@@ -38424,7 +39098,7 @@ function createPluginBlock(build, policyStore) {
38424
39098
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38425
39099
  import { randomUUID as randomUUID16 } from "crypto";
38426
39100
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
38427
- import { join as join26 } from "path";
39101
+ import { join as join27 } from "path";
38428
39102
 
38429
39103
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
38430
39104
  import { rename as rename2 } from "fs/promises";
@@ -38448,7 +39122,7 @@ async function publishByRename(tmp, file2, move = rename2) {
38448
39122
 
38449
39123
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38450
39124
  function createPolicyStore(dir = dataDir()) {
38451
- const file2 = join26(dir, "policy-cache.json");
39125
+ const file2 = join27(dir, "policy-cache.json");
38452
39126
  async function read() {
38453
39127
  try {
38454
39128
  const raw = await readFile2(file2, "utf8");
@@ -38679,11 +39353,11 @@ function readStorePosture(dbPath2) {
38679
39353
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
38680
39354
  import { randomUUID as randomUUID17 } from "crypto";
38681
39355
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
38682
- import { join as join27 } from "path";
39356
+ import { join as join28 } from "path";
38683
39357
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
38684
39358
  function createPostureStore(dir = settingsDir(), legacyDir) {
38685
- const file2 = join27(dir, "posture-state.json");
38686
- const legacyFile = legacyDir === void 0 ? null : join27(legacyDir, "posture-state.json");
39359
+ const file2 = join28(dir, "posture-state.json");
39360
+ const legacyFile = legacyDir === void 0 ? null : join28(legacyDir, "posture-state.json");
38687
39361
  async function persist(state) {
38688
39362
  await ensureDataDir(dir);
38689
39363
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -38752,7 +39426,7 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
38752
39426
 
38753
39427
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
38754
39428
  import { readFileSync as readFileSync18 } from "fs";
38755
- import { join as join28 } from "path";
39429
+ import { join as join29 } from "path";
38756
39430
 
38757
39431
  // ../../packages/plugin-runtime/src/attached/status.ts
38758
39432
  var REFUSAL_LINES = {
@@ -38773,6 +39447,14 @@ import { spawn as spawn2 } from "child_process";
38773
39447
  import { fileURLToPath as fileURLToPath3 } from "url";
38774
39448
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
38775
39449
 
39450
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
39451
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
39452
+
39453
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
39454
+ import { spawn as spawn3 } from "child_process";
39455
+ import { fileURLToPath as fileURLToPath4 } from "url";
39456
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
39457
+
38776
39458
  // ../../packages/plugin-runtime/src/attached/factory.ts
38777
39459
  import { hostname as hostname6 } from "os";
38778
39460
 
@@ -39248,7 +39930,7 @@ function markerDirs(dataDir2) {
39248
39930
  function alreadyClaimed(dirs, marker, sessionId) {
39249
39931
  return dirs.some((dir) => {
39250
39932
  try {
39251
- return readFileSync19(join29(dir, marker), "utf8") === sessionId;
39933
+ return readFileSync19(join30(dir, marker), "utf8") === sessionId;
39252
39934
  } catch {
39253
39935
  return false;
39254
39936
  }
@@ -39258,7 +39940,7 @@ function recordClaim(dirs, marker, sessionId) {
39258
39940
  for (const dir of dirs) {
39259
39941
  try {
39260
39942
  mkdirSync5(dir, { recursive: true, mode: DATA_DIR_MODE });
39261
- writeFileSync8(join29(dir, marker), sessionId, { mode: DATA_FILE_MODE });
39943
+ writeFileSync8(join30(dir, marker), sessionId, { mode: DATA_FILE_MODE });
39262
39944
  return;
39263
39945
  } catch {
39264
39946
  }
@@ -39335,7 +40017,17 @@ async function main() {
39335
40017
  if (typeof value !== "string" || value === "") continue;
39336
40018
  const result = await runtime.capture(
39337
40019
  { kind, sourceTool: SOURCE_TOOL.Codex, text: value, metadata },
39338
- kind === "tool_use" ? { persist: "with-findings" } : {}
40020
+ {
40021
+ ...kind === "tool_use" ? { persist: "with-findings" } : {},
40022
+ // Per FIELD: a field that EXECUTES cannot be masked in place, because
40023
+ // rewriting a command changes what runs. The stored `apply_patch`
40024
+ // field can be, and keeps true redaction. A redact on the executable
40025
+ // one degrades to the configured `redactFallback` inside the runtime,
40026
+ // where the emitted decision, the recorded action and the ledger all
40027
+ // read one answer — this hook used to escalate it and record it as
40028
+ // `redact`, describing a masking that never happened.
40029
+ rewritable: !spec.executable
40030
+ }
39339
40031
  );
39340
40032
  scanned.push({ spec, result });
39341
40033
  }