@yourstandby/fabric 0.0.0-stage → 0.1.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.
- package/README.md +24 -2
- package/dist/daemon.js +3797 -0
- package/dist/index.js +3916 -0
- package/package.json +30 -4
- package/src/artifacts.test.ts +148 -0
- package/src/artifacts.ts +247 -0
- package/src/daemon-runner.ts +218 -0
- package/src/daemon.ts +524 -0
- package/src/discovery.ts +332 -0
- package/src/dispatch.test.ts +154 -0
- package/src/dispatch.ts +459 -0
- package/src/dispatcher.test.ts +321 -0
- package/src/dispatcher.ts +794 -0
- package/src/fabric-apply.test.ts +115 -0
- package/src/fabric-apply.ts +137 -0
- package/src/fabric-avatar.test.ts +51 -0
- package/src/fabric-discovery.test.ts +100 -0
- package/src/fabric-history.test.ts +68 -0
- package/src/fabric-keepalive.test.ts +112 -0
- package/src/fabric-notify.test.ts +269 -0
- package/src/fabric-pairing.test.ts +164 -0
- package/src/fabric-runner.test.ts +113 -0
- package/src/fabric-runner.ts +167 -0
- package/src/fabric-score.test.ts +74 -0
- package/src/fabric-server.ts +101 -0
- package/src/fabric-service.test.ts +279 -0
- package/src/fabric-service.ts +745 -0
- package/src/fabric-view.test.ts +118 -0
- package/src/fabric-view.ts +182 -0
- package/src/fabric-worktree.test.ts +139 -0
- package/src/fabric-worktree.ts +232 -0
- package/src/history.ts +127 -0
- package/src/host-load.ts +18 -0
- package/src/index.ts +27 -0
- package/src/pairing.test.ts +212 -0
- package/src/pairing.ts +342 -0
- package/src/peer-store.test.ts +212 -0
- package/src/peer-store.ts +243 -0
- package/src/transport.test.ts +247 -0
- package/src/transport.ts +709 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { FabricService } from "./fabric-service.ts";
|
|
7
|
+
|
|
8
|
+
function tempDir(): { dir: string; cleanup: () => void } {
|
|
9
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-notify-"));
|
|
10
|
+
return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test("B 干完主动推 task_done:A 的台账不用等对账就变 done", async () => {
|
|
14
|
+
const a = tempDir();
|
|
15
|
+
const b = tempDir();
|
|
16
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `nt-a-${process.pid}` });
|
|
17
|
+
const svcB = new FabricService({
|
|
18
|
+
agentDir: b.dir,
|
|
19
|
+
host: "127.0.0.1",
|
|
20
|
+
port: 0,
|
|
21
|
+
ctx: `nt-b-${process.pid}`,
|
|
22
|
+
labels: () => ["build"],
|
|
23
|
+
runner: {
|
|
24
|
+
async run() {
|
|
25
|
+
return { summary: "远程干完了", files: ["src/x.ts"], exitCode: 0, result: Buffer.from("bytes") };
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
try {
|
|
30
|
+
await svcA.start();
|
|
31
|
+
await svcB.start();
|
|
32
|
+
for (const [from, to, name] of [
|
|
33
|
+
[svcA, svcB, "机-B"],
|
|
34
|
+
[svcB, svcA, "机-A"],
|
|
35
|
+
] as const) {
|
|
36
|
+
const paired = from.pair({
|
|
37
|
+
nodeId: to.nodeId,
|
|
38
|
+
publicKey: to.identity.publicKey,
|
|
39
|
+
name,
|
|
40
|
+
address: "127.0.0.1",
|
|
41
|
+
port: to.port,
|
|
42
|
+
grants: "workspace-rw+bash",
|
|
43
|
+
});
|
|
44
|
+
assert.equal(paired.ok, true);
|
|
45
|
+
}
|
|
46
|
+
await waitFor(() => svcA.connectionStates()[svcB.nodeId] === "online", 5_000);
|
|
47
|
+
await waitFor(() => svcB.connectionStates()[svcA.nodeId] === "online", 5_000);
|
|
48
|
+
|
|
49
|
+
const dispatched = await svcA.dispatch({
|
|
50
|
+
prompt: "跑构建",
|
|
51
|
+
labels: ["build"],
|
|
52
|
+
estimatedMs: 300_000,
|
|
53
|
+
deadlineClass: "build",
|
|
54
|
+
});
|
|
55
|
+
assert.equal(dispatched.ok, true);
|
|
56
|
+
if (!dispatched.ok) return;
|
|
57
|
+
|
|
58
|
+
// 关键:不调 reconcile,等推送把状态推成 done
|
|
59
|
+
await waitFor(() => svcA.outbound.get(dispatched.taskId)?.state === "done", 5_000);
|
|
60
|
+
const record = svcA.outbound.get(dispatched.taskId);
|
|
61
|
+
assert.equal(record?.summary, "远程干完了");
|
|
62
|
+
assert.deepEqual(record?.files, ["src/x.ts"]);
|
|
63
|
+
assert.equal(record?.finishedAt !== null, true);
|
|
64
|
+
} finally {
|
|
65
|
+
await svcA.stop();
|
|
66
|
+
await svcB.stop();
|
|
67
|
+
a.cleanup();
|
|
68
|
+
b.cleanup();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("B 失败也推 task_failed", async () => {
|
|
73
|
+
const a = tempDir();
|
|
74
|
+
const b = tempDir();
|
|
75
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `nf-a-${process.pid}` });
|
|
76
|
+
const svcB = new FabricService({
|
|
77
|
+
agentDir: b.dir,
|
|
78
|
+
host: "127.0.0.1",
|
|
79
|
+
port: 0,
|
|
80
|
+
ctx: `nf-b-${process.pid}`,
|
|
81
|
+
runner: {
|
|
82
|
+
async run() {
|
|
83
|
+
throw new Error("model overloaded");
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
await svcA.start();
|
|
89
|
+
await svcB.start();
|
|
90
|
+
for (const [from, to, name] of [
|
|
91
|
+
[svcA, svcB, "机-B"],
|
|
92
|
+
[svcB, svcA, "机-A"],
|
|
93
|
+
] as const) {
|
|
94
|
+
from.pair({
|
|
95
|
+
nodeId: to.nodeId,
|
|
96
|
+
publicKey: to.identity.publicKey,
|
|
97
|
+
name,
|
|
98
|
+
address: "127.0.0.1",
|
|
99
|
+
port: to.port,
|
|
100
|
+
grants: "workspace-rw+bash",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
await waitFor(() => svcA.connectionStates()[svcB.nodeId] === "online", 5_000);
|
|
104
|
+
await waitFor(() => svcB.connectionStates()[svcA.nodeId] === "online", 5_000);
|
|
105
|
+
|
|
106
|
+
const dispatched = await svcA.dispatch({
|
|
107
|
+
prompt: "干一件会失败的事",
|
|
108
|
+
estimatedMs: 300_000,
|
|
109
|
+
deadlineClass: "build",
|
|
110
|
+
});
|
|
111
|
+
assert.equal(dispatched.ok, true);
|
|
112
|
+
if (!dispatched.ok) return;
|
|
113
|
+
await waitFor(() => svcA.outbound.get(dispatched.taskId)?.state === "failed", 5_000);
|
|
114
|
+
assert.match(svcA.outbound.get(dispatched.taskId)?.error ?? "", /model overloaded/);
|
|
115
|
+
} finally {
|
|
116
|
+
await svcA.stop();
|
|
117
|
+
await svcB.stop();
|
|
118
|
+
a.cleanup();
|
|
119
|
+
b.cleanup();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
async function waitFor(predicate: () => boolean, timeoutMs: number): Promise<void> {
|
|
124
|
+
const startedAt = Date.now();
|
|
125
|
+
while (!predicate()) {
|
|
126
|
+
if (Date.now() - startedAt > timeoutMs) throw new Error("condition not met in time");
|
|
127
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
test("进度:runner 上报的真实里程碑按 2 秒节流,并落进两端台账", async () => {
|
|
132
|
+
const a = tempDir();
|
|
133
|
+
const b = tempDir();
|
|
134
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pg-a-${process.pid}` });
|
|
135
|
+
const svcB = new FabricService({
|
|
136
|
+
agentDir: b.dir,
|
|
137
|
+
host: "127.0.0.1",
|
|
138
|
+
port: 0,
|
|
139
|
+
ctx: `pg-b-${process.pid}`,
|
|
140
|
+
runner: {
|
|
141
|
+
async run(input) {
|
|
142
|
+
// 连续 5 次上报:只有第一条能过(2 秒窗口内其余全被节流)
|
|
143
|
+
for (let index = 0; index < 5; index += 1) {
|
|
144
|
+
input.onProgress({ code: "workspace_ready", text: `阶段 ${index}` });
|
|
145
|
+
}
|
|
146
|
+
return { summary: "done", files: [], exitCode: 0 };
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
try {
|
|
151
|
+
await svcA.start();
|
|
152
|
+
await svcB.start();
|
|
153
|
+
for (const [from, to, name] of [
|
|
154
|
+
[svcA, svcB, "机-B"],
|
|
155
|
+
[svcB, svcA, "机-A"],
|
|
156
|
+
] as const) {
|
|
157
|
+
from.pair({
|
|
158
|
+
nodeId: to.nodeId,
|
|
159
|
+
publicKey: to.identity.publicKey,
|
|
160
|
+
name,
|
|
161
|
+
address: "127.0.0.1",
|
|
162
|
+
port: to.port,
|
|
163
|
+
grants: "workspace-rw+bash",
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
await waitFor(() => svcA.connectionStates()[svcB.nodeId] === "online", 5_000);
|
|
167
|
+
await waitFor(() => svcB.connectionStates()[svcA.nodeId] === "online", 5_000);
|
|
168
|
+
|
|
169
|
+
const dispatched = await svcA.dispatch({
|
|
170
|
+
prompt: "跑一个会报进度的活",
|
|
171
|
+
estimatedMs: 300_000,
|
|
172
|
+
deadlineClass: "build",
|
|
173
|
+
});
|
|
174
|
+
assert.equal(dispatched.ok, true);
|
|
175
|
+
if (!dispatched.ok) return;
|
|
176
|
+
await waitFor(() => svcA.outbound.get(dispatched.taskId)?.state === "done", 5_000);
|
|
177
|
+
const record = svcA.outbound.get(dispatched.taskId);
|
|
178
|
+
// 只有第一条进度过了节流;终态后保留最后一次,不会被清空
|
|
179
|
+
assert.equal(record?.progress, "阶段 0");
|
|
180
|
+
assert.ok((record?.progressAt ?? 0) > 0);
|
|
181
|
+
} finally {
|
|
182
|
+
await svcA.stop();
|
|
183
|
+
await svcB.stop();
|
|
184
|
+
a.cleanup();
|
|
185
|
+
b.cleanup();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("进程内 runner 的里程碑按顺序推进到「正在收集改动与结论」", async () => {
|
|
190
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-progress-"));
|
|
191
|
+
try {
|
|
192
|
+
const { createSubagentRunner } = await import("./fabric-runner.ts");
|
|
193
|
+
const seen: string[] = [];
|
|
194
|
+
const runner = createSubagentRunner(dir, {
|
|
195
|
+
prepareWorkspace: async () => ({ cwd: dir }),
|
|
196
|
+
spawn: async () => ({ ok: true, text: "完成" }),
|
|
197
|
+
});
|
|
198
|
+
await runner.run({
|
|
199
|
+
taskId: "aaaaaaaaaaaaaaaa",
|
|
200
|
+
prompt: "干活",
|
|
201
|
+
workspaceHint: "",
|
|
202
|
+
grant: "workspace-rw",
|
|
203
|
+
tools: [],
|
|
204
|
+
input: null,
|
|
205
|
+
signal: new AbortController().signal,
|
|
206
|
+
onProgress: (note) => seen.push(note.text),
|
|
207
|
+
});
|
|
208
|
+
assert.deepEqual(seen, ["已在隔离工作区就绪", "子会话开始执行", "正在收集改动与结论"]);
|
|
209
|
+
} finally {
|
|
210
|
+
rmSync(dir, { recursive: true, force: true });
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
test("进度带结构化 code/params:接收方按自己语言渲染,兜底文案仍在", async () => {
|
|
216
|
+
const a = tempDir();
|
|
217
|
+
const b = tempDir();
|
|
218
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pc-a-${process.pid}` });
|
|
219
|
+
const svcB = new FabricService({
|
|
220
|
+
agentDir: b.dir,
|
|
221
|
+
host: "127.0.0.1",
|
|
222
|
+
port: 0,
|
|
223
|
+
ctx: `pc-b-${process.pid}`,
|
|
224
|
+
runner: {
|
|
225
|
+
async run(input) {
|
|
226
|
+
input.onProgress({ code: "tool_running", params: { tool: "bash", detail: "npm test" }, text: "正在 bash:npm test" });
|
|
227
|
+
return { summary: "done", files: [], exitCode: 0 };
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
try {
|
|
232
|
+
await svcA.start();
|
|
233
|
+
await svcB.start();
|
|
234
|
+
for (const [from, to, name] of [
|
|
235
|
+
[svcA, svcB, "机-B"],
|
|
236
|
+
[svcB, svcA, "机-A"],
|
|
237
|
+
] as const) {
|
|
238
|
+
from.pair({
|
|
239
|
+
nodeId: to.nodeId,
|
|
240
|
+
publicKey: to.identity.publicKey,
|
|
241
|
+
name,
|
|
242
|
+
address: "127.0.0.1",
|
|
243
|
+
port: to.port,
|
|
244
|
+
grants: "workspace-rw+bash",
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
await waitFor(() => svcA.connectionStates()[svcB.nodeId] === "online", 5_000);
|
|
248
|
+
await waitFor(() => svcB.connectionStates()[svcA.nodeId] === "online", 5_000);
|
|
249
|
+
|
|
250
|
+
const dispatched = await svcA.dispatch({
|
|
251
|
+
prompt: "跑测试",
|
|
252
|
+
estimatedMs: 300_000,
|
|
253
|
+
deadlineClass: "build",
|
|
254
|
+
});
|
|
255
|
+
assert.equal(dispatched.ok, true);
|
|
256
|
+
if (!dispatched.ok) return;
|
|
257
|
+
await waitFor(() => svcA.outbound.get(dispatched.taskId)?.state === "done", 5_000);
|
|
258
|
+
const record = svcA.outbound.get(dispatched.taskId);
|
|
259
|
+
assert.equal(record?.progressCode, "tool_running");
|
|
260
|
+
assert.deepEqual(record?.progressParams, { tool: "bash", detail: "npm test" });
|
|
261
|
+
assert.equal(record?.progress, "正在 bash:npm test");
|
|
262
|
+
} finally {
|
|
263
|
+
await svcA.stop();
|
|
264
|
+
await svcB.stop();
|
|
265
|
+
a.cleanup();
|
|
266
|
+
b.cleanup();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { FabricService } from "./fabric-service.ts";
|
|
7
|
+
|
|
8
|
+
function tempDir(): { dir: string; cleanup: () => void } {
|
|
9
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-pairreq-"));
|
|
10
|
+
return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test("点一下确认:A 请求 -> B 面板看到同一个 6 位码 -> B 允许 -> A 自动落盘并连上", async () => {
|
|
14
|
+
const a = tempDir();
|
|
15
|
+
const b = tempDir();
|
|
16
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pr-a-${process.pid}` });
|
|
17
|
+
const svcB = new FabricService({ agentDir: b.dir, host: "127.0.0.1", port: 0, ctx: `pr-b-${process.pid}` });
|
|
18
|
+
try {
|
|
19
|
+
await svcA.start();
|
|
20
|
+
await svcB.start();
|
|
21
|
+
|
|
22
|
+
const requested = await svcA.requestPairing({
|
|
23
|
+
address: "127.0.0.1",
|
|
24
|
+
port: svcB.port,
|
|
25
|
+
name: "机-A",
|
|
26
|
+
});
|
|
27
|
+
assert.equal(requested.ok, true);
|
|
28
|
+
if (!requested.ok) return;
|
|
29
|
+
assert.equal(requested.nodeId, svcB.nodeId);
|
|
30
|
+
assert.match(requested.sas, /^\d{6}$/);
|
|
31
|
+
|
|
32
|
+
const pendingB = svcB.pendingRequests();
|
|
33
|
+
assert.equal(pendingB.inbound.length, 1);
|
|
34
|
+
assert.equal(pendingB.inbound[0].nodeId, svcA.nodeId);
|
|
35
|
+
assert.equal(pendingB.inbound[0].name, "机-A");
|
|
36
|
+
// 两端必须显示同一个数字,否则用户核对没有意义
|
|
37
|
+
assert.equal(pendingB.inbound[0].sas, requested.sas);
|
|
38
|
+
|
|
39
|
+
// 允许之前:两端都还没有 peer 记录
|
|
40
|
+
assert.deepEqual(svcA.snapshot().peers, []);
|
|
41
|
+
assert.deepEqual(svcB.snapshot().peers, []);
|
|
42
|
+
|
|
43
|
+
const confirmed = svcB.confirmPairRequest(svcA.nodeId, true, "workspace-rw+bash", "机-A");
|
|
44
|
+
assert.equal(confirmed.ok, true);
|
|
45
|
+
assert.equal(confirmed.peer?.grants, "workspace-rw+bash");
|
|
46
|
+
assert.deepEqual(svcB.snapshot().peers.map((peer) => peer.name), ["机-A"]);
|
|
47
|
+
|
|
48
|
+
const resolved = await svcA.resolveOutboundPairings();
|
|
49
|
+
assert.deepEqual(resolved.resolved, [svcB.nodeId]);
|
|
50
|
+
assert.deepEqual(resolved.denied, []);
|
|
51
|
+
const peerA = svcA.snapshot().peers;
|
|
52
|
+
assert.equal(peerA.length, 1);
|
|
53
|
+
// A 侧记录的档位是「A 给 B 的权限」,默认只读;B 侧给 A 的档位由 B 用户决定。
|
|
54
|
+
assert.equal(peerA[0].grants, "read-only");
|
|
55
|
+
|
|
56
|
+
await waitFor(() => svcA.connectionStates()[svcB.nodeId] === "online", 5_000);
|
|
57
|
+
await waitFor(() => svcB.connectionStates()[svcA.nodeId] === "online", 5_000);
|
|
58
|
+
assert.deepEqual(svcA.pendingRequests().outbound, []);
|
|
59
|
+
} finally {
|
|
60
|
+
await svcA.stop();
|
|
61
|
+
await svcB.stop();
|
|
62
|
+
a.cleanup();
|
|
63
|
+
b.cleanup();
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("拒绝配对:不落任何 peer 记录,两端仍是陌生人", async () => {
|
|
68
|
+
const a = tempDir();
|
|
69
|
+
const b = tempDir();
|
|
70
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pr-c-${process.pid}` });
|
|
71
|
+
const svcB = new FabricService({ agentDir: b.dir, host: "127.0.0.1", port: 0, ctx: `pr-d-${process.pid}` });
|
|
72
|
+
try {
|
|
73
|
+
await svcA.start();
|
|
74
|
+
await svcB.start();
|
|
75
|
+
const requested = await svcA.requestPairing({ address: "127.0.0.1", port: svcB.port });
|
|
76
|
+
assert.equal(requested.ok, true);
|
|
77
|
+
if (!requested.ok) return;
|
|
78
|
+
assert.equal(svcB.confirmPairRequest(svcA.nodeId, false).ok, true);
|
|
79
|
+
assert.deepEqual(svcB.snapshot().peers, []);
|
|
80
|
+
// 被拒后发起方立刻知道(不用等 10 分钟 TTL),请求收摊,且不落任何 peer 记录
|
|
81
|
+
const probed = await svcA.resolveOutboundPairings();
|
|
82
|
+
assert.deepEqual(probed.resolved, []);
|
|
83
|
+
assert.deepEqual(probed.denied, [svcB.nodeId]);
|
|
84
|
+
assert.equal(svcA.pendingRequests().outbound.length, 0);
|
|
85
|
+
assert.deepEqual(svcA.pendingRequests().denied, [svcB.nodeId]);
|
|
86
|
+
assert.deepEqual(svcA.snapshot().peers, []);
|
|
87
|
+
} finally {
|
|
88
|
+
await svcA.stop();
|
|
89
|
+
await svcB.stop();
|
|
90
|
+
a.cleanup();
|
|
91
|
+
b.cleanup();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("未配对的连接只允许 pair.request:别的方法一律拒绝", async () => {
|
|
96
|
+
const a = tempDir();
|
|
97
|
+
const b = tempDir();
|
|
98
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pr-e-${process.pid}` });
|
|
99
|
+
const svcB = new FabricService({ agentDir: b.dir, host: "127.0.0.1", port: 0, ctx: `pr-f-${process.pid}` });
|
|
100
|
+
try {
|
|
101
|
+
await svcA.start();
|
|
102
|
+
await svcB.start();
|
|
103
|
+
const requested = await svcA.requestPairing({ address: "127.0.0.1", port: svcB.port });
|
|
104
|
+
assert.equal(requested.ok, true);
|
|
105
|
+
// 冒充「已配对」直接调 dispatch 会撞在受限会话上,任务不会落地
|
|
106
|
+
const { dialFabricLink } = await import("./transport.ts");
|
|
107
|
+
const link = await dialFabricLink({
|
|
108
|
+
identity: svcA.identity,
|
|
109
|
+
peer: { address: "127.0.0.1", port: svcB.port },
|
|
110
|
+
});
|
|
111
|
+
try {
|
|
112
|
+
await assert.rejects(() => link.call("task.submit", { taskId: "aaaaaaaaaaaaaaaa", prompt: "x" }));
|
|
113
|
+
assert.deepEqual(svcB.inbound.list(), []);
|
|
114
|
+
} finally {
|
|
115
|
+
link.close();
|
|
116
|
+
}
|
|
117
|
+
} finally {
|
|
118
|
+
await svcA.stop();
|
|
119
|
+
await svcB.stop();
|
|
120
|
+
a.cleanup();
|
|
121
|
+
b.cleanup();
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
async function waitFor(predicate: () => boolean, timeoutMs: number): Promise<void> {
|
|
126
|
+
const startedAt = Date.now();
|
|
127
|
+
while (!predicate()) {
|
|
128
|
+
if (Date.now() - startedAt > timeoutMs) throw new Error("condition not met in time");
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
test("有人敲门时立刻通知本机 UI(不等面板轮询)", async () => {
|
|
134
|
+
const a = tempDir();
|
|
135
|
+
const b = tempDir();
|
|
136
|
+
const notices: { nodeId: string; name: string; sas: string }[] = [];
|
|
137
|
+
const svcA = new FabricService({ agentDir: a.dir, host: "127.0.0.1", port: 0, ctx: `pn-a-${process.pid}` });
|
|
138
|
+
const svcB = new FabricService({
|
|
139
|
+
agentDir: b.dir,
|
|
140
|
+
host: "127.0.0.1",
|
|
141
|
+
port: 0,
|
|
142
|
+
ctx: `pn-b-${process.pid}`,
|
|
143
|
+
onPairRequestNotice: (request) =>
|
|
144
|
+
notices.push({ nodeId: request.nodeId, name: request.name, sas: request.sas }),
|
|
145
|
+
});
|
|
146
|
+
try {
|
|
147
|
+
await svcA.start();
|
|
148
|
+
await svcB.start();
|
|
149
|
+
const requested = await svcA.requestPairing({ address: "127.0.0.1", port: svcB.port, name: "机-A" });
|
|
150
|
+
assert.equal(requested.ok, true);
|
|
151
|
+
if (!requested.ok) return;
|
|
152
|
+
// 同步通知:请求到达那一刻就该回调,不需要等下一次 /api/fleet 轮询
|
|
153
|
+
assert.equal(notices.length, 1);
|
|
154
|
+
assert.equal(notices[0].nodeId, svcA.nodeId);
|
|
155
|
+
assert.equal(notices[0].name, "机-A");
|
|
156
|
+
assert.equal(notices[0].sas, requested.sas);
|
|
157
|
+
} finally {
|
|
158
|
+
await svcA.stop();
|
|
159
|
+
await svcB.stop();
|
|
160
|
+
a.cleanup();
|
|
161
|
+
b.cleanup();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import {
|
|
7
|
+
FABRIC_SUMMARY_CHARS,
|
|
8
|
+
clampSummary,
|
|
9
|
+
createScratchWorkspaceProvider,
|
|
10
|
+
createSubagentRunner,
|
|
11
|
+
extractFiles,
|
|
12
|
+
} from "./fabric-runner.ts";
|
|
13
|
+
import { createIdentity } from "./pairing.ts";
|
|
14
|
+
import { FabricDispatcher } from "./dispatcher.ts";
|
|
15
|
+
|
|
16
|
+
test("摘要截断:进对方上下文的必须短", () => {
|
|
17
|
+
assert.equal(clampSummary(" ok "), "ok");
|
|
18
|
+
const long = "x".repeat(FABRIC_SUMMARY_CHARS + 100);
|
|
19
|
+
const clamped = clampSummary(long);
|
|
20
|
+
assert.equal(clamped.length, FABRIC_SUMMARY_CHARS);
|
|
21
|
+
assert.ok(clamped.endsWith("…"));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("只提取相对路径,绝对路径与 .. 一律丢掉", () => {
|
|
25
|
+
const files = extractFiles(
|
|
26
|
+
"改动:src/app.ts 和 components/Chat.tsx,另见 /etc/passwd 与 ../../secret.txt",
|
|
27
|
+
);
|
|
28
|
+
assert.deepEqual(files, ["src/app.ts", "components/Chat.tsx"]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("没有工作区提供者就拒绝执行:绝不在用户当前目录跑远程任务", async () => {
|
|
32
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-runner-"));
|
|
33
|
+
try {
|
|
34
|
+
const runner = createSubagentRunner(dir, {
|
|
35
|
+
spawn: async () => ({ ok: true, text: "never" }),
|
|
36
|
+
});
|
|
37
|
+
await assert.rejects(
|
|
38
|
+
() =>
|
|
39
|
+
runner.run({
|
|
40
|
+
taskId: "aaaaaaaaaaaaaaaa",
|
|
41
|
+
prompt: "x",
|
|
42
|
+
workspaceHint: "",
|
|
43
|
+
grant: "workspace-rw",
|
|
44
|
+
tools: [],
|
|
45
|
+
input: null,
|
|
46
|
+
signal: new AbortController().signal,
|
|
47
|
+
onProgress: () => undefined,
|
|
48
|
+
}),
|
|
49
|
+
/E_UNSUPPORTED/,
|
|
50
|
+
);
|
|
51
|
+
} finally {
|
|
52
|
+
rmSync(dir, { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("工具集只来自档位:对端申请什么都不加", async () => {
|
|
57
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-runner-"));
|
|
58
|
+
const seen: { tools: string[]; cwd: string }[] = [];
|
|
59
|
+
try {
|
|
60
|
+
const runner = createSubagentRunner(dir, {
|
|
61
|
+
prepareWorkspace: createScratchWorkspaceProvider(dir),
|
|
62
|
+
spawn: async (input) => {
|
|
63
|
+
seen.push({ tools: input.tools, cwd: input.cwd });
|
|
64
|
+
return { ok: true, text: "改好了 src/a.ts 与 src/b.ts" };
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
const result = await runner.run({
|
|
68
|
+
taskId: "bbbbbbbbbbbbbbbb",
|
|
69
|
+
prompt: "修一下",
|
|
70
|
+
workspaceHint: "repo",
|
|
71
|
+
grant: "read-only",
|
|
72
|
+
tools: ["bash", "edit"],
|
|
73
|
+
input: null,
|
|
74
|
+
signal: new AbortController().signal,
|
|
75
|
+
onProgress: () => undefined,
|
|
76
|
+
});
|
|
77
|
+
assert.deepEqual(seen[0].tools, ["find", "grep", "ls", "read"]);
|
|
78
|
+
assert.ok(seen[0].cwd.includes("bbbbbbbbbbbbbbbb"));
|
|
79
|
+
assert.deepEqual(result.files, ["src/a.ts", "src/b.ts"]);
|
|
80
|
+
assert.match(result.summary, /改好了/);
|
|
81
|
+
assert.equal(result.exitCode, 0);
|
|
82
|
+
} finally {
|
|
83
|
+
rmSync(dir, { recursive: true, force: true });
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("子会话失败时抛错,Dispatcher 会把任务记为 failed", async () => {
|
|
88
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-runner-"));
|
|
89
|
+
try {
|
|
90
|
+
const runner = createSubagentRunner(dir, {
|
|
91
|
+
prepareWorkspace: createScratchWorkspaceProvider(dir),
|
|
92
|
+
spawn: async () => ({ ok: false, text: "", error: "model overloaded" }),
|
|
93
|
+
});
|
|
94
|
+
const dispatcher = new FabricDispatcher({
|
|
95
|
+
agentDir: dir,
|
|
96
|
+
identity: createIdentity(),
|
|
97
|
+
runner,
|
|
98
|
+
});
|
|
99
|
+
await dispatcher.handleInbound(
|
|
100
|
+
"task.submit",
|
|
101
|
+
{ taskId: "cccccccccccccccc", prompt: "干点活", deadlineClass: "build" },
|
|
102
|
+
{ nodeId: "0123456789abcdef", grants: "workspace-rw" },
|
|
103
|
+
);
|
|
104
|
+
const startedAt = Date.now();
|
|
105
|
+
while (dispatcher.inbound.get("cccccccccccccccc")?.state !== "failed") {
|
|
106
|
+
if (Date.now() - startedAt > 3_000) throw new Error("任务没有落到 failed");
|
|
107
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
108
|
+
}
|
|
109
|
+
assert.match(dispatcher.inbound.get("cccccccccccccccc")?.error ?? "", /model overloaded/);
|
|
110
|
+
} finally {
|
|
111
|
+
rmSync(dir, { recursive: true, force: true });
|
|
112
|
+
}
|
|
113
|
+
});
|