@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
  // src/remediation/entry.ts
495
495
  import { readFileSync as readFileSync24 } from "fs";
496
- import { fileURLToPath as fileURLToPath5 } from "url";
496
+ import { fileURLToPath as fileURLToPath6 } from "url";
497
497
 
498
498
  // ../../packages/persistence/src/attached-derived.ts
499
499
  import { rmSync } from "fs";
@@ -506,6 +506,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
506
506
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
507
507
  import { join as join2 } from "path";
508
508
 
509
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
510
+ var DEFERRED_MIGRATION_TAGS = [
511
+ "0031_audit_capture_by_time_index",
512
+ "0032_audit_capture_by_id_index",
513
+ "0033_audit_capture_location_index",
514
+ "0034_findings_read_indexes"
515
+ ];
516
+
509
517
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
510
518
  var SQLITE_MIGRATIONS = [
511
519
  {
@@ -623,6 +631,30 @@ var SQLITE_MIGRATIONS = [
623
631
  {
624
632
  tag: "0028_activity_session_probe_indexes",
625
633
  sql: "CREATE INDEX `idx_audit_session_prompt` ON `audit_events` (`root_session_id`) WHERE event_type = 'prompt';--> statement-breakpoint\nCREATE INDEX `idx_audit_session_share` ON `audit_events` (`root_session_id`) WHERE event_type = 'share';--> statement-breakpoint\nCREATE INDEX `idx_audit_ended_at` ON `audit_events` (`ended_at`,`root_session_id`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\nCREATE INDEX `idx_audit_session_ended` ON `audit_events` (`root_session_id`,`ended_at`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\n-- Expression index for the activity list's turns rollup: a live-captured\n-- session's turns are the DISTINCT `run_key` across its `llm_call` leaves, and\n-- carrying the extracted key in the index answers that count from the index\n-- alone instead of parsing every leaf's attribute bag. Written by hand, as\n-- 0013's `idx_audit_code_change_path` was: drizzle-kit cannot emit an\n-- expression containing a comma, so this index is not declared in sqlite.ts.\nCREATE INDEX `idx_audit_session_run_key` ON `audit_events` (`root_session_id`, json_extract(`attributes`, '$.run_key')) WHERE `event_type` = 'llm_call';\n"
634
+ },
635
+ {
636
+ tag: "0029_audit_capture_rollup_index",
637
+ sql: "CREATE INDEX `idx_audit_capture_rollup` ON `audit_events` (`event_type`,`started_at`,`repo`,`id`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
638
+ },
639
+ {
640
+ tag: "0030_audit_content_expiry",
641
+ sql: "ALTER TABLE `audit_events` ADD `content_expired_at` integer;--> statement-breakpoint\nCREATE INDEX `idx_audit_expirable_body` ON `audit_events` (`started_at`) WHERE content IS NOT NULL;"
642
+ },
643
+ {
644
+ tag: "0031_audit_capture_by_time_index",
645
+ sql: "CREATE INDEX `idx_audit_capture_by_time` ON `audit_events` (`started_at`,`id`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
646
+ },
647
+ {
648
+ tag: "0032_audit_capture_by_id_index",
649
+ sql: "CREATE INDEX `idx_audit_capture_by_id` ON `audit_events` (`id`,`started_at`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
650
+ },
651
+ {
652
+ tag: "0033_audit_capture_location_index",
653
+ sql: "CREATE INDEX `idx_audit_capture_location` ON `audit_events` (`repo`,`file_path`,`started_at`,`id`,`event_type`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
654
+ },
655
+ {
656
+ tag: "0034_findings_read_indexes",
657
+ sql: "CREATE INDEX `idx_inspection_definitions_rule` ON `inspection_definitions` (`rule_id`,`severity`,`category`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_def` ON `inspection_findings` (`inspection_definition_id`,`audit_event_id`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_event_cover` ON `inspection_findings` (`audit_event_id`,`inspection_definition_id`,`action_taken`,`finding_key`,`id`);"
626
658
  }
627
659
  ];
628
660
 
@@ -20670,6 +20702,15 @@ var FindingCategory = external_exports.enum([
20670
20702
  ]).meta({ id: "FindingCategory" });
20671
20703
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20672
20704
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20705
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20706
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20707
+ var FindingDelivery = external_exports.object({
20708
+ state: FindingDeliveryState,
20709
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20710
+ at: external_exports.iso.datetime().optional(),
20711
+ // Only on `not_sent`, and only when a known reason was recorded.
20712
+ reason: SyncFailureReason.optional()
20713
+ }).meta({ id: "FindingDelivery" });
20673
20714
  var ResolutionMethod = external_exports.enum([
20674
20715
  "enforced-in-flight",
20675
20716
  "fixed-at-source",
@@ -20726,7 +20767,10 @@ var FindingInstance = external_exports.object({
20726
20767
  // The session that event belongs to, when it has one — the seam a
20727
20768
  // per-instance "view session" link needs. Absent for events captured
20728
20769
  // outside a session.
20729
- sessionId: external_exports.string().optional()
20770
+ sessionId: external_exports.string().optional(),
20771
+ // The delivery state of the event above (see FindingDelivery). Optional so
20772
+ // readers that do not project it stay valid.
20773
+ delivery: FindingDelivery.optional()
20730
20774
  }).meta({ id: "FindingInstance" });
20731
20775
  var FindingGroup = external_exports.object({
20732
20776
  id: external_exports.string(),
@@ -20778,7 +20822,10 @@ var FindingFacets = external_exports.object({
20778
20822
  // Host tool (attributes.tool_name). Present only on the instance-level
20779
20823
  // reads, which can filter by it; the type-level read omits the dimension
20780
20824
  // because a group spans tools.
20781
- tool: external_exports.array(FindingFacetItem).optional()
20825
+ tool: external_exports.array(FindingFacetItem).optional(),
20826
+ // Delivery states (FindingDeliveryState). Present only on the
20827
+ // instance-level reads, like `tool`.
20828
+ deployment: external_exports.array(FindingFacetItem).optional()
20782
20829
  }).meta({ id: "FindingFacets" });
20783
20830
  var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20784
20831
  id: "FindingTypeSummary"
@@ -20889,6 +20936,8 @@ var ListFindingInstancesQuery = external_exports.object({
20889
20936
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20890
20937
  // where the free-text `q` can only match the rendered "via Bash" label.
20891
20938
  tool: external_exports.array(external_exports.string()).optional(),
20939
+ // The delivery state of each finding's event (see FindingDelivery).
20940
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20892
20941
  // Exact repository / file-path matches, for the drill-down out of the
20893
20942
  // locations view. A row whose event carries no repo/file matches neither.
20894
20943
  repo: external_exports.string().optional(),
@@ -20909,6 +20958,10 @@ var ListFindingInstancesResponse = external_exports.object({
20909
20958
  items: external_exports.array(FindingInstanceDetail),
20910
20959
  nextCursor: external_exports.string().nullable()
20911
20960
  }).meta({ id: "ListFindingInstancesResponse" });
20961
+ var ListFindingInstancesPage = external_exports.object({
20962
+ items: external_exports.array(FindingInstanceDetail),
20963
+ nextCursor: external_exports.string().nullable()
20964
+ }).meta({ id: "ListFindingInstancesPage" });
20912
20965
  var FindingLocationSummary = external_exports.object({
20913
20966
  // Opaque, stable, minted from the pair by encodeLocationId. It exists
20914
20967
  // because a location's identity is two values and a URL param carries one:
@@ -20951,6 +21004,8 @@ var ListFindingLocationsQuery = external_exports.object({
20951
21004
  // instances that match, and folds its status from those.
20952
21005
  status: external_exports.array(FindingStatus).optional(),
20953
21006
  tool: external_exports.array(external_exports.string()).optional(),
21007
+ // The delivery state of each finding's event (see FindingDelivery).
21008
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20954
21009
  q: external_exports.string().optional(),
20955
21010
  sessionId: external_exports.string().optional(),
20956
21011
  from: external_exports.iso.datetime().optional(),
@@ -21153,6 +21208,10 @@ var CaptureAttributes = external_exports.object({
21153
21208
  // to 'allow' — the enforcement audit trail's link back to the grant that
21154
21209
  // authorized the bypass.
21155
21210
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21211
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21212
+ // join back to the `llm_call` leaf for the same assistant turn.
21213
+ message_id: external_exports.string().optional(),
21214
+ conversation_id: external_exports.string().optional(),
21156
21215
  // Whole milliseconds this capture's inspection blocked its caller — the
21157
21216
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21158
21217
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21161,7 +21220,19 @@ var CaptureAttributes = external_exports.object({
21161
21220
  // inline json_extract and is not itself an optimization.
21162
21221
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21163
21222
  // before the measurement shipped — never present as a placeholder 0.
21164
- inspection_ms: external_exports.number().int().nonnegative().optional()
21223
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21224
+ // What a `redact` this capture could not carry out became instead (see
21225
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21226
+ // degrade actually happened, so absence is the ordinary case rather than a
21227
+ // reader having to distinguish it from a zero.
21228
+ //
21229
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21230
+ // so on a multi-finding row this does not say which finding degraded, and
21231
+ // its presence does not mean the fallback decided the capture's action. A
21232
+ // capture denied by another finding's own Block policy carries `block`
21233
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21234
+ // repeated rather than referenced because a store reader opens this file.
21235
+ redact_degraded_to: ActionTaken.optional()
21165
21236
  }).catchall(external_exports.unknown());
21166
21237
  var ToolCallInspection = external_exports.object({
21167
21238
  ruleId: external_exports.string().min(1),
@@ -21360,7 +21431,17 @@ var AuditEvent = external_exports.object({
21360
21431
  /** `share` to a first-party/internal destination. */
21361
21432
  internal: external_exports.boolean(),
21362
21433
  /** Event needs review (e.g. unverified egress). */
21363
- flagged: external_exports.boolean()
21434
+ flagged: external_exports.boolean(),
21435
+ /**
21436
+ * The body this event's `title` is drawn from was cleared by local body
21437
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21438
+ *
21439
+ * A separate flag rather than a sentinel written into `title`: the title is
21440
+ * rendered text, and a store-layer module that invented display copy for it
21441
+ * would be choosing words the view is supposed to choose. Additive and
21442
+ * defaulted, so an older producer still validates.
21443
+ */
21444
+ bodyExpired: external_exports.boolean().default(false)
21364
21445
  }).meta({ id: "ActivityAuditEvent" });
21365
21446
  var ActivitySessionSummary = external_exports.object({
21366
21447
  id: external_exports.string(),
@@ -22701,6 +22782,12 @@ var EventMetadata = external_exports.object({
22701
22782
  // to 'allow' — the enforcement audit trail's link back to the grant that
22702
22783
  // authorized the bypass. Absent on captures where no exception applied.
22703
22784
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22785
+ // The assistant message this capture belongs to, and the conversation it sits
22786
+ // in — set by the browser extension's network capture so a stored `response`
22787
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22788
+ // on every other capture path, which has no such id.
22789
+ messageId: external_exports.string().optional(),
22790
+ conversationId: external_exports.string().optional(),
22704
22791
  // How long THIS capture's inspection blocked its caller, in whole
22705
22792
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22706
22793
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22713,7 +22800,37 @@ var EventMetadata = external_exports.object({
22713
22800
  // Absent is also what every pre-measurement client writes, and what a
22714
22801
  // clock failure degrades to — a reader must treat absence as "not measured"
22715
22802
  // and never as a zero, which would read as "inspection is free".
22716
- inspectionMs: external_exports.number().int().nonnegative().optional()
22803
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22804
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22805
+ // workspace's `redactFallback`, applied because the field could not be
22806
+ // masked in place (a shell command, a URL, or any argument on a host whose
22807
+ // hook contract offers no rewrite channel).
22808
+ //
22809
+ // It exists because the action alone cannot say why. A finding recorded as
22810
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22811
+ // assigned Redact on a field that could not take one — and those are
22812
+ // different facts about the same row: the first is a policy the user chose,
22813
+ // the second is a masking the host could not perform. Absent means no
22814
+ // degrade happened, which is every ordinary capture.
22815
+ //
22816
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22817
+ // is the CAPTURE while `actionTaken` is per FINDING:
22818
+ //
22819
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22820
+ // `redact` alongside a finding ASSIGNED the same action stores both
22821
+ // identically and one reason for the pair; attributing it to both
22822
+ // describes the assigned one wrongly, and to neither loses the degrade.
22823
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22824
+ // became, not the reason the capture ended as it did — a capture denied
22825
+ // by some other finding's own Block policy still carries `block` here,
22826
+ // and clearing the workspace's fallback would not have let it through.
22827
+ // Gate on the value against what a fallback can produce; never read the
22828
+ // field's presence as "this was the fallback's doing".
22829
+ //
22830
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22831
+ // Closing either means moving the reason onto the finding row, which
22832
+ // already carries its own action.
22833
+ redactDegradedTo: ActionTaken.optional()
22717
22834
  }).meta({ id: "EventMetadata" });
22718
22835
  var Event = external_exports.object({
22719
22836
  id: external_exports.guid(),
@@ -22823,7 +22940,32 @@ var RotateKeyInput = external_exports.object({
22823
22940
  confirmation: external_exports.string()
22824
22941
  });
22825
22942
 
22943
+ // ../../packages/schema/src/zod/finding-delivery.ts
22944
+ var KNOWN_REASONS = SyncFailureReason.options;
22945
+ function knownReason(value) {
22946
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22947
+ }
22948
+ function deriveFindingDelivery(row) {
22949
+ if (row.kind === "code_change") return { state: "local_scan" };
22950
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22951
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22952
+ }
22953
+ if (row.syncedAt !== null) {
22954
+ const reason = knownReason(row.syncFailure);
22955
+ return {
22956
+ state: "not_sent",
22957
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22958
+ ...reason === void 0 ? {} : { reason }
22959
+ };
22960
+ }
22961
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22962
+ return { state: "never_offered" };
22963
+ }
22964
+
22826
22965
  // ../../packages/schema/src/zod/findings-group-build.ts
22966
+ function lookupOwn(map2, key) {
22967
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22968
+ }
22827
22969
  function toApiAction(dbVal) {
22828
22970
  const map2 = {
22829
22971
  log: "monitored",
@@ -22832,7 +22974,7 @@ function toApiAction(dbVal) {
22832
22974
  warn: "warned",
22833
22975
  allow: "allowed"
22834
22976
  };
22835
- return map2[dbVal] ?? "allowed";
22977
+ return lookupOwn(map2, dbVal) ?? "allowed";
22836
22978
  }
22837
22979
  function toApiCategory(dbVal) {
22838
22980
  if (dbVal === "code_context") return "source_code";
@@ -22840,13 +22982,18 @@ function toApiCategory(dbVal) {
22840
22982
  return parsed2.success ? parsed2.data : "custom";
22841
22983
  }
22842
22984
  function toApiProvider(sourceTool) {
22843
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22985
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22844
22986
  }
22845
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22987
+ var FINDING_STATUS_PRECEDENCE = [
22988
+ "open",
22989
+ "handled",
22990
+ "dismissed",
22991
+ "resolved"
22992
+ ];
22846
22993
  function foldGroupStatus(instanceStatuses) {
22847
22994
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22848
22995
  if (statuses.size === 0) return void 0;
22849
- for (const candidate of STATUS_PRECEDENCE) {
22996
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22850
22997
  if (statuses.has(candidate)) return candidate;
22851
22998
  }
22852
22999
  return void 0;
@@ -22953,11 +23100,16 @@ function applyFindingFilters(types, opts) {
22953
23100
  }
22954
23101
  return filtered;
22955
23102
  }
22956
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22957
- var SEVERITY_RANK = SEVERITY_ORDER;
23103
+ function rankByOrder(members2) {
23104
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23105
+ }
23106
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23107
+ function severityRank(severity) {
23108
+ return lookupOwn(SEVERITY_RANK, severity);
23109
+ }
22958
23110
  function compareFindingGroupOrder(a, b) {
22959
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22960
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23111
+ const rankA = severityRank(a.severity) ?? -1;
23112
+ const rankB = severityRank(b.severity) ?? -1;
22961
23113
  const severityDiff = rankA - rankB;
22962
23114
  if (severityDiff !== 0) return severityDiff;
22963
23115
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
@@ -23032,6 +23184,20 @@ function computeFindingFacets(allTypes, opts) {
23032
23184
  }
23033
23185
 
23034
23186
  // ../../packages/schema/src/zod/findings-flat-build.ts
23187
+ function compareCodePoints(a, b) {
23188
+ const aIter = a[Symbol.iterator]();
23189
+ const bIter = b[Symbol.iterator]();
23190
+ for (; ; ) {
23191
+ const aNext = aIter.next();
23192
+ const bNext = bIter.next();
23193
+ if (aNext.done && bNext.done) return 0;
23194
+ if (aNext.done) return -1;
23195
+ if (bNext.done) return 1;
23196
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23197
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23198
+ if (aPoint !== bPoint) return aPoint - bPoint;
23199
+ }
23200
+ }
23035
23201
  function rowHaystack(row) {
23036
23202
  return [
23037
23203
  row.ruleId,
@@ -23056,6 +23222,8 @@ function matchesDimension(row, opts, dimension) {
23056
23222
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23057
23223
  case "statuses":
23058
23224
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23225
+ case "deliveries":
23226
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23059
23227
  case "tools":
23060
23228
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23061
23229
  // An EMPTY value is a real filter here, not an absent one. The location
@@ -23082,6 +23250,7 @@ var DIMENSIONS = [
23082
23250
  "providers",
23083
23251
  "actions",
23084
23252
  "statuses",
23253
+ "deliveries",
23085
23254
  "tools",
23086
23255
  "repo",
23087
23256
  "file",
@@ -23095,10 +23264,19 @@ function matchesInstanceFilters(row, opts, except) {
23095
23264
  return true;
23096
23265
  }
23097
23266
  function toItems(counts) {
23098
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23267
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23268
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23269
+ // NFD spelling of the same text) as equal, so a count tie between
23270
+ // them would otherwise be ordered by whichever the Map iteration
23271
+ // produced. compareCodePoints breaks that tie deterministically, which
23272
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23273
+ // which it need not: foldFacetTuples runs this same sort over grouped
23274
+ // tuples, so both paths order facets identically by construction.
23275
+ compareCodePoints(a.value, b.value)
23276
+ );
23099
23277
  }
23100
- function bump(counts, value) {
23101
- counts.set(value, (counts.get(value) ?? 0) + 1);
23278
+ function bump(counts, value, by = 1) {
23279
+ counts.set(value, (counts.get(value) ?? 0) + by);
23102
23280
  }
23103
23281
  function createInstanceFacetAccumulator(opts) {
23104
23282
  const severity = /* @__PURE__ */ new Map();
@@ -23107,6 +23285,7 @@ function createInstanceFacetAccumulator(opts) {
23107
23285
  const action = /* @__PURE__ */ new Map();
23108
23286
  const status = /* @__PURE__ */ new Map();
23109
23287
  const tool = /* @__PURE__ */ new Map();
23288
+ const deployment = /* @__PURE__ */ new Map();
23110
23289
  return {
23111
23290
  add(row) {
23112
23291
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23121,6 +23300,9 @@ function createInstanceFacetAccumulator(opts) {
23121
23300
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23122
23301
  bump(tool, row.toolName);
23123
23302
  }
23303
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23304
+ bump(deployment, row.delivery.state);
23305
+ }
23124
23306
  },
23125
23307
  facets: () => ({
23126
23308
  severity: toItems(severity),
@@ -23128,7 +23310,8 @@ function createInstanceFacetAccumulator(opts) {
23128
23310
  provider: toItems(provider),
23129
23311
  action: toItems(action),
23130
23312
  status: toItems(status),
23131
- tool: toItems(tool)
23313
+ tool: toItems(tool),
23314
+ deployment: toItems(deployment)
23132
23315
  })
23133
23316
  };
23134
23317
  }
@@ -23142,6 +23325,7 @@ function toInstanceDetail(row) {
23142
23325
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23143
23326
  eventId: row.eventId,
23144
23327
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23328
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23145
23329
  ...row.user === void 0 ? {} : { user: row.user },
23146
23330
  action: toApiAction(row.actionTaken),
23147
23331
  detectedAt: row.occurredAt,
@@ -23156,12 +23340,6 @@ function toInstanceDetail(row) {
23156
23340
  policy: { id: `category:${category}`, name: category }
23157
23341
  };
23158
23342
  }
23159
- var SEVERITY_ORDER2 = {
23160
- critical: 0,
23161
- high: 1,
23162
- medium: 2,
23163
- low: 3
23164
- };
23165
23343
  function newLocationAccumulator() {
23166
23344
  return {
23167
23345
  instanceCount: 0,
@@ -23176,7 +23354,7 @@ function newLocationAccumulator() {
23176
23354
  }
23177
23355
  function addToLocation(acc, row) {
23178
23356
  acc.instanceCount += 1;
23179
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23357
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23180
23358
  if (rank < acc.maxSeverityRank) {
23181
23359
  acc.maxSeverityRank = rank;
23182
23360
  acc.maxSeverity = row.severity;
@@ -23186,15 +23364,15 @@ function addToLocation(acc, row) {
23186
23364
  acc.ruleIds.add(row.ruleId);
23187
23365
  }
23188
23366
  function compareLocationOrder(a, b) {
23189
- const rankA = SEVERITY_ORDER2[a.maxSeverity] ?? -1;
23190
- const rankB = SEVERITY_ORDER2[b.maxSeverity] ?? -1;
23367
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23368
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23191
23369
  if (rankA !== rankB) return rankA - rankB;
23192
23370
  if (a.latestDetectedAt !== b.latestDetectedAt) {
23193
23371
  return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23194
23372
  }
23195
- if (a.repo !== b.repo) return a.repo < b.repo ? -1 : 1;
23196
- if (a.file !== b.file) return a.file < b.file ? -1 : 1;
23197
- return 0;
23373
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23374
+ if (repoDiff !== 0) return repoDiff;
23375
+ return compareCodePoints(a.file, b.file);
23198
23376
  }
23199
23377
  function encodeLocationId(repo, file2) {
23200
23378
  return `${encodePart(repo)}/${encodePart(file2)}`;
@@ -23269,6 +23447,11 @@ var Policy = external_exports.object({
23269
23447
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23270
23448
  provenance: PolicyProvenance.optional()
23271
23449
  }).meta({ id: "Policy" });
23450
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23451
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23452
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23453
+ id: "RedactFallback"
23454
+ });
23272
23455
  var PolicyBundle = external_exports.object({
23273
23456
  version: external_exports.string(),
23274
23457
  policies: external_exports.array(Policy),
@@ -23316,6 +23499,16 @@ var PolicyBundle = external_exports.object({
23316
23499
  // control plane), so no name resolution stands between the decision and the
23317
23500
  // comparison.
23318
23501
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23502
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23503
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23504
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23505
+ // a control plane can tighten a machine and never loosen one — the same
23506
+ // direction `mergeRaiseOnly` enforces for policies.
23507
+ //
23508
+ // Optional so an older backend, and an older on-disk cache, still parses;
23509
+ // absent leaves the device's own setting in force, which is the behaviour
23510
+ // that predates the field and the safe direction to default.
23511
+ redactFallback: RedactFallback.optional(),
23319
23512
  customKeywords: external_exports.array(external_exports.string()),
23320
23513
  fetchedAt: external_exports.iso.datetime()
23321
23514
  }).meta({ id: "PolicyBundle" });
@@ -23345,11 +23538,6 @@ function severityFloorPolicy(category) {
23345
23538
  const peak = CATEGORY_PEAK_SEVERITY[category];
23346
23539
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23347
23540
  }
23348
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23349
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23350
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23351
- id: "RedactFallback"
23352
- });
23353
23541
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23354
23542
  var BUILTIN_POLICY_SPECS = {
23355
23543
  monitor: {
@@ -23405,6 +23593,11 @@ function isActionAtLeast(action, floor) {
23405
23593
  function strongerAction(a, b) {
23406
23594
  return actionRank(a) >= actionRank(b) ? a : b;
23407
23595
  }
23596
+ function strongerRedactFallback(local, remote) {
23597
+ if (remote === void 0) return local;
23598
+ const localAction = builtinPolicyToAction(local);
23599
+ return localAction === strongerAction(localAction, builtinPolicyToAction(remote)) ? local : remote;
23600
+ }
23408
23601
  function weakestBuiltinAtLeast(floor) {
23409
23602
  return KNOWN_BUILTIN_IDS.find((id) => isActionAtLeast(builtinPolicyToAction(id), floor)) ?? "block";
23410
23603
  }
@@ -23645,7 +23838,7 @@ var VaultConsent = external_exports.object({
23645
23838
  });
23646
23839
 
23647
23840
  // ../../packages/schema/src/zod/local.ts
23648
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23841
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23649
23842
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23650
23843
  var RunMode = external_exports.enum(["standalone", "attached"]);
23651
23844
  var ControlPlaneConnection = external_exports.object({
@@ -23665,6 +23858,15 @@ var HistorySyncConsent = external_exports.object({
23665
23858
  payloadVersion: external_exports.number().int().positive(),
23666
23859
  endpoint: external_exports.string()
23667
23860
  });
23861
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23862
+ var BodyRetention = external_exports.object({
23863
+ enabled: external_exports.boolean().default(false),
23864
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23865
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23866
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23867
+ // candidate set that is already bounded by "delivered, or never owed".
23868
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23869
+ }).meta({ id: "BodyRetention" });
23668
23870
  var WorkspaceSettings = external_exports.object({
23669
23871
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23670
23872
  runMode: RunMode.default("standalone"),
@@ -23713,7 +23915,13 @@ var WorkspaceSettings = external_exports.object({
23713
23915
  // carry prompt/reply/tool-output text in `content`; the key name predates
23714
23916
  // both widenings. Absent until granted, and a grant for a different endpoint
23715
23917
  // or an older payload no longer counts.
23716
- historySyncConsent: HistorySyncConsent.optional()
23918
+ historySyncConsent: HistorySyncConsent.optional(),
23919
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23920
+ // body never removes the row or its findings.
23921
+ bodyRetention: BodyRetention.default({
23922
+ enabled: false,
23923
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23924
+ })
23717
23925
  });
23718
23926
  function defaultWorkspaceSettings() {
23719
23927
  return WorkspaceSettings.parse({});
@@ -23808,12 +24016,15 @@ function toCaptureAttributes(event) {
23808
24016
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23809
24017
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23810
24018
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24019
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23811
24020
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23812
24021
  // has ever populated either), but every legacy metadata key still rides
23813
24022
  // the bag rather than being silently dropped — CaptureAttributes'
23814
24023
  // `.catchall(z.unknown())` carries the long tail.
23815
24024
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23816
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24025
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24026
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24027
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23817
24028
  };
23818
24029
  }
23819
24030
  function captureDefinitionVersion(finding) {
@@ -23841,13 +24052,22 @@ var ManagedSettingKey = external_exports.enum([
23841
24052
  "vaultInlineReveal",
23842
24053
  "modelJudgeConsent",
23843
24054
  "dataSharesInPlace",
23844
- "redactFallback"
24055
+ "redactFallback",
24056
+ // Pins the toggle and the day count together — see BodyRetention on why the
24057
+ // two are one unit. An administrator mandating a window wants the count
24058
+ // enforced with it, not one a user can widen while the toggle stays on.
24059
+ "bodyRetention"
23845
24060
  ]).meta({ id: "ManagedSettingKey" });
23846
24061
  function isManagedSettingKey(value) {
23847
24062
  return ManagedSettingKey.safeParse(value).success;
23848
24063
  }
23849
24064
  var ManagedSettingsValues = external_exports.object({
23850
24065
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24066
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24067
+ // plain, non-strict objects: a key under either that this build does not know
24068
+ // is stripped and nothing reports it. The unknown-value split in
24069
+ // ManagedSettings below classifies top-level names only, so it stops at
24070
+ // these boundaries.
23851
24071
  controlPlane: external_exports.object({
23852
24072
  endpoint: external_exports.string().min(1),
23853
24073
  label: external_exports.string().min(1).optional()
@@ -23858,7 +24078,8 @@ var ManagedSettingsValues = external_exports.object({
23858
24078
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23859
24079
  modelJudgeConsent: external_exports.boolean().optional(),
23860
24080
  dataSharesInPlace: external_exports.boolean().optional(),
23861
- redactFallback: RedactFallback.optional()
24081
+ redactFallback: RedactFallback.optional(),
24082
+ bodyRetention: BodyRetention.optional()
23862
24083
  }).meta({ id: "ManagedSettingsValues" });
23863
24084
  var ManagedSettings = external_exports.object({
23864
24085
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23866,7 +24087,21 @@ var ManagedSettings = external_exports.object({
23866
24087
  // decision from a bug. Absent renders as a generic "your organization".
23867
24088
  organization: external_exports.string().min(1).optional(),
23868
24089
  // What the administrator pinned.
23869
- values: ManagedSettingsValues.default({}),
24090
+ //
24091
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24092
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24093
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24094
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24095
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24096
+ // exactly the file an administrator is most likely to write while a fleet
24097
+ // is mid-upgrade.
24098
+ //
24099
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24100
+ // file, which is the outcome the lock half already rejected — an older
24101
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24102
+ // value still fails, because the nested schema is re-run over the known
24103
+ // subset and its issues are re-raised on this parse.
24104
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23870
24105
  // Which of those the user may not change. A key here with no matching value
23871
24106
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23872
24107
  // the user may still override. The two are separable on purpose.
@@ -23879,17 +24114,31 @@ var ManagedSettings = external_exports.object({
23879
24114
  // the fleets most likely to carry a version skew. A name outside the enum
23880
24115
  // is still never HONOURED: the lockable set stays explicit above.
23881
24116
  lockedFields: external_exports.array(external_exports.string()).default([])
23882
- }).transform(({ lockedFields, ...rest }) => {
24117
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
23883
24118
  const known = [];
23884
24119
  const unknown2 = [];
23885
24120
  for (const name of lockedFields) {
23886
24121
  if (isManagedSettingKey(name)) known.push(name);
23887
24122
  else unknown2.push(name);
23888
24123
  }
24124
+ const knownValues = /* @__PURE__ */ Object.create(null);
24125
+ const unknownValues = [];
24126
+ for (const [name, value] of Object.entries(values)) {
24127
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24128
+ else unknownValues.push(name);
24129
+ }
24130
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24131
+ if (!pinned.success) {
24132
+ for (const issue2 of pinned.error.issues)
24133
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24134
+ return external_exports.NEVER;
24135
+ }
23889
24136
  return {
23890
24137
  ...rest,
24138
+ values: pinned.data,
23891
24139
  lockedFields: known,
23892
- ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {}
24140
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24141
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
23893
24142
  };
23894
24143
  }).meta({ id: "ManagedSettings" });
23895
24144
 
@@ -24153,7 +24402,23 @@ var SaveSettingsInput = external_exports.object({
24153
24402
  modelJudgeConsent: ModelJudgeConsentChoice,
24154
24403
  historySyncConsent: HistorySyncConsentChoice,
24155
24404
  vaultConsent: external_exports.string(),
24156
- vaultInlineReveal: external_exports.string()
24405
+ vaultInlineReveal: external_exports.string(),
24406
+ // Widened to `string` like its neighbours rather than typed as
24407
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24408
+ // the call site, so the domain check receives the type it was written for.
24409
+ //
24410
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24411
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24412
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24413
+ // trade against. The real cost runs the other way and is the part worth
24414
+ // knowing: a value this schema admits and the domain enum then rejects lands
24415
+ // on the action's shared refusal, which names NO field, where a shape
24416
+ // rejection reaches `malformedInput` and names the schema key.
24417
+ redactFallback: external_exports.string(),
24418
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24419
+ // `BodyRetention`'s and the action checks it there, so there is one place
24420
+ // that decides what a legal horizon is rather than two that can drift.
24421
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24157
24422
  });
24158
24423
  var AttachInput = external_exports.object({
24159
24424
  endpoint: external_exports.string(),
@@ -24325,6 +24590,52 @@ function reviewSeverityRank(reasons) {
24325
24590
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24326
24591
  }
24327
24592
 
24593
+ // ../../packages/schema/src/zod/web-capture.ts
24594
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24595
+ var WebUsage = external_exports.object({
24596
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24597
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24598
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24599
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24600
+ });
24601
+ var WebToolCall = external_exports.object({
24602
+ toolUseId: external_exports.string().min(1),
24603
+ toolName: external_exports.string().min(1),
24604
+ target: external_exports.string().optional(),
24605
+ isError: external_exports.boolean().optional(),
24606
+ inputSize: external_exports.number().int().nonnegative().optional(),
24607
+ outputSize: external_exports.number().int().nonnegative().optional()
24608
+ });
24609
+ var WebExchange = external_exports.object({
24610
+ messageId: external_exports.string().min(1),
24611
+ startedAt: external_exports.iso.datetime(),
24612
+ model: external_exports.string().optional(),
24613
+ usage: WebUsage.optional(),
24614
+ usageSource: WebUsageSource,
24615
+ stopReason: external_exports.string().optional(),
24616
+ conversationId: external_exports.string().optional(),
24617
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24618
+ toolCalls: external_exports.array(WebToolCall).default([]),
24619
+ // Absent when the adapter recovered no text. Capped by the caller at
24620
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24621
+ // short capture is never mistaken for a short reply.
24622
+ responseText: external_exports.string().optional(),
24623
+ truncated: external_exports.boolean().default(false)
24624
+ });
24625
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24626
+ var WebCaptureStatus = external_exports.object({
24627
+ patched: external_exports.boolean(),
24628
+ live: external_exports.boolean(),
24629
+ blind: external_exports.boolean(),
24630
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24631
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24632
+ parseFailures: external_exports.number().int().nonnegative(),
24633
+ unparsedBodies: external_exports.number().int().nonnegative(),
24634
+ // The adapter-declared JSON key paths that were absent from a real payload —
24635
+ // the earliest signal that a site's contract moved.
24636
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24637
+ });
24638
+
24328
24639
  // ../../packages/persistence/src/paths.ts
24329
24640
  import {
24330
24641
  chmodSync,
@@ -24685,6 +24996,22 @@ function discardStore(file2, backup) {
24685
24996
  }
24686
24997
  }
24687
24998
 
24999
+ // ../../packages/persistence/src/internal/sql-functions.ts
25000
+ var utf8 = new TextDecoder();
25001
+ function akaLower(value) {
25002
+ if (value === null) return null;
25003
+ if (typeof value === "string") return value.toLowerCase();
25004
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
25005
+ return utf8.decode(value).toLowerCase();
25006
+ }
25007
+ function registerSqlFunctions(db) {
25008
+ db.function(
25009
+ "aka_lower",
25010
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
25011
+ akaLower
25012
+ );
25013
+ }
25014
+
24688
25015
  // ../../packages/persistence/src/internal/sql-text.ts
24689
25016
  function escapeLikePattern(s) {
24690
25017
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24769,6 +25096,11 @@ function schemaObjectExists(db, kind, name) {
24769
25096
  function indexExists(db, name) {
24770
25097
  return schemaObjectExists(db, "index", name);
24771
25098
  }
25099
+ function indexColumns(db, name) {
25100
+ if (!indexExists(db, name)) return [];
25101
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25102
+ return columns.map((c) => c.name).filter((c) => c !== null);
25103
+ }
24772
25104
  function columnNames(db, table2, opts) {
24773
25105
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24774
25106
  const columns = db.prepare(`PRAGMA ${pragma}(${table2})`).all();
@@ -24830,176 +25162,818 @@ function mapRowsTolerant(rows, map2) {
24830
25162
  return out;
24831
25163
  }
24832
25164
 
24833
- // ../../packages/persistence/src/migrations.ts
24834
- function describeObject(object2) {
24835
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24836
- }
24837
- function splitStatements(sql) {
24838
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24839
- }
24840
- function createdIndexName(statement) {
24841
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24842
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25165
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25166
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25167
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25168
+
25169
+ // ../../packages/persistence/src/sync-failure.ts
25170
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25171
+ function syncFailureRejectCondition(column = "sync_failure") {
25172
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25173
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24843
25174
  }
24844
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24845
- function applyMigrations(db, file2) {
24846
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24847
- db.exec(
24848
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24849
- );
24850
- const applied = new Set(
24851
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24852
- );
24853
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24854
- const record2 = db.prepare(
24855
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24856
- );
24857
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24858
- if (applied.has(migration.tag)) continue;
24859
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24860
- const evidence = evidenceObjects(migration.sql);
24861
- const present2 = evidence.filter((o) => evidenceExists(db, o));
24862
- if (present2.length > 0 && present2.length < evidence.length) {
24863
- const missing = evidence.filter((o) => !present2.includes(o));
24864
- const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present2.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 ? present2.length === evidence.length : preLedgerStore && index < legacyCount;
24869
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24870
- const statements = splitStatements(migration.sql);
24871
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24872
- try {
24873
- withTransaction(
24874
- db,
24875
- () => {
24876
- for (const statement of statements) {
24877
- const indexName = createdIndexName(statement);
24878
- if (indexName === void 0) {
24879
- if (alreadyApplied) continue;
24880
- } else if (indexExists(db, indexName)) {
24881
- continue;
24882
- }
24883
- db.exec(statement);
24884
- }
24885
- if (wantsFkOff && !alreadyApplied) {
24886
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24887
- if (violations.length > 0) {
24888
- throw new Error(
24889
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24890
- );
24891
- }
24892
- }
24893
- record2.run(migration.tag, Date.now());
24894
- },
24895
- "IMMEDIATE"
24896
- );
24897
- } finally {
24898
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24899
- }
25175
+
25176
+ // ../../packages/persistence/src/repositories/history-sync.ts
25177
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25178
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25179
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25180
+ var COUNTED_EVENT_TYPES = [
25181
+ ...STRUCTURAL_EVENT_TYPES,
25182
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25183
+ ];
25184
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25185
+ var PARTITION_BUCKETS = `
25186
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25187
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25188
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25189
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25190
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25191
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25192
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25193
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25194
+ -- added later lands in no bucket and fails the sum assertion, instead
25195
+ -- of silently joining this one.
25196
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25197
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25198
+ THEN 1 ELSE 0 END) AS failed,
25199
+ COUNT(*) AS total`;
25200
+ var COUNTED_SCOPE = `
25201
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25202
+ AND (
25203
+ event_type IN (${TYPE_LIST})
25204
+ OR synced_at IS NOT NULL
25205
+ OR outbox_owed = 1
25206
+ )`;
25207
+ var SKIPPED = -1;
25208
+ var ROW_COLUMNS = `id,
25209
+ parent_id AS parentId,
25210
+ root_session_id AS rootSessionId,
25211
+ event_type AS eventType,
25212
+ host_id AS hostId,
25213
+ harness_id AS harnessId,
25214
+ source_project_id AS sourceProjectId,
25215
+ started_at AS startedAt,
25216
+ ended_at AS endedAt,
25217
+ severity,
25218
+ priority,
25219
+ content,
25220
+ content_hash AS contentHash,
25221
+ attributes`;
25222
+ var SqliteHistorySyncRepository = class {
25223
+ constructor(db) {
25224
+ this.db = db;
25225
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25226
+ this.sessionsStmt = db.prepare(
25227
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25228
+ FROM audit_events
25229
+ WHERE synced_at IS NULL
25230
+ AND event_type IN (${TYPE_LIST})
25231
+ AND started_at < :before
25232
+ GROUP BY sessionId
25233
+ ORDER BY earliest
25234
+ LIMIT :limit`
25235
+ );
25236
+ this.rowsStmt = db.prepare(
25237
+ `SELECT ${ROW_COLUMNS}
25238
+ FROM audit_events
25239
+ WHERE synced_at IS NULL
25240
+ AND event_type IN (${TYPE_LIST})
25241
+ AND started_at < :before
25242
+ AND COALESCE(root_session_id, id) = :sessionId
25243
+ ORDER BY (event_type = 'session') DESC, started_at
25244
+ LIMIT :limit`
25245
+ );
25246
+ this.captureRowsStmt = db.prepare(
25247
+ `SELECT ${ROW_COLUMNS}
25248
+ FROM audit_events
25249
+ WHERE synced_at IS NULL
25250
+ AND sync_claimed_at IS NULL
25251
+ AND outbox_owed = 1
25252
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25253
+ AND started_at < :before
25254
+ ORDER BY started_at
25255
+ LIMIT :limit`
25256
+ );
25257
+ this.markOwedStmt = db.prepare(
25258
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25259
+ );
25260
+ this.markCaptureBacklogOwedStmt = db.prepare(
25261
+ `UPDATE audit_events SET outbox_owed = 1
25262
+ WHERE synced_at IS NULL
25263
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25264
+ AND started_at < :before`
25265
+ );
25266
+ this.stampStmt = db.prepare(
25267
+ `UPDATE audit_events
25268
+ SET synced_at = :at,
25269
+ sync_claimed_at = NULL,
25270
+ sync_failed_at = :failedAt,
25271
+ sync_failure = :failure
25272
+ WHERE id = :id`
25273
+ );
25274
+ this.claimRowStmt = db.prepare(
25275
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25276
+ );
25277
+ this.releaseRowStmt = db.prepare(
25278
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25279
+ );
25280
+ this.releaseStaleClaimsStmt = db.prepare(
25281
+ `UPDATE audit_events SET sync_claimed_at = NULL
25282
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25283
+ );
25284
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25285
+ FROM audit_events${COUNTED_SCOPE}`);
25286
+ this.partitionByKindStmt = db.prepare(
25287
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25288
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25289
+ GROUP BY event_type`
25290
+ );
25291
+ this.countsStmt = db.prepare(
25292
+ `SELECT
25293
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25294
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25295
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25296
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25297
+ THEN 1 ELSE 0 END) AS skipped,
25298
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25299
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25300
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25301
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25302
+ FROM audit_events
25303
+ WHERE event_type IN (${TYPE_LIST})`
25304
+ );
25305
+ this.captureSkipCountStmt = db.prepare(
25306
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25307
+ // way the structural totals are. The split exists because a refusal is
25308
+ // terminal only against the deployment that gave it, and the structural
25309
+ // re-arm frees it on a change of deployment. The capture lane has no such
25310
+ // escape: re-arming a capture would offer one deployment's undelivered
25311
+ // prompts, with their text, to a deployment that never saw them, which is
25312
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25313
+ // reasons mean the same thing — this row will not be sent — and splitting
25314
+ // them would put refused captures in a bucket nothing reads and nothing
25315
+ // frees.
25316
+ `SELECT COUNT(*) AS skipped
25317
+ FROM audit_events
25318
+ WHERE synced_at = ${String(SKIPPED)}
25319
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25320
+ );
25321
+ this.fingerprintStmt = db.prepare(
25322
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25323
+ FROM history_sync WHERE id = 1`
25324
+ );
25325
+ this.setFingerprintStmt = db.prepare(
25326
+ `UPDATE history_sync
25327
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25328
+ WHERE id = 1`
25329
+ );
25330
+ this.disownCapturesStmt = db.prepare(
25331
+ `UPDATE audit_events SET outbox_owed = NULL
25332
+ WHERE outbox_owed IS NOT NULL
25333
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25334
+ AND started_at < :attachedAt`
25335
+ );
25336
+ this.rearmStmt = db.prepare(
25337
+ `UPDATE audit_events
25338
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25339
+ WHERE (synced_at > 0
25340
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25341
+ AND event_type IN (${TYPE_LIST})`
25342
+ );
25343
+ this.claimStmt = db.prepare(
25344
+ `UPDATE history_sync
25345
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25346
+ WHERE id = 1
25347
+ AND (owner_pid IS NULL
25348
+ OR heartbeat_at IS NULL
25349
+ OR heartbeat_at < :staleBefore
25350
+ OR heartbeat_at > :now)`
25351
+ );
25352
+ this.heartbeatStmt = db.prepare(
25353
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25354
+ );
25355
+ this.releaseStmt = db.prepare(
25356
+ `UPDATE history_sync
25357
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25358
+ WHERE id = 1 AND owner_pid = :pid`
25359
+ );
25360
+ this.closeWindowStmt = db.prepare(
25361
+ `UPDATE audit_events
25362
+ SET synced_at = ${String(SKIPPED)},
25363
+ sync_failed_at = :at,
25364
+ sync_failure = 'detached_undelivered'
25365
+ WHERE synced_at IS NULL
25366
+ AND event_type IN (${TYPE_LIST})
25367
+ AND started_at >= :attachedAt`
25368
+ );
25369
+ this.releaseBoundaryStmt = db.prepare(
25370
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25371
+ );
25372
+ this.freezeBoundaryStmt = db.prepare(
25373
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25374
+ );
25375
+ this.leaseStmt = db.prepare(
25376
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25377
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25378
+ FROM history_sync WHERE id = 1`
25379
+ );
25380
+ this.inspectionsStmt = db.prepare(
25381
+ `SELECT d.rule_id AS ruleId,
25382
+ d.name AS ruleName,
25383
+ d.version AS ruleVersion,
25384
+ d.category AS category,
25385
+ d.severity AS severity,
25386
+ f.span_start AS spanStart,
25387
+ f.span_end AS spanEnd,
25388
+ f.masked_match AS maskedMatch,
25389
+ f.action_taken AS actionTaken,
25390
+ f.confidence AS confidence
25391
+ FROM inspection_findings f
25392
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25393
+ WHERE f.audit_event_id = :auditEventId
25394
+ ORDER BY f.span_start, f.id`
25395
+ );
24900
25396
  }
24901
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24902
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25397
+ db;
25398
+ ensureRowStmt;
25399
+ sessionsStmt;
25400
+ rowsStmt;
25401
+ stampStmt;
25402
+ countsStmt;
25403
+ fingerprintStmt;
25404
+ setFingerprintStmt;
25405
+ rearmStmt;
25406
+ claimStmt;
25407
+ heartbeatStmt;
25408
+ releaseStmt;
25409
+ leaseStmt;
25410
+ inspectionsStmt;
25411
+ closeWindowStmt;
25412
+ releaseBoundaryStmt;
25413
+ freezeBoundaryStmt;
25414
+ captureRowsStmt;
25415
+ markOwedStmt;
25416
+ markCaptureBacklogOwedStmt;
25417
+ captureSkipCountStmt;
25418
+ disownCapturesStmt;
25419
+ partitionStmt;
25420
+ partitionByKindStmt;
25421
+ claimRowStmt;
25422
+ releaseRowStmt;
25423
+ releaseStaleClaimsStmt;
25424
+ /**
25425
+ * The masked detections recorded against one tool call.
25426
+ *
25427
+ * These travel with the event because a tool call's target is not
25428
+ * re-inspectable from the event alone — unlike a capture, where the text
25429
+ * itself is re-scannable. What crosses is the masked match and the rule that
25430
+ * produced it, never the value.
25431
+ */
25432
+ inspectionsFor(auditEventId) {
25433
+ return allRows(this.inspectionsStmt, { auditEventId });
24903
25434
  }
24904
- ensureSyncedAtColumn(db, "audit_events");
24905
- ensureScanLedgerTable(db);
24906
- ensureHistorySyncTable(db);
24907
- ensureBlockedDetectionsTable(db);
24908
- ensureRuleProbeCacheTable(db);
24909
- ensureWriteGateTrigger(db);
24910
- ensureTokenUsageColumns(db);
24911
- reconcileSourceProjectIds(db);
24912
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24913
- const drained = runLegacyHistoryBackfill(db);
24914
- if (drained) applyLegacyDropMigration(db, file2);
25435
+ /**
25436
+ * Sessions with structural rows still to send, oldest first.
25437
+ *
25438
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25439
+ * read. Anything recorded after the machine attached is the live forward
25440
+ * path's to deliver; this drain exists for what was recorded before it, and a
25441
+ * row both paths send is at best a duplicate request and at worst — for a
25442
+ * session root — an overwrite of the inventory ids the live path resolved.
25443
+ */
25444
+ pendingSessions(limit, before) {
25445
+ return allRows(this.sessionsStmt, { limit, before }).map(
25446
+ (r) => r.sessionId
25447
+ );
24915
25448
  }
24916
- }
24917
- function readLegacyTables(db) {
24918
- let holdsRows = false;
24919
- const marks = [];
24920
- for (const table2 of ["events", "findings"]) {
24921
- try {
24922
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
24923
- if (row === void 0) {
24924
- holdsRows = true;
24925
- marks.push(`${table2}:unreadable`);
24926
- continue;
24927
- }
24928
- if (row.n > 0) holdsRows = true;
24929
- marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
24930
- } catch {
24931
- holdsRows = true;
24932
- marks.push(`${table2}:unreadable`);
24933
- }
25449
+ /** One session's undelivered structural rows within the backlog, root first. */
25450
+ pendingRows(sessionId, limit, before) {
25451
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24934
25452
  }
24935
- return { holdsRows, mark: marks.join("|") };
24936
- }
24937
- function applyLegacyDropMigration(db, file2) {
24938
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24939
- if (!migration) return;
24940
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24941
- if (file2 !== void 0 && before?.holdsRows === true) {
24942
- try {
24943
- backupBeforeLegacyDrop(db, file2);
24944
- } catch (error61) {
24945
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24946
- return;
24947
- }
25453
+ /**
25454
+ * Captures this machine still owes the deployment, oldest first.
25455
+ *
25456
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25457
+ * by a time window — see captureRowsStmt for why a window could not express
25458
+ * this. `before` is the grace window that leaves a just-recorded capture to
25459
+ * the live path.
25460
+ */
25461
+ pendingCaptureRows(limit, before) {
25462
+ return allRows(this.captureRowsStmt, { limit, before });
24948
25463
  }
24949
- try {
25464
+ /**
25465
+ * Record that a capture is OWED to the deployment.
25466
+ *
25467
+ * Written by the attached forward path when a live send did not confirm
25468
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25469
+ * a fact rather than an inference: the machine was attached, the send did not
25470
+ * land, so the row is owed — which no time window can state, because the same
25471
+ * window that holds the rows a past attachment left owed also holds every
25472
+ * capture recorded while the machine was DETACHED, and those were never
25473
+ * offered to anyone.
25474
+ *
25475
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25476
+ * out of the drain's read.
25477
+ */
25478
+ markCaptureOwed(id) {
25479
+ this.markOwedStmt.run({ id });
25480
+ }
25481
+ /**
25482
+ * Mark every capture already on disk as owed, as of `before`.
25483
+ *
25484
+ * The consent-time backfill, called once from `aka attach` when a human
25485
+ * grants existing-history consent — never from an ongoing drain pass, and
25486
+ * never inferred from a boundary that could later move. `before` is the
25487
+ * caller's own "now" at the moment consent was granted, so what this marks
25488
+ * is exactly the backlog the consent prompt already counted, not whatever a
25489
+ * later re-attach or key rotation might widen it to.
25490
+ *
25491
+ * Returns how many rows matched, for the caller to log or test against. Not a
25492
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25493
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25494
+ */
25495
+ markCaptureBacklogOwed(before) {
25496
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25497
+ }
25498
+ /**
25499
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25500
+ *
25501
+ * CLEARS any failure reason in the same statement. A row that failed against
25502
+ * one deployment and then landed is delivered, and leaving the reason behind
25503
+ * would leave the store holding two contradictory answers about one row —
25504
+ * with the surface free to render either.
25505
+ */
25506
+ markSynced(ids, atMs) {
25507
+ this.stampAll(ids, atMs, null);
25508
+ }
25509
+ /**
25510
+ * Record that THIS MACHINE cannot express the row on the wire.
25511
+ *
25512
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25513
+ * payload, or a body the client itself refused to send. It fails identically
25514
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25515
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25516
+ * is retried; marking those would turn one outage into permanent data loss.
25517
+ */
25518
+ markSkipped(ids, atMs) {
25519
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25520
+ }
25521
+ /**
25522
+ * Record that THIS DEPLOYMENT refused the row.
25523
+ *
25524
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25525
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25526
+ * row is outstanding rather than why. What separates them is the reason, and
25527
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25528
+ * on one body, so it is terminal only for as long as this machine points at
25529
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25530
+ *
25531
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25532
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25533
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25534
+ */
25535
+ markRefused(ids, atMs) {
25536
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25537
+ }
25538
+ eachInTransaction(ids, run) {
25539
+ if (ids.length === 0) return;
24950
25540
  withTransaction(
24951
- db,
25541
+ this.db,
24952
25542
  () => {
24953
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24954
- if (alreadyDropped) return;
24955
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24956
- akaWarn(
24957
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24958
- );
24959
- return;
24960
- }
24961
- for (const statement of splitStatements(migration.sql)) {
24962
- db.exec(statement);
24963
- }
24964
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24965
- migration.tag,
24966
- Date.now()
24967
- );
25543
+ for (const id of ids) run(id);
24968
25544
  },
24969
25545
  "IMMEDIATE"
24970
25546
  );
24971
- } catch (error61) {
24972
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24973
25547
  }
24974
- }
24975
- function backupBeforeLegacyDrop(db, file2) {
24976
- reapStalePartials(file2);
24977
- const backup = backupPath(file2, "pre-drop");
24978
- snapshotStore(db, backup);
24979
- return backup;
24980
- }
24981
- var TOKEN_USAGE_COLUMNS = [
24982
- {
24983
- name: "input_tokens",
24984
- ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
24985
- },
24986
- {
24987
- name: "output_tokens",
24988
- ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
24989
- },
24990
- {
24991
- name: "cache_creation_input_tokens",
24992
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
24993
- },
24994
- {
24995
- name: "cache_read_input_tokens",
24996
- ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
24997
- },
24998
- {
24999
- name: "model",
25000
- ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25001
- },
25002
- {
25548
+ stampAll(ids, value, failure, failedAtMs) {
25549
+ if (ids.length === 0) return;
25550
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25551
+ withTransaction(
25552
+ this.db,
25553
+ () => {
25554
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25555
+ },
25556
+ "IMMEDIATE"
25557
+ );
25558
+ }
25559
+ /**
25560
+ * Claim rows as in-flight.
25561
+ *
25562
+ * Advisory in exactly the sense the lease is: it records that a send is in
25563
+ * progress so a surface can say so, and a lost claim costs a row showing as
25564
+ * queued while it is actually being sent. It is not exclusion — the far side
25565
+ * settles a duplicate on the row id.
25566
+ */
25567
+ claimRows(ids, atMs) {
25568
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25569
+ }
25570
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25571
+ releaseRows(ids) {
25572
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25573
+ }
25574
+ /**
25575
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25576
+ *
25577
+ * A process killed between claiming and settling leaves rows claimed with
25578
+ * nothing left to settle them. Without this they read as "sending" for ever.
25579
+ */
25580
+ releaseStaleClaims(staleBefore) {
25581
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25582
+ }
25583
+ /**
25584
+ * Every tracked row in exactly one delivery state.
25585
+ *
25586
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25587
+ * pick up now", which is a different question from "what state is this row
25588
+ * in" — and a machine that has never attached has no boundary to pass, so
25589
+ * requiring one would force a caller to invent one and report the whole store
25590
+ * as queued.
25591
+ */
25592
+ /**
25593
+ * The same partition, one row per kind that a lane carries.
25594
+ *
25595
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25596
+ * scope decides which rows exist at all, so a kind that has never been
25597
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25598
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25599
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25600
+ * different things.
25601
+ */
25602
+ partitionByKind() {
25603
+ return allRows(
25604
+ this.partitionByKindStmt,
25605
+ {}
25606
+ ).map((row) => ({
25607
+ kind: row.kind,
25608
+ queued: row.queued ?? 0,
25609
+ inProgress: row.inProgress ?? 0,
25610
+ synced: row.synced ?? 0,
25611
+ failed: row.failed ?? 0,
25612
+ refused: row.refused ?? 0,
25613
+ detached: row.detached ?? 0,
25614
+ total: row.total ?? 0
25615
+ }));
25616
+ }
25617
+ partition() {
25618
+ const row = getRow(this.partitionStmt, {});
25619
+ return {
25620
+ queued: row?.queued ?? 0,
25621
+ inProgress: row?.inProgress ?? 0,
25622
+ synced: row?.synced ?? 0,
25623
+ failed: row?.failed ?? 0,
25624
+ refused: row?.refused ?? 0,
25625
+ detached: row?.detached ?? 0,
25626
+ total: row?.total ?? 0
25627
+ };
25628
+ }
25629
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25630
+ counts(before) {
25631
+ const row = getRow(this.countsStmt, { before });
25632
+ const captures = getRow(this.captureSkipCountStmt);
25633
+ return {
25634
+ pending: row?.pending ?? 0,
25635
+ sent: row?.sent ?? 0,
25636
+ skipped: row?.skipped ?? 0,
25637
+ refused: row?.refused ?? 0,
25638
+ detached: row?.detached ?? 0,
25639
+ capturesSkipped: captures?.skipped ?? 0
25640
+ };
25641
+ }
25642
+ /**
25643
+ * The deployment the current stamps were made against, and where its backlog
25644
+ * ends.
25645
+ *
25646
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25647
+ * machine that has never drained is — and every writer below seeds the row
25648
+ * before it needs one, so nothing depends on this creating it. Keeping the
25649
+ * write off the gate path matters because the gate runs on every pass while a
25650
+ * write has to take the database's write lock.
25651
+ */
25652
+ deployment() {
25653
+ const row = getRow(
25654
+ this.fingerprintStmt
25655
+ );
25656
+ return {
25657
+ fingerprint: row?.fingerprint ?? void 0,
25658
+ backlogBefore: row?.backlogBefore ?? void 0
25659
+ };
25660
+ }
25661
+ /**
25662
+ * Point the ledger at a different deployment, discarding what it recorded
25663
+ * about the previous one.
25664
+ *
25665
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25666
+ * machine has just left are undelivered as far as the new one is concerned.
25667
+ * All four in one transaction, so a crash between them cannot leave stamps
25668
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25669
+ * a disown with no re-mark to follow it.
25670
+ *
25671
+ * The boundary is written HERE and only here, which is what freezes it: a
25672
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25673
+ * unchanged, so this never runs and the backlog does not widen back over rows
25674
+ * the live path has since delivered.
25675
+ *
25676
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25677
+ * granted existing-history consent for the deployment this call is arming —
25678
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25679
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25680
+ * apart. Passed only when that grant is valid, since this method has no way
25681
+ * to check consent itself and must not mark a row owed for a machine that
25682
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25683
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25684
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25685
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25686
+ * on the cleared side of that bound — and the re-mark in the same
25687
+ * transaction is what puts those rows back. A crash between the two cannot
25688
+ * strand the ledger disowned with nothing re-marked — the transaction either
25689
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25690
+ * committed re-enters this method on the very next pass. Omit it (the
25691
+ * structural-only tests do) to exercise the disown in isolation.
25692
+ *
25693
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25694
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25695
+ * live path can mark a capture owed from the moment `aka attach` writes the
25696
+ * descriptor, before the drain's first pass ever reaches this method, and
25697
+ * such a row sits at or after the bound rather than below it. What keeps the
25698
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25699
+ * bound — disown runs first, re-mark second, both inside the one
25700
+ * transaction above.
25701
+ */
25702
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25703
+ this.ensureRowStmt.run();
25704
+ withTransaction(
25705
+ this.db,
25706
+ () => {
25707
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25708
+ this.rearmStmt.run();
25709
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25710
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25711
+ }
25712
+ if (backfillCapturesBefore !== void 0) {
25713
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25714
+ }
25715
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25716
+ },
25717
+ "IMMEDIATE"
25718
+ );
25719
+ }
25720
+ /**
25721
+ * End the attached period: hand its rows to the live path, and release the
25722
+ * boundary so the next attachment can freeze a new one.
25723
+ *
25724
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25725
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25726
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25727
+ * during the detached period, because the machine is not attached. Rows
25728
+ * recorded in that window sit after the boundary and before the re-attach, so
25729
+ * neither path takes them, and the pending count reports none outstanding.
25730
+ *
25731
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25732
+ * closing attachment's to deliver and are no longer outstanding — that is what
25733
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25734
+ * distinction is not academic: this used to write a delivery TIME, which every
25735
+ * read treats as delivery, so one detach turned a window of undelivered rows
25736
+ * into a window of delivered ones and no surface could tell. It writes the
25737
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25738
+ * "received" stop being the same fact.
25739
+ *
25740
+ * A change of deployment still frees them (see the re-arm), because the next
25741
+ * deployment has seen none of this machine's history — so the rows reach it
25742
+ * exactly as they did when this wrote a delivery time.
25743
+ *
25744
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25745
+ * window unstamped — that half-state would re-send the whole attached period
25746
+ * on the next attach, which is the failure the boundary exists to prevent.
25747
+ */
25748
+ closeAttachedWindow(attachedAtMs, atMs) {
25749
+ this.ensureRowStmt.run();
25750
+ withTransaction(
25751
+ this.db,
25752
+ () => {
25753
+ const row = getRow(this.fingerprintStmt);
25754
+ const from = row?.backlogBefore ?? attachedAtMs;
25755
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25756
+ this.releaseBoundaryStmt.run();
25757
+ },
25758
+ "IMMEDIATE"
25759
+ );
25760
+ }
25761
+ /**
25762
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25763
+ *
25764
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25765
+ * different deployment and therefore discards what was delivered to the old
25766
+ * one: here the recipient is the same, so everything already sent to it stays
25767
+ * sent.
25768
+ */
25769
+ freezeBoundary(backlogBefore) {
25770
+ this.ensureRowStmt.run();
25771
+ this.freezeBoundaryStmt.run({ backlogBefore });
25772
+ }
25773
+ /** Take the claim, or report that someone live already holds it. */
25774
+ claim(pid, host, nowMs, staleAfterMs) {
25775
+ this.ensureRowStmt.run();
25776
+ let taken = false;
25777
+ withTransaction(
25778
+ this.db,
25779
+ () => {
25780
+ const result = this.claimStmt.run({
25781
+ pid,
25782
+ host,
25783
+ now: nowMs,
25784
+ staleBefore: nowMs - staleAfterMs
25785
+ });
25786
+ taken = result.changes === 1;
25787
+ },
25788
+ "IMMEDIATE"
25789
+ );
25790
+ return taken;
25791
+ }
25792
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25793
+ heartbeat(pid, nowMs) {
25794
+ this.heartbeatStmt.run({ now: nowMs, pid });
25795
+ }
25796
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25797
+ release(pid) {
25798
+ this.releaseStmt.run({ pid });
25799
+ }
25800
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25801
+ lease() {
25802
+ return getRow(this.leaseStmt);
25803
+ }
25804
+ };
25805
+
25806
+ // ../../packages/persistence/src/migrations.ts
25807
+ function describeObject(object2) {
25808
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25809
+ }
25810
+ function splitStatements(sql) {
25811
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25812
+ }
25813
+ function createdIndexName(statement) {
25814
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25815
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25816
+ }
25817
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25818
+ function applyMigrations(db, file2, options = {}) {
25819
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25820
+ db.exec(
25821
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25822
+ );
25823
+ const applied = new Set(
25824
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25825
+ );
25826
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25827
+ const record2 = db.prepare(
25828
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25829
+ );
25830
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25831
+ if (applied.has(migration.tag)) continue;
25832
+ if (options.skipTags?.has(migration.tag) === true) continue;
25833
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25834
+ const evidence = evidenceObjects(migration.sql);
25835
+ const present2 = evidence.filter((o) => evidenceExists(db, o));
25836
+ if (present2.length > 0 && present2.length < evidence.length) {
25837
+ const missing = evidence.filter((o) => !present2.includes(o));
25838
+ const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present2.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 ? present2.length === evidence.length : preLedgerStore && index < legacyCount;
25843
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25844
+ const statements = splitStatements(migration.sql);
25845
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25846
+ try {
25847
+ withTransaction(
25848
+ db,
25849
+ () => {
25850
+ for (const statement of statements) {
25851
+ const indexName = createdIndexName(statement);
25852
+ if (indexName === void 0) {
25853
+ if (alreadyApplied) continue;
25854
+ } else if (indexExists(db, indexName)) {
25855
+ continue;
25856
+ }
25857
+ db.exec(statement);
25858
+ }
25859
+ if (wantsFkOff && !alreadyApplied) {
25860
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25861
+ if (violations.length > 0) {
25862
+ throw new Error(
25863
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25864
+ );
25865
+ }
25866
+ }
25867
+ record2.run(migration.tag, Date.now());
25868
+ },
25869
+ "IMMEDIATE"
25870
+ );
25871
+ } finally {
25872
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25873
+ }
25874
+ }
25875
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25876
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25877
+ }
25878
+ ensureSyncedAtColumn(db, "audit_events");
25879
+ ensureScanLedgerTable(db);
25880
+ ensureHistorySyncTable(db);
25881
+ ensureBlockedDetectionsTable(db);
25882
+ ensureRuleProbeCacheTable(db);
25883
+ ensureWriteGateTrigger(db);
25884
+ ensureTokenUsageColumns(db);
25885
+ reconcileSourceProjectIds(db);
25886
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25887
+ const drained = runLegacyHistoryBackfill(db);
25888
+ if (drained) applyLegacyDropMigration(db, file2);
25889
+ }
25890
+ }
25891
+ function readLegacyTables(db) {
25892
+ let holdsRows = false;
25893
+ const marks = [];
25894
+ for (const table2 of ["events", "findings"]) {
25895
+ try {
25896
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
25897
+ if (row === void 0) {
25898
+ holdsRows = true;
25899
+ marks.push(`${table2}:unreadable`);
25900
+ continue;
25901
+ }
25902
+ if (row.n > 0) holdsRows = true;
25903
+ marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
25904
+ } catch {
25905
+ holdsRows = true;
25906
+ marks.push(`${table2}:unreadable`);
25907
+ }
25908
+ }
25909
+ return { holdsRows, mark: marks.join("|") };
25910
+ }
25911
+ function applyLegacyDropMigration(db, file2) {
25912
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25913
+ if (!migration) return;
25914
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25915
+ if (file2 !== void 0 && before?.holdsRows === true) {
25916
+ try {
25917
+ backupBeforeLegacyDrop(db, file2);
25918
+ } catch (error61) {
25919
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25920
+ return;
25921
+ }
25922
+ }
25923
+ try {
25924
+ withTransaction(
25925
+ db,
25926
+ () => {
25927
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25928
+ if (alreadyDropped) return;
25929
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25930
+ akaWarn(
25931
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25932
+ );
25933
+ return;
25934
+ }
25935
+ for (const statement of splitStatements(migration.sql)) {
25936
+ db.exec(statement);
25937
+ }
25938
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25939
+ migration.tag,
25940
+ Date.now()
25941
+ );
25942
+ },
25943
+ "IMMEDIATE"
25944
+ );
25945
+ } catch (error61) {
25946
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25947
+ }
25948
+ }
25949
+ function backupBeforeLegacyDrop(db, file2) {
25950
+ reapStalePartials(file2);
25951
+ const backup = backupPath(file2, "pre-drop");
25952
+ snapshotStore(db, backup);
25953
+ return backup;
25954
+ }
25955
+ var TOKEN_USAGE_COLUMNS = [
25956
+ {
25957
+ name: "input_tokens",
25958
+ ddl: "ALTER TABLE audit_events ADD COLUMN input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.input_tokens')) VIRTUAL"
25959
+ },
25960
+ {
25961
+ name: "output_tokens",
25962
+ ddl: "ALTER TABLE audit_events ADD COLUMN output_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.output_tokens')) VIRTUAL"
25963
+ },
25964
+ {
25965
+ name: "cache_creation_input_tokens",
25966
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_creation_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_creation_input_tokens')) VIRTUAL"
25967
+ },
25968
+ {
25969
+ name: "cache_read_input_tokens",
25970
+ ddl: "ALTER TABLE audit_events ADD COLUMN cache_read_input_tokens integer GENERATED ALWAYS AS (json_extract(attributes, '$.cache_read_input_tokens')) VIRTUAL"
25971
+ },
25972
+ {
25973
+ name: "model",
25974
+ ddl: "ALTER TABLE audit_events ADD COLUMN model text GENERATED ALWAYS AS (json_extract(attributes, '$.model')) VIRTUAL"
25975
+ },
25976
+ {
25003
25977
  name: "provider",
25004
25978
  ddl: "ALTER TABLE audit_events ADD COLUMN provider text GENERATED ALWAYS AS (json_extract(attributes, '$.provider')) VIRTUAL"
25005
25979
  }
@@ -25262,10 +26236,62 @@ function ensureSyncedAtColumn(db, table2) {
25262
26236
  if (!columns.includes("outbox_owed")) {
25263
26237
  db.exec(`ALTER TABLE ${table2} ADD COLUMN outbox_owed integer`);
25264
26238
  }
26239
+ if (!columns.includes("sync_failed_at")) {
26240
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failed_at integer`);
26241
+ }
26242
+ if (!columns.includes("sync_failure")) {
26243
+ withTransaction(
26244
+ db,
26245
+ () => {
26246
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failure text`);
26247
+ db.exec(
26248
+ `UPDATE ${table2} SET synced_at = NULL
26249
+ WHERE synced_at = -1
26250
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26251
+ );
26252
+ },
26253
+ "IMMEDIATE"
26254
+ );
26255
+ }
25265
26256
  db.exec(
25266
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25267
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26257
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26258
+ BEFORE UPDATE OF sync_failure ON ${table2}
26259
+ WHEN ${syncFailureRejectCondition()}
26260
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25268
26261
  );
26262
+ const syncIndexColumns = [
26263
+ "event_type",
26264
+ "synced_at",
26265
+ "sync_claimed_at",
26266
+ "started_at",
26267
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26268
+ // has to be in the index for the read to stay covered — but putting it
26269
+ // ahead of `started_at` would reorder the prefix the structural drain's
26270
+ // reads match on.
26271
+ "sync_failure"
26272
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26273
+ //
26274
+ // The delivery-state read tests it — a capture's state depends on whether a
26275
+ // live forward marked it owed — so carrying it here makes that read covering
26276
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26277
+ // But a sixth column changes what the planner charges for this index, and
26278
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26279
+ // then stops choosing the per-session index for the token rollup and walks
26280
+ // every `llm_call` in the store through the event-type index instead. That
26281
+ // read grows with the store; this one does not.
26282
+ //
26283
+ // 40 ms on the largest store measured, once per render, is a cost worth
26284
+ // paying to leave every other read's plan where it was.
26285
+ ];
26286
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26287
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26288
+ if (!syncIndexMatches) {
26289
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26290
+ db.exec(
26291
+ `CREATE INDEX idx_audit_events_sync
26292
+ ON audit_events (${syncIndexColumns.join(", ")})`
26293
+ );
26294
+ }
25269
26295
  db.exec(
25270
26296
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25271
26297
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25487,7 +26513,11 @@ function buildAuditEvent(row) {
25487
26513
  link: linkParsed?.success ? linkParsed.data : null,
25488
26514
  targetId: row.target_id,
25489
26515
  internal: intToBool(row.internal),
25490
- flagged: intToBool(row.flagged)
26516
+ flagged: intToBool(row.flagged),
26517
+ // Only meaningful when the title came out empty — a row whose body was
26518
+ // expired but whose title fell back to `tool_name` still has something to
26519
+ // render, and flagging it would make the view apologise for nothing.
26520
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25491
26521
  };
25492
26522
  }
25493
26523
  var TIMELINE_COLUMNS = `
@@ -25495,6 +26525,7 @@ var TIMELINE_COLUMNS = `
25495
26525
  event_type,
25496
26526
  started_at,
25497
26527
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26528
+ content_expired_at,
25498
26529
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25499
26530
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25500
26531
  json_extract(attributes, '$.severity') AS severity,
@@ -26160,6 +27191,88 @@ var SqliteAuditEventsRepository = class {
26160
27191
  }
26161
27192
  };
26162
27193
 
27194
+ // ../../packages/persistence/src/repositories/body-retention.ts
27195
+ var DEFAULT_BATCH_SIZE = 500;
27196
+ var DEFAULT_MAX_ROWS = 5e4;
27197
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27198
+ var SqliteBodyRetentionRepository = class {
27199
+ constructor(db) {
27200
+ this.db = db;
27201
+ const select = (laneClause) => `
27202
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27203
+ FROM audit_events
27204
+ WHERE content IS NOT NULL
27205
+ AND started_at < :cutoff
27206
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27207
+ ${laneClause}
27208
+ ORDER BY started_at
27209
+ LIMIT :limit`;
27210
+ this.candidatesStmt = this.db.prepare(select(""));
27211
+ this.candidatesSyncSafeStmt = this.db.prepare(
27212
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27213
+ );
27214
+ this.heldBySyncStmt = this.db.prepare(`
27215
+ SELECT COUNT(*) AS n
27216
+ FROM audit_events
27217
+ WHERE content IS NOT NULL
27218
+ AND started_at < :cutoff
27219
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27220
+ AND synced_at IS NULL`);
27221
+ this.expireStmt = this.db.prepare(
27222
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27223
+ );
27224
+ }
27225
+ db;
27226
+ candidatesStmt;
27227
+ candidatesSyncSafeStmt;
27228
+ heldBySyncStmt;
27229
+ expireStmt;
27230
+ /** How many bytes a pass with these options would free, changing nothing. */
27231
+ preview(opts) {
27232
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27233
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27234
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27235
+ return {
27236
+ rowsExpired: rows.length,
27237
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27238
+ rowsHeldBySync: this.countHeldBySync(opts)
27239
+ };
27240
+ }
27241
+ /** Clear eligible bodies, in bounded batches. */
27242
+ expire(opts) {
27243
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27244
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27245
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27246
+ let rowsExpired = 0;
27247
+ let bytesFreed = 0;
27248
+ let done = true;
27249
+ while (rowsExpired < maxRows) {
27250
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27251
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27252
+ if (batch.length === 0) break;
27253
+ withTransaction(
27254
+ this.db,
27255
+ () => {
27256
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27257
+ },
27258
+ "IMMEDIATE"
27259
+ );
27260
+ rowsExpired += batch.length;
27261
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27262
+ if (batch.length < remaining) break;
27263
+ if (rowsExpired >= maxRows) {
27264
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27265
+ }
27266
+ }
27267
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27268
+ }
27269
+ countHeldBySync(opts) {
27270
+ if (opts.sweepSyncLane) return 0;
27271
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27272
+ return row.n;
27273
+ }
27274
+ };
27275
+
26163
27276
  // ../../packages/persistence/src/repositories/classified-data.ts
26164
27277
  var SqliteClassifiedDataRepository = class {
26165
27278
  constructor(db) {
@@ -26988,7 +28101,15 @@ function toFlatFindingRow(r) {
26988
28101
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26989
28102
  eventId: r.event_id,
26990
28103
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26991
- status: deriveInstanceStatus(r)
28104
+ status: deriveInstanceStatus(r),
28105
+ delivery: deriveFindingDelivery({
28106
+ kind: r.kind,
28107
+ syncedAt: r.synced_at,
28108
+ syncClaimedAt: r.sync_claimed_at,
28109
+ syncFailedAt: r.sync_failed_at,
28110
+ syncFailure: r.sync_failure,
28111
+ outboxOwed: r.outbox_owed
28112
+ })
26992
28113
  };
26993
28114
  }
26994
28115
  function encodeGroupCursor(group) {
@@ -27052,7 +28173,10 @@ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS c
27052
28173
  e.tool_name AS tool_name,
27053
28174
  f.audit_event_id AS event_id, e.root_session_id AS session_id,
27054
28175
  e.event_type AS kind, f.finding_key AS finding_key,
27055
- ${latestResolutionStatusSql("f")} AS latest_status`;
28176
+ ${latestResolutionStatusSql("f")} AS latest_status,
28177
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28178
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28179
+ e.outbox_owed AS outbox_owed`;
27056
28180
  var DAY_MS3 = 864e5;
27057
28181
  var SqliteFindingsRepository = class {
27058
28182
  constructor(db) {
@@ -27297,6 +28421,7 @@ var SqliteFindingsRepository = class {
27297
28421
  providers: query.provider,
27298
28422
  actions: query.action,
27299
28423
  statuses: query.status,
28424
+ deliveries: query.deployment,
27300
28425
  tools: query.tool,
27301
28426
  repo: query.repo,
27302
28427
  file: query.file,
@@ -27364,6 +28489,7 @@ var SqliteFindingsRepository = class {
27364
28489
  providers: query.provider,
27365
28490
  actions: query.action,
27366
28491
  statuses: query.status,
28492
+ deliveries: query.deployment,
27367
28493
  tools: query.tool,
27368
28494
  q: query.q
27369
28495
  };
@@ -27627,7 +28753,9 @@ var SqliteFindingsRepository = class {
27627
28753
  )
27628
28754
  );
27629
28755
  for (const row of grouped) {
27630
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28756
+ if (Object.hasOwn(byAction, row.action_taken)) {
28757
+ byAction[row.action_taken] = row.c;
28758
+ }
27631
28759
  }
27632
28760
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27633
28761
  const sevRows = allRows(
@@ -27644,7 +28772,9 @@ var SqliteFindingsRepository = class {
27644
28772
  )
27645
28773
  );
27646
28774
  for (const row of sevRows) {
27647
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28775
+ if (Object.hasOwn(bySeverity, row.severity)) {
28776
+ bySeverity[row.severity] = row.c;
28777
+ }
27648
28778
  }
27649
28779
  const categories = ENFORCEABLE_CATEGORIES;
27650
28780
  const enabledRows = allRows(
@@ -27693,525 +28823,6 @@ function isoDay(ms) {
27693
28823
  return new Date(ms).toISOString().slice(0, 10);
27694
28824
  }
27695
28825
 
27696
- // ../../packages/persistence/src/repositories/history-sync.ts
27697
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27698
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27699
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27700
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27701
- var SKIPPED = -1;
27702
- var ROW_COLUMNS = `id,
27703
- parent_id AS parentId,
27704
- root_session_id AS rootSessionId,
27705
- event_type AS eventType,
27706
- host_id AS hostId,
27707
- harness_id AS harnessId,
27708
- source_project_id AS sourceProjectId,
27709
- started_at AS startedAt,
27710
- ended_at AS endedAt,
27711
- severity,
27712
- priority,
27713
- content,
27714
- content_hash AS contentHash,
27715
- attributes`;
27716
- var SqliteHistorySyncRepository = class {
27717
- constructor(db) {
27718
- this.db = db;
27719
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27720
- this.sessionsStmt = db.prepare(
27721
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27722
- FROM audit_events
27723
- WHERE synced_at IS NULL
27724
- AND event_type IN (${TYPE_LIST})
27725
- AND started_at < :before
27726
- GROUP BY sessionId
27727
- ORDER BY earliest
27728
- LIMIT :limit`
27729
- );
27730
- this.rowsStmt = db.prepare(
27731
- `SELECT ${ROW_COLUMNS}
27732
- FROM audit_events
27733
- WHERE synced_at IS NULL
27734
- AND event_type IN (${TYPE_LIST})
27735
- AND started_at < :before
27736
- AND COALESCE(root_session_id, id) = :sessionId
27737
- ORDER BY (event_type = 'session') DESC, started_at
27738
- LIMIT :limit`
27739
- );
27740
- this.captureRowsStmt = db.prepare(
27741
- `SELECT ${ROW_COLUMNS}
27742
- FROM audit_events
27743
- WHERE synced_at IS NULL
27744
- AND sync_claimed_at IS NULL
27745
- AND outbox_owed = 1
27746
- AND event_type IN (${CAPTURE_TYPE_LIST})
27747
- AND started_at < :before
27748
- ORDER BY started_at
27749
- LIMIT :limit`
27750
- );
27751
- this.markOwedStmt = db.prepare(
27752
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27753
- );
27754
- this.markCaptureBacklogOwedStmt = db.prepare(
27755
- `UPDATE audit_events SET outbox_owed = 1
27756
- WHERE synced_at IS NULL
27757
- AND event_type IN (${CAPTURE_TYPE_LIST})
27758
- AND started_at < :before`
27759
- );
27760
- this.stampStmt = db.prepare(
27761
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27762
- );
27763
- this.claimRowStmt = db.prepare(
27764
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27765
- );
27766
- this.releaseRowStmt = db.prepare(
27767
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27768
- );
27769
- this.releaseStaleClaimsStmt = db.prepare(
27770
- `UPDATE audit_events SET sync_claimed_at = NULL
27771
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27772
- );
27773
- this.partitionStmt = db.prepare(
27774
- `SELECT
27775
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27776
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27777
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27778
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27779
- COUNT(*) AS total
27780
- FROM audit_events
27781
- WHERE event_type IN (${TYPE_LIST})`
27782
- );
27783
- this.countsStmt = db.prepare(
27784
- `SELECT
27785
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27786
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27787
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27788
- FROM audit_events
27789
- WHERE event_type IN (${TYPE_LIST})`
27790
- );
27791
- this.captureSkipCountStmt = db.prepare(
27792
- `SELECT COUNT(*) AS skipped
27793
- FROM audit_events
27794
- WHERE synced_at = ${String(SKIPPED)}
27795
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27796
- );
27797
- this.fingerprintStmt = db.prepare(
27798
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27799
- FROM history_sync WHERE id = 1`
27800
- );
27801
- this.setFingerprintStmt = db.prepare(
27802
- `UPDATE history_sync
27803
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27804
- WHERE id = 1`
27805
- );
27806
- this.disownCapturesStmt = db.prepare(
27807
- `UPDATE audit_events SET outbox_owed = NULL
27808
- WHERE outbox_owed IS NOT NULL
27809
- AND event_type IN (${CAPTURE_TYPE_LIST})
27810
- AND started_at < :attachedAt`
27811
- );
27812
- this.rearmStmt = db.prepare(
27813
- `UPDATE audit_events SET synced_at = NULL
27814
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27815
- );
27816
- this.claimStmt = db.prepare(
27817
- `UPDATE history_sync
27818
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27819
- WHERE id = 1
27820
- AND (owner_pid IS NULL
27821
- OR heartbeat_at IS NULL
27822
- OR heartbeat_at < :staleBefore
27823
- OR heartbeat_at > :now)`
27824
- );
27825
- this.heartbeatStmt = db.prepare(
27826
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27827
- );
27828
- this.releaseStmt = db.prepare(
27829
- `UPDATE history_sync
27830
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27831
- WHERE id = 1 AND owner_pid = :pid`
27832
- );
27833
- this.closeWindowStmt = db.prepare(
27834
- `UPDATE audit_events SET synced_at = :at
27835
- WHERE synced_at IS NULL
27836
- AND event_type IN (${TYPE_LIST})
27837
- AND started_at >= :attachedAt`
27838
- );
27839
- this.releaseBoundaryStmt = db.prepare(
27840
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27841
- );
27842
- this.freezeBoundaryStmt = db.prepare(
27843
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27844
- );
27845
- this.leaseStmt = db.prepare(
27846
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27847
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27848
- FROM history_sync WHERE id = 1`
27849
- );
27850
- this.inspectionsStmt = db.prepare(
27851
- `SELECT d.rule_id AS ruleId,
27852
- d.name AS ruleName,
27853
- d.version AS ruleVersion,
27854
- d.category AS category,
27855
- d.severity AS severity,
27856
- f.span_start AS spanStart,
27857
- f.span_end AS spanEnd,
27858
- f.masked_match AS maskedMatch,
27859
- f.action_taken AS actionTaken,
27860
- f.confidence AS confidence
27861
- FROM inspection_findings f
27862
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27863
- WHERE f.audit_event_id = :auditEventId
27864
- ORDER BY f.span_start, f.id`
27865
- );
27866
- }
27867
- db;
27868
- ensureRowStmt;
27869
- sessionsStmt;
27870
- rowsStmt;
27871
- stampStmt;
27872
- countsStmt;
27873
- fingerprintStmt;
27874
- setFingerprintStmt;
27875
- rearmStmt;
27876
- claimStmt;
27877
- heartbeatStmt;
27878
- releaseStmt;
27879
- leaseStmt;
27880
- inspectionsStmt;
27881
- closeWindowStmt;
27882
- releaseBoundaryStmt;
27883
- freezeBoundaryStmt;
27884
- captureRowsStmt;
27885
- markOwedStmt;
27886
- markCaptureBacklogOwedStmt;
27887
- captureSkipCountStmt;
27888
- disownCapturesStmt;
27889
- partitionStmt;
27890
- claimRowStmt;
27891
- releaseRowStmt;
27892
- releaseStaleClaimsStmt;
27893
- /**
27894
- * The masked detections recorded against one tool call.
27895
- *
27896
- * These travel with the event because a tool call's target is not
27897
- * re-inspectable from the event alone — unlike a capture, where the text
27898
- * itself is re-scannable. What crosses is the masked match and the rule that
27899
- * produced it, never the value.
27900
- */
27901
- inspectionsFor(auditEventId) {
27902
- return allRows(this.inspectionsStmt, { auditEventId });
27903
- }
27904
- /**
27905
- * Sessions with structural rows still to send, oldest first.
27906
- *
27907
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27908
- * read. Anything recorded after the machine attached is the live forward
27909
- * path's to deliver; this drain exists for what was recorded before it, and a
27910
- * row both paths send is at best a duplicate request and at worst — for a
27911
- * session root — an overwrite of the inventory ids the live path resolved.
27912
- */
27913
- pendingSessions(limit, before) {
27914
- return allRows(this.sessionsStmt, { limit, before }).map(
27915
- (r) => r.sessionId
27916
- );
27917
- }
27918
- /** One session's undelivered structural rows within the backlog, root first. */
27919
- pendingRows(sessionId, limit, before) {
27920
- return allRows(this.rowsStmt, { sessionId, limit, before });
27921
- }
27922
- /**
27923
- * Captures this machine still owes the deployment, oldest first.
27924
- *
27925
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27926
- * by a time window — see captureRowsStmt for why a window could not express
27927
- * this. `before` is the grace window that leaves a just-recorded capture to
27928
- * the live path.
27929
- */
27930
- pendingCaptureRows(limit, before) {
27931
- return allRows(this.captureRowsStmt, { limit, before });
27932
- }
27933
- /**
27934
- * Record that a capture is OWED to the deployment.
27935
- *
27936
- * Written by the attached forward path when a live send did not confirm
27937
- * delivery, and read by the drain as the whole of its eligibility test. It is
27938
- * a fact rather than an inference: the machine was attached, the send did not
27939
- * land, so the row is owed — which no time window can state, because the same
27940
- * window that holds the rows a past attachment left owed also holds every
27941
- * capture recorded while the machine was DETACHED, and those were never
27942
- * offered to anyone.
27943
- *
27944
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27945
- * out of the drain's read.
27946
- */
27947
- markCaptureOwed(id) {
27948
- this.markOwedStmt.run({ id });
27949
- }
27950
- /**
27951
- * Mark every capture already on disk as owed, as of `before`.
27952
- *
27953
- * The consent-time backfill, called once from `aka attach` when a human
27954
- * grants existing-history consent — never from an ongoing drain pass, and
27955
- * never inferred from a boundary that could later move. `before` is the
27956
- * caller's own "now" at the moment consent was granted, so what this marks
27957
- * is exactly the backlog the consent prompt already counted, not whatever a
27958
- * later re-attach or key rotation might widen it to.
27959
- *
27960
- * Returns how many rows matched, for the caller to log or test against. Not a
27961
- * count of NEWLY marked rows — a row still unsynced from an earlier call
27962
- * matches again and is counted again, the same as `UPDATE`'s own `changes`.
27963
- */
27964
- markCaptureBacklogOwed(before) {
27965
- return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
27966
- }
27967
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27968
- markSynced(ids, atMs) {
27969
- this.stampAll(ids, atMs);
27970
- }
27971
- /**
27972
- * Record that a row will never be sent.
27973
- *
27974
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27975
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27976
- * is retried; marking those would turn one outage into permanent data loss.
27977
- */
27978
- markSkipped(ids) {
27979
- this.stampAll(ids, SKIPPED);
27980
- }
27981
- eachInTransaction(ids, run) {
27982
- if (ids.length === 0) return;
27983
- withTransaction(
27984
- this.db,
27985
- () => {
27986
- for (const id of ids) run(id);
27987
- },
27988
- "IMMEDIATE"
27989
- );
27990
- }
27991
- stampAll(ids, value) {
27992
- if (ids.length === 0) return;
27993
- withTransaction(
27994
- this.db,
27995
- () => {
27996
- for (const id of ids) this.stampStmt.run({ at: value, id });
27997
- },
27998
- "IMMEDIATE"
27999
- );
28000
- }
28001
- /**
28002
- * Claim rows as in-flight.
28003
- *
28004
- * Advisory in exactly the sense the lease is: it records that a send is in
28005
- * progress so a surface can say so, and a lost claim costs a row showing as
28006
- * queued while it is actually being sent. It is not exclusion — the far side
28007
- * settles a duplicate on the row id.
28008
- */
28009
- claimRows(ids, atMs) {
28010
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
28011
- }
28012
- /** Give back a claim without settling — the send failed, the row is queued again. */
28013
- releaseRows(ids) {
28014
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
28015
- }
28016
- /**
28017
- * Clear claims older than `staleBefore`, and report how many were cleared.
28018
- *
28019
- * A process killed between claiming and settling leaves rows claimed with
28020
- * nothing left to settle them. Without this they read as "sending" for ever.
28021
- */
28022
- releaseStaleClaims(staleBefore) {
28023
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
28024
- }
28025
- /**
28026
- * Every tracked row in exactly one delivery state.
28027
- *
28028
- * Takes no boundary on purpose. The boundary answers "what should the drain
28029
- * pick up now", which is a different question from "what state is this row
28030
- * in" — and a machine that has never attached has no boundary to pass, so
28031
- * requiring one would force a caller to invent one and report the whole store
28032
- * as queued.
28033
- */
28034
- partition() {
28035
- const row = getRow(this.partitionStmt, {});
28036
- return {
28037
- queued: row?.queued ?? 0,
28038
- inProgress: row?.inProgress ?? 0,
28039
- synced: row?.synced ?? 0,
28040
- failed: row?.failed ?? 0,
28041
- total: row?.total ?? 0
28042
- };
28043
- }
28044
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
28045
- counts(before) {
28046
- const row = getRow(
28047
- this.countsStmt,
28048
- { before }
28049
- );
28050
- const captures = getRow(this.captureSkipCountStmt);
28051
- return {
28052
- pending: row?.pending ?? 0,
28053
- sent: row?.sent ?? 0,
28054
- skipped: row?.skipped ?? 0,
28055
- capturesSkipped: captures?.skipped ?? 0
28056
- };
28057
- }
28058
- /**
28059
- * The deployment the current stamps were made against, and where its backlog
28060
- * ends.
28061
- *
28062
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
28063
- * machine that has never drained is — and every writer below seeds the row
28064
- * before it needs one, so nothing depends on this creating it. Keeping the
28065
- * write off the gate path matters because the gate runs on every pass while a
28066
- * write has to take the database's write lock.
28067
- */
28068
- deployment() {
28069
- const row = getRow(
28070
- this.fingerprintStmt
28071
- );
28072
- return {
28073
- fingerprint: row?.fingerprint ?? void 0,
28074
- backlogBefore: row?.backlogBefore ?? void 0
28075
- };
28076
- }
28077
- /**
28078
- * Point the ledger at a different deployment, discarding what it recorded
28079
- * about the previous one.
28080
- *
28081
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
28082
- * machine has just left are undelivered as far as the new one is concerned.
28083
- * All four in one transaction, so a crash between them cannot leave stamps
28084
- * attributed to the wrong deployment, a boundary that belongs to another, or
28085
- * a disown with no re-mark to follow it.
28086
- *
28087
- * The boundary is written HERE and only here, which is what freezes it: a
28088
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28089
- * unchanged, so this never runs and the backlog does not widen back over rows
28090
- * the live path has since delivered.
28091
- *
28092
- * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
28093
- * granted existing-history consent for the deployment this call is arming —
28094
- * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
28095
- * instant, `backlogBefore` is the ATTACH instant, and the two can be far
28096
- * apart. Passed only when that grant is valid, since this method has no way
28097
- * to check consent itself and must not mark a row owed for a machine that
28098
- * never agreed to it. Applied AFTER the disown above, in the SAME
28099
- * transaction: what the disown clears is every marker below `backlogBefore`,
28100
- * which includes this deployment's OWN pre-attach rows — `aka attach` calls
28101
- * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
28102
- * on the cleared side of that bound — and the re-mark in the same
28103
- * transaction is what puts those rows back. A crash between the two cannot
28104
- * strand the ledger disowned with nothing re-marked — the transaction either
28105
- * lands whole or not at all, and a fingerprint mismatch that has not yet
28106
- * committed re-enters this method on the very next pass. Omit it (the
28107
- * structural-only tests do) to exercise the disown in isolation.
28108
- *
28109
- * The disown is bounded by `backlogBefore`, which is what keeps it from
28110
- * touching a marker the NEW deployment's OWN live path has already set: B's
28111
- * live path can mark a capture owed from the moment `aka attach` writes the
28112
- * descriptor, before the drain's first pass ever reaches this method, and
28113
- * such a row sits at or after the bound rather than below it. What keeps the
28114
- * disown from eating THIS SAME CALL's own re-mark is the order, not the
28115
- * bound — disown runs first, re-mark second, both inside the one
28116
- * transaction above.
28117
- */
28118
- rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
28119
- this.ensureRowStmt.run();
28120
- withTransaction(
28121
- this.db,
28122
- () => {
28123
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28124
- this.rearmStmt.run();
28125
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28126
- this.disownCapturesStmt.run({ attachedAt: backlogBefore });
28127
- }
28128
- if (backfillCapturesBefore !== void 0) {
28129
- this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
28130
- }
28131
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28132
- },
28133
- "IMMEDIATE"
28134
- );
28135
- }
28136
- /**
28137
- * End the attached period: hand its rows to the live path, and release the
28138
- * boundary so the next attachment can freeze a new one.
28139
- *
28140
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28141
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28142
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28143
- * during the detached period, because the machine is not attached. Rows
28144
- * recorded in that window sit after the boundary and before the re-attach, so
28145
- * neither path takes them, and the pending count reports none outstanding.
28146
- *
28147
- * Stamping the attached window is not a claim that every one of those rows
28148
- * reached the deployment — the live path drops on failure and says so
28149
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28150
- * status quo: they sit outside the frozen boundary today and are equally never
28151
- * re-sent. Making it explicit is what lets the boundary move.
28152
- *
28153
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28154
- * window unstamped — that half-state would re-send the whole attached period
28155
- * on the next attach, which is the failure the boundary exists to prevent.
28156
- */
28157
- closeAttachedWindow(attachedAtMs, atMs) {
28158
- this.ensureRowStmt.run();
28159
- withTransaction(
28160
- this.db,
28161
- () => {
28162
- const row = getRow(this.fingerprintStmt);
28163
- const from = row?.backlogBefore ?? attachedAtMs;
28164
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28165
- this.releaseBoundaryStmt.run();
28166
- },
28167
- "IMMEDIATE"
28168
- );
28169
- }
28170
- /**
28171
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28172
- *
28173
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28174
- * different deployment and therefore discards what was delivered to the old
28175
- * one: here the recipient is the same, so everything already sent to it stays
28176
- * sent.
28177
- */
28178
- freezeBoundary(backlogBefore) {
28179
- this.ensureRowStmt.run();
28180
- this.freezeBoundaryStmt.run({ backlogBefore });
28181
- }
28182
- /** Take the claim, or report that someone live already holds it. */
28183
- claim(pid, host, nowMs, staleAfterMs) {
28184
- this.ensureRowStmt.run();
28185
- let taken = false;
28186
- withTransaction(
28187
- this.db,
28188
- () => {
28189
- const result = this.claimStmt.run({
28190
- pid,
28191
- host,
28192
- now: nowMs,
28193
- staleBefore: nowMs - staleAfterMs
28194
- });
28195
- taken = result.changes === 1;
28196
- },
28197
- "IMMEDIATE"
28198
- );
28199
- return taken;
28200
- }
28201
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28202
- heartbeat(pid, nowMs) {
28203
- this.heartbeatStmt.run({ now: nowMs, pid });
28204
- }
28205
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28206
- release(pid) {
28207
- this.releaseStmt.run({ pid });
28208
- }
28209
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28210
- lease() {
28211
- return getRow(this.leaseStmt);
28212
- }
28213
- };
28214
-
28215
28826
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28216
28827
  var SqliteInspectionDefinitionsRepository = class {
28217
28828
  constructor(db) {
@@ -28439,6 +29050,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28439
29050
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28440
29051
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28441
29052
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29053
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28442
29054
  if (values.vaultConsent !== void 0) {
28443
29055
  merged.vaultConsent = values.vaultConsent ? (
28444
29056
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30946,7 +31558,7 @@ var SqliteSecurityRepository = class {
30946
31558
  ELSE 0
30947
31559
  END) AS open_at_rest
30948
31560
  FROM inspection_findings f
30949
- JOIN audit_events e ON e.id = f.audit_event_id
31561
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30950
31562
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30951
31563
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30952
31564
  ON latest.finding_key = f.finding_key
@@ -31172,7 +31784,7 @@ var SqliteSecurityRepository = class {
31172
31784
  this.db.prepare(
31173
31785
  `SELECT e.repo AS repo, count(*) AS c
31174
31786
  FROM inspection_findings f
31175
- JOIN audit_events e ON e.id = f.audit_event_id
31787
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31176
31788
  WHERE e.started_at >= :from AND e.started_at < :to
31177
31789
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31178
31790
  AND e.repo IS NOT NULL
@@ -31296,7 +31908,7 @@ var SqliteSecurityRepository = class {
31296
31908
  d.severity AS severity,
31297
31909
  COUNT(*) AS count
31298
31910
  FROM inspection_findings f
31299
- JOIN audit_events e ON e.id = f.audit_event_id
31911
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31300
31912
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31301
31913
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31302
31914
  ON latest.finding_key = f.finding_key
@@ -31331,7 +31943,7 @@ var SqliteSecurityRepository = class {
31331
31943
  `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31332
31944
  d.rule_id AS rule_id, d.category AS category
31333
31945
  FROM inspection_findings f
31334
- JOIN audit_events e ON e.id = f.audit_event_id
31946
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31335
31947
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31336
31948
  WHERE e.started_at >= :from AND e.started_at < :to
31337
31949
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -32172,6 +32784,7 @@ function openWithPragmas(file2) {
32172
32784
  db.exec("PRAGMA journal_mode = WAL");
32173
32785
  db.exec("PRAGMA busy_timeout = 2000");
32174
32786
  db.exec("PRAGMA foreign_keys = ON");
32787
+ registerSqlFunctions(db);
32175
32788
  } catch (err) {
32176
32789
  closeQuietly(db);
32177
32790
  throw err;
@@ -32201,7 +32814,7 @@ function backupLegacyStore(db, file2) {
32201
32814
  discardStore(file2, backup);
32202
32815
  return backup;
32203
32816
  }
32204
- function openAndInitialize(file2, base) {
32817
+ function openAndInitialize(file2, base, skipTags) {
32205
32818
  let db = openWithPragmas(file2);
32206
32819
  try {
32207
32820
  if (isForeignSqliteLineage(db)) {
@@ -32211,7 +32824,7 @@ function openAndInitialize(file2, base) {
32211
32824
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32212
32825
  );
32213
32826
  }
32214
- applyMigrations(db, file2);
32827
+ applyMigrations(db, file2, { skipTags });
32215
32828
  tightenPerms(file2);
32216
32829
  const policies = new SqlitePoliciesRepository(db);
32217
32830
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32226,6 +32839,7 @@ function openAndInitialize(file2, base) {
32226
32839
  exceptions: new SqliteExceptionsRepository(db),
32227
32840
  resolutions: new SqliteResolutionsRepository(db),
32228
32841
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32842
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32229
32843
  security: new SqliteSecurityRepository(db),
32230
32844
  detections: new SqliteDetectionsRepository(db),
32231
32845
  shares: new SqliteSharesRepository(db),
@@ -32248,7 +32862,8 @@ function openAndInitialize(file2, base) {
32248
32862
  throw err;
32249
32863
  }
32250
32864
  }
32251
- function openLocalDatabase(dir) {
32865
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32866
+ function openLocalDatabase(dir, options = {}) {
32252
32867
  ensureDataDirSync(dir);
32253
32868
  const file2 = join7(dir, DB_FILENAME);
32254
32869
  reapStalePartials(file2);
@@ -32260,6 +32875,7 @@ function openLocalDatabase(dir) {
32260
32875
  installedPacks,
32261
32876
  scanLedger,
32262
32877
  historySync,
32878
+ bodyRetention,
32263
32879
  secretVault,
32264
32880
  exceptions,
32265
32881
  resolutions,
@@ -32283,7 +32899,8 @@ function openLocalDatabase(dir) {
32283
32899
  // `dir` is always `<base>/data` — every caller resolves it through
32284
32900
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32285
32901
  // settings/ and data/, and the pack-policy floor needs both halves.
32286
- dirname2(dir)
32902
+ dirname2(dir),
32903
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32287
32904
  );
32288
32905
  function captureRowId(event) {
32289
32906
  return captureId(
@@ -32476,6 +33093,7 @@ function openLocalDatabase(dir) {
32476
33093
  installedPacks,
32477
33094
  scanLedger,
32478
33095
  historySync,
33096
+ bodyRetention,
32479
33097
  secretVault,
32480
33098
  exceptions,
32481
33099
  resolutions,
@@ -32516,8 +33134,35 @@ function openLocalDatabase(dir) {
32516
33134
 
32517
33135
  // ../../packages/persistence/src/egress-wire.ts
32518
33136
  import { createHash as createHash3 } from "crypto";
33137
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33138
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33139
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33140
+ var FILE_URL = /^file:\/\//i;
33141
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33142
+ var SLASH = "/".charCodeAt(0);
33143
+ var GIT_SUFFIX = ".git";
33144
+ function trimSlashes(path) {
33145
+ let start = 0;
33146
+ let end = path.length;
33147
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33148
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33149
+ return path.slice(start, end);
33150
+ }
33151
+ function canonicalGitUrl(url2) {
33152
+ const trimmed = url2.trim();
33153
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33154
+ const scheme = SCHEME_FORM.exec(trimmed);
33155
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33156
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33157
+ if (host === void 0) return trimmed;
33158
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33159
+ const bare = trimSlashes(path);
33160
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33161
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33162
+ }
32519
33163
  function hashProjectKey(projectKey) {
32520
- return createHash3("sha256").update(projectKey, "utf8").digest("hex");
33164
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33165
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
32521
33166
  }
32522
33167
  function toIngestHit(hit) {
32523
33168
  return {
@@ -32684,18 +33329,50 @@ function fingerprintValue(key, raw) {
32684
33329
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32685
33330
  }
32686
33331
 
33332
+ // ../../packages/persistence/src/forward-health.ts
33333
+ import { readFileSync as readFileSync7 } from "fs";
33334
+ import { join as join9 } from "path";
33335
+ var FAILURES = /* @__PURE__ */ new Set([
33336
+ "unauthorized",
33337
+ "forbidden",
33338
+ "unreachable"
33339
+ ]);
33340
+ var BREAKER_COOLDOWN_MS = 3e4;
33341
+ function parseForwardHealth(raw, nowMs) {
33342
+ try {
33343
+ const parsed2 = JSON.parse(raw);
33344
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33345
+ const record2 = parsed2;
33346
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33347
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33348
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33349
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33350
+ } catch {
33351
+ return null;
33352
+ }
33353
+ }
33354
+ function isForwardPaused(health, nowMs) {
33355
+ const openedAtMs = health?.openedAtMs ?? null;
33356
+ if (openedAtMs === null) return false;
33357
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33358
+ }
33359
+
32687
33360
  // ../../packages/persistence/src/history-backfill.ts
32688
33361
  import { existsSync as existsSync4 } from "fs";
32689
- import { join as join9 } from "path";
33362
+ import { join as join10 } from "path";
32690
33363
 
32691
33364
  // ../../packages/persistence/src/history-preview.ts
32692
33365
  import { existsSync as existsSync5 } from "fs";
32693
- import { join as join10 } from "path";
33366
+ import { join as join11 } from "path";
32694
33367
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32695
33368
 
33369
+ // ../../packages/persistence/src/history-sync-state.ts
33370
+ import { readFileSync as readFileSync8 } from "fs";
33371
+ import { join as join12 } from "path";
33372
+
32696
33373
  // ../../packages/persistence/src/store-symlinks.ts
32697
33374
  import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32698
- import { dirname as dirname3, join as join11, resolve } from "path";
33375
+ import { dirname as dirname3, join as join13, resolve } from "path";
32699
33376
 
32700
33377
  // ../../packages/persistence/src/vault/crypto.ts
32701
33378
  import {
@@ -32709,19 +33386,19 @@ import {
32709
33386
  // ../../packages/persistence/src/vault/key-provider.ts
32710
33387
  import { execFileSync } from "child_process";
32711
33388
  import { randomBytes as randomBytes2 } from "crypto";
32712
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32713
- import { join as join12 } from "path";
33389
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33390
+ import { join as join14 } from "path";
32714
33391
 
32715
33392
  // ../../packages/persistence/src/vault/vault.ts
32716
33393
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32717
33394
 
32718
33395
  // ../../packages/persistence/src/warn-era-cap.ts
32719
33396
  import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
32720
- import { join as join13 } from "path";
33397
+ import { join as join15 } from "path";
32721
33398
  var MARKER = "warn-era-capped";
32722
33399
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32723
33400
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32724
- const marker = join13(dataDir2, MARKER);
33401
+ const marker = join15(dataDir2, MARKER);
32725
33402
  if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32726
33403
  const capped = db.policies.capCategoryActions();
32727
33404
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
@@ -32731,7 +33408,7 @@ function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32731
33408
 
32732
33409
  // ../../packages/plugin-sdk/src/config.ts
32733
33410
  import { existsSync as existsSync8 } from "fs";
32734
- import { join as join14 } from "path";
33411
+ import { join as join16 } from "path";
32735
33412
 
32736
33413
  // ../../packages/plugin-sdk/src/provider-env.ts
32737
33414
  var DEFAULT_ANTHROPIC_HOST = "api.anthropic.com";
@@ -32785,7 +33462,7 @@ function resolveProvider() {
32785
33462
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32786
33463
  try {
32787
33464
  ensureLayoutDirSync(base);
32788
- const settingsFile = join14(settingsDir(base), "settings.json");
33465
+ const settingsFile = join16(settingsDir(base), "settings.json");
32789
33466
  if (existsSync8(settingsFile)) tightenFile(settingsFile);
32790
33467
  } catch {
32791
33468
  }
@@ -32809,9 +33486,9 @@ function resolveProviderSafe(resolveProviderFn) {
32809
33486
  }
32810
33487
 
32811
33488
  // ../../packages/plugin-sdk/src/config-inventory.ts
32812
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33489
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32813
33490
  import { homedir as homedir2 } from "os";
32814
- import { basename as basename3, join as join16 } from "path";
33491
+ import { basename as basename3, join as join18 } from "path";
32815
33492
 
32816
33493
  // ../../packages/detections/src/egress/registry.ts
32817
33494
  var EXTRACTOR_VERSION = "1";
@@ -35900,8 +36577,8 @@ function bundledDetections() {
35900
36577
  }
35901
36578
 
35902
36579
  // ../../packages/plugin-sdk/src/repo.ts
35903
- import { existsSync as existsSync9, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
35904
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
36580
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
36581
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
35905
36582
  function resolveRepo(cwd) {
35906
36583
  try {
35907
36584
  const root = findGitRoot(cwd);
@@ -35923,36 +36600,36 @@ function resolveWorktreeRoot(cwd) {
35923
36600
  function findGitRoot(start) {
35924
36601
  let dir = start;
35925
36602
  for (; ; ) {
35926
- if (existsSync9(join15(dir, ".git"))) return dir;
36603
+ if (existsSync9(join17(dir, ".git"))) return dir;
35927
36604
  const parent = dirname4(dir);
35928
36605
  if (parent === dir) return void 0;
35929
36606
  dir = parent;
35930
36607
  }
35931
36608
  }
35932
36609
  function resolveGitContext(root) {
35933
- const dotGit = join15(root, ".git");
36610
+ const dotGit = join17(root, ".git");
35934
36611
  try {
35935
36612
  if (statSync6(dotGit).isDirectory()) {
35936
- return { configPath: join15(dotGit, "config"), headRoot: root };
36613
+ return { configPath: join17(dotGit, "config"), headRoot: root };
35937
36614
  }
35938
36615
  } catch {
35939
36616
  return void 0;
35940
36617
  }
35941
36618
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
35942
36619
  if (!target) return void 0;
35943
- const gitdir = isAbsolute(target) ? target : join15(root, target);
35944
- if (existsSync9(join15(gitdir, "config"))) {
35945
- return { configPath: join15(gitdir, "config"), headRoot: root };
36620
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
36621
+ if (existsSync9(join17(gitdir, "config"))) {
36622
+ return { configPath: join17(gitdir, "config"), headRoot: root };
35946
36623
  }
35947
- const commonRaw = safeRead(join15(gitdir, "commondir"))?.trim();
36624
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
35948
36625
  if (!commonRaw) return void 0;
35949
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join15(gitdir, commonRaw);
36626
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
35950
36627
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
35951
- return { configPath: join15(commonGitDir, "config"), headRoot };
36628
+ return { configPath: join17(commonGitDir, "config"), headRoot };
35952
36629
  }
35953
36630
  function safeRead(path) {
35954
36631
  try {
35955
- return readFileSync8(path, "utf8");
36632
+ return readFileSync10(path, "utf8");
35956
36633
  } catch {
35957
36634
  return void 0;
35958
36635
  }
@@ -36496,8 +37173,8 @@ function createGuardedScanner(partition, gateway, opts) {
36496
37173
  }
36497
37174
 
36498
37175
  // ../../packages/plugin-sdk/src/host-floor.ts
36499
- import { readFileSync as readFileSync11 } from "fs";
36500
- import { join as join18 } from "path";
37176
+ import { readFileSync as readFileSync13 } from "fs";
37177
+ import { join as join20 } from "path";
36501
37178
 
36502
37179
  // ../../packages/plugin-sdk/src/model-governance.ts
36503
37180
  import {
@@ -36505,11 +37182,11 @@ import {
36505
37182
  fstatSync,
36506
37183
  mkdirSync as mkdirSync2,
36507
37184
  openSync as openSync2,
36508
- readFileSync as readFileSync10,
37185
+ readFileSync as readFileSync12,
36509
37186
  readSync,
36510
37187
  writeFileSync as writeFileSync5
36511
37188
  } from "fs";
36512
- import { join as join17 } from "path";
37189
+ import { join as join19 } from "path";
36513
37190
  var TAIL_BYTES = 256 * 1024;
36514
37191
 
36515
37192
  // ../../packages/plugin-sdk/src/host-floor.ts
@@ -36532,15 +37209,15 @@ var HOST_FLOORS = {
36532
37209
 
36533
37210
  // ../../packages/plugin-sdk/src/ignore-layers.ts
36534
37211
  var import_ignore = __toESM(require_ignore(), 1);
36535
- import { readFileSync as readFileSync12 } from "fs";
36536
- import { join as join19 } from "path";
37212
+ import { readFileSync as readFileSync14 } from "fs";
37213
+ import { join as join21 } from "path";
36537
37214
 
36538
37215
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
36539
37216
  import { arch, hostname as hostname4, platform, release } from "os";
36540
37217
 
36541
37218
  // ../../packages/plugin-sdk/src/nudge.ts
36542
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
36543
- import { join as join20 } from "path";
37219
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
37220
+ import { join as join22 } from "path";
36544
37221
 
36545
37222
  // ../../packages/plugin-sdk/src/paths.ts
36546
37223
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -36593,7 +37270,7 @@ function applyCategoryPosture(posture, repo, mode = "fill-gaps") {
36593
37270
 
36594
37271
  // ../../packages/plugin-sdk/src/project-files.ts
36595
37272
  import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
36596
- import { basename as basename5, join as join21 } from "path";
37273
+ import { basename as basename5, join as join23 } from "path";
36597
37274
 
36598
37275
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
36599
37276
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -36678,7 +37355,7 @@ function createPluginRuntime(gateway, settings, opts) {
36678
37355
  bundlesPacked = true;
36679
37356
  }
36680
37357
  const policyMode = settings.policy;
36681
- const redactFallback = settings.redactFallback;
37358
+ let redactFallback = settings.redactFallback;
36682
37359
  const dataDir2 = opts?.dataDir;
36683
37360
  let rules = [];
36684
37361
  let scanner;
@@ -36722,6 +37399,7 @@ function createPluginRuntime(gateway, settings, opts) {
36722
37399
  rules = [...verified, ...unverified];
36723
37400
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
36724
37401
  bundleExceptions = bundle.exceptions ?? [];
37402
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
36725
37403
  initialized = true;
36726
37404
  }
36727
37405
  let cachedKey;
@@ -36760,11 +37438,13 @@ function createPluginRuntime(gateway, settings, opts) {
36760
37438
  function decide(findings, text, excepted, rewritable = true) {
36761
37439
  if (findings.length === 0) return { action: "log", text, findings: [] };
36762
37440
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
37441
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
37442
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
36763
37443
  let worst = "log";
36764
37444
  for (const finding of findings) {
36765
37445
  worst = strongerAction(worst, actionFor(finding));
36766
37446
  }
36767
- if (worst === "block") return { action: "block", text: null, findings };
37447
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
36768
37448
  if (worst === "redact") {
36769
37449
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
36770
37450
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -36774,9 +37454,13 @@ function createPluginRuntime(gateway, settings, opts) {
36774
37454
  findings,
36775
37455
  enforcedFindings: redactFindings,
36776
37456
  reversibleFindings
37457
+ // No `degraded` here, and it is not an omission: `rewritable` is per
37458
+ // CAPTURE, so on an unrewritable field every redact has already become
37459
+ // the fallback and this branch is unreachable. Spreading it would read
37460
+ // as a case that can happen.
36777
37461
  };
36778
37462
  }
36779
- return { action: worst, text, findings };
37463
+ return { action: worst, text, findings, ...degraded };
36780
37464
  }
36781
37465
  function fingerprintOf(key, finding, cache) {
36782
37466
  let fp = cache.get(finding);
@@ -36905,8 +37589,8 @@ function createPluginRuntime(gateway, settings, opts) {
36905
37589
  };
36906
37590
  }
36907
37591
  }
36908
- async function processText(text, context) {
36909
- return (await evaluate(text, context, {})).decision;
37592
+ async function processText(text, context, opts2 = {}) {
37593
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
36910
37594
  }
36911
37595
  async function capture(input2, opts2 = {}) {
36912
37596
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -36929,10 +37613,12 @@ function createPluginRuntime(gateway, settings, opts) {
36929
37613
  );
36930
37614
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
36931
37615
  const inspectionMs = elapsedMs(timingStartedAt);
36932
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
37616
+ const redactDegradedTo = decision.redactDegradedTo;
37617
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
36933
37618
  ...input2.metadata,
36934
37619
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
36935
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
37620
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
37621
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
36936
37622
  } : input2.metadata;
36937
37623
  const event = buildIngestEvent({
36938
37624
  kind: input2.kind,
@@ -37004,7 +37690,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
37004
37690
 
37005
37691
  // ../../packages/plugin-sdk/src/throttle.ts
37006
37692
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
37007
- import { join as join22 } from "path";
37693
+ import { join as join24 } from "path";
37008
37694
 
37009
37695
  // ../../packages/setup-wizard/src/remediation/chain.ts
37010
37696
  var REMEDIATION_OPTIONS = [
@@ -37047,7 +37733,7 @@ function routeRemediationOption(option, handlers) {
37047
37733
 
37048
37734
  // ../../packages/setup-wizard/src/remediation/rotation-checklist.ts
37049
37735
  import { writeFileSync as writeFileSync8 } from "fs";
37050
- import { join as join23 } from "path";
37736
+ import { join as join25 } from "path";
37051
37737
  var GENERIC_CONSOLE_PATH = "rotate via the provider's own console";
37052
37738
  var CONSOLE_PATHS = {
37053
37739
  anthropic: "console.anthropic.com \u2192 Settings \u2192 API keys",
@@ -37147,7 +37833,7 @@ function generateRotationChecklist(input2) {
37147
37833
  try {
37148
37834
  const target = resolveRotationChecklistTarget(input2.cwd);
37149
37835
  targetDirectory = target.directory;
37150
- const filePath = join23(target.directory, "rotation-checklist.md");
37836
+ const filePath = join25(target.directory, "rotation-checklist.md");
37151
37837
  writeRotationChecklist(input2.entries, target.directory);
37152
37838
  return {
37153
37839
  status: "written",
@@ -37258,9 +37944,9 @@ var RANK = Object.fromEntries(
37258
37944
  );
37259
37945
 
37260
37946
  // ../../packages/setup-wizard/src/triage/plan-file.ts
37261
- import { mkdtempSync, readFileSync as readFileSync14, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37947
+ import { mkdtempSync, readFileSync as readFileSync16, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37262
37948
  import { tmpdir } from "os";
37263
- import { basename as basename6, dirname as dirname6, join as join24 } from "path";
37949
+ import { basename as basename6, dirname as dirname6, join as join26 } from "path";
37264
37950
  var SuppressionEntrySchema = external_exports.object({
37265
37951
  ruleId: external_exports.string(),
37266
37952
  category: DetectionCategory,
@@ -37404,8 +38090,8 @@ function show(body) {
37404
38090
  }
37405
38091
 
37406
38092
  // src/skills-registry.ts
37407
- import { readdirSync as readdirSync5, readFileSync as readFileSync15 } from "fs";
37408
- import { join as join25 } from "path";
38093
+ import { readdirSync as readdirSync5, readFileSync as readFileSync17 } from "fs";
38094
+ import { join as join27 } from "path";
37409
38095
  import { fileURLToPath as fileURLToPath2 } from "url";
37410
38096
  var SKILLS_DIR = fileURLToPath2(new URL("../skills", import.meta.url));
37411
38097
  function frontmatterName(content) {
@@ -37418,7 +38104,7 @@ function readRegisteredSkills() {
37418
38104
  return readdirSync5(SKILLS_DIR, { withFileTypes: true }).filter((entry) => entry.isDirectory()).flatMap((entry) => {
37419
38105
  let content;
37420
38106
  try {
37421
- content = readFileSync15(join25(SKILLS_DIR, entry.name, "SKILL.md"), "utf8");
38107
+ content = readFileSync17(join27(SKILLS_DIR, entry.name, "SKILL.md"), "utf8");
37422
38108
  } catch {
37423
38109
  return [];
37424
38110
  }
@@ -37664,10 +38350,10 @@ function parsed(schema, body, route2) {
37664
38350
  }
37665
38351
  function withoutTrailingSlashes(endpoint) {
37666
38352
  let end = endpoint.length;
37667
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
38353
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
37668
38354
  return endpoint.slice(0, end);
37669
38355
  }
37670
- var SLASH = "/".charCodeAt(0);
38356
+ var SLASH2 = "/".charCodeAt(0);
37671
38357
  function createRemoteClient(options) {
37672
38358
  const base = withoutTrailingSlashes(options.endpoint);
37673
38359
  const url2 = (route2) => `${base}${route2}`;
@@ -37849,11 +38535,11 @@ function withTimeout(promise2, ms) {
37849
38535
  }
37850
38536
 
37851
38537
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
37852
- import { readFileSync as readFileSync16 } from "fs";
37853
- import { join as join26 } from "path";
38538
+ import { readFileSync as readFileSync18 } from "fs";
38539
+ import { join as join28 } from "path";
37854
38540
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
37855
38541
  function forwardDropsPath(dataDir2) {
37856
- return join26(dataDir2, FORWARD_DROPS_FILENAME);
38542
+ return join28(dataDir2, FORWARD_DROPS_FILENAME);
37857
38543
  }
37858
38544
  function recordForwardDrops(dataDir2, count, nowMs) {
37859
38545
  if (count <= 0) return;
@@ -37871,7 +38557,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
37871
38557
  }
37872
38558
  function readForwardDrops(dataDir2) {
37873
38559
  try {
37874
- const parsed2 = JSON.parse(readFileSync16(forwardDropsPath(dataDir2), "utf8"));
38560
+ const parsed2 = JSON.parse(readFileSync18(forwardDropsPath(dataDir2), "utf8"));
37875
38561
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
37876
38562
  const record2 = parsed2;
37877
38563
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -37889,9 +38575,8 @@ function readForwardDrops(dataDir2) {
37889
38575
 
37890
38576
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
37891
38577
  import { randomUUID as randomUUID15 } from "crypto";
37892
- import { readFileSync as readFileSync17 } from "fs";
37893
38578
  import { readFile, rename, writeFile } from "fs/promises";
37894
- import { join as join27 } from "path";
38579
+ import { join as join29 } from "path";
37895
38580
  function isInvalidRequest(err) {
37896
38581
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
37897
38582
  }
@@ -37905,31 +38590,12 @@ function isServerRejection(err) {
37905
38590
  var FORWARD_BUDGET_MS = 1500;
37906
38591
  var DECISION_PATH_BUDGET_MS = 800;
37907
38592
  var BREAKER_FAILURE_THRESHOLD = 3;
37908
- var BREAKER_COOLDOWN_MS = 3e4;
37909
38593
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
37910
- var FAILURES = /* @__PURE__ */ new Set([
37911
- "unauthorized",
37912
- "forbidden",
37913
- "unreachable"
37914
- ]);
37915
38594
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
37916
38595
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
37917
- function parseBreakerState(raw, nowMs) {
37918
- try {
37919
- const parsed2 = JSON.parse(raw);
37920
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
37921
- const record2 = parsed2;
37922
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
37923
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
37924
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
37925
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
37926
- } catch {
37927
- return null;
37928
- }
37929
- }
37930
38596
  function createForwardPolicy(deps) {
37931
38597
  const now = deps.now ?? (() => Date.now());
37932
- const file2 = join27(deps.dir, STATE_FILENAME);
38598
+ const file2 = join29(deps.dir, STATE_FILENAME);
37933
38599
  let state = null;
37934
38600
  let loading = null;
37935
38601
  async function readState() {
@@ -37939,7 +38605,7 @@ function createForwardPolicy(deps) {
37939
38605
  } catch {
37940
38606
  return { ...CLOSED };
37941
38607
  }
37942
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
38608
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
37943
38609
  }
37944
38610
  async function load() {
37945
38611
  if (state !== null) return state;
@@ -37985,7 +38651,7 @@ function createForwardPolicy(deps) {
37985
38651
  };
37986
38652
  const at = now();
37987
38653
  if (current.openedAtMs !== null) {
37988
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
38654
+ if (isForwardPaused(current, at)) {
37989
38655
  return { ok: false, reason: "breaker-open" };
37990
38656
  }
37991
38657
  await persist({
@@ -38522,7 +39188,18 @@ var AttachedDataGateway = class {
38522
39188
  // and the spread above would otherwise drop the field silently — which is
38523
39189
  // exactly what it did, leaving the whole control inert on every device
38524
39190
  // while every test around it stayed green.
38525
- prohibitedModels: cached2.prohibitedModels
39191
+ prohibitedModels: cached2.prohibitedModels,
39192
+ // NAMED for the same reason as the line above, and it is the same defect
39193
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
39194
+ // only the cache carries is dropped in silence. That is what left
39195
+ // `prohibitedModels` inert on every attached device with every test
39196
+ // around it green.
39197
+ //
39198
+ // Taken from the cache rather than merged here, because merging it needs
39199
+ // the device's own SETTING — which is not a bundle field and is not in
39200
+ // scope at this seam. The runtime does that merge, raise-only, where both
39201
+ // values are in hand (createPluginRuntime's ensureInitialized).
39202
+ redactFallback: cached2.redactFallback
38526
39203
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
38527
39204
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
38528
39205
  // it emits, so an 'authored' policy arriving from the control plane
@@ -38650,10 +39327,6 @@ function toolAuditEvent(input2) {
38650
39327
  };
38651
39328
  }
38652
39329
 
38653
- // ../../packages/plugin-runtime/src/attached/history-state.ts
38654
- import { readFileSync as readFileSync18 } from "fs";
38655
- import { join as join28 } from "path";
38656
-
38657
39330
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
38658
39331
  import { createHash as createHash6 } from "crypto";
38659
39332
  import { hostname as hostname5 } from "os";
@@ -38662,6 +39335,10 @@ import { hostname as hostname5 } from "os";
38662
39335
  var CORRELATION_ID = EventMetadata.shape.correlationId;
38663
39336
  var TRACE_ID = EventMetadata.shape.traceId;
38664
39337
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
39338
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
39339
+
39340
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
39341
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
38665
39342
 
38666
39343
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
38667
39344
  import { spawn } from "child_process";
@@ -38688,7 +39365,7 @@ function createPluginBlock(build, policyStore) {
38688
39365
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38689
39366
  import { randomUUID as randomUUID16 } from "crypto";
38690
39367
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
38691
- import { join as join29 } from "path";
39368
+ import { join as join30 } from "path";
38692
39369
 
38693
39370
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
38694
39371
  import { rename as rename2 } from "fs/promises";
@@ -38712,7 +39389,7 @@ async function publishByRename(tmp, file2, move = rename2) {
38712
39389
 
38713
39390
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38714
39391
  function createPolicyStore(dir = dataDir()) {
38715
- const file2 = join29(dir, "policy-cache.json");
39392
+ const file2 = join30(dir, "policy-cache.json");
38716
39393
  async function read() {
38717
39394
  try {
38718
39395
  const raw = await readFile2(file2, "utf8");
@@ -38943,11 +39620,11 @@ function readStorePosture(dbPath2) {
38943
39620
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
38944
39621
  import { randomUUID as randomUUID17 } from "crypto";
38945
39622
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
38946
- import { join as join30 } from "path";
39623
+ import { join as join31 } from "path";
38947
39624
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
38948
39625
  function createPostureStore(dir = settingsDir(), legacyDir) {
38949
- const file2 = join30(dir, "posture-state.json");
38950
- const legacyFile = legacyDir === void 0 ? null : join30(legacyDir, "posture-state.json");
39626
+ const file2 = join31(dir, "posture-state.json");
39627
+ const legacyFile = legacyDir === void 0 ? null : join31(legacyDir, "posture-state.json");
38951
39628
  async function persist(state) {
38952
39629
  await ensureDataDir(dir);
38953
39630
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -39016,7 +39693,7 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
39016
39693
 
39017
39694
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
39018
39695
  import { readFileSync as readFileSync20 } from "fs";
39019
- import { join as join31 } from "path";
39696
+ import { join as join32 } from "path";
39020
39697
 
39021
39698
  // ../../packages/plugin-runtime/src/attached/status.ts
39022
39699
  var REFUSAL_LINES = {
@@ -39037,6 +39714,14 @@ import { spawn as spawn2 } from "child_process";
39037
39714
  import { fileURLToPath as fileURLToPath4 } from "url";
39038
39715
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
39039
39716
 
39717
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
39718
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
39719
+
39720
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
39721
+ import { spawn as spawn3 } from "child_process";
39722
+ import { fileURLToPath as fileURLToPath5 } from "url";
39723
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
39724
+
39040
39725
  // ../../packages/plugin-runtime/src/attached/factory.ts
39041
39726
  import { hostname as hostname6 } from "os";
39042
39727
 
@@ -39489,15 +40174,24 @@ var EXCEPTION_RETENTION_MS = 90 * 24 * 60 * 60 * 1e3;
39489
40174
  // src/history/transcripts.ts
39490
40175
  import { closeSync as closeSync3, openSync as openSync3, readdirSync as readdirSync6, readFileSync as readFileSync21, readSync as readSync2, statSync as statSync10 } from "fs";
39491
40176
  import { homedir as homedir3 } from "os";
39492
- import { join as join32 } from "path";
40177
+ import { join as join33 } from "path";
39493
40178
  function transcriptsDir(home) {
39494
- return join32(home ?? homedir3(), ".codex", "sessions");
40179
+ return join33(home ?? homedir3(), ".codex", "sessions");
39495
40180
  }
39496
40181
  var DAY_MS5 = 24 * 60 * 60 * 1e3;
39497
40182
 
39498
40183
  // src/remediation/redact.ts
39499
- import { readFileSync as readFileSync22, realpathSync as realpathSync4, renameSync as renameSync5, rmSync as rmSync8, writeFileSync as writeFileSync10 } from "fs";
39500
- import { isAbsolute as isAbsolute2, relative, resolve as resolve2 } from "path";
40184
+ import {
40185
+ lstatSync as lstatSync4,
40186
+ readdirSync as readdirSync7,
40187
+ readFileSync as readFileSync22,
40188
+ realpathSync as realpathSync4,
40189
+ renameSync as renameSync5,
40190
+ rmSync as rmSync8,
40191
+ statSync as statSync11,
40192
+ writeFileSync as writeFileSync10
40193
+ } from "fs";
40194
+ import { basename as basename7, dirname as dirname7, isAbsolute as isAbsolute2, join as join34, relative, resolve as resolve2 } from "path";
39501
40195
  var REDACTED_PLACEHOLDER = "[REDACTED:SECRET]";
39502
40196
  function platformRedactionScope(home) {
39503
40197
  return { artifactRoots: [transcriptsDir(home)] };
@@ -39520,6 +40214,41 @@ function resolveRedactableArtifact(filePath, scope) {
39520
40214
  if (realTarget === null) return null;
39521
40215
  return scope.artifactRoots.some((root) => isWithinRoot(realTarget, root)) ? realTarget : null;
39522
40216
  }
40217
+ var TMP_SUFFIX = ".aka-redact.tmp";
40218
+ function tempPathFor(realPath) {
40219
+ return `${realPath}.${String(process.pid)}${TMP_SUFFIX}`;
40220
+ }
40221
+ function isProcessAlive(pid) {
40222
+ try {
40223
+ process.kill(pid, 0);
40224
+ return true;
40225
+ } catch (error61) {
40226
+ return error61.code !== "ESRCH";
40227
+ }
40228
+ }
40229
+ function sweepStrandedTemps(realPath) {
40230
+ const dir = dirname7(realPath);
40231
+ const prefix = `${basename7(realPath)}.`;
40232
+ let names;
40233
+ try {
40234
+ names = readdirSync7(dir);
40235
+ } catch {
40236
+ return;
40237
+ }
40238
+ for (const name of names) {
40239
+ if (!name.startsWith(prefix) || !name.endsWith(TMP_SUFFIX)) continue;
40240
+ const pidPart = name.slice(prefix.length, name.length - TMP_SUFFIX.length);
40241
+ if (!/^\d+$/.test(pidPart)) continue;
40242
+ const pid = Number(pidPart);
40243
+ if (pid !== process.pid && isProcessAlive(pid)) continue;
40244
+ try {
40245
+ const stranded = join34(dir, name);
40246
+ if (!lstatSync4(stranded).isFile()) continue;
40247
+ rmSync8(stranded, { force: true });
40248
+ } catch {
40249
+ }
40250
+ }
40251
+ }
39523
40252
  function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39524
40253
  const byFile = /* @__PURE__ */ new Map();
39525
40254
  for (const target of targets) {
@@ -39534,11 +40263,14 @@ function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39534
40263
  const struck = [];
39535
40264
  for (const [filePath, fileTargets] of byFile) {
39536
40265
  let content;
40266
+ let mode;
39537
40267
  try {
40268
+ mode = statSync11(filePath).mode & 511;
39538
40269
  content = readFileSync22(filePath, "utf8");
39539
40270
  } catch {
39540
40271
  continue;
39541
40272
  }
40273
+ sweepStrandedTemps(filePath);
39542
40274
  const struckHere = [];
39543
40275
  const struckValues = /* @__PURE__ */ new Set();
39544
40276
  for (const target of fileTargets) {
@@ -39552,9 +40284,9 @@ function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39552
40284
  struckHere.push(target);
39553
40285
  }
39554
40286
  if (struckHere.length === 0) continue;
39555
- const tmpPath = `${filePath}.aka-redact.tmp`;
40287
+ const tmpPath = tempPathFor(filePath);
39556
40288
  try {
39557
- writeFileSync10(tmpPath, content);
40289
+ writeFileSync10(tmpPath, content, { mode, flag: "wx" });
39558
40290
  renameSync5(tmpPath, filePath);
39559
40291
  } catch {
39560
40292
  try {
@@ -39765,7 +40497,7 @@ async function route(frameText, rawOption, rawPosture) {
39765
40497
  break;
39766
40498
  }
39767
40499
  }
39768
- if (process.argv[1] && fileURLToPath5(import.meta.url) === process.argv[1]) {
40500
+ if (process.argv[1] && fileURLToPath6(import.meta.url) === process.argv[1]) {
39769
40501
  try {
39770
40502
  const argv = process.argv.slice(2);
39771
40503
  const optionIndex = argv.indexOf("--option");