@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,105 @@
|
|
|
1
|
+
import { afterEach, beforeEach, expect, spyOn, test } from "bun:test";
|
|
2
|
+
import { createHash, generateKeyPairSync } from "node:crypto";
|
|
3
|
+
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import * as ed from "@noble/ed25519";
|
|
7
|
+
import { hashes } from "@noble/ed25519";
|
|
8
|
+
import { signEnvelope } from "../src/lib/signEnvelope.js";
|
|
9
|
+
import { AgentRuntime } from "../src/runtime/agent.js";
|
|
10
|
+
import type { MailClient } from "../src/io/mail.js";
|
|
11
|
+
|
|
12
|
+
hashes.sha512 = (message: Uint8Array) => new Uint8Array(createHash("sha512").update(message).digest());
|
|
13
|
+
|
|
14
|
+
const SENDER_SEED = Buffer.alloc(32, 0xfb);
|
|
15
|
+
const PUBLIC_KEY = Buffer.from(ed.getPublicKey(SENDER_SEED));
|
|
16
|
+
let root: string;
|
|
17
|
+
let fetchSpy: ReturnType<typeof spyOn>;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
root = mkdtempSync(join(tmpdir(), "runtime-key-"));
|
|
21
|
+
fetchSpy = spyOn(globalThis, "fetch");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
fetchSpy.mockRestore();
|
|
26
|
+
rmSync(root, { recursive: true, force: true });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
for (const [format, key] of [
|
|
30
|
+
["malformed", "not-a-key!!"],
|
|
31
|
+
["unreachable", ""],
|
|
32
|
+
["lowercase hex", PUBLIC_KEY.toString("hex")],
|
|
33
|
+
["uppercase hex", PUBLIC_KEY.toString("hex").toUpperCase()],
|
|
34
|
+
["base64url", PUBLIC_KEY.toString("base64url")],
|
|
35
|
+
["padded base64url", PUBLIC_KEY.toString("base64url") + "="],
|
|
36
|
+
["base64", PUBLIC_KEY.toString("base64")],
|
|
37
|
+
["unpadded base64", PUBLIC_KEY.toString("base64").replace(/=+$/, "")],
|
|
38
|
+
] as const) {
|
|
39
|
+
test(`AgentRuntime handles a ${format} Flair key`, async () => {
|
|
40
|
+
expect(PUBLIC_KEY.toString("base64url")).toMatch(/[-_]/);
|
|
41
|
+
const keyPath = join(root, "reader.pem");
|
|
42
|
+
const { privateKey } = generateKeyPairSync("ed25519");
|
|
43
|
+
writeFileSync(keyPath, privateKey.export({ type: "pkcs8", format: "pem" }));
|
|
44
|
+
let reads = 0;
|
|
45
|
+
fetchSpy.mockImplementation(async (input, init) => {
|
|
46
|
+
const url = new URL(String(input));
|
|
47
|
+
expect(url.origin).toBe("http://flair.test");
|
|
48
|
+
if (format === "unreachable") { reads++; throw new Error("Flair unreachable"); }
|
|
49
|
+
if (url.pathname === "/Health") return new Response("ok");
|
|
50
|
+
expect(url.pathname).toBe("/Agent/flint");
|
|
51
|
+
expect(new Headers(init?.headers).get("Authorization")).toMatch(/^TPS-Ed25519 anvil:/);
|
|
52
|
+
reads++;
|
|
53
|
+
return Response.json({ id: "flint", name: "flint", publicKey: key });
|
|
54
|
+
});
|
|
55
|
+
const runtime = new AgentRuntime({
|
|
56
|
+
agentId: "anvil",
|
|
57
|
+
name: "anvil",
|
|
58
|
+
mailDir: join(root, "mail"),
|
|
59
|
+
memoryPath: join(root, "memory.jsonl"),
|
|
60
|
+
workspace: root,
|
|
61
|
+
llm: { provider: "ollama", model: "stub" },
|
|
62
|
+
flair: { url: "http://flair.test", keyPath },
|
|
63
|
+
});
|
|
64
|
+
const timestamp = new Date().toISOString();
|
|
65
|
+
const envelope = signEnvelope({
|
|
66
|
+
v: 1,
|
|
67
|
+
from: "flint",
|
|
68
|
+
to: "anvil",
|
|
69
|
+
body: "verified delivery",
|
|
70
|
+
messageId: "runtime-key",
|
|
71
|
+
timestamp,
|
|
72
|
+
delegationChain: [
|
|
73
|
+
{ agent: "system", kind: "human", timestamp, rationale: "originates", signature: null },
|
|
74
|
+
{ agent: "flint", kind: "agent", timestamp, rationale: "sends", signature: null },
|
|
75
|
+
],
|
|
76
|
+
}, { flint: SENDER_SEED });
|
|
77
|
+
const inbox = join(root, "mail", "anvil");
|
|
78
|
+
mkdirSync(join(inbox, "new"), { recursive: true });
|
|
79
|
+
writeFileSync(join(inbox, "new", "message.json"), JSON.stringify({
|
|
80
|
+
from: "flint", to: "anvil", body: JSON.stringify(envelope),
|
|
81
|
+
}));
|
|
82
|
+
const messages = await (runtime as unknown as { mail: MailClient }).mail.checkNewMail();
|
|
83
|
+
expect(reads).toBeGreaterThan(0);
|
|
84
|
+
if (format === "malformed" || format === "unreachable") {
|
|
85
|
+
expect(messages).toEqual([]);
|
|
86
|
+
expect(readdirSync(join(inbox, "cur"))).toEqual([]);
|
|
87
|
+
if (format === "malformed") {
|
|
88
|
+
expect(readdirSync(join(inbox, "new"))).toEqual([]);
|
|
89
|
+
expect(readdirSync(join(inbox, "dlq")).sort()).toEqual(["message.json", "message.json.reason"]);
|
|
90
|
+
const reason = readFileSync(join(inbox, "dlq", "message.json.reason"), "utf8");
|
|
91
|
+
expect(reason).toContain("class: invalid");
|
|
92
|
+
expect(reason).toContain("malformed public key for flint");
|
|
93
|
+
} else {
|
|
94
|
+
expect(readdirSync(join(inbox, "new"))).toEqual(["message.json"]);
|
|
95
|
+
expect(readdirSync(join(inbox, "dlq")).sort()).toEqual([]);
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
expect(messages).toHaveLength(1);
|
|
100
|
+
expect(messages[0]?.verifiedEnvelope?.body).toBe("verified delivery");
|
|
101
|
+
expect(readdirSync(join(inbox, "new"))).toEqual([]);
|
|
102
|
+
expect(readdirSync(join(inbox, "cur"))).toEqual(["message.json"]);
|
|
103
|
+
expect(readdirSync(join(inbox, "dlq")).sort()).toEqual([]);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { afterEach, beforeEach, expect, spyOn, test } from "bun:test";
|
|
2
|
+
import { createHash, generateKeyPairSync } from "node:crypto";
|
|
3
|
+
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import * as ed from "@noble/ed25519";
|
|
7
|
+
import { hashes } from "@noble/ed25519";
|
|
8
|
+
import { signEnvelope } from "../src/lib/signEnvelope.js";
|
|
9
|
+
import { AgentRuntime } from "../src/runtime/agent.js";
|
|
10
|
+
import type { MailClient } from "../src/io/mail.js";
|
|
11
|
+
import { FlairContextProvider } from "../src/io/flair.js";
|
|
12
|
+
|
|
13
|
+
hashes.sha512 = (message: Uint8Array) => new Uint8Array(createHash("sha512").update(message).digest());
|
|
14
|
+
|
|
15
|
+
const SENDER = "agent-a";
|
|
16
|
+
const MAILBOX = "agent-b";
|
|
17
|
+
const SENDER_SEED = Buffer.alloc(32, 0x33);
|
|
18
|
+
const PUBLIC_KEY = Buffer.from(ed.getPublicKey(SENDER_SEED));
|
|
19
|
+
let root: string;
|
|
20
|
+
let fetchSpy: ReturnType<typeof spyOn>;
|
|
21
|
+
/** What the stub hub serves as the sender's publicKey. */
|
|
22
|
+
let servedKey: string;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
root = mkdtempSync(join(tmpdir(), "runtime-pending-key-"));
|
|
26
|
+
fetchSpy = spyOn(globalThis, "fetch");
|
|
27
|
+
fetchSpy.mockImplementation(async (input) => {
|
|
28
|
+
const url = new URL(String(input));
|
|
29
|
+
expect(url.origin).toBe("http://flair.test");
|
|
30
|
+
if (url.pathname === "/Health") return new Response("ok");
|
|
31
|
+
expect(url.pathname).toBe(`/Agent/${SENDER}`);
|
|
32
|
+
return Response.json({ id: SENDER, name: SENDER, publicKey: servedKey });
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
fetchSpy.mockRestore();
|
|
38
|
+
rmSync(root, { recursive: true, force: true });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function setup(): { mail: MailClient; inbox: string } {
|
|
42
|
+
const keyPath = join(root, "reader.pem");
|
|
43
|
+
const { privateKey } = generateKeyPairSync("ed25519");
|
|
44
|
+
writeFileSync(keyPath, privateKey.export({ type: "pkcs8", format: "pem" }));
|
|
45
|
+
const runtime = new AgentRuntime({
|
|
46
|
+
agentId: MAILBOX,
|
|
47
|
+
name: MAILBOX,
|
|
48
|
+
mailDir: join(root, "mail"),
|
|
49
|
+
memoryPath: join(root, "memory.jsonl"),
|
|
50
|
+
workspace: root,
|
|
51
|
+
llm: { provider: "ollama", model: "stub" },
|
|
52
|
+
flair: { url: "http://flair.test", keyPath },
|
|
53
|
+
});
|
|
54
|
+
const timestamp = new Date().toISOString();
|
|
55
|
+
const envelope = signEnvelope({
|
|
56
|
+
v: 1,
|
|
57
|
+
from: SENDER,
|
|
58
|
+
to: MAILBOX,
|
|
59
|
+
body: "verified delivery",
|
|
60
|
+
messageId: "runtime-pending-key",
|
|
61
|
+
timestamp,
|
|
62
|
+
delegationChain: [
|
|
63
|
+
{ agent: "system", kind: "human", timestamp, rationale: "originates", signature: null },
|
|
64
|
+
{ agent: SENDER, kind: "agent", timestamp, rationale: "sends", signature: null },
|
|
65
|
+
],
|
|
66
|
+
}, { [SENDER]: SENDER_SEED });
|
|
67
|
+
const inbox = join(root, "mail", MAILBOX);
|
|
68
|
+
mkdirSync(join(inbox, "new"), { recursive: true });
|
|
69
|
+
writeFileSync(join(inbox, "new", "message.json"), JSON.stringify({
|
|
70
|
+
from: SENDER, to: MAILBOX, body: JSON.stringify(envelope),
|
|
71
|
+
}));
|
|
72
|
+
return { mail: (runtime as unknown as { mail: MailClient }).mail, inbox };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
test("AgentRuntime keeps mail from a pending sender in new/ and promotes it once the key appears", async () => {
|
|
76
|
+
const { mail, inbox } = setup();
|
|
77
|
+
servedKey = "pending";
|
|
78
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
79
|
+
expect(readdirSync(join(inbox, "new"))).toEqual(["message.json"]);
|
|
80
|
+
expect(readdirSync(join(inbox, "cur"))).toEqual([]);
|
|
81
|
+
expect(readdirSync(join(inbox, "dlq"))).toEqual([]);
|
|
82
|
+
|
|
83
|
+
servedKey = PUBLIC_KEY.toString("hex");
|
|
84
|
+
const messages = await mail.checkNewMail();
|
|
85
|
+
expect(messages).toHaveLength(1);
|
|
86
|
+
expect(messages[0]?.verifiedEnvelope?.body).toBe("verified delivery");
|
|
87
|
+
expect(readdirSync(join(inbox, "new"))).toEqual([]);
|
|
88
|
+
expect(readdirSync(join(inbox, "cur"))).toEqual(["message.json"]);
|
|
89
|
+
expect(readdirSync(join(inbox, "dlq"))).toEqual([]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("AgentRuntime's verifier keeps the record in new/ when its provider returns a pending key", async () => {
|
|
93
|
+
const getAgent = spyOn(FlairContextProvider.prototype, "getAgent")
|
|
94
|
+
.mockResolvedValue({ id: SENDER, name: SENDER, publicKey: "pending" });
|
|
95
|
+
try {
|
|
96
|
+
const { mail, inbox } = setup();
|
|
97
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
98
|
+
expect(getAgent).toHaveBeenCalled();
|
|
99
|
+
expect(readdirSync(join(inbox, "new"))).toEqual(["message.json"]);
|
|
100
|
+
expect(readdirSync(join(inbox, "dlq"))).toEqual([]);
|
|
101
|
+
} finally {
|
|
102
|
+
getAgent.mockRestore();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
for (const key of ["PENDING", " pending", "pending="]) {
|
|
107
|
+
test(`AgentRuntime dead-letters a near-miss of the pending sentinel (${JSON.stringify(key)}) as invalid`, async () => {
|
|
108
|
+
const { mail, inbox } = setup();
|
|
109
|
+
servedKey = key;
|
|
110
|
+
expect(await mail.checkNewMail()).toEqual([]);
|
|
111
|
+
expect(readdirSync(join(inbox, "new"))).toEqual([]);
|
|
112
|
+
expect(readdirSync(join(inbox, "dlq")).sort()).toEqual(["message.json", "message.json.reason"]);
|
|
113
|
+
const reason = readFileSync(join(inbox, "dlq", "message.json.reason"), "utf8");
|
|
114
|
+
expect(reason).toContain("class: invalid");
|
|
115
|
+
expect(reason).toContain(`malformed public key for ${SENDER}`);
|
|
116
|
+
});
|
|
117
|
+
}
|