@akasecurity/ai-tc-codex 0.9.10 → 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
@@ -492,8 +492,8 @@ var require_ignore = __commonJS({
492
492
  });
493
493
 
494
494
  // ../../packages/plugin-sdk/src/config.ts
495
- import { existsSync as existsSync7 } from "fs";
496
- import { join as join13 } from "path";
495
+ import { existsSync as existsSync8 } from "fs";
496
+ import { join as join16 } from "path";
497
497
 
498
498
  // ../../packages/persistence/src/attached-derived.ts
499
499
  import { rmSync } from "fs";
@@ -506,6 +506,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
506
506
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
507
507
  import { join as join2 } from "path";
508
508
 
509
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
510
+ var DEFERRED_MIGRATION_TAGS = [
511
+ "0031_audit_capture_by_time_index",
512
+ "0032_audit_capture_by_id_index",
513
+ "0033_audit_capture_location_index",
514
+ "0034_findings_read_indexes"
515
+ ];
516
+
509
517
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
510
518
  var SQLITE_MIGRATIONS = [
511
519
  {
@@ -623,6 +631,30 @@ var SQLITE_MIGRATIONS = [
623
631
  {
624
632
  tag: "0028_activity_session_probe_indexes",
625
633
  sql: "CREATE INDEX `idx_audit_session_prompt` ON `audit_events` (`root_session_id`) WHERE event_type = 'prompt';--> statement-breakpoint\nCREATE INDEX `idx_audit_session_share` ON `audit_events` (`root_session_id`) WHERE event_type = 'share';--> statement-breakpoint\nCREATE INDEX `idx_audit_ended_at` ON `audit_events` (`ended_at`,`root_session_id`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\nCREATE INDEX `idx_audit_session_ended` ON `audit_events` (`root_session_id`,`ended_at`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\n-- Expression index for the activity list's turns rollup: a live-captured\n-- session's turns are the DISTINCT `run_key` across its `llm_call` leaves, and\n-- carrying the extracted key in the index answers that count from the index\n-- alone instead of parsing every leaf's attribute bag. Written by hand, as\n-- 0013's `idx_audit_code_change_path` was: drizzle-kit cannot emit an\n-- expression containing a comma, so this index is not declared in sqlite.ts.\nCREATE INDEX `idx_audit_session_run_key` ON `audit_events` (`root_session_id`, json_extract(`attributes`, '$.run_key')) WHERE `event_type` = 'llm_call';\n"
634
+ },
635
+ {
636
+ tag: "0029_audit_capture_rollup_index",
637
+ sql: "CREATE INDEX `idx_audit_capture_rollup` ON `audit_events` (`event_type`,`started_at`,`repo`,`id`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
638
+ },
639
+ {
640
+ tag: "0030_audit_content_expiry",
641
+ sql: "ALTER TABLE `audit_events` ADD `content_expired_at` integer;--> statement-breakpoint\nCREATE INDEX `idx_audit_expirable_body` ON `audit_events` (`started_at`) WHERE content IS NOT NULL;"
642
+ },
643
+ {
644
+ tag: "0031_audit_capture_by_time_index",
645
+ sql: "CREATE INDEX `idx_audit_capture_by_time` ON `audit_events` (`started_at`,`id`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
646
+ },
647
+ {
648
+ tag: "0032_audit_capture_by_id_index",
649
+ sql: "CREATE INDEX `idx_audit_capture_by_id` ON `audit_events` (`id`,`started_at`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
650
+ },
651
+ {
652
+ tag: "0033_audit_capture_location_index",
653
+ sql: "CREATE INDEX `idx_audit_capture_location` ON `audit_events` (`repo`,`file_path`,`started_at`,`id`,`event_type`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
654
+ },
655
+ {
656
+ tag: "0034_findings_read_indexes",
657
+ sql: "CREATE INDEX `idx_inspection_definitions_rule` ON `inspection_definitions` (`rule_id`,`severity`,`category`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_def` ON `inspection_findings` (`inspection_definition_id`,`audit_event_id`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_event_cover` ON `inspection_findings` (`audit_event_id`,`inspection_definition_id`,`action_taken`,`finding_key`,`id`);"
626
658
  }
627
659
  ];
628
660
 
@@ -20623,7 +20655,7 @@ var TOOL_TO_HARNESS = {
20623
20655
  [SOURCE_TOOL.ClaudeAi]: HARNESS.ClaudeAi
20624
20656
  };
20625
20657
  function harnessFromTool(tool) {
20626
- return TOOL_TO_HARNESS[tool] ?? tool;
20658
+ return (Object.hasOwn(TOOL_TO_HARNESS, tool) ? TOOL_TO_HARNESS[tool] : void 0) ?? tool;
20627
20659
  }
20628
20660
 
20629
20661
  // ../../packages/schema/src/zod/finding.ts
@@ -20673,6 +20705,15 @@ var FindingCategory = external_exports.enum([
20673
20705
  ]).meta({ id: "FindingCategory" });
20674
20706
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20675
20707
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20708
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20709
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20710
+ var FindingDelivery = external_exports.object({
20711
+ state: FindingDeliveryState,
20712
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20713
+ at: external_exports.iso.datetime().optional(),
20714
+ // Only on `not_sent`, and only when a known reason was recorded.
20715
+ reason: SyncFailureReason.optional()
20716
+ }).meta({ id: "FindingDelivery" });
20676
20717
  var ResolutionMethod = external_exports.enum([
20677
20718
  "enforced-in-flight",
20678
20719
  "fixed-at-source",
@@ -20729,7 +20770,10 @@ var FindingInstance = external_exports.object({
20729
20770
  // The session that event belongs to, when it has one — the seam a
20730
20771
  // per-instance "view session" link needs. Absent for events captured
20731
20772
  // outside a session.
20732
- sessionId: external_exports.string().optional()
20773
+ sessionId: external_exports.string().optional(),
20774
+ // The delivery state of the event above (see FindingDelivery). Optional so
20775
+ // readers that do not project it stay valid.
20776
+ delivery: FindingDelivery.optional()
20733
20777
  }).meta({ id: "FindingInstance" });
20734
20778
  var FindingGroup = external_exports.object({
20735
20779
  id: external_exports.string(),
@@ -20746,13 +20790,11 @@ var FindingGroup = external_exports.object({
20746
20790
  latestDetectedAt: external_exports.iso.datetime(),
20747
20791
  instances: external_exports.array(FindingInstance),
20748
20792
  // Derived from instances' statuses with open-dominates precedence (see
20749
- // buildFindingGroups). Undefined only when no instance carries a status.
20793
+ // foldGroupStatus). Undefined only when no instance carries a status.
20750
20794
  status: FindingStatus.optional(),
20751
- // The distinct people across the WHOLE group, not just the `instances`
20752
- // preview — from the store's whole-group aggregate when it supplies one,
20753
- // else folded from the rows (see buildFindingGroups). Undefined when no
20754
- // instance carries a user, or when the store supplied whole-group folds
20755
- // without one.
20795
+ // The distinct people across the WHOLE group, not just the instances
20796
+ // carried here. Undefined when no instance carries a user, or when the
20797
+ // store supplied whole-group folds without one.
20756
20798
  users: external_exports.array(FindingUser).optional()
20757
20799
  }).meta({ id: "FindingGroup" });
20758
20800
  var FindingStats = external_exports.object({
@@ -20781,21 +20823,34 @@ var FindingFacets = external_exports.object({
20781
20823
  // counted under no value.
20782
20824
  status: external_exports.array(FindingFacetItem),
20783
20825
  // Host tool (attributes.tool_name). Present only on the instance-level
20784
- // reads, which can filter by it; the grouped read omits the dimension
20826
+ // reads, which can filter by it; the type-level read omits the dimension
20785
20827
  // because a group spans tools.
20786
- tool: external_exports.array(FindingFacetItem).optional()
20828
+ tool: external_exports.array(FindingFacetItem).optional(),
20829
+ // Delivery states (FindingDeliveryState). Present only on the
20830
+ // instance-level reads, like `tool`.
20831
+ deployment: external_exports.array(FindingFacetItem).optional()
20787
20832
  }).meta({ id: "FindingFacets" });
20788
- var DEFAULT_GROUPED_FINDINGS_LIMIT = 50;
20789
- var ListGroupedFindingsQuery = external_exports.object({
20833
+ var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20834
+ id: "FindingTypeSummary"
20835
+ });
20836
+ var DEFAULT_FINDING_TYPES_LIMIT = 50;
20837
+ var MAX_FINDING_TYPES_LIMIT = 100;
20838
+ var ListFindingTypesQuery = external_exports.object({
20790
20839
  // NOTE: severity filters by Severity (critical/high/medium/low), not by
20791
- // FindingAction.
20840
+ // FindingAction. It narrows TYPES: a type's severity is the one its newest
20841
+ // firing version carries, and this list pages types.
20842
+ //
20843
+ // That is NOT a claim the findings of a type share it. A rule can hold several
20844
+ // definition versions at different severities, so a type kept by this filter
20845
+ // can hold findings that individually do not match — see totals.findings on
20846
+ // ListFindingTypesResponse, which counts them all.
20792
20847
  severity: external_exports.array(Severity).optional(),
20793
20848
  subtype: external_exports.array(external_exports.string()).optional(),
20794
20849
  provider: external_exports.array(FindingProvider).optional(),
20795
20850
  action: external_exports.array(FindingAction).optional(),
20796
- // Matches a group's DERIVED status (see FindingGroup.status), not its
20797
- // individual instances' — so a filtered group's Status column always reads
20798
- // one of the requested values.
20851
+ // Matches a type's DERIVED status (see FindingGroup.status), not its
20852
+ // individual findings' — so a filtered row's status always reads one of the
20853
+ // requested values.
20799
20854
  status: external_exports.array(FindingStatus).optional(),
20800
20855
  q: external_exports.string().optional(),
20801
20856
  // Scope to findings whose event carries this session id (the Activity page's
@@ -20805,23 +20860,37 @@ var ListGroupedFindingsQuery = external_exports.object({
20805
20860
  // from a time-scoped page (Activity's range) can carry that scope. Absent
20806
20861
  // means all time — this list has no default window.
20807
20862
  from: external_exports.iso.datetime().optional(),
20808
- // A group or instance id that must appear in the page even when the cursor
20809
- // has already advanced past its sort position. This is what keeps the
20810
- // Findings page's one-shot ?finding= deep link resolving once the list
20811
- // paginates: the target group is appended out of sort order rather than
20812
- // scanning forward for it. Never affects totals, facets or the cursor.
20863
+ // A RULE id that must appear in the page even when the cursor has already
20864
+ // advanced past its sort position. This is what keeps the selected type
20865
+ // visible in the list once it paginates: the target is appended out of sort
20866
+ // order rather than scanned forward for. Never affects totals, facets or the
20867
+ // cursor. Unlike the grouped read this replaces, it names a rule only — an
20868
+ // instance id is resolved by `findingInstance`, which is a primary-key seek
20869
+ // and so is not bounded by what any page happens to hold.
20813
20870
  includeId: external_exports.string().optional(),
20814
- groupBy: external_exports.literal("type").optional(),
20815
- limit: external_exports.coerce.number().int().min(1).max(100).optional(),
20871
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_TYPES_LIMIT).optional(),
20816
20872
  cursor: external_exports.string().optional()
20817
20873
  });
20818
- var ListGroupedFindingsResponse = external_exports.object({
20874
+ var ListFindingTypesResponse = external_exports.object({
20819
20875
  totals: external_exports.object({
20876
+ // Findings belonging to the matching TYPES — not findings that each match
20877
+ // the filters. The filters here select types, so a type that survives
20878
+ // contributes its whole instanceCount.
20879
+ //
20880
+ // `status` is the one exception, narrowed per finding via
20881
+ // countInstancesByStatus. `severity`, `provider` and `action` are not, so
20882
+ // this can exceed what the instance read reports for the same filters: a
20883
+ // rule whose severity moved between versions is kept on its newest and
20884
+ // still counts its older findings. Narrowing the other three needs
20885
+ // per-dimension counts the aggregate does not carry today.
20820
20886
  findings: external_exports.number().int().nonnegative(),
20821
- groups: external_exports.number().int().nonnegative()
20887
+ // Counts TYPES, which is the unit this read pages. The instance read's
20888
+ // own totals count findings; the two deliberately answer different
20889
+ // questions and are never summed.
20890
+ types: external_exports.number().int().nonnegative()
20822
20891
  }),
20823
20892
  facets: FindingFacets,
20824
- items: external_exports.array(FindingGroup),
20893
+ items: external_exports.array(FindingTypeSummary),
20825
20894
  nextCursor: external_exports.string().nullable(),
20826
20895
  // Present only on session-scoped queries (`sessionId` set): per ruleId, how
20827
20896
  // many times that rule fired in the session's persisted transcript. Findings
@@ -20829,7 +20898,7 @@ var ListGroupedFindingsResponse = external_exports.object({
20829
20898
  // every firing, so the two numbers legitimately differ — this map lets a
20830
20899
  // session-scoped view show both.
20831
20900
  sessionFirings: external_exports.record(external_exports.string(), external_exports.number().int().nonnegative()).optional()
20832
- }).meta({ id: "ListGroupedFindingsResponse" });
20901
+ }).meta({ id: "ListFindingTypesResponse" });
20833
20902
  var ApplyFindingActionRequest = external_exports.object({
20834
20903
  // 'quarantined' is system-assigned (see FindingAction) — clients may not set
20835
20904
  // it, so it is excluded from the request contract. The mapping helper
@@ -20859,16 +20928,19 @@ var DEFAULT_FLAT_FINDINGS_LIMIT = 50;
20859
20928
  var MAX_FLAT_FINDINGS_LIMIT = 200;
20860
20929
  var ListFindingInstancesQuery = external_exports.object({
20861
20930
  severity: external_exports.array(Severity).optional(),
20862
- // Rule ids, the same vocabulary the grouped list's `subtype` carries.
20931
+ // Rule ids, the same vocabulary the types list's `subtype` carries. Pinning
20932
+ // ONE of them is how the master/detail view scopes its right-hand panel.
20863
20933
  subtype: external_exports.array(external_exports.string()).optional(),
20864
20934
  provider: external_exports.array(FindingProvider).optional(),
20865
20935
  action: external_exports.array(FindingAction).optional(),
20866
20936
  // Matches each instance's OWN derived status (deriveFindingStatus), unlike
20867
- // the grouped query's group-level fold.
20937
+ // the types query's type-level fold.
20868
20938
  status: external_exports.array(FindingStatus).optional(),
20869
20939
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20870
20940
  // where the free-text `q` can only match the rendered "via Bash" label.
20871
20941
  tool: external_exports.array(external_exports.string()).optional(),
20942
+ // The delivery state of each finding's event (see FindingDelivery).
20943
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20872
20944
  // Exact repository / file-path matches, for the drill-down out of the
20873
20945
  // locations view. A row whose event carries no repo/file matches neither.
20874
20946
  repo: external_exports.string().optional(),
@@ -20881,37 +20953,51 @@ var ListFindingInstancesQuery = external_exports.object({
20881
20953
  });
20882
20954
  var ListFindingInstancesResponse = external_exports.object({
20883
20955
  // Instances matching the filters across the whole scope, not just this
20884
- // page — cursor-independent, like the grouped list's totals.
20956
+ // page — cursor-independent, like the types list's totals.
20885
20957
  totals: external_exports.object({ findings: external_exports.number().int().nonnegative() }),
20886
- // Counts in INSTANCES here, where the grouped response counts groups. Each
20958
+ // Counts in INSTANCES here, where the types response counts types. Each
20887
20959
  // dimension still excludes its own filter.
20888
20960
  facets: FindingFacets,
20889
20961
  items: external_exports.array(FindingInstanceDetail),
20890
20962
  nextCursor: external_exports.string().nullable()
20891
20963
  }).meta({ id: "ListFindingInstancesResponse" });
20892
- var FindingLocationFile = external_exports.object({
20893
- // Empty when the instances carried no file path (a prompt or a tool call
20894
- // with no file attribution).
20895
- file: external_exports.string(),
20896
- instanceCount: external_exports.number().int().nonnegative(),
20897
- maxSeverity: Severity,
20898
- latestDetectedAt: external_exports.iso.datetime(),
20899
- // Folded from the instances' derived statuses with the same
20900
- // open-dominates precedence a group uses.
20901
- status: FindingStatus.optional(),
20902
- // Distinct rules seen at this location, capped — the row shows them as
20903
- // chips, and the count is what conveys scale.
20904
- ruleIds: external_exports.array(external_exports.string())
20905
- }).meta({ id: "FindingLocationFile" });
20906
- var FindingLocationRepo = external_exports.object({
20964
+ var ListFindingInstancesPage = external_exports.object({
20965
+ items: external_exports.array(FindingInstanceDetail),
20966
+ nextCursor: external_exports.string().nullable()
20967
+ }).meta({ id: "ListFindingInstancesPage" });
20968
+ var FindingLocationSummary = external_exports.object({
20969
+ // Opaque, stable, minted from the pair by encodeLocationId. It exists
20970
+ // because a location's identity is two values and a URL param carries one:
20971
+ // `?loc=` names a location the way `?rule=` names a type. Only ever compared
20972
+ // for EQUALITY — the page's selection check, this read's `includeId`, the
20973
+ // client's page dedupe — never decoded, and never a sort key.
20974
+ id: external_exports.string(),
20907
20975
  /** Empty when the instances carried no repo attribute. */
20908
20976
  repo: external_exports.string(),
20977
+ // Empty when the instances carried no file path (a prompt, or a tool call
20978
+ // with no file attribution). Both halves empty is a real location — usually
20979
+ // the largest one in a store — and is selectable like any other.
20980
+ file: external_exports.string(),
20909
20981
  instanceCount: external_exports.number().int().nonnegative(),
20982
+ // The WORST severity present, not the first row's. It is this list's primary
20983
+ // sort key, so it is also what explains why a row is where it is, and it is
20984
+ // how a reader decides what to open without opening everything.
20910
20985
  maxSeverity: Severity,
20911
20986
  latestDetectedAt: external_exports.iso.datetime(),
20987
+ // Folded from the instances' derived statuses with the same open-dominates
20988
+ // precedence a group uses, so it answers "is anything left to do here" and
20989
+ // not much more: a location holding 1 open among 40 resolved reads like one
20990
+ // holding 40 open. That loss is accepted — the panel beside this list
20991
+ // carries each finding's own status, and instanceCount sits next to the
20992
+ // badge.
20912
20993
  status: FindingStatus.optional(),
20913
- files: external_exports.array(FindingLocationFile)
20914
- }).meta({ id: "FindingLocationRepo" });
20994
+ // Every distinct rule seen at this location, UNCAPPED — so the length is a
20995
+ // tally rather than a sample and a row can say how many there are. Bounded
20996
+ // by the ruleset, not by the store. The view bounds what it DISPLAYS.
20997
+ ruleIds: external_exports.array(external_exports.string())
20998
+ }).meta({ id: "FindingLocationSummary" });
20999
+ var DEFAULT_FINDING_LOCATIONS_LIMIT = 50;
21000
+ var MAX_FINDING_LOCATIONS_LIMIT = 100;
20915
21001
  var ListFindingLocationsQuery = external_exports.object({
20916
21002
  severity: external_exports.array(Severity).optional(),
20917
21003
  subtype: external_exports.array(external_exports.string()).optional(),
@@ -20921,21 +21007,47 @@ var ListFindingLocationsQuery = external_exports.object({
20921
21007
  // instances that match, and folds its status from those.
20922
21008
  status: external_exports.array(FindingStatus).optional(),
20923
21009
  tool: external_exports.array(external_exports.string()).optional(),
21010
+ // The delivery state of each finding's event (see FindingDelivery).
21011
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20924
21012
  q: external_exports.string().optional(),
20925
21013
  sessionId: external_exports.string().optional(),
20926
21014
  from: external_exports.iso.datetime().optional(),
20927
- limit: external_exports.coerce.number().int().min(1).max(500).optional()
21015
+ // A LOCATION id (see FindingLocationSummary.id) that must appear in the page
21016
+ // even when the cursor has already advanced past its sort position — the
21017
+ // counterpart of ListFindingTypesQuery.includeId, and needed far more often
21018
+ // here. Selecting a row pushes the URL, which re-renders the server and resets
21019
+ // the client's page cache to page 0; with distinct (repo, file) pairs running
21020
+ // into the thousands, a selection sitting off page 0 is the ordinary case
21021
+ // rather than a deep-link corner. Never affects totals, facets or the cursor.
21022
+ includeId: external_exports.string().optional(),
21023
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_LOCATIONS_LIMIT).optional(),
21024
+ cursor: external_exports.string().optional()
20928
21025
  });
20929
21026
  var ListFindingLocationsResponse = external_exports.object({
20930
21027
  totals: external_exports.object({
21028
+ // Findings matching the filters across the whole scope. Unlike the types
21029
+ // read's same-named field this needs no caveat: the filters here narrow
21030
+ // per finding, so this is the sum of every row's instanceCount.
20931
21031
  findings: external_exports.number().int().nonnegative(),
20932
- repos: external_exports.number().int().nonnegative(),
20933
- files: external_exports.number().int().nonnegative()
21032
+ // Counts LOCATIONS, the unit this read pages — the number the paginator
21033
+ // states. The facets beside it count FINDINGS (see below); a surface
21034
+ // showing both says which is which.
21035
+ locations: external_exports.number().int().nonnegative()
20934
21036
  }),
20935
- /** Sorted by max severity, then most recent. */
20936
- items: external_exports.array(FindingLocationRepo),
20937
- /** Whether `limit` truncated the repo list. */
20938
- hasMore: external_exports.boolean()
21037
+ // Counts in FINDINGS, where the types response counts types, each dimension
21038
+ // still excluding its own filter. Deliberately not locations: counting those
21039
+ // needs a set of location keys per dimension per value — memory tracking the
21040
+ // store times the vocabulary, in a read whose scan promises flat memory —
21041
+ // and the cheap per-location version is not an approximation but WRONG. A
21042
+ // location holding {claudecode, block} and {codex, warn} would survive
21043
+ // provider=claudecode AND action=warn, under which no single finding
21044
+ // matches, so the facet would contradict the instanceCount this whole view
21045
+ // rests on. Findings also keep the toolbar in the same unit as the page
21046
+ // tally and the panel it sits above.
21047
+ facets: FindingFacets,
21048
+ /** Sorted by max severity, then most recent, then (repo, file). */
21049
+ items: external_exports.array(FindingLocationSummary),
21050
+ nextCursor: external_exports.string().nullable()
20939
21051
  }).meta({ id: "ListFindingLocationsResponse" });
20940
21052
 
20941
21053
  // ../../packages/schema/src/zod/meta.ts
@@ -21099,6 +21211,10 @@ var CaptureAttributes = external_exports.object({
21099
21211
  // to 'allow' — the enforcement audit trail's link back to the grant that
21100
21212
  // authorized the bypass.
21101
21213
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21214
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21215
+ // join back to the `llm_call` leaf for the same assistant turn.
21216
+ message_id: external_exports.string().optional(),
21217
+ conversation_id: external_exports.string().optional(),
21102
21218
  // Whole milliseconds this capture's inspection blocked its caller — the
21103
21219
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21104
21220
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21107,7 +21223,19 @@ var CaptureAttributes = external_exports.object({
21107
21223
  // inline json_extract and is not itself an optimization.
21108
21224
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21109
21225
  // before the measurement shipped — never present as a placeholder 0.
21110
- inspection_ms: external_exports.number().int().nonnegative().optional()
21226
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21227
+ // What a `redact` this capture could not carry out became instead (see
21228
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21229
+ // degrade actually happened, so absence is the ordinary case rather than a
21230
+ // reader having to distinguish it from a zero.
21231
+ //
21232
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21233
+ // so on a multi-finding row this does not say which finding degraded, and
21234
+ // its presence does not mean the fallback decided the capture's action. A
21235
+ // capture denied by another finding's own Block policy carries `block`
21236
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21237
+ // repeated rather than referenced because a store reader opens this file.
21238
+ redact_degraded_to: ActionTaken.optional()
21111
21239
  }).catchall(external_exports.unknown());
21112
21240
  var ToolCallInspection = external_exports.object({
21113
21241
  ruleId: external_exports.string().min(1),
@@ -21306,7 +21434,17 @@ var AuditEvent = external_exports.object({
21306
21434
  /** `share` to a first-party/internal destination. */
21307
21435
  internal: external_exports.boolean(),
21308
21436
  /** Event needs review (e.g. unverified egress). */
21309
- flagged: external_exports.boolean()
21437
+ flagged: external_exports.boolean(),
21438
+ /**
21439
+ * The body this event's `title` is drawn from was cleared by local body
21440
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21441
+ *
21442
+ * A separate flag rather than a sentinel written into `title`: the title is
21443
+ * rendered text, and a store-layer module that invented display copy for it
21444
+ * would be choosing words the view is supposed to choose. Additive and
21445
+ * defaulted, so an older producer still validates.
21446
+ */
21447
+ bodyExpired: external_exports.boolean().default(false)
21310
21448
  }).meta({ id: "ActivityAuditEvent" });
21311
21449
  var ActivitySessionSummary = external_exports.object({
21312
21450
  id: external_exports.string(),
@@ -22104,6 +22242,14 @@ var ControlPlaneErrorBody = external_exports.object({
22104
22242
  message: external_exports.string().optional()
22105
22243
  }).optional()
22106
22244
  });
22245
+ var RemoteFailureKind = external_exports.enum([
22246
+ "unauthorized",
22247
+ "forbidden",
22248
+ "route-absent",
22249
+ "invalid-request",
22250
+ "rejected",
22251
+ "unreachable"
22252
+ ]);
22107
22253
  var AttachDeviceRequest = external_exports.object({
22108
22254
  // This machine's own continuity id, so re-attaching ROTATES the credential
22109
22255
  // on one machine record instead of producing a second one. Client-minted
@@ -22639,6 +22785,12 @@ var EventMetadata = external_exports.object({
22639
22785
  // to 'allow' — the enforcement audit trail's link back to the grant that
22640
22786
  // authorized the bypass. Absent on captures where no exception applied.
22641
22787
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22788
+ // The assistant message this capture belongs to, and the conversation it sits
22789
+ // in — set by the browser extension's network capture so a stored `response`
22790
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22791
+ // on every other capture path, which has no such id.
22792
+ messageId: external_exports.string().optional(),
22793
+ conversationId: external_exports.string().optional(),
22642
22794
  // How long THIS capture's inspection blocked its caller, in whole
22643
22795
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22644
22796
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22651,7 +22803,37 @@ var EventMetadata = external_exports.object({
22651
22803
  // Absent is also what every pre-measurement client writes, and what a
22652
22804
  // clock failure degrades to — a reader must treat absence as "not measured"
22653
22805
  // and never as a zero, which would read as "inspection is free".
22654
- inspectionMs: external_exports.number().int().nonnegative().optional()
22806
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22807
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22808
+ // workspace's `redactFallback`, applied because the field could not be
22809
+ // masked in place (a shell command, a URL, or any argument on a host whose
22810
+ // hook contract offers no rewrite channel).
22811
+ //
22812
+ // It exists because the action alone cannot say why. A finding recorded as
22813
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22814
+ // assigned Redact on a field that could not take one — and those are
22815
+ // different facts about the same row: the first is a policy the user chose,
22816
+ // the second is a masking the host could not perform. Absent means no
22817
+ // degrade happened, which is every ordinary capture.
22818
+ //
22819
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22820
+ // is the CAPTURE while `actionTaken` is per FINDING:
22821
+ //
22822
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22823
+ // `redact` alongside a finding ASSIGNED the same action stores both
22824
+ // identically and one reason for the pair; attributing it to both
22825
+ // describes the assigned one wrongly, and to neither loses the degrade.
22826
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22827
+ // became, not the reason the capture ended as it did — a capture denied
22828
+ // by some other finding's own Block policy still carries `block` here,
22829
+ // and clearing the workspace's fallback would not have let it through.
22830
+ // Gate on the value against what a fallback can produce; never read the
22831
+ // field's presence as "this was the fallback's doing".
22832
+ //
22833
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22834
+ // Closing either means moving the reason onto the finding row, which
22835
+ // already carries its own action.
22836
+ redactDegradedTo: ActionTaken.optional()
22655
22837
  }).meta({ id: "EventMetadata" });
22656
22838
  var Event = external_exports.object({
22657
22839
  id: external_exports.guid(),
@@ -22761,7 +22943,32 @@ var RotateKeyInput = external_exports.object({
22761
22943
  confirmation: external_exports.string()
22762
22944
  });
22763
22945
 
22946
+ // ../../packages/schema/src/zod/finding-delivery.ts
22947
+ var KNOWN_REASONS = SyncFailureReason.options;
22948
+ function knownReason(value) {
22949
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22950
+ }
22951
+ function deriveFindingDelivery(row) {
22952
+ if (row.kind === "code_change") return { state: "local_scan" };
22953
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22954
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22955
+ }
22956
+ if (row.syncedAt !== null) {
22957
+ const reason = knownReason(row.syncFailure);
22958
+ return {
22959
+ state: "not_sent",
22960
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22961
+ ...reason === void 0 ? {} : { reason }
22962
+ };
22963
+ }
22964
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22965
+ return { state: "never_offered" };
22966
+ }
22967
+
22764
22968
  // ../../packages/schema/src/zod/findings-group-build.ts
22969
+ function lookupOwn(map2, key) {
22970
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22971
+ }
22765
22972
  function toApiAction(dbVal) {
22766
22973
  const map2 = {
22767
22974
  log: "monitored",
@@ -22770,7 +22977,7 @@ function toApiAction(dbVal) {
22770
22977
  warn: "warned",
22771
22978
  allow: "allowed"
22772
22979
  };
22773
- return map2[dbVal] ?? "allowed";
22980
+ return lookupOwn(map2, dbVal) ?? "allowed";
22774
22981
  }
22775
22982
  function toApiCategory(dbVal) {
22776
22983
  if (dbVal === "code_context") return "source_code";
@@ -22778,13 +22985,18 @@ function toApiCategory(dbVal) {
22778
22985
  return parsed2.success ? parsed2.data : "custom";
22779
22986
  }
22780
22987
  function toApiProvider(sourceTool) {
22781
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22988
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22782
22989
  }
22783
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22990
+ var FINDING_STATUS_PRECEDENCE = [
22991
+ "open",
22992
+ "handled",
22993
+ "dismissed",
22994
+ "resolved"
22995
+ ];
22784
22996
  function foldGroupStatus(instanceStatuses) {
22785
22997
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22786
22998
  if (statuses.size === 0) return void 0;
22787
- for (const candidate of STATUS_PRECEDENCE) {
22999
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22788
23000
  if (statuses.has(candidate)) return candidate;
22789
23001
  }
22790
23002
  return void 0;
@@ -22797,139 +23009,62 @@ function deriveFindingStatus(row) {
22797
23009
  if (row.latestResolutionStatus === "dismissed") return "dismissed";
22798
23010
  return "open";
22799
23011
  }
22800
- function distinctUsers(instances) {
22801
- const seen = /* @__PURE__ */ new Set();
22802
- const users = [];
22803
- for (const i of instances) {
22804
- if (i.user === void 0 || seen.has(i.user.id)) continue;
22805
- seen.add(i.user.id);
22806
- users.push(i.user);
22807
- }
22808
- return users;
22809
- }
22810
23012
  function sortUsers(users) {
22811
23013
  return [...users].sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
22812
23014
  }
22813
- function buildFindingGroups(rows, opts = {}) {
22814
- const overrides = opts.overrides;
23015
+ function buildFindingTypes(aggregates, opts = {}) {
22815
23016
  const packNames = opts.packNames;
22816
- const aggregates = opts.aggregates;
22817
- const byRuleId = /* @__PURE__ */ new Map();
22818
- for (const row of rows) {
22819
- const existing = byRuleId.get(row.ruleId);
22820
- if (existing) existing.push(row);
22821
- else byRuleId.set(row.ruleId, [row]);
22822
- }
22823
- const groups = [];
22824
- for (const [ruleId, ruleRows] of byRuleId) {
22825
- const instances = ruleRows.map((r) => {
22826
- const effectiveDbAction = overrides?.get(r.id) ?? r.actionTaken;
22827
- return {
22828
- id: r.id,
22829
- provider: toApiProvider(r.sourceTool),
22830
- repo: r.repo,
22831
- file: r.file,
22832
- ...r.toolName === void 0 ? {} : { toolName: r.toolName },
22833
- ...r.eventId === void 0 ? {} : { eventId: r.eventId },
22834
- ...r.sessionId === void 0 ? {} : { sessionId: r.sessionId },
22835
- ...r.user === void 0 ? {} : { user: r.user },
22836
- action: toApiAction(effectiveDbAction),
22837
- detectedAt: r.occurredAt,
22838
- confidence: r.confidence,
22839
- status: r.status
22840
- };
22841
- });
22842
- const agg = aggregates?.get(ruleId);
22843
- const users = agg ? sortUsers(agg.users ?? []) : distinctUsers(instances);
22844
- const latestDetectedAt = agg?.latestDetectedAt ?? ruleRows.reduce(
22845
- (max, r) => r.occurredAt > max ? r.occurredAt : max,
22846
- ruleRows[0]?.occurredAt ?? (/* @__PURE__ */ new Date(0)).toISOString()
22847
- );
22848
- const seenProviders = /* @__PURE__ */ new Set();
22849
- const providers = (agg ? [...new Set(agg.sourceTools.map(toApiProvider))].sort() : instances.map((i) => i.provider)).filter((p) => {
22850
- if (seenProviders.has(p)) return false;
22851
- seenProviders.add(p);
22852
- return true;
22853
- });
22854
- const actionSet = new Set(
22855
- agg ? agg.actionsTaken.map(toApiAction) : instances.map((i) => i.action)
22856
- );
23017
+ const types = [];
23018
+ for (const [ruleId, agg] of aggregates) {
23019
+ const users = sortUsers(agg.users ?? []);
23020
+ const providers = [...new Set(agg.sourceTools.map(toApiProvider))].sort();
23021
+ const actionSet = new Set(agg.actionsTaken.map(toApiAction));
22857
23022
  const aggregateAction = actionSet.size === 1 ? [...actionSet][0] ?? null : null;
22858
- const severity = ruleRows[0]?.severity ?? "low";
22859
- const detection = {
22860
- id: ruleId,
22861
- name: packNames?.get(ruleId) ?? null
22862
- };
22863
- const apiCategory = toApiCategory(ruleRows[0]?.category ?? "custom");
22864
- const policy = { id: `category:${apiCategory}`, name: apiCategory };
22865
- const match = {
22866
- maskedValue: ruleRows[0]?.maskedMatch ?? "",
22867
- contextPrefix: ""
22868
- // empty (pending privacy review)
22869
- };
22870
- const status = foldGroupStatus(
22871
- agg ? agg.statusInputs.map(deriveFindingStatus) : instances.map((i) => i.status)
22872
- );
22873
- const group = {
23023
+ const apiCategory = toApiCategory(agg.category ?? "custom");
23024
+ const type = {
22874
23025
  id: ruleId,
22875
23026
  category: apiCategory,
22876
23027
  subtype: ruleId,
22877
23028
  // human label comes with pack metadata later
22878
- severity,
22879
- match,
22880
- detection,
22881
- policy,
22882
- instanceCount: agg?.instanceCount ?? instances.length,
23029
+ severity: agg.severity ?? "low",
23030
+ detection: { id: ruleId, name: packNames?.get(ruleId) ?? null },
23031
+ policy: { id: `category:${apiCategory}`, name: apiCategory },
23032
+ instanceCount: agg.instanceCount,
22883
23033
  providers,
22884
23034
  aggregateAction,
22885
- latestDetectedAt,
22886
- instances,
22887
- status,
23035
+ latestDetectedAt: agg.latestDetectedAt,
23036
+ status: foldGroupStatus(agg.statusInputs.map(deriveFindingStatus)),
22888
23037
  ...users.length > 0 ? { users } : {}
22889
23038
  };
22890
- if (agg) {
22891
- actionsCache.set(group, [...actionSet]);
22892
- if (agg.searchText !== void 0) {
22893
- haystackCache.set(group, buildHaystack(group, agg.searchText));
22894
- }
23039
+ actionsCache.set(type, [...actionSet]);
23040
+ if (agg.searchText !== void 0) {
23041
+ haystackCache.set(type, buildHaystack(type, agg.searchText));
22895
23042
  }
22896
- groups.push(group);
23043
+ types.push(type);
22897
23044
  }
22898
- return groups;
23045
+ return types;
22899
23046
  }
22900
23047
  var haystackCache = /* @__PURE__ */ new WeakMap();
22901
- function buildHaystack(g, extra) {
23048
+ function buildHaystack(t, extra) {
22902
23049
  return [
22903
- g.subtype,
22904
- g.category,
22905
- g.match.maskedValue,
22906
- g.policy.name,
22907
- g.id,
22908
- ...g.instances.map((i) => i.repo),
22909
- ...g.instances.map((i) => i.file),
22910
- ...g.instances.map((i) => i.toolName ? `via ${i.toolName}` : ""),
22911
- ...g.instances.map((i) => i.id),
22912
- // The people: the whole group's list when the store folded one, plus the
22913
- // preview's own — the two overlap, and a haystack does not mind.
22914
- ...(g.users ?? []).map((u) => u.name),
22915
- ...g.instances.map((i) => i.user?.name ?? ""),
23050
+ t.subtype,
23051
+ t.category,
23052
+ t.policy.name,
23053
+ t.id,
23054
+ ...(t.users ?? []).map((u) => u.name),
22916
23055
  ...extra === void 0 ? [] : [extra]
22917
23056
  ].join(" ").toLowerCase();
22918
23057
  }
22919
- function groupHaystack(g) {
22920
- const cached2 = haystackCache.get(g);
23058
+ function typeHaystack(t) {
23059
+ const cached2 = haystackCache.get(t);
22921
23060
  if (cached2 !== void 0) return cached2;
22922
- const haystack = buildHaystack(g);
22923
- haystackCache.set(g, haystack);
23061
+ const haystack = buildHaystack(t);
23062
+ haystackCache.set(t, haystack);
22924
23063
  return haystack;
22925
23064
  }
22926
23065
  var actionsCache = /* @__PURE__ */ new WeakMap();
22927
- function groupActions(g) {
22928
- const cached2 = actionsCache.get(g);
22929
- if (cached2 !== void 0) return cached2;
22930
- const actions = [...new Set(g.instances.map((i) => i.action))];
22931
- actionsCache.set(g, actions);
22932
- return actions;
23066
+ function typeActions(t) {
23067
+ return actionsCache.get(t) ?? [];
22933
23068
  }
22934
23069
  function countInstancesByStatus(statusInputs, statuses) {
22935
23070
  const statusSet = new Set(statuses);
@@ -22940,8 +23075,8 @@ function countInstancesByStatus(statusInputs, statuses) {
22940
23075
  }
22941
23076
  return sum;
22942
23077
  }
22943
- function applyFindingFilters(groups, opts) {
22944
- let filtered = groups;
23078
+ function applyFindingFilters(types, opts) {
23079
+ let filtered = types;
22945
23080
  if (opts.severity && opts.severity.length > 0) {
22946
23081
  const sevSet = new Set(opts.severity);
22947
23082
  filtered = filtered.filter((g) => sevSet.has(g.severity));
@@ -22952,7 +23087,7 @@ function applyFindingFilters(groups, opts) {
22952
23087
  }
22953
23088
  if (opts.actions && opts.actions.length > 0) {
22954
23089
  const actionSet = new Set(opts.actions);
22955
- filtered = filtered.filter((g) => groupActions(g).some((a) => actionSet.has(a)));
23090
+ filtered = filtered.filter((t) => typeActions(t).some((a) => actionSet.has(a)));
22956
23091
  }
22957
23092
  if (opts.subtype && opts.subtype.length > 0) {
22958
23093
  const subtypeSet = new Set(opts.subtype);
@@ -22964,26 +23099,31 @@ function applyFindingFilters(groups, opts) {
22964
23099
  }
22965
23100
  if (opts.q) {
22966
23101
  const q = opts.q.toLowerCase();
22967
- filtered = filtered.filter((g) => groupHaystack(g).includes(q));
23102
+ filtered = filtered.filter((t) => typeHaystack(t).includes(q));
22968
23103
  }
22969
23104
  return filtered;
22970
23105
  }
22971
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22972
- var SEVERITY_RANK = SEVERITY_ORDER;
23106
+ function rankByOrder(members2) {
23107
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23108
+ }
23109
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23110
+ function severityRank(severity) {
23111
+ return lookupOwn(SEVERITY_RANK, severity);
23112
+ }
22973
23113
  function compareFindingGroupOrder(a, b) {
22974
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22975
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23114
+ const rankA = severityRank(a.severity) ?? -1;
23115
+ const rankB = severityRank(b.severity) ?? -1;
22976
23116
  const severityDiff = rankA - rankB;
22977
23117
  if (severityDiff !== 0) return severityDiff;
22978
23118
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
22979
23119
  if (recencyDiff !== 0) return recencyDiff;
22980
23120
  return a.id.localeCompare(b.id);
22981
23121
  }
22982
- function sortFindingGroups(groups) {
22983
- return [...groups].sort(compareFindingGroupOrder);
23122
+ function sortFindingTypes(types) {
23123
+ return [...types].sort(compareFindingGroupOrder);
22984
23124
  }
22985
- function computeFindingFacets(allGroups, opts) {
22986
- const forSeverity = applyFindingFilters(allGroups, {
23125
+ function computeFindingFacets(allTypes, opts) {
23126
+ const forSeverity = applyFindingFilters(allTypes, {
22987
23127
  providers: opts.providers,
22988
23128
  actions: opts.actions,
22989
23129
  statuses: opts.statuses,
@@ -22994,7 +23134,7 @@ function computeFindingFacets(allGroups, opts) {
22994
23134
  for (const g of forSeverity) {
22995
23135
  severityMap.set(g.severity, (severityMap.get(g.severity) ?? 0) + 1);
22996
23136
  }
22997
- const forProvider = applyFindingFilters(allGroups, {
23137
+ const forProvider = applyFindingFilters(allTypes, {
22998
23138
  actions: opts.actions,
22999
23139
  statuses: opts.statuses,
23000
23140
  q: opts.q,
@@ -23005,7 +23145,7 @@ function computeFindingFacets(allGroups, opts) {
23005
23145
  for (const g of forProvider) {
23006
23146
  for (const p of g.providers) providerMap.set(p, (providerMap.get(p) ?? 0) + 1);
23007
23147
  }
23008
- const forAction = applyFindingFilters(allGroups, {
23148
+ const forAction = applyFindingFilters(allTypes, {
23009
23149
  providers: opts.providers,
23010
23150
  statuses: opts.statuses,
23011
23151
  q: opts.q,
@@ -23014,9 +23154,9 @@ function computeFindingFacets(allGroups, opts) {
23014
23154
  });
23015
23155
  const actionMap = /* @__PURE__ */ new Map();
23016
23156
  for (const g of forAction) {
23017
- for (const a of groupActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23157
+ for (const a of typeActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23018
23158
  }
23019
- const forSubtype = applyFindingFilters(allGroups, {
23159
+ const forSubtype = applyFindingFilters(allTypes, {
23020
23160
  providers: opts.providers,
23021
23161
  actions: opts.actions,
23022
23162
  statuses: opts.statuses,
@@ -23025,7 +23165,7 @@ function computeFindingFacets(allGroups, opts) {
23025
23165
  });
23026
23166
  const subtypeMap = /* @__PURE__ */ new Map();
23027
23167
  for (const g of forSubtype) subtypeMap.set(g.subtype, (subtypeMap.get(g.subtype) ?? 0) + 1);
23028
- const forStatus = applyFindingFilters(allGroups, {
23168
+ const forStatus = applyFindingFilters(allTypes, {
23029
23169
  providers: opts.providers,
23030
23170
  actions: opts.actions,
23031
23171
  q: opts.q,
@@ -23047,6 +23187,20 @@ function computeFindingFacets(allGroups, opts) {
23047
23187
  }
23048
23188
 
23049
23189
  // ../../packages/schema/src/zod/findings-flat-build.ts
23190
+ function compareCodePoints(a, b) {
23191
+ const aIter = a[Symbol.iterator]();
23192
+ const bIter = b[Symbol.iterator]();
23193
+ for (; ; ) {
23194
+ const aNext = aIter.next();
23195
+ const bNext = bIter.next();
23196
+ if (aNext.done && bNext.done) return 0;
23197
+ if (aNext.done) return -1;
23198
+ if (bNext.done) return 1;
23199
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23200
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23201
+ if (aPoint !== bPoint) return aPoint - bPoint;
23202
+ }
23203
+ }
23050
23204
  function rowHaystack(row) {
23051
23205
  return [
23052
23206
  row.ruleId,
@@ -23071,12 +23225,24 @@ function matchesDimension(row, opts, dimension) {
23071
23225
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23072
23226
  case "statuses":
23073
23227
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23228
+ case "deliveries":
23229
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23074
23230
  case "tools":
23075
23231
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23232
+ // An EMPTY value is a real filter here, not an absent one. The location
23233
+ // list buckets a finding whose event recorded no repo — or no file — under
23234
+ // the empty string, and selecting that bucket has to narrow the panel to
23235
+ // exactly it. Only `undefined` means "no filter"; a caller that wants every
23236
+ // row omits the key, which every call site already does.
23237
+ //
23238
+ // Reading '' as unset is what this replaced, and it failed in the one place
23239
+ // it mattered: the no-repo/no-file bucket is often the largest in a real
23240
+ // store, and its panel dropped both predicates and returned the WHOLE scope
23241
+ // — a row reading 3 findings beside a panel listing every finding there is.
23076
23242
  case "repo":
23077
- return opts.repo === void 0 || opts.repo === "" || row.repo === opts.repo;
23243
+ return opts.repo === void 0 || row.repo === opts.repo;
23078
23244
  case "file":
23079
- return opts.file === void 0 || opts.file === "" || row.file === opts.file;
23245
+ return opts.file === void 0 || row.file === opts.file;
23080
23246
  case "q":
23081
23247
  return !opts.q || rowHaystack(row).includes(opts.q.toLowerCase());
23082
23248
  }
@@ -23087,6 +23253,7 @@ var DIMENSIONS = [
23087
23253
  "providers",
23088
23254
  "actions",
23089
23255
  "statuses",
23256
+ "deliveries",
23090
23257
  "tools",
23091
23258
  "repo",
23092
23259
  "file",
@@ -23100,10 +23267,19 @@ function matchesInstanceFilters(row, opts, except) {
23100
23267
  return true;
23101
23268
  }
23102
23269
  function toItems(counts) {
23103
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23270
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23271
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23272
+ // NFD spelling of the same text) as equal, so a count tie between
23273
+ // them would otherwise be ordered by whichever the Map iteration
23274
+ // produced. compareCodePoints breaks that tie deterministically, which
23275
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23276
+ // which it need not: foldFacetTuples runs this same sort over grouped
23277
+ // tuples, so both paths order facets identically by construction.
23278
+ compareCodePoints(a.value, b.value)
23279
+ );
23104
23280
  }
23105
- function bump(counts, value) {
23106
- counts.set(value, (counts.get(value) ?? 0) + 1);
23281
+ function bump(counts, value, by = 1) {
23282
+ counts.set(value, (counts.get(value) ?? 0) + by);
23107
23283
  }
23108
23284
  function createInstanceFacetAccumulator(opts) {
23109
23285
  const severity = /* @__PURE__ */ new Map();
@@ -23112,6 +23288,7 @@ function createInstanceFacetAccumulator(opts) {
23112
23288
  const action = /* @__PURE__ */ new Map();
23113
23289
  const status = /* @__PURE__ */ new Map();
23114
23290
  const tool = /* @__PURE__ */ new Map();
23291
+ const deployment = /* @__PURE__ */ new Map();
23115
23292
  return {
23116
23293
  add(row) {
23117
23294
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23126,6 +23303,9 @@ function createInstanceFacetAccumulator(opts) {
23126
23303
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23127
23304
  bump(tool, row.toolName);
23128
23305
  }
23306
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23307
+ bump(deployment, row.delivery.state);
23308
+ }
23129
23309
  },
23130
23310
  facets: () => ({
23131
23311
  severity: toItems(severity),
@@ -23133,7 +23313,8 @@ function createInstanceFacetAccumulator(opts) {
23133
23313
  provider: toItems(provider),
23134
23314
  action: toItems(action),
23135
23315
  status: toItems(status),
23136
- tool: toItems(tool)
23316
+ tool: toItems(tool),
23317
+ deployment: toItems(deployment)
23137
23318
  })
23138
23319
  };
23139
23320
  }
@@ -23147,6 +23328,7 @@ function toInstanceDetail(row) {
23147
23328
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23148
23329
  eventId: row.eventId,
23149
23330
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23331
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23150
23332
  ...row.user === void 0 ? {} : { user: row.user },
23151
23333
  action: toApiAction(row.actionTaken),
23152
23334
  detectedAt: row.occurredAt,
@@ -23161,12 +23343,6 @@ function toInstanceDetail(row) {
23161
23343
  policy: { id: `category:${category}`, name: category }
23162
23344
  };
23163
23345
  }
23164
- var SEVERITY_ORDER2 = {
23165
- critical: 0,
23166
- high: 1,
23167
- medium: 2,
23168
- low: 3
23169
- };
23170
23346
  function newLocationAccumulator() {
23171
23347
  return {
23172
23348
  instanceCount: 0,
@@ -23181,7 +23357,7 @@ function newLocationAccumulator() {
23181
23357
  }
23182
23358
  function addToLocation(acc, row) {
23183
23359
  acc.instanceCount += 1;
23184
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23360
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23185
23361
  if (rank < acc.maxSeverityRank) {
23186
23362
  acc.maxSeverityRank = rank;
23187
23363
  acc.maxSeverity = row.severity;
@@ -23190,6 +23366,23 @@ function addToLocation(acc, row) {
23190
23366
  acc.statuses.push(row.status);
23191
23367
  acc.ruleIds.add(row.ruleId);
23192
23368
  }
23369
+ function compareLocationOrder(a, b) {
23370
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23371
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23372
+ if (rankA !== rankB) return rankA - rankB;
23373
+ if (a.latestDetectedAt !== b.latestDetectedAt) {
23374
+ return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23375
+ }
23376
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23377
+ if (repoDiff !== 0) return repoDiff;
23378
+ return compareCodePoints(a.file, b.file);
23379
+ }
23380
+ function encodeLocationId(repo, file2) {
23381
+ return `${encodePart(repo)}/${encodePart(file2)}`;
23382
+ }
23383
+ function encodePart(value) {
23384
+ return encodeURIComponent(value.replace(/[\uD800-\uDFFF]/gu, "\uFFFD"));
23385
+ }
23193
23386
 
23194
23387
  // ../../packages/schema/src/zod/installed-pack.ts
23195
23388
  var InstalledPack = external_exports.object({
@@ -23257,6 +23450,11 @@ var Policy = external_exports.object({
23257
23450
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23258
23451
  provenance: PolicyProvenance.optional()
23259
23452
  }).meta({ id: "Policy" });
23453
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23454
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23455
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23456
+ id: "RedactFallback"
23457
+ });
23260
23458
  var PolicyBundle = external_exports.object({
23261
23459
  version: external_exports.string(),
23262
23460
  policies: external_exports.array(Policy),
@@ -23304,6 +23502,16 @@ var PolicyBundle = external_exports.object({
23304
23502
  // control plane), so no name resolution stands between the decision and the
23305
23503
  // comparison.
23306
23504
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23505
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23506
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23507
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23508
+ // a control plane can tighten a machine and never loosen one — the same
23509
+ // direction `mergeRaiseOnly` enforces for policies.
23510
+ //
23511
+ // Optional so an older backend, and an older on-disk cache, still parses;
23512
+ // absent leaves the device's own setting in force, which is the behaviour
23513
+ // that predates the field and the safe direction to default.
23514
+ redactFallback: RedactFallback.optional(),
23307
23515
  customKeywords: external_exports.array(external_exports.string()),
23308
23516
  fetchedAt: external_exports.iso.datetime()
23309
23517
  }).meta({ id: "PolicyBundle" });
@@ -23333,11 +23541,6 @@ function severityFloorPolicy(category) {
23333
23541
  const peak = CATEGORY_PEAK_SEVERITY[category];
23334
23542
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23335
23543
  }
23336
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23337
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23338
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23339
- id: "RedactFallback"
23340
- });
23341
23544
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23342
23545
  var BUILTIN_POLICY_SPECS = {
23343
23546
  monitor: {
@@ -23633,7 +23836,7 @@ var VaultConsent = external_exports.object({
23633
23836
  });
23634
23837
 
23635
23838
  // ../../packages/schema/src/zod/local.ts
23636
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23839
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23637
23840
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23638
23841
  var RunMode = external_exports.enum(["standalone", "attached"]);
23639
23842
  var ControlPlaneConnection = external_exports.object({
@@ -23653,6 +23856,15 @@ var HistorySyncConsent = external_exports.object({
23653
23856
  payloadVersion: external_exports.number().int().positive(),
23654
23857
  endpoint: external_exports.string()
23655
23858
  });
23859
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23860
+ var BodyRetention = external_exports.object({
23861
+ enabled: external_exports.boolean().default(false),
23862
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23863
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23864
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23865
+ // candidate set that is already bounded by "delivered, or never owed".
23866
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23867
+ }).meta({ id: "BodyRetention" });
23656
23868
  var WorkspaceSettings = external_exports.object({
23657
23869
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23658
23870
  runMode: RunMode.default("standalone"),
@@ -23696,12 +23908,18 @@ var WorkspaceSettings = external_exports.object({
23696
23908
  // covers the current payload and must be re-granted.
23697
23909
  modelJudgeConsent: ModelJudgeConsent.optional(),
23698
23910
  // Records that the user consented to the DEFERRED send — the outbox — along
23699
- // with the payload shape and the endpoint they agreed to. Since payload v2
23700
- // that covers both the pre-attach backlog and undelivered captures (which
23701
- // carry prompt/reply text in `content`); the key name predates the widening.
23702
- // Absent until granted, and a grant for a different endpoint or an older
23703
- // payload no longer counts.
23704
- historySyncConsent: HistorySyncConsent.optional()
23911
+ // with the payload shape and the endpoint they agreed to. Since payload v3
23912
+ // that covers the pre-attach backlog AND undelivered captures alike, and both
23913
+ // carry prompt/reply/tool-output text in `content`; the key name predates
23914
+ // both widenings. Absent until granted, and a grant for a different endpoint
23915
+ // or an older payload no longer counts.
23916
+ historySyncConsent: HistorySyncConsent.optional(),
23917
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23918
+ // body never removes the row or its findings.
23919
+ bodyRetention: BodyRetention.default({
23920
+ enabled: false,
23921
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23922
+ })
23705
23923
  });
23706
23924
  function defaultWorkspaceSettings() {
23707
23925
  return WorkspaceSettings.parse({});
@@ -23796,12 +24014,15 @@ function toCaptureAttributes(event) {
23796
24014
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23797
24015
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23798
24016
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24017
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23799
24018
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23800
24019
  // has ever populated either), but every legacy metadata key still rides
23801
24020
  // the bag rather than being silently dropped — CaptureAttributes'
23802
24021
  // `.catchall(z.unknown())` carries the long tail.
23803
24022
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23804
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24023
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24024
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24025
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23805
24026
  };
23806
24027
  }
23807
24028
  function captureDefinitionVersion(finding) {
@@ -23829,10 +24050,22 @@ var ManagedSettingKey = external_exports.enum([
23829
24050
  "vaultInlineReveal",
23830
24051
  "modelJudgeConsent",
23831
24052
  "dataSharesInPlace",
23832
- "redactFallback"
24053
+ "redactFallback",
24054
+ // Pins the toggle and the day count together — see BodyRetention on why the
24055
+ // two are one unit. An administrator mandating a window wants the count
24056
+ // enforced with it, not one a user can widen while the toggle stays on.
24057
+ "bodyRetention"
23833
24058
  ]).meta({ id: "ManagedSettingKey" });
24059
+ function isManagedSettingKey(value) {
24060
+ return ManagedSettingKey.safeParse(value).success;
24061
+ }
23834
24062
  var ManagedSettingsValues = external_exports.object({
23835
24063
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24064
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24065
+ // plain, non-strict objects: a key under either that this build does not know
24066
+ // is stripped and nothing reports it. The unknown-value split in
24067
+ // ManagedSettings below classifies top-level names only, so it stops at
24068
+ // these boundaries.
23836
24069
  controlPlane: external_exports.object({
23837
24070
  endpoint: external_exports.string().min(1),
23838
24071
  label: external_exports.string().min(1).optional()
@@ -23843,7 +24076,8 @@ var ManagedSettingsValues = external_exports.object({
23843
24076
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23844
24077
  modelJudgeConsent: external_exports.boolean().optional(),
23845
24078
  dataSharesInPlace: external_exports.boolean().optional(),
23846
- redactFallback: RedactFallback.optional()
24079
+ redactFallback: RedactFallback.optional(),
24080
+ bodyRetention: BodyRetention.optional()
23847
24081
  }).meta({ id: "ManagedSettingsValues" });
23848
24082
  var ManagedSettings = external_exports.object({
23849
24083
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23851,11 +24085,59 @@ var ManagedSettings = external_exports.object({
23851
24085
  // decision from a bug. Absent renders as a generic "your organization".
23852
24086
  organization: external_exports.string().min(1).optional(),
23853
24087
  // What the administrator pinned.
23854
- values: ManagedSettingsValues.default({}),
24088
+ //
24089
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24090
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24091
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24092
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24093
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24094
+ // exactly the file an administrator is most likely to write while a fleet
24095
+ // is mid-upgrade.
24096
+ //
24097
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24098
+ // file, which is the outcome the lock half already rejected — an older
24099
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24100
+ // value still fails, because the nested schema is re-run over the known
24101
+ // subset and its issues are re-raised on this parse.
24102
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23855
24103
  // Which of those the user may not change. A key here with no matching value
23856
24104
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23857
24105
  // the user may still override. The two are separable on purpose.
23858
- lockedFields: external_exports.array(ManagedSettingKey).default([])
24106
+ //
24107
+ // Parsed as NAMES rather than as the enum, and split below: a name this
24108
+ // build does not know is dropped from the locked set and reported, never a
24109
+ // reason to refuse the file. The same shape reaches an older build whenever
24110
+ // an administrator locks a key a newer build added, and refusing it there
24111
+ // ran that build entirely unmanaged — every pin and lock gone — on exactly
24112
+ // the fleets most likely to carry a version skew. A name outside the enum
24113
+ // is still never HONOURED: the lockable set stays explicit above.
24114
+ lockedFields: external_exports.array(external_exports.string()).default([])
24115
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
24116
+ const known = [];
24117
+ const unknown2 = [];
24118
+ for (const name of lockedFields) {
24119
+ if (isManagedSettingKey(name)) known.push(name);
24120
+ else unknown2.push(name);
24121
+ }
24122
+ const knownValues = /* @__PURE__ */ Object.create(null);
24123
+ const unknownValues = [];
24124
+ for (const [name, value] of Object.entries(values)) {
24125
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24126
+ else unknownValues.push(name);
24127
+ }
24128
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24129
+ if (!pinned.success) {
24130
+ for (const issue2 of pinned.error.issues)
24131
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24132
+ return external_exports.NEVER;
24133
+ }
24134
+ return {
24135
+ ...rest,
24136
+ values: pinned.data,
24137
+ lockedFields: known,
24138
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24139
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
24140
+ };
23859
24141
  }).meta({ id: "ManagedSettings" });
23860
24142
 
23861
24143
  // ../../packages/schema/src/zod/project-files.ts
@@ -23979,7 +24261,11 @@ var FindingsTimeseriesPoint = external_exports.object({
23979
24261
  timestamp: external_exports.iso.date(),
23980
24262
  critical: external_exports.number().int().nonnegative(),
23981
24263
  high: external_exports.number().int().nonnegative(),
23982
- medium: external_exports.number().int().nonnegative()
24264
+ medium: external_exports.number().int().nonnegative(),
24265
+ // Optional and additive, so a producer written against the earlier
24266
+ // three-series contract keeps validating. A consumer plotting it resolves the
24267
+ // absent case itself — the chart point requires a number.
24268
+ low: external_exports.number().int().nonnegative().optional()
23983
24269
  }).meta({ id: "FindingsTimeseriesPoint" });
23984
24270
  var FindingsTimeseriesResponse = external_exports.object({
23985
24271
  range: TimeRange,
@@ -24005,6 +24291,10 @@ var ResolvedFeedItem = external_exports.object({
24005
24291
  findingKey: external_exports.string(),
24006
24292
  ruleId: external_exports.string(),
24007
24293
  severity: Severity,
24294
+ // Repository slug, and the file path RELATIVE to it. The pair is what
24295
+ // identifies the file: a bare path matches the same name in every repo.
24296
+ // Optional and additive; empty when the event carried no repo.
24297
+ repo: external_exports.string().optional(),
24008
24298
  path: external_exports.string(),
24009
24299
  // ISO-8601 datetime (matches FindingInstance.detectedAt / the rest of the
24010
24300
  // findings domain). The reader `.toISOString()`s the DB epoch-ms values.
@@ -24110,7 +24400,23 @@ var SaveSettingsInput = external_exports.object({
24110
24400
  modelJudgeConsent: ModelJudgeConsentChoice,
24111
24401
  historySyncConsent: HistorySyncConsentChoice,
24112
24402
  vaultConsent: external_exports.string(),
24113
- vaultInlineReveal: external_exports.string()
24403
+ vaultInlineReveal: external_exports.string(),
24404
+ // Widened to `string` like its neighbours rather than typed as
24405
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24406
+ // the call site, so the domain check receives the type it was written for.
24407
+ //
24408
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24409
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24410
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24411
+ // trade against. The real cost runs the other way and is the part worth
24412
+ // knowing: a value this schema admits and the domain enum then rejects lands
24413
+ // on the action's shared refusal, which names NO field, where a shape
24414
+ // rejection reaches `malformedInput` and names the schema key.
24415
+ redactFallback: external_exports.string(),
24416
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24417
+ // `BodyRetention`'s and the action checks it there, so there is one place
24418
+ // that decides what a legal horizon is rather than two that can drift.
24419
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24114
24420
  });
24115
24421
  var AttachInput = external_exports.object({
24116
24422
  endpoint: external_exports.string(),
@@ -24282,6 +24588,52 @@ function reviewSeverityRank(reasons) {
24282
24588
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24283
24589
  }
24284
24590
 
24591
+ // ../../packages/schema/src/zod/web-capture.ts
24592
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24593
+ var WebUsage = external_exports.object({
24594
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24595
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24596
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24597
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24598
+ });
24599
+ var WebToolCall = external_exports.object({
24600
+ toolUseId: external_exports.string().min(1),
24601
+ toolName: external_exports.string().min(1),
24602
+ target: external_exports.string().optional(),
24603
+ isError: external_exports.boolean().optional(),
24604
+ inputSize: external_exports.number().int().nonnegative().optional(),
24605
+ outputSize: external_exports.number().int().nonnegative().optional()
24606
+ });
24607
+ var WebExchange = external_exports.object({
24608
+ messageId: external_exports.string().min(1),
24609
+ startedAt: external_exports.iso.datetime(),
24610
+ model: external_exports.string().optional(),
24611
+ usage: WebUsage.optional(),
24612
+ usageSource: WebUsageSource,
24613
+ stopReason: external_exports.string().optional(),
24614
+ conversationId: external_exports.string().optional(),
24615
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24616
+ toolCalls: external_exports.array(WebToolCall).default([]),
24617
+ // Absent when the adapter recovered no text. Capped by the caller at
24618
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24619
+ // short capture is never mistaken for a short reply.
24620
+ responseText: external_exports.string().optional(),
24621
+ truncated: external_exports.boolean().default(false)
24622
+ });
24623
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24624
+ var WebCaptureStatus = external_exports.object({
24625
+ patched: external_exports.boolean(),
24626
+ live: external_exports.boolean(),
24627
+ blind: external_exports.boolean(),
24628
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24629
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24630
+ parseFailures: external_exports.number().int().nonnegative(),
24631
+ unparsedBodies: external_exports.number().int().nonnegative(),
24632
+ // The adapter-declared JSON key paths that were absent from a real payload —
24633
+ // the earliest signal that a site's contract moved.
24634
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24635
+ });
24636
+
24285
24637
  // ../../packages/persistence/src/paths.ts
24286
24638
  import {
24287
24639
  chmodSync,
@@ -24612,6 +24964,22 @@ function discardStore(file2, backup) {
24612
24964
  }
24613
24965
  }
24614
24966
 
24967
+ // ../../packages/persistence/src/internal/sql-functions.ts
24968
+ var utf8 = new TextDecoder();
24969
+ function akaLower(value) {
24970
+ if (value === null) return null;
24971
+ if (typeof value === "string") return value.toLowerCase();
24972
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
24973
+ return utf8.decode(value).toLowerCase();
24974
+ }
24975
+ function registerSqlFunctions(db) {
24976
+ db.function(
24977
+ "aka_lower",
24978
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
24979
+ akaLower
24980
+ );
24981
+ }
24982
+
24615
24983
  // ../../packages/persistence/src/internal/sql-text.ts
24616
24984
  function escapeLikePattern(s) {
24617
24985
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24696,6 +25064,11 @@ function schemaObjectExists(db, kind, name) {
24696
25064
  function indexExists(db, name) {
24697
25065
  return schemaObjectExists(db, "index", name);
24698
25066
  }
25067
+ function indexColumns(db, name) {
25068
+ if (!indexExists(db, name)) return [];
25069
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25070
+ return columns.map((c) => c.name).filter((c) => c !== null);
25071
+ }
24699
25072
  function columnNames(db, table, opts) {
24700
25073
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24701
25074
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).all();
@@ -24757,146 +25130,788 @@ function mapRowsTolerant(rows, map2) {
24757
25130
  return out;
24758
25131
  }
24759
25132
 
24760
- // ../../packages/persistence/src/migrations.ts
24761
- function describeObject(object2) {
24762
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24763
- }
24764
- function splitStatements(sql) {
24765
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24766
- }
24767
- function createdIndexName(statement) {
24768
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24769
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25133
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25134
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25135
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25136
+
25137
+ // ../../packages/persistence/src/sync-failure.ts
25138
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25139
+ function syncFailureRejectCondition(column = "sync_failure") {
25140
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25141
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24770
25142
  }
24771
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24772
- function applyMigrations(db, file2) {
24773
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24774
- db.exec(
24775
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24776
- );
24777
- const applied = new Set(
24778
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24779
- );
24780
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24781
- const record2 = db.prepare(
24782
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24783
- );
24784
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24785
- if (applied.has(migration.tag)) continue;
24786
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24787
- const evidence = evidenceObjects(migration.sql);
24788
- const present = evidence.filter((o) => evidenceExists(db, o));
24789
- if (present.length > 0 && present.length < evidence.length) {
24790
- const missing = evidence.filter((o) => !present.includes(o));
24791
- const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
24792
- akaWarn(message);
24793
- throw new Error(`[aka] ${message}`);
24794
- }
24795
- const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
24796
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24797
- const statements = splitStatements(migration.sql);
24798
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24799
- try {
24800
- withTransaction(
24801
- db,
24802
- () => {
24803
- for (const statement of statements) {
24804
- const indexName = createdIndexName(statement);
24805
- if (indexName === void 0) {
24806
- if (alreadyApplied) continue;
24807
- } else if (indexExists(db, indexName)) {
24808
- continue;
24809
- }
24810
- db.exec(statement);
24811
- }
24812
- if (wantsFkOff && !alreadyApplied) {
24813
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24814
- if (violations.length > 0) {
24815
- throw new Error(
24816
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24817
- );
24818
- }
24819
- }
24820
- record2.run(migration.tag, Date.now());
24821
- },
24822
- "IMMEDIATE"
24823
- );
24824
- } finally {
24825
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24826
- }
25143
+
25144
+ // ../../packages/persistence/src/repositories/history-sync.ts
25145
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25146
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25147
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25148
+ var COUNTED_EVENT_TYPES = [
25149
+ ...STRUCTURAL_EVENT_TYPES,
25150
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25151
+ ];
25152
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25153
+ var PARTITION_BUCKETS = `
25154
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25155
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25156
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25157
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25158
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25159
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25160
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25161
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25162
+ -- added later lands in no bucket and fails the sum assertion, instead
25163
+ -- of silently joining this one.
25164
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25165
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25166
+ THEN 1 ELSE 0 END) AS failed,
25167
+ COUNT(*) AS total`;
25168
+ var COUNTED_SCOPE = `
25169
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25170
+ AND (
25171
+ event_type IN (${TYPE_LIST})
25172
+ OR synced_at IS NOT NULL
25173
+ OR outbox_owed = 1
25174
+ )`;
25175
+ var SKIPPED = -1;
25176
+ var ROW_COLUMNS = `id,
25177
+ parent_id AS parentId,
25178
+ root_session_id AS rootSessionId,
25179
+ event_type AS eventType,
25180
+ host_id AS hostId,
25181
+ harness_id AS harnessId,
25182
+ source_project_id AS sourceProjectId,
25183
+ started_at AS startedAt,
25184
+ ended_at AS endedAt,
25185
+ severity,
25186
+ priority,
25187
+ content,
25188
+ content_hash AS contentHash,
25189
+ attributes`;
25190
+ var SqliteHistorySyncRepository = class {
25191
+ constructor(db) {
25192
+ this.db = db;
25193
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25194
+ this.sessionsStmt = db.prepare(
25195
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25196
+ FROM audit_events
25197
+ WHERE synced_at IS NULL
25198
+ AND event_type IN (${TYPE_LIST})
25199
+ AND started_at < :before
25200
+ GROUP BY sessionId
25201
+ ORDER BY earliest
25202
+ LIMIT :limit`
25203
+ );
25204
+ this.rowsStmt = db.prepare(
25205
+ `SELECT ${ROW_COLUMNS}
25206
+ FROM audit_events
25207
+ WHERE synced_at IS NULL
25208
+ AND event_type IN (${TYPE_LIST})
25209
+ AND started_at < :before
25210
+ AND COALESCE(root_session_id, id) = :sessionId
25211
+ ORDER BY (event_type = 'session') DESC, started_at
25212
+ LIMIT :limit`
25213
+ );
25214
+ this.captureRowsStmt = db.prepare(
25215
+ `SELECT ${ROW_COLUMNS}
25216
+ FROM audit_events
25217
+ WHERE synced_at IS NULL
25218
+ AND sync_claimed_at IS NULL
25219
+ AND outbox_owed = 1
25220
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25221
+ AND started_at < :before
25222
+ ORDER BY started_at
25223
+ LIMIT :limit`
25224
+ );
25225
+ this.markOwedStmt = db.prepare(
25226
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25227
+ );
25228
+ this.markCaptureBacklogOwedStmt = db.prepare(
25229
+ `UPDATE audit_events SET outbox_owed = 1
25230
+ WHERE synced_at IS NULL
25231
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25232
+ AND started_at < :before`
25233
+ );
25234
+ this.stampStmt = db.prepare(
25235
+ `UPDATE audit_events
25236
+ SET synced_at = :at,
25237
+ sync_claimed_at = NULL,
25238
+ sync_failed_at = :failedAt,
25239
+ sync_failure = :failure
25240
+ WHERE id = :id`
25241
+ );
25242
+ this.claimRowStmt = db.prepare(
25243
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25244
+ );
25245
+ this.releaseRowStmt = db.prepare(
25246
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25247
+ );
25248
+ this.releaseStaleClaimsStmt = db.prepare(
25249
+ `UPDATE audit_events SET sync_claimed_at = NULL
25250
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25251
+ );
25252
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25253
+ FROM audit_events${COUNTED_SCOPE}`);
25254
+ this.partitionByKindStmt = db.prepare(
25255
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25256
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25257
+ GROUP BY event_type`
25258
+ );
25259
+ this.countsStmt = db.prepare(
25260
+ `SELECT
25261
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25262
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25263
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25264
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25265
+ THEN 1 ELSE 0 END) AS skipped,
25266
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25267
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25268
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25269
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25270
+ FROM audit_events
25271
+ WHERE event_type IN (${TYPE_LIST})`
25272
+ );
25273
+ this.captureSkipCountStmt = db.prepare(
25274
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25275
+ // way the structural totals are. The split exists because a refusal is
25276
+ // terminal only against the deployment that gave it, and the structural
25277
+ // re-arm frees it on a change of deployment. The capture lane has no such
25278
+ // escape: re-arming a capture would offer one deployment's undelivered
25279
+ // prompts, with their text, to a deployment that never saw them, which is
25280
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25281
+ // reasons mean the same thing — this row will not be sent — and splitting
25282
+ // them would put refused captures in a bucket nothing reads and nothing
25283
+ // frees.
25284
+ `SELECT COUNT(*) AS skipped
25285
+ FROM audit_events
25286
+ WHERE synced_at = ${String(SKIPPED)}
25287
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25288
+ );
25289
+ this.fingerprintStmt = db.prepare(
25290
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25291
+ FROM history_sync WHERE id = 1`
25292
+ );
25293
+ this.setFingerprintStmt = db.prepare(
25294
+ `UPDATE history_sync
25295
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25296
+ WHERE id = 1`
25297
+ );
25298
+ this.disownCapturesStmt = db.prepare(
25299
+ `UPDATE audit_events SET outbox_owed = NULL
25300
+ WHERE outbox_owed IS NOT NULL
25301
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25302
+ AND started_at < :attachedAt`
25303
+ );
25304
+ this.rearmStmt = db.prepare(
25305
+ `UPDATE audit_events
25306
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25307
+ WHERE (synced_at > 0
25308
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25309
+ AND event_type IN (${TYPE_LIST})`
25310
+ );
25311
+ this.claimStmt = db.prepare(
25312
+ `UPDATE history_sync
25313
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25314
+ WHERE id = 1
25315
+ AND (owner_pid IS NULL
25316
+ OR heartbeat_at IS NULL
25317
+ OR heartbeat_at < :staleBefore
25318
+ OR heartbeat_at > :now)`
25319
+ );
25320
+ this.heartbeatStmt = db.prepare(
25321
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25322
+ );
25323
+ this.releaseStmt = db.prepare(
25324
+ `UPDATE history_sync
25325
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25326
+ WHERE id = 1 AND owner_pid = :pid`
25327
+ );
25328
+ this.closeWindowStmt = db.prepare(
25329
+ `UPDATE audit_events
25330
+ SET synced_at = ${String(SKIPPED)},
25331
+ sync_failed_at = :at,
25332
+ sync_failure = 'detached_undelivered'
25333
+ WHERE synced_at IS NULL
25334
+ AND event_type IN (${TYPE_LIST})
25335
+ AND started_at >= :attachedAt`
25336
+ );
25337
+ this.releaseBoundaryStmt = db.prepare(
25338
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25339
+ );
25340
+ this.freezeBoundaryStmt = db.prepare(
25341
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25342
+ );
25343
+ this.leaseStmt = db.prepare(
25344
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25345
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25346
+ FROM history_sync WHERE id = 1`
25347
+ );
25348
+ this.inspectionsStmt = db.prepare(
25349
+ `SELECT d.rule_id AS ruleId,
25350
+ d.name AS ruleName,
25351
+ d.version AS ruleVersion,
25352
+ d.category AS category,
25353
+ d.severity AS severity,
25354
+ f.span_start AS spanStart,
25355
+ f.span_end AS spanEnd,
25356
+ f.masked_match AS maskedMatch,
25357
+ f.action_taken AS actionTaken,
25358
+ f.confidence AS confidence
25359
+ FROM inspection_findings f
25360
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25361
+ WHERE f.audit_event_id = :auditEventId
25362
+ ORDER BY f.span_start, f.id`
25363
+ );
24827
25364
  }
24828
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24829
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25365
+ db;
25366
+ ensureRowStmt;
25367
+ sessionsStmt;
25368
+ rowsStmt;
25369
+ stampStmt;
25370
+ countsStmt;
25371
+ fingerprintStmt;
25372
+ setFingerprintStmt;
25373
+ rearmStmt;
25374
+ claimStmt;
25375
+ heartbeatStmt;
25376
+ releaseStmt;
25377
+ leaseStmt;
25378
+ inspectionsStmt;
25379
+ closeWindowStmt;
25380
+ releaseBoundaryStmt;
25381
+ freezeBoundaryStmt;
25382
+ captureRowsStmt;
25383
+ markOwedStmt;
25384
+ markCaptureBacklogOwedStmt;
25385
+ captureSkipCountStmt;
25386
+ disownCapturesStmt;
25387
+ partitionStmt;
25388
+ partitionByKindStmt;
25389
+ claimRowStmt;
25390
+ releaseRowStmt;
25391
+ releaseStaleClaimsStmt;
25392
+ /**
25393
+ * The masked detections recorded against one tool call.
25394
+ *
25395
+ * These travel with the event because a tool call's target is not
25396
+ * re-inspectable from the event alone — unlike a capture, where the text
25397
+ * itself is re-scannable. What crosses is the masked match and the rule that
25398
+ * produced it, never the value.
25399
+ */
25400
+ inspectionsFor(auditEventId) {
25401
+ return allRows(this.inspectionsStmt, { auditEventId });
24830
25402
  }
24831
- ensureSyncedAtColumn(db, "audit_events");
24832
- ensureScanLedgerTable(db);
24833
- ensureHistorySyncTable(db);
24834
- ensureBlockedDetectionsTable(db);
24835
- ensureRuleProbeCacheTable(db);
24836
- ensureWriteGateTrigger(db);
24837
- ensureTokenUsageColumns(db);
24838
- reconcileSourceProjectIds(db);
24839
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24840
- const drained = runLegacyHistoryBackfill(db);
24841
- if (drained) applyLegacyDropMigration(db, file2);
25403
+ /**
25404
+ * Sessions with structural rows still to send, oldest first.
25405
+ *
25406
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25407
+ * read. Anything recorded after the machine attached is the live forward
25408
+ * path's to deliver; this drain exists for what was recorded before it, and a
25409
+ * row both paths send is at best a duplicate request and at worst — for a
25410
+ * session root — an overwrite of the inventory ids the live path resolved.
25411
+ */
25412
+ pendingSessions(limit, before) {
25413
+ return allRows(this.sessionsStmt, { limit, before }).map(
25414
+ (r) => r.sessionId
25415
+ );
24842
25416
  }
24843
- }
24844
- function readLegacyTables(db) {
24845
- let holdsRows = false;
24846
- const marks = [];
24847
- for (const table of ["events", "findings"]) {
24848
- try {
24849
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
24850
- if (row === void 0) {
24851
- holdsRows = true;
24852
- marks.push(`${table}:unreadable`);
24853
- continue;
24854
- }
24855
- if (row.n > 0) holdsRows = true;
24856
- marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
24857
- } catch {
24858
- holdsRows = true;
24859
- marks.push(`${table}:unreadable`);
24860
- }
25417
+ /** One session's undelivered structural rows within the backlog, root first. */
25418
+ pendingRows(sessionId, limit, before) {
25419
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24861
25420
  }
24862
- return { holdsRows, mark: marks.join("|") };
24863
- }
24864
- function applyLegacyDropMigration(db, file2) {
24865
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24866
- if (!migration) return;
24867
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24868
- if (file2 !== void 0 && before?.holdsRows === true) {
24869
- try {
24870
- backupBeforeLegacyDrop(db, file2);
24871
- } catch (error61) {
24872
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24873
- return;
24874
- }
25421
+ /**
25422
+ * Captures this machine still owes the deployment, oldest first.
25423
+ *
25424
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25425
+ * by a time window — see captureRowsStmt for why a window could not express
25426
+ * this. `before` is the grace window that leaves a just-recorded capture to
25427
+ * the live path.
25428
+ */
25429
+ pendingCaptureRows(limit, before) {
25430
+ return allRows(this.captureRowsStmt, { limit, before });
24875
25431
  }
24876
- try {
25432
+ /**
25433
+ * Record that a capture is OWED to the deployment.
25434
+ *
25435
+ * Written by the attached forward path when a live send did not confirm
25436
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25437
+ * a fact rather than an inference: the machine was attached, the send did not
25438
+ * land, so the row is owed — which no time window can state, because the same
25439
+ * window that holds the rows a past attachment left owed also holds every
25440
+ * capture recorded while the machine was DETACHED, and those were never
25441
+ * offered to anyone.
25442
+ *
25443
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25444
+ * out of the drain's read.
25445
+ */
25446
+ markCaptureOwed(id) {
25447
+ this.markOwedStmt.run({ id });
25448
+ }
25449
+ /**
25450
+ * Mark every capture already on disk as owed, as of `before`.
25451
+ *
25452
+ * The consent-time backfill, called once from `aka attach` when a human
25453
+ * grants existing-history consent — never from an ongoing drain pass, and
25454
+ * never inferred from a boundary that could later move. `before` is the
25455
+ * caller's own "now" at the moment consent was granted, so what this marks
25456
+ * is exactly the backlog the consent prompt already counted, not whatever a
25457
+ * later re-attach or key rotation might widen it to.
25458
+ *
25459
+ * Returns how many rows matched, for the caller to log or test against. Not a
25460
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25461
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25462
+ */
25463
+ markCaptureBacklogOwed(before) {
25464
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25465
+ }
25466
+ /**
25467
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25468
+ *
25469
+ * CLEARS any failure reason in the same statement. A row that failed against
25470
+ * one deployment and then landed is delivered, and leaving the reason behind
25471
+ * would leave the store holding two contradictory answers about one row —
25472
+ * with the surface free to render either.
25473
+ */
25474
+ markSynced(ids, atMs) {
25475
+ this.stampAll(ids, atMs, null);
25476
+ }
25477
+ /**
25478
+ * Record that THIS MACHINE cannot express the row on the wire.
25479
+ *
25480
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25481
+ * payload, or a body the client itself refused to send. It fails identically
25482
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25483
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25484
+ * is retried; marking those would turn one outage into permanent data loss.
25485
+ */
25486
+ markSkipped(ids, atMs) {
25487
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25488
+ }
25489
+ /**
25490
+ * Record that THIS DEPLOYMENT refused the row.
25491
+ *
25492
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25493
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25494
+ * row is outstanding rather than why. What separates them is the reason, and
25495
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25496
+ * on one body, so it is terminal only for as long as this machine points at
25497
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25498
+ *
25499
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25500
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25501
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25502
+ */
25503
+ markRefused(ids, atMs) {
25504
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25505
+ }
25506
+ eachInTransaction(ids, run) {
25507
+ if (ids.length === 0) return;
24877
25508
  withTransaction(
24878
- db,
25509
+ this.db,
24879
25510
  () => {
24880
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24881
- if (alreadyDropped) return;
24882
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24883
- akaWarn(
24884
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24885
- );
24886
- return;
24887
- }
24888
- for (const statement of splitStatements(migration.sql)) {
24889
- db.exec(statement);
24890
- }
24891
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24892
- migration.tag,
24893
- Date.now()
24894
- );
25511
+ for (const id of ids) run(id);
24895
25512
  },
24896
25513
  "IMMEDIATE"
24897
25514
  );
24898
- } catch (error61) {
24899
- akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
25515
+ }
25516
+ stampAll(ids, value, failure, failedAtMs) {
25517
+ if (ids.length === 0) return;
25518
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25519
+ withTransaction(
25520
+ this.db,
25521
+ () => {
25522
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25523
+ },
25524
+ "IMMEDIATE"
25525
+ );
25526
+ }
25527
+ /**
25528
+ * Claim rows as in-flight.
25529
+ *
25530
+ * Advisory in exactly the sense the lease is: it records that a send is in
25531
+ * progress so a surface can say so, and a lost claim costs a row showing as
25532
+ * queued while it is actually being sent. It is not exclusion — the far side
25533
+ * settles a duplicate on the row id.
25534
+ */
25535
+ claimRows(ids, atMs) {
25536
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25537
+ }
25538
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25539
+ releaseRows(ids) {
25540
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25541
+ }
25542
+ /**
25543
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25544
+ *
25545
+ * A process killed between claiming and settling leaves rows claimed with
25546
+ * nothing left to settle them. Without this they read as "sending" for ever.
25547
+ */
25548
+ releaseStaleClaims(staleBefore) {
25549
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25550
+ }
25551
+ /**
25552
+ * Every tracked row in exactly one delivery state.
25553
+ *
25554
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25555
+ * pick up now", which is a different question from "what state is this row
25556
+ * in" — and a machine that has never attached has no boundary to pass, so
25557
+ * requiring one would force a caller to invent one and report the whole store
25558
+ * as queued.
25559
+ */
25560
+ /**
25561
+ * The same partition, one row per kind that a lane carries.
25562
+ *
25563
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25564
+ * scope decides which rows exist at all, so a kind that has never been
25565
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25566
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25567
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25568
+ * different things.
25569
+ */
25570
+ partitionByKind() {
25571
+ return allRows(
25572
+ this.partitionByKindStmt,
25573
+ {}
25574
+ ).map((row) => ({
25575
+ kind: row.kind,
25576
+ queued: row.queued ?? 0,
25577
+ inProgress: row.inProgress ?? 0,
25578
+ synced: row.synced ?? 0,
25579
+ failed: row.failed ?? 0,
25580
+ refused: row.refused ?? 0,
25581
+ detached: row.detached ?? 0,
25582
+ total: row.total ?? 0
25583
+ }));
25584
+ }
25585
+ partition() {
25586
+ const row = getRow(this.partitionStmt, {});
25587
+ return {
25588
+ queued: row?.queued ?? 0,
25589
+ inProgress: row?.inProgress ?? 0,
25590
+ synced: row?.synced ?? 0,
25591
+ failed: row?.failed ?? 0,
25592
+ refused: row?.refused ?? 0,
25593
+ detached: row?.detached ?? 0,
25594
+ total: row?.total ?? 0
25595
+ };
25596
+ }
25597
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25598
+ counts(before) {
25599
+ const row = getRow(this.countsStmt, { before });
25600
+ const captures = getRow(this.captureSkipCountStmt);
25601
+ return {
25602
+ pending: row?.pending ?? 0,
25603
+ sent: row?.sent ?? 0,
25604
+ skipped: row?.skipped ?? 0,
25605
+ refused: row?.refused ?? 0,
25606
+ detached: row?.detached ?? 0,
25607
+ capturesSkipped: captures?.skipped ?? 0
25608
+ };
25609
+ }
25610
+ /**
25611
+ * The deployment the current stamps were made against, and where its backlog
25612
+ * ends.
25613
+ *
25614
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25615
+ * machine that has never drained is — and every writer below seeds the row
25616
+ * before it needs one, so nothing depends on this creating it. Keeping the
25617
+ * write off the gate path matters because the gate runs on every pass while a
25618
+ * write has to take the database's write lock.
25619
+ */
25620
+ deployment() {
25621
+ const row = getRow(
25622
+ this.fingerprintStmt
25623
+ );
25624
+ return {
25625
+ fingerprint: row?.fingerprint ?? void 0,
25626
+ backlogBefore: row?.backlogBefore ?? void 0
25627
+ };
25628
+ }
25629
+ /**
25630
+ * Point the ledger at a different deployment, discarding what it recorded
25631
+ * about the previous one.
25632
+ *
25633
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25634
+ * machine has just left are undelivered as far as the new one is concerned.
25635
+ * All four in one transaction, so a crash between them cannot leave stamps
25636
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25637
+ * a disown with no re-mark to follow it.
25638
+ *
25639
+ * The boundary is written HERE and only here, which is what freezes it: a
25640
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25641
+ * unchanged, so this never runs and the backlog does not widen back over rows
25642
+ * the live path has since delivered.
25643
+ *
25644
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25645
+ * granted existing-history consent for the deployment this call is arming —
25646
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25647
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25648
+ * apart. Passed only when that grant is valid, since this method has no way
25649
+ * to check consent itself and must not mark a row owed for a machine that
25650
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25651
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25652
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25653
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25654
+ * on the cleared side of that bound — and the re-mark in the same
25655
+ * transaction is what puts those rows back. A crash between the two cannot
25656
+ * strand the ledger disowned with nothing re-marked — the transaction either
25657
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25658
+ * committed re-enters this method on the very next pass. Omit it (the
25659
+ * structural-only tests do) to exercise the disown in isolation.
25660
+ *
25661
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25662
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25663
+ * live path can mark a capture owed from the moment `aka attach` writes the
25664
+ * descriptor, before the drain's first pass ever reaches this method, and
25665
+ * such a row sits at or after the bound rather than below it. What keeps the
25666
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25667
+ * bound — disown runs first, re-mark second, both inside the one
25668
+ * transaction above.
25669
+ */
25670
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25671
+ this.ensureRowStmt.run();
25672
+ withTransaction(
25673
+ this.db,
25674
+ () => {
25675
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25676
+ this.rearmStmt.run();
25677
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25678
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25679
+ }
25680
+ if (backfillCapturesBefore !== void 0) {
25681
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25682
+ }
25683
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25684
+ },
25685
+ "IMMEDIATE"
25686
+ );
25687
+ }
25688
+ /**
25689
+ * End the attached period: hand its rows to the live path, and release the
25690
+ * boundary so the next attachment can freeze a new one.
25691
+ *
25692
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25693
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25694
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25695
+ * during the detached period, because the machine is not attached. Rows
25696
+ * recorded in that window sit after the boundary and before the re-attach, so
25697
+ * neither path takes them, and the pending count reports none outstanding.
25698
+ *
25699
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25700
+ * closing attachment's to deliver and are no longer outstanding — that is what
25701
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25702
+ * distinction is not academic: this used to write a delivery TIME, which every
25703
+ * read treats as delivery, so one detach turned a window of undelivered rows
25704
+ * into a window of delivered ones and no surface could tell. It writes the
25705
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25706
+ * "received" stop being the same fact.
25707
+ *
25708
+ * A change of deployment still frees them (see the re-arm), because the next
25709
+ * deployment has seen none of this machine's history — so the rows reach it
25710
+ * exactly as they did when this wrote a delivery time.
25711
+ *
25712
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25713
+ * window unstamped — that half-state would re-send the whole attached period
25714
+ * on the next attach, which is the failure the boundary exists to prevent.
25715
+ */
25716
+ closeAttachedWindow(attachedAtMs, atMs) {
25717
+ this.ensureRowStmt.run();
25718
+ withTransaction(
25719
+ this.db,
25720
+ () => {
25721
+ const row = getRow(this.fingerprintStmt);
25722
+ const from = row?.backlogBefore ?? attachedAtMs;
25723
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25724
+ this.releaseBoundaryStmt.run();
25725
+ },
25726
+ "IMMEDIATE"
25727
+ );
25728
+ }
25729
+ /**
25730
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25731
+ *
25732
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25733
+ * different deployment and therefore discards what was delivered to the old
25734
+ * one: here the recipient is the same, so everything already sent to it stays
25735
+ * sent.
25736
+ */
25737
+ freezeBoundary(backlogBefore) {
25738
+ this.ensureRowStmt.run();
25739
+ this.freezeBoundaryStmt.run({ backlogBefore });
25740
+ }
25741
+ /** Take the claim, or report that someone live already holds it. */
25742
+ claim(pid, host, nowMs, staleAfterMs) {
25743
+ this.ensureRowStmt.run();
25744
+ let taken = false;
25745
+ withTransaction(
25746
+ this.db,
25747
+ () => {
25748
+ const result = this.claimStmt.run({
25749
+ pid,
25750
+ host,
25751
+ now: nowMs,
25752
+ staleBefore: nowMs - staleAfterMs
25753
+ });
25754
+ taken = result.changes === 1;
25755
+ },
25756
+ "IMMEDIATE"
25757
+ );
25758
+ return taken;
25759
+ }
25760
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25761
+ heartbeat(pid, nowMs) {
25762
+ this.heartbeatStmt.run({ now: nowMs, pid });
25763
+ }
25764
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25765
+ release(pid) {
25766
+ this.releaseStmt.run({ pid });
25767
+ }
25768
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25769
+ lease() {
25770
+ return getRow(this.leaseStmt);
25771
+ }
25772
+ };
25773
+
25774
+ // ../../packages/persistence/src/migrations.ts
25775
+ function describeObject(object2) {
25776
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25777
+ }
25778
+ function splitStatements(sql) {
25779
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25780
+ }
25781
+ function createdIndexName(statement) {
25782
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25783
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25784
+ }
25785
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25786
+ function applyMigrations(db, file2, options = {}) {
25787
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25788
+ db.exec(
25789
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25790
+ );
25791
+ const applied = new Set(
25792
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25793
+ );
25794
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25795
+ const record2 = db.prepare(
25796
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25797
+ );
25798
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25799
+ if (applied.has(migration.tag)) continue;
25800
+ if (options.skipTags?.has(migration.tag) === true) continue;
25801
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25802
+ const evidence = evidenceObjects(migration.sql);
25803
+ const present = evidence.filter((o) => evidenceExists(db, o));
25804
+ if (present.length > 0 && present.length < evidence.length) {
25805
+ const missing = evidence.filter((o) => !present.includes(o));
25806
+ const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
25807
+ akaWarn(message);
25808
+ throw new Error(`[aka] ${message}`);
25809
+ }
25810
+ const alreadyApplied = evidence.length > 0 ? present.length === evidence.length : preLedgerStore && index < legacyCount;
25811
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25812
+ const statements = splitStatements(migration.sql);
25813
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25814
+ try {
25815
+ withTransaction(
25816
+ db,
25817
+ () => {
25818
+ for (const statement of statements) {
25819
+ const indexName = createdIndexName(statement);
25820
+ if (indexName === void 0) {
25821
+ if (alreadyApplied) continue;
25822
+ } else if (indexExists(db, indexName)) {
25823
+ continue;
25824
+ }
25825
+ db.exec(statement);
25826
+ }
25827
+ if (wantsFkOff && !alreadyApplied) {
25828
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25829
+ if (violations.length > 0) {
25830
+ throw new Error(
25831
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25832
+ );
25833
+ }
25834
+ }
25835
+ record2.run(migration.tag, Date.now());
25836
+ },
25837
+ "IMMEDIATE"
25838
+ );
25839
+ } finally {
25840
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25841
+ }
25842
+ }
25843
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25844
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25845
+ }
25846
+ ensureSyncedAtColumn(db, "audit_events");
25847
+ ensureScanLedgerTable(db);
25848
+ ensureHistorySyncTable(db);
25849
+ ensureBlockedDetectionsTable(db);
25850
+ ensureRuleProbeCacheTable(db);
25851
+ ensureWriteGateTrigger(db);
25852
+ ensureTokenUsageColumns(db);
25853
+ reconcileSourceProjectIds(db);
25854
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25855
+ const drained = runLegacyHistoryBackfill(db);
25856
+ if (drained) applyLegacyDropMigration(db, file2);
25857
+ }
25858
+ }
25859
+ function readLegacyTables(db) {
25860
+ let holdsRows = false;
25861
+ const marks = [];
25862
+ for (const table of ["events", "findings"]) {
25863
+ try {
25864
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table}`).get();
25865
+ if (row === void 0) {
25866
+ holdsRows = true;
25867
+ marks.push(`${table}:unreadable`);
25868
+ continue;
25869
+ }
25870
+ if (row.n > 0) holdsRows = true;
25871
+ marks.push(`${table}:${String(row.n)}:${String(row.hi)}`);
25872
+ } catch {
25873
+ holdsRows = true;
25874
+ marks.push(`${table}:unreadable`);
25875
+ }
25876
+ }
25877
+ return { holdsRows, mark: marks.join("|") };
25878
+ }
25879
+ function applyLegacyDropMigration(db, file2) {
25880
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25881
+ if (!migration) return;
25882
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25883
+ if (file2 !== void 0 && before?.holdsRows === true) {
25884
+ try {
25885
+ backupBeforeLegacyDrop(db, file2);
25886
+ } catch (error61) {
25887
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25888
+ return;
25889
+ }
25890
+ }
25891
+ try {
25892
+ withTransaction(
25893
+ db,
25894
+ () => {
25895
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25896
+ if (alreadyDropped) return;
25897
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25898
+ akaWarn(
25899
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25900
+ );
25901
+ return;
25902
+ }
25903
+ for (const statement of splitStatements(migration.sql)) {
25904
+ db.exec(statement);
25905
+ }
25906
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25907
+ migration.tag,
25908
+ Date.now()
25909
+ );
25910
+ },
25911
+ "IMMEDIATE"
25912
+ );
25913
+ } catch (error61) {
25914
+ akaWarn(`legacy events/findings drop failed; deferring: ${String(error61)}`);
24900
25915
  }
24901
25916
  }
24902
25917
  function backupBeforeLegacyDrop(db, file2) {
@@ -25189,10 +26204,62 @@ function ensureSyncedAtColumn(db, table) {
25189
26204
  if (!columns.includes("outbox_owed")) {
25190
26205
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25191
26206
  }
26207
+ if (!columns.includes("sync_failed_at")) {
26208
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26209
+ }
26210
+ if (!columns.includes("sync_failure")) {
26211
+ withTransaction(
26212
+ db,
26213
+ () => {
26214
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26215
+ db.exec(
26216
+ `UPDATE ${table} SET synced_at = NULL
26217
+ WHERE synced_at = -1
26218
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26219
+ );
26220
+ },
26221
+ "IMMEDIATE"
26222
+ );
26223
+ }
25192
26224
  db.exec(
25193
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25194
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26225
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26226
+ BEFORE UPDATE OF sync_failure ON ${table}
26227
+ WHEN ${syncFailureRejectCondition()}
26228
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25195
26229
  );
26230
+ const syncIndexColumns = [
26231
+ "event_type",
26232
+ "synced_at",
26233
+ "sync_claimed_at",
26234
+ "started_at",
26235
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26236
+ // has to be in the index for the read to stay covered — but putting it
26237
+ // ahead of `started_at` would reorder the prefix the structural drain's
26238
+ // reads match on.
26239
+ "sync_failure"
26240
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26241
+ //
26242
+ // The delivery-state read tests it — a capture's state depends on whether a
26243
+ // live forward marked it owed — so carrying it here makes that read covering
26244
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26245
+ // But a sixth column changes what the planner charges for this index, and
26246
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26247
+ // then stops choosing the per-session index for the token rollup and walks
26248
+ // every `llm_call` in the store through the event-type index instead. That
26249
+ // read grows with the store; this one does not.
26250
+ //
26251
+ // 40 ms on the largest store measured, once per render, is a cost worth
26252
+ // paying to leave every other read's plan where it was.
26253
+ ];
26254
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26255
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26256
+ if (!syncIndexMatches) {
26257
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26258
+ db.exec(
26259
+ `CREATE INDEX idx_audit_events_sync
26260
+ ON audit_events (${syncIndexColumns.join(", ")})`
26261
+ );
26262
+ }
25196
26263
  db.exec(
25197
26264
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25198
26265
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25414,7 +26481,11 @@ function buildAuditEvent(row) {
25414
26481
  link: linkParsed?.success ? linkParsed.data : null,
25415
26482
  targetId: row.target_id,
25416
26483
  internal: intToBool(row.internal),
25417
- flagged: intToBool(row.flagged)
26484
+ flagged: intToBool(row.flagged),
26485
+ // Only meaningful when the title came out empty — a row whose body was
26486
+ // expired but whose title fell back to `tool_name` still has something to
26487
+ // render, and flagging it would make the view apologise for nothing.
26488
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25418
26489
  };
25419
26490
  }
25420
26491
  var TIMELINE_COLUMNS = `
@@ -25422,6 +26493,7 @@ var TIMELINE_COLUMNS = `
25422
26493
  event_type,
25423
26494
  started_at,
25424
26495
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26496
+ content_expired_at,
25425
26497
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25426
26498
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25427
26499
  json_extract(attributes, '$.severity') AS severity,
@@ -25548,7 +26620,8 @@ var SqliteActivityRepository = class {
25548
26620
  SELECT 1 FROM audit_events d
25549
26621
  WHERE d.root_session_id = audit_events.id
25550
26622
  AND (d.content LIKE ? ESCAPE '\\'
25551
- OR json_extract(d.attributes, '$.detail') LIKE ? ESCAPE '\\')))`
26623
+ OR coalesce(json_extract(d.attributes, '$.detail'),
26624
+ json_extract(d.attributes, '$.target')) LIKE ? ESCAPE '\\')))`
25552
26625
  );
25553
26626
  params.push(pattern, pattern, pattern, pattern, pattern, pattern);
25554
26627
  }
@@ -26086,6 +27159,88 @@ var SqliteAuditEventsRepository = class {
26086
27159
  }
26087
27160
  };
26088
27161
 
27162
+ // ../../packages/persistence/src/repositories/body-retention.ts
27163
+ var DEFAULT_BATCH_SIZE = 500;
27164
+ var DEFAULT_MAX_ROWS = 5e4;
27165
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27166
+ var SqliteBodyRetentionRepository = class {
27167
+ constructor(db) {
27168
+ this.db = db;
27169
+ const select = (laneClause) => `
27170
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27171
+ FROM audit_events
27172
+ WHERE content IS NOT NULL
27173
+ AND started_at < :cutoff
27174
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27175
+ ${laneClause}
27176
+ ORDER BY started_at
27177
+ LIMIT :limit`;
27178
+ this.candidatesStmt = this.db.prepare(select(""));
27179
+ this.candidatesSyncSafeStmt = this.db.prepare(
27180
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27181
+ );
27182
+ this.heldBySyncStmt = this.db.prepare(`
27183
+ SELECT COUNT(*) AS n
27184
+ FROM audit_events
27185
+ WHERE content IS NOT NULL
27186
+ AND started_at < :cutoff
27187
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27188
+ AND synced_at IS NULL`);
27189
+ this.expireStmt = this.db.prepare(
27190
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27191
+ );
27192
+ }
27193
+ db;
27194
+ candidatesStmt;
27195
+ candidatesSyncSafeStmt;
27196
+ heldBySyncStmt;
27197
+ expireStmt;
27198
+ /** How many bytes a pass with these options would free, changing nothing. */
27199
+ preview(opts) {
27200
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27201
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27202
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27203
+ return {
27204
+ rowsExpired: rows.length,
27205
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27206
+ rowsHeldBySync: this.countHeldBySync(opts)
27207
+ };
27208
+ }
27209
+ /** Clear eligible bodies, in bounded batches. */
27210
+ expire(opts) {
27211
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27212
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27213
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27214
+ let rowsExpired = 0;
27215
+ let bytesFreed = 0;
27216
+ let done = true;
27217
+ while (rowsExpired < maxRows) {
27218
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27219
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27220
+ if (batch.length === 0) break;
27221
+ withTransaction(
27222
+ this.db,
27223
+ () => {
27224
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27225
+ },
27226
+ "IMMEDIATE"
27227
+ );
27228
+ rowsExpired += batch.length;
27229
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27230
+ if (batch.length < remaining) break;
27231
+ if (rowsExpired >= maxRows) {
27232
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27233
+ }
27234
+ }
27235
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27236
+ }
27237
+ countHeldBySync(opts) {
27238
+ if (opts.sweepSyncLane) return 0;
27239
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27240
+ return row.n;
27241
+ }
27242
+ };
27243
+
26089
27244
  // ../../packages/persistence/src/repositories/classified-data.ts
26090
27245
  var SqliteClassifiedDataRepository = class {
26091
27246
  constructor(db) {
@@ -26886,23 +28041,6 @@ var LATEST_RESOLUTION_BY_KEY_SQL = `(
26886
28041
  )`;
26887
28042
 
26888
28043
  // ../../packages/persistence/src/repositories/findings.ts
26889
- var PREVIEW_INSTANCES_PER_GROUP = 200;
26890
- var DEFAULT_LOCATIONS_LIMIT = 100;
26891
- var LOCATION_RULE_IDS_CAP = 20;
26892
- function compareLocationOrder(a, b) {
26893
- return compareFindingGroupOrder(
26894
- {
26895
- severity: a.maxSeverity,
26896
- latestDetectedAt: a.latestDetectedAt,
26897
- id: ""
26898
- },
26899
- {
26900
- severity: b.maxSeverity,
26901
- latestDetectedAt: b.latestDetectedAt,
26902
- id: ""
26903
- }
26904
- );
26905
- }
26906
28044
  var CONCAT_SEP = ",";
26907
28045
  var TUPLE_SEP = "|";
26908
28046
  function splitConcat(value) {
@@ -26931,7 +28069,15 @@ function toFlatFindingRow(r) {
26931
28069
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26932
28070
  eventId: r.event_id,
26933
28071
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26934
- status: deriveInstanceStatus(r)
28072
+ status: deriveInstanceStatus(r),
28073
+ delivery: deriveFindingDelivery({
28074
+ kind: r.kind,
28075
+ syncedAt: r.synced_at,
28076
+ syncClaimedAt: r.sync_claimed_at,
28077
+ syncFailedAt: r.sync_failed_at,
28078
+ syncFailure: r.sync_failure,
28079
+ outboxOwed: r.outbox_owed
28080
+ })
26935
28081
  };
26936
28082
  }
26937
28083
  function encodeGroupCursor(group) {
@@ -26954,13 +28100,51 @@ function decodeGroupCursor(cursor) {
26954
28100
  return null;
26955
28101
  }
26956
28102
  function firstAfter(sorted, cursor) {
26957
- const index = sorted.findIndex((g) => compareFindingGroupOrder(g, cursor) > 0);
28103
+ const index = sorted.findIndex((t) => compareFindingGroupOrder(t, cursor) > 0);
26958
28104
  return index === -1 ? sorted.length : index;
26959
28105
  }
26960
28106
  function findDeepLinked(sorted, page, id) {
26961
- if (page.some((g) => g.id === id || g.instances.some((i) => i.id === id))) return void 0;
26962
- return sorted.find((g) => g.id === id || g.instances.some((i) => i.id === id));
28107
+ if (page.some((t) => t.id === id)) return void 0;
28108
+ return sorted.find((t) => t.id === id);
28109
+ }
28110
+ function encodeLocationCursor(location) {
28111
+ const payload = {
28112
+ sev: location.maxSeverity,
28113
+ t: location.latestDetectedAt,
28114
+ r: location.repo,
28115
+ f: location.file
28116
+ };
28117
+ return Buffer.from(JSON.stringify(payload)).toString("base64url");
28118
+ }
28119
+ function decodeLocationCursor(cursor) {
28120
+ const parsed2 = parseJsonObject(Buffer.from(cursor, "base64url").toString("utf8"));
28121
+ if (parsed2 !== void 0 && typeof parsed2.sev === "string" && typeof parsed2.t === "string" && typeof parsed2.r === "string" && typeof parsed2.f === "string") {
28122
+ return { maxSeverity: parsed2.sev, latestDetectedAt: parsed2.t, repo: parsed2.r, file: parsed2.f };
28123
+ }
28124
+ return null;
28125
+ }
28126
+ function firstLocationAfter(sorted, cursor) {
28127
+ const index = sorted.findIndex((l) => compareLocationOrder(l, cursor) > 0);
28128
+ return index === -1 ? sorted.length : index;
28129
+ }
28130
+ function findDeepLinkedLocation(sorted, page, id) {
28131
+ if (page.some((l) => l.id === id)) return void 0;
28132
+ return sorted.find((l) => l.id === id);
26963
28133
  }
28134
+ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS category,
28135
+ d.severity AS severity, f.masked_match AS masked_match,
28136
+ f.action_taken AS action_taken, f.confidence AS confidence,
28137
+ e.started_at AS occurred_at,
28138
+ e.source_tool AS source_tool,
28139
+ e.repo AS repo,
28140
+ e.file_path AS file,
28141
+ e.tool_name AS tool_name,
28142
+ f.audit_event_id AS event_id, e.root_session_id AS session_id,
28143
+ e.event_type AS kind, f.finding_key AS finding_key,
28144
+ ${latestResolutionStatusSql("f")} AS latest_status,
28145
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28146
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28147
+ e.outbox_owed AS outbox_owed`;
26964
28148
  var DAY_MS3 = 864e5;
26965
28149
  var SqliteFindingsRepository = class {
26966
28150
  constructor(db) {
@@ -27081,30 +28265,26 @@ var SqliteFindingsRepository = class {
27081
28265
  );
27082
28266
  }
27083
28267
  /**
27084
- * Grouped findings for the dashboard — joins inspection_findings⋈audit_events
27085
- * ⋈inspection_definitions (repo/file/toolName from the audit event's
27086
- * attributes bag, rule_id/category/severity from the definition), scoped to
27087
- * the four capture kinds (audit_events also holds structural/reconciler/scan
27088
- * rows this list must never surface), groups by ruleId, computes
27089
- * per-filter-excluded facets, applies the requested filters, and sorts by
27090
- * severity then recency. Filtering
27091
- * and faceting run in JS via the shared @akasecurity/schema helpers. `totals`
27092
- * reflect the full filtered set; `items` is the requested
27093
- * page (default 50); no cursor (nextCursor is always null). Under a `status`
27094
- * filter, `totals.findings` counts only instances whose derived status was
27095
- * requested, and each item's instance preview is narrowed the same way.
28268
+ * Finding TYPES for the dashboard — one row per rule, scoped to the four
28269
+ * capture kinds (audit_events also holds structural/reconciler/scan rows this
28270
+ * list must never surface), with per-filter-excluded facets, the requested
28271
+ * filters applied, and sorted by severity then recency. Filtering and faceting
28272
+ * run in JS via the shared @akasecurity/schema helpers. `totals` reflect the
28273
+ * full filtered set; `items` is the requested page (default 50), keyset-paged.
28274
+ * Under a `status` filter, `totals.findings` counts only findings whose
28275
+ * derived status was requested.
28276
+ *
28277
+ * ONE read, which materializes no findings: a single aggregate per rule_id,
28278
+ * folding EVERY finding into the numbers a type row and the filters need
28279
+ * (count, severity, category, providers, actions, statuses, latest, search
28280
+ * text). The findings OF a type come from listFindingInstances scoped to
28281
+ * `subtype`, so neither list bounds the other and no per-type cap exists.
27096
28282
  *
27097
- * Two reads, neither of which materializes a row per finding:
27098
- * 1. one aggregate row per rule_id, folding EVERY instance into the numbers
27099
- * the group and the filters need (count, providers, actions, statuses,
27100
- * latest, search text);
27101
- * 2. each group's newest PREVIEW_INSTANCES_PER_GROUP instances, which
27102
- * populate `instances` for the table's expanded rows.
27103
28283
  * The aggregates carry raw DB values and are translated by the same
27104
- * @akasecurity/schema mappers the row path uses, so no enum mapping or status
27105
- * rule is ever restated in SQL.
28284
+ * @akasecurity/schema mappers every other path uses, so no enum mapping or
28285
+ * status rule is ever restated in SQL.
27106
28286
  */
27107
- listGroupedFindings(query) {
28287
+ listFindingTypes(query) {
27108
28288
  const sessionPredicate = query.sessionId ? ` AND e.root_session_id = :sessionId` : "";
27109
28289
  const fromMs = query.from === void 0 ? void 0 : isoToEpochMillis(query.from);
27110
28290
  const fromPredicate = fromMs === void 0 ? "" : ` AND e.started_at >= :fromMs`;
@@ -27117,12 +28297,7 @@ var SqliteFindingsRepository = class {
27117
28297
  predicate,
27118
28298
  params: sessionParams
27119
28299
  });
27120
- const rows = this.previewRows(aggregates, {
27121
- sessionId: query.sessionId,
27122
- from: query.from
27123
- });
27124
- const groupable = rows.map(toFlatFindingRow);
27125
- const allGroups = buildFindingGroups(groupable, { aggregates });
28300
+ const allTypes = buildFindingTypes(aggregates);
27126
28301
  const filterOpts = {
27127
28302
  severity: query.severity,
27128
28303
  providers: query.provider,
@@ -27131,30 +28306,25 @@ var SqliteFindingsRepository = class {
27131
28306
  subtype: query.subtype,
27132
28307
  q: query.q
27133
28308
  };
27134
- const facets = computeFindingFacets(allGroups, filterOpts);
27135
- const sorted = sortFindingGroups(applyFindingFilters(allGroups, filterOpts));
28309
+ const facets = computeFindingFacets(allTypes, filterOpts);
28310
+ const sorted = sortFindingTypes(applyFindingFilters(allTypes, filterOpts));
27136
28311
  const statusFilter = query.status ?? [];
27137
28312
  const totals = {
27138
- findings: sorted.reduce((acc, g) => {
27139
- if (statusFilter.length === 0) return acc + g.instanceCount;
27140
- const agg = aggregates.get(g.id);
27141
- return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? g.instanceCount : g.instanceCount);
28313
+ findings: sorted.reduce((acc, t) => {
28314
+ if (statusFilter.length === 0) return acc + t.instanceCount;
28315
+ const agg = aggregates.get(t.id);
28316
+ return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? t.instanceCount : t.instanceCount);
27142
28317
  }, 0),
27143
- groups: sorted.length
28318
+ types: sorted.length
27144
28319
  };
27145
- const limit = query.limit ?? DEFAULT_GROUPED_FINDINGS_LIMIT;
28320
+ const limit = query.limit ?? DEFAULT_FINDING_TYPES_LIMIT;
27146
28321
  const cursor = query.cursor === void 0 ? null : decodeGroupCursor(query.cursor);
27147
28322
  const start = cursor === null ? 0 : firstAfter(sorted, cursor);
27148
28323
  const page = sorted.slice(start, start + limit);
27149
28324
  const lastOnPage = page.at(-1);
27150
28325
  const nextCursor = start + limit < sorted.length && lastOnPage ? encodeGroupCursor(lastOnPage) : null;
27151
28326
  const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinked(sorted, page, query.includeId);
27152
- const statusSet = statusFilter.length > 0 ? new Set(statusFilter) : null;
27153
- const narrow = (g) => statusSet ? {
27154
- ...g,
27155
- instances: g.instances.filter((i) => i.status !== void 0 && statusSet.has(i.status))
27156
- } : g;
27157
- const items = [...page, ...deepLinked ? [deepLinked] : []].map(narrow);
28327
+ const items = [...page, ...deepLinked ? [deepLinked] : []];
27158
28328
  return Promise.resolve({
27159
28329
  totals,
27160
28330
  facets,
@@ -27165,7 +28335,7 @@ var SqliteFindingsRepository = class {
27165
28335
  }
27166
28336
  /**
27167
28337
  * One row per rule_id, folding EVERY instance of the group into the values
27168
- * buildFindingGroups cannot recover from a preview. Bounded by the number of
28338
+ * buildFindingTypes cannot recover from an aggregate. Bounded by the number of
27169
28339
  * distinct rule_ids (the installed packs' rules), not by the store's size.
27170
28340
  *
27171
28341
  * A single scan, folded in two levels: the inner SELECT groups by
@@ -27219,6 +28389,7 @@ var SqliteFindingsRepository = class {
27219
28389
  providers: query.provider,
27220
28390
  actions: query.action,
27221
28391
  statuses: query.status,
28392
+ deliveries: query.deployment,
27222
28393
  tools: query.tool,
27223
28394
  repo: query.repo,
27224
28395
  file: query.file,
@@ -27259,13 +28430,25 @@ var SqliteFindingsRepository = class {
27259
28430
  });
27260
28431
  }
27261
28432
  /**
27262
- * The same findings folded by location: repository, then file within it.
28433
+ * The same findings folded by WHERE they live — one row per (repo, file) pair.
27263
28434
  *
27264
28435
  * The grouping keys come from the capturing event's attributes, which is what
27265
- * the local store relates a finding to — there is no finding↔asset row to
27266
- * group by instead. A repo or file the event did not record folds into the
27267
- * empty-string bucket, which the view renders but does not link, since no
27268
- * filter can name it.
28436
+ * the local store relates a finding to; there is no finding↔asset row to group
28437
+ * by instead. A repo or file the event did not record folds into the
28438
+ * empty-string bucket, which is a real location like any other: it is listed,
28439
+ * it is selectable, and its `?loc=` token is as good as any other row's.
28440
+ *
28441
+ * ONE flat list rather than repos nesting files. A rollup can only be paged by
28442
+ * repo, which leaves the file list inside it unbounded — the shape the by-type
28443
+ * list was rebuilt to remove — and two-level pagination inside an
28444
+ * expand/collapse table is what pushed that view to master/detail in the first
28445
+ * place.
28446
+ *
28447
+ * Every filter narrows the FINDINGS and the locations fall out of what
28448
+ * survives, so each row's `instanceCount` is exactly what listFindingInstances
28449
+ * reports for the same filters scoped to that pair. The view depends on it:
28450
+ * one toolbar sits over both panels precisely because a location owns none of
28451
+ * its fields.
27269
28452
  */
27270
28453
  listFindingLocations(query) {
27271
28454
  const opts = {
@@ -27274,16 +28457,20 @@ var SqliteFindingsRepository = class {
27274
28457
  providers: query.provider,
27275
28458
  actions: query.action,
27276
28459
  statuses: query.status,
28460
+ deliveries: query.deployment,
27277
28461
  tools: query.tool,
27278
28462
  q: query.q
27279
28463
  };
27280
- const limit = query.limit ?? DEFAULT_LOCATIONS_LIMIT;
28464
+ const limit = query.limit ?? DEFAULT_FINDING_LOCATIONS_LIMIT;
28465
+ const cursor = query.cursor === void 0 ? null : decodeLocationCursor(query.cursor);
27281
28466
  const byRepo = /* @__PURE__ */ new Map();
28467
+ const accumulator = createInstanceFacetAccumulator(opts);
27282
28468
  let total = 0;
27283
28469
  for (const row of this.scanFindingRows({
27284
28470
  sessionId: query.sessionId,
27285
28471
  from: query.from
27286
28472
  })) {
28473
+ accumulator.add(row);
27287
28474
  if (!matchesInstanceFilters(row, opts)) continue;
27288
28475
  total += 1;
27289
28476
  let files = byRepo.get(row.repo);
@@ -27298,103 +28485,35 @@ var SqliteFindingsRepository = class {
27298
28485
  }
27299
28486
  addToLocation(acc, row);
27300
28487
  }
27301
- let fileCount = 0;
27302
- const repos = [...byRepo.entries()].map(([repo, files]) => {
27303
- fileCount += files.size;
27304
- const fileRows = [...files.entries()].map(([file2, acc]) => ({
27305
- file: file2,
27306
- instanceCount: acc.instanceCount,
27307
- maxSeverity: acc.maxSeverity,
27308
- latestDetectedAt: acc.latestDetectedAt,
27309
- ...foldGroupStatus(acc.statuses) === void 0 ? {} : { status: foldGroupStatus(acc.statuses) },
27310
- ruleIds: [...acc.ruleIds].slice(0, LOCATION_RULE_IDS_CAP)
27311
- })).sort(compareLocationOrder);
27312
- const rollup = fileRows.reduce(
27313
- (a, f) => ({
27314
- instanceCount: a.instanceCount + f.instanceCount,
27315
- maxSeverity: compareLocationOrder(f, a) < 0 ? f.maxSeverity : a.maxSeverity,
27316
- latestDetectedAt: f.latestDetectedAt > a.latestDetectedAt ? f.latestDetectedAt : a.latestDetectedAt
27317
- }),
27318
- {
27319
- instanceCount: 0,
27320
- maxSeverity: fileRows[0]?.maxSeverity ?? "low",
27321
- latestDetectedAt: ""
27322
- }
27323
- );
27324
- const statuses = fileRows.map((f) => f.status);
27325
- const folded = foldGroupStatus(statuses);
27326
- return {
27327
- repo,
27328
- instanceCount: rollup.instanceCount,
27329
- maxSeverity: rollup.maxSeverity,
27330
- latestDetectedAt: rollup.latestDetectedAt,
27331
- ...folded === void 0 ? {} : { status: folded },
27332
- files: fileRows
27333
- };
27334
- });
27335
- repos.sort(compareLocationOrder);
28488
+ const sorted = [];
28489
+ for (const [repo, files] of byRepo) {
28490
+ for (const [file2, acc] of files) {
28491
+ const status = foldGroupStatus(acc.statuses);
28492
+ sorted.push({
28493
+ id: encodeLocationId(repo, file2),
28494
+ repo,
28495
+ file: file2,
28496
+ instanceCount: acc.instanceCount,
28497
+ maxSeverity: acc.maxSeverity,
28498
+ latestDetectedAt: acc.latestDetectedAt,
28499
+ ...status === void 0 ? {} : { status },
28500
+ ruleIds: [...acc.ruleIds]
28501
+ });
28502
+ }
28503
+ }
28504
+ sorted.sort(compareLocationOrder);
28505
+ const start = cursor === null ? 0 : firstLocationAfter(sorted, cursor);
28506
+ const page = sorted.slice(start, start + limit);
28507
+ const lastOnPage = page.at(-1);
28508
+ const nextCursor = start + limit < sorted.length && lastOnPage ? encodeLocationCursor(lastOnPage) : null;
28509
+ const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinkedLocation(sorted, page, query.includeId);
27336
28510
  return Promise.resolve({
27337
- totals: { findings: total, repos: repos.length, files: fileCount },
27338
- items: repos.slice(0, limit),
27339
- hasMore: repos.length > limit
28511
+ totals: { findings: total, locations: sorted.length },
28512
+ facets: accumulator.facets(),
28513
+ items: [...page, ...deepLinked ? [deepLinked] : []],
28514
+ nextCursor
27340
28515
  });
27341
28516
  }
27342
- /**
27343
- * Each group's newest instances, for the table's expanded rows.
27344
- *
27345
- * ONE index-ordered scan with early termination, and the shape is the point.
27346
- * The natural spelling — `ROW_NUMBER() OVER (PARTITION BY rule_id ORDER BY
27347
- * started_at DESC)` then `WHERE rn <= cap` — sorts EVERY finding in scope
27348
- * through a temp B-tree to keep a bounded preview of each group, and then
27349
- * sorts the survivors again for the page order. Both sorts grow with the
27350
- * store while the answer does not.
27351
- *
27352
- * Instead the scan walks `audit_events` newest-first off `idx_audit_started_at`
27353
- * (or the session or window index the scope names — see `findingScanSql`),
27354
- * which is already the order the page wants, and keeps rows per rule until
27355
- * each rule has as many as it can show. The aggregate the caller already holds
27356
- * says how many that is: `min(instanceCount, PREVIEW_INSTANCES_PER_GROUP)`
27357
- * per rule, summed, is the number of rows this scan has to find, and it stops
27358
- * on the last one. That sum is bounded by `rules * PREVIEW_INSTANCES_PER_GROUP`
27359
- * (8,000 at this repo's 40-rule bench corpus), not by a fixed row count — a
27360
- * store with many firing rules widens it. The bound that DOES hold
27361
- * unconditionally is the sorted form's floor: this scan visits at most as
27362
- * many rows as `ROW_NUMBER() OVER (PARTITION BY rule_id …)` would have
27363
- * sorted, and stops the moment every rule has its cap, where the sorted form
27364
- * sorts the whole scope regardless. The true worst case — the rarest rule's
27365
- * wanted instances sitting at the tail of the scope — is one pass over
27366
- * everything in scope with a block sort of the id tie-break only, never a
27367
- * sort of the scope, which is still that floor.
27368
- *
27369
- * A row whose rule the aggregate did not see is skipped: the two statements
27370
- * run without a shared snapshot, so a capture landing between them can add a
27371
- * rule here that has no counts there, and the counts are what the group is
27372
- * built from.
27373
- */
27374
- previewRows(aggregates, scope) {
27375
- const wanted = /* @__PURE__ */ new Map();
27376
- let remaining = 0;
27377
- for (const [ruleId, agg] of aggregates) {
27378
- const n = Math.min(agg.instanceCount, PREVIEW_INSTANCES_PER_GROUP);
27379
- wanted.set(ruleId, n);
27380
- remaining += n;
27381
- }
27382
- const rows = [];
27383
- if (remaining === 0) return rows;
27384
- const { sql, params } = this.findingScanSql(scope);
27385
- const taken = /* @__PURE__ */ new Map();
27386
- for (const r of iterateRows(this.db.prepare(sql), params)) {
27387
- const want = wanted.get(r.rule_id);
27388
- if (want === void 0) continue;
27389
- const have = taken.get(r.rule_id) ?? 0;
27390
- if (have >= want) continue;
27391
- taken.set(r.rule_id, have + 1);
27392
- rows.push(r);
27393
- remaining -= 1;
27394
- if (remaining === 0) break;
27395
- }
27396
- return rows;
27397
- }
27398
28517
  /**
27399
28518
  * Every finding in scope as a FlatFindingRow, newest first, streamed.
27400
28519
  *
@@ -27421,6 +28540,33 @@ var SqliteFindingsRepository = class {
27421
28540
  yield toFlatFindingRow(r);
27422
28541
  }
27423
28542
  }
28543
+ /**
28544
+ * One finding by its own id, or null when no such row exists.
28545
+ *
28546
+ * A primary-key seek on `inspection_findings`, so its cost does not grow with
28547
+ * the store — and, unlike anything derived from a list page, it resolves a
28548
+ * finding of ANY age. That is what the Findings page's one-shot `?finding=`
28549
+ * deep link needs: the id it carries may name a finding thousands of rows
28550
+ * older than anything a first page holds.
28551
+ *
28552
+ * Deliberately UNFILTERED — no capture-kind, session or time predicate. It
28553
+ * RESOLVES an id; whether that row would survive the list's current filters is
28554
+ * a different question, and hiding the target because a filter excludes it is
28555
+ * worse than showing it.
28556
+ *
28557
+ * `groupId` on the result IS the rule id, so this one read answers both "which
28558
+ * type should the list select?" and "what does the drawer show?".
28559
+ */
28560
+ findingInstance(id) {
28561
+ const row = this.db.prepare(
28562
+ `SELECT ${FINDING_ROW_COLUMNS_SQL}
28563
+ FROM inspection_findings f
28564
+ JOIN audit_events e ON e.id = f.audit_event_id
28565
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
28566
+ WHERE f.id = ?`
28567
+ ).get(id);
28568
+ return Promise.resolve(row === void 0 ? null : toInstanceDetail(toFlatFindingRow(row)));
28569
+ }
27424
28570
  /**
27425
28571
  * The one statement both instance-level scans run: every finding in scope,
27426
28572
  * joined to its event and definition, newest first.
@@ -27454,17 +28600,7 @@ var SqliteFindingsRepository = class {
27454
28600
  conditions.push("e.started_at >= ?");
27455
28601
  params.push(isoToEpochMillis(scope.from));
27456
28602
  }
27457
- const sql = `SELECT f.id AS id, d.rule_id AS rule_id, d.category AS category,
27458
- d.severity AS severity, f.masked_match AS masked_match,
27459
- f.action_taken AS action_taken, f.confidence AS confidence,
27460
- e.started_at AS occurred_at,
27461
- e.source_tool AS source_tool,
27462
- e.repo AS repo,
27463
- e.file_path AS file,
27464
- e.tool_name AS tool_name,
27465
- f.audit_event_id AS event_id, e.root_session_id AS session_id,
27466
- e.event_type AS kind, f.finding_key AS finding_key,
27467
- ${latestResolutionStatusSql("f")} AS latest_status
28603
+ const sql = `SELECT ${FINDING_ROW_COLUMNS_SQL}
27468
28604
  FROM audit_events e
27469
28605
  CROSS JOIN inspection_findings f ON f.audit_event_id = e.id
27470
28606
  CROSS JOIN inspection_definitions d ON d.id = f.inspection_definition_id
@@ -27478,6 +28614,26 @@ var SqliteFindingsRepository = class {
27478
28614
  group_concat(DISTINCT 'via ' || e.tool_name) AS tool_names` : `, NULL AS repos, NULL AS files, NULL AS tool_names`;
27479
28615
  const rows = this.db.prepare(
27480
28616
  `SELECT rule_id,
28617
+ -- BARE columns beside max(latest_at), which is deliberate and
28618
+ -- is SQLite's documented behaviour: with a single min()/max()
28619
+ -- in an aggregate query, every bare column takes its value from
28620
+ -- the row that produced the extremum. So these are the severity
28621
+ -- and category of the definition whose finding is NEWEST, which
28622
+ -- is what the row-based build they replaced read off its first
28623
+ -- (newest-first) row.
28624
+ --
28625
+ -- min() is WRONG here and was the defect: inspection_definitions
28626
+ -- holds one row per rule VERSION (see its writer \u2014 a version bump
28627
+ -- mints a new row), so a rule whose severity moved between
28628
+ -- versions has several, and min() picks the ALPHABETICALLY
28629
+ -- smallest \u2014 'low' over 'medium', but 'critical' over 'high'.
28630
+ -- That is arbitrary in direction, and it feeds the badge, the
28631
+ -- filter, the facet counts and the primary sort key.
28632
+ --
28633
+ -- Adding a second min()/max() aggregate here would make these
28634
+ -- bare columns ambiguous again; keep max(latest_at) the only one.
28635
+ severity,
28636
+ category,
27481
28637
  sum(tuple_count) AS instance_count,
27482
28638
  max(latest_at) AS latest_at,
27483
28639
  group_concat(source_tools) AS source_tools,
@@ -27488,6 +28644,14 @@ var SqliteFindingsRepository = class {
27488
28644
  group_concat(tool_names) AS tool_names
27489
28645
  FROM (
27490
28646
  SELECT d.rule_id AS rule_id,
28647
+ -- Severity and category are columns of the DEFINITION, and
28648
+ -- a rule can have SEVERAL definitions (one per version), so
28649
+ -- these are grouped on below and resolved to the newest
28650
+ -- firing version by the outer query's bare-column select.
28651
+ -- They ride the aggregate because the type build has no rows
28652
+ -- to read them off \u2014 see buildFindingTypes.
28653
+ d.severity AS severity,
28654
+ d.category AS category,
27491
28655
  e.event_type || '${TUPLE_SEP}' ||
27492
28656
  (CASE WHEN f.finding_key IS NULL THEN '' ELSE 'k' END) || '${TUPLE_SEP}' ||
27493
28657
  coalesce(latest.status, '') AS status_tuple,
@@ -27502,7 +28666,7 @@ var SqliteFindingsRepository = class {
27502
28666
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
27503
28667
  ON latest.finding_key = f.finding_key
27504
28668
  ${scope.predicate}
27505
- GROUP BY d.rule_id, status_tuple
28669
+ GROUP BY d.rule_id, d.severity, d.category, status_tuple
27506
28670
  )
27507
28671
  GROUP BY rule_id`
27508
28672
  ).all(scope.params);
@@ -27511,6 +28675,8 @@ var SqliteFindingsRepository = class {
27511
28675
  r.rule_id,
27512
28676
  {
27513
28677
  instanceCount: r.instance_count,
28678
+ severity: r.severity,
28679
+ category: r.category,
27514
28680
  sourceTools: splitConcat(r.source_tools),
27515
28681
  actionsTaken: splitConcat(r.actions_taken),
27516
28682
  statusInputs: splitConcat(r.status_inputs).map((tuple2) => {
@@ -27527,7 +28693,7 @@ var SqliteFindingsRepository = class {
27527
28693
  latestDetectedAt: epochMillisToIso(r.latest_at),
27528
28694
  // Free text only — joined and substring-matched, so group_concat's
27529
28695
  // commas need no unpicking (a repo/path containing one still matches).
27530
- // Left undefined (not '') when unfetched, so buildFindingGroups can
28696
+ // Left undefined (not '') when unfetched, so buildFindingTypes can
27531
28697
  // tell "no q this request" from "a group with no repo/file at all"
27532
28698
  // and skip priming a haystack nothing will read.
27533
28699
  ...withSearchText ? {
@@ -27555,7 +28721,9 @@ var SqliteFindingsRepository = class {
27555
28721
  )
27556
28722
  );
27557
28723
  for (const row of grouped) {
27558
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28724
+ if (Object.hasOwn(byAction, row.action_taken)) {
28725
+ byAction[row.action_taken] = row.c;
28726
+ }
27559
28727
  }
27560
28728
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27561
28729
  const sevRows = allRows(
@@ -27572,7 +28740,9 @@ var SqliteFindingsRepository = class {
27572
28740
  )
27573
28741
  );
27574
28742
  for (const row of sevRows) {
27575
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28743
+ if (Object.hasOwn(bySeverity, row.severity)) {
28744
+ bySeverity[row.severity] = row.c;
28745
+ }
27576
28746
  }
27577
28747
  const categories = ENFORCEABLE_CATEGORIES;
27578
28748
  const enabledRows = allRows(
@@ -27621,469 +28791,6 @@ function isoDay(ms) {
27621
28791
  return new Date(ms).toISOString().slice(0, 10);
27622
28792
  }
27623
28793
 
27624
- // ../../packages/persistence/src/repositories/history-sync.ts
27625
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27626
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27627
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27628
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27629
- var SKIPPED = -1;
27630
- var ROW_COLUMNS = `id,
27631
- parent_id AS parentId,
27632
- root_session_id AS rootSessionId,
27633
- event_type AS eventType,
27634
- host_id AS hostId,
27635
- harness_id AS harnessId,
27636
- source_project_id AS sourceProjectId,
27637
- started_at AS startedAt,
27638
- ended_at AS endedAt,
27639
- severity,
27640
- priority,
27641
- content,
27642
- content_hash AS contentHash,
27643
- attributes`;
27644
- var SqliteHistorySyncRepository = class {
27645
- constructor(db) {
27646
- this.db = db;
27647
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27648
- this.sessionsStmt = db.prepare(
27649
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27650
- FROM audit_events
27651
- WHERE synced_at IS NULL
27652
- AND event_type IN (${TYPE_LIST})
27653
- AND started_at < :before
27654
- GROUP BY sessionId
27655
- ORDER BY earliest
27656
- LIMIT :limit`
27657
- );
27658
- this.rowsStmt = db.prepare(
27659
- `SELECT ${ROW_COLUMNS}
27660
- FROM audit_events
27661
- WHERE synced_at IS NULL
27662
- AND event_type IN (${TYPE_LIST})
27663
- AND started_at < :before
27664
- AND COALESCE(root_session_id, id) = :sessionId
27665
- ORDER BY (event_type = 'session') DESC, started_at
27666
- LIMIT :limit`
27667
- );
27668
- this.captureRowsStmt = db.prepare(
27669
- `SELECT ${ROW_COLUMNS}
27670
- FROM audit_events
27671
- WHERE synced_at IS NULL
27672
- AND sync_claimed_at IS NULL
27673
- AND outbox_owed = 1
27674
- AND event_type IN (${CAPTURE_TYPE_LIST})
27675
- AND started_at < :before
27676
- ORDER BY started_at
27677
- LIMIT :limit`
27678
- );
27679
- this.markOwedStmt = db.prepare(
27680
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27681
- );
27682
- this.stampStmt = db.prepare(
27683
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27684
- );
27685
- this.claimRowStmt = db.prepare(
27686
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27687
- );
27688
- this.releaseRowStmt = db.prepare(
27689
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27690
- );
27691
- this.releaseStaleClaimsStmt = db.prepare(
27692
- `UPDATE audit_events SET sync_claimed_at = NULL
27693
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27694
- );
27695
- this.partitionStmt = db.prepare(
27696
- `SELECT
27697
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27698
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27699
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27700
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27701
- COUNT(*) AS total
27702
- FROM audit_events
27703
- WHERE event_type IN (${TYPE_LIST})`
27704
- );
27705
- this.countsStmt = db.prepare(
27706
- `SELECT
27707
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27708
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27709
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27710
- FROM audit_events
27711
- WHERE event_type IN (${TYPE_LIST})`
27712
- );
27713
- this.captureSkipCountStmt = db.prepare(
27714
- `SELECT COUNT(*) AS skipped
27715
- FROM audit_events
27716
- WHERE synced_at = ${String(SKIPPED)}
27717
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27718
- );
27719
- this.fingerprintStmt = db.prepare(
27720
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27721
- FROM history_sync WHERE id = 1`
27722
- );
27723
- this.setFingerprintStmt = db.prepare(
27724
- `UPDATE history_sync
27725
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27726
- WHERE id = 1`
27727
- );
27728
- this.disownCapturesStmt = db.prepare(
27729
- `UPDATE audit_events SET outbox_owed = NULL
27730
- WHERE outbox_owed IS NOT NULL AND event_type IN (${CAPTURE_TYPE_LIST})`
27731
- );
27732
- this.rearmStmt = db.prepare(
27733
- `UPDATE audit_events SET synced_at = NULL
27734
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27735
- );
27736
- this.claimStmt = db.prepare(
27737
- `UPDATE history_sync
27738
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27739
- WHERE id = 1
27740
- AND (owner_pid IS NULL
27741
- OR heartbeat_at IS NULL
27742
- OR heartbeat_at < :staleBefore
27743
- OR heartbeat_at > :now)`
27744
- );
27745
- this.heartbeatStmt = db.prepare(
27746
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27747
- );
27748
- this.releaseStmt = db.prepare(
27749
- `UPDATE history_sync
27750
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27751
- WHERE id = 1 AND owner_pid = :pid`
27752
- );
27753
- this.closeWindowStmt = db.prepare(
27754
- `UPDATE audit_events SET synced_at = :at
27755
- WHERE synced_at IS NULL
27756
- AND event_type IN (${TYPE_LIST})
27757
- AND started_at >= :attachedAt`
27758
- );
27759
- this.releaseBoundaryStmt = db.prepare(
27760
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27761
- );
27762
- this.freezeBoundaryStmt = db.prepare(
27763
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27764
- );
27765
- this.leaseStmt = db.prepare(
27766
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27767
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27768
- FROM history_sync WHERE id = 1`
27769
- );
27770
- this.inspectionsStmt = db.prepare(
27771
- `SELECT d.rule_id AS ruleId,
27772
- d.name AS ruleName,
27773
- d.version AS ruleVersion,
27774
- d.category AS category,
27775
- d.severity AS severity,
27776
- f.span_start AS spanStart,
27777
- f.span_end AS spanEnd,
27778
- f.masked_match AS maskedMatch,
27779
- f.action_taken AS actionTaken,
27780
- f.confidence AS confidence
27781
- FROM inspection_findings f
27782
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27783
- WHERE f.audit_event_id = :auditEventId
27784
- ORDER BY f.span_start, f.id`
27785
- );
27786
- }
27787
- db;
27788
- ensureRowStmt;
27789
- sessionsStmt;
27790
- rowsStmt;
27791
- stampStmt;
27792
- countsStmt;
27793
- fingerprintStmt;
27794
- setFingerprintStmt;
27795
- rearmStmt;
27796
- claimStmt;
27797
- heartbeatStmt;
27798
- releaseStmt;
27799
- leaseStmt;
27800
- inspectionsStmt;
27801
- closeWindowStmt;
27802
- releaseBoundaryStmt;
27803
- freezeBoundaryStmt;
27804
- captureRowsStmt;
27805
- markOwedStmt;
27806
- captureSkipCountStmt;
27807
- disownCapturesStmt;
27808
- partitionStmt;
27809
- claimRowStmt;
27810
- releaseRowStmt;
27811
- releaseStaleClaimsStmt;
27812
- /**
27813
- * The masked detections recorded against one tool call.
27814
- *
27815
- * These travel with the event because a tool call's target is not
27816
- * re-inspectable from the event alone — unlike a capture, where the text
27817
- * itself is re-scannable. What crosses is the masked match and the rule that
27818
- * produced it, never the value.
27819
- */
27820
- inspectionsFor(auditEventId) {
27821
- return allRows(this.inspectionsStmt, { auditEventId });
27822
- }
27823
- /**
27824
- * Sessions with structural rows still to send, oldest first.
27825
- *
27826
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27827
- * read. Anything recorded after the machine attached is the live forward
27828
- * path's to deliver; this drain exists for what was recorded before it, and a
27829
- * row both paths send is at best a duplicate request and at worst — for a
27830
- * session root — an overwrite of the inventory ids the live path resolved.
27831
- */
27832
- pendingSessions(limit, before) {
27833
- return allRows(this.sessionsStmt, { limit, before }).map(
27834
- (r) => r.sessionId
27835
- );
27836
- }
27837
- /** One session's undelivered structural rows within the backlog, root first. */
27838
- pendingRows(sessionId, limit, before) {
27839
- return allRows(this.rowsStmt, { sessionId, limit, before });
27840
- }
27841
- /**
27842
- * Captures this machine still owes the deployment, oldest first.
27843
- *
27844
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27845
- * by a time window — see captureRowsStmt for why a window could not express
27846
- * this. `before` is the grace window that leaves a just-recorded capture to
27847
- * the live path.
27848
- */
27849
- pendingCaptureRows(limit, before) {
27850
- return allRows(this.captureRowsStmt, { limit, before });
27851
- }
27852
- /**
27853
- * Record that a capture is OWED to the deployment.
27854
- *
27855
- * Written by the attached forward path when a live send did not confirm
27856
- * delivery, and read by the drain as the whole of its eligibility test. It is
27857
- * a fact rather than an inference: the machine was attached, the send did not
27858
- * land, so the row is owed — which no time window can state, because the same
27859
- * window that holds the rows a past attachment left owed also holds every
27860
- * capture recorded while the machine was DETACHED, and those were never
27861
- * offered to anyone.
27862
- *
27863
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27864
- * out of the drain's read.
27865
- */
27866
- markCaptureOwed(id) {
27867
- this.markOwedStmt.run({ id });
27868
- }
27869
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27870
- markSynced(ids, atMs) {
27871
- this.stampAll(ids, atMs);
27872
- }
27873
- /**
27874
- * Record that a row will never be sent.
27875
- *
27876
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27877
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27878
- * is retried; marking those would turn one outage into permanent data loss.
27879
- */
27880
- markSkipped(ids) {
27881
- this.stampAll(ids, SKIPPED);
27882
- }
27883
- eachInTransaction(ids, run) {
27884
- if (ids.length === 0) return;
27885
- withTransaction(
27886
- this.db,
27887
- () => {
27888
- for (const id of ids) run(id);
27889
- },
27890
- "IMMEDIATE"
27891
- );
27892
- }
27893
- stampAll(ids, value) {
27894
- if (ids.length === 0) return;
27895
- withTransaction(
27896
- this.db,
27897
- () => {
27898
- for (const id of ids) this.stampStmt.run({ at: value, id });
27899
- },
27900
- "IMMEDIATE"
27901
- );
27902
- }
27903
- /**
27904
- * Claim rows as in-flight.
27905
- *
27906
- * Advisory in exactly the sense the lease is: it records that a send is in
27907
- * progress so a surface can say so, and a lost claim costs a row showing as
27908
- * queued while it is actually being sent. It is not exclusion — the far side
27909
- * settles a duplicate on the row id.
27910
- */
27911
- claimRows(ids, atMs) {
27912
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
27913
- }
27914
- /** Give back a claim without settling — the send failed, the row is queued again. */
27915
- releaseRows(ids) {
27916
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
27917
- }
27918
- /**
27919
- * Clear claims older than `staleBefore`, and report how many were cleared.
27920
- *
27921
- * A process killed between claiming and settling leaves rows claimed with
27922
- * nothing left to settle them. Without this they read as "sending" for ever.
27923
- */
27924
- releaseStaleClaims(staleBefore) {
27925
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
27926
- }
27927
- /**
27928
- * Every tracked row in exactly one delivery state.
27929
- *
27930
- * Takes no boundary on purpose. The boundary answers "what should the drain
27931
- * pick up now", which is a different question from "what state is this row
27932
- * in" — and a machine that has never attached has no boundary to pass, so
27933
- * requiring one would force a caller to invent one and report the whole store
27934
- * as queued.
27935
- */
27936
- partition() {
27937
- const row = getRow(this.partitionStmt, {});
27938
- return {
27939
- queued: row?.queued ?? 0,
27940
- inProgress: row?.inProgress ?? 0,
27941
- synced: row?.synced ?? 0,
27942
- failed: row?.failed ?? 0,
27943
- total: row?.total ?? 0
27944
- };
27945
- }
27946
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
27947
- counts(before) {
27948
- const row = getRow(
27949
- this.countsStmt,
27950
- { before }
27951
- );
27952
- const captures = getRow(this.captureSkipCountStmt);
27953
- return {
27954
- pending: row?.pending ?? 0,
27955
- sent: row?.sent ?? 0,
27956
- skipped: row?.skipped ?? 0,
27957
- capturesSkipped: captures?.skipped ?? 0
27958
- };
27959
- }
27960
- /**
27961
- * The deployment the current stamps were made against, and where its backlog
27962
- * ends.
27963
- *
27964
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
27965
- * machine that has never drained is — and every writer below seeds the row
27966
- * before it needs one, so nothing depends on this creating it. Keeping the
27967
- * write off the gate path matters because the gate runs on every pass while a
27968
- * write has to take the database's write lock.
27969
- */
27970
- deployment() {
27971
- const row = getRow(
27972
- this.fingerprintStmt
27973
- );
27974
- return {
27975
- fingerprint: row?.fingerprint ?? void 0,
27976
- backlogBefore: row?.backlogBefore ?? void 0
27977
- };
27978
- }
27979
- /**
27980
- * Point the ledger at a different deployment, discarding what it recorded
27981
- * about the previous one.
27982
- *
27983
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
27984
- * machine has just left are undelivered as far as the new one is concerned.
27985
- * All three in one transaction, so a crash between them cannot leave stamps
27986
- * attributed to the wrong deployment, or a boundary that belongs to another.
27987
- *
27988
- * The boundary is written HERE and only here, which is what freezes it: a
27989
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
27990
- * unchanged, so this never runs and the backlog does not widen back over rows
27991
- * the live path has since delivered.
27992
- */
27993
- rearmFor(fingerprint, backlogBefore) {
27994
- this.ensureRowStmt.run();
27995
- withTransaction(
27996
- this.db,
27997
- () => {
27998
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
27999
- this.rearmStmt.run();
28000
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28001
- this.disownCapturesStmt.run();
28002
- }
28003
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28004
- },
28005
- "IMMEDIATE"
28006
- );
28007
- }
28008
- /**
28009
- * End the attached period: hand its rows to the live path, and release the
28010
- * boundary so the next attachment can freeze a new one.
28011
- *
28012
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28013
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28014
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28015
- * during the detached period, because the machine is not attached. Rows
28016
- * recorded in that window sit after the boundary and before the re-attach, so
28017
- * neither path takes them, and the pending count reports none outstanding.
28018
- *
28019
- * Stamping the attached window is not a claim that every one of those rows
28020
- * reached the deployment — the live path drops on failure and says so
28021
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28022
- * status quo: they sit outside the frozen boundary today and are equally never
28023
- * re-sent. Making it explicit is what lets the boundary move.
28024
- *
28025
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28026
- * window unstamped — that half-state would re-send the whole attached period
28027
- * on the next attach, which is the failure the boundary exists to prevent.
28028
- */
28029
- closeAttachedWindow(attachedAtMs, atMs) {
28030
- this.ensureRowStmt.run();
28031
- withTransaction(
28032
- this.db,
28033
- () => {
28034
- const row = getRow(this.fingerprintStmt);
28035
- const from = row?.backlogBefore ?? attachedAtMs;
28036
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28037
- this.releaseBoundaryStmt.run();
28038
- },
28039
- "IMMEDIATE"
28040
- );
28041
- }
28042
- /**
28043
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28044
- *
28045
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28046
- * different deployment and therefore discards what was delivered to the old
28047
- * one: here the recipient is the same, so everything already sent to it stays
28048
- * sent.
28049
- */
28050
- freezeBoundary(backlogBefore) {
28051
- this.ensureRowStmt.run();
28052
- this.freezeBoundaryStmt.run({ backlogBefore });
28053
- }
28054
- /** Take the claim, or report that someone live already holds it. */
28055
- claim(pid, host, nowMs, staleAfterMs) {
28056
- this.ensureRowStmt.run();
28057
- let taken = false;
28058
- withTransaction(
28059
- this.db,
28060
- () => {
28061
- const result = this.claimStmt.run({
28062
- pid,
28063
- host,
28064
- now: nowMs,
28065
- staleBefore: nowMs - staleAfterMs
28066
- });
28067
- taken = result.changes === 1;
28068
- },
28069
- "IMMEDIATE"
28070
- );
28071
- return taken;
28072
- }
28073
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28074
- heartbeat(pid, nowMs) {
28075
- this.heartbeatStmt.run({ now: nowMs, pid });
28076
- }
28077
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28078
- release(pid) {
28079
- this.releaseStmt.run({ pid });
28080
- }
28081
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28082
- lease() {
28083
- return getRow(this.leaseStmt);
28084
- }
28085
- };
28086
-
28087
28794
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28088
28795
  var SqliteInspectionDefinitionsRepository = class {
28089
28796
  constructor(db) {
@@ -28275,7 +28982,8 @@ function managedSettingsPaths(platform2 = process.platform) {
28275
28982
  }
28276
28983
  return [posix.join("/etc", "aka", MANAGED_SETTINGS_FILENAME)];
28277
28984
  }
28278
- function readManagedSettings(paths = managedSettingsPaths()) {
28985
+ var testOnlyManagedPaths = null;
28986
+ function readManagedSettings(paths = testOnlyManagedPaths ?? managedSettingsPaths()) {
28279
28987
  for (const path of paths) {
28280
28988
  let text;
28281
28989
  try {
@@ -28310,6 +29018,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28310
29018
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28311
29019
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28312
29020
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29021
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28313
29022
  if (values.vaultConsent !== void 0) {
28314
29023
  merged.vaultConsent = values.vaultConsent ? (
28315
29024
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30755,7 +31464,7 @@ function toUtcDateString(ms) {
30755
31464
  return new Date(ms).toISOString().slice(0, 10);
30756
31465
  }
30757
31466
  function isTimeseriesSeverity(s) {
30758
- return s === "critical" || s === "high" || s === "medium";
31467
+ return s === "critical" || s === "high" || s === "medium" || s === "low";
30759
31468
  }
30760
31469
  var SqliteSecurityRepository = class {
30761
31470
  constructor(db, now = () => Date.now()) {
@@ -30817,7 +31526,7 @@ var SqliteSecurityRepository = class {
30817
31526
  ELSE 0
30818
31527
  END) AS open_at_rest
30819
31528
  FROM inspection_findings f
30820
- JOIN audit_events e ON e.id = f.audit_event_id
31529
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30821
31530
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30822
31531
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30823
31532
  ON latest.finding_key = f.finding_key
@@ -30884,12 +31593,16 @@ var SqliteSecurityRepository = class {
30884
31593
  const now = this.now();
30885
31594
  const windowStart = startOfUtcDay2(now) - (lenDays - 1) * DAY_MS4;
30886
31595
  const rows = this.findingsInRange(windowStart, now);
30887
- const points = Array.from({ length: numBuckets }, (_, i) => ({
30888
- timestamp: toUtcDateString(windowStart + i * bucketMs),
30889
- critical: 0,
30890
- high: 0,
30891
- medium: 0
30892
- }));
31596
+ const points = Array.from(
31597
+ { length: numBuckets },
31598
+ (_, i) => ({
31599
+ timestamp: toUtcDateString(windowStart + i * bucketMs),
31600
+ critical: 0,
31601
+ high: 0,
31602
+ medium: 0,
31603
+ low: 0
31604
+ })
31605
+ );
30893
31606
  for (const r of rows) {
30894
31607
  const idx = Math.floor((r.occurredAt - windowStart) / bucketMs);
30895
31608
  const bucket = points[idx];
@@ -31039,7 +31752,7 @@ var SqliteSecurityRepository = class {
31039
31752
  this.db.prepare(
31040
31753
  `SELECT e.repo AS repo, count(*) AS c
31041
31754
  FROM inspection_findings f
31042
- JOIN audit_events e ON e.id = f.audit_event_id
31755
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31043
31756
  WHERE e.started_at >= :from AND e.started_at < :to
31044
31757
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31045
31758
  AND e.repo IS NOT NULL
@@ -31107,6 +31820,7 @@ var SqliteSecurityRepository = class {
31107
31820
  `SELECT f.finding_key AS finding_key,
31108
31821
  d.rule_id AS rule_id,
31109
31822
  d.severity AS severity,
31823
+ e.repo AS repo,
31110
31824
  e.file_path AS path,
31111
31825
  COALESCE(f.first_detected_at, e.started_at) AS first_detected_at,
31112
31826
  latest.resolved_at AS latest_resolved_at
@@ -31126,6 +31840,7 @@ var SqliteSecurityRepository = class {
31126
31840
  const items = rows.map((r) => ({
31127
31841
  findingKey: r.finding_key,
31128
31842
  ruleId: r.rule_id,
31843
+ repo: r.repo ?? "",
31129
31844
  severity: r.severity,
31130
31845
  path: r.path ?? "",
31131
31846
  resolvedAt: new Date(r.latest_resolved_at).toISOString(),
@@ -31135,15 +31850,68 @@ var SqliteSecurityRepository = class {
31135
31850
  }));
31136
31851
  return Promise.resolve({ items });
31137
31852
  }
31853
+ /**
31854
+ * Per-rule tallies of the findings that are still OPEN, whole-store.
31855
+ *
31856
+ * Scoped by status rather than by time, because the card this feeds is a to-do
31857
+ * list: a secret committed three weeks ago and never rotated is still the most
31858
+ * important thing to fix, and any window hides it. It carried a "newest N
31859
+ * findings" cap and then a range; the first meant a different span on every
31860
+ * machine, and the second reported "no recommendations" over live exposure.
31861
+ *
31862
+ * `open` mirrors `deriveFindingStatus` — at-rest, minus resolved and dismissed —
31863
+ * so a row's count is exactly what `?status=open&type=<rule>` returns. Note that
31864
+ * is NOT `severitySummary`'s `openAtRest`, which keeps dismissed findings (a
31865
+ * dismissal is a judgement, not a remediation) and drops untracked legacy rows.
31866
+ * The two answer different questions and only this one has to match a link.
31867
+ *
31868
+ * Aggregated in SQL: the result is O(distinct rule × category × severity), so a
31869
+ * whole-store scope costs a grouped scan rather than a row per finding.
31870
+ */
31871
+ recommendationInputs() {
31872
+ const rows = allRows(
31873
+ this.db.prepare(
31874
+ `SELECT d.rule_id AS rule_id,
31875
+ d.category AS category,
31876
+ d.severity AS severity,
31877
+ COUNT(*) AS count
31878
+ FROM inspection_findings f
31879
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31880
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31881
+ LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31882
+ ON latest.finding_key = f.finding_key
31883
+ WHERE e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31884
+ AND e.event_type = 'code_change'
31885
+ AND (
31886
+ f.finding_key IS NULL
31887
+ OR latest.status IS NULL
31888
+ OR latest.status NOT IN ('resolved', 'dismissed')
31889
+ )
31890
+ GROUP BY d.rule_id, d.category, d.severity`
31891
+ )
31892
+ );
31893
+ return Promise.resolve(
31894
+ rows.map((r) => ({
31895
+ ruleId: r.rule_id,
31896
+ category: r.category,
31897
+ severity: r.severity,
31898
+ count: r.count
31899
+ }))
31900
+ );
31901
+ }
31138
31902
  // Findings whose parent event occurred in [fromMs, toMs), with the parent's
31139
31903
  // epoch-millis timestamp. started_at is an INTEGER column, so the bounds stay
31140
31904
  // numeric and the JS aggregations bucket/split on ms directly.
31141
31905
  findingsInRange(fromMs, toMs) {
31142
31906
  const rows = allRows(
31143
31907
  this.db.prepare(
31144
- `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken
31908
+ // `rule_id`/`category` cost nothing extra: inspection_definitions is already
31909
+ // joined for `severity`, so they are two more columns off a row this read
31910
+ // already fetches. They feed the recommended-actions rollup.
31911
+ `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31912
+ d.rule_id AS rule_id, d.category AS category
31145
31913
  FROM inspection_findings f
31146
- JOIN audit_events e ON e.id = f.audit_event_id
31914
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31147
31915
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31148
31916
  WHERE e.started_at >= :from AND e.started_at < :to
31149
31917
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -31154,7 +31922,9 @@ var SqliteSecurityRepository = class {
31154
31922
  return rows.map((r) => ({
31155
31923
  occurredAt: r.occurred_at,
31156
31924
  severity: r.severity,
31157
- actionTaken: r.action_taken
31925
+ actionTaken: r.action_taken,
31926
+ ruleId: r.rule_id,
31927
+ category: r.category
31158
31928
  }));
31159
31929
  }
31160
31930
  };
@@ -31982,6 +32752,7 @@ function openWithPragmas(file2) {
31982
32752
  db.exec("PRAGMA journal_mode = WAL");
31983
32753
  db.exec("PRAGMA busy_timeout = 2000");
31984
32754
  db.exec("PRAGMA foreign_keys = ON");
32755
+ registerSqlFunctions(db);
31985
32756
  } catch (err) {
31986
32757
  closeQuietly(db);
31987
32758
  throw err;
@@ -32011,7 +32782,7 @@ function backupLegacyStore(db, file2) {
32011
32782
  discardStore(file2, backup);
32012
32783
  return backup;
32013
32784
  }
32014
- function openAndInitialize(file2, base) {
32785
+ function openAndInitialize(file2, base, skipTags) {
32015
32786
  let db = openWithPragmas(file2);
32016
32787
  try {
32017
32788
  if (isForeignSqliteLineage(db)) {
@@ -32021,7 +32792,7 @@ function openAndInitialize(file2, base) {
32021
32792
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32022
32793
  );
32023
32794
  }
32024
- applyMigrations(db, file2);
32795
+ applyMigrations(db, file2, { skipTags });
32025
32796
  tightenPerms(file2);
32026
32797
  const policies = new SqlitePoliciesRepository(db);
32027
32798
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32036,6 +32807,7 @@ function openAndInitialize(file2, base) {
32036
32807
  exceptions: new SqliteExceptionsRepository(db),
32037
32808
  resolutions: new SqliteResolutionsRepository(db),
32038
32809
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32810
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32039
32811
  security: new SqliteSecurityRepository(db),
32040
32812
  detections: new SqliteDetectionsRepository(db),
32041
32813
  shares: new SqliteSharesRepository(db),
@@ -32058,7 +32830,8 @@ function openAndInitialize(file2, base) {
32058
32830
  throw err;
32059
32831
  }
32060
32832
  }
32061
- function openLocalDatabase(dir) {
32833
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32834
+ function openLocalDatabase(dir, options = {}) {
32062
32835
  ensureDataDirSync(dir);
32063
32836
  const file2 = join7(dir, DB_FILENAME);
32064
32837
  reapStalePartials(file2);
@@ -32070,6 +32843,7 @@ function openLocalDatabase(dir) {
32070
32843
  installedPacks,
32071
32844
  scanLedger,
32072
32845
  historySync,
32846
+ bodyRetention,
32073
32847
  secretVault,
32074
32848
  exceptions,
32075
32849
  resolutions,
@@ -32093,7 +32867,8 @@ function openLocalDatabase(dir) {
32093
32867
  // `dir` is always `<base>/data` — every caller resolves it through
32094
32868
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32095
32869
  // settings/ and data/, and the pack-policy floor needs both halves.
32096
- dirname2(dir)
32870
+ dirname2(dir),
32871
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32097
32872
  );
32098
32873
  function captureRowId(event) {
32099
32874
  return captureId(
@@ -32286,6 +33061,7 @@ function openLocalDatabase(dir) {
32286
33061
  installedPacks,
32287
33062
  scanLedger,
32288
33063
  historySync,
33064
+ bodyRetention,
32289
33065
  secretVault,
32290
33066
  exceptions,
32291
33067
  resolutions,
@@ -32324,8 +33100,72 @@ function openLocalDatabase(dir) {
32324
33100
  };
32325
33101
  }
32326
33102
 
32327
- // ../../packages/persistence/src/finding-key.ts
33103
+ // ../../packages/persistence/src/egress-wire.ts
32328
33104
  import { createHash as createHash3 } from "crypto";
33105
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33106
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33107
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33108
+ var FILE_URL = /^file:\/\//i;
33109
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33110
+ var SLASH = "/".charCodeAt(0);
33111
+ var GIT_SUFFIX = ".git";
33112
+ function trimSlashes(path) {
33113
+ let start = 0;
33114
+ let end = path.length;
33115
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33116
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33117
+ return path.slice(start, end);
33118
+ }
33119
+ function canonicalGitUrl(url2) {
33120
+ const trimmed = url2.trim();
33121
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33122
+ const scheme = SCHEME_FORM.exec(trimmed);
33123
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33124
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33125
+ if (host === void 0) return trimmed;
33126
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33127
+ const bare = trimSlashes(path);
33128
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33129
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33130
+ }
33131
+ function hashProjectKey(projectKey) {
33132
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33133
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
33134
+ }
33135
+ function toIngestHit(hit) {
33136
+ return {
33137
+ host: hit.host,
33138
+ kind: hit.kind,
33139
+ name: hit.name,
33140
+ category: hit.category,
33141
+ trust: hit.trust,
33142
+ network: hit.network,
33143
+ method: hit.method,
33144
+ transport: hit.transport,
33145
+ url: hit.url,
33146
+ template: hit.template,
33147
+ dataClass: hit.dataClass,
33148
+ site: {
33149
+ file: hit.site.file,
33150
+ line: hit.site.line,
33151
+ dynamic: hit.site.dynamic,
33152
+ vendored: hit.site.vendored
33153
+ }
33154
+ };
33155
+ }
33156
+ function toEgressIngestRequest(input2) {
33157
+ const { hits, droppedFiles } = capHits(input2.hits, input2.reconcile.mode);
33158
+ const reconcile = withoutDroppedFiles(input2.reconcile, droppedFiles);
33159
+ return {
33160
+ projectKey: hashProjectKey(input2.projectKey),
33161
+ project: input2.project,
33162
+ reconcile,
33163
+ hits: hits.map(toIngestHit)
33164
+ };
33165
+ }
33166
+
33167
+ // ../../packages/persistence/src/finding-key.ts
33168
+ import { createHash as createHash4 } from "crypto";
32329
33169
 
32330
33170
  // ../../packages/persistence/src/fingerprint.ts
32331
33171
  import { createHmac, randomBytes } from "crypto";
@@ -32366,14 +33206,50 @@ function readFingerprintKey(dataDir2) {
32366
33206
  return parseKeyFile(raw);
32367
33207
  }
32368
33208
 
32369
- // ../../packages/persistence/src/history-preview.ts
32370
- import { existsSync as existsSync4 } from "fs";
33209
+ // ../../packages/persistence/src/forward-health.ts
33210
+ import { readFileSync as readFileSync7 } from "fs";
32371
33211
  import { join as join9 } from "path";
33212
+ var FAILURES = /* @__PURE__ */ new Set([
33213
+ "unauthorized",
33214
+ "forbidden",
33215
+ "unreachable"
33216
+ ]);
33217
+ var BREAKER_COOLDOWN_MS = 3e4;
33218
+ function parseForwardHealth(raw, nowMs) {
33219
+ try {
33220
+ const parsed2 = JSON.parse(raw);
33221
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33222
+ const record2 = parsed2;
33223
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33224
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33225
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33226
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33227
+ } catch {
33228
+ return null;
33229
+ }
33230
+ }
33231
+ function isForwardPaused(health, nowMs) {
33232
+ const openedAtMs = health?.openedAtMs ?? null;
33233
+ if (openedAtMs === null) return false;
33234
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33235
+ }
33236
+
33237
+ // ../../packages/persistence/src/history-backfill.ts
33238
+ import { existsSync as existsSync4 } from "fs";
33239
+ import { join as join10 } from "path";
33240
+
33241
+ // ../../packages/persistence/src/history-preview.ts
33242
+ import { existsSync as existsSync5 } from "fs";
33243
+ import { join as join11 } from "path";
32372
33244
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32373
33245
 
33246
+ // ../../packages/persistence/src/history-sync-state.ts
33247
+ import { readFileSync as readFileSync8 } from "fs";
33248
+ import { join as join12 } from "path";
33249
+
32374
33250
  // ../../packages/persistence/src/store-symlinks.ts
32375
- import { existsSync as existsSync5, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32376
- import { dirname as dirname3, join as join10, resolve } from "path";
33251
+ import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
33252
+ import { dirname as dirname3, join as join13, resolve } from "path";
32377
33253
 
32378
33254
  // ../../packages/persistence/src/vault/crypto.ts
32379
33255
  import {
@@ -32387,20 +33263,20 @@ import {
32387
33263
  // ../../packages/persistence/src/vault/key-provider.ts
32388
33264
  import { execFileSync } from "child_process";
32389
33265
  import { randomBytes as randomBytes2 } from "crypto";
32390
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32391
- import { join as join11 } from "path";
33266
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33267
+ import { join as join14 } from "path";
32392
33268
 
32393
33269
  // ../../packages/persistence/src/vault/vault.ts
32394
33270
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32395
33271
 
32396
33272
  // ../../packages/persistence/src/warn-era-cap.ts
32397
- import { existsSync as existsSync6, writeFileSync as writeFileSync4 } from "fs";
32398
- import { join as join12 } from "path";
33273
+ import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
33274
+ import { join as join15 } from "path";
32399
33275
  var MARKER = "warn-era-capped";
32400
33276
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32401
33277
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32402
- const marker = join12(dataDir2, MARKER);
32403
- if (existsSync6(marker)) return { capped: 0, skipped: "already-run" };
33278
+ const marker = join15(dataDir2, MARKER);
33279
+ if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32404
33280
  const capped = db.policies.capCategoryActions();
32405
33281
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
32406
33282
  `, { mode: DATA_FILE_MODE });
@@ -32459,8 +33335,8 @@ function resolveProvider() {
32459
33335
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32460
33336
  try {
32461
33337
  ensureLayoutDirSync(base);
32462
- const settingsFile = join13(settingsDir(base), "settings.json");
32463
- if (existsSync7(settingsFile)) tightenFile(settingsFile);
33338
+ const settingsFile = join16(settingsDir(base), "settings.json");
33339
+ if (existsSync8(settingsFile)) tightenFile(settingsFile);
32464
33340
  } catch {
32465
33341
  }
32466
33342
  migrateLegacyLayout(base);
@@ -32483,9 +33359,9 @@ function resolveProviderSafe(resolveProviderFn) {
32483
33359
  }
32484
33360
 
32485
33361
  // ../../packages/plugin-sdk/src/config-inventory.ts
32486
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33362
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32487
33363
  import { homedir as homedir2 } from "os";
32488
- import { basename as basename3, join as join15 } from "path";
33364
+ import { basename as basename3, join as join18 } from "path";
32489
33365
 
32490
33366
  // ../../packages/detections/src/egress/registry.ts
32491
33367
  var EXTRACTOR_VERSION = "1";
@@ -35529,8 +36405,8 @@ function scanText(text, ruleVersions) {
35529
36405
  }
35530
36406
 
35531
36407
  // ../../packages/plugin-sdk/src/repo.ts
35532
- import { existsSync as existsSync8, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
35533
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join14, sep as sep2 } from "path";
36408
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
36409
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
35534
36410
  function resolveRepoIdentity(cwd) {
35535
36411
  try {
35536
36412
  const root = findGitRoot(cwd);
@@ -35552,36 +36428,36 @@ function resolveRepoIdentity(cwd) {
35552
36428
  function findGitRoot(start) {
35553
36429
  let dir = start;
35554
36430
  for (; ; ) {
35555
- if (existsSync8(join14(dir, ".git"))) return dir;
36431
+ if (existsSync9(join17(dir, ".git"))) return dir;
35556
36432
  const parent = dirname4(dir);
35557
36433
  if (parent === dir) return void 0;
35558
36434
  dir = parent;
35559
36435
  }
35560
36436
  }
35561
36437
  function resolveGitContext(root) {
35562
- const dotGit = join14(root, ".git");
36438
+ const dotGit = join17(root, ".git");
35563
36439
  try {
35564
36440
  if (statSync6(dotGit).isDirectory()) {
35565
- return { configPath: join14(dotGit, "config"), headRoot: root };
36441
+ return { configPath: join17(dotGit, "config"), headRoot: root };
35566
36442
  }
35567
36443
  } catch {
35568
36444
  return void 0;
35569
36445
  }
35570
36446
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
35571
36447
  if (!target) return void 0;
35572
- const gitdir = isAbsolute(target) ? target : join14(root, target);
35573
- if (existsSync8(join14(gitdir, "config"))) {
35574
- return { configPath: join14(gitdir, "config"), headRoot: root };
36448
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
36449
+ if (existsSync9(join17(gitdir, "config"))) {
36450
+ return { configPath: join17(gitdir, "config"), headRoot: root };
35575
36451
  }
35576
- const commonRaw = safeRead(join14(gitdir, "commondir"))?.trim();
36452
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
35577
36453
  if (!commonRaw) return void 0;
35578
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join14(gitdir, commonRaw);
36454
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
35579
36455
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
35580
- return { configPath: join14(commonGitDir, "config"), headRoot };
36456
+ return { configPath: join17(commonGitDir, "config"), headRoot };
35581
36457
  }
35582
36458
  function safeRead(path) {
35583
36459
  try {
35584
- return readFileSync8(path, "utf8");
36460
+ return readFileSync10(path, "utf8");
35585
36461
  } catch {
35586
36462
  return void 0;
35587
36463
  }
@@ -35619,17 +36495,52 @@ function slugFromUrl(url2) {
35619
36495
  }
35620
36496
 
35621
36497
  // ../../packages/plugin-sdk/src/events.ts
35622
- import { createHash as createHash4, randomUUID as randomUUID13 } from "crypto";
36498
+ import { createHash as createHash5, randomUUID as randomUUID13 } from "crypto";
35623
36499
 
35624
36500
  // ../../packages/plugin-sdk/src/isolated-scan.ts
35625
- import { existsSync as existsSync9 } from "fs";
36501
+ import { existsSync as existsSync10 } from "fs";
35626
36502
  import { fileURLToPath } from "url";
35627
36503
  import { Worker } from "worker_threads";
35628
36504
 
36505
+ // ../../packages/plugin-sdk/src/host-floor.ts
36506
+ import { readFileSync as readFileSync13 } from "fs";
36507
+ import { join as join20 } from "path";
36508
+
36509
+ // ../../packages/plugin-sdk/src/model-governance.ts
36510
+ import {
36511
+ closeSync as closeSync2,
36512
+ fstatSync,
36513
+ mkdirSync as mkdirSync2,
36514
+ openSync as openSync2,
36515
+ readFileSync as readFileSync12,
36516
+ readSync,
36517
+ writeFileSync as writeFileSync5
36518
+ } from "fs";
36519
+ import { join as join19 } from "path";
36520
+ var TAIL_BYTES = 256 * 1024;
36521
+
36522
+ // ../../packages/plugin-sdk/src/host-floor.ts
36523
+ var HOST_FEATURE = {
36524
+ ModelSwitch: "model-switch",
36525
+ VaultPointerDisplay: "vault-pointer-display"
36526
+ };
36527
+ var HOST_FLOORS = {
36528
+ [HOST_FEATURE.ModelSwitch]: {
36529
+ label: "model-switch protection",
36530
+ hookEvents: ["PreModelSwitch", "PostModelSwitch"],
36531
+ since: "2.1.251"
36532
+ },
36533
+ [HOST_FEATURE.VaultPointerDisplay]: {
36534
+ label: "vault pointer display",
36535
+ hookEvents: ["MessageDisplay"],
36536
+ since: "2.1.152"
36537
+ }
36538
+ };
36539
+
35629
36540
  // ../../packages/plugin-sdk/src/ignore-layers.ts
35630
36541
  var import_ignore = __toESM(require_ignore(), 1);
35631
- import { readFileSync as readFileSync10 } from "fs";
35632
- import { join as join16 } from "path";
36542
+ import { readFileSync as readFileSync14 } from "fs";
36543
+ import { join as join21 } from "path";
35633
36544
 
35634
36545
  // ../../packages/plugin-sdk/src/inventory-resolver.ts
35635
36546
  import { arch, hostname as hostname4, platform, release } from "os";
@@ -35660,30 +36571,17 @@ function resolveInventoryContext(input2) {
35660
36571
  return ctx;
35661
36572
  }
35662
36573
 
35663
- // ../../packages/plugin-sdk/src/model-governance.ts
35664
- import {
35665
- closeSync as closeSync2,
35666
- fstatSync,
35667
- mkdirSync as mkdirSync2,
35668
- openSync as openSync2,
35669
- readFileSync as readFileSync11,
35670
- readSync,
35671
- writeFileSync as writeFileSync5
35672
- } from "fs";
35673
- import { join as join17 } from "path";
35674
- var TAIL_BYTES = 256 * 1024;
35675
-
35676
36574
  // ../../packages/plugin-sdk/src/nudge.ts
35677
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync12, writeFileSync as writeFileSync6 } from "fs";
35678
- import { join as join18 } from "path";
36575
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
36576
+ import { join as join22 } from "path";
35679
36577
 
35680
36578
  // ../../packages/plugin-sdk/src/paths.ts
35681
36579
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
35682
36580
  import { basename as basename4, dirname as dirname5, sep as sep3 } from "path";
35683
36581
 
35684
36582
  // ../../packages/plugin-sdk/src/project-files.ts
35685
- import { existsSync as existsSync10, readdirSync as readdirSync4 } from "fs";
35686
- import { basename as basename5, join as join19 } from "path";
36583
+ import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
36584
+ import { basename as basename5, join as join23 } from "path";
35687
36585
 
35688
36586
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
35689
36587
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -35729,44 +36627,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
35729
36627
 
35730
36628
  // ../../packages/plugin-sdk/src/throttle.ts
35731
36629
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
35732
- import { join as join20 } from "path";
35733
-
35734
- // ../../packages/plugin-runtime/src/attached/egress-wire.ts
35735
- import { createHash as createHash5 } from "crypto";
35736
- function hashProjectKey(projectKey) {
35737
- return createHash5("sha256").update(projectKey, "utf8").digest("hex");
35738
- }
35739
- function toIngestHit(hit) {
35740
- return {
35741
- host: hit.host,
35742
- kind: hit.kind,
35743
- name: hit.name,
35744
- category: hit.category,
35745
- trust: hit.trust,
35746
- network: hit.network,
35747
- method: hit.method,
35748
- transport: hit.transport,
35749
- url: hit.url,
35750
- template: hit.template,
35751
- dataClass: hit.dataClass,
35752
- site: {
35753
- file: hit.site.file,
35754
- line: hit.site.line,
35755
- dynamic: hit.site.dynamic,
35756
- vendored: hit.site.vendored
35757
- }
35758
- };
35759
- }
35760
- function toEgressIngestRequest(input2) {
35761
- const { hits, droppedFiles } = capHits(input2.hits, input2.reconcile.mode);
35762
- const reconcile = withoutDroppedFiles(input2.reconcile, droppedFiles);
35763
- return {
35764
- projectKey: hashProjectKey(input2.projectKey),
35765
- project: input2.project,
35766
- reconcile,
35767
- hits: hits.map(toIngestHit)
35768
- };
35769
- }
36630
+ import { join as join24 } from "path";
35770
36631
 
35771
36632
  // ../../packages/remote/src/http.ts
35772
36633
  import { request as httpRequest } from "http";
@@ -35951,10 +36812,10 @@ function parsed(schema, body, route) {
35951
36812
  }
35952
36813
  function withoutTrailingSlashes(endpoint) {
35953
36814
  let end = endpoint.length;
35954
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
36815
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
35955
36816
  return endpoint.slice(0, end);
35956
36817
  }
35957
- var SLASH = "/".charCodeAt(0);
36818
+ var SLASH2 = "/".charCodeAt(0);
35958
36819
  function createRemoteClient(options) {
35959
36820
  const base = withoutTrailingSlashes(options.endpoint);
35960
36821
  const url2 = (route) => `${base}${route}`;
@@ -36047,6 +36908,7 @@ function createRemoteClient(options) {
36047
36908
  url: url2(ROUTES.shares),
36048
36909
  body: JSON.stringify(validated.data)
36049
36910
  });
36911
+ if (response.status === 404) throw new RemoteRouteAbsent(ROUTES.shares);
36050
36912
  okBody(response);
36051
36913
  },
36052
36914
  async pollCommand() {
@@ -36069,19 +36931,51 @@ function createRemoteClient(options) {
36069
36931
  };
36070
36932
  }
36071
36933
 
36072
- // ../../packages/plugin-runtime/src/attached/failure.ts
36934
+ // ../../packages/remote/src/failure-kind.ts
36073
36935
  function statusOf(err) {
36074
36936
  if (typeof err !== "object" || err === null || !("status" in err)) return null;
36075
36937
  const { status } = err;
36076
36938
  if (typeof status !== "number" || !Number.isInteger(status)) return null;
36077
36939
  return status >= 100 && status <= 599 ? status : null;
36078
36940
  }
36079
- function classifyFailure(err) {
36080
- switch (statusOf(err)) {
36941
+ function nameOf(err) {
36942
+ if (typeof err !== "object" || err === null || !("name" in err)) return null;
36943
+ return typeof err.name === "string" ? err.name : null;
36944
+ }
36945
+ function classifyRemoteFailure(err) {
36946
+ switch (nameOf(err)) {
36947
+ case "RemoteRouteAbsent":
36948
+ return "route-absent";
36949
+ case "RemoteRequestInvalid":
36950
+ return "invalid-request";
36951
+ case "RemoteResponseInvalid":
36952
+ return "rejected";
36953
+ default:
36954
+ break;
36955
+ }
36956
+ const status = statusOf(err);
36957
+ if (status === null) return "unreachable";
36958
+ switch (status) {
36081
36959
  case 401:
36082
36960
  return "unauthorized";
36083
36961
  case 403:
36084
36962
  return "forbidden";
36963
+ case 429:
36964
+ return "unreachable";
36965
+ case 404:
36966
+ return "unreachable";
36967
+ default:
36968
+ return status >= 400 && status <= 499 ? "rejected" : "unreachable";
36969
+ }
36970
+ }
36971
+
36972
+ // ../../packages/plugin-runtime/src/attached/failure.ts
36973
+ function classifyFailure(err) {
36974
+ switch (classifyRemoteFailure(err)) {
36975
+ case "unauthorized":
36976
+ return "unauthorized";
36977
+ case "forbidden":
36978
+ return "forbidden";
36085
36979
  default:
36086
36980
  return "unreachable";
36087
36981
  }
@@ -36103,11 +36997,11 @@ function withTimeout(promise2, ms) {
36103
36997
  }
36104
36998
 
36105
36999
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
36106
- import { readFileSync as readFileSync13 } from "fs";
36107
- import { join as join21 } from "path";
37000
+ import { readFileSync as readFileSync16 } from "fs";
37001
+ import { join as join25 } from "path";
36108
37002
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
36109
37003
  function forwardDropsPath(dataDir2) {
36110
- return join21(dataDir2, FORWARD_DROPS_FILENAME);
37004
+ return join25(dataDir2, FORWARD_DROPS_FILENAME);
36111
37005
  }
36112
37006
  function recordForwardDrops(dataDir2, count, nowMs) {
36113
37007
  if (count <= 0) return;
@@ -36125,7 +37019,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
36125
37019
  }
36126
37020
  function readForwardDrops(dataDir2) {
36127
37021
  try {
36128
- const parsed2 = JSON.parse(readFileSync13(forwardDropsPath(dataDir2), "utf8"));
37022
+ const parsed2 = JSON.parse(readFileSync16(forwardDropsPath(dataDir2), "utf8"));
36129
37023
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
36130
37024
  const record2 = parsed2;
36131
37025
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -36143,9 +37037,8 @@ function readForwardDrops(dataDir2) {
36143
37037
 
36144
37038
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
36145
37039
  import { randomUUID as randomUUID15 } from "crypto";
36146
- import { readFileSync as readFileSync14 } from "fs";
36147
37040
  import { readFile, rename, writeFile } from "fs/promises";
36148
- import { join as join22 } from "path";
37041
+ import { join as join26 } from "path";
36149
37042
  function isInvalidRequest(err) {
36150
37043
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
36151
37044
  }
@@ -36159,31 +37052,12 @@ function isServerRejection(err) {
36159
37052
  var FORWARD_BUDGET_MS = 1500;
36160
37053
  var DECISION_PATH_BUDGET_MS = 800;
36161
37054
  var BREAKER_FAILURE_THRESHOLD = 3;
36162
- var BREAKER_COOLDOWN_MS = 3e4;
36163
37055
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
36164
- var FAILURES = /* @__PURE__ */ new Set([
36165
- "unauthorized",
36166
- "forbidden",
36167
- "unreachable"
36168
- ]);
36169
37056
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
36170
37057
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
36171
- function parseBreakerState(raw, nowMs) {
36172
- try {
36173
- const parsed2 = JSON.parse(raw);
36174
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
36175
- const record2 = parsed2;
36176
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
36177
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
36178
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
36179
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
36180
- } catch {
36181
- return null;
36182
- }
36183
- }
36184
37058
  function createForwardPolicy(deps) {
36185
37059
  const now = deps.now ?? (() => Date.now());
36186
- const file2 = join22(deps.dir, STATE_FILENAME);
37060
+ const file2 = join26(deps.dir, STATE_FILENAME);
36187
37061
  let state = null;
36188
37062
  let loading = null;
36189
37063
  async function readState() {
@@ -36193,7 +37067,7 @@ function createForwardPolicy(deps) {
36193
37067
  } catch {
36194
37068
  return { ...CLOSED };
36195
37069
  }
36196
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
37070
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
36197
37071
  }
36198
37072
  async function load() {
36199
37073
  if (state !== null) return state;
@@ -36239,7 +37113,7 @@ function createForwardPolicy(deps) {
36239
37113
  };
36240
37114
  const at = now();
36241
37115
  if (current.openedAtMs !== null) {
36242
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
37116
+ if (isForwardPaused(current, at)) {
36243
37117
  return { ok: false, reason: "breaker-open" };
36244
37118
  }
36245
37119
  await persist({
@@ -36776,7 +37650,18 @@ var AttachedDataGateway = class {
36776
37650
  // and the spread above would otherwise drop the field silently — which is
36777
37651
  // exactly what it did, leaving the whole control inert on every device
36778
37652
  // while every test around it stayed green.
36779
- prohibitedModels: cached2.prohibitedModels
37653
+ prohibitedModels: cached2.prohibitedModels,
37654
+ // NAMED for the same reason as the line above, and it is the same defect
37655
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
37656
+ // only the cache carries is dropped in silence. That is what left
37657
+ // `prohibitedModels` inert on every attached device with every test
37658
+ // around it green.
37659
+ //
37660
+ // Taken from the cache rather than merged here, because merging it needs
37661
+ // the device's own SETTING — which is not a bundle field and is not in
37662
+ // scope at this seam. The runtime does that merge, raise-only, where both
37663
+ // values are in hand (createPluginRuntime's ensureInitialized).
37664
+ redactFallback: cached2.redactFallback
36780
37665
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
36781
37666
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
36782
37667
  // it emits, so an 'authored' policy arriving from the control plane
@@ -36904,10 +37789,6 @@ function toolAuditEvent(input2) {
36904
37789
  };
36905
37790
  }
36906
37791
 
36907
- // ../../packages/plugin-runtime/src/attached/history-state.ts
36908
- import { readFileSync as readFileSync15 } from "fs";
36909
- import { join as join23 } from "path";
36910
-
36911
37792
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
36912
37793
  import { createHash as createHash6 } from "crypto";
36913
37794
  import { hostname as hostname5 } from "os";
@@ -36916,6 +37797,10 @@ import { hostname as hostname5 } from "os";
36916
37797
  var CORRELATION_ID = EventMetadata.shape.correlationId;
36917
37798
  var TRACE_ID = EventMetadata.shape.traceId;
36918
37799
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
37800
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
37801
+
37802
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
37803
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
36919
37804
 
36920
37805
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
36921
37806
  import { spawn } from "child_process";
@@ -36923,14 +37808,14 @@ import { fileURLToPath as fileURLToPath2 } from "url";
36923
37808
  var HISTORY_SYNC_THROTTLE_MS = 5 * 60 * 1e3;
36924
37809
 
36925
37810
  // ../../packages/plugin-runtime/src/attached/plugin-block.ts
36926
- import { readFileSync as readFileSync16 } from "fs";
37811
+ import { readFileSync as readFileSync17 } from "fs";
36927
37812
  var manifestBuildCache = /* @__PURE__ */ new Map();
36928
37813
  function readManifestBuild(manifestUrl, packageName) {
36929
37814
  const key = manifestUrl.href;
36930
37815
  if (!manifestBuildCache.has(key)) {
36931
37816
  let build;
36932
37817
  try {
36933
- const manifest = JSON.parse(readFileSync16(manifestUrl, "utf8"));
37818
+ const manifest = JSON.parse(readFileSync17(manifestUrl, "utf8"));
36934
37819
  build = typeof manifest.version === "string" && manifest.version.length > 0 ? { package: packageName, version: manifest.version } : void 0;
36935
37820
  } catch {
36936
37821
  build = void 0;
@@ -36957,7 +37842,7 @@ function createPluginBlock(build, policyStore) {
36957
37842
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
36958
37843
  import { randomUUID as randomUUID16 } from "crypto";
36959
37844
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
36960
- import { join as join24 } from "path";
37845
+ import { join as join27 } from "path";
36961
37846
 
36962
37847
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
36963
37848
  import { rename as rename2 } from "fs/promises";
@@ -36981,7 +37866,7 @@ async function publishByRename(tmp, file2, move = rename2) {
36981
37866
 
36982
37867
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
36983
37868
  function createPolicyStore(dir = dataDir()) {
36984
- const file2 = join24(dir, "policy-cache.json");
37869
+ const file2 = join27(dir, "policy-cache.json");
36985
37870
  async function read() {
36986
37871
  try {
36987
37872
  const raw = await readFile2(file2, "utf8");
@@ -37212,11 +38097,11 @@ function readStorePosture(dbPath2) {
37212
38097
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
37213
38098
  import { randomUUID as randomUUID17 } from "crypto";
37214
38099
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
37215
- import { join as join25 } from "path";
38100
+ import { join as join28 } from "path";
37216
38101
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
37217
38102
  function createPostureStore(dir = settingsDir(), legacyDir) {
37218
- const file2 = join25(dir, "posture-state.json");
37219
- const legacyFile = legacyDir === void 0 ? null : join25(legacyDir, "posture-state.json");
38103
+ const file2 = join28(dir, "posture-state.json");
38104
+ const legacyFile = legacyDir === void 0 ? null : join28(legacyDir, "posture-state.json");
37220
38105
  async function persist(state) {
37221
38106
  await ensureDataDir(dir);
37222
38107
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -37284,8 +38169,8 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
37284
38169
  }
37285
38170
 
37286
38171
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
37287
- import { readFileSync as readFileSync17 } from "fs";
37288
- import { join as join26 } from "path";
38172
+ import { readFileSync as readFileSync18 } from "fs";
38173
+ import { join as join29 } from "path";
37289
38174
 
37290
38175
  // ../../packages/plugin-runtime/src/attached/status.ts
37291
38176
  var REFUSAL_LINES = {
@@ -37306,6 +38191,14 @@ import { spawn as spawn2 } from "child_process";
37306
38191
  import { fileURLToPath as fileURLToPath3 } from "url";
37307
38192
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
37308
38193
 
38194
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
38195
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
38196
+
38197
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
38198
+ import { spawn as spawn3 } from "child_process";
38199
+ import { fileURLToPath as fileURLToPath4 } from "url";
38200
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
38201
+
37309
38202
  // ../../packages/plugin-runtime/src/attached/factory.ts
37310
38203
  import { hostname as hostname6 } from "os";
37311
38204
 
@@ -37769,13 +38662,13 @@ import {
37769
38662
  fstatSync as fstatSync2,
37770
38663
  mkdirSync as mkdirSync5,
37771
38664
  openSync as openSync3,
37772
- readFileSync as readFileSync18,
38665
+ readFileSync as readFileSync19,
37773
38666
  readSync as readSync2,
37774
38667
  writeFileSync as writeFileSync8
37775
38668
  } from "fs";
37776
- import { join as join27 } from "path";
38669
+ import { join as join30 } from "path";
37777
38670
  function offsetsDir(dataDir2) {
37778
- return join27(dataDir2, "usage-offsets");
38671
+ return join30(dataDir2, "usage-offsets");
37779
38672
  }
37780
38673
  var SAFE_SESSION_ID = /^[A-Za-z0-9._-]+$/;
37781
38674
  function safeSessionId(sessionId) {
@@ -37785,11 +38678,11 @@ function safeSessionId(sessionId) {
37785
38678
  return createHash7("sha256").update(sessionId).digest("hex");
37786
38679
  }
37787
38680
  function offsetPath(dataDir2, sessionId) {
37788
- return join27(offsetsDir(dataDir2), safeSessionId(sessionId));
38681
+ return join30(offsetsDir(dataDir2), safeSessionId(sessionId));
37789
38682
  }
37790
38683
  function readOffset(dataDir2, sessionId) {
37791
38684
  try {
37792
- const raw = readFileSync18(offsetPath(dataDir2, sessionId), "utf8");
38685
+ const raw = readFileSync19(offsetPath(dataDir2, sessionId), "utf8");
37793
38686
  const parsed2 = JSON.parse(raw);
37794
38687
  if (typeof parsed2 === "object" && parsed2 !== null) {
37795
38688
  const rec = parsed2;
@@ -37847,9 +38740,9 @@ function readTail(transcriptPath, startOffset) {
37847
38740
  }
37848
38741
 
37849
38742
  // src/history/transcripts.ts
37850
- import { closeSync as closeSync4, openSync as openSync4, readdirSync as readdirSync5, readFileSync as readFileSync19, readSync as readSync3, statSync as statSync10 } from "fs";
38743
+ import { closeSync as closeSync4, openSync as openSync4, readdirSync as readdirSync5, readFileSync as readFileSync20, readSync as readSync3, statSync as statSync10 } from "fs";
37851
38744
  import { homedir as homedir3 } from "os";
37852
- import { join as join28 } from "path";
38745
+ import { join as join31 } from "path";
37853
38746
  function isRecord(value) {
37854
38747
  return typeof value === "object" && value !== null;
37855
38748
  }