@octopi-agent/engine 0.102.10 → 0.121.10

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 (317) hide show
  1. package/dist/builtin-model-info.js +28 -7
  2. package/dist/config-admin/agent-presets.d.ts +32 -0
  3. package/dist/config-admin/agent-presets.js +133 -0
  4. package/dist/config-admin/discover.d.ts +33 -4
  5. package/dist/config-admin/discover.js +101 -6
  6. package/dist/config-admin/env.d.ts +11 -0
  7. package/dist/config-admin/env.js +41 -0
  8. package/dist/config-admin/error-copy.d.ts +106 -0
  9. package/dist/config-admin/error-copy.js +287 -0
  10. package/dist/config-admin/i18n.d.ts +21 -0
  11. package/dist/config-admin/i18n.js +19 -0
  12. package/dist/config-admin/index.d.ts +6 -2
  13. package/dist/config-admin/index.js +3 -1
  14. package/dist/config-admin/model-presets.d.ts +14 -0
  15. package/dist/config-admin/model-presets.js +595 -70
  16. package/dist/config-admin/onboarding-apply.d.ts +71 -0
  17. package/dist/config-admin/onboarding-apply.js +235 -0
  18. package/dist/config-admin/onboarding.d.ts +154 -0
  19. package/dist/config-admin/onboarding.js +270 -0
  20. package/dist/config-admin/redact.d.ts +42 -9
  21. package/dist/config-admin/redact.js +69 -5
  22. package/dist/config-schema/agent.d.ts +5 -1
  23. package/dist/config-schema/agent.js +20 -2
  24. package/dist/config-schema/browser.d.ts +62 -0
  25. package/dist/config-schema/browser.js +89 -0
  26. package/dist/config-schema/context.d.ts +7 -2
  27. package/dist/config-schema/context.js +6 -3
  28. package/dist/config-schema/engine.d.ts +86 -7
  29. package/dist/config-schema/engine.js +9 -2
  30. package/dist/config-schema/governance.d.ts +12 -1
  31. package/dist/config-schema/governance.js +10 -2
  32. package/dist/config-schema/models.d.ts +12 -8
  33. package/dist/config-schema/models.js +16 -1
  34. package/dist/config-schema/run.d.ts +4 -0
  35. package/dist/config-schema/run.js +10 -0
  36. package/dist/config-schema/shared.d.ts +1 -0
  37. package/dist/config-schema/shared.js +4 -2
  38. package/dist/config-schema/substrate.d.ts +5 -0
  39. package/dist/config-schema/substrate.js +8 -0
  40. package/dist/config.d.ts +116 -10
  41. package/dist/config.js +109 -13
  42. package/dist/file-mode.d.ts +67 -0
  43. package/dist/file-mode.js +182 -0
  44. package/dist/fork-child.d.ts +82 -0
  45. package/dist/fork-child.js +123 -0
  46. package/dist/harness/activation/delegated-run.d.ts +42 -0
  47. package/dist/harness/activation/delegated-run.js +71 -0
  48. package/dist/harness/activation/dispatcher.js +4 -0
  49. package/dist/harness/activation/index.d.ts +1 -0
  50. package/dist/harness/activation/run-backend.d.ts +92 -0
  51. package/dist/harness/activation/run-backend.js +16 -0
  52. package/dist/harness/activation/run-protocol.d.ts +392 -0
  53. package/dist/harness/activation/run-protocol.js +19 -0
  54. package/dist/harness/activation/runtime.d.ts +33 -0
  55. package/dist/harness/activation/runtime.js +119 -28
  56. package/dist/harness/activation/types.d.ts +19 -2
  57. package/dist/harness/agent/builder.d.ts +12 -0
  58. package/dist/harness/agent/builder.js +78 -22
  59. package/dist/harness/agent/config-bridge.d.ts +10 -1
  60. package/dist/harness/agent/config-bridge.js +15 -3
  61. package/dist/harness/agent/registered-tool-adapter.d.ts +27 -0
  62. package/dist/harness/agent/registered-tool-adapter.js +67 -0
  63. package/dist/harness/agent/wire-agent-builder.d.ts +2 -0
  64. package/dist/harness/agent/wire-agent-builder.js +9 -2
  65. package/dist/harness/capabilities/document/backends/plain.js +2 -1
  66. package/dist/harness/capabilities/document/legacy/factory.js +3 -2
  67. package/dist/harness/collaboration/autonomous-subsystem/audit/reader.d.ts +17 -0
  68. package/dist/harness/collaboration/autonomous-subsystem/audit/reader.js +66 -36
  69. package/dist/harness/collaboration/autonomous-subsystem/audit/writer.d.ts +6 -2
  70. package/dist/harness/collaboration/autonomous-subsystem/audit/writer.js +27 -7
  71. package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +13 -0
  72. package/dist/harness/collaboration/autonomous-subsystem/loader.js +1 -0
  73. package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +11 -0
  74. package/dist/harness/collaboration/autonomous-subsystem/runtime.js +17 -0
  75. package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +13 -3
  76. package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +48 -39
  77. package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -0
  78. package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +4 -0
  79. package/dist/harness/collaboration/delegation/child-run.d.ts +9 -0
  80. package/dist/harness/collaboration/delegation/child-run.js +34 -2
  81. package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
  82. package/dist/harness/collaboration/delegation/service.d.ts +9 -4
  83. package/dist/harness/collaboration/delegation/service.js +33 -12
  84. package/dist/harness/collaboration/delegation/store.d.ts +2 -0
  85. package/dist/harness/collaboration/delegation/store.js +1 -0
  86. package/dist/harness/collaboration/delegation/types.d.ts +11 -0
  87. package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
  88. package/dist/harness/collaboration/delegation/wiring.js +1 -0
  89. package/dist/harness/context/capabilities/compact/engine.d.ts +44 -5
  90. package/dist/harness/context/capabilities/compact/engine.js +127 -39
  91. package/dist/harness/context/capabilities/compact/index.d.ts +2 -1
  92. package/dist/harness/context/capabilities/compact/index.js +1 -1
  93. package/dist/harness/context/capabilities/compact/types.d.ts +59 -9
  94. package/dist/harness/context/capabilities/summary/gate.d.ts +19 -2
  95. package/dist/harness/context/capabilities/summary/gate.js +53 -6
  96. package/dist/harness/context/capabilities/summary/registry.js +20 -0
  97. package/dist/harness/context/capabilities/summary/tool-binding.js +16 -12
  98. package/dist/harness/context/capabilities/summary/tool-bindings.d.ts +13 -0
  99. package/dist/harness/context/capabilities/summary/tool-bindings.js +22 -0
  100. package/dist/harness/context/capabilities/summary/types.d.ts +16 -0
  101. package/dist/harness/context/constitution/default-agents.md +85 -0
  102. package/dist/harness/context/context-size-ledger.js +8 -1
  103. package/dist/harness/context/default-context-engine.d.ts +100 -6
  104. package/dist/harness/context/default-context-engine.js +382 -89
  105. package/dist/harness/context/index.d.ts +2 -1
  106. package/dist/harness/context/index.js +1 -0
  107. package/dist/harness/context/layer-health.d.ts +4 -0
  108. package/dist/harness/context/layer-health.js +4 -0
  109. package/dist/harness/context/layer-types.d.ts +57 -0
  110. package/dist/harness/context/layer-types.js +73 -1
  111. package/dist/harness/context/layers.d.ts +3 -0
  112. package/dist/harness/context/layers.js +13 -6
  113. package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
  114. package/dist/harness/context/prefix-fingerprint.js +52 -0
  115. package/dist/harness/context/summarize.js +18 -0
  116. package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
  117. package/dist/harness/context/system-prompt-assembler.js +5 -2
  118. package/dist/harness/context/tool-result-aging.d.ts +104 -16
  119. package/dist/harness/context/tool-result-aging.js +211 -57
  120. package/dist/harness/context/types.d.ts +71 -4
  121. package/dist/harness/context/types.js +25 -1
  122. package/dist/harness/extension/execution-environment/effect-policy.d.ts +4 -3
  123. package/dist/harness/extension/execution-environment/effect-policy.js +2 -2
  124. package/dist/harness/extension/execution-environment/privileged-exec.js +4 -1
  125. package/dist/harness/extension/execution-environment/workspace.js +1 -1
  126. package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +27 -1
  127. package/dist/harness/extension/plugin-ecosystem/channel-types.js +11 -2
  128. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +12 -0
  129. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +25 -0
  130. package/dist/harness/extension/plugin-ecosystem/commands/builtin.js +28 -1
  131. package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
  132. package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
  133. package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +168 -1
  134. package/dist/harness/extension/plugin-ecosystem/plugins/api.js +156 -15
  135. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.d.ts +95 -0
  136. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.js +170 -0
  137. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.d.ts +178 -0
  138. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.js +314 -0
  139. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +40 -5
  140. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +45 -26
  141. package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -1
  142. package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +11 -1
  143. package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +42 -76
  144. package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +30 -1
  145. package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +46 -0
  146. package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +9 -0
  147. package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +19 -14
  148. package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +3 -1
  149. package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +1 -1
  150. package/dist/harness/extension/plugin-ecosystem/tools/browser-types.d.ts +333 -0
  151. package/dist/harness/extension/plugin-ecosystem/tools/browser-types.js +16 -0
  152. package/dist/harness/extension/plugin-ecosystem/tools/browser.d.ts +64 -0
  153. package/dist/harness/extension/plugin-ecosystem/tools/browser.js +692 -0
  154. package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +0 -37
  155. package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +108 -4
  156. package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +18 -0
  157. package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +34 -0
  158. package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +0 -6
  159. package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +57 -4
  160. package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +81 -13
  161. package/dist/harness/extension/plugin-ecosystem/tools/http.js +5 -0
  162. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -7
  163. package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +126 -14
  164. package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +1 -1
  165. package/dist/harness/extension/plugin-ecosystem/tools/platform.js +44 -5
  166. package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +10 -24
  167. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +7 -0
  168. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -0
  169. package/dist/harness/governance/credentials/db.d.ts +9 -0
  170. package/dist/harness/governance/credentials/db.js +122 -31
  171. package/dist/harness/governance/credentials/store.d.ts +68 -7
  172. package/dist/harness/governance/credentials/store.js +177 -64
  173. package/dist/harness/governance/credentials/types.d.ts +13 -2
  174. package/dist/harness/governance/index.d.ts +1 -0
  175. package/dist/harness/governance/index.js +1 -0
  176. package/dist/harness/governance/registry/db.d.ts +32 -0
  177. package/dist/harness/governance/registry/db.js +142 -0
  178. package/dist/harness/governance/registry/index.d.ts +9 -0
  179. package/dist/harness/governance/registry/index.js +9 -0
  180. package/dist/harness/governance/registry/service.d.ts +381 -0
  181. package/dist/harness/governance/registry/service.js +825 -0
  182. package/dist/harness/governance/registry/types.d.ts +77 -0
  183. package/dist/harness/governance/registry/types.js +7 -0
  184. package/dist/harness/governance/security/default-security-guard.d.ts +3 -0
  185. package/dist/harness/governance/security/default-security-guard.js +12 -1
  186. package/dist/harness/governance/security/risk-evaluator.d.ts +13 -3
  187. package/dist/harness/governance/security/risk-evaluator.js +307 -21
  188. package/dist/harness/governance/security/shell-parser-treesitter.js +12 -4
  189. package/dist/harness/governance/session-acl/tool-rights.d.ts +1 -0
  190. package/dist/harness/index.d.ts +5 -3
  191. package/dist/harness/index.js +2 -2
  192. package/dist/harness/knowledge/boot-config.d.ts +2 -0
  193. package/dist/harness/knowledge/boot-config.js +2 -0
  194. package/dist/harness/knowledge/client.d.ts +53 -7
  195. package/dist/harness/knowledge/client.js +77 -20
  196. package/dist/harness/knowledge/db.d.ts +16 -0
  197. package/dist/harness/knowledge/db.js +107 -41
  198. package/dist/harness/knowledge/engine-thread.js +5 -7
  199. package/dist/harness/knowledge/grounding.js +4 -1
  200. package/dist/harness/knowledge/http-app.d.ts +15 -0
  201. package/dist/harness/knowledge/http-app.js +82 -21
  202. package/dist/harness/knowledge/http-bridge.d.ts +15 -0
  203. package/dist/harness/knowledge/http-bridge.js +19 -0
  204. package/dist/harness/knowledge/knowledge-serve-child.js +2 -3
  205. package/dist/harness/knowledge/query-service.d.ts +12 -2
  206. package/dist/harness/knowledge/query-service.js +75 -41
  207. package/dist/harness/knowledge/query-worker-client.d.ts +5 -1
  208. package/dist/harness/knowledge/query-worker-client.js +4 -1
  209. package/dist/harness/knowledge/query-worker.js +5 -3
  210. package/dist/harness/knowledge/read-http.d.ts +2 -0
  211. package/dist/harness/knowledge/read-http.js +9 -2
  212. package/dist/harness/knowledge/retriever.d.ts +1 -1
  213. package/dist/harness/knowledge/retriever.js +1 -2
  214. package/dist/harness/knowledge/serve.d.ts +6 -1
  215. package/dist/harness/knowledge/serve.js +5 -6
  216. package/dist/harness/knowledge/source-store.d.ts +101 -5
  217. package/dist/harness/knowledge/source-store.js +297 -71
  218. package/dist/harness/knowledge/start-service-process.d.ts +27 -8
  219. package/dist/harness/knowledge/start-service-process.js +49 -90
  220. package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
  221. package/dist/harness/knowledge/tenant-gate.js +167 -0
  222. package/dist/harness/knowledge/writer-service.d.ts +18 -2
  223. package/dist/harness/knowledge/writer-service.js +80 -16
  224. package/dist/harness/knowledge/writer-worker-client.d.ts +9 -2
  225. package/dist/harness/knowledge/writer-worker-client.js +20 -3
  226. package/dist/harness/knowledge/writer-worker.js +11 -4
  227. package/dist/harness/memory/backfill-trigger.d.ts +10 -3
  228. package/dist/harness/memory/backfill-trigger.js +19 -19
  229. package/dist/harness/memory/cognition-seeds.d.ts +45 -0
  230. package/dist/harness/memory/cognition-seeds.js +67 -0
  231. package/dist/harness/memory/cognition.d.ts +12 -3
  232. package/dist/harness/memory/cognition.js +55 -35
  233. package/dist/harness/memory/conceptualizer.d.ts +11 -1
  234. package/dist/harness/memory/conceptualizer.js +10 -1
  235. package/dist/harness/memory/context-slice.d.ts +42 -0
  236. package/dist/harness/memory/context-slice.js +197 -0
  237. package/dist/harness/memory/health-probe.d.ts +8 -2
  238. package/dist/harness/memory/health-probe.js +10 -13
  239. package/dist/harness/memory/index.d.ts +5 -1
  240. package/dist/harness/memory/index.js +3 -1
  241. package/dist/harness/memory/sqlite/agent-db.d.ts +1 -0
  242. package/dist/harness/memory/sqlite/agent-db.js +9 -8
  243. package/dist/harness/memory/sqlite/cognition-store.d.ts +26 -3
  244. package/dist/harness/memory/sqlite/cognition-store.js +103 -45
  245. package/dist/harness/memory/sqlite/memory-store.js +4 -0
  246. package/dist/harness/memory/sqlite/sqlite-vec.d.ts +24 -1
  247. package/dist/harness/memory/sqlite/sqlite-vec.js +37 -1
  248. package/dist/harness/memory/types.d.ts +18 -2
  249. package/dist/harness/observability/observer/hub.d.ts +10 -0
  250. package/dist/harness/observability/observer/hub.js +95 -2
  251. package/dist/harness/run/agent/agent.js +3 -2
  252. package/dist/harness/run/concurrency/tool-validator.js +4 -3
  253. package/dist/harness/run/reliability/run-agent.d.ts +22 -0
  254. package/dist/harness/run/reliability/run-agent.js +79 -10
  255. package/dist/harness/run/run-host.d.ts +302 -0
  256. package/dist/harness/run/run-host.js +978 -0
  257. package/dist/harness/run/run-scope.d.ts +11 -0
  258. package/dist/harness/run/run-scope.js +19 -1
  259. package/dist/harness/run/runner.d.ts +149 -22
  260. package/dist/harness/run/runner.js +352 -35
  261. package/dist/harness/session/in-memory-store.js +13 -1
  262. package/dist/harness/session/schedules/service.d.ts +10 -2
  263. package/dist/harness/session/schedules/service.js +13 -2
  264. package/dist/harness/session/tasks/tools.js +12 -0
  265. package/dist/harness/session/types.d.ts +10 -14
  266. package/dist/harness/shared/events/agent-event-map.d.ts +29 -0
  267. package/dist/harness/shared/types/agent-definition.d.ts +6 -0
  268. package/dist/index.d.ts +3 -2
  269. package/dist/index.js +2 -1
  270. package/dist/integration/agent-runtime/channel-message-source.d.ts +3 -1
  271. package/dist/integration/agent-runtime/channel-message-source.js +4 -3
  272. package/dist/integration/browser/camoufox.d.ts +40 -0
  273. package/dist/integration/browser/camoufox.js +80 -0
  274. package/dist/integration/browser/index.d.ts +91 -0
  275. package/dist/integration/browser/index.js +110 -0
  276. package/dist/integration/browser/install.d.ts +47 -0
  277. package/dist/integration/browser/install.js +214 -0
  278. package/dist/integration/browser/playwright.d.ts +224 -0
  279. package/dist/integration/browser/playwright.js +925 -0
  280. package/dist/integration/browser/service.d.ts +41 -0
  281. package/dist/integration/browser/service.js +181 -0
  282. package/dist/integration/browser/stealth.d.ts +42 -0
  283. package/dist/integration/browser/stealth.js +104 -0
  284. package/dist/integration/providers/anthropic-message-normalize.d.ts +20 -0
  285. package/dist/integration/providers/anthropic-message-normalize.js +48 -0
  286. package/dist/integration/providers/anthropic.js +27 -2
  287. package/dist/integration/providers/fetch-timeout.d.ts +17 -0
  288. package/dist/integration/providers/fetch-timeout.js +24 -0
  289. package/dist/integration/providers/openai.d.ts +8 -0
  290. package/dist/integration/providers/openai.js +41 -1
  291. package/dist/integration/providers/wire-dump-diff.js +2 -1
  292. package/dist/integration/providers/wire-dump.d.ts +56 -2
  293. package/dist/integration/providers/wire-dump.js +119 -6
  294. package/dist/integration/storage/archive-manager.d.ts +14 -2
  295. package/dist/integration/storage/archive-manager.js +24 -8
  296. package/dist/integration/storage/atomic-tmp.d.ts +12 -0
  297. package/dist/integration/storage/atomic-tmp.js +36 -0
  298. package/dist/integration/storage/jsonl.d.ts +9 -0
  299. package/dist/integration/storage/jsonl.js +137 -38
  300. package/dist/integration/storage/session-index.d.ts +5 -0
  301. package/dist/integration/storage/session-index.js +25 -4
  302. package/dist/integration/tools/assemble.d.ts +50 -0
  303. package/dist/integration/tools/assemble.js +137 -0
  304. package/dist/paths.d.ts +18 -0
  305. package/dist/paths.js +36 -0
  306. package/dist/subsystems/cognition-steward/conceptualize/config.yaml +3 -1
  307. package/dist/subsystems/cognition-steward/conceptualize/handler.js +74 -5
  308. package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +3 -2
  309. package/dist/subsystems/cognition-steward/consolidate/config.yaml +11 -2
  310. package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +6 -3
  311. package/dist/subsystems/cognition-steward/consolidate/handler.js +266 -28
  312. package/dist/subsystems/memory-steward/backfill/handler.js +3 -2
  313. package/dist/subsystems/memory-steward/govern/config.yaml +6 -1
  314. package/dist/subsystems/wisdom-steward/attribute/config.yaml +5 -1
  315. package/dist/subsystems/wisdom-steward/formation/config.yaml +3 -0
  316. package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
  317. package/package.json +5 -3
@@ -5,7 +5,8 @@
5
5
  * 读走 KnowledgeQueryService。**禁止**在此打开 knowledge.db。
6
6
  * 纯读 GET 在主线程 `read-http.ts` 直达 Meta/Search,不经本层。
7
7
  */
8
- import { matchKnowledgeToken } from './http-bridge.js';
8
+ import { DEFAULT_TENANT_ID, PLATFORM_TENANT_ID } from '@octopi-agent/core/types.js';
9
+ import { matchKnowledgeToken, resolveTenantClaim } from './http-bridge.js';
9
10
  import { promotionThresholdsFromParams } from './http-params.js';
10
11
  function json(res, status, payload) {
11
12
  const buf = Buffer.from(JSON.stringify(payload), 'utf8');
@@ -29,6 +30,21 @@ function mapWriteError(res, e) {
29
30
  err(res, 403, 'not_principal_owner', msg);
30
31
  return true;
31
32
  }
33
+ if (code === 'project_not_visible' ||
34
+ code === 'source_not_visible' ||
35
+ /not visible to writer/i.test(msg)) {
36
+ err(res, 403, code || 'not_visible', msg);
37
+ return true;
38
+ }
39
+ if (code === 'visibility_requires_platform_claim' ||
40
+ /requires an explicit platform claim/i.test(msg)) {
41
+ err(res, 403, 'visibility_requires_platform_claim', msg);
42
+ return true;
43
+ }
44
+ if (code === 'public_projects_disabled') {
45
+ err(res, 403, 'public_projects_disabled', msg);
46
+ return true;
47
+ }
32
48
  if (code === 'principal_not_registered' || /principal not registered/i.test(msg)) {
33
49
  err(res, 403, 'principal_not_registered', msg);
34
50
  return true;
@@ -73,8 +89,25 @@ export class KnowledgeHttpApp {
73
89
  query() {
74
90
  return this.opts.query;
75
91
  }
92
+ /** 写身份:归属域(platform token 无 claim → DEFAULT,不写 platform 共享面) */
76
93
  identityOf(ctx) {
77
- return { tenantId: ctx.tenantId, gatewayId: ctx.gatewayId };
94
+ return {
95
+ tenantId: ctx.writeTenantId ?? ctx.tenantId,
96
+ gatewayId: ctx.gatewayId,
97
+ claimed: ctx.claimed,
98
+ };
99
+ }
100
+ /**
101
+ * 读身份:claim-or-token 域(v0.113.10 语义「读口不受影响」)——platform broker 无 claim
102
+ * = 运维面全量;claim 在场 = 租户收口。写域不得代劳读域(T3b 自审修复:曾共用 identityOf,
103
+ * 把运维读误收窄到 DEFAULT)。
104
+ */
105
+ readIdentityOf(ctx) {
106
+ return {
107
+ tenantId: ctx.tenantId,
108
+ gatewayId: ctx.gatewayId,
109
+ claimed: ctx.claimed,
110
+ };
78
111
  }
79
112
  route(method, pattern, handler) {
80
113
  const keys = [];
@@ -110,7 +143,7 @@ export class KnowledgeHttpApp {
110
143
  });
111
144
  });
112
145
  this.route('GET', '/v1/principals/:agentId', async (_req, res, ctx, params) => {
113
- const row = await this.query().getPrincipal(this.identityOf(ctx), params.agentId);
146
+ const row = await this.query().getPrincipal(this.readIdentityOf(ctx), params.agentId);
114
147
  if (!row)
115
148
  return err(res, 404, 'principal_not_registered', 'principal not found');
116
149
  json(res, 200, { ok: true, data: row });
@@ -118,7 +151,7 @@ export class KnowledgeHttpApp {
118
151
  this.route('GET', '/v1/projects', async (_req, res, ctx) => {
119
152
  json(res, 200, {
120
153
  ok: true,
121
- data: await this.query().listProjects(this.identityOf(ctx)),
154
+ data: await this.query().listProjects(this.readIdentityOf(ctx)),
122
155
  });
123
156
  });
124
157
  this.route('POST', '/v1/projects', async (_req, res, ctx, _p, body) => {
@@ -139,6 +172,12 @@ export class KnowledgeHttpApp {
139
172
  }
140
173
  }
141
174
  });
175
+ this.route('GET', '/v1/projects/:projectKey', async (_req, res, ctx, params) => {
176
+ const row = await this.query().getProject(this.readIdentityOf(ctx), params.projectKey);
177
+ if (!row)
178
+ return err(res, 404, 'project_not_found', 'project not found');
179
+ json(res, 200, { ok: true, data: row });
180
+ });
142
181
  this.route('DELETE', '/v1/projects/:projectKey', async (_req, res, ctx, params) => {
143
182
  try {
144
183
  const data = await this.write.removeProject(this.identityOf(ctx), params.projectKey);
@@ -199,7 +238,7 @@ export class KnowledgeHttpApp {
199
238
  : {}),
200
239
  ...(projectKey != null && projectKey !== '' ? { projectKey } : {}),
201
240
  ...(sessionId != null && sessionId !== '' ? { sessionId } : {}),
202
- identity: this.identityOf(ctx),
241
+ identity: this.readIdentityOf(ctx),
203
242
  });
204
243
  json(res, 200, { ok: true, data: list });
205
244
  });
@@ -218,7 +257,7 @@ export class KnowledgeHttpApp {
218
257
  }
219
258
  });
220
259
  this.route('GET', '/v1/sources/:sid', async (_req, res, ctx, params) => {
221
- const detail = await this.query().getSourceDetail(params.sid, this.identityOf(ctx));
260
+ const detail = await this.query().getSourceDetail(params.sid, this.readIdentityOf(ctx));
222
261
  if (!detail) {
223
262
  return err(res, 404, 'source_not_found', 'source not found');
224
263
  }
@@ -374,12 +413,12 @@ export class KnowledgeHttpApp {
374
413
  ...(url.searchParams.get('pageSize')
375
414
  ? { pageSize: Number(url.searchParams.get('pageSize')) }
376
415
  : {}),
377
- }, this.identityOf(ctx));
416
+ }, this.readIdentityOf(ctx));
378
417
  return json(res, 200, { ok: true, data });
379
418
  }
380
419
  json(res, 200, {
381
420
  ok: true,
382
- data: await this.query().listFiles(params.sid, this.identityOf(ctx)),
421
+ data: await this.query().listFiles(params.sid, this.readIdentityOf(ctx)),
383
422
  });
384
423
  });
385
424
  this.route('GET', '/v1/principals/:agentId/search', async (req, res, ctx, params) => {
@@ -390,7 +429,7 @@ export class KnowledgeHttpApp {
390
429
  q: url.searchParams.get('q') ?? '',
391
430
  sessionId: url.searchParams.get('sessionId') ?? undefined,
392
431
  limit: Number(url.searchParams.get('limit') ?? 8),
393
- identity: this.identityOf(ctx),
432
+ identity: this.readIdentityOf(ctx),
394
433
  });
395
434
  json(res, 200, { ok: true, data: result });
396
435
  });
@@ -407,13 +446,13 @@ export class KnowledgeHttpApp {
407
446
  sessionId: url.searchParams.get('sessionId') ?? undefined,
408
447
  limit: Number(url.searchParams.get('limit') ?? 8),
409
448
  ...(recall ? { recall } : {}),
410
- identity: this.identityOf(ctx),
449
+ identity: this.readIdentityOf(ctx),
411
450
  });
412
451
  json(res, 200, { ok: true, data: result });
413
452
  });
414
453
  this.route('GET', '/v1/principals/:agentId/stats', async (_req, res, ctx, params) => {
415
454
  await this.write.ensurePrincipal(this.identityOf(ctx), params.agentId, this.opts.autoRegisterPrincipals ?? true);
416
- const principal = await this.query().principalStats(params.agentId, this.identityOf(ctx));
455
+ const principal = await this.query().principalStats(params.agentId, this.readIdentityOf(ctx));
417
456
  json(res, 200, {
418
457
  ok: true,
419
458
  data: {
@@ -427,7 +466,7 @@ export class KnowledgeHttpApp {
427
466
  await this.write.ensurePrincipal(this.identityOf(ctx), params.agentId, this.opts.autoRegisterPrincipals ?? true);
428
467
  json(res, 200, {
429
468
  ok: true,
430
- data: await this.query().catalog(params.agentId, this.identityOf(ctx)),
469
+ data: await this.query().catalog(params.agentId, this.readIdentityOf(ctx)),
431
470
  });
432
471
  });
433
472
  this.route('GET', '/v1/principals/:agentId/chunks', async (req, res, ctx, params) => {
@@ -438,7 +477,7 @@ export class KnowledgeHttpApp {
438
477
  if (!sourceId || !path) {
439
478
  return err(res, 400, 'bad_request', 'sourceId and path required');
440
479
  }
441
- const data = await this.query().listChunks(params.agentId, sourceId, path, this.identityOf(ctx), ctx.sessionId);
480
+ const data = await this.query().listChunks(params.agentId, sourceId, path, this.readIdentityOf(ctx), ctx.sessionId);
442
481
  if (data == null) {
443
482
  return err(res, 404, 'file_not_found', 'source not found');
444
483
  }
@@ -451,13 +490,13 @@ export class KnowledgeHttpApp {
451
490
  return err(res, 400, 'bad_request', 'chunkId or (sourceId+path) required');
452
491
  }
453
492
  if (b.chunkId) {
454
- const data = await this.query().read(params.agentId, b, this.identityOf(ctx), ctx.sessionId);
493
+ const data = await this.query().read(params.agentId, b, this.readIdentityOf(ctx), ctx.sessionId);
455
494
  if (!data.found) {
456
495
  return err(res, 404, 'file_not_found', 'chunk not found');
457
496
  }
458
497
  return json(res, 200, { ok: true, data });
459
498
  }
460
- const chunks = await this.query().listChunks(params.agentId, b.sourceId, b.path, this.identityOf(ctx), ctx.sessionId);
499
+ const chunks = await this.query().listChunks(params.agentId, b.sourceId, b.path, this.readIdentityOf(ctx), ctx.sessionId);
461
500
  if (chunks == null) {
462
501
  return err(res, 404, 'source_not_found', 'source not found');
463
502
  }
@@ -476,7 +515,7 @@ export class KnowledgeHttpApp {
476
515
  await this.write.ensurePrincipal(this.identityOf(ctx), params.agentId, this.opts.autoRegisterPrincipals ?? true);
477
516
  json(res, 200, {
478
517
  ok: true,
479
- data: await this.query().visibility(params.agentId, this.identityOf(ctx)),
518
+ data: await this.query().visibility(params.agentId, this.readIdentityOf(ctx)),
480
519
  });
481
520
  });
482
521
  this.route('POST', '/v1/principals/:agentId/visibility', async (_req, res, ctx, params, body) => {
@@ -500,6 +539,12 @@ export class KnowledgeHttpApp {
500
539
  else if (op === 'unhide' && sourceId) {
501
540
  await this.write.unhideSource(identity, agentId, sourceId);
502
541
  }
542
+ else if (op === 'mount' && sourceId) {
543
+ await this.write.mountSource(identity, agentId, sourceId);
544
+ }
545
+ else if (op === 'unmount' && sourceId) {
546
+ await this.write.unmountSource(identity, agentId, sourceId);
547
+ }
503
548
  else {
504
549
  return err(res, 400, 'bad_request', 'unknown visibility op');
505
550
  }
@@ -521,7 +566,7 @@ export class KnowledgeHttpApp {
521
566
  return err(res, 400, 'session_required', 'sessionId required');
522
567
  json(res, 200, {
523
568
  ok: true,
524
- data: await this.query().sessionVisibility(agentId, sessionId, this.identityOf(ctx)),
569
+ data: await this.query().sessionVisibility(agentId, sessionId, this.readIdentityOf(ctx)),
525
570
  });
526
571
  });
527
572
  this.route('PUT', '/v1/principals/:agentId/session-visibility', async (_req, res, ctx, params, body) => {
@@ -567,11 +612,12 @@ export class KnowledgeHttpApp {
567
612
  return err(res, 400, 'session_required', 'sessionId required');
568
613
  const targetType = url.searchParams.get('targetType');
569
614
  const targetId = url.searchParams.get('targetId');
615
+ const op = url.searchParams.get('op');
570
616
  if (targetType && targetId) {
571
617
  await this.write.clearSessionVisibility(this.identityOf(ctx), sessionId, {
572
618
  targetType: targetType,
573
619
  targetId,
574
- });
620
+ }, op === 'include' || op === 'exclude' ? op : undefined);
575
621
  json(res, 200, { ok: true, data: { sessionId, targetType, targetId } });
576
622
  return;
577
623
  }
@@ -583,7 +629,7 @@ export class KnowledgeHttpApp {
583
629
  json(res, 200, {
584
630
  ok: true,
585
631
  data: await this.query().listJobs({
586
- identity: this.identityOf(ctx),
632
+ identity: this.readIdentityOf(ctx),
587
633
  sourceId: url.searchParams.get('sourceId') ?? undefined,
588
634
  status: url.searchParams.get('status') ?? undefined,
589
635
  }),
@@ -597,7 +643,7 @@ export class KnowledgeHttpApp {
597
643
  if (!(await this.write.isSourceOwner(sourceId, ctx.gatewayId))) {
598
644
  return err(res, 403, 'not_resource_owner', 'not source owner');
599
645
  }
600
- const detail = await this.query().getSourceDetail(sourceId, this.identityOf(ctx));
646
+ const detail = await this.query().getSourceDetail(sourceId, this.readIdentityOf(ctx));
601
647
  if (!detail)
602
648
  return err(res, 404, 'source_not_found', 'source not found');
603
649
  json(res, 200, { ok: true, data: detail.jobControl });
@@ -672,7 +718,22 @@ export class KnowledgeHttpApp {
672
718
  const hit = matchKnowledgeToken(this.opts.tokens, req.headers.authorization);
673
719
  if (!hit)
674
720
  return null;
675
- return { tenantId: hit.tenantId, gatewayId: hit.gatewayId };
721
+ // 租户断言(T3):x-octopi-tenant 上行;重复头/越域断言按未认证拒(fail-loud,不回落)
722
+ const claim = req.headers['x-octopi-tenant'];
723
+ if (Array.isArray(claim))
724
+ return null;
725
+ const resolved = resolveTenantClaim(hit.tenantId, claim ? claim : undefined);
726
+ if ('error' in resolved)
727
+ return null;
728
+ // 写归属域(T3a 收尾护栏):platform 域(broker)无显式 claim 时,写一律降级 DEFAULT 域。
729
+ // 读口不受影响(platform = 全量视图);写绝不能把用户资产落进 platform 共享面。
730
+ const writeTenantId = resolved.tenantId === PLATFORM_TENANT_ID && !claim ? DEFAULT_TENANT_ID : resolved.tenantId;
731
+ return {
732
+ tenantId: resolved.tenantId,
733
+ gatewayId: hit.gatewayId,
734
+ claimed: Boolean(claim),
735
+ writeTenantId,
736
+ };
676
737
  }
677
738
  }
678
739
  /**
@@ -8,6 +8,21 @@ export interface KnowledgeTokenInfo {
8
8
  tenantId: string;
9
9
  gatewayId: string;
10
10
  }
11
+ /**
12
+ * 租户断言(受信 gateway 上行,arch/multi-tenancy.md T3):
13
+ * `x-octopi-tenant` = 调用方断言的 run 归属租户(与 X-Registry-Token 同族的归属通道)。
14
+ * token 域 = PLATFORM(跨域受信)→ 断言任意域放行;token 域 = 其他 → 只可断言本域。
15
+ * 无断言 = token 域(旧语义)。
16
+ *
17
+ * @param tokenTenantId - token 绑定域
18
+ * @param claimed - 请求断言的租户(可缺省)
19
+ * @returns 生效域;断言越域时 `{ error }`(调用方按 401/403 拒)
20
+ */
21
+ export declare function resolveTenantClaim(tokenTenantId: string, claimed: string | undefined): {
22
+ tenantId: string;
23
+ } | {
24
+ error: 'tenant_mismatch';
25
+ };
11
26
  /**
12
27
  * Token → (tenant, gateway)。主线程 401 快速拒绝 + Engine 再验(同一实现)。
13
28
  */
@@ -3,6 +3,25 @@
3
3
  *
4
4
  * 主线程不得跑 Knowledge 业务/SQLite;只转发,保证 /health 在引擎忙时仍可答。
5
5
  */
6
+ import { PLATFORM_TENANT_ID } from '@octopi-agent/core/types.js';
7
+ /**
8
+ * 租户断言(受信 gateway 上行,arch/multi-tenancy.md T3):
9
+ * `x-octopi-tenant` = 调用方断言的 run 归属租户(与 X-Registry-Token 同族的归属通道)。
10
+ * token 域 = PLATFORM(跨域受信)→ 断言任意域放行;token 域 = 其他 → 只可断言本域。
11
+ * 无断言 = token 域(旧语义)。
12
+ *
13
+ * @param tokenTenantId - token 绑定域
14
+ * @param claimed - 请求断言的租户(可缺省)
15
+ * @returns 生效域;断言越域时 `{ error }`(调用方按 401/403 拒)
16
+ */
17
+ export function resolveTenantClaim(tokenTenantId, claimed) {
18
+ if (!claimed)
19
+ return { tenantId: tokenTenantId };
20
+ if (tokenTenantId === PLATFORM_TENANT_ID || tokenTenantId === claimed) {
21
+ return { tenantId: claimed };
22
+ }
23
+ return { error: 'tenant_mismatch' };
24
+ }
6
25
  /**
7
26
  * Token → (tenant, gateway)。主线程 401 快速拒绝 + Engine 再验(同一实现)。
8
27
  */
@@ -51,9 +51,8 @@ async function main() {
51
51
  ingest: boot.ingest ?? null,
52
52
  retriever: boot.retriever ?? null,
53
53
  autoDescribe: boot.autoDescribe,
54
- ...(boot.sqliteVecExtensionPath
55
- ? { sqliteVecExtensionPath: boot.sqliteVecExtensionPath }
56
- : {}),
54
+ sqliteVec: boot.sqliteVec,
55
+ allowPublic: boot.allowPublic,
57
56
  });
58
57
  let closing = false;
59
58
  const shutdown = async (code) => {
@@ -110,6 +110,8 @@ export interface KnowledgeQueryService {
110
110
  autoGround(query: AutoGroundQuery): Promise<AutoGroundDecision>;
111
111
  listSources(query: ListSourcesQuery): Promise<KnowledgeSource[]>;
112
112
  listProjects(identity?: QueryIdentity): Promise<Array<Record<string, unknown>>>;
113
+ /** 单项目登记行(含归属域/可见性);未登记/不可见 = null(管理面域继承用) */
114
+ getProject(identity: QueryIdentity, projectKey: string): Promise<Record<string, unknown> | null>;
113
115
  getSource(sourceId: string, identity?: QueryIdentity): Promise<{
114
116
  source: KnowledgeSource;
115
117
  stats: SourceStatsBundle;
@@ -192,6 +194,7 @@ export declare class LocalKnowledgeQueryService implements KnowledgeQueryService
192
194
  autoGround(query: AutoGroundQuery): Promise<AutoGroundDecision>;
193
195
  listSources(query: ListSourcesQuery): Promise<KnowledgeSource[]>;
194
196
  listProjects(identity?: QueryIdentity): Promise<Array<Record<string, unknown>>>;
197
+ getProject(identity: QueryIdentity, projectKey: string): Promise<Record<string, unknown> | null>;
195
198
  getSource(sourceId: string, identity?: QueryIdentity): Promise<{
196
199
  source: KnowledgeSource;
197
200
  stats: SourceStatsBundle;
@@ -252,7 +255,11 @@ export declare class LocalKnowledgeQueryService implements KnowledgeQueryService
252
255
  minHits?: number;
253
256
  }): Promise<PromotionCandidate[]>;
254
257
  dispose(): void;
255
- private gatewayVisible;
258
+ /**
259
+ * 控制面读口域闸(T3b,评审 MAJ-4):租户闸 ⊕ 网关漏斗 ⊕ 项目跨域闸,单源委托 source-store
260
+ * (原为本地第二套 gatewayVisible 实现——无租户检查,跨租户可读;且违反单源律)。
261
+ */
262
+ private domainVisible;
256
263
  }
257
264
  /**
258
265
  * 组装只读服务。
@@ -272,7 +279,10 @@ export declare function createKnowledgeQueryService(opts: {
272
279
  providers?: Record<string, unknown>;
273
280
  embedding?: unknown;
274
281
  } | null;
275
- sqliteVecExtensionPath?: string;
282
+ sqliteVec?: boolean | {
283
+ extensionPath?: string;
284
+ require?: boolean;
285
+ };
276
286
  /** Worker 角色:meta=列表/控制面,search=检索/catalog,all=单 Worker 全量 */
277
287
  role?: QueryWorkerRole;
278
288
  queryTimeoutMs?: number;
@@ -7,6 +7,7 @@
7
7
  * **禁止**在此接口上实现任何写路径(唯一写者是 Writer Worker / LocalKnowledgeWriteService)。
8
8
  */
9
9
  import { readJobControlState, } from './job-control-state.js';
10
+ import { ownerFunnelSql, projectGateSql, tenantGateSql, tenantOwnGateSql, tenantPrecedenceOrder } from './tenant-gate.js';
10
11
  import { KnowledgeHitLog } from './hit-log.js';
11
12
  /**
12
13
  * 进程内只读实现(测试 / 共享 :memory:)。
@@ -43,7 +44,7 @@ export class LocalKnowledgeQueryService {
43
44
  const { sources } = this.deps;
44
45
  let list = sources.list();
45
46
  if (query.identity) {
46
- list = list.filter((s) => this.gatewayVisible(s, query.identity));
47
+ list = sources.withProjectRowCache(() => list.filter((s) => this.domainVisible(s, query.identity)));
47
48
  }
48
49
  if (query.scopeLevel === 'global' || query.scopeLevel === 'project' || query.scopeLevel === 'session') {
49
50
  list = list.filter((s) => s.scopeRef.level === query.scopeLevel);
@@ -66,18 +67,21 @@ export class LocalKnowledgeQueryService {
66
67
  gatewayId: identity?.gatewayId,
67
68
  });
68
69
  }
70
+ async getProject(identity, projectKey) {
71
+ return this.deps.sources.getProject(projectKey, identity);
72
+ }
69
73
  async getSource(sourceId, identity) {
70
74
  const src = this.deps.sources.get(sourceId);
71
75
  if (!src)
72
76
  return null;
73
- if (identity && !this.gatewayVisible(src, identity))
77
+ if (identity && !this.domainVisible(src, identity))
74
78
  return null;
75
79
  return { source: src, stats: this.deps.index.sourceStats(sourceId) };
76
80
  }
77
81
  async listFiles(sourceId, identity) {
78
82
  if (identity) {
79
83
  const src = this.deps.sources.get(sourceId);
80
- if (!src || !this.gatewayVisible(src, identity))
84
+ if (!src || !this.domainVisible(src, identity))
81
85
  return [];
82
86
  }
83
87
  return this.deps.index.listFiles(sourceId);
@@ -85,7 +89,7 @@ export class LocalKnowledgeQueryService {
85
89
  async listFilesPaged(sourceId, opts, identity) {
86
90
  if (identity) {
87
91
  const src = this.deps.sources.get(sourceId);
88
- if (!src || !this.gatewayVisible(src, identity)) {
92
+ if (!src || !this.domainVisible(src, identity)) {
89
93
  return {
90
94
  items: [],
91
95
  total: 0,
@@ -100,20 +104,32 @@ export class LocalKnowledgeQueryService {
100
104
  }
101
105
  async principalStats(agentId, identity) {
102
106
  const { sources, db } = this.deps;
103
- const visible = sources.listVisible(agentId, undefined, identity).filter((s) => {
104
- return !identity || this.gatewayVisible(s, identity);
105
- });
106
- const stats = db.stats();
107
+ const visible = sources.withProjectRowCache(() => sources.listVisible(agentId, undefined, identity).filter((s) => {
108
+ return !identity || this.domainVisible(s, identity);
109
+ }));
110
+ // 评审 m7(拍板 B):面板统计=本视图口径(只数可见源);全库口径属运维面
111
+ // (doctor/ops),等运维功能落地后再回接,避免「统计有数、列表为空」式误读
112
+ const stats = db.statsForSourceIds(visible.map((s) => s.id));
113
+ const assignedProjects = sources.withProjectRowCache(() => sources
114
+ .list()
115
+ .filter((s) => s.scopeRef.level === 'project' &&
116
+ sources.isVisible(s, agentId, undefined, identity) &&
117
+ (!identity || this.domainVisible(s, identity)))
118
+ .map((s) => s.scopeRef.key));
119
+ const hiddenSourceIds = sources.listHidden(agentId, identity ?? {});
120
+ // 可见性计数器同口径:报本 agent/本身份自己的行,不报全库注册表规模
121
+ stats.projectAgents = assignedProjects.length;
122
+ stats.agentHidden = hiddenSourceIds.length;
123
+ stats.agentMounted = sources.listMounted(agentId, identity ?? {}).length;
124
+ const sessionGate = tenantOwnGateSql('tenant_id', identity ?? {});
125
+ stats.sessionVisibility = db.raw
126
+ .prepare(`SELECT COUNT(*) AS count FROM knowledge_session_visibility WHERE ${sessionGate.sql}`)
127
+ .get(...sessionGate.params).count;
107
128
  return {
108
129
  stats,
109
130
  visibility: {
110
- assignedProjects: sources
111
- .list()
112
- .filter((s) => s.scopeRef.level === 'project' &&
113
- sources.isVisible(s, agentId, undefined, identity) &&
114
- (!identity || this.gatewayVisible(s, identity)))
115
- .map((s) => s.scopeRef.key),
116
- hiddenSourceIds: sources.listHidden(agentId, identity ?? {}),
131
+ assignedProjects,
132
+ hiddenSourceIds,
117
133
  },
118
134
  visibleSources: visible.length,
119
135
  };
@@ -122,17 +138,17 @@ export class LocalKnowledgeQueryService {
122
138
  const items = this.deps.sources.catalogFor(agentId, { identity });
123
139
  if (!identity)
124
140
  return items;
125
- return items.filter((item) => {
141
+ return this.deps.sources.withProjectRowCache(() => items.filter((item) => {
126
142
  const src = this.deps.sources.get(item.id);
127
- return !src || this.gatewayVisible(src, identity);
128
- });
143
+ return !src || this.domainVisible(src, identity);
144
+ }));
129
145
  }
130
146
  async listChunks(agentId, sourceId, path, identity, sessionId) {
131
147
  const { sources, index } = this.deps;
132
148
  const src = sources.get(sourceId);
133
149
  if (!src)
134
150
  return null;
135
- if (identity && !this.gatewayVisible(src, identity))
151
+ if (identity && !this.domainVisible(src, identity))
136
152
  return null;
137
153
  if (!sources.isVisible(src, agentId, sessionId, identity))
138
154
  return null;
@@ -143,7 +159,7 @@ export class LocalKnowledgeQueryService {
143
159
  const visible = (src) => {
144
160
  if (!src)
145
161
  return false;
146
- if (identity && !this.gatewayVisible(src, identity))
162
+ if (identity && !this.domainVisible(src, identity))
147
163
  return false;
148
164
  return sources.isVisible(src, agentId, sessionId, identity);
149
165
  };
@@ -189,7 +205,7 @@ export class LocalKnowledgeQueryService {
189
205
  .list()
190
206
  .filter((s) => s.scopeRef.level === 'project' &&
191
207
  sources.isVisible(s, agentId, undefined, identity) &&
192
- (!identity || this.gatewayVisible(s, identity)))
208
+ (!identity || this.domainVisible(s, identity)))
193
209
  .map((s) => s.scopeRef.key);
194
210
  return {
195
211
  assignedProjects: [...new Set(assigned)],
@@ -205,7 +221,7 @@ export class LocalKnowledgeQueryService {
205
221
  if (item.targetType !== 'source')
206
222
  return true;
207
223
  const src = this.deps.sources.get(item.targetId);
208
- return !src || this.gatewayVisible(src, identity);
224
+ return !src || this.domainVisible(src, identity);
209
225
  });
210
226
  }
211
227
  async dbStats() {
@@ -216,7 +232,7 @@ export class LocalKnowledgeQueryService {
216
232
  const src = sources.get(sourceId);
217
233
  if (!src)
218
234
  return null;
219
- if (identity && !this.gatewayVisible(src, identity))
235
+ if (identity && !this.domainVisible(src, identity))
220
236
  return null;
221
237
  const jobControl = readJobControlState(db, index, sourceId, {
222
238
  embeddingEnabled: this.deps.embeddingEnabled ?? true,
@@ -232,18 +248,22 @@ export class LocalKnowledgeQueryService {
232
248
  };
233
249
  }
234
250
  async getPrincipal(identity, agentId) {
251
+ const gate = tenantOwnGateSql('tenant_id', identity);
252
+ const order = tenantPrecedenceOrder('tenant_id', identity);
235
253
  const row = this.deps.db.raw
236
254
  .prepare(`SELECT * FROM knowledge_principals
237
- WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ?`)
238
- .get(identity.tenantId ?? 'default', identity.gatewayId ?? 'default', agentId);
255
+ WHERE ${gate.sql} AND gateway_id = ? AND local_agent_id = ?
256
+ ORDER BY ${order.sql} LIMIT 1`)
257
+ .get(...gate.params, identity.gatewayId ?? 'default', agentId, ...order.params);
239
258
  return row ?? null;
240
259
  }
241
260
  async isPrincipalForeign(identity, agentId) {
261
+ const gate = tenantOwnGateSql('tenant_id', identity);
242
262
  const row = this.deps.db.raw
243
263
  .prepare(`SELECT 1 AS ok FROM knowledge_principals
244
- WHERE tenant_id = ? AND local_agent_id = ? AND gateway_id != ?
264
+ WHERE ${gate.sql} AND local_agent_id = ? AND gateway_id != ?
245
265
  LIMIT 1`)
246
- .get(identity.tenantId ?? 'default', agentId, identity.gatewayId ?? 'default');
266
+ .get(...gate.params, agentId, identity.gatewayId ?? 'default');
247
267
  return Boolean(row);
248
268
  }
249
269
  async isSourceOwner(sourceId, gatewayId) {
@@ -273,10 +293,24 @@ export class LocalKnowledgeQueryService {
273
293
  };
274
294
  }
275
295
  async listJobs(query) {
296
+ const tenantGate = tenantGateSql('s.tenant_id', query.identity);
297
+ const funnel = ownerFunnelSql('s.registered_by', 's.visibility', query.identity);
298
+ const projectGate = projectGateSql('p.tenant_id', 'p.visibility', query.identity);
299
+ // 源范围与读侧同口径(评审 m1):租户闸 ⊕ 归属漏斗 ⊕ 项目跨域闸——job 行携带 path/last_error,
300
+ // 不得成为项目闸旁路的元数据泄漏面
276
301
  const where = [
277
- `source_id IN (SELECT id FROM knowledge_sources WHERE tenant_id = ? AND (registered_by = ? OR visibility = 'public'))`,
302
+ `source_id IN (
303
+ SELECT s.id FROM knowledge_sources s
304
+ WHERE ${tenantGate.sql} AND ${funnel.sql}
305
+ AND (s.scope_level != 'project' OR EXISTS (
306
+ SELECT 1 FROM knowledge_projects p
307
+ WHERE p.project_key = s.scope_key
308
+ AND IFNULL(p.tenant_id, '') = IFNULL(s.tenant_id, '')
309
+ AND ${projectGate.sql}
310
+ ))
311
+ )`,
278
312
  ];
279
- const args = [query.identity.tenantId ?? 'default', query.identity.gatewayId ?? 'default'];
313
+ const args = [...tenantGate.params, ...funnel.params, ...projectGate.params];
280
314
  if (query.sourceId) {
281
315
  where.push('source_id = ?');
282
316
  args.push(query.sourceId);
@@ -296,17 +330,17 @@ export class LocalKnowledgeQueryService {
296
330
  dispose() {
297
331
  /* 只读;连接由调用方持有 */
298
332
  }
299
- gatewayVisible(src, identity) {
300
- const row = this.deps.db.raw
301
- .prepare('SELECT registered_by, visibility FROM knowledge_sources WHERE id = ?')
302
- .get(src.id);
303
- if (!row)
304
- return true;
305
- if (!identity.gatewayId)
306
- return true;
307
- if (row.registered_by === identity.gatewayId)
308
- return true;
309
- return row.visibility === 'public';
333
+ /**
334
+ * 控制面读口域闸(T3b,评审 MAJ-4):租户闸 ⊕ 网关漏斗 ⊕ 项目跨域闸,单源委托 source-store
335
+ * (原为本地第二套 gatewayVisible 实现——无租户检查,跨租户可读;且违反单源律)。
336
+ */
337
+ domainVisible(src, identity) {
338
+ if (src.scopeRef.level === 'project' &&
339
+ !this.deps.sources.isProjectVisible(src, src.scopeRef.key, identity)) {
340
+ return false;
341
+ }
342
+ return (this.deps.sources.isTenantVisible(src, identity) &&
343
+ this.deps.sources.isGatewayVisible(src, identity));
310
344
  }
311
345
  }
312
346
  export async function createKnowledgeQueryService(opts) {
@@ -319,7 +353,7 @@ export async function createKnowledgeQueryService(opts) {
319
353
  return WorkerQueryService.start({
320
354
  dbPath: opts.dbPath,
321
355
  embeddingModels: opts.embeddingModels ?? null,
322
- sqliteVecExtensionPath: opts.sqliteVecExtensionPath,
356
+ sqliteVec: opts.sqliteVec,
323
357
  role: opts.role ?? 'all',
324
358
  queryTimeoutMs: opts.queryTimeoutMs,
325
359
  embed: opts.embed ?? null,
@@ -23,7 +23,10 @@ export declare class WorkerQueryService implements KnowledgeQueryService {
23
23
  */
24
24
  static start(opts: {
25
25
  dbPath: string;
26
- sqliteVecExtensionPath?: string;
26
+ sqliteVec?: boolean | {
27
+ extensionPath?: string;
28
+ require?: boolean;
29
+ };
27
30
  queryTimeoutMs?: number;
28
31
  role?: QueryWorkerRole;
29
32
  embeddingModels?: {
@@ -41,6 +44,7 @@ export declare class WorkerQueryService implements KnowledgeQueryService {
41
44
  autoGround(query: AutoGroundQuery): Promise<AutoGroundDecision>;
42
45
  listSources(query: ListSourcesQuery): Promise<KnowledgeSource[]>;
43
46
  listProjects(identity?: QueryIdentity): Promise<Array<Record<string, unknown>>>;
47
+ getProject(identity: QueryIdentity, projectKey: string): Promise<Record<string, unknown> | null>;
44
48
  getSource(sourceId: string, identity?: QueryIdentity): Promise<{
45
49
  source: KnowledgeSource;
46
50
  stats: SourceStatsBundle;
@@ -72,7 +72,7 @@ export class WorkerQueryService {
72
72
  const worker = new Worker(resolveWorkerUrl('./query-worker.js', import.meta.url), {
73
73
  workerData: {
74
74
  dbPath: opts.dbPath,
75
- sqliteVecExtensionPath: opts.sqliteVecExtensionPath,
75
+ sqliteVec: opts.sqliteVec,
76
76
  embeddingModels: opts.embeddingModels ?? null,
77
77
  role: opts.role ?? 'all',
78
78
  embed: opts.embed ?? null,
@@ -129,6 +129,9 @@ export class WorkerQueryService {
129
129
  async listProjects(identity) {
130
130
  return (await this.call({ method: 'listProjects', identity }));
131
131
  }
132
+ async getProject(identity, projectKey) {
133
+ return (await this.call({ method: 'getProject', identity, projectKey }));
134
+ }
132
135
  async getSource(sourceId, identity) {
133
136
  return (await this.call({ method: 'getSource', sourceId, identity }));
134
137
  }