@apralabs/apra-fleet 0.4.4 → 0.4.5

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.
Files changed (268) hide show
  1. package/README.md +49 -4
  2. package/dist/agents/backlog-groomer.md +5 -0
  3. package/dist/agents/ci-watcher.md +5 -0
  4. package/dist/agents/deployer.md +20 -2
  5. package/dist/agents/doer.md +38 -2
  6. package/dist/agents/harvester.md +12 -9
  7. package/dist/agents/integ-test-runner.md +5 -0
  8. package/dist/agents/kb-reconciler.md +6 -0
  9. package/dist/agents/plan-reviewer.md +5 -0
  10. package/dist/agents/planner.md +5 -0
  11. package/dist/agents/regression-test-runner.md +5 -0
  12. package/dist/agents/reviewer.md +67 -15
  13. package/dist/agents/schemas/deployer-output.json +5 -0
  14. package/dist/agents/schemas/doer-output.json +78 -2
  15. package/dist/agents/schemas/reviewer-output.json +104 -2
  16. package/dist/cli/auth.js +5 -1
  17. package/dist/cli/beads-pin.js +6 -7
  18. package/dist/cli/claude-mod.d.ts +75 -0
  19. package/dist/cli/claude-mod.js +288 -0
  20. package/dist/cli/dolt-install.d.ts +1 -1
  21. package/dist/cli/dolt-install.js +5 -1
  22. package/dist/cli/fleet-se-guard.d.ts +8 -0
  23. package/dist/cli/fleet-se-guard.js +19 -3
  24. package/dist/cli/install-guard.d.ts +9 -0
  25. package/dist/cli/install-guard.js +24 -0
  26. package/dist/cli/install.d.ts +12 -0
  27. package/dist/cli/install.js +180 -20
  28. package/dist/cli/kb-profile.d.ts +16 -0
  29. package/dist/cli/kb-profile.js +81 -0
  30. package/dist/cli/start.js +3 -0
  31. package/dist/cli/uninstall.js +13 -3
  32. package/dist/cli/update.d.ts +4 -0
  33. package/dist/cli/update.js +19 -3
  34. package/dist/commands/kb-server.d.ts +10 -1
  35. package/dist/commands/kb-server.js +285 -11
  36. package/dist/index.js +22 -1
  37. package/dist/os/git-executable.d.ts +57 -0
  38. package/dist/os/git-executable.js +85 -0
  39. package/dist/os/go-concurrency.d.ts +44 -0
  40. package/dist/os/go-concurrency.js +53 -0
  41. package/dist/os/index.d.ts +1 -0
  42. package/dist/os/index.js +1 -0
  43. package/dist/os/linux.d.ts +2 -0
  44. package/dist/os/linux.js +10 -1
  45. package/dist/os/os-commands.d.ts +25 -0
  46. package/dist/os/os-commands.js +2 -0
  47. package/dist/os/windows-executable.d.ts +53 -0
  48. package/dist/os/windows-executable.js +185 -0
  49. package/dist/os/windows-gitbash.d.ts +23 -14
  50. package/dist/os/windows-gitbash.js +40 -16
  51. package/dist/os/windows.d.ts +2 -0
  52. package/dist/os/windows.js +37 -6
  53. package/dist/paths.d.ts +10 -1
  54. package/dist/paths.js +19 -2
  55. package/dist/services/agy-project.d.ts +7 -1
  56. package/dist/services/agy-project.js +7 -2
  57. package/dist/services/auth-socket.d.ts +18 -0
  58. package/dist/services/auth-socket.js +30 -4
  59. package/dist/services/credential-store.d.ts +33 -0
  60. package/dist/services/credential-store.js +79 -2
  61. package/dist/services/http-transport.d.ts +29 -0
  62. package/dist/services/http-transport.js +166 -37
  63. package/dist/services/kb-tool-profile.d.ts +36 -0
  64. package/dist/services/kb-tool-profile.js +88 -0
  65. package/dist/services/knowledge/bible-blob-id.js +10 -1
  66. package/dist/services/knowledge/bible-import.d.ts +108 -0
  67. package/dist/services/knowledge/bible-import.js +137 -4
  68. package/dist/services/knowledge/file-hash.js +6 -5
  69. package/dist/services/knowledge/http-provider.d.ts +303 -20
  70. package/dist/services/knowledge/http-provider.js +795 -125
  71. package/dist/services/knowledge/kb-config.d.ts +65 -1
  72. package/dist/services/knowledge/kb-config.js +143 -3
  73. package/dist/services/knowledge/kb-http-auth-state.d.ts +26 -0
  74. package/dist/services/knowledge/kb-http-auth-state.js +40 -0
  75. package/dist/services/knowledge/kb-providers.d.ts +9 -2
  76. package/dist/services/knowledge/kb-providers.js +67 -37
  77. package/dist/services/knowledge/kb-removed-scope-keys.js +1 -1
  78. package/dist/services/knowledge/kb-self.d.ts +122 -9
  79. package/dist/services/knowledge/kb-self.js +196 -36
  80. package/dist/services/knowledge/member-bible-view.js +16 -3
  81. package/dist/services/knowledge/path-validation.d.ts +7 -0
  82. package/dist/services/knowledge/path-validation.js +11 -0
  83. package/dist/services/knowledge/project-slug.js +3 -2
  84. package/dist/services/knowledge/repository-header.d.ts +77 -0
  85. package/dist/services/knowledge/repository-header.js +233 -0
  86. package/dist/services/knowledge/require-sqlite-project.d.ts +1 -1
  87. package/dist/services/knowledge/require-sqlite-project.js +1 -1
  88. package/dist/services/knowledge/sqlite-provider.d.ts +169 -0
  89. package/dist/services/knowledge/sqlite-provider.js +416 -4
  90. package/dist/services/knowledge/types.d.ts +15 -2
  91. package/dist/services/member-busy.d.ts +12 -0
  92. package/dist/services/member-busy.js +20 -0
  93. package/dist/services/member-config-io.d.ts +3 -2
  94. package/dist/services/member-config-io.js +7 -4
  95. package/dist/services/member-fleet-install.js +1 -0
  96. package/dist/services/member-tool-allowlist.d.ts +1 -1
  97. package/dist/services/member-tool-allowlist.js +8 -4
  98. package/dist/services/port-holder.js +2 -1
  99. package/dist/services/service-manager/types.d.ts +17 -1
  100. package/dist/services/service-manager/windows.d.ts +7 -2
  101. package/dist/services/service-manager/windows.js +28 -11
  102. package/dist/services/service-notice.d.ts +7 -0
  103. package/dist/services/service-notice.js +19 -0
  104. package/dist/services/session-registry.d.ts +13 -0
  105. package/dist/services/session-registry.js +13 -0
  106. package/dist/services/session-roots.d.ts +67 -0
  107. package/dist/services/session-roots.js +126 -0
  108. package/dist/services/statusline.d.ts +21 -0
  109. package/dist/services/statusline.js +97 -1
  110. package/dist/services/supervisor-service-cleanup.js +2 -1
  111. package/dist/services/tool-registry.js +108 -20
  112. package/dist/services/tool-scope.d.ts +34 -1
  113. package/dist/services/tool-scope.js +27 -2
  114. package/dist/services/vcs/azure-devops.js +10 -0
  115. package/dist/services/vcs/bitbucket.d.ts +22 -1
  116. package/dist/services/vcs/bitbucket.js +163 -17
  117. package/dist/services/vcs/types.d.ts +25 -2
  118. package/dist/services/watch/project-resolver.js +2 -1
  119. package/dist/skills/pm/cost.md +5 -1
  120. package/dist/skills/pm/index.md +9 -9
  121. package/dist/skills/pm/sprint.md +2 -1
  122. package/dist/tools/check-status.d.ts +51 -0
  123. package/dist/tools/check-status.js +124 -12
  124. package/dist/tools/code-intelligence-freshness.js +1 -1
  125. package/dist/tools/code-intelligence-gitnexus.d.ts +19 -0
  126. package/dist/tools/code-intelligence-gitnexus.js +166 -9
  127. package/dist/tools/code-intelligence-readiness.d.ts +58 -1
  128. package/dist/tools/code-intelligence-readiness.js +93 -8
  129. package/dist/tools/code-intelligence-reindex.d.ts +25 -1
  130. package/dist/tools/code-intelligence-reindex.js +76 -4
  131. package/dist/tools/code-intelligence.d.ts +1 -1
  132. package/dist/tools/code-intelligence.js +1 -1
  133. package/dist/tools/execute-command.js +14 -8
  134. package/dist/tools/execute-prompt.js +6 -2
  135. package/dist/tools/fleet-snapshot.d.ts +21 -0
  136. package/dist/tools/fleet-snapshot.js +78 -0
  137. package/dist/tools/kb-bible-commit.d.ts +25 -11
  138. package/dist/tools/kb-bible-commit.js +142 -33
  139. package/dist/tools/kb-capture.d.ts +2 -2
  140. package/dist/tools/kb-context.js +47 -27
  141. package/dist/tools/kb-demote.d.ts +27 -0
  142. package/dist/tools/kb-demote.js +37 -0
  143. package/dist/tools/kb-export.d.ts +35 -3
  144. package/dist/tools/kb-export.js +56 -13
  145. package/dist/tools/kb-feedback.d.ts +2 -0
  146. package/dist/tools/kb-feedback.js +17 -7
  147. package/dist/tools/kb-harvest.js +28 -9
  148. package/dist/tools/kb-import.d.ts +18 -2
  149. package/dist/tools/kb-import.js +24 -9
  150. package/dist/tools/kb-invalidate.d.ts +11 -3
  151. package/dist/tools/kb-invalidate.js +80 -7
  152. package/dist/tools/kb-list.js +38 -10
  153. package/dist/tools/kb-promote.js +6 -4
  154. package/dist/tools/kb-query.d.ts +3 -0
  155. package/dist/tools/kb-query.js +181 -37
  156. package/dist/tools/kb-session-prime.d.ts +4 -4
  157. package/dist/tools/kb-session-prime.js +89 -9
  158. package/dist/tools/kb-setup.d.ts +20 -0
  159. package/dist/tools/kb-setup.js +149 -11
  160. package/dist/tools/list-members.js +1 -1
  161. package/dist/tools/member-detail.js +7 -3
  162. package/dist/tools/provision-vcs-auth.d.ts +28 -3
  163. package/dist/tools/provision-vcs-auth.js +53 -7
  164. package/dist/tools/receive-files.js +7 -4
  165. package/dist/tools/send-files.js +5 -4
  166. package/dist/tools/send-message.d.ts +3 -0
  167. package/dist/tools/send-message.js +15 -2
  168. package/dist/tools/stop-prompt.js +3 -1
  169. package/dist/tools/vcs-credential-exec.d.ts +45 -3
  170. package/dist/tools/vcs-credential-exec.js +90 -11
  171. package/dist/utils/process-utils.js +5 -1
  172. package/dist/utils/redact-known-secrets.d.ts +17 -0
  173. package/dist/utils/redact-known-secrets.js +70 -0
  174. package/dist/utils/skill-matrix.js +1 -1
  175. package/dist/utils/vcs-provider-detect.js +2 -2
  176. package/dist/workflows/auto-sprint.js +245 -9
  177. package/package.json +10 -7
  178. package/packages/apra-fleet-client/package.json +1 -1
  179. package/packages/apra-fleet-client/src/client/api.mjs +172 -19
  180. package/packages/apra-fleet-client/src/client/reconnecting-transport.mjs +35 -2
  181. package/packages/apra-fleet-client/src/client/transport.mjs +6 -0
  182. package/packages/apra-fleet-se/apra-pm/agents/backlog-groomer.md +5 -0
  183. package/packages/apra-fleet-se/apra-pm/agents/ci-watcher.md +5 -0
  184. package/packages/apra-fleet-se/apra-pm/agents/deployer.md +20 -2
  185. package/packages/apra-fleet-se/apra-pm/agents/doer.md +38 -2
  186. package/packages/apra-fleet-se/apra-pm/agents/harvester.md +12 -9
  187. package/packages/apra-fleet-se/apra-pm/agents/integ-test-runner.md +5 -0
  188. package/packages/apra-fleet-se/apra-pm/agents/kb-reconciler.md +6 -0
  189. package/packages/apra-fleet-se/apra-pm/agents/plan-reviewer.md +5 -0
  190. package/packages/apra-fleet-se/apra-pm/agents/planner.md +5 -0
  191. package/packages/apra-fleet-se/apra-pm/agents/regression-test-runner.md +5 -0
  192. package/packages/apra-fleet-se/apra-pm/agents/reviewer.md +67 -15
  193. package/packages/apra-fleet-se/apra-pm/agents/schemas/deployer-output.json +5 -0
  194. package/packages/apra-fleet-se/apra-pm/agents/schemas/doer-output.json +78 -2
  195. package/packages/apra-fleet-se/apra-pm/agents/schemas/reviewer-output.json +104 -2
  196. package/packages/apra-fleet-se/apra-pm/skills/pm/cost.md +5 -1
  197. package/packages/apra-fleet-se/apra-pm/skills/pm/index.md +9 -9
  198. package/packages/apra-fleet-se/apra-pm/skills/pm/sprint.md +2 -1
  199. package/packages/apra-fleet-se/bin/cli.mjs +104 -31
  200. package/packages/apra-fleet-se/bin/serve.mjs +46 -8
  201. package/packages/apra-fleet-se/fleet-sprint/abort.mjs +184 -32
  202. package/packages/apra-fleet-se/fleet-sprint/bd-sync-command.mjs +69 -0
  203. package/packages/apra-fleet-se/fleet-sprint/beads-children.mjs +21 -0
  204. package/packages/apra-fleet-se/fleet-sprint/beads-lock-probe.mjs +176 -0
  205. package/packages/apra-fleet-se/fleet-sprint/beads-reachability.mjs +126 -0
  206. package/packages/apra-fleet-se/fleet-sprint/beads-scope.mjs +158 -1
  207. package/packages/apra-fleet-se/fleet-sprint/contracts.mjs +82 -2
  208. package/packages/apra-fleet-se/fleet-sprint/coordination.mjs +23 -6
  209. package/packages/apra-fleet-se/fleet-sprint/decision-notify.mjs +338 -0
  210. package/packages/apra-fleet-se/fleet-sprint/decision-wait.mjs +731 -0
  211. package/packages/apra-fleet-se/fleet-sprint/decisions.mjs +372 -0
  212. package/packages/apra-fleet-se/fleet-sprint/dispatch-role.mjs +44 -11
  213. package/packages/apra-fleet-se/fleet-sprint/docs/README.md +4 -1
  214. package/packages/apra-fleet-se/fleet-sprint/dolt-settle.mjs +9 -3
  215. package/packages/apra-fleet-se/fleet-sprint/dolt-sync.mjs +314 -19
  216. package/packages/apra-fleet-se/fleet-sprint/errors.mjs +72 -4
  217. package/packages/apra-fleet-se/fleet-sprint/git-sync.mjs +7 -1
  218. package/packages/apra-fleet-se/fleet-sprint/git-topology.mjs +112 -1
  219. package/packages/apra-fleet-se/fleet-sprint/guarded-modules.mjs +36 -2
  220. package/packages/apra-fleet-se/fleet-sprint/kb.mjs +736 -101
  221. package/packages/apra-fleet-se/fleet-sprint/member-init-probe.mjs +1 -1
  222. package/packages/apra-fleet-se/fleet-sprint/member-provisioning.mjs +7 -1
  223. package/packages/apra-fleet-se/fleet-sprint/phases/deploy.mjs +49 -2
  224. package/packages/apra-fleet-se/fleet-sprint/phases/develop.mjs +206 -3
  225. package/packages/apra-fleet-se/fleet-sprint/phases/final-review.mjs +61 -3
  226. package/packages/apra-fleet-se/fleet-sprint/phases/harvest.mjs +81 -31
  227. package/packages/apra-fleet-se/fleet-sprint/phases/plan.mjs +7 -3
  228. package/packages/apra-fleet-se/fleet-sprint/phases/publish-pr.mjs +39 -14
  229. package/packages/apra-fleet-se/fleet-sprint/phases/re-review.mjs +17 -1
  230. package/packages/apra-fleet-se/fleet-sprint/phases/review.mjs +18 -3
  231. package/packages/apra-fleet-se/fleet-sprint/pr-body.mjs +14 -1
  232. package/packages/apra-fleet-se/fleet-sprint/prompts.mjs +32 -5
  233. package/packages/apra-fleet-se/fleet-sprint/runner.js +824 -98
  234. package/packages/apra-fleet-se/fleet-sprint/se-os-commands.mjs +4 -2
  235. package/packages/apra-fleet-se/fleet-sprint/se-posix.mjs +54 -1
  236. package/packages/apra-fleet-se/fleet-sprint/se-windows.mjs +29 -2
  237. package/packages/apra-fleet-se/fleet-sprint/set-aside.mjs +288 -0
  238. package/packages/apra-fleet-se/fleet-sprint/skills/fleet-supervisor/SKILL.md +3 -2
  239. package/packages/apra-fleet-se/fleet-sprint/sprint-args.mjs +20 -0
  240. package/packages/apra-fleet-se/fleet-sprint/sprint-report.mjs +21 -0
  241. package/packages/apra-fleet-se/fleet-sprint/sprint-state.mjs +9 -0
  242. package/packages/apra-fleet-se/fleet-sprint/vcs-auth.mjs +605 -32
  243. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/bitbucket.mjs +811 -17
  244. package/packages/apra-fleet-se/fleet-sprint/vcs-providers/index.mjs +89 -1
  245. package/packages/apra-fleet-se/fleet-sprint/viewer-extensions.mjs +225 -2
  246. package/packages/apra-fleet-se/fleet-sprint/worklists.mjs +59 -3
  247. package/packages/apra-fleet-se/src/supervisor/api.mjs +148 -8
  248. package/packages/apra-fleet-se/src/supervisor/dashboard.mjs +6 -3
  249. package/packages/apra-fleet-se/src/supervisor/dolt-mutex.mjs +30 -10
  250. package/packages/apra-fleet-se/src/supervisor/fleet-members.mjs +17 -4
  251. package/packages/apra-fleet-se/src/supervisor/history.mjs +8 -1
  252. package/packages/apra-fleet-se/src/supervisor/id-allocator.mjs +92 -2
  253. package/packages/apra-fleet-se/src/supervisor/proxy.mjs +26 -1
  254. package/packages/apra-fleet-se/src/supervisor/server.mjs +151 -8
  255. package/packages/apra-fleet-se/src/supervisor/spawner.mjs +11 -9
  256. package/packages/apra-fleet-se/src/supervisor/watchdog.mjs +7 -1
  257. package/packages/apra-fleet-workflow/package.json +2 -1
  258. package/packages/apra-fleet-workflow/src/viewer/index.mjs +86 -5
  259. package/packages/apra-fleet-workflow/src/viewer/request-guard.mjs +222 -0
  260. package/packages/apra-fleet-workflow/src/workflow/index.mjs +34 -2
  261. package/plugins/claude-code/fleet-status/.claude-plugin/plugin.json +7 -0
  262. package/plugins/claude-code/fleet-status/hooks/hooks.json +1 -0
  263. package/plugins/claude-code/fleet-status/hooks/register.tsx +906 -0
  264. package/plugins/claude-code/fleet-status/types/index.d.ts +88 -0
  265. package/skills/fleet/auth-bitbucket.md +47 -20
  266. package/skills/fleet/onboarding.md +1 -1
  267. package/skills/fleet/skill-matrix.md +3 -3
  268. package/version.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { KB_REMOVED_SCOPE_KEYS_SHAPE } from '../services/knowledge/kb-removed-scope-keys.js';
3
- import { getSelfReadKb } from '../services/knowledge/kb-self.js';
3
+ import { getSelfReadKb, getMemberSplitRead, mergeSplitById } from '../services/knowledge/kb-self.js';
4
+ import { HttpKbProvider } from '../services/knowledge/http-provider.js';
4
5
  import { requireSqliteProject } from '../services/knowledge/require-sqlite-project.js';
5
6
  // T3.3 (F8a, D8): kb_list -- a read-only audit view over the CONFIRMED (or any
6
7
  // filtered) set. Distinct from kb_query: no FTS, no L2 expansion, and
@@ -35,22 +36,49 @@ function normaliseConfidence(confidence) {
35
36
  }
36
37
  export async function kbList(input, anchor) {
37
38
  const confidence = normaliseConfidence(input.confidence);
38
- // MEMBER session -> the checkout bible view (kb-self.ts getSelfReadKb).
39
- // An explicit INFERRED/UNVERIFIED request goes to the per-repo DB, own
40
- // captures only (ownerTag).
41
- const { providers, ownerTag } = await getSelfReadKb(anchor, confidence);
42
- const sqliteProvider = requireSqliteProject(providers.project, 'kb_list');
43
- const entries = await sqliteProvider.list({
44
- confidence: confidence?.length ? confidence : DEFAULT_CONFIDENCE,
39
+ const filters = {
45
40
  // Default read is CONFIRMED *and* undisputed; an explicit tier list opts out.
46
41
  exclude_disputed: !confidence?.length,
47
42
  type: input.type,
48
43
  module: input.module,
49
44
  symbol: input.symbol,
50
45
  tag: input.tag,
51
- owner_tag: ownerTag,
52
46
  limit: input.limit,
53
- });
47
+ };
48
+ let entries;
49
+ // MEMBER mixed-tier request (CONFIRMED plus another tier): CONFIRMED from
50
+ // the checkout bible view, plus every requested tier from the per-repo DB,
51
+ // own captures only (kb-self.ts getMemberSplitRead). Merged by id (bible copy
52
+ // wins), re-sorted into list()'s id order, then limited. Under an http
53
+ // project provider both halves are the KB server's list route (the own half
54
+ // owner-tagged); the bible is not used.
55
+ const split = await getMemberSplitRead(anchor, confidence, 'kb_list');
56
+ if (split) {
57
+ const [fromBible, fromOwn] = await Promise.all([
58
+ split.bible.project.list({ ...filters, confidence: split.bible.tiers }),
59
+ split.own.project.list({ ...filters, confidence: split.own.tiers, owner_tag: split.own.ownerTag }),
60
+ ]);
61
+ entries = mergeSplitById(fromBible, fromOwn, {
62
+ order: (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
63
+ limit: input.limit,
64
+ });
65
+ }
66
+ else {
67
+ // MEMBER session -> the checkout bible view (kb-self.ts getSelfReadKb).
68
+ // A request naming only INFERRED/UNVERIFIED goes to the per-repo DB, own
69
+ // captures only (ownerTag). Under an http project provider every session
70
+ // reads the server's list route (an optional route: a server without it
71
+ // is E-KB-HTTP-UNSUPPORTED).
72
+ const { providers, ownerTag } = await getSelfReadKb(anchor, confidence);
73
+ const project = providers.project instanceof HttpKbProvider
74
+ ? providers.project
75
+ : requireSqliteProject(providers.project, 'kb_list');
76
+ entries = await project.list({
77
+ ...filters,
78
+ confidence: confidence?.length ? confidence : DEFAULT_CONFIDENCE,
79
+ owner_tag: ownerTag,
80
+ });
81
+ }
54
82
  const results = entries.map(e => ({
55
83
  id: e.id,
56
84
  type: e.type,
@@ -1,7 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { KB_REMOVED_SCOPE_KEYS_SHAPE } from '../services/knowledge/kb-removed-scope-keys.js';
3
3
  import { getSelfKbProviders, memberOwnerTag } from '../services/knowledge/kb-self.js';
4
- import { requireSqliteProject } from '../services/knowledge/require-sqlite-project.js';
5
4
  export const kbPromoteSchema = z.object({
6
5
  id: z.string().min(1).describe('ID of the KB entry to promote'),
7
6
  reason: z.string().optional().describe('Reason for promotion (appended to content as evidence trail)'),
@@ -11,11 +10,14 @@ export const kbPromoteSchema = z.object({
11
10
  });
12
11
  export async function kbPromote(input, anchor) {
13
12
  const providers = await getSelfKbProviders(anchor);
14
- // MEMBER session: only the caller's own captures (member:<uuid>) can be
15
- // promoted; any other id is the same not-found as an unknown id.
13
+ // MEMBER session over SQLite: only the caller's own captures (member:<uuid>)
14
+ // can be promoted; any other id is the same not-found as an unknown id.
15
+ // Over http fleet does NOT tag-check: the promote is sent by id and the KB
16
+ // server enforces which ids the caller may promote (its project scope and
17
+ // any per-caller scope it keeps), refusing the rest itself.
16
18
  const ownerTag = memberOwnerTag(anchor);
17
19
  const result = ownerTag !== undefined
18
- ? await requireSqliteProject(providers.project, 'kb_promote').promote(input.id, input.reason, { ownerTag })
20
+ ? await providers.project.promote(input.id, input.reason, { ownerTag })
19
21
  : await providers.project.promote(input.id, input.reason);
20
22
  return JSON.stringify({
21
23
  id: result.id,
@@ -13,6 +13,7 @@ export declare const kbQuerySchema: z.ZodObject<{
13
13
  expand_related: z.ZodOptional<z.ZodBoolean>;
14
14
  confidence: z.ZodOptional<z.ZodArray<z.ZodEnum<["CONFIRMED", "INFERRED", "UNVERIFIED"]>, "many">>;
15
15
  exclude_disputed: z.ZodOptional<z.ZodBoolean>;
16
+ own_scope: z.ZodOptional<z.ZodBoolean>;
16
17
  }, "strip", z.ZodTypeAny, {
17
18
  tag?: string | undefined;
18
19
  query?: string | undefined;
@@ -26,6 +27,7 @@ export declare const kbQuerySchema: z.ZodObject<{
26
27
  repo_path?: unknown;
27
28
  repo_remote_url?: unknown;
28
29
  expand_related?: boolean | undefined;
30
+ own_scope?: boolean | undefined;
29
31
  }, {
30
32
  tag?: string | undefined;
31
33
  query?: string | undefined;
@@ -39,6 +41,7 @@ export declare const kbQuerySchema: z.ZodObject<{
39
41
  repo_path?: unknown;
40
42
  repo_remote_url?: unknown;
41
43
  expand_related?: boolean | undefined;
44
+ own_scope?: boolean | undefined;
42
45
  }>;
43
46
  export type KbQueryInput = z.infer<typeof kbQuerySchema>;
44
47
  export declare function kbQuery(input: KbQueryInput, anchor?: KbAnchor): Promise<string>;
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { KB_REMOVED_SCOPE_KEYS_SHAPE } from '../services/knowledge/kb-removed-scope-keys.js';
3
- import { getSelfReadKb } from '../services/knowledge/kb-self.js';
4
- import { requireSqliteProject } from '../services/knowledge/require-sqlite-project.js';
3
+ import { getSelfReadKb, getMemberSplitRead, mergeSplitById, memberOwnerTag, } from '../services/knowledge/kb-self.js';
4
+ import { HttpKbProvider, KbHttpUnsupportedError } from '../services/knowledge/http-provider.js';
5
5
  const L2_CONTENT_CAP = 3200;
6
6
  export const kbQuerySchema = z.object({
7
7
  query: z.string().min(1).optional().describe('Free-text search string. Required unless flagged_only is true or tag is provided.'),
@@ -26,6 +26,13 @@ export const kbQuerySchema = z.object({
26
26
  .describe('Only return entries whose confidence tier is in this list (e.g. ["CONFIRMED"]). Applies to l1_results, l2_expanded and related_claims alike. Default when omitted: ["CONFIRMED"] -- INFERRED and UNVERIFIED entries are returned only when listed explicitly. Ignored when flagged_only is true.'),
27
27
  exclude_disputed: z.boolean().optional()
28
28
  .describe('Drop entries on either side of an unresolved contradiction (flagged_for_review, or contradiction_of set). Applies to l1_results, l2_expanded and related_claims alike. Default true when confidence is omitted (CONFIRMED-undisputed default); default false when confidence is given explicitly. Ignored when flagged_only is true.'),
29
+ // MEMBER own-scope escape hatch from the bible view (kb-self.ts
30
+ // getSelfReadKb): the own half of the member split read
31
+ // (getMemberSplitRead) on its own. Opt-in and default-off: every existing
32
+ // caller's routing (CONFIRMED -> bible view, INFERRED/UNVERIFIED -> per-repo
33
+ // DB, both -> split) is unchanged unless this is set.
34
+ own_scope: z.boolean().optional()
35
+ .describe('MEMBER session only. A default CONFIRMED read is answered from the checkout bible view, where every row is untagged -- a tag: "member:<id>" filter can never match there. Set true to force the per-repo DB instead, with the caller\'s own ownerTag applied, so a member can read back a CONFIRMED row it itself promoted (e.g. to check what it may kb_demote). For a request naming CONFIRMED plus another tier it drops the bible half of the merged member read and returns only the own half (the caller\'s own captures at every requested tier). No effect for a FULL session, an in-process caller passing an explicit anchor, or a request naming only INFERRED/UNVERIFIED -- those already read the per-repo DB. It changes which store answers and nothing else, so to span everything kb_demote accepts combine it with confidence: ["CONFIRMED"] and include_stale: true: a stale or contradiction-flagged CONFIRMED row is still demotable, but the default read drops stale rows and (with no explicit confidence list) disputed ones. The widened read also returns superseded rows, which kb_demote refuses -- drop those yourself.'),
29
36
  // Removed pre-redesign scope keys: declared only so a caller still passing one
30
37
  // is refused with E-SCOPE-KEY-REMOVED instead of silently re-scoped.
31
38
  ...KB_REMOVED_SCOPE_KEYS_SHAPE,
@@ -43,6 +50,120 @@ function passesTrustFilter(e, input, ownerTag) {
43
50
  return false;
44
51
  return true;
45
52
  }
53
+ // The flagged_only predicate (the same condition SqliteProvider.query applies
54
+ // in SQL): either half of a contradiction pair.
55
+ function isFlagged(e) {
56
+ return Boolean(e.flagged_for_review || e.contradiction_of);
57
+ }
58
+ // One provider's L1 page with the trust filter re-applied (see passesTrustFilter).
59
+ async function trustedL1(provider, queryOpts, input, ownerTag) {
60
+ const first = await provider.query(queryOpts);
61
+ const filtering = Boolean(input.confidence?.length || input.exclude_disputed || ownerTag);
62
+ if (!filtering)
63
+ return first.results;
64
+ let kept = first.results.filter(e => passesTrustFilter(e, input, ownerTag));
65
+ // A provider that ignored the filter may have spent its whole limit on
66
+ // entries just dropped here. A full page that filtered short is the only
67
+ // signal of that (the sqlite provider filters in SQL, so it never trips
68
+ // this); re-ask once with a wider window and trim back to the limit.
69
+ if (kept.length < queryOpts.limit && first.results.length >= queryOpts.limit) {
70
+ const wider = await provider.query({ ...queryOpts, limit: queryOpts.limit * 4 });
71
+ kept = wider.results.filter(e => passesTrustFilter(e, input, ownerTag));
72
+ }
73
+ return kept.slice(0, queryOpts.limit);
74
+ }
75
+ function capL2Content(e) {
76
+ return {
77
+ ...e,
78
+ content: e.content.length > L2_CONTENT_CAP
79
+ ? e.content.slice(0, L2_CONTENT_CAP) + '...[truncated]'
80
+ : e.content,
81
+ };
82
+ }
83
+ // MEMBER mixed-tier query (kb-self.ts getMemberSplitRead). Each half is
84
+ // queried with only its own tiers and owner tag: the CONFIRMED half (bible
85
+ // view + global, untagged) exactly as a CONFIRMED-only member read, the own
86
+ // half (every requested tier, CONFIRMED included) from the per-repo DB +
87
+ // global with the member's owner tag.
88
+ //
89
+ // Ranking: provider ranks (FTS bm25) are not comparable across two databases,
90
+ // so the merged order is fixed: the bible half in its own rank order, then the
91
+ // own half in its rank order. The limit is applied per side AFTER that merge
92
+ // (project side = bible view + per-repo DB, global side = both global halves),
93
+ // mirroring the per-provider limit of the unsplit read, so the CONFIRMED-only
94
+ // result is always a prefix of each side and the mixed result a superset of it.
95
+ async function splitQuery(split, raw) {
96
+ const input = { ...raw, exclude_disputed: raw.exclude_disputed ?? false };
97
+ const limit = input.limit ?? 20;
98
+ const baseOpts = {
99
+ query: input.query,
100
+ type: input.type,
101
+ tag: input.tag,
102
+ limit,
103
+ l1_only: true,
104
+ include_stale: input.include_stale ?? false,
105
+ include_superseded: input.include_stale ?? false,
106
+ exclude_disputed: input.exclude_disputed,
107
+ };
108
+ const bibleInput = { ...input, confidence: split.bible.tiers };
109
+ const ownInput = { ...input, confidence: split.own.tiers };
110
+ const bibleOpts = { ...baseOpts, confidence: split.bible.tiers };
111
+ const ownOpts = { ...baseOpts, confidence: split.own.tiers, owner_tag: split.own.ownerTag };
112
+ const { ownerTag } = split.own;
113
+ const viewL1 = await trustedL1(split.bible.project, bibleOpts, bibleInput);
114
+ const repoL1 = await trustedL1(split.own.project, ownOpts, ownInput, ownerTag);
115
+ const globalConfirmedL1 = await trustedL1(split.bible.global, bibleOpts, bibleInput);
116
+ const globalOwnL1 = await trustedL1(split.own.global, ownOpts, ownInput, ownerTag);
117
+ const projectL1 = mergeSplitById(viewL1, repoL1, { limit });
118
+ const globalL1 = mergeSplitById(globalConfirmedL1, globalOwnL1, { limit });
119
+ // Which store owns each id, so its L2 copy is fetched from that store.
120
+ const viewIds = new Set(viewL1.map(e => e.id));
121
+ const repoIds = new Set(repoL1.map(e => e.id));
122
+ const seenTitles = new Set(projectL1.map(e => e.title));
123
+ const mergedL1 = [...projectL1, ...globalL1.filter(e => !seenTitles.has(e.title))];
124
+ const top5Ids = mergedL1.slice(0, 5).map(e => e.id);
125
+ let l2Results = mergedL1.slice(0, 5);
126
+ if (top5Ids.length > 0) {
127
+ const fromView = top5Ids.filter(id => viewIds.has(id));
128
+ const fromRepo = top5Ids.filter(id => !viewIds.has(id) && repoIds.has(id));
129
+ const fromGlobal = top5Ids.filter(id => !viewIds.has(id) && !repoIds.has(id));
130
+ const fetched = new Map();
131
+ if (fromView.length)
132
+ for (const e of (await split.bible.project.query({ ids: fromView })).results)
133
+ fetched.set(e.id, e);
134
+ if (fromRepo.length)
135
+ for (const e of (await split.own.project.query({ ids: fromRepo })).results)
136
+ fetched.set(e.id, e);
137
+ if (fromGlobal.length)
138
+ for (const e of (await split.bible.global.query({ ids: fromGlobal })).results)
139
+ fetched.set(e.id, e);
140
+ l2Results = top5Ids.flatMap(id => (fetched.has(id) ? [capL2Content(fetched.get(id))] : []));
141
+ }
142
+ let relatedClaims = [];
143
+ if (input.expand_related && top5Ids.length > 0) {
144
+ try {
145
+ const fromBible = (await split.bible.project.relatedClaims(top5Ids, undefined, {
146
+ confidence: split.bible.tiers,
147
+ exclude_disputed: input.exclude_disputed,
148
+ })).filter(e => passesTrustFilter(e, bibleInput));
149
+ // The per-repo half stays owner-tag filtered.
150
+ const fromOwn = (await split.own.project.relatedClaims(top5Ids, undefined, {
151
+ confidence: split.own.tiers,
152
+ exclude_disputed: input.exclude_disputed,
153
+ owner_tag: ownerTag,
154
+ })).filter(e => passesTrustFilter(e, ownInput, ownerTag));
155
+ relatedClaims = mergeSplitById(fromBible, fromOwn);
156
+ }
157
+ catch {
158
+ relatedClaims = [];
159
+ }
160
+ }
161
+ return JSON.stringify({
162
+ l1_results: mergedL1,
163
+ l2_expanded: l2Results,
164
+ ...(input.expand_related ? { related_claims: relatedClaims } : {}),
165
+ });
166
+ }
46
167
  export async function kbQuery(input, anchor) {
47
168
  // Tag-only calls are valid (HIGH-1 fix): the provider's plain (non-FTS)
48
169
  // branch supports a queryless listing, so `kb_query({ tag })` lists all
@@ -50,14 +171,45 @@ export async function kbQuery(input, anchor) {
50
171
  if (!input.query && !input.flagged_only && !input.tag) {
51
172
  throw new Error('Provide query (free-text search), tag (exact-match tag listing), or flagged_only: true (list contradictions)');
52
173
  }
174
+ // A MEMBER request naming CONFIRMED plus another tier is split: CONFIRMED
175
+ // from the checkout bible view, plus the per-repo DB at every requested
176
+ // tier, own captures only (kb-self.ts getMemberSplitRead). flagged_only
177
+ // never splits. Under an http project provider both halves
178
+ // are the KB server: its CONFIRMED set, plus the member's own captures.
179
+ // own_scope never splits either: it asks for the OWN half of that read
180
+ // alone -- every requested tier, CONFIRMED included, from the per-repo KB
181
+ // with the caller's ownerTag (getSelfReadKb below) -- so the bible half,
182
+ // whose rows are untagged and not the caller's to kb_demote, must not be
183
+ // merged in.
184
+ if (!input.flagged_only && !input.own_scope) {
185
+ const split = await getMemberSplitRead(anchor, input.confidence, 'kb_query');
186
+ if (split)
187
+ return splitQuery(split, input);
188
+ }
53
189
  // A MEMBER session reads its checkout bible view unless it explicitly asks
54
- // for INFERRED/UNVERIFIED (kb-self.ts getSelfReadKb); flagged_only ignores
55
- // the confidence filter, so it is answered from the view too.
56
- // An explicit INFERRED/UNVERIFIED request in a MEMBER session goes to the
57
- // per-repo DB and sees only the caller's own captures (ownerTag).
58
- const { providers, ownerTag } = await getSelfReadKb(anchor, input.flagged_only ? undefined : input.confidence);
59
- if (ownerTag !== undefined)
60
- requireSqliteProject(providers.project, 'kb_query');
190
+ // for INFERRED/UNVERIFIED, or opts into own_scope (kb-self.ts
191
+ // getSelfReadKb); flagged_only ignores the confidence filter, so it is
192
+ // answered from the view too (own_scope is ignored in that branch).
193
+ // An explicit INFERRED/UNVERIFIED request, or own_scope, in a MEMBER
194
+ // session goes to the per-repo KB and sees only the caller's own captures
195
+ // (ownerTag).
196
+ // Under an http project provider every MEMBER read goes to the server;
197
+ // ownerTag is enforced the same way, fleet-side. A MEMBER flagged_only read
198
+ // is refused there: the bible is not used over http, and the KB server
199
+ // holds every member's captures at every tier while a flagged listing
200
+ // carries no owner tag, so sending it there would show the member other
201
+ // members' entries.
202
+ const { providers, ownerTag } = await getSelfReadKb(anchor, input.flagged_only ? undefined : input.confidence, { ownScope: input.flagged_only ? undefined : input.own_scope });
203
+ // own_scope exists to find what the caller may kb_demote. Over http that is
204
+ // only possible when the KB server serves the optional demote route, so
205
+ // without it a MEMBER own_scope read is refused with the route named.
206
+ if (input.own_scope && !input.flagged_only && memberOwnerTag(anchor) !== undefined
207
+ && providers.project instanceof HttpKbProvider && !(await providers.project.servesRoute('demote'))) {
208
+ throw new KbHttpUnsupportedError('kb_query own_scope in a MEMBER session (the KB server does not list the demote route in GET /api/kb/capabilities)', 'nothing was read', 'use a KB server that serves the demote route, or a local SQLite KB (kb_setup provider sqlite).');
209
+ }
210
+ if (input.flagged_only && memberOwnerTag(anchor) !== undefined && providers.project instanceof HttpKbProvider) {
211
+ throw new KbHttpUnsupportedError('kb_query flagged_only in a MEMBER session', 'nothing was read', 'list contradiction-flagged pairs from a FULL session, or use a local SQLite KB (kb_setup provider sqlite).');
212
+ }
61
213
  if (input.flagged_only) {
62
214
  const flaggedOpts = {
63
215
  query: input.query,
@@ -68,12 +220,15 @@ export async function kbQuery(input, anchor) {
68
220
  limit: input.limit ?? 100,
69
221
  l1_only: false,
70
222
  };
71
- const projectFlagged = await providers.project.query(flaggedOpts);
72
- const globalFlagged = await providers.global.query(flaggedOpts);
73
- const seen = new Set(projectFlagged.results.map(e => e.id));
223
+ // isFlagged re-applies the flagged filter to what a provider returned: the
224
+ // sqlite provider filters in SQL, a remote KB server predating the
225
+ // flagged_only parameter ignores it and answers with a plain listing.
226
+ const projectFlagged = (await providers.project.query(flaggedOpts)).results.filter(isFlagged);
227
+ const globalFlagged = (await providers.global.query(flaggedOpts)).results.filter(isFlagged);
228
+ const seen = new Set(projectFlagged.map(e => e.id));
74
229
  const merged = [
75
- ...projectFlagged.results,
76
- ...globalFlagged.results.filter(e => !seen.has(e.id)),
230
+ ...projectFlagged,
231
+ ...globalFlagged.filter(e => !seen.has(e.id)),
77
232
  ];
78
233
  return JSON.stringify({
79
234
  flagged_entries: merged,
@@ -106,24 +261,8 @@ export async function kbQuery(input, anchor) {
106
261
  // The trust filter runs PER PROVIDER, before the title de-dup below: filtering
107
262
  // after the merge would let an excluded project entry shadow an admissible
108
263
  // global entry of the same title and then vanish, taking both with it.
109
- const filtering = Boolean(input.confidence?.length || input.exclude_disputed || ownerTag);
110
- const trustedL1 = async (provider) => {
111
- const first = await provider.query(queryOpts);
112
- if (!filtering)
113
- return first.results;
114
- let kept = first.results.filter(e => passesTrustFilter(e, input, ownerTag));
115
- // A provider that ignored the filter may have spent its whole limit on
116
- // entries just dropped here. A full page that filtered short is the only
117
- // signal of that (the sqlite provider filters in SQL, so it never trips
118
- // this); re-ask once with a wider window and trim back to the limit.
119
- if (kept.length < queryOpts.limit && first.results.length >= queryOpts.limit) {
120
- const wider = await provider.query({ ...queryOpts, limit: queryOpts.limit * 4 });
121
- kept = wider.results.filter(e => passesTrustFilter(e, input, ownerTag));
122
- }
123
- return kept.slice(0, queryOpts.limit);
124
- };
125
- const projectL1 = await trustedL1(providers.project);
126
- const globalL1 = await trustedL1(providers.global);
264
+ const projectL1 = await trustedL1(providers.project, queryOpts, input, ownerTag);
265
+ const globalL1 = await trustedL1(providers.global, queryOpts, input, ownerTag);
127
266
  // Merge project first, deduplicate global entries by title
128
267
  const seen = new Set(projectL1.map(e => e.title));
129
268
  const mergedL1 = [
@@ -133,14 +272,19 @@ export async function kbQuery(input, anchor) {
133
272
  const top5Ids = mergedL1.slice(0, 5).map(e => e.id);
134
273
  let l2Results = mergedL1.slice(0, 5);
135
274
  if (top5Ids.length > 0) {
136
- // L2 fetch: check project first, then global for IDs not found in project
137
- const projectL2 = await providers.project.query({ ids: top5Ids });
138
- const projectL2Ids = new Set(projectL2.results.map(e => e.id));
275
+ // L2 fetch: check project first, then global for IDs not found in project.
276
+ // Each answer keeps only the ids it was asked for: a remote KB server that
277
+ // ignores the ids parameter must not inject arbitrary entries into L2.
278
+ const requested = new Set(top5Ids);
279
+ const projectL2 = (await providers.project.query({ ids: top5Ids })).results
280
+ .filter(e => requested.has(e.id));
281
+ const projectL2Ids = new Set(projectL2.map(e => e.id));
139
282
  const missingIds = top5Ids.filter(id => !projectL2Ids.has(id));
283
+ const missing = new Set(missingIds);
140
284
  const globalL2Results = missingIds.length > 0
141
- ? (await providers.global.query({ ids: missingIds })).results
285
+ ? (await providers.global.query({ ids: missingIds })).results.filter(e => missing.has(e.id))
142
286
  : [];
143
- l2Results = [...projectL2.results, ...globalL2Results].map(e => ({
287
+ l2Results = [...projectL2, ...globalL2Results].map(e => ({
144
288
  ...e,
145
289
  content: e.content.length > L2_CONTENT_CAP
146
290
  ? e.content.slice(0, L2_CONTENT_CAP) + '...[truncated]'
@@ -11,19 +11,19 @@ export declare const kbSessionPrimeSchema: z.ZodObject<{
11
11
  }, "strip", z.ZodTypeAny, {
12
12
  repo?: unknown;
13
13
  confidence?: ("CONFIRMED" | "INFERRED" | "UNVERIFIED")[] | undefined;
14
- repo_path?: unknown;
15
- repo_remote_url?: unknown;
16
14
  session_files?: string[] | undefined;
17
15
  hint_symbols?: string[] | undefined;
18
16
  hint_modules?: string[] | undefined;
17
+ repo_path?: unknown;
18
+ repo_remote_url?: unknown;
19
19
  }, {
20
20
  repo?: unknown;
21
21
  confidence?: ("CONFIRMED" | "INFERRED" | "UNVERIFIED")[] | undefined;
22
- repo_path?: unknown;
23
- repo_remote_url?: unknown;
24
22
  session_files?: string[] | undefined;
25
23
  hint_symbols?: string[] | undefined;
26
24
  hint_modules?: string[] | undefined;
25
+ repo_path?: unknown;
26
+ repo_remote_url?: unknown;
27
27
  }>;
28
28
  export type KbSessionPrimeInput = z.infer<typeof kbSessionPrimeSchema>;
29
29
  export declare const NEIGHBOR_CAP = 10;
@@ -2,9 +2,10 @@ import { z } from 'zod';
2
2
  import { KB_REMOVED_SCOPE_KEYS_SHAPE } from '../services/knowledge/kb-removed-scope-keys.js';
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
5
- import { getSelfReadKb } from '../services/knowledge/kb-self.js';
6
- import { requireSqliteProject } from '../services/knowledge/require-sqlite-project.js';
5
+ import { getSelfReadKb, getMemberSplitRead, mergeSplitById } from '../services/knowledge/kb-self.js';
7
6
  import { validateFilePaths } from '../services/knowledge/path-validation.js';
7
+ import { HttpKbProvider } from '../services/knowledge/http-provider.js';
8
+ import { extractBibleDemotions, tombstonedIds } from '../services/knowledge/bible-import.js';
8
9
  import { getProvider } from './code-intelligence.js';
9
10
  import { FLEET_DIR } from '../paths.js';
10
11
  export const kbSessionPrimeSchema = z.object({
@@ -154,12 +155,73 @@ export async function kbSessionPrime(input, anchor) {
154
155
  // That explicit request is answered from the per-repo DB with only the
155
156
  // caller's own captures (ownerTag): every block below honours it, and the
156
157
  // bible cold-seeds are skipped (bible entries are nobody's own capture).
158
+ //
159
+ // A MEMBER request naming CONFIRMED plus another tier is split (kb-self.ts
160
+ // getMemberSplitRead): each half is primed by primeFrom below, the CONFIRMED
161
+ // half exactly as a CONFIRMED-only request (bible view, cold-seeds included,
162
+ // decided on that half's own entries), every requested tier from the
163
+ // per-repo DB owner-tagged (no cold-seed). The two results are then merged.
164
+ // Under an http project provider both halves are primed by
165
+ // the KB server: the CONFIRMED half unfiltered, the own half owner-tagged.
166
+ const split = await getMemberSplitRead(anchor, input.confidence, 'kb_session_prime');
167
+ if (split) {
168
+ const fromBible = await primeFrom({ project: split.bible.project, global: split.bible.global, folder: split.anchor.folder }, input, split.bible.tiers, false);
169
+ const fromOwn = await primeFrom({ project: split.own.project, global: split.own.global, folder: split.anchor.folder, ownerTag: split.own.ownerTag }, input, split.own.tiers, false);
170
+ return JSON.stringify(mergePrimed(fromBible, fromOwn, input));
171
+ }
172
+ // Under an http project provider a MEMBER prime of any tier
173
+ // is answered by the server; ownerTag is enforced the same way, fleet-side.
157
174
  const { providers, anchor: resolved, ownerTag } = await getSelfReadKb(anchor, input.confidence);
158
- if (ownerTag !== undefined)
159
- requireSqliteProject(providers.project, 'kb_session_prime');
160
175
  // Default-trusted reads: CONFIRMED + undisputed unless the caller lists tiers.
161
176
  const confidence = input.confidence?.length ? input.confidence : ['CONFIRMED'];
162
177
  const exclude_disputed = !input.confidence?.length;
178
+ return JSON.stringify(await primeFrom({ project: providers.project, global: providers.global, folder: resolved.folder, ownerTag }, input, confidence, exclude_disputed));
179
+ }
180
+ /**
181
+ * Merge the two halves of a MEMBER split prime. top_entries: every bible-half
182
+ * entry, then own-half entries the bible half did not already return (bible
183
+ * copy wins), with graph-neighbor additions capped at ADDED_ENTRY_CAP across
184
+ * the merged list (bible half first, so the CONFIRMED-only result survives
185
+ * intact). Files: a file fresh in either half is fresh (bible summary first);
186
+ * stale_files is the union minus those fresh files. session_warm and
187
+ * token_estimate are recomputed the way prime() computes them.
188
+ */
189
+ function mergePrimed(bible, own, input) {
190
+ let neighbors = 0;
191
+ const top_entries = mergeSplitById(bible.top_entries ?? [], own.top_entries ?? []).filter((e) => {
192
+ if (e.via !== 'graph-neighbor')
193
+ return true;
194
+ neighbors += 1;
195
+ return neighbors <= ADDED_ENTRY_CAP;
196
+ });
197
+ const fresh_summaries = [...(bible.fresh_summaries ?? [])];
198
+ const freshFiles = new Set(fresh_summaries.map(r => r.file));
199
+ for (const r of own.fresh_summaries ?? []) {
200
+ if (freshFiles.has(r.file))
201
+ continue;
202
+ freshFiles.add(r.file);
203
+ fresh_summaries.push(r);
204
+ }
205
+ const stale_files = [...new Set([...(bible.stale_files ?? []), ...(own.stale_files ?? [])])]
206
+ .filter(f => !freshFiles.has(f));
207
+ let token_estimate = 0;
208
+ for (const e of top_entries)
209
+ token_estimate += Math.ceil((e.summary?.length || 0) / 4);
210
+ for (const r of fresh_summaries)
211
+ token_estimate += Math.ceil((r.summary?.length || 0) / 4);
212
+ return {
213
+ ...bible,
214
+ session_warm: input.session_files?.length ? stale_files.length === 0 : true,
215
+ stale_files,
216
+ top_entries,
217
+ fresh_summaries,
218
+ token_estimate,
219
+ };
220
+ }
221
+ async function primeFrom(source, input, confidence, exclude_disputed) {
222
+ const providers = source;
223
+ const { ownerTag } = source;
224
+ const resolved = { folder: source.folder };
163
225
  const result = await providers.project.prime({
164
226
  confidence,
165
227
  exclude_disputed,
@@ -277,20 +339,38 @@ export async function kbSessionPrime(input, anchor) {
277
339
  // live-KB hit gathered above. Non-fatal: the entire block is a hard skip --
278
340
  // missing file, unreadable/malformed JSON, or a bad shape leaves `result`
279
341
  // exactly as built above (same contract as the neighbor block).
280
- if (ownerTag === undefined && (result.top_entries ?? []).length < COLD_KB_MAX) {
342
+ // Never under an http project provider: the KB server is the source of
343
+ // truth there, and the checkout bible is not read.
344
+ if (ownerTag === undefined && !(providers.project instanceof HttpKbProvider) && (result.top_entries ?? []).length < COLD_KB_MAX) {
281
345
  try {
282
346
  const repoRoot = localDirOrNull(resolved.folder);
283
347
  const canonicalPath = repoRoot ? path.join(repoRoot, '.fleet', 'kb-canonical.json') : null;
284
348
  if (canonicalPath && fs.existsSync(canonicalPath)) {
285
349
  const raw = fs.readFileSync(canonicalPath, 'utf-8');
286
- const entries = bibleEntries(JSON.parse(raw));
350
+ const parsedBible = JSON.parse(raw);
351
+ const entries = bibleEntries(parsedBible);
352
+ // The bible's EXPLICIT demotion tombstones, via the one shared reader.
353
+ const dropped = tombstonedIds(extractBibleDemotions(parsedBible));
287
354
  if (entries.length > 0) {
288
355
  const existingIds = new Set((result.top_entries ?? []).map(e => e.id));
289
356
  const hintSymbols = input.hint_symbols ?? [];
290
357
  const hintModules = input.hint_modules ?? [];
291
- const valid = entries
358
+ const shaped = entries
292
359
  .filter(isCanonicalBibleEntry)
293
- .filter(e => !existingIds.has(e.id));
360
+ .filter(e => !existingIds.has(e.id))
361
+ // A tombstoned id is demoted knowledge: never seed it, whatever the
362
+ // local row says (including when there is none).
363
+ .filter(e => !dropped.has(e.id));
364
+ // ONE skip predicate for the rest: seed a bible id only when this
365
+ // clone has NO row for it, or holds a LIVE CONFIRMED one. A row that
366
+ // exists but is INFERRED, stale, superseded or flagged_for_review is
367
+ // knowledge this clone has already stopped trusting, and re-seeding
368
+ // the bible's CONFIRMED copy of it would quietly undo that. Note what
369
+ // this deliberately is NOT: a demoted_at special case. Demotion is
370
+ // only one of the four ways a row stops being live CONFIRMED, and the
371
+ // single predicate covers it with no extra branch.
372
+ const liveness = providers.project.getLiveConfirmedState(shaped.map(e => e.id));
373
+ const valid = shaped.filter(e => liveness.get(e.id) !== false);
294
374
  let ordered = valid;
295
375
  if (hintSymbols.length > 0 || hintModules.length > 0) {
296
376
  const matched = valid.filter(e => canonicalMatchesHints(e, hintSymbols, hintModules));
@@ -387,6 +467,6 @@ export async function kbSessionPrime(input, anchor) {
387
467
  // Hard skip: leave `result` exactly as built above.
388
468
  }
389
469
  }
390
- return JSON.stringify(result);
470
+ return result;
391
471
  }
392
472
  //# sourceMappingURL=kb-session-prime.js.map
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { type KbAnchor } from '../services/knowledge/kb-self.js';
3
+ import { type KbToolProfile } from '../services/kb-tool-profile.js';
3
4
  export declare const kbSetupSchema: z.ZodObject<{
4
5
  repo_path: z.ZodOptional<z.ZodUnknown>;
5
6
  repo: z.ZodOptional<z.ZodUnknown>;
@@ -7,11 +8,17 @@ export declare const kbSetupSchema: z.ZodObject<{
7
8
  provider: z.ZodOptional<z.ZodEnum<["sqlite", "http"]>>;
8
9
  remote: z.ZodOptional<z.ZodString>;
9
10
  token: z.ZodOptional<z.ZodString>;
11
+ request_timeout_ms: z.ZodOptional<z.ZodNumber>;
12
+ tool_profile: z.ZodOptional<z.ZodEnum<["full", "developer", "custom"]>>;
13
+ tool_profile_tools: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
10
14
  }, "strip", z.ZodTypeAny, {
11
15
  remote?: string | undefined;
12
16
  repo?: unknown;
13
17
  provider?: "http" | "sqlite" | undefined;
14
18
  token?: string | undefined;
19
+ request_timeout_ms?: number | undefined;
20
+ tool_profile?: "full" | "custom" | "developer" | undefined;
21
+ tool_profile_tools?: string[] | undefined;
15
22
  repo_path?: unknown;
16
23
  repo_remote_url?: unknown;
17
24
  }, {
@@ -19,9 +26,22 @@ export declare const kbSetupSchema: z.ZodObject<{
19
26
  repo?: unknown;
20
27
  provider?: "http" | "sqlite" | undefined;
21
28
  token?: string | undefined;
29
+ request_timeout_ms?: number | undefined;
30
+ tool_profile?: "full" | "custom" | "developer" | undefined;
31
+ tool_profile_tools?: string[] | undefined;
22
32
  repo_path?: unknown;
23
33
  repo_remote_url?: unknown;
24
34
  }>;
25
35
  export type KbSetupInput = z.infer<typeof kbSetupSchema>;
36
+ /**
37
+ * Set the install-wide KB tool profile in the KB config, leaving every other
38
+ * key as it was (the `apra-fleet kb profile` CLI verb). Returns the steps.
39
+ * Throws KbConfigUnreadableError, writing nothing, when the existing file
40
+ * cannot be parsed: unlike kb_setup, this verb has no way to restore the remote
41
+ * url and token a rewrite would lose.
42
+ */
43
+ export declare function writeKbToolProfile(profile: KbToolProfile): {
44
+ steps: string[];
45
+ };
26
46
  export declare function kbSetup(input: KbSetupInput, anchor?: KbAnchor): Promise<string>;
27
47
  //# sourceMappingURL=kb-setup.d.ts.map