@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
  // src/remediation/entry.ts
495
- import { readFileSync as readFileSync23 } from "fs";
496
- import { fileURLToPath as fileURLToPath5 } from "url";
495
+ import { readFileSync as readFileSync24 } from "fs";
496
+ import { fileURLToPath as fileURLToPath6 } from "url";
497
497
 
498
498
  // ../../packages/persistence/src/attached-derived.ts
499
499
  import { rmSync } from "fs";
@@ -506,6 +506,14 @@ var ATTACHED_FORWARD_DROPS_FILENAME = "attached-forward-drops.json";
506
506
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
507
507
  import { join as join2 } from "path";
508
508
 
509
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
510
+ var DEFERRED_MIGRATION_TAGS = [
511
+ "0031_audit_capture_by_time_index",
512
+ "0032_audit_capture_by_id_index",
513
+ "0033_audit_capture_location_index",
514
+ "0034_findings_read_indexes"
515
+ ];
516
+
509
517
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
510
518
  var SQLITE_MIGRATIONS = [
511
519
  {
@@ -623,6 +631,30 @@ var SQLITE_MIGRATIONS = [
623
631
  {
624
632
  tag: "0028_activity_session_probe_indexes",
625
633
  sql: "CREATE INDEX `idx_audit_session_prompt` ON `audit_events` (`root_session_id`) WHERE event_type = 'prompt';--> statement-breakpoint\nCREATE INDEX `idx_audit_session_share` ON `audit_events` (`root_session_id`) WHERE event_type = 'share';--> statement-breakpoint\nCREATE INDEX `idx_audit_ended_at` ON `audit_events` (`ended_at`,`root_session_id`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\nCREATE INDEX `idx_audit_session_ended` ON `audit_events` (`root_session_id`,`ended_at`) WHERE ended_at IS NOT NULL;--> statement-breakpoint\n-- Expression index for the activity list's turns rollup: a live-captured\n-- session's turns are the DISTINCT `run_key` across its `llm_call` leaves, and\n-- carrying the extracted key in the index answers that count from the index\n-- alone instead of parsing every leaf's attribute bag. Written by hand, as\n-- 0013's `idx_audit_code_change_path` was: drizzle-kit cannot emit an\n-- expression containing a comma, so this index is not declared in sqlite.ts.\nCREATE INDEX `idx_audit_session_run_key` ON `audit_events` (`root_session_id`, json_extract(`attributes`, '$.run_key')) WHERE `event_type` = 'llm_call';\n"
634
+ },
635
+ {
636
+ tag: "0029_audit_capture_rollup_index",
637
+ sql: "CREATE INDEX `idx_audit_capture_rollup` ON `audit_events` (`event_type`,`started_at`,`repo`,`id`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
638
+ },
639
+ {
640
+ tag: "0030_audit_content_expiry",
641
+ sql: "ALTER TABLE `audit_events` ADD `content_expired_at` integer;--> statement-breakpoint\nCREATE INDEX `idx_audit_expirable_body` ON `audit_events` (`started_at`) WHERE content IS NOT NULL;"
642
+ },
643
+ {
644
+ tag: "0031_audit_capture_by_time_index",
645
+ sql: "CREATE INDEX `idx_audit_capture_by_time` ON `audit_events` (`started_at`,`id`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
646
+ },
647
+ {
648
+ tag: "0032_audit_capture_by_id_index",
649
+ sql: "CREATE INDEX `idx_audit_capture_by_id` ON `audit_events` (`id`,`started_at`,`event_type`,`root_session_id`,`source_tool`,`repo`,`file_path`,`tool_name`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
650
+ },
651
+ {
652
+ tag: "0033_audit_capture_location_index",
653
+ sql: "CREATE INDEX `idx_audit_capture_location` ON `audit_events` (`repo`,`file_path`,`started_at`,`id`,`event_type`) WHERE event_type IN ('prompt','response','code_change','tool_use');"
654
+ },
655
+ {
656
+ tag: "0034_findings_read_indexes",
657
+ sql: "CREATE INDEX `idx_inspection_definitions_rule` ON `inspection_definitions` (`rule_id`,`severity`,`category`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_def` ON `inspection_findings` (`inspection_definition_id`,`audit_event_id`);--> statement-breakpoint\nCREATE INDEX `idx_inspection_findings_event_cover` ON `inspection_findings` (`audit_event_id`,`inspection_definition_id`,`action_taken`,`finding_key`,`id`);"
626
658
  }
627
659
  ];
628
660
 
@@ -20670,6 +20702,15 @@ var FindingCategory = external_exports.enum([
20670
20702
  ]).meta({ id: "FindingCategory" });
20671
20703
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20672
20704
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20705
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20706
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20707
+ var FindingDelivery = external_exports.object({
20708
+ state: FindingDeliveryState,
20709
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20710
+ at: external_exports.iso.datetime().optional(),
20711
+ // Only on `not_sent`, and only when a known reason was recorded.
20712
+ reason: SyncFailureReason.optional()
20713
+ }).meta({ id: "FindingDelivery" });
20673
20714
  var ResolutionMethod = external_exports.enum([
20674
20715
  "enforced-in-flight",
20675
20716
  "fixed-at-source",
@@ -20726,7 +20767,10 @@ var FindingInstance = external_exports.object({
20726
20767
  // The session that event belongs to, when it has one — the seam a
20727
20768
  // per-instance "view session" link needs. Absent for events captured
20728
20769
  // outside a session.
20729
- sessionId: external_exports.string().optional()
20770
+ sessionId: external_exports.string().optional(),
20771
+ // The delivery state of the event above (see FindingDelivery). Optional so
20772
+ // readers that do not project it stay valid.
20773
+ delivery: FindingDelivery.optional()
20730
20774
  }).meta({ id: "FindingInstance" });
20731
20775
  var FindingGroup = external_exports.object({
20732
20776
  id: external_exports.string(),
@@ -20743,13 +20787,11 @@ var FindingGroup = external_exports.object({
20743
20787
  latestDetectedAt: external_exports.iso.datetime(),
20744
20788
  instances: external_exports.array(FindingInstance),
20745
20789
  // Derived from instances' statuses with open-dominates precedence (see
20746
- // buildFindingGroups). Undefined only when no instance carries a status.
20790
+ // foldGroupStatus). Undefined only when no instance carries a status.
20747
20791
  status: FindingStatus.optional(),
20748
- // The distinct people across the WHOLE group, not just the `instances`
20749
- // preview — from the store's whole-group aggregate when it supplies one,
20750
- // else folded from the rows (see buildFindingGroups). Undefined when no
20751
- // instance carries a user, or when the store supplied whole-group folds
20752
- // without one.
20792
+ // The distinct people across the WHOLE group, not just the instances
20793
+ // carried here. Undefined when no instance carries a user, or when the
20794
+ // store supplied whole-group folds without one.
20753
20795
  users: external_exports.array(FindingUser).optional()
20754
20796
  }).meta({ id: "FindingGroup" });
20755
20797
  var FindingStats = external_exports.object({
@@ -20778,21 +20820,34 @@ var FindingFacets = external_exports.object({
20778
20820
  // counted under no value.
20779
20821
  status: external_exports.array(FindingFacetItem),
20780
20822
  // Host tool (attributes.tool_name). Present only on the instance-level
20781
- // reads, which can filter by it; the grouped read omits the dimension
20823
+ // reads, which can filter by it; the type-level read omits the dimension
20782
20824
  // because a group spans tools.
20783
- tool: external_exports.array(FindingFacetItem).optional()
20825
+ tool: external_exports.array(FindingFacetItem).optional(),
20826
+ // Delivery states (FindingDeliveryState). Present only on the
20827
+ // instance-level reads, like `tool`.
20828
+ deployment: external_exports.array(FindingFacetItem).optional()
20784
20829
  }).meta({ id: "FindingFacets" });
20785
- var DEFAULT_GROUPED_FINDINGS_LIMIT = 50;
20786
- var ListGroupedFindingsQuery = external_exports.object({
20830
+ var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20831
+ id: "FindingTypeSummary"
20832
+ });
20833
+ var DEFAULT_FINDING_TYPES_LIMIT = 50;
20834
+ var MAX_FINDING_TYPES_LIMIT = 100;
20835
+ var ListFindingTypesQuery = external_exports.object({
20787
20836
  // NOTE: severity filters by Severity (critical/high/medium/low), not by
20788
- // FindingAction.
20837
+ // FindingAction. It narrows TYPES: a type's severity is the one its newest
20838
+ // firing version carries, and this list pages types.
20839
+ //
20840
+ // That is NOT a claim the findings of a type share it. A rule can hold several
20841
+ // definition versions at different severities, so a type kept by this filter
20842
+ // can hold findings that individually do not match — see totals.findings on
20843
+ // ListFindingTypesResponse, which counts them all.
20789
20844
  severity: external_exports.array(Severity).optional(),
20790
20845
  subtype: external_exports.array(external_exports.string()).optional(),
20791
20846
  provider: external_exports.array(FindingProvider).optional(),
20792
20847
  action: external_exports.array(FindingAction).optional(),
20793
- // Matches a group's DERIVED status (see FindingGroup.status), not its
20794
- // individual instances' — so a filtered group's Status column always reads
20795
- // one of the requested values.
20848
+ // Matches a type's DERIVED status (see FindingGroup.status), not its
20849
+ // individual findings' — so a filtered row's status always reads one of the
20850
+ // requested values.
20796
20851
  status: external_exports.array(FindingStatus).optional(),
20797
20852
  q: external_exports.string().optional(),
20798
20853
  // Scope to findings whose event carries this session id (the Activity page's
@@ -20802,23 +20857,37 @@ var ListGroupedFindingsQuery = external_exports.object({
20802
20857
  // from a time-scoped page (Activity's range) can carry that scope. Absent
20803
20858
  // means all time — this list has no default window.
20804
20859
  from: external_exports.iso.datetime().optional(),
20805
- // A group or instance id that must appear in the page even when the cursor
20806
- // has already advanced past its sort position. This is what keeps the
20807
- // Findings page's one-shot ?finding= deep link resolving once the list
20808
- // paginates: the target group is appended out of sort order rather than
20809
- // scanning forward for it. Never affects totals, facets or the cursor.
20860
+ // A RULE id that must appear in the page even when the cursor has already
20861
+ // advanced past its sort position. This is what keeps the selected type
20862
+ // visible in the list once it paginates: the target is appended out of sort
20863
+ // order rather than scanned forward for. Never affects totals, facets or the
20864
+ // cursor. Unlike the grouped read this replaces, it names a rule only — an
20865
+ // instance id is resolved by `findingInstance`, which is a primary-key seek
20866
+ // and so is not bounded by what any page happens to hold.
20810
20867
  includeId: external_exports.string().optional(),
20811
- groupBy: external_exports.literal("type").optional(),
20812
- limit: external_exports.coerce.number().int().min(1).max(100).optional(),
20868
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_TYPES_LIMIT).optional(),
20813
20869
  cursor: external_exports.string().optional()
20814
20870
  });
20815
- var ListGroupedFindingsResponse = external_exports.object({
20871
+ var ListFindingTypesResponse = external_exports.object({
20816
20872
  totals: external_exports.object({
20873
+ // Findings belonging to the matching TYPES — not findings that each match
20874
+ // the filters. The filters here select types, so a type that survives
20875
+ // contributes its whole instanceCount.
20876
+ //
20877
+ // `status` is the one exception, narrowed per finding via
20878
+ // countInstancesByStatus. `severity`, `provider` and `action` are not, so
20879
+ // this can exceed what the instance read reports for the same filters: a
20880
+ // rule whose severity moved between versions is kept on its newest and
20881
+ // still counts its older findings. Narrowing the other three needs
20882
+ // per-dimension counts the aggregate does not carry today.
20817
20883
  findings: external_exports.number().int().nonnegative(),
20818
- groups: external_exports.number().int().nonnegative()
20884
+ // Counts TYPES, which is the unit this read pages. The instance read's
20885
+ // own totals count findings; the two deliberately answer different
20886
+ // questions and are never summed.
20887
+ types: external_exports.number().int().nonnegative()
20819
20888
  }),
20820
20889
  facets: FindingFacets,
20821
- items: external_exports.array(FindingGroup),
20890
+ items: external_exports.array(FindingTypeSummary),
20822
20891
  nextCursor: external_exports.string().nullable(),
20823
20892
  // Present only on session-scoped queries (`sessionId` set): per ruleId, how
20824
20893
  // many times that rule fired in the session's persisted transcript. Findings
@@ -20826,7 +20895,7 @@ var ListGroupedFindingsResponse = external_exports.object({
20826
20895
  // every firing, so the two numbers legitimately differ — this map lets a
20827
20896
  // session-scoped view show both.
20828
20897
  sessionFirings: external_exports.record(external_exports.string(), external_exports.number().int().nonnegative()).optional()
20829
- }).meta({ id: "ListGroupedFindingsResponse" });
20898
+ }).meta({ id: "ListFindingTypesResponse" });
20830
20899
  var ApplyFindingActionRequest = external_exports.object({
20831
20900
  // 'quarantined' is system-assigned (see FindingAction) — clients may not set
20832
20901
  // it, so it is excluded from the request contract. The mapping helper
@@ -20856,16 +20925,19 @@ var DEFAULT_FLAT_FINDINGS_LIMIT = 50;
20856
20925
  var MAX_FLAT_FINDINGS_LIMIT = 200;
20857
20926
  var ListFindingInstancesQuery = external_exports.object({
20858
20927
  severity: external_exports.array(Severity).optional(),
20859
- // Rule ids, the same vocabulary the grouped list's `subtype` carries.
20928
+ // Rule ids, the same vocabulary the types list's `subtype` carries. Pinning
20929
+ // ONE of them is how the master/detail view scopes its right-hand panel.
20860
20930
  subtype: external_exports.array(external_exports.string()).optional(),
20861
20931
  provider: external_exports.array(FindingProvider).optional(),
20862
20932
  action: external_exports.array(FindingAction).optional(),
20863
20933
  // Matches each instance's OWN derived status (deriveFindingStatus), unlike
20864
- // the grouped query's group-level fold.
20934
+ // the types query's type-level fold.
20865
20935
  status: external_exports.array(FindingStatus).optional(),
20866
20936
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20867
20937
  // where the free-text `q` can only match the rendered "via Bash" label.
20868
20938
  tool: external_exports.array(external_exports.string()).optional(),
20939
+ // The delivery state of each finding's event (see FindingDelivery).
20940
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20869
20941
  // Exact repository / file-path matches, for the drill-down out of the
20870
20942
  // locations view. A row whose event carries no repo/file matches neither.
20871
20943
  repo: external_exports.string().optional(),
@@ -20878,37 +20950,51 @@ var ListFindingInstancesQuery = external_exports.object({
20878
20950
  });
20879
20951
  var ListFindingInstancesResponse = external_exports.object({
20880
20952
  // Instances matching the filters across the whole scope, not just this
20881
- // page — cursor-independent, like the grouped list's totals.
20953
+ // page — cursor-independent, like the types list's totals.
20882
20954
  totals: external_exports.object({ findings: external_exports.number().int().nonnegative() }),
20883
- // Counts in INSTANCES here, where the grouped response counts groups. Each
20955
+ // Counts in INSTANCES here, where the types response counts types. Each
20884
20956
  // dimension still excludes its own filter.
20885
20957
  facets: FindingFacets,
20886
20958
  items: external_exports.array(FindingInstanceDetail),
20887
20959
  nextCursor: external_exports.string().nullable()
20888
20960
  }).meta({ id: "ListFindingInstancesResponse" });
20889
- var FindingLocationFile = external_exports.object({
20890
- // Empty when the instances carried no file path (a prompt or a tool call
20891
- // with no file attribution).
20892
- file: external_exports.string(),
20893
- instanceCount: external_exports.number().int().nonnegative(),
20894
- maxSeverity: Severity,
20895
- latestDetectedAt: external_exports.iso.datetime(),
20896
- // Folded from the instances' derived statuses with the same
20897
- // open-dominates precedence a group uses.
20898
- status: FindingStatus.optional(),
20899
- // Distinct rules seen at this location, capped — the row shows them as
20900
- // chips, and the count is what conveys scale.
20901
- ruleIds: external_exports.array(external_exports.string())
20902
- }).meta({ id: "FindingLocationFile" });
20903
- var FindingLocationRepo = external_exports.object({
20961
+ var ListFindingInstancesPage = external_exports.object({
20962
+ items: external_exports.array(FindingInstanceDetail),
20963
+ nextCursor: external_exports.string().nullable()
20964
+ }).meta({ id: "ListFindingInstancesPage" });
20965
+ var FindingLocationSummary = external_exports.object({
20966
+ // Opaque, stable, minted from the pair by encodeLocationId. It exists
20967
+ // because a location's identity is two values and a URL param carries one:
20968
+ // `?loc=` names a location the way `?rule=` names a type. Only ever compared
20969
+ // for EQUALITY — the page's selection check, this read's `includeId`, the
20970
+ // client's page dedupe — never decoded, and never a sort key.
20971
+ id: external_exports.string(),
20904
20972
  /** Empty when the instances carried no repo attribute. */
20905
20973
  repo: external_exports.string(),
20974
+ // Empty when the instances carried no file path (a prompt, or a tool call
20975
+ // with no file attribution). Both halves empty is a real location — usually
20976
+ // the largest one in a store — and is selectable like any other.
20977
+ file: external_exports.string(),
20906
20978
  instanceCount: external_exports.number().int().nonnegative(),
20979
+ // The WORST severity present, not the first row's. It is this list's primary
20980
+ // sort key, so it is also what explains why a row is where it is, and it is
20981
+ // how a reader decides what to open without opening everything.
20907
20982
  maxSeverity: Severity,
20908
20983
  latestDetectedAt: external_exports.iso.datetime(),
20984
+ // Folded from the instances' derived statuses with the same open-dominates
20985
+ // precedence a group uses, so it answers "is anything left to do here" and
20986
+ // not much more: a location holding 1 open among 40 resolved reads like one
20987
+ // holding 40 open. That loss is accepted — the panel beside this list
20988
+ // carries each finding's own status, and instanceCount sits next to the
20989
+ // badge.
20909
20990
  status: FindingStatus.optional(),
20910
- files: external_exports.array(FindingLocationFile)
20911
- }).meta({ id: "FindingLocationRepo" });
20991
+ // Every distinct rule seen at this location, UNCAPPED — so the length is a
20992
+ // tally rather than a sample and a row can say how many there are. Bounded
20993
+ // by the ruleset, not by the store. The view bounds what it DISPLAYS.
20994
+ ruleIds: external_exports.array(external_exports.string())
20995
+ }).meta({ id: "FindingLocationSummary" });
20996
+ var DEFAULT_FINDING_LOCATIONS_LIMIT = 50;
20997
+ var MAX_FINDING_LOCATIONS_LIMIT = 100;
20912
20998
  var ListFindingLocationsQuery = external_exports.object({
20913
20999
  severity: external_exports.array(Severity).optional(),
20914
21000
  subtype: external_exports.array(external_exports.string()).optional(),
@@ -20918,21 +21004,47 @@ var ListFindingLocationsQuery = external_exports.object({
20918
21004
  // instances that match, and folds its status from those.
20919
21005
  status: external_exports.array(FindingStatus).optional(),
20920
21006
  tool: external_exports.array(external_exports.string()).optional(),
21007
+ // The delivery state of each finding's event (see FindingDelivery).
21008
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20921
21009
  q: external_exports.string().optional(),
20922
21010
  sessionId: external_exports.string().optional(),
20923
21011
  from: external_exports.iso.datetime().optional(),
20924
- limit: external_exports.coerce.number().int().min(1).max(500).optional()
21012
+ // A LOCATION id (see FindingLocationSummary.id) that must appear in the page
21013
+ // even when the cursor has already advanced past its sort position — the
21014
+ // counterpart of ListFindingTypesQuery.includeId, and needed far more often
21015
+ // here. Selecting a row pushes the URL, which re-renders the server and resets
21016
+ // the client's page cache to page 0; with distinct (repo, file) pairs running
21017
+ // into the thousands, a selection sitting off page 0 is the ordinary case
21018
+ // rather than a deep-link corner. Never affects totals, facets or the cursor.
21019
+ includeId: external_exports.string().optional(),
21020
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_LOCATIONS_LIMIT).optional(),
21021
+ cursor: external_exports.string().optional()
20925
21022
  });
20926
21023
  var ListFindingLocationsResponse = external_exports.object({
20927
21024
  totals: external_exports.object({
21025
+ // Findings matching the filters across the whole scope. Unlike the types
21026
+ // read's same-named field this needs no caveat: the filters here narrow
21027
+ // per finding, so this is the sum of every row's instanceCount.
20928
21028
  findings: external_exports.number().int().nonnegative(),
20929
- repos: external_exports.number().int().nonnegative(),
20930
- files: external_exports.number().int().nonnegative()
21029
+ // Counts LOCATIONS, the unit this read pages — the number the paginator
21030
+ // states. The facets beside it count FINDINGS (see below); a surface
21031
+ // showing both says which is which.
21032
+ locations: external_exports.number().int().nonnegative()
20931
21033
  }),
20932
- /** Sorted by max severity, then most recent. */
20933
- items: external_exports.array(FindingLocationRepo),
20934
- /** Whether `limit` truncated the repo list. */
20935
- hasMore: external_exports.boolean()
21034
+ // Counts in FINDINGS, where the types response counts types, each dimension
21035
+ // still excluding its own filter. Deliberately not locations: counting those
21036
+ // needs a set of location keys per dimension per value — memory tracking the
21037
+ // store times the vocabulary, in a read whose scan promises flat memory —
21038
+ // and the cheap per-location version is not an approximation but WRONG. A
21039
+ // location holding {claudecode, block} and {codex, warn} would survive
21040
+ // provider=claudecode AND action=warn, under which no single finding
21041
+ // matches, so the facet would contradict the instanceCount this whole view
21042
+ // rests on. Findings also keep the toolbar in the same unit as the page
21043
+ // tally and the panel it sits above.
21044
+ facets: FindingFacets,
21045
+ /** Sorted by max severity, then most recent, then (repo, file). */
21046
+ items: external_exports.array(FindingLocationSummary),
21047
+ nextCursor: external_exports.string().nullable()
20936
21048
  }).meta({ id: "ListFindingLocationsResponse" });
20937
21049
 
20938
21050
  // ../../packages/schema/src/zod/meta.ts
@@ -21096,6 +21208,10 @@ var CaptureAttributes = external_exports.object({
21096
21208
  // to 'allow' — the enforcement audit trail's link back to the grant that
21097
21209
  // authorized the bypass.
21098
21210
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21211
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21212
+ // join back to the `llm_call` leaf for the same assistant turn.
21213
+ message_id: external_exports.string().optional(),
21214
+ conversation_id: external_exports.string().optional(),
21099
21215
  // Whole milliseconds this capture's inspection blocked its caller — the
21100
21216
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21101
21217
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21104,7 +21220,19 @@ var CaptureAttributes = external_exports.object({
21104
21220
  // inline json_extract and is not itself an optimization.
21105
21221
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21106
21222
  // before the measurement shipped — never present as a placeholder 0.
21107
- inspection_ms: external_exports.number().int().nonnegative().optional()
21223
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21224
+ // What a `redact` this capture could not carry out became instead (see
21225
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21226
+ // degrade actually happened, so absence is the ordinary case rather than a
21227
+ // reader having to distinguish it from a zero.
21228
+ //
21229
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21230
+ // so on a multi-finding row this does not say which finding degraded, and
21231
+ // its presence does not mean the fallback decided the capture's action. A
21232
+ // capture denied by another finding's own Block policy carries `block`
21233
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21234
+ // repeated rather than referenced because a store reader opens this file.
21235
+ redact_degraded_to: ActionTaken.optional()
21108
21236
  }).catchall(external_exports.unknown());
21109
21237
  var ToolCallInspection = external_exports.object({
21110
21238
  ruleId: external_exports.string().min(1),
@@ -21303,7 +21431,17 @@ var AuditEvent = external_exports.object({
21303
21431
  /** `share` to a first-party/internal destination. */
21304
21432
  internal: external_exports.boolean(),
21305
21433
  /** Event needs review (e.g. unverified egress). */
21306
- flagged: external_exports.boolean()
21434
+ flagged: external_exports.boolean(),
21435
+ /**
21436
+ * The body this event's `title` is drawn from was cleared by local body
21437
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21438
+ *
21439
+ * A separate flag rather than a sentinel written into `title`: the title is
21440
+ * rendered text, and a store-layer module that invented display copy for it
21441
+ * would be choosing words the view is supposed to choose. Additive and
21442
+ * defaulted, so an older producer still validates.
21443
+ */
21444
+ bodyExpired: external_exports.boolean().default(false)
21307
21445
  }).meta({ id: "ActivityAuditEvent" });
21308
21446
  var ActivitySessionSummary = external_exports.object({
21309
21447
  id: external_exports.string(),
@@ -22101,6 +22239,14 @@ var ControlPlaneErrorBody = external_exports.object({
22101
22239
  message: external_exports.string().optional()
22102
22240
  }).optional()
22103
22241
  });
22242
+ var RemoteFailureKind = external_exports.enum([
22243
+ "unauthorized",
22244
+ "forbidden",
22245
+ "route-absent",
22246
+ "invalid-request",
22247
+ "rejected",
22248
+ "unreachable"
22249
+ ]);
22104
22250
  var AttachDeviceRequest = external_exports.object({
22105
22251
  // This machine's own continuity id, so re-attaching ROTATES the credential
22106
22252
  // on one machine record instead of producing a second one. Client-minted
@@ -22636,6 +22782,12 @@ var EventMetadata = external_exports.object({
22636
22782
  // to 'allow' — the enforcement audit trail's link back to the grant that
22637
22783
  // authorized the bypass. Absent on captures where no exception applied.
22638
22784
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22785
+ // The assistant message this capture belongs to, and the conversation it sits
22786
+ // in — set by the browser extension's network capture so a stored `response`
22787
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22788
+ // on every other capture path, which has no such id.
22789
+ messageId: external_exports.string().optional(),
22790
+ conversationId: external_exports.string().optional(),
22639
22791
  // How long THIS capture's inspection blocked its caller, in whole
22640
22792
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22641
22793
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22648,7 +22800,37 @@ var EventMetadata = external_exports.object({
22648
22800
  // Absent is also what every pre-measurement client writes, and what a
22649
22801
  // clock failure degrades to — a reader must treat absence as "not measured"
22650
22802
  // and never as a zero, which would read as "inspection is free".
22651
- inspectionMs: external_exports.number().int().nonnegative().optional()
22803
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22804
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22805
+ // workspace's `redactFallback`, applied because the field could not be
22806
+ // masked in place (a shell command, a URL, or any argument on a host whose
22807
+ // hook contract offers no rewrite channel).
22808
+ //
22809
+ // It exists because the action alone cannot say why. A finding recorded as
22810
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22811
+ // assigned Redact on a field that could not take one — and those are
22812
+ // different facts about the same row: the first is a policy the user chose,
22813
+ // the second is a masking the host could not perform. Absent means no
22814
+ // degrade happened, which is every ordinary capture.
22815
+ //
22816
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22817
+ // is the CAPTURE while `actionTaken` is per FINDING:
22818
+ //
22819
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22820
+ // `redact` alongside a finding ASSIGNED the same action stores both
22821
+ // identically and one reason for the pair; attributing it to both
22822
+ // describes the assigned one wrongly, and to neither loses the degrade.
22823
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22824
+ // became, not the reason the capture ended as it did — a capture denied
22825
+ // by some other finding's own Block policy still carries `block` here,
22826
+ // and clearing the workspace's fallback would not have let it through.
22827
+ // Gate on the value against what a fallback can produce; never read the
22828
+ // field's presence as "this was the fallback's doing".
22829
+ //
22830
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22831
+ // Closing either means moving the reason onto the finding row, which
22832
+ // already carries its own action.
22833
+ redactDegradedTo: ActionTaken.optional()
22652
22834
  }).meta({ id: "EventMetadata" });
22653
22835
  var Event = external_exports.object({
22654
22836
  id: external_exports.guid(),
@@ -22758,7 +22940,32 @@ var RotateKeyInput = external_exports.object({
22758
22940
  confirmation: external_exports.string()
22759
22941
  });
22760
22942
 
22943
+ // ../../packages/schema/src/zod/finding-delivery.ts
22944
+ var KNOWN_REASONS = SyncFailureReason.options;
22945
+ function knownReason(value) {
22946
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22947
+ }
22948
+ function deriveFindingDelivery(row) {
22949
+ if (row.kind === "code_change") return { state: "local_scan" };
22950
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22951
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22952
+ }
22953
+ if (row.syncedAt !== null) {
22954
+ const reason = knownReason(row.syncFailure);
22955
+ return {
22956
+ state: "not_sent",
22957
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22958
+ ...reason === void 0 ? {} : { reason }
22959
+ };
22960
+ }
22961
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22962
+ return { state: "never_offered" };
22963
+ }
22964
+
22761
22965
  // ../../packages/schema/src/zod/findings-group-build.ts
22966
+ function lookupOwn(map2, key) {
22967
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22968
+ }
22762
22969
  function toApiAction(dbVal) {
22763
22970
  const map2 = {
22764
22971
  log: "monitored",
@@ -22767,7 +22974,7 @@ function toApiAction(dbVal) {
22767
22974
  warn: "warned",
22768
22975
  allow: "allowed"
22769
22976
  };
22770
- return map2[dbVal] ?? "allowed";
22977
+ return lookupOwn(map2, dbVal) ?? "allowed";
22771
22978
  }
22772
22979
  function toApiCategory(dbVal) {
22773
22980
  if (dbVal === "code_context") return "source_code";
@@ -22775,13 +22982,18 @@ function toApiCategory(dbVal) {
22775
22982
  return parsed2.success ? parsed2.data : "custom";
22776
22983
  }
22777
22984
  function toApiProvider(sourceTool) {
22778
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22985
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22779
22986
  }
22780
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22987
+ var FINDING_STATUS_PRECEDENCE = [
22988
+ "open",
22989
+ "handled",
22990
+ "dismissed",
22991
+ "resolved"
22992
+ ];
22781
22993
  function foldGroupStatus(instanceStatuses) {
22782
22994
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22783
22995
  if (statuses.size === 0) return void 0;
22784
- for (const candidate of STATUS_PRECEDENCE) {
22996
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22785
22997
  if (statuses.has(candidate)) return candidate;
22786
22998
  }
22787
22999
  return void 0;
@@ -22794,139 +23006,62 @@ function deriveFindingStatus(row) {
22794
23006
  if (row.latestResolutionStatus === "dismissed") return "dismissed";
22795
23007
  return "open";
22796
23008
  }
22797
- function distinctUsers(instances) {
22798
- const seen = /* @__PURE__ */ new Set();
22799
- const users = [];
22800
- for (const i of instances) {
22801
- if (i.user === void 0 || seen.has(i.user.id)) continue;
22802
- seen.add(i.user.id);
22803
- users.push(i.user);
22804
- }
22805
- return users;
22806
- }
22807
23009
  function sortUsers(users) {
22808
23010
  return [...users].sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
22809
23011
  }
22810
- function buildFindingGroups(rows, opts = {}) {
22811
- const overrides = opts.overrides;
23012
+ function buildFindingTypes(aggregates, opts = {}) {
22812
23013
  const packNames = opts.packNames;
22813
- const aggregates = opts.aggregates;
22814
- const byRuleId = /* @__PURE__ */ new Map();
22815
- for (const row of rows) {
22816
- const existing = byRuleId.get(row.ruleId);
22817
- if (existing) existing.push(row);
22818
- else byRuleId.set(row.ruleId, [row]);
22819
- }
22820
- const groups = [];
22821
- for (const [ruleId, ruleRows] of byRuleId) {
22822
- const instances = ruleRows.map((r) => {
22823
- const effectiveDbAction = overrides?.get(r.id) ?? r.actionTaken;
22824
- return {
22825
- id: r.id,
22826
- provider: toApiProvider(r.sourceTool),
22827
- repo: r.repo,
22828
- file: r.file,
22829
- ...r.toolName === void 0 ? {} : { toolName: r.toolName },
22830
- ...r.eventId === void 0 ? {} : { eventId: r.eventId },
22831
- ...r.sessionId === void 0 ? {} : { sessionId: r.sessionId },
22832
- ...r.user === void 0 ? {} : { user: r.user },
22833
- action: toApiAction(effectiveDbAction),
22834
- detectedAt: r.occurredAt,
22835
- confidence: r.confidence,
22836
- status: r.status
22837
- };
22838
- });
22839
- const agg = aggregates?.get(ruleId);
22840
- const users = agg ? sortUsers(agg.users ?? []) : distinctUsers(instances);
22841
- const latestDetectedAt = agg?.latestDetectedAt ?? ruleRows.reduce(
22842
- (max, r) => r.occurredAt > max ? r.occurredAt : max,
22843
- ruleRows[0]?.occurredAt ?? (/* @__PURE__ */ new Date(0)).toISOString()
22844
- );
22845
- const seenProviders = /* @__PURE__ */ new Set();
22846
- const providers = (agg ? [...new Set(agg.sourceTools.map(toApiProvider))].sort() : instances.map((i) => i.provider)).filter((p) => {
22847
- if (seenProviders.has(p)) return false;
22848
- seenProviders.add(p);
22849
- return true;
22850
- });
22851
- const actionSet = new Set(
22852
- agg ? agg.actionsTaken.map(toApiAction) : instances.map((i) => i.action)
22853
- );
23014
+ const types = [];
23015
+ for (const [ruleId, agg] of aggregates) {
23016
+ const users = sortUsers(agg.users ?? []);
23017
+ const providers = [...new Set(agg.sourceTools.map(toApiProvider))].sort();
23018
+ const actionSet = new Set(agg.actionsTaken.map(toApiAction));
22854
23019
  const aggregateAction = actionSet.size === 1 ? [...actionSet][0] ?? null : null;
22855
- const severity = ruleRows[0]?.severity ?? "low";
22856
- const detection = {
22857
- id: ruleId,
22858
- name: packNames?.get(ruleId) ?? null
22859
- };
22860
- const apiCategory = toApiCategory(ruleRows[0]?.category ?? "custom");
22861
- const policy = { id: `category:${apiCategory}`, name: apiCategory };
22862
- const match = {
22863
- maskedValue: ruleRows[0]?.maskedMatch ?? "",
22864
- contextPrefix: ""
22865
- // empty (pending privacy review)
22866
- };
22867
- const status = foldGroupStatus(
22868
- agg ? agg.statusInputs.map(deriveFindingStatus) : instances.map((i) => i.status)
22869
- );
22870
- const group = {
23020
+ const apiCategory = toApiCategory(agg.category ?? "custom");
23021
+ const type = {
22871
23022
  id: ruleId,
22872
23023
  category: apiCategory,
22873
23024
  subtype: ruleId,
22874
23025
  // human label comes with pack metadata later
22875
- severity,
22876
- match,
22877
- detection,
22878
- policy,
22879
- instanceCount: agg?.instanceCount ?? instances.length,
23026
+ severity: agg.severity ?? "low",
23027
+ detection: { id: ruleId, name: packNames?.get(ruleId) ?? null },
23028
+ policy: { id: `category:${apiCategory}`, name: apiCategory },
23029
+ instanceCount: agg.instanceCount,
22880
23030
  providers,
22881
23031
  aggregateAction,
22882
- latestDetectedAt,
22883
- instances,
22884
- status,
23032
+ latestDetectedAt: agg.latestDetectedAt,
23033
+ status: foldGroupStatus(agg.statusInputs.map(deriveFindingStatus)),
22885
23034
  ...users.length > 0 ? { users } : {}
22886
23035
  };
22887
- if (agg) {
22888
- actionsCache.set(group, [...actionSet]);
22889
- if (agg.searchText !== void 0) {
22890
- haystackCache.set(group, buildHaystack(group, agg.searchText));
22891
- }
23036
+ actionsCache.set(type, [...actionSet]);
23037
+ if (agg.searchText !== void 0) {
23038
+ haystackCache.set(type, buildHaystack(type, agg.searchText));
22892
23039
  }
22893
- groups.push(group);
23040
+ types.push(type);
22894
23041
  }
22895
- return groups;
23042
+ return types;
22896
23043
  }
22897
23044
  var haystackCache = /* @__PURE__ */ new WeakMap();
22898
- function buildHaystack(g, extra) {
23045
+ function buildHaystack(t, extra) {
22899
23046
  return [
22900
- g.subtype,
22901
- g.category,
22902
- g.match.maskedValue,
22903
- g.policy.name,
22904
- g.id,
22905
- ...g.instances.map((i) => i.repo),
22906
- ...g.instances.map((i) => i.file),
22907
- ...g.instances.map((i) => i.toolName ? `via ${i.toolName}` : ""),
22908
- ...g.instances.map((i) => i.id),
22909
- // The people: the whole group's list when the store folded one, plus the
22910
- // preview's own — the two overlap, and a haystack does not mind.
22911
- ...(g.users ?? []).map((u) => u.name),
22912
- ...g.instances.map((i) => i.user?.name ?? ""),
23047
+ t.subtype,
23048
+ t.category,
23049
+ t.policy.name,
23050
+ t.id,
23051
+ ...(t.users ?? []).map((u) => u.name),
22913
23052
  ...extra === void 0 ? [] : [extra]
22914
23053
  ].join(" ").toLowerCase();
22915
23054
  }
22916
- function groupHaystack(g) {
22917
- const cached2 = haystackCache.get(g);
23055
+ function typeHaystack(t) {
23056
+ const cached2 = haystackCache.get(t);
22918
23057
  if (cached2 !== void 0) return cached2;
22919
- const haystack = buildHaystack(g);
22920
- haystackCache.set(g, haystack);
23058
+ const haystack = buildHaystack(t);
23059
+ haystackCache.set(t, haystack);
22921
23060
  return haystack;
22922
23061
  }
22923
23062
  var actionsCache = /* @__PURE__ */ new WeakMap();
22924
- function groupActions(g) {
22925
- const cached2 = actionsCache.get(g);
22926
- if (cached2 !== void 0) return cached2;
22927
- const actions = [...new Set(g.instances.map((i) => i.action))];
22928
- actionsCache.set(g, actions);
22929
- return actions;
23063
+ function typeActions(t) {
23064
+ return actionsCache.get(t) ?? [];
22930
23065
  }
22931
23066
  function countInstancesByStatus(statusInputs, statuses) {
22932
23067
  const statusSet = new Set(statuses);
@@ -22937,8 +23072,8 @@ function countInstancesByStatus(statusInputs, statuses) {
22937
23072
  }
22938
23073
  return sum;
22939
23074
  }
22940
- function applyFindingFilters(groups, opts) {
22941
- let filtered = groups;
23075
+ function applyFindingFilters(types, opts) {
23076
+ let filtered = types;
22942
23077
  if (opts.severity && opts.severity.length > 0) {
22943
23078
  const sevSet = new Set(opts.severity);
22944
23079
  filtered = filtered.filter((g) => sevSet.has(g.severity));
@@ -22949,7 +23084,7 @@ function applyFindingFilters(groups, opts) {
22949
23084
  }
22950
23085
  if (opts.actions && opts.actions.length > 0) {
22951
23086
  const actionSet = new Set(opts.actions);
22952
- filtered = filtered.filter((g) => groupActions(g).some((a) => actionSet.has(a)));
23087
+ filtered = filtered.filter((t) => typeActions(t).some((a) => actionSet.has(a)));
22953
23088
  }
22954
23089
  if (opts.subtype && opts.subtype.length > 0) {
22955
23090
  const subtypeSet = new Set(opts.subtype);
@@ -22961,26 +23096,31 @@ function applyFindingFilters(groups, opts) {
22961
23096
  }
22962
23097
  if (opts.q) {
22963
23098
  const q = opts.q.toLowerCase();
22964
- filtered = filtered.filter((g) => groupHaystack(g).includes(q));
23099
+ filtered = filtered.filter((t) => typeHaystack(t).includes(q));
22965
23100
  }
22966
23101
  return filtered;
22967
23102
  }
22968
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22969
- var SEVERITY_RANK = SEVERITY_ORDER;
23103
+ function rankByOrder(members2) {
23104
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23105
+ }
23106
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23107
+ function severityRank(severity) {
23108
+ return lookupOwn(SEVERITY_RANK, severity);
23109
+ }
22970
23110
  function compareFindingGroupOrder(a, b) {
22971
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22972
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23111
+ const rankA = severityRank(a.severity) ?? -1;
23112
+ const rankB = severityRank(b.severity) ?? -1;
22973
23113
  const severityDiff = rankA - rankB;
22974
23114
  if (severityDiff !== 0) return severityDiff;
22975
23115
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
22976
23116
  if (recencyDiff !== 0) return recencyDiff;
22977
23117
  return a.id.localeCompare(b.id);
22978
23118
  }
22979
- function sortFindingGroups(groups) {
22980
- return [...groups].sort(compareFindingGroupOrder);
23119
+ function sortFindingTypes(types) {
23120
+ return [...types].sort(compareFindingGroupOrder);
22981
23121
  }
22982
- function computeFindingFacets(allGroups, opts) {
22983
- const forSeverity = applyFindingFilters(allGroups, {
23122
+ function computeFindingFacets(allTypes, opts) {
23123
+ const forSeverity = applyFindingFilters(allTypes, {
22984
23124
  providers: opts.providers,
22985
23125
  actions: opts.actions,
22986
23126
  statuses: opts.statuses,
@@ -22991,7 +23131,7 @@ function computeFindingFacets(allGroups, opts) {
22991
23131
  for (const g of forSeverity) {
22992
23132
  severityMap.set(g.severity, (severityMap.get(g.severity) ?? 0) + 1);
22993
23133
  }
22994
- const forProvider = applyFindingFilters(allGroups, {
23134
+ const forProvider = applyFindingFilters(allTypes, {
22995
23135
  actions: opts.actions,
22996
23136
  statuses: opts.statuses,
22997
23137
  q: opts.q,
@@ -23002,7 +23142,7 @@ function computeFindingFacets(allGroups, opts) {
23002
23142
  for (const g of forProvider) {
23003
23143
  for (const p of g.providers) providerMap.set(p, (providerMap.get(p) ?? 0) + 1);
23004
23144
  }
23005
- const forAction = applyFindingFilters(allGroups, {
23145
+ const forAction = applyFindingFilters(allTypes, {
23006
23146
  providers: opts.providers,
23007
23147
  statuses: opts.statuses,
23008
23148
  q: opts.q,
@@ -23011,9 +23151,9 @@ function computeFindingFacets(allGroups, opts) {
23011
23151
  });
23012
23152
  const actionMap = /* @__PURE__ */ new Map();
23013
23153
  for (const g of forAction) {
23014
- for (const a of groupActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23154
+ for (const a of typeActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23015
23155
  }
23016
- const forSubtype = applyFindingFilters(allGroups, {
23156
+ const forSubtype = applyFindingFilters(allTypes, {
23017
23157
  providers: opts.providers,
23018
23158
  actions: opts.actions,
23019
23159
  statuses: opts.statuses,
@@ -23022,7 +23162,7 @@ function computeFindingFacets(allGroups, opts) {
23022
23162
  });
23023
23163
  const subtypeMap = /* @__PURE__ */ new Map();
23024
23164
  for (const g of forSubtype) subtypeMap.set(g.subtype, (subtypeMap.get(g.subtype) ?? 0) + 1);
23025
- const forStatus = applyFindingFilters(allGroups, {
23165
+ const forStatus = applyFindingFilters(allTypes, {
23026
23166
  providers: opts.providers,
23027
23167
  actions: opts.actions,
23028
23168
  q: opts.q,
@@ -23044,6 +23184,20 @@ function computeFindingFacets(allGroups, opts) {
23044
23184
  }
23045
23185
 
23046
23186
  // ../../packages/schema/src/zod/findings-flat-build.ts
23187
+ function compareCodePoints(a, b) {
23188
+ const aIter = a[Symbol.iterator]();
23189
+ const bIter = b[Symbol.iterator]();
23190
+ for (; ; ) {
23191
+ const aNext = aIter.next();
23192
+ const bNext = bIter.next();
23193
+ if (aNext.done && bNext.done) return 0;
23194
+ if (aNext.done) return -1;
23195
+ if (bNext.done) return 1;
23196
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23197
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23198
+ if (aPoint !== bPoint) return aPoint - bPoint;
23199
+ }
23200
+ }
23047
23201
  function rowHaystack(row) {
23048
23202
  return [
23049
23203
  row.ruleId,
@@ -23068,12 +23222,24 @@ function matchesDimension(row, opts, dimension) {
23068
23222
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23069
23223
  case "statuses":
23070
23224
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23225
+ case "deliveries":
23226
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23071
23227
  case "tools":
23072
23228
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23229
+ // An EMPTY value is a real filter here, not an absent one. The location
23230
+ // list buckets a finding whose event recorded no repo — or no file — under
23231
+ // the empty string, and selecting that bucket has to narrow the panel to
23232
+ // exactly it. Only `undefined` means "no filter"; a caller that wants every
23233
+ // row omits the key, which every call site already does.
23234
+ //
23235
+ // Reading '' as unset is what this replaced, and it failed in the one place
23236
+ // it mattered: the no-repo/no-file bucket is often the largest in a real
23237
+ // store, and its panel dropped both predicates and returned the WHOLE scope
23238
+ // — a row reading 3 findings beside a panel listing every finding there is.
23073
23239
  case "repo":
23074
- return opts.repo === void 0 || opts.repo === "" || row.repo === opts.repo;
23240
+ return opts.repo === void 0 || row.repo === opts.repo;
23075
23241
  case "file":
23076
- return opts.file === void 0 || opts.file === "" || row.file === opts.file;
23242
+ return opts.file === void 0 || row.file === opts.file;
23077
23243
  case "q":
23078
23244
  return !opts.q || rowHaystack(row).includes(opts.q.toLowerCase());
23079
23245
  }
@@ -23084,6 +23250,7 @@ var DIMENSIONS = [
23084
23250
  "providers",
23085
23251
  "actions",
23086
23252
  "statuses",
23253
+ "deliveries",
23087
23254
  "tools",
23088
23255
  "repo",
23089
23256
  "file",
@@ -23097,10 +23264,19 @@ function matchesInstanceFilters(row, opts, except) {
23097
23264
  return true;
23098
23265
  }
23099
23266
  function toItems(counts) {
23100
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23267
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23268
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23269
+ // NFD spelling of the same text) as equal, so a count tie between
23270
+ // them would otherwise be ordered by whichever the Map iteration
23271
+ // produced. compareCodePoints breaks that tie deterministically, which
23272
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23273
+ // which it need not: foldFacetTuples runs this same sort over grouped
23274
+ // tuples, so both paths order facets identically by construction.
23275
+ compareCodePoints(a.value, b.value)
23276
+ );
23101
23277
  }
23102
- function bump(counts, value) {
23103
- counts.set(value, (counts.get(value) ?? 0) + 1);
23278
+ function bump(counts, value, by = 1) {
23279
+ counts.set(value, (counts.get(value) ?? 0) + by);
23104
23280
  }
23105
23281
  function createInstanceFacetAccumulator(opts) {
23106
23282
  const severity = /* @__PURE__ */ new Map();
@@ -23109,6 +23285,7 @@ function createInstanceFacetAccumulator(opts) {
23109
23285
  const action = /* @__PURE__ */ new Map();
23110
23286
  const status = /* @__PURE__ */ new Map();
23111
23287
  const tool = /* @__PURE__ */ new Map();
23288
+ const deployment = /* @__PURE__ */ new Map();
23112
23289
  return {
23113
23290
  add(row) {
23114
23291
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23123,6 +23300,9 @@ function createInstanceFacetAccumulator(opts) {
23123
23300
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23124
23301
  bump(tool, row.toolName);
23125
23302
  }
23303
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23304
+ bump(deployment, row.delivery.state);
23305
+ }
23126
23306
  },
23127
23307
  facets: () => ({
23128
23308
  severity: toItems(severity),
@@ -23130,7 +23310,8 @@ function createInstanceFacetAccumulator(opts) {
23130
23310
  provider: toItems(provider),
23131
23311
  action: toItems(action),
23132
23312
  status: toItems(status),
23133
- tool: toItems(tool)
23313
+ tool: toItems(tool),
23314
+ deployment: toItems(deployment)
23134
23315
  })
23135
23316
  };
23136
23317
  }
@@ -23144,6 +23325,7 @@ function toInstanceDetail(row) {
23144
23325
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23145
23326
  eventId: row.eventId,
23146
23327
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23328
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23147
23329
  ...row.user === void 0 ? {} : { user: row.user },
23148
23330
  action: toApiAction(row.actionTaken),
23149
23331
  detectedAt: row.occurredAt,
@@ -23158,12 +23340,6 @@ function toInstanceDetail(row) {
23158
23340
  policy: { id: `category:${category}`, name: category }
23159
23341
  };
23160
23342
  }
23161
- var SEVERITY_ORDER2 = {
23162
- critical: 0,
23163
- high: 1,
23164
- medium: 2,
23165
- low: 3
23166
- };
23167
23343
  function newLocationAccumulator() {
23168
23344
  return {
23169
23345
  instanceCount: 0,
@@ -23178,7 +23354,7 @@ function newLocationAccumulator() {
23178
23354
  }
23179
23355
  function addToLocation(acc, row) {
23180
23356
  acc.instanceCount += 1;
23181
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23357
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23182
23358
  if (rank < acc.maxSeverityRank) {
23183
23359
  acc.maxSeverityRank = rank;
23184
23360
  acc.maxSeverity = row.severity;
@@ -23187,6 +23363,23 @@ function addToLocation(acc, row) {
23187
23363
  acc.statuses.push(row.status);
23188
23364
  acc.ruleIds.add(row.ruleId);
23189
23365
  }
23366
+ function compareLocationOrder(a, b) {
23367
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23368
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23369
+ if (rankA !== rankB) return rankA - rankB;
23370
+ if (a.latestDetectedAt !== b.latestDetectedAt) {
23371
+ return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23372
+ }
23373
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23374
+ if (repoDiff !== 0) return repoDiff;
23375
+ return compareCodePoints(a.file, b.file);
23376
+ }
23377
+ function encodeLocationId(repo, file2) {
23378
+ return `${encodePart(repo)}/${encodePart(file2)}`;
23379
+ }
23380
+ function encodePart(value) {
23381
+ return encodeURIComponent(value.replace(/[\uD800-\uDFFF]/gu, "\uFFFD"));
23382
+ }
23190
23383
 
23191
23384
  // ../../packages/schema/src/zod/installed-pack.ts
23192
23385
  var InstalledPack = external_exports.object({
@@ -23254,6 +23447,11 @@ var Policy = external_exports.object({
23254
23447
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23255
23448
  provenance: PolicyProvenance.optional()
23256
23449
  }).meta({ id: "Policy" });
23450
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23451
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23452
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23453
+ id: "RedactFallback"
23454
+ });
23257
23455
  var PolicyBundle = external_exports.object({
23258
23456
  version: external_exports.string(),
23259
23457
  policies: external_exports.array(Policy),
@@ -23301,6 +23499,16 @@ var PolicyBundle = external_exports.object({
23301
23499
  // control plane), so no name resolution stands between the decision and the
23302
23500
  // comparison.
23303
23501
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23502
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23503
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23504
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23505
+ // a control plane can tighten a machine and never loosen one — the same
23506
+ // direction `mergeRaiseOnly` enforces for policies.
23507
+ //
23508
+ // Optional so an older backend, and an older on-disk cache, still parses;
23509
+ // absent leaves the device's own setting in force, which is the behaviour
23510
+ // that predates the field and the safe direction to default.
23511
+ redactFallback: RedactFallback.optional(),
23304
23512
  customKeywords: external_exports.array(external_exports.string()),
23305
23513
  fetchedAt: external_exports.iso.datetime()
23306
23514
  }).meta({ id: "PolicyBundle" });
@@ -23330,11 +23538,6 @@ function severityFloorPolicy(category) {
23330
23538
  const peak = CATEGORY_PEAK_SEVERITY[category];
23331
23539
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23332
23540
  }
23333
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23334
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23335
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23336
- id: "RedactFallback"
23337
- });
23338
23541
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23339
23542
  var BUILTIN_POLICY_SPECS = {
23340
23543
  monitor: {
@@ -23390,6 +23593,11 @@ function isActionAtLeast(action, floor) {
23390
23593
  function strongerAction(a, b) {
23391
23594
  return actionRank(a) >= actionRank(b) ? a : b;
23392
23595
  }
23596
+ function strongerRedactFallback(local, remote) {
23597
+ if (remote === void 0) return local;
23598
+ const localAction = builtinPolicyToAction(local);
23599
+ return localAction === strongerAction(localAction, builtinPolicyToAction(remote)) ? local : remote;
23600
+ }
23393
23601
  function weakestBuiltinAtLeast(floor) {
23394
23602
  return KNOWN_BUILTIN_IDS.find((id) => isActionAtLeast(builtinPolicyToAction(id), floor)) ?? "block";
23395
23603
  }
@@ -23630,7 +23838,7 @@ var VaultConsent = external_exports.object({
23630
23838
  });
23631
23839
 
23632
23840
  // ../../packages/schema/src/zod/local.ts
23633
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23841
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23634
23842
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23635
23843
  var RunMode = external_exports.enum(["standalone", "attached"]);
23636
23844
  var ControlPlaneConnection = external_exports.object({
@@ -23650,6 +23858,15 @@ var HistorySyncConsent = external_exports.object({
23650
23858
  payloadVersion: external_exports.number().int().positive(),
23651
23859
  endpoint: external_exports.string()
23652
23860
  });
23861
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23862
+ var BodyRetention = external_exports.object({
23863
+ enabled: external_exports.boolean().default(false),
23864
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23865
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23866
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23867
+ // candidate set that is already bounded by "delivered, or never owed".
23868
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23869
+ }).meta({ id: "BodyRetention" });
23653
23870
  var WorkspaceSettings = external_exports.object({
23654
23871
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23655
23872
  runMode: RunMode.default("standalone"),
@@ -23693,12 +23910,18 @@ var WorkspaceSettings = external_exports.object({
23693
23910
  // covers the current payload and must be re-granted.
23694
23911
  modelJudgeConsent: ModelJudgeConsent.optional(),
23695
23912
  // Records that the user consented to the DEFERRED send — the outbox — along
23696
- // with the payload shape and the endpoint they agreed to. Since payload v2
23697
- // that covers both the pre-attach backlog and undelivered captures (which
23698
- // carry prompt/reply text in `content`); the key name predates the widening.
23699
- // Absent until granted, and a grant for a different endpoint or an older
23700
- // payload no longer counts.
23701
- historySyncConsent: HistorySyncConsent.optional()
23913
+ // with the payload shape and the endpoint they agreed to. Since payload v3
23914
+ // that covers the pre-attach backlog AND undelivered captures alike, and both
23915
+ // carry prompt/reply/tool-output text in `content`; the key name predates
23916
+ // both widenings. Absent until granted, and a grant for a different endpoint
23917
+ // or an older payload no longer counts.
23918
+ historySyncConsent: HistorySyncConsent.optional(),
23919
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23920
+ // body never removes the row or its findings.
23921
+ bodyRetention: BodyRetention.default({
23922
+ enabled: false,
23923
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23924
+ })
23702
23925
  });
23703
23926
  function defaultWorkspaceSettings() {
23704
23927
  return WorkspaceSettings.parse({});
@@ -23793,12 +24016,15 @@ function toCaptureAttributes(event) {
23793
24016
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23794
24017
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23795
24018
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24019
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23796
24020
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23797
24021
  // has ever populated either), but every legacy metadata key still rides
23798
24022
  // the bag rather than being silently dropped — CaptureAttributes'
23799
24023
  // `.catchall(z.unknown())` carries the long tail.
23800
24024
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23801
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24025
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24026
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24027
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23802
24028
  };
23803
24029
  }
23804
24030
  function captureDefinitionVersion(finding) {
@@ -23826,10 +24052,22 @@ var ManagedSettingKey = external_exports.enum([
23826
24052
  "vaultInlineReveal",
23827
24053
  "modelJudgeConsent",
23828
24054
  "dataSharesInPlace",
23829
- "redactFallback"
24055
+ "redactFallback",
24056
+ // Pins the toggle and the day count together — see BodyRetention on why the
24057
+ // two are one unit. An administrator mandating a window wants the count
24058
+ // enforced with it, not one a user can widen while the toggle stays on.
24059
+ "bodyRetention"
23830
24060
  ]).meta({ id: "ManagedSettingKey" });
24061
+ function isManagedSettingKey(value) {
24062
+ return ManagedSettingKey.safeParse(value).success;
24063
+ }
23831
24064
  var ManagedSettingsValues = external_exports.object({
23832
24065
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24066
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24067
+ // plain, non-strict objects: a key under either that this build does not know
24068
+ // is stripped and nothing reports it. The unknown-value split in
24069
+ // ManagedSettings below classifies top-level names only, so it stops at
24070
+ // these boundaries.
23833
24071
  controlPlane: external_exports.object({
23834
24072
  endpoint: external_exports.string().min(1),
23835
24073
  label: external_exports.string().min(1).optional()
@@ -23840,7 +24078,8 @@ var ManagedSettingsValues = external_exports.object({
23840
24078
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23841
24079
  modelJudgeConsent: external_exports.boolean().optional(),
23842
24080
  dataSharesInPlace: external_exports.boolean().optional(),
23843
- redactFallback: RedactFallback.optional()
24081
+ redactFallback: RedactFallback.optional(),
24082
+ bodyRetention: BodyRetention.optional()
23844
24083
  }).meta({ id: "ManagedSettingsValues" });
23845
24084
  var ManagedSettings = external_exports.object({
23846
24085
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23848,11 +24087,59 @@ var ManagedSettings = external_exports.object({
23848
24087
  // decision from a bug. Absent renders as a generic "your organization".
23849
24088
  organization: external_exports.string().min(1).optional(),
23850
24089
  // What the administrator pinned.
23851
- values: ManagedSettingsValues.default({}),
24090
+ //
24091
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24092
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24093
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24094
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24095
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24096
+ // exactly the file an administrator is most likely to write while a fleet
24097
+ // is mid-upgrade.
24098
+ //
24099
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24100
+ // file, which is the outcome the lock half already rejected — an older
24101
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24102
+ // value still fails, because the nested schema is re-run over the known
24103
+ // subset and its issues are re-raised on this parse.
24104
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23852
24105
  // Which of those the user may not change. A key here with no matching value
23853
24106
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23854
24107
  // the user may still override. The two are separable on purpose.
23855
- lockedFields: external_exports.array(ManagedSettingKey).default([])
24108
+ //
24109
+ // Parsed as NAMES rather than as the enum, and split below: a name this
24110
+ // build does not know is dropped from the locked set and reported, never a
24111
+ // reason to refuse the file. The same shape reaches an older build whenever
24112
+ // an administrator locks a key a newer build added, and refusing it there
24113
+ // ran that build entirely unmanaged — every pin and lock gone — on exactly
24114
+ // the fleets most likely to carry a version skew. A name outside the enum
24115
+ // is still never HONOURED: the lockable set stays explicit above.
24116
+ lockedFields: external_exports.array(external_exports.string()).default([])
24117
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
24118
+ const known = [];
24119
+ const unknown2 = [];
24120
+ for (const name of lockedFields) {
24121
+ if (isManagedSettingKey(name)) known.push(name);
24122
+ else unknown2.push(name);
24123
+ }
24124
+ const knownValues = /* @__PURE__ */ Object.create(null);
24125
+ const unknownValues = [];
24126
+ for (const [name, value] of Object.entries(values)) {
24127
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24128
+ else unknownValues.push(name);
24129
+ }
24130
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24131
+ if (!pinned.success) {
24132
+ for (const issue2 of pinned.error.issues)
24133
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24134
+ return external_exports.NEVER;
24135
+ }
24136
+ return {
24137
+ ...rest,
24138
+ values: pinned.data,
24139
+ lockedFields: known,
24140
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24141
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
24142
+ };
23856
24143
  }).meta({ id: "ManagedSettings" });
23857
24144
 
23858
24145
  // ../../packages/schema/src/zod/project-files.ts
@@ -23976,7 +24263,11 @@ var FindingsTimeseriesPoint = external_exports.object({
23976
24263
  timestamp: external_exports.iso.date(),
23977
24264
  critical: external_exports.number().int().nonnegative(),
23978
24265
  high: external_exports.number().int().nonnegative(),
23979
- medium: external_exports.number().int().nonnegative()
24266
+ medium: external_exports.number().int().nonnegative(),
24267
+ // Optional and additive, so a producer written against the earlier
24268
+ // three-series contract keeps validating. A consumer plotting it resolves the
24269
+ // absent case itself — the chart point requires a number.
24270
+ low: external_exports.number().int().nonnegative().optional()
23980
24271
  }).meta({ id: "FindingsTimeseriesPoint" });
23981
24272
  var FindingsTimeseriesResponse = external_exports.object({
23982
24273
  range: TimeRange,
@@ -24002,6 +24293,10 @@ var ResolvedFeedItem = external_exports.object({
24002
24293
  findingKey: external_exports.string(),
24003
24294
  ruleId: external_exports.string(),
24004
24295
  severity: Severity,
24296
+ // Repository slug, and the file path RELATIVE to it. The pair is what
24297
+ // identifies the file: a bare path matches the same name in every repo.
24298
+ // Optional and additive; empty when the event carried no repo.
24299
+ repo: external_exports.string().optional(),
24005
24300
  path: external_exports.string(),
24006
24301
  // ISO-8601 datetime (matches FindingInstance.detectedAt / the rest of the
24007
24302
  // findings domain). The reader `.toISOString()`s the DB epoch-ms values.
@@ -24107,7 +24402,23 @@ var SaveSettingsInput = external_exports.object({
24107
24402
  modelJudgeConsent: ModelJudgeConsentChoice,
24108
24403
  historySyncConsent: HistorySyncConsentChoice,
24109
24404
  vaultConsent: external_exports.string(),
24110
- vaultInlineReveal: external_exports.string()
24405
+ vaultInlineReveal: external_exports.string(),
24406
+ // Widened to `string` like its neighbours rather than typed as
24407
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24408
+ // the call site, so the domain check receives the type it was written for.
24409
+ //
24410
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24411
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24412
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24413
+ // trade against. The real cost runs the other way and is the part worth
24414
+ // knowing: a value this schema admits and the domain enum then rejects lands
24415
+ // on the action's shared refusal, which names NO field, where a shape
24416
+ // rejection reaches `malformedInput` and names the schema key.
24417
+ redactFallback: external_exports.string(),
24418
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24419
+ // `BodyRetention`'s and the action checks it there, so there is one place
24420
+ // that decides what a legal horizon is rather than two that can drift.
24421
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24111
24422
  });
24112
24423
  var AttachInput = external_exports.object({
24113
24424
  endpoint: external_exports.string(),
@@ -24279,6 +24590,52 @@ function reviewSeverityRank(reasons) {
24279
24590
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24280
24591
  }
24281
24592
 
24593
+ // ../../packages/schema/src/zod/web-capture.ts
24594
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24595
+ var WebUsage = external_exports.object({
24596
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24597
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24598
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24599
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24600
+ });
24601
+ var WebToolCall = external_exports.object({
24602
+ toolUseId: external_exports.string().min(1),
24603
+ toolName: external_exports.string().min(1),
24604
+ target: external_exports.string().optional(),
24605
+ isError: external_exports.boolean().optional(),
24606
+ inputSize: external_exports.number().int().nonnegative().optional(),
24607
+ outputSize: external_exports.number().int().nonnegative().optional()
24608
+ });
24609
+ var WebExchange = external_exports.object({
24610
+ messageId: external_exports.string().min(1),
24611
+ startedAt: external_exports.iso.datetime(),
24612
+ model: external_exports.string().optional(),
24613
+ usage: WebUsage.optional(),
24614
+ usageSource: WebUsageSource,
24615
+ stopReason: external_exports.string().optional(),
24616
+ conversationId: external_exports.string().optional(),
24617
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24618
+ toolCalls: external_exports.array(WebToolCall).default([]),
24619
+ // Absent when the adapter recovered no text. Capped by the caller at
24620
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24621
+ // short capture is never mistaken for a short reply.
24622
+ responseText: external_exports.string().optional(),
24623
+ truncated: external_exports.boolean().default(false)
24624
+ });
24625
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24626
+ var WebCaptureStatus = external_exports.object({
24627
+ patched: external_exports.boolean(),
24628
+ live: external_exports.boolean(),
24629
+ blind: external_exports.boolean(),
24630
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24631
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24632
+ parseFailures: external_exports.number().int().nonnegative(),
24633
+ unparsedBodies: external_exports.number().int().nonnegative(),
24634
+ // The adapter-declared JSON key paths that were absent from a real payload —
24635
+ // the earliest signal that a site's contract moved.
24636
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24637
+ });
24638
+
24282
24639
  // ../../packages/persistence/src/paths.ts
24283
24640
  import {
24284
24641
  chmodSync,
@@ -24639,6 +24996,22 @@ function discardStore(file2, backup) {
24639
24996
  }
24640
24997
  }
24641
24998
 
24999
+ // ../../packages/persistence/src/internal/sql-functions.ts
25000
+ var utf8 = new TextDecoder();
25001
+ function akaLower(value) {
25002
+ if (value === null) return null;
25003
+ if (typeof value === "string") return value.toLowerCase();
25004
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
25005
+ return utf8.decode(value).toLowerCase();
25006
+ }
25007
+ function registerSqlFunctions(db) {
25008
+ db.function(
25009
+ "aka_lower",
25010
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
25011
+ akaLower
25012
+ );
25013
+ }
25014
+
24642
25015
  // ../../packages/persistence/src/internal/sql-text.ts
24643
25016
  function escapeLikePattern(s) {
24644
25017
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24723,6 +25096,11 @@ function schemaObjectExists(db, kind, name) {
24723
25096
  function indexExists(db, name) {
24724
25097
  return schemaObjectExists(db, "index", name);
24725
25098
  }
25099
+ function indexColumns(db, name) {
25100
+ if (!indexExists(db, name)) return [];
25101
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25102
+ return columns.map((c) => c.name).filter((c) => c !== null);
25103
+ }
24726
25104
  function columnNames(db, table2, opts) {
24727
25105
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24728
25106
  const columns = db.prepare(`PRAGMA ${pragma}(${table2})`).all();
@@ -24784,141 +25162,783 @@ function mapRowsTolerant(rows, map2) {
24784
25162
  return out;
24785
25163
  }
24786
25164
 
24787
- // ../../packages/persistence/src/migrations.ts
24788
- function describeObject(object2) {
24789
- return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
24790
- }
24791
- function splitStatements(sql) {
24792
- return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
24793
- }
24794
- function createdIndexName(statement) {
24795
- const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
24796
- return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25165
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25166
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25167
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25168
+
25169
+ // ../../packages/persistence/src/sync-failure.ts
25170
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25171
+ function syncFailureRejectCondition(column = "sync_failure") {
25172
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25173
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
24797
25174
  }
24798
- var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24799
- function applyMigrations(db, file2) {
24800
- const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24801
- db.exec(
24802
- "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
24803
- );
24804
- const applied = new Set(
24805
- db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
24806
- );
24807
- const preLedgerStore = applied.size === 0 && legacyCount > 0;
24808
- const record2 = db.prepare(
24809
- "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
24810
- );
24811
- for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24812
- if (applied.has(migration.tag)) continue;
24813
- if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24814
- const evidence = evidenceObjects(migration.sql);
24815
- const present2 = evidence.filter((o) => evidenceExists(db, o));
24816
- if (present2.length > 0 && present2.length < evidence.length) {
24817
- const missing = evidence.filter((o) => !present2.includes(o));
24818
- const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present2.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
24819
- akaWarn(message);
24820
- throw new Error(`[aka] ${message}`);
24821
- }
24822
- const alreadyApplied = evidence.length > 0 ? present2.length === evidence.length : preLedgerStore && index < legacyCount;
24823
- const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
24824
- const statements = splitStatements(migration.sql);
24825
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
24826
- try {
24827
- withTransaction(
24828
- db,
24829
- () => {
24830
- for (const statement of statements) {
24831
- const indexName = createdIndexName(statement);
24832
- if (indexName === void 0) {
24833
- if (alreadyApplied) continue;
24834
- } else if (indexExists(db, indexName)) {
24835
- continue;
24836
- }
24837
- db.exec(statement);
24838
- }
24839
- if (wantsFkOff && !alreadyApplied) {
24840
- const violations = db.prepare("PRAGMA foreign_key_check").all();
24841
- if (violations.length > 0) {
24842
- throw new Error(
24843
- `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
24844
- );
24845
- }
24846
- }
24847
- record2.run(migration.tag, Date.now());
24848
- },
24849
- "IMMEDIATE"
24850
- );
24851
- } finally {
24852
- if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
24853
- }
25175
+
25176
+ // ../../packages/persistence/src/repositories/history-sync.ts
25177
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25178
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25179
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25180
+ var COUNTED_EVENT_TYPES = [
25181
+ ...STRUCTURAL_EVENT_TYPES,
25182
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25183
+ ];
25184
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25185
+ var PARTITION_BUCKETS = `
25186
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25187
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25188
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25189
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25190
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25191
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25192
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25193
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25194
+ -- added later lands in no bucket and fails the sum assertion, instead
25195
+ -- of silently joining this one.
25196
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25197
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25198
+ THEN 1 ELSE 0 END) AS failed,
25199
+ COUNT(*) AS total`;
25200
+ var COUNTED_SCOPE = `
25201
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25202
+ AND (
25203
+ event_type IN (${TYPE_LIST})
25204
+ OR synced_at IS NOT NULL
25205
+ OR outbox_owed = 1
25206
+ )`;
25207
+ var SKIPPED = -1;
25208
+ var ROW_COLUMNS = `id,
25209
+ parent_id AS parentId,
25210
+ root_session_id AS rootSessionId,
25211
+ event_type AS eventType,
25212
+ host_id AS hostId,
25213
+ harness_id AS harnessId,
25214
+ source_project_id AS sourceProjectId,
25215
+ started_at AS startedAt,
25216
+ ended_at AS endedAt,
25217
+ severity,
25218
+ priority,
25219
+ content,
25220
+ content_hash AS contentHash,
25221
+ attributes`;
25222
+ var SqliteHistorySyncRepository = class {
25223
+ constructor(db) {
25224
+ this.db = db;
25225
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25226
+ this.sessionsStmt = db.prepare(
25227
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25228
+ FROM audit_events
25229
+ WHERE synced_at IS NULL
25230
+ AND event_type IN (${TYPE_LIST})
25231
+ AND started_at < :before
25232
+ GROUP BY sessionId
25233
+ ORDER BY earliest
25234
+ LIMIT :limit`
25235
+ );
25236
+ this.rowsStmt = db.prepare(
25237
+ `SELECT ${ROW_COLUMNS}
25238
+ FROM audit_events
25239
+ WHERE synced_at IS NULL
25240
+ AND event_type IN (${TYPE_LIST})
25241
+ AND started_at < :before
25242
+ AND COALESCE(root_session_id, id) = :sessionId
25243
+ ORDER BY (event_type = 'session') DESC, started_at
25244
+ LIMIT :limit`
25245
+ );
25246
+ this.captureRowsStmt = db.prepare(
25247
+ `SELECT ${ROW_COLUMNS}
25248
+ FROM audit_events
25249
+ WHERE synced_at IS NULL
25250
+ AND sync_claimed_at IS NULL
25251
+ AND outbox_owed = 1
25252
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25253
+ AND started_at < :before
25254
+ ORDER BY started_at
25255
+ LIMIT :limit`
25256
+ );
25257
+ this.markOwedStmt = db.prepare(
25258
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25259
+ );
25260
+ this.markCaptureBacklogOwedStmt = db.prepare(
25261
+ `UPDATE audit_events SET outbox_owed = 1
25262
+ WHERE synced_at IS NULL
25263
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25264
+ AND started_at < :before`
25265
+ );
25266
+ this.stampStmt = db.prepare(
25267
+ `UPDATE audit_events
25268
+ SET synced_at = :at,
25269
+ sync_claimed_at = NULL,
25270
+ sync_failed_at = :failedAt,
25271
+ sync_failure = :failure
25272
+ WHERE id = :id`
25273
+ );
25274
+ this.claimRowStmt = db.prepare(
25275
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25276
+ );
25277
+ this.releaseRowStmt = db.prepare(
25278
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25279
+ );
25280
+ this.releaseStaleClaimsStmt = db.prepare(
25281
+ `UPDATE audit_events SET sync_claimed_at = NULL
25282
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25283
+ );
25284
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25285
+ FROM audit_events${COUNTED_SCOPE}`);
25286
+ this.partitionByKindStmt = db.prepare(
25287
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25288
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25289
+ GROUP BY event_type`
25290
+ );
25291
+ this.countsStmt = db.prepare(
25292
+ `SELECT
25293
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25294
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25295
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25296
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25297
+ THEN 1 ELSE 0 END) AS skipped,
25298
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25299
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25300
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25301
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25302
+ FROM audit_events
25303
+ WHERE event_type IN (${TYPE_LIST})`
25304
+ );
25305
+ this.captureSkipCountStmt = db.prepare(
25306
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25307
+ // way the structural totals are. The split exists because a refusal is
25308
+ // terminal only against the deployment that gave it, and the structural
25309
+ // re-arm frees it on a change of deployment. The capture lane has no such
25310
+ // escape: re-arming a capture would offer one deployment's undelivered
25311
+ // prompts, with their text, to a deployment that never saw them, which is
25312
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25313
+ // reasons mean the same thing — this row will not be sent — and splitting
25314
+ // them would put refused captures in a bucket nothing reads and nothing
25315
+ // frees.
25316
+ `SELECT COUNT(*) AS skipped
25317
+ FROM audit_events
25318
+ WHERE synced_at = ${String(SKIPPED)}
25319
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25320
+ );
25321
+ this.fingerprintStmt = db.prepare(
25322
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25323
+ FROM history_sync WHERE id = 1`
25324
+ );
25325
+ this.setFingerprintStmt = db.prepare(
25326
+ `UPDATE history_sync
25327
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25328
+ WHERE id = 1`
25329
+ );
25330
+ this.disownCapturesStmt = db.prepare(
25331
+ `UPDATE audit_events SET outbox_owed = NULL
25332
+ WHERE outbox_owed IS NOT NULL
25333
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25334
+ AND started_at < :attachedAt`
25335
+ );
25336
+ this.rearmStmt = db.prepare(
25337
+ `UPDATE audit_events
25338
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25339
+ WHERE (synced_at > 0
25340
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25341
+ AND event_type IN (${TYPE_LIST})`
25342
+ );
25343
+ this.claimStmt = db.prepare(
25344
+ `UPDATE history_sync
25345
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25346
+ WHERE id = 1
25347
+ AND (owner_pid IS NULL
25348
+ OR heartbeat_at IS NULL
25349
+ OR heartbeat_at < :staleBefore
25350
+ OR heartbeat_at > :now)`
25351
+ );
25352
+ this.heartbeatStmt = db.prepare(
25353
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25354
+ );
25355
+ this.releaseStmt = db.prepare(
25356
+ `UPDATE history_sync
25357
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25358
+ WHERE id = 1 AND owner_pid = :pid`
25359
+ );
25360
+ this.closeWindowStmt = db.prepare(
25361
+ `UPDATE audit_events
25362
+ SET synced_at = ${String(SKIPPED)},
25363
+ sync_failed_at = :at,
25364
+ sync_failure = 'detached_undelivered'
25365
+ WHERE synced_at IS NULL
25366
+ AND event_type IN (${TYPE_LIST})
25367
+ AND started_at >= :attachedAt`
25368
+ );
25369
+ this.releaseBoundaryStmt = db.prepare(
25370
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25371
+ );
25372
+ this.freezeBoundaryStmt = db.prepare(
25373
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25374
+ );
25375
+ this.leaseStmt = db.prepare(
25376
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25377
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25378
+ FROM history_sync WHERE id = 1`
25379
+ );
25380
+ this.inspectionsStmt = db.prepare(
25381
+ `SELECT d.rule_id AS ruleId,
25382
+ d.name AS ruleName,
25383
+ d.version AS ruleVersion,
25384
+ d.category AS category,
25385
+ d.severity AS severity,
25386
+ f.span_start AS spanStart,
25387
+ f.span_end AS spanEnd,
25388
+ f.masked_match AS maskedMatch,
25389
+ f.action_taken AS actionTaken,
25390
+ f.confidence AS confidence
25391
+ FROM inspection_findings f
25392
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25393
+ WHERE f.audit_event_id = :auditEventId
25394
+ ORDER BY f.span_start, f.id`
25395
+ );
24854
25396
  }
24855
- if (legacyCount < SQLITE_MIGRATIONS.length) {
24856
- db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25397
+ db;
25398
+ ensureRowStmt;
25399
+ sessionsStmt;
25400
+ rowsStmt;
25401
+ stampStmt;
25402
+ countsStmt;
25403
+ fingerprintStmt;
25404
+ setFingerprintStmt;
25405
+ rearmStmt;
25406
+ claimStmt;
25407
+ heartbeatStmt;
25408
+ releaseStmt;
25409
+ leaseStmt;
25410
+ inspectionsStmt;
25411
+ closeWindowStmt;
25412
+ releaseBoundaryStmt;
25413
+ freezeBoundaryStmt;
25414
+ captureRowsStmt;
25415
+ markOwedStmt;
25416
+ markCaptureBacklogOwedStmt;
25417
+ captureSkipCountStmt;
25418
+ disownCapturesStmt;
25419
+ partitionStmt;
25420
+ partitionByKindStmt;
25421
+ claimRowStmt;
25422
+ releaseRowStmt;
25423
+ releaseStaleClaimsStmt;
25424
+ /**
25425
+ * The masked detections recorded against one tool call.
25426
+ *
25427
+ * These travel with the event because a tool call's target is not
25428
+ * re-inspectable from the event alone — unlike a capture, where the text
25429
+ * itself is re-scannable. What crosses is the masked match and the rule that
25430
+ * produced it, never the value.
25431
+ */
25432
+ inspectionsFor(auditEventId) {
25433
+ return allRows(this.inspectionsStmt, { auditEventId });
24857
25434
  }
24858
- ensureSyncedAtColumn(db, "audit_events");
24859
- ensureScanLedgerTable(db);
24860
- ensureHistorySyncTable(db);
24861
- ensureBlockedDetectionsTable(db);
24862
- ensureRuleProbeCacheTable(db);
24863
- ensureWriteGateTrigger(db);
24864
- ensureTokenUsageColumns(db);
24865
- reconcileSourceProjectIds(db);
24866
- if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
24867
- const drained = runLegacyHistoryBackfill(db);
24868
- if (drained) applyLegacyDropMigration(db, file2);
25435
+ /**
25436
+ * Sessions with structural rows still to send, oldest first.
25437
+ *
25438
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25439
+ * read. Anything recorded after the machine attached is the live forward
25440
+ * path's to deliver; this drain exists for what was recorded before it, and a
25441
+ * row both paths send is at best a duplicate request and at worst — for a
25442
+ * session root — an overwrite of the inventory ids the live path resolved.
25443
+ */
25444
+ pendingSessions(limit, before) {
25445
+ return allRows(this.sessionsStmt, { limit, before }).map(
25446
+ (r) => r.sessionId
25447
+ );
24869
25448
  }
24870
- }
24871
- function readLegacyTables(db) {
24872
- let holdsRows = false;
24873
- const marks = [];
24874
- for (const table2 of ["events", "findings"]) {
24875
- try {
24876
- const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
24877
- if (row === void 0) {
24878
- holdsRows = true;
24879
- marks.push(`${table2}:unreadable`);
24880
- continue;
24881
- }
24882
- if (row.n > 0) holdsRows = true;
24883
- marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
24884
- } catch {
24885
- holdsRows = true;
24886
- marks.push(`${table2}:unreadable`);
24887
- }
25449
+ /** One session's undelivered structural rows within the backlog, root first. */
25450
+ pendingRows(sessionId, limit, before) {
25451
+ return allRows(this.rowsStmt, { sessionId, limit, before });
24888
25452
  }
24889
- return { holdsRows, mark: marks.join("|") };
24890
- }
24891
- function applyLegacyDropMigration(db, file2) {
24892
- const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
24893
- if (!migration) return;
24894
- const before = file2 === void 0 ? void 0 : readLegacyTables(db);
24895
- if (file2 !== void 0 && before?.holdsRows === true) {
24896
- try {
24897
- backupBeforeLegacyDrop(db, file2);
24898
- } catch (error61) {
24899
- akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
24900
- return;
24901
- }
25453
+ /**
25454
+ * Captures this machine still owes the deployment, oldest first.
25455
+ *
25456
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25457
+ * by a time window — see captureRowsStmt for why a window could not express
25458
+ * this. `before` is the grace window that leaves a just-recorded capture to
25459
+ * the live path.
25460
+ */
25461
+ pendingCaptureRows(limit, before) {
25462
+ return allRows(this.captureRowsStmt, { limit, before });
24902
25463
  }
24903
- try {
25464
+ /**
25465
+ * Record that a capture is OWED to the deployment.
25466
+ *
25467
+ * Written by the attached forward path when a live send did not confirm
25468
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25469
+ * a fact rather than an inference: the machine was attached, the send did not
25470
+ * land, so the row is owed — which no time window can state, because the same
25471
+ * window that holds the rows a past attachment left owed also holds every
25472
+ * capture recorded while the machine was DETACHED, and those were never
25473
+ * offered to anyone.
25474
+ *
25475
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25476
+ * out of the drain's read.
25477
+ */
25478
+ markCaptureOwed(id) {
25479
+ this.markOwedStmt.run({ id });
25480
+ }
25481
+ /**
25482
+ * Mark every capture already on disk as owed, as of `before`.
25483
+ *
25484
+ * The consent-time backfill, called once from `aka attach` when a human
25485
+ * grants existing-history consent — never from an ongoing drain pass, and
25486
+ * never inferred from a boundary that could later move. `before` is the
25487
+ * caller's own "now" at the moment consent was granted, so what this marks
25488
+ * is exactly the backlog the consent prompt already counted, not whatever a
25489
+ * later re-attach or key rotation might widen it to.
25490
+ *
25491
+ * Returns how many rows matched, for the caller to log or test against. Not a
25492
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25493
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25494
+ */
25495
+ markCaptureBacklogOwed(before) {
25496
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25497
+ }
25498
+ /**
25499
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25500
+ *
25501
+ * CLEARS any failure reason in the same statement. A row that failed against
25502
+ * one deployment and then landed is delivered, and leaving the reason behind
25503
+ * would leave the store holding two contradictory answers about one row —
25504
+ * with the surface free to render either.
25505
+ */
25506
+ markSynced(ids, atMs) {
25507
+ this.stampAll(ids, atMs, null);
25508
+ }
25509
+ /**
25510
+ * Record that THIS MACHINE cannot express the row on the wire.
25511
+ *
25512
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25513
+ * payload, or a body the client itself refused to send. It fails identically
25514
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25515
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25516
+ * is retried; marking those would turn one outage into permanent data loss.
25517
+ */
25518
+ markSkipped(ids, atMs) {
25519
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25520
+ }
25521
+ /**
25522
+ * Record that THIS DEPLOYMENT refused the row.
25523
+ *
25524
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25525
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25526
+ * row is outstanding rather than why. What separates them is the reason, and
25527
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25528
+ * on one body, so it is terminal only for as long as this machine points at
25529
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25530
+ *
25531
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25532
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25533
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25534
+ */
25535
+ markRefused(ids, atMs) {
25536
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25537
+ }
25538
+ eachInTransaction(ids, run) {
25539
+ if (ids.length === 0) return;
24904
25540
  withTransaction(
24905
- db,
25541
+ this.db,
24906
25542
  () => {
24907
- const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
24908
- if (alreadyDropped) return;
24909
- if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
24910
- akaWarn(
24911
- "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
24912
- );
24913
- return;
24914
- }
24915
- for (const statement of splitStatements(migration.sql)) {
24916
- db.exec(statement);
24917
- }
24918
- db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
24919
- migration.tag,
24920
- Date.now()
24921
- );
25543
+ for (const id of ids) run(id);
25544
+ },
25545
+ "IMMEDIATE"
25546
+ );
25547
+ }
25548
+ stampAll(ids, value, failure, failedAtMs) {
25549
+ if (ids.length === 0) return;
25550
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25551
+ withTransaction(
25552
+ this.db,
25553
+ () => {
25554
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25555
+ },
25556
+ "IMMEDIATE"
25557
+ );
25558
+ }
25559
+ /**
25560
+ * Claim rows as in-flight.
25561
+ *
25562
+ * Advisory in exactly the sense the lease is: it records that a send is in
25563
+ * progress so a surface can say so, and a lost claim costs a row showing as
25564
+ * queued while it is actually being sent. It is not exclusion — the far side
25565
+ * settles a duplicate on the row id.
25566
+ */
25567
+ claimRows(ids, atMs) {
25568
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25569
+ }
25570
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25571
+ releaseRows(ids) {
25572
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25573
+ }
25574
+ /**
25575
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25576
+ *
25577
+ * A process killed between claiming and settling leaves rows claimed with
25578
+ * nothing left to settle them. Without this they read as "sending" for ever.
25579
+ */
25580
+ releaseStaleClaims(staleBefore) {
25581
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25582
+ }
25583
+ /**
25584
+ * Every tracked row in exactly one delivery state.
25585
+ *
25586
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25587
+ * pick up now", which is a different question from "what state is this row
25588
+ * in" — and a machine that has never attached has no boundary to pass, so
25589
+ * requiring one would force a caller to invent one and report the whole store
25590
+ * as queued.
25591
+ */
25592
+ /**
25593
+ * The same partition, one row per kind that a lane carries.
25594
+ *
25595
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25596
+ * scope decides which rows exist at all, so a kind that has never been
25597
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25598
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25599
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25600
+ * different things.
25601
+ */
25602
+ partitionByKind() {
25603
+ return allRows(
25604
+ this.partitionByKindStmt,
25605
+ {}
25606
+ ).map((row) => ({
25607
+ kind: row.kind,
25608
+ queued: row.queued ?? 0,
25609
+ inProgress: row.inProgress ?? 0,
25610
+ synced: row.synced ?? 0,
25611
+ failed: row.failed ?? 0,
25612
+ refused: row.refused ?? 0,
25613
+ detached: row.detached ?? 0,
25614
+ total: row.total ?? 0
25615
+ }));
25616
+ }
25617
+ partition() {
25618
+ const row = getRow(this.partitionStmt, {});
25619
+ return {
25620
+ queued: row?.queued ?? 0,
25621
+ inProgress: row?.inProgress ?? 0,
25622
+ synced: row?.synced ?? 0,
25623
+ failed: row?.failed ?? 0,
25624
+ refused: row?.refused ?? 0,
25625
+ detached: row?.detached ?? 0,
25626
+ total: row?.total ?? 0
25627
+ };
25628
+ }
25629
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25630
+ counts(before) {
25631
+ const row = getRow(this.countsStmt, { before });
25632
+ const captures = getRow(this.captureSkipCountStmt);
25633
+ return {
25634
+ pending: row?.pending ?? 0,
25635
+ sent: row?.sent ?? 0,
25636
+ skipped: row?.skipped ?? 0,
25637
+ refused: row?.refused ?? 0,
25638
+ detached: row?.detached ?? 0,
25639
+ capturesSkipped: captures?.skipped ?? 0
25640
+ };
25641
+ }
25642
+ /**
25643
+ * The deployment the current stamps were made against, and where its backlog
25644
+ * ends.
25645
+ *
25646
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25647
+ * machine that has never drained is — and every writer below seeds the row
25648
+ * before it needs one, so nothing depends on this creating it. Keeping the
25649
+ * write off the gate path matters because the gate runs on every pass while a
25650
+ * write has to take the database's write lock.
25651
+ */
25652
+ deployment() {
25653
+ const row = getRow(
25654
+ this.fingerprintStmt
25655
+ );
25656
+ return {
25657
+ fingerprint: row?.fingerprint ?? void 0,
25658
+ backlogBefore: row?.backlogBefore ?? void 0
25659
+ };
25660
+ }
25661
+ /**
25662
+ * Point the ledger at a different deployment, discarding what it recorded
25663
+ * about the previous one.
25664
+ *
25665
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25666
+ * machine has just left are undelivered as far as the new one is concerned.
25667
+ * All four in one transaction, so a crash between them cannot leave stamps
25668
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25669
+ * a disown with no re-mark to follow it.
25670
+ *
25671
+ * The boundary is written HERE and only here, which is what freezes it: a
25672
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25673
+ * unchanged, so this never runs and the backlog does not widen back over rows
25674
+ * the live path has since delivered.
25675
+ *
25676
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25677
+ * granted existing-history consent for the deployment this call is arming —
25678
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25679
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25680
+ * apart. Passed only when that grant is valid, since this method has no way
25681
+ * to check consent itself and must not mark a row owed for a machine that
25682
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25683
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25684
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25685
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25686
+ * on the cleared side of that bound — and the re-mark in the same
25687
+ * transaction is what puts those rows back. A crash between the two cannot
25688
+ * strand the ledger disowned with nothing re-marked — the transaction either
25689
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25690
+ * committed re-enters this method on the very next pass. Omit it (the
25691
+ * structural-only tests do) to exercise the disown in isolation.
25692
+ *
25693
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25694
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25695
+ * live path can mark a capture owed from the moment `aka attach` writes the
25696
+ * descriptor, before the drain's first pass ever reaches this method, and
25697
+ * such a row sits at or after the bound rather than below it. What keeps the
25698
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25699
+ * bound — disown runs first, re-mark second, both inside the one
25700
+ * transaction above.
25701
+ */
25702
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25703
+ this.ensureRowStmt.run();
25704
+ withTransaction(
25705
+ this.db,
25706
+ () => {
25707
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25708
+ this.rearmStmt.run();
25709
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25710
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25711
+ }
25712
+ if (backfillCapturesBefore !== void 0) {
25713
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25714
+ }
25715
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25716
+ },
25717
+ "IMMEDIATE"
25718
+ );
25719
+ }
25720
+ /**
25721
+ * End the attached period: hand its rows to the live path, and release the
25722
+ * boundary so the next attachment can freeze a new one.
25723
+ *
25724
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25725
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25726
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25727
+ * during the detached period, because the machine is not attached. Rows
25728
+ * recorded in that window sit after the boundary and before the re-attach, so
25729
+ * neither path takes them, and the pending count reports none outstanding.
25730
+ *
25731
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25732
+ * closing attachment's to deliver and are no longer outstanding — that is what
25733
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25734
+ * distinction is not academic: this used to write a delivery TIME, which every
25735
+ * read treats as delivery, so one detach turned a window of undelivered rows
25736
+ * into a window of delivered ones and no surface could tell. It writes the
25737
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25738
+ * "received" stop being the same fact.
25739
+ *
25740
+ * A change of deployment still frees them (see the re-arm), because the next
25741
+ * deployment has seen none of this machine's history — so the rows reach it
25742
+ * exactly as they did when this wrote a delivery time.
25743
+ *
25744
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25745
+ * window unstamped — that half-state would re-send the whole attached period
25746
+ * on the next attach, which is the failure the boundary exists to prevent.
25747
+ */
25748
+ closeAttachedWindow(attachedAtMs, atMs) {
25749
+ this.ensureRowStmt.run();
25750
+ withTransaction(
25751
+ this.db,
25752
+ () => {
25753
+ const row = getRow(this.fingerprintStmt);
25754
+ const from = row?.backlogBefore ?? attachedAtMs;
25755
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25756
+ this.releaseBoundaryStmt.run();
25757
+ },
25758
+ "IMMEDIATE"
25759
+ );
25760
+ }
25761
+ /**
25762
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25763
+ *
25764
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25765
+ * different deployment and therefore discards what was delivered to the old
25766
+ * one: here the recipient is the same, so everything already sent to it stays
25767
+ * sent.
25768
+ */
25769
+ freezeBoundary(backlogBefore) {
25770
+ this.ensureRowStmt.run();
25771
+ this.freezeBoundaryStmt.run({ backlogBefore });
25772
+ }
25773
+ /** Take the claim, or report that someone live already holds it. */
25774
+ claim(pid, host, nowMs, staleAfterMs) {
25775
+ this.ensureRowStmt.run();
25776
+ let taken = false;
25777
+ withTransaction(
25778
+ this.db,
25779
+ () => {
25780
+ const result = this.claimStmt.run({
25781
+ pid,
25782
+ host,
25783
+ now: nowMs,
25784
+ staleBefore: nowMs - staleAfterMs
25785
+ });
25786
+ taken = result.changes === 1;
25787
+ },
25788
+ "IMMEDIATE"
25789
+ );
25790
+ return taken;
25791
+ }
25792
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25793
+ heartbeat(pid, nowMs) {
25794
+ this.heartbeatStmt.run({ now: nowMs, pid });
25795
+ }
25796
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25797
+ release(pid) {
25798
+ this.releaseStmt.run({ pid });
25799
+ }
25800
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25801
+ lease() {
25802
+ return getRow(this.leaseStmt);
25803
+ }
25804
+ };
25805
+
25806
+ // ../../packages/persistence/src/migrations.ts
25807
+ function describeObject(object2) {
25808
+ return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
25809
+ }
25810
+ function splitStatements(sql) {
25811
+ return sql.split(/-->\s*statement-breakpoint/).map((s) => s.trim()).filter((s) => s.length > 0);
25812
+ }
25813
+ function createdIndexName(statement) {
25814
+ const body = statement.replace(/^(?:\s*--[^\n]*\n?)+/, "").trimStart();
25815
+ return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
25816
+ }
25817
+ var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
25818
+ function applyMigrations(db, file2, options = {}) {
25819
+ const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
25820
+ db.exec(
25821
+ "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
25822
+ );
25823
+ const applied = new Set(
25824
+ db.prepare("SELECT tag FROM migration_ledger").all().map((r) => r.tag)
25825
+ );
25826
+ const preLedgerStore = applied.size === 0 && legacyCount > 0;
25827
+ const record2 = db.prepare(
25828
+ "INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)"
25829
+ );
25830
+ for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
25831
+ if (applied.has(migration.tag)) continue;
25832
+ if (options.skipTags?.has(migration.tag) === true) continue;
25833
+ if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
25834
+ const evidence = evidenceObjects(migration.sql);
25835
+ const present2 = evidence.filter((o) => evidenceExists(db, o));
25836
+ if (present2.length > 0 && present2.length < evidence.length) {
25837
+ const missing = evidence.filter((o) => !present2.includes(o));
25838
+ const message = `sqlite migration ${migration.tag} has no ledger row, but the store already has ${present2.map(describeObject).join(", ")} while missing ${missing.map(describeObject).join(", ")} \u2014 the schema diverged from the migration history; refusing to replay or skip.`;
25839
+ akaWarn(message);
25840
+ throw new Error(`[aka] ${message}`);
25841
+ }
25842
+ const alreadyApplied = evidence.length > 0 ? present2.length === evidence.length : preLedgerStore && index < legacyCount;
25843
+ const wantsFkOff = /PRAGMA foreign_keys\s*=\s*OFF/i.test(migration.sql);
25844
+ const statements = splitStatements(migration.sql);
25845
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = OFF");
25846
+ try {
25847
+ withTransaction(
25848
+ db,
25849
+ () => {
25850
+ for (const statement of statements) {
25851
+ const indexName = createdIndexName(statement);
25852
+ if (indexName === void 0) {
25853
+ if (alreadyApplied) continue;
25854
+ } else if (indexExists(db, indexName)) {
25855
+ continue;
25856
+ }
25857
+ db.exec(statement);
25858
+ }
25859
+ if (wantsFkOff && !alreadyApplied) {
25860
+ const violations = db.prepare("PRAGMA foreign_key_check").all();
25861
+ if (violations.length > 0) {
25862
+ throw new Error(
25863
+ `[aka] sqlite migration ${migration.tag} left ${String(violations.length)} foreign-key violation(s); rolling back.`
25864
+ );
25865
+ }
25866
+ }
25867
+ record2.run(migration.tag, Date.now());
25868
+ },
25869
+ "IMMEDIATE"
25870
+ );
25871
+ } finally {
25872
+ if (wantsFkOff) db.exec("PRAGMA foreign_keys = ON");
25873
+ }
25874
+ }
25875
+ if (legacyCount < SQLITE_MIGRATIONS.length) {
25876
+ db.exec(`PRAGMA user_version = ${String(SQLITE_MIGRATIONS.length)}`);
25877
+ }
25878
+ ensureSyncedAtColumn(db, "audit_events");
25879
+ ensureScanLedgerTable(db);
25880
+ ensureHistorySyncTable(db);
25881
+ ensureBlockedDetectionsTable(db);
25882
+ ensureRuleProbeCacheTable(db);
25883
+ ensureWriteGateTrigger(db);
25884
+ ensureTokenUsageColumns(db);
25885
+ reconcileSourceProjectIds(db);
25886
+ if (!applied.has(LEGACY_DROP_MIGRATION_TAG)) {
25887
+ const drained = runLegacyHistoryBackfill(db);
25888
+ if (drained) applyLegacyDropMigration(db, file2);
25889
+ }
25890
+ }
25891
+ function readLegacyTables(db) {
25892
+ let holdsRows = false;
25893
+ const marks = [];
25894
+ for (const table2 of ["events", "findings"]) {
25895
+ try {
25896
+ const row = db.prepare(`SELECT count(*) AS n, ifnull(max(rowid), -1) AS hi FROM ${table2}`).get();
25897
+ if (row === void 0) {
25898
+ holdsRows = true;
25899
+ marks.push(`${table2}:unreadable`);
25900
+ continue;
25901
+ }
25902
+ if (row.n > 0) holdsRows = true;
25903
+ marks.push(`${table2}:${String(row.n)}:${String(row.hi)}`);
25904
+ } catch {
25905
+ holdsRows = true;
25906
+ marks.push(`${table2}:unreadable`);
25907
+ }
25908
+ }
25909
+ return { holdsRows, mark: marks.join("|") };
25910
+ }
25911
+ function applyLegacyDropMigration(db, file2) {
25912
+ const migration = SQLITE_MIGRATIONS.find((m) => m.tag === LEGACY_DROP_MIGRATION_TAG);
25913
+ if (!migration) return;
25914
+ const before = file2 === void 0 ? void 0 : readLegacyTables(db);
25915
+ if (file2 !== void 0 && before?.holdsRows === true) {
25916
+ try {
25917
+ backupBeforeLegacyDrop(db, file2);
25918
+ } catch (error61) {
25919
+ akaWarn(`legacy events/findings backup failed; deferring the drop: ${String(error61)}`);
25920
+ return;
25921
+ }
25922
+ }
25923
+ try {
25924
+ withTransaction(
25925
+ db,
25926
+ () => {
25927
+ const alreadyDropped = db.prepare("SELECT 1 FROM migration_ledger WHERE tag = ?").get(migration.tag);
25928
+ if (alreadyDropped) return;
25929
+ if (before !== void 0 && readLegacyTables(db).mark !== before.mark) {
25930
+ akaWarn(
25931
+ "legacy events/findings rows changed after the pre-drop snapshot decision; deferring the drop so the next open can copy them first."
25932
+ );
25933
+ return;
25934
+ }
25935
+ for (const statement of splitStatements(migration.sql)) {
25936
+ db.exec(statement);
25937
+ }
25938
+ db.prepare("INSERT OR IGNORE INTO migration_ledger (tag, applied_at) VALUES (?, ?)").run(
25939
+ migration.tag,
25940
+ Date.now()
25941
+ );
24922
25942
  },
24923
25943
  "IMMEDIATE"
24924
25944
  );
@@ -25216,10 +26236,62 @@ function ensureSyncedAtColumn(db, table2) {
25216
26236
  if (!columns.includes("outbox_owed")) {
25217
26237
  db.exec(`ALTER TABLE ${table2} ADD COLUMN outbox_owed integer`);
25218
26238
  }
26239
+ if (!columns.includes("sync_failed_at")) {
26240
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failed_at integer`);
26241
+ }
26242
+ if (!columns.includes("sync_failure")) {
26243
+ withTransaction(
26244
+ db,
26245
+ () => {
26246
+ db.exec(`ALTER TABLE ${table2} ADD COLUMN sync_failure text`);
26247
+ db.exec(
26248
+ `UPDATE ${table2} SET synced_at = NULL
26249
+ WHERE synced_at = -1
26250
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26251
+ );
26252
+ },
26253
+ "IMMEDIATE"
26254
+ );
26255
+ }
25219
26256
  db.exec(
25220
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25221
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26257
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26258
+ BEFORE UPDATE OF sync_failure ON ${table2}
26259
+ WHEN ${syncFailureRejectCondition()}
26260
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25222
26261
  );
26262
+ const syncIndexColumns = [
26263
+ "event_type",
26264
+ "synced_at",
26265
+ "sync_claimed_at",
26266
+ "started_at",
26267
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26268
+ // has to be in the index for the read to stay covered — but putting it
26269
+ // ahead of `started_at` would reorder the prefix the structural drain's
26270
+ // reads match on.
26271
+ "sync_failure"
26272
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26273
+ //
26274
+ // The delivery-state read tests it — a capture's state depends on whether a
26275
+ // live forward marked it owed — so carrying it here makes that read covering
26276
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26277
+ // But a sixth column changes what the planner charges for this index, and
26278
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26279
+ // then stops choosing the per-session index for the token rollup and walks
26280
+ // every `llm_call` in the store through the event-type index instead. That
26281
+ // read grows with the store; this one does not.
26282
+ //
26283
+ // 40 ms on the largest store measured, once per render, is a cost worth
26284
+ // paying to leave every other read's plan where it was.
26285
+ ];
26286
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26287
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26288
+ if (!syncIndexMatches) {
26289
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26290
+ db.exec(
26291
+ `CREATE INDEX idx_audit_events_sync
26292
+ ON audit_events (${syncIndexColumns.join(", ")})`
26293
+ );
26294
+ }
25223
26295
  db.exec(
25224
26296
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25225
26297
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25441,7 +26513,11 @@ function buildAuditEvent(row) {
25441
26513
  link: linkParsed?.success ? linkParsed.data : null,
25442
26514
  targetId: row.target_id,
25443
26515
  internal: intToBool(row.internal),
25444
- flagged: intToBool(row.flagged)
26516
+ flagged: intToBool(row.flagged),
26517
+ // Only meaningful when the title came out empty — a row whose body was
26518
+ // expired but whose title fell back to `tool_name` still has something to
26519
+ // render, and flagging it would make the view apologise for nothing.
26520
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25445
26521
  };
25446
26522
  }
25447
26523
  var TIMELINE_COLUMNS = `
@@ -25449,6 +26525,7 @@ var TIMELINE_COLUMNS = `
25449
26525
  event_type,
25450
26526
  started_at,
25451
26527
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26528
+ content_expired_at,
25452
26529
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25453
26530
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25454
26531
  json_extract(attributes, '$.severity') AS severity,
@@ -25575,7 +26652,8 @@ var SqliteActivityRepository = class {
25575
26652
  SELECT 1 FROM audit_events d
25576
26653
  WHERE d.root_session_id = audit_events.id
25577
26654
  AND (d.content LIKE ? ESCAPE '\\'
25578
- OR json_extract(d.attributes, '$.detail') LIKE ? ESCAPE '\\')))`
26655
+ OR coalesce(json_extract(d.attributes, '$.detail'),
26656
+ json_extract(d.attributes, '$.target')) LIKE ? ESCAPE '\\')))`
25579
26657
  );
25580
26658
  params.push(pattern, pattern, pattern, pattern, pattern, pattern);
25581
26659
  }
@@ -26113,6 +27191,88 @@ var SqliteAuditEventsRepository = class {
26113
27191
  }
26114
27192
  };
26115
27193
 
27194
+ // ../../packages/persistence/src/repositories/body-retention.ts
27195
+ var DEFAULT_BATCH_SIZE = 500;
27196
+ var DEFAULT_MAX_ROWS = 5e4;
27197
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27198
+ var SqliteBodyRetentionRepository = class {
27199
+ constructor(db) {
27200
+ this.db = db;
27201
+ const select = (laneClause) => `
27202
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27203
+ FROM audit_events
27204
+ WHERE content IS NOT NULL
27205
+ AND started_at < :cutoff
27206
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27207
+ ${laneClause}
27208
+ ORDER BY started_at
27209
+ LIMIT :limit`;
27210
+ this.candidatesStmt = this.db.prepare(select(""));
27211
+ this.candidatesSyncSafeStmt = this.db.prepare(
27212
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27213
+ );
27214
+ this.heldBySyncStmt = this.db.prepare(`
27215
+ SELECT COUNT(*) AS n
27216
+ FROM audit_events
27217
+ WHERE content IS NOT NULL
27218
+ AND started_at < :cutoff
27219
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27220
+ AND synced_at IS NULL`);
27221
+ this.expireStmt = this.db.prepare(
27222
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27223
+ );
27224
+ }
27225
+ db;
27226
+ candidatesStmt;
27227
+ candidatesSyncSafeStmt;
27228
+ heldBySyncStmt;
27229
+ expireStmt;
27230
+ /** How many bytes a pass with these options would free, changing nothing. */
27231
+ preview(opts) {
27232
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27233
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27234
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27235
+ return {
27236
+ rowsExpired: rows.length,
27237
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27238
+ rowsHeldBySync: this.countHeldBySync(opts)
27239
+ };
27240
+ }
27241
+ /** Clear eligible bodies, in bounded batches. */
27242
+ expire(opts) {
27243
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27244
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27245
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27246
+ let rowsExpired = 0;
27247
+ let bytesFreed = 0;
27248
+ let done = true;
27249
+ while (rowsExpired < maxRows) {
27250
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27251
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27252
+ if (batch.length === 0) break;
27253
+ withTransaction(
27254
+ this.db,
27255
+ () => {
27256
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27257
+ },
27258
+ "IMMEDIATE"
27259
+ );
27260
+ rowsExpired += batch.length;
27261
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27262
+ if (batch.length < remaining) break;
27263
+ if (rowsExpired >= maxRows) {
27264
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27265
+ }
27266
+ }
27267
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27268
+ }
27269
+ countHeldBySync(opts) {
27270
+ if (opts.sweepSyncLane) return 0;
27271
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27272
+ return row.n;
27273
+ }
27274
+ };
27275
+
26116
27276
  // ../../packages/persistence/src/repositories/classified-data.ts
26117
27277
  var SqliteClassifiedDataRepository = class {
26118
27278
  constructor(db) {
@@ -26913,23 +28073,6 @@ var LATEST_RESOLUTION_BY_KEY_SQL = `(
26913
28073
  )`;
26914
28074
 
26915
28075
  // ../../packages/persistence/src/repositories/findings.ts
26916
- var PREVIEW_INSTANCES_PER_GROUP = 200;
26917
- var DEFAULT_LOCATIONS_LIMIT = 100;
26918
- var LOCATION_RULE_IDS_CAP = 20;
26919
- function compareLocationOrder(a, b) {
26920
- return compareFindingGroupOrder(
26921
- {
26922
- severity: a.maxSeverity,
26923
- latestDetectedAt: a.latestDetectedAt,
26924
- id: ""
26925
- },
26926
- {
26927
- severity: b.maxSeverity,
26928
- latestDetectedAt: b.latestDetectedAt,
26929
- id: ""
26930
- }
26931
- );
26932
- }
26933
28076
  var CONCAT_SEP = ",";
26934
28077
  var TUPLE_SEP = "|";
26935
28078
  function splitConcat(value) {
@@ -26958,7 +28101,15 @@ function toFlatFindingRow(r) {
26958
28101
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26959
28102
  eventId: r.event_id,
26960
28103
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26961
- status: deriveInstanceStatus(r)
28104
+ status: deriveInstanceStatus(r),
28105
+ delivery: deriveFindingDelivery({
28106
+ kind: r.kind,
28107
+ syncedAt: r.synced_at,
28108
+ syncClaimedAt: r.sync_claimed_at,
28109
+ syncFailedAt: r.sync_failed_at,
28110
+ syncFailure: r.sync_failure,
28111
+ outboxOwed: r.outbox_owed
28112
+ })
26962
28113
  };
26963
28114
  }
26964
28115
  function encodeGroupCursor(group) {
@@ -26981,13 +28132,51 @@ function decodeGroupCursor(cursor) {
26981
28132
  return null;
26982
28133
  }
26983
28134
  function firstAfter(sorted, cursor) {
26984
- const index = sorted.findIndex((g) => compareFindingGroupOrder(g, cursor) > 0);
28135
+ const index = sorted.findIndex((t) => compareFindingGroupOrder(t, cursor) > 0);
26985
28136
  return index === -1 ? sorted.length : index;
26986
28137
  }
26987
28138
  function findDeepLinked(sorted, page, id) {
26988
- if (page.some((g) => g.id === id || g.instances.some((i) => i.id === id))) return void 0;
26989
- return sorted.find((g) => g.id === id || g.instances.some((i) => i.id === id));
28139
+ if (page.some((t) => t.id === id)) return void 0;
28140
+ return sorted.find((t) => t.id === id);
28141
+ }
28142
+ function encodeLocationCursor(location) {
28143
+ const payload = {
28144
+ sev: location.maxSeverity,
28145
+ t: location.latestDetectedAt,
28146
+ r: location.repo,
28147
+ f: location.file
28148
+ };
28149
+ return Buffer.from(JSON.stringify(payload)).toString("base64url");
28150
+ }
28151
+ function decodeLocationCursor(cursor) {
28152
+ const parsed2 = parseJsonObject(Buffer.from(cursor, "base64url").toString("utf8"));
28153
+ if (parsed2 !== void 0 && typeof parsed2.sev === "string" && typeof parsed2.t === "string" && typeof parsed2.r === "string" && typeof parsed2.f === "string") {
28154
+ return { maxSeverity: parsed2.sev, latestDetectedAt: parsed2.t, repo: parsed2.r, file: parsed2.f };
28155
+ }
28156
+ return null;
28157
+ }
28158
+ function firstLocationAfter(sorted, cursor) {
28159
+ const index = sorted.findIndex((l) => compareLocationOrder(l, cursor) > 0);
28160
+ return index === -1 ? sorted.length : index;
26990
28161
  }
28162
+ function findDeepLinkedLocation(sorted, page, id) {
28163
+ if (page.some((l) => l.id === id)) return void 0;
28164
+ return sorted.find((l) => l.id === id);
28165
+ }
28166
+ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS category,
28167
+ d.severity AS severity, f.masked_match AS masked_match,
28168
+ f.action_taken AS action_taken, f.confidence AS confidence,
28169
+ e.started_at AS occurred_at,
28170
+ e.source_tool AS source_tool,
28171
+ e.repo AS repo,
28172
+ e.file_path AS file,
28173
+ e.tool_name AS tool_name,
28174
+ f.audit_event_id AS event_id, e.root_session_id AS session_id,
28175
+ e.event_type AS kind, f.finding_key AS finding_key,
28176
+ ${latestResolutionStatusSql("f")} AS latest_status,
28177
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28178
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28179
+ e.outbox_owed AS outbox_owed`;
26991
28180
  var DAY_MS3 = 864e5;
26992
28181
  var SqliteFindingsRepository = class {
26993
28182
  constructor(db) {
@@ -27108,30 +28297,26 @@ var SqliteFindingsRepository = class {
27108
28297
  );
27109
28298
  }
27110
28299
  /**
27111
- * Grouped findings for the dashboard — joins inspection_findings⋈audit_events
27112
- * ⋈inspection_definitions (repo/file/toolName from the audit event's
27113
- * attributes bag, rule_id/category/severity from the definition), scoped to
27114
- * the four capture kinds (audit_events also holds structural/reconciler/scan
27115
- * rows this list must never surface), groups by ruleId, computes
27116
- * per-filter-excluded facets, applies the requested filters, and sorts by
27117
- * severity then recency. Filtering
27118
- * and faceting run in JS via the shared @akasecurity/schema helpers. `totals`
27119
- * reflect the full filtered set; `items` is the requested
27120
- * page (default 50); no cursor (nextCursor is always null). Under a `status`
27121
- * filter, `totals.findings` counts only instances whose derived status was
27122
- * requested, and each item's instance preview is narrowed the same way.
28300
+ * Finding TYPES for the dashboard — one row per rule, scoped to the four
28301
+ * capture kinds (audit_events also holds structural/reconciler/scan rows this
28302
+ * list must never surface), with per-filter-excluded facets, the requested
28303
+ * filters applied, and sorted by severity then recency. Filtering and faceting
28304
+ * run in JS via the shared @akasecurity/schema helpers. `totals` reflect the
28305
+ * full filtered set; `items` is the requested page (default 50), keyset-paged.
28306
+ * Under a `status` filter, `totals.findings` counts only findings whose
28307
+ * derived status was requested.
28308
+ *
28309
+ * ONE read, which materializes no findings: a single aggregate per rule_id,
28310
+ * folding EVERY finding into the numbers a type row and the filters need
28311
+ * (count, severity, category, providers, actions, statuses, latest, search
28312
+ * text). The findings OF a type come from listFindingInstances scoped to
28313
+ * `subtype`, so neither list bounds the other and no per-type cap exists.
27123
28314
  *
27124
- * Two reads, neither of which materializes a row per finding:
27125
- * 1. one aggregate row per rule_id, folding EVERY instance into the numbers
27126
- * the group and the filters need (count, providers, actions, statuses,
27127
- * latest, search text);
27128
- * 2. each group's newest PREVIEW_INSTANCES_PER_GROUP instances, which
27129
- * populate `instances` for the table's expanded rows.
27130
28315
  * The aggregates carry raw DB values and are translated by the same
27131
- * @akasecurity/schema mappers the row path uses, so no enum mapping or status
27132
- * rule is ever restated in SQL.
28316
+ * @akasecurity/schema mappers every other path uses, so no enum mapping or
28317
+ * status rule is ever restated in SQL.
27133
28318
  */
27134
- listGroupedFindings(query) {
28319
+ listFindingTypes(query) {
27135
28320
  const sessionPredicate = query.sessionId ? ` AND e.root_session_id = :sessionId` : "";
27136
28321
  const fromMs = query.from === void 0 ? void 0 : isoToEpochMillis(query.from);
27137
28322
  const fromPredicate = fromMs === void 0 ? "" : ` AND e.started_at >= :fromMs`;
@@ -27144,12 +28329,7 @@ var SqliteFindingsRepository = class {
27144
28329
  predicate,
27145
28330
  params: sessionParams
27146
28331
  });
27147
- const rows = this.previewRows(aggregates, {
27148
- sessionId: query.sessionId,
27149
- from: query.from
27150
- });
27151
- const groupable = rows.map(toFlatFindingRow);
27152
- const allGroups = buildFindingGroups(groupable, { aggregates });
28332
+ const allTypes = buildFindingTypes(aggregates);
27153
28333
  const filterOpts = {
27154
28334
  severity: query.severity,
27155
28335
  providers: query.provider,
@@ -27158,30 +28338,25 @@ var SqliteFindingsRepository = class {
27158
28338
  subtype: query.subtype,
27159
28339
  q: query.q
27160
28340
  };
27161
- const facets = computeFindingFacets(allGroups, filterOpts);
27162
- const sorted = sortFindingGroups(applyFindingFilters(allGroups, filterOpts));
28341
+ const facets = computeFindingFacets(allTypes, filterOpts);
28342
+ const sorted = sortFindingTypes(applyFindingFilters(allTypes, filterOpts));
27163
28343
  const statusFilter = query.status ?? [];
27164
28344
  const totals = {
27165
- findings: sorted.reduce((acc, g) => {
27166
- if (statusFilter.length === 0) return acc + g.instanceCount;
27167
- const agg = aggregates.get(g.id);
27168
- return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? g.instanceCount : g.instanceCount);
28345
+ findings: sorted.reduce((acc, t) => {
28346
+ if (statusFilter.length === 0) return acc + t.instanceCount;
28347
+ const agg = aggregates.get(t.id);
28348
+ return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? t.instanceCount : t.instanceCount);
27169
28349
  }, 0),
27170
- groups: sorted.length
28350
+ types: sorted.length
27171
28351
  };
27172
- const limit = query.limit ?? DEFAULT_GROUPED_FINDINGS_LIMIT;
28352
+ const limit = query.limit ?? DEFAULT_FINDING_TYPES_LIMIT;
27173
28353
  const cursor = query.cursor === void 0 ? null : decodeGroupCursor(query.cursor);
27174
28354
  const start = cursor === null ? 0 : firstAfter(sorted, cursor);
27175
28355
  const page = sorted.slice(start, start + limit);
27176
28356
  const lastOnPage = page.at(-1);
27177
28357
  const nextCursor = start + limit < sorted.length && lastOnPage ? encodeGroupCursor(lastOnPage) : null;
27178
28358
  const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinked(sorted, page, query.includeId);
27179
- const statusSet = statusFilter.length > 0 ? new Set(statusFilter) : null;
27180
- const narrow = (g) => statusSet ? {
27181
- ...g,
27182
- instances: g.instances.filter((i) => i.status !== void 0 && statusSet.has(i.status))
27183
- } : g;
27184
- const items = [...page, ...deepLinked ? [deepLinked] : []].map(narrow);
28359
+ const items = [...page, ...deepLinked ? [deepLinked] : []];
27185
28360
  return Promise.resolve({
27186
28361
  totals,
27187
28362
  facets,
@@ -27192,7 +28367,7 @@ var SqliteFindingsRepository = class {
27192
28367
  }
27193
28368
  /**
27194
28369
  * One row per rule_id, folding EVERY instance of the group into the values
27195
- * buildFindingGroups cannot recover from a preview. Bounded by the number of
28370
+ * buildFindingTypes cannot recover from an aggregate. Bounded by the number of
27196
28371
  * distinct rule_ids (the installed packs' rules), not by the store's size.
27197
28372
  *
27198
28373
  * A single scan, folded in two levels: the inner SELECT groups by
@@ -27246,6 +28421,7 @@ var SqliteFindingsRepository = class {
27246
28421
  providers: query.provider,
27247
28422
  actions: query.action,
27248
28423
  statuses: query.status,
28424
+ deliveries: query.deployment,
27249
28425
  tools: query.tool,
27250
28426
  repo: query.repo,
27251
28427
  file: query.file,
@@ -27286,13 +28462,25 @@ var SqliteFindingsRepository = class {
27286
28462
  });
27287
28463
  }
27288
28464
  /**
27289
- * The same findings folded by location: repository, then file within it.
28465
+ * The same findings folded by WHERE they live — one row per (repo, file) pair.
27290
28466
  *
27291
28467
  * The grouping keys come from the capturing event's attributes, which is what
27292
- * the local store relates a finding to — there is no finding↔asset row to
27293
- * group by instead. A repo or file the event did not record folds into the
27294
- * empty-string bucket, which the view renders but does not link, since no
27295
- * filter can name it.
28468
+ * the local store relates a finding to; there is no finding↔asset row to group
28469
+ * by instead. A repo or file the event did not record folds into the
28470
+ * empty-string bucket, which is a real location like any other: it is listed,
28471
+ * it is selectable, and its `?loc=` token is as good as any other row's.
28472
+ *
28473
+ * ONE flat list rather than repos nesting files. A rollup can only be paged by
28474
+ * repo, which leaves the file list inside it unbounded — the shape the by-type
28475
+ * list was rebuilt to remove — and two-level pagination inside an
28476
+ * expand/collapse table is what pushed that view to master/detail in the first
28477
+ * place.
28478
+ *
28479
+ * Every filter narrows the FINDINGS and the locations fall out of what
28480
+ * survives, so each row's `instanceCount` is exactly what listFindingInstances
28481
+ * reports for the same filters scoped to that pair. The view depends on it:
28482
+ * one toolbar sits over both panels precisely because a location owns none of
28483
+ * its fields.
27296
28484
  */
27297
28485
  listFindingLocations(query) {
27298
28486
  const opts = {
@@ -27301,16 +28489,20 @@ var SqliteFindingsRepository = class {
27301
28489
  providers: query.provider,
27302
28490
  actions: query.action,
27303
28491
  statuses: query.status,
28492
+ deliveries: query.deployment,
27304
28493
  tools: query.tool,
27305
28494
  q: query.q
27306
28495
  };
27307
- const limit = query.limit ?? DEFAULT_LOCATIONS_LIMIT;
28496
+ const limit = query.limit ?? DEFAULT_FINDING_LOCATIONS_LIMIT;
28497
+ const cursor = query.cursor === void 0 ? null : decodeLocationCursor(query.cursor);
27308
28498
  const byRepo = /* @__PURE__ */ new Map();
28499
+ const accumulator = createInstanceFacetAccumulator(opts);
27309
28500
  let total = 0;
27310
28501
  for (const row of this.scanFindingRows({
27311
28502
  sessionId: query.sessionId,
27312
28503
  from: query.from
27313
28504
  })) {
28505
+ accumulator.add(row);
27314
28506
  if (!matchesInstanceFilters(row, opts)) continue;
27315
28507
  total += 1;
27316
28508
  let files = byRepo.get(row.repo);
@@ -27325,103 +28517,35 @@ var SqliteFindingsRepository = class {
27325
28517
  }
27326
28518
  addToLocation(acc, row);
27327
28519
  }
27328
- let fileCount = 0;
27329
- const repos = [...byRepo.entries()].map(([repo, files]) => {
27330
- fileCount += files.size;
27331
- const fileRows = [...files.entries()].map(([file2, acc]) => ({
27332
- file: file2,
27333
- instanceCount: acc.instanceCount,
27334
- maxSeverity: acc.maxSeverity,
27335
- latestDetectedAt: acc.latestDetectedAt,
27336
- ...foldGroupStatus(acc.statuses) === void 0 ? {} : { status: foldGroupStatus(acc.statuses) },
27337
- ruleIds: [...acc.ruleIds].slice(0, LOCATION_RULE_IDS_CAP)
27338
- })).sort(compareLocationOrder);
27339
- const rollup = fileRows.reduce(
27340
- (a, f) => ({
27341
- instanceCount: a.instanceCount + f.instanceCount,
27342
- maxSeverity: compareLocationOrder(f, a) < 0 ? f.maxSeverity : a.maxSeverity,
27343
- latestDetectedAt: f.latestDetectedAt > a.latestDetectedAt ? f.latestDetectedAt : a.latestDetectedAt
27344
- }),
27345
- {
27346
- instanceCount: 0,
27347
- maxSeverity: fileRows[0]?.maxSeverity ?? "low",
27348
- latestDetectedAt: ""
27349
- }
27350
- );
27351
- const statuses = fileRows.map((f) => f.status);
27352
- const folded = foldGroupStatus(statuses);
27353
- return {
27354
- repo,
27355
- instanceCount: rollup.instanceCount,
27356
- maxSeverity: rollup.maxSeverity,
27357
- latestDetectedAt: rollup.latestDetectedAt,
27358
- ...folded === void 0 ? {} : { status: folded },
27359
- files: fileRows
27360
- };
27361
- });
27362
- repos.sort(compareLocationOrder);
28520
+ const sorted = [];
28521
+ for (const [repo, files] of byRepo) {
28522
+ for (const [file2, acc] of files) {
28523
+ const status = foldGroupStatus(acc.statuses);
28524
+ sorted.push({
28525
+ id: encodeLocationId(repo, file2),
28526
+ repo,
28527
+ file: file2,
28528
+ instanceCount: acc.instanceCount,
28529
+ maxSeverity: acc.maxSeverity,
28530
+ latestDetectedAt: acc.latestDetectedAt,
28531
+ ...status === void 0 ? {} : { status },
28532
+ ruleIds: [...acc.ruleIds]
28533
+ });
28534
+ }
28535
+ }
28536
+ sorted.sort(compareLocationOrder);
28537
+ const start = cursor === null ? 0 : firstLocationAfter(sorted, cursor);
28538
+ const page = sorted.slice(start, start + limit);
28539
+ const lastOnPage = page.at(-1);
28540
+ const nextCursor = start + limit < sorted.length && lastOnPage ? encodeLocationCursor(lastOnPage) : null;
28541
+ const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinkedLocation(sorted, page, query.includeId);
27363
28542
  return Promise.resolve({
27364
- totals: { findings: total, repos: repos.length, files: fileCount },
27365
- items: repos.slice(0, limit),
27366
- hasMore: repos.length > limit
28543
+ totals: { findings: total, locations: sorted.length },
28544
+ facets: accumulator.facets(),
28545
+ items: [...page, ...deepLinked ? [deepLinked] : []],
28546
+ nextCursor
27367
28547
  });
27368
28548
  }
27369
- /**
27370
- * Each group's newest instances, for the table's expanded rows.
27371
- *
27372
- * ONE index-ordered scan with early termination, and the shape is the point.
27373
- * The natural spelling — `ROW_NUMBER() OVER (PARTITION BY rule_id ORDER BY
27374
- * started_at DESC)` then `WHERE rn <= cap` — sorts EVERY finding in scope
27375
- * through a temp B-tree to keep a bounded preview of each group, and then
27376
- * sorts the survivors again for the page order. Both sorts grow with the
27377
- * store while the answer does not.
27378
- *
27379
- * Instead the scan walks `audit_events` newest-first off `idx_audit_started_at`
27380
- * (or the session or window index the scope names — see `findingScanSql`),
27381
- * which is already the order the page wants, and keeps rows per rule until
27382
- * each rule has as many as it can show. The aggregate the caller already holds
27383
- * says how many that is: `min(instanceCount, PREVIEW_INSTANCES_PER_GROUP)`
27384
- * per rule, summed, is the number of rows this scan has to find, and it stops
27385
- * on the last one. That sum is bounded by `rules * PREVIEW_INSTANCES_PER_GROUP`
27386
- * (8,000 at this repo's 40-rule bench corpus), not by a fixed row count — a
27387
- * store with many firing rules widens it. The bound that DOES hold
27388
- * unconditionally is the sorted form's floor: this scan visits at most as
27389
- * many rows as `ROW_NUMBER() OVER (PARTITION BY rule_id …)` would have
27390
- * sorted, and stops the moment every rule has its cap, where the sorted form
27391
- * sorts the whole scope regardless. The true worst case — the rarest rule's
27392
- * wanted instances sitting at the tail of the scope — is one pass over
27393
- * everything in scope with a block sort of the id tie-break only, never a
27394
- * sort of the scope, which is still that floor.
27395
- *
27396
- * A row whose rule the aggregate did not see is skipped: the two statements
27397
- * run without a shared snapshot, so a capture landing between them can add a
27398
- * rule here that has no counts there, and the counts are what the group is
27399
- * built from.
27400
- */
27401
- previewRows(aggregates, scope) {
27402
- const wanted = /* @__PURE__ */ new Map();
27403
- let remaining = 0;
27404
- for (const [ruleId, agg] of aggregates) {
27405
- const n = Math.min(agg.instanceCount, PREVIEW_INSTANCES_PER_GROUP);
27406
- wanted.set(ruleId, n);
27407
- remaining += n;
27408
- }
27409
- const rows = [];
27410
- if (remaining === 0) return rows;
27411
- const { sql, params } = this.findingScanSql(scope);
27412
- const taken = /* @__PURE__ */ new Map();
27413
- for (const r of iterateRows(this.db.prepare(sql), params)) {
27414
- const want = wanted.get(r.rule_id);
27415
- if (want === void 0) continue;
27416
- const have = taken.get(r.rule_id) ?? 0;
27417
- if (have >= want) continue;
27418
- taken.set(r.rule_id, have + 1);
27419
- rows.push(r);
27420
- remaining -= 1;
27421
- if (remaining === 0) break;
27422
- }
27423
- return rows;
27424
- }
27425
28549
  /**
27426
28550
  * Every finding in scope as a FlatFindingRow, newest first, streamed.
27427
28551
  *
@@ -27448,6 +28572,33 @@ var SqliteFindingsRepository = class {
27448
28572
  yield toFlatFindingRow(r);
27449
28573
  }
27450
28574
  }
28575
+ /**
28576
+ * One finding by its own id, or null when no such row exists.
28577
+ *
28578
+ * A primary-key seek on `inspection_findings`, so its cost does not grow with
28579
+ * the store — and, unlike anything derived from a list page, it resolves a
28580
+ * finding of ANY age. That is what the Findings page's one-shot `?finding=`
28581
+ * deep link needs: the id it carries may name a finding thousands of rows
28582
+ * older than anything a first page holds.
28583
+ *
28584
+ * Deliberately UNFILTERED — no capture-kind, session or time predicate. It
28585
+ * RESOLVES an id; whether that row would survive the list's current filters is
28586
+ * a different question, and hiding the target because a filter excludes it is
28587
+ * worse than showing it.
28588
+ *
28589
+ * `groupId` on the result IS the rule id, so this one read answers both "which
28590
+ * type should the list select?" and "what does the drawer show?".
28591
+ */
28592
+ findingInstance(id) {
28593
+ const row = this.db.prepare(
28594
+ `SELECT ${FINDING_ROW_COLUMNS_SQL}
28595
+ FROM inspection_findings f
28596
+ JOIN audit_events e ON e.id = f.audit_event_id
28597
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
28598
+ WHERE f.id = ?`
28599
+ ).get(id);
28600
+ return Promise.resolve(row === void 0 ? null : toInstanceDetail(toFlatFindingRow(row)));
28601
+ }
27451
28602
  /**
27452
28603
  * The one statement both instance-level scans run: every finding in scope,
27453
28604
  * joined to its event and definition, newest first.
@@ -27481,17 +28632,7 @@ var SqliteFindingsRepository = class {
27481
28632
  conditions.push("e.started_at >= ?");
27482
28633
  params.push(isoToEpochMillis(scope.from));
27483
28634
  }
27484
- const sql = `SELECT f.id AS id, d.rule_id AS rule_id, d.category AS category,
27485
- d.severity AS severity, f.masked_match AS masked_match,
27486
- f.action_taken AS action_taken, f.confidence AS confidence,
27487
- e.started_at AS occurred_at,
27488
- e.source_tool AS source_tool,
27489
- e.repo AS repo,
27490
- e.file_path AS file,
27491
- e.tool_name AS tool_name,
27492
- f.audit_event_id AS event_id, e.root_session_id AS session_id,
27493
- e.event_type AS kind, f.finding_key AS finding_key,
27494
- ${latestResolutionStatusSql("f")} AS latest_status
28635
+ const sql = `SELECT ${FINDING_ROW_COLUMNS_SQL}
27495
28636
  FROM audit_events e
27496
28637
  CROSS JOIN inspection_findings f ON f.audit_event_id = e.id
27497
28638
  CROSS JOIN inspection_definitions d ON d.id = f.inspection_definition_id
@@ -27505,6 +28646,26 @@ var SqliteFindingsRepository = class {
27505
28646
  group_concat(DISTINCT 'via ' || e.tool_name) AS tool_names` : `, NULL AS repos, NULL AS files, NULL AS tool_names`;
27506
28647
  const rows = this.db.prepare(
27507
28648
  `SELECT rule_id,
28649
+ -- BARE columns beside max(latest_at), which is deliberate and
28650
+ -- is SQLite's documented behaviour: with a single min()/max()
28651
+ -- in an aggregate query, every bare column takes its value from
28652
+ -- the row that produced the extremum. So these are the severity
28653
+ -- and category of the definition whose finding is NEWEST, which
28654
+ -- is what the row-based build they replaced read off its first
28655
+ -- (newest-first) row.
28656
+ --
28657
+ -- min() is WRONG here and was the defect: inspection_definitions
28658
+ -- holds one row per rule VERSION (see its writer \u2014 a version bump
28659
+ -- mints a new row), so a rule whose severity moved between
28660
+ -- versions has several, and min() picks the ALPHABETICALLY
28661
+ -- smallest \u2014 'low' over 'medium', but 'critical' over 'high'.
28662
+ -- That is arbitrary in direction, and it feeds the badge, the
28663
+ -- filter, the facet counts and the primary sort key.
28664
+ --
28665
+ -- Adding a second min()/max() aggregate here would make these
28666
+ -- bare columns ambiguous again; keep max(latest_at) the only one.
28667
+ severity,
28668
+ category,
27508
28669
  sum(tuple_count) AS instance_count,
27509
28670
  max(latest_at) AS latest_at,
27510
28671
  group_concat(source_tools) AS source_tools,
@@ -27515,6 +28676,14 @@ var SqliteFindingsRepository = class {
27515
28676
  group_concat(tool_names) AS tool_names
27516
28677
  FROM (
27517
28678
  SELECT d.rule_id AS rule_id,
28679
+ -- Severity and category are columns of the DEFINITION, and
28680
+ -- a rule can have SEVERAL definitions (one per version), so
28681
+ -- these are grouped on below and resolved to the newest
28682
+ -- firing version by the outer query's bare-column select.
28683
+ -- They ride the aggregate because the type build has no rows
28684
+ -- to read them off \u2014 see buildFindingTypes.
28685
+ d.severity AS severity,
28686
+ d.category AS category,
27518
28687
  e.event_type || '${TUPLE_SEP}' ||
27519
28688
  (CASE WHEN f.finding_key IS NULL THEN '' ELSE 'k' END) || '${TUPLE_SEP}' ||
27520
28689
  coalesce(latest.status, '') AS status_tuple,
@@ -27529,7 +28698,7 @@ var SqliteFindingsRepository = class {
27529
28698
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
27530
28699
  ON latest.finding_key = f.finding_key
27531
28700
  ${scope.predicate}
27532
- GROUP BY d.rule_id, status_tuple
28701
+ GROUP BY d.rule_id, d.severity, d.category, status_tuple
27533
28702
  )
27534
28703
  GROUP BY rule_id`
27535
28704
  ).all(scope.params);
@@ -27538,6 +28707,8 @@ var SqliteFindingsRepository = class {
27538
28707
  r.rule_id,
27539
28708
  {
27540
28709
  instanceCount: r.instance_count,
28710
+ severity: r.severity,
28711
+ category: r.category,
27541
28712
  sourceTools: splitConcat(r.source_tools),
27542
28713
  actionsTaken: splitConcat(r.actions_taken),
27543
28714
  statusInputs: splitConcat(r.status_inputs).map((tuple2) => {
@@ -27554,7 +28725,7 @@ var SqliteFindingsRepository = class {
27554
28725
  latestDetectedAt: epochMillisToIso(r.latest_at),
27555
28726
  // Free text only — joined and substring-matched, so group_concat's
27556
28727
  // commas need no unpicking (a repo/path containing one still matches).
27557
- // Left undefined (not '') when unfetched, so buildFindingGroups can
28728
+ // Left undefined (not '') when unfetched, so buildFindingTypes can
27558
28729
  // tell "no q this request" from "a group with no repo/file at all"
27559
28730
  // and skip priming a haystack nothing will read.
27560
28731
  ...withSearchText ? {
@@ -27582,7 +28753,9 @@ var SqliteFindingsRepository = class {
27582
28753
  )
27583
28754
  );
27584
28755
  for (const row of grouped) {
27585
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28756
+ if (Object.hasOwn(byAction, row.action_taken)) {
28757
+ byAction[row.action_taken] = row.c;
28758
+ }
27586
28759
  }
27587
28760
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27588
28761
  const sevRows = allRows(
@@ -27599,7 +28772,9 @@ var SqliteFindingsRepository = class {
27599
28772
  )
27600
28773
  );
27601
28774
  for (const row of sevRows) {
27602
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28775
+ if (Object.hasOwn(bySeverity, row.severity)) {
28776
+ bySeverity[row.severity] = row.c;
28777
+ }
27603
28778
  }
27604
28779
  const categories = ENFORCEABLE_CATEGORIES;
27605
28780
  const enabledRows = allRows(
@@ -27648,469 +28823,6 @@ function isoDay(ms) {
27648
28823
  return new Date(ms).toISOString().slice(0, 10);
27649
28824
  }
27650
28825
 
27651
- // ../../packages/persistence/src/repositories/history-sync.ts
27652
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27653
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27654
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27655
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27656
- var SKIPPED = -1;
27657
- var ROW_COLUMNS = `id,
27658
- parent_id AS parentId,
27659
- root_session_id AS rootSessionId,
27660
- event_type AS eventType,
27661
- host_id AS hostId,
27662
- harness_id AS harnessId,
27663
- source_project_id AS sourceProjectId,
27664
- started_at AS startedAt,
27665
- ended_at AS endedAt,
27666
- severity,
27667
- priority,
27668
- content,
27669
- content_hash AS contentHash,
27670
- attributes`;
27671
- var SqliteHistorySyncRepository = class {
27672
- constructor(db) {
27673
- this.db = db;
27674
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27675
- this.sessionsStmt = db.prepare(
27676
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27677
- FROM audit_events
27678
- WHERE synced_at IS NULL
27679
- AND event_type IN (${TYPE_LIST})
27680
- AND started_at < :before
27681
- GROUP BY sessionId
27682
- ORDER BY earliest
27683
- LIMIT :limit`
27684
- );
27685
- this.rowsStmt = db.prepare(
27686
- `SELECT ${ROW_COLUMNS}
27687
- FROM audit_events
27688
- WHERE synced_at IS NULL
27689
- AND event_type IN (${TYPE_LIST})
27690
- AND started_at < :before
27691
- AND COALESCE(root_session_id, id) = :sessionId
27692
- ORDER BY (event_type = 'session') DESC, started_at
27693
- LIMIT :limit`
27694
- );
27695
- this.captureRowsStmt = db.prepare(
27696
- `SELECT ${ROW_COLUMNS}
27697
- FROM audit_events
27698
- WHERE synced_at IS NULL
27699
- AND sync_claimed_at IS NULL
27700
- AND outbox_owed = 1
27701
- AND event_type IN (${CAPTURE_TYPE_LIST})
27702
- AND started_at < :before
27703
- ORDER BY started_at
27704
- LIMIT :limit`
27705
- );
27706
- this.markOwedStmt = db.prepare(
27707
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27708
- );
27709
- this.stampStmt = db.prepare(
27710
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27711
- );
27712
- this.claimRowStmt = db.prepare(
27713
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27714
- );
27715
- this.releaseRowStmt = db.prepare(
27716
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27717
- );
27718
- this.releaseStaleClaimsStmt = db.prepare(
27719
- `UPDATE audit_events SET sync_claimed_at = NULL
27720
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27721
- );
27722
- this.partitionStmt = db.prepare(
27723
- `SELECT
27724
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27725
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27726
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27727
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27728
- COUNT(*) AS total
27729
- FROM audit_events
27730
- WHERE event_type IN (${TYPE_LIST})`
27731
- );
27732
- this.countsStmt = db.prepare(
27733
- `SELECT
27734
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27735
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27736
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27737
- FROM audit_events
27738
- WHERE event_type IN (${TYPE_LIST})`
27739
- );
27740
- this.captureSkipCountStmt = db.prepare(
27741
- `SELECT COUNT(*) AS skipped
27742
- FROM audit_events
27743
- WHERE synced_at = ${String(SKIPPED)}
27744
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27745
- );
27746
- this.fingerprintStmt = db.prepare(
27747
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27748
- FROM history_sync WHERE id = 1`
27749
- );
27750
- this.setFingerprintStmt = db.prepare(
27751
- `UPDATE history_sync
27752
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27753
- WHERE id = 1`
27754
- );
27755
- this.disownCapturesStmt = db.prepare(
27756
- `UPDATE audit_events SET outbox_owed = NULL
27757
- WHERE outbox_owed IS NOT NULL AND event_type IN (${CAPTURE_TYPE_LIST})`
27758
- );
27759
- this.rearmStmt = db.prepare(
27760
- `UPDATE audit_events SET synced_at = NULL
27761
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27762
- );
27763
- this.claimStmt = db.prepare(
27764
- `UPDATE history_sync
27765
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27766
- WHERE id = 1
27767
- AND (owner_pid IS NULL
27768
- OR heartbeat_at IS NULL
27769
- OR heartbeat_at < :staleBefore
27770
- OR heartbeat_at > :now)`
27771
- );
27772
- this.heartbeatStmt = db.prepare(
27773
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27774
- );
27775
- this.releaseStmt = db.prepare(
27776
- `UPDATE history_sync
27777
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27778
- WHERE id = 1 AND owner_pid = :pid`
27779
- );
27780
- this.closeWindowStmt = db.prepare(
27781
- `UPDATE audit_events SET synced_at = :at
27782
- WHERE synced_at IS NULL
27783
- AND event_type IN (${TYPE_LIST})
27784
- AND started_at >= :attachedAt`
27785
- );
27786
- this.releaseBoundaryStmt = db.prepare(
27787
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27788
- );
27789
- this.freezeBoundaryStmt = db.prepare(
27790
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27791
- );
27792
- this.leaseStmt = db.prepare(
27793
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27794
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27795
- FROM history_sync WHERE id = 1`
27796
- );
27797
- this.inspectionsStmt = db.prepare(
27798
- `SELECT d.rule_id AS ruleId,
27799
- d.name AS ruleName,
27800
- d.version AS ruleVersion,
27801
- d.category AS category,
27802
- d.severity AS severity,
27803
- f.span_start AS spanStart,
27804
- f.span_end AS spanEnd,
27805
- f.masked_match AS maskedMatch,
27806
- f.action_taken AS actionTaken,
27807
- f.confidence AS confidence
27808
- FROM inspection_findings f
27809
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27810
- WHERE f.audit_event_id = :auditEventId
27811
- ORDER BY f.span_start, f.id`
27812
- );
27813
- }
27814
- db;
27815
- ensureRowStmt;
27816
- sessionsStmt;
27817
- rowsStmt;
27818
- stampStmt;
27819
- countsStmt;
27820
- fingerprintStmt;
27821
- setFingerprintStmt;
27822
- rearmStmt;
27823
- claimStmt;
27824
- heartbeatStmt;
27825
- releaseStmt;
27826
- leaseStmt;
27827
- inspectionsStmt;
27828
- closeWindowStmt;
27829
- releaseBoundaryStmt;
27830
- freezeBoundaryStmt;
27831
- captureRowsStmt;
27832
- markOwedStmt;
27833
- captureSkipCountStmt;
27834
- disownCapturesStmt;
27835
- partitionStmt;
27836
- claimRowStmt;
27837
- releaseRowStmt;
27838
- releaseStaleClaimsStmt;
27839
- /**
27840
- * The masked detections recorded against one tool call.
27841
- *
27842
- * These travel with the event because a tool call's target is not
27843
- * re-inspectable from the event alone — unlike a capture, where the text
27844
- * itself is re-scannable. What crosses is the masked match and the rule that
27845
- * produced it, never the value.
27846
- */
27847
- inspectionsFor(auditEventId) {
27848
- return allRows(this.inspectionsStmt, { auditEventId });
27849
- }
27850
- /**
27851
- * Sessions with structural rows still to send, oldest first.
27852
- *
27853
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27854
- * read. Anything recorded after the machine attached is the live forward
27855
- * path's to deliver; this drain exists for what was recorded before it, and a
27856
- * row both paths send is at best a duplicate request and at worst — for a
27857
- * session root — an overwrite of the inventory ids the live path resolved.
27858
- */
27859
- pendingSessions(limit, before) {
27860
- return allRows(this.sessionsStmt, { limit, before }).map(
27861
- (r) => r.sessionId
27862
- );
27863
- }
27864
- /** One session's undelivered structural rows within the backlog, root first. */
27865
- pendingRows(sessionId, limit, before) {
27866
- return allRows(this.rowsStmt, { sessionId, limit, before });
27867
- }
27868
- /**
27869
- * Captures this machine still owes the deployment, oldest first.
27870
- *
27871
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27872
- * by a time window — see captureRowsStmt for why a window could not express
27873
- * this. `before` is the grace window that leaves a just-recorded capture to
27874
- * the live path.
27875
- */
27876
- pendingCaptureRows(limit, before) {
27877
- return allRows(this.captureRowsStmt, { limit, before });
27878
- }
27879
- /**
27880
- * Record that a capture is OWED to the deployment.
27881
- *
27882
- * Written by the attached forward path when a live send did not confirm
27883
- * delivery, and read by the drain as the whole of its eligibility test. It is
27884
- * a fact rather than an inference: the machine was attached, the send did not
27885
- * land, so the row is owed — which no time window can state, because the same
27886
- * window that holds the rows a past attachment left owed also holds every
27887
- * capture recorded while the machine was DETACHED, and those were never
27888
- * offered to anyone.
27889
- *
27890
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27891
- * out of the drain's read.
27892
- */
27893
- markCaptureOwed(id) {
27894
- this.markOwedStmt.run({ id });
27895
- }
27896
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27897
- markSynced(ids, atMs) {
27898
- this.stampAll(ids, atMs);
27899
- }
27900
- /**
27901
- * Record that a row will never be sent.
27902
- *
27903
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27904
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27905
- * is retried; marking those would turn one outage into permanent data loss.
27906
- */
27907
- markSkipped(ids) {
27908
- this.stampAll(ids, SKIPPED);
27909
- }
27910
- eachInTransaction(ids, run) {
27911
- if (ids.length === 0) return;
27912
- withTransaction(
27913
- this.db,
27914
- () => {
27915
- for (const id of ids) run(id);
27916
- },
27917
- "IMMEDIATE"
27918
- );
27919
- }
27920
- stampAll(ids, value) {
27921
- if (ids.length === 0) return;
27922
- withTransaction(
27923
- this.db,
27924
- () => {
27925
- for (const id of ids) this.stampStmt.run({ at: value, id });
27926
- },
27927
- "IMMEDIATE"
27928
- );
27929
- }
27930
- /**
27931
- * Claim rows as in-flight.
27932
- *
27933
- * Advisory in exactly the sense the lease is: it records that a send is in
27934
- * progress so a surface can say so, and a lost claim costs a row showing as
27935
- * queued while it is actually being sent. It is not exclusion — the far side
27936
- * settles a duplicate on the row id.
27937
- */
27938
- claimRows(ids, atMs) {
27939
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
27940
- }
27941
- /** Give back a claim without settling — the send failed, the row is queued again. */
27942
- releaseRows(ids) {
27943
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
27944
- }
27945
- /**
27946
- * Clear claims older than `staleBefore`, and report how many were cleared.
27947
- *
27948
- * A process killed between claiming and settling leaves rows claimed with
27949
- * nothing left to settle them. Without this they read as "sending" for ever.
27950
- */
27951
- releaseStaleClaims(staleBefore) {
27952
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
27953
- }
27954
- /**
27955
- * Every tracked row in exactly one delivery state.
27956
- *
27957
- * Takes no boundary on purpose. The boundary answers "what should the drain
27958
- * pick up now", which is a different question from "what state is this row
27959
- * in" — and a machine that has never attached has no boundary to pass, so
27960
- * requiring one would force a caller to invent one and report the whole store
27961
- * as queued.
27962
- */
27963
- partition() {
27964
- const row = getRow(this.partitionStmt, {});
27965
- return {
27966
- queued: row?.queued ?? 0,
27967
- inProgress: row?.inProgress ?? 0,
27968
- synced: row?.synced ?? 0,
27969
- failed: row?.failed ?? 0,
27970
- total: row?.total ?? 0
27971
- };
27972
- }
27973
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
27974
- counts(before) {
27975
- const row = getRow(
27976
- this.countsStmt,
27977
- { before }
27978
- );
27979
- const captures = getRow(this.captureSkipCountStmt);
27980
- return {
27981
- pending: row?.pending ?? 0,
27982
- sent: row?.sent ?? 0,
27983
- skipped: row?.skipped ?? 0,
27984
- capturesSkipped: captures?.skipped ?? 0
27985
- };
27986
- }
27987
- /**
27988
- * The deployment the current stamps were made against, and where its backlog
27989
- * ends.
27990
- *
27991
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
27992
- * machine that has never drained is — and every writer below seeds the row
27993
- * before it needs one, so nothing depends on this creating it. Keeping the
27994
- * write off the gate path matters because the gate runs on every pass while a
27995
- * write has to take the database's write lock.
27996
- */
27997
- deployment() {
27998
- const row = getRow(
27999
- this.fingerprintStmt
28000
- );
28001
- return {
28002
- fingerprint: row?.fingerprint ?? void 0,
28003
- backlogBefore: row?.backlogBefore ?? void 0
28004
- };
28005
- }
28006
- /**
28007
- * Point the ledger at a different deployment, discarding what it recorded
28008
- * about the previous one.
28009
- *
28010
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
28011
- * machine has just left are undelivered as far as the new one is concerned.
28012
- * All three in one transaction, so a crash between them cannot leave stamps
28013
- * attributed to the wrong deployment, or a boundary that belongs to another.
28014
- *
28015
- * The boundary is written HERE and only here, which is what freezes it: a
28016
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
28017
- * unchanged, so this never runs and the backlog does not widen back over rows
28018
- * the live path has since delivered.
28019
- */
28020
- rearmFor(fingerprint, backlogBefore) {
28021
- this.ensureRowStmt.run();
28022
- withTransaction(
28023
- this.db,
28024
- () => {
28025
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
28026
- this.rearmStmt.run();
28027
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
28028
- this.disownCapturesStmt.run();
28029
- }
28030
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
28031
- },
28032
- "IMMEDIATE"
28033
- );
28034
- }
28035
- /**
28036
- * End the attached period: hand its rows to the live path, and release the
28037
- * boundary so the next attachment can freeze a new one.
28038
- *
28039
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
28040
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
28041
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
28042
- * during the detached period, because the machine is not attached. Rows
28043
- * recorded in that window sit after the boundary and before the re-attach, so
28044
- * neither path takes them, and the pending count reports none outstanding.
28045
- *
28046
- * Stamping the attached window is not a claim that every one of those rows
28047
- * reached the deployment — the live path drops on failure and says so
28048
- * elsewhere. It records that they were ITS to deliver, which is exactly the
28049
- * status quo: they sit outside the frozen boundary today and are equally never
28050
- * re-sent. Making it explicit is what lets the boundary move.
28051
- *
28052
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
28053
- * window unstamped — that half-state would re-send the whole attached period
28054
- * on the next attach, which is the failure the boundary exists to prevent.
28055
- */
28056
- closeAttachedWindow(attachedAtMs, atMs) {
28057
- this.ensureRowStmt.run();
28058
- withTransaction(
28059
- this.db,
28060
- () => {
28061
- const row = getRow(this.fingerprintStmt);
28062
- const from = row?.backlogBefore ?? attachedAtMs;
28063
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
28064
- this.releaseBoundaryStmt.run();
28065
- },
28066
- "IMMEDIATE"
28067
- );
28068
- }
28069
- /**
28070
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28071
- *
28072
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28073
- * different deployment and therefore discards what was delivered to the old
28074
- * one: here the recipient is the same, so everything already sent to it stays
28075
- * sent.
28076
- */
28077
- freezeBoundary(backlogBefore) {
28078
- this.ensureRowStmt.run();
28079
- this.freezeBoundaryStmt.run({ backlogBefore });
28080
- }
28081
- /** Take the claim, or report that someone live already holds it. */
28082
- claim(pid, host, nowMs, staleAfterMs) {
28083
- this.ensureRowStmt.run();
28084
- let taken = false;
28085
- withTransaction(
28086
- this.db,
28087
- () => {
28088
- const result = this.claimStmt.run({
28089
- pid,
28090
- host,
28091
- now: nowMs,
28092
- staleBefore: nowMs - staleAfterMs
28093
- });
28094
- taken = result.changes === 1;
28095
- },
28096
- "IMMEDIATE"
28097
- );
28098
- return taken;
28099
- }
28100
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28101
- heartbeat(pid, nowMs) {
28102
- this.heartbeatStmt.run({ now: nowMs, pid });
28103
- }
28104
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28105
- release(pid) {
28106
- this.releaseStmt.run({ pid });
28107
- }
28108
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28109
- lease() {
28110
- return getRow(this.leaseStmt);
28111
- }
28112
- };
28113
-
28114
28826
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28115
28827
  var SqliteInspectionDefinitionsRepository = class {
28116
28828
  constructor(db) {
@@ -28302,7 +29014,8 @@ function managedSettingsPaths(platform2 = process.platform) {
28302
29014
  }
28303
29015
  return [posix.join("/etc", "aka", MANAGED_SETTINGS_FILENAME)];
28304
29016
  }
28305
- function readManagedSettings(paths = managedSettingsPaths()) {
29017
+ var testOnlyManagedPaths = null;
29018
+ function readManagedSettings(paths = testOnlyManagedPaths ?? managedSettingsPaths()) {
28306
29019
  for (const path of paths) {
28307
29020
  let text;
28308
29021
  try {
@@ -28337,6 +29050,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28337
29050
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28338
29051
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28339
29052
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
29053
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28340
29054
  if (values.vaultConsent !== void 0) {
28341
29055
  merged.vaultConsent = values.vaultConsent ? (
28342
29056
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30782,7 +31496,7 @@ function toUtcDateString(ms) {
30782
31496
  return new Date(ms).toISOString().slice(0, 10);
30783
31497
  }
30784
31498
  function isTimeseriesSeverity(s) {
30785
- return s === "critical" || s === "high" || s === "medium";
31499
+ return s === "critical" || s === "high" || s === "medium" || s === "low";
30786
31500
  }
30787
31501
  var SqliteSecurityRepository = class {
30788
31502
  constructor(db, now = () => Date.now()) {
@@ -30844,7 +31558,7 @@ var SqliteSecurityRepository = class {
30844
31558
  ELSE 0
30845
31559
  END) AS open_at_rest
30846
31560
  FROM inspection_findings f
30847
- JOIN audit_events e ON e.id = f.audit_event_id
31561
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30848
31562
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30849
31563
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30850
31564
  ON latest.finding_key = f.finding_key
@@ -30911,12 +31625,16 @@ var SqliteSecurityRepository = class {
30911
31625
  const now = this.now();
30912
31626
  const windowStart = startOfUtcDay2(now) - (lenDays - 1) * DAY_MS4;
30913
31627
  const rows = this.findingsInRange(windowStart, now);
30914
- const points = Array.from({ length: numBuckets }, (_, i) => ({
30915
- timestamp: toUtcDateString(windowStart + i * bucketMs),
30916
- critical: 0,
30917
- high: 0,
30918
- medium: 0
30919
- }));
31628
+ const points = Array.from(
31629
+ { length: numBuckets },
31630
+ (_, i) => ({
31631
+ timestamp: toUtcDateString(windowStart + i * bucketMs),
31632
+ critical: 0,
31633
+ high: 0,
31634
+ medium: 0,
31635
+ low: 0
31636
+ })
31637
+ );
30920
31638
  for (const r of rows) {
30921
31639
  const idx = Math.floor((r.occurredAt - windowStart) / bucketMs);
30922
31640
  const bucket = points[idx];
@@ -31066,7 +31784,7 @@ var SqliteSecurityRepository = class {
31066
31784
  this.db.prepare(
31067
31785
  `SELECT e.repo AS repo, count(*) AS c
31068
31786
  FROM inspection_findings f
31069
- JOIN audit_events e ON e.id = f.audit_event_id
31787
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31070
31788
  WHERE e.started_at >= :from AND e.started_at < :to
31071
31789
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31072
31790
  AND e.repo IS NOT NULL
@@ -31134,6 +31852,7 @@ var SqliteSecurityRepository = class {
31134
31852
  `SELECT f.finding_key AS finding_key,
31135
31853
  d.rule_id AS rule_id,
31136
31854
  d.severity AS severity,
31855
+ e.repo AS repo,
31137
31856
  e.file_path AS path,
31138
31857
  COALESCE(f.first_detected_at, e.started_at) AS first_detected_at,
31139
31858
  latest.resolved_at AS latest_resolved_at
@@ -31153,6 +31872,7 @@ var SqliteSecurityRepository = class {
31153
31872
  const items = rows.map((r) => ({
31154
31873
  findingKey: r.finding_key,
31155
31874
  ruleId: r.rule_id,
31875
+ repo: r.repo ?? "",
31156
31876
  severity: r.severity,
31157
31877
  path: r.path ?? "",
31158
31878
  resolvedAt: new Date(r.latest_resolved_at).toISOString(),
@@ -31162,15 +31882,68 @@ var SqliteSecurityRepository = class {
31162
31882
  }));
31163
31883
  return Promise.resolve({ items });
31164
31884
  }
31885
+ /**
31886
+ * Per-rule tallies of the findings that are still OPEN, whole-store.
31887
+ *
31888
+ * Scoped by status rather than by time, because the card this feeds is a to-do
31889
+ * list: a secret committed three weeks ago and never rotated is still the most
31890
+ * important thing to fix, and any window hides it. It carried a "newest N
31891
+ * findings" cap and then a range; the first meant a different span on every
31892
+ * machine, and the second reported "no recommendations" over live exposure.
31893
+ *
31894
+ * `open` mirrors `deriveFindingStatus` — at-rest, minus resolved and dismissed —
31895
+ * so a row's count is exactly what `?status=open&type=<rule>` returns. Note that
31896
+ * is NOT `severitySummary`'s `openAtRest`, which keeps dismissed findings (a
31897
+ * dismissal is a judgement, not a remediation) and drops untracked legacy rows.
31898
+ * The two answer different questions and only this one has to match a link.
31899
+ *
31900
+ * Aggregated in SQL: the result is O(distinct rule × category × severity), so a
31901
+ * whole-store scope costs a grouped scan rather than a row per finding.
31902
+ */
31903
+ recommendationInputs() {
31904
+ const rows = allRows(
31905
+ this.db.prepare(
31906
+ `SELECT d.rule_id AS rule_id,
31907
+ d.category AS category,
31908
+ d.severity AS severity,
31909
+ COUNT(*) AS count
31910
+ FROM inspection_findings f
31911
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31912
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31913
+ LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31914
+ ON latest.finding_key = f.finding_key
31915
+ WHERE e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31916
+ AND e.event_type = 'code_change'
31917
+ AND (
31918
+ f.finding_key IS NULL
31919
+ OR latest.status IS NULL
31920
+ OR latest.status NOT IN ('resolved', 'dismissed')
31921
+ )
31922
+ GROUP BY d.rule_id, d.category, d.severity`
31923
+ )
31924
+ );
31925
+ return Promise.resolve(
31926
+ rows.map((r) => ({
31927
+ ruleId: r.rule_id,
31928
+ category: r.category,
31929
+ severity: r.severity,
31930
+ count: r.count
31931
+ }))
31932
+ );
31933
+ }
31165
31934
  // Findings whose parent event occurred in [fromMs, toMs), with the parent's
31166
31935
  // epoch-millis timestamp. started_at is an INTEGER column, so the bounds stay
31167
31936
  // numeric and the JS aggregations bucket/split on ms directly.
31168
31937
  findingsInRange(fromMs, toMs) {
31169
31938
  const rows = allRows(
31170
31939
  this.db.prepare(
31171
- `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken
31940
+ // `rule_id`/`category` cost nothing extra: inspection_definitions is already
31941
+ // joined for `severity`, so they are two more columns off a row this read
31942
+ // already fetches. They feed the recommended-actions rollup.
31943
+ `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31944
+ d.rule_id AS rule_id, d.category AS category
31172
31945
  FROM inspection_findings f
31173
- JOIN audit_events e ON e.id = f.audit_event_id
31946
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31174
31947
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31175
31948
  WHERE e.started_at >= :from AND e.started_at < :to
31176
31949
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -31181,7 +31954,9 @@ var SqliteSecurityRepository = class {
31181
31954
  return rows.map((r) => ({
31182
31955
  occurredAt: r.occurred_at,
31183
31956
  severity: r.severity,
31184
- actionTaken: r.action_taken
31957
+ actionTaken: r.action_taken,
31958
+ ruleId: r.rule_id,
31959
+ category: r.category
31185
31960
  }));
31186
31961
  }
31187
31962
  };
@@ -32009,6 +32784,7 @@ function openWithPragmas(file2) {
32009
32784
  db.exec("PRAGMA journal_mode = WAL");
32010
32785
  db.exec("PRAGMA busy_timeout = 2000");
32011
32786
  db.exec("PRAGMA foreign_keys = ON");
32787
+ registerSqlFunctions(db);
32012
32788
  } catch (err) {
32013
32789
  closeQuietly(db);
32014
32790
  throw err;
@@ -32038,7 +32814,7 @@ function backupLegacyStore(db, file2) {
32038
32814
  discardStore(file2, backup);
32039
32815
  return backup;
32040
32816
  }
32041
- function openAndInitialize(file2, base) {
32817
+ function openAndInitialize(file2, base, skipTags) {
32042
32818
  let db = openWithPragmas(file2);
32043
32819
  try {
32044
32820
  if (isForeignSqliteLineage(db)) {
@@ -32048,7 +32824,7 @@ function openAndInitialize(file2, base) {
32048
32824
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
32049
32825
  );
32050
32826
  }
32051
- applyMigrations(db, file2);
32827
+ applyMigrations(db, file2, { skipTags });
32052
32828
  tightenPerms(file2);
32053
32829
  const policies = new SqlitePoliciesRepository(db);
32054
32830
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -32063,6 +32839,7 @@ function openAndInitialize(file2, base) {
32063
32839
  exceptions: new SqliteExceptionsRepository(db),
32064
32840
  resolutions: new SqliteResolutionsRepository(db),
32065
32841
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32842
+ bodyRetention: new SqliteBodyRetentionRepository(db),
32066
32843
  security: new SqliteSecurityRepository(db),
32067
32844
  detections: new SqliteDetectionsRepository(db),
32068
32845
  shares: new SqliteSharesRepository(db),
@@ -32085,7 +32862,8 @@ function openAndInitialize(file2, base) {
32085
32862
  throw err;
32086
32863
  }
32087
32864
  }
32088
- function openLocalDatabase(dir) {
32865
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32866
+ function openLocalDatabase(dir, options = {}) {
32089
32867
  ensureDataDirSync(dir);
32090
32868
  const file2 = join7(dir, DB_FILENAME);
32091
32869
  reapStalePartials(file2);
@@ -32097,6 +32875,7 @@ function openLocalDatabase(dir) {
32097
32875
  installedPacks,
32098
32876
  scanLedger,
32099
32877
  historySync,
32878
+ bodyRetention,
32100
32879
  secretVault,
32101
32880
  exceptions,
32102
32881
  resolutions,
@@ -32120,7 +32899,8 @@ function openLocalDatabase(dir) {
32120
32899
  // `dir` is always `<base>/data` — every caller resolves it through
32121
32900
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32122
32901
  // settings/ and data/, and the pack-policy floor needs both halves.
32123
- dirname2(dir)
32902
+ dirname2(dir),
32903
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32124
32904
  );
32125
32905
  function captureRowId(event) {
32126
32906
  return captureId(
@@ -32313,6 +33093,7 @@ function openLocalDatabase(dir) {
32313
33093
  installedPacks,
32314
33094
  scanLedger,
32315
33095
  historySync,
33096
+ bodyRetention,
32316
33097
  secretVault,
32317
33098
  exceptions,
32318
33099
  resolutions,
@@ -32351,14 +33132,78 @@ function openLocalDatabase(dir) {
32351
33132
  };
32352
33133
  }
32353
33134
 
32354
- // ../../packages/persistence/src/finding-key.ts
33135
+ // ../../packages/persistence/src/egress-wire.ts
32355
33136
  import { createHash as createHash3 } from "crypto";
33137
+ var PROJECT_KEY_DIGEST_VERSION = "v2";
33138
+ var SCP_FORM = /^(?:[^@/]+@)?([^/:]+):(.+)$/;
33139
+ var DOS_DRIVE = /^[A-Za-z]:[\\/]/;
33140
+ var FILE_URL = /^file:\/\//i;
33141
+ var SCHEME_FORM = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?(\/.*)?$/i;
33142
+ var SLASH = "/".charCodeAt(0);
33143
+ var GIT_SUFFIX = ".git";
33144
+ function trimSlashes(path) {
33145
+ let start = 0;
33146
+ let end = path.length;
33147
+ while (start < end && path.charCodeAt(start) === SLASH) start += 1;
33148
+ while (end > start && path.charCodeAt(end - 1) === SLASH) end -= 1;
33149
+ return path.slice(start, end);
33150
+ }
33151
+ function canonicalGitUrl(url2) {
33152
+ const trimmed = url2.trim();
33153
+ if (DOS_DRIVE.test(trimmed) || FILE_URL.test(trimmed)) return trimmed;
33154
+ const scheme = SCHEME_FORM.exec(trimmed);
33155
+ const scp = scheme === null ? SCP_FORM.exec(trimmed) : null;
33156
+ const host = (scheme?.[1] ?? scp?.[1])?.toLowerCase();
33157
+ if (host === void 0) return trimmed;
33158
+ const path = (scheme === null ? scp?.[2] : scheme[2]) ?? "";
33159
+ const bare = trimSlashes(path);
33160
+ const cleaned = bare.endsWith(GIT_SUFFIX) ? bare.slice(0, -GIT_SUFFIX.length) : bare;
33161
+ return cleaned === "" ? host : `${host}/${cleaned}`;
33162
+ }
33163
+ function hashProjectKey(projectKey) {
33164
+ const canonical = projectKey.startsWith("git:") ? `git:${canonicalGitUrl(projectKey.slice("git:".length))}` : projectKey;
33165
+ return createHash3("sha256").update(`${PROJECT_KEY_DIGEST_VERSION}:${canonical}`, "utf8").digest("hex");
33166
+ }
33167
+ function toIngestHit(hit) {
33168
+ return {
33169
+ host: hit.host,
33170
+ kind: hit.kind,
33171
+ name: hit.name,
33172
+ category: hit.category,
33173
+ trust: hit.trust,
33174
+ network: hit.network,
33175
+ method: hit.method,
33176
+ transport: hit.transport,
33177
+ url: hit.url,
33178
+ template: hit.template,
33179
+ dataClass: hit.dataClass,
33180
+ site: {
33181
+ file: hit.site.file,
33182
+ line: hit.site.line,
33183
+ dynamic: hit.site.dynamic,
33184
+ vendored: hit.site.vendored
33185
+ }
33186
+ };
33187
+ }
33188
+ function toEgressIngestRequest(input2) {
33189
+ const { hits, droppedFiles } = capHits(input2.hits, input2.reconcile.mode);
33190
+ const reconcile = withoutDroppedFiles(input2.reconcile, droppedFiles);
33191
+ return {
33192
+ projectKey: hashProjectKey(input2.projectKey),
33193
+ project: input2.project,
33194
+ reconcile,
33195
+ hits: hits.map(toIngestHit)
33196
+ };
33197
+ }
33198
+
33199
+ // ../../packages/persistence/src/finding-key.ts
33200
+ import { createHash as createHash4 } from "crypto";
32356
33201
  function normalizeFilePath(filePath) {
32357
33202
  return filePath.replaceAll("\\", "/");
32358
33203
  }
32359
33204
  function computeFindingKey(input2) {
32360
33205
  const normalizedPath = normalizeFilePath(input2.filePath);
32361
- return createHash3("sha256").update(`${input2.ruleId}\0${normalizedPath}\0${input2.valueFingerprint}`).digest("hex");
33206
+ return createHash4("sha256").update(`${input2.ruleId}\0${normalizedPath}\0${input2.valueFingerprint}`).digest("hex");
32362
33207
  }
32363
33208
 
32364
33209
  // ../../packages/persistence/src/fingerprint.ts
@@ -32484,14 +33329,50 @@ function fingerprintValue(key, raw) {
32484
33329
  return createHmac("sha256", key.material).update(raw, "utf8").digest("hex");
32485
33330
  }
32486
33331
 
32487
- // ../../packages/persistence/src/history-preview.ts
32488
- import { existsSync as existsSync4 } from "fs";
33332
+ // ../../packages/persistence/src/forward-health.ts
33333
+ import { readFileSync as readFileSync7 } from "fs";
32489
33334
  import { join as join9 } from "path";
33335
+ var FAILURES = /* @__PURE__ */ new Set([
33336
+ "unauthorized",
33337
+ "forbidden",
33338
+ "unreachable"
33339
+ ]);
33340
+ var BREAKER_COOLDOWN_MS = 3e4;
33341
+ function parseForwardHealth(raw, nowMs) {
33342
+ try {
33343
+ const parsed2 = JSON.parse(raw);
33344
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33345
+ const record2 = parsed2;
33346
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33347
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33348
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33349
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33350
+ } catch {
33351
+ return null;
33352
+ }
33353
+ }
33354
+ function isForwardPaused(health, nowMs) {
33355
+ const openedAtMs = health?.openedAtMs ?? null;
33356
+ if (openedAtMs === null) return false;
33357
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33358
+ }
33359
+
33360
+ // ../../packages/persistence/src/history-backfill.ts
33361
+ import { existsSync as existsSync4 } from "fs";
33362
+ import { join as join10 } from "path";
33363
+
33364
+ // ../../packages/persistence/src/history-preview.ts
33365
+ import { existsSync as existsSync5 } from "fs";
33366
+ import { join as join11 } from "path";
32490
33367
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32491
33368
 
33369
+ // ../../packages/persistence/src/history-sync-state.ts
33370
+ import { readFileSync as readFileSync8 } from "fs";
33371
+ import { join as join12 } from "path";
33372
+
32492
33373
  // ../../packages/persistence/src/store-symlinks.ts
32493
- import { existsSync as existsSync5, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32494
- import { dirname as dirname3, join as join10, resolve } from "path";
33374
+ import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
33375
+ import { dirname as dirname3, join as join13, resolve } from "path";
32495
33376
 
32496
33377
  // ../../packages/persistence/src/vault/crypto.ts
32497
33378
  import {
@@ -32505,20 +33386,20 @@ import {
32505
33386
  // ../../packages/persistence/src/vault/key-provider.ts
32506
33387
  import { execFileSync } from "child_process";
32507
33388
  import { randomBytes as randomBytes2 } from "crypto";
32508
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32509
- import { join as join11 } from "path";
33389
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33390
+ import { join as join14 } from "path";
32510
33391
 
32511
33392
  // ../../packages/persistence/src/vault/vault.ts
32512
33393
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32513
33394
 
32514
33395
  // ../../packages/persistence/src/warn-era-cap.ts
32515
- import { existsSync as existsSync6, writeFileSync as writeFileSync4 } from "fs";
32516
- import { join as join12 } from "path";
33396
+ import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
33397
+ import { join as join15 } from "path";
32517
33398
  var MARKER = "warn-era-capped";
32518
33399
  function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32519
33400
  if (policyMode !== "warn") return { capped: 0, skipped: "not-warn" };
32520
- const marker = join12(dataDir2, MARKER);
32521
- if (existsSync6(marker)) return { capped: 0, skipped: "already-run" };
33401
+ const marker = join15(dataDir2, MARKER);
33402
+ if (existsSync7(marker)) return { capped: 0, skipped: "already-run" };
32522
33403
  const capped = db.policies.capCategoryActions();
32523
33404
  writeFileSync4(marker, `${new Date(Date.now()).toISOString()}
32524
33405
  `, { mode: DATA_FILE_MODE });
@@ -32526,8 +33407,8 @@ function capWarnEraEnforcementOnce(db, policyMode, dataDir2) {
32526
33407
  }
32527
33408
 
32528
33409
  // ../../packages/plugin-sdk/src/config.ts
32529
- import { existsSync as existsSync7 } from "fs";
32530
- import { join as join13 } from "path";
33410
+ import { existsSync as existsSync8 } from "fs";
33411
+ import { join as join16 } from "path";
32531
33412
 
32532
33413
  // ../../packages/plugin-sdk/src/provider-env.ts
32533
33414
  var DEFAULT_ANTHROPIC_HOST = "api.anthropic.com";
@@ -32581,8 +33462,8 @@ function resolveProvider() {
32581
33462
  function loadConfig(base = defaultDataDir(), resolveProviderFn = resolveProvider) {
32582
33463
  try {
32583
33464
  ensureLayoutDirSync(base);
32584
- const settingsFile = join13(settingsDir(base), "settings.json");
32585
- if (existsSync7(settingsFile)) tightenFile(settingsFile);
33465
+ const settingsFile = join16(settingsDir(base), "settings.json");
33466
+ if (existsSync8(settingsFile)) tightenFile(settingsFile);
32586
33467
  } catch {
32587
33468
  }
32588
33469
  migrateLegacyLayout(base);
@@ -32605,9 +33486,9 @@ function resolveProviderSafe(resolveProviderFn) {
32605
33486
  }
32606
33487
 
32607
33488
  // ../../packages/plugin-sdk/src/config-inventory.ts
32608
- import { readdirSync as readdirSync2, readFileSync as readFileSync9, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33489
+ import { readdirSync as readdirSync2, readFileSync as readFileSync11, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32609
33490
  import { homedir as homedir2 } from "os";
32610
- import { basename as basename3, join as join15 } from "path";
33491
+ import { basename as basename3, join as join18 } from "path";
32611
33492
 
32612
33493
  // ../../packages/detections/src/egress/registry.ts
32613
33494
  var EXTRACTOR_VERSION = "1";
@@ -35696,8 +36577,8 @@ function bundledDetections() {
35696
36577
  }
35697
36578
 
35698
36579
  // ../../packages/plugin-sdk/src/repo.ts
35699
- import { existsSync as existsSync8, readFileSync as readFileSync8, statSync as statSync6 } from "fs";
35700
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join14, sep as sep2 } from "path";
36580
+ import { existsSync as existsSync9, readFileSync as readFileSync10, statSync as statSync6 } from "fs";
36581
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join17, sep as sep2 } from "path";
35701
36582
  function resolveRepo(cwd) {
35702
36583
  try {
35703
36584
  const root = findGitRoot(cwd);
@@ -35719,36 +36600,36 @@ function resolveWorktreeRoot(cwd) {
35719
36600
  function findGitRoot(start) {
35720
36601
  let dir = start;
35721
36602
  for (; ; ) {
35722
- if (existsSync8(join14(dir, ".git"))) return dir;
36603
+ if (existsSync9(join17(dir, ".git"))) return dir;
35723
36604
  const parent = dirname4(dir);
35724
36605
  if (parent === dir) return void 0;
35725
36606
  dir = parent;
35726
36607
  }
35727
36608
  }
35728
36609
  function resolveGitContext(root) {
35729
- const dotGit = join14(root, ".git");
36610
+ const dotGit = join17(root, ".git");
35730
36611
  try {
35731
36612
  if (statSync6(dotGit).isDirectory()) {
35732
- return { configPath: join14(dotGit, "config"), headRoot: root };
36613
+ return { configPath: join17(dotGit, "config"), headRoot: root };
35733
36614
  }
35734
36615
  } catch {
35735
36616
  return void 0;
35736
36617
  }
35737
36618
  const target = /^gitdir:\s*(.+?)\s*$/m.exec(safeRead(dotGit) ?? "")?.[1];
35738
36619
  if (!target) return void 0;
35739
- const gitdir = isAbsolute(target) ? target : join14(root, target);
35740
- if (existsSync8(join14(gitdir, "config"))) {
35741
- return { configPath: join14(gitdir, "config"), headRoot: root };
36620
+ const gitdir = isAbsolute(target) ? target : join17(root, target);
36621
+ if (existsSync9(join17(gitdir, "config"))) {
36622
+ return { configPath: join17(gitdir, "config"), headRoot: root };
35742
36623
  }
35743
- const commonRaw = safeRead(join14(gitdir, "commondir"))?.trim();
36624
+ const commonRaw = safeRead(join17(gitdir, "commondir"))?.trim();
35744
36625
  if (!commonRaw) return void 0;
35745
- const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join14(gitdir, commonRaw);
36626
+ const commonGitDir = isAbsolute(commonRaw) ? commonRaw : join17(gitdir, commonRaw);
35746
36627
  const headRoot = basename2(commonGitDir) === ".git" ? dirname4(commonGitDir) : root;
35747
- return { configPath: join14(commonGitDir, "config"), headRoot };
36628
+ return { configPath: join17(commonGitDir, "config"), headRoot };
35748
36629
  }
35749
36630
  function safeRead(path) {
35750
36631
  try {
35751
- return readFileSync8(path, "utf8");
36632
+ return readFileSync10(path, "utf8");
35752
36633
  } catch {
35753
36634
  return void 0;
35754
36635
  }
@@ -35786,9 +36667,9 @@ function slugFromUrl(url2) {
35786
36667
  }
35787
36668
 
35788
36669
  // ../../packages/plugin-sdk/src/events.ts
35789
- import { createHash as createHash4, randomUUID as randomUUID13 } from "crypto";
36670
+ import { createHash as createHash5, randomUUID as randomUUID13 } from "crypto";
35790
36671
  function contentHashOf(text) {
35791
- return createHash4("sha256").update(text).digest("hex");
36672
+ return createHash5("sha256").update(text).digest("hex");
35792
36673
  }
35793
36674
  function buildIngestEvent(input2) {
35794
36675
  const contentHash = input2.contentHash ?? contentHashOf(input2.content);
@@ -35814,7 +36695,7 @@ function buildIngestEvent(input2) {
35814
36695
  }
35815
36696
 
35816
36697
  // ../../packages/plugin-sdk/src/isolated-scan.ts
35817
- import { existsSync as existsSync9 } from "fs";
36698
+ import { existsSync as existsSync10 } from "fs";
35818
36699
  import { fileURLToPath } from "url";
35819
36700
  import { Worker } from "worker_threads";
35820
36701
  var ISOLATED_SCAN_BUDGET_MS = 2e3;
@@ -35828,7 +36709,7 @@ function resolveWorkerUrl() {
35828
36709
  for (const name of ["scan-worker.js", "scan-worker.ts"]) {
35829
36710
  const candidate = new URL(name, import.meta.url);
35830
36711
  try {
35831
- if (existsSync9(fileURLToPath(candidate))) {
36712
+ if (existsSync10(fileURLToPath(candidate))) {
35832
36713
  resolvedWorkerUrl = candidate;
35833
36714
  return candidate;
35834
36715
  }
@@ -36291,13 +37172,9 @@ function createGuardedScanner(partition, gateway, opts) {
36291
37172
  };
36292
37173
  }
36293
37174
 
36294
- // ../../packages/plugin-sdk/src/ignore-layers.ts
36295
- var import_ignore = __toESM(require_ignore(), 1);
36296
- import { readFileSync as readFileSync10 } from "fs";
36297
- import { join as join16 } from "path";
36298
-
36299
- // ../../packages/plugin-sdk/src/inventory-resolver.ts
36300
- import { arch, hostname as hostname4, platform, release } from "os";
37175
+ // ../../packages/plugin-sdk/src/host-floor.ts
37176
+ import { readFileSync as readFileSync13 } from "fs";
37177
+ import { join as join20 } from "path";
36301
37178
 
36302
37179
  // ../../packages/plugin-sdk/src/model-governance.ts
36303
37180
  import {
@@ -36305,16 +37182,42 @@ import {
36305
37182
  fstatSync,
36306
37183
  mkdirSync as mkdirSync2,
36307
37184
  openSync as openSync2,
36308
- readFileSync as readFileSync11,
37185
+ readFileSync as readFileSync12,
36309
37186
  readSync,
36310
37187
  writeFileSync as writeFileSync5
36311
37188
  } from "fs";
36312
- import { join as join17 } from "path";
37189
+ import { join as join19 } from "path";
36313
37190
  var TAIL_BYTES = 256 * 1024;
36314
37191
 
37192
+ // ../../packages/plugin-sdk/src/host-floor.ts
37193
+ var HOST_FEATURE = {
37194
+ ModelSwitch: "model-switch",
37195
+ VaultPointerDisplay: "vault-pointer-display"
37196
+ };
37197
+ var HOST_FLOORS = {
37198
+ [HOST_FEATURE.ModelSwitch]: {
37199
+ label: "model-switch protection",
37200
+ hookEvents: ["PreModelSwitch", "PostModelSwitch"],
37201
+ since: "2.1.251"
37202
+ },
37203
+ [HOST_FEATURE.VaultPointerDisplay]: {
37204
+ label: "vault pointer display",
37205
+ hookEvents: ["MessageDisplay"],
37206
+ since: "2.1.152"
37207
+ }
37208
+ };
37209
+
37210
+ // ../../packages/plugin-sdk/src/ignore-layers.ts
37211
+ var import_ignore = __toESM(require_ignore(), 1);
37212
+ import { readFileSync as readFileSync14 } from "fs";
37213
+ import { join as join21 } from "path";
37214
+
37215
+ // ../../packages/plugin-sdk/src/inventory-resolver.ts
37216
+ import { arch, hostname as hostname4, platform, release } from "os";
37217
+
36315
37218
  // ../../packages/plugin-sdk/src/nudge.ts
36316
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync12, writeFileSync as writeFileSync6 } from "fs";
36317
- import { join as join18 } from "path";
37219
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync15, writeFileSync as writeFileSync6 } from "fs";
37220
+ import { join as join22 } from "path";
36318
37221
 
36319
37222
  // ../../packages/plugin-sdk/src/paths.ts
36320
37223
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
@@ -36366,8 +37269,8 @@ function applyCategoryPosture(posture, repo, mode = "fill-gaps") {
36366
37269
  }
36367
37270
 
36368
37271
  // ../../packages/plugin-sdk/src/project-files.ts
36369
- import { existsSync as existsSync10, readdirSync as readdirSync4 } from "fs";
36370
- import { basename as basename5, join as join19 } from "path";
37272
+ import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
37273
+ import { basename as basename5, join as join23 } from "path";
36371
37274
 
36372
37275
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
36373
37276
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -36408,6 +37311,14 @@ function safeMaskedMatch(rawMatch) {
36408
37311
  // ../../packages/plugin-sdk/src/runtime.ts
36409
37312
  import { randomUUID as randomUUID14 } from "crypto";
36410
37313
  var ENFORCEMENT_CEILING_ENABLED = false;
37314
+ function applyEnforcementCeiling(action, policyMode, enabled) {
37315
+ if (!enabled || policyMode !== "warn") return action;
37316
+ return action === "block" || action === "redact" ? "warn" : action;
37317
+ }
37318
+ function resolveEnforcedAction(action, opts) {
37319
+ const degraded = !opts.rewritable && action === "redact" ? builtinPolicyToAction(opts.redactFallback) : action;
37320
+ return applyEnforcementCeiling(degraded, opts.policyMode, opts.ceilingEnabled);
37321
+ }
36411
37322
  function startTiming() {
36412
37323
  try {
36413
37324
  return performance.now();
@@ -36444,7 +37355,7 @@ function createPluginRuntime(gateway, settings, opts) {
36444
37355
  bundlesPacked = true;
36445
37356
  }
36446
37357
  const policyMode = settings.policy;
36447
- const redactFallback = settings.redactFallback;
37358
+ let redactFallback = settings.redactFallback;
36448
37359
  const dataDir2 = opts?.dataDir;
36449
37360
  let rules = [];
36450
37361
  let scanner;
@@ -36488,6 +37399,7 @@ function createPluginRuntime(gateway, settings, opts) {
36488
37399
  rules = [...verified, ...unverified];
36489
37400
  scanner = createGuardedScanner({ verified, unverified }, gateway, opts?.scanIsolation);
36490
37401
  bundleExceptions = bundle.exceptions ?? [];
37402
+ redactFallback = strongerRedactFallback(settings.redactFallback, bundle.redactFallback);
36491
37403
  initialized = true;
36492
37404
  }
36493
37405
  let cachedKey;
@@ -36516,21 +37428,23 @@ function createPluginRuntime(gateway, settings, opts) {
36516
37428
  }
36517
37429
  function actionForFinding(finding, excepted, rewritable = true) {
36518
37430
  if (excepted?.has(finding)) return "allow";
36519
- const action = resolveAction(finding.ruleId, finding.category);
36520
- if (!rewritable && action === "redact") return builtinPolicyToAction(redactFallback);
36521
- if (ENFORCEMENT_CEILING_ENABLED && policyMode === "warn" && (action === "block" || action === "redact")) {
36522
- return "warn";
36523
- }
36524
- return action;
37431
+ return resolveEnforcedAction(resolveAction(finding.ruleId, finding.category), {
37432
+ policyMode,
37433
+ redactFallback,
37434
+ rewritable,
37435
+ ceilingEnabled: ENFORCEMENT_CEILING_ENABLED
37436
+ });
36525
37437
  }
36526
37438
  function decide(findings, text, excepted, rewritable = true) {
36527
37439
  if (findings.length === 0) return { action: "log", text, findings: [] };
36528
37440
  const actionFor = (finding) => actionForFinding(finding, excepted, rewritable);
37441
+ const degradedActions = rewritable ? [] : findings.filter((f) => actionForFinding(f, excepted, true) === "redact").map(actionFor);
37442
+ const degraded = degradedActions.length === 0 ? {} : { redactDegradedTo: degradedActions.reduce((a, b) => strongerAction(a, b)) };
36529
37443
  let worst = "log";
36530
37444
  for (const finding of findings) {
36531
37445
  worst = strongerAction(worst, actionFor(finding));
36532
37446
  }
36533
- if (worst === "block") return { action: "block", text: null, findings };
37447
+ if (worst === "block") return { action: "block", text: null, findings, ...degraded };
36534
37448
  if (worst === "redact") {
36535
37449
  const redactFindings = findings.filter((f) => actionFor(f) === "redact");
36536
37450
  const reversibleFindings = redactFindings.filter((f) => resolver.isReversible(f.ruleId));
@@ -36540,9 +37454,13 @@ function createPluginRuntime(gateway, settings, opts) {
36540
37454
  findings,
36541
37455
  enforcedFindings: redactFindings,
36542
37456
  reversibleFindings
37457
+ // No `degraded` here, and it is not an omission: `rewritable` is per
37458
+ // CAPTURE, so on an unrewritable field every redact has already become
37459
+ // the fallback and this branch is unreachable. Spreading it would read
37460
+ // as a case that can happen.
36543
37461
  };
36544
37462
  }
36545
- return { action: worst, text, findings };
37463
+ return { action: worst, text, findings, ...degraded };
36546
37464
  }
36547
37465
  function fingerprintOf(key, finding, cache) {
36548
37466
  let fp = cache.get(finding);
@@ -36671,8 +37589,8 @@ function createPluginRuntime(gateway, settings, opts) {
36671
37589
  };
36672
37590
  }
36673
37591
  }
36674
- async function processText(text, context) {
36675
- return (await evaluate(text, context, {})).decision;
37592
+ async function processText(text, context, opts2 = {}) {
37593
+ return (await evaluate(text, context, {}, opts2.rewritable)).decision;
36676
37594
  }
36677
37595
  async function capture(input2, opts2 = {}) {
36678
37596
  const timingStartedAt = input2.occurredAt === void 0 ? startTiming() : void 0;
@@ -36695,10 +37613,12 @@ function createPluginRuntime(gateway, settings, opts) {
36695
37613
  );
36696
37614
  const storedContent = maskedFindings.length > 0 ? redact(input2.text, maskedFindings) : input2.text;
36697
37615
  const inspectionMs = elapsedMs(timingStartedAt);
36698
- const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 ? {
37616
+ const redactDegradedTo = decision.redactDegradedTo;
37617
+ const metadata = exceptionIds.length > 0 || inspectionMs !== void 0 || redactDegradedTo !== void 0 ? {
36699
37618
  ...input2.metadata,
36700
37619
  ...exceptionIds.length > 0 ? { exceptionIds } : {},
36701
- ...inspectionMs !== void 0 ? { inspectionMs } : {}
37620
+ ...inspectionMs !== void 0 ? { inspectionMs } : {},
37621
+ ...redactDegradedTo !== void 0 ? { redactDegradedTo } : {}
36702
37622
  } : input2.metadata;
36703
37623
  const event = buildIngestEvent({
36704
37624
  kind: input2.kind,
@@ -36770,7 +37690,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
36770
37690
 
36771
37691
  // ../../packages/plugin-sdk/src/throttle.ts
36772
37692
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
36773
- import { join as join20 } from "path";
37693
+ import { join as join24 } from "path";
36774
37694
 
36775
37695
  // ../../packages/setup-wizard/src/remediation/chain.ts
36776
37696
  var REMEDIATION_OPTIONS = [
@@ -36813,7 +37733,7 @@ function routeRemediationOption(option, handlers) {
36813
37733
 
36814
37734
  // ../../packages/setup-wizard/src/remediation/rotation-checklist.ts
36815
37735
  import { writeFileSync as writeFileSync8 } from "fs";
36816
- import { join as join21 } from "path";
37736
+ import { join as join25 } from "path";
36817
37737
  var GENERIC_CONSOLE_PATH = "rotate via the provider's own console";
36818
37738
  var CONSOLE_PATHS = {
36819
37739
  anthropic: "console.anthropic.com \u2192 Settings \u2192 API keys",
@@ -36913,7 +37833,7 @@ function generateRotationChecklist(input2) {
36913
37833
  try {
36914
37834
  const target = resolveRotationChecklistTarget(input2.cwd);
36915
37835
  targetDirectory = target.directory;
36916
- const filePath = join21(target.directory, "rotation-checklist.md");
37836
+ const filePath = join25(target.directory, "rotation-checklist.md");
36917
37837
  writeRotationChecklist(input2.entries, target.directory);
36918
37838
  return {
36919
37839
  status: "written",
@@ -37024,9 +37944,9 @@ var RANK = Object.fromEntries(
37024
37944
  );
37025
37945
 
37026
37946
  // ../../packages/setup-wizard/src/triage/plan-file.ts
37027
- import { mkdtempSync, readFileSync as readFileSync13, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37947
+ import { mkdtempSync, readFileSync as readFileSync16, rmdirSync, rmSync as rmSync7, writeFileSync as writeFileSync9 } from "fs";
37028
37948
  import { tmpdir } from "os";
37029
- import { basename as basename6, dirname as dirname6, join as join22 } from "path";
37949
+ import { basename as basename6, dirname as dirname6, join as join26 } from "path";
37030
37950
  var SuppressionEntrySchema = external_exports.object({
37031
37951
  ruleId: external_exports.string(),
37032
37952
  category: DetectionCategory,
@@ -37170,8 +38090,8 @@ function show(body) {
37170
38090
  }
37171
38091
 
37172
38092
  // src/skills-registry.ts
37173
- import { readdirSync as readdirSync5, readFileSync as readFileSync14 } from "fs";
37174
- import { join as join23 } from "path";
38093
+ import { readdirSync as readdirSync5, readFileSync as readFileSync17 } from "fs";
38094
+ import { join as join27 } from "path";
37175
38095
  import { fileURLToPath as fileURLToPath2 } from "url";
37176
38096
  var SKILLS_DIR = fileURLToPath2(new URL("../skills", import.meta.url));
37177
38097
  function frontmatterName(content) {
@@ -37184,7 +38104,7 @@ function readRegisteredSkills() {
37184
38104
  return readdirSync5(SKILLS_DIR, { withFileTypes: true }).filter((entry) => entry.isDirectory()).flatMap((entry) => {
37185
38105
  let content;
37186
38106
  try {
37187
- content = readFileSync14(join23(SKILLS_DIR, entry.name, "SKILL.md"), "utf8");
38107
+ content = readFileSync17(join27(SKILLS_DIR, entry.name, "SKILL.md"), "utf8");
37188
38108
  } catch {
37189
38109
  return [];
37190
38110
  }
@@ -37245,44 +38165,7 @@ function renderRemediationDecision(findings, moreCount, registry2) {
37245
38165
  }
37246
38166
 
37247
38167
  // src/remediation/surfaced-redact.ts
37248
- import { readFileSync as readFileSync22 } from "fs";
37249
-
37250
- // ../../packages/plugin-runtime/src/attached/egress-wire.ts
37251
- import { createHash as createHash5 } from "crypto";
37252
- function hashProjectKey(projectKey) {
37253
- return createHash5("sha256").update(projectKey, "utf8").digest("hex");
37254
- }
37255
- function toIngestHit(hit) {
37256
- return {
37257
- host: hit.host,
37258
- kind: hit.kind,
37259
- name: hit.name,
37260
- category: hit.category,
37261
- trust: hit.trust,
37262
- network: hit.network,
37263
- method: hit.method,
37264
- transport: hit.transport,
37265
- url: hit.url,
37266
- template: hit.template,
37267
- dataClass: hit.dataClass,
37268
- site: {
37269
- file: hit.site.file,
37270
- line: hit.site.line,
37271
- dynamic: hit.site.dynamic,
37272
- vendored: hit.site.vendored
37273
- }
37274
- };
37275
- }
37276
- function toEgressIngestRequest(input2) {
37277
- const { hits, droppedFiles } = capHits(input2.hits, input2.reconcile.mode);
37278
- const reconcile = withoutDroppedFiles(input2.reconcile, droppedFiles);
37279
- return {
37280
- projectKey: hashProjectKey(input2.projectKey),
37281
- project: input2.project,
37282
- reconcile,
37283
- hits: hits.map(toIngestHit)
37284
- };
37285
- }
38168
+ import { readFileSync as readFileSync23 } from "fs";
37286
38169
 
37287
38170
  // ../../packages/remote/src/http.ts
37288
38171
  import { request as httpRequest } from "http";
@@ -37467,10 +38350,10 @@ function parsed(schema, body, route2) {
37467
38350
  }
37468
38351
  function withoutTrailingSlashes(endpoint) {
37469
38352
  let end = endpoint.length;
37470
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
38353
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
37471
38354
  return endpoint.slice(0, end);
37472
38355
  }
37473
- var SLASH = "/".charCodeAt(0);
38356
+ var SLASH2 = "/".charCodeAt(0);
37474
38357
  function createRemoteClient(options) {
37475
38358
  const base = withoutTrailingSlashes(options.endpoint);
37476
38359
  const url2 = (route2) => `${base}${route2}`;
@@ -37563,6 +38446,7 @@ function createRemoteClient(options) {
37563
38446
  url: url2(ROUTES.shares),
37564
38447
  body: JSON.stringify(validated.data)
37565
38448
  });
38449
+ if (response.status === 404) throw new RemoteRouteAbsent(ROUTES.shares);
37566
38450
  okBody(response);
37567
38451
  },
37568
38452
  async pollCommand() {
@@ -37585,19 +38469,51 @@ function createRemoteClient(options) {
37585
38469
  };
37586
38470
  }
37587
38471
 
37588
- // ../../packages/plugin-runtime/src/attached/failure.ts
38472
+ // ../../packages/remote/src/failure-kind.ts
37589
38473
  function statusOf(err) {
37590
38474
  if (typeof err !== "object" || err === null || !("status" in err)) return null;
37591
38475
  const { status } = err;
37592
38476
  if (typeof status !== "number" || !Number.isInteger(status)) return null;
37593
38477
  return status >= 100 && status <= 599 ? status : null;
37594
38478
  }
37595
- function classifyFailure(err) {
37596
- switch (statusOf(err)) {
38479
+ function nameOf(err) {
38480
+ if (typeof err !== "object" || err === null || !("name" in err)) return null;
38481
+ return typeof err.name === "string" ? err.name : null;
38482
+ }
38483
+ function classifyRemoteFailure(err) {
38484
+ switch (nameOf(err)) {
38485
+ case "RemoteRouteAbsent":
38486
+ return "route-absent";
38487
+ case "RemoteRequestInvalid":
38488
+ return "invalid-request";
38489
+ case "RemoteResponseInvalid":
38490
+ return "rejected";
38491
+ default:
38492
+ break;
38493
+ }
38494
+ const status = statusOf(err);
38495
+ if (status === null) return "unreachable";
38496
+ switch (status) {
37597
38497
  case 401:
37598
38498
  return "unauthorized";
37599
38499
  case 403:
37600
38500
  return "forbidden";
38501
+ case 429:
38502
+ return "unreachable";
38503
+ case 404:
38504
+ return "unreachable";
38505
+ default:
38506
+ return status >= 400 && status <= 499 ? "rejected" : "unreachable";
38507
+ }
38508
+ }
38509
+
38510
+ // ../../packages/plugin-runtime/src/attached/failure.ts
38511
+ function classifyFailure(err) {
38512
+ switch (classifyRemoteFailure(err)) {
38513
+ case "unauthorized":
38514
+ return "unauthorized";
38515
+ case "forbidden":
38516
+ return "forbidden";
37601
38517
  default:
37602
38518
  return "unreachable";
37603
38519
  }
@@ -37619,11 +38535,11 @@ function withTimeout(promise2, ms) {
37619
38535
  }
37620
38536
 
37621
38537
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
37622
- import { readFileSync as readFileSync15 } from "fs";
37623
- import { join as join24 } from "path";
38538
+ import { readFileSync as readFileSync18 } from "fs";
38539
+ import { join as join28 } from "path";
37624
38540
  var FORWARD_DROPS_FILENAME = ATTACHED_FORWARD_DROPS_FILENAME;
37625
38541
  function forwardDropsPath(dataDir2) {
37626
- return join24(dataDir2, FORWARD_DROPS_FILENAME);
38542
+ return join28(dataDir2, FORWARD_DROPS_FILENAME);
37627
38543
  }
37628
38544
  function recordForwardDrops(dataDir2, count, nowMs) {
37629
38545
  if (count <= 0) return;
@@ -37641,7 +38557,7 @@ function recordForwardDrops(dataDir2, count, nowMs) {
37641
38557
  }
37642
38558
  function readForwardDrops(dataDir2) {
37643
38559
  try {
37644
- const parsed2 = JSON.parse(readFileSync15(forwardDropsPath(dataDir2), "utf8"));
38560
+ const parsed2 = JSON.parse(readFileSync18(forwardDropsPath(dataDir2), "utf8"));
37645
38561
  if (typeof parsed2 !== "object" || parsed2 === null) return null;
37646
38562
  const record2 = parsed2;
37647
38563
  if (typeof record2.droppedForwards !== "number" || !Number.isFinite(record2.droppedForwards)) {
@@ -37659,9 +38575,8 @@ function readForwardDrops(dataDir2) {
37659
38575
 
37660
38576
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
37661
38577
  import { randomUUID as randomUUID15 } from "crypto";
37662
- import { readFileSync as readFileSync16 } from "fs";
37663
38578
  import { readFile, rename, writeFile } from "fs/promises";
37664
- import { join as join25 } from "path";
38579
+ import { join as join29 } from "path";
37665
38580
  function isInvalidRequest(err) {
37666
38581
  return typeof err === "object" && err !== null && err.name === "RemoteRequestInvalid";
37667
38582
  }
@@ -37675,31 +38590,12 @@ function isServerRejection(err) {
37675
38590
  var FORWARD_BUDGET_MS = 1500;
37676
38591
  var DECISION_PATH_BUDGET_MS = 800;
37677
38592
  var BREAKER_FAILURE_THRESHOLD = 3;
37678
- var BREAKER_COOLDOWN_MS = 3e4;
37679
38593
  var CLOSED = { consecutiveFailures: 0, openedAtMs: null, lastFailure: null };
37680
- var FAILURES = /* @__PURE__ */ new Set([
37681
- "unauthorized",
37682
- "forbidden",
37683
- "unreachable"
37684
- ]);
37685
38594
  var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
37686
38595
  var STATE_FILENAME = FORWARD_STATE_FILENAME;
37687
- function parseBreakerState(raw, nowMs) {
37688
- try {
37689
- const parsed2 = JSON.parse(raw);
37690
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
37691
- const record2 = parsed2;
37692
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
37693
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
37694
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
37695
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
37696
- } catch {
37697
- return null;
37698
- }
37699
- }
37700
38596
  function createForwardPolicy(deps) {
37701
38597
  const now = deps.now ?? (() => Date.now());
37702
- const file2 = join25(deps.dir, STATE_FILENAME);
38598
+ const file2 = join29(deps.dir, STATE_FILENAME);
37703
38599
  let state = null;
37704
38600
  let loading = null;
37705
38601
  async function readState() {
@@ -37709,7 +38605,7 @@ function createForwardPolicy(deps) {
37709
38605
  } catch {
37710
38606
  return { ...CLOSED };
37711
38607
  }
37712
- return parseBreakerState(raw, now()) ?? { ...CLOSED };
38608
+ return parseForwardHealth(raw, now()) ?? { ...CLOSED };
37713
38609
  }
37714
38610
  async function load() {
37715
38611
  if (state !== null) return state;
@@ -37755,7 +38651,7 @@ function createForwardPolicy(deps) {
37755
38651
  };
37756
38652
  const at = now();
37757
38653
  if (current.openedAtMs !== null) {
37758
- if (at - current.openedAtMs < BREAKER_COOLDOWN_MS) {
38654
+ if (isForwardPaused(current, at)) {
37759
38655
  return { ok: false, reason: "breaker-open" };
37760
38656
  }
37761
38657
  await persist({
@@ -38292,7 +39188,18 @@ var AttachedDataGateway = class {
38292
39188
  // and the spread above would otherwise drop the field silently — which is
38293
39189
  // exactly what it did, leaving the whole control inert on every device
38294
39190
  // while every test around it stayed green.
38295
- prohibitedModels: cached2.prohibitedModels
39191
+ prohibitedModels: cached2.prohibitedModels,
39192
+ // NAMED for the same reason as the line above, and it is the same defect
39193
+ // if it is not: `...local` above spreads the DEVICE's bundle, so a field
39194
+ // only the cache carries is dropped in silence. That is what left
39195
+ // `prohibitedModels` inert on every attached device with every test
39196
+ // around it green.
39197
+ //
39198
+ // Taken from the cache rather than merged here, because merging it needs
39199
+ // the device's own SETTING — which is not a bundle field and is not in
39200
+ // scope at this seam. The runtime does that merge, raise-only, where both
39201
+ // values are in hand (createPluginRuntime's ensureInitialized).
39202
+ redactFallback: cached2.redactFallback
38296
39203
  // ALSO HONOURED FROM THE CACHE, and not a bundle field at all: each
38297
39204
  // merged policy's own `provenance`. `mergeRaiseOnly` spreads the policies
38298
39205
  // it emits, so an 'authored' policy arriving from the control plane
@@ -38420,10 +39327,6 @@ function toolAuditEvent(input2) {
38420
39327
  };
38421
39328
  }
38422
39329
 
38423
- // ../../packages/plugin-runtime/src/attached/history-state.ts
38424
- import { readFileSync as readFileSync17 } from "fs";
38425
- import { join as join26 } from "path";
38426
-
38427
39330
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
38428
39331
  import { createHash as createHash6 } from "crypto";
38429
39332
  import { hostname as hostname5 } from "os";
@@ -38432,6 +39335,10 @@ import { hostname as hostname5 } from "os";
38432
39335
  var CORRELATION_ID = EventMetadata.shape.correlationId;
38433
39336
  var TRACE_ID = EventMetadata.shape.traceId;
38434
39337
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
39338
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
39339
+
39340
+ // ../../packages/plugin-runtime/src/attached/history-sync.ts
39341
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
38435
39342
 
38436
39343
  // ../../packages/plugin-runtime/src/attached/history-sync-trigger.ts
38437
39344
  import { spawn } from "child_process";
@@ -38439,7 +39346,7 @@ import { fileURLToPath as fileURLToPath3 } from "url";
38439
39346
  var HISTORY_SYNC_THROTTLE_MS = 5 * 60 * 1e3;
38440
39347
 
38441
39348
  // ../../packages/plugin-runtime/src/attached/plugin-block.ts
38442
- import { readFileSync as readFileSync18 } from "fs";
39349
+ import { readFileSync as readFileSync19 } from "fs";
38443
39350
  function createPluginBlock(build, policyStore) {
38444
39351
  return async () => {
38445
39352
  const cached2 = await policyStore.read();
@@ -38458,7 +39365,7 @@ function createPluginBlock(build, policyStore) {
38458
39365
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38459
39366
  import { randomUUID as randomUUID16 } from "crypto";
38460
39367
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
38461
- import { join as join27 } from "path";
39368
+ import { join as join30 } from "path";
38462
39369
 
38463
39370
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
38464
39371
  import { rename as rename2 } from "fs/promises";
@@ -38482,7 +39389,7 @@ async function publishByRename(tmp, file2, move = rename2) {
38482
39389
 
38483
39390
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
38484
39391
  function createPolicyStore(dir = dataDir()) {
38485
- const file2 = join27(dir, "policy-cache.json");
39392
+ const file2 = join30(dir, "policy-cache.json");
38486
39393
  async function read() {
38487
39394
  try {
38488
39395
  const raw = await readFile2(file2, "utf8");
@@ -38713,11 +39620,11 @@ function readStorePosture(dbPath2) {
38713
39620
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
38714
39621
  import { randomUUID as randomUUID17 } from "crypto";
38715
39622
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
38716
- import { join as join28 } from "path";
39623
+ import { join as join31 } from "path";
38717
39624
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
38718
39625
  function createPostureStore(dir = settingsDir(), legacyDir) {
38719
- const file2 = join28(dir, "posture-state.json");
38720
- const legacyFile = legacyDir === void 0 ? null : join28(legacyDir, "posture-state.json");
39626
+ const file2 = join31(dir, "posture-state.json");
39627
+ const legacyFile = legacyDir === void 0 ? null : join31(legacyDir, "posture-state.json");
38721
39628
  async function persist(state) {
38722
39629
  await ensureDataDir(dir);
38723
39630
  const tmp = `${file2}.${randomUUID17()}.tmp`;
@@ -38785,8 +39692,8 @@ function createPostureStore(dir = settingsDir(), legacyDir) {
38785
39692
  }
38786
39693
 
38787
39694
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
38788
- import { readFileSync as readFileSync19 } from "fs";
38789
- import { join as join29 } from "path";
39695
+ import { readFileSync as readFileSync20 } from "fs";
39696
+ import { join as join32 } from "path";
38790
39697
 
38791
39698
  // ../../packages/plugin-runtime/src/attached/status.ts
38792
39699
  var REFUSAL_LINES = {
@@ -38807,6 +39714,14 @@ import { spawn as spawn2 } from "child_process";
38807
39714
  import { fileURLToPath as fileURLToPath4 } from "url";
38808
39715
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
38809
39716
 
39717
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
39718
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
39719
+
39720
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
39721
+ import { spawn as spawn3 } from "child_process";
39722
+ import { fileURLToPath as fileURLToPath5 } from "url";
39723
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
39724
+
38810
39725
  // ../../packages/plugin-runtime/src/attached/factory.ts
38811
39726
  import { hostname as hostname6 } from "os";
38812
39727
 
@@ -39257,17 +40172,26 @@ import { randomUUID as randomUUID19 } from "crypto";
39257
40172
  var EXCEPTION_RETENTION_MS = 90 * 24 * 60 * 60 * 1e3;
39258
40173
 
39259
40174
  // src/history/transcripts.ts
39260
- import { closeSync as closeSync3, openSync as openSync3, readdirSync as readdirSync6, readFileSync as readFileSync20, readSync as readSync2, statSync as statSync10 } from "fs";
40175
+ import { closeSync as closeSync3, openSync as openSync3, readdirSync as readdirSync6, readFileSync as readFileSync21, readSync as readSync2, statSync as statSync10 } from "fs";
39261
40176
  import { homedir as homedir3 } from "os";
39262
- import { join as join30 } from "path";
40177
+ import { join as join33 } from "path";
39263
40178
  function transcriptsDir(home) {
39264
- return join30(home ?? homedir3(), ".codex", "sessions");
40179
+ return join33(home ?? homedir3(), ".codex", "sessions");
39265
40180
  }
39266
40181
  var DAY_MS5 = 24 * 60 * 60 * 1e3;
39267
40182
 
39268
40183
  // src/remediation/redact.ts
39269
- import { readFileSync as readFileSync21, realpathSync as realpathSync4, renameSync as renameSync5, rmSync as rmSync8, writeFileSync as writeFileSync10 } from "fs";
39270
- import { isAbsolute as isAbsolute2, relative, resolve as resolve2 } from "path";
40184
+ import {
40185
+ lstatSync as lstatSync4,
40186
+ readdirSync as readdirSync7,
40187
+ readFileSync as readFileSync22,
40188
+ realpathSync as realpathSync4,
40189
+ renameSync as renameSync5,
40190
+ rmSync as rmSync8,
40191
+ statSync as statSync11,
40192
+ writeFileSync as writeFileSync10
40193
+ } from "fs";
40194
+ import { basename as basename7, dirname as dirname7, isAbsolute as isAbsolute2, join as join34, relative, resolve as resolve2 } from "path";
39271
40195
  var REDACTED_PLACEHOLDER = "[REDACTED:SECRET]";
39272
40196
  function platformRedactionScope(home) {
39273
40197
  return { artifactRoots: [transcriptsDir(home)] };
@@ -39290,6 +40214,41 @@ function resolveRedactableArtifact(filePath, scope) {
39290
40214
  if (realTarget === null) return null;
39291
40215
  return scope.artifactRoots.some((root) => isWithinRoot(realTarget, root)) ? realTarget : null;
39292
40216
  }
40217
+ var TMP_SUFFIX = ".aka-redact.tmp";
40218
+ function tempPathFor(realPath) {
40219
+ return `${realPath}.${String(process.pid)}${TMP_SUFFIX}`;
40220
+ }
40221
+ function isProcessAlive(pid) {
40222
+ try {
40223
+ process.kill(pid, 0);
40224
+ return true;
40225
+ } catch (error61) {
40226
+ return error61.code !== "ESRCH";
40227
+ }
40228
+ }
40229
+ function sweepStrandedTemps(realPath) {
40230
+ const dir = dirname7(realPath);
40231
+ const prefix = `${basename7(realPath)}.`;
40232
+ let names;
40233
+ try {
40234
+ names = readdirSync7(dir);
40235
+ } catch {
40236
+ return;
40237
+ }
40238
+ for (const name of names) {
40239
+ if (!name.startsWith(prefix) || !name.endsWith(TMP_SUFFIX)) continue;
40240
+ const pidPart = name.slice(prefix.length, name.length - TMP_SUFFIX.length);
40241
+ if (!/^\d+$/.test(pidPart)) continue;
40242
+ const pid = Number(pidPart);
40243
+ if (pid !== process.pid && isProcessAlive(pid)) continue;
40244
+ try {
40245
+ const stranded = join34(dir, name);
40246
+ if (!lstatSync4(stranded).isFile()) continue;
40247
+ rmSync8(stranded, { force: true });
40248
+ } catch {
40249
+ }
40250
+ }
40251
+ }
39293
40252
  function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39294
40253
  const byFile = /* @__PURE__ */ new Map();
39295
40254
  for (const target of targets) {
@@ -39304,11 +40263,14 @@ function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39304
40263
  const struck = [];
39305
40264
  for (const [filePath, fileTargets] of byFile) {
39306
40265
  let content;
40266
+ let mode;
39307
40267
  try {
39308
- content = readFileSync21(filePath, "utf8");
40268
+ mode = statSync11(filePath).mode & 511;
40269
+ content = readFileSync22(filePath, "utf8");
39309
40270
  } catch {
39310
40271
  continue;
39311
40272
  }
40273
+ sweepStrandedTemps(filePath);
39312
40274
  const struckHere = [];
39313
40275
  const struckValues = /* @__PURE__ */ new Set();
39314
40276
  for (const target of fileTargets) {
@@ -39322,9 +40284,9 @@ function redactLeakedKeysDetailed(targets, scope = platformRedactionScope()) {
39322
40284
  struckHere.push(target);
39323
40285
  }
39324
40286
  if (struckHere.length === 0) continue;
39325
- const tmpPath = `${filePath}.aka-redact.tmp`;
40287
+ const tmpPath = tempPathFor(filePath);
39326
40288
  try {
39327
- writeFileSync10(tmpPath, content);
40289
+ writeFileSync10(tmpPath, content, { mode, flag: "wx" });
39328
40290
  renameSync5(tmpPath, filePath);
39329
40291
  } catch {
39330
40292
  try {
@@ -39381,7 +40343,7 @@ async function redactSurfacedSecrets(findings, overrides = {}) {
39381
40343
  for (const [filePath, fileFindings] of byFile) {
39382
40344
  let content;
39383
40345
  try {
39384
- content = readFileSync22(filePath, "utf8");
40346
+ content = readFileSync23(filePath, "utf8");
39385
40347
  } catch {
39386
40348
  unrecovered.push(...fileFindings);
39387
40349
  continue;
@@ -39535,11 +40497,11 @@ async function route(frameText, rawOption, rawPosture) {
39535
40497
  break;
39536
40498
  }
39537
40499
  }
39538
- if (process.argv[1] && fileURLToPath5(import.meta.url) === process.argv[1]) {
40500
+ if (process.argv[1] && fileURLToPath6(import.meta.url) === process.argv[1]) {
39539
40501
  try {
39540
40502
  const argv = process.argv.slice(2);
39541
40503
  const optionIndex = argv.indexOf("--option");
39542
- const frameText = readFileSync23(0, "utf8");
40504
+ const frameText = readFileSync24(0, "utf8");
39543
40505
  if (optionIndex === -1) {
39544
40506
  present(frameText);
39545
40507
  } else {