@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
package/src/history.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同类任务的历史耗时:选机时用来替代「模型瞎估」的那个 estimatedMs。
|
|
3
|
+
*
|
|
4
|
+
* 只按能力标签聚合成一个中位数,不记任务内容——这里存的东西不该能反推出你在做什么。
|
|
5
|
+
* 读失败一律当「没历史」,绝不能因为一个提示性文件把派活整条链路搞挂。
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
|
|
10
|
+
/** 每个标签组合留多少条样本:20 条足够算中位,又不至于让文件长胖。 */
|
|
11
|
+
export const HISTORY_SAMPLES = 20;
|
|
12
|
+
/** 最多记多少个标签组合:超了淘汰最久没更新的。 */
|
|
13
|
+
export const HISTORY_MAX_KEYS = 32;
|
|
14
|
+
|
|
15
|
+
export interface HistoryEntry {
|
|
16
|
+
samples: number[];
|
|
17
|
+
lastOk: boolean;
|
|
18
|
+
updatedAt: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type HistoryBook = Record<string, HistoryEntry>;
|
|
22
|
+
|
|
23
|
+
/** 标签集合 → 键:排序去重,保证 ["build","gpu"] 与 ["gpu","build"] 是同一条历史。 */
|
|
24
|
+
export function historyKey(labels: readonly string[]): string {
|
|
25
|
+
const clean = [...new Set(labels.map((label) => label.trim().toLowerCase()).filter(Boolean))].sort();
|
|
26
|
+
return clean.length === 0 ? "*" : clean.join(",");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function historyPath(agentDir: string): string {
|
|
30
|
+
return join(agentDir, "fabric", "history.json");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function median(values: readonly number[]): number | null {
|
|
34
|
+
if (values.length === 0) return null;
|
|
35
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
36
|
+
const middle = Math.floor(sorted.length / 2);
|
|
37
|
+
return sorted.length % 2 === 1
|
|
38
|
+
? (sorted[middle] ?? 0)
|
|
39
|
+
: Math.round(((sorted[middle - 1] ?? 0) + (sorted[middle] ?? 0)) / 2);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class FabricHistory {
|
|
43
|
+
private book: HistoryBook | null = null;
|
|
44
|
+
|
|
45
|
+
// 注意:这里不用 TS 参数属性——node --test 是 strip-only 模式,参数属性会直接报语法错。
|
|
46
|
+
private readonly agentDir: string;
|
|
47
|
+
private readonly overridePath: string | null;
|
|
48
|
+
|
|
49
|
+
constructor(agentDir: string, overridePath?: string) {
|
|
50
|
+
this.agentDir = agentDir;
|
|
51
|
+
this.overridePath = overridePath ?? null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private file(): string {
|
|
55
|
+
return this.overridePath ?? historyPath(this.agentDir);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private read(): HistoryBook {
|
|
59
|
+
if (this.book) return this.book;
|
|
60
|
+
try {
|
|
61
|
+
const file = this.file();
|
|
62
|
+
if (!existsSync(file)) {
|
|
63
|
+
this.book = {};
|
|
64
|
+
return this.book;
|
|
65
|
+
}
|
|
66
|
+
const raw: unknown = JSON.parse(readFileSync(file, "utf8"));
|
|
67
|
+
const book: HistoryBook = {};
|
|
68
|
+
if (raw && typeof raw === "object") {
|
|
69
|
+
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
70
|
+
if (!value || typeof value !== "object") continue;
|
|
71
|
+
const entry = value as Record<string, unknown>;
|
|
72
|
+
const samples = Array.isArray(entry.samples)
|
|
73
|
+
? entry.samples.filter((item): item is number => typeof item === "number" && item >= 0)
|
|
74
|
+
: [];
|
|
75
|
+
book[key] = {
|
|
76
|
+
samples: samples.slice(-HISTORY_SAMPLES),
|
|
77
|
+
lastOk: entry.lastOk === true,
|
|
78
|
+
updatedAt: typeof entry.updatedAt === "number" ? entry.updatedAt : 0,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
this.book = book;
|
|
83
|
+
} catch {
|
|
84
|
+
this.book = {};
|
|
85
|
+
}
|
|
86
|
+
return this.book;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private write(): void {
|
|
90
|
+
const book = this.read();
|
|
91
|
+
const keys = Object.keys(book);
|
|
92
|
+
if (keys.length > HISTORY_MAX_KEYS) {
|
|
93
|
+
const keep = keys
|
|
94
|
+
.sort((a, b) => (book[b]?.updatedAt ?? 0) - (book[a]?.updatedAt ?? 0))
|
|
95
|
+
.slice(0, HISTORY_MAX_KEYS);
|
|
96
|
+
for (const key of keys) if (!keep.includes(key)) delete book[key];
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const file = this.file();
|
|
100
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
101
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
102
|
+
writeFileSync(temp, `${JSON.stringify(book, null, 2)}\n`, "utf8");
|
|
103
|
+
renameSync(temp, file);
|
|
104
|
+
} catch {
|
|
105
|
+
// 写不进去就只在内存里用:提示性数据不值得让派活失败。
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
record(labels: readonly string[] | string, durationMs: number, ok: boolean, now = Date.now()): void {
|
|
110
|
+
const key = typeof labels === "string" ? labels : historyKey(labels);
|
|
111
|
+
const book = this.read();
|
|
112
|
+
const current = book[key] ?? { samples: [], lastOk: false, updatedAt: now };
|
|
113
|
+
const samples = [...current.samples, Math.max(0, Math.round(durationMs))].slice(-HISTORY_SAMPLES);
|
|
114
|
+
book[key] = { samples, lastOk: ok, updatedAt: now };
|
|
115
|
+
this.write();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 中位耗时;没有样本返回 null(调用方据此区分「没跑过」与「跑了 0 秒」)。 */
|
|
119
|
+
p50(labels: readonly string[] | string): number | null {
|
|
120
|
+
const key = typeof labels === "string" ? labels : historyKey(labels);
|
|
121
|
+
return median(this.read()[key]?.samples ?? []);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
snapshot(): HistoryBook {
|
|
125
|
+
return { ...this.read() };
|
|
126
|
+
}
|
|
127
|
+
}
|
package/src/host-load.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本机负载:只报能报的,报不出来的宁可留空。
|
|
3
|
+
*
|
|
4
|
+
* loadavg 在 Windows 上恒为 0,所以这里返回 null 而不是 0——
|
|
5
|
+
* 报 0 会让「这台最闲」变成一句谎话,选机就会持续押在 Windows 机器上。
|
|
6
|
+
*/
|
|
7
|
+
import { cpus, freemem, loadavg } from "node:os";
|
|
8
|
+
|
|
9
|
+
export function cpuLoad(): number | null {
|
|
10
|
+
const cores = cpus().length || 1;
|
|
11
|
+
const average = loadavg()[0] ?? 0;
|
|
12
|
+
if (!(average > 0)) return null;
|
|
13
|
+
return Math.min(1, average / cores);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function memAvailableMb(): number {
|
|
17
|
+
return Math.round(freemem() / (1024 * 1024));
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yourstandby/fabric 的公开入口。
|
|
3
|
+
*
|
|
4
|
+
* 这里只做「一个入口、一套导出」:外部(PiForge / CLI / MCP)都从本文件取类型与实现,
|
|
5
|
+
* 不直接摸内部文件路径。内部模块之间的 import 仍用相对路径,方便后续继续拆分。
|
|
6
|
+
*/
|
|
7
|
+
export {
|
|
8
|
+
FabricService,
|
|
9
|
+
type FabricServiceOptions,
|
|
10
|
+
type InboundPairRequest,
|
|
11
|
+
type OutboundPairRequest,
|
|
12
|
+
type PairResult,
|
|
13
|
+
} from "./fabric-service.ts";
|
|
14
|
+
export type { FabricRemoteRunner, SubmitInput } from "./dispatcher.ts";
|
|
15
|
+
export { FabricDispatcher } from "./dispatcher.ts";
|
|
16
|
+
export {
|
|
17
|
+
createShellRunner,
|
|
18
|
+
prepareWorkspace,
|
|
19
|
+
buildCommand,
|
|
20
|
+
quotePrompt,
|
|
21
|
+
type ShellRunnerOptions,
|
|
22
|
+
DEFAULT_TASK_TIMEOUT_MS,
|
|
23
|
+
} from "./daemon-runner.ts";
|
|
24
|
+
export { scanLan, broadcastAddresses, DISC_ANNOUNCE_PORT, type NearbyDevice } from "./discovery.ts";
|
|
25
|
+
export { main, parseArgs, callDaemon, controlSocketPath, DEFAULT_AGENT_DIR } from "./daemon.ts";
|
|
26
|
+
export { buildPeerRows, buildTaskRows, summarize } from "./fabric-view.ts";
|
|
27
|
+
export * from "../../../contract/domains/fabric.ts";
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import {
|
|
7
|
+
PAIRING_LOCKOUT_MS,
|
|
8
|
+
PAIRING_MAX_FAILURES,
|
|
9
|
+
PAIRING_WINDOW_MS,
|
|
10
|
+
PairingGuard,
|
|
11
|
+
PairingSession,
|
|
12
|
+
challengeMac,
|
|
13
|
+
createIdentity,
|
|
14
|
+
deriveSas,
|
|
15
|
+
deriveSessionKeys,
|
|
16
|
+
deriveSharedSecret,
|
|
17
|
+
identityPath,
|
|
18
|
+
loadOrCreateIdentity,
|
|
19
|
+
newNonce,
|
|
20
|
+
nodeIdOf,
|
|
21
|
+
setIdentityPathForTest,
|
|
22
|
+
verifyChallengeMac,
|
|
23
|
+
} from "./pairing.ts";
|
|
24
|
+
|
|
25
|
+
function setup() {
|
|
26
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-fabric-pair-"));
|
|
27
|
+
const file = join(dir, "fabric", "identity.json");
|
|
28
|
+
setIdentityPathForTest(file);
|
|
29
|
+
return {
|
|
30
|
+
dir,
|
|
31
|
+
file,
|
|
32
|
+
cleanup: () => {
|
|
33
|
+
setIdentityPathForTest(null);
|
|
34
|
+
rmSync(dir, { recursive: true, force: true });
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("首次运行生成身份,之后原样读回(nodeId 不变)", () => {
|
|
40
|
+
const env = setup();
|
|
41
|
+
try {
|
|
42
|
+
assert.equal(identityPath(env.dir), join(env.dir, "fabric", "identity.json"));
|
|
43
|
+
const first = loadOrCreateIdentity(env.dir);
|
|
44
|
+
assert.equal(first.nodeId.length, 16);
|
|
45
|
+
assert.equal(first.nodeId, nodeIdOf(first.publicKey));
|
|
46
|
+
const second = loadOrCreateIdentity(env.dir);
|
|
47
|
+
assert.equal(second.nodeId, first.nodeId);
|
|
48
|
+
assert.equal(second.publicKey, first.publicKey);
|
|
49
|
+
const raw = JSON.parse(readFileSync(env.file, "utf8")) as Record<string, unknown>;
|
|
50
|
+
assert.equal(raw.nodeId, first.nodeId);
|
|
51
|
+
if (process.platform !== "win32") {
|
|
52
|
+
assert.equal(statSync(env.file).mode & 0o777, 0o600);
|
|
53
|
+
}
|
|
54
|
+
} finally {
|
|
55
|
+
env.cleanup();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("身份文件被手改到公私钥不匹配时重建,而不是带着错身份跑", () => {
|
|
60
|
+
const env = setup();
|
|
61
|
+
try {
|
|
62
|
+
const a = loadOrCreateIdentity(env.dir);
|
|
63
|
+
const b = createIdentity();
|
|
64
|
+
writeFileSync(
|
|
65
|
+
env.file,
|
|
66
|
+
JSON.stringify({ ...a, privateKey: b.privateKey }),
|
|
67
|
+
"utf8",
|
|
68
|
+
);
|
|
69
|
+
const rebuilt = loadOrCreateIdentity(env.dir);
|
|
70
|
+
assert.notEqual(rebuilt.privateKey, b.privateKey);
|
|
71
|
+
assert.equal(rebuilt.nodeId, nodeIdOf(rebuilt.publicKey));
|
|
72
|
+
} finally {
|
|
73
|
+
env.cleanup();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("两端 ECDH 得到同一个 secret,SAS 对称且为 6 位数字", () => {
|
|
78
|
+
const a = createIdentity();
|
|
79
|
+
const b = createIdentity();
|
|
80
|
+
const secretA = deriveSharedSecret(a, b.publicKey);
|
|
81
|
+
const secretB = deriveSharedSecret(b, a.publicKey);
|
|
82
|
+
assert.deepEqual(secretA, secretB);
|
|
83
|
+
assert.equal(secretA.length, 32);
|
|
84
|
+
|
|
85
|
+
const sasA = deriveSas(secretA, a.nodeId, b.nodeId);
|
|
86
|
+
const sasB = deriveSas(secretB, b.nodeId, a.nodeId);
|
|
87
|
+
assert.equal(sasA, sasB);
|
|
88
|
+
assert.match(sasA, /^\d{6}$/);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("SAS 绑定双方 nodeId:同一个 secret 换对端就不同值", () => {
|
|
92
|
+
const a = createIdentity();
|
|
93
|
+
const b = createIdentity();
|
|
94
|
+
const c = createIdentity();
|
|
95
|
+
const secret = deriveSharedSecret(a, b.publicKey);
|
|
96
|
+
assert.notEqual(deriveSas(secret, a.nodeId, b.nodeId), deriveSas(secret, a.nodeId, c.nodeId));
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("会话密钥两个方向不同,长度 32", () => {
|
|
100
|
+
const a = createIdentity();
|
|
101
|
+
const b = createIdentity();
|
|
102
|
+
const secret = deriveSharedSecret(a, b.publicKey);
|
|
103
|
+
const keys = deriveSessionKeys(secret, a.nodeId, b.nodeId);
|
|
104
|
+
assert.equal(keys.aToB.length, 32);
|
|
105
|
+
assert.equal(keys.bToA.length, 32);
|
|
106
|
+
assert.notDeepEqual(keys.aToB, keys.bToA);
|
|
107
|
+
const swapped = deriveSessionKeys(secret, b.nodeId, a.nodeId);
|
|
108
|
+
assert.deepEqual(swapped.aToB, keys.aToB);
|
|
109
|
+
assert.deepEqual(swapped.bToA, keys.bToA);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("HMAC 挑战:对端用自己的方向密钥能过,改一位就过不了", () => {
|
|
113
|
+
const a = createIdentity();
|
|
114
|
+
const b = createIdentity();
|
|
115
|
+
const keys = deriveSessionKeys(deriveSharedSecret(a, b.publicKey), a.nodeId, b.nodeId);
|
|
116
|
+
const nonce = newNonce();
|
|
117
|
+
const mac = challengeMac(keys.aToB, nonce, a.nodeId);
|
|
118
|
+
assert.equal(verifyChallengeMac(keys.aToB, nonce, a.nodeId, mac), true);
|
|
119
|
+
assert.equal(verifyChallengeMac(keys.bToA, nonce, a.nodeId, mac), false);
|
|
120
|
+
assert.equal(verifyChallengeMac(keys.aToB, newNonce(), a.nodeId, mac), false);
|
|
121
|
+
assert.equal(verifyChallengeMac(keys.aToB, nonce, a.nodeId, "deadbeef"), false);
|
|
122
|
+
assert.equal(verifyChallengeMac(keys.aToB, nonce, a.nodeId, "不是十六进制"), false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("配对会话:SAS 对得上就成功,错三次烧掉会话", () => {
|
|
126
|
+
const env = setup();
|
|
127
|
+
try {
|
|
128
|
+
const me = loadOrCreateIdentity(env.dir);
|
|
129
|
+
const peer = createIdentity();
|
|
130
|
+
const now = 1_000_000;
|
|
131
|
+
const session = new PairingSession(me, peer.nodeId, peer.publicKey, now);
|
|
132
|
+
assert.equal(session.expiresAt, now + PAIRING_WINDOW_MS);
|
|
133
|
+
assert.match(session.sas, /^\d{6}$/);
|
|
134
|
+
assert.equal(session.isExpired(now + PAIRING_WINDOW_MS - 1), false);
|
|
135
|
+
assert.equal(session.isExpired(now + PAIRING_WINDOW_MS + 1), true);
|
|
136
|
+
|
|
137
|
+
const wrong = session.sas === "000000" ? "000001" : "000000";
|
|
138
|
+
for (let attempt = 1; attempt < PAIRING_MAX_FAILURES; attempt += 1) {
|
|
139
|
+
const failed = session.confirm({ sas: wrong }, now + 10);
|
|
140
|
+
assert.equal(failed.ok === false && failed.code, "E_PAIR_SAS_MISMATCH");
|
|
141
|
+
assert.equal(session.isClosed(), false);
|
|
142
|
+
}
|
|
143
|
+
const burned = session.confirm({ sas: wrong }, now + 20);
|
|
144
|
+
assert.equal(burned.ok === false && burned.code, "E_PAIR_LOCKED");
|
|
145
|
+
assert.equal(session.isClosed(), true);
|
|
146
|
+
// 烧掉后即使 SAS 正确也不能再用,必须重新发起配对(并受 PairingGuard 锁定约束)。
|
|
147
|
+
const tooLate = session.confirm({ sas: session.sas }, now + 30);
|
|
148
|
+
assert.equal(tooLate.ok === false && tooLate.code, "E_PAIR_WINDOW_CLOSED");
|
|
149
|
+
|
|
150
|
+
const fresh = new PairingSession(me, peer.nodeId, peer.publicKey, now + 40);
|
|
151
|
+
assert.equal(fresh.confirm({ sas: fresh.sas }, now + 50).ok, true);
|
|
152
|
+
} finally {
|
|
153
|
+
env.cleanup();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("配对会话:确认后就关闭,过期则拒绝", () => {
|
|
158
|
+
const env = setup();
|
|
159
|
+
try {
|
|
160
|
+
const me = loadOrCreateIdentity(env.dir);
|
|
161
|
+
const peer = createIdentity();
|
|
162
|
+
const now = 2_000_000;
|
|
163
|
+
const session = new PairingSession(me, peer.nodeId, peer.publicKey, now);
|
|
164
|
+
const results = session.confirm({ sas: session.sas }, now + 5);
|
|
165
|
+
assert.equal(results.ok, true);
|
|
166
|
+
if (results.ok) {
|
|
167
|
+
assert.deepEqual(results.sharedSecret, deriveSharedSecret(me, peer.publicKey));
|
|
168
|
+
}
|
|
169
|
+
const again = session.confirm({ sas: session.sas }, now + 6);
|
|
170
|
+
assert.equal(again.ok === false && again.code, "E_PAIR_WINDOW_CLOSED");
|
|
171
|
+
|
|
172
|
+
const stale = new PairingSession(me, peer.nodeId, peer.publicKey, now);
|
|
173
|
+
const expired = stale.confirm({}, now + PAIRING_WINDOW_MS + 1);
|
|
174
|
+
assert.equal(expired.ok === false && expired.code, "E_PAIR_WINDOW_CLOSED");
|
|
175
|
+
} finally {
|
|
176
|
+
env.cleanup();
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("拒绝配对不留下任何状态:同一会话改口仍可成功", () => {
|
|
181
|
+
const env = setup();
|
|
182
|
+
try {
|
|
183
|
+
const me = loadOrCreateIdentity(env.dir);
|
|
184
|
+
const peer = createIdentity();
|
|
185
|
+
const now = 3_000_000;
|
|
186
|
+
const session = new PairingSession(me, peer.nodeId, peer.publicKey, now);
|
|
187
|
+
session.reject();
|
|
188
|
+
const after = session.confirm({ sas: session.sas }, now + 1);
|
|
189
|
+
assert.equal(after.ok === false && after.code, "E_PAIR_WINDOW_CLOSED");
|
|
190
|
+
} finally {
|
|
191
|
+
env.cleanup();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("锁定按 nodeId 隔离:另一台机器不受牵连", () => {
|
|
196
|
+
const guard = new PairingGuard();
|
|
197
|
+
const now = 5_000_000;
|
|
198
|
+
for (let attempt = 0; attempt < PAIRING_MAX_FAILURES; attempt += 1) {
|
|
199
|
+
guard.recordFailure("0123456789abcdef", now);
|
|
200
|
+
}
|
|
201
|
+
assert.equal(guard.lockedUntil("0123456789abcdef"), now + PAIRING_LOCKOUT_MS);
|
|
202
|
+
assert.equal(guard.lockedUntil("fedcba9876543210"), 0);
|
|
203
|
+
guard.reset("0123456789abcdef");
|
|
204
|
+
assert.equal(guard.lockedUntil("0123456789abcdef"), 0);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("非法公钥(不是 X25519 SPKI)直接报错,不返回半成品 secret", () => {
|
|
208
|
+
const me = createIdentity();
|
|
209
|
+
assert.throws(() => deriveSharedSecret(me, "bm90LWEta2V5"), (error: unknown) =>
|
|
210
|
+
String((error as Error).message).includes("X25519"),
|
|
211
|
+
);
|
|
212
|
+
});
|