@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,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mail-client-lock-namespace.test.ts — cli#616.
|
|
3
|
+
*
|
|
4
|
+
* MailClient must take its acceptance stripe from its configured `mailDir`, not
|
|
5
|
+
* from the process environment, which is set here to a different root. The
|
|
6
|
+
* stripe holder reaches the store through a symlink and MailClient through the
|
|
7
|
+
* store itself; that pair reaches one filesystem lock either way. The namespace
|
|
8
|
+
* case checks that the derived lock root strings are equal as well.
|
|
9
|
+
*/
|
|
10
|
+
import { afterEach, beforeEach, expect, test } from "bun:test";
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { MailClient } from "../src/io/mail.js";
|
|
16
|
+
import { relayAcceptanceLockRoot } from "../src/lib/relay-lock.js";
|
|
17
|
+
import { canonicalMailRoot, mailboxLockRoot } from "../src/lib/mail-lock-namespace.js";
|
|
18
|
+
import { senderKeyResolver, signedRelayRecord } from "./helpers/signed-relay-record.js";
|
|
19
|
+
|
|
20
|
+
const AGENT = "mailbox";
|
|
21
|
+
const BRANCH = "remote";
|
|
22
|
+
const ID = "reserved";
|
|
23
|
+
|
|
24
|
+
let root: string;
|
|
25
|
+
let store: string;
|
|
26
|
+
let alias: string;
|
|
27
|
+
let savedEnv: Record<string, string | undefined>;
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
root = mkdtempSync(join(tmpdir(), "mail-client-lock-namespace-"));
|
|
31
|
+
store = join(root, "store");
|
|
32
|
+
mkdirSync(store, { recursive: true });
|
|
33
|
+
alias = join(root, "alias");
|
|
34
|
+
symlinkSync(store, alias, "dir");
|
|
35
|
+
savedEnv = {};
|
|
36
|
+
for (const key of ["HOME", "TPS_MAIL_DIR"]) savedEnv[key] = process.env[key];
|
|
37
|
+
process.env.HOME = root;
|
|
38
|
+
// The MailClient environment names a path that is NOT the store: only the
|
|
39
|
+
// configured mailDir does.
|
|
40
|
+
process.env.TPS_MAIL_DIR = join(root, "elsewhere");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
for (const [key, value] of Object.entries(savedEnv)) {
|
|
45
|
+
if (value === undefined) delete process.env[key];
|
|
46
|
+
else process.env[key] = value;
|
|
47
|
+
}
|
|
48
|
+
rmSync(root, { recursive: true, force: true });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function client(): MailClient {
|
|
52
|
+
return new MailClient(store, undefined, AGENT, senderKeyResolver());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Write `raw` as a source record in the mailbox's new/ directory. */
|
|
56
|
+
function seedSource(file: string, raw: string): string {
|
|
57
|
+
const dir = join(store, AGENT, "new");
|
|
58
|
+
mkdirSync(dir, { recursive: true });
|
|
59
|
+
const source = join(dir, file);
|
|
60
|
+
writeFileSync(source, raw);
|
|
61
|
+
return source;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A relayed, unverifiable source record in the mailbox's new/ directory. */
|
|
65
|
+
function seedRelayedSource(): string {
|
|
66
|
+
return seedSource("dl.json", JSON.stringify({ from: "sender", body: "invalid", relayDelivery: { branchId: BRANCH, id: ID } }));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Hold the acceptance stripe through `mailRoot`, as CLI relay acceptance does. */
|
|
70
|
+
function holdStripe(mailRoot: string): Promise<{ release: () => Promise<void> }> {
|
|
71
|
+
const script = join(root, "holder.ts");
|
|
72
|
+
writeFileSync(
|
|
73
|
+
script,
|
|
74
|
+
`
|
|
75
|
+
const { acquireMailLockSync } = await import(${JSON.stringify(new URL("../src/lib/mail-lock.js", import.meta.url).href)});
|
|
76
|
+
const { relayAcceptanceLockRoot } = await import(${JSON.stringify(new URL("../src/lib/relay-lock.js", import.meta.url).href)});
|
|
77
|
+
const lock = acquireMailLockSync(relayAcceptanceLockRoot(process.env.BRANCH, process.env.ID));
|
|
78
|
+
if (!lock) throw new Error("holder could not acquire");
|
|
79
|
+
process.stdout.write("ready");
|
|
80
|
+
process.stdin.once("data", () => { lock.release(); process.exit(0); });
|
|
81
|
+
`,
|
|
82
|
+
);
|
|
83
|
+
const child = spawn("bun", [script], {
|
|
84
|
+
env: { ...process.env, TPS_MAIL_DIR: mailRoot, BRANCH, ID },
|
|
85
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
86
|
+
});
|
|
87
|
+
const exited = new Promise<number | null>((resolve) => child.once("exit", resolve));
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
child.once("error", reject);
|
|
90
|
+
child.once("exit", (code) => reject(new Error(`holder exited ${code} before ready`)));
|
|
91
|
+
child.stdout.once("data", (chunk: Buffer) => {
|
|
92
|
+
if (chunk.toString() === "ready") {
|
|
93
|
+
resolve({ release: async () => { child.stdin.end("release"); await exited; } });
|
|
94
|
+
} else reject(new Error(`holder printed ${JSON.stringify(chunk.toString())}`));
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
test("MailClient leaves a relayed dead-letter source until acceptance releases the stripe", async () => {
|
|
100
|
+
const source = seedRelayedSource();
|
|
101
|
+
const mail = client();
|
|
102
|
+
const holder = await holdStripe(alias);
|
|
103
|
+
try {
|
|
104
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
105
|
+
expect(existsSync(source)).toBe(true);
|
|
106
|
+
expect(existsSync(join(store, AGENT, "dlq", "dl.json"))).toBe(false);
|
|
107
|
+
} finally {
|
|
108
|
+
await holder.release();
|
|
109
|
+
}
|
|
110
|
+
expect(await mail.checkNewMail()).toHaveLength(0);
|
|
111
|
+
expect(existsSync(join(store, AGENT, "dlq", "dl.json"))).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("MailClient leaves a valid relayed record in new/ until acceptance releases the stripe, then promotes it", async () => {
|
|
115
|
+
const raw = signedRelayRecord(AGENT, "promoted", { branchId: BRANCH, id: ID });
|
|
116
|
+
const source = seedSource("pr.json", raw);
|
|
117
|
+
const promoted = join(store, AGENT, "cur", "pr.json");
|
|
118
|
+
const mail = client();
|
|
119
|
+
const holder = await holdStripe(alias);
|
|
120
|
+
try {
|
|
121
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
122
|
+
expect(readFileSync(source, "utf8")).toBe(raw);
|
|
123
|
+
expect(existsSync(promoted)).toBe(false);
|
|
124
|
+
} finally {
|
|
125
|
+
await holder.release();
|
|
126
|
+
}
|
|
127
|
+
expect((await mail.checkNewMail()).map((message) => message.filename)).toEqual(["pr.json"]);
|
|
128
|
+
expect(existsSync(source)).toBe(false);
|
|
129
|
+
expect(readFileSync(promoted, "utf8")).toBe(raw);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("the same path in the environment and the configured mailDir still shares the stripe", async () => {
|
|
133
|
+
const source = seedRelayedSource();
|
|
134
|
+
const mail = client();
|
|
135
|
+
const holder = await holdStripe(store);
|
|
136
|
+
try {
|
|
137
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
138
|
+
expect(existsSync(source)).toBe(true);
|
|
139
|
+
} finally {
|
|
140
|
+
await holder.release();
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("the stripe and mailbox lock root strings are equal for the store, whatever path reaches it", () => {
|
|
145
|
+
expect(relayAcceptanceLockRoot(BRANCH, ID, alias)).toBe(relayAcceptanceLockRoot(BRANCH, ID, store));
|
|
146
|
+
expect(mailboxLockRoot(alias, AGENT)).toBe(mailboxLockRoot(store, AGENT));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a mail root that does not exist yet is created, then resolved", () => {
|
|
150
|
+
const pending = join(alias, "pending", "store");
|
|
151
|
+
expect(existsSync(pending)).toBe(false);
|
|
152
|
+
// The temp directory may itself sit behind a symlink (macOS /var -> /private/var).
|
|
153
|
+
expect(canonicalMailRoot(pending)).toBe(join(realpathSync(store), "pending", "store"));
|
|
154
|
+
expect(existsSync(pending)).toBe(true);
|
|
155
|
+
});
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/** cli#636: canonical operation path strings across symlink changes. */
|
|
2
|
+
import { afterEach, beforeEach, expect, spyOn, test } from "bun:test";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { basename, join } from "node:path";
|
|
8
|
+
import * as ed from "@noble/ed25519";
|
|
9
|
+
import { MailClient, type MailMessage } from "../src/io/mail.js";
|
|
10
|
+
import { bridgePrincipalIds } from "../src/lib/bridge-identity.js";
|
|
11
|
+
import { acceptanceLockRoot, mailboxLockRoot } from "../src/lib/mail-lock-namespace.js";
|
|
12
|
+
import { hasCommittedMessageId, mailboxReplayStore } from "../src/lib/mailbox-policy.js";
|
|
13
|
+
import type { FlairClient } from "../src/lib/signEnvelope.js";
|
|
14
|
+
import { senderKeyResolver, signedRelayRecord } from "./helpers/signed-relay-record.js";
|
|
15
|
+
|
|
16
|
+
const AGENT = "mailbox";
|
|
17
|
+
const BRANCH = "remote";
|
|
18
|
+
// A wire-valid relayed delivery id (a UUID).
|
|
19
|
+
const ID = "6f1c2a3e-8d4b-4c5a-9e7f-1a2b3c4d5e6f";
|
|
20
|
+
|
|
21
|
+
let root: string;
|
|
22
|
+
let storeOld: string;
|
|
23
|
+
let storeNew: string;
|
|
24
|
+
let alias: string;
|
|
25
|
+
let savedEnv: Record<string, string | undefined>;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
root = mkdtempSync(join(tmpdir(), "mail-client-repoint-root-"));
|
|
29
|
+
storeOld = join(root, "store-old");
|
|
30
|
+
storeNew = join(root, "store-new");
|
|
31
|
+
mkdirSync(storeOld, { recursive: true });
|
|
32
|
+
mkdirSync(storeNew, { recursive: true });
|
|
33
|
+
alias = join(root, "mail");
|
|
34
|
+
symlinkSync(storeOld, alias, "dir");
|
|
35
|
+
// The two roots name different bridge principals: in the old root the sender
|
|
36
|
+
// is a bridge principal (trust tier "external"); in the new root it is not.
|
|
37
|
+
writeBridgePrincipal(storeOld, "sender");
|
|
38
|
+
writeBridgePrincipal(storeNew, "other-bridge");
|
|
39
|
+
savedEnv = {};
|
|
40
|
+
for (const key of ["HOME", "TPS_BRIDGE_AGENT_ID"]) savedEnv[key] = process.env[key];
|
|
41
|
+
process.env.HOME = root;
|
|
42
|
+
delete process.env.TPS_BRIDGE_AGENT_ID;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
for (const [key, value] of Object.entries(savedEnv)) {
|
|
47
|
+
if (value === undefined) delete process.env[key];
|
|
48
|
+
else process.env[key] = value;
|
|
49
|
+
}
|
|
50
|
+
rmSync(root, { recursive: true, force: true });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
function writeBridgePrincipal(store: string, id: string): void {
|
|
54
|
+
mkdirSync(join(store, ".bridge-principals"), { recursive: true });
|
|
55
|
+
writeFileSync(join(store, ".bridge-principals", `${id}.json`), JSON.stringify({ id }));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Point the symlink `link` at `target`. */
|
|
59
|
+
function repoint(link: string, target: string): void {
|
|
60
|
+
unlinkSync(link);
|
|
61
|
+
symlinkSync(target, link, "dir");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Write `raw` as `file` in `<store>/<AGENT>/new` and return its path. */
|
|
65
|
+
function seed(store: string, file: string, raw: string): string {
|
|
66
|
+
mkdirSync(join(store, AGENT, "new"), { recursive: true });
|
|
67
|
+
const source = join(store, AGENT, "new", file);
|
|
68
|
+
writeFileSync(source, raw);
|
|
69
|
+
return source;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A verifier whose first key lookup pauses until `resume()`; later lookups do
|
|
74
|
+
* not pause. With `wrongKey` every lookup returns a key the record was not
|
|
75
|
+
* signed with, so verification rejects the record.
|
|
76
|
+
*/
|
|
77
|
+
function pausedVerifier(opts: { wrongKey?: boolean } = {}) {
|
|
78
|
+
const resolver = senderKeyResolver();
|
|
79
|
+
let resume!: () => void;
|
|
80
|
+
const gate = new Promise<void>((resolve) => { resume = resolve; });
|
|
81
|
+
let entered!: () => void;
|
|
82
|
+
const paused = new Promise<void>((resolve) => { entered = resolve; });
|
|
83
|
+
let first = true;
|
|
84
|
+
const client: FlairClient = {
|
|
85
|
+
async getAgent() {
|
|
86
|
+
if (first) {
|
|
87
|
+
first = false;
|
|
88
|
+
entered();
|
|
89
|
+
await gate;
|
|
90
|
+
}
|
|
91
|
+
if (opts.wrongKey) return { publicKey: Buffer.from(ed.getPublicKey(Buffer.alloc(32, 2))) };
|
|
92
|
+
return resolver.getAgent();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return { client, paused, resume };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Run checkNewMail(), apply `change` while it is paused in verification, and return its result. */
|
|
99
|
+
async function checkWithChangeDuringVerification(
|
|
100
|
+
mail: MailClient,
|
|
101
|
+
verifier: ReturnType<typeof pausedVerifier>,
|
|
102
|
+
change: () => void,
|
|
103
|
+
): Promise<MailMessage[]> {
|
|
104
|
+
const pending = mail.checkNewMail();
|
|
105
|
+
await Promise.race([
|
|
106
|
+
verifier.paused,
|
|
107
|
+
pending.then(() => { throw new Error("the operation finished without reaching verification"); }),
|
|
108
|
+
]);
|
|
109
|
+
change();
|
|
110
|
+
verifier.resume();
|
|
111
|
+
return pending;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Hold the lock at `lockRoot` in a real second process and release it on stdin. */
|
|
115
|
+
function holdLock(lockRoot: string): Promise<{ release: () => Promise<void> }> {
|
|
116
|
+
const script = join(root, "holder.ts");
|
|
117
|
+
writeFileSync(
|
|
118
|
+
script,
|
|
119
|
+
`
|
|
120
|
+
const { mkdirSync } = await import("node:fs");
|
|
121
|
+
const { acquireMailLockSync } = await import(${JSON.stringify(new URL("../src/lib/mail-lock.js", import.meta.url).href)});
|
|
122
|
+
mkdirSync(process.env.HOLD_ROOT, { recursive: true });
|
|
123
|
+
const lock = acquireMailLockSync(process.env.HOLD_ROOT);
|
|
124
|
+
if (!lock) throw new Error("holder could not acquire");
|
|
125
|
+
process.stdout.write("ready");
|
|
126
|
+
process.stdin.once("data", () => { lock.release(); process.exit(0); });
|
|
127
|
+
`,
|
|
128
|
+
);
|
|
129
|
+
const child = spawn("bun", [script], { env: { ...process.env, HOLD_ROOT: lockRoot }, stdio: ["pipe", "pipe", "inherit"] });
|
|
130
|
+
const exited = new Promise<number | null>((resolve) => child.once("exit", resolve));
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
child.once("error", reject);
|
|
133
|
+
child.once("exit", (code) => reject(new Error(`holder exited ${code} before ready`)));
|
|
134
|
+
child.stdout.once("data", (chunk: Buffer) => {
|
|
135
|
+
if (chunk.toString() === "ready") {
|
|
136
|
+
resolve({ release: async () => { child.stdin.end("release"); await exited; } });
|
|
137
|
+
} else reject(new Error(`holder printed ${JSON.stringify(chunk.toString())}`));
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
test("a live client does not promote under the old root's lock, delivers the envelope once, and refuses its replay", async () => {
|
|
143
|
+
// The client is constructed while the alias points at the old store.
|
|
144
|
+
const mail = new MailClient(alias, undefined, AGENT, senderKeyResolver());
|
|
145
|
+
|
|
146
|
+
// Re-point the mail root under the live client, between operations. The new
|
|
147
|
+
// root is a real mailbox with one record seeded in new/. A client that
|
|
148
|
+
// resolves the root per operation reads and promotes here.
|
|
149
|
+
repoint(alias, storeNew);
|
|
150
|
+
const raw = signedRelayRecord(AGENT, "repointed", { branchId: BRANCH, id: ID });
|
|
151
|
+
const source = seed(storeNew, "r.json", raw);
|
|
152
|
+
for (const dir of ["cur", "dlq", "outbox"]) mkdirSync(join(storeNew, AGENT, dir), { recursive: true });
|
|
153
|
+
|
|
154
|
+
// A real second process holds the new root's mailbox lock.
|
|
155
|
+
const holder = await holdLock(mailboxLockRoot(storeNew, AGENT));
|
|
156
|
+
try {
|
|
157
|
+
// It must not promote under the old root's lock.
|
|
158
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
159
|
+
expect(existsSync(source)).toBe(true);
|
|
160
|
+
expect(existsSync(join(storeNew, AGENT, "cur", "r.json"))).toBe(false);
|
|
161
|
+
} finally {
|
|
162
|
+
await holder.release();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// After release the one envelope is delivered exactly once.
|
|
166
|
+
expect((await mail.checkNewMail()).map((message) => message.filename)).toEqual(["r.json"]);
|
|
167
|
+
expect(existsSync(source)).toBe(false);
|
|
168
|
+
expect(readFileSync(join(storeNew, AGENT, "cur", "r.json"), "utf8")).toBe(raw);
|
|
169
|
+
|
|
170
|
+
// The same signed envelope presented again under a new file name reaches the
|
|
171
|
+
// replay gate in the new root, which refuses it: no delivery, and the record
|
|
172
|
+
// is dead-lettered with a `replay` sidecar.
|
|
173
|
+
const again = seed(storeNew, "r-again.json", raw);
|
|
174
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
175
|
+
expect(existsSync(again)).toBe(false);
|
|
176
|
+
expect(existsSync(join(storeNew, AGENT, "cur", "r-again.json"))).toBe(false);
|
|
177
|
+
expect(readFileSync(join(storeNew, AGENT, "dlq", "r-again.json"), "utf8")).toBe(raw);
|
|
178
|
+
expect(readFileSync(join(storeNew, AGENT, "dlq", "r-again.json.reason"), "utf8").split("\n")[0]).toBe("class: replay");
|
|
179
|
+
}, 20_000);
|
|
180
|
+
|
|
181
|
+
test("a configured mail-root repoint during verification derives the promotion stripe from the initial canonical string", async () => {
|
|
182
|
+
const verifier = pausedVerifier();
|
|
183
|
+
const mail = new MailClient(alias, undefined, AGENT, verifier.client);
|
|
184
|
+
const source = seed(storeOld, "r.json", signedRelayRecord(AGENT, "stripe", { branchId: BRANCH, id: ID }));
|
|
185
|
+
|
|
186
|
+
// A real second process holds the delivery's acceptance stripe on the OLD
|
|
187
|
+
// root; the same stripe on the new root is free.
|
|
188
|
+
const holder = await holdLock(acceptanceLockRoot(storeOld, BRANCH, ID));
|
|
189
|
+
try {
|
|
190
|
+
const delivered = await checkWithChangeDuringVerification(mail, verifier, () => repoint(alias, storeNew));
|
|
191
|
+
expect(delivered).toEqual([]);
|
|
192
|
+
expect(existsSync(source)).toBe(true);
|
|
193
|
+
expect(existsSync(join(storeOld, AGENT, "cur", "r.json"))).toBe(false);
|
|
194
|
+
expect(existsSync(join(storeNew, AGENT, "cur", "r.json"))).toBe(false);
|
|
195
|
+
} finally {
|
|
196
|
+
await holder.release();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Control: with the stripe free, the record is deliverable from the old root.
|
|
200
|
+
repoint(alias, storeOld);
|
|
201
|
+
expect((await mail.checkNewMail()).map((message) => message.filename)).toEqual(["r.json"]);
|
|
202
|
+
expect(existsSync(join(storeOld, AGENT, "cur", "r.json"))).toBe(true);
|
|
203
|
+
}, 20_000);
|
|
204
|
+
|
|
205
|
+
for (const rejected of [false, true]) {
|
|
206
|
+
test(`canonical-root replacement redirects the ${rejected ? "dead-letter" : "promotion"} lock claim to the replacement target`, async () => {
|
|
207
|
+
const box = join(root, "box");
|
|
208
|
+
mkdirSync(box);
|
|
209
|
+
symlinkSync(box, join(storeOld, AGENT), "dir");
|
|
210
|
+
const verifier = pausedVerifier({ wrongKey: rejected });
|
|
211
|
+
const mail = new MailClient(alias, undefined, AGENT, verifier.client);
|
|
212
|
+
const raw = signedRelayRecord(AGENT, "resolved-target", { branchId: BRANCH, id: ID });
|
|
213
|
+
seed(storeOld, "r.json", raw);
|
|
214
|
+
const stripe = acceptanceLockRoot(storeOld, BRANCH, ID);
|
|
215
|
+
const movedStore = join(root, "moved-store");
|
|
216
|
+
const mkdir = fs.mkdirSync;
|
|
217
|
+
const realpath = fs.realpathSync;
|
|
218
|
+
const mkdirPaths: string[] = [];
|
|
219
|
+
const realpathInputs: string[] = [];
|
|
220
|
+
const physicalClaims: string[] = [];
|
|
221
|
+
let retargeted = false;
|
|
222
|
+
const mkdirSpy = spyOn(fs, "mkdirSync").mockImplementation(((...args: Parameters<typeof mkdir>) => {
|
|
223
|
+
if (retargeted) mkdirPaths.push(String(args[0]));
|
|
224
|
+
const result = mkdir(...args);
|
|
225
|
+
if (retargeted && String(args[0]) === join(stripe, ".mail-lock.claim")) {
|
|
226
|
+
physicalClaims.push(realpath(String(args[0])));
|
|
227
|
+
}
|
|
228
|
+
return result;
|
|
229
|
+
}) as typeof mkdir);
|
|
230
|
+
const realpathSpy = spyOn(fs, "realpathSync").mockImplementation(((...args: Parameters<typeof realpath>) => {
|
|
231
|
+
if (retargeted) realpathInputs.push(String(args[0]));
|
|
232
|
+
return realpath(...args);
|
|
233
|
+
}) as typeof realpath);
|
|
234
|
+
let delivered: MailMessage[];
|
|
235
|
+
try {
|
|
236
|
+
delivered = await checkWithChangeDuringVerification(mail, verifier, () => {
|
|
237
|
+
renameSync(storeOld, movedStore);
|
|
238
|
+
symlinkSync(storeNew, storeOld, "dir");
|
|
239
|
+
retargeted = true;
|
|
240
|
+
});
|
|
241
|
+
} finally {
|
|
242
|
+
mkdirSpy.mockRestore();
|
|
243
|
+
realpathSpy.mockRestore();
|
|
244
|
+
}
|
|
245
|
+
expect(realpathInputs).toEqual([]);
|
|
246
|
+
expect(mkdirPaths).toContain(stripe);
|
|
247
|
+
expect(mkdirPaths).toContain(join(stripe, ".mail-lock.claim"));
|
|
248
|
+
expect(mkdirPaths.some((path) => path.startsWith(join(storeNew, ".relay-accept-locks")))).toBe(false);
|
|
249
|
+
// Documents where the OS places the claim, not exclusion of root replacement.
|
|
250
|
+
expect(physicalClaims).toEqual([join(storeNew, ".relay-accept-locks", basename(stripe), ".mail-lock.claim")]);
|
|
251
|
+
expect(existsSync(join(movedStore, ".relay-accept-locks"))).toBe(false);
|
|
252
|
+
expect(delivered.map((message) => message.filename)).toEqual(rejected ? [] : ["r.json"]);
|
|
253
|
+
expect(existsSync(join(box, "new", "r.json"))).toBe(false);
|
|
254
|
+
expect(readFileSync(join(box, rejected ? "dlq" : "cur", "r.json"), "utf8")).toBe(raw);
|
|
255
|
+
expect(existsSync(join(storeNew, AGENT))).toBe(false);
|
|
256
|
+
if (rejected) {
|
|
257
|
+
expect(readFileSync(join(box, "dlq", "r.json.reason"), "utf8").split("\n")[0]).toBe("class: invalid");
|
|
258
|
+
} else {
|
|
259
|
+
expect(mkdirPaths).toContain(join(box, ".mail-lock.claim"));
|
|
260
|
+
expect(hasCommittedMessageId(box, "resolved-target")).toBe(true);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
test("a configured mail-root repoint during verification reads trust through the initial canonical string", async () => {
|
|
266
|
+
expect(bridgePrincipalIds(storeOld).has("sender")).toBe(true);
|
|
267
|
+
expect(bridgePrincipalIds(storeNew).has("sender")).toBe(false);
|
|
268
|
+
const verifier = pausedVerifier();
|
|
269
|
+
const mail = new MailClient(alias, undefined, AGENT, verifier.client);
|
|
270
|
+
const raw = signedRelayRecord(AGENT, "trust", { branchId: BRANCH, id: ID });
|
|
271
|
+
seed(storeOld, "r.json", raw);
|
|
272
|
+
|
|
273
|
+
const delivered = await checkWithChangeDuringVerification(mail, verifier, () => repoint(alias, storeNew));
|
|
274
|
+
expect(delivered.map((message) => [message.filename, message.trustTier])).toEqual([["r.json", "external"]]);
|
|
275
|
+
expect(readFileSync(join(storeOld, AGENT, "cur", "r.json"), "utf8")).toBe(raw);
|
|
276
|
+
expect(existsSync(join(storeNew, AGENT, "cur", "r.json"))).toBe(false);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("a configured mail-root repoint during verification derives the rejection stripe from the initial canonical string", async () => {
|
|
280
|
+
const verifier = pausedVerifier({ wrongKey: true });
|
|
281
|
+
const mail = new MailClient(alias, undefined, AGENT, verifier.client);
|
|
282
|
+
const raw = signedRelayRecord(AGENT, "rejected", { branchId: BRANCH, id: ID });
|
|
283
|
+
const source = seed(storeOld, "r.json", raw);
|
|
284
|
+
|
|
285
|
+
const holder = await holdLock(acceptanceLockRoot(storeOld, BRANCH, ID));
|
|
286
|
+
try {
|
|
287
|
+
expect(await checkWithChangeDuringVerification(mail, verifier, () => repoint(alias, storeNew))).toEqual([]);
|
|
288
|
+
// The rejection did not move the record under the new root's free stripe.
|
|
289
|
+
expect(existsSync(source)).toBe(true);
|
|
290
|
+
expect(existsSync(join(storeOld, AGENT, "dlq", "r.json"))).toBe(false);
|
|
291
|
+
expect(existsSync(join(storeOld, AGENT, "dlq", "r.json.reason"))).toBe(false);
|
|
292
|
+
} finally {
|
|
293
|
+
await holder.release();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Control: with the stripe free, the same record is dead-lettered as invalid.
|
|
297
|
+
repoint(alias, storeOld);
|
|
298
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
299
|
+
expect(existsSync(source)).toBe(false);
|
|
300
|
+
expect(readFileSync(join(storeOld, AGENT, "dlq", "r.json"), "utf8")).toBe(raw);
|
|
301
|
+
expect(readFileSync(join(storeOld, AGENT, "dlq", "r.json.reason"), "utf8").split("\n")[0]).toBe("class: invalid");
|
|
302
|
+
}, 20_000);
|
|
303
|
+
|
|
304
|
+
test("interrupted-placement recovery reads trust through the initial canonical string after a configured mail-root repoint", async () => {
|
|
305
|
+
const verifier = pausedVerifier();
|
|
306
|
+
const mail = new MailClient(alias, undefined, AGENT, verifier.client);
|
|
307
|
+
const mailbox = join(storeOld, AGENT);
|
|
308
|
+
const source = seed(storeOld, "m1.json", signedRelayRecord(AGENT, "recovered", { branchId: BRANCH, id: ID }));
|
|
309
|
+
// A placement interrupted after the move into cur/: recovery verifies the cur
|
|
310
|
+
// copy, classifies its trust and completes it.
|
|
311
|
+
mailboxReplayStore(mailbox).beginPlacement("recovered", "m1.json", source);
|
|
312
|
+
renameSync(source, join(mailbox, "cur", "m1.json"));
|
|
313
|
+
|
|
314
|
+
const delivered = await checkWithChangeDuringVerification(mail, verifier, () => repoint(alias, storeNew));
|
|
315
|
+
expect(delivered.map((message) => [message.filename, message.trustTier])).toEqual([["m1.json", "external"]]);
|
|
316
|
+
expect(hasCommittedMessageId(mailbox, "recovered")).toBe(true);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test("a mailbox-directory repoint during verification derives maildir and replay paths from the initial canonical string", async () => {
|
|
320
|
+
const store = join(root, "store");
|
|
321
|
+
const boxOld = join(root, "box-old");
|
|
322
|
+
const boxNew = join(root, "box-new");
|
|
323
|
+
for (const box of [boxOld, boxNew]) {
|
|
324
|
+
for (const dir of ["new", "cur", "dlq", "outbox"]) mkdirSync(join(box, dir), { recursive: true });
|
|
325
|
+
}
|
|
326
|
+
mkdirSync(store, { recursive: true });
|
|
327
|
+
const link = join(store, AGENT);
|
|
328
|
+
symlinkSync(boxOld, link, "dir");
|
|
329
|
+
const verifier = pausedVerifier();
|
|
330
|
+
const mail = new MailClient(store, undefined, AGENT, verifier.client);
|
|
331
|
+
const raw = signedRelayRecord(AGENT, "box", { branchId: BRANCH, id: ID });
|
|
332
|
+
writeFileSync(join(boxOld, "new", "r.json"), raw);
|
|
333
|
+
|
|
334
|
+
const delivered = await checkWithChangeDuringVerification(mail, verifier, () => repoint(link, boxNew));
|
|
335
|
+
expect(delivered.map((message) => message.filename)).toEqual(["r.json"]);
|
|
336
|
+
expect(existsSync(join(boxOld, "new", "r.json"))).toBe(false);
|
|
337
|
+
expect(readFileSync(join(boxOld, "cur", "r.json"), "utf8")).toBe(raw);
|
|
338
|
+
expect(hasCommittedMessageId(boxOld, "box")).toBe(true);
|
|
339
|
+
expect(existsSync(join(boxNew, "cur", "r.json"))).toBe(false);
|
|
340
|
+
expect(hasCommittedMessageId(boxNew, "box")).toBe(false);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("deliverOutbox derives the recipient path from the initial canonical mail-root string after a configured symlink repoint", async () => {
|
|
344
|
+
const mail = new MailClient(alias, undefined, AGENT, senderKeyResolver());
|
|
345
|
+
const sentAt = "2026-10-13T00:00:00.000Z";
|
|
346
|
+
writeFileSync(join(storeOld, AGENT, "outbox", "o.json"), JSON.stringify({ to: "peer", body: "x", sentAt }));
|
|
347
|
+
const dest = "2026-10-13T00-00-00-000Z-o.json";
|
|
348
|
+
|
|
349
|
+
// Repoint the mail root immediately before the underlying outbox read.
|
|
350
|
+
const read = fs.readFileSync;
|
|
351
|
+
let repointed = false;
|
|
352
|
+
const spy = spyOn(fs, "readFileSync").mockImplementation(((...args: Parameters<typeof read>) => {
|
|
353
|
+
if (!repointed && String(args[0]).endsWith(join("outbox", "o.json"))) {
|
|
354
|
+
repointed = true;
|
|
355
|
+
repoint(alias, storeNew);
|
|
356
|
+
}
|
|
357
|
+
return read(...args);
|
|
358
|
+
}) as typeof read);
|
|
359
|
+
try {
|
|
360
|
+
mail.deliverOutbox();
|
|
361
|
+
} finally {
|
|
362
|
+
spy.mockRestore();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
expect(repointed).toBe(true);
|
|
366
|
+
expect(existsSync(join(storeOld, "peer", "new", dest))).toBe(true);
|
|
367
|
+
expect(existsSync(join(storeNew, "peer", "new", dest))).toBe(false);
|
|
368
|
+
});
|