@akasecurity/ai-tc-claude-code 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
@@ -491,9 +491,6 @@ var require_ignore = __commonJS({
491
491
  }
492
492
  });
493
493
 
494
- // ../../packages/plugin-runtime/src/attached/egress-wire.ts
495
- import { createHash as createHash4 } from "crypto";
496
-
497
494
  // ../../packages/persistence/src/attached-derived.ts
498
495
  import { rmSync } from "fs";
499
496
  import { join } from "path";
@@ -505,6 +502,14 @@ var ATTACHED_HISTORY_SYNC_STATE_FILENAME = "attached-history-sync.json";
505
502
  import { chmodSync as chmodSync2, lstatSync as lstatSync2, readFileSync, rmSync as rmSync3, statSync } from "fs";
506
503
  import { join as join2 } from "path";
507
504
 
505
+ // ../../packages/schema/src/drizzle/deferred-migrations.ts
506
+ var DEFERRED_MIGRATION_TAGS = [
507
+ "0031_audit_capture_by_time_index",
508
+ "0032_audit_capture_by_id_index",
509
+ "0033_audit_capture_location_index",
510
+ "0034_findings_read_indexes"
511
+ ];
512
+
508
513
  // ../../packages/schema/src/drizzle/sqlite-ddl.ts
509
514
  var SQLITE_MIGRATIONS = [
510
515
  {
@@ -622,6 +627,30 @@ var SQLITE_MIGRATIONS = [
622
627
  {
623
628
  tag: "0028_activity_session_probe_indexes",
624
629
  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"
630
+ },
631
+ {
632
+ tag: "0029_audit_capture_rollup_index",
633
+ 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');"
634
+ },
635
+ {
636
+ tag: "0030_audit_content_expiry",
637
+ 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;"
638
+ },
639
+ {
640
+ tag: "0031_audit_capture_by_time_index",
641
+ 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');"
642
+ },
643
+ {
644
+ tag: "0032_audit_capture_by_id_index",
645
+ 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');"
646
+ },
647
+ {
648
+ tag: "0033_audit_capture_location_index",
649
+ 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');"
650
+ },
651
+ {
652
+ tag: "0034_findings_read_indexes",
653
+ 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`);"
625
654
  }
626
655
  ];
627
656
 
@@ -20669,6 +20698,15 @@ var FindingCategory = external_exports.enum([
20669
20698
  ]).meta({ id: "FindingCategory" });
20670
20699
  var FindingOrigin = external_exports.enum(["in-flight", "at-rest"]).meta({ id: "FindingOrigin" });
20671
20700
  var FindingStatus = external_exports.enum(["open", "handled", "resolved", "dismissed"]).meta({ id: "FindingStatus" });
20701
+ var SyncFailureReason = external_exports.enum(["deployment_refused", "payload_invalid", "detached_undelivered"]).meta({ id: "SyncFailureReason" });
20702
+ var FindingDeliveryState = external_exports.enum(["sent", "queued", "not_sent", "never_offered", "local_scan"]).meta({ id: "FindingDeliveryState" });
20703
+ var FindingDelivery = external_exports.object({
20704
+ state: FindingDeliveryState,
20705
+ // The delivery time for `sent`; the failure time for `not_sent` when recorded.
20706
+ at: external_exports.iso.datetime().optional(),
20707
+ // Only on `not_sent`, and only when a known reason was recorded.
20708
+ reason: SyncFailureReason.optional()
20709
+ }).meta({ id: "FindingDelivery" });
20672
20710
  var ResolutionMethod = external_exports.enum([
20673
20711
  "enforced-in-flight",
20674
20712
  "fixed-at-source",
@@ -20725,7 +20763,10 @@ var FindingInstance = external_exports.object({
20725
20763
  // The session that event belongs to, when it has one — the seam a
20726
20764
  // per-instance "view session" link needs. Absent for events captured
20727
20765
  // outside a session.
20728
- sessionId: external_exports.string().optional()
20766
+ sessionId: external_exports.string().optional(),
20767
+ // The delivery state of the event above (see FindingDelivery). Optional so
20768
+ // readers that do not project it stay valid.
20769
+ delivery: FindingDelivery.optional()
20729
20770
  }).meta({ id: "FindingInstance" });
20730
20771
  var FindingGroup = external_exports.object({
20731
20772
  id: external_exports.string(),
@@ -20742,13 +20783,11 @@ var FindingGroup = external_exports.object({
20742
20783
  latestDetectedAt: external_exports.iso.datetime(),
20743
20784
  instances: external_exports.array(FindingInstance),
20744
20785
  // Derived from instances' statuses with open-dominates precedence (see
20745
- // buildFindingGroups). Undefined only when no instance carries a status.
20786
+ // foldGroupStatus). Undefined only when no instance carries a status.
20746
20787
  status: FindingStatus.optional(),
20747
- // The distinct people across the WHOLE group, not just the `instances`
20748
- // preview — from the store's whole-group aggregate when it supplies one,
20749
- // else folded from the rows (see buildFindingGroups). Undefined when no
20750
- // instance carries a user, or when the store supplied whole-group folds
20751
- // without one.
20788
+ // The distinct people across the WHOLE group, not just the instances
20789
+ // carried here. Undefined when no instance carries a user, or when the
20790
+ // store supplied whole-group folds without one.
20752
20791
  users: external_exports.array(FindingUser).optional()
20753
20792
  }).meta({ id: "FindingGroup" });
20754
20793
  var FindingStats = external_exports.object({
@@ -20777,21 +20816,34 @@ var FindingFacets = external_exports.object({
20777
20816
  // counted under no value.
20778
20817
  status: external_exports.array(FindingFacetItem),
20779
20818
  // Host tool (attributes.tool_name). Present only on the instance-level
20780
- // reads, which can filter by it; the grouped read omits the dimension
20819
+ // reads, which can filter by it; the type-level read omits the dimension
20781
20820
  // because a group spans tools.
20782
- tool: external_exports.array(FindingFacetItem).optional()
20821
+ tool: external_exports.array(FindingFacetItem).optional(),
20822
+ // Delivery states (FindingDeliveryState). Present only on the
20823
+ // instance-level reads, like `tool`.
20824
+ deployment: external_exports.array(FindingFacetItem).optional()
20783
20825
  }).meta({ id: "FindingFacets" });
20784
- var DEFAULT_GROUPED_FINDINGS_LIMIT = 50;
20785
- var ListGroupedFindingsQuery = external_exports.object({
20826
+ var FindingTypeSummary = FindingGroup.omit({ instances: true, match: true }).meta({
20827
+ id: "FindingTypeSummary"
20828
+ });
20829
+ var DEFAULT_FINDING_TYPES_LIMIT = 50;
20830
+ var MAX_FINDING_TYPES_LIMIT = 100;
20831
+ var ListFindingTypesQuery = external_exports.object({
20786
20832
  // NOTE: severity filters by Severity (critical/high/medium/low), not by
20787
- // FindingAction.
20833
+ // FindingAction. It narrows TYPES: a type's severity is the one its newest
20834
+ // firing version carries, and this list pages types.
20835
+ //
20836
+ // That is NOT a claim the findings of a type share it. A rule can hold several
20837
+ // definition versions at different severities, so a type kept by this filter
20838
+ // can hold findings that individually do not match — see totals.findings on
20839
+ // ListFindingTypesResponse, which counts them all.
20788
20840
  severity: external_exports.array(Severity).optional(),
20789
20841
  subtype: external_exports.array(external_exports.string()).optional(),
20790
20842
  provider: external_exports.array(FindingProvider).optional(),
20791
20843
  action: external_exports.array(FindingAction).optional(),
20792
- // Matches a group's DERIVED status (see FindingGroup.status), not its
20793
- // individual instances' — so a filtered group's Status column always reads
20794
- // one of the requested values.
20844
+ // Matches a type's DERIVED status (see FindingGroup.status), not its
20845
+ // individual findings' — so a filtered row's status always reads one of the
20846
+ // requested values.
20795
20847
  status: external_exports.array(FindingStatus).optional(),
20796
20848
  q: external_exports.string().optional(),
20797
20849
  // Scope to findings whose event carries this session id (the Activity page's
@@ -20801,23 +20853,37 @@ var ListGroupedFindingsQuery = external_exports.object({
20801
20853
  // from a time-scoped page (Activity's range) can carry that scope. Absent
20802
20854
  // means all time — this list has no default window.
20803
20855
  from: external_exports.iso.datetime().optional(),
20804
- // A group or instance id that must appear in the page even when the cursor
20805
- // has already advanced past its sort position. This is what keeps the
20806
- // Findings page's one-shot ?finding= deep link resolving once the list
20807
- // paginates: the target group is appended out of sort order rather than
20808
- // scanning forward for it. Never affects totals, facets or the cursor.
20856
+ // A RULE id that must appear in the page even when the cursor has already
20857
+ // advanced past its sort position. This is what keeps the selected type
20858
+ // visible in the list once it paginates: the target is appended out of sort
20859
+ // order rather than scanned forward for. Never affects totals, facets or the
20860
+ // cursor. Unlike the grouped read this replaces, it names a rule only — an
20861
+ // instance id is resolved by `findingInstance`, which is a primary-key seek
20862
+ // and so is not bounded by what any page happens to hold.
20809
20863
  includeId: external_exports.string().optional(),
20810
- groupBy: external_exports.literal("type").optional(),
20811
- limit: external_exports.coerce.number().int().min(1).max(100).optional(),
20864
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_TYPES_LIMIT).optional(),
20812
20865
  cursor: external_exports.string().optional()
20813
20866
  });
20814
- var ListGroupedFindingsResponse = external_exports.object({
20867
+ var ListFindingTypesResponse = external_exports.object({
20815
20868
  totals: external_exports.object({
20869
+ // Findings belonging to the matching TYPES — not findings that each match
20870
+ // the filters. The filters here select types, so a type that survives
20871
+ // contributes its whole instanceCount.
20872
+ //
20873
+ // `status` is the one exception, narrowed per finding via
20874
+ // countInstancesByStatus. `severity`, `provider` and `action` are not, so
20875
+ // this can exceed what the instance read reports for the same filters: a
20876
+ // rule whose severity moved between versions is kept on its newest and
20877
+ // still counts its older findings. Narrowing the other three needs
20878
+ // per-dimension counts the aggregate does not carry today.
20816
20879
  findings: external_exports.number().int().nonnegative(),
20817
- groups: external_exports.number().int().nonnegative()
20880
+ // Counts TYPES, which is the unit this read pages. The instance read's
20881
+ // own totals count findings; the two deliberately answer different
20882
+ // questions and are never summed.
20883
+ types: external_exports.number().int().nonnegative()
20818
20884
  }),
20819
20885
  facets: FindingFacets,
20820
- items: external_exports.array(FindingGroup),
20886
+ items: external_exports.array(FindingTypeSummary),
20821
20887
  nextCursor: external_exports.string().nullable(),
20822
20888
  // Present only on session-scoped queries (`sessionId` set): per ruleId, how
20823
20889
  // many times that rule fired in the session's persisted transcript. Findings
@@ -20825,7 +20891,7 @@ var ListGroupedFindingsResponse = external_exports.object({
20825
20891
  // every firing, so the two numbers legitimately differ — this map lets a
20826
20892
  // session-scoped view show both.
20827
20893
  sessionFirings: external_exports.record(external_exports.string(), external_exports.number().int().nonnegative()).optional()
20828
- }).meta({ id: "ListGroupedFindingsResponse" });
20894
+ }).meta({ id: "ListFindingTypesResponse" });
20829
20895
  var ApplyFindingActionRequest = external_exports.object({
20830
20896
  // 'quarantined' is system-assigned (see FindingAction) — clients may not set
20831
20897
  // it, so it is excluded from the request contract. The mapping helper
@@ -20855,16 +20921,19 @@ var DEFAULT_FLAT_FINDINGS_LIMIT = 50;
20855
20921
  var MAX_FLAT_FINDINGS_LIMIT = 200;
20856
20922
  var ListFindingInstancesQuery = external_exports.object({
20857
20923
  severity: external_exports.array(Severity).optional(),
20858
- // Rule ids, the same vocabulary the grouped list's `subtype` carries.
20924
+ // Rule ids, the same vocabulary the types list's `subtype` carries. Pinning
20925
+ // ONE of them is how the master/detail view scopes its right-hand panel.
20859
20926
  subtype: external_exports.array(external_exports.string()).optional(),
20860
20927
  provider: external_exports.array(FindingProvider).optional(),
20861
20928
  action: external_exports.array(FindingAction).optional(),
20862
20929
  // Matches each instance's OWN derived status (deriveFindingStatus), unlike
20863
- // the grouped query's group-level fold.
20930
+ // the types query's type-level fold.
20864
20931
  status: external_exports.array(FindingStatus).optional(),
20865
20932
  // Exact host-tool names (attributes.tool_name, e.g. 'Bash'). A real filter,
20866
20933
  // where the free-text `q` can only match the rendered "via Bash" label.
20867
20934
  tool: external_exports.array(external_exports.string()).optional(),
20935
+ // The delivery state of each finding's event (see FindingDelivery).
20936
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20868
20937
  // Exact repository / file-path matches, for the drill-down out of the
20869
20938
  // locations view. A row whose event carries no repo/file matches neither.
20870
20939
  repo: external_exports.string().optional(),
@@ -20877,37 +20946,51 @@ var ListFindingInstancesQuery = external_exports.object({
20877
20946
  });
20878
20947
  var ListFindingInstancesResponse = external_exports.object({
20879
20948
  // Instances matching the filters across the whole scope, not just this
20880
- // page — cursor-independent, like the grouped list's totals.
20949
+ // page — cursor-independent, like the types list's totals.
20881
20950
  totals: external_exports.object({ findings: external_exports.number().int().nonnegative() }),
20882
- // Counts in INSTANCES here, where the grouped response counts groups. Each
20951
+ // Counts in INSTANCES here, where the types response counts types. Each
20883
20952
  // dimension still excludes its own filter.
20884
20953
  facets: FindingFacets,
20885
20954
  items: external_exports.array(FindingInstanceDetail),
20886
20955
  nextCursor: external_exports.string().nullable()
20887
20956
  }).meta({ id: "ListFindingInstancesResponse" });
20888
- var FindingLocationFile = external_exports.object({
20889
- // Empty when the instances carried no file path (a prompt or a tool call
20890
- // with no file attribution).
20891
- file: external_exports.string(),
20892
- instanceCount: external_exports.number().int().nonnegative(),
20893
- maxSeverity: Severity,
20894
- latestDetectedAt: external_exports.iso.datetime(),
20895
- // Folded from the instances' derived statuses with the same
20896
- // open-dominates precedence a group uses.
20897
- status: FindingStatus.optional(),
20898
- // Distinct rules seen at this location, capped — the row shows them as
20899
- // chips, and the count is what conveys scale.
20900
- ruleIds: external_exports.array(external_exports.string())
20901
- }).meta({ id: "FindingLocationFile" });
20902
- var FindingLocationRepo = external_exports.object({
20957
+ var ListFindingInstancesPage = external_exports.object({
20958
+ items: external_exports.array(FindingInstanceDetail),
20959
+ nextCursor: external_exports.string().nullable()
20960
+ }).meta({ id: "ListFindingInstancesPage" });
20961
+ var FindingLocationSummary = external_exports.object({
20962
+ // Opaque, stable, minted from the pair by encodeLocationId. It exists
20963
+ // because a location's identity is two values and a URL param carries one:
20964
+ // `?loc=` names a location the way `?rule=` names a type. Only ever compared
20965
+ // for EQUALITY — the page's selection check, this read's `includeId`, the
20966
+ // client's page dedupe — never decoded, and never a sort key.
20967
+ id: external_exports.string(),
20903
20968
  /** Empty when the instances carried no repo attribute. */
20904
20969
  repo: external_exports.string(),
20970
+ // Empty when the instances carried no file path (a prompt, or a tool call
20971
+ // with no file attribution). Both halves empty is a real location — usually
20972
+ // the largest one in a store — and is selectable like any other.
20973
+ file: external_exports.string(),
20905
20974
  instanceCount: external_exports.number().int().nonnegative(),
20975
+ // The WORST severity present, not the first row's. It is this list's primary
20976
+ // sort key, so it is also what explains why a row is where it is, and it is
20977
+ // how a reader decides what to open without opening everything.
20906
20978
  maxSeverity: Severity,
20907
20979
  latestDetectedAt: external_exports.iso.datetime(),
20980
+ // Folded from the instances' derived statuses with the same open-dominates
20981
+ // precedence a group uses, so it answers "is anything left to do here" and
20982
+ // not much more: a location holding 1 open among 40 resolved reads like one
20983
+ // holding 40 open. That loss is accepted — the panel beside this list
20984
+ // carries each finding's own status, and instanceCount sits next to the
20985
+ // badge.
20908
20986
  status: FindingStatus.optional(),
20909
- files: external_exports.array(FindingLocationFile)
20910
- }).meta({ id: "FindingLocationRepo" });
20987
+ // Every distinct rule seen at this location, UNCAPPED — so the length is a
20988
+ // tally rather than a sample and a row can say how many there are. Bounded
20989
+ // by the ruleset, not by the store. The view bounds what it DISPLAYS.
20990
+ ruleIds: external_exports.array(external_exports.string())
20991
+ }).meta({ id: "FindingLocationSummary" });
20992
+ var DEFAULT_FINDING_LOCATIONS_LIMIT = 50;
20993
+ var MAX_FINDING_LOCATIONS_LIMIT = 100;
20911
20994
  var ListFindingLocationsQuery = external_exports.object({
20912
20995
  severity: external_exports.array(Severity).optional(),
20913
20996
  subtype: external_exports.array(external_exports.string()).optional(),
@@ -20917,21 +21000,47 @@ var ListFindingLocationsQuery = external_exports.object({
20917
21000
  // instances that match, and folds its status from those.
20918
21001
  status: external_exports.array(FindingStatus).optional(),
20919
21002
  tool: external_exports.array(external_exports.string()).optional(),
21003
+ // The delivery state of each finding's event (see FindingDelivery).
21004
+ deployment: external_exports.array(FindingDeliveryState).optional(),
20920
21005
  q: external_exports.string().optional(),
20921
21006
  sessionId: external_exports.string().optional(),
20922
21007
  from: external_exports.iso.datetime().optional(),
20923
- limit: external_exports.coerce.number().int().min(1).max(500).optional()
21008
+ // A LOCATION id (see FindingLocationSummary.id) that must appear in the page
21009
+ // even when the cursor has already advanced past its sort position — the
21010
+ // counterpart of ListFindingTypesQuery.includeId, and needed far more often
21011
+ // here. Selecting a row pushes the URL, which re-renders the server and resets
21012
+ // the client's page cache to page 0; with distinct (repo, file) pairs running
21013
+ // into the thousands, a selection sitting off page 0 is the ordinary case
21014
+ // rather than a deep-link corner. Never affects totals, facets or the cursor.
21015
+ includeId: external_exports.string().optional(),
21016
+ limit: external_exports.coerce.number().int().min(1).max(MAX_FINDING_LOCATIONS_LIMIT).optional(),
21017
+ cursor: external_exports.string().optional()
20924
21018
  });
20925
21019
  var ListFindingLocationsResponse = external_exports.object({
20926
21020
  totals: external_exports.object({
21021
+ // Findings matching the filters across the whole scope. Unlike the types
21022
+ // read's same-named field this needs no caveat: the filters here narrow
21023
+ // per finding, so this is the sum of every row's instanceCount.
20927
21024
  findings: external_exports.number().int().nonnegative(),
20928
- repos: external_exports.number().int().nonnegative(),
20929
- files: external_exports.number().int().nonnegative()
21025
+ // Counts LOCATIONS, the unit this read pages — the number the paginator
21026
+ // states. The facets beside it count FINDINGS (see below); a surface
21027
+ // showing both says which is which.
21028
+ locations: external_exports.number().int().nonnegative()
20930
21029
  }),
20931
- /** Sorted by max severity, then most recent. */
20932
- items: external_exports.array(FindingLocationRepo),
20933
- /** Whether `limit` truncated the repo list. */
20934
- hasMore: external_exports.boolean()
21030
+ // Counts in FINDINGS, where the types response counts types, each dimension
21031
+ // still excluding its own filter. Deliberately not locations: counting those
21032
+ // needs a set of location keys per dimension per value — memory tracking the
21033
+ // store times the vocabulary, in a read whose scan promises flat memory —
21034
+ // and the cheap per-location version is not an approximation but WRONG. A
21035
+ // location holding {claudecode, block} and {codex, warn} would survive
21036
+ // provider=claudecode AND action=warn, under which no single finding
21037
+ // matches, so the facet would contradict the instanceCount this whole view
21038
+ // rests on. Findings also keep the toolbar in the same unit as the page
21039
+ // tally and the panel it sits above.
21040
+ facets: FindingFacets,
21041
+ /** Sorted by max severity, then most recent, then (repo, file). */
21042
+ items: external_exports.array(FindingLocationSummary),
21043
+ nextCursor: external_exports.string().nullable()
20935
21044
  }).meta({ id: "ListFindingLocationsResponse" });
20936
21045
 
20937
21046
  // ../../packages/schema/src/zod/meta.ts
@@ -21095,6 +21204,10 @@ var CaptureAttributes = external_exports.object({
21095
21204
  // to 'allow' — the enforcement audit trail's link back to the grant that
21096
21205
  // authorized the bypass.
21097
21206
  exception_ids: external_exports.array(external_exports.guid()).optional(),
21207
+ // The persisted spellings of EventMetadata's messageId/conversationId — the
21208
+ // join back to the `llm_call` leaf for the same assistant turn.
21209
+ message_id: external_exports.string().optional(),
21210
+ conversation_id: external_exports.string().optional(),
21098
21211
  // Whole milliseconds this capture's inspection blocked its caller — the
21099
21212
  // plugin's own added latency (see EventMetadata.inspectionMs, whose value
21100
21213
  // this is). Promoted to the `inspection_ms` generated column so the facet is
@@ -21103,7 +21216,19 @@ var CaptureAttributes = external_exports.object({
21103
21216
  // inline json_extract and is not itself an optimization.
21104
21217
  // ABSENT on replayed captures (backfill / worktree scan) and on rows written
21105
21218
  // before the measurement shipped — never present as a placeholder 0.
21106
- inspection_ms: external_exports.number().int().nonnegative().optional()
21219
+ inspection_ms: external_exports.number().int().nonnegative().optional(),
21220
+ // What a `redact` this capture could not carry out became instead (see
21221
+ // EventMetadata.redactDegradedTo, whose value this is). Present only when a
21222
+ // degrade actually happened, so absence is the ordinary case rather than a
21223
+ // reader having to distinguish it from a zero.
21224
+ //
21225
+ // PER CAPTURE, while `inspection_findings.action_taken` is per finding —
21226
+ // so on a multi-finding row this does not say which finding degraded, and
21227
+ // its presence does not mean the fallback decided the capture's action. A
21228
+ // capture denied by another finding's own Block policy carries `block`
21229
+ // here too. The full statement is on EventMetadata.redactDegradedTo; it is
21230
+ // repeated rather than referenced because a store reader opens this file.
21231
+ redact_degraded_to: ActionTaken.optional()
21107
21232
  }).catchall(external_exports.unknown());
21108
21233
  var ToolCallInspection = external_exports.object({
21109
21234
  ruleId: external_exports.string().min(1),
@@ -21302,7 +21427,17 @@ var AuditEvent = external_exports.object({
21302
21427
  /** `share` to a first-party/internal destination. */
21303
21428
  internal: external_exports.boolean(),
21304
21429
  /** Event needs review (e.g. unverified egress). */
21305
- flagged: external_exports.boolean()
21430
+ flagged: external_exports.boolean(),
21431
+ /**
21432
+ * The body this event's `title` is drawn from was cleared by local body
21433
+ * expiry, so an EMPTY title here means "gone", not "never had one".
21434
+ *
21435
+ * A separate flag rather than a sentinel written into `title`: the title is
21436
+ * rendered text, and a store-layer module that invented display copy for it
21437
+ * would be choosing words the view is supposed to choose. Additive and
21438
+ * defaulted, so an older producer still validates.
21439
+ */
21440
+ bodyExpired: external_exports.boolean().default(false)
21306
21441
  }).meta({ id: "ActivityAuditEvent" });
21307
21442
  var ActivitySessionSummary = external_exports.object({
21308
21443
  id: external_exports.string(),
@@ -22100,6 +22235,14 @@ var ControlPlaneErrorBody = external_exports.object({
22100
22235
  message: external_exports.string().optional()
22101
22236
  }).optional()
22102
22237
  });
22238
+ var RemoteFailureKind = external_exports.enum([
22239
+ "unauthorized",
22240
+ "forbidden",
22241
+ "route-absent",
22242
+ "invalid-request",
22243
+ "rejected",
22244
+ "unreachable"
22245
+ ]);
22103
22246
  var AttachDeviceRequest = external_exports.object({
22104
22247
  // This machine's own continuity id, so re-attaching ROTATES the credential
22105
22248
  // on one machine record instead of producing a second one. Client-minted
@@ -22635,6 +22778,12 @@ var EventMetadata = external_exports.object({
22635
22778
  // to 'allow' — the enforcement audit trail's link back to the grant that
22636
22779
  // authorized the bypass. Absent on captures where no exception applied.
22637
22780
  exceptionIds: external_exports.array(external_exports.guid()).optional(),
22781
+ // The assistant message this capture belongs to, and the conversation it sits
22782
+ // in — set by the browser extension's network capture so a stored `response`
22783
+ // row can be joined to the `llm_call` leaf describing the same turn. Absent
22784
+ // on every other capture path, which has no such id.
22785
+ messageId: external_exports.string().optional(),
22786
+ conversationId: external_exports.string().optional(),
22638
22787
  // How long THIS capture's inspection blocked its caller, in whole
22639
22788
  // milliseconds — the plugin's own added latency, NOT the LLM call it sat in
22640
22789
  // front of. Measured inside `capture()` (@akasecurity/plugin-sdk) across
@@ -22647,7 +22796,37 @@ var EventMetadata = external_exports.object({
22647
22796
  // Absent is also what every pre-measurement client writes, and what a
22648
22797
  // clock failure degrades to — a reader must treat absence as "not measured"
22649
22798
  // and never as a zero, which would read as "inspection is free".
22650
- inspectionMs: external_exports.number().int().nonnegative().optional()
22799
+ inspectionMs: external_exports.number().int().nonnegative().optional(),
22800
+ // What a `redact` this capture COULD NOT CARRY OUT became instead — the
22801
+ // workspace's `redactFallback`, applied because the field could not be
22802
+ // masked in place (a shell command, a URL, or any argument on a host whose
22803
+ // hook contract offers no rewrite channel).
22804
+ //
22805
+ // It exists because the action alone cannot say why. A finding recorded as
22806
+ // `warn` reads identically whether its detection was ASSIGNED Warn or was
22807
+ // assigned Redact on a field that could not take one — and those are
22808
+ // different facts about the same row: the first is a policy the user chose,
22809
+ // the second is a masking the host could not perform. Absent means no
22810
+ // degrade happened, which is every ordinary capture.
22811
+ //
22812
+ // TWO LIMITS a reader of a stored row has to know, because the grain here
22813
+ // is the CAPTURE while `actionTaken` is per FINDING:
22814
+ //
22815
+ // - It does not say WHICH finding degraded. A capture carrying a degraded
22816
+ // `redact` alongside a finding ASSIGNED the same action stores both
22817
+ // identically and one reason for the pair; attributing it to both
22818
+ // describes the assigned one wrongly, and to neither loses the degrade.
22819
+ // - PRESENCE IS NOT CAUSATION. The value is the action the lost redact
22820
+ // became, not the reason the capture ended as it did — a capture denied
22821
+ // by some other finding's own Block policy still carries `block` here,
22822
+ // and clearing the workspace's fallback would not have let it through.
22823
+ // Gate on the value against what a fallback can produce; never read the
22824
+ // field's presence as "this was the fallback's doing".
22825
+ //
22826
+ // Both are pinned as behaviour in @akasecurity/plugin-sdk's runtime suite.
22827
+ // Closing either means moving the reason onto the finding row, which
22828
+ // already carries its own action.
22829
+ redactDegradedTo: ActionTaken.optional()
22651
22830
  }).meta({ id: "EventMetadata" });
22652
22831
  var Event = external_exports.object({
22653
22832
  id: external_exports.guid(),
@@ -22757,7 +22936,32 @@ var RotateKeyInput = external_exports.object({
22757
22936
  confirmation: external_exports.string()
22758
22937
  });
22759
22938
 
22939
+ // ../../packages/schema/src/zod/finding-delivery.ts
22940
+ var KNOWN_REASONS = SyncFailureReason.options;
22941
+ function knownReason(value) {
22942
+ return value !== null && KNOWN_REASONS.includes(value) ? value : void 0;
22943
+ }
22944
+ function deriveFindingDelivery(row) {
22945
+ if (row.kind === "code_change") return { state: "local_scan" };
22946
+ if (row.syncedAt !== null && row.syncedAt > 0) {
22947
+ return { state: "sent", at: epochMillisToIso(row.syncedAt) };
22948
+ }
22949
+ if (row.syncedAt !== null) {
22950
+ const reason = knownReason(row.syncFailure);
22951
+ return {
22952
+ state: "not_sent",
22953
+ ...row.syncFailedAt === null ? {} : { at: epochMillisToIso(row.syncFailedAt) },
22954
+ ...reason === void 0 ? {} : { reason }
22955
+ };
22956
+ }
22957
+ if (row.outboxOwed === 1 || row.syncClaimedAt !== null) return { state: "queued" };
22958
+ return { state: "never_offered" };
22959
+ }
22960
+
22760
22961
  // ../../packages/schema/src/zod/findings-group-build.ts
22962
+ function lookupOwn(map2, key) {
22963
+ return Object.hasOwn(map2, key) ? map2[key] : void 0;
22964
+ }
22761
22965
  function toApiAction(dbVal) {
22762
22966
  const map2 = {
22763
22967
  log: "monitored",
@@ -22766,7 +22970,7 @@ function toApiAction(dbVal) {
22766
22970
  warn: "warned",
22767
22971
  allow: "allowed"
22768
22972
  };
22769
- return map2[dbVal] ?? "allowed";
22973
+ return lookupOwn(map2, dbVal) ?? "allowed";
22770
22974
  }
22771
22975
  function toApiCategory(dbVal) {
22772
22976
  if (dbVal === "code_context") return "source_code";
@@ -22774,13 +22978,18 @@ function toApiCategory(dbVal) {
22774
22978
  return parsed2.success ? parsed2.data : "custom";
22775
22979
  }
22776
22980
  function toApiProvider(sourceTool) {
22777
- return TOOL_TO_HARNESS[sourceTool] ?? HARNESS.Api;
22981
+ return lookupOwn(TOOL_TO_HARNESS, sourceTool) ?? HARNESS.Api;
22778
22982
  }
22779
- var STATUS_PRECEDENCE = ["open", "handled", "dismissed", "resolved"];
22983
+ var FINDING_STATUS_PRECEDENCE = [
22984
+ "open",
22985
+ "handled",
22986
+ "dismissed",
22987
+ "resolved"
22988
+ ];
22780
22989
  function foldGroupStatus(instanceStatuses) {
22781
22990
  const statuses = new Set(instanceStatuses.filter((s) => s !== void 0));
22782
22991
  if (statuses.size === 0) return void 0;
22783
- for (const candidate of STATUS_PRECEDENCE) {
22992
+ for (const candidate of FINDING_STATUS_PRECEDENCE) {
22784
22993
  if (statuses.has(candidate)) return candidate;
22785
22994
  }
22786
22995
  return void 0;
@@ -22793,139 +23002,62 @@ function deriveFindingStatus(row) {
22793
23002
  if (row.latestResolutionStatus === "dismissed") return "dismissed";
22794
23003
  return "open";
22795
23004
  }
22796
- function distinctUsers(instances) {
22797
- const seen = /* @__PURE__ */ new Set();
22798
- const users = [];
22799
- for (const i of instances) {
22800
- if (i.user === void 0 || seen.has(i.user.id)) continue;
22801
- seen.add(i.user.id);
22802
- users.push(i.user);
22803
- }
22804
- return users;
22805
- }
22806
23005
  function sortUsers(users) {
22807
23006
  return [...users].sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
22808
23007
  }
22809
- function buildFindingGroups(rows, opts = {}) {
22810
- const overrides = opts.overrides;
23008
+ function buildFindingTypes(aggregates, opts = {}) {
22811
23009
  const packNames = opts.packNames;
22812
- const aggregates = opts.aggregates;
22813
- const byRuleId = /* @__PURE__ */ new Map();
22814
- for (const row of rows) {
22815
- const existing = byRuleId.get(row.ruleId);
22816
- if (existing) existing.push(row);
22817
- else byRuleId.set(row.ruleId, [row]);
22818
- }
22819
- const groups = [];
22820
- for (const [ruleId, ruleRows] of byRuleId) {
22821
- const instances = ruleRows.map((r) => {
22822
- const effectiveDbAction = overrides?.get(r.id) ?? r.actionTaken;
22823
- return {
22824
- id: r.id,
22825
- provider: toApiProvider(r.sourceTool),
22826
- repo: r.repo,
22827
- file: r.file,
22828
- ...r.toolName === void 0 ? {} : { toolName: r.toolName },
22829
- ...r.eventId === void 0 ? {} : { eventId: r.eventId },
22830
- ...r.sessionId === void 0 ? {} : { sessionId: r.sessionId },
22831
- ...r.user === void 0 ? {} : { user: r.user },
22832
- action: toApiAction(effectiveDbAction),
22833
- detectedAt: r.occurredAt,
22834
- confidence: r.confidence,
22835
- status: r.status
22836
- };
22837
- });
22838
- const agg = aggregates?.get(ruleId);
22839
- const users = agg ? sortUsers(agg.users ?? []) : distinctUsers(instances);
22840
- const latestDetectedAt = agg?.latestDetectedAt ?? ruleRows.reduce(
22841
- (max, r) => r.occurredAt > max ? r.occurredAt : max,
22842
- ruleRows[0]?.occurredAt ?? (/* @__PURE__ */ new Date(0)).toISOString()
22843
- );
22844
- const seenProviders = /* @__PURE__ */ new Set();
22845
- const providers = (agg ? [...new Set(agg.sourceTools.map(toApiProvider))].sort() : instances.map((i) => i.provider)).filter((p) => {
22846
- if (seenProviders.has(p)) return false;
22847
- seenProviders.add(p);
22848
- return true;
22849
- });
22850
- const actionSet = new Set(
22851
- agg ? agg.actionsTaken.map(toApiAction) : instances.map((i) => i.action)
22852
- );
23010
+ const types = [];
23011
+ for (const [ruleId, agg] of aggregates) {
23012
+ const users = sortUsers(agg.users ?? []);
23013
+ const providers = [...new Set(agg.sourceTools.map(toApiProvider))].sort();
23014
+ const actionSet = new Set(agg.actionsTaken.map(toApiAction));
22853
23015
  const aggregateAction = actionSet.size === 1 ? [...actionSet][0] ?? null : null;
22854
- const severity = ruleRows[0]?.severity ?? "low";
22855
- const detection = {
22856
- id: ruleId,
22857
- name: packNames?.get(ruleId) ?? null
22858
- };
22859
- const apiCategory = toApiCategory(ruleRows[0]?.category ?? "custom");
22860
- const policy = { id: `category:${apiCategory}`, name: apiCategory };
22861
- const match = {
22862
- maskedValue: ruleRows[0]?.maskedMatch ?? "",
22863
- contextPrefix: ""
22864
- // empty (pending privacy review)
22865
- };
22866
- const status = foldGroupStatus(
22867
- agg ? agg.statusInputs.map(deriveFindingStatus) : instances.map((i) => i.status)
22868
- );
22869
- const group = {
23016
+ const apiCategory = toApiCategory(agg.category ?? "custom");
23017
+ const type = {
22870
23018
  id: ruleId,
22871
23019
  category: apiCategory,
22872
23020
  subtype: ruleId,
22873
23021
  // human label comes with pack metadata later
22874
- severity,
22875
- match,
22876
- detection,
22877
- policy,
22878
- instanceCount: agg?.instanceCount ?? instances.length,
23022
+ severity: agg.severity ?? "low",
23023
+ detection: { id: ruleId, name: packNames?.get(ruleId) ?? null },
23024
+ policy: { id: `category:${apiCategory}`, name: apiCategory },
23025
+ instanceCount: agg.instanceCount,
22879
23026
  providers,
22880
23027
  aggregateAction,
22881
- latestDetectedAt,
22882
- instances,
22883
- status,
23028
+ latestDetectedAt: agg.latestDetectedAt,
23029
+ status: foldGroupStatus(agg.statusInputs.map(deriveFindingStatus)),
22884
23030
  ...users.length > 0 ? { users } : {}
22885
23031
  };
22886
- if (agg) {
22887
- actionsCache.set(group, [...actionSet]);
22888
- if (agg.searchText !== void 0) {
22889
- haystackCache.set(group, buildHaystack(group, agg.searchText));
22890
- }
23032
+ actionsCache.set(type, [...actionSet]);
23033
+ if (agg.searchText !== void 0) {
23034
+ haystackCache.set(type, buildHaystack(type, agg.searchText));
22891
23035
  }
22892
- groups.push(group);
23036
+ types.push(type);
22893
23037
  }
22894
- return groups;
23038
+ return types;
22895
23039
  }
22896
23040
  var haystackCache = /* @__PURE__ */ new WeakMap();
22897
- function buildHaystack(g, extra) {
23041
+ function buildHaystack(t, extra) {
22898
23042
  return [
22899
- g.subtype,
22900
- g.category,
22901
- g.match.maskedValue,
22902
- g.policy.name,
22903
- g.id,
22904
- ...g.instances.map((i) => i.repo),
22905
- ...g.instances.map((i) => i.file),
22906
- ...g.instances.map((i) => i.toolName ? `via ${i.toolName}` : ""),
22907
- ...g.instances.map((i) => i.id),
22908
- // The people: the whole group's list when the store folded one, plus the
22909
- // preview's own — the two overlap, and a haystack does not mind.
22910
- ...(g.users ?? []).map((u) => u.name),
22911
- ...g.instances.map((i) => i.user?.name ?? ""),
23043
+ t.subtype,
23044
+ t.category,
23045
+ t.policy.name,
23046
+ t.id,
23047
+ ...(t.users ?? []).map((u) => u.name),
22912
23048
  ...extra === void 0 ? [] : [extra]
22913
23049
  ].join(" ").toLowerCase();
22914
23050
  }
22915
- function groupHaystack(g) {
22916
- const cached2 = haystackCache.get(g);
23051
+ function typeHaystack(t) {
23052
+ const cached2 = haystackCache.get(t);
22917
23053
  if (cached2 !== void 0) return cached2;
22918
- const haystack = buildHaystack(g);
22919
- haystackCache.set(g, haystack);
23054
+ const haystack = buildHaystack(t);
23055
+ haystackCache.set(t, haystack);
22920
23056
  return haystack;
22921
23057
  }
22922
23058
  var actionsCache = /* @__PURE__ */ new WeakMap();
22923
- function groupActions(g) {
22924
- const cached2 = actionsCache.get(g);
22925
- if (cached2 !== void 0) return cached2;
22926
- const actions = [...new Set(g.instances.map((i) => i.action))];
22927
- actionsCache.set(g, actions);
22928
- return actions;
23059
+ function typeActions(t) {
23060
+ return actionsCache.get(t) ?? [];
22929
23061
  }
22930
23062
  function countInstancesByStatus(statusInputs, statuses) {
22931
23063
  const statusSet = new Set(statuses);
@@ -22936,8 +23068,8 @@ function countInstancesByStatus(statusInputs, statuses) {
22936
23068
  }
22937
23069
  return sum;
22938
23070
  }
22939
- function applyFindingFilters(groups, opts) {
22940
- let filtered = groups;
23071
+ function applyFindingFilters(types, opts) {
23072
+ let filtered = types;
22941
23073
  if (opts.severity && opts.severity.length > 0) {
22942
23074
  const sevSet = new Set(opts.severity);
22943
23075
  filtered = filtered.filter((g) => sevSet.has(g.severity));
@@ -22948,7 +23080,7 @@ function applyFindingFilters(groups, opts) {
22948
23080
  }
22949
23081
  if (opts.actions && opts.actions.length > 0) {
22950
23082
  const actionSet = new Set(opts.actions);
22951
- filtered = filtered.filter((g) => groupActions(g).some((a) => actionSet.has(a)));
23083
+ filtered = filtered.filter((t) => typeActions(t).some((a) => actionSet.has(a)));
22952
23084
  }
22953
23085
  if (opts.subtype && opts.subtype.length > 0) {
22954
23086
  const subtypeSet = new Set(opts.subtype);
@@ -22960,26 +23092,31 @@ function applyFindingFilters(groups, opts) {
22960
23092
  }
22961
23093
  if (opts.q) {
22962
23094
  const q = opts.q.toLowerCase();
22963
- filtered = filtered.filter((g) => groupHaystack(g).includes(q));
23095
+ filtered = filtered.filter((t) => typeHaystack(t).includes(q));
22964
23096
  }
22965
23097
  return filtered;
22966
23098
  }
22967
- var SEVERITY_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
22968
- var SEVERITY_RANK = SEVERITY_ORDER;
23099
+ function rankByOrder(members2) {
23100
+ return Object.fromEntries(members2.map((member, index) => [member, index]));
23101
+ }
23102
+ var SEVERITY_RANK = rankByOrder(Severity.options);
23103
+ function severityRank(severity) {
23104
+ return lookupOwn(SEVERITY_RANK, severity);
23105
+ }
22969
23106
  function compareFindingGroupOrder(a, b) {
22970
- const rankA = SEVERITY_RANK[a.severity] ?? -1;
22971
- const rankB = SEVERITY_RANK[b.severity] ?? -1;
23107
+ const rankA = severityRank(a.severity) ?? -1;
23108
+ const rankB = severityRank(b.severity) ?? -1;
22972
23109
  const severityDiff = rankA - rankB;
22973
23110
  if (severityDiff !== 0) return severityDiff;
22974
23111
  const recencyDiff = b.latestDetectedAt.localeCompare(a.latestDetectedAt);
22975
23112
  if (recencyDiff !== 0) return recencyDiff;
22976
23113
  return a.id.localeCompare(b.id);
22977
23114
  }
22978
- function sortFindingGroups(groups) {
22979
- return [...groups].sort(compareFindingGroupOrder);
23115
+ function sortFindingTypes(types) {
23116
+ return [...types].sort(compareFindingGroupOrder);
22980
23117
  }
22981
- function computeFindingFacets(allGroups, opts) {
22982
- const forSeverity = applyFindingFilters(allGroups, {
23118
+ function computeFindingFacets(allTypes, opts) {
23119
+ const forSeverity = applyFindingFilters(allTypes, {
22983
23120
  providers: opts.providers,
22984
23121
  actions: opts.actions,
22985
23122
  statuses: opts.statuses,
@@ -22990,7 +23127,7 @@ function computeFindingFacets(allGroups, opts) {
22990
23127
  for (const g of forSeverity) {
22991
23128
  severityMap.set(g.severity, (severityMap.get(g.severity) ?? 0) + 1);
22992
23129
  }
22993
- const forProvider = applyFindingFilters(allGroups, {
23130
+ const forProvider = applyFindingFilters(allTypes, {
22994
23131
  actions: opts.actions,
22995
23132
  statuses: opts.statuses,
22996
23133
  q: opts.q,
@@ -23001,7 +23138,7 @@ function computeFindingFacets(allGroups, opts) {
23001
23138
  for (const g of forProvider) {
23002
23139
  for (const p of g.providers) providerMap.set(p, (providerMap.get(p) ?? 0) + 1);
23003
23140
  }
23004
- const forAction = applyFindingFilters(allGroups, {
23141
+ const forAction = applyFindingFilters(allTypes, {
23005
23142
  providers: opts.providers,
23006
23143
  statuses: opts.statuses,
23007
23144
  q: opts.q,
@@ -23010,9 +23147,9 @@ function computeFindingFacets(allGroups, opts) {
23010
23147
  });
23011
23148
  const actionMap = /* @__PURE__ */ new Map();
23012
23149
  for (const g of forAction) {
23013
- for (const a of groupActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23150
+ for (const a of typeActions(g)) actionMap.set(a, (actionMap.get(a) ?? 0) + 1);
23014
23151
  }
23015
- const forSubtype = applyFindingFilters(allGroups, {
23152
+ const forSubtype = applyFindingFilters(allTypes, {
23016
23153
  providers: opts.providers,
23017
23154
  actions: opts.actions,
23018
23155
  statuses: opts.statuses,
@@ -23021,7 +23158,7 @@ function computeFindingFacets(allGroups, opts) {
23021
23158
  });
23022
23159
  const subtypeMap = /* @__PURE__ */ new Map();
23023
23160
  for (const g of forSubtype) subtypeMap.set(g.subtype, (subtypeMap.get(g.subtype) ?? 0) + 1);
23024
- const forStatus = applyFindingFilters(allGroups, {
23161
+ const forStatus = applyFindingFilters(allTypes, {
23025
23162
  providers: opts.providers,
23026
23163
  actions: opts.actions,
23027
23164
  q: opts.q,
@@ -23043,6 +23180,20 @@ function computeFindingFacets(allGroups, opts) {
23043
23180
  }
23044
23181
 
23045
23182
  // ../../packages/schema/src/zod/findings-flat-build.ts
23183
+ function compareCodePoints(a, b) {
23184
+ const aIter = a[Symbol.iterator]();
23185
+ const bIter = b[Symbol.iterator]();
23186
+ for (; ; ) {
23187
+ const aNext = aIter.next();
23188
+ const bNext = bIter.next();
23189
+ if (aNext.done && bNext.done) return 0;
23190
+ if (aNext.done) return -1;
23191
+ if (bNext.done) return 1;
23192
+ const aPoint = aNext.value.codePointAt(0) ?? 0;
23193
+ const bPoint = bNext.value.codePointAt(0) ?? 0;
23194
+ if (aPoint !== bPoint) return aPoint - bPoint;
23195
+ }
23196
+ }
23046
23197
  function rowHaystack(row) {
23047
23198
  return [
23048
23199
  row.ruleId,
@@ -23067,12 +23218,24 @@ function matchesDimension(row, opts, dimension) {
23067
23218
  return !opts.actions?.length || opts.actions.includes(toApiAction(row.actionTaken));
23068
23219
  case "statuses":
23069
23220
  return !opts.statuses?.length || row.status !== void 0 && opts.statuses.includes(row.status);
23221
+ case "deliveries":
23222
+ return !opts.deliveries?.length || row.delivery !== void 0 && opts.deliveries.includes(row.delivery.state);
23070
23223
  case "tools":
23071
23224
  return !opts.tools?.length || row.toolName !== void 0 && opts.tools.includes(row.toolName);
23225
+ // An EMPTY value is a real filter here, not an absent one. The location
23226
+ // list buckets a finding whose event recorded no repo — or no file — under
23227
+ // the empty string, and selecting that bucket has to narrow the panel to
23228
+ // exactly it. Only `undefined` means "no filter"; a caller that wants every
23229
+ // row omits the key, which every call site already does.
23230
+ //
23231
+ // Reading '' as unset is what this replaced, and it failed in the one place
23232
+ // it mattered: the no-repo/no-file bucket is often the largest in a real
23233
+ // store, and its panel dropped both predicates and returned the WHOLE scope
23234
+ // — a row reading 3 findings beside a panel listing every finding there is.
23072
23235
  case "repo":
23073
- return opts.repo === void 0 || opts.repo === "" || row.repo === opts.repo;
23236
+ return opts.repo === void 0 || row.repo === opts.repo;
23074
23237
  case "file":
23075
- return opts.file === void 0 || opts.file === "" || row.file === opts.file;
23238
+ return opts.file === void 0 || row.file === opts.file;
23076
23239
  case "q":
23077
23240
  return !opts.q || rowHaystack(row).includes(opts.q.toLowerCase());
23078
23241
  }
@@ -23083,6 +23246,7 @@ var DIMENSIONS = [
23083
23246
  "providers",
23084
23247
  "actions",
23085
23248
  "statuses",
23249
+ "deliveries",
23086
23250
  "tools",
23087
23251
  "repo",
23088
23252
  "file",
@@ -23096,10 +23260,19 @@ function matchesInstanceFilters(row, opts, except) {
23096
23260
  return true;
23097
23261
  }
23098
23262
  function toItems(counts) {
23099
- return [...counts.entries()].map(([value, count]) => ({ value, count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
23263
+ return [...counts.entries()].map(([value, count]) => ({ value, count })).sort(
23264
+ (a, b) => b.count - a.count || a.value.localeCompare(b.value) || // localeCompare reports canonically-equivalent strings (an NFC and an
23265
+ // NFD spelling of the same text) as equal, so a count tie between
23266
+ // them would otherwise be ordered by whichever the Map iteration
23267
+ // produced. compareCodePoints breaks that tie deterministically, which
23268
+ // makes this a TOTAL order — not one that agrees with SQL collation,
23269
+ // which it need not: foldFacetTuples runs this same sort over grouped
23270
+ // tuples, so both paths order facets identically by construction.
23271
+ compareCodePoints(a.value, b.value)
23272
+ );
23100
23273
  }
23101
- function bump(counts, value) {
23102
- counts.set(value, (counts.get(value) ?? 0) + 1);
23274
+ function bump(counts, value, by = 1) {
23275
+ counts.set(value, (counts.get(value) ?? 0) + by);
23103
23276
  }
23104
23277
  function createInstanceFacetAccumulator(opts) {
23105
23278
  const severity = /* @__PURE__ */ new Map();
@@ -23108,6 +23281,7 @@ function createInstanceFacetAccumulator(opts) {
23108
23281
  const action = /* @__PURE__ */ new Map();
23109
23282
  const status = /* @__PURE__ */ new Map();
23110
23283
  const tool = /* @__PURE__ */ new Map();
23284
+ const deployment = /* @__PURE__ */ new Map();
23111
23285
  return {
23112
23286
  add(row) {
23113
23287
  if (matchesInstanceFilters(row, opts, "severity")) bump(severity, row.severity);
@@ -23122,6 +23296,9 @@ function createInstanceFacetAccumulator(opts) {
23122
23296
  if (row.toolName !== void 0 && matchesInstanceFilters(row, opts, "tools")) {
23123
23297
  bump(tool, row.toolName);
23124
23298
  }
23299
+ if (row.delivery !== void 0 && matchesInstanceFilters(row, opts, "deliveries")) {
23300
+ bump(deployment, row.delivery.state);
23301
+ }
23125
23302
  },
23126
23303
  facets: () => ({
23127
23304
  severity: toItems(severity),
@@ -23129,7 +23306,8 @@ function createInstanceFacetAccumulator(opts) {
23129
23306
  provider: toItems(provider),
23130
23307
  action: toItems(action),
23131
23308
  status: toItems(status),
23132
- tool: toItems(tool)
23309
+ tool: toItems(tool),
23310
+ deployment: toItems(deployment)
23133
23311
  })
23134
23312
  };
23135
23313
  }
@@ -23143,6 +23321,7 @@ function toInstanceDetail(row) {
23143
23321
  ...row.toolName === void 0 ? {} : { toolName: row.toolName },
23144
23322
  eventId: row.eventId,
23145
23323
  ...row.sessionId === void 0 ? {} : { sessionId: row.sessionId },
23324
+ ...row.delivery === void 0 ? {} : { delivery: row.delivery },
23146
23325
  ...row.user === void 0 ? {} : { user: row.user },
23147
23326
  action: toApiAction(row.actionTaken),
23148
23327
  detectedAt: row.occurredAt,
@@ -23157,12 +23336,6 @@ function toInstanceDetail(row) {
23157
23336
  policy: { id: `category:${category}`, name: category }
23158
23337
  };
23159
23338
  }
23160
- var SEVERITY_ORDER2 = {
23161
- critical: 0,
23162
- high: 1,
23163
- medium: 2,
23164
- low: 3
23165
- };
23166
23339
  function newLocationAccumulator() {
23167
23340
  return {
23168
23341
  instanceCount: 0,
@@ -23177,7 +23350,7 @@ function newLocationAccumulator() {
23177
23350
  }
23178
23351
  function addToLocation(acc, row) {
23179
23352
  acc.instanceCount += 1;
23180
- const rank = SEVERITY_ORDER2[row.severity] ?? Number.MAX_SAFE_INTEGER - 1;
23353
+ const rank = severityRank(row.severity) ?? Number.MAX_SAFE_INTEGER - 1;
23181
23354
  if (rank < acc.maxSeverityRank) {
23182
23355
  acc.maxSeverityRank = rank;
23183
23356
  acc.maxSeverity = row.severity;
@@ -23186,6 +23359,23 @@ function addToLocation(acc, row) {
23186
23359
  acc.statuses.push(row.status);
23187
23360
  acc.ruleIds.add(row.ruleId);
23188
23361
  }
23362
+ function compareLocationOrder(a, b) {
23363
+ const rankA = severityRank(a.maxSeverity) ?? -1;
23364
+ const rankB = severityRank(b.maxSeverity) ?? -1;
23365
+ if (rankA !== rankB) return rankA - rankB;
23366
+ if (a.latestDetectedAt !== b.latestDetectedAt) {
23367
+ return a.latestDetectedAt < b.latestDetectedAt ? 1 : -1;
23368
+ }
23369
+ const repoDiff = compareCodePoints(a.repo, b.repo);
23370
+ if (repoDiff !== 0) return repoDiff;
23371
+ return compareCodePoints(a.file, b.file);
23372
+ }
23373
+ function encodeLocationId(repo, file2) {
23374
+ return `${encodePart(repo)}/${encodePart(file2)}`;
23375
+ }
23376
+ function encodePart(value) {
23377
+ return encodeURIComponent(value.replace(/[\uD800-\uDFFF]/gu, "\uFFFD"));
23378
+ }
23189
23379
 
23190
23380
  // ../../packages/schema/src/zod/installed-pack.ts
23191
23381
  var InstalledPack = external_exports.object({
@@ -23253,6 +23443,11 @@ var Policy = external_exports.object({
23253
23443
  // test `prohibitedModels` passes and `reversibleRuleIds` fails.
23254
23444
  provenance: PolicyProvenance.optional()
23255
23445
  }).meta({ id: "Policy" });
23446
+ var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23447
+ var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23448
+ var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23449
+ id: "RedactFallback"
23450
+ });
23256
23451
  var PolicyBundle = external_exports.object({
23257
23452
  version: external_exports.string(),
23258
23453
  policies: external_exports.array(Policy),
@@ -23300,6 +23495,16 @@ var PolicyBundle = external_exports.object({
23300
23495
  // control plane), so no name resolution stands between the decision and the
23301
23496
  // comparison.
23302
23497
  prohibitedModels: external_exports.array(external_exports.string()).optional(),
23498
+ // What a resolved `redact` becomes on a field the host cannot rewrite, as
23499
+ // the ORGANIZATION would have it. Merged raise-only against the device's own
23500
+ // `WorkspaceSettings.redactFallback` (see strongerRedactFallback below), so
23501
+ // a control plane can tighten a machine and never loosen one — the same
23502
+ // direction `mergeRaiseOnly` enforces for policies.
23503
+ //
23504
+ // Optional so an older backend, and an older on-disk cache, still parses;
23505
+ // absent leaves the device's own setting in force, which is the behaviour
23506
+ // that predates the field and the safe direction to default.
23507
+ redactFallback: RedactFallback.optional(),
23303
23508
  customKeywords: external_exports.array(external_exports.string()),
23304
23509
  fetchedAt: external_exports.iso.datetime()
23305
23510
  }).meta({ id: "PolicyBundle" });
@@ -23329,11 +23534,6 @@ function severityFloorPolicy(category) {
23329
23534
  const peak = CATEGORY_PEAK_SEVERITY[category];
23330
23535
  return peak === "critical" || peak === "high" ? "warn" : "monitor";
23331
23536
  }
23332
- var KNOWN_BUILTIN_IDS = ["monitor", "warn", "redact", "vault", "block"];
23333
- var BuiltinPolicyId = external_exports.enum(KNOWN_BUILTIN_IDS).meta({ id: "BuiltinPolicyId" });
23334
- var RedactFallback = BuiltinPolicyId.extract(["monitor", "warn", "block"]).meta({
23335
- id: "RedactFallback"
23336
- });
23337
23537
  var BUILTIN_ORDER = KNOWN_BUILTIN_IDS;
23338
23538
  var BUILTIN_POLICY_SPECS = {
23339
23539
  monitor: {
@@ -23626,9 +23826,9 @@ var VaultConsent = external_exports.object({
23626
23826
  });
23627
23827
 
23628
23828
  // ../../packages/schema/src/zod/local.ts
23629
- var WORKSPACE_SETTINGS_SPEC_VERSION = 7;
23829
+ var WORKSPACE_SETTINGS_SPEC_VERSION = 8;
23630
23830
  var MODEL_JUDGE_PAYLOAD_VERSION = 1;
23631
- var HISTORY_SYNC_PAYLOAD_VERSION = 2;
23831
+ var HISTORY_SYNC_PAYLOAD_VERSION = 3;
23632
23832
  var RunMode = external_exports.enum(["standalone", "attached"]);
23633
23833
  var ControlPlaneConnection = external_exports.object({
23634
23834
  endpoint: external_exports.string().min(1),
@@ -23651,6 +23851,15 @@ function isHistorySyncConsentValid(consent, endpoint) {
23651
23851
  if (consent === void 0 || endpoint === void 0) return false;
23652
23852
  return consent.payloadVersion === HISTORY_SYNC_PAYLOAD_VERSION && consent.endpoint === endpoint;
23653
23853
  }
23854
+ var BODY_RETENTION_DEFAULT_DAYS = 30;
23855
+ var BodyRetention = external_exports.object({
23856
+ enabled: external_exports.boolean().default(false),
23857
+ // Never 0, and the ceiling is a fat-finger guard rather than a policy
23858
+ // limit — `enabled` is the real gate. A low value cannot reach a row the
23859
+ // sync ledger still owes: the sweep's age filter only ever NARROWS a
23860
+ // candidate set that is already bounded by "delivered, or never owed".
23861
+ retainDays: external_exports.number().int().min(1).max(3650).default(BODY_RETENTION_DEFAULT_DAYS)
23862
+ }).meta({ id: "BodyRetention" });
23654
23863
  var WorkspaceSettings = external_exports.object({
23655
23864
  specVersion: external_exports.number().int().positive().default(WORKSPACE_SETTINGS_SPEC_VERSION),
23656
23865
  runMode: RunMode.default("standalone"),
@@ -23694,12 +23903,18 @@ var WorkspaceSettings = external_exports.object({
23694
23903
  // covers the current payload and must be re-granted.
23695
23904
  modelJudgeConsent: ModelJudgeConsent.optional(),
23696
23905
  // Records that the user consented to the DEFERRED send — the outbox — along
23697
- // with the payload shape and the endpoint they agreed to. Since payload v2
23698
- // that covers both the pre-attach backlog and undelivered captures (which
23699
- // carry prompt/reply text in `content`); the key name predates the widening.
23700
- // Absent until granted, and a grant for a different endpoint or an older
23701
- // payload no longer counts.
23702
- historySyncConsent: HistorySyncConsent.optional()
23906
+ // with the payload shape and the endpoint they agreed to. Since payload v3
23907
+ // that covers the pre-attach backlog AND undelivered captures alike, and both
23908
+ // carry prompt/reply/tool-output text in `content`; the key name predates
23909
+ // both widenings. Absent until granted, and a grant for a different endpoint
23910
+ // or an older payload no longer counts.
23911
+ historySyncConsent: HistorySyncConsent.optional(),
23912
+ // Local body expiry (see BodyRetention). Off until switched on; expiring a
23913
+ // body never removes the row or its findings.
23914
+ bodyRetention: BodyRetention.default({
23915
+ enabled: false,
23916
+ retainDays: BODY_RETENTION_DEFAULT_DAYS
23917
+ })
23703
23918
  });
23704
23919
  function defaultWorkspaceSettings() {
23705
23920
  return WorkspaceSettings.parse({});
@@ -23794,12 +24009,15 @@ function toCaptureAttributes(event) {
23794
24009
  ...metadata?.traceId !== void 0 ? { trace_id: metadata.traceId } : {},
23795
24010
  ...metadata?.exceptionIds !== void 0 ? { exception_ids: metadata.exceptionIds } : {},
23796
24011
  ...metadata?.inspectionMs !== void 0 ? { inspection_ms: metadata.inspectionMs } : {},
24012
+ ...metadata?.redactDegradedTo !== void 0 ? { redact_degraded_to: metadata.redactDegradedTo } : {},
23797
24013
  // `model`/`turnIndex` have no dedicated CaptureAttributes field (no writer
23798
24014
  // has ever populated either), but every legacy metadata key still rides
23799
24015
  // the bag rather than being silently dropped — CaptureAttributes'
23800
24016
  // `.catchall(z.unknown())` carries the long tail.
23801
24017
  ...metadata?.model !== void 0 ? { model: metadata.model } : {},
23802
- ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {}
24018
+ ...metadata?.turnIndex !== void 0 ? { turn_index: metadata.turnIndex } : {},
24019
+ ...metadata?.messageId !== void 0 ? { message_id: metadata.messageId } : {},
24020
+ ...metadata?.conversationId !== void 0 ? { conversation_id: metadata.conversationId } : {}
23803
24021
  };
23804
24022
  }
23805
24023
  function captureDefinitionVersion(finding) {
@@ -23827,10 +24045,22 @@ var ManagedSettingKey = external_exports.enum([
23827
24045
  "vaultInlineReveal",
23828
24046
  "modelJudgeConsent",
23829
24047
  "dataSharesInPlace",
23830
- "redactFallback"
24048
+ "redactFallback",
24049
+ // Pins the toggle and the day count together — see BodyRetention on why the
24050
+ // two are one unit. An administrator mandating a window wants the count
24051
+ // enforced with it, not one a user can widen while the toggle stays on.
24052
+ "bodyRetention"
23831
24053
  ]).meta({ id: "ManagedSettingKey" });
24054
+ function isManagedSettingKey(value) {
24055
+ return ManagedSettingKey.safeParse(value).success;
24056
+ }
23832
24057
  var ManagedSettingsValues = external_exports.object({
23833
24058
  runMode: external_exports.enum(["standalone", "attached"]).optional(),
24059
+ // `controlPlane` and `bodyRetention` are the two nested values, and both are
24060
+ // plain, non-strict objects: a key under either that this build does not know
24061
+ // is stripped and nothing reports it. The unknown-value split in
24062
+ // ManagedSettings below classifies top-level names only, so it stops at
24063
+ // these boundaries.
23834
24064
  controlPlane: external_exports.object({
23835
24065
  endpoint: external_exports.string().min(1),
23836
24066
  label: external_exports.string().min(1).optional()
@@ -23841,7 +24071,8 @@ var ManagedSettingsValues = external_exports.object({
23841
24071
  vaultInlineReveal: external_exports.enum(["masked", "full", "off"]).optional(),
23842
24072
  modelJudgeConsent: external_exports.boolean().optional(),
23843
24073
  dataSharesInPlace: external_exports.boolean().optional(),
23844
- redactFallback: RedactFallback.optional()
24074
+ redactFallback: RedactFallback.optional(),
24075
+ bodyRetention: BodyRetention.optional()
23845
24076
  }).meta({ id: "ManagedSettingsValues" });
23846
24077
  var ManagedSettings = external_exports.object({
23847
24078
  specVersion: external_exports.number().int().positive().default(MANAGED_SETTINGS_SPEC_VERSION),
@@ -23849,11 +24080,59 @@ var ManagedSettings = external_exports.object({
23849
24080
  // decision from a bug. Absent renders as a generic "your organization".
23850
24081
  organization: external_exports.string().min(1).optional(),
23851
24082
  // What the administrator pinned.
23852
- values: ManagedSettingsValues.default({}),
24083
+ //
24084
+ // Parsed as a RECORD rather than as the nested schema, and split below for
24085
+ // the same reason `lockedFields` is parsed as names: a plain `z.object`
24086
+ // drops an unrecognised key and succeeds, so a pin this build does not know
24087
+ // vanished and nothing anywhere said so. A pin with no lock is a supported
24088
+ // shape — it is a DEFAULT the user may still change — so that silence hit
24089
+ // exactly the file an administrator is most likely to write while a fleet
24090
+ // is mid-upgrade.
24091
+ //
24092
+ // Splitting here rather than calling `.strict()`: strict would REFUSE the
24093
+ // file, which is the outcome the lock half already rejected — an older
24094
+ // build then runs entirely unmanaged, every pin and lock gone. A bad KNOWN
24095
+ // value still fails, because the nested schema is re-run over the known
24096
+ // subset and its issues are re-raised on this parse.
24097
+ values: external_exports.record(external_exports.string(), external_exports.unknown()).default({}),
23853
24098
  // Which of those the user may not change. A key here with no matching value
23854
24099
  // freezes whatever the user last chose; a value with no lock is a DEFAULT
23855
24100
  // the user may still override. The two are separable on purpose.
23856
- lockedFields: external_exports.array(ManagedSettingKey).default([])
24101
+ //
24102
+ // Parsed as NAMES rather than as the enum, and split below: a name this
24103
+ // build does not know is dropped from the locked set and reported, never a
24104
+ // reason to refuse the file. The same shape reaches an older build whenever
24105
+ // an administrator locks a key a newer build added, and refusing it there
24106
+ // ran that build entirely unmanaged — every pin and lock gone — on exactly
24107
+ // the fleets most likely to carry a version skew. A name outside the enum
24108
+ // is still never HONOURED: the lockable set stays explicit above.
24109
+ lockedFields: external_exports.array(external_exports.string()).default([])
24110
+ }).transform(({ lockedFields, values, ...rest }, ctx) => {
24111
+ const known = [];
24112
+ const unknown2 = [];
24113
+ for (const name of lockedFields) {
24114
+ if (isManagedSettingKey(name)) known.push(name);
24115
+ else unknown2.push(name);
24116
+ }
24117
+ const knownValues = /* @__PURE__ */ Object.create(null);
24118
+ const unknownValues = [];
24119
+ for (const [name, value] of Object.entries(values)) {
24120
+ if (Object.hasOwn(ManagedSettingsValues.shape, name)) knownValues[name] = value;
24121
+ else unknownValues.push(name);
24122
+ }
24123
+ const pinned = ManagedSettingsValues.safeParse(knownValues);
24124
+ if (!pinned.success) {
24125
+ for (const issue2 of pinned.error.issues)
24126
+ ctx.addIssue({ ...issue2, path: ["values", ...issue2.path] });
24127
+ return external_exports.NEVER;
24128
+ }
24129
+ return {
24130
+ ...rest,
24131
+ values: pinned.data,
24132
+ lockedFields: known,
24133
+ ...unknown2.length > 0 ? { unknownLockedFields: unknown2 } : {},
24134
+ ...unknownValues.length > 0 ? { unknownValueFields: unknownValues } : {}
24135
+ };
23857
24136
  }).meta({ id: "ManagedSettings" });
23858
24137
 
23859
24138
  // ../../packages/schema/src/zod/project-files.ts
@@ -23977,7 +24256,11 @@ var FindingsTimeseriesPoint = external_exports.object({
23977
24256
  timestamp: external_exports.iso.date(),
23978
24257
  critical: external_exports.number().int().nonnegative(),
23979
24258
  high: external_exports.number().int().nonnegative(),
23980
- medium: external_exports.number().int().nonnegative()
24259
+ medium: external_exports.number().int().nonnegative(),
24260
+ // Optional and additive, so a producer written against the earlier
24261
+ // three-series contract keeps validating. A consumer plotting it resolves the
24262
+ // absent case itself — the chart point requires a number.
24263
+ low: external_exports.number().int().nonnegative().optional()
23981
24264
  }).meta({ id: "FindingsTimeseriesPoint" });
23982
24265
  var FindingsTimeseriesResponse = external_exports.object({
23983
24266
  range: TimeRange,
@@ -24003,6 +24286,10 @@ var ResolvedFeedItem = external_exports.object({
24003
24286
  findingKey: external_exports.string(),
24004
24287
  ruleId: external_exports.string(),
24005
24288
  severity: Severity,
24289
+ // Repository slug, and the file path RELATIVE to it. The pair is what
24290
+ // identifies the file: a bare path matches the same name in every repo.
24291
+ // Optional and additive; empty when the event carried no repo.
24292
+ repo: external_exports.string().optional(),
24006
24293
  path: external_exports.string(),
24007
24294
  // ISO-8601 datetime (matches FindingInstance.detectedAt / the rest of the
24008
24295
  // findings domain). The reader `.toISOString()`s the DB epoch-ms values.
@@ -24108,7 +24395,23 @@ var SaveSettingsInput = external_exports.object({
24108
24395
  modelJudgeConsent: ModelJudgeConsentChoice,
24109
24396
  historySyncConsent: HistorySyncConsentChoice,
24110
24397
  vaultConsent: external_exports.string(),
24111
- vaultInlineReveal: external_exports.string()
24398
+ vaultInlineReveal: external_exports.string(),
24399
+ // Widened to `string` like its neighbours rather than typed as
24400
+ // `RedactFallback`, on this module's own layering rule: shape here, VALUE at
24401
+ // the call site, so the domain check receives the type it was written for.
24402
+ //
24403
+ // NOT because a narrower schema would reject differently. `parseActionInput`
24404
+ // is a `safeParse` wrapper and throws for no field schema, so either spelling
24405
+ // reaches a recoverable `{ ok: false }` and there is no rejected promise to
24406
+ // trade against. The real cost runs the other way and is the part worth
24407
+ // knowing: a value this schema admits and the domain enum then rejects lands
24408
+ // on the action's shared refusal, which names NO field, where a shape
24409
+ // rejection reaches `malformedInput` and names the schema key.
24410
+ redactFallback: external_exports.string(),
24411
+ // Shape only, the way the enum fields above are strings only: the RANGE is
24412
+ // `BodyRetention`'s and the action checks it there, so there is one place
24413
+ // that decides what a legal horizon is rather than two that can drift.
24414
+ bodyRetention: external_exports.object({ enabled: external_exports.boolean(), retainDays: external_exports.number() })
24112
24415
  });
24113
24416
  var AttachInput = external_exports.object({
24114
24417
  endpoint: external_exports.string(),
@@ -24280,6 +24583,52 @@ function reviewSeverityRank(reasons) {
24280
24583
  return Math.min(...reasons.map((r) => REVIEW_SEVERITY_RANK[r]));
24281
24584
  }
24282
24585
 
24586
+ // ../../packages/schema/src/zod/web-capture.ts
24587
+ var WebUsageSource = external_exports.enum(["site", "estimated", "none"]);
24588
+ var WebUsage = external_exports.object({
24589
+ inputTokens: external_exports.number().int().nonnegative().optional(),
24590
+ outputTokens: external_exports.number().int().nonnegative().optional(),
24591
+ cacheReadInputTokens: external_exports.number().int().nonnegative().optional(),
24592
+ cacheCreationInputTokens: external_exports.number().int().nonnegative().optional()
24593
+ });
24594
+ var WebToolCall = external_exports.object({
24595
+ toolUseId: external_exports.string().min(1),
24596
+ toolName: external_exports.string().min(1),
24597
+ target: external_exports.string().optional(),
24598
+ isError: external_exports.boolean().optional(),
24599
+ inputSize: external_exports.number().int().nonnegative().optional(),
24600
+ outputSize: external_exports.number().int().nonnegative().optional()
24601
+ });
24602
+ var WebExchange = external_exports.object({
24603
+ messageId: external_exports.string().min(1),
24604
+ startedAt: external_exports.iso.datetime(),
24605
+ model: external_exports.string().optional(),
24606
+ usage: WebUsage.optional(),
24607
+ usageSource: WebUsageSource,
24608
+ stopReason: external_exports.string().optional(),
24609
+ conversationId: external_exports.string().optional(),
24610
+ turnIndex: external_exports.number().int().nonnegative().optional(),
24611
+ toolCalls: external_exports.array(WebToolCall).default([]),
24612
+ // Absent when the adapter recovered no text. Capped by the caller at
24613
+ // RESPONSE_TEXT_MAX_BYTES; `truncated` records that the cap was reached, so a
24614
+ // short capture is never mistaken for a short reply.
24615
+ responseText: external_exports.string().optional(),
24616
+ truncated: external_exports.boolean().default(false)
24617
+ });
24618
+ var RESPONSE_TEXT_MAX_BYTES = 2 * 1024 * 1024;
24619
+ var WebCaptureStatus = external_exports.object({
24620
+ patched: external_exports.boolean(),
24621
+ live: external_exports.boolean(),
24622
+ blind: external_exports.boolean(),
24623
+ sendsSeenDom: external_exports.number().int().nonnegative(),
24624
+ exchangesSeenNet: external_exports.number().int().nonnegative(),
24625
+ parseFailures: external_exports.number().int().nonnegative(),
24626
+ unparsedBodies: external_exports.number().int().nonnegative(),
24627
+ // The adapter-declared JSON key paths that were absent from a real payload —
24628
+ // the earliest signal that a site's contract moved.
24629
+ shapeMisses: external_exports.array(external_exports.string()).default([])
24630
+ });
24631
+
24283
24632
  // ../../packages/persistence/src/paths.ts
24284
24633
  import {
24285
24634
  chmodSync,
@@ -24568,6 +24917,22 @@ function discardStore(file2, backup) {
24568
24917
  }
24569
24918
  }
24570
24919
 
24920
+ // ../../packages/persistence/src/internal/sql-functions.ts
24921
+ var utf8 = new TextDecoder();
24922
+ function akaLower(value) {
24923
+ if (value === null) return null;
24924
+ if (typeof value === "string") return value.toLowerCase();
24925
+ if (typeof value === "number" || typeof value === "bigint") return String(value).toLowerCase();
24926
+ return utf8.decode(value).toLowerCase();
24927
+ }
24928
+ function registerSqlFunctions(db) {
24929
+ db.function(
24930
+ "aka_lower",
24931
+ { deterministic: true, directOnly: true, useBigIntArguments: true },
24932
+ akaLower
24933
+ );
24934
+ }
24935
+
24571
24936
  // ../../packages/persistence/src/internal/sql-text.ts
24572
24937
  function escapeLikePattern(s) {
24573
24938
  return s.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
@@ -24652,6 +25017,11 @@ function schemaObjectExists(db, kind, name) {
24652
25017
  function indexExists(db, name) {
24653
25018
  return schemaObjectExists(db, "index", name);
24654
25019
  }
25020
+ function indexColumns(db, name) {
25021
+ if (!indexExists(db, name)) return [];
25022
+ const columns = db.prepare(`PRAGMA index_info(${name})`).all();
25023
+ return columns.map((c) => c.name).filter((c) => c !== null);
25024
+ }
24655
25025
  function columnNames(db, table, opts) {
24656
25026
  const pragma = opts?.includeGenerated ? "table_xinfo" : "table_info";
24657
25027
  const columns = db.prepare(`PRAGMA ${pragma}(${table})`).all();
@@ -24713,6 +25083,648 @@ function mapRowsTolerant(rows, map2) {
24713
25083
  return out;
24714
25084
  }
24715
25085
 
25086
+ // ../../packages/persistence/src/internal/outbox-lane.ts
25087
+ var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
25088
+ var OUTBOX_CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25089
+
25090
+ // ../../packages/persistence/src/sync-failure.ts
25091
+ var SYNC_FAILURE_REASONS = SyncFailureReason.options;
25092
+ function syncFailureRejectCondition(column = "sync_failure") {
25093
+ const members2 = SYNC_FAILURE_REASONS.map((r) => `'${r}'`).join(", ");
25094
+ return `NEW.${column} IS NOT NULL AND NEW.${column} NOT IN (${members2})`;
25095
+ }
25096
+
25097
+ // ../../packages/persistence/src/repositories/history-sync.ts
25098
+ var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
25099
+ var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25100
+ var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_TYPE_LIST;
25101
+ var COUNTED_EVENT_TYPES = [
25102
+ ...STRUCTURAL_EVENT_TYPES,
25103
+ ...OUTBOX_CAPTURE_EVENT_TYPES
25104
+ ];
25105
+ var COUNTED_TYPE_LIST = COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
25106
+ var PARTITION_BUCKETS = `
25107
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
25108
+ SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
25109
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
25110
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25111
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25112
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25113
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached,
25114
+ -- Spelled as what it INCLUDES rather than what it excludes, so a reason
25115
+ -- added later lands in no bucket and fails the sum assertion, instead
25116
+ -- of silently joining this one.
25117
+ SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0
25118
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25119
+ THEN 1 ELSE 0 END) AS failed,
25120
+ COUNT(*) AS total`;
25121
+ var COUNTED_SCOPE = `
25122
+ WHERE event_type IN (${COUNTED_TYPE_LIST})
25123
+ AND (
25124
+ event_type IN (${TYPE_LIST})
25125
+ OR synced_at IS NOT NULL
25126
+ OR outbox_owed = 1
25127
+ )`;
25128
+ var SKIPPED = -1;
25129
+ var HISTORY_SYNC_LEASE_STALE_MS = 6e4;
25130
+ var ROW_COLUMNS = `id,
25131
+ parent_id AS parentId,
25132
+ root_session_id AS rootSessionId,
25133
+ event_type AS eventType,
25134
+ host_id AS hostId,
25135
+ harness_id AS harnessId,
25136
+ source_project_id AS sourceProjectId,
25137
+ started_at AS startedAt,
25138
+ ended_at AS endedAt,
25139
+ severity,
25140
+ priority,
25141
+ content,
25142
+ content_hash AS contentHash,
25143
+ attributes`;
25144
+ var SqliteHistorySyncRepository = class {
25145
+ constructor(db) {
25146
+ this.db = db;
25147
+ this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
25148
+ this.sessionsStmt = db.prepare(
25149
+ `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
25150
+ FROM audit_events
25151
+ WHERE synced_at IS NULL
25152
+ AND event_type IN (${TYPE_LIST})
25153
+ AND started_at < :before
25154
+ GROUP BY sessionId
25155
+ ORDER BY earliest
25156
+ LIMIT :limit`
25157
+ );
25158
+ this.rowsStmt = db.prepare(
25159
+ `SELECT ${ROW_COLUMNS}
25160
+ FROM audit_events
25161
+ WHERE synced_at IS NULL
25162
+ AND event_type IN (${TYPE_LIST})
25163
+ AND started_at < :before
25164
+ AND COALESCE(root_session_id, id) = :sessionId
25165
+ ORDER BY (event_type = 'session') DESC, started_at
25166
+ LIMIT :limit`
25167
+ );
25168
+ this.captureRowsStmt = db.prepare(
25169
+ `SELECT ${ROW_COLUMNS}
25170
+ FROM audit_events
25171
+ WHERE synced_at IS NULL
25172
+ AND sync_claimed_at IS NULL
25173
+ AND outbox_owed = 1
25174
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25175
+ AND started_at < :before
25176
+ ORDER BY started_at
25177
+ LIMIT :limit`
25178
+ );
25179
+ this.markOwedStmt = db.prepare(
25180
+ `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
25181
+ );
25182
+ this.markCaptureBacklogOwedStmt = db.prepare(
25183
+ `UPDATE audit_events SET outbox_owed = 1
25184
+ WHERE synced_at IS NULL
25185
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25186
+ AND started_at < :before`
25187
+ );
25188
+ this.stampStmt = db.prepare(
25189
+ `UPDATE audit_events
25190
+ SET synced_at = :at,
25191
+ sync_claimed_at = NULL,
25192
+ sync_failed_at = :failedAt,
25193
+ sync_failure = :failure
25194
+ WHERE id = :id`
25195
+ );
25196
+ this.claimRowStmt = db.prepare(
25197
+ `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
25198
+ );
25199
+ this.releaseRowStmt = db.prepare(
25200
+ `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
25201
+ );
25202
+ this.releaseStaleClaimsStmt = db.prepare(
25203
+ `UPDATE audit_events SET sync_claimed_at = NULL
25204
+ WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
25205
+ );
25206
+ this.partitionStmt = db.prepare(`SELECT${PARTITION_BUCKETS}
25207
+ FROM audit_events${COUNTED_SCOPE}`);
25208
+ this.partitionByKindStmt = db.prepare(
25209
+ `SELECT event_type AS kind,${PARTITION_BUCKETS}
25210
+ FROM audit_events INDEXED BY idx_audit_events_sync${COUNTED_SCOPE}
25211
+ GROUP BY event_type`
25212
+ );
25213
+ this.countsStmt = db.prepare(
25214
+ `SELECT
25215
+ SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
25216
+ SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
25217
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25218
+ AND (sync_failure IS NULL OR sync_failure = 'payload_invalid')
25219
+ THEN 1 ELSE 0 END) AS skipped,
25220
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25221
+ AND sync_failure = 'deployment_refused' THEN 1 ELSE 0 END) AS refused,
25222
+ SUM(CASE WHEN synced_at = ${String(SKIPPED)}
25223
+ AND sync_failure = 'detached_undelivered' THEN 1 ELSE 0 END) AS detached
25224
+ FROM audit_events
25225
+ WHERE event_type IN (${TYPE_LIST})`
25226
+ );
25227
+ this.captureSkipCountStmt = db.prepare(
25228
+ // EVERY sentinel capture, whatever the reason — deliberately NOT split the
25229
+ // way the structural totals are. The split exists because a refusal is
25230
+ // terminal only against the deployment that gave it, and the structural
25231
+ // re-arm frees it on a change of deployment. The capture lane has no such
25232
+ // escape: re-arming a capture would offer one deployment's undelivered
25233
+ // prompts, with their text, to a deployment that never saw them, which is
25234
+ // exactly what disownCapturesStmt exists to prevent. So on this lane both
25235
+ // reasons mean the same thing — this row will not be sent — and splitting
25236
+ // them would put refused captures in a bucket nothing reads and nothing
25237
+ // frees.
25238
+ `SELECT COUNT(*) AS skipped
25239
+ FROM audit_events
25240
+ WHERE synced_at = ${String(SKIPPED)}
25241
+ AND event_type IN (${CAPTURE_TYPE_LIST})`
25242
+ );
25243
+ this.fingerprintStmt = db.prepare(
25244
+ `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
25245
+ FROM history_sync WHERE id = 1`
25246
+ );
25247
+ this.setFingerprintStmt = db.prepare(
25248
+ `UPDATE history_sync
25249
+ SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
25250
+ WHERE id = 1`
25251
+ );
25252
+ this.disownCapturesStmt = db.prepare(
25253
+ `UPDATE audit_events SET outbox_owed = NULL
25254
+ WHERE outbox_owed IS NOT NULL
25255
+ AND event_type IN (${CAPTURE_TYPE_LIST})
25256
+ AND started_at < :attachedAt`
25257
+ );
25258
+ this.rearmStmt = db.prepare(
25259
+ `UPDATE audit_events
25260
+ SET synced_at = NULL, sync_failed_at = NULL, sync_failure = NULL
25261
+ WHERE (synced_at > 0
25262
+ OR sync_failure IN ('deployment_refused', 'detached_undelivered'))
25263
+ AND event_type IN (${TYPE_LIST})`
25264
+ );
25265
+ this.claimStmt = db.prepare(
25266
+ `UPDATE history_sync
25267
+ SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
25268
+ WHERE id = 1
25269
+ AND (owner_pid IS NULL
25270
+ OR heartbeat_at IS NULL
25271
+ OR heartbeat_at < :staleBefore
25272
+ OR heartbeat_at > :now)`
25273
+ );
25274
+ this.heartbeatStmt = db.prepare(
25275
+ `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
25276
+ );
25277
+ this.releaseStmt = db.prepare(
25278
+ `UPDATE history_sync
25279
+ SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
25280
+ WHERE id = 1 AND owner_pid = :pid`
25281
+ );
25282
+ this.closeWindowStmt = db.prepare(
25283
+ `UPDATE audit_events
25284
+ SET synced_at = ${String(SKIPPED)},
25285
+ sync_failed_at = :at,
25286
+ sync_failure = 'detached_undelivered'
25287
+ WHERE synced_at IS NULL
25288
+ AND event_type IN (${TYPE_LIST})
25289
+ AND started_at >= :attachedAt`
25290
+ );
25291
+ this.releaseBoundaryStmt = db.prepare(
25292
+ `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
25293
+ );
25294
+ this.freezeBoundaryStmt = db.prepare(
25295
+ `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
25296
+ );
25297
+ this.leaseStmt = db.prepare(
25298
+ `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
25299
+ acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
25300
+ FROM history_sync WHERE id = 1`
25301
+ );
25302
+ this.inspectionsStmt = db.prepare(
25303
+ `SELECT d.rule_id AS ruleId,
25304
+ d.name AS ruleName,
25305
+ d.version AS ruleVersion,
25306
+ d.category AS category,
25307
+ d.severity AS severity,
25308
+ f.span_start AS spanStart,
25309
+ f.span_end AS spanEnd,
25310
+ f.masked_match AS maskedMatch,
25311
+ f.action_taken AS actionTaken,
25312
+ f.confidence AS confidence
25313
+ FROM inspection_findings f
25314
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
25315
+ WHERE f.audit_event_id = :auditEventId
25316
+ ORDER BY f.span_start, f.id`
25317
+ );
25318
+ }
25319
+ db;
25320
+ ensureRowStmt;
25321
+ sessionsStmt;
25322
+ rowsStmt;
25323
+ stampStmt;
25324
+ countsStmt;
25325
+ fingerprintStmt;
25326
+ setFingerprintStmt;
25327
+ rearmStmt;
25328
+ claimStmt;
25329
+ heartbeatStmt;
25330
+ releaseStmt;
25331
+ leaseStmt;
25332
+ inspectionsStmt;
25333
+ closeWindowStmt;
25334
+ releaseBoundaryStmt;
25335
+ freezeBoundaryStmt;
25336
+ captureRowsStmt;
25337
+ markOwedStmt;
25338
+ markCaptureBacklogOwedStmt;
25339
+ captureSkipCountStmt;
25340
+ disownCapturesStmt;
25341
+ partitionStmt;
25342
+ partitionByKindStmt;
25343
+ claimRowStmt;
25344
+ releaseRowStmt;
25345
+ releaseStaleClaimsStmt;
25346
+ /**
25347
+ * The masked detections recorded against one tool call.
25348
+ *
25349
+ * These travel with the event because a tool call's target is not
25350
+ * re-inspectable from the event alone — unlike a capture, where the text
25351
+ * itself is re-scannable. What crosses is the masked match and the rule that
25352
+ * produced it, never the value.
25353
+ */
25354
+ inspectionsFor(auditEventId) {
25355
+ return allRows(this.inspectionsStmt, { auditEventId });
25356
+ }
25357
+ /**
25358
+ * Sessions with structural rows still to send, oldest first.
25359
+ *
25360
+ * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
25361
+ * read. Anything recorded after the machine attached is the live forward
25362
+ * path's to deliver; this drain exists for what was recorded before it, and a
25363
+ * row both paths send is at best a duplicate request and at worst — for a
25364
+ * session root — an overwrite of the inventory ids the live path resolved.
25365
+ */
25366
+ pendingSessions(limit, before) {
25367
+ return allRows(this.sessionsStmt, { limit, before }).map(
25368
+ (r) => r.sessionId
25369
+ );
25370
+ }
25371
+ /** One session's undelivered structural rows within the backlog, root first. */
25372
+ pendingRows(sessionId, limit, before) {
25373
+ return allRows(this.rowsStmt, { sessionId, limit, before });
25374
+ }
25375
+ /**
25376
+ * Captures this machine still owes the deployment, oldest first.
25377
+ *
25378
+ * Selected by the `outbox_owed` marker the attached forward path writes, not
25379
+ * by a time window — see captureRowsStmt for why a window could not express
25380
+ * this. `before` is the grace window that leaves a just-recorded capture to
25381
+ * the live path.
25382
+ */
25383
+ pendingCaptureRows(limit, before) {
25384
+ return allRows(this.captureRowsStmt, { limit, before });
25385
+ }
25386
+ /**
25387
+ * Record that a capture is OWED to the deployment.
25388
+ *
25389
+ * Written by the attached forward path when a live send did not confirm
25390
+ * delivery, and read by the drain as the whole of its eligibility test. It is
25391
+ * a fact rather than an inference: the machine was attached, the send did not
25392
+ * land, so the row is owed — which no time window can state, because the same
25393
+ * window that holds the rows a past attachment left owed also holds every
25394
+ * capture recorded while the machine was DETACHED, and those were never
25395
+ * offered to anyone.
25396
+ *
25397
+ * Idempotent, and never un-set: `markSynced` settling the row is what takes it
25398
+ * out of the drain's read.
25399
+ */
25400
+ markCaptureOwed(id) {
25401
+ this.markOwedStmt.run({ id });
25402
+ }
25403
+ /**
25404
+ * Mark every capture already on disk as owed, as of `before`.
25405
+ *
25406
+ * The consent-time backfill, called once from `aka attach` when a human
25407
+ * grants existing-history consent — never from an ongoing drain pass, and
25408
+ * never inferred from a boundary that could later move. `before` is the
25409
+ * caller's own "now" at the moment consent was granted, so what this marks
25410
+ * is exactly the backlog the consent prompt already counted, not whatever a
25411
+ * later re-attach or key rotation might widen it to.
25412
+ *
25413
+ * Returns how many rows matched, for the caller to log or test against. Not a
25414
+ * count of NEWLY marked rows — a row still unsynced from an earlier call
25415
+ * matches again and is counted again, the same as `UPDATE`'s own `changes`.
25416
+ */
25417
+ markCaptureBacklogOwed(before) {
25418
+ return Number(this.markCaptureBacklogOwedStmt.run({ before }).changes);
25419
+ }
25420
+ /**
25421
+ * Record delivery. Called only AFTER the far side has accepted the rows.
25422
+ *
25423
+ * CLEARS any failure reason in the same statement. A row that failed against
25424
+ * one deployment and then landed is delivered, and leaving the reason behind
25425
+ * would leave the store holding two contradictory answers about one row —
25426
+ * with the surface free to render either.
25427
+ */
25428
+ markSynced(ids, atMs) {
25429
+ this.stampAll(ids, atMs, null);
25430
+ }
25431
+ /**
25432
+ * Record that THIS MACHINE cannot express the row on the wire.
25433
+ *
25434
+ * Reserved for a local defect — a row that cannot be rebuilt into a valid
25435
+ * payload, or a body the client itself refused to send. It fails identically
25436
+ * against every deployment, so it is terminal everywhere and the re-arm leaves
25437
+ * it alone. A row that merely failed to REACH the deployment stays NULL, so it
25438
+ * is retried; marking those would turn one outage into permanent data loss.
25439
+ */
25440
+ markSkipped(ids, atMs) {
25441
+ this.stampAll(ids, SKIPPED, "payload_invalid", atMs);
25442
+ }
25443
+ /**
25444
+ * Record that THIS DEPLOYMENT refused the row.
25445
+ *
25446
+ * The same sentinel as `markSkipped`, and deliberately so: both stop the row
25447
+ * being re-offered on this lane, and `synced_at` goes on answering whether a
25448
+ * row is outstanding rather than why. What separates them is the reason, and
25449
+ * what the reason buys is the re-arm — a refusal is one deployment's verdict
25450
+ * on one body, so it is terminal only for as long as this machine points at
25451
+ * that deployment, and `rearmFor` clears it when the deployment changes.
25452
+ *
25453
+ * Leaving such a row NULL instead would be worse than the loss it replaces:
25454
+ * these reads carry no cursor, so an unstamped row the deployment refuses is
25455
+ * the head of every subsequent page, and the lane stalls behind it for ever.
25456
+ */
25457
+ markRefused(ids, atMs) {
25458
+ this.stampAll(ids, SKIPPED, "deployment_refused", atMs);
25459
+ }
25460
+ eachInTransaction(ids, run) {
25461
+ if (ids.length === 0) return;
25462
+ withTransaction(
25463
+ this.db,
25464
+ () => {
25465
+ for (const id of ids) run(id);
25466
+ },
25467
+ "IMMEDIATE"
25468
+ );
25469
+ }
25470
+ stampAll(ids, value, failure, failedAtMs) {
25471
+ if (ids.length === 0) return;
25472
+ const failedAt = failure === null ? null : failedAtMs ?? null;
25473
+ withTransaction(
25474
+ this.db,
25475
+ () => {
25476
+ for (const id of ids) this.stampStmt.run({ at: value, failedAt, failure, id });
25477
+ },
25478
+ "IMMEDIATE"
25479
+ );
25480
+ }
25481
+ /**
25482
+ * Claim rows as in-flight.
25483
+ *
25484
+ * Advisory in exactly the sense the lease is: it records that a send is in
25485
+ * progress so a surface can say so, and a lost claim costs a row showing as
25486
+ * queued while it is actually being sent. It is not exclusion — the far side
25487
+ * settles a duplicate on the row id.
25488
+ */
25489
+ claimRows(ids, atMs) {
25490
+ this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
25491
+ }
25492
+ /** Give back a claim without settling — the send failed, the row is queued again. */
25493
+ releaseRows(ids) {
25494
+ this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
25495
+ }
25496
+ /**
25497
+ * Clear claims older than `staleBefore`, and report how many were cleared.
25498
+ *
25499
+ * A process killed between claiming and settling leaves rows claimed with
25500
+ * nothing left to settle them. Without this they read as "sending" for ever.
25501
+ */
25502
+ releaseStaleClaims(staleBefore) {
25503
+ return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
25504
+ }
25505
+ /**
25506
+ * Every tracked row in exactly one delivery state.
25507
+ *
25508
+ * Takes no boundary on purpose. The boundary answers "what should the drain
25509
+ * pick up now", which is a different question from "what state is this row
25510
+ * in" — and a machine that has never attached has no boundary to pass, so
25511
+ * requiring one would force a caller to invent one and report the whole store
25512
+ * as queued.
25513
+ */
25514
+ /**
25515
+ * The same partition, one row per kind that a lane carries.
25516
+ *
25517
+ * A kind with nothing to report is ABSENT rather than a row of zeros: the
25518
+ * scope decides which rows exist at all, so a kind that has never been
25519
+ * recorded — or whose captures nobody ever owed — produces no group. A caller
25520
+ * rendering a fixed list of kinds must therefore treat a missing one as "no
25521
+ * rows", never as "zero sent"; the two look identical in a bar and mean
25522
+ * different things.
25523
+ */
25524
+ partitionByKind() {
25525
+ return allRows(
25526
+ this.partitionByKindStmt,
25527
+ {}
25528
+ ).map((row) => ({
25529
+ kind: row.kind,
25530
+ queued: row.queued ?? 0,
25531
+ inProgress: row.inProgress ?? 0,
25532
+ synced: row.synced ?? 0,
25533
+ failed: row.failed ?? 0,
25534
+ refused: row.refused ?? 0,
25535
+ detached: row.detached ?? 0,
25536
+ total: row.total ?? 0
25537
+ }));
25538
+ }
25539
+ partition() {
25540
+ const row = getRow(this.partitionStmt, {});
25541
+ return {
25542
+ queued: row?.queued ?? 0,
25543
+ inProgress: row?.inProgress ?? 0,
25544
+ synced: row?.synced ?? 0,
25545
+ failed: row?.failed ?? 0,
25546
+ refused: row?.refused ?? 0,
25547
+ detached: row?.detached ?? 0,
25548
+ total: row?.total ?? 0
25549
+ };
25550
+ }
25551
+ /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
25552
+ counts(before) {
25553
+ const row = getRow(this.countsStmt, { before });
25554
+ const captures = getRow(this.captureSkipCountStmt);
25555
+ return {
25556
+ pending: row?.pending ?? 0,
25557
+ sent: row?.sent ?? 0,
25558
+ skipped: row?.skipped ?? 0,
25559
+ refused: row?.refused ?? 0,
25560
+ detached: row?.detached ?? 0,
25561
+ capturesSkipped: captures?.skipped ?? 0
25562
+ };
25563
+ }
25564
+ /**
25565
+ * The deployment the current stamps were made against, and where its backlog
25566
+ * ends.
25567
+ *
25568
+ * READ-ONLY. An absent row reads as an absent deployment, which is what a
25569
+ * machine that has never drained is — and every writer below seeds the row
25570
+ * before it needs one, so nothing depends on this creating it. Keeping the
25571
+ * write off the gate path matters because the gate runs on every pass while a
25572
+ * write has to take the database's write lock.
25573
+ */
25574
+ deployment() {
25575
+ const row = getRow(
25576
+ this.fingerprintStmt
25577
+ );
25578
+ return {
25579
+ fingerprint: row?.fingerprint ?? void 0,
25580
+ backlogBefore: row?.backlogBefore ?? void 0
25581
+ };
25582
+ }
25583
+ /**
25584
+ * Point the ledger at a different deployment, discarding what it recorded
25585
+ * about the previous one.
25586
+ *
25587
+ * Delivery is a fact about ONE recipient: rows sent to the deployment a
25588
+ * machine has just left are undelivered as far as the new one is concerned.
25589
+ * All four in one transaction, so a crash between them cannot leave stamps
25590
+ * attributed to the wrong deployment, a boundary that belongs to another, or
25591
+ * a disown with no re-mark to follow it.
25592
+ *
25593
+ * The boundary is written HERE and only here, which is what freezes it: a
25594
+ * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
25595
+ * unchanged, so this never runs and the backlog does not widen back over rows
25596
+ * the live path has since delivered.
25597
+ *
25598
+ * `backfillCapturesBefore` is the caller's OWN "now" at the instant a human
25599
+ * granted existing-history consent for the deployment this call is arming —
25600
+ * a DIFFERENT instant from `backlogBefore`: the re-mark boundary is the GRANT
25601
+ * instant, `backlogBefore` is the ATTACH instant, and the two can be far
25602
+ * apart. Passed only when that grant is valid, since this method has no way
25603
+ * to check consent itself and must not mark a row owed for a machine that
25604
+ * never agreed to it. Applied AFTER the disown above, in the SAME
25605
+ * transaction: what the disown clears is every marker below `backlogBefore`,
25606
+ * which includes this deployment's OWN pre-attach rows — `aka attach` calls
25607
+ * `seedCaptureBacklogOwed` at the attach instant, so every row it marks sits
25608
+ * on the cleared side of that bound — and the re-mark in the same
25609
+ * transaction is what puts those rows back. A crash between the two cannot
25610
+ * strand the ledger disowned with nothing re-marked — the transaction either
25611
+ * lands whole or not at all, and a fingerprint mismatch that has not yet
25612
+ * committed re-enters this method on the very next pass. Omit it (the
25613
+ * structural-only tests do) to exercise the disown in isolation.
25614
+ *
25615
+ * The disown is bounded by `backlogBefore`, which is what keeps it from
25616
+ * touching a marker the NEW deployment's OWN live path has already set: B's
25617
+ * live path can mark a capture owed from the moment `aka attach` writes the
25618
+ * descriptor, before the drain's first pass ever reaches this method, and
25619
+ * such a row sits at or after the bound rather than below it. What keeps the
25620
+ * disown from eating THIS SAME CALL's own re-mark is the order, not the
25621
+ * bound — disown runs first, re-mark second, both inside the one
25622
+ * transaction above.
25623
+ */
25624
+ rearmFor(fingerprint, backlogBefore, backfillCapturesBefore) {
25625
+ this.ensureRowStmt.run();
25626
+ withTransaction(
25627
+ this.db,
25628
+ () => {
25629
+ const previous = getRow(this.fingerprintStmt)?.fingerprint;
25630
+ this.rearmStmt.run();
25631
+ if (previous !== null && previous !== void 0 && previous !== fingerprint) {
25632
+ this.disownCapturesStmt.run({ attachedAt: backlogBefore });
25633
+ }
25634
+ if (backfillCapturesBefore !== void 0) {
25635
+ this.markCaptureBacklogOwedStmt.run({ before: backfillCapturesBefore });
25636
+ }
25637
+ this.setFingerprintStmt.run({ fingerprint, backlogBefore });
25638
+ },
25639
+ "IMMEDIATE"
25640
+ );
25641
+ }
25642
+ /**
25643
+ * End the attached period: hand its rows to the live path, and release the
25644
+ * boundary so the next attachment can freeze a new one.
25645
+ *
25646
+ * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
25647
+ * nothing delivers. The fingerprint is unchanged, so the boundary is never
25648
+ * re-frozen and stays at the FIRST attachment — while nothing forwards at all
25649
+ * during the detached period, because the machine is not attached. Rows
25650
+ * recorded in that window sit after the boundary and before the re-attach, so
25651
+ * neither path takes them, and the pending count reports none outstanding.
25652
+ *
25653
+ * WHAT IT RECORDS, and what it deliberately does not. These rows were the
25654
+ * closing attachment's to deliver and are no longer outstanding — that is what
25655
+ * lets the boundary move. It is NOT a claim that any of them arrived, and the
25656
+ * distinction is not academic: this used to write a delivery TIME, which every
25657
+ * read treats as delivery, so one detach turned a window of undelivered rows
25658
+ * into a window of delivered ones and no surface could tell. It writes the
25659
+ * skip sentinel and a reason of its own instead, so "no longer owed" and
25660
+ * "received" stop being the same fact.
25661
+ *
25662
+ * A change of deployment still frees them (see the re-arm), because the next
25663
+ * deployment has seen none of this machine's history — so the rows reach it
25664
+ * exactly as they did when this wrote a delivery time.
25665
+ *
25666
+ * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
25667
+ * window unstamped — that half-state would re-send the whole attached period
25668
+ * on the next attach, which is the failure the boundary exists to prevent.
25669
+ */
25670
+ closeAttachedWindow(attachedAtMs, atMs) {
25671
+ this.ensureRowStmt.run();
25672
+ withTransaction(
25673
+ this.db,
25674
+ () => {
25675
+ const row = getRow(this.fingerprintStmt);
25676
+ const from = row?.backlogBefore ?? attachedAtMs;
25677
+ this.closeWindowStmt.run({ at: atMs, attachedAt: from });
25678
+ this.releaseBoundaryStmt.run();
25679
+ },
25680
+ "IMMEDIATE"
25681
+ );
25682
+ }
25683
+ /**
25684
+ * Freeze a boundary for the deployment already on file, KEEPING the stamps.
25685
+ *
25686
+ * The re-attach half of the above. Distinct from `rearmFor`, which is for a
25687
+ * different deployment and therefore discards what was delivered to the old
25688
+ * one: here the recipient is the same, so everything already sent to it stays
25689
+ * sent.
25690
+ */
25691
+ freezeBoundary(backlogBefore) {
25692
+ this.ensureRowStmt.run();
25693
+ this.freezeBoundaryStmt.run({ backlogBefore });
25694
+ }
25695
+ /** Take the claim, or report that someone live already holds it. */
25696
+ claim(pid, host, nowMs, staleAfterMs) {
25697
+ this.ensureRowStmt.run();
25698
+ let taken = false;
25699
+ withTransaction(
25700
+ this.db,
25701
+ () => {
25702
+ const result = this.claimStmt.run({
25703
+ pid,
25704
+ host,
25705
+ now: nowMs,
25706
+ staleBefore: nowMs - staleAfterMs
25707
+ });
25708
+ taken = result.changes === 1;
25709
+ },
25710
+ "IMMEDIATE"
25711
+ );
25712
+ return taken;
25713
+ }
25714
+ /** Say the holder is still alive. A no-op once the claim has moved on. */
25715
+ heartbeat(pid, nowMs) {
25716
+ this.heartbeatStmt.run({ now: nowMs, pid });
25717
+ }
25718
+ /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
25719
+ release(pid) {
25720
+ this.releaseStmt.run({ pid });
25721
+ }
25722
+ /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
25723
+ lease() {
25724
+ return getRow(this.leaseStmt);
25725
+ }
25726
+ };
25727
+
24716
25728
  // ../../packages/persistence/src/migrations.ts
24717
25729
  function describeObject(object2) {
24718
25730
  return object2.kind === "column" ? `column ${object2.table}.${object2.name}` : `table ${object2.name}`;
@@ -24725,7 +25737,7 @@ function createdIndexName(statement) {
24725
25737
  return /^CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?`([^`]+)`/.exec(body)?.[1];
24726
25738
  }
24727
25739
  var LEGACY_DROP_MIGRATION_TAG = "0014_drop_legacy_events_findings";
24728
- function applyMigrations(db, file2) {
25740
+ function applyMigrations(db, file2, options = {}) {
24729
25741
  const legacyCount = db.prepare("PRAGMA user_version").get().user_version;
24730
25742
  db.exec(
24731
25743
  "CREATE TABLE IF NOT EXISTS migration_ledger (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)"
@@ -24739,6 +25751,7 @@ function applyMigrations(db, file2) {
24739
25751
  );
24740
25752
  for (const [index, migration] of SQLITE_MIGRATIONS.entries()) {
24741
25753
  if (applied.has(migration.tag)) continue;
25754
+ if (options.skipTags?.has(migration.tag) === true) continue;
24742
25755
  if (migration.tag === LEGACY_DROP_MIGRATION_TAG) continue;
24743
25756
  const evidence = evidenceObjects(migration.sql);
24744
25757
  const present = evidence.filter((o) => evidenceExists(db, o));
@@ -25145,10 +26158,62 @@ function ensureSyncedAtColumn(db, table) {
25145
26158
  if (!columns.includes("outbox_owed")) {
25146
26159
  db.exec(`ALTER TABLE ${table} ADD COLUMN outbox_owed integer`);
25147
26160
  }
26161
+ if (!columns.includes("sync_failed_at")) {
26162
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failed_at integer`);
26163
+ }
26164
+ if (!columns.includes("sync_failure")) {
26165
+ withTransaction(
26166
+ db,
26167
+ () => {
26168
+ db.exec(`ALTER TABLE ${table} ADD COLUMN sync_failure text`);
26169
+ db.exec(
26170
+ `UPDATE ${table} SET synced_at = NULL
26171
+ WHERE synced_at = -1
26172
+ AND event_type IN (${COUNTED_EVENT_TYPES.map((t) => `'${t}'`).join(", ")})`
26173
+ );
26174
+ },
26175
+ "IMMEDIATE"
26176
+ );
26177
+ }
25148
26178
  db.exec(
25149
- `CREATE INDEX IF NOT EXISTS idx_audit_events_sync
25150
- ON audit_events (event_type, synced_at, sync_claimed_at, started_at)`
26179
+ `CREATE TRIGGER IF NOT EXISTS aka_sync_failure_guard
26180
+ BEFORE UPDATE OF sync_failure ON ${table}
26181
+ WHEN ${syncFailureRejectCondition()}
26182
+ BEGIN SELECT RAISE(ABORT, 'sync_failure is not one of the recorded reasons'); END`
25151
26183
  );
26184
+ const syncIndexColumns = [
26185
+ "event_type",
26186
+ "synced_at",
26187
+ "sync_claimed_at",
26188
+ "started_at",
26189
+ // Appended LAST on purpose. The delivery-state read now projects it, so it
26190
+ // has to be in the index for the read to stay covered — but putting it
26191
+ // ahead of `started_at` would reorder the prefix the structural drain's
26192
+ // reads match on.
26193
+ "sync_failure"
26194
+ // `outbox_owed` is DELIBERATELY ABSENT, and it was measured both ways.
26195
+ //
26196
+ // The delivery-state read tests it — a capture's state depends on whether a
26197
+ // live forward marked it owed — so carrying it here makes that read covering
26198
+ // rather than a row fetch per row: 16 ms against 40 ms on a real 6 GB store.
26199
+ // But a sixth column changes what the planner charges for this index, and
26200
+ // with no ANALYZE statistics it plans from schema shape alone: measured, it
26201
+ // then stops choosing the per-session index for the token rollup and walks
26202
+ // every `llm_call` in the store through the event-type index instead. That
26203
+ // read grows with the store; this one does not.
26204
+ //
26205
+ // 40 ms on the largest store measured, once per render, is a cost worth
26206
+ // paying to leave every other read's plan where it was.
26207
+ ];
26208
+ const currentSyncIndex = indexColumns(db, "idx_audit_events_sync");
26209
+ const syncIndexMatches = currentSyncIndex.length === syncIndexColumns.length && currentSyncIndex.every((column, i) => column === syncIndexColumns[i]);
26210
+ if (!syncIndexMatches) {
26211
+ db.exec("DROP INDEX IF EXISTS idx_audit_events_sync");
26212
+ db.exec(
26213
+ `CREATE INDEX idx_audit_events_sync
26214
+ ON audit_events (${syncIndexColumns.join(", ")})`
26215
+ );
26216
+ }
25152
26217
  db.exec(
25153
26218
  `CREATE INDEX IF NOT EXISTS idx_audit_outbox_owed
25154
26219
  ON audit_events (event_type, synced_at, sync_claimed_at, started_at) WHERE outbox_owed = 1`
@@ -25370,7 +26435,11 @@ function buildAuditEvent(row) {
25370
26435
  link: linkParsed?.success ? linkParsed.data : null,
25371
26436
  targetId: row.target_id,
25372
26437
  internal: intToBool(row.internal),
25373
- flagged: intToBool(row.flagged)
26438
+ flagged: intToBool(row.flagged),
26439
+ // Only meaningful when the title came out empty — a row whose body was
26440
+ // expired but whose title fell back to `tool_name` still has something to
26441
+ // render, and flagging it would make the view apologise for nothing.
26442
+ bodyExpired: row.content_expired_at !== null && (row.title ?? "") === ""
25374
26443
  };
25375
26444
  }
25376
26445
  var TIMELINE_COLUMNS = `
@@ -25378,6 +26447,7 @@ var TIMELINE_COLUMNS = `
25378
26447
  event_type,
25379
26448
  started_at,
25380
26449
  coalesce(content, json_extract(attributes, '$.tool_name')) AS title,
26450
+ content_expired_at,
25381
26451
  coalesce(json_extract(attributes, '$.detail'), json_extract(attributes, '$.target')) AS detail,
25382
26452
  coalesce(json_extract(attributes, '$.tool_name'), json_extract(attributes, '$.tool')) AS tool,
25383
26453
  json_extract(attributes, '$.severity') AS severity,
@@ -25504,7 +26574,8 @@ var SqliteActivityRepository = class {
25504
26574
  SELECT 1 FROM audit_events d
25505
26575
  WHERE d.root_session_id = audit_events.id
25506
26576
  AND (d.content LIKE ? ESCAPE '\\'
25507
- OR json_extract(d.attributes, '$.detail') LIKE ? ESCAPE '\\')))`
26577
+ OR coalesce(json_extract(d.attributes, '$.detail'),
26578
+ json_extract(d.attributes, '$.target')) LIKE ? ESCAPE '\\')))`
25508
26579
  );
25509
26580
  params.push(pattern, pattern, pattern, pattern, pattern, pattern);
25510
26581
  }
@@ -26042,6 +27113,88 @@ var SqliteAuditEventsRepository = class {
26042
27113
  }
26043
27114
  };
26044
27115
 
27116
+ // ../../packages/persistence/src/repositories/body-retention.ts
27117
+ var DEFAULT_BATCH_SIZE = 500;
27118
+ var DEFAULT_MAX_ROWS = 5e4;
27119
+ var SYNC_LANE_TYPES_SQL = OUTBOX_CAPTURE_TYPE_LIST;
27120
+ var SqliteBodyRetentionRepository = class {
27121
+ constructor(db) {
27122
+ this.db = db;
27123
+ const select = (laneClause) => `
27124
+ SELECT id, LENGTH(CAST(content AS BLOB)) AS bytes
27125
+ FROM audit_events
27126
+ WHERE content IS NOT NULL
27127
+ AND started_at < :cutoff
27128
+ AND event_type IN (${CAPTURE_EVENT_TYPES_SQL})
27129
+ ${laneClause}
27130
+ ORDER BY started_at
27131
+ LIMIT :limit`;
27132
+ this.candidatesStmt = this.db.prepare(select(""));
27133
+ this.candidatesSyncSafeStmt = this.db.prepare(
27134
+ select(`AND (event_type NOT IN (${SYNC_LANE_TYPES_SQL}) OR synced_at IS NOT NULL)`)
27135
+ );
27136
+ this.heldBySyncStmt = this.db.prepare(`
27137
+ SELECT COUNT(*) AS n
27138
+ FROM audit_events
27139
+ WHERE content IS NOT NULL
27140
+ AND started_at < :cutoff
27141
+ AND event_type IN (${SYNC_LANE_TYPES_SQL})
27142
+ AND synced_at IS NULL`);
27143
+ this.expireStmt = this.db.prepare(
27144
+ `UPDATE audit_events SET content = NULL, content_expired_at = :now WHERE id = :id`
27145
+ );
27146
+ }
27147
+ db;
27148
+ candidatesStmt;
27149
+ candidatesSyncSafeStmt;
27150
+ heldBySyncStmt;
27151
+ expireStmt;
27152
+ /** How many bytes a pass with these options would free, changing nothing. */
27153
+ preview(opts) {
27154
+ const limit = opts.maxRows ?? DEFAULT_MAX_ROWS;
27155
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27156
+ const rows = stmt.all({ cutoff: opts.cutoff, limit });
27157
+ return {
27158
+ rowsExpired: rows.length,
27159
+ bytesFreed: rows.reduce((sum, r) => sum + r.bytes, 0),
27160
+ rowsHeldBySync: this.countHeldBySync(opts)
27161
+ };
27162
+ }
27163
+ /** Clear eligible bodies, in bounded batches. */
27164
+ expire(opts) {
27165
+ const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
27166
+ const maxRows = opts.maxRows ?? DEFAULT_MAX_ROWS;
27167
+ const stmt = opts.sweepSyncLane ? this.candidatesStmt : this.candidatesSyncSafeStmt;
27168
+ let rowsExpired = 0;
27169
+ let bytesFreed = 0;
27170
+ let done = true;
27171
+ while (rowsExpired < maxRows) {
27172
+ const remaining = Math.min(batchSize, maxRows - rowsExpired);
27173
+ const batch = stmt.all({ cutoff: opts.cutoff, limit: remaining });
27174
+ if (batch.length === 0) break;
27175
+ withTransaction(
27176
+ this.db,
27177
+ () => {
27178
+ for (const row of batch) this.expireStmt.run({ id: row.id, now: opts.now });
27179
+ },
27180
+ "IMMEDIATE"
27181
+ );
27182
+ rowsExpired += batch.length;
27183
+ bytesFreed += batch.reduce((sum, r) => sum + r.bytes, 0);
27184
+ if (batch.length < remaining) break;
27185
+ if (rowsExpired >= maxRows) {
27186
+ done = stmt.all({ cutoff: opts.cutoff, limit: 1 }).length === 0;
27187
+ }
27188
+ }
27189
+ return { rowsExpired, bytesFreed, rowsHeldBySync: this.countHeldBySync(opts), done };
27190
+ }
27191
+ countHeldBySync(opts) {
27192
+ if (opts.sweepSyncLane) return 0;
27193
+ const row = this.heldBySyncStmt.get({ cutoff: opts.cutoff });
27194
+ return row.n;
27195
+ }
27196
+ };
27197
+
26045
27198
  // ../../packages/persistence/src/repositories/classified-data.ts
26046
27199
  var SqliteClassifiedDataRepository = class {
26047
27200
  constructor(db) {
@@ -26842,23 +27995,6 @@ var LATEST_RESOLUTION_BY_KEY_SQL = `(
26842
27995
  )`;
26843
27996
 
26844
27997
  // ../../packages/persistence/src/repositories/findings.ts
26845
- var PREVIEW_INSTANCES_PER_GROUP = 200;
26846
- var DEFAULT_LOCATIONS_LIMIT = 100;
26847
- var LOCATION_RULE_IDS_CAP = 20;
26848
- function compareLocationOrder(a, b) {
26849
- return compareFindingGroupOrder(
26850
- {
26851
- severity: a.maxSeverity,
26852
- latestDetectedAt: a.latestDetectedAt,
26853
- id: ""
26854
- },
26855
- {
26856
- severity: b.maxSeverity,
26857
- latestDetectedAt: b.latestDetectedAt,
26858
- id: ""
26859
- }
26860
- );
26861
- }
26862
27998
  var CONCAT_SEP = ",";
26863
27999
  var TUPLE_SEP = "|";
26864
28000
  function splitConcat(value) {
@@ -26887,7 +28023,15 @@ function toFlatFindingRow(r) {
26887
28023
  ...r.tool_name === null ? {} : { toolName: r.tool_name },
26888
28024
  eventId: r.event_id,
26889
28025
  ...r.session_id === null ? {} : { sessionId: r.session_id },
26890
- status: deriveInstanceStatus(r)
28026
+ status: deriveInstanceStatus(r),
28027
+ delivery: deriveFindingDelivery({
28028
+ kind: r.kind,
28029
+ syncedAt: r.synced_at,
28030
+ syncClaimedAt: r.sync_claimed_at,
28031
+ syncFailedAt: r.sync_failed_at,
28032
+ syncFailure: r.sync_failure,
28033
+ outboxOwed: r.outbox_owed
28034
+ })
26891
28035
  };
26892
28036
  }
26893
28037
  function encodeGroupCursor(group) {
@@ -26910,13 +28054,51 @@ function decodeGroupCursor(cursor) {
26910
28054
  return null;
26911
28055
  }
26912
28056
  function firstAfter(sorted, cursor) {
26913
- const index = sorted.findIndex((g) => compareFindingGroupOrder(g, cursor) > 0);
28057
+ const index = sorted.findIndex((t) => compareFindingGroupOrder(t, cursor) > 0);
26914
28058
  return index === -1 ? sorted.length : index;
26915
28059
  }
26916
28060
  function findDeepLinked(sorted, page, id) {
26917
- if (page.some((g) => g.id === id || g.instances.some((i) => i.id === id))) return void 0;
26918
- return sorted.find((g) => g.id === id || g.instances.some((i) => i.id === id));
28061
+ if (page.some((t) => t.id === id)) return void 0;
28062
+ return sorted.find((t) => t.id === id);
28063
+ }
28064
+ function encodeLocationCursor(location) {
28065
+ const payload = {
28066
+ sev: location.maxSeverity,
28067
+ t: location.latestDetectedAt,
28068
+ r: location.repo,
28069
+ f: location.file
28070
+ };
28071
+ return Buffer.from(JSON.stringify(payload)).toString("base64url");
28072
+ }
28073
+ function decodeLocationCursor(cursor) {
28074
+ const parsed2 = parseJsonObject(Buffer.from(cursor, "base64url").toString("utf8"));
28075
+ if (parsed2 !== void 0 && typeof parsed2.sev === "string" && typeof parsed2.t === "string" && typeof parsed2.r === "string" && typeof parsed2.f === "string") {
28076
+ return { maxSeverity: parsed2.sev, latestDetectedAt: parsed2.t, repo: parsed2.r, file: parsed2.f };
28077
+ }
28078
+ return null;
28079
+ }
28080
+ function firstLocationAfter(sorted, cursor) {
28081
+ const index = sorted.findIndex((l) => compareLocationOrder(l, cursor) > 0);
28082
+ return index === -1 ? sorted.length : index;
26919
28083
  }
28084
+ function findDeepLinkedLocation(sorted, page, id) {
28085
+ if (page.some((l) => l.id === id)) return void 0;
28086
+ return sorted.find((l) => l.id === id);
28087
+ }
28088
+ var FINDING_ROW_COLUMNS_SQL = `f.id AS id, d.rule_id AS rule_id, d.category AS category,
28089
+ d.severity AS severity, f.masked_match AS masked_match,
28090
+ f.action_taken AS action_taken, f.confidence AS confidence,
28091
+ e.started_at AS occurred_at,
28092
+ e.source_tool AS source_tool,
28093
+ e.repo AS repo,
28094
+ e.file_path AS file,
28095
+ e.tool_name AS tool_name,
28096
+ f.audit_event_id AS event_id, e.root_session_id AS session_id,
28097
+ e.event_type AS kind, f.finding_key AS finding_key,
28098
+ ${latestResolutionStatusSql("f")} AS latest_status,
28099
+ e.synced_at AS synced_at, e.sync_claimed_at AS sync_claimed_at,
28100
+ e.sync_failed_at AS sync_failed_at, e.sync_failure AS sync_failure,
28101
+ e.outbox_owed AS outbox_owed`;
26920
28102
  var DAY_MS3 = 864e5;
26921
28103
  var SqliteFindingsRepository = class {
26922
28104
  constructor(db) {
@@ -27037,30 +28219,26 @@ var SqliteFindingsRepository = class {
27037
28219
  );
27038
28220
  }
27039
28221
  /**
27040
- * Grouped findings for the dashboard — joins inspection_findings⋈audit_events
27041
- * ⋈inspection_definitions (repo/file/toolName from the audit event's
27042
- * attributes bag, rule_id/category/severity from the definition), scoped to
27043
- * the four capture kinds (audit_events also holds structural/reconciler/scan
27044
- * rows this list must never surface), groups by ruleId, computes
27045
- * per-filter-excluded facets, applies the requested filters, and sorts by
27046
- * severity then recency. Filtering
27047
- * and faceting run in JS via the shared @akasecurity/schema helpers. `totals`
27048
- * reflect the full filtered set; `items` is the requested
27049
- * page (default 50); no cursor (nextCursor is always null). Under a `status`
27050
- * filter, `totals.findings` counts only instances whose derived status was
27051
- * requested, and each item's instance preview is narrowed the same way.
28222
+ * Finding TYPES for the dashboard — one row per rule, scoped to the four
28223
+ * capture kinds (audit_events also holds structural/reconciler/scan rows this
28224
+ * list must never surface), with per-filter-excluded facets, the requested
28225
+ * filters applied, and sorted by severity then recency. Filtering and faceting
28226
+ * run in JS via the shared @akasecurity/schema helpers. `totals` reflect the
28227
+ * full filtered set; `items` is the requested page (default 50), keyset-paged.
28228
+ * Under a `status` filter, `totals.findings` counts only findings whose
28229
+ * derived status was requested.
28230
+ *
28231
+ * ONE read, which materializes no findings: a single aggregate per rule_id,
28232
+ * folding EVERY finding into the numbers a type row and the filters need
28233
+ * (count, severity, category, providers, actions, statuses, latest, search
28234
+ * text). The findings OF a type come from listFindingInstances scoped to
28235
+ * `subtype`, so neither list bounds the other and no per-type cap exists.
27052
28236
  *
27053
- * Two reads, neither of which materializes a row per finding:
27054
- * 1. one aggregate row per rule_id, folding EVERY instance into the numbers
27055
- * the group and the filters need (count, providers, actions, statuses,
27056
- * latest, search text);
27057
- * 2. each group's newest PREVIEW_INSTANCES_PER_GROUP instances, which
27058
- * populate `instances` for the table's expanded rows.
27059
28237
  * The aggregates carry raw DB values and are translated by the same
27060
- * @akasecurity/schema mappers the row path uses, so no enum mapping or status
27061
- * rule is ever restated in SQL.
28238
+ * @akasecurity/schema mappers every other path uses, so no enum mapping or
28239
+ * status rule is ever restated in SQL.
27062
28240
  */
27063
- listGroupedFindings(query) {
28241
+ listFindingTypes(query) {
27064
28242
  const sessionPredicate = query.sessionId ? ` AND e.root_session_id = :sessionId` : "";
27065
28243
  const fromMs = query.from === void 0 ? void 0 : isoToEpochMillis(query.from);
27066
28244
  const fromPredicate = fromMs === void 0 ? "" : ` AND e.started_at >= :fromMs`;
@@ -27073,12 +28251,7 @@ var SqliteFindingsRepository = class {
27073
28251
  predicate,
27074
28252
  params: sessionParams
27075
28253
  });
27076
- const rows = this.previewRows(aggregates, {
27077
- sessionId: query.sessionId,
27078
- from: query.from
27079
- });
27080
- const groupable = rows.map(toFlatFindingRow);
27081
- const allGroups = buildFindingGroups(groupable, { aggregates });
28254
+ const allTypes = buildFindingTypes(aggregates);
27082
28255
  const filterOpts = {
27083
28256
  severity: query.severity,
27084
28257
  providers: query.provider,
@@ -27087,30 +28260,25 @@ var SqliteFindingsRepository = class {
27087
28260
  subtype: query.subtype,
27088
28261
  q: query.q
27089
28262
  };
27090
- const facets = computeFindingFacets(allGroups, filterOpts);
27091
- const sorted = sortFindingGroups(applyFindingFilters(allGroups, filterOpts));
28263
+ const facets = computeFindingFacets(allTypes, filterOpts);
28264
+ const sorted = sortFindingTypes(applyFindingFilters(allTypes, filterOpts));
27092
28265
  const statusFilter = query.status ?? [];
27093
28266
  const totals = {
27094
- findings: sorted.reduce((acc, g) => {
27095
- if (statusFilter.length === 0) return acc + g.instanceCount;
27096
- const agg = aggregates.get(g.id);
27097
- return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? g.instanceCount : g.instanceCount);
28267
+ findings: sorted.reduce((acc, t) => {
28268
+ if (statusFilter.length === 0) return acc + t.instanceCount;
28269
+ const agg = aggregates.get(t.id);
28270
+ return acc + (agg ? countInstancesByStatus(agg.statusInputs, statusFilter) ?? t.instanceCount : t.instanceCount);
27098
28271
  }, 0),
27099
- groups: sorted.length
28272
+ types: sorted.length
27100
28273
  };
27101
- const limit = query.limit ?? DEFAULT_GROUPED_FINDINGS_LIMIT;
28274
+ const limit = query.limit ?? DEFAULT_FINDING_TYPES_LIMIT;
27102
28275
  const cursor = query.cursor === void 0 ? null : decodeGroupCursor(query.cursor);
27103
28276
  const start = cursor === null ? 0 : firstAfter(sorted, cursor);
27104
28277
  const page = sorted.slice(start, start + limit);
27105
28278
  const lastOnPage = page.at(-1);
27106
28279
  const nextCursor = start + limit < sorted.length && lastOnPage ? encodeGroupCursor(lastOnPage) : null;
27107
28280
  const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinked(sorted, page, query.includeId);
27108
- const statusSet = statusFilter.length > 0 ? new Set(statusFilter) : null;
27109
- const narrow = (g) => statusSet ? {
27110
- ...g,
27111
- instances: g.instances.filter((i) => i.status !== void 0 && statusSet.has(i.status))
27112
- } : g;
27113
- const items = [...page, ...deepLinked ? [deepLinked] : []].map(narrow);
28281
+ const items = [...page, ...deepLinked ? [deepLinked] : []];
27114
28282
  return Promise.resolve({
27115
28283
  totals,
27116
28284
  facets,
@@ -27121,7 +28289,7 @@ var SqliteFindingsRepository = class {
27121
28289
  }
27122
28290
  /**
27123
28291
  * One row per rule_id, folding EVERY instance of the group into the values
27124
- * buildFindingGroups cannot recover from a preview. Bounded by the number of
28292
+ * buildFindingTypes cannot recover from an aggregate. Bounded by the number of
27125
28293
  * distinct rule_ids (the installed packs' rules), not by the store's size.
27126
28294
  *
27127
28295
  * A single scan, folded in two levels: the inner SELECT groups by
@@ -27175,6 +28343,7 @@ var SqliteFindingsRepository = class {
27175
28343
  providers: query.provider,
27176
28344
  actions: query.action,
27177
28345
  statuses: query.status,
28346
+ deliveries: query.deployment,
27178
28347
  tools: query.tool,
27179
28348
  repo: query.repo,
27180
28349
  file: query.file,
@@ -27215,13 +28384,25 @@ var SqliteFindingsRepository = class {
27215
28384
  });
27216
28385
  }
27217
28386
  /**
27218
- * The same findings folded by location: repository, then file within it.
28387
+ * The same findings folded by WHERE they live — one row per (repo, file) pair.
27219
28388
  *
27220
28389
  * The grouping keys come from the capturing event's attributes, which is what
27221
- * the local store relates a finding to — there is no finding↔asset row to
27222
- * group by instead. A repo or file the event did not record folds into the
27223
- * empty-string bucket, which the view renders but does not link, since no
27224
- * filter can name it.
28390
+ * the local store relates a finding to; there is no finding↔asset row to group
28391
+ * by instead. A repo or file the event did not record folds into the
28392
+ * empty-string bucket, which is a real location like any other: it is listed,
28393
+ * it is selectable, and its `?loc=` token is as good as any other row's.
28394
+ *
28395
+ * ONE flat list rather than repos nesting files. A rollup can only be paged by
28396
+ * repo, which leaves the file list inside it unbounded — the shape the by-type
28397
+ * list was rebuilt to remove — and two-level pagination inside an
28398
+ * expand/collapse table is what pushed that view to master/detail in the first
28399
+ * place.
28400
+ *
28401
+ * Every filter narrows the FINDINGS and the locations fall out of what
28402
+ * survives, so each row's `instanceCount` is exactly what listFindingInstances
28403
+ * reports for the same filters scoped to that pair. The view depends on it:
28404
+ * one toolbar sits over both panels precisely because a location owns none of
28405
+ * its fields.
27225
28406
  */
27226
28407
  listFindingLocations(query) {
27227
28408
  const opts = {
@@ -27230,16 +28411,20 @@ var SqliteFindingsRepository = class {
27230
28411
  providers: query.provider,
27231
28412
  actions: query.action,
27232
28413
  statuses: query.status,
28414
+ deliveries: query.deployment,
27233
28415
  tools: query.tool,
27234
28416
  q: query.q
27235
28417
  };
27236
- const limit = query.limit ?? DEFAULT_LOCATIONS_LIMIT;
28418
+ const limit = query.limit ?? DEFAULT_FINDING_LOCATIONS_LIMIT;
28419
+ const cursor = query.cursor === void 0 ? null : decodeLocationCursor(query.cursor);
27237
28420
  const byRepo = /* @__PURE__ */ new Map();
28421
+ const accumulator = createInstanceFacetAccumulator(opts);
27238
28422
  let total = 0;
27239
28423
  for (const row of this.scanFindingRows({
27240
28424
  sessionId: query.sessionId,
27241
28425
  from: query.from
27242
28426
  })) {
28427
+ accumulator.add(row);
27243
28428
  if (!matchesInstanceFilters(row, opts)) continue;
27244
28429
  total += 1;
27245
28430
  let files = byRepo.get(row.repo);
@@ -27254,103 +28439,35 @@ var SqliteFindingsRepository = class {
27254
28439
  }
27255
28440
  addToLocation(acc, row);
27256
28441
  }
27257
- let fileCount = 0;
27258
- const repos = [...byRepo.entries()].map(([repo, files]) => {
27259
- fileCount += files.size;
27260
- const fileRows = [...files.entries()].map(([file2, acc]) => ({
27261
- file: file2,
27262
- instanceCount: acc.instanceCount,
27263
- maxSeverity: acc.maxSeverity,
27264
- latestDetectedAt: acc.latestDetectedAt,
27265
- ...foldGroupStatus(acc.statuses) === void 0 ? {} : { status: foldGroupStatus(acc.statuses) },
27266
- ruleIds: [...acc.ruleIds].slice(0, LOCATION_RULE_IDS_CAP)
27267
- })).sort(compareLocationOrder);
27268
- const rollup = fileRows.reduce(
27269
- (a, f) => ({
27270
- instanceCount: a.instanceCount + f.instanceCount,
27271
- maxSeverity: compareLocationOrder(f, a) < 0 ? f.maxSeverity : a.maxSeverity,
27272
- latestDetectedAt: f.latestDetectedAt > a.latestDetectedAt ? f.latestDetectedAt : a.latestDetectedAt
27273
- }),
27274
- {
27275
- instanceCount: 0,
27276
- maxSeverity: fileRows[0]?.maxSeverity ?? "low",
27277
- latestDetectedAt: ""
27278
- }
27279
- );
27280
- const statuses = fileRows.map((f) => f.status);
27281
- const folded = foldGroupStatus(statuses);
27282
- return {
27283
- repo,
27284
- instanceCount: rollup.instanceCount,
27285
- maxSeverity: rollup.maxSeverity,
27286
- latestDetectedAt: rollup.latestDetectedAt,
27287
- ...folded === void 0 ? {} : { status: folded },
27288
- files: fileRows
27289
- };
27290
- });
27291
- repos.sort(compareLocationOrder);
28442
+ const sorted = [];
28443
+ for (const [repo, files] of byRepo) {
28444
+ for (const [file2, acc] of files) {
28445
+ const status = foldGroupStatus(acc.statuses);
28446
+ sorted.push({
28447
+ id: encodeLocationId(repo, file2),
28448
+ repo,
28449
+ file: file2,
28450
+ instanceCount: acc.instanceCount,
28451
+ maxSeverity: acc.maxSeverity,
28452
+ latestDetectedAt: acc.latestDetectedAt,
28453
+ ...status === void 0 ? {} : { status },
28454
+ ruleIds: [...acc.ruleIds]
28455
+ });
28456
+ }
28457
+ }
28458
+ sorted.sort(compareLocationOrder);
28459
+ const start = cursor === null ? 0 : firstLocationAfter(sorted, cursor);
28460
+ const page = sorted.slice(start, start + limit);
28461
+ const lastOnPage = page.at(-1);
28462
+ const nextCursor = start + limit < sorted.length && lastOnPage ? encodeLocationCursor(lastOnPage) : null;
28463
+ const deepLinked = query.includeId === void 0 || query.includeId === "" ? void 0 : findDeepLinkedLocation(sorted, page, query.includeId);
27292
28464
  return Promise.resolve({
27293
- totals: { findings: total, repos: repos.length, files: fileCount },
27294
- items: repos.slice(0, limit),
27295
- hasMore: repos.length > limit
28465
+ totals: { findings: total, locations: sorted.length },
28466
+ facets: accumulator.facets(),
28467
+ items: [...page, ...deepLinked ? [deepLinked] : []],
28468
+ nextCursor
27296
28469
  });
27297
28470
  }
27298
- /**
27299
- * Each group's newest instances, for the table's expanded rows.
27300
- *
27301
- * ONE index-ordered scan with early termination, and the shape is the point.
27302
- * The natural spelling — `ROW_NUMBER() OVER (PARTITION BY rule_id ORDER BY
27303
- * started_at DESC)` then `WHERE rn <= cap` — sorts EVERY finding in scope
27304
- * through a temp B-tree to keep a bounded preview of each group, and then
27305
- * sorts the survivors again for the page order. Both sorts grow with the
27306
- * store while the answer does not.
27307
- *
27308
- * Instead the scan walks `audit_events` newest-first off `idx_audit_started_at`
27309
- * (or the session or window index the scope names — see `findingScanSql`),
27310
- * which is already the order the page wants, and keeps rows per rule until
27311
- * each rule has as many as it can show. The aggregate the caller already holds
27312
- * says how many that is: `min(instanceCount, PREVIEW_INSTANCES_PER_GROUP)`
27313
- * per rule, summed, is the number of rows this scan has to find, and it stops
27314
- * on the last one. That sum is bounded by `rules * PREVIEW_INSTANCES_PER_GROUP`
27315
- * (8,000 at this repo's 40-rule bench corpus), not by a fixed row count — a
27316
- * store with many firing rules widens it. The bound that DOES hold
27317
- * unconditionally is the sorted form's floor: this scan visits at most as
27318
- * many rows as `ROW_NUMBER() OVER (PARTITION BY rule_id …)` would have
27319
- * sorted, and stops the moment every rule has its cap, where the sorted form
27320
- * sorts the whole scope regardless. The true worst case — the rarest rule's
27321
- * wanted instances sitting at the tail of the scope — is one pass over
27322
- * everything in scope with a block sort of the id tie-break only, never a
27323
- * sort of the scope, which is still that floor.
27324
- *
27325
- * A row whose rule the aggregate did not see is skipped: the two statements
27326
- * run without a shared snapshot, so a capture landing between them can add a
27327
- * rule here that has no counts there, and the counts are what the group is
27328
- * built from.
27329
- */
27330
- previewRows(aggregates, scope) {
27331
- const wanted = /* @__PURE__ */ new Map();
27332
- let remaining = 0;
27333
- for (const [ruleId, agg] of aggregates) {
27334
- const n = Math.min(agg.instanceCount, PREVIEW_INSTANCES_PER_GROUP);
27335
- wanted.set(ruleId, n);
27336
- remaining += n;
27337
- }
27338
- const rows = [];
27339
- if (remaining === 0) return rows;
27340
- const { sql, params } = this.findingScanSql(scope);
27341
- const taken = /* @__PURE__ */ new Map();
27342
- for (const r of iterateRows(this.db.prepare(sql), params)) {
27343
- const want = wanted.get(r.rule_id);
27344
- if (want === void 0) continue;
27345
- const have = taken.get(r.rule_id) ?? 0;
27346
- if (have >= want) continue;
27347
- taken.set(r.rule_id, have + 1);
27348
- rows.push(r);
27349
- remaining -= 1;
27350
- if (remaining === 0) break;
27351
- }
27352
- return rows;
27353
- }
27354
28471
  /**
27355
28472
  * Every finding in scope as a FlatFindingRow, newest first, streamed.
27356
28473
  *
@@ -27377,6 +28494,33 @@ var SqliteFindingsRepository = class {
27377
28494
  yield toFlatFindingRow(r);
27378
28495
  }
27379
28496
  }
28497
+ /**
28498
+ * One finding by its own id, or null when no such row exists.
28499
+ *
28500
+ * A primary-key seek on `inspection_findings`, so its cost does not grow with
28501
+ * the store — and, unlike anything derived from a list page, it resolves a
28502
+ * finding of ANY age. That is what the Findings page's one-shot `?finding=`
28503
+ * deep link needs: the id it carries may name a finding thousands of rows
28504
+ * older than anything a first page holds.
28505
+ *
28506
+ * Deliberately UNFILTERED — no capture-kind, session or time predicate. It
28507
+ * RESOLVES an id; whether that row would survive the list's current filters is
28508
+ * a different question, and hiding the target because a filter excludes it is
28509
+ * worse than showing it.
28510
+ *
28511
+ * `groupId` on the result IS the rule id, so this one read answers both "which
28512
+ * type should the list select?" and "what does the drawer show?".
28513
+ */
28514
+ findingInstance(id) {
28515
+ const row = this.db.prepare(
28516
+ `SELECT ${FINDING_ROW_COLUMNS_SQL}
28517
+ FROM inspection_findings f
28518
+ JOIN audit_events e ON e.id = f.audit_event_id
28519
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
28520
+ WHERE f.id = ?`
28521
+ ).get(id);
28522
+ return Promise.resolve(row === void 0 ? null : toInstanceDetail(toFlatFindingRow(row)));
28523
+ }
27380
28524
  /**
27381
28525
  * The one statement both instance-level scans run: every finding in scope,
27382
28526
  * joined to its event and definition, newest first.
@@ -27410,17 +28554,7 @@ var SqliteFindingsRepository = class {
27410
28554
  conditions.push("e.started_at >= ?");
27411
28555
  params.push(isoToEpochMillis(scope.from));
27412
28556
  }
27413
- const sql = `SELECT f.id AS id, d.rule_id AS rule_id, d.category AS category,
27414
- d.severity AS severity, f.masked_match AS masked_match,
27415
- f.action_taken AS action_taken, f.confidence AS confidence,
27416
- e.started_at AS occurred_at,
27417
- e.source_tool AS source_tool,
27418
- e.repo AS repo,
27419
- e.file_path AS file,
27420
- e.tool_name AS tool_name,
27421
- f.audit_event_id AS event_id, e.root_session_id AS session_id,
27422
- e.event_type AS kind, f.finding_key AS finding_key,
27423
- ${latestResolutionStatusSql("f")} AS latest_status
28557
+ const sql = `SELECT ${FINDING_ROW_COLUMNS_SQL}
27424
28558
  FROM audit_events e
27425
28559
  CROSS JOIN inspection_findings f ON f.audit_event_id = e.id
27426
28560
  CROSS JOIN inspection_definitions d ON d.id = f.inspection_definition_id
@@ -27434,6 +28568,26 @@ var SqliteFindingsRepository = class {
27434
28568
  group_concat(DISTINCT 'via ' || e.tool_name) AS tool_names` : `, NULL AS repos, NULL AS files, NULL AS tool_names`;
27435
28569
  const rows = this.db.prepare(
27436
28570
  `SELECT rule_id,
28571
+ -- BARE columns beside max(latest_at), which is deliberate and
28572
+ -- is SQLite's documented behaviour: with a single min()/max()
28573
+ -- in an aggregate query, every bare column takes its value from
28574
+ -- the row that produced the extremum. So these are the severity
28575
+ -- and category of the definition whose finding is NEWEST, which
28576
+ -- is what the row-based build they replaced read off its first
28577
+ -- (newest-first) row.
28578
+ --
28579
+ -- min() is WRONG here and was the defect: inspection_definitions
28580
+ -- holds one row per rule VERSION (see its writer \u2014 a version bump
28581
+ -- mints a new row), so a rule whose severity moved between
28582
+ -- versions has several, and min() picks the ALPHABETICALLY
28583
+ -- smallest \u2014 'low' over 'medium', but 'critical' over 'high'.
28584
+ -- That is arbitrary in direction, and it feeds the badge, the
28585
+ -- filter, the facet counts and the primary sort key.
28586
+ --
28587
+ -- Adding a second min()/max() aggregate here would make these
28588
+ -- bare columns ambiguous again; keep max(latest_at) the only one.
28589
+ severity,
28590
+ category,
27437
28591
  sum(tuple_count) AS instance_count,
27438
28592
  max(latest_at) AS latest_at,
27439
28593
  group_concat(source_tools) AS source_tools,
@@ -27444,6 +28598,14 @@ var SqliteFindingsRepository = class {
27444
28598
  group_concat(tool_names) AS tool_names
27445
28599
  FROM (
27446
28600
  SELECT d.rule_id AS rule_id,
28601
+ -- Severity and category are columns of the DEFINITION, and
28602
+ -- a rule can have SEVERAL definitions (one per version), so
28603
+ -- these are grouped on below and resolved to the newest
28604
+ -- firing version by the outer query's bare-column select.
28605
+ -- They ride the aggregate because the type build has no rows
28606
+ -- to read them off \u2014 see buildFindingTypes.
28607
+ d.severity AS severity,
28608
+ d.category AS category,
27447
28609
  e.event_type || '${TUPLE_SEP}' ||
27448
28610
  (CASE WHEN f.finding_key IS NULL THEN '' ELSE 'k' END) || '${TUPLE_SEP}' ||
27449
28611
  coalesce(latest.status, '') AS status_tuple,
@@ -27458,7 +28620,7 @@ var SqliteFindingsRepository = class {
27458
28620
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
27459
28621
  ON latest.finding_key = f.finding_key
27460
28622
  ${scope.predicate}
27461
- GROUP BY d.rule_id, status_tuple
28623
+ GROUP BY d.rule_id, d.severity, d.category, status_tuple
27462
28624
  )
27463
28625
  GROUP BY rule_id`
27464
28626
  ).all(scope.params);
@@ -27467,6 +28629,8 @@ var SqliteFindingsRepository = class {
27467
28629
  r.rule_id,
27468
28630
  {
27469
28631
  instanceCount: r.instance_count,
28632
+ severity: r.severity,
28633
+ category: r.category,
27470
28634
  sourceTools: splitConcat(r.source_tools),
27471
28635
  actionsTaken: splitConcat(r.actions_taken),
27472
28636
  statusInputs: splitConcat(r.status_inputs).map((tuple2) => {
@@ -27483,7 +28647,7 @@ var SqliteFindingsRepository = class {
27483
28647
  latestDetectedAt: epochMillisToIso(r.latest_at),
27484
28648
  // Free text only — joined and substring-matched, so group_concat's
27485
28649
  // commas need no unpicking (a repo/path containing one still matches).
27486
- // Left undefined (not '') when unfetched, so buildFindingGroups can
28650
+ // Left undefined (not '') when unfetched, so buildFindingTypes can
27487
28651
  // tell "no q this request" from "a group with no repo/file at all"
27488
28652
  // and skip priming a haystack nothing will read.
27489
28653
  ...withSearchText ? {
@@ -27511,7 +28675,9 @@ var SqliteFindingsRepository = class {
27511
28675
  )
27512
28676
  );
27513
28677
  for (const row of grouped) {
27514
- if (row.action_taken in byAction) byAction[row.action_taken] = row.c;
28678
+ if (Object.hasOwn(byAction, row.action_taken)) {
28679
+ byAction[row.action_taken] = row.c;
28680
+ }
27515
28681
  }
27516
28682
  const bySeverity = { critical: 0, high: 0, medium: 0, low: 0 };
27517
28683
  const sevRows = allRows(
@@ -27528,7 +28694,9 @@ var SqliteFindingsRepository = class {
27528
28694
  )
27529
28695
  );
27530
28696
  for (const row of sevRows) {
27531
- if (row.severity in bySeverity) bySeverity[row.severity] = row.c;
28697
+ if (Object.hasOwn(bySeverity, row.severity)) {
28698
+ bySeverity[row.severity] = row.c;
28699
+ }
27532
28700
  }
27533
28701
  const categories = ENFORCEABLE_CATEGORIES;
27534
28702
  const enabledRows = allRows(
@@ -27577,469 +28745,6 @@ function isoDay(ms) {
27577
28745
  return new Date(ms).toISOString().slice(0, 10);
27578
28746
  }
27579
28747
 
27580
- // ../../packages/persistence/src/repositories/history-sync.ts
27581
- var STRUCTURAL_EVENT_TYPES = ["session", "llm_call", "tool_call"];
27582
- var TYPE_LIST = STRUCTURAL_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27583
- var OUTBOX_CAPTURE_EVENT_TYPES = ["prompt", "response", "tool_use"];
27584
- var CAPTURE_TYPE_LIST = OUTBOX_CAPTURE_EVENT_TYPES.map((t) => `'${t}'`).join(", ");
27585
- var SKIPPED = -1;
27586
- var ROW_COLUMNS = `id,
27587
- parent_id AS parentId,
27588
- root_session_id AS rootSessionId,
27589
- event_type AS eventType,
27590
- host_id AS hostId,
27591
- harness_id AS harnessId,
27592
- source_project_id AS sourceProjectId,
27593
- started_at AS startedAt,
27594
- ended_at AS endedAt,
27595
- severity,
27596
- priority,
27597
- content,
27598
- content_hash AS contentHash,
27599
- attributes`;
27600
- var SqliteHistorySyncRepository = class {
27601
- constructor(db) {
27602
- this.db = db;
27603
- this.ensureRowStmt = db.prepare(`INSERT OR IGNORE INTO history_sync (id) VALUES (1)`);
27604
- this.sessionsStmt = db.prepare(
27605
- `SELECT COALESCE(root_session_id, id) AS sessionId, MIN(started_at) AS earliest
27606
- FROM audit_events
27607
- WHERE synced_at IS NULL
27608
- AND event_type IN (${TYPE_LIST})
27609
- AND started_at < :before
27610
- GROUP BY sessionId
27611
- ORDER BY earliest
27612
- LIMIT :limit`
27613
- );
27614
- this.rowsStmt = db.prepare(
27615
- `SELECT ${ROW_COLUMNS}
27616
- FROM audit_events
27617
- WHERE synced_at IS NULL
27618
- AND event_type IN (${TYPE_LIST})
27619
- AND started_at < :before
27620
- AND COALESCE(root_session_id, id) = :sessionId
27621
- ORDER BY (event_type = 'session') DESC, started_at
27622
- LIMIT :limit`
27623
- );
27624
- this.captureRowsStmt = db.prepare(
27625
- `SELECT ${ROW_COLUMNS}
27626
- FROM audit_events
27627
- WHERE synced_at IS NULL
27628
- AND sync_claimed_at IS NULL
27629
- AND outbox_owed = 1
27630
- AND event_type IN (${CAPTURE_TYPE_LIST})
27631
- AND started_at < :before
27632
- ORDER BY started_at
27633
- LIMIT :limit`
27634
- );
27635
- this.markOwedStmt = db.prepare(
27636
- `UPDATE audit_events SET outbox_owed = 1 WHERE id = :id AND synced_at IS NULL`
27637
- );
27638
- this.stampStmt = db.prepare(
27639
- `UPDATE audit_events SET synced_at = :at, sync_claimed_at = NULL WHERE id = :id`
27640
- );
27641
- this.claimRowStmt = db.prepare(
27642
- `UPDATE audit_events SET sync_claimed_at = :at WHERE id = :id AND synced_at IS NULL`
27643
- );
27644
- this.releaseRowStmt = db.prepare(
27645
- `UPDATE audit_events SET sync_claimed_at = NULL WHERE id = :id`
27646
- );
27647
- this.releaseStaleClaimsStmt = db.prepare(
27648
- `UPDATE audit_events SET sync_claimed_at = NULL
27649
- WHERE sync_claimed_at IS NOT NULL AND sync_claimed_at < :staleBefore`
27650
- );
27651
- this.partitionStmt = db.prepare(
27652
- `SELECT
27653
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NULL THEN 1 ELSE 0 END) AS queued,
27654
- SUM(CASE WHEN synced_at IS NULL AND sync_claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS inProgress,
27655
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS synced,
27656
- SUM(CASE WHEN synced_at IS NOT NULL AND synced_at <= 0 THEN 1 ELSE 0 END) AS failed,
27657
- COUNT(*) AS total
27658
- FROM audit_events
27659
- WHERE event_type IN (${TYPE_LIST})`
27660
- );
27661
- this.countsStmt = db.prepare(
27662
- `SELECT
27663
- SUM(CASE WHEN synced_at IS NULL AND started_at < :before THEN 1 ELSE 0 END) AS pending,
27664
- SUM(CASE WHEN synced_at > 0 THEN 1 ELSE 0 END) AS sent,
27665
- SUM(CASE WHEN synced_at = ${String(SKIPPED)} THEN 1 ELSE 0 END) AS skipped
27666
- FROM audit_events
27667
- WHERE event_type IN (${TYPE_LIST})`
27668
- );
27669
- this.captureSkipCountStmt = db.prepare(
27670
- `SELECT COUNT(*) AS skipped
27671
- FROM audit_events
27672
- WHERE synced_at = ${String(SKIPPED)}
27673
- AND event_type IN (${CAPTURE_TYPE_LIST})`
27674
- );
27675
- this.fingerprintStmt = db.prepare(
27676
- `SELECT endpoint_fingerprint AS fingerprint, backlog_before AS backlogBefore
27677
- FROM history_sync WHERE id = 1`
27678
- );
27679
- this.setFingerprintStmt = db.prepare(
27680
- `UPDATE history_sync
27681
- SET endpoint_fingerprint = :fingerprint, backlog_before = :backlogBefore
27682
- WHERE id = 1`
27683
- );
27684
- this.disownCapturesStmt = db.prepare(
27685
- `UPDATE audit_events SET outbox_owed = NULL
27686
- WHERE outbox_owed IS NOT NULL AND event_type IN (${CAPTURE_TYPE_LIST})`
27687
- );
27688
- this.rearmStmt = db.prepare(
27689
- `UPDATE audit_events SET synced_at = NULL
27690
- WHERE synced_at > 0 AND event_type IN (${TYPE_LIST})`
27691
- );
27692
- this.claimStmt = db.prepare(
27693
- `UPDATE history_sync
27694
- SET owner_pid = :pid, owner_host = :host, acquired_at = :now, heartbeat_at = :now
27695
- WHERE id = 1
27696
- AND (owner_pid IS NULL
27697
- OR heartbeat_at IS NULL
27698
- OR heartbeat_at < :staleBefore
27699
- OR heartbeat_at > :now)`
27700
- );
27701
- this.heartbeatStmt = db.prepare(
27702
- `UPDATE history_sync SET heartbeat_at = :now WHERE id = 1 AND owner_pid = :pid`
27703
- );
27704
- this.releaseStmt = db.prepare(
27705
- `UPDATE history_sync
27706
- SET owner_pid = NULL, owner_host = NULL, acquired_at = NULL, heartbeat_at = NULL
27707
- WHERE id = 1 AND owner_pid = :pid`
27708
- );
27709
- this.closeWindowStmt = db.prepare(
27710
- `UPDATE audit_events SET synced_at = :at
27711
- WHERE synced_at IS NULL
27712
- AND event_type IN (${TYPE_LIST})
27713
- AND started_at >= :attachedAt`
27714
- );
27715
- this.releaseBoundaryStmt = db.prepare(
27716
- `UPDATE history_sync SET backlog_before = NULL WHERE id = 1`
27717
- );
27718
- this.freezeBoundaryStmt = db.prepare(
27719
- `UPDATE history_sync SET backlog_before = :backlogBefore WHERE id = 1`
27720
- );
27721
- this.leaseStmt = db.prepare(
27722
- `SELECT owner_pid AS ownerPid, owner_host AS ownerHost,
27723
- acquired_at AS acquiredAt, heartbeat_at AS heartbeatAt
27724
- FROM history_sync WHERE id = 1`
27725
- );
27726
- this.inspectionsStmt = db.prepare(
27727
- `SELECT d.rule_id AS ruleId,
27728
- d.name AS ruleName,
27729
- d.version AS ruleVersion,
27730
- d.category AS category,
27731
- d.severity AS severity,
27732
- f.span_start AS spanStart,
27733
- f.span_end AS spanEnd,
27734
- f.masked_match AS maskedMatch,
27735
- f.action_taken AS actionTaken,
27736
- f.confidence AS confidence
27737
- FROM inspection_findings f
27738
- JOIN inspection_definitions d ON d.id = f.inspection_definition_id
27739
- WHERE f.audit_event_id = :auditEventId
27740
- ORDER BY f.span_start, f.id`
27741
- );
27742
- }
27743
- db;
27744
- ensureRowStmt;
27745
- sessionsStmt;
27746
- rowsStmt;
27747
- stampStmt;
27748
- countsStmt;
27749
- fingerprintStmt;
27750
- setFingerprintStmt;
27751
- rearmStmt;
27752
- claimStmt;
27753
- heartbeatStmt;
27754
- releaseStmt;
27755
- leaseStmt;
27756
- inspectionsStmt;
27757
- closeWindowStmt;
27758
- releaseBoundaryStmt;
27759
- freezeBoundaryStmt;
27760
- captureRowsStmt;
27761
- markOwedStmt;
27762
- captureSkipCountStmt;
27763
- disownCapturesStmt;
27764
- partitionStmt;
27765
- claimRowStmt;
27766
- releaseRowStmt;
27767
- releaseStaleClaimsStmt;
27768
- /**
27769
- * The masked detections recorded against one tool call.
27770
- *
27771
- * These travel with the event because a tool call's target is not
27772
- * re-inspectable from the event alone — unlike a capture, where the text
27773
- * itself is re-scannable. What crosses is the masked match and the rule that
27774
- * produced it, never the value.
27775
- */
27776
- inspectionsFor(auditEventId) {
27777
- return allRows(this.inspectionsStmt, { auditEventId });
27778
- }
27779
- /**
27780
- * Sessions with structural rows still to send, oldest first.
27781
- *
27782
- * BOUNDED BY THE BACKLOG BOUNDARY, which is the whole correctness of this
27783
- * read. Anything recorded after the machine attached is the live forward
27784
- * path's to deliver; this drain exists for what was recorded before it, and a
27785
- * row both paths send is at best a duplicate request and at worst — for a
27786
- * session root — an overwrite of the inventory ids the live path resolved.
27787
- */
27788
- pendingSessions(limit, before) {
27789
- return allRows(this.sessionsStmt, { limit, before }).map(
27790
- (r) => r.sessionId
27791
- );
27792
- }
27793
- /** One session's undelivered structural rows within the backlog, root first. */
27794
- pendingRows(sessionId, limit, before) {
27795
- return allRows(this.rowsStmt, { sessionId, limit, before });
27796
- }
27797
- /**
27798
- * Captures this machine still owes the deployment, oldest first.
27799
- *
27800
- * Selected by the `outbox_owed` marker the attached forward path writes, not
27801
- * by a time window — see captureRowsStmt for why a window could not express
27802
- * this. `before` is the grace window that leaves a just-recorded capture to
27803
- * the live path.
27804
- */
27805
- pendingCaptureRows(limit, before) {
27806
- return allRows(this.captureRowsStmt, { limit, before });
27807
- }
27808
- /**
27809
- * Record that a capture is OWED to the deployment.
27810
- *
27811
- * Written by the attached forward path when a live send did not confirm
27812
- * delivery, and read by the drain as the whole of its eligibility test. It is
27813
- * a fact rather than an inference: the machine was attached, the send did not
27814
- * land, so the row is owed — which no time window can state, because the same
27815
- * window that holds the rows a past attachment left owed also holds every
27816
- * capture recorded while the machine was DETACHED, and those were never
27817
- * offered to anyone.
27818
- *
27819
- * Idempotent, and never un-set: `markSynced` settling the row is what takes it
27820
- * out of the drain's read.
27821
- */
27822
- markCaptureOwed(id) {
27823
- this.markOwedStmt.run({ id });
27824
- }
27825
- /** Record delivery. Called only AFTER the far side has accepted the rows. */
27826
- markSynced(ids, atMs) {
27827
- this.stampAll(ids, atMs);
27828
- }
27829
- /**
27830
- * Record that a row will never be sent.
27831
- *
27832
- * Reserved for a local defect — a row that cannot be rebuilt into a valid
27833
- * payload. A row that merely failed to reach the deployment stays NULL, so it
27834
- * is retried; marking those would turn one outage into permanent data loss.
27835
- */
27836
- markSkipped(ids) {
27837
- this.stampAll(ids, SKIPPED);
27838
- }
27839
- eachInTransaction(ids, run) {
27840
- if (ids.length === 0) return;
27841
- withTransaction(
27842
- this.db,
27843
- () => {
27844
- for (const id of ids) run(id);
27845
- },
27846
- "IMMEDIATE"
27847
- );
27848
- }
27849
- stampAll(ids, value) {
27850
- if (ids.length === 0) return;
27851
- withTransaction(
27852
- this.db,
27853
- () => {
27854
- for (const id of ids) this.stampStmt.run({ at: value, id });
27855
- },
27856
- "IMMEDIATE"
27857
- );
27858
- }
27859
- /**
27860
- * Claim rows as in-flight.
27861
- *
27862
- * Advisory in exactly the sense the lease is: it records that a send is in
27863
- * progress so a surface can say so, and a lost claim costs a row showing as
27864
- * queued while it is actually being sent. It is not exclusion — the far side
27865
- * settles a duplicate on the row id.
27866
- */
27867
- claimRows(ids, atMs) {
27868
- this.eachInTransaction(ids, (id) => this.claimRowStmt.run({ at: atMs, id }));
27869
- }
27870
- /** Give back a claim without settling — the send failed, the row is queued again. */
27871
- releaseRows(ids) {
27872
- this.eachInTransaction(ids, (id) => this.releaseRowStmt.run({ id }));
27873
- }
27874
- /**
27875
- * Clear claims older than `staleBefore`, and report how many were cleared.
27876
- *
27877
- * A process killed between claiming and settling leaves rows claimed with
27878
- * nothing left to settle them. Without this they read as "sending" for ever.
27879
- */
27880
- releaseStaleClaims(staleBefore) {
27881
- return Number(this.releaseStaleClaimsStmt.run({ staleBefore }).changes);
27882
- }
27883
- /**
27884
- * Every tracked row in exactly one delivery state.
27885
- *
27886
- * Takes no boundary on purpose. The boundary answers "what should the drain
27887
- * pick up now", which is a different question from "what state is this row
27888
- * in" — and a machine that has never attached has no boundary to pass, so
27889
- * requiring one would force a caller to invent one and report the whole store
27890
- * as queued.
27891
- */
27892
- partition() {
27893
- const row = getRow(this.partitionStmt, {});
27894
- return {
27895
- queued: row?.queued ?? 0,
27896
- inProgress: row?.inProgress ?? 0,
27897
- synced: row?.synced ?? 0,
27898
- failed: row?.failed ?? 0,
27899
- total: row?.total ?? 0
27900
- };
27901
- }
27902
- /** `pending` counts only what is inside the backlog; sent and skipped are totals. */
27903
- counts(before) {
27904
- const row = getRow(
27905
- this.countsStmt,
27906
- { before }
27907
- );
27908
- const captures = getRow(this.captureSkipCountStmt);
27909
- return {
27910
- pending: row?.pending ?? 0,
27911
- sent: row?.sent ?? 0,
27912
- skipped: row?.skipped ?? 0,
27913
- capturesSkipped: captures?.skipped ?? 0
27914
- };
27915
- }
27916
- /**
27917
- * The deployment the current stamps were made against, and where its backlog
27918
- * ends.
27919
- *
27920
- * READ-ONLY. An absent row reads as an absent deployment, which is what a
27921
- * machine that has never drained is — and every writer below seeds the row
27922
- * before it needs one, so nothing depends on this creating it. Keeping the
27923
- * write off the gate path matters because the gate runs on every pass while a
27924
- * write has to take the database's write lock.
27925
- */
27926
- deployment() {
27927
- const row = getRow(
27928
- this.fingerprintStmt
27929
- );
27930
- return {
27931
- fingerprint: row?.fingerprint ?? void 0,
27932
- backlogBefore: row?.backlogBefore ?? void 0
27933
- };
27934
- }
27935
- /**
27936
- * Point the ledger at a different deployment, discarding what it recorded
27937
- * about the previous one.
27938
- *
27939
- * Delivery is a fact about ONE recipient: rows sent to the deployment a
27940
- * machine has just left are undelivered as far as the new one is concerned.
27941
- * All three in one transaction, so a crash between them cannot leave stamps
27942
- * attributed to the wrong deployment, or a boundary that belongs to another.
27943
- *
27944
- * The boundary is written HERE and only here, which is what freezes it: a
27945
- * re-attach to the SAME deployment (a key rotation) leaves the fingerprint
27946
- * unchanged, so this never runs and the backlog does not widen back over rows
27947
- * the live path has since delivered.
27948
- */
27949
- rearmFor(fingerprint, backlogBefore) {
27950
- this.ensureRowStmt.run();
27951
- withTransaction(
27952
- this.db,
27953
- () => {
27954
- const previous = getRow(this.fingerprintStmt)?.fingerprint;
27955
- this.rearmStmt.run();
27956
- if (previous !== null && previous !== void 0 && previous !== fingerprint) {
27957
- this.disownCapturesStmt.run();
27958
- }
27959
- this.setFingerprintStmt.run({ fingerprint, backlogBefore });
27960
- },
27961
- "IMMEDIATE"
27962
- );
27963
- }
27964
- /**
27965
- * End the attached period: hand its rows to the live path, and release the
27966
- * boundary so the next attachment can freeze a new one.
27967
- *
27968
- * WITHOUT THIS, a detach and a re-attach to the same deployment leave a window
27969
- * nothing delivers. The fingerprint is unchanged, so the boundary is never
27970
- * re-frozen and stays at the FIRST attachment — while nothing forwards at all
27971
- * during the detached period, because the machine is not attached. Rows
27972
- * recorded in that window sit after the boundary and before the re-attach, so
27973
- * neither path takes them, and the pending count reports none outstanding.
27974
- *
27975
- * Stamping the attached window is not a claim that every one of those rows
27976
- * reached the deployment — the live path drops on failure and says so
27977
- * elsewhere. It records that they were ITS to deliver, which is exactly the
27978
- * status quo: they sit outside the frozen boundary today and are equally never
27979
- * re-sent. Making it explicit is what lets the boundary move.
27980
- *
27981
- * ONE TRANSACTION, so a crash cannot release the boundary while leaving the
27982
- * window unstamped — that half-state would re-send the whole attached period
27983
- * on the next attach, which is the failure the boundary exists to prevent.
27984
- */
27985
- closeAttachedWindow(attachedAtMs, atMs) {
27986
- this.ensureRowStmt.run();
27987
- withTransaction(
27988
- this.db,
27989
- () => {
27990
- const row = getRow(this.fingerprintStmt);
27991
- const from = row?.backlogBefore ?? attachedAtMs;
27992
- this.closeWindowStmt.run({ at: atMs, attachedAt: from });
27993
- this.releaseBoundaryStmt.run();
27994
- },
27995
- "IMMEDIATE"
27996
- );
27997
- }
27998
- /**
27999
- * Freeze a boundary for the deployment already on file, KEEPING the stamps.
28000
- *
28001
- * The re-attach half of the above. Distinct from `rearmFor`, which is for a
28002
- * different deployment and therefore discards what was delivered to the old
28003
- * one: here the recipient is the same, so everything already sent to it stays
28004
- * sent.
28005
- */
28006
- freezeBoundary(backlogBefore) {
28007
- this.ensureRowStmt.run();
28008
- this.freezeBoundaryStmt.run({ backlogBefore });
28009
- }
28010
- /** Take the claim, or report that someone live already holds it. */
28011
- claim(pid, host, nowMs, staleAfterMs) {
28012
- this.ensureRowStmt.run();
28013
- let taken = false;
28014
- withTransaction(
28015
- this.db,
28016
- () => {
28017
- const result = this.claimStmt.run({
28018
- pid,
28019
- host,
28020
- now: nowMs,
28021
- staleBefore: nowMs - staleAfterMs
28022
- });
28023
- taken = result.changes === 1;
28024
- },
28025
- "IMMEDIATE"
28026
- );
28027
- return taken;
28028
- }
28029
- /** Say the holder is still alive. A no-op once the claim has moved on. */
28030
- heartbeat(pid, nowMs) {
28031
- this.heartbeatStmt.run({ now: nowMs, pid });
28032
- }
28033
- /** Give the claim up. Scoped to this holder, so a taken-over claim is left be. */
28034
- release(pid) {
28035
- this.releaseStmt.run({ pid });
28036
- }
28037
- /** Who holds the claim, if anyone. Read-only, for the same reason as above. */
28038
- lease() {
28039
- return getRow(this.leaseStmt);
28040
- }
28041
- };
28042
-
28043
28748
  // ../../packages/persistence/src/repositories/inspection-definitions.ts
28044
28749
  var SqliteInspectionDefinitionsRepository = class {
28045
28750
  constructor(db) {
@@ -28206,7 +28911,8 @@ function managedSettingsPaths(platform2 = process.platform) {
28206
28911
  }
28207
28912
  return [posix.join("/etc", "aka", MANAGED_SETTINGS_FILENAME)];
28208
28913
  }
28209
- function readManagedSettings(paths = managedSettingsPaths()) {
28914
+ var testOnlyManagedPaths = null;
28915
+ function readManagedSettings(paths = testOnlyManagedPaths ?? managedSettingsPaths()) {
28210
28916
  for (const path of paths) {
28211
28917
  let text;
28212
28918
  try {
@@ -28241,6 +28947,7 @@ function overlayManagedSettings(settings, managed, now = () => /* @__PURE__ */ n
28241
28947
  if (values.vaultInlineReveal !== void 0) merged.vaultInlineReveal = values.vaultInlineReveal;
28242
28948
  if (values.dataSharesInPlace !== void 0) merged.dataSharesInPlace = values.dataSharesInPlace;
28243
28949
  if (values.redactFallback !== void 0) merged.redactFallback = values.redactFallback;
28950
+ if (values.bodyRetention !== void 0) merged.bodyRetention = values.bodyRetention;
28244
28951
  if (values.vaultConsent !== void 0) {
28245
28952
  merged.vaultConsent = values.vaultConsent ? (
28246
28953
  // Keep an existing valid grant so its acknowledgedAt survives; mint one
@@ -30686,7 +31393,7 @@ function toUtcDateString(ms) {
30686
31393
  return new Date(ms).toISOString().slice(0, 10);
30687
31394
  }
30688
31395
  function isTimeseriesSeverity(s) {
30689
- return s === "critical" || s === "high" || s === "medium";
31396
+ return s === "critical" || s === "high" || s === "medium" || s === "low";
30690
31397
  }
30691
31398
  var SqliteSecurityRepository = class {
30692
31399
  constructor(db, now = () => Date.now()) {
@@ -30748,7 +31455,7 @@ var SqliteSecurityRepository = class {
30748
31455
  ELSE 0
30749
31456
  END) AS open_at_rest
30750
31457
  FROM inspection_findings f
30751
- JOIN audit_events e ON e.id = f.audit_event_id
31458
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30752
31459
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
30753
31460
  LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
30754
31461
  ON latest.finding_key = f.finding_key
@@ -30815,12 +31522,16 @@ var SqliteSecurityRepository = class {
30815
31522
  const now = this.now();
30816
31523
  const windowStart = startOfUtcDay2(now) - (lenDays - 1) * DAY_MS4;
30817
31524
  const rows = this.findingsInRange(windowStart, now);
30818
- const points = Array.from({ length: numBuckets }, (_, i) => ({
30819
- timestamp: toUtcDateString(windowStart + i * bucketMs),
30820
- critical: 0,
30821
- high: 0,
30822
- medium: 0
30823
- }));
31525
+ const points = Array.from(
31526
+ { length: numBuckets },
31527
+ (_, i) => ({
31528
+ timestamp: toUtcDateString(windowStart + i * bucketMs),
31529
+ critical: 0,
31530
+ high: 0,
31531
+ medium: 0,
31532
+ low: 0
31533
+ })
31534
+ );
30824
31535
  for (const r of rows) {
30825
31536
  const idx = Math.floor((r.occurredAt - windowStart) / bucketMs);
30826
31537
  const bucket = points[idx];
@@ -30970,7 +31681,7 @@ var SqliteSecurityRepository = class {
30970
31681
  this.db.prepare(
30971
31682
  `SELECT e.repo AS repo, count(*) AS c
30972
31683
  FROM inspection_findings f
30973
- JOIN audit_events e ON e.id = f.audit_event_id
31684
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
30974
31685
  WHERE e.started_at >= :from AND e.started_at < :to
30975
31686
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
30976
31687
  AND e.repo IS NOT NULL
@@ -31038,6 +31749,7 @@ var SqliteSecurityRepository = class {
31038
31749
  `SELECT f.finding_key AS finding_key,
31039
31750
  d.rule_id AS rule_id,
31040
31751
  d.severity AS severity,
31752
+ e.repo AS repo,
31041
31753
  e.file_path AS path,
31042
31754
  COALESCE(f.first_detected_at, e.started_at) AS first_detected_at,
31043
31755
  latest.resolved_at AS latest_resolved_at
@@ -31057,6 +31769,7 @@ var SqliteSecurityRepository = class {
31057
31769
  const items = rows.map((r) => ({
31058
31770
  findingKey: r.finding_key,
31059
31771
  ruleId: r.rule_id,
31772
+ repo: r.repo ?? "",
31060
31773
  severity: r.severity,
31061
31774
  path: r.path ?? "",
31062
31775
  resolvedAt: new Date(r.latest_resolved_at).toISOString(),
@@ -31066,15 +31779,68 @@ var SqliteSecurityRepository = class {
31066
31779
  }));
31067
31780
  return Promise.resolve({ items });
31068
31781
  }
31782
+ /**
31783
+ * Per-rule tallies of the findings that are still OPEN, whole-store.
31784
+ *
31785
+ * Scoped by status rather than by time, because the card this feeds is a to-do
31786
+ * list: a secret committed three weeks ago and never rotated is still the most
31787
+ * important thing to fix, and any window hides it. It carried a "newest N
31788
+ * findings" cap and then a range; the first meant a different span on every
31789
+ * machine, and the second reported "no recommendations" over live exposure.
31790
+ *
31791
+ * `open` mirrors `deriveFindingStatus` — at-rest, minus resolved and dismissed —
31792
+ * so a row's count is exactly what `?status=open&type=<rule>` returns. Note that
31793
+ * is NOT `severitySummary`'s `openAtRest`, which keeps dismissed findings (a
31794
+ * dismissal is a judgement, not a remediation) and drops untracked legacy rows.
31795
+ * The two answer different questions and only this one has to match a link.
31796
+ *
31797
+ * Aggregated in SQL: the result is O(distinct rule × category × severity), so a
31798
+ * whole-store scope costs a grouped scan rather than a row per finding.
31799
+ */
31800
+ recommendationInputs() {
31801
+ const rows = allRows(
31802
+ this.db.prepare(
31803
+ `SELECT d.rule_id AS rule_id,
31804
+ d.category AS category,
31805
+ d.severity AS severity,
31806
+ COUNT(*) AS count
31807
+ FROM inspection_findings f
31808
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31809
+ JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31810
+ LEFT JOIN ${LATEST_RESOLUTION_BY_KEY_SQL} latest
31811
+ ON latest.finding_key = f.finding_key
31812
+ WHERE e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
31813
+ AND e.event_type = 'code_change'
31814
+ AND (
31815
+ f.finding_key IS NULL
31816
+ OR latest.status IS NULL
31817
+ OR latest.status NOT IN ('resolved', 'dismissed')
31818
+ )
31819
+ GROUP BY d.rule_id, d.category, d.severity`
31820
+ )
31821
+ );
31822
+ return Promise.resolve(
31823
+ rows.map((r) => ({
31824
+ ruleId: r.rule_id,
31825
+ category: r.category,
31826
+ severity: r.severity,
31827
+ count: r.count
31828
+ }))
31829
+ );
31830
+ }
31069
31831
  // Findings whose parent event occurred in [fromMs, toMs), with the parent's
31070
31832
  // epoch-millis timestamp. started_at is an INTEGER column, so the bounds stay
31071
31833
  // numeric and the JS aggregations bucket/split on ms directly.
31072
31834
  findingsInRange(fromMs, toMs) {
31073
31835
  const rows = allRows(
31074
31836
  this.db.prepare(
31075
- `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken
31837
+ // `rule_id`/`category` cost nothing extra: inspection_definitions is already
31838
+ // joined for `severity`, so they are two more columns off a row this read
31839
+ // already fetches. They feed the recommended-actions rollup.
31840
+ `SELECT e.started_at AS occurred_at, d.severity AS severity, f.action_taken AS action_taken,
31841
+ d.rule_id AS rule_id, d.category AS category
31076
31842
  FROM inspection_findings f
31077
- JOIN audit_events e ON e.id = f.audit_event_id
31843
+ JOIN audit_events e INDEXED BY idx_audit_capture_rollup ON e.id = f.audit_event_id
31078
31844
  JOIN inspection_definitions d ON d.id = f.inspection_definition_id
31079
31845
  WHERE e.started_at >= :from AND e.started_at < :to
31080
31846
  AND e.event_type IN (${CAPTURE_EVENT_TYPES_SQL})
@@ -31085,7 +31851,9 @@ var SqliteSecurityRepository = class {
31085
31851
  return rows.map((r) => ({
31086
31852
  occurredAt: r.occurred_at,
31087
31853
  severity: r.severity,
31088
- actionTaken: r.action_taken
31854
+ actionTaken: r.action_taken,
31855
+ ruleId: r.rule_id,
31856
+ category: r.category
31089
31857
  }));
31090
31858
  }
31091
31859
  };
@@ -31913,6 +32681,7 @@ function openWithPragmas(file2) {
31913
32681
  db.exec("PRAGMA journal_mode = WAL");
31914
32682
  db.exec("PRAGMA busy_timeout = 2000");
31915
32683
  db.exec("PRAGMA foreign_keys = ON");
32684
+ registerSqlFunctions(db);
31916
32685
  } catch (err) {
31917
32686
  closeQuietly(db);
31918
32687
  throw err;
@@ -31942,7 +32711,7 @@ function backupLegacyStore(db, file2) {
31942
32711
  discardStore(file2, backup);
31943
32712
  return backup;
31944
32713
  }
31945
- function openAndInitialize(file2, base) {
32714
+ function openAndInitialize(file2, base, skipTags) {
31946
32715
  let db = openWithPragmas(file2);
31947
32716
  try {
31948
32717
  if (isForeignSqliteLineage(db)) {
@@ -31952,7 +32721,7 @@ function openAndInitialize(file2, base) {
31952
32721
  `Detected an older, incompatible (tenant-bearing) ${DB_FILENAME}; backed it up to ${backup} and created a fresh store.`
31953
32722
  );
31954
32723
  }
31955
- applyMigrations(db, file2);
32724
+ applyMigrations(db, file2, { skipTags });
31956
32725
  tightenPerms(file2);
31957
32726
  const policies = new SqlitePoliciesRepository(db);
31958
32727
  const installedPacks = new SqliteInstalledPacksRepository(db, base);
@@ -31967,6 +32736,7 @@ function openAndInitialize(file2, base) {
31967
32736
  exceptions: new SqliteExceptionsRepository(db),
31968
32737
  resolutions: new SqliteResolutionsRepository(db),
31969
32738
  ruleProbeCache: new SqliteRuleProbeCacheRepository(db),
32739
+ bodyRetention: new SqliteBodyRetentionRepository(db),
31970
32740
  security: new SqliteSecurityRepository(db),
31971
32741
  detections: new SqliteDetectionsRepository(db),
31972
32742
  shares: new SqliteSharesRepository(db),
@@ -31989,7 +32759,8 @@ function openAndInitialize(file2, base) {
31989
32759
  throw err;
31990
32760
  }
31991
32761
  }
31992
- function openLocalDatabase(dir) {
32762
+ var DEFERRED_TAGS = new Set(DEFERRED_MIGRATION_TAGS);
32763
+ function openLocalDatabase(dir, options = {}) {
31993
32764
  ensureDataDirSync(dir);
31994
32765
  const file2 = join7(dir, DB_FILENAME);
31995
32766
  reapStalePartials(file2);
@@ -32001,6 +32772,7 @@ function openLocalDatabase(dir) {
32001
32772
  installedPacks,
32002
32773
  scanLedger,
32003
32774
  historySync,
32775
+ bodyRetention,
32004
32776
  secretVault,
32005
32777
  exceptions,
32006
32778
  resolutions,
@@ -32024,7 +32796,8 @@ function openLocalDatabase(dir) {
32024
32796
  // `dir` is always `<base>/data` — every caller resolves it through
32025
32797
  // `dataDir()` — so its parent is the `~/.aka` base the layout splits into
32026
32798
  // settings/ and data/, and the pack-policy floor needs both halves.
32027
- dirname2(dir)
32799
+ dirname2(dir),
32800
+ options.applyDeferredMigrations === true ? void 0 : DEFERRED_TAGS
32028
32801
  );
32029
32802
  function captureRowId(event) {
32030
32803
  return captureId(
@@ -32217,6 +32990,7 @@ function openLocalDatabase(dir) {
32217
32990
  installedPacks,
32218
32991
  scanLedger,
32219
32992
  historySync,
32993
+ bodyRetention,
32220
32994
  secretVault,
32221
32995
  exceptions,
32222
32996
  resolutions,
@@ -32255,8 +33029,12 @@ function openLocalDatabase(dir) {
32255
33029
  };
32256
33030
  }
32257
33031
 
32258
- // ../../packages/persistence/src/finding-key.ts
33032
+ // ../../packages/persistence/src/egress-wire.ts
32259
33033
  import { createHash as createHash3 } from "crypto";
33034
+ var SLASH = "/".charCodeAt(0);
33035
+
33036
+ // ../../packages/persistence/src/finding-key.ts
33037
+ import { createHash as createHash4 } from "crypto";
32260
33038
 
32261
33039
  // ../../packages/persistence/src/fingerprint.ts
32262
33040
  import { createHmac, randomBytes } from "crypto";
@@ -32264,14 +33042,113 @@ import { existsSync as existsSync3, readFileSync as readFileSync6 } from "fs";
32264
33042
  import { join as join8 } from "path";
32265
33043
  import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
32266
33044
 
32267
- // ../../packages/persistence/src/history-preview.ts
32268
- import { existsSync as existsSync4 } from "fs";
33045
+ // ../../packages/persistence/src/forward-health.ts
33046
+ import { readFileSync as readFileSync7 } from "fs";
32269
33047
  import { join as join9 } from "path";
33048
+ var FAILURES = /* @__PURE__ */ new Set([
33049
+ "unauthorized",
33050
+ "forbidden",
33051
+ "unreachable"
33052
+ ]);
33053
+ var BREAKER_COOLDOWN_MS = 3e4;
33054
+ function parseForwardHealth(raw, nowMs) {
33055
+ try {
33056
+ const parsed2 = JSON.parse(raw);
33057
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33058
+ const record2 = parsed2;
33059
+ const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33060
+ const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33061
+ const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33062
+ return { consecutiveFailures: failures, openedAtMs, lastFailure };
33063
+ } catch {
33064
+ return null;
33065
+ }
33066
+ }
33067
+ function readForwardHealth(dir, nowMs = Date.now()) {
33068
+ try {
33069
+ return parseForwardHealth(
33070
+ readFileSync7(join9(dir, ATTACHED_FORWARD_STATE_FILENAME), "utf8"),
33071
+ nowMs
33072
+ );
33073
+ } catch {
33074
+ return null;
33075
+ }
33076
+ }
33077
+ function isForwardPaused(health, nowMs) {
33078
+ const openedAtMs = health?.openedAtMs ?? null;
33079
+ if (openedAtMs === null) return false;
33080
+ return nowMs - openedAtMs < BREAKER_COOLDOWN_MS;
33081
+ }
33082
+
33083
+ // ../../packages/persistence/src/history-backfill.ts
33084
+ import { existsSync as existsSync4 } from "fs";
33085
+ import { join as join10 } from "path";
33086
+
33087
+ // ../../packages/persistence/src/history-preview.ts
33088
+ import { existsSync as existsSync5 } from "fs";
33089
+ import { join as join11 } from "path";
32270
33090
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
32271
33091
 
33092
+ // ../../packages/persistence/src/history-sync-state.ts
33093
+ import { readFileSync as readFileSync8 } from "fs";
33094
+ import { join as join12 } from "path";
33095
+ var HISTORY_SYNC_STATE_FILENAME = ATTACHED_HISTORY_SYNC_STATE_FILENAME;
33096
+ var PHASES = /* @__PURE__ */ new Set(["filling", "complete"]);
33097
+ var OUTCOMES = /* @__PURE__ */ new Set([
33098
+ "ok",
33099
+ "unreachable",
33100
+ "refused",
33101
+ "interrupted"
33102
+ ]);
33103
+ var SPEC_VERSION = 1;
33104
+ function historySyncStatePath(dataDir2) {
33105
+ return join12(dataDir2, HISTORY_SYNC_STATE_FILENAME);
33106
+ }
33107
+ function writeHistorySyncState(dataDir2, state) {
33108
+ try {
33109
+ ensureDataDirSync(dataDir2);
33110
+ const persisted = { specVersion: SPEC_VERSION, ...state };
33111
+ writeOwnerOnlyFileSync(historySyncStatePath(dataDir2), `${JSON.stringify(persisted)}
33112
+ `);
33113
+ } catch {
33114
+ }
33115
+ }
33116
+ function readHistorySyncState(dataDir2) {
33117
+ try {
33118
+ const parsed2 = JSON.parse(readFileSync8(historySyncStatePath(dataDir2), "utf8"));
33119
+ if (typeof parsed2 !== "object" || parsed2 === null) return null;
33120
+ const r = parsed2;
33121
+ if (r.specVersion !== SPEC_VERSION) return null;
33122
+ if (typeof r.phase !== "string" || !PHASES.has(r.phase)) return null;
33123
+ if (typeof r.lastOutcome !== "string" || !OUTCOMES.has(r.lastOutcome)) return null;
33124
+ if (!isCount(r.lastPassAtMs)) return null;
33125
+ if (!isCount(r.sentTotal) || !isCount(r.pendingTotal) || !isCount(r.skippedTotal)) return null;
33126
+ if (!isNullableCount(r.startedAtMs) || !isNullableCount(r.completedAtMs)) return null;
33127
+ return {
33128
+ specVersion: SPEC_VERSION,
33129
+ phase: r.phase,
33130
+ lastOutcome: r.lastOutcome,
33131
+ lastPassAtMs: r.lastPassAtMs,
33132
+ sentTotal: r.sentTotal,
33133
+ pendingTotal: r.pendingTotal,
33134
+ skippedTotal: r.skippedTotal,
33135
+ startedAtMs: r.startedAtMs,
33136
+ completedAtMs: r.completedAtMs
33137
+ };
33138
+ } catch {
33139
+ return null;
33140
+ }
33141
+ }
33142
+ function isCount(v) {
33143
+ return typeof v === "number" && Number.isFinite(v) && v >= 0;
33144
+ }
33145
+ function isNullableCount(v) {
33146
+ return v === null || isCount(v);
33147
+ }
33148
+
32272
33149
  // ../../packages/persistence/src/store-symlinks.ts
32273
- import { existsSync as existsSync5, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
32274
- import { dirname as dirname3, join as join10, resolve } from "path";
33150
+ import { existsSync as existsSync6, lstatSync as lstatSync3, readlinkSync, realpathSync, statSync as statSync4 } from "fs";
33151
+ import { dirname as dirname3, join as join13, resolve } from "path";
32275
33152
 
32276
33153
  // ../../packages/persistence/src/vault/crypto.ts
32277
33154
  import {
@@ -32285,15 +33162,15 @@ import {
32285
33162
  // ../../packages/persistence/src/vault/key-provider.ts
32286
33163
  import { execFileSync } from "child_process";
32287
33164
  import { randomBytes as randomBytes2 } from "crypto";
32288
- import { chmodSync as chmodSync3, readFileSync as readFileSync7, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
32289
- import { join as join11 } from "path";
33165
+ import { chmodSync as chmodSync3, readFileSync as readFileSync9, renameSync as renameSync4, rmSync as rmSync6, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
33166
+ import { join as join14 } from "path";
32290
33167
 
32291
33168
  // ../../packages/persistence/src/vault/vault.ts
32292
33169
  import { randomBytes as randomBytes3, randomUUID as randomUUID12 } from "crypto";
32293
33170
 
32294
33171
  // ../../packages/persistence/src/warn-era-cap.ts
32295
- import { existsSync as existsSync6, writeFileSync as writeFileSync4 } from "fs";
32296
- import { join as join12 } from "path";
33172
+ import { existsSync as existsSync7, writeFileSync as writeFileSync4 } from "fs";
33173
+ import { join as join15 } from "path";
32297
33174
 
32298
33175
  // ../../packages/remote/src/http.ts
32299
33176
  import { request as httpRequest } from "http";
@@ -32478,10 +33355,10 @@ function parsed(schema, body, route) {
32478
33355
  }
32479
33356
  function withoutTrailingSlashes(endpoint) {
32480
33357
  let end = endpoint.length;
32481
- while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH) end -= 1;
33358
+ while (end > 0 && endpoint.charCodeAt(end - 1) === SLASH2) end -= 1;
32482
33359
  return endpoint.slice(0, end);
32483
33360
  }
32484
- var SLASH = "/".charCodeAt(0);
33361
+ var SLASH2 = "/".charCodeAt(0);
32485
33362
  function createRemoteClient(options) {
32486
33363
  const base = withoutTrailingSlashes(options.endpoint);
32487
33364
  const url2 = (route) => `${base}${route}`;
@@ -32574,6 +33451,7 @@ function createRemoteClient(options) {
32574
33451
  url: url2(ROUTES.shares),
32575
33452
  body: JSON.stringify(validated.data)
32576
33453
  });
33454
+ if (response.status === 404) throw new RemoteRouteAbsent(ROUTES.shares);
32577
33455
  okBody(response);
32578
33456
  },
32579
33457
  async pollCommand() {
@@ -32597,18 +33475,17 @@ function createRemoteClient(options) {
32597
33475
  }
32598
33476
 
32599
33477
  // ../../packages/plugin-runtime/src/attached/forward-drops.ts
32600
- import { readFileSync as readFileSync8 } from "fs";
32601
- import { join as join13 } from "path";
33478
+ import { readFileSync as readFileSync10 } from "fs";
33479
+ import { join as join16 } from "path";
32602
33480
 
32603
33481
  // ../../packages/plugin-runtime/src/attached/forward-policy.ts
32604
33482
  import { randomUUID as randomUUID15 } from "crypto";
32605
- import { readFileSync as readFileSync14 } from "fs";
32606
33483
  import { readFile, rename, writeFile } from "fs/promises";
32607
- import { join as join22 } from "path";
33484
+ import { join as join26 } from "path";
32608
33485
 
32609
33486
  // ../../packages/plugin-sdk/src/config.ts
32610
- import { existsSync as existsSync7 } from "fs";
32611
- import { join as join14 } from "path";
33487
+ import { existsSync as existsSync8 } from "fs";
33488
+ import { join as join17 } from "path";
32612
33489
 
32613
33490
  // ../../packages/plugin-sdk/src/provider-env.ts
32614
33491
  var booleanish = external_exports.string().optional().transform((v) => {
@@ -32629,9 +33506,9 @@ var providerEnvShape = {
32629
33506
  var ProviderEnvSchema = external_exports.object(providerEnvShape);
32630
33507
 
32631
33508
  // ../../packages/plugin-sdk/src/config-inventory.ts
32632
- import { readdirSync as readdirSync2, readFileSync as readFileSync10, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
33509
+ import { readdirSync as readdirSync2, readFileSync as readFileSync12, realpathSync as realpathSync2, statSync as statSync7 } from "fs";
32633
33510
  import { homedir as homedir2 } from "os";
32634
- import { basename as basename3, join as join16 } from "path";
33511
+ import { basename as basename3, join as join19 } from "path";
32635
33512
 
32636
33513
  // ../../packages/detections/src/egress/registry.ts
32637
33514
  var EXTRACTOR_VERSION = "1";
@@ -33389,24 +34266,20 @@ var CPU_CORROBORATION_SHARE = 0.2;
33389
34266
  var CORROBORATION_FLOOR_MS = BUDGET_MS * CPU_CORROBORATION_SHARE;
33390
34267
 
33391
34268
  // ../../packages/plugin-sdk/src/repo.ts
33392
- import { existsSync as existsSync8, readFileSync as readFileSync9, statSync as statSync6 } from "fs";
33393
- import { basename as basename2, dirname as dirname4, isAbsolute, join as join15, sep as sep2 } from "path";
34269
+ import { existsSync as existsSync9, readFileSync as readFileSync11, statSync as statSync6 } from "fs";
34270
+ import { basename as basename2, dirname as dirname4, isAbsolute, join as join18, sep as sep2 } from "path";
33394
34271
 
33395
34272
  // ../../packages/plugin-sdk/src/events.ts
33396
34273
  import { createHash as createHash5, randomUUID as randomUUID13 } from "crypto";
33397
34274
 
33398
34275
  // ../../packages/plugin-sdk/src/isolated-scan.ts
33399
- import { existsSync as existsSync9 } from "fs";
34276
+ import { existsSync as existsSync10 } from "fs";
33400
34277
  import { fileURLToPath } from "url";
33401
34278
  import { Worker } from "worker_threads";
33402
34279
 
33403
- // ../../packages/plugin-sdk/src/ignore-layers.ts
33404
- var import_ignore = __toESM(require_ignore(), 1);
33405
- import { readFileSync as readFileSync11 } from "fs";
33406
- import { join as join17 } from "path";
33407
-
33408
- // ../../packages/plugin-sdk/src/inventory-resolver.ts
33409
- import { arch, hostname as hostname4, platform, release } from "os";
34280
+ // ../../packages/plugin-sdk/src/host-floor.ts
34281
+ import { readFileSync as readFileSync14 } from "fs";
34282
+ import { join as join21 } from "path";
33410
34283
 
33411
34284
  // ../../packages/plugin-sdk/src/model-governance.ts
33412
34285
  import {
@@ -33414,24 +34287,50 @@ import {
33414
34287
  fstatSync,
33415
34288
  mkdirSync as mkdirSync2,
33416
34289
  openSync as openSync2,
33417
- readFileSync as readFileSync12,
34290
+ readFileSync as readFileSync13,
33418
34291
  readSync,
33419
34292
  writeFileSync as writeFileSync5
33420
34293
  } from "fs";
33421
- import { join as join18 } from "path";
34294
+ import { join as join20 } from "path";
33422
34295
  var TAIL_BYTES = 256 * 1024;
33423
34296
 
34297
+ // ../../packages/plugin-sdk/src/host-floor.ts
34298
+ var HOST_FEATURE = {
34299
+ ModelSwitch: "model-switch",
34300
+ VaultPointerDisplay: "vault-pointer-display"
34301
+ };
34302
+ var HOST_FLOORS = {
34303
+ [HOST_FEATURE.ModelSwitch]: {
34304
+ label: "model-switch protection",
34305
+ hookEvents: ["PreModelSwitch", "PostModelSwitch"],
34306
+ since: "2.1.251"
34307
+ },
34308
+ [HOST_FEATURE.VaultPointerDisplay]: {
34309
+ label: "vault pointer display",
34310
+ hookEvents: ["MessageDisplay"],
34311
+ since: "2.1.152"
34312
+ }
34313
+ };
34314
+
34315
+ // ../../packages/plugin-sdk/src/ignore-layers.ts
34316
+ var import_ignore = __toESM(require_ignore(), 1);
34317
+ import { readFileSync as readFileSync15 } from "fs";
34318
+ import { join as join22 } from "path";
34319
+
34320
+ // ../../packages/plugin-sdk/src/inventory-resolver.ts
34321
+ import { arch, hostname as hostname4, platform, release } from "os";
34322
+
33424
34323
  // ../../packages/plugin-sdk/src/nudge.ts
33425
- import { mkdirSync as mkdirSync3, readFileSync as readFileSync13, writeFileSync as writeFileSync6 } from "fs";
33426
- import { join as join19 } from "path";
34324
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync16, writeFileSync as writeFileSync6 } from "fs";
34325
+ import { join as join23 } from "path";
33427
34326
 
33428
34327
  // ../../packages/plugin-sdk/src/paths.ts
33429
34328
  import { readdirSync as readdirSync3, realpathSync as realpathSync3 } from "fs";
33430
34329
  import { basename as basename4, dirname as dirname5, sep as sep3 } from "path";
33431
34330
 
33432
34331
  // ../../packages/plugin-sdk/src/project-files.ts
33433
- import { existsSync as existsSync10, readdirSync as readdirSync4 } from "fs";
33434
- import { basename as basename5, join as join20 } from "path";
34332
+ import { existsSync as existsSync11, readdirSync as readdirSync4 } from "fs";
34333
+ import { basename as basename5, join as join24 } from "path";
33435
34334
 
33436
34335
  // ../../packages/plugin-sdk/src/provider-env-antigravity.ts
33437
34336
  var optionalBaseUrl2 = external_exports.preprocess((v) => {
@@ -33467,94 +34366,7 @@ var THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1e3;
33467
34366
 
33468
34367
  // ../../packages/plugin-sdk/src/throttle.ts
33469
34368
  import { mkdirSync as mkdirSync4, statSync as statSync8, writeFileSync as writeFileSync7 } from "fs";
33470
- import { join as join21 } from "path";
33471
-
33472
- // ../../packages/plugin-runtime/src/attached/forward-policy.ts
33473
- var BREAKER_COOLDOWN_MS = 3e4;
33474
- var FAILURES = /* @__PURE__ */ new Set([
33475
- "unauthorized",
33476
- "forbidden",
33477
- "unreachable"
33478
- ]);
33479
- var FORWARD_STATE_FILENAME = ATTACHED_FORWARD_STATE_FILENAME;
33480
- var STATE_FILENAME = FORWARD_STATE_FILENAME;
33481
- function parseBreakerState(raw, nowMs) {
33482
- try {
33483
- const parsed2 = JSON.parse(raw);
33484
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
33485
- const record2 = parsed2;
33486
- const failures = typeof record2.consecutiveFailures === "number" && record2.consecutiveFailures >= 0 ? record2.consecutiveFailures : 0;
33487
- const openedAtMs = typeof record2.openedAtMs === "number" && Number.isFinite(record2.openedAtMs) && record2.openedAtMs <= nowMs ? record2.openedAtMs : null;
33488
- const lastFailure = typeof record2.lastFailure === "string" && FAILURES.has(record2.lastFailure) ? record2.lastFailure : null;
33489
- return { consecutiveFailures: failures, openedAtMs, lastFailure };
33490
- } catch {
33491
- return null;
33492
- }
33493
- }
33494
- function readForwardHealth(dir, nowMs = Date.now()) {
33495
- try {
33496
- return parseBreakerState(readFileSync14(join22(dir, STATE_FILENAME), "utf8"), nowMs);
33497
- } catch {
33498
- return null;
33499
- }
33500
- }
33501
-
33502
- // ../../packages/plugin-runtime/src/attached/history-state.ts
33503
- import { readFileSync as readFileSync15 } from "fs";
33504
- import { join as join23 } from "path";
33505
- var HISTORY_SYNC_STATE_FILENAME = ATTACHED_HISTORY_SYNC_STATE_FILENAME;
33506
- var PHASES = /* @__PURE__ */ new Set(["filling", "complete"]);
33507
- var OUTCOMES = /* @__PURE__ */ new Set([
33508
- "ok",
33509
- "unreachable",
33510
- "refused",
33511
- "interrupted"
33512
- ]);
33513
- var SPEC_VERSION = 1;
33514
- function historySyncStatePath(dataDir2) {
33515
- return join23(dataDir2, HISTORY_SYNC_STATE_FILENAME);
33516
- }
33517
- function writeHistorySyncState(dataDir2, state) {
33518
- try {
33519
- ensureDataDirSync(dataDir2);
33520
- const persisted = { specVersion: SPEC_VERSION, ...state };
33521
- writeOwnerOnlyFileSync(historySyncStatePath(dataDir2), `${JSON.stringify(persisted)}
33522
- `);
33523
- } catch {
33524
- }
33525
- }
33526
- function readHistorySyncState(dataDir2) {
33527
- try {
33528
- const parsed2 = JSON.parse(readFileSync15(historySyncStatePath(dataDir2), "utf8"));
33529
- if (typeof parsed2 !== "object" || parsed2 === null) return null;
33530
- const r = parsed2;
33531
- if (r.specVersion !== SPEC_VERSION) return null;
33532
- if (typeof r.phase !== "string" || !PHASES.has(r.phase)) return null;
33533
- if (typeof r.lastOutcome !== "string" || !OUTCOMES.has(r.lastOutcome)) return null;
33534
- if (!isCount(r.lastPassAtMs)) return null;
33535
- if (!isCount(r.sentTotal) || !isCount(r.pendingTotal) || !isCount(r.skippedTotal)) return null;
33536
- if (!isNullableCount(r.startedAtMs) || !isNullableCount(r.completedAtMs)) return null;
33537
- return {
33538
- specVersion: SPEC_VERSION,
33539
- phase: r.phase,
33540
- lastOutcome: r.lastOutcome,
33541
- lastPassAtMs: r.lastPassAtMs,
33542
- sentTotal: r.sentTotal,
33543
- pendingTotal: r.pendingTotal,
33544
- skippedTotal: r.skippedTotal,
33545
- startedAtMs: r.startedAtMs,
33546
- completedAtMs: r.completedAtMs
33547
- };
33548
- } catch {
33549
- return null;
33550
- }
33551
- }
33552
- function isCount(v) {
33553
- return typeof v === "number" && Number.isFinite(v) && v >= 0;
33554
- }
33555
- function isNullableCount(v) {
33556
- return v === null || isCount(v);
33557
- }
34369
+ import { join as join25 } from "path";
33558
34370
 
33559
34371
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
33560
34372
  import { createHash as createHash6 } from "crypto";
@@ -33601,7 +34413,20 @@ function rebuildCapture(row) {
33601
34413
  // it, IngestEvent.safeParse then refuses the assembled event, and the whole
33602
34414
  // capture becomes a permanent skip. That is the per-row door the rest of this
33603
34415
  // function exists to avoid, and this field was the last one still using it.
33604
- ...withList("exceptionIds", keepAll(attributes.exception_ids, EXCEPTION_ID))
34416
+ ...withList("exceptionIds", keepAll(attributes.exception_ids, EXCEPTION_ID)),
34417
+ // Carried, for the same reason exceptionIds is: it says WHY an enforced
34418
+ // detection let a value through, and a capture that means one thing live
34419
+ // and another when drained is worse than one that carries neither.
34420
+ //
34421
+ // Parsed rather than type-checked. The bag is free-form JSON, so a hand
34422
+ // edit or an older build can leave any string here; an unparseable one is
34423
+ // dropped rather than assembled into an event the deployment would refuse
34424
+ // with a 400 — which, on a drain that reads the head of the unstamped set
34425
+ // with no cursor, is the per-row door that retires the whole lane.
34426
+ ...withField(
34427
+ "redactDegradedTo",
34428
+ keep(stringOrUndefined(attributes.redact_degraded_to), REDACT_DEGRADED_TO)
34429
+ )
33605
34430
  // inspectionMs is DELIBERATELY not carried. It measures latency a live host
33606
34431
  // session actually waited on, and a row being drained hours later is not
33607
34432
  // that; the field's own contract says a replay leaves it absent rather than
@@ -33630,6 +34455,7 @@ function keepAll(value, schema) {
33630
34455
  var CORRELATION_ID = EventMetadata.shape.correlationId;
33631
34456
  var TRACE_ID = EventMetadata.shape.traceId;
33632
34457
  var EXCEPTION_ID = EventMetadata.shape.exceptionIds.unwrap().element;
34458
+ var REDACT_DEGRADED_TO = EventMetadata.shape.redactDegradedTo.unwrap();
33633
34459
  function isoOrUndefined(epochMs) {
33634
34460
  if (!Number.isFinite(epochMs)) return void 0;
33635
34461
  const iso = new Date(epochMs);
@@ -33709,7 +34535,6 @@ function attributesOf(raw) {
33709
34535
  // ../../packages/plugin-runtime/src/attached/history-sync.ts
33710
34536
  var HISTORY_REQUEST_TIMEOUT_MS = 15e3;
33711
34537
  var HISTORY_PASS_BUDGET_MS = 12e4;
33712
- var HISTORY_LEASE_STALE_MS = 6e4;
33713
34538
  var HEARTBEAT_EVERY_MS = 1e4;
33714
34539
  var PACE_INTERVAL_MS = 200;
33715
34540
  var MAX_ATTEMPTS = 4;
@@ -33718,6 +34543,23 @@ var SESSION_PAGE = 25;
33718
34543
  var ROW_PAGE = 200;
33719
34544
  var CAPTURE_GRACE_MS = 3e4;
33720
34545
  var CAPTURE_BATCH_SIZE = INGEST_BATCH_MAX;
34546
+ var CAPTURE_BATCH_BYTES = 1024 * 1024;
34547
+ function underByteCeiling(ready) {
34548
+ const fitting = [];
34549
+ const oversized = [];
34550
+ let bytes = 0;
34551
+ for (const item of ready) {
34552
+ const size = Buffer.byteLength(item.event.content, "utf8");
34553
+ if (size > CAPTURE_BATCH_BYTES) {
34554
+ oversized.push(item.id);
34555
+ continue;
34556
+ }
34557
+ if (bytes + size > CAPTURE_BATCH_BYTES) break;
34558
+ bytes += size;
34559
+ fitting.push(item);
34560
+ }
34561
+ return { fitting, oversized };
34562
+ }
33721
34563
  var STRUCTURAL_BUDGET_SHARE = 0.7;
33722
34564
  var BATCH_SIZE = AUDIT_EVENT_BATCH_MAX;
33723
34565
  function endpointFingerprint(endpoint) {
@@ -33738,14 +34580,16 @@ async function runHistorySync(deps) {
33738
34580
  if (!isAttached(settings)) return didNotRun("not-attached");
33739
34581
  const connection = settings.controlPlane;
33740
34582
  if (connection === void 0) return didNotRun("not-attached");
33741
- if (!isHistorySyncConsentValid(settings.historySyncConsent, connection.endpoint)) {
34583
+ const consent = settings.historySyncConsent;
34584
+ if (!isHistorySyncConsentValid(consent, connection.endpoint) || consent === void 0) {
33742
34585
  return didNotRun("no-consent");
33743
34586
  }
34587
+ const consentAcknowledgedAtMs = Date.parse(consent.acknowledgedAt);
34588
+ if (!Number.isFinite(consentAcknowledgedAtMs)) return didNotRun("attachment-unreadable");
33744
34589
  const state = readControlPlaneCredentialFile(deps.settingsDir, connection);
33745
34590
  if (!state.usable) return didNotRun("credential-unusable");
33746
34591
  const nowMs = now();
33747
- const openedAtMs = readForwardHealth(deps.dataDir, nowMs)?.openedAtMs ?? null;
33748
- if (openedAtMs !== null && nowMs - openedAtMs < BREAKER_COOLDOWN_MS) {
34592
+ if (isForwardPaused(readForwardHealth(deps.dataDir, nowMs), nowMs)) {
33749
34593
  return didNotRun("breaker-open");
33750
34594
  }
33751
34595
  db = (deps.openStore ?? openLocalDatabase)(deps.dataDir);
@@ -33756,7 +34600,7 @@ async function runHistorySync(deps) {
33756
34600
  const recorded = ledger.deployment();
33757
34601
  let backlogBefore;
33758
34602
  if (recorded.fingerprint !== fingerprint) {
33759
- ledger.rearmFor(fingerprint, attachedAtMs);
34603
+ ledger.rearmFor(fingerprint, attachedAtMs, consentAcknowledgedAtMs);
33760
34604
  backlogBefore = attachedAtMs;
33761
34605
  } else if (recorded.backlogBefore === void 0) {
33762
34606
  ledger.freezeBoundary(attachedAtMs);
@@ -33765,7 +34609,7 @@ async function runHistorySync(deps) {
33765
34609
  backlogBefore = recorded.backlogBefore;
33766
34610
  }
33767
34611
  const pid = process.pid;
33768
- if (!ledger.claim(pid, hostname5(), now(), HISTORY_LEASE_STALE_MS)) {
34612
+ if (!ledger.claim(pid, hostname5(), now(), HISTORY_SYNC_LEASE_STALE_MS)) {
33769
34613
  return didNotRun("already-running");
33770
34614
  }
33771
34615
  const client = deps.sendBatch !== void 0 && deps.sendCaptures !== void 0 && deps.sendOne !== void 0 ? void 0 : createRemoteClient({
@@ -33815,6 +34659,7 @@ async function runHistorySync(deps) {
33815
34659
  async function drain(d) {
33816
34660
  const startedAt = d.now();
33817
34661
  const deadline = startedAt + d.budgetMs;
34662
+ d.ledger.releaseStaleClaims(startedAt - HISTORY_SYNC_LEASE_STALE_MS);
33818
34663
  const structuralDeadline = Math.min(deadline, startedAt + d.budgetMs * STRUCTURAL_BUDGET_SHARE);
33819
34664
  let sent = 0;
33820
34665
  let skipped = 0;
@@ -33837,7 +34682,7 @@ async function drain(d) {
33837
34682
  for (const row of rows) {
33838
34683
  const event = rebuildAuditEvent(row, d.ledger.inspectionsFor(row.id));
33839
34684
  if (event === void 0) {
33840
- d.ledger.markSkipped([row.id]);
34685
+ d.ledger.markSkipped([row.id], d.now());
33841
34686
  skipped += 1;
33842
34687
  continue;
33843
34688
  }
@@ -33849,7 +34694,14 @@ async function drain(d) {
33849
34694
  break outer;
33850
34695
  }
33851
34696
  const chunk = ready.slice(i, i + BATCH_SIZE);
33852
- const result = await sendChunk(d, chunk, beat);
34697
+ const chunkIds = chunk.map((c) => c.id);
34698
+ d.ledger.claimRows(chunkIds, d.now());
34699
+ let result;
34700
+ try {
34701
+ result = await sendChunk(d, chunk, beat);
34702
+ } finally {
34703
+ releaseClaim(d, chunkIds);
34704
+ }
33853
34705
  sent += result.sent;
33854
34706
  skipped += result.skipped;
33855
34707
  if (result.stopped !== void 0) {
@@ -33907,7 +34759,7 @@ async function drainCaptures(d, deadline, beat) {
33907
34759
  ready.push({ id: row.id, event });
33908
34760
  }
33909
34761
  if (unbuildable.length > 0) {
33910
- d.ledger.markSkipped(unbuildable);
34762
+ d.ledger.markSkipped(unbuildable, d.now());
33911
34763
  skipped += unbuildable.length;
33912
34764
  }
33913
34765
  if (ready.length === 0) {
@@ -33915,7 +34767,24 @@ async function drainCaptures(d, deadline, beat) {
33915
34767
  await d.sleep(PACE_INTERVAL_MS);
33916
34768
  continue;
33917
34769
  }
33918
- const result = await sendCaptureChunk(d, ready, beat);
34770
+ const { fitting, oversized } = underByteCeiling(ready);
34771
+ if (oversized.length > 0) {
34772
+ d.ledger.markSkipped(oversized, d.now());
34773
+ skipped += oversized.length;
34774
+ }
34775
+ if (fitting.length === 0) {
34776
+ beat();
34777
+ await d.sleep(PACE_INTERVAL_MS);
34778
+ continue;
34779
+ }
34780
+ const readyIds = fitting.map((r) => r.id);
34781
+ d.ledger.claimRows(readyIds, d.now());
34782
+ let result;
34783
+ try {
34784
+ result = await sendCaptureChunk(d, fitting, beat);
34785
+ } finally {
34786
+ releaseClaim(d, readyIds);
34787
+ }
33919
34788
  sent += result.sent;
33920
34789
  skipped += result.skipped;
33921
34790
  if (result.stopped !== void 0) return { sent, skipped, stopped: result.stopped };
@@ -33943,7 +34812,7 @@ async function sendCaptureChunk(d, chunk, beat) {
33943
34812
  {
33944
34813
  const only = chunk.length === 1 ? chunk[0] : void 0;
33945
34814
  if (only !== void 0) {
33946
- d.ledger.markSkipped([only.id]);
34815
+ recordGaveUp(d, [only.id], outcome.verdict);
33947
34816
  return { sent: 0, skipped: 1 };
33948
34817
  }
33949
34818
  return await isolate(d, chunk, beat);
@@ -33970,7 +34839,8 @@ async function sendCapturesWithRetries(d, chunk, beat) {
33970
34839
  } catch (err) {
33971
34840
  const kind = classify(err);
33972
34841
  if (kind === "refused") return { verdict: "refused" };
33973
- if (kind === "skip") return { verdict: "skip" };
34842
+ if (kind === "payload-invalid") return { verdict: "payload-invalid" };
34843
+ if (kind === "deployment-refused") return { verdict: "deployment-refused" };
33974
34844
  if (attempt === MAX_ATTEMPTS - 1) return { verdict: "unreachable" };
33975
34845
  const ceiling = Math.min(MAX_BACKOFF_MS, 1e3 * 2 ** attempt);
33976
34846
  beat();
@@ -34012,7 +34882,7 @@ async function sendSingleRow(d, one, beat) {
34012
34882
  if (outcome.verdict === "refused" || outcome.verdict === "unreachable") {
34013
34883
  return { sent: 0, skipped: 0, stopped: outcome.verdict };
34014
34884
  }
34015
- d.ledger.markSkipped([one.id]);
34885
+ recordGaveUp(d, [one.id], outcome.verdict);
34016
34886
  return { sent: 0, skipped: 1 };
34017
34887
  }
34018
34888
  async function isolateStructural(d, chunk, beat) {
@@ -34036,7 +34906,8 @@ async function sendWithRetries(d, attempt, beat) {
34036
34906
  } catch (err) {
34037
34907
  const kind = classify(err);
34038
34908
  if (kind === "refused") return { verdict: "refused" };
34039
- if (kind === "skip") return { verdict: "skip" };
34909
+ if (kind === "payload-invalid") return { verdict: "payload-invalid" };
34910
+ if (kind === "deployment-refused") return { verdict: "deployment-refused" };
34040
34911
  if (i === MAX_ATTEMPTS - 1) return { verdict: "unreachable" };
34041
34912
  const ceiling = Math.min(MAX_BACKOFF_MS, 1e3 * 2 ** i);
34042
34913
  beat();
@@ -34045,6 +34916,22 @@ async function sendWithRetries(d, attempt, beat) {
34045
34916
  }
34046
34917
  return { verdict: "unreachable" };
34047
34918
  }
34919
+ function releaseClaim(d, ids) {
34920
+ try {
34921
+ d.ledger.releaseRows(ids);
34922
+ } catch {
34923
+ }
34924
+ }
34925
+ function recordGaveUp(d, ids, verdict) {
34926
+ switch (verdict) {
34927
+ case "deployment-refused":
34928
+ d.ledger.markRefused(ids, d.now());
34929
+ return;
34930
+ case "payload-invalid":
34931
+ d.ledger.markSkipped(ids, d.now());
34932
+ return;
34933
+ }
34934
+ }
34048
34935
  function statusOf2(err) {
34049
34936
  if (typeof err !== "object" || err === null || !("status" in err)) return null;
34050
34937
  const { status } = err;
@@ -34052,7 +34939,7 @@ function statusOf2(err) {
34052
34939
  return status >= 100 && status <= 599 ? status : null;
34053
34940
  }
34054
34941
  function classify(err) {
34055
- if (err.name === "RemoteRequestInvalid") return "skip";
34942
+ if (err.name === "RemoteRequestInvalid") return "payload-invalid";
34056
34943
  switch (statusOf2(err)) {
34057
34944
  // Terminal in a way a timeout is not: the credential may have died with an
34058
34945
  // offboarded member, and every later row would fail the same way.
@@ -34060,11 +34947,11 @@ function classify(err) {
34060
34947
  case 403:
34061
34948
  return "refused";
34062
34949
  // The deployment understood the request and rejected it. Re-sending an
34063
- // identical body cannot change that.
34950
+ // identical body TO THIS DEPLOYMENT cannot change that.
34064
34951
  case 400:
34065
34952
  case 413:
34066
34953
  case 422:
34067
- return "skip";
34954
+ return "deployment-refused";
34068
34955
  default:
34069
34956
  return "retry";
34070
34957
  }
@@ -34100,7 +34987,18 @@ async function runHistorySyncPass(base = defaultDataDir(), seams = {}) {
34100
34987
  // field whose capture half is one pass wide, so the surface beside
34101
34988
  // `sentTotal` and `pendingTotal` would announce a permanent loss once and
34102
34989
  // drop it on the next pass, while the rows stayed gone.
34103
- skippedTotal: result.counts.skipped + result.counts.capturesSkipped,
34990
+ //
34991
+ // EVERY reason the store can name, not just the original one. Splitting
34992
+ // `skipped` into its reasons gave the store a truer answer and silently
34993
+ // shrank this number twice over: a structural row a deployment rejected,
34994
+ // and a window a detach closed over undelivered, both used to land in
34995
+ // `skipped` and reach a surface, and after the splits each reached
34996
+ // nothing. A bucket this sum does not name is a row that disappears from
34997
+ // the only total anyone sees.
34998
+ //
34999
+ // The capture term needs no counterpart: that lane keeps both of its
35000
+ // reasons in one total, because neither is ever freed there.
35001
+ skippedTotal: result.counts.skipped + result.counts.refused + result.counts.detached + result.counts.capturesSkipped,
34104
35002
  // The first pass that ran is when this machine started sending, and it
34105
35003
  // keeps that answer across every later pass.
34106
35004
  startedAtMs: previous?.startedAtMs ?? result.atMs,
@@ -34132,12 +35030,12 @@ import { fileURLToPath as fileURLToPath2 } from "url";
34132
35030
  var HISTORY_SYNC_THROTTLE_MS = 5 * 60 * 1e3;
34133
35031
 
34134
35032
  // ../../packages/plugin-runtime/src/attached/plugin-block.ts
34135
- import { readFileSync as readFileSync16 } from "fs";
35033
+ import { readFileSync as readFileSync17 } from "fs";
34136
35034
 
34137
35035
  // ../../packages/plugin-runtime/src/attached/policy-store.ts
34138
35036
  import { randomUUID as randomUUID16 } from "crypto";
34139
35037
  import { readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
34140
- import { join as join24 } from "path";
35038
+ import { join as join27 } from "path";
34141
35039
 
34142
35040
  // ../../packages/plugin-runtime/src/attached/atomic-publish.ts
34143
35041
  import { rename as rename2 } from "fs/promises";
@@ -34152,11 +35050,11 @@ import { DatabaseSync as DatabaseSync4 } from "node:sqlite";
34152
35050
  // ../../packages/plugin-runtime/src/attached/posture-store.ts
34153
35051
  import { randomUUID as randomUUID17 } from "crypto";
34154
35052
  import { readFile as readFile3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
34155
- import { join as join25 } from "path";
35053
+ import { join as join28 } from "path";
34156
35054
 
34157
35055
  // ../../packages/plugin-runtime/src/attached/sync-state.ts
34158
- import { readFileSync as readFileSync17 } from "fs";
34159
- import { join as join26 } from "path";
35056
+ import { readFileSync as readFileSync18 } from "fs";
35057
+ import { join as join29 } from "path";
34160
35058
 
34161
35059
  // ../../packages/plugin-runtime/src/attached/status.ts
34162
35060
  var REFUSAL_LINES = {
@@ -34177,6 +35075,14 @@ import { spawn as spawn2 } from "child_process";
34177
35075
  import { fileURLToPath as fileURLToPath3 } from "url";
34178
35076
  var SYNC_THROTTLE_MS = 15 * 60 * 1e3;
34179
35077
 
35078
+ // ../../packages/plugin-runtime/src/content-retention-pass.ts
35079
+ var MAX_ROWS_PER_SWEEP = 50 * 1e3;
35080
+
35081
+ // ../../packages/plugin-runtime/src/content-retention-trigger.ts
35082
+ import { spawn as spawn3 } from "child_process";
35083
+ import { fileURLToPath as fileURLToPath4 } from "url";
35084
+ var CONTENT_RETENTION_THROTTLE_MS = 60 * 60 * 1e3;
35085
+
34180
35086
  // ../../packages/plugin-runtime/src/attached/factory.ts
34181
35087
  import { hostname as hostname6 } from "os";
34182
35088