@wenaixi/searchfuse 0.1.0 → 0.1.2
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/CHANGELOG.md +157 -1
- package/README.md +2 -10
- package/README.zh-CN.md +2 -10
- package/SECURITY.md +36 -2
- package/claude/.claude-plugin/plugin.json +1 -1
- package/claude/hooks/register.ts +90 -18
- package/dist/kernel.js +17 -6
- package/dist/main.js +64 -21
- package/dist/{search-BIW13Csk.js → search-DeT416xs.js} +123 -11
- package/dist/serve-ensure.js +1 -140
- package/dist/serveEnsure-BTj9IifL.js +632 -0
- package/dsh/index.js +137 -3
- package/omp/extension.ts +263 -29
- package/omp/host-types.d.ts +47 -0
- package/omp/tsconfig.json +13 -0
- package/package.json +3 -7
- package/skills/searchfuse/SKILL.md +4 -4
- package/skills/searchfuse/references/configure.md +4 -2
- package/skills/searchfuse/references/runtime.md +1 -1
- package/skills/searchfuse/scripts/run.ps1 +1 -1
- package/skills/searchfuse/scripts/run.sh +1 -1
- package/dist/serve-Bfqz-nCB.js +0 -279
- package/docs/commit.md +0 -77
- package/docs/dsh.md +0 -205
- package/docs/harness-setup.md +0 -78
- package/docs/parity-matrix.md +0 -61
- package/docs/searxng-setup.md +0 -117
- package/docs/security.md +0 -59
- package/docs/settings-redesign-decisions.md +0 -407
- package/docs/superpowers/plans/2026-11-24-serve-lifecycle.md +0 -880
- package/docs/superpowers/reports/2026-11-24-serve-lifecycle-ledger.md +0 -211
- package/docs/superpowers/specs/2026-11-20-searchfuse-dual-host-design.md +0 -172
- package/docs/superpowers/specs/2026-11-21-searchfuse-tri-host-design.md +0 -659
- package/docs/superpowers/specs/2026-11-21-tri-host-takeover-design.md +0 -363
- package/docs/superpowers/specs/2026-11-22-settings-card-refactor-design.md +0 -117
- package/docs/superpowers/specs/2026-11-24-serve-lifecycle-design.md +0 -155
- package/docs/testing.md +0 -92
- package/docs/troubleshooting.md +0 -258
|
@@ -1,880 +0,0 @@
|
|
|
1
|
-
# 本地服务生命周期 Implementation Plan
|
|
2
|
-
|
|
3
|
-
> 状态:**已执行完成**(2026-11-26 核实:`src/serveEnsure.ts`、`src/serveEnsure.test.ts`、
|
|
4
|
-
> `src/util/isAlive.ts` 均存在,Claude hook 的三级兜底在 `claude/hooks/register.ts` 的
|
|
5
|
-
> `wakeServe` 里)。下面的 checkbox 是执行时的工作清单,**没有回填勾选**;判断是否完成
|
|
6
|
-
> 请看代码与测试,不要看这些方框。
|
|
7
|
-
|
|
8
|
-
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
9
|
-
|
|
10
|
-
**Goal:** 让 Claude Code 侧在本地服务不可用时能自己拉起来,并把「服务在不在」这一个判据统一到单处。
|
|
11
|
-
|
|
12
|
-
**Architecture:** 抽一个 `ensureServeRunning()` 放在 `src/serveEnsure.ts`,复用现成的 `serve --ensure` 幂等语义。dsh 的两个入口**不能** import `src/`(编译边界,已实测),所以 `dsh/index.ts` 自足实现同构的一份,与既有的 `claudeHostStatus` / `ompHostStatus` 同一个模式。Claude 的 hook 沙箱没有 `process`,它走自己已有的 `$.process.run` 跑同一条命令。两份实现的**行为**必须逐条对齐,验收时两边都要验。
|
|
13
|
-
|
|
14
|
-
**Tech Stack:** TypeScript(strict)、Node 内置 `node:http` / `node:child_process` / `node:fs`、vitest 3.2.4
|
|
15
|
-
|
|
16
|
-
**Spec:** `docs/superpowers/specs/2026-11-24-serve-lifecycle-design.md`
|
|
17
|
-
|
|
18
|
-
## Global Constraints
|
|
19
|
-
|
|
20
|
-
- 两个 tsconfig 必须都保持 0 错误:`npx tsc --noEmit`(src 含测试)与 `npx tsc -p tsconfig.dsh.json --noEmit`(`dsh/index.ts`、`dsh/client.ts`)。
|
|
21
|
-
- 改了 `dsh/*.ts` 必须 `npx tsc -p tsconfig.dsh.json --outDir dsh` 重新编译,否则宿主读的是旧产物。
|
|
22
|
-
- 所有注释与文档用简体中文,不使用 emoji。
|
|
23
|
-
- 文档注释不写空洞排比、夸张宣传、没有信息量的总结。
|
|
24
|
-
- 不新增任何依赖。
|
|
25
|
-
- 探本地端口必须一次只探一个,给足超时,且看毫秒数——同批探针会把慢的误判成超时。
|
|
26
|
-
- 提交消息用 `python -I -c` 写入 `.git-commit-msg.tmp`(已在 `.gitignore` 里),再用 `git commit -F`。不要用 PowerShell here-string。
|
|
27
|
-
- 每完成一个任务立刻 commit。
|
|
28
|
-
|
|
29
|
-
---
|
|
30
|
-
|
|
31
|
-
### Task 1: `ensureServeRunning` 的判据与拉起
|
|
32
|
-
|
|
33
|
-
**Files:**
|
|
34
|
-
- Create: `src/serveEnsure.ts`
|
|
35
|
-
- Test: `src/serveEnsure.test.ts`
|
|
36
|
-
|
|
37
|
-
**Interfaces:**
|
|
38
|
-
- Consumes: `runtimePath()` 从 `src/serve.ts:51` 导入。
|
|
39
|
-
- Produces:
|
|
40
|
-
```ts
|
|
41
|
-
export interface EnsureOutcome {
|
|
42
|
-
started: boolean;
|
|
43
|
-
port?: number;
|
|
44
|
-
reason?: string;
|
|
45
|
-
}
|
|
46
|
-
export interface EnsureOptions {
|
|
47
|
-
/** 拉起子进程的 node 可执行文件;默认按 PATH 解析裸名 'node'。 */
|
|
48
|
-
nodeBinary?: string;
|
|
49
|
-
/** CLI 入口的绝对路径;默认从本文件位置推 ../dist/main.js。 */
|
|
50
|
-
cliPath?: string;
|
|
51
|
-
/** spawn 到退出码返回的上限,默认 20 秒。 */
|
|
52
|
-
timeoutMs?: number;
|
|
53
|
-
}
|
|
54
|
-
export function ensureServeRunning(options?: EnsureOptions): Promise<EnsureOutcome>;
|
|
55
|
-
export function readServeRuntimeFile(file?: string): { port: number; token: string } | undefined;
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
- [ ] **Step 1: 写失败的测试**
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
// src/serveEnsure.test.ts
|
|
62
|
-
import * as fs from 'node:fs';
|
|
63
|
-
import * as os from 'node:os';
|
|
64
|
-
import * as path from 'node:path';
|
|
65
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
66
|
-
import { ensureServeRunning, readServeRuntimeFile } from './serveEnsure.ts';
|
|
67
|
-
|
|
68
|
-
const created: string[] = [];
|
|
69
|
-
afterEach(() => {
|
|
70
|
-
for (const dir of created.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
/** 造一个临时 runtime.json,用完删掉。 */
|
|
74
|
-
function tempRuntime(contents: unknown): string {
|
|
75
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'searchfuse-ensure-'));
|
|
76
|
-
created.push(dir);
|
|
77
|
-
const file = path.join(dir, 'runtime.json');
|
|
78
|
-
fs.writeFileSync(file, JSON.stringify(contents), 'utf-8');
|
|
79
|
-
return file;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
describe('readServeRuntimeFile', () => {
|
|
83
|
-
it('读回端口与 token', () => {
|
|
84
|
-
const file = tempRuntime({ port: 61746, token: 'abc' });
|
|
85
|
-
expect(readServeRuntimeFile(file)).toEqual({ port: 61746, token: 'abc' });
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('文件不存在时返回 undefined,不抛异常', () => {
|
|
89
|
-
expect(readServeRuntimeFile('/nonexistent/runtime.json')).toBeUndefined();
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('形状不对时返回 undefined', () => {
|
|
93
|
-
const file = tempRuntime({ port: 'not a number', token: 'abc' });
|
|
94
|
-
expect(readServeRuntimeFile(file)).toBeUndefined();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('ensureServeRunning', () => {
|
|
99
|
-
it('服务已在跑时不动文件、不 spawn', async () => {
|
|
100
|
-
const file = tempRuntime({ port: 61746, token: 'abc' });
|
|
101
|
-
const before = fs.statSync(file).mtimeMs;
|
|
102
|
-
// 61746 上没有服务,所以这条走 spawn。用一个必然失败的 CLI 让 spawn 立刻返回,
|
|
103
|
-
// 从而只验「没有改写 runtime.json」这一件事。
|
|
104
|
-
const outcome = await ensureServeRunning({
|
|
105
|
-
cliPath: '/nonexistent/main.js',
|
|
106
|
-
timeoutMs: 500,
|
|
107
|
-
});
|
|
108
|
-
expect(fs.statSync(file).mtimeMs).toBe(before);
|
|
109
|
-
expect(outcome.started).toBe(false);
|
|
110
|
-
expect(typeof outcome.reason).toBe('string');
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
- [ ] **Step 2: 跑测试确认失败**
|
|
116
|
-
|
|
117
|
-
Run: `npx vitest run src/serveEnsure.test.ts`
|
|
118
|
-
Expected: FAIL —— `Failed to resolve import "./serveEnsure.ts"`
|
|
119
|
-
|
|
120
|
-
- [ ] **Step 3: 实现**
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
// src/serveEnsure.ts
|
|
124
|
-
import { spawn } from 'node:child_process';
|
|
125
|
-
import * as fs from 'node:fs';
|
|
126
|
-
import * as http from 'node:http';
|
|
127
|
-
import * as path from 'node:path';
|
|
128
|
-
import { fileURLToPath } from 'node:url';
|
|
129
|
-
import { runtimePath } from './serve.ts';
|
|
130
|
-
|
|
131
|
-
/** 拉起/检查的结果。`reason` 只在没起来时出现,给调用方一行可读的解释。 */
|
|
132
|
-
export interface EnsureOutcome {
|
|
133
|
-
started: boolean;
|
|
134
|
-
port?: number;
|
|
135
|
-
reason?: string;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export interface EnsureOptions {
|
|
139
|
-
/** 拉起子进程用的 node 可执行文件;默认按 PATH 解析裸名 'node'。 */
|
|
140
|
-
nodeBinary?: string;
|
|
141
|
-
/** CLI 入口绝对路径;默认从本文件位置推 ../dist/main.js。 */
|
|
142
|
-
cliPath?: string;
|
|
143
|
-
/** spawn 到退出码返回的上限,默认 20 秒。 */
|
|
144
|
-
timeoutMs?: number;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
148
|
-
/** 探 /healthz 的上限。服务是本机回环,超过就算不通。 */
|
|
149
|
-
const HEALTH_TIMEOUT_MS = 2_000;
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 读 `~/.searchfuse/runtime.json`。
|
|
153
|
-
*
|
|
154
|
-
* 文件可能被 `serve --ensure` 写入、被 `serve` 的 close() 删除,也可能
|
|
155
|
-
* 上一次进程被杀后残留成「指向死端口」。所以形状检查必须严格:
|
|
156
|
-
* 端口不是数字就直接当没有,宁可多拉起一次,也不要拿一个坏端口去连。
|
|
157
|
-
*/
|
|
158
|
-
export function readServeRuntimeFile(
|
|
159
|
-
file = runtimePath(),
|
|
160
|
-
): { port: number; token: string } | undefined {
|
|
161
|
-
try {
|
|
162
|
-
const raw = fs.readFileSync(file, 'utf-8');
|
|
163
|
-
const parsed = JSON.parse(raw) as { port?: unknown; token?: unknown };
|
|
164
|
-
if (typeof parsed.port !== 'number' || !Number.isInteger(parsed.port)) return undefined;
|
|
165
|
-
if (typeof parsed.token !== 'string' || parsed.token === '') return undefined;
|
|
166
|
-
return { port: parsed.port, token: parsed.token };
|
|
167
|
-
} catch {
|
|
168
|
-
return undefined;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** 探一次 /healthz。返回是否 200。 */
|
|
173
|
-
function healthOnce(port: number): Promise<boolean> {
|
|
174
|
-
return new Promise((resolve) => {
|
|
175
|
-
const req = http.request(
|
|
176
|
-
{
|
|
177
|
-
host: '127.0.0.1',
|
|
178
|
-
port,
|
|
179
|
-
path: '/healthz',
|
|
180
|
-
method: 'GET',
|
|
181
|
-
timeout: HEALTH_TIMEOUT_MS,
|
|
182
|
-
// dsh 与 omp 都走 keep-alive,探针必须主动断开,否则会等连接超时。
|
|
183
|
-
headers: { Connection: 'close' },
|
|
184
|
-
},
|
|
185
|
-
(res) => {
|
|
186
|
-
res.resume();
|
|
187
|
-
resolve(res.statusCode === 200);
|
|
188
|
-
},
|
|
189
|
-
);
|
|
190
|
-
req.on('error', () => resolve(false));
|
|
191
|
-
req.on('timeout', () => {
|
|
192
|
-
req.destroy();
|
|
193
|
-
resolve(false);
|
|
194
|
-
});
|
|
195
|
-
req.end();
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 确保本地服务在跑。已在跑就什么都不做。
|
|
201
|
-
*
|
|
202
|
-
* 顺序是「先探文件、再探 /healthz、最后才 spawn」:文件在但服务已死是最阴的
|
|
203
|
-
* 状态(服务不能用、文件却指向它),直接 spawn 会撞上仍被占用的端口。
|
|
204
|
-
*
|
|
205
|
-
* 拉起复用 `serve --ensure` 而不是自己起服务:它已经处理了「端口 0 由内核
|
|
206
|
-
* 分配」「已在跑就 exit 0」两个边界。在插件里重写一遍必然漂移。
|
|
207
|
-
*
|
|
208
|
-
* **不能等子进程退出码(2026-11-24 实测)。** `serve --ensure` 在服务不在跑时
|
|
209
|
-
* 会常驻不退出(`src/main.ts:326-333` 只写 stdout,没有 exit),所以等退出码
|
|
210
|
-
* 永远等不到;超时到点杀掉子进程,杀掉的正是刚起来的服务。改为 spawn 后
|
|
211
|
-
* 轮询 `/healthz`,子进程 detached 且不 kill。
|
|
212
|
-
*/
|
|
213
|
-
export async function ensureServeRunning(options: EnsureOptions = {}): Promise<EnsureOutcome> {
|
|
214
|
-
const file = runtimePath();
|
|
215
|
-
const existing = readServeRuntimeFile(file);
|
|
216
|
-
if (existing !== undefined && (await healthOnce(existing.port))) {
|
|
217
|
-
return { started: false, port: existing.port };
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const cliPath =
|
|
221
|
-
options.cliPath ?? path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'dist', 'main.js');
|
|
222
|
-
const nodeBinary = options.nodeBinary ?? 'node';
|
|
223
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
224
|
-
|
|
225
|
-
const spawned = await new Promise<boolean>((resolve) => {
|
|
226
|
-
let child;
|
|
227
|
-
try {
|
|
228
|
-
child = spawn(nodeBinary, [cliPath, 'serve', '--ensure'], {
|
|
229
|
-
stdio: 'ignore',
|
|
230
|
-
windowsHide: true,
|
|
231
|
-
// 它是常驻服务,不能跟着调用方的生命周期走。
|
|
232
|
-
detached: true,
|
|
233
|
-
});
|
|
234
|
-
} catch {
|
|
235
|
-
resolve(false);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
// 不 kill、不等 close:它本来就不该退出。只处理起不来(如 node 不在 PATH)。
|
|
239
|
-
child.on('error', () => resolve(false));
|
|
240
|
-
child.unref();
|
|
241
|
-
resolve(true);
|
|
242
|
-
});
|
|
243
|
-
if (!spawned) return { started: false, reason: 'could not start the serve process' };
|
|
244
|
-
|
|
245
|
-
// 轮询到通为止。500ms 一次,总上限与 spawn 超时同一个数。
|
|
246
|
-
const deadline = Date.now() + timeoutMs;
|
|
247
|
-
while (Date.now() < deadline) {
|
|
248
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
249
|
-
const probe = readServeRuntimeFile(file);
|
|
250
|
-
if (probe !== undefined && (await healthOnce(probe.port))) {
|
|
251
|
-
return { started: true, port: probe.port };
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return { started: false, reason: `serve did not become healthy within ${timeoutMs} ms` };
|
|
255
|
-
}
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
- [ ] **Step 4: 跑测试确认通过**
|
|
259
|
-
|
|
260
|
-
Run: `npx vitest run src/serveEnsure.test.ts`
|
|
261
|
-
Expected: PASS,4 条全绿
|
|
262
|
-
|
|
263
|
-
- [ ] **Step 5: 类型检查与提交**
|
|
264
|
-
|
|
265
|
-
```bash
|
|
266
|
-
npx tsc --noEmit
|
|
267
|
-
python -I -c "import io; io.open('.git-commit-msg.tmp','w',encoding='utf-8',newline='\n').write('新增 ensureServeRunning:本地服务的唯一判据\n\n复用 serve --ensure 的幂等语义,先探 runtime.json 与 /healthz,\n服务真的不在才 spawn。文件在但服务已死是最阴的状态,直接 spawn 会撞上\n仍被占用的端口,所以顺序不能倒过来。\n')"
|
|
268
|
-
git add src/serveEnsure.ts src/serveEnsure.test.ts .git-commit-msg.tmp
|
|
269
|
-
git commit -F .git-commit-msg.tmp
|
|
270
|
-
rm -f .git-commit-msg.tmp
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
---
|
|
274
|
-
|
|
275
|
-
### Task 2: dsh 侧接入——摘要、动作、启动拉起
|
|
276
|
-
|
|
277
|
-
**Files:**
|
|
278
|
-
- Modify: `dsh/index.ts`(`engineSummary` 加 `serve` 段;`registerConfigRoute` 加动作分支;`apply` 末尾非阻塞拉起)
|
|
279
|
-
- Test: `src/dshPlugin.test.ts`
|
|
280
|
-
|
|
281
|
-
**注意:** 前端的服务状态行与「拉起」按钮属于 **Task 3**,本任务**不碰** `dsh/client.ts`。
|
|
282
|
-
|
|
283
|
-
**Interfaces:**
|
|
284
|
-
- Consumes: Task 1 的行为语义,但**不 import 它的模块**——见下方关键约束。
|
|
285
|
-
- Produces:
|
|
286
|
-
- `engineSummary()` 新增字段 `serve: { running: boolean; port?: number }`
|
|
287
|
-
- `POST /searchfuse/config` 接受 `{ "action": "ensureServe" }`,返回 `{ ok: boolean; port?: number; message?: string }`
|
|
288
|
-
|
|
289
|
-
**关键约束(2026-11-24 实测):`dsh/*.ts` 不能 import `src/`。**
|
|
290
|
-
|
|
291
|
-
`tsconfig.dsh.json` 的 `files` 只有两个入口,且 `src/` 用的是 `.ts` 扩展名导入
|
|
292
|
-
(`import './config.ts'`)。一旦 `dsh/index.ts` 去 import `src/serveEnsure.ts`,
|
|
293
|
-
TypeScript 会顺着依赖树把整个 `src/` 拉进这次编译,于是满屏 `TS5097`
|
|
294
|
-
(`allowImportingTsExtensions` 未开启)。实测报错第一条是
|
|
295
|
-
`dsh/index.ts(29,29): error TS5097`,随后是 `src/aggregate/rrf.ts`、`src/config.ts` 等。
|
|
296
|
-
|
|
297
|
-
所以 dsh 侧**自足实现**判据,与既有的 `claudeHostStatus` / `ompHostStatus`
|
|
298
|
-
完全同一个模式:`dsh/index.ts` 已经有一整套「自己读文件、自己比对」的实现,
|
|
299
|
-
它们也没有 import `src/`。这不是重复,是编译边界决定的。
|
|
300
|
-
|
|
301
|
-
代价是判据有两份实现(`src/serveEnsure.ts` 一份、`dsh/index.ts` 一份),
|
|
302
|
-
所以两边的**行为必须逐条对齐**,Task 5 的验收里要同时验两边。
|
|
303
|
-
|
|
304
|
-
- [ ] **Step 1: 写失败的测试**
|
|
305
|
-
|
|
306
|
-
追加到 `src/dshPlugin.test.ts`:
|
|
307
|
-
|
|
308
|
-
```ts
|
|
309
|
-
describe('本地服务状态与手动拉起', () => {
|
|
310
|
-
it('摘要里带上服务在不在', async () => {
|
|
311
|
-
const summary = await callConfigRoute({ method: 'GET' });
|
|
312
|
-
expect(summary).toHaveProperty('serve');
|
|
313
|
-
expect(typeof summary.serve.running).toBe('boolean');
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it('POST action=ensureServe 返回端口', async () => {
|
|
317
|
-
const result = await callConfigRoute({
|
|
318
|
-
method: 'POST',
|
|
319
|
-
body: JSON.stringify({ action: 'ensureServe' }),
|
|
320
|
-
contentType: 'application/json',
|
|
321
|
-
});
|
|
322
|
-
expect(result.ok).toBe(true);
|
|
323
|
-
expect(typeof result.port).toBe('number');
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
**注意:** `callConfigRoute` 需要按 `CLAUDE.md` 里的最小夹具写,`res` 必须有 `writeHead` 与 `end`。若测试文件里已有同名辅助函数,直接复用,不要新建第二个。
|
|
329
|
-
|
|
330
|
-
- [ ] **Step 2: 跑测试确认失败**
|
|
331
|
-
|
|
332
|
-
Run: `npx vitest run src/dshPlugin.test.ts`
|
|
333
|
-
Expected: FAIL —— `expected undefined to be a boolean`(`serve` 段还不存在)
|
|
334
|
-
|
|
335
|
-
- [ ] **Step 3: 实现 `engineSummary` 的 `serve` 段**
|
|
336
|
-
|
|
337
|
-
在 `dsh/index.ts` 的 `engineSummary()` 返回对象里,紧邻 `hosts: hostStatuses(config)` 加:
|
|
338
|
-
|
|
339
|
-
```ts
|
|
340
|
-
serve: await serveStatus(),
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
新增函数,紧邻 `hostStatuses` 之后。**注意:它自己读文件、自己探端口,不 import `src/`。**
|
|
344
|
-
|
|
345
|
-
```ts
|
|
346
|
-
/** `~/.searchfuse/runtime.json` 里我们关心的两字段。 */
|
|
347
|
-
interface ServeRuntimeFile {
|
|
348
|
-
port: number;
|
|
349
|
-
token: string;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* 读 runtime.json。形状严格:端口不是整数就当没有——宁可多拉起一次,
|
|
354
|
-
* 也不要拿一个坏端口去连。
|
|
355
|
-
*/
|
|
356
|
-
function readServeRuntime(): ServeRuntimeFile | undefined {
|
|
357
|
-
try {
|
|
358
|
-
const raw = readFileSync(join(homedir(), '.searchfuse', 'runtime.json'), 'utf-8');
|
|
359
|
-
const parsed = JSON.parse(raw) as { port?: unknown; token?: unknown };
|
|
360
|
-
if (typeof parsed.port !== 'number' || !Number.isInteger(parsed.port)) return undefined;
|
|
361
|
-
if (typeof parsed.token !== 'string' || parsed.token === '') return undefined;
|
|
362
|
-
return { port: parsed.port, token: parsed.token };
|
|
363
|
-
} catch {
|
|
364
|
-
return undefined;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* 本地服务在不在。
|
|
370
|
-
*
|
|
371
|
-
* 判据与 `src/serveEnsure.ts` 的 `ensureServeRunning` 前两步完全相同:读
|
|
372
|
-
* runtime.json、探 /healthz。两份实现在编译边界上互不相通(dsh 的两个入口
|
|
373
|
-
* 不能 import src),所以行为必须逐条对齐,改一处时另一处要跟着改。
|
|
374
|
-
*
|
|
375
|
-
* 探不通时只报 `running: false`,不报原因:卡片要的是一个状态。
|
|
376
|
-
*/
|
|
377
|
-
async function serveStatus(): Promise<{ running: boolean; port?: number }> {
|
|
378
|
-
const existing = readServeRuntime();
|
|
379
|
-
if (existing === undefined) return { running: false };
|
|
380
|
-
try {
|
|
381
|
-
const res = await fetch(`http://127.0.0.1:${existing.port}/healthz`, {
|
|
382
|
-
signal: AbortSignal.timeout(2000),
|
|
383
|
-
});
|
|
384
|
-
return res.ok ? { running: true, port: existing.port } : { running: false };
|
|
385
|
-
} catch {
|
|
386
|
-
return { running: false };
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* 拉起本地服务:跑 `serve --ensure`。它已在跑就 exit 0,所以重复调用安全。
|
|
392
|
-
*
|
|
393
|
-
* CLI 路径从 `PACKAGE_ROOT` 推,不假设与源码树同构——插件在 pnpm 的快照里
|
|
394
|
-
* 运行时,`../dist/main.js` 指向的是快照内的副本,那正是我们要用的那份。
|
|
395
|
-
*
|
|
396
|
-
* **不能等子进程退出码(2026-11-24 实测)。** 服务不在跑时 `serve --ensure`
|
|
397
|
-
* 常驻不退出,等退出码永远等不到,超时杀掉反而是刚起来的服务。它与
|
|
398
|
-
* `src/serveEnsure.ts` 的 `ensureServeRunning` 是同一个模型,改一处必须改另一处。
|
|
399
|
-
*/
|
|
400
|
-
async function startServe(): Promise<{ started: boolean; port?: number; reason?: string }> {
|
|
401
|
-
const cliPath = join(PACKAGE_ROOT, 'dist', 'main.js');
|
|
402
|
-
const spawned = await new Promise<boolean>((resolve) => {
|
|
403
|
-
let child;
|
|
404
|
-
try {
|
|
405
|
-
child = spawnHidden('node', [cliPath, 'serve', '--ensure'], {
|
|
406
|
-
stdio: 'ignore',
|
|
407
|
-
detached: true,
|
|
408
|
-
});
|
|
409
|
-
} catch {
|
|
410
|
-
resolve(false);
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
// 不 kill、不等 close:它本来就不该退出。
|
|
414
|
-
child.on('error', () => resolve(false));
|
|
415
|
-
child.unref();
|
|
416
|
-
resolve(true);
|
|
417
|
-
});
|
|
418
|
-
if (!spawned) return { started: false, reason: 'could not start the serve process' };
|
|
419
|
-
|
|
420
|
-
const deadline = Date.now() + SERVE_ENSURE_TIMEOUT_MS;
|
|
421
|
-
while (Date.now() < deadline) {
|
|
422
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
423
|
-
const probe = readServeRuntime();
|
|
424
|
-
if (probe !== undefined && (await healthOnce(probe.port))) {
|
|
425
|
-
return { started: true, port: probe.port };
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return { started: false, reason: `serve did not become healthy within ${SERVE_ENSURE_TIMEOUT_MS} ms` };
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/** 探一次 /healthz。返回是否 200。与 `src/serveEnsure.ts` 的同名逻辑一致。 */
|
|
432
|
-
function healthOnce(port: number): Promise<boolean> {
|
|
433
|
-
return new Promise((resolve) => {
|
|
434
|
-
const req = httpRequest(
|
|
435
|
-
{
|
|
436
|
-
host: '127.0.0.1',
|
|
437
|
-
port,
|
|
438
|
-
path: '/healthz',
|
|
439
|
-
method: 'GET',
|
|
440
|
-
timeout: 2000,
|
|
441
|
-
// 探针必须主动断开,否则会等 keep-alive 超时。
|
|
442
|
-
headers: { Connection: 'close' },
|
|
443
|
-
},
|
|
444
|
-
(res) => {
|
|
445
|
-
res.resume();
|
|
446
|
-
resolve(res.statusCode === 200);
|
|
447
|
-
},
|
|
448
|
-
);
|
|
449
|
-
req.on('error', () => resolve(false));
|
|
450
|
-
req.on('timeout', () => {
|
|
451
|
-
req.destroy();
|
|
452
|
-
resolve(false);
|
|
453
|
-
});
|
|
454
|
-
req.end();
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
`httpRequest` 需要从 `node:http` 导入(`dsh/index.ts` 顶部若还没有 `import { request as httpRequest } from 'node:http'` 就加上)。
|
|
460
|
-
|
|
461
|
-
新增常量,紧邻已有的 `CLI_PATH` 声明:
|
|
462
|
-
|
|
463
|
-
```ts
|
|
464
|
-
/** 拉起服务的上限。与 omp 扩展侧的 WAKE_TIMEOUT_MS 保持一致。 */
|
|
465
|
-
const SERVE_ENSURE_TIMEOUT_MS = 20_000;
|
|
466
|
-
```
|
|
467
|
-
|
|
468
|
-
`spawnHidden` 已在文件顶部导入(`dsh/index.ts:28`),不需要新增导入。
|
|
469
|
-
|
|
470
|
-
- [ ] **Step 4: 实现路由动作**
|
|
471
|
-
|
|
472
|
-
在 `registerConfigRoute` 的 POST 分支里,`applyCardSettings` 调用之前插入:
|
|
473
|
-
|
|
474
|
-
```ts
|
|
475
|
-
const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) as Bag;
|
|
476
|
-
// 独立动作,不走 applyCardSettings:它是一次会 spawn 子进程的副作用,
|
|
477
|
-
// 而 applyCardSettings 的契约是「只写配置文件的那些段」。
|
|
478
|
-
if (body?.action === 'ensureServe') {
|
|
479
|
-
const outcome = await startServe();
|
|
480
|
-
send(200, {
|
|
481
|
-
ok: outcome.started || outcome.port !== undefined,
|
|
482
|
-
...(outcome.port === undefined ? {} : { port: outcome.port }),
|
|
483
|
-
...(outcome.reason === undefined ? {} : { message: outcome.reason }),
|
|
484
|
-
});
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
applyCardSettings(body);
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
**注意:** 原来的代码是 `applyCardSettings(JSON.parse(...))`,要把它改成先解析到 `body`、再传递。
|
|
491
|
-
|
|
492
|
-
- [ ] **Step 5: (已作废)不实现「apply 末尾自动拉起」**
|
|
493
|
-
|
|
494
|
-
**实施时推翻:** 原计划要在 `apply()` 末尾 `void startServe()`。实测它会把
|
|
495
|
-
`dsh web search provider` 与 `dsh CLI budgets` 两组共 5 条既有测试打下线——
|
|
496
|
-
那些用例断言「发到服务的请求恰好 1 个、首个 path 是 `/v1/search`」,而 `apply`
|
|
497
|
-
时的 healthz 探针把计数变成 2、把首个请求变成 `/healthz`。
|
|
498
|
-
|
|
499
|
-
而且它本来就违反三条:拖慢每次插件加载、dsh 自己走 CLI 根本不需要服务、
|
|
500
|
-
用户没在用服务时也白起一个常驻进程。
|
|
501
|
-
|
|
502
|
-
**所以 dsh 侧只在卡片的「拉起」动作(Step 4)里真的 spawn。** 不减少任何
|
|
503
|
-
实际能力:需要服务的另外两个宿主各有自己的 ensure。这一步没有代码要写。
|
|
504
|
-
|
|
505
|
-
- [ ] **Step 6: 跑测试确认通过**
|
|
506
|
-
|
|
507
|
-
Run: `npx vitest run src/dshPlugin.test.ts`
|
|
508
|
-
Expected: PASS,含新增 2 条
|
|
509
|
-
|
|
510
|
-
- [ ] **Step 7: 编译并提交**
|
|
511
|
-
|
|
512
|
-
```bash
|
|
513
|
-
npx tsc -p tsconfig.dsh.json --outDir dsh
|
|
514
|
-
npx tsc --noEmit
|
|
515
|
-
python -I -c "import io; io.open('.git-commit-msg.tmp','w',encoding='utf-8',newline='\n').write('dsh 侧接入服务生命周期:摘要报状态、路由可手动拉起、启动时非阻塞拉起\n\n摘要里的 serve 段与 ensureServeRunning 用同一套判据,避免两处得出两种答案。\n拉起是独立动作而不是配置补丁:它会 spawn 子进程,不属于「只写配置文件」。\n启动时的拉起不阻塞 apply,失败只记一行日志。\n')"
|
|
516
|
-
git add dsh/index.js dsh/index.ts src/dshPlugin.test.ts .git-commit-msg.tmp
|
|
517
|
-
git commit -F .git-commit-msg.tmp
|
|
518
|
-
rm -f .git-commit-msg.tmp
|
|
519
|
-
```
|
|
520
|
-
|
|
521
|
-
---
|
|
522
|
-
|
|
523
|
-
### Task 3: 卡片上的服务状态与拉起按钮
|
|
524
|
-
|
|
525
|
-
**Files:**
|
|
526
|
-
- Modify: `dsh/client.ts`
|
|
527
|
-
- Test: `src/dshClient.test.ts`
|
|
528
|
-
|
|
529
|
-
**Interfaces:**
|
|
530
|
-
- Consumes: Task 2 的 `summary.serve` 与 `POST {action:'ensureServe'}`
|
|
531
|
-
- Produces: `__card.serveLabel(summary)` 纯函数,返回 `'running' | 'stopped' | 'unknown'`
|
|
532
|
-
|
|
533
|
-
- [ ] **Step 1: 写失败的测试**
|
|
534
|
-
|
|
535
|
-
追加到 `src/dshClient.test.ts`:
|
|
536
|
-
|
|
537
|
-
```ts
|
|
538
|
-
describe('服务状态行', () => {
|
|
539
|
-
it('服务在跑时报 running', () => {
|
|
540
|
-
expect(card.serveLabel({ serve: { running: true, port: 61746 } })).toBe('running');
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
it('服务不在时报 stopped', () => {
|
|
544
|
-
expect(card.serveLabel({ serve: { running: false } })).toBe('stopped');
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
it('旧版摘要没有 serve 段时报 unknown,而不是假装不在', () => {
|
|
548
|
-
expect(card.serveLabel({})).toBe('unknown');
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
- [ ] **Step 2: 跑测试确认失败**
|
|
554
|
-
|
|
555
|
-
Run: `npx vitest run src/dshClient.test.ts`
|
|
556
|
-
Expected: FAIL —— `card.serveLabel is not a function`
|
|
557
|
-
|
|
558
|
-
- [ ] **Step 3: 实现纯函数**
|
|
559
|
-
|
|
560
|
-
在 `dsh/client.ts` 里,紧邻 `searxngModeOf` 之后:
|
|
561
|
-
|
|
562
|
-
```ts
|
|
563
|
-
/**
|
|
564
|
-
* 服务状态的三态。
|
|
565
|
-
*
|
|
566
|
-
* 旧版摘要没有 `serve` 段时返回 'unknown' 而不是 'stopped':把「不知道」
|
|
567
|
-
* 报成「没在跑」会导致卡片一直挂着拉起按钮,而那可能是一次误报。
|
|
568
|
-
*/
|
|
569
|
-
function serveLabel(summary: Bag): string {
|
|
570
|
-
var serve = summary?.serve;
|
|
571
|
-
if (serve === undefined || serve === null) return 'unknown';
|
|
572
|
-
return serve.running === true ? 'running' : 'stopped';
|
|
573
|
-
}
|
|
574
|
-
```
|
|
575
|
-
|
|
576
|
-
并加进 `exports.__card` 列表:
|
|
577
|
-
|
|
578
|
-
```ts
|
|
579
|
-
serveLabel: serveLabel,
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
- [ ] **Step 4: 跑测试确认通过**
|
|
583
|
-
|
|
584
|
-
Run: `npx vitest run src/dshClient.test.ts`
|
|
585
|
-
Expected: PASS
|
|
586
|
-
|
|
587
|
-
- [ ] **Step 5: 渲染状态行与按钮**
|
|
588
|
-
|
|
589
|
-
在卡片列表之前(与聚合/退避模式同一层)加一段。文案必须同时进 `TEXT.en` 与 `TEXT.zh`:
|
|
590
|
-
|
|
591
|
-
```ts
|
|
592
|
-
serveSection: 'Local service',
|
|
593
|
-
serveRunning: 'Running on port {port}',
|
|
594
|
-
serveStopped: 'Not running',
|
|
595
|
-
serveUnknown: 'Status unknown',
|
|
596
|
-
serveStart: 'Start it',
|
|
597
|
-
serveStartHint: 'Starts the shared search service. Other harnesses will use it too.',
|
|
598
|
-
```
|
|
599
|
-
|
|
600
|
-
```ts
|
|
601
|
-
serveSection: '本地服务',
|
|
602
|
-
serveRunning: '运行中,端口 {port}',
|
|
603
|
-
serveStopped: '未运行',
|
|
604
|
-
serveUnknown: '状态未知',
|
|
605
|
-
serveStart: '拉起',
|
|
606
|
-
serveStartHint: '拉起后,其他宿主的搜索也会走这个服务。',
|
|
607
|
-
```
|
|
608
|
-
|
|
609
|
-
渲染逻辑:`serveLabel(summary) !== 'running'` 时才显示按钮;点击 POST `{action:'ensureServe'}`。按钮点击后重新拉一次 summary(复用现有的 refresh 调用)。
|
|
610
|
-
|
|
611
|
-
**注意:** 卡片上不允许出现 `title` 属性(既有的样式门禁守着这一条)。提示文案用可见的说明行,不用 tooltip。
|
|
612
|
-
|
|
613
|
-
- [ ] **Step 6: 跑样式门禁与测试**
|
|
614
|
-
|
|
615
|
-
Run: `npx vitest run src/dshClient.test.ts`
|
|
616
|
-
Expected: PASS,含既有的 CSS token 门禁
|
|
617
|
-
|
|
618
|
-
- [ ] **Step 7: 编译并提交**
|
|
619
|
-
|
|
620
|
-
```bash
|
|
621
|
-
npx tsc -p tsconfig.dsh.json --outDir dsh
|
|
622
|
-
python -I -c "import io; io.open('.git-commit-msg.tmp','w',encoding='utf-8',newline='\n').write('卡片显示本地服务状态,并提供手动拉起\n\n三态而不是两态:旧版摘要没有 serve 段时报 unknown,把「不知道」当成\n「没在跑」会让卡片一直挂着按钮。文案中英各一套。\n')"
|
|
623
|
-
git add dsh/client.js dsh/client.ts src/dshClient.test.ts .git-commit-msg.tmp
|
|
624
|
-
git commit -F .git-commit-msg.tmp
|
|
625
|
-
rm -f .git-commit-msg.tmp
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
---
|
|
629
|
-
|
|
630
|
-
### Task 4: Claude hook 的第三级兜底
|
|
631
|
-
|
|
632
|
-
**Files:**
|
|
633
|
-
- Modify: `claude/hooks/register.ts`
|
|
634
|
-
- Test: `src/hosts.test.ts`(加一个纯函数的路径推导测试)
|
|
635
|
-
|
|
636
|
-
**Interfaces:**
|
|
637
|
-
- Consumes: `searchfuseHosts.claudeCode.kernelPath`(settings 里已有的绝对路径)
|
|
638
|
-
- Produces: `serveCliPathFromKernel(kernelPath)` 导出,返回同目录下的 `main.js`
|
|
639
|
-
|
|
640
|
-
**关键约束:** hook 沙箱里 `globalThis.process` 是 undefined,**不能** import `src/serveEnsure.ts`(它依赖 `node:child_process` 的模块级导入)。Claude 侧只能走 `$.process.run`。
|
|
641
|
-
|
|
642
|
-
- [ ] **Step 1: 写失败的测试**
|
|
643
|
-
|
|
644
|
-
追加到 `src/hosts.test.ts`:
|
|
645
|
-
|
|
646
|
-
```ts
|
|
647
|
-
describe('serveCliPathFromKernel', () => {
|
|
648
|
-
it('从 kernel.js 推出同目录的 main.js', () => {
|
|
649
|
-
expect(serveCliPathFromKernel('E:\\x\\dist\\kernel.js')).toBe('E:\\x\\dist\\main.js');
|
|
650
|
-
});
|
|
651
|
-
|
|
652
|
-
it('路径里没有 kernel.js 时返回 undefined,不乱猜', () => {
|
|
653
|
-
expect(serveCliPathFromKernel('E:\\x\\dist\\other.js')).toBeUndefined();
|
|
654
|
-
});
|
|
655
|
-
});
|
|
656
|
-
```
|
|
657
|
-
|
|
658
|
-
- [ ] **Step 2: 跑测试确认失败**
|
|
659
|
-
|
|
660
|
-
Run: `npx vitest run src/hosts.test.ts`
|
|
661
|
-
Expected: FAIL —— `serveCliPathFromKernel is not a function`
|
|
662
|
-
|
|
663
|
-
- [ ] **Step 3: 实现路径推导**
|
|
664
|
-
|
|
665
|
-
在 `src/hosts.ts` 里导出:
|
|
666
|
-
|
|
667
|
-
```ts
|
|
668
|
-
/**
|
|
669
|
-
* 从 kernel 的绝对路径推出同目录下的 CLI 入口。
|
|
670
|
-
*
|
|
671
|
-
* 为什么从 kernel 推而不是各写一份:`hosts sync` 只往 settings 里写 kernelPath
|
|
672
|
-
* 这一个绝对路径,两处各推一次就会漂移。两个产物永远在同一个 dist 目录。
|
|
673
|
-
*/
|
|
674
|
-
export function serveCliPathFromKernel(kernelPath: string): string | undefined {
|
|
675
|
-
if (!/kernel\.js$/.test(kernelPath)) return undefined;
|
|
676
|
-
return kernelPath.replace(/kernel\.js$/, 'main.js');
|
|
677
|
-
}
|
|
678
|
-
```
|
|
679
|
-
|
|
680
|
-
- [ ] **Step 4: 跑测试确认通过**
|
|
681
|
-
|
|
682
|
-
Run: `npx vitest run src/hosts.test.ts`
|
|
683
|
-
Expected: PASS
|
|
684
|
-
|
|
685
|
-
- [ ] **Step 5: 在 `claude/hooks/register.ts` 的 `fetchOrSpawn` 之后加第三级**
|
|
686
|
-
|
|
687
|
-
把 `fetchOrSpawn` 末尾改为:
|
|
688
|
-
|
|
689
|
-
```ts
|
|
690
|
-
// 路径二:子进程。kernel 的绝对路径同样来自 settings。
|
|
691
|
-
const kernelPath = await kernelPathFromSettings($, next);
|
|
692
|
-
if (kernelPath === undefined) return undefined;
|
|
693
|
-
try {
|
|
694
|
-
const run = await $.process.run({
|
|
695
|
-
argv: ['node', kernelPath, ...target.kernelArgs],
|
|
696
|
-
timeoutMs: SEARCH_TIMEOUT_MS,
|
|
697
|
-
signal: next.signal,
|
|
698
|
-
});
|
|
699
|
-
if (run.exitCode !== 0) return undefined;
|
|
700
|
-
return run.stdout;
|
|
701
|
-
} catch {
|
|
702
|
-
return undefined;
|
|
703
|
-
}
|
|
704
|
-
```
|
|
705
|
-
|
|
706
|
-
并在调用方(`searchOrRead`)里,收到 `undefined` 之后加:
|
|
707
|
-
|
|
708
|
-
```ts
|
|
709
|
-
/**
|
|
710
|
-
* 两条路都失败时,尝试拉起服务再走一次 HTTP。
|
|
711
|
-
*
|
|
712
|
-
* 为什么放在这里而不是 `fetchOrSpawn` 内部:这两条路失败意味着这次搜索本来
|
|
713
|
-
* 就要失败了,此刻多付一次 `serve --ensure` 的成本是可以接受的;如果放在
|
|
714
|
-
* 每条路径内部,每次 tool.call 都会多 spawn 一个进程探一次,代价完全不同。
|
|
715
|
-
*
|
|
716
|
-
* 沙箱里没有 `process`,所以用 `$.process.run` 跑裸名 'node',与已有的
|
|
717
|
-
* 子进程兜底完全同一套做法。
|
|
718
|
-
*/
|
|
719
|
-
async function wakeServe($: HostDollar, next: HostNext): Promise<boolean> {
|
|
720
|
-
const kernelPath = await kernelPathFromSettings($, next);
|
|
721
|
-
if (kernelPath === undefined) return false;
|
|
722
|
-
const cliPath = kernelPath.replace(/kernel\.js$/, 'main.js');
|
|
723
|
-
if (cliPath === kernelPath) return false;
|
|
724
|
-
try {
|
|
725
|
-
const run = await $.process.run({
|
|
726
|
-
argv: ['node', cliPath, 'serve', '--ensure'],
|
|
727
|
-
timeoutMs: WAKE_TIMEOUT_MS,
|
|
728
|
-
signal: next.signal,
|
|
729
|
-
});
|
|
730
|
-
return run.exitCode === 0;
|
|
731
|
-
} catch {
|
|
732
|
-
return false;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
```
|
|
736
|
-
|
|
737
|
-
新增常量,紧邻已有的 `SEARCH_TIMEOUT_MS`:
|
|
738
|
-
|
|
739
|
-
```ts
|
|
740
|
-
/** 唤醒服务的上限。与 omp 侧同名常量保持一致。 */
|
|
741
|
-
const WAKE_TIMEOUT_MS = 15_000;
|
|
742
|
-
```
|
|
743
|
-
|
|
744
|
-
在 `searchOrRead` 里接上:
|
|
745
|
-
|
|
746
|
-
```ts
|
|
747
|
-
const first = await fetchOrSpawn($, next, target);
|
|
748
|
-
if (first !== undefined) return first;
|
|
749
|
-
if (await wakeServe($, next)) {
|
|
750
|
-
// 服务刚起来,再走一次 HTTP;仍然失败就交回原生实现。
|
|
751
|
-
const retry = await callServe($, next, target.endpoint, target.params);
|
|
752
|
-
if (retry !== undefined) return retry;
|
|
753
|
-
}
|
|
754
|
-
return undefined;
|
|
755
|
-
```
|
|
756
|
-
|
|
757
|
-
**注意:** 若 `searchOrRead` 的实际函数名或结构与此不同,以源码为准,不要照抄这里的名字——先 `grep -n "fetchOrSpawn" claude/hooks/register.ts` 看真实调用点。
|
|
758
|
-
|
|
759
|
-
- [ ] **Step 6: 类型检查与提交**
|
|
760
|
-
|
|
761
|
-
```bash
|
|
762
|
-
npx tsc --noEmit
|
|
763
|
-
python -I -c "import io; io.open('.git-commit-msg.tmp','w',encoding='utf-8',newline='\n').write('Claude hook 加第三级兜底:两条路都失败时拉起服务再试一次\n\nhook 沙箱里没有 globalThis.process,所以不能复用 src/serveEnsure.ts,\n只能走 $.process.run 跑与 omp 侧同一条 serve --ensure。\n兜底放在两条路都失败之后,而不是每条路径内部——后者会让每次 tool.call\n都多 spawn 一个进程探一次。\n')"
|
|
764
|
-
git add claude/hooks/register.ts src/hosts.ts src/hosts.test.ts .git-commit-msg.tmp
|
|
765
|
-
git commit -F .git-commit-msg.tmp
|
|
766
|
-
rm -f .git-commit-msg.tmp
|
|
767
|
-
```
|
|
768
|
-
|
|
769
|
-
---
|
|
770
|
-
|
|
771
|
-
### Task 5: 端到端验收
|
|
772
|
-
|
|
773
|
-
**Files:**
|
|
774
|
-
- 无新增文件。这是验证任务。
|
|
775
|
-
|
|
776
|
-
- [ ] **Step 1: 停掉服务,删掉 runtime.json**
|
|
777
|
-
|
|
778
|
-
```bash
|
|
779
|
-
powershell -NoProfile -Command "Get-Process node -ErrorAction SilentlyContinue | Where-Object { \$_.CommandLine -like '*serve*' } | Stop-Process -Force"
|
|
780
|
-
rm -f ~/.searchfuse/runtime.json
|
|
781
|
-
```
|
|
782
|
-
|
|
783
|
-
- [ ] **Step 2: 验 S1——服务已跑时不改写文件**
|
|
784
|
-
|
|
785
|
-
```bash
|
|
786
|
-
node dist/main.js serve --daemon &
|
|
787
|
-
sleep 3
|
|
788
|
-
node -e "const fs=require('fs'),os=require('os'),path=require('path');
|
|
789
|
-
const f=path.join(os.homedir(),'.searchfuse','runtime.json');
|
|
790
|
-
const before=fs.statSync(f).mtimeMs;
|
|
791
|
-
import('./dist/serveEnsure.js').then(async m=>{
|
|
792
|
-
const r=await m.ensureServeRunning();
|
|
793
|
-
console.log('started=',r.started,'port=',r.port);
|
|
794
|
-
console.log('mtime unchanged:',fs.statSync(f).mtimeMs===before);
|
|
795
|
-
});"
|
|
796
|
-
```
|
|
797
|
-
|
|
798
|
-
Expected: `started= false`,`mtime unchanged: true`
|
|
799
|
-
|
|
800
|
-
- [ ] **Step 3: 验 S2、S3——服务死了也能拉起来**
|
|
801
|
-
|
|
802
|
-
```bash
|
|
803
|
-
rm -f ~/.searchfuse/runtime.json
|
|
804
|
-
node -e "import('./dist/serveEnsure.js').then(async m=>{
|
|
805
|
-
const r=await m.ensureServeRunning();
|
|
806
|
-
console.log('started=',r.started,'port=',r.port);
|
|
807
|
-
});"
|
|
808
|
-
node -e "const fs=require('fs'),os=require('os'),path=require('path');
|
|
809
|
-
const rt=JSON.parse(fs.readFileSync(path.join(os.homedir(),'.searchfuse','runtime.json'),'utf8'));
|
|
810
|
-
require('http').get({port:rt.port,path:'/healthz',headers:{Connection:'close'}},res=>{
|
|
811
|
-
console.log('healthz=',res.statusCode);res.resume();}).on('error',e=>console.log('ERR',e.code));"
|
|
812
|
-
```
|
|
813
|
-
|
|
814
|
-
Expected: `started= true`、`port= <数字>`、`healthz= 200`
|
|
815
|
-
|
|
816
|
-
**注意:** `dist/serveEnsure.js` 需要 `vite build` 才会出现(它是 `src/` 的产物)。先跑 `pnpm build`。
|
|
817
|
-
|
|
818
|
-
- [ ] **Step 4: 验 S5、S6——路由动作与跨站拒绝**
|
|
819
|
-
|
|
820
|
-
```bash
|
|
821
|
-
node -e "(async()=>{
|
|
822
|
-
const registered=[];
|
|
823
|
-
const scope={webServer:{register:(r)=>{registered.push(r);return()=>{}}},settings:{register:()=>{}},effect:(fn)=>fn()};
|
|
824
|
-
const ctx={effect:(fn)=>fn(),inject:(d,k)=>k(scope),tools:{register:()=>()=>{}},web:{registerSearchProvider:()=>()=>{}},on:()=>()=>{}};
|
|
825
|
-
const mod=await import('./dsh/index.js');
|
|
826
|
-
mod.apply(ctx,{searchProvider:false,readPage:false,engineTools:false});
|
|
827
|
-
const route=registered.find(r=>r.path==='/searchfuse/config');
|
|
828
|
-
function call(method,body,origin){
|
|
829
|
-
return new Promise(resolve=>{
|
|
830
|
-
const res={writeHead:(s)=>{res.status=s},end:(b)=>{res.body=b;resolve(res)}};
|
|
831
|
-
const req={method,url:'/searchfuse/config',headers:{host:'127.0.0.1',...(origin?{origin}:{})},[Symbol.asyncIterator]:async function*(){if(body)yield Buffer.from(JSON.stringify(body))}};
|
|
832
|
-
route.handler(req,res);
|
|
833
|
-
});
|
|
834
|
-
}
|
|
835
|
-
const ok=await call('POST',{action:'ensureServe'});
|
|
836
|
-
console.log('S5 status',ok.status,'body',ok.body.slice(0,120));
|
|
837
|
-
const evil=await call('POST',{action:'ensureServe'},'https://evil.example');
|
|
838
|
-
console.log('S6 status',evil.status,'(应为 403)');
|
|
839
|
-
})()"
|
|
840
|
-
```
|
|
841
|
-
|
|
842
|
-
Expected: S5 返回 200 且含 `"ok":true` 与 `"port"`;S6 返回 403
|
|
843
|
-
|
|
844
|
-
- [ ] **Step 5: 全量测试与两个 tsconfig**
|
|
845
|
-
|
|
846
|
-
```bash
|
|
847
|
-
npx tsc --noEmit && npx tsc -p tsconfig.dsh.json --noEmit
|
|
848
|
-
npx vitest run
|
|
849
|
-
```
|
|
850
|
-
|
|
851
|
-
Expected: 两个 tsconfig 都是 0 错误;全量测试的失败只允许是已知的 flaky(`main.test.ts` 的 spawn 用例、`mcp/protocol.test.ts`)
|
|
852
|
-
|
|
853
|
-
- [ ] **Step 6: 更新 CLAUDE.md 并提交**
|
|
854
|
-
|
|
855
|
-
把实测输出(端口、mtime 结论、S6 的 403)记进 `CLAUDE.md` 的「本地服务生命周期」一节,然后:
|
|
856
|
-
|
|
857
|
-
```bash
|
|
858
|
-
python -I -c "import io; io.open('.git-commit-msg.tmp','w',encoding='utf-8',newline='\n').write('端到端验收:本地服务的三条自愈路径\n\nS1 服务已跑时不改写 runtime.json、S2/S3 服务缺失时能拉起、\nS5 路由动作返回端口、S6 跨站仍被 403 拒绝。\n')"
|
|
859
|
-
git add -A ':!CLAUDE.md'
|
|
860
|
-
git commit -F .git-commit-msg.tmp
|
|
861
|
-
rm -f .git-commit-msg.tmp
|
|
862
|
-
```
|
|
863
|
-
|
|
864
|
-
---
|
|
865
|
-
|
|
866
|
-
## Self-Review
|
|
867
|
-
|
|
868
|
-
**Spec 覆盖:**
|
|
869
|
-
|
|
870
|
-
| spec 要求 | 落在哪个任务 |
|
|
871
|
-
| :-- | :-- |
|
|
872
|
-
| §3.1 `ensureServeRunning()` 判据 | Task 1(`src/` 侧)、Task 2 Step 3(`dsh/` 侧的等价实现) |
|
|
873
|
-
| §3.2 三个调用点(Claude / dsh / omp 不改) | Task 2(dsh)、Task 4(Claude) |
|
|
874
|
-
| §3.3 卡片动作 `{action:'ensureServe'}` 与 `serve` 摘要段 | Task 2(后端)、Task 3(前端) |
|
|
875
|
-
| §3.4 omp 假阳性(已完成) | 不在本计划范围,已由 `1b5cd79` 落地 |
|
|
876
|
-
| §四 S1–S7 | Task 5 覆盖 S1、S2、S3、S5、S6;S4(Claude 实跑)与 S7(插件加载不卡)**没有独立断言**——已知缺口,见下 |
|
|
877
|
-
|
|
878
|
-
**类型一致性:** `EnsureOutcome` / `EnsureOptions` / `readServeRuntimeFile`(Task 1 定义)与 `ServeRuntimeFile` / `readServeRuntime` / `serveStatus` / `startServe`(Task 2 定义)分属两棵编译树,同名语义、不同名字,这是刻意的:两处不可能互相 import。`serveCliPathFromKernel` 在 Task 4 定义并测试。
|
|
879
|
-
|
|
880
|
-
**已知缺口(执行时要盯着):** S4 与 S7 没有独立的失败断言。补法是在 Task 5 追加两条:一条用 `claude -p` 实跑搜索(服务删掉的情况下应仍返回结果),一条用不存在的 `cliPath` 加载插件并计时。
|