@tpsdev-ai/agent 0.6.0 → 0.7.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/dist/io/flair.d.ts +10 -0
- package/dist/io/flair.d.ts.map +1 -1
- package/dist/io/flair.js +14 -0
- package/dist/io/flair.js.map +1 -1
- package/dist/io/mail.d.ts +21 -6
- package/dist/io/mail.d.ts.map +1 -1
- package/dist/io/mail.js +175 -40
- package/dist/io/mail.js.map +1 -1
- package/dist/runtime/agent.d.ts.map +1 -1
- package/dist/runtime/agent.js +4 -1
- package/dist/runtime/agent.js.map +1 -1
- package/package.json +1 -1
- package/src/io/flair.ts +14 -0
- package/src/io/mail.ts +202 -45
- package/src/runtime/agent.ts +4 -1
- package/test/io.test.ts +7 -8
- package/test/mail-promote-guard.test.ts +325 -0
package/dist/io/flair.d.ts
CHANGED
|
@@ -29,6 +29,16 @@ export declare class FlairContextProvider {
|
|
|
29
29
|
private sign;
|
|
30
30
|
private req;
|
|
31
31
|
ping(): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve an agent from Flair.
|
|
34
|
+
*
|
|
35
|
+
* Distinguishes "Flair says this principal does not exist" (return null) from
|
|
36
|
+
* "Flair could not be reached" (THROW). The signature verifier built on this
|
|
37
|
+
* would otherwise see null for BOTH and dead-letter every message as a
|
|
38
|
+
* terminal "principal not found" for the duration of a Flair outage. The
|
|
39
|
+
* shared mail-verify client applies the same null→ping→throw rule; the two
|
|
40
|
+
* must not diverge.
|
|
41
|
+
*/
|
|
32
42
|
getAgent(name: string): Promise<FlairAgent | null>;
|
|
33
43
|
getSoul(): Promise<FlairSoulEntry[]>;
|
|
34
44
|
searchMemories(query: string, limit?: number): Promise<FlairMemory[]>;
|
package/dist/io/flair.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flair.d.ts","sourceRoot":"","sources":["../../src/io/flair.ts"],"names":[],"mappings":"AAAA;;;;;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AC7B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,WAAgB;IAUrD,OAAO,CAAC,IAAI;YAoBE,GAAG;IAmBX,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC;IAS9B;;;;;;;;;OASG;IACG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAYlD,OAAO,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAQpC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,SAAI,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAOhE,kBAAkB,CAAC,IAAI,SAAI,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAOpD,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,SAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBpF;;;OAGG;IACG,iBAAiB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAoCzD"}
|
package/dist/io/flair.js
CHANGED
|
@@ -65,11 +65,25 @@ export class FlairContextProvider {
|
|
|
65
65
|
return false;
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Resolve an agent from Flair.
|
|
70
|
+
*
|
|
71
|
+
* Distinguishes "Flair says this principal does not exist" (return null) from
|
|
72
|
+
* "Flair could not be reached" (THROW). The signature verifier built on this
|
|
73
|
+
* would otherwise see null for BOTH and dead-letter every message as a
|
|
74
|
+
* terminal "principal not found" for the duration of a Flair outage. The
|
|
75
|
+
* shared mail-verify client applies the same null→ping→throw rule; the two
|
|
76
|
+
* must not diverge.
|
|
77
|
+
*/
|
|
68
78
|
async getAgent(name) {
|
|
69
79
|
try {
|
|
70
80
|
return await this.req("GET", `/Agent/${name}`);
|
|
71
81
|
}
|
|
72
82
|
catch {
|
|
83
|
+
// Either the agent is absent (a reachable 404) or Flair is unreachable.
|
|
84
|
+
if (!(await this.ping())) {
|
|
85
|
+
throw new Error(`Flair unreachable at ${this.url} while resolving agent "${name}"`);
|
|
86
|
+
}
|
|
73
87
|
return null;
|
|
74
88
|
}
|
|
75
89
|
}
|
package/dist/io/flair.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flair.js","sourceRoot":"","sources":["../../src/io/flair.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAsBjC,MAAM,OAAO,oBAAoB;IACd,GAAG,CAAS;IACZ,OAAO,CAAS;IAChB,OAAO,CAAS;IAEjC,YAAY,OAAe,EAAE,SAAsB,EAAE;QACnD,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,uBAAuB,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAC/F,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO;YACV,MAAM,CAAC,OAAO;gBACd,IAAI,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,MAAM,CAAC,CAAC;IAC1D,CAAC;IAED,8EAA8E;IAEtE,IAAI,CAAC,MAAc,EAAE,IAAY;QACvC,MAAM,EAAE,GAAG,IA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C,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;oBAC7D,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,YAAY;YACd,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;CACF"}
|
package/dist/io/mail.d.ts
CHANGED
|
@@ -23,11 +23,20 @@ export declare class MailClient {
|
|
|
23
23
|
private inboxCur;
|
|
24
24
|
private inboxDlq;
|
|
25
25
|
private outboxNew;
|
|
26
|
+
/** One-shot guard so a misconfigured (verifier-less) mailbox warns once, not per poll. */
|
|
27
|
+
private warnedNoVerifier;
|
|
26
28
|
constructor(mailDir: string, events?: EventLogger | undefined, agentId?: string, flairClient?: FlairClient | undefined);
|
|
27
29
|
/**
|
|
28
|
-
* Return all messages in inbox/new and move them to inbox/cur
|
|
29
|
-
*
|
|
30
|
-
*
|
|
30
|
+
* Return all messages in inbox/new and move them to inbox/cur/.
|
|
31
|
+
*
|
|
32
|
+
* Verification is MANDATORY — a record is promoted ONLY after its signed
|
|
33
|
+
* envelope verifies against the local Flair. There is no unverified path:
|
|
34
|
+
* - NO verifier configured → refuse; the record stays in new/ (an absent
|
|
35
|
+
* client must never mean "promote without verifying");
|
|
36
|
+
* - the verifier THROWS (Flair unreachable) → refuse; the record stays in
|
|
37
|
+
* new/ for a later check (a throw must never mean "pass");
|
|
38
|
+
* - the verifier REJECTS → dead-letter to dlq/ with a `.reason` sidecar.
|
|
39
|
+
* Unverified input must never reach the tool-holding model.
|
|
31
40
|
*/
|
|
32
41
|
checkNewMail(): Promise<MailMessage[]>;
|
|
33
42
|
/** Write a message to outbox/new for relay delivery. */
|
|
@@ -35,9 +44,15 @@ export declare class MailClient {
|
|
|
35
44
|
/** Deliver all messages in outbox to recipient inboxes (local relay). */
|
|
36
45
|
deliverOutbox(): void;
|
|
37
46
|
/**
|
|
38
|
-
* Verify a mail body against the v1 signed envelope spec
|
|
39
|
-
*
|
|
40
|
-
*
|
|
47
|
+
* Verify a mail body against the v1 signed envelope spec AND this mailbox's
|
|
48
|
+
* policy: signature, wrapper→envelope `from` binding, recipient binding
|
|
49
|
+
* (`envelope.to` must be this mailbox's agent), and `messageId`/`timestamp`
|
|
50
|
+
* shape. These are the checks the shared `promote()` applies, so this path
|
|
51
|
+
* cannot present mail the shared path would reject.
|
|
52
|
+
*
|
|
53
|
+
* Returns a terminal `{ pass: false, class, reason }` on a deterministic
|
|
54
|
+
* rejection. Called ONLY when a verifier is configured; a THROW (Flair
|
|
55
|
+
* unreachable) is a refusal the caller acts on, never a pass.
|
|
41
56
|
*/
|
|
42
57
|
private verifyMailBody;
|
|
43
58
|
}
|
package/dist/io/mail.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../../src/io/mail.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAkB,KAAK,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1E,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;IACjB,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAC;CACd;
|
|
1
|
+
{"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../../src/io/mail.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAkB,KAAK,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1E,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;IACjB,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAC;CACd;AA8CD;;;;GAIG;AACH,qBAAa,UAAU;aASH,OAAO,EAAE,MAAM;IAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;IAX/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,SAAS,CAAS;IAC1B,0FAA0F;IAC1F,OAAO,CAAC,gBAAgB,CAAS;gBAGf,OAAO,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,YAAA,EACpB,OAAO,SAAY,EACnB,WAAW,CAAC,EAAE,WAAW,YAAA;IAW5C;;;;;;;;;;;OAWG;IACG,YAAY,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAyH5C,wDAAwD;IAClD,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA8BvD,yEAAyE;IACzE,aAAa,IAAI,IAAI;IAqBrB;;;;;;;;;;OAUG;YACW,cAAc;CA8F7B"}
|
package/dist/io/mail.js
CHANGED
|
@@ -2,6 +2,30 @@ import { existsSync, mkdirSync, readdirSync, renameSync, readFileSync, writeFile
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { sanitizeError } from "../telemetry/events.js";
|
|
4
4
|
import { verifyEnvelope } from "../lib/signEnvelope.js";
|
|
5
|
+
/**
|
|
6
|
+
* The stable reason string `verifyEnvelope` returns when an agent-kind chain
|
|
7
|
+
* entry or `envelope.from` cannot be resolved from the LOCAL Flair. An ABSENT
|
|
8
|
+
* principal is a topology condition, not a forgery verdict, so it is labelled
|
|
9
|
+
* `unresolvable-principal` rather than `invalid`. Duplicated from the shared
|
|
10
|
+
* promotion boundary because `@tpsdev-ai/agent` cannot import `packages/cli`
|
|
11
|
+
* (the dependency runs the other way: cli → agent).
|
|
12
|
+
*/
|
|
13
|
+
const UNRESOLVABLE_PRINCIPAL_REASON_RE = /^agent (.+) not found in Flair$/;
|
|
14
|
+
/**
|
|
15
|
+
* Write the dlq sidecar for a TERMINAL rejection.
|
|
16
|
+
*
|
|
17
|
+
* It mirrors the shared mail convention's FORMAT — file name `<record>.reason`
|
|
18
|
+
* and a first line `class: <class>`, the shape the shared re-drive's
|
|
19
|
+
* `readReasonSidecar` parses — and reuses the shared class NAMES. It does NOT
|
|
20
|
+
* reproduce the shared reject boundary: only the terminal classes above are
|
|
21
|
+
* emitted, and a retryable outcome (Flair unreachable) writes NO sidecar at all
|
|
22
|
+
* (the record is left in `new/`). So it is "format + the terminal class names
|
|
23
|
+
* this path emits", not the full boundary. (This path previously wrote
|
|
24
|
+
* `${file}.reject` with a bare reason, which no shared reader scans.)
|
|
25
|
+
*/
|
|
26
|
+
function writeRejectSidecar(dlqDir, filename, cls, reason) {
|
|
27
|
+
writeFileSync(join(dlqDir, `${filename}.reason`), `class: ${cls}\nPromote rejected at ${new Date().toISOString()}\nReason: ${reason}\n`, "utf-8");
|
|
28
|
+
}
|
|
5
29
|
/**
|
|
6
30
|
* Maildir-compatible mail client.
|
|
7
31
|
* Reads from mailDir/inbox/new and moves processed messages to mailDir/inbox/cur.
|
|
@@ -16,6 +40,8 @@ export class MailClient {
|
|
|
16
40
|
inboxCur;
|
|
17
41
|
inboxDlq;
|
|
18
42
|
outboxNew;
|
|
43
|
+
/** One-shot guard so a misconfigured (verifier-less) mailbox warns once, not per poll. */
|
|
44
|
+
warnedNoVerifier = false;
|
|
19
45
|
constructor(mailDir, events, agentId = "unknown", flairClient) {
|
|
20
46
|
this.mailDir = mailDir;
|
|
21
47
|
this.events = events;
|
|
@@ -30,39 +56,103 @@ export class MailClient {
|
|
|
30
56
|
}
|
|
31
57
|
}
|
|
32
58
|
/**
|
|
33
|
-
* Return all messages in inbox/new and move them to inbox/cur
|
|
34
|
-
*
|
|
35
|
-
*
|
|
59
|
+
* Return all messages in inbox/new and move them to inbox/cur/.
|
|
60
|
+
*
|
|
61
|
+
* Verification is MANDATORY — a record is promoted ONLY after its signed
|
|
62
|
+
* envelope verifies against the local Flair. There is no unverified path:
|
|
63
|
+
* - NO verifier configured → refuse; the record stays in new/ (an absent
|
|
64
|
+
* client must never mean "promote without verifying");
|
|
65
|
+
* - the verifier THROWS (Flair unreachable) → refuse; the record stays in
|
|
66
|
+
* new/ for a later check (a throw must never mean "pass");
|
|
67
|
+
* - the verifier REJECTS → dead-letter to dlq/ with a `.reason` sidecar.
|
|
68
|
+
* Unverified input must never reach the tool-holding model.
|
|
36
69
|
*/
|
|
37
70
|
async checkNewMail() {
|
|
38
71
|
if (!existsSync(this.inboxNew))
|
|
39
72
|
return [];
|
|
40
73
|
const files = readdirSync(this.inboxNew).filter((f) => !f.startsWith(".") && !f.includes("/") && !f.includes("\\"));
|
|
41
74
|
const messages = [];
|
|
75
|
+
// No verifier → NOTHING is promotable. Refuse (never rename into cur/) and
|
|
76
|
+
// leave the records in new/ so a later check with a verifier — or the
|
|
77
|
+
// shared promote() path — can process them.
|
|
78
|
+
if (!this.flairClient) {
|
|
79
|
+
if (files.length === 0)
|
|
80
|
+
return [];
|
|
81
|
+
this.events?.emit({
|
|
82
|
+
type: "mail.receive",
|
|
83
|
+
agent: this.agentId,
|
|
84
|
+
status: "rejected",
|
|
85
|
+
from: "unknown",
|
|
86
|
+
durationMs: 0,
|
|
87
|
+
error: "no verifier configured — refusing to promote unverified mail",
|
|
88
|
+
});
|
|
89
|
+
if (!this.warnedNoVerifier) {
|
|
90
|
+
this.warnedNoVerifier = true;
|
|
91
|
+
console.error(`[MailClient] no Flair verifier configured for "${this.agentId}": refusing to promote ` +
|
|
92
|
+
`${files.length} unverified message(s) from new/. Unverified input must never reach the ` +
|
|
93
|
+
`model — configure flair (url + key) or promote through the shared promote() lifecycle.`);
|
|
94
|
+
}
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
42
97
|
for (const file of files) {
|
|
43
98
|
const started = Date.now();
|
|
44
99
|
const srcPath = join(this.inboxNew, file);
|
|
100
|
+
let body;
|
|
101
|
+
try {
|
|
102
|
+
body = readFileSync(srcPath, "utf-8");
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
this.events?.emit({
|
|
106
|
+
type: "mail.receive",
|
|
107
|
+
agent: this.agentId,
|
|
108
|
+
status: "error",
|
|
109
|
+
from: "unknown",
|
|
110
|
+
durationMs: Date.now() - started,
|
|
111
|
+
error: sanitizeError(err),
|
|
112
|
+
});
|
|
113
|
+
continue; // leave in new/ — a later check retries
|
|
114
|
+
}
|
|
115
|
+
// Verify. A THROW is a refusal, not a pass: leave the record in new/ for a
|
|
116
|
+
// later check (a Flair outage self-heals) and never promote it.
|
|
117
|
+
let verifyResult;
|
|
45
118
|
try {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
119
|
+
verifyResult = await this.verifyMailBody(body);
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
const detail = sanitizeError(err);
|
|
123
|
+
this.events?.emit({
|
|
124
|
+
type: "mail.receive",
|
|
125
|
+
agent: this.agentId,
|
|
126
|
+
status: "error",
|
|
127
|
+
from: "unknown",
|
|
128
|
+
durationMs: Date.now() - started,
|
|
129
|
+
error: detail,
|
|
130
|
+
});
|
|
131
|
+
console.error(`[MailClient] verification could not run for ${file} — NOT promoting: ${detail}`);
|
|
132
|
+
continue; // leave in new/
|
|
133
|
+
}
|
|
134
|
+
if (!verifyResult.pass) {
|
|
135
|
+
const dlqPath = join(this.inboxDlq, file);
|
|
136
|
+
try {
|
|
137
|
+
if (srcPath !== dlqPath)
|
|
52
138
|
renameSync(srcPath, dlqPath);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
status: "rejected",
|
|
58
|
-
from: verifyResult.from ?? "unknown",
|
|
59
|
-
durationMs: Date.now() - started,
|
|
60
|
-
error: verifyResult.reason,
|
|
61
|
-
});
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
139
|
+
writeRejectSidecar(this.inboxDlq, file, verifyResult.class, verifyResult.reason);
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
console.error(`[MailClient] failed to dead-letter ${file}: ${sanitizeError(err)}`);
|
|
64
143
|
}
|
|
65
|
-
|
|
144
|
+
this.events?.emit({
|
|
145
|
+
type: "mail.receive",
|
|
146
|
+
agent: this.agentId,
|
|
147
|
+
status: "rejected",
|
|
148
|
+
from: verifyResult.from ?? "unknown",
|
|
149
|
+
durationMs: Date.now() - started,
|
|
150
|
+
error: verifyResult.reason,
|
|
151
|
+
});
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
// Promote to cur/ — verified, and addressed to this mailbox.
|
|
155
|
+
try {
|
|
66
156
|
const dstPath = join(this.inboxCur, file);
|
|
67
157
|
renameSync(srcPath, dstPath);
|
|
68
158
|
let headers = {};
|
|
@@ -146,11 +236,17 @@ export class MailClient {
|
|
|
146
236
|
}
|
|
147
237
|
}
|
|
148
238
|
/**
|
|
149
|
-
* Verify a mail body against the v1 signed envelope spec
|
|
150
|
-
*
|
|
151
|
-
*
|
|
239
|
+
* Verify a mail body against the v1 signed envelope spec AND this mailbox's
|
|
240
|
+
* policy: signature, wrapper→envelope `from` binding, recipient binding
|
|
241
|
+
* (`envelope.to` must be this mailbox's agent), and `messageId`/`timestamp`
|
|
242
|
+
* shape. These are the checks the shared `promote()` applies, so this path
|
|
243
|
+
* cannot present mail the shared path would reject.
|
|
244
|
+
*
|
|
245
|
+
* Returns a terminal `{ pass: false, class, reason }` on a deterministic
|
|
246
|
+
* rejection. Called ONLY when a verifier is configured; a THROW (Flair
|
|
247
|
+
* unreachable) is a refusal the caller acts on, never a pass.
|
|
152
248
|
*/
|
|
153
|
-
async verifyMailBody(body
|
|
249
|
+
async verifyMailBody(body) {
|
|
154
250
|
const client = this.flairClient;
|
|
155
251
|
// 1. Parse the mail file as JSON
|
|
156
252
|
let mailMsg;
|
|
@@ -158,10 +254,10 @@ export class MailClient {
|
|
|
158
254
|
mailMsg = JSON.parse(body);
|
|
159
255
|
}
|
|
160
256
|
catch {
|
|
161
|
-
return { pass: false, reason: "json parse error: invalid JSON" };
|
|
257
|
+
return { pass: false, class: "invalid", reason: "json parse error: invalid JSON" };
|
|
162
258
|
}
|
|
163
259
|
if (!mailMsg.body || typeof mailMsg.body !== "string") {
|
|
164
|
-
return { pass: false, reason: "json parse error: missing body field" };
|
|
260
|
+
return { pass: false, class: "invalid", reason: "json parse error: missing body field" };
|
|
165
261
|
}
|
|
166
262
|
// 2. Parse the body field as an envelope
|
|
167
263
|
let envelope;
|
|
@@ -169,28 +265,67 @@ export class MailClient {
|
|
|
169
265
|
envelope = JSON.parse(mailMsg.body);
|
|
170
266
|
}
|
|
171
267
|
catch {
|
|
172
|
-
return { pass: false, reason: "json parse error: invalid envelope body", from: mailMsg.from };
|
|
268
|
+
return { pass: false, class: "invalid", reason: "json parse error: invalid envelope body", from: mailMsg.from };
|
|
173
269
|
}
|
|
174
270
|
if (envelope == null || typeof envelope !== "object" || Array.isArray(envelope)) {
|
|
175
|
-
return { pass: false, reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
271
|
+
return { pass: false, class: "invalid", reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
176
272
|
}
|
|
177
273
|
const env = envelope;
|
|
178
274
|
if (typeof env.v !== "number" ||
|
|
179
275
|
!Array.isArray(env.delegationChain) ||
|
|
180
276
|
typeof env.signature !== "string") {
|
|
181
|
-
return { pass: false, reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
277
|
+
return { pass: false, class: "invalid", reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
182
278
|
}
|
|
183
|
-
// 3. Verify the
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
279
|
+
// 3. Verify the signature. A THROW here (Flair unreachable) is NOT a pass —
|
|
280
|
+
// it propagates to checkNewMail(), which refuses to promote and leaves
|
|
281
|
+
// the record in new/ for a later check. The verifier adapter throws on an
|
|
282
|
+
// outage, so an outage is RETRYABLE rather than a terminal "not found".
|
|
283
|
+
const vr = await verifyEnvelope(env, client);
|
|
284
|
+
if (!vr.ok) {
|
|
285
|
+
const cls = UNRESOLVABLE_PRINCIPAL_REASON_RE.test(vr.reason)
|
|
286
|
+
? "unresolvable-principal"
|
|
287
|
+
: "invalid";
|
|
288
|
+
return { pass: false, class: cls, reason: vr.reason, from: mailMsg.from };
|
|
189
289
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
290
|
+
// 4. The wrapper `from` is what consumers route by, and it is unverified; a
|
|
291
|
+
// wrapper/envelope mismatch is itself a reject.
|
|
292
|
+
if (mailMsg.from !== env.from) {
|
|
293
|
+
return {
|
|
294
|
+
pass: false,
|
|
295
|
+
class: "invalid",
|
|
296
|
+
reason: `wrapper/envelope from mismatch (wrapper.from=${String(mailMsg.from)}, envelope.from=${String(env.from)})`,
|
|
297
|
+
from: mailMsg.from,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
// 5. Recipient binding. A signature is NOT recipient-bound, so a correctly
|
|
301
|
+
// signed envelope addressed to another principal must not be presented
|
|
302
|
+
// here (deliverOutbox writes into any recipient's new/).
|
|
303
|
+
if (env.to !== this.agentId) {
|
|
304
|
+
return {
|
|
305
|
+
pass: false,
|
|
306
|
+
class: "wrong-recipient",
|
|
307
|
+
reason: `wrong-recipient (envelope.to=${String(env.to)}, mailbox=${this.agentId})`,
|
|
308
|
+
from: mailMsg.from,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
// 6. `messageId` shape.
|
|
312
|
+
if (typeof env.messageId !== "string" || env.messageId.trim() === "") {
|
|
313
|
+
return {
|
|
314
|
+
pass: false,
|
|
315
|
+
class: "invalid",
|
|
316
|
+
reason: `invalid messageId (must be a non-empty string, got ${JSON.stringify(env.messageId)})`,
|
|
317
|
+
from: mailMsg.from,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
// 7. `timestamp` shape — a malformed/absent timestamp must not be silently
|
|
321
|
+
// accepted (the shared path rejects it too).
|
|
322
|
+
if (typeof env.timestamp !== "string" || Number.isNaN(Date.parse(env.timestamp))) {
|
|
323
|
+
return {
|
|
324
|
+
pass: false,
|
|
325
|
+
class: "invalid",
|
|
326
|
+
reason: `invalid timestamp (must be an ISO-8601 string, got ${JSON.stringify(env.timestamp)})`,
|
|
327
|
+
from: mailMsg.from,
|
|
328
|
+
};
|
|
194
329
|
}
|
|
195
330
|
return { pass: true };
|
|
196
331
|
}
|
package/dist/io/mail.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/io/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAoB,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/io/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;A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AAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;YACjF,OAAO;gBACL,IAAI,EAAE,KAAK;gBACX,KAAK,EAAE,SAAS;gBAChB,MAAM,EAAE,sDAAsD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG;gBAC9F,IAAI,EAAE,OAAO,CAAC,IAAI;aACnB,CAAC;QACJ,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/runtime/agent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAY9C,qBAAa,YAAY;aAMK,MAAM,EAAE,WAAW;IAL/C,OAAO,CAAC,IAAI,CAAY;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkB;IAC3C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;gBAExB,MAAM,EAAE,WAAW;
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/runtime/agent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAY9C,qBAAa,YAAY;aAMK,MAAM,EAAE,WAAW;IAL/C,OAAO,CAAC,IAAI,CAAY;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkB;IAC3C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;gBAExB,MAAM,EAAE,WAAW;IA0CzC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrB,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBvC,uBAAuB,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASzE,QAAQ,IAAI,MAAM;IAIlB,SAAS,IAAI,OAAO;IAIpB,kBAAkB,IAAI,MAAM;CAG7B"}
|
package/dist/runtime/agent.js
CHANGED
|
@@ -19,7 +19,10 @@ export class AgentRuntime {
|
|
|
19
19
|
this.config = config;
|
|
20
20
|
const events = new EventLogger(config.agentId, join(config.workspace, ".tps", "events"));
|
|
21
21
|
this.flair = config.flair ? new FlairContextProvider(config.agentId, config.flair) : null;
|
|
22
|
-
// Build FlairClient adapter for envelope verification
|
|
22
|
+
// Build FlairClient adapter for envelope verification. The provider's
|
|
23
|
+
// getAgent() disambiguates a Flair outage (throws → retryable) from an
|
|
24
|
+
// absent principal (null → terminal), so a signature verifier built on it
|
|
25
|
+
// does not dead-letter every message while Flair is down.
|
|
23
26
|
let flairClient;
|
|
24
27
|
if (this.flair) {
|
|
25
28
|
flairClient = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/runtime/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC3D,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OA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;IACnC,CAAC;IAED,KAAK,CAAC,uBAAuB,CAAC,EAAU,EAAE,OAAe;QACvD,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAO;QACxB,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC;QAC5D,CAAC;QAAC,MAAM,CAAC;YACP,uCAAuC;QACzC,CAAC;IACH,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC9B,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IACvC,CAAC;IAED,kBAAkB;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,CAAC;IAC9C,CAAC;CACF"}
|
package/package.json
CHANGED
package/src/io/flair.ts
CHANGED
|
@@ -93,10 +93,24 @@ export class FlairContextProvider {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Resolve an agent from Flair.
|
|
98
|
+
*
|
|
99
|
+
* Distinguishes "Flair says this principal does not exist" (return null) from
|
|
100
|
+
* "Flair could not be reached" (THROW). The signature verifier built on this
|
|
101
|
+
* would otherwise see null for BOTH and dead-letter every message as a
|
|
102
|
+
* terminal "principal not found" for the duration of a Flair outage. The
|
|
103
|
+
* shared mail-verify client applies the same null→ping→throw rule; the two
|
|
104
|
+
* must not diverge.
|
|
105
|
+
*/
|
|
96
106
|
async getAgent(name: string): Promise<FlairAgent | null> {
|
|
97
107
|
try {
|
|
98
108
|
return await this.req<FlairAgent>("GET", `/Agent/${name}`);
|
|
99
109
|
} catch {
|
|
110
|
+
// Either the agent is absent (a reachable 404) or Flair is unreachable.
|
|
111
|
+
if (!(await this.ping())) {
|
|
112
|
+
throw new Error(`Flair unreachable at ${this.url} while resolving agent "${name}"`);
|
|
113
|
+
}
|
|
100
114
|
return null;
|
|
101
115
|
}
|
|
102
116
|
}
|
package/src/io/mail.ts
CHANGED
|
@@ -14,6 +14,50 @@ export interface MailMessage {
|
|
|
14
14
|
from: string;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The mailbox reject classes this path can emit. It uses the SHARED class NAMES
|
|
19
|
+
* (`invalid`, `unresolvable-principal`, `wrong-recipient`) so a dlq sidecar it
|
|
20
|
+
* writes is readable by the shared tooling. It is NOT the full shared class set:
|
|
21
|
+
* this path emits only TERMINAL classes — an outage is refused, not
|
|
22
|
+
* dead-lettered (the record stays in `new/`), so no retryable class is written
|
|
23
|
+
* here.
|
|
24
|
+
*/
|
|
25
|
+
type MailboxRejectClass = "invalid" | "unresolvable-principal" | "wrong-recipient";
|
|
26
|
+
|
|
27
|
+
type VerifyOutcome =
|
|
28
|
+
| { pass: true }
|
|
29
|
+
| { pass: false; class: MailboxRejectClass; reason: string; from?: string };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The stable reason string `verifyEnvelope` returns when an agent-kind chain
|
|
33
|
+
* entry or `envelope.from` cannot be resolved from the LOCAL Flair. An ABSENT
|
|
34
|
+
* principal is a topology condition, not a forgery verdict, so it is labelled
|
|
35
|
+
* `unresolvable-principal` rather than `invalid`. Duplicated from the shared
|
|
36
|
+
* promotion boundary because `@tpsdev-ai/agent` cannot import `packages/cli`
|
|
37
|
+
* (the dependency runs the other way: cli → agent).
|
|
38
|
+
*/
|
|
39
|
+
const UNRESOLVABLE_PRINCIPAL_REASON_RE = /^agent (.+) not found in Flair$/;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Write the dlq sidecar for a TERMINAL rejection.
|
|
43
|
+
*
|
|
44
|
+
* It mirrors the shared mail convention's FORMAT — file name `<record>.reason`
|
|
45
|
+
* and a first line `class: <class>`, the shape the shared re-drive's
|
|
46
|
+
* `readReasonSidecar` parses — and reuses the shared class NAMES. It does NOT
|
|
47
|
+
* reproduce the shared reject boundary: only the terminal classes above are
|
|
48
|
+
* emitted, and a retryable outcome (Flair unreachable) writes NO sidecar at all
|
|
49
|
+
* (the record is left in `new/`). So it is "format + the terminal class names
|
|
50
|
+
* this path emits", not the full boundary. (This path previously wrote
|
|
51
|
+
* `${file}.reject` with a bare reason, which no shared reader scans.)
|
|
52
|
+
*/
|
|
53
|
+
function writeRejectSidecar(dlqDir: string, filename: string, cls: MailboxRejectClass, reason: string): void {
|
|
54
|
+
writeFileSync(
|
|
55
|
+
join(dlqDir, `${filename}.reason`),
|
|
56
|
+
`class: ${cls}\nPromote rejected at ${new Date().toISOString()}\nReason: ${reason}\n`,
|
|
57
|
+
"utf-8",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
17
61
|
/**
|
|
18
62
|
* Maildir-compatible mail client.
|
|
19
63
|
* Reads from mailDir/inbox/new and moves processed messages to mailDir/inbox/cur.
|
|
@@ -24,6 +68,8 @@ export class MailClient {
|
|
|
24
68
|
private inboxCur: string;
|
|
25
69
|
private inboxDlq: string;
|
|
26
70
|
private outboxNew: string;
|
|
71
|
+
/** One-shot guard so a misconfigured (verifier-less) mailbox warns once, not per poll. */
|
|
72
|
+
private warnedNoVerifier = false;
|
|
27
73
|
|
|
28
74
|
constructor(
|
|
29
75
|
public readonly mailDir: string,
|
|
@@ -41,9 +87,16 @@ export class MailClient {
|
|
|
41
87
|
}
|
|
42
88
|
|
|
43
89
|
/**
|
|
44
|
-
* Return all messages in inbox/new and move them to inbox/cur
|
|
45
|
-
*
|
|
46
|
-
*
|
|
90
|
+
* Return all messages in inbox/new and move them to inbox/cur/.
|
|
91
|
+
*
|
|
92
|
+
* Verification is MANDATORY — a record is promoted ONLY after its signed
|
|
93
|
+
* envelope verifies against the local Flair. There is no unverified path:
|
|
94
|
+
* - NO verifier configured → refuse; the record stays in new/ (an absent
|
|
95
|
+
* client must never mean "promote without verifying");
|
|
96
|
+
* - the verifier THROWS (Flair unreachable) → refuse; the record stays in
|
|
97
|
+
* new/ for a later check (a throw must never mean "pass");
|
|
98
|
+
* - the verifier REJECTS → dead-letter to dlq/ with a `.reason` sidecar.
|
|
99
|
+
* Unverified input must never reach the tool-holding model.
|
|
47
100
|
*/
|
|
48
101
|
async checkNewMail(): Promise<MailMessage[]> {
|
|
49
102
|
if (!existsSync(this.inboxNew)) return [];
|
|
@@ -51,32 +104,89 @@ export class MailClient {
|
|
|
51
104
|
const files = readdirSync(this.inboxNew).filter((f) => !f.startsWith(".") && !f.includes("/") && !f.includes("\\"));
|
|
52
105
|
const messages: MailMessage[] = [];
|
|
53
106
|
|
|
107
|
+
// No verifier → NOTHING is promotable. Refuse (never rename into cur/) and
|
|
108
|
+
// leave the records in new/ so a later check with a verifier — or the
|
|
109
|
+
// shared promote() path — can process them.
|
|
110
|
+
if (!this.flairClient) {
|
|
111
|
+
if (files.length === 0) return [];
|
|
112
|
+
this.events?.emit({
|
|
113
|
+
type: "mail.receive",
|
|
114
|
+
agent: this.agentId,
|
|
115
|
+
status: "rejected",
|
|
116
|
+
from: "unknown",
|
|
117
|
+
durationMs: 0,
|
|
118
|
+
error: "no verifier configured — refusing to promote unverified mail",
|
|
119
|
+
});
|
|
120
|
+
if (!this.warnedNoVerifier) {
|
|
121
|
+
this.warnedNoVerifier = true;
|
|
122
|
+
console.error(
|
|
123
|
+
`[MailClient] no Flair verifier configured for "${this.agentId}": refusing to promote ` +
|
|
124
|
+
`${files.length} unverified message(s) from new/. Unverified input must never reach the ` +
|
|
125
|
+
`model — configure flair (url + key) or promote through the shared promote() lifecycle.`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
|
|
54
131
|
for (const file of files) {
|
|
55
132
|
const started = Date.now();
|
|
56
133
|
const srcPath = join(this.inboxNew, file);
|
|
134
|
+
|
|
135
|
+
let body: string;
|
|
136
|
+
try {
|
|
137
|
+
body = readFileSync(srcPath, "utf-8");
|
|
138
|
+
} catch (err) {
|
|
139
|
+
this.events?.emit({
|
|
140
|
+
type: "mail.receive",
|
|
141
|
+
agent: this.agentId,
|
|
142
|
+
status: "error",
|
|
143
|
+
from: "unknown",
|
|
144
|
+
durationMs: Date.now() - started,
|
|
145
|
+
error: sanitizeError(err),
|
|
146
|
+
});
|
|
147
|
+
continue; // leave in new/ — a later check retries
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Verify. A THROW is a refusal, not a pass: leave the record in new/ for a
|
|
151
|
+
// later check (a Flair outage self-heals) and never promote it.
|
|
152
|
+
let verifyResult: VerifyOutcome;
|
|
57
153
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
154
|
+
verifyResult = await this.verifyMailBody(body);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
const detail = sanitizeError(err);
|
|
157
|
+
this.events?.emit({
|
|
158
|
+
type: "mail.receive",
|
|
159
|
+
agent: this.agentId,
|
|
160
|
+
status: "error",
|
|
161
|
+
from: "unknown",
|
|
162
|
+
durationMs: Date.now() - started,
|
|
163
|
+
error: detail,
|
|
164
|
+
});
|
|
165
|
+
console.error(`[MailClient] verification could not run for ${file} — NOT promoting: ${detail}`);
|
|
166
|
+
continue; // leave in new/
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!verifyResult.pass) {
|
|
170
|
+
const dlqPath = join(this.inboxDlq, file);
|
|
171
|
+
try {
|
|
172
|
+
if (srcPath !== dlqPath) renameSync(srcPath, dlqPath);
|
|
173
|
+
writeRejectSidecar(this.inboxDlq, file, verifyResult.class, verifyResult.reason);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
console.error(`[MailClient] failed to dead-letter ${file}: ${sanitizeError(err)}`);
|
|
77
176
|
}
|
|
177
|
+
this.events?.emit({
|
|
178
|
+
type: "mail.receive",
|
|
179
|
+
agent: this.agentId,
|
|
180
|
+
status: "rejected",
|
|
181
|
+
from: verifyResult.from ?? "unknown",
|
|
182
|
+
durationMs: Date.now() - started,
|
|
183
|
+
error: verifyResult.reason,
|
|
184
|
+
});
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
78
187
|
|
|
79
|
-
|
|
188
|
+
// Promote to cur/ — verified, and addressed to this mailbox.
|
|
189
|
+
try {
|
|
80
190
|
const dstPath = join(this.inboxCur, file);
|
|
81
191
|
renameSync(srcPath, dstPath);
|
|
82
192
|
let headers: Record<string, string> = {};
|
|
@@ -163,14 +273,17 @@ export class MailClient {
|
|
|
163
273
|
}
|
|
164
274
|
|
|
165
275
|
/**
|
|
166
|
-
* Verify a mail body against the v1 signed envelope spec
|
|
167
|
-
*
|
|
168
|
-
*
|
|
276
|
+
* Verify a mail body against the v1 signed envelope spec AND this mailbox's
|
|
277
|
+
* policy: signature, wrapper→envelope `from` binding, recipient binding
|
|
278
|
+
* (`envelope.to` must be this mailbox's agent), and `messageId`/`timestamp`
|
|
279
|
+
* shape. These are the checks the shared `promote()` applies, so this path
|
|
280
|
+
* cannot present mail the shared path would reject.
|
|
281
|
+
*
|
|
282
|
+
* Returns a terminal `{ pass: false, class, reason }` on a deterministic
|
|
283
|
+
* rejection. Called ONLY when a verifier is configured; a THROW (Flair
|
|
284
|
+
* unreachable) is a refusal the caller acts on, never a pass.
|
|
169
285
|
*/
|
|
170
|
-
private async verifyMailBody(
|
|
171
|
-
body: string,
|
|
172
|
-
filename: string,
|
|
173
|
-
): Promise<{ pass: true } | { pass: false; reason: string; from?: string }> {
|
|
286
|
+
private async verifyMailBody(body: string): Promise<VerifyOutcome> {
|
|
174
287
|
const client = this.flairClient!;
|
|
175
288
|
|
|
176
289
|
// 1. Parse the mail file as JSON
|
|
@@ -178,11 +291,11 @@ export class MailClient {
|
|
|
178
291
|
try {
|
|
179
292
|
mailMsg = JSON.parse(body);
|
|
180
293
|
} catch {
|
|
181
|
-
return { pass: false, reason: "json parse error: invalid JSON" };
|
|
294
|
+
return { pass: false, class: "invalid", reason: "json parse error: invalid JSON" };
|
|
182
295
|
}
|
|
183
296
|
|
|
184
297
|
if (!mailMsg.body || typeof mailMsg.body !== "string") {
|
|
185
|
-
return { pass: false, reason: "json parse error: missing body field" };
|
|
298
|
+
return { pass: false, class: "invalid", reason: "json parse error: missing body field" };
|
|
186
299
|
}
|
|
187
300
|
|
|
188
301
|
// 2. Parse the body field as an envelope
|
|
@@ -190,11 +303,11 @@ export class MailClient {
|
|
|
190
303
|
try {
|
|
191
304
|
envelope = JSON.parse(mailMsg.body);
|
|
192
305
|
} catch {
|
|
193
|
-
return { pass: false, reason: "json parse error: invalid envelope body", from: mailMsg.from };
|
|
306
|
+
return { pass: false, class: "invalid", reason: "json parse error: invalid envelope body", from: mailMsg.from };
|
|
194
307
|
}
|
|
195
308
|
|
|
196
309
|
if (envelope == null || typeof envelope !== "object" || Array.isArray(envelope)) {
|
|
197
|
-
return { pass: false, reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
310
|
+
return { pass: false, class: "invalid", reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
198
311
|
}
|
|
199
312
|
|
|
200
313
|
const env = envelope as Record<string, unknown>;
|
|
@@ -203,19 +316,63 @@ export class MailClient {
|
|
|
203
316
|
!Array.isArray(env.delegationChain) ||
|
|
204
317
|
typeof env.signature !== "string"
|
|
205
318
|
) {
|
|
206
|
-
return { pass: false, reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
319
|
+
return { pass: false, class: "invalid", reason: "unsigned envelope (v1 required)", from: mailMsg.from };
|
|
207
320
|
}
|
|
208
321
|
|
|
209
|
-
// 3. Verify the
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
322
|
+
// 3. Verify the signature. A THROW here (Flair unreachable) is NOT a pass —
|
|
323
|
+
// it propagates to checkNewMail(), which refuses to promote and leaves
|
|
324
|
+
// the record in new/ for a later check. The verifier adapter throws on an
|
|
325
|
+
// outage, so an outage is RETRYABLE rather than a terminal "not found".
|
|
326
|
+
const vr = await verifyEnvelope(env as any, client);
|
|
327
|
+
if (!vr.ok) {
|
|
328
|
+
const cls: MailboxRejectClass = UNRESOLVABLE_PRINCIPAL_REASON_RE.test(vr.reason)
|
|
329
|
+
? "unresolvable-principal"
|
|
330
|
+
: "invalid";
|
|
331
|
+
return { pass: false, class: cls, reason: vr.reason, from: mailMsg.from };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// 4. The wrapper `from` is what consumers route by, and it is unverified; a
|
|
335
|
+
// wrapper/envelope mismatch is itself a reject.
|
|
336
|
+
if (mailMsg.from !== env.from) {
|
|
337
|
+
return {
|
|
338
|
+
pass: false,
|
|
339
|
+
class: "invalid",
|
|
340
|
+
reason: `wrapper/envelope from mismatch (wrapper.from=${String(mailMsg.from)}, envelope.from=${String(env.from)})`,
|
|
341
|
+
from: mailMsg.from,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// 5. Recipient binding. A signature is NOT recipient-bound, so a correctly
|
|
346
|
+
// signed envelope addressed to another principal must not be presented
|
|
347
|
+
// here (deliverOutbox writes into any recipient's new/).
|
|
348
|
+
if (env.to !== this.agentId) {
|
|
349
|
+
return {
|
|
350
|
+
pass: false,
|
|
351
|
+
class: "wrong-recipient",
|
|
352
|
+
reason: `wrong-recipient (envelope.to=${String(env.to)}, mailbox=${this.agentId})`,
|
|
353
|
+
from: mailMsg.from,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// 6. `messageId` shape.
|
|
358
|
+
if (typeof env.messageId !== "string" || env.messageId.trim() === "") {
|
|
359
|
+
return {
|
|
360
|
+
pass: false,
|
|
361
|
+
class: "invalid",
|
|
362
|
+
reason: `invalid messageId (must be a non-empty string, got ${JSON.stringify(env.messageId)})`,
|
|
363
|
+
from: mailMsg.from,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// 7. `timestamp` shape — a malformed/absent timestamp must not be silently
|
|
368
|
+
// accepted (the shared path rejects it too).
|
|
369
|
+
if (typeof env.timestamp !== "string" || Number.isNaN(Date.parse(env.timestamp))) {
|
|
370
|
+
return {
|
|
371
|
+
pass: false,
|
|
372
|
+
class: "invalid",
|
|
373
|
+
reason: `invalid timestamp (must be an ISO-8601 string, got ${JSON.stringify(env.timestamp)})`,
|
|
374
|
+
from: mailMsg.from,
|
|
375
|
+
};
|
|
219
376
|
}
|
|
220
377
|
|
|
221
378
|
return { pass: true };
|
package/src/runtime/agent.ts
CHANGED
|
@@ -25,7 +25,10 @@ export class AgentRuntime {
|
|
|
25
25
|
);
|
|
26
26
|
this.flair = config.flair ? new FlairContextProvider(config.agentId, config.flair) : null;
|
|
27
27
|
|
|
28
|
-
// Build FlairClient adapter for envelope verification
|
|
28
|
+
// Build FlairClient adapter for envelope verification. The provider's
|
|
29
|
+
// getAgent() disambiguates a Flair outage (throws → retryable) from an
|
|
30
|
+
// absent principal (null → terminal), so a signature verifier built on it
|
|
31
|
+
// does not dead-letter every message while Flair is down.
|
|
29
32
|
let flairClient: FlairClient | undefined;
|
|
30
33
|
if (this.flair) {
|
|
31
34
|
flairClient = {
|
package/test/io.test.ts
CHANGED
|
@@ -31,17 +31,16 @@ describe("MailClient", () => {
|
|
|
31
31
|
expect(msgs).toEqual([]);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
test("checkNewMail
|
|
35
|
-
const { writeFileSync } = await import("node:fs");
|
|
34
|
+
test("checkNewMail refuses to promote when no verifier is configured (cli#380 F1)", async () => {
|
|
35
|
+
const { writeFileSync, existsSync } = await import("node:fs");
|
|
36
36
|
writeFileSync(join(tmpDir, "testagent", "new", "test-1.json"), "hello world", "utf-8");
|
|
37
37
|
|
|
38
|
+
// No FlairClient → verification cannot run. An absent verifier must never
|
|
39
|
+
// mean "promote without verifying": the record is NOT moved.
|
|
38
40
|
const msgs = await client.checkNewMail();
|
|
39
|
-
expect(msgs.length).toBe(
|
|
40
|
-
expect(
|
|
41
|
-
|
|
42
|
-
const { existsSync } = await import("node:fs");
|
|
43
|
-
expect(existsSync(join(tmpDir, "testagent", "new", "test-1.json"))).toBe(false);
|
|
44
|
-
expect(existsSync(join(tmpDir, "testagent", "cur", "test-1.json"))).toBe(true);
|
|
41
|
+
expect(msgs.length).toBe(0);
|
|
42
|
+
expect(existsSync(join(tmpDir, "testagent", "new", "test-1.json"))).toBe(true);
|
|
43
|
+
expect(existsSync(join(tmpDir, "testagent", "cur", "test-1.json"))).toBe(false);
|
|
45
44
|
});
|
|
46
45
|
|
|
47
46
|
test("sendMail writes a file to outbox/new", async () => {
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mail-promote-guard.test.ts — cli#380 F1 (the packages/agent maildir→model path).
|
|
3
|
+
*
|
|
4
|
+
* `packages/agent/src/io/mail.ts` is a FOURTH consumer that reaches a
|
|
5
|
+
* tool-holding EventLoop (AgentRuntime's EventLoop), alongside the three
|
|
6
|
+
* runtimes moved onto the shared promote() lifecycle. It promoted `new/`→`cur/`
|
|
7
|
+
* with verification OPTIONAL, two ways:
|
|
8
|
+
*
|
|
9
|
+
* 1. `checkNewMail()` guarded verification behind `if (this.flairClient)`, so
|
|
10
|
+
* a runtime built without Flair config promoted unverified mail;
|
|
11
|
+
* 2. `verifyMailBody()` swallowed a verifier THROW and returned `pass: true`,
|
|
12
|
+
* so a Flair outage promoted unverified mail.
|
|
13
|
+
*
|
|
14
|
+
* These drills pin the fail-CLOSED contract: no verifier → no promotion;
|
|
15
|
+
* a throw → no promotion; a deterministic reject → dlq/ with the shared
|
|
16
|
+
* `.reason` sidecar. Both defects are RED at 545e23cd and GREEN after.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
20
|
+
import { mkdtempSync, rmSync, writeFileSync, readFileSync, existsSync, readdirSync, mkdirSync } from "node:fs";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
import { tmpdir } from "node:os";
|
|
23
|
+
import { randomUUID } from "node:crypto";
|
|
24
|
+
import * as ed from "@noble/ed25519";
|
|
25
|
+
import { signEnvelope, type Envelope, type ChainEntry, type FlairClient } from "../src/lib/signEnvelope.js";
|
|
26
|
+
import { FlairContextProvider } from "../src/io/flair.js";
|
|
27
|
+
import { MailClient } from "../src/io/mail.js";
|
|
28
|
+
|
|
29
|
+
const AGENT = "mailbox";
|
|
30
|
+
|
|
31
|
+
function seed(b: number): Buffer {
|
|
32
|
+
return Buffer.alloc(32, b);
|
|
33
|
+
}
|
|
34
|
+
function pub(seedBytes: Buffer): Buffer {
|
|
35
|
+
return Buffer.from(ed.getPublicKey(new Uint8Array(seedBytes)));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** FlairClient stub resolving the named agents to their public keys. */
|
|
39
|
+
function flairClient(agents: Record<string, Buffer>): FlairClient {
|
|
40
|
+
return {
|
|
41
|
+
async getAgent(name: string) {
|
|
42
|
+
const pk = agents[name];
|
|
43
|
+
return pk ? { publicKey: pk } : null;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A verifier that cannot run (Flair unreachable). */
|
|
49
|
+
const throwingFlair: FlairClient = {
|
|
50
|
+
async getAgent() {
|
|
51
|
+
throw new Error("Flair unreachable: ECONNREFUSED");
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The FlairClient adapter AgentRuntime builds for envelope verification
|
|
57
|
+
* (a thin wrapper over the provider). Mirrored here so the outage drill
|
|
58
|
+
* exercises the REAL provider, not a stub.
|
|
59
|
+
*/
|
|
60
|
+
function providerVerifier(provider: FlairContextProvider): FlairClient {
|
|
61
|
+
return {
|
|
62
|
+
async getAgent(name: string) {
|
|
63
|
+
const a = await provider.getAgent(name);
|
|
64
|
+
if (!a) return null;
|
|
65
|
+
return { publicKey: Buffer.from(a.publicKey, "hex") };
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function signedEnvelope(
|
|
71
|
+
from: string,
|
|
72
|
+
to: string,
|
|
73
|
+
body: string,
|
|
74
|
+
seeds: Record<string, Buffer>,
|
|
75
|
+
overrides: Partial<Envelope> = {},
|
|
76
|
+
): Envelope {
|
|
77
|
+
const now = new Date().toISOString();
|
|
78
|
+
const chain: ChainEntry[] = [
|
|
79
|
+
{ agent: "system", kind: "human", timestamp: now, rationale: "originates", signature: null },
|
|
80
|
+
{ agent: from, kind: "agent", timestamp: now, rationale: "sends", signature: null },
|
|
81
|
+
];
|
|
82
|
+
const envelope: Envelope = {
|
|
83
|
+
v: 1,
|
|
84
|
+
from,
|
|
85
|
+
to,
|
|
86
|
+
body,
|
|
87
|
+
messageId: randomUUID(),
|
|
88
|
+
timestamp: now,
|
|
89
|
+
delegationChain: chain,
|
|
90
|
+
...overrides,
|
|
91
|
+
};
|
|
92
|
+
return signEnvelope(envelope, { [from]: seeds[from]! });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe("agent MailClient promotion is fail-closed (cli#380 F1)", () => {
|
|
96
|
+
let tmpDir: string;
|
|
97
|
+
const FLINT = seed(0x01);
|
|
98
|
+
const KERN = seed(0x02);
|
|
99
|
+
|
|
100
|
+
beforeEach(() => {
|
|
101
|
+
tmpDir = mkdtempSync(join(tmpdir(), "tps-agent-mail-guard-"));
|
|
102
|
+
});
|
|
103
|
+
afterEach(() => {
|
|
104
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const inbox = (d: "new" | "cur" | "dlq") => join(tmpDir, AGENT, d);
|
|
108
|
+
const files = (d: "new" | "cur" | "dlq") =>
|
|
109
|
+
existsSync(inbox(d)) ? readdirSync(inbox(d)).filter((f) => f.endsWith(".json")) : [];
|
|
110
|
+
const wrapper = (from: string, env: Envelope) => ({ from, to: AGENT, body: JSON.stringify(env) });
|
|
111
|
+
const plant = (wrapperObj: unknown, name = "m1.json") => {
|
|
112
|
+
mkdirSync(inbox("new"), { recursive: true });
|
|
113
|
+
writeFileSync(join(inbox("new"), name), JSON.stringify(wrapperObj), "utf-8");
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// ── F1 defect 1: verification was optional (no client ⇒ unverified promote) ─
|
|
117
|
+
test("NO verifier: refuses to promote — the record stays in new/", async () => {
|
|
118
|
+
const env = signedEnvelope("flint", AGENT, "hello", { flint: FLINT });
|
|
119
|
+
plant(wrapper("flint", env));
|
|
120
|
+
|
|
121
|
+
const client = new MailClient(tmpDir, undefined, AGENT); // no flairClient
|
|
122
|
+
const msgs = await client.checkNewMail();
|
|
123
|
+
|
|
124
|
+
expect(msgs.length).toBe(0);
|
|
125
|
+
expect(files("new")).toContain("m1.json"); // untouched
|
|
126
|
+
expect(files("cur").length).toBe(0); // never promoted
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// ── F1 defect 2: the verifier throw was swallowed and treated as a pass ────
|
|
130
|
+
test("THROWING verifier: refuses to promote — a throw must not mean pass", async () => {
|
|
131
|
+
const env = signedEnvelope("flint", AGENT, "hello", { flint: FLINT });
|
|
132
|
+
plant(wrapper("flint", env));
|
|
133
|
+
|
|
134
|
+
const client = new MailClient(tmpDir, undefined, AGENT, throwingFlair);
|
|
135
|
+
const msgs = await client.checkNewMail();
|
|
136
|
+
|
|
137
|
+
expect(msgs.length).toBe(0);
|
|
138
|
+
expect(files("new")).toContain("m1.json");
|
|
139
|
+
expect(files("cur").length).toBe(0);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// ── Deterministic rejection → dlq with the SHARED `.reason` sidecar ────────
|
|
143
|
+
test("a signature rejection dead-letters to dlq with a `.reason` sidecar (class: invalid)", async () => {
|
|
144
|
+
const env = signedEnvelope("flint", AGENT, "tampered", { flint: FLINT });
|
|
145
|
+
// Resolve flint to the WRONG key → signature verification fails.
|
|
146
|
+
const wrongKey = flairClient({ flint: pub(KERN) });
|
|
147
|
+
plant(wrapper("flint", env));
|
|
148
|
+
|
|
149
|
+
const client = new MailClient(tmpDir, undefined, AGENT, wrongKey);
|
|
150
|
+
const msgs = await client.checkNewMail();
|
|
151
|
+
|
|
152
|
+
expect(msgs.length).toBe(0);
|
|
153
|
+
expect(files("cur").length).toBe(0);
|
|
154
|
+
expect(files("dlq")).toContain("m1.json");
|
|
155
|
+
|
|
156
|
+
const sidecar = join(inbox("dlq"), "m1.json.reason");
|
|
157
|
+
expect(existsSync(sidecar)).toBe(true);
|
|
158
|
+
expect(readFileSync(sidecar, "utf-8")).toContain("class: invalid");
|
|
159
|
+
// Not the old `.reject` sidecar the shared re-drive cannot read.
|
|
160
|
+
expect(existsSync(join(inbox("dlq"), "m1.json.reject"))).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ── Topology failure is labelled as such, not as forgery (mirrors #383) ────
|
|
164
|
+
test("an unresolvable principal dead-letters as `unresolvable-principal`, not `invalid`", async () => {
|
|
165
|
+
const ghost = seed(0x09);
|
|
166
|
+
const env = signedEnvelope("ghost", AGENT, "from nowhere", { ghost });
|
|
167
|
+
const reachableNoGhost = flairClient({ flint: pub(FLINT) }); // Flair up, ghost absent
|
|
168
|
+
plant(wrapper("ghost", env));
|
|
169
|
+
|
|
170
|
+
const client = new MailClient(tmpDir, undefined, AGENT, reachableNoGhost);
|
|
171
|
+
const msgs = await client.checkNewMail();
|
|
172
|
+
|
|
173
|
+
expect(msgs.length).toBe(0);
|
|
174
|
+
expect(files("dlq")).toContain("m1.json");
|
|
175
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain(
|
|
176
|
+
"class: unresolvable-principal",
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// ── A non-envelope body is a terminal reject too ───────────────────────────
|
|
181
|
+
test("a plain (non-envelope) body is dead-lettered, never promoted", async () => {
|
|
182
|
+
plant({ from: "flint", to: AGENT, body: "plain text, no envelope" });
|
|
183
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
184
|
+
const msgs = await client.checkNewMail();
|
|
185
|
+
|
|
186
|
+
expect(msgs.length).toBe(0);
|
|
187
|
+
expect(files("cur").length).toBe(0);
|
|
188
|
+
expect(files("dlq")).toContain("m1.json");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// ── Positive control: a valid signed envelope IS promoted ──────────────────
|
|
192
|
+
test("a valid signed envelope with a working verifier is promoted to cur/", async () => {
|
|
193
|
+
const env = signedEnvelope("flint", AGENT, "do the thing", { flint: FLINT });
|
|
194
|
+
plant(wrapper("flint", env));
|
|
195
|
+
|
|
196
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
197
|
+
const msgs = await client.checkNewMail();
|
|
198
|
+
|
|
199
|
+
expect(msgs.length).toBe(1);
|
|
200
|
+
expect(msgs[0]!.from).toBe("flint");
|
|
201
|
+
// The agent MailClient presents the raw record body (unchanged); the
|
|
202
|
+
// verified content is the signed envelope inside it.
|
|
203
|
+
const raw = JSON.parse(msgs[0]!.body) as { from: string; body: string };
|
|
204
|
+
expect(raw.from).toBe("flint");
|
|
205
|
+
expect((JSON.parse(raw.body) as Envelope).body).toBe("do the thing");
|
|
206
|
+
expect(files("cur")).toContain("m1.json");
|
|
207
|
+
expect(files("new").length).toBe(0);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// ── The refusal is non-destructive: a later verifier promotes the same file ─
|
|
211
|
+
test("a record refused without a verifier is promoted once a verifier is present", async () => {
|
|
212
|
+
const env = signedEnvelope("flint", AGENT, "heal me", { flint: FLINT });
|
|
213
|
+
plant(wrapper("flint", env));
|
|
214
|
+
|
|
215
|
+
const noVerifier = new MailClient(tmpDir, undefined, AGENT);
|
|
216
|
+
expect((await noVerifier.checkNewMail()).length).toBe(0);
|
|
217
|
+
expect(files("new")).toContain("m1.json");
|
|
218
|
+
|
|
219
|
+
const withVerifier = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
220
|
+
const healed = await withVerifier.checkNewMail();
|
|
221
|
+
expect(healed.length).toBe(1);
|
|
222
|
+
expect(files("cur")).toContain("m1.json");
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// ── Finding 1: an outage is RETRYABLE, not a terminal dead-letter ───────────
|
|
226
|
+
|
|
227
|
+
test("an UNREACHABLE Flair is a RETRYABLE refusal (stays in new/), not a terminal dead-letter", async () => {
|
|
228
|
+
const env = signedEnvelope("flint", AGENT, "during outage", { flint: FLINT });
|
|
229
|
+
plant(wrapper("flint", env));
|
|
230
|
+
|
|
231
|
+
// Real provider against a dead endpoint. getAgent() swallows the connection
|
|
232
|
+
// error to null; the verifier must probe /Health to tell "absent" from "down".
|
|
233
|
+
const provider = new FlairContextProvider(AGENT, {
|
|
234
|
+
url: "http://127.0.0.1:1",
|
|
235
|
+
keyPath: join(tmpDir, "no-key"),
|
|
236
|
+
});
|
|
237
|
+
const client = new MailClient(tmpDir, undefined, AGENT, providerVerifier(provider));
|
|
238
|
+
const msgs = await client.checkNewMail();
|
|
239
|
+
|
|
240
|
+
expect(msgs.length).toBe(0);
|
|
241
|
+
expect(files("dlq").length).toBe(0); // NOT dead-lettered
|
|
242
|
+
expect(files("new")).toContain("m1.json"); // left for a later check (retryable)
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("a REACHABLE Flair that does not know the principal IS terminal (unresolvable-principal)", async () => {
|
|
246
|
+
// Control for the disambiguation: the SAME null from getAgent, but /Health is up.
|
|
247
|
+
const server = Bun.serve({
|
|
248
|
+
port: 0,
|
|
249
|
+
fetch(req) {
|
|
250
|
+
const p = new URL(req.url).pathname;
|
|
251
|
+
if (p === "/Health") return new Response("ok", { status: 200 });
|
|
252
|
+
return new Response("not found", { status: 404 });
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
try {
|
|
256
|
+
const env = signedEnvelope("flint", AGENT, "absent", { flint: FLINT });
|
|
257
|
+
plant(wrapper("flint", env));
|
|
258
|
+
const provider = new FlairContextProvider(AGENT, {
|
|
259
|
+
url: server.url.href,
|
|
260
|
+
keyPath: join(tmpDir, "no-key"),
|
|
261
|
+
});
|
|
262
|
+
const client = new MailClient(tmpDir, undefined, AGENT, providerVerifier(provider));
|
|
263
|
+
const msgs = await client.checkNewMail();
|
|
264
|
+
|
|
265
|
+
expect(msgs.length).toBe(0);
|
|
266
|
+
expect(files("dlq")).toContain("m1.json");
|
|
267
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain(
|
|
268
|
+
"class: unresolvable-principal",
|
|
269
|
+
);
|
|
270
|
+
} finally {
|
|
271
|
+
server.stop(true);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ── Finding 2: the guard checks WHO FOR, and the wrapper binding ────────────
|
|
276
|
+
|
|
277
|
+
test("a correctly-signed envelope addressed to ANOTHER principal is not presented (wrong-recipient)", async () => {
|
|
278
|
+
const env = signedEnvelope("flint", "someone-else", "not for me", { flint: FLINT });
|
|
279
|
+
plant({ from: "flint", to: "someone-else", body: JSON.stringify(env) });
|
|
280
|
+
|
|
281
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
282
|
+
const msgs = await client.checkNewMail();
|
|
283
|
+
|
|
284
|
+
expect(msgs.length).toBe(0);
|
|
285
|
+
expect(files("cur").length).toBe(0);
|
|
286
|
+
expect(files("dlq")).toContain("m1.json");
|
|
287
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain("class: wrong-recipient");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("a wrapper whose from disagrees with the envelope is rejected (invalid)", async () => {
|
|
291
|
+
const env = signedEnvelope("flint", AGENT, "spoofed wrapper", { flint: FLINT });
|
|
292
|
+
plant({ from: "nathan", to: AGENT, body: JSON.stringify(env) });
|
|
293
|
+
|
|
294
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
295
|
+
const msgs = await client.checkNewMail();
|
|
296
|
+
|
|
297
|
+
expect(msgs.length).toBe(0);
|
|
298
|
+
expect(files("dlq")).toContain("m1.json");
|
|
299
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain("class: invalid");
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("an envelope with a malformed messageId is rejected (invalid)", async () => {
|
|
303
|
+
const env = signedEnvelope("flint", AGENT, "bad id", { flint: FLINT }, { messageId: "" });
|
|
304
|
+
plant(wrapper("flint", env));
|
|
305
|
+
|
|
306
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
307
|
+
const msgs = await client.checkNewMail();
|
|
308
|
+
|
|
309
|
+
expect(msgs.length).toBe(0);
|
|
310
|
+
expect(files("dlq")).toContain("m1.json");
|
|
311
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain("class: invalid");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("an envelope with a malformed timestamp is rejected (invalid)", async () => {
|
|
315
|
+
const env = signedEnvelope("flint", AGENT, "bad ts", { flint: FLINT }, { timestamp: "not-a-date" });
|
|
316
|
+
plant(wrapper("flint", env));
|
|
317
|
+
|
|
318
|
+
const client = new MailClient(tmpDir, undefined, AGENT, flairClient({ flint: pub(FLINT) }));
|
|
319
|
+
const msgs = await client.checkNewMail();
|
|
320
|
+
|
|
321
|
+
expect(msgs.length).toBe(0);
|
|
322
|
+
expect(files("dlq")).toContain("m1.json");
|
|
323
|
+
expect(readFileSync(join(inbox("dlq"), "m1.json.reason"), "utf-8")).toContain("class: invalid");
|
|
324
|
+
});
|
|
325
|
+
});
|