@sanqianx/project-knowledge 5.0.30 → 5.0.31
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.
- package/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +21 -0
- package/_site/_test/appearance-settings-test.js +148 -0
- package/_site/_test/helpers/parallel-capture-safety-r5-cap-shell.js +606 -0
- package/_site/_test/helpers/parallel-capture-safety-r5-memory-shell.js +464 -0
- package/_site/_test/helpers/parallel-dock-acceptance-env.js +301 -0
- package/_site/_test/helpers/parallel-dock-acceptance-r2-env.js +378 -0
- package/_site/_test/helpers/parallel-dock-acceptance-r2-ui.js +314 -0
- package/_site/_test/helpers/parallel-dock-acceptance-ui.js +195 -0
- package/_site/_test/helpers/parallel-dock-isolation-r3-credential-preload.js +113 -0
- package/_site/_test/helpers/parallel-dock-isolation-r3-env.js +307 -0
- package/_site/_test/helpers/parallel-dock-isolation-r3-fixture.js +373 -0
- package/_site/_test/helpers/parallel-dock-isolation-r3-ui.js +314 -0
- package/_site/_test/helpers/parallel-engine-reconcile-r4-harness.js +340 -0
- package/_site/_test/helpers/parallel-engine-reconcile-r4-standin-analyzer.js +82 -0
- package/_site/_test/helpers/parallel-memory-capture-r3-cap-shell.js +605 -0
- package/_site/_test/helpers/parallel-memory-capture-r3-shell.js +176 -0
- package/_site/_test/helpers/parallel-memory-capture-r4-cap-shell.js +577 -0
- package/_site/_test/helpers/parallel-memory-injection-shell.js +723 -0
- package/_site/_test/helpers/parallel-memory-injection-wb-host.js +81 -0
- package/_site/_test/helpers/parallel-workbench-drafts-r5-fixture.js +295 -0
- package/_site/_test/helpers/parallel-workbench-drafts-r5-harness.js +573 -0
- package/_site/_test/helpers/parallel-workbench-drafts-r5-host.js +69 -0
- package/_site/_test/helpers/parallel-workbench-runtime-harness.js +587 -0
- package/_site/_test/helpers/qa-r45-harness.js +94 -0
- package/_site/_test/helpers/qa-r48-env.js +64 -0
- package/_site/_test/helpers/qa-r51-env.cjs +77 -0
- package/_site/_test/parallel-capture-safety-r5-cap006-probe.js +524 -0
- package/_site/_test/parallel-capture-safety-r5-cap008e-probe.js +577 -0
- package/_site/_test/parallel-capture-safety-r5-mem005-health-test.js +320 -0
- package/_site/_test/parallel-engine-reconcile-r4-eng007-probe.js +304 -0
- package/_site/_test/parallel-logging-acceptance-log003-probe.js +78 -0
- package/_site/_test/parallel-logging-acceptance-log004-probe.js +85 -0
- package/_site/_test/parallel-memory-capture-r3-cap001-probe.js +238 -0
- package/_site/_test/parallel-memory-capture-r3-cap006-probe.js +371 -0
- package/_site/_test/parallel-memory-capture-r3-cap008-probe.js +439 -0
- package/_site/_test/parallel-memory-capture-r3-mem005-persistence-test.js +271 -0
- package/_site/_test/parallel-memory-capture-r4-cap006-probe.js +519 -0
- package/_site/_test/parallel-memory-capture-r4-cap008e-probe.js +569 -0
- package/_site/_test/parallel-memory-capture-r4-mem005-health-test.js +314 -0
- package/_site/_test/parallel-memory-injection-origin-probe.js +412 -0
- package/_site/_test/parallel-memory-injection-probe.js +411 -0
- package/_site/_test/parallel-workbench-drafts-r4-probe.js +477 -0
- package/_site/_test/parallel-workbench-drafts-r5-chrome-env-bisect.js +89 -0
- package/_site/_test/parallel-workbench-drafts-r5-probe.js +593 -0
- package/_site/_test/parallel-workbench-runtime-minimax-probe.js +321 -0
- package/_site/_test/parallel-workbench-runtime-wb004-ordering.js +307 -0
- package/_site/_test/parallel-workbench-runtime-wb005-drafts.js +313 -0
- package/_site/_test/parallel-workbench-runtime-wb008-abort.js +321 -0
- package/_site/_test/qa-r45-offline-backfill-test.js +279 -0
- package/_site/_test/qa-r48-sys003-blank-secret-test.js +258 -0
- package/_site/_test/t07c-cln-001-002-007-coverage-test.js +135 -0
- package/_site/_test/t07d-git-rel-coverage-test.js +224 -0
- package/_site/_test/t07e-qta-coverage-test.js +195 -0
- package/_site/_test/t07f-mem-008-coverage-test.js +101 -0
- package/_site/lib/server-app.js +11 -2
- package/_site/lib/settings-store.js +43 -1
- package/module-runtime-manifest.json +6 -6
- package/package.json +1 -1
- package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.mcp.json +1 -1
- package/ui/index.html +18 -1
- package/ui/product.css +6 -2
- package/ui/product.js +86 -1
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// _site/_test/helpers/parallel-capture-safety-r5-memory-shell.js
|
|
3
|
+
//
|
|
4
|
+
// P-CAPTURE-SAFETY-R5 copy of the shared memory shell
|
|
5
|
+
// (helpers/parallel-memory-shell.js, read-only). Exactly one behavioral gap is
|
|
6
|
+
// closed on top of the frozen logic: the in-process engine is now created with
|
|
7
|
+
// an explicit vectorUrl pointed at a lane-local vector endpoint. The shared
|
|
8
|
+
// shell leaves vectorUrl unset, and the engine's built-in default is
|
|
9
|
+
// http://127.0.0.1:8787 — the LIVE desktop vector-hub on this machine. Through
|
|
10
|
+
// that default the R4 mem005 runs overwrote the real scope-brain-brain-default
|
|
11
|
+
// collection (see the R5 vectorhub-incident investigation); no case may be
|
|
12
|
+
// exempt from isolation just because it "didn't use" the variable. The
|
|
13
|
+
// lane-local endpoint is the accepted implementation from the frozen R4 cap
|
|
14
|
+
// shell, reused via the sibling R5 cap-shell copy. Everything else is
|
|
15
|
+
// byte-identical to the shared shell.
|
|
16
|
+
//
|
|
17
|
+
// Lane harness for the P-MEMORY-01 memory lane.
|
|
18
|
+
//
|
|
19
|
+
// Every case runs against a real, isolated product instance:
|
|
20
|
+
// * its own port, data dir, knowledge root and Bridge journal home (the
|
|
21
|
+
// shared spawn-server helper already isolates those);
|
|
22
|
+
// * the REAL vendored knowledge engine, in-process, with a declared
|
|
23
|
+
// stand-in analyzer (helpers/parallel-memory-analyzer.js) that only
|
|
24
|
+
// replaces the model step;
|
|
25
|
+
// * a pass-through proxy in front of the engine, so the product's own
|
|
26
|
+
// runtime token reaches the engine the way it does in production;
|
|
27
|
+
// * real HTTP module services for terminal / vector-hub / event-bridge;
|
|
28
|
+
// * a real Chrome (CDP) in its own profile.
|
|
29
|
+
//
|
|
30
|
+
// Nothing here reaches the developer's live 5757/5790/8787/8790 endpoints,
|
|
31
|
+
// the real HOME, the real knowledge directories or the real memories.
|
|
32
|
+
|
|
33
|
+
const assert = require('node:assert/strict');
|
|
34
|
+
const crypto = require('node:crypto');
|
|
35
|
+
const fs = require('node:fs');
|
|
36
|
+
const os = require('node:os');
|
|
37
|
+
const path = require('node:path');
|
|
38
|
+
const { spawnSync } = require('node:child_process');
|
|
39
|
+
|
|
40
|
+
const { spawnServer } = require('../helpers/spawn-server');
|
|
41
|
+
const { findChrome } = require('../helpers/find-chrome');
|
|
42
|
+
const { launchCdpBrowser, waitFor } = require('../helpers/cdp-browser');
|
|
43
|
+
const { attachNetworkCapture } = require('../helpers/cdp-network-capture');
|
|
44
|
+
const { createModuleFixture, createUpstreamProxy } = require('../helpers/module-fixture');
|
|
45
|
+
const freePort = require('../helpers/free-port');
|
|
46
|
+
const { createMemoryStandinAnalyzer } = require('./parallel-memory-analyzer');
|
|
47
|
+
const { createEventBridgeStandin } = require('./parallel-memory-event-bridge');
|
|
48
|
+
const { startLaneVectorHub } = require('./parallel-capture-safety-r5-cap-shell');
|
|
49
|
+
|
|
50
|
+
const ROOT = path.resolve(__dirname, '../../..');
|
|
51
|
+
const ENGINE_ENTRY = path.join(ROOT, '_modules', 'knowledge-engine', 'src', 'server.js');
|
|
52
|
+
const LIVE_PORTS = new Set([5757, 5790, 8787, 8790]);
|
|
53
|
+
|
|
54
|
+
function isInside(candidate, parent) {
|
|
55
|
+
const relative = path.relative(parent, candidate);
|
|
56
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The lane temp root. It must live outside the repository: the knowledge
|
|
61
|
+
* fixture refuses an in-repo temp root, and the lane rule is that every
|
|
62
|
+
* artefact of a test lives outside the checkout.
|
|
63
|
+
*/
|
|
64
|
+
function resolveTempRoot() {
|
|
65
|
+
const chosen = process.env.PK_TEST_TEMP_ROOT || process.env.TEMP || process.env.TMP || process.env.TMPDIR || os.tmpdir();
|
|
66
|
+
const root = path.resolve(String(chosen));
|
|
67
|
+
if (isInside(root, ROOT)) throw new Error(`refusing to use a temp root inside the repository: ${root}`);
|
|
68
|
+
return root;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function git(cwd, args) {
|
|
72
|
+
const result = spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', windowsHide: true, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } });
|
|
73
|
+
assert.equal(result.status, 0, `git ${args.join(' ')} failed: ${result.stderr}`);
|
|
74
|
+
return String(result.stdout || '').trim();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A real Git repository with real commits: the product imports it for real. */
|
|
78
|
+
function makeRepo(root, name, files = { 'README.md': `# ${name}\n` }) {
|
|
79
|
+
const repo = path.join(root, name);
|
|
80
|
+
fs.mkdirSync(repo, { recursive: true });
|
|
81
|
+
git(repo, ['init', '-q', '-b', 'main']);
|
|
82
|
+
git(repo, ['config', 'user.email', 'parallel-memory@local.invalid']);
|
|
83
|
+
git(repo, ['config', 'user.name', 'Parallel Memory Regression']);
|
|
84
|
+
git(repo, ['config', 'commit.gpgsign', 'false']);
|
|
85
|
+
for (const [file, content] of Object.entries(files)) fs.writeFileSync(path.join(repo, file), content, 'utf8');
|
|
86
|
+
git(repo, ['add', '.']);
|
|
87
|
+
git(repo, ['commit', '-qm', 'baseline']);
|
|
88
|
+
return { path: repo, headCommit: git(repo, ['rev-parse', 'HEAD']) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A real follow-up commit in an existing repository.
|
|
93
|
+
*
|
|
94
|
+
* The engine registers a project with `trackingStartCommit` set to the commit
|
|
95
|
+
* that was HEAD at import time, and it treats a run for that very commit as
|
|
96
|
+
* already analysed. A maintenance run therefore has to target a commit that
|
|
97
|
+
* was made after the import — exactly the production flow.
|
|
98
|
+
*/
|
|
99
|
+
function commitFile(repo, file, content, message = 'follow-up') {
|
|
100
|
+
const full = path.join(repo.path, file);
|
|
101
|
+
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
102
|
+
fs.writeFileSync(full, content, 'utf8');
|
|
103
|
+
git(repo.path, ['add', '.']);
|
|
104
|
+
git(repo.path, ['commit', '-qm', message]);
|
|
105
|
+
repo.headCommit = git(repo.path, ['rev-parse', 'HEAD']);
|
|
106
|
+
return repo.headCommit;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function startHarness({ tag, evidenceDir, viewport = { width: 1600, height: 900 } } = {}) {
|
|
110
|
+
const tempRoot = resolveTempRoot();
|
|
111
|
+
fs.mkdirSync(tempRoot, { recursive: true });
|
|
112
|
+
// The shared spawn-server helper and every other lane helper create their
|
|
113
|
+
// directories with os.tmpdir(); point that at the lane temp root so nothing
|
|
114
|
+
// is written inside the checkout.
|
|
115
|
+
process.env.TEMP = tempRoot;
|
|
116
|
+
process.env.TMP = tempRoot;
|
|
117
|
+
process.env.TMPDIR = tempRoot;
|
|
118
|
+
|
|
119
|
+
const fixture = fs.mkdtempSync(path.join(tempRoot, `pk-parallel-memory-${tag}-`));
|
|
120
|
+
const evidence = evidenceDir || path.join(fixture, 'evidence');
|
|
121
|
+
fs.mkdirSync(evidence, { recursive: true });
|
|
122
|
+
const dataDir = path.join(fixture, 'data');
|
|
123
|
+
const knowledgeRoot = path.join(fixture, 'knowledge');
|
|
124
|
+
fs.mkdirSync(knowledgeRoot, { recursive: true });
|
|
125
|
+
|
|
126
|
+
const plan = { document: '', memoryCandidates: [] };
|
|
127
|
+
const engineToken = crypto.randomBytes(24).toString('hex');
|
|
128
|
+
// The lane-local vector endpoint must exist BEFORE the engine: the engine's
|
|
129
|
+
// index/scope steps default to the LIVE hub at 127.0.0.1:8787 when no
|
|
130
|
+
// vectorUrl is passed (R5 incident finding; the R4 cap shell proved this
|
|
131
|
+
// endpoint contract).
|
|
132
|
+
const laneVector = startLaneVectorHub({ tag: `${tag}-lane-vector` });
|
|
133
|
+
await laneVector.start();
|
|
134
|
+
const { createEngineServer } = require(ENGINE_ENTRY);
|
|
135
|
+
const engine = createEngineServer({
|
|
136
|
+
dataDir: path.join(fixture, 'engine'),
|
|
137
|
+
runtimeToken: engineToken,
|
|
138
|
+
debug: true,
|
|
139
|
+
consume: false,
|
|
140
|
+
vectorUrl: laneVector.url,
|
|
141
|
+
analyzer: createMemoryStandinAnalyzer({ plan }),
|
|
142
|
+
});
|
|
143
|
+
await engine.ready;
|
|
144
|
+
await new Promise(resolve => engine.server.listen(0, '127.0.0.1', resolve));
|
|
145
|
+
const engineUrl = `http://127.0.0.1:${engine.server.address().port}`;
|
|
146
|
+
assert.equal(LIVE_PORTS.has(engine.server.address().port), false, 'the lane engine must never use a live port');
|
|
147
|
+
const engineProxy = createUpstreamProxy({ target: engineUrl, token: engineToken, tag: 'engine' });
|
|
148
|
+
await engineProxy.start();
|
|
149
|
+
|
|
150
|
+
const terminal = createModuleFixture({ kind: 'terminal', tag: `${tag}-terminal` });
|
|
151
|
+
const vectorHub = createModuleFixture({ kind: 'vectorHub', tag: `${tag}-vectorhub` });
|
|
152
|
+
// The shared event-bridge fixture answers with `bridge-1`, a journal id the
|
|
153
|
+
// engine rejects, so the project never reaches the engine. This lane uses
|
|
154
|
+
// its own stand-in that derives the real `sha256:` workspace id.
|
|
155
|
+
const eventBridge = await createEventBridgeStandin({ tag: `${tag}-eventbridge` });
|
|
156
|
+
await terminal.start();
|
|
157
|
+
await vectorHub.start();
|
|
158
|
+
await eventBridge.start();
|
|
159
|
+
|
|
160
|
+
const port = await freePort();
|
|
161
|
+
const base = `http://127.0.0.1:${port}`;
|
|
162
|
+
const server = spawnServer({
|
|
163
|
+
root: ROOT,
|
|
164
|
+
port,
|
|
165
|
+
dataDir,
|
|
166
|
+
tag: `parallel-memory-${tag}`,
|
|
167
|
+
analysisOwner: 'engine',
|
|
168
|
+
extraEnv: {
|
|
169
|
+
KB_MODULES_REGISTER: '1',
|
|
170
|
+
KB_MODULES_AUTOSTART: '0',
|
|
171
|
+
KB_TERMINAL_URL: terminal.url,
|
|
172
|
+
KB_VECTORHUB_URL: vectorHub.url,
|
|
173
|
+
KB_EVENTBRIDGE_URL: eventBridge.url,
|
|
174
|
+
KB_KNOWLEDGE_ENGINE_URL: engineProxy.url,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const productApi = async (method, route, body) => {
|
|
179
|
+
const response = await fetch(base + route, {
|
|
180
|
+
method,
|
|
181
|
+
headers: body === undefined ? undefined : { 'Content-Type': 'application/json' },
|
|
182
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
183
|
+
signal: AbortSignal.timeout(20000),
|
|
184
|
+
});
|
|
185
|
+
return { status: response.status, body: await response.json().catch(() => null) };
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const engineApi = async (method, route, body) => {
|
|
189
|
+
const response = await fetch(engineUrl + route, {
|
|
190
|
+
method,
|
|
191
|
+
headers: {
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
...(engineToken ? { Authorization: `Bearer ${engineToken}` } : {}),
|
|
194
|
+
},
|
|
195
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
196
|
+
signal: AbortSignal.timeout(20000),
|
|
197
|
+
});
|
|
198
|
+
return { status: response.status, body: await response.json().catch(() => null) };
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
await waitFor(async () => {
|
|
202
|
+
try { return (await productApi('GET', '/api/health')).status === 200; } catch { return false; }
|
|
203
|
+
}, 'the isolated product instance', 30000);
|
|
204
|
+
const settings = await productApi('PATCH', '/api/settings', { knowledge: { rootPath: knowledgeRoot } });
|
|
205
|
+
assert.equal(settings.status, 200, `the isolated instance accepts its own knowledge root: ${JSON.stringify(settings.body)}`);
|
|
206
|
+
|
|
207
|
+
let browser = null;
|
|
208
|
+
let network = null;
|
|
209
|
+
try {
|
|
210
|
+
browser = await launchCdpBrowser({
|
|
211
|
+
chrome: findChrome(),
|
|
212
|
+
profileDir: path.join(fixture, 'browser'),
|
|
213
|
+
url: `${base}/`,
|
|
214
|
+
width: viewport.width,
|
|
215
|
+
height: viewport.height,
|
|
216
|
+
});
|
|
217
|
+
await browser.setViewport(viewport.width, viewport.height);
|
|
218
|
+
await browser.send('Network.enable');
|
|
219
|
+
network = attachNetworkCapture(browser);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
await server.cleanup();
|
|
222
|
+
await engineProxy.close();
|
|
223
|
+
await terminal.close();
|
|
224
|
+
await vectorHub.close();
|
|
225
|
+
await eventBridge.close();
|
|
226
|
+
await laneVector.close();
|
|
227
|
+
await engine.close().catch(() => {});
|
|
228
|
+
fs.rmSync(fixture, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const run = expression => browser.evaluate(expression);
|
|
233
|
+
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
234
|
+
|
|
235
|
+
/** Ask the shell to read the project registry again. */
|
|
236
|
+
const requestRegistryRefresh = () => run("document.dispatchEvent(new Event('visibilitychange'))");
|
|
237
|
+
|
|
238
|
+
/** A real page reload, so "it survives a refresh" is proven by the browser. */
|
|
239
|
+
const reload = async () => {
|
|
240
|
+
await browser.send('Page.reload', {});
|
|
241
|
+
await waitFor(() => run("document.readyState === 'complete'"), 'the page reload', 20000);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const pressBackspace = async (count = 1) => {
|
|
245
|
+
for (let index = 0; index < count; index += 1) await pressKey('Backspace', { code: 'Backspace', keyCode: 8 });
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/** Set a select's value and fire the real change event the shell listens to. */
|
|
249
|
+
const selectOption = async (selector, value) => {
|
|
250
|
+
const changed = await run(`(() => {
|
|
251
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
252
|
+
if (!el) return { missing: true };
|
|
253
|
+
el.value = ${JSON.stringify(value)};
|
|
254
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
255
|
+
return { value: el.value };
|
|
256
|
+
})()`);
|
|
257
|
+
assert.ok(changed && !changed.missing, `the select ${selector} must exist`);
|
|
258
|
+
assert.equal(changed.value, value, `the select ${selector} must accept the value ${value}`);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/** A native pointer click: the hit test runs first and must resolve to the target. */
|
|
262
|
+
const nativeClick = async selector => {
|
|
263
|
+
const probe = await run(`(() => {
|
|
264
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
265
|
+
if (!el) return { missing: true };
|
|
266
|
+
const rect = el.getBoundingClientRect();
|
|
267
|
+
if (rect.width <= 0 || rect.height <= 0) return { invisible: true };
|
|
268
|
+
const x = rect.left + rect.width / 2;
|
|
269
|
+
const y = rect.top + rect.height / 2;
|
|
270
|
+
const hit = document.elementFromPoint(x, y);
|
|
271
|
+
return { x, y, hitIsTarget: Boolean(hit && (hit === el || el.contains(hit))), label: (el.textContent || '').trim().slice(0, 40) };
|
|
272
|
+
})()`);
|
|
273
|
+
assert.ok(probe && !probe.missing, `the control ${selector} must exist in the page`);
|
|
274
|
+
assert.ok(!probe.invisible, `the control ${selector} must be visible`);
|
|
275
|
+
assert.ok(probe.hitIsTarget, `the control ${selector} must be the hit target; something else is on top`);
|
|
276
|
+
const common = { x: probe.x, y: probe.y, button: 'left', clickCount: 1 };
|
|
277
|
+
await browser.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: probe.x, y: probe.y, button: 'none', clickCount: 0 });
|
|
278
|
+
await browser.send('Input.dispatchMouseEvent', { type: 'mousePressed', ...common });
|
|
279
|
+
await browser.send('Input.dispatchMouseEvent', { type: 'mouseReleased', ...common });
|
|
280
|
+
return probe;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const pressKey = async (key, { code, keyCode, text } = {}) => {
|
|
284
|
+
const base = { key, code: code || key, windowsVirtualKeyCode: keyCode, nativeVirtualKeyCode: keyCode };
|
|
285
|
+
await browser.send('Input.dispatchKeyEvent', { type: text ? 'keyDown' : 'rawKeyDown', ...base, text });
|
|
286
|
+
await browser.send('Input.dispatchKeyEvent', { type: 'keyUp', ...base });
|
|
287
|
+
};
|
|
288
|
+
const pressTab = () => pressKey('Tab', { code: 'Tab', keyCode: 9 });
|
|
289
|
+
const pressEnter = () => pressKey('Enter', { code: 'Enter', keyCode: 13, text: '\r' });
|
|
290
|
+
|
|
291
|
+
/** Type into a focused field with real key events. */
|
|
292
|
+
const typeText = async text => {
|
|
293
|
+
for (const character of String(text)) {
|
|
294
|
+
const upper = character.toUpperCase();
|
|
295
|
+
const isLower = character >= 'a' && character <= 'z';
|
|
296
|
+
const isUpper = character >= 'A' && character <= 'Z';
|
|
297
|
+
const isDigit = character >= '0' && character <= '9';
|
|
298
|
+
const code = isLower ? `Key${upper}` : isUpper ? `Key${character}` : isDigit ? `Digit${character}` : '';
|
|
299
|
+
const keyCode = isLower ? upper.charCodeAt(0) : isUpper || isDigit ? character.charCodeAt(0) : 0;
|
|
300
|
+
const base = { key: character, code, windowsVirtualKeyCode: keyCode, nativeVirtualKeyCode: keyCode };
|
|
301
|
+
await browser.send('Input.dispatchKeyEvent', { type: 'keyDown', ...base, text: character, unmodifiedText: character });
|
|
302
|
+
await browser.send('Input.dispatchKeyEvent', { type: 'keyUp', ...base });
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/** Import a real project through the real product API. */
|
|
307
|
+
const importProject = async (repo, displayName, { domainId } = {}) => {
|
|
308
|
+
const knowledgePath = path.join(knowledgeRoot, displayName);
|
|
309
|
+
fs.mkdirSync(knowledgePath, { recursive: true });
|
|
310
|
+
const result = await productApi('POST', '/api/projects/import', { localPath: repo.path, knowledgePath, displayName });
|
|
311
|
+
assert.equal(result.status, 201, `the project import must succeed: ${JSON.stringify(result.body)}`);
|
|
312
|
+
const projectId = result.body.projectId;
|
|
313
|
+
if (domainId) {
|
|
314
|
+
const patched = await productApi('PATCH', `/api/projects/${encodeURIComponent(projectId)}`, { domainId });
|
|
315
|
+
assert.equal(patched.status, 200, `the domain binding must succeed: ${JSON.stringify(patched.body)}`);
|
|
316
|
+
}
|
|
317
|
+
return { projectId, project: result.body.project, knowledgePath, modules: result.body.modules };
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** Add a real domain to the brain of the isolated instance. */
|
|
321
|
+
const addDomain = async name => {
|
|
322
|
+
const result = await productApi('POST', '/api/domains', { name });
|
|
323
|
+
assert.equal(result.status, 201, `the brain domain must be accepted: ${JSON.stringify(result.body)}`);
|
|
324
|
+
return result.body.domain;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/** The knowledge directory the engine uses for the brain scope. */
|
|
328
|
+
const brainScopePath = () => {
|
|
329
|
+
const scope = engine.store.state.sharedScopes.find(entry => entry.kind === 'brain');
|
|
330
|
+
assert.ok(scope, 'the engine must know the brain scope');
|
|
331
|
+
return scope.knowledgePath;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** The knowledge directory the engine uses for one domain scope. */
|
|
335
|
+
const domainScopePath = domainId => {
|
|
336
|
+
const scope = engine.store.state.sharedScopes.find(entry => entry.kind === 'domain' && entry.id === domainId);
|
|
337
|
+
assert.ok(scope, `the engine must know the domain scope ${domainId}`);
|
|
338
|
+
return scope.knowledgePath;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/** Every memory the product's own snapshot endpoint serves. */
|
|
342
|
+
const productMemories = async () => {
|
|
343
|
+
const snapshot = await productApi('GET', '/api/engine/snapshot?summary=1&limit=50');
|
|
344
|
+
assert.equal(snapshot.status, 200, `the product snapshot must be readable: ${JSON.stringify(snapshot.body)}`);
|
|
345
|
+
return snapshot.body.memories || [];
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** The memories the engine would inject into a task for one project. */
|
|
349
|
+
const injectedMemories = async projectId => {
|
|
350
|
+
const context = await engineApi('POST', '/api/context', { projectId, query: '长期记忆', task: 'memory-lane' });
|
|
351
|
+
assert.equal(context.status, 200, `the engine context must be readable: ${JSON.stringify(context.body)}`);
|
|
352
|
+
return context.body.memories || [];
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* A memory candidate that satisfies the engine's own provenance gate: it
|
|
357
|
+
* quotes the real human event, the quote contains the memory text, and it
|
|
358
|
+
* declares why the text is a durable, cross-project owner preference.
|
|
359
|
+
*/
|
|
360
|
+
const memoryCandidate = ({ text, event, scopeType = 'global' }) => ({
|
|
361
|
+
text,
|
|
362
|
+
quote: text,
|
|
363
|
+
scopeType,
|
|
364
|
+
sourceEventId: event.eventId,
|
|
365
|
+
sourceHash: event.contentHash,
|
|
366
|
+
eligibility: {
|
|
367
|
+
durable: true,
|
|
368
|
+
crossProject: true,
|
|
369
|
+
reason: '人工明确表达的长期偏好',
|
|
370
|
+
scopeReason: scopeType === 'global' ? '适用于所有领域与项目' : '适用于指定领域',
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Drive the real pipeline to a candidate: a real follow-up commit, a real
|
|
376
|
+
* conversation event bound to it, the stand-in analyzer's manifest and the
|
|
377
|
+
* engine's own promotion and capture.
|
|
378
|
+
*/
|
|
379
|
+
const produceCandidate = async ({ repo, projectId, text, scopeType = 'global', inputOrigin = 'human', file = 'src/candidate.js' }) => {
|
|
380
|
+
const head = commitFile(repo, file, `// ${text}\n`, 'candidate commit');
|
|
381
|
+
const event = await ingestUserEvent({
|
|
382
|
+
projectId,
|
|
383
|
+
commitSha: head,
|
|
384
|
+
content: `人工要求:${text}`,
|
|
385
|
+
inputOrigin,
|
|
386
|
+
});
|
|
387
|
+
plan.document = `# 冻结证据\n\n${text}\n`;
|
|
388
|
+
plan.memoryCandidates = [memoryCandidate({ text, event, scopeType })];
|
|
389
|
+
const run = await triggerRun(projectId, head);
|
|
390
|
+
return { run, event, commitSha: head, capture: run.memoryCapture };
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Append a real conversation event to the engine's own conversation store.
|
|
395
|
+
* The engine's own binder freezes it into the commit snapshot, and the
|
|
396
|
+
* engine's own eligibility gate reads its inputOrigin.
|
|
397
|
+
*/
|
|
398
|
+
const ingestUserEvent = async ({ projectId, commitSha, content, inputOrigin = 'human', turnId, eventId }) => {
|
|
399
|
+
const id = eventId || `event-${crypto.randomUUID()}`;
|
|
400
|
+
const turn = turnId || `turn-${crypto.randomUUID()}`;
|
|
401
|
+
const result = await engine.coordinator.conversations.appendEvent(projectId, {
|
|
402
|
+
eventId: id,
|
|
403
|
+
sequence: 1,
|
|
404
|
+
source: 'claude-code',
|
|
405
|
+
eventType: 'user_prompt',
|
|
406
|
+
role: 'user',
|
|
407
|
+
inputOrigin,
|
|
408
|
+
content,
|
|
409
|
+
sessionId: `session-${projectId}`,
|
|
410
|
+
turnId: turn,
|
|
411
|
+
capturedAt: new Date().toISOString(),
|
|
412
|
+
identityConfidence: 'exact',
|
|
413
|
+
explicitCommitSha: commitSha,
|
|
414
|
+
});
|
|
415
|
+
return { eventId: id, turnId: turn, contentHash: result.event.contentHash, inputOrigin: result.event.inputOrigin };
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/** Run the real maintenance pipeline for a commit and wait for the run. */
|
|
419
|
+
const triggerRun = async (projectId, commitSha, timeoutMs = 60000) => {
|
|
420
|
+
const started = await engineApi('POST', '/api/runs', { projectId, commitSha });
|
|
421
|
+
assert.equal(started.status, 200, `the run must be accepted: ${JSON.stringify(started.body)}`);
|
|
422
|
+
const runId = started.body.run.runId;
|
|
423
|
+
const deadline = Date.now() + timeoutMs;
|
|
424
|
+
let last = null;
|
|
425
|
+
while (Date.now() < deadline) {
|
|
426
|
+
const current = await engineApi('GET', `/api/runs/${encodeURIComponent(runId)}`);
|
|
427
|
+
last = current.body && current.body.run;
|
|
428
|
+
if (last && ['succeeded', 'failed'].includes(last.state)) {
|
|
429
|
+
if (last.state === 'failed') throw new Error(`the run failed: ${JSON.stringify(last.error)}`);
|
|
430
|
+
return last;
|
|
431
|
+
}
|
|
432
|
+
if (last && last.promotedAt && last.memoryCapture) return last;
|
|
433
|
+
await wait(150);
|
|
434
|
+
}
|
|
435
|
+
throw new Error(`the run did not finish in time: ${JSON.stringify(last)}`);
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const cleanup = async () => {
|
|
439
|
+
await browser?.close();
|
|
440
|
+
await server.cleanup();
|
|
441
|
+
await engineProxy.close();
|
|
442
|
+
await terminal.close();
|
|
443
|
+
await vectorHub.close();
|
|
444
|
+
await eventBridge.close();
|
|
445
|
+
await laneVector.close();
|
|
446
|
+
await engine.close().catch(() => {});
|
|
447
|
+
fs.rmSync(fixture, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
return {
|
|
451
|
+
fixture, evidence, dataDir, knowledgeRoot, tempRoot, plan,
|
|
452
|
+
base, server, productApi, engineApi, engine, engineUrl, engineProxy,
|
|
453
|
+
terminal, vectorHub, eventBridge, laneVector,
|
|
454
|
+
browser, run, wait, waitFor, network,
|
|
455
|
+
requestRegistryRefresh, reload, pressBackspace, selectOption,
|
|
456
|
+
nativeClick, pressKey, pressTab, pressEnter, typeText,
|
|
457
|
+
importProject, addDomain, ingestUserEvent, triggerRun,
|
|
458
|
+
brainScopePath, domainScopePath, productMemories, injectedMemories,
|
|
459
|
+
memoryCandidate, produceCandidate,
|
|
460
|
+
cleanup,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
module.exports = { startHarness, makeRepo, commitFile, ROOT };
|