@sanqianx/project-knowledge 5.0.18 → 5.0.20-rc.1

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 (209) hide show
  1. package/.agents/plugins/marketplace.json +3 -3
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/CHANGELOG.md +14 -0
  4. package/LICENSE +201 -201
  5. package/NOTICE +5 -5
  6. package/README.md +349 -349
  7. package/_modules/knowledge-engine/README.md +4 -0
  8. package/_modules/knowledge-engine/src/analysis-contract.js +4 -3
  9. package/_modules/knowledge-engine/src/analysis-failures.js +39 -0
  10. package/_modules/knowledge-engine/src/analysis-worker.js +6 -2
  11. package/_modules/knowledge-engine/src/coordinator.js +8 -0
  12. package/_modules/knowledge-engine/src/headless-analyzer.js +2 -1
  13. package/_modules/knowledge-engine/src/memories.js +10 -5
  14. package/_modules/knowledge-engine/src/memory-eligibility.js +74 -0
  15. package/_modules/knowledge-engine/src/retrieval-ranking.js +19 -1
  16. package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +1 -0
  17. package/_modules/knowledge-engine/src/runtime/commit-prompt.js +12 -9
  18. package/_modules/knowledge-engine/src/runtime/conversation-store.js +2 -0
  19. package/_modules/knowledge-engine/src/server.js +1 -1
  20. package/_site/_test/agent-capture-mcp-ui-test.js +67 -67
  21. package/_site/_test/ai-profile-test.js +75 -75
  22. package/_site/_test/ai-vendor-presets-test.js +91 -91
  23. package/_site/_test/automatic-context-test.js +70 -70
  24. package/_site/_test/bin-cli-test.js +230 -230
  25. package/_site/_test/bridge-adapter-test.js +95 -95
  26. package/_site/_test/bridge-consumer-service-test.js +310 -310
  27. package/_site/_test/candidate-bin-content-test.js +35 -0
  28. package/_site/_test/candidate-product-assets-test.js +13 -0
  29. package/_site/_test/candidate-release-contract-test.js +14 -14
  30. package/_site/_test/capture-hook-dedup-test.js +158 -158
  31. package/_site/_test/claude-executable-discovery-test.js +28 -28
  32. package/_site/_test/claude-session-lifecycle-sweep-test.js +252 -252
  33. package/_site/_test/cli-instance-management-test.js +62 -62
  34. package/_site/_test/client-capture-restart-e2e-test.js +189 -189
  35. package/_site/_test/codex-conversation-projection-test.js +60 -60
  36. package/_site/_test/codex-marketplace-scoped-path-test.js +186 -186
  37. package/_site/_test/codex-notify-capture-e2e-test.js +101 -101
  38. package/_site/_test/codex-notify-fanout-test.js +134 -134
  39. package/_site/_test/codex-notify-upgrade-test.js +253 -253
  40. package/_site/_test/commit-boundary-freeze-test.js +147 -147
  41. package/_site/_test/commit-conversation-binding-test.js +4 -4
  42. package/_site/_test/configuration-generation-test.js +22 -22
  43. package/_site/_test/context-pack-test.js +200 -200
  44. package/_site/_test/conversation-api-test.js +10 -10
  45. package/_site/_test/conversation-origin-test.js +15 -0
  46. package/_site/_test/cross-repo-isolation-e2e-test.js +187 -187
  47. package/_site/_test/embedding-config-test.js +32 -32
  48. package/_site/_test/fixtures/fake-browser-exit.js +10 -10
  49. package/_site/_test/fixtures/fake-browser-hang.js +10 -10
  50. package/_site/_test/fixtures/integration-busy-worker.cjs +6 -6
  51. package/_site/_test/fixtures/make-git-repos.js +92 -92
  52. package/_site/_test/fixtures/retrieval-gold-queries.json +32 -0
  53. package/_site/_test/fresh-hook-reimport-test.js +53 -53
  54. package/_site/_test/fresh-product-data-test.js +56 -56
  55. package/_site/_test/git-path-compatibility-test.js +80 -80
  56. package/_site/_test/git-validation-test.js +81 -81
  57. package/_site/_test/hook-worktree-test.js +42 -42
  58. package/_site/_test/index-builder-test.js +41 -41
  59. package/_site/_test/index-error-display-contract-test.js +10 -10
  60. package/_site/_test/integration-adapters-test.js +67 -67
  61. package/_site/_test/integration-capture-setup-test.js +118 -118
  62. package/_site/_test/integration-runtime-binding-a2-test.js +566 -566
  63. package/_site/_test/integration-runtime-binding-a3-test.js +227 -227
  64. package/_site/_test/integration-runtime-binding-test.js +473 -473
  65. package/_site/_test/integration-status-scan-test.js +29 -29
  66. package/_site/_test/kb-framework-test.js +57 -57
  67. package/_site/_test/kbpath-follow-test.js +63 -63
  68. package/_site/_test/knowledge-db-test.js +144 -144
  69. package/_site/_test/knowledge-engine-client-test.js +109 -109
  70. package/_site/_test/knowledge-maintenance-test.js +40 -40
  71. package/_site/_test/knowledge-panel-ui-contract-test.js +112 -0
  72. package/_site/_test/knowledge-scopes-test.js +37 -37
  73. package/_site/_test/knowledge-storage-location-test.js +28 -28
  74. package/_site/_test/launcher-readiness-test.js +80 -80
  75. package/_site/_test/legacy-claude-session-alias-test.js +106 -106
  76. package/_site/_test/legacy-routes-removed-test.js +47 -47
  77. package/_site/_test/legacy-workbench-exclusion-test.js +92 -92
  78. package/_site/_test/live-gateway-test.js +48 -48
  79. package/_site/_test/local-marketplace-test.js +16 -16
  80. package/_site/_test/log-redaction-test.js +58 -58
  81. package/_site/_test/markdown-maintenance-test.js +11 -11
  82. package/_site/_test/model-context-windows-test.js +89 -89
  83. package/_site/_test/module-agent-start-proxy-test.js +65 -65
  84. package/_site/_test/module-artifact-integrity-test.js +27 -27
  85. package/_site/_test/module-boundary-test.js +30 -30
  86. package/_site/_test/module-bridge-eventbridge-test.js +202 -202
  87. package/_site/_test/module-dynamic-port-test.js +118 -118
  88. package/_site/_test/module-index-proxy-test.js +33 -0
  89. package/_site/_test/module-model-configuration-test.js +44 -44
  90. package/_site/_test/module-process-lifecycle-test.js +35 -35
  91. package/_site/_test/module-stream-proxy-test.js +34 -34
  92. package/_site/_test/module-terminal-restart-test.js +63 -0
  93. package/_site/_test/module-wiring-unbind-race-test.js +59 -59
  94. package/_site/_test/native-context-integrations-test.js +86 -86
  95. package/_site/_test/orphaned-claim-recovery-test.js +80 -80
  96. package/_site/_test/package-boundary-test.js +43 -42
  97. package/_site/_test/package-startup-test.js +56 -56
  98. package/_site/_test/packaged-ui-smoke-test.js +88 -88
  99. package/_site/_test/pr-consumer-contract-test.js +55 -55
  100. package/_site/_test/product-brand-contract-test.js +25 -25
  101. package/_site/_test/product-diagnostics-ui-test.js +93 -93
  102. package/_site/_test/product-import-ui-test.js +104 -104
  103. package/_site/_test/product-progress-status-test.js +12 -1
  104. package/_site/_test/project-remove-running-guard-test.js +79 -79
  105. package/_site/_test/project-store-test.js +90 -90
  106. package/_site/_test/registry-events-test.js +38 -0
  107. package/_site/_test/release-version-sync-test.js +115 -115
  108. package/_site/_test/requirement-binding-test.js +113 -113
  109. package/_site/_test/runtime-endpoint-test.js +64 -64
  110. package/_site/_test/scanner-test.js +83 -83
  111. package/_site/_test/server-security-test.js +96 -96
  112. package/_site/_test/sessions-stream-test.js +93 -93
  113. package/_site/_test/shared-scope-root-binding-test.js +67 -67
  114. package/_site/_test/simple-import-test.js +98 -98
  115. package/_site/_test/startup-analysis-disabled-test.js +6 -6
  116. package/_site/_test/team-knowledge-store-sparse-checkout-test.js +196 -196
  117. package/_site/_test/team-knowledge-store-test.js +132 -132
  118. package/_site/_test/test-server-cleanup-test.js +35 -35
  119. package/_site/_test/test-server-module-isolation-test.js +37 -37
  120. package/_site/_test/tracking-start-test.js +35 -35
  121. package/_site/_test/workbench-context-receipts-test.js +22 -0
  122. package/_site/_test/workspace-ui-contract-test.js +31 -31
  123. package/_site/_test/zcode-capture-integration-test.js +108 -108
  124. package/_site/lib/ai-vendor-presets.js +83 -83
  125. package/_site/lib/ai-workspace.js +3 -3
  126. package/_site/lib/atomic-file.js +204 -204
  127. package/_site/lib/bridge-adapter.js +260 -260
  128. package/_site/lib/capture-hook-config.js +108 -108
  129. package/_site/lib/claude-cli-runner.js +89 -89
  130. package/_site/lib/cli-instances.js +43 -43
  131. package/_site/lib/codex-context-status.js +17 -17
  132. package/_site/lib/codex-notify-config.js +193 -193
  133. package/_site/lib/commit-conversation-binder.js +13 -12
  134. package/_site/lib/commit-reconciler.js +38 -38
  135. package/_site/lib/context-pack-builder.js +299 -299
  136. package/_site/lib/conversation-exclusions.js +204 -204
  137. package/_site/lib/conversation-query-service.js +17 -17
  138. package/_site/lib/conversation-store.js +2 -0
  139. package/_site/lib/data-dir.js +13 -13
  140. package/_site/lib/embedding-service.js +85 -85
  141. package/_site/lib/engine-connection.js +93 -90
  142. package/_site/lib/engine-knowledge-client.js +72 -72
  143. package/_site/lib/folder-picker-output.js +3 -3
  144. package/_site/lib/git-path.js +14 -14
  145. package/_site/lib/github-team-store.js +3 -3
  146. package/_site/lib/index-builder.js +157 -157
  147. package/_site/lib/integration-status-scan.js +99 -99
  148. package/_site/lib/integration-status-worker.js +14 -14
  149. package/_site/lib/kb-framework.js +15 -15
  150. package/_site/lib/knowledge-db.js +363 -363
  151. package/_site/lib/knowledge-schema.js +121 -121
  152. package/_site/lib/live-gateway.js +78 -76
  153. package/_site/lib/llm-client.js +12 -12
  154. package/_site/lib/model-context-windows.js +82 -82
  155. package/_site/lib/module-bridge.js +874 -865
  156. package/_site/lib/module-port-resolver.js +57 -57
  157. package/_site/lib/post-commit-automation.js +54 -54
  158. package/_site/lib/product-data.js +35 -35
  159. package/_site/lib/project-lifecycle-service.js +14 -14
  160. package/_site/lib/project-registry-store.js +129 -129
  161. package/_site/lib/prompt-registry.js +148 -148
  162. package/_site/lib/registry-events.js +16 -0
  163. package/_site/lib/requirement-adapters.js +10 -10
  164. package/_site/lib/requirement-binder.js +112 -112
  165. package/_site/lib/runtime-control.js +89 -89
  166. package/_site/lib/runtime-endpoint.js +5 -5
  167. package/_site/lib/server-app.js +10 -1
  168. package/_site/lib/zcode-context-integration.js +60 -60
  169. package/_site/scripts/codex-notify-fanout.cjs +95 -95
  170. package/_site/scripts/install-module-candidates.js +69 -69
  171. package/_site/scripts/pack-module-candidates.js +89 -72
  172. package/_site/scripts/sync-product-content.js +28 -28
  173. package/_site/scripts/sync-release-version.js +164 -164
  174. package/_site/scripts/vendor-modules.js +49 -49
  175. package/_site/scripts/verify-module-runtime.js +50 -50
  176. package/_site/scripts/verify-product-runtime.js +36 -36
  177. package/_site/scripts/verify-retrieval-gold.js +83 -0
  178. package/_site/server.js +15 -15
  179. package/_site/start.bat +26 -26
  180. package/_site/stop.bat +11 -11
  181. package/bin/project-knowledge-integrations.js +254 -254
  182. package/bin/project-knowledge-kb.js +67 -67
  183. package/bin/project-knowledge-mcp.js +8 -8
  184. package/bin/project-knowledge.js +170 -170
  185. package/claude-prompts.json +21 -21
  186. package/docs/README.zh-CN.md +205 -205
  187. package/docs/assets/architecture.svg +181 -181
  188. package/docs/assets/logo.svg +49 -49
  189. package/docs/fresh-v5-start.md +42 -42
  190. package/docs/leader-glm-workbench-playbook.md +122 -119
  191. package/docs/project-registry-schema.md +11 -11
  192. package/docs/release-5.0.16-acceptance.md +176 -176
  193. package/docs/release-5.0.17-acceptance.md +117 -117
  194. package/docs/release-5.0.19-acceptance.md +39 -0
  195. package/docs/team-knowledge-mode-a-plan.md +73 -73
  196. package/module-runtime-manifest.json +126 -104
  197. package/package.json +110 -108
  198. package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
  199. package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
  200. package/plugins/project-knowledge/.mcp.json +1 -1
  201. package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
  202. package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
  203. package/templates/change.md +30 -30
  204. package/templates/project-readme.md +18 -18
  205. package/ui/index.html +1 -1
  206. package/ui/knowledge-panel.html +118 -0
  207. package/ui/product.css +8 -0
  208. package/ui/product.js +175 -8
  209. package/vendor-manifest.json +55 -53
@@ -1,202 +1,202 @@
1
- // Module bridge: ai-coding-event-bridge console leg.
2
- //
3
- // Exercises the third embedded module end to end against a stub console
4
- // server (same surface as packages/console/src/server.js): proxy mapping,
5
- // idempotent registration by Git work tree path, removal (404-tolerant),
6
- // aggregated projection, health, and the supervised-spawn spec (vendored
7
- // runtime, command override, bridge-home passthrough). Real module services
8
- // are never contacted — terminal/vector-hub point at a reserved loopback
9
- // port that refuses instantly.
10
-
11
- const assert = require('assert');
12
- const http = require('http');
13
- const { PassThrough } = require('stream');
14
- const { ModuleBridge, defaultEventBridgeUrl } = require('../lib/module-bridge');
15
-
16
- const DEAD_URL = 'http://127.0.0.1:1'; // reserved port: connection refused, never a real service
17
-
18
- function makeStoreStub() {
19
- const store = {
20
- state: { modules: {} },
21
- readConfig: () => ({ displayName: 'demo', repoPath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' }),
22
- readState: () => store.state,
23
- updateState: async (_id, mutator) => { mutator(store.state); },
24
- };
25
- return store;
26
- }
27
-
28
- function fakeRes() {
29
- return {
30
- statusCode: 0, headers: {}, body: '',
31
- writeHead(code, headers) { this.statusCode = code; this.headers = headers || {}; },
32
- end(buf) { this.body = buf ? Buffer.from(buf).toString('utf8') : ''; },
33
- };
34
- }
35
-
36
- function fakeReq(body) {
37
- const req = new PassThrough();
38
- req.method = 'GET';
39
- req.headers = {};
40
- req.end(body || undefined);
41
- return req;
42
- }
43
-
44
- async function main() {
45
- assert.strictEqual(defaultEventBridgeUrl(), 'http://127.0.0.1:8790', 'loopback default URL');
46
-
47
- // ---- stub console server (same API surface the shell integrates against) ----
48
- const hits = [];
49
- const registered = [];
50
- const stub = http.createServer((req, res) => {
51
- const url = new URL(req.url, 'http://localhost');
52
- hits.push(`${req.method} ${url.pathname}${url.search}`);
53
- // Real console health carries registeredProjects; the identity check
54
- // rejects generic {"ok":true} bodies from foreign port squatters.
55
- if (url.pathname === '/api/health') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ ok: true, registeredProjects: registered.length })); return; }
56
- if (url.pathname === '/api/echo') {
57
- res.writeHead(200, { 'Content-Type': 'application/json' });
58
- res.end(JSON.stringify({ echo: `${req.method} ${url.pathname}`, query: Object.fromEntries(url.searchParams) }));
59
- return;
60
- }
61
- if (url.pathname === '/api/projects' && req.method === 'GET') {
62
- res.writeHead(200, { 'Content-Type': 'application/json' });
63
- res.end(JSON.stringify({ projects: registered }));
64
- return;
65
- }
66
- if (url.pathname === '/api/projects' && req.method === 'POST') {
67
- let body = '';
68
- req.on('data', c => { body += c; });
69
- req.on('end', () => {
70
- const parsed = JSON.parse(body || '{}');
71
- if (registered.some(p => p.path === parsed.path)) {
72
- res.writeHead(409, { 'Content-Type': 'application/json' });
73
- res.end(JSON.stringify({ error: { code: 'PROJECT_DUPLICATE', message: 'duplicate' } }));
74
- return;
75
- }
76
- const project = { id: `sha256:${registered.length + 1}`, name: 'demo', path: parsed.path, store: parsed.store || 'global', auto: false, turns: 3 };
77
- registered.push(project);
78
- res.writeHead(201, { 'Content-Type': 'application/json' });
79
- res.end(JSON.stringify({ project }));
80
- });
81
- return;
82
- }
83
- if (url.pathname === '/api/projects' && req.method === 'DELETE') {
84
- const id = url.searchParams.get('id');
85
- const index = registered.findIndex(p => p.id === id);
86
- if (index === -1) {
87
- res.writeHead(404, { 'Content-Type': 'application/json' });
88
- res.end(JSON.stringify({ error: { code: 'PROJECT_NOT_FOUND', message: 'unknown' } }));
89
- return;
90
- }
91
- registered.splice(index, 1);
92
- res.writeHead(200, { 'Content-Type': 'application/json' });
93
- res.end(JSON.stringify({ removed: true }));
94
- return;
95
- }
96
- res.writeHead(404, { 'Content-Type': 'application/json' });
97
- res.end(JSON.stringify({ error: { code: 'NOT_FOUND', message: req.url } }));
98
- });
99
- await new Promise(resolve => stub.listen(0, '127.0.0.1', resolve));
100
- const stubUrl = `http://127.0.0.1:${stub.address().port}`;
101
-
102
- const savedEnv = { ...process.env };
103
- delete process.env.KB_MODULES_REGISTER;
104
- delete process.env.AI_CODING_EVENT_BRIDGE_HOME;
105
- delete process.env.KB_EVENTBRIDGE_COMMAND;
106
-
107
- try {
108
- const bridge = new ModuleBridge({
109
- terminalUrl: DEAD_URL,
110
- vectorHubUrl: DEAD_URL,
111
- eventBridgeUrl: stubUrl,
112
- engineUrl: DEAD_URL,
113
- registryStore: { listIds: () => ['demo-proj'], readDisplaySnapshot: id => id === 'demo-proj' ? { displayName: 'demo' } : null },
114
- projectStore: makeStoreStub(),
115
- });
116
-
117
- // ---- proxy mapping: /api/eventbridge/<rest> -> <console>/api/<rest> ----
118
- assert.strictEqual(bridge._eventBridgeApi('/api/eventbridge/projects'), `${stubUrl}/api/projects`, 'api prefix mapping');
119
- const pres = fakeRes();
120
- await bridge.proxyEventBridge(fakeReq(), pres, '/api/eventbridge/echo', '?x=1');
121
- assert.strictEqual(pres.statusCode, 200, 'proxy reaches the console');
122
- assert.deepStrictEqual(JSON.parse(pres.body).query, { x: '1' }, 'proxy forwards query string');
123
-
124
- // ---- registration: POST { path, store: <knowledge>/dev-conversations } ----
125
- const wiringStore = bridge.projectStore;
126
- const first = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
127
- assert.strictEqual(first.eventBridge.ok, true, 'event-bridge leg registers');
128
- assert.strictEqual(first.eventBridge.status, 'registered', 'first registration is not a reuse');
129
- assert.ok(first.eventBridge.projectId, 'registration returns the console project id');
130
- const postHits = hits.filter(h => h.startsWith('POST /api/projects'));
131
- assert.strictEqual(postHits.length, 1, 'exactly one POST issued');
132
- assert.ok(wiringStore.state.modules.eventBridge, 'wiring persisted');
133
- assert.ok(wiringStore.state.modules.eventBridge.store.endsWith('dev-conversations'), 'store follows the recommended layout');
134
-
135
- // ---- idempotent registration: same path is reused, no second POST ----
136
- const second = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
137
- assert.strictEqual(second.eventBridge.ok, true, 're-registration succeeds');
138
- assert.strictEqual(second.eventBridge.reused, true, 're-registration reuses by path');
139
- assert.strictEqual(hits.filter(h => h.startsWith('POST /api/projects')).length, 1, 'no duplicate POST on re-registration');
140
-
141
- // ---- aggregated projection ----
142
- const aggregate = await bridge.aggregatedProjects();
143
- assert.strictEqual(aggregate.eventBridgeAvailable, true, 'event-bridge available flag');
144
- const projected = aggregate.projects.find(p => p.projectId === 'demo-proj');
145
- assert.strictEqual(projected.modules.eventBridge.registered, true, 'sidebar projection marks event-bridge registered');
146
- assert.strictEqual(projected.modules.eventBridge.turns, 3, 'sidebar projection carries turn count');
147
-
148
- // ---- health ----
149
- const health = await bridge.modulesHealth();
150
- assert.strictEqual(health.modules.eventBridge.up, true, 'health reports event-bridge up');
151
- assert.strictEqual(health.modules.terminal.up, false, 'health keeps terminal leg honest');
152
-
153
- // An unreachable writer must block every subsequent removal.
154
- const beforeRemoval = hits.length;
155
- await assert.rejects(bridge.removeProjectLinks({ projectId: 'demo-proj' }), { code: 'PROJECT_BUSY' });
156
- assert.strictEqual(hits.length, beforeRemoval, 'offline engine cannot remove other module bindings');
157
- bridge.engineUrl = stubUrl; // This stub returns 404: the writer is already absent.
158
- // ---- removal: DELETE ?id=..., wiring cleared ----
159
- const removal = await bridge.removeProjectLinks({ projectId: 'demo-proj' });
160
- assert.strictEqual(removal.eventBridge.ok, true, 'removal succeeds');
161
- assert.ok(hits.some(h => h.startsWith(`DELETE /api/projects?id=${encodeURIComponent(first.eventBridge.projectId)}`)), 'DELETE carries the console project id');
162
- assert.strictEqual(wiringStore.state.modules.eventBridge.status, 'removed', 'wiring marked removed');
163
-
164
- // ---- removal when the console no longer knows the project: 404 tolerated ----
165
- const removalAgain = await bridge.removeProjectLinks({ projectId: 'demo-proj' });
166
- assert.strictEqual(removalAgain.eventBridge.ok, true, '404 on removal counts as removed');
167
-
168
- // ---- supervised spawn spec ----
169
- const specs = bridge._supervisedSpecs();
170
- const ebSpec = specs.find(s => s.name === 'event-bridge');
171
- assert.ok(ebSpec, 'supervisor specs include event-bridge');
172
- assert.ok(ebSpec.command.includes('serve'), 'spec serves the console');
173
- // Windows 路径分隔符无关:命令里的绝对路径在 win32 上是反斜杠
174
- assert.ok(ebSpec.command.replace(new RegExp("\\\\+", 'g'), '/').includes('@sanqianx/ai-coding-event-bridge-console'), 'spec spawns the packaged console runtime; actual=' + ebSpec.command);
175
- assert.ok(ebSpec.url === stubUrl, 'spec binds the configured URL');
176
-
177
- process.env.KB_EVENTBRIDGE_COMMAND = 'node C:/explicit/console-bin.js serve --port 9001';
178
- const overridden = bridge._supervisedSpecs().find(s => s.name === 'event-bridge');
179
- assert.ok(overridden.command.includes('C:/explicit/console-bin.js'), 'KB_EVENTBRIDGE_COMMAND override wins');
180
-
181
- process.env.AI_CODING_EVENT_BRIDGE_HOME = 'D:/bridge-home';
182
- delete process.env.KB_EVENTBRIDGE_COMMAND;
183
- const withHome = bridge._supervisedSpecs().find(s => s.name === 'event-bridge');
184
- assert.ok(withHome.command.includes('--home'), 'bridge home is passed through to the console');
185
- assert.ok(withHome.command.includes('D:\\bridge-home') || withHome.command.includes('D:/bridge-home'), 'bridge home path is quoted into the command line');
186
-
187
- // ---- KB_MODULES_REGISTER=0 keeps the fan-out off ----
188
- process.env.KB_MODULES_REGISTER = '0';
189
- const disabled = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
190
- assert.strictEqual(disabled.eventBridge.status, 'disabled', 'registration honors KB_MODULES_REGISTER=0');
191
- } finally {
192
- process.env = savedEnv;
193
- stub.close();
194
- }
195
-
196
- console.log('module-bridge-eventbridge-test PASS');
197
- }
198
-
199
- main().catch(error => {
200
- console.error(error && error.stack ? error.stack : error);
201
- process.exit(1);
202
- });
1
+ // Module bridge: ai-coding-event-bridge console leg.
2
+ //
3
+ // Exercises the third embedded module end to end against a stub console
4
+ // server (same surface as packages/console/src/server.js): proxy mapping,
5
+ // idempotent registration by Git work tree path, removal (404-tolerant),
6
+ // aggregated projection, health, and the supervised-spawn spec (vendored
7
+ // runtime, command override, bridge-home passthrough). Real module services
8
+ // are never contacted — terminal/vector-hub point at a reserved loopback
9
+ // port that refuses instantly.
10
+
11
+ const assert = require('assert');
12
+ const http = require('http');
13
+ const { PassThrough } = require('stream');
14
+ const { ModuleBridge, defaultEventBridgeUrl } = require('../lib/module-bridge');
15
+
16
+ const DEAD_URL = 'http://127.0.0.1:1'; // reserved port: connection refused, never a real service
17
+
18
+ function makeStoreStub() {
19
+ const store = {
20
+ state: { modules: {} },
21
+ readConfig: () => ({ displayName: 'demo', repoPath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' }),
22
+ readState: () => store.state,
23
+ updateState: async (_id, mutator) => { mutator(store.state); },
24
+ };
25
+ return store;
26
+ }
27
+
28
+ function fakeRes() {
29
+ return {
30
+ statusCode: 0, headers: {}, body: '',
31
+ writeHead(code, headers) { this.statusCode = code; this.headers = headers || {}; },
32
+ end(buf) { this.body = buf ? Buffer.from(buf).toString('utf8') : ''; },
33
+ };
34
+ }
35
+
36
+ function fakeReq(body) {
37
+ const req = new PassThrough();
38
+ req.method = 'GET';
39
+ req.headers = {};
40
+ req.end(body || undefined);
41
+ return req;
42
+ }
43
+
44
+ async function main() {
45
+ assert.strictEqual(defaultEventBridgeUrl(), 'http://127.0.0.1:8790', 'loopback default URL');
46
+
47
+ // ---- stub console server (same API surface the shell integrates against) ----
48
+ const hits = [];
49
+ const registered = [];
50
+ const stub = http.createServer((req, res) => {
51
+ const url = new URL(req.url, 'http://localhost');
52
+ hits.push(`${req.method} ${url.pathname}${url.search}`);
53
+ // Real console health carries registeredProjects; the identity check
54
+ // rejects generic {"ok":true} bodies from foreign port squatters.
55
+ if (url.pathname === '/api/health') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ ok: true, registeredProjects: registered.length })); return; }
56
+ if (url.pathname === '/api/echo') {
57
+ res.writeHead(200, { 'Content-Type': 'application/json' });
58
+ res.end(JSON.stringify({ echo: `${req.method} ${url.pathname}`, query: Object.fromEntries(url.searchParams) }));
59
+ return;
60
+ }
61
+ if (url.pathname === '/api/projects' && req.method === 'GET') {
62
+ res.writeHead(200, { 'Content-Type': 'application/json' });
63
+ res.end(JSON.stringify({ projects: registered }));
64
+ return;
65
+ }
66
+ if (url.pathname === '/api/projects' && req.method === 'POST') {
67
+ let body = '';
68
+ req.on('data', c => { body += c; });
69
+ req.on('end', () => {
70
+ const parsed = JSON.parse(body || '{}');
71
+ if (registered.some(p => p.path === parsed.path)) {
72
+ res.writeHead(409, { 'Content-Type': 'application/json' });
73
+ res.end(JSON.stringify({ error: { code: 'PROJECT_DUPLICATE', message: 'duplicate' } }));
74
+ return;
75
+ }
76
+ const project = { id: `sha256:${registered.length + 1}`, name: 'demo', path: parsed.path, store: parsed.store || 'global', auto: false, turns: 3 };
77
+ registered.push(project);
78
+ res.writeHead(201, { 'Content-Type': 'application/json' });
79
+ res.end(JSON.stringify({ project }));
80
+ });
81
+ return;
82
+ }
83
+ if (url.pathname === '/api/projects' && req.method === 'DELETE') {
84
+ const id = url.searchParams.get('id');
85
+ const index = registered.findIndex(p => p.id === id);
86
+ if (index === -1) {
87
+ res.writeHead(404, { 'Content-Type': 'application/json' });
88
+ res.end(JSON.stringify({ error: { code: 'PROJECT_NOT_FOUND', message: 'unknown' } }));
89
+ return;
90
+ }
91
+ registered.splice(index, 1);
92
+ res.writeHead(200, { 'Content-Type': 'application/json' });
93
+ res.end(JSON.stringify({ removed: true }));
94
+ return;
95
+ }
96
+ res.writeHead(404, { 'Content-Type': 'application/json' });
97
+ res.end(JSON.stringify({ error: { code: 'NOT_FOUND', message: req.url } }));
98
+ });
99
+ await new Promise(resolve => stub.listen(0, '127.0.0.1', resolve));
100
+ const stubUrl = `http://127.0.0.1:${stub.address().port}`;
101
+
102
+ const savedEnv = { ...process.env };
103
+ delete process.env.KB_MODULES_REGISTER;
104
+ delete process.env.AI_CODING_EVENT_BRIDGE_HOME;
105
+ delete process.env.KB_EVENTBRIDGE_COMMAND;
106
+
107
+ try {
108
+ const bridge = new ModuleBridge({
109
+ terminalUrl: DEAD_URL,
110
+ vectorHubUrl: DEAD_URL,
111
+ eventBridgeUrl: stubUrl,
112
+ engineUrl: DEAD_URL,
113
+ registryStore: { listIds: () => ['demo-proj'], readDisplaySnapshot: id => id === 'demo-proj' ? { displayName: 'demo' } : null },
114
+ projectStore: makeStoreStub(),
115
+ });
116
+
117
+ // ---- proxy mapping: /api/eventbridge/<rest> -> <console>/api/<rest> ----
118
+ assert.strictEqual(bridge._eventBridgeApi('/api/eventbridge/projects'), `${stubUrl}/api/projects`, 'api prefix mapping');
119
+ const pres = fakeRes();
120
+ await bridge.proxyEventBridge(fakeReq(), pres, '/api/eventbridge/echo', '?x=1');
121
+ assert.strictEqual(pres.statusCode, 200, 'proxy reaches the console');
122
+ assert.deepStrictEqual(JSON.parse(pres.body).query, { x: '1' }, 'proxy forwards query string');
123
+
124
+ // ---- registration: POST { path, store: <knowledge>/dev-conversations } ----
125
+ const wiringStore = bridge.projectStore;
126
+ const first = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
127
+ assert.strictEqual(first.eventBridge.ok, true, 'event-bridge leg registers');
128
+ assert.strictEqual(first.eventBridge.status, 'registered', 'first registration is not a reuse');
129
+ assert.ok(first.eventBridge.projectId, 'registration returns the console project id');
130
+ const postHits = hits.filter(h => h.startsWith('POST /api/projects'));
131
+ assert.strictEqual(postHits.length, 1, 'exactly one POST issued');
132
+ assert.ok(wiringStore.state.modules.eventBridge, 'wiring persisted');
133
+ assert.ok(wiringStore.state.modules.eventBridge.store.endsWith('dev-conversations'), 'store follows the recommended layout');
134
+
135
+ // ---- idempotent registration: same path is reused, no second POST ----
136
+ const second = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
137
+ assert.strictEqual(second.eventBridge.ok, true, 're-registration succeeds');
138
+ assert.strictEqual(second.eventBridge.reused, true, 're-registration reuses by path');
139
+ assert.strictEqual(hits.filter(h => h.startsWith('POST /api/projects')).length, 1, 'no duplicate POST on re-registration');
140
+
141
+ // ---- aggregated projection ----
142
+ const aggregate = await bridge.aggregatedProjects();
143
+ assert.strictEqual(aggregate.eventBridgeAvailable, true, 'event-bridge available flag');
144
+ const projected = aggregate.projects.find(p => p.projectId === 'demo-proj');
145
+ assert.strictEqual(projected.modules.eventBridge.registered, true, 'sidebar projection marks event-bridge registered');
146
+ assert.strictEqual(projected.modules.eventBridge.turns, 3, 'sidebar projection carries turn count');
147
+
148
+ // ---- health ----
149
+ const health = await bridge.modulesHealth();
150
+ assert.strictEqual(health.modules.eventBridge.up, true, 'health reports event-bridge up');
151
+ assert.strictEqual(health.modules.terminal.up, false, 'health keeps terminal leg honest');
152
+
153
+ // An unreachable writer must block every subsequent removal.
154
+ const beforeRemoval = hits.length;
155
+ await assert.rejects(bridge.removeProjectLinks({ projectId: 'demo-proj' }), { code: 'PROJECT_BUSY' });
156
+ assert.strictEqual(hits.length, beforeRemoval, 'offline engine cannot remove other module bindings');
157
+ bridge.engineUrl = stubUrl; // This stub returns 404: the writer is already absent.
158
+ // ---- removal: DELETE ?id=..., wiring cleared ----
159
+ const removal = await bridge.removeProjectLinks({ projectId: 'demo-proj' });
160
+ assert.strictEqual(removal.eventBridge.ok, true, 'removal succeeds');
161
+ assert.ok(hits.some(h => h.startsWith(`DELETE /api/projects?id=${encodeURIComponent(first.eventBridge.projectId)}`)), 'DELETE carries the console project id');
162
+ assert.strictEqual(wiringStore.state.modules.eventBridge.status, 'removed', 'wiring marked removed');
163
+
164
+ // ---- removal when the console no longer knows the project: 404 tolerated ----
165
+ const removalAgain = await bridge.removeProjectLinks({ projectId: 'demo-proj' });
166
+ assert.strictEqual(removalAgain.eventBridge.ok, true, '404 on removal counts as removed');
167
+
168
+ // ---- supervised spawn spec ----
169
+ const specs = bridge._supervisedSpecs();
170
+ const ebSpec = specs.find(s => s.name === 'event-bridge');
171
+ assert.ok(ebSpec, 'supervisor specs include event-bridge');
172
+ assert.ok(ebSpec.command.includes('serve'), 'spec serves the console');
173
+ // Windows 路径分隔符无关:命令里的绝对路径在 win32 上是反斜杠
174
+ assert.ok(ebSpec.command.replace(new RegExp("\\\\+", 'g'), '/').includes('@sanqianx/ai-coding-event-bridge-console'), 'spec spawns the packaged console runtime; actual=' + ebSpec.command);
175
+ assert.ok(ebSpec.url === stubUrl, 'spec binds the configured URL');
176
+
177
+ process.env.KB_EVENTBRIDGE_COMMAND = 'node C:/explicit/console-bin.js serve --port 9001';
178
+ const overridden = bridge._supervisedSpecs().find(s => s.name === 'event-bridge');
179
+ assert.ok(overridden.command.includes('C:/explicit/console-bin.js'), 'KB_EVENTBRIDGE_COMMAND override wins');
180
+
181
+ process.env.AI_CODING_EVENT_BRIDGE_HOME = 'D:/bridge-home';
182
+ delete process.env.KB_EVENTBRIDGE_COMMAND;
183
+ const withHome = bridge._supervisedSpecs().find(s => s.name === 'event-bridge');
184
+ assert.ok(withHome.command.includes('--home'), 'bridge home is passed through to the console');
185
+ assert.ok(withHome.command.includes('D:\\bridge-home') || withHome.command.includes('D:/bridge-home'), 'bridge home path is quoted into the command line');
186
+
187
+ // ---- KB_MODULES_REGISTER=0 keeps the fan-out off ----
188
+ process.env.KB_MODULES_REGISTER = '0';
189
+ const disabled = await bridge.registerProjectLinks({ projectId: 'demo-proj', name: 'demo', workspacePath: 'D:/work/demo', knowledgePath: 'D:/kb/demo' });
190
+ assert.strictEqual(disabled.eventBridge.status, 'disabled', 'registration honors KB_MODULES_REGISTER=0');
191
+ } finally {
192
+ process.env = savedEnv;
193
+ stub.close();
194
+ }
195
+
196
+ console.log('module-bridge-eventbridge-test PASS');
197
+ }
198
+
199
+ main().catch(error => {
200
+ console.error(error && error.stack ? error.stack : error);
201
+ process.exit(1);
202
+ });