@tpsdev-ai/agent 0.7.0 → 0.9.0
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/bun.lock +27 -15
- package/bunfig.toml +17 -0
- package/dist/bin.js +4 -3
- package/dist/bin.js.map +1 -1
- package/dist/config.d.ts +8 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +38 -3
- package/dist/config.js.map +1 -1
- package/dist/home.d.ts +3 -0
- package/dist/home.d.ts.map +1 -0
- package/dist/home.js +18 -0
- package/dist/home.js.map +1 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/io/flair.d.ts +1 -10
- package/dist/io/flair.d.ts.map +1 -1
- package/dist/io/flair.js +29 -28
- package/dist/io/flair.js.map +1 -1
- package/dist/io/mail.d.ts +184 -30
- package/dist/io/mail.d.ts.map +1 -1
- package/dist/io/mail.js +944 -185
- package/dist/io/mail.js.map +1 -1
- package/dist/lib/agent-keys.d.ts +106 -0
- package/dist/lib/agent-keys.d.ts.map +1 -0
- package/dist/lib/agent-keys.js +298 -0
- package/dist/lib/agent-keys.js.map +1 -0
- package/dist/lib/bridge-identity.d.ts +10 -0
- package/dist/lib/bridge-identity.d.ts.map +1 -0
- package/dist/lib/bridge-identity.js +56 -0
- package/dist/lib/bridge-identity.js.map +1 -0
- package/dist/lib/mail-lock-namespace.d.ts +19 -0
- package/dist/lib/mail-lock-namespace.d.ts.map +1 -0
- package/dist/lib/mail-lock-namespace.js +58 -0
- package/dist/lib/mail-lock-namespace.js.map +1 -0
- package/dist/lib/mail-lock.d.ts +81 -0
- package/dist/lib/mail-lock.d.ts.map +1 -0
- package/dist/lib/mail-lock.js +319 -0
- package/dist/lib/mail-lock.js.map +1 -0
- package/dist/lib/mailbox-filename.d.ts +2 -0
- package/dist/lib/mailbox-filename.d.ts.map +1 -0
- package/dist/lib/mailbox-filename.js +4 -0
- package/dist/lib/mailbox-filename.js.map +1 -0
- package/dist/lib/mailbox-policy.d.ts +245 -0
- package/dist/lib/mailbox-policy.d.ts.map +1 -0
- package/dist/lib/mailbox-policy.js +949 -0
- package/dist/lib/mailbox-policy.js.map +1 -0
- package/dist/lib/public-key.d.ts +6 -0
- package/dist/lib/public-key.d.ts.map +1 -0
- package/dist/lib/public-key.js +45 -0
- package/dist/lib/public-key.js.map +1 -0
- package/dist/lib/registry-key.d.ts +2 -0
- package/dist/lib/registry-key.d.ts.map +1 -0
- package/dist/lib/registry-key.js +10 -0
- package/dist/lib/registry-key.js.map +1 -0
- package/dist/lib/relay-lock.d.ts +7 -0
- package/dist/lib/relay-lock.d.ts.map +1 -0
- package/dist/lib/relay-lock.js +19 -0
- package/dist/lib/relay-lock.js.map +1 -0
- package/dist/lib/signEnvelope.d.ts +12 -0
- package/dist/lib/signEnvelope.d.ts.map +1 -1
- package/dist/lib/signEnvelope.js.map +1 -1
- package/dist/lib/signing-key.d.ts +2 -0
- package/dist/lib/signing-key.d.ts.map +1 -0
- package/dist/lib/signing-key.js +9 -0
- package/dist/lib/signing-key.js.map +1 -0
- package/dist/lib/topic-recipient.d.ts +2 -0
- package/dist/lib/topic-recipient.d.ts.map +1 -0
- package/dist/lib/topic-recipient.js +2 -0
- package/dist/lib/topic-recipient.js.map +1 -0
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/llm/provider.js +9 -7
- package/dist/llm/provider.js.map +1 -1
- package/dist/runtime/agent.d.ts.map +1 -1
- package/dist/runtime/agent.js +32 -25
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/event-loop.d.ts +21 -2
- package/dist/runtime/event-loop.d.ts.map +1 -1
- package/dist/runtime/event-loop.js +199 -54
- package/dist/runtime/event-loop.js.map +1 -1
- package/dist/runtime/types.d.ts +11 -0
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js +13 -1
- package/dist/runtime/types.js.map +1 -1
- package/package.json +3 -4
- package/src/bin.ts +4 -3
- package/src/config.ts +46 -3
- package/src/home.ts +18 -0
- package/src/index.ts +33 -2
- package/src/io/flair.ts +23 -28
- package/src/io/mail.ts +1007 -200
- package/src/lib/agent-keys.ts +308 -0
- package/src/lib/bridge-identity.ts +49 -0
- package/src/lib/mail-lock-namespace.ts +64 -0
- package/src/lib/mail-lock.ts +337 -0
- package/src/lib/mailbox-filename.ts +3 -0
- package/src/lib/mailbox-policy.ts +1085 -0
- package/src/lib/public-key.ts +49 -0
- package/src/lib/registry-key.ts +8 -0
- package/src/lib/relay-lock.ts +20 -0
- package/src/lib/signEnvelope.ts +12 -0
- package/src/lib/signing-key.ts +8 -0
- package/src/lib/topic-recipient.ts +1 -0
- package/src/llm/provider.ts +9 -7
- package/src/runtime/agent.ts +30 -27
- package/src/runtime/event-loop.ts +221 -68
- package/src/runtime/types.ts +14 -0
- package/test/config.test.ts +85 -2
- package/test/core-loop.test.ts +6 -4
- package/test/governance.test.ts +4 -1
- package/test/helpers/event-loop-child.mjs +50 -0
- package/test/helpers/mail-delivery-worker.mjs +16 -0
- package/test/helpers/mail-kill-child.mjs +104 -0
- package/test/helpers/signed-relay-record.ts +31 -0
- package/test/home-per-call.test.ts +72 -0
- package/test/io.test.ts +81 -14
- package/test/mail-client-lock-namespace.test.ts +155 -0
- package/test/mail-client-repoint-root.test.ts +368 -0
- package/test/mail-delivery-at-least-once.test.ts +1238 -0
- package/test/mail-event-loop-delivery.test.ts +769 -0
- package/test/mail-intent-phases.test.ts +304 -0
- package/test/mail-interrupted-recovery.test.ts +393 -0
- package/test/mail-lock-pid-state.test.ts +238 -0
- package/test/mail-lock.test.ts +96 -0
- package/test/mail-partial-flair.test.ts +102 -0
- package/test/mail-placement-replay.test.ts +116 -0
- package/test/mail-promote-guard.test.ts +305 -21
- package/test/mail-relay-reservation.test.ts +60 -0
- package/test/mailbox-filename.test.ts +16 -0
- package/test/public-key.test.ts +108 -0
- package/test/runtime-key-encoding.test.ts +105 -0
- package/test/runtime-pending-key.test.ts +117 -0
- package/test/security/mail-trust.test.ts +406 -109
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/** Filesystem crash and unresolved-intent regressions for the per-message delivery API. */
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, renameSync, writeFileSync, rmSync, unlinkSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import * as ed from "@noble/ed25519";
|
|
10
|
+
import { type DeliverNextResult, MailClient } from "../src/io/mail.js";
|
|
11
|
+
import { hasCommittedMessageId, mailboxReplayStore } from "../src/lib/mailbox-policy.js";
|
|
12
|
+
import { acquireMailLock } from "../src/lib/mail-lock.js";
|
|
13
|
+
import { mailboxLockRoot } from "../src/lib/mail-lock-namespace.js";
|
|
14
|
+
import { signEnvelope, type FlairClient } from "../src/lib/signEnvelope.js";
|
|
15
|
+
|
|
16
|
+
const AGENT = "agent-a";
|
|
17
|
+
const FROM = "agent-b";
|
|
18
|
+
const SEED = Buffer.alloc(32, 0x24);
|
|
19
|
+
const CHILD = fileURLToPath(new URL("./helpers/mail-kill-child.mjs", import.meta.url));
|
|
20
|
+
const MODULE = new URL("../src/io/mail.ts", import.meta.url).href;
|
|
21
|
+
|
|
22
|
+
let mailDir: string;
|
|
23
|
+
let mailboxRoot: string;
|
|
24
|
+
const curPath = () => join(mailboxRoot, "cur", "m1.json");
|
|
25
|
+
const newPath = () => join(mailboxRoot, "new", "m1.json");
|
|
26
|
+
|
|
27
|
+
/** The verifier MailClient uses: resolves FROM to its public key, no network. */
|
|
28
|
+
function verifier(): FlairClient {
|
|
29
|
+
return {
|
|
30
|
+
async getAgent(name: string) {
|
|
31
|
+
return name === FROM ? { publicKey: Buffer.from(ed.getPublicKey(SEED)) } : null;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
mailDir = mkdtempSync(join(tmpdir(), "mail-recovery-"));
|
|
38
|
+
mailboxRoot = join(mailDir, AGENT);
|
|
39
|
+
mkdirSync(join(mailboxRoot, "new"), { recursive: true });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(() => rmSync(mailDir, { recursive: true, force: true }));
|
|
43
|
+
|
|
44
|
+
/** Plant one signed record in new/ and return its path. */
|
|
45
|
+
function plant(messageId: string, body = "recovery-body"): string {
|
|
46
|
+
const timestamp = new Date().toISOString();
|
|
47
|
+
const envelope = signEnvelope(
|
|
48
|
+
{
|
|
49
|
+
v: 1,
|
|
50
|
+
from: FROM,
|
|
51
|
+
to: AGENT,
|
|
52
|
+
body,
|
|
53
|
+
messageId,
|
|
54
|
+
timestamp,
|
|
55
|
+
delegationChain: [
|
|
56
|
+
{ agent: "system", kind: "human", timestamp, rationale: "originates", signature: null },
|
|
57
|
+
{ agent: FROM, kind: "agent", timestamp, rationale: "sends", signature: null },
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
{ [FROM]: SEED },
|
|
61
|
+
);
|
|
62
|
+
const source = newPath();
|
|
63
|
+
writeFileSync(source, JSON.stringify({ from: FROM, body: JSON.stringify(envelope) }));
|
|
64
|
+
return source;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const jsonFiles = (dir: string): string[] =>
|
|
68
|
+
existsSync(dir)
|
|
69
|
+
? readdirSync(dir, { withFileTypes: true })
|
|
70
|
+
.filter((e) => e.isFile() && e.name.endsWith(".json"))
|
|
71
|
+
.map((e) => e.name)
|
|
72
|
+
: [];
|
|
73
|
+
|
|
74
|
+
interface KillRun {
|
|
75
|
+
signal: NodeJS.Signals | null;
|
|
76
|
+
reached: boolean;
|
|
77
|
+
stderr: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Run deliverNext() in a child and SIGKILL it at `killAt`; resolve on exit. */
|
|
81
|
+
function runAndKillAt(killAt: string, source: string, after = false, timeoutMs = 20_000): Promise<KillRun> {
|
|
82
|
+
const marker = join(mailDir, `paused.${killAt}`);
|
|
83
|
+
const child = spawn(process.execPath, [CHILD, mailDir, AGENT], {
|
|
84
|
+
env: {
|
|
85
|
+
HOME: mailDir,
|
|
86
|
+
PATH: process.env.PATH,
|
|
87
|
+
TPS_MAIL_MODULE: MODULE,
|
|
88
|
+
TPS_DRIVE: "deliverNext",
|
|
89
|
+
TPS_KILL_AT: killAt,
|
|
90
|
+
...(after ? { TPS_KILL_AFTER: "1" } : {}),
|
|
91
|
+
TPS_KILL_MARKER: marker,
|
|
92
|
+
TPS_MAIL_ROOT: mailboxRoot,
|
|
93
|
+
TPS_SRC: source,
|
|
94
|
+
TPS_CUR: curPath(),
|
|
95
|
+
TPS_FROM: FROM,
|
|
96
|
+
TPS_SEED: SEED.toString("base64"),
|
|
97
|
+
},
|
|
98
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
99
|
+
});
|
|
100
|
+
return new Promise<KillRun>((resolve, reject) => {
|
|
101
|
+
let stderr = "";
|
|
102
|
+
let settled = false;
|
|
103
|
+
child.stderr.on("data", (d) => {
|
|
104
|
+
stderr += d.toString();
|
|
105
|
+
});
|
|
106
|
+
const stop = () => {
|
|
107
|
+
clearInterval(poll);
|
|
108
|
+
clearTimeout(deadline);
|
|
109
|
+
settled = true;
|
|
110
|
+
};
|
|
111
|
+
const poll = setInterval(() => {
|
|
112
|
+
if (existsSync(marker)) {
|
|
113
|
+
clearInterval(poll);
|
|
114
|
+
child.kill("SIGKILL");
|
|
115
|
+
}
|
|
116
|
+
}, 15);
|
|
117
|
+
const deadline = setTimeout(() => {
|
|
118
|
+
if (settled) return;
|
|
119
|
+
stop();
|
|
120
|
+
child.kill("SIGKILL");
|
|
121
|
+
reject(new Error(`child never reached ${killAt} (stderr: ${stderr})`));
|
|
122
|
+
}, timeoutMs);
|
|
123
|
+
child.once("error", (e) => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
stop();
|
|
126
|
+
reject(e);
|
|
127
|
+
});
|
|
128
|
+
child.once("exit", (_code, signal) => {
|
|
129
|
+
if (settled) return;
|
|
130
|
+
stop();
|
|
131
|
+
resolve({ signal, reached: existsSync(marker), stderr });
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const reasonOf = (result: DeliverNextResult) => (result.status === "deferred" ? result.reason : result.status);
|
|
137
|
+
|
|
138
|
+
async function assertDeliveredUntilCompleted(messageId: string): Promise<void> {
|
|
139
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
140
|
+
const first = await client.deliverNext();
|
|
141
|
+
if (first.status !== "delivered") throw new Error(`expected a delivery, got ${JSON.stringify(first)}`);
|
|
142
|
+
expect(first.delivery.message.verifiedEnvelope?.messageId).toBe(messageId);
|
|
143
|
+
expect(reasonOf(await client.deliverNext())).toBe("active");
|
|
144
|
+
expect(client.releaseDelivery(first.delivery)).toEqual({ status: "released" });
|
|
145
|
+
const second = await client.deliverNext();
|
|
146
|
+
if (second.status !== "delivered") throw new Error(`expected a delivery, got ${JSON.stringify(second)}`);
|
|
147
|
+
expect(second.delivery.message.verifiedEnvelope?.messageId).toBe(messageId);
|
|
148
|
+
expect(await client.completeDelivery(second.delivery)).toEqual({ status: "completed" });
|
|
149
|
+
expect(await client.deliverNext()).toEqual({ status: "idle" });
|
|
150
|
+
expect(existsSync(newPath())).toBe(false);
|
|
151
|
+
expect(jsonFiles(join(mailboxRoot, "cur"))).toEqual(["m1.json"]);
|
|
152
|
+
expect(jsonFiles(join(mailboxRoot, "dlq"))).toEqual([]);
|
|
153
|
+
expect(hasCommittedMessageId(mailboxRoot, messageId)).toBe(true);
|
|
154
|
+
expect(
|
|
155
|
+
readdirSync(mailboxRoot).some((n) => n.startsWith(".placement-") && n.endsWith(".json")),
|
|
156
|
+
).toBe(false);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const CASES: Array<{ at: string; after?: boolean }> = [
|
|
160
|
+
{ at: "intent-write" },
|
|
161
|
+
{ at: "link-to-cur" },
|
|
162
|
+
{ at: "source-removal" },
|
|
163
|
+
{ at: "ledger-commit" },
|
|
164
|
+
{ at: "phase-write", after: true },
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
describe("MailClient recovery at the tested placement boundaries (cli#600)", () => {
|
|
168
|
+
for (const { at, after } of CASES) {
|
|
169
|
+
test(`recovers, releases, re-presents and completes after a kill at ${at}`, async () => {
|
|
170
|
+
const messageId = `kill-${at}`;
|
|
171
|
+
const source = plant(messageId);
|
|
172
|
+
const run = await runAndKillAt(at, source, after);
|
|
173
|
+
expect(run.reached, `child never reached ${at}; stderr=${run.stderr}`).toBe(true);
|
|
174
|
+
expect(run.signal).toBe("SIGKILL");
|
|
175
|
+
await assertDeliveredUntilCompleted(messageId);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
for (const at of ["link-to-cur", "source-removal"]) {
|
|
181
|
+
test(`a kill after ${at} leaves the original message deliverable`, async () => {
|
|
182
|
+
const messageId = `after-${at}`;
|
|
183
|
+
const run = await runAndKillAt(at, plant(messageId), true);
|
|
184
|
+
expect(run.reached, run.stderr).toBe(true);
|
|
185
|
+
expect(run.signal).toBe("SIGKILL");
|
|
186
|
+
await assertDeliveredUntilCompleted(messageId);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test(`a different-byte replacement after a kill after ${at} is withheld`, async () => {
|
|
190
|
+
const run = await runAndKillAt(at, plant(`replaced-${at}`), true);
|
|
191
|
+
expect(run.reached, run.stderr).toBe(true);
|
|
192
|
+
expect(run.signal).toBe("SIGKILL");
|
|
193
|
+
const name = readdirSync(mailboxRoot).find((name) => name.startsWith(".placement-") && name.endsWith(".json"))!;
|
|
194
|
+
const intent = readFileSync(join(mailboxRoot, name));
|
|
195
|
+
const bytes = readFileSync(curPath(), "utf8");
|
|
196
|
+
unlinkSync(curPath());
|
|
197
|
+
writeFileSync(curPath(), bytes + " ");
|
|
198
|
+
const c = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
199
|
+
expect(await c.deliverNext()).toMatchObject({ status: "deferred", reason: "record-changed" });
|
|
200
|
+
expect(readFileSync(join(mailboxRoot, name))).toEqual(intent);
|
|
201
|
+
expect(readFileSync(curPath(), "utf8")).toBe(bytes + " ");
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
describe("pending record classification", () => {
|
|
206
|
+
function plantLater(): void {
|
|
207
|
+
renameSync(plant("later-mail"), join(mailboxRoot, "new", "z-later.json"));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function deliverLater(client: MailClient): Promise<void> {
|
|
211
|
+
const later = await client.deliverNext();
|
|
212
|
+
expect(later.status).toBe("delivered");
|
|
213
|
+
if (later.status !== "delivered") throw new Error(JSON.stringify(later));
|
|
214
|
+
expect(later.delivery.message.verifiedEnvelope?.messageId).toBe("later-mail");
|
|
215
|
+
expect(await client.completeDelivery(later.delivery)).toEqual({ status: "completed" });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
test("a raw wrapper with an extra envelope object recovers after a kill before the phase write", async () => {
|
|
219
|
+
plantLater();
|
|
220
|
+
const source = plant("raw-extra-envelope");
|
|
221
|
+
const raw = { ...JSON.parse(readFileSync(source, "utf8")), envelope: { extra: true } };
|
|
222
|
+
writeFileSync(source, JSON.stringify(raw));
|
|
223
|
+
const run = await runAndKillAt("phase-write", source);
|
|
224
|
+
expect(run.reached, run.stderr).toBe(true);
|
|
225
|
+
expect(run.signal).toBe("SIGKILL");
|
|
226
|
+
expect(mailboxReplayStore(mailboxRoot).placementPhase("m1.json")).toBe("placing");
|
|
227
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
228
|
+
const recovered = await client.deliverNext();
|
|
229
|
+
expect(recovered.status).toBe("delivered");
|
|
230
|
+
if (recovered.status !== "delivered") throw new Error(JSON.stringify(recovered));
|
|
231
|
+
expect(recovered.delivery.message.verifiedEnvelope?.messageId).toBe("raw-extra-envelope");
|
|
232
|
+
expect(JSON.parse(recovered.delivery.message.body)).toEqual(raw);
|
|
233
|
+
expect(await client.completeDelivery(recovered.delivery)).toEqual({ status: "completed" });
|
|
234
|
+
expect(mailboxReplayStore(mailboxRoot).placementPhase("m1.json")).toBeNull();
|
|
235
|
+
await deliverLater(client);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test.each([null, {}, { envelope: {} }, { body: "plain text", envelopeId: "self-asserted" }])(
|
|
239
|
+
"a record matching neither kind is quarantined and its intent cleared: %j", async (record) => {
|
|
240
|
+
plantLater();
|
|
241
|
+
const source = plant("invalid-pending");
|
|
242
|
+
writeFileSync(source, JSON.stringify(record));
|
|
243
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
244
|
+
const replay = mailboxReplayStore(mailboxRoot);
|
|
245
|
+
renameSync(source, curPath());
|
|
246
|
+
replay.beginPlacement("invalid-pending", "m1.json", curPath());
|
|
247
|
+
expect(await client.deliverNext()).toMatchObject({ status: "deferred", reason: "pending-record-invalid" });
|
|
248
|
+
expect(replay.placementPhase("m1.json")).toBeNull();
|
|
249
|
+
expect(existsSync(curPath())).toBe(false);
|
|
250
|
+
expect(JSON.parse(readFileSync(join(mailboxRoot, "dlq", "m1.json"), "utf8"))).toEqual(record);
|
|
251
|
+
expect(readFileSync(join(mailboxRoot, "dlq", "m1.json.reason"), "utf8")).toContain("Reason: pending-record-invalid");
|
|
252
|
+
await deliverLater(client);
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
test.each(["both", "raw", "cli"])("overlapping shapes select by verification: %s", async (verified) => {
|
|
257
|
+
plantLater();
|
|
258
|
+
const source = plant("raw-inner");
|
|
259
|
+
const raw = JSON.parse(readFileSync(source, "utf8"));
|
|
260
|
+
plant("cli-outer", raw.body);
|
|
261
|
+
const envelope = JSON.parse(JSON.parse(readFileSync(source, "utf8")).body);
|
|
262
|
+
const record = {
|
|
263
|
+
id: "record-id", from: FROM, to: AGENT, body: raw.body, timestamp: envelope.timestamp,
|
|
264
|
+
read: false, envelopeId: envelope.messageId, envelope,
|
|
265
|
+
trustTier: "internal", checkedOutAt: new Date().toISOString(), checkedOutBy: AGENT, deliveryAttempts: 1,
|
|
266
|
+
};
|
|
267
|
+
if (verified === "raw") record.envelope.signature = "invalid";
|
|
268
|
+
if (verified === "cli") {
|
|
269
|
+
const inner = JSON.parse(record.body);
|
|
270
|
+
inner.signature = "invalid";
|
|
271
|
+
record.body = JSON.stringify(inner);
|
|
272
|
+
plant("cli-outer", record.body);
|
|
273
|
+
record.envelope = JSON.parse(JSON.parse(readFileSync(source, "utf8")).body);
|
|
274
|
+
record.timestamp = record.envelope.timestamp;
|
|
275
|
+
}
|
|
276
|
+
writeFileSync(source, JSON.stringify(record));
|
|
277
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
278
|
+
const replay = mailboxReplayStore(mailboxRoot);
|
|
279
|
+
renameSync(source, curPath());
|
|
280
|
+
replay.beginPlacement(verified === "cli" ? "cli-outer" : "raw-inner", "m1.json", curPath());
|
|
281
|
+
const result = await client.deliverNext();
|
|
282
|
+
if (verified === "both") {
|
|
283
|
+
expect(result).toMatchObject({ status: "deferred", reason: "pending-record-ambiguous" });
|
|
284
|
+
expect(replay.placementPhase("m1.json")).toBeNull();
|
|
285
|
+
expect(readFileSync(join(mailboxRoot, "dlq", "m1.json.reason"), "utf8")).toContain("Reason: pending-record-ambiguous");
|
|
286
|
+
await deliverLater(client);
|
|
287
|
+
} else if (verified === "raw") {
|
|
288
|
+
expect(result.status).toBe("delivered");
|
|
289
|
+
if (result.status !== "delivered") throw new Error(JSON.stringify(result));
|
|
290
|
+
expect(result.delivery.message.verifiedEnvelope?.messageId).toBe("raw-inner");
|
|
291
|
+
expect(await client.completeDelivery(result.delivery)).toEqual({ status: "completed" });
|
|
292
|
+
await deliverLater(client);
|
|
293
|
+
} else {
|
|
294
|
+
expect(result).toMatchObject({ status: "deferred", reason: "cli-record" });
|
|
295
|
+
expect(replay.placementPhase("m1.json")).toBe("placing");
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("MailClient defers unresolved placements", () => {
|
|
301
|
+
test("a mismatched cur copy and same-id new copy leave the pending intent unchanged", async () => {
|
|
302
|
+
const messageId = "mismatched-copy";
|
|
303
|
+
const source = plant(messageId);
|
|
304
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
305
|
+
mailboxReplayStore(mailboxRoot).beginPlacement(messageId, "m1.json", source);
|
|
306
|
+
const original = readFileSync(source);
|
|
307
|
+
plant("unrelated-cur-copy");
|
|
308
|
+
copyFileSync(source, curPath());
|
|
309
|
+
writeFileSync(source, original);
|
|
310
|
+
const intentPath = join(mailboxRoot, readdirSync(mailboxRoot).find((n) => n.startsWith(".placement-") && n.endsWith(".json"))!);
|
|
311
|
+
const intent = readFileSync(intentPath);
|
|
312
|
+
|
|
313
|
+
// The cur/ copy verifies, but as another message: deferred by name, twice, unchanged.
|
|
314
|
+
expect(reasonOf(await client.deliverNext())).toBe("verification-failed");
|
|
315
|
+
expect(reasonOf(await client.deliverNext())).toBe("verification-failed");
|
|
316
|
+
expect(readFileSync(intentPath).equals(intent)).toBe(true);
|
|
317
|
+
expect(existsSync(source)).toBe(true);
|
|
318
|
+
expect(hasCommittedMessageId(mailboxRoot, messageId)).toBe(false);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("different invalid UTF-8 bytes with equal decoded text defer recovery", async () => {
|
|
322
|
+
const messageId = "changed-raw-bytes";
|
|
323
|
+
const source = plant(messageId);
|
|
324
|
+
const record = readFileSync(source);
|
|
325
|
+
const bytes = (byte: number) => Buffer.concat([
|
|
326
|
+
Buffer.from('{"transport":"'), Buffer.from([byte]), Buffer.from('",'), record.subarray(1),
|
|
327
|
+
]);
|
|
328
|
+
const original = bytes(0x80);
|
|
329
|
+
const replacement = bytes(0x81);
|
|
330
|
+
expect(original.equals(replacement)).toBe(false);
|
|
331
|
+
expect(original.toString("utf8")).toBe(replacement.toString("utf8"));
|
|
332
|
+
writeFileSync(source, original);
|
|
333
|
+
mkdirSync(join(mailboxRoot, "cur"));
|
|
334
|
+
renameSync(source, curPath());
|
|
335
|
+
mailboxReplayStore(mailboxRoot).beginPlacement(messageId, "m1.json", curPath());
|
|
336
|
+
const normal = verifier();
|
|
337
|
+
let changed = false;
|
|
338
|
+
const client = new MailClient(mailDir, undefined, AGENT, {
|
|
339
|
+
async getAgent(name: string) {
|
|
340
|
+
if (!changed) {
|
|
341
|
+
changed = true;
|
|
342
|
+
writeFileSync(curPath(), replacement);
|
|
343
|
+
}
|
|
344
|
+
return normal.getAgent(name);
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
expect(reasonOf(await client.deliverNext())).toBe("record-changed");
|
|
349
|
+
expect(mailboxReplayStore(mailboxRoot).hasPendingPlacement(messageId, "m1.json")).toBe(true);
|
|
350
|
+
expect(hasCommittedMessageId(mailboxRoot, messageId)).toBe(false);
|
|
351
|
+
expect(reasonOf(await client.deliverNext())).toBe("record-changed");
|
|
352
|
+
expect(mailboxReplayStore(mailboxRoot).hasPendingPlacement(messageId, "m1.json")).toBe(true);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("an older intent without identity with only a cur copy is withheld", async () => {
|
|
356
|
+
const messageId = "older-intent";
|
|
357
|
+
const source = plant(messageId);
|
|
358
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
359
|
+
mailboxReplayStore(mailboxRoot).beginPlacement(messageId, "m1.json");
|
|
360
|
+
renameSync(source, curPath());
|
|
361
|
+
expect(reasonOf(await client.deliverNext())).toBe("record-changed");
|
|
362
|
+
expect(mailboxReplayStore(mailboxRoot).hasPendingPlacement(messageId, "m1.json")).toBe(true);
|
|
363
|
+
expect(hasCommittedMessageId(mailboxRoot, messageId)).toBe(false);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("absent-copy cleanup preserves an intent while a mismatched cur copy exists", async () => {
|
|
367
|
+
const messageId = "appeared-copy";
|
|
368
|
+
const source = plant(messageId);
|
|
369
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
370
|
+
mailboxReplayStore(mailboxRoot).beginPlacement(messageId, "m1.json", source);
|
|
371
|
+
copyFileSync(source, curPath());
|
|
372
|
+
await (client as unknown as { finishUnlinkedPlacement(id: string, file: string, root: string): Promise<void> })
|
|
373
|
+
.finishUnlinkedPlacement(messageId, "m1.json", mailboxLockRoot(mailDir, AGENT));
|
|
374
|
+
expect(mailboxReplayStore(mailboxRoot).hasPendingPlacement(messageId, "m1.json")).toBe(true);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("lock-acquisition failure leaves a cur-only placement pending", async () => {
|
|
378
|
+
const messageId = "lock-failure";
|
|
379
|
+
const source = plant(messageId);
|
|
380
|
+
const client = new MailClient(mailDir, undefined, AGENT, verifier());
|
|
381
|
+
renameSync(source, curPath());
|
|
382
|
+
mailboxReplayStore(mailboxRoot).beginPlacement(messageId, "m1.json", curPath());
|
|
383
|
+
const lock = await acquireMailLock(realpathSync(mailboxRoot));
|
|
384
|
+
expect(lock).not.toBeNull();
|
|
385
|
+
try {
|
|
386
|
+
expect(reasonOf(await client.deliverNext())).toBe("lock-busy");
|
|
387
|
+
expect(mailboxReplayStore(mailboxRoot).hasPendingPlacement(messageId, "m1.json")).toBe(true);
|
|
388
|
+
} finally {
|
|
389
|
+
lock?.release();
|
|
390
|
+
}
|
|
391
|
+
await assertDeliveredUntilCompleted(messageId);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cli#677 — pidState, the pid check the mail lock and the launch-snapshot sweep
|
|
3
|
+
* share, against real processes.
|
|
4
|
+
*/
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
6
|
+
import { type ChildProcess, spawn, spawnSync } from "node:child_process";
|
|
7
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { spawnLiveProcess } from "../../../test/helpers/live-process.js";
|
|
11
|
+
import {
|
|
12
|
+
acquireMailLockSync,
|
|
13
|
+
mailLockPath,
|
|
14
|
+
pidState,
|
|
15
|
+
pidStateFromProbeError,
|
|
16
|
+
processIsGone,
|
|
17
|
+
processStartToken,
|
|
18
|
+
} from "../src/lib/mail-lock.js";
|
|
19
|
+
|
|
20
|
+
/** A pid that has exited and been reaped by this process. */
|
|
21
|
+
function reapedPid(): number {
|
|
22
|
+
const result = spawnSync(process.execPath, ["-e", ""], { timeout: 10_000 });
|
|
23
|
+
expect(result.pid).toBeGreaterThan(0);
|
|
24
|
+
return result.pid as number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A pid state read from a Linux `/proc/<pid>/stat` line. The `comm` field may
|
|
28
|
+
* contain spaces and parentheses, so the fields are parsed after the last `)`. */
|
|
29
|
+
function procStatState(pid: number): string | null {
|
|
30
|
+
try {
|
|
31
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf-8");
|
|
32
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[0] || null;
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Spawn a child that exits immediately, then block this process's event loop
|
|
40
|
+
* until its SIGCHLD is pending. The child is then a zombie that `kill(pid, 0)`
|
|
41
|
+
* still answers, because the parent has not reaped it.
|
|
42
|
+
*/
|
|
43
|
+
function unreapedZombie(): { child: ChildProcess; pid: number } {
|
|
44
|
+
const child = spawn(process.execPath, ["-e", ""], { stdio: "ignore" });
|
|
45
|
+
const pid = child.pid as number;
|
|
46
|
+
expect(pid).toBeGreaterThan(0);
|
|
47
|
+
const deadline = Date.now() + 10_000;
|
|
48
|
+
while (procStatState(pid) !== "Z") {
|
|
49
|
+
if (Date.now() > deadline) throw new Error(`pid ${pid} did not become a zombie`);
|
|
50
|
+
// spawnSync blocks the event loop, so this process cannot reap the child.
|
|
51
|
+
spawnSync(process.execPath, ["-e", "setTimeout(() => {}, 50)"], { timeout: 10_000 });
|
|
52
|
+
}
|
|
53
|
+
return { child, pid };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A running child process and its start token. */
|
|
57
|
+
async function liveProcess(): Promise<{ child: ChildProcess; pid: number; token: string }> {
|
|
58
|
+
const child = spawnLiveProcess();
|
|
59
|
+
const pid = child.pid as number;
|
|
60
|
+
expect(pid).toBeGreaterThan(0);
|
|
61
|
+
let token: string | null = null;
|
|
62
|
+
const deadline = Date.now() + 5_000;
|
|
63
|
+
while (token === null && Date.now() < deadline) {
|
|
64
|
+
token = processStartToken(pid);
|
|
65
|
+
if (token === null) await new Promise((resolveWait) => setTimeout(resolveWait, 20));
|
|
66
|
+
}
|
|
67
|
+
expect(token).not.toBeNull();
|
|
68
|
+
return { child, pid, token: token as string };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("pidState", () => {
|
|
72
|
+
test("a pid above 2147483647 is invalid", () => {
|
|
73
|
+
for (const pid of [2_147_483_648, 4_294_967_297, 2 ** 53]) {
|
|
74
|
+
expect(pidState(pid), String(pid)).toBe("invalid");
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a pid of 0, a negative pid and a fractional pid are invalid", () => {
|
|
79
|
+
for (const pid of [0, -1, 1.5, Number.NaN]) {
|
|
80
|
+
expect(pidState(pid), String(pid)).toBe("invalid");
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a running process is alive, and so is pid 1", async () => {
|
|
85
|
+
const { child, pid } = await liveProcess();
|
|
86
|
+
try {
|
|
87
|
+
expect(pidState(pid)).toBe("alive");
|
|
88
|
+
expect(pidState(process.pid)).toBe("alive");
|
|
89
|
+
// pid 1 always exists; when this process may not signal it, the probe fails with EPERM.
|
|
90
|
+
expect(pidState(1)).toBe("alive");
|
|
91
|
+
} finally {
|
|
92
|
+
child.kill("SIGKILL");
|
|
93
|
+
}
|
|
94
|
+
}, 15_000);
|
|
95
|
+
|
|
96
|
+
test("an exited and reaped process is gone", () => {
|
|
97
|
+
expect(pidState(reapedPid())).toBe("gone");
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("pidState on Linux", () => {
|
|
102
|
+
// A zombie's state is visible in /proc/<pid>/stat; elsewhere the
|
|
103
|
+
// kill(pid, 0) answer is the whole probe, so these cases skip there.
|
|
104
|
+
const children: ChildProcess[] = [];
|
|
105
|
+
|
|
106
|
+
afterEach(async () => {
|
|
107
|
+
for (const child of children.splice(0)) {
|
|
108
|
+
if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
|
|
109
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
110
|
+
await Promise.race([
|
|
111
|
+
new Promise<void>((resolve) => child.once("exit", () => resolve())),
|
|
112
|
+
new Promise<void>((resolve) => setTimeout(resolve, 2_000)),
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test.skipIf(process.platform !== "linux")(
|
|
119
|
+
"an exited child that is not reaped is gone",
|
|
120
|
+
() => {
|
|
121
|
+
const { child, pid } = unreapedZombie();
|
|
122
|
+
children.push(child);
|
|
123
|
+
expect(procStatState(pid)).toBe("Z");
|
|
124
|
+
expect(pidState(pid)).toBe("gone");
|
|
125
|
+
// processIsGone with the owner's real start token, and the lock-owner
|
|
126
|
+
// reclaim, inherit the classifier.
|
|
127
|
+
const token = processStartToken(pid);
|
|
128
|
+
expect(token).not.toBeNull();
|
|
129
|
+
expect(processIsGone(pid, token)).toBe(true);
|
|
130
|
+
// acquireMailLockSync is synchronous, so the child stays unreaped.
|
|
131
|
+
const root = mkdtempSync(join(tmpdir(), "mail-lock-zombie-"));
|
|
132
|
+
try {
|
|
133
|
+
mkdirSync(mailLockPath(root));
|
|
134
|
+
writeFileSync(
|
|
135
|
+
join(mailLockPath(root), "owner.json"),
|
|
136
|
+
JSON.stringify({ pid, startToken: token, nonce: "planted" }),
|
|
137
|
+
);
|
|
138
|
+
const lock = acquireMailLockSync(root, { timeoutMs: 2_000 });
|
|
139
|
+
expect(lock).not.toBeNull();
|
|
140
|
+
lock?.release();
|
|
141
|
+
} finally {
|
|
142
|
+
rmSync(root, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
15_000,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
test.skipIf(process.platform !== "linux")(
|
|
149
|
+
"a live child is still alive",
|
|
150
|
+
() => {
|
|
151
|
+
const child = spawnLiveProcess();
|
|
152
|
+
children.push(child);
|
|
153
|
+
expect(pidState(child.pid as number)).toBe("alive");
|
|
154
|
+
},
|
|
155
|
+
15_000,
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
test.skipIf(process.platform !== "linux")(
|
|
159
|
+
"a failed stat read keeps alive",
|
|
160
|
+
() => {
|
|
161
|
+
const child = spawnLiveProcess();
|
|
162
|
+
children.push(child);
|
|
163
|
+
const pid = child.pid as number;
|
|
164
|
+
expect(pidState(pid, () => null)).toBe("alive");
|
|
165
|
+
expect(pidState(pid, () => [])).toBe("alive");
|
|
166
|
+
},
|
|
167
|
+
15_000,
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("pidStateFromProbeError", () => {
|
|
172
|
+
// Errors carrying the codes process.kill raises, so EPERM is covered on any host.
|
|
173
|
+
test("classifies EPERM as alive, ESRCH as gone, an argument error as invalid, and any other error as alive", () => {
|
|
174
|
+
const withCode = (code: string) => Object.assign(new Error(code), { code });
|
|
175
|
+
expect(pidStateFromProbeError(withCode("EPERM"))).toBe("alive");
|
|
176
|
+
expect(pidStateFromProbeError(withCode("ESRCH"))).toBe("gone");
|
|
177
|
+
for (const code of ["ERR_INVALID_ARG_TYPE", "ERR_OUT_OF_RANGE", "EINVAL", "ERANGE"]) {
|
|
178
|
+
expect(pidStateFromProbeError(withCode(code)), code).toBe("invalid");
|
|
179
|
+
}
|
|
180
|
+
expect(pidStateFromProbeError(withCode("EIO"))).toBe("alive");
|
|
181
|
+
expect(pidStateFromProbeError(new Error("no code"))).toBe("alive");
|
|
182
|
+
expect(pidStateFromProbeError(null)).toBe("alive");
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("mail lock owner pid", () => {
|
|
187
|
+
let root: string;
|
|
188
|
+
beforeEach(() => {
|
|
189
|
+
root = mkdtempSync(join(tmpdir(), "mail-lock-pid-"));
|
|
190
|
+
});
|
|
191
|
+
afterEach(() => {
|
|
192
|
+
rmSync(root, { recursive: true, force: true });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
function plantLock(owner: Record<string, unknown>): string {
|
|
196
|
+
const ownerFile = join(mailLockPath(root), "owner.json");
|
|
197
|
+
mkdirSync(mailLockPath(root));
|
|
198
|
+
writeFileSync(ownerFile, JSON.stringify(owner));
|
|
199
|
+
return ownerFile;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
test("a lock whose owner pid is above 2147483647 is not reclaimed", () => {
|
|
203
|
+
const owner = { pid: 2_147_483_648, startToken: processStartToken(process.pid), nonce: "planted" };
|
|
204
|
+
const ownerFile = plantLock(owner);
|
|
205
|
+
|
|
206
|
+
expect(acquireMailLockSync(root, { timeoutMs: 50 })).toBeNull();
|
|
207
|
+
expect(JSON.parse(readFileSync(ownerFile, "utf-8"))).toEqual(owner);
|
|
208
|
+
expect(processIsGone(owner.pid, owner.startToken)).toBe(false);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test("a lock whose owner is gone is reclaimed", () => {
|
|
212
|
+
const pid = reapedPid();
|
|
213
|
+
plantLock({ pid, startToken: "proc:1", nonce: "planted" });
|
|
214
|
+
expect(processIsGone(pid, "proc:1")).toBe(true);
|
|
215
|
+
|
|
216
|
+
const lock = acquireMailLockSync(root, { timeoutMs: 2_000 });
|
|
217
|
+
try {
|
|
218
|
+
expect(lock).not.toBeNull();
|
|
219
|
+
expect(JSON.parse(readFileSync(join(mailLockPath(root), "owner.json"), "utf-8")).pid).toBe(process.pid);
|
|
220
|
+
} finally {
|
|
221
|
+
lock?.release();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("a lock held by a running process is not reclaimed", async () => {
|
|
226
|
+
const { child, pid, token } = await liveProcess();
|
|
227
|
+
try {
|
|
228
|
+
const owner = { pid, startToken: token, nonce: "planted" };
|
|
229
|
+
const ownerFile = plantLock(owner);
|
|
230
|
+
|
|
231
|
+
expect(acquireMailLockSync(root, { timeoutMs: 50 })).toBeNull();
|
|
232
|
+
expect(JSON.parse(readFileSync(ownerFile, "utf-8"))).toEqual(owner);
|
|
233
|
+
expect(processIsGone(pid, token)).toBe(false);
|
|
234
|
+
} finally {
|
|
235
|
+
child.kill("SIGKILL");
|
|
236
|
+
}
|
|
237
|
+
}, 15_000);
|
|
238
|
+
});
|