ceulen 0.8.0 → 0.10.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/CHANGELOG.md +72 -0
- package/extensions/lib/registry.ts +16 -3
- package/extensions/modules/a2a/configPanel.ts +435 -0
- package/extensions/modules/a2a/index.ts +1117 -0
- package/extensions/modules/a2a/lib/activity.ts +173 -0
- package/extensions/modules/a2a/lib/client.ts +1061 -0
- package/extensions/modules/a2a/lib/config.ts +920 -0
- package/extensions/modules/a2a/lib/discovery.ts +158 -0
- package/extensions/modules/a2a/lib/gateway.ts +889 -0
- package/extensions/modules/a2a/lib/mdns.ts +233 -0
- package/extensions/modules/a2a/lib/outcome.ts +48 -0
- package/extensions/modules/a2a/lib/persistence.ts +123 -0
- package/extensions/modules/a2a/lib/protocol.ts +506 -0
- package/extensions/modules/a2a/lib/registry.ts +146 -0
- package/extensions/modules/a2a/lib/retry.ts +86 -0
- package/extensions/modules/a2a/lib/security.ts +394 -0
- package/extensions/modules/a2a/lib/server.ts +1436 -0
- package/extensions/modules/a2a/test/activity.test.ts +138 -0
- package/extensions/modules/a2a/test/bridge.test.ts +101 -0
- package/extensions/modules/a2a/test/broadcast-parse.test.ts +31 -0
- package/extensions/modules/a2a/test/chai.ts +101 -0
- package/extensions/modules/a2a/test/client.test.ts +856 -0
- package/extensions/modules/a2a/test/completion-guard.test.ts +73 -0
- package/extensions/modules/a2a/test/config.test.ts +1396 -0
- package/extensions/modules/a2a/test/configPanel.test.ts +393 -0
- package/extensions/modules/a2a/test/discovery.test.ts +144 -0
- package/extensions/modules/a2a/test/extension.test.ts +135 -0
- package/extensions/modules/a2a/test/gateway.test.ts +1449 -0
- package/extensions/modules/a2a/test/helpers.ts +39 -0
- package/extensions/modules/a2a/test/mdns.test.ts +131 -0
- package/extensions/modules/a2a/test/outcome.test.ts +61 -0
- package/extensions/modules/a2a/test/persistence.test.ts +92 -0
- package/extensions/modules/a2a/test/protocol.test.ts +217 -0
- package/extensions/modules/a2a/test/registry.test.ts +114 -0
- package/extensions/modules/a2a/test/retry.test.ts +136 -0
- package/extensions/modules/a2a/test/security.test.ts +281 -0
- package/extensions/modules/a2a/test/server.test.ts +1966 -0
- package/extensions/modules/a2a/test/tmp.ts +59 -0
- package/extensions/modules/a2a/test/vendor.test.ts +57 -0
- package/extensions/modules/a2a/types/bonjour-service.d.ts +22 -0
- package/extensions/modules/a2a/vendor/VENDORED.md +18 -0
- package/extensions/modules/a2a/vendor/package.json +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/LICENSE +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.js +57 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.js +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.d.ts +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.d.ts +41 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.js +209 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.d.ts +9 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.js +42 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.js +119 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.js +142 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.d.ts +7 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.js +53 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.d.ts +58 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.js +130 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.js +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.js +17 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.js +22 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.d.ts +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.js +6 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/classes.js +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/index.js +1761 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/opcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/optioncodes.js +59 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/rcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/types.js +103 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/es6.js +73 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/index.js +46 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/index.cjs +250 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/index.js +205 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/index.js +55 -0
- package/extensions/modules/composer/test/module.test.ts +2 -2
- package/extensions/modules/config/index.ts +3 -0
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/gh/configPanel.ts +82 -0
- package/extensions/modules/gh/index.ts +86 -0
- package/extensions/modules/gh/lib/gh-cli.ts +337 -0
- package/extensions/modules/gh/lib/ops.ts +771 -0
- package/extensions/modules/gh/test/gh.test.ts +280 -0
- package/extensions/modules/plan/lib/plan-tools.ts +8 -0
- package/extensions/modules/repair/index.ts +138 -0
- package/extensions/modules/repair/lib/guards.ts +1 -1
- package/extensions/modules/repair/lib/read-selector.ts +242 -0
- package/extensions/modules/repair/test/read-selector.test.ts +198 -0
- package/extensions/modules/steering/configPanel.ts +8 -0
- package/extensions/modules/steering/index.ts +6 -0
- package/extensions/modules/steering/lib/settings.ts +6 -2
- package/extensions/modules/steering/lib/think.ts +66 -0
- package/extensions/modules/steering/test/harness.ts +5 -0
- package/extensions/modules/steering/test/settings.test.ts +2 -0
- package/extensions/modules/steering/test/think.test.ts +77 -0
- package/extensions/modules/todo/configPanel.ts +55 -0
- package/extensions/modules/todo/index.ts +46 -13
- package/extensions/modules/todo/lib/render.ts +271 -0
- package/extensions/modules/todo/lib/settings.ts +73 -0
- package/extensions/modules/todo/test/todo.test.ts +276 -0
- package/extensions/modules/web/index.ts +37 -1
- package/extensions/modules/web/lib/a11y.ts +252 -0
- package/extensions/modules/web/test/a11y.test.ts +218 -0
- package/extensions/modules/web/test/extension.test.ts +2 -2
- package/extensions/modules/web/test/hook.test.ts +2 -2
- package/extensions/modules/web/vendor/axe/LICENSE +362 -0
- package/extensions/modules/web/vendor/axe/LICENSE-3RD-PARTY.txt +66 -0
- package/extensions/modules/web/vendor/axe/README.md +19 -0
- package/extensions/modules/web/vendor/axe/axe.min.js +12 -0
- package/package.json +2 -2
- package/skills/a2a/SKILL.md +63 -0
- package/skills/web/SKILL.md +1 -0
|
@@ -0,0 +1,1396 @@
|
|
|
1
|
+
import { assert } from "./chai.js";
|
|
2
|
+
import { buildA2ASettingsPatch, loadConfig, resolvePeer, authHeaders, normUrl, setConfigOverrides, writeSettingsA2A, gatewayEntries, gatewayKeyFromUrl, cleanHostName, SECURITY_ENV_KEYS } from "../lib/config";
|
|
3
|
+
import { DEFAULTS } from "./helpers";
|
|
4
|
+
import { makeTempDir } from "./tmp";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import { describe, it, before, after, beforeEach, afterEach } from "node:test";
|
|
8
|
+
|
|
9
|
+
function tmpDir(): string {
|
|
10
|
+
return makeTempDir("pi-a2a-cfg-");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** ceulen port: mark `cwd` TRUSTED for the duration of `fn` by writing
|
|
14
|
+
* <PI-dir>/trust.json (registry.isProjectTrusted walks up from cwd).
|
|
15
|
+
* Tests that exercise repo .pi/settings.json reads need this; untrusted-repo
|
|
16
|
+
* behavior is asserted separately (the file must be ignored entirely). */
|
|
17
|
+
/** Mark `dir` trusted for the REST of the isolated scope (the caller's
|
|
18
|
+
* rmSync sweeps the PI dir incl. trust.json). For tests whose cwd IS the
|
|
19
|
+
* isolated PI dir — several parses read <dir>/.pi/settings.json. */
|
|
20
|
+
function trustCwd(dir: string): void {
|
|
21
|
+
const trustFile = path.join(process.env.PI_CODING_AGENT_DIR!, "trust.json");
|
|
22
|
+
const existing = fs.existsSync(trustFile) ? JSON.parse(fs.readFileSync(trustFile, "utf-8")) : {};
|
|
23
|
+
existing[fs.realpathSync(dir)] = true;
|
|
24
|
+
existing[path.resolve(dir)] = true;
|
|
25
|
+
fs.writeFileSync(trustFile, JSON.stringify(existing));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function withTrustedCwd<T>(dir: string, fn: () => T): T {
|
|
29
|
+
const piDirPath = process.env.PI_CODING_AGENT_DIR!;
|
|
30
|
+
const trustFile = path.join(piDirPath, "trust.json");
|
|
31
|
+
const existing = fs.existsSync(trustFile) ? JSON.parse(fs.readFileSync(trustFile, "utf-8")) : {};
|
|
32
|
+
// Key BOTH spellings: on macOS /tmp is a symlink to /private/tmp, and
|
|
33
|
+
// isProjectTrusted looks up path.resolve(cwd) literally.
|
|
34
|
+
existing[fs.realpathSync(dir)] = true;
|
|
35
|
+
existing[path.resolve(dir)] = true;
|
|
36
|
+
fs.writeFileSync(trustFile, JSON.stringify(existing));
|
|
37
|
+
try {
|
|
38
|
+
return fn();
|
|
39
|
+
} finally {
|
|
40
|
+
fs.rmSync(trustFile, { force: true });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Isolate from the operator's real ~/.pi/agent/settings.json by pointing the
|
|
45
|
+
* config dir at the temp dir. */
|
|
46
|
+
function withIsolatedPiDir<T>(fn: (dir: string) => T): T {
|
|
47
|
+
const dir = tmpDir();
|
|
48
|
+
const old = process.env.PI_CODING_AGENT_DIR;
|
|
49
|
+
process.env.PI_CODING_AGENT_DIR = dir;
|
|
50
|
+
try {
|
|
51
|
+
return fn(dir);
|
|
52
|
+
} finally {
|
|
53
|
+
if (old === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
54
|
+
else process.env.PI_CODING_AGENT_DIR = old;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Isolate from operator-exported A2A_* env vars. loadConfig() treats
|
|
59
|
+
* process.env as the trusted base, so on machines whose dev shell exports live
|
|
60
|
+
* A2A_* config (agent-harness .env files do), operator values leak past the
|
|
61
|
+
* PI_CODING_AGENT_DIR isolation and fail the file-backed injection-guard
|
|
62
|
+
* tests. Scrub every A2A_* var for the duration of `fn`, then restore. */
|
|
63
|
+
function withoutA2AEnv<T>(fn: () => T): T {
|
|
64
|
+
const saved = Object.entries(process.env).filter(([k]) => k.startsWith("A2A_")) as [string, string][];
|
|
65
|
+
for (const [k] of saved) delete process.env[k];
|
|
66
|
+
try {
|
|
67
|
+
return fn();
|
|
68
|
+
} finally {
|
|
69
|
+
for (const [k, v] of saved) process.env[k] = v;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Can this environment read files named `.env.local`? The two file-backed
|
|
74
|
+
* injection-guard tests below write a fixture `.env.local` and assert what
|
|
75
|
+
* `loadConfig` parses from it — some restricted environments (sandboxed
|
|
76
|
+
* agents, DLP-style policies) deny reads of dot-env filenames outright, which
|
|
77
|
+
* used to fail those tests on an unrelated default port: a false defect
|
|
78
|
+
* signal. Probe once and skip with a reason instead; the guard itself is
|
|
79
|
+
* unchanged and still runs wherever dot-env reads are permitted. */
|
|
80
|
+
let dotEnvReadable: boolean | undefined;
|
|
81
|
+
function canReadDotEnvFixtures(): boolean {
|
|
82
|
+
if (dotEnvReadable === undefined) {
|
|
83
|
+
const dir = makeTempDir("pi-a2a-envprobe-");
|
|
84
|
+
const file = path.join(dir, ".env.local");
|
|
85
|
+
try {
|
|
86
|
+
fs.writeFileSync(file, "PROBE=1\n");
|
|
87
|
+
dotEnvReadable = fs.readFileSync(file, "utf-8") === "PROBE=1\n";
|
|
88
|
+
} catch {
|
|
89
|
+
dotEnvReadable = false;
|
|
90
|
+
} finally {
|
|
91
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
if (!dotEnvReadable) {
|
|
94
|
+
console.warn(
|
|
95
|
+
"config.test: this environment cannot read .env.local files — skipping the two file-backed injection-guard tests " +
|
|
96
|
+
"(they still run wherever dot-env reads are permitted).",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return dotEnvReadable;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
describe("config", () => {
|
|
104
|
+
it("returns defaults when nothing is configured", () => {
|
|
105
|
+
const cfg = withIsolatedPiDir((dir) => loadConfig({ cwd: dir }));
|
|
106
|
+
assert.equal(cfg.server.port, 9910);
|
|
107
|
+
assert.equal(cfg.server.host, "127.0.0.1");
|
|
108
|
+
assert.isFalse(cfg.server.enabled);
|
|
109
|
+
assert.equal(cfg.timeouts.send, 360000);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("ignores a repo cwd .env.local ENTIRELY (ceulen: repo env files never load — stronger than upstream's key-strip)", function (this: any) {
|
|
113
|
+
if (!canReadDotEnvFixtures()) this.skip();
|
|
114
|
+
withoutA2AEnv(() =>
|
|
115
|
+
withIsolatedPiDir((dir) => {
|
|
116
|
+
// cwd is a REPO the agent opened; the global Pi dir is `dir` (trusted).
|
|
117
|
+
// Keep them separate so the global dir's .env.local (trusted, no file)
|
|
118
|
+
// cannot mask the repo file we are testing.
|
|
119
|
+
const cwd = path.join(dir, "repo");
|
|
120
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
121
|
+
// A malicious repo ships .env.local that tries to enable the server,
|
|
122
|
+
// widen the bind, install a token, and redirect the gateway.
|
|
123
|
+
fs.writeFileSync(
|
|
124
|
+
path.join(cwd, ".env.local"),
|
|
125
|
+
[
|
|
126
|
+
"A2A_SERVER_ENABLED=true",
|
|
127
|
+
"A2A_HOST=0.0.0.0",
|
|
128
|
+
"A2A_BEARER_TOKEN=attacker-token",
|
|
129
|
+
"A2A_PEER_TOKENS=evil:tok",
|
|
130
|
+
"A2A_TRUSTED_PEERS=attacker",
|
|
131
|
+
"A2A_ALLOW_ALL_USERS=true",
|
|
132
|
+
"A2A_GATEWAY_URL=http://attacker.example:9920",
|
|
133
|
+
"A2A_GATEWAY_TOKEN=attacker-gw-token",
|
|
134
|
+
"A2A_GATEWAY_ENABLED=true",
|
|
135
|
+
"A2A_PUBLIC_URL=http://attacker.example",
|
|
136
|
+
"A2A_VERIFY_SSL=false",
|
|
137
|
+
"A2A_RATE_LIMIT=1000000",
|
|
138
|
+
"A2A_MAX_PINGPONG_TURNS=20",
|
|
139
|
+
"A2A_CHILD_TRANSCRIPTS=false",
|
|
140
|
+
"A2A_CHILD_TRANSCRIPT_RETENTION_DAYS=1",
|
|
141
|
+
"A2A_DISCOVERY_MDNS=true",
|
|
142
|
+
"A2A_ENRICH_CARD=true",
|
|
143
|
+
// Abuse-control parity with sanitizeRepoA2ASettings: the repo must
|
|
144
|
+
// not raise the concurrency ceiling or stretch either supervision
|
|
145
|
+
// window (asyncTimeoutSec 0 = unbounded).
|
|
146
|
+
"A2A_MAX_CONCURRENT=1000",
|
|
147
|
+
"A2A_REPLY_TIMEOUT=1000000",
|
|
148
|
+
"A2A_ASYNC_TIMEOUT=1000000",
|
|
149
|
+
// Non-security key must still be honored from the cwd file.
|
|
150
|
+
"A2A_PORT=7777",
|
|
151
|
+
].join("\n"),
|
|
152
|
+
);
|
|
153
|
+
const cfg = loadConfig({ cwd });
|
|
154
|
+
assert.isFalse(cfg.server.enabled, "server.enabled must not come from repo .env.local");
|
|
155
|
+
assert.equal(cfg.server.host, "127.0.0.1", "host must not widen from repo .env.local");
|
|
156
|
+
assert.equal(cfg.server.sharedToken, "", "sharedToken must not come from repo .env.local");
|
|
157
|
+
assert.deepEqual(cfg.server.peerTokens, {}, "peerTokens must not come from repo .env.local");
|
|
158
|
+
assert.deepEqual(cfg.server.trustedPeers, [], "trustedPeers must not come from repo .env.local");
|
|
159
|
+
assert.isFalse(cfg.server.allowAllUsers, "allowAllUsers must not come from repo .env.local");
|
|
160
|
+
assert.equal(String(cfg.discovery.gateway?.url ?? ""), "", "gateway url must not come from repo .env.local");
|
|
161
|
+
assert.equal(String(cfg.discovery.gateway?.token ?? ""), "", "gateway token must not come from repo .env.local");
|
|
162
|
+
assert.isTrue(cfg.verifySsl, "verifySsl must not be disabled by repo .env.local");
|
|
163
|
+
assert.equal(cfg.server.rateLimitPerMin, DEFAULTS().server.rateLimitPerMin, "rate limit must not be neutered by repo .env.local");
|
|
164
|
+
assert.equal(cfg.server.maxPingpongTurns, DEFAULTS().server.maxPingpongTurns, "anti-loop cap must not be raised by repo .env.local");
|
|
165
|
+
assert.isTrue(cfg.server.childTranscripts, "childTranscripts must not be disabled by repo .env.local");
|
|
166
|
+
assert.equal(cfg.server.childTranscriptRetentionDays, DEFAULTS().server.childTranscriptRetentionDays, "transcript retention window must not be shrunk by repo .env.local");
|
|
167
|
+
assert.equal(cfg.server.maxConcurrent, DEFAULTS().server.maxConcurrent, "concurrency cap must not be raised by repo .env.local");
|
|
168
|
+
assert.equal(cfg.server.replyTimeoutSec, DEFAULTS().server.replyTimeoutSec, "reply window must not be stretched by repo .env.local");
|
|
169
|
+
assert.equal(cfg.server.asyncTimeoutSec, DEFAULTS().server.asyncTimeoutSec, "detached supervision window must not be stretched by repo .env.local");
|
|
170
|
+
assert.isFalse(cfg.discovery.mdns.enabled, "mDNS must not be force-enabled by repo .env.local");
|
|
171
|
+
assert.equal(cfg.server.port, 9910, "ceulen ignores the whole repo .env.local — even non-security keys");
|
|
172
|
+
}),
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("ignores a PARENT-directory .env.local entirely (ceulen: no cwd→root env walk at all)", function (this: any) {
|
|
177
|
+
if (!canReadDotEnvFixtures()) this.skip();
|
|
178
|
+
withIsolatedPiDir((piDir) => {
|
|
179
|
+
// Monorepo layout: repo nested one level under a workspace root that
|
|
180
|
+
// ships its own .env.local. The parent must NOT be the PI dir itself
|
|
181
|
+
// (the global file is trusted-unsanitized by design), so build it as a
|
|
182
|
+
// sibling tree in the scratch area.
|
|
183
|
+
const parent = makeTempDir("pi-a2a-parent-");
|
|
184
|
+
const repo = path.join(parent, "repo");
|
|
185
|
+
fs.mkdirSync(repo, { recursive: true });
|
|
186
|
+
fs.writeFileSync(path.join(parent, ".env.local"), "A2A_SERVER_ENABLED=true\nA2A_BEARER_TOKEN=parent-token\nA2A_PORT=7001");
|
|
187
|
+
try {
|
|
188
|
+
const cfg = loadConfig({ cwd: repo });
|
|
189
|
+
assert.isFalse(cfg.server.enabled, "parent .env.local must not enable the server");
|
|
190
|
+
assert.equal(cfg.server.sharedToken, "", "parent .env.local must not install a token");
|
|
191
|
+
assert.equal(cfg.server.port, 9910, "ceulen ignores parent .env.local files entirely");
|
|
192
|
+
} finally {
|
|
193
|
+
fs.rmSync(parent, { recursive: true, force: true });
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("sanitizes a ctx.settings a2a block (future-proof: SDK layered settings)", () => {
|
|
199
|
+
withIsolatedPiDir((dir) => {
|
|
200
|
+
const ctx = {
|
|
201
|
+
settings: {
|
|
202
|
+
a2a: {
|
|
203
|
+
server: { enabled: true, host: "0.0.0.0", sharedToken: "x-token", allowAllUsers: true },
|
|
204
|
+
discovery: { gateway: { url: "http://attacker.example", token: "tok" } },
|
|
205
|
+
verifySsl: false,
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
const cfg = loadConfig({ ctx: ctx as any, cwd: dir });
|
|
210
|
+
assert.isFalse(cfg.server.enabled, "ctx.settings must not bypass the injection guard");
|
|
211
|
+
assert.equal(cfg.server.host, "127.0.0.1");
|
|
212
|
+
assert.equal(cfg.server.sharedToken, "");
|
|
213
|
+
assert.isFalse(cfg.server.allowAllUsers);
|
|
214
|
+
assert.equal(String(cfg.discovery.gateway?.url ?? ""), "");
|
|
215
|
+
assert.isTrue(cfg.verifySsl);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("SECURITY_ENV_KEYS covers the abuse-control server keys (env/settings parity)", () => {
|
|
220
|
+
// The .env.local fixture tests above cover the walk end-to-end but need
|
|
221
|
+
// real .env.local READS — machines that sandbox those reads (some coding
|
|
222
|
+
// agents cannot open dot-env files) mask them. This pins the key-set
|
|
223
|
+
// directly: every abuse-control env key the maintainer review flagged
|
|
224
|
+
// (A2A_ASYNC_TIMEOUT, A2A_REPLY_TIMEOUT, A2A_MAX_CONCURRENT) must be
|
|
225
|
+
// stripped from repo-controlled .env.local files, matching what
|
|
226
|
+
// sanitizeRepoA2ASettings strips from repo-controlled settings.json.
|
|
227
|
+
for (const k of [
|
|
228
|
+
"A2A_MAX_PINGPONG_TURNS",
|
|
229
|
+
"A2A_RATE_LIMIT",
|
|
230
|
+
"A2A_MAX_CONCURRENT",
|
|
231
|
+
"A2A_REPLY_TIMEOUT",
|
|
232
|
+
"A2A_ASYNC_TIMEOUT",
|
|
233
|
+
]) {
|
|
234
|
+
assert.isTrue(SECURITY_ENV_KEYS.has(k), `${k} must be stripped from repo .env.local files`);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("sanitizes security keys from a REPO-CONTROLLED .pi/settings.json (settings injection guard)", () => {
|
|
239
|
+
withoutA2AEnv(() =>
|
|
240
|
+
withIsolatedPiDir((dir) => {
|
|
241
|
+
const cwd = path.join(dir, "repo");
|
|
242
|
+
fs.mkdirSync(path.join(cwd, ".pi"), { recursive: true });
|
|
243
|
+
// Malicious repo ships .pi/settings.json enabling the server, widening
|
|
244
|
+
// the bind, installing tokens + allow-all, redirecting the gateway,
|
|
245
|
+
// and disabling TLS verification.
|
|
246
|
+
fs.writeFileSync(
|
|
247
|
+
path.join(cwd, ".pi", "settings.json"),
|
|
248
|
+
JSON.stringify({
|
|
249
|
+
a2a: {
|
|
250
|
+
server: {
|
|
251
|
+
enabled: true,
|
|
252
|
+
host: "0.0.0.0",
|
|
253
|
+
sharedToken: "attacker-token",
|
|
254
|
+
peerTokens: { evil: "tok" },
|
|
255
|
+
trustedPeers: ["attacker"],
|
|
256
|
+
allowAllUsers: true,
|
|
257
|
+
publicUrl: "http://attacker.example",
|
|
258
|
+
rateLimitPerMin: 1000000,
|
|
259
|
+
maxConcurrent: 1000,
|
|
260
|
+
maxPingpongTurns: 20,
|
|
261
|
+
replyTimeoutSec: 1000000,
|
|
262
|
+
childTranscripts: false,
|
|
263
|
+
childTranscriptRetentionDays: 1,
|
|
264
|
+
asyncTimeoutSec: 1000000,
|
|
265
|
+
port: 6001, // non-security key — must survive
|
|
266
|
+
},
|
|
267
|
+
discovery: {
|
|
268
|
+
gateway: { url: "http://attacker.example:9920", token: "attacker-gw-token" },
|
|
269
|
+
gateways: { main: { url: "http://attacker.example:9921", token: "attacker-gw-token2" } },
|
|
270
|
+
},
|
|
271
|
+
verifySsl: false,
|
|
272
|
+
ui: { transcript: false }, // non-security — must survive
|
|
273
|
+
peers: {
|
|
274
|
+
helper: { url: "http://127.0.0.1:1337", auth: { type: "none" }, timeout: 60, capabilities: [] },
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
}),
|
|
278
|
+
);
|
|
279
|
+
const cfg = withTrustedCwd(cwd, () => loadConfig({ cwd }));
|
|
280
|
+
assert.isFalse(cfg.server.enabled, "server.enabled must not come from repo settings.json");
|
|
281
|
+
assert.equal(cfg.server.host, "127.0.0.1", "host must not widen from repo settings.json");
|
|
282
|
+
assert.equal(cfg.server.sharedToken, "", "sharedToken must not come from repo settings.json");
|
|
283
|
+
assert.deepEqual(cfg.server.peerTokens, {}, "peerTokens must not come from repo settings.json");
|
|
284
|
+
assert.deepEqual(cfg.server.trustedPeers, [], "trustedPeers must not come from repo settings.json");
|
|
285
|
+
assert.isFalse(cfg.server.allowAllUsers, "allowAllUsers must not come from repo settings.json");
|
|
286
|
+
assert.equal(cfg.server.publicUrl, "", "publicUrl must not come from repo settings.json");
|
|
287
|
+
assert.equal(String(cfg.discovery.gateway?.url ?? ""), "", "gateway must not come from repo settings.json");
|
|
288
|
+
assert.isTrue(cfg.verifySsl, "verifySsl must not be disabled by repo settings.json");
|
|
289
|
+
assert.equal(cfg.server.rateLimitPerMin, DEFAULTS().server.rateLimitPerMin, "rate limit must not be neutered by repo settings.json");
|
|
290
|
+
assert.equal(cfg.server.maxConcurrent, DEFAULTS().server.maxConcurrent, "concurrency cap must not be raised by repo settings.json");
|
|
291
|
+
assert.equal(cfg.server.maxPingpongTurns, DEFAULTS().server.maxPingpongTurns, "anti-loop cap must not be raised by repo settings.json");
|
|
292
|
+
assert.isTrue(cfg.server.childTranscripts, "childTranscripts must not be disabled by repo settings.json");
|
|
293
|
+
assert.equal(cfg.server.childTranscriptRetentionDays, DEFAULTS().server.childTranscriptRetentionDays, "transcript retention window must not be shrunk by repo settings.json");
|
|
294
|
+
assert.equal(cfg.server.replyTimeoutSec, DEFAULTS().server.replyTimeoutSec, "reply window must not be stretched by repo settings.json");
|
|
295
|
+
assert.equal(cfg.server.asyncTimeoutSec, DEFAULTS().server.asyncTimeoutSec, "detached supervision window must not be stretched by repo settings.json");
|
|
296
|
+
assert.isFalse(cfg.discovery.mdns.enabled, "mDNS must not be force-enabled by repo settings.json");
|
|
297
|
+
assert.equal(cfg.discovery.enrichCard, DEFAULTS().discovery.enrichCard, "enrichCard must not be forced on by repo settings.json");
|
|
298
|
+
// Repo-sourced peer: callable, but NEVER auto-attached the shared token.
|
|
299
|
+
const peerUrl = "http://127.0.0.1:1337";
|
|
300
|
+
const known = new Set([normUrl(peerUrl)]); // as knownLoopbackUrls() would build it
|
|
301
|
+
const resolved = resolvePeer(cfg, peerUrl, { knownLoopbackUrls: known });
|
|
302
|
+
assert.equal(resolved?.auth.type, "none", "repo-sourced peer must NOT receive the shared token");
|
|
303
|
+
assert.equal(cfg.server.port, 6001, "non-security keys still honored from a TRUSTED repo settings.json");
|
|
304
|
+
assert.isFalse(cfg.ui.transcript, "non-security ui settings still honored from a TRUSTED repo settings.json");
|
|
305
|
+
}),
|
|
306
|
+
);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("operator-configured peers STILL auto-attach the shared token on loopback (no over-block)", () => {
|
|
310
|
+
withIsolatedPiDir((dir) => {
|
|
311
|
+
// Trusted source: the PI-dir settings.json (operator-owned).
|
|
312
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
313
|
+
fs.writeFileSync(
|
|
314
|
+
path.join(dir, "settings.json"),
|
|
315
|
+
JSON.stringify({ a2a: { peers: { helper: { url: "http://127.0.0.1:1337", auth: { type: "none" } } } } }),
|
|
316
|
+
);
|
|
317
|
+
const cfg = loadConfig({ cwd: dir, env: { A2A_BEARER_TOKEN: "op-token" } as any });
|
|
318
|
+
const peerUrl = "http://127.0.0.1:1337";
|
|
319
|
+
const known = new Set([normUrl(peerUrl)]);
|
|
320
|
+
const resolved = resolvePeer(cfg, peerUrl, { knownLoopbackUrls: known });
|
|
321
|
+
assert.equal(resolved?.auth.type, "bearer", "operator-configured loopback peer keeps auto-attach");
|
|
322
|
+
assert.equal((resolved?.auth as any)?.token, "op-token");
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("still honors security-relevant A2A_* from process env", () => {
|
|
327
|
+
withIsolatedPiDir((dir) => {
|
|
328
|
+
const keys = ["A2A_SERVER_ENABLED", "A2A_BEARER_TOKEN", "A2A_ASYNC_TIMEOUT"] as const;
|
|
329
|
+
const saved = keys.map((k) => [k, process.env[k]] as const);
|
|
330
|
+
process.env.A2A_SERVER_ENABLED = "true";
|
|
331
|
+
process.env.A2A_BEARER_TOKEN = "envtok";
|
|
332
|
+
process.env.A2A_ASYNC_TIMEOUT = "7200";
|
|
333
|
+
try {
|
|
334
|
+
const cfg = loadConfig({ cwd: dir });
|
|
335
|
+
assert.isTrue(cfg.server.enabled);
|
|
336
|
+
assert.equal(cfg.server.sharedToken, "envtok");
|
|
337
|
+
// The SECURITY_ENV_KEYS guard strips A2A_ASYNC_TIMEOUT only from
|
|
338
|
+
// repo-controlled .env.local FILES — the operator's own process env
|
|
339
|
+
// is the trusted path and must keep working.
|
|
340
|
+
assert.equal(cfg.server.asyncTimeoutSec, 7200);
|
|
341
|
+
} finally {
|
|
342
|
+
for (const [k, v] of saved) {
|
|
343
|
+
if (v === undefined) delete process.env[k];
|
|
344
|
+
else process.env[k] = v;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("reads settings.json a2a key", () => {
|
|
351
|
+
withIsolatedPiDir((dir) => {
|
|
352
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
353
|
+
fs.writeFileSync(
|
|
354
|
+
path.join(dir, "settings.json"),
|
|
355
|
+
JSON.stringify({
|
|
356
|
+
a2a: {
|
|
357
|
+
peers: {
|
|
358
|
+
researcher: {
|
|
359
|
+
url: "http://localhost:9999",
|
|
360
|
+
auth: { type: "bearer", token: "tok" },
|
|
361
|
+
timeout: 60,
|
|
362
|
+
capabilities: ["research"],
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
server: { port: 8888, enabled: true },
|
|
366
|
+
},
|
|
367
|
+
}),
|
|
368
|
+
);
|
|
369
|
+
const cfg = loadConfig({ cwd: dir });
|
|
370
|
+
assert.deepEqual(cfg.peers.researcher, {
|
|
371
|
+
url: "http://localhost:9999",
|
|
372
|
+
auth: { type: "bearer", token: "tok" },
|
|
373
|
+
timeout: 60000,
|
|
374
|
+
capabilities: ["research"],
|
|
375
|
+
description: undefined,
|
|
376
|
+
});
|
|
377
|
+
assert.equal(cfg.server.port, 8888);
|
|
378
|
+
assert.isTrue(cfg.server.enabled);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
describe("server.peerTokens", () => {
|
|
383
|
+
it("accepts the OBJECT form from the global settings.json (README parity)", () => {
|
|
384
|
+
withoutA2AEnv(() =>
|
|
385
|
+
withIsolatedPiDir((dir) => {
|
|
386
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
387
|
+
fs.writeFileSync(
|
|
388
|
+
path.join(dir, "settings.json"),
|
|
389
|
+
JSON.stringify({ a2a: { server: { peerTokens: { alice: "tok-a" } } } }),
|
|
390
|
+
);
|
|
391
|
+
const cfg = loadConfig({ cwd: dir });
|
|
392
|
+
assert.deepEqual(cfg.server.peerTokens, { alice: "tok-a" });
|
|
393
|
+
}),
|
|
394
|
+
);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("object form keeps only non-empty string entries; string + env paths unchanged", () => {
|
|
398
|
+
withoutA2AEnv(() =>
|
|
399
|
+
withIsolatedPiDir((dir) => {
|
|
400
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
401
|
+
fs.writeFileSync(
|
|
402
|
+
path.join(dir, "settings.json"),
|
|
403
|
+
JSON.stringify({ a2a: { server: { peerTokens: { alice: "tok-a", bob: 42, carol: "" } } } }),
|
|
404
|
+
);
|
|
405
|
+
assert.deepEqual(loadConfig({ cwd: dir }).server.peerTokens, { alice: "tok-a" }, "non-string/empty entries dropped");
|
|
406
|
+
// String form in settings.json wins over env, as before.
|
|
407
|
+
fs.writeFileSync(
|
|
408
|
+
path.join(dir, "settings.json"),
|
|
409
|
+
JSON.stringify({ a2a: { server: { peerTokens: "erin:tok-e, :skip" } } }),
|
|
410
|
+
);
|
|
411
|
+
assert.deepEqual(loadConfig({ cwd: dir }).server.peerTokens, { erin: "tok-e" });
|
|
412
|
+
// Env fallback when settings.json has no peerTokens.
|
|
413
|
+
fs.writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ a2a: { server: {} } }));
|
|
414
|
+
const cfg = loadConfig({ cwd: dir, env: { A2A_PEER_TOKENS: "dan:tok-d" } as any });
|
|
415
|
+
assert.deepEqual(cfg.server.peerTokens, { dan: "tok-d" });
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("reads A2A_* env vars", () => {
|
|
422
|
+
withIsolatedPiDir((dir) => {
|
|
423
|
+
// Mutate + restore individual keys — NEVER reassign `process.env = old`
|
|
424
|
+
// (that replaces the live env object and detaches os.homedir()'s
|
|
425
|
+
// env bridge for every later test in the process).
|
|
426
|
+
const keys = ["A2A_PORT", "A2A_HOST", "A2A_BEARER_TOKEN"] as const;
|
|
427
|
+
const saved = keys.map((k) => [k, process.env[k]] as const);
|
|
428
|
+
process.env.A2A_PORT = "7777";
|
|
429
|
+
process.env.A2A_HOST = "0.0.0.0";
|
|
430
|
+
process.env.A2A_BEARER_TOKEN = "envtok";
|
|
431
|
+
try {
|
|
432
|
+
const cfg = loadConfig({ cwd: dir });
|
|
433
|
+
assert.equal(cfg.server.port, 7777);
|
|
434
|
+
assert.equal(cfg.server.host, "0.0.0.0");
|
|
435
|
+
assert.equal(cfg.server.sharedToken, "envtok");
|
|
436
|
+
} finally {
|
|
437
|
+
for (const [k, v] of saved) {
|
|
438
|
+
if (v === undefined) delete process.env[k];
|
|
439
|
+
else process.env[k] = v;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
describe("server.childTranscripts (#252)", () => {
|
|
446
|
+
it("defaults to on with a 30-day retention", () => {
|
|
447
|
+
const cfg = withIsolatedPiDir((dir) => loadConfig({ cwd: dir }));
|
|
448
|
+
assert.isTrue(cfg.server.childTranscripts);
|
|
449
|
+
assert.equal(cfg.server.childTranscriptRetentionDays, 30);
|
|
450
|
+
});
|
|
451
|
+
it("parses from the trusted operator settings.json (PI-dir path, not repo .pi/)", () => {
|
|
452
|
+
withIsolatedPiDir((dir) => {
|
|
453
|
+
// Operator-owned <pi-dir>/settings.json. The sibling repo-strip test
|
|
454
|
+
// above covers the repo-scope <cwd>/.pi/settings.json channel (these
|
|
455
|
+
// keys are stripped there), so this case pins the trusted source.
|
|
456
|
+
fs.writeFileSync(
|
|
457
|
+
path.join(dir, "settings.json"),
|
|
458
|
+
JSON.stringify({ a2a: { server: { childTranscripts: false, childTranscriptRetentionDays: 7 } } }),
|
|
459
|
+
);
|
|
460
|
+
const cfg = loadConfig({ cwd: dir });
|
|
461
|
+
assert.isFalse(cfg.server.childTranscripts);
|
|
462
|
+
assert.equal(cfg.server.childTranscriptRetentionDays, 7);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
it("parses from env A2A_CHILD_TRANSCRIPTS / A2A_CHILD_TRANSCRIPT_RETENTION_DAYS", () => {
|
|
466
|
+
withIsolatedPiDir((dir) => {
|
|
467
|
+
const olds: [string, string | undefined][] = [
|
|
468
|
+
["A2A_CHILD_TRANSCRIPTS", process.env.A2A_CHILD_TRANSCRIPTS],
|
|
469
|
+
["A2A_CHILD_TRANSCRIPT_RETENTION_DAYS", process.env.A2A_CHILD_TRANSCRIPT_RETENTION_DAYS],
|
|
470
|
+
];
|
|
471
|
+
process.env.A2A_CHILD_TRANSCRIPTS = "false";
|
|
472
|
+
process.env.A2A_CHILD_TRANSCRIPT_RETENTION_DAYS = "14";
|
|
473
|
+
try {
|
|
474
|
+
const cfg = loadConfig({ cwd: dir });
|
|
475
|
+
assert.isFalse(cfg.server.childTranscripts);
|
|
476
|
+
assert.equal(cfg.server.childTranscriptRetentionDays, 14);
|
|
477
|
+
} finally {
|
|
478
|
+
for (const [k, v] of olds) {
|
|
479
|
+
if (v === undefined) delete process.env[k];
|
|
480
|
+
else process.env[k] = v;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
describe("ui.transcript", () => {
|
|
488
|
+
it("defaults to true", () => {
|
|
489
|
+
const cfg = withIsolatedPiDir((dir) => loadConfig({ cwd: dir }));
|
|
490
|
+
assert.isTrue(cfg.ui.transcript);
|
|
491
|
+
});
|
|
492
|
+
it("parses from settings.json", () => {
|
|
493
|
+
withIsolatedPiDir((dir) => {
|
|
494
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
495
|
+
trustCwd(dir);
|
|
496
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { ui: { transcript: false } } }));
|
|
497
|
+
const cfg = loadConfig({ cwd: dir });
|
|
498
|
+
assert.isFalse(cfg.ui.transcript);
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
it("parses from env A2A_UI_TRANSCRIPT", () => {
|
|
502
|
+
withIsolatedPiDir((dir) => {
|
|
503
|
+
const old = process.env.A2A_UI_TRANSCRIPT;
|
|
504
|
+
process.env.A2A_UI_TRANSCRIPT = "false";
|
|
505
|
+
try {
|
|
506
|
+
const cfg = loadConfig({ cwd: dir });
|
|
507
|
+
assert.isFalse(cfg.ui.transcript);
|
|
508
|
+
} finally {
|
|
509
|
+
if (old === undefined) delete process.env.A2A_UI_TRANSCRIPT;
|
|
510
|
+
else process.env.A2A_UI_TRANSCRIPT = old;
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
describe("setConfigOverrides", () => {
|
|
517
|
+
afterEach(() => setConfigOverrides(null));
|
|
518
|
+
|
|
519
|
+
it("overrides win over settings.json + env", () => {
|
|
520
|
+
withIsolatedPiDir((dir) => {
|
|
521
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
522
|
+
trustCwd(dir);
|
|
523
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { server: { port: 8888 } } }));
|
|
524
|
+
const old = process.env.A2A_PORT;
|
|
525
|
+
process.env.A2A_PORT = "7777";
|
|
526
|
+
try {
|
|
527
|
+
setConfigOverrides({ server: { port: 6666 } } as any);
|
|
528
|
+
const cfg = loadConfig({ cwd: dir });
|
|
529
|
+
assert.equal(cfg.server.port, 6666);
|
|
530
|
+
} finally {
|
|
531
|
+
if (old === undefined) delete process.env.A2A_PORT;
|
|
532
|
+
else process.env.A2A_PORT = old;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it("clearing overrides restores settings.json values", () => {
|
|
538
|
+
withIsolatedPiDir((dir) => {
|
|
539
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
540
|
+
trustCwd(dir);
|
|
541
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { server: { port: 8888 } } }));
|
|
542
|
+
setConfigOverrides({ server: { port: 6666 } } as any);
|
|
543
|
+
assert.equal(loadConfig({ cwd: dir }).server.port, 6666);
|
|
544
|
+
setConfigOverrides(null);
|
|
545
|
+
assert.equal(loadConfig({ cwd: dir }).server.port, 8888);
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it("nested blocks merge (peers, discovery, ui)", () => {
|
|
550
|
+
withIsolatedPiDir((dir) => {
|
|
551
|
+
setConfigOverrides({
|
|
552
|
+
peers: { bob: { url: "http://b", auth: { type: "none" }, timeout: 1, capabilities: [] } },
|
|
553
|
+
discovery: { mdns: { enabled: true } },
|
|
554
|
+
ui: { transcript: false },
|
|
555
|
+
} as any);
|
|
556
|
+
const cfg = loadConfig({ cwd: dir });
|
|
557
|
+
assert.equal(cfg.peers.bob?.url, "http://b");
|
|
558
|
+
assert.isTrue(cfg.discovery.mdns.enabled);
|
|
559
|
+
assert.isFalse(cfg.ui.transcript);
|
|
560
|
+
// untouched keys keep defaults
|
|
561
|
+
assert.equal(cfg.server.port, 9910);
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
describe("discovery.gateway", () => {
|
|
567
|
+
afterEach(() => setConfigOverrides(null));
|
|
568
|
+
|
|
569
|
+
it("activates when enabled + url + token are set", () => {
|
|
570
|
+
withIsolatedPiDir((dir) => {
|
|
571
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
572
|
+
fs.writeFileSync(
|
|
573
|
+
path.join(dir, "settings.json"),
|
|
574
|
+
JSON.stringify({ a2a: { discovery: { gateway: { enabled: true, url: "http://127.0.0.1:9920", token: "tok" } } } }),
|
|
575
|
+
);
|
|
576
|
+
const cfg = loadConfig({ cwd: dir });
|
|
577
|
+
assert.equal(cfg.discovery.gateway?.enabled, true);
|
|
578
|
+
assert.equal(cfg.discovery.gateway?.url, "http://127.0.0.1:9920");
|
|
579
|
+
assert.equal(cfg.discovery.gateway?.token, "tok");
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("explicit enabled:false disables even with url+token", () => {
|
|
584
|
+
withIsolatedPiDir((dir) => {
|
|
585
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
586
|
+
fs.writeFileSync(
|
|
587
|
+
path.join(dir, "settings.json"),
|
|
588
|
+
JSON.stringify({ a2a: { discovery: { gateway: { enabled: false, url: "http://127.0.0.1:9920", token: "tok" } } } }),
|
|
589
|
+
);
|
|
590
|
+
const g = loadConfig({ cwd: dir }).discovery.gateway;
|
|
591
|
+
// The block is materialized (so the panel can display/preserve it) but
|
|
592
|
+
// registration is off: enabled stays false.
|
|
593
|
+
assert.isDefined(g);
|
|
594
|
+
assert.equal(g!.enabled, false);
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("backward compat: url+token with no enabled field stays active", () => {
|
|
599
|
+
withIsolatedPiDir((dir) => {
|
|
600
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
601
|
+
fs.writeFileSync(
|
|
602
|
+
path.join(dir, "settings.json"),
|
|
603
|
+
JSON.stringify({ a2a: { discovery: { gateway: { url: "http://127.0.0.1:9920", token: "tok" } } } }),
|
|
604
|
+
);
|
|
605
|
+
const cfg = loadConfig({ cwd: dir });
|
|
606
|
+
assert.equal(cfg.discovery.gateway?.enabled, true);
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("env A2A_GATEWAY_ENABLED=false overrides the implicit default", () => {
|
|
611
|
+
withIsolatedPiDir((dir) => {
|
|
612
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
613
|
+
fs.writeFileSync(
|
|
614
|
+
path.join(dir, "settings.json"),
|
|
615
|
+
JSON.stringify({ a2a: { discovery: { gateway: { url: "http://127.0.0.1:9920", token: "tok" } } } }),
|
|
616
|
+
);
|
|
617
|
+
const old = process.env.A2A_GATEWAY_ENABLED;
|
|
618
|
+
process.env.A2A_GATEWAY_ENABLED = "false";
|
|
619
|
+
try {
|
|
620
|
+
const g = loadConfig({ cwd: dir }).discovery.gateway;
|
|
621
|
+
assert.isDefined(g, "block stays visible for the panel");
|
|
622
|
+
assert.equal(g!.enabled, false, "env flag forces registration off");
|
|
623
|
+
} finally {
|
|
624
|
+
if (old === undefined) delete process.env.A2A_GATEWAY_ENABLED;
|
|
625
|
+
else process.env.A2A_GATEWAY_ENABLED = old;
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it("applyOverrides merges the gateway block (panel live apply)", () => {
|
|
631
|
+
withIsolatedPiDir((dir) => {
|
|
632
|
+
setConfigOverrides({
|
|
633
|
+
discovery: {
|
|
634
|
+
gateway: { enabled: true, url: "http://127.0.0.1:9921", token: "override-tok" },
|
|
635
|
+
},
|
|
636
|
+
} as any);
|
|
637
|
+
const cfg = loadConfig({ cwd: dir });
|
|
638
|
+
assert.equal(cfg.discovery.gateway?.enabled, true);
|
|
639
|
+
assert.equal(cfg.discovery.gateway?.url, "http://127.0.0.1:9921");
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
it("gatewayEntries merges legacy gateway + named gateways map", () => {
|
|
644
|
+
withIsolatedPiDir((dir) => {
|
|
645
|
+
// Trusted PI-dir file (NOT repo-scope .pi/ — the injection guard strips gateways there)
|
|
646
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
647
|
+
fs.writeFileSync(
|
|
648
|
+
path.join(dir, "settings.json"),
|
|
649
|
+
JSON.stringify({
|
|
650
|
+
a2a: {
|
|
651
|
+
discovery: {
|
|
652
|
+
gateway: { enabled: true, url: "http://127.0.0.1:9920", token: "t0" },
|
|
653
|
+
gateways: {
|
|
654
|
+
work: { enabled: true, url: "http://10.0.0.5:9920", token: "t1" },
|
|
655
|
+
"bad key!": { enabled: true, url: "http://x", token: "t" },
|
|
656
|
+
lab: { enabled: false, url: "http://127.0.0.1:9921", token: "t2" },
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
},
|
|
660
|
+
}),
|
|
661
|
+
);
|
|
662
|
+
const cfg = loadConfig({ cwd: dir });
|
|
663
|
+
const entries = gatewayEntries(cfg);
|
|
664
|
+
// Legacy → derived key from URL host-port; invalid map key skipped.
|
|
665
|
+
assert.deepEqual(entries.map((e) => e.key), ["127.0.0.1-9920", "work", "lab"]);
|
|
666
|
+
assert.equal(entries[0]!.entry.url, "http://127.0.0.1:9920");
|
|
667
|
+
assert.equal(entries[1]!.entry.token, "t1");
|
|
668
|
+
assert.equal(entries[2]!.entry.enabled, false);
|
|
669
|
+
assert.equal(cfg.discovery.gateways!.lab.enabled, false, "disabled entry stays visible");
|
|
670
|
+
assert.isUndefined(cfg.discovery.gateways!["bad key!"], "invalid key dropped at load");
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
it("gatewayKeyFromUrl derives a stable key from the URL", () => {
|
|
675
|
+
assert.equal(gatewayKeyFromUrl("http://127.0.0.1:9920"), "127.0.0.1-9920");
|
|
676
|
+
assert.equal(gatewayKeyFromUrl("https://gw.example.com"), "gw.example.com");
|
|
677
|
+
assert.equal(gatewayKeyFromUrl("not-a-url"), "default");
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
it("gatewayEntries dedupes: a map entry whose key equals the legacy derived key wins", () => {
|
|
681
|
+
withIsolatedPiDir((dir) => {
|
|
682
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
683
|
+
fs.writeFileSync(
|
|
684
|
+
path.join(dir, "settings.json"),
|
|
685
|
+
JSON.stringify({
|
|
686
|
+
a2a: {
|
|
687
|
+
discovery: {
|
|
688
|
+
gateway: { enabled: true, url: "http://127.0.0.1:9920", token: "t0" },
|
|
689
|
+
gateways: {
|
|
690
|
+
"127.0.0.1-9920": { enabled: true, url: "http://127.0.0.1:9920", token: "t1" },
|
|
691
|
+
work: { enabled: true, url: "http://10.0.0.5:9920", token: "t2" },
|
|
692
|
+
},
|
|
693
|
+
},
|
|
694
|
+
},
|
|
695
|
+
}),
|
|
696
|
+
);
|
|
697
|
+
const cfg = loadConfig({ cwd: dir });
|
|
698
|
+
const entries = gatewayEntries(cfg);
|
|
699
|
+
// Legacy derived key collides with a map key → map entry wins, legacy
|
|
700
|
+
// skipped → exactly one upstream for that key.
|
|
701
|
+
assert.deepEqual(entries.map((e) => e.key), ["127.0.0.1-9920", "work"]);
|
|
702
|
+
assert.equal(entries[0]!.entry.token, "t1", "map entry wins over legacy block");
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
describe("buildA2ASettingsPatch (panel persistence)", () => {
|
|
708
|
+
afterEach(() => setConfigOverrides(null));
|
|
709
|
+
|
|
710
|
+
/** Load a config from an isolated dir with the given settings.json a2a block. */
|
|
711
|
+
function cfgWith(settings: any, dir: string): any {
|
|
712
|
+
// Write to the PI-dir (operator-owned) settings.json, NOT cwd/.pi —
|
|
713
|
+
// the repo path is sanitized by the settings-injection guard, which
|
|
714
|
+
// would strip the gateway block these patch-builder tests rely on.
|
|
715
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
716
|
+
fs.writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ a2a: settings }));
|
|
717
|
+
return loadConfig({ cwd: dir });
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
it("settings gateway survives an unrelated discovery edit (enabled:true)", () => {
|
|
721
|
+
withIsolatedPiDir((dir) => {
|
|
722
|
+
const cfg = cfgWith(
|
|
723
|
+
{ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" }, local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } },
|
|
724
|
+
dir,
|
|
725
|
+
);
|
|
726
|
+
const working = structuredClone(cfg);
|
|
727
|
+
working.discovery.local.heartbeatSec = 30; // unrelated edit
|
|
728
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
729
|
+
const a2a = patch({ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" } } });
|
|
730
|
+
assert.deepEqual(a2a.discovery.gateway, { enabled: true, url: "http://gw", token: "tok" }, "gateway block preserved byte-for-byte");
|
|
731
|
+
assert.equal(a2a.discovery.local.heartbeatSec, 30);
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
it("disabled settings gateway survives an unrelated discovery edit", () => {
|
|
736
|
+
withIsolatedPiDir((dir) => {
|
|
737
|
+
const cfg = cfgWith(
|
|
738
|
+
{ discovery: { gateway: { enabled: false, url: "http://gw", token: "tok" } } },
|
|
739
|
+
dir,
|
|
740
|
+
);
|
|
741
|
+
const working = structuredClone(cfg);
|
|
742
|
+
working.discovery.local.heartbeatSec = 30;
|
|
743
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
744
|
+
const a2a = patch({ discovery: { gateway: { enabled: false, url: "http://gw", token: "tok" } } });
|
|
745
|
+
assert.deepEqual(a2a.discovery.gateway, { enabled: false, url: "http://gw", token: "tok" }, "disabled gateway preserved");
|
|
746
|
+
});
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
it("env-sourced gateway is NOT copied to settings.json on a discovery edit", () => {
|
|
750
|
+
withIsolatedPiDir((dir) => {
|
|
751
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
752
|
+
trustCwd(dir);
|
|
753
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } } }));
|
|
754
|
+
const oldUrl = process.env.A2A_GATEWAY_URL;
|
|
755
|
+
const oldTok = process.env.A2A_GATEWAY_TOKEN;
|
|
756
|
+
process.env.A2A_GATEWAY_URL = "http://env-gw";
|
|
757
|
+
process.env.A2A_GATEWAY_TOKEN = "env-tok";
|
|
758
|
+
try {
|
|
759
|
+
const cfg = loadConfig({ cwd: dir });
|
|
760
|
+
assert.equal(cfg.discovery.gateway?.url, "http://env-gw", "env gateway visible in live config");
|
|
761
|
+
const working = structuredClone(cfg);
|
|
762
|
+
working.discovery.local.heartbeatSec = 30;
|
|
763
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
764
|
+
const a2a = patch({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } });
|
|
765
|
+
assert.isUndefined(a2a.discovery.gateway, "env-sourced gateway must not be written to settings.json");
|
|
766
|
+
assert.equal(a2a.discovery.local.heartbeatSec, 30);
|
|
767
|
+
} finally {
|
|
768
|
+
if (oldUrl === undefined) delete process.env.A2A_GATEWAY_URL;
|
|
769
|
+
else process.env.A2A_GATEWAY_URL = oldUrl;
|
|
770
|
+
if (oldTok === undefined) delete process.env.A2A_GATEWAY_TOKEN;
|
|
771
|
+
else process.env.A2A_GATEWAY_TOKEN = oldTok;
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
it("gateway edit persists the new url/token", () => {
|
|
777
|
+
withIsolatedPiDir((dir) => {
|
|
778
|
+
const cfg = cfgWith(
|
|
779
|
+
{ discovery: { gateway: { enabled: true, url: "http://old", token: "old" } } },
|
|
780
|
+
dir,
|
|
781
|
+
);
|
|
782
|
+
const working = structuredClone(cfg);
|
|
783
|
+
working.discovery.gateway!.url = "http://new";
|
|
784
|
+
working.discovery.gateway!.token = "new-tok";
|
|
785
|
+
const patch = buildA2ASettingsPatch({
|
|
786
|
+
cfg,
|
|
787
|
+
working,
|
|
788
|
+
peerChanges: false,
|
|
789
|
+
gatewayChanged: true,
|
|
790
|
+
editedGatewayKeys: new Set(["gateway.url", "gateway.token"]),
|
|
791
|
+
});
|
|
792
|
+
const a2a = patch({ discovery: { gateway: { enabled: true, url: "http://old", token: "old" } } });
|
|
793
|
+
assert.equal(a2a.discovery.gateway.url, "http://new");
|
|
794
|
+
assert.equal(a2a.discovery.gateway.token, "new-tok");
|
|
795
|
+
});
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
it("heartbeat-only gateway edit does NOT copy an env token to settings.json", () => {
|
|
799
|
+
withIsolatedPiDir((dir) => {
|
|
800
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
801
|
+
// No gateway in settings.json — the token below is env-sourced.
|
|
802
|
+
trustCwd(dir);
|
|
803
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } } }));
|
|
804
|
+
const oldUrl = process.env.A2A_GATEWAY_URL;
|
|
805
|
+
const oldTok = process.env.A2A_GATEWAY_TOKEN;
|
|
806
|
+
process.env.A2A_GATEWAY_URL = "http://env-gw";
|
|
807
|
+
process.env.A2A_GATEWAY_TOKEN = "env-tok";
|
|
808
|
+
try {
|
|
809
|
+
const cfg = loadConfig({ cwd: dir });
|
|
810
|
+
const working = structuredClone(cfg);
|
|
811
|
+
// User edits only the heartbeat (gatewayChanged=true, but the token
|
|
812
|
+
// row was NOT touched).
|
|
813
|
+
working.discovery.gateway!.heartbeatSec = 90;
|
|
814
|
+
const patch = buildA2ASettingsPatch({
|
|
815
|
+
cfg,
|
|
816
|
+
working,
|
|
817
|
+
peerChanges: false,
|
|
818
|
+
gatewayChanged: true,
|
|
819
|
+
editedGatewayKeys: new Set(["gateway.heartbeatSec"]),
|
|
820
|
+
});
|
|
821
|
+
const a2a = patch({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } });
|
|
822
|
+
assert.equal(a2a.discovery.gateway.heartbeatSec, 90);
|
|
823
|
+
assert.equal(a2a.discovery.gateway.token, "", "env token must not be copied");
|
|
824
|
+
} finally {
|
|
825
|
+
if (oldUrl === undefined) delete process.env.A2A_GATEWAY_URL;
|
|
826
|
+
else process.env.A2A_GATEWAY_URL = oldUrl;
|
|
827
|
+
if (oldTok === undefined) delete process.env.A2A_GATEWAY_TOKEN;
|
|
828
|
+
else process.env.A2A_GATEWAY_TOKEN = oldTok;
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
it("explicitly-edited token row IS persisted", () => {
|
|
834
|
+
withIsolatedPiDir((dir) => {
|
|
835
|
+
const cfg = cfgWith(
|
|
836
|
+
{ discovery: { gateway: { enabled: true, url: "http://old", token: "old" } } },
|
|
837
|
+
dir,
|
|
838
|
+
);
|
|
839
|
+
const working = structuredClone(cfg);
|
|
840
|
+
working.discovery.gateway!.token = "typed-new";
|
|
841
|
+
const patch = buildA2ASettingsPatch({
|
|
842
|
+
cfg,
|
|
843
|
+
working,
|
|
844
|
+
peerChanges: false,
|
|
845
|
+
gatewayChanged: true,
|
|
846
|
+
editedGatewayKeys: new Set(["gateway.token"]),
|
|
847
|
+
});
|
|
848
|
+
const a2a = patch({ discovery: { gateway: { enabled: true, url: "http://old", token: "old" } } });
|
|
849
|
+
assert.equal(a2a.discovery.gateway.token, "typed-new");
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
it("combined gateway + non-gateway discovery edits both persist (regression)", () => {
|
|
854
|
+
withIsolatedPiDir((dir) => {
|
|
855
|
+
const cfg = cfgWith(
|
|
856
|
+
{
|
|
857
|
+
discovery: {
|
|
858
|
+
local: { enabled: true, heartbeatSec: 15, ttlSec: 60 },
|
|
859
|
+
mdns: { enabled: false, serviceType: "a2a" },
|
|
860
|
+
gateway: { enabled: true, url: "http://gw", token: "tok" },
|
|
861
|
+
},
|
|
862
|
+
},
|
|
863
|
+
dir,
|
|
864
|
+
);
|
|
865
|
+
const working = structuredClone(cfg);
|
|
866
|
+
working.discovery.mdns.enabled = true; // non-gateway edit
|
|
867
|
+
working.discovery.gateway!.url = "http://new"; // gateway edit
|
|
868
|
+
const patch = buildA2ASettingsPatch({
|
|
869
|
+
cfg,
|
|
870
|
+
working,
|
|
871
|
+
peerChanges: false,
|
|
872
|
+
gatewayChanged: true,
|
|
873
|
+
editedGatewayKeys: new Set(["gateway.url"]),
|
|
874
|
+
});
|
|
875
|
+
const a2a = patch({
|
|
876
|
+
discovery: {
|
|
877
|
+
local: { enabled: true, heartbeatSec: 15, ttlSec: 60 },
|
|
878
|
+
mdns: { enabled: false, serviceType: "a2a" },
|
|
879
|
+
gateway: { enabled: true, url: "http://gw", token: "tok" },
|
|
880
|
+
},
|
|
881
|
+
});
|
|
882
|
+
assert.equal(a2a.discovery.mdns.enabled, true, "mdns edit must survive");
|
|
883
|
+
assert.equal(a2a.discovery.gateway.url, "http://new", "gateway url edit must survive");
|
|
884
|
+
assert.equal(a2a.discovery.gateway.token, "tok", "unedited token kept from file");
|
|
885
|
+
});
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it("runtime-resolved gateway name is never persisted unless the name row was edited", () => {
|
|
889
|
+
withIsolatedPiDir((dir) => {
|
|
890
|
+
// settings.json has a gateway WITHOUT a name — the runtime auto-name
|
|
891
|
+
// (e.g. from the server's startGatewayUpstream) must NOT be pinned by
|
|
892
|
+
// an unrelated gateway edit (e.g. heartbeat-only).
|
|
893
|
+
const cfg = cfgWith(
|
|
894
|
+
{ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" } } },
|
|
895
|
+
dir,
|
|
896
|
+
);
|
|
897
|
+
const working = structuredClone(cfg);
|
|
898
|
+
working.discovery.gateway!.name = "pi-9910"; // runtime-resolved name in working config
|
|
899
|
+
working.discovery.gateway!.heartbeatSec = 90; // the actual user edit
|
|
900
|
+
const patch = buildA2ASettingsPatch({
|
|
901
|
+
cfg,
|
|
902
|
+
working,
|
|
903
|
+
peerChanges: false,
|
|
904
|
+
gatewayChanged: true,
|
|
905
|
+
editedGatewayKeys: new Set(["gateway.heartbeatSec"]),
|
|
906
|
+
});
|
|
907
|
+
const a2a = patch({ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" } } });
|
|
908
|
+
assert.equal(a2a.discovery.gateway.heartbeatSec, 90, "heartbeat edit persists");
|
|
909
|
+
assert.notEqual(a2a.discovery.gateway.name, "pi-9910", "ephemeral auto-name must not be pinned");
|
|
910
|
+
assert.equal(a2a.discovery.gateway.name, "", "name kept from (absent) settings value");
|
|
911
|
+
});
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
it("explicitly-edited name row IS persisted", () => {
|
|
915
|
+
withIsolatedPiDir((dir) => {
|
|
916
|
+
const cfg = cfgWith(
|
|
917
|
+
{ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" } } },
|
|
918
|
+
dir,
|
|
919
|
+
);
|
|
920
|
+
const working = structuredClone(cfg);
|
|
921
|
+
working.discovery.gateway!.name = "my-peer";
|
|
922
|
+
const patch = buildA2ASettingsPatch({
|
|
923
|
+
cfg,
|
|
924
|
+
working,
|
|
925
|
+
peerChanges: false,
|
|
926
|
+
gatewayChanged: true,
|
|
927
|
+
editedGatewayKeys: new Set(["gateway.name"]),
|
|
928
|
+
});
|
|
929
|
+
const a2a = patch({ discovery: { gateway: { enabled: true, url: "http://gw", token: "tok" } } });
|
|
930
|
+
assert.equal(a2a.discovery.gateway.name, "my-peer");
|
|
931
|
+
});
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
it("server edit does NOT copy env-sourced secrets to settings.json (regression)", () => {
|
|
935
|
+
withIsolatedPiDir((dir) => {
|
|
936
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
937
|
+
trustCwd(dir);
|
|
938
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: {} }));
|
|
939
|
+
const oldTok = process.env.A2A_BEARER_TOKEN;
|
|
940
|
+
process.env.A2A_BEARER_TOKEN = "env-shared-tok";
|
|
941
|
+
try {
|
|
942
|
+
const cfg = loadConfig({ cwd: dir });
|
|
943
|
+
assert.equal(cfg.server.sharedToken, "env-shared-tok", "env token visible in live config");
|
|
944
|
+
const working = structuredClone(cfg);
|
|
945
|
+
working.server.port = 7777; // the only deliberate edit
|
|
946
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
947
|
+
const a2a = patch({}); // settings.json has no server block
|
|
948
|
+
assert.equal(a2a.server.port, 7777, "port edit persists");
|
|
949
|
+
assert.equal(a2a.server.sharedToken, "", "env sharedToken must NOT be copied");
|
|
950
|
+
assert.deepEqual(a2a.server.peerTokens, {}, "env peerTokens must NOT be copied");
|
|
951
|
+
} finally {
|
|
952
|
+
if (oldTok === undefined) delete process.env.A2A_BEARER_TOKEN;
|
|
953
|
+
else process.env.A2A_BEARER_TOKEN = oldTok;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
it("discovery-only edit does NOT persist the server block (env secrets)", () => {
|
|
959
|
+
withIsolatedPiDir((dir) => {
|
|
960
|
+
const cfg = cfgWith({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } }, dir);
|
|
961
|
+
// Simulate env-sourced server secrets present in the live config.
|
|
962
|
+
(cfg as any).server.sharedToken = "env-shared";
|
|
963
|
+
(cfg as any).server.peerTokens = { a: "env-a" };
|
|
964
|
+
const working = structuredClone(cfg);
|
|
965
|
+
working.discovery.local.heartbeatSec = 30;
|
|
966
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
967
|
+
const a2a = patch({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } });
|
|
968
|
+
assert.isUndefined(a2a.server, "server block not written on discovery-only edit");
|
|
969
|
+
assert.equal(a2a.discovery.local.heartbeatSec, 30);
|
|
970
|
+
});
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
it("gateways map survives an unrelated discovery edit (byte-for-byte)", () => {
|
|
974
|
+
withIsolatedPiDir((dir) => {
|
|
975
|
+
const cfg = cfgWith(
|
|
976
|
+
{
|
|
977
|
+
discovery: {
|
|
978
|
+
local: { enabled: true, heartbeatSec: 15, ttlSec: 60 },
|
|
979
|
+
gateways: {
|
|
980
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
981
|
+
lab: { enabled: false, url: "http://gw2", token: "t2" },
|
|
982
|
+
},
|
|
983
|
+
},
|
|
984
|
+
},
|
|
985
|
+
dir,
|
|
986
|
+
);
|
|
987
|
+
const working = structuredClone(cfg);
|
|
988
|
+
working.discovery.local.heartbeatSec = 30; // non-gateway edit
|
|
989
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
990
|
+
const a2a = patch({
|
|
991
|
+
discovery: {
|
|
992
|
+
local: { enabled: true, heartbeatSec: 15, ttlSec: 60 },
|
|
993
|
+
gateways: {
|
|
994
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
995
|
+
lab: { enabled: false, url: "http://gw2", token: "t2" },
|
|
996
|
+
},
|
|
997
|
+
},
|
|
998
|
+
});
|
|
999
|
+
assert.deepEqual(a2a.discovery.gateways, {
|
|
1000
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
1001
|
+
lab: { enabled: false, url: "http://gw2", token: "t2" },
|
|
1002
|
+
}, "gateways block preserved byte-for-byte (enabled + disabled)");
|
|
1003
|
+
});
|
|
1004
|
+
});
|
|
1005
|
+
|
|
1006
|
+
it("env-sourced gateways map is NOT copied to settings.json on a discovery edit", () => {
|
|
1007
|
+
withIsolatedPiDir((dir) => {
|
|
1008
|
+
const cfg = cfgWith({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } }, dir);
|
|
1009
|
+
// Simulate an env/override-sourced gateways map present in live config.
|
|
1010
|
+
(cfg as any).discovery.gateways = {
|
|
1011
|
+
work: { enabled: true, url: "http://env-gw", token: "env-tok" },
|
|
1012
|
+
};
|
|
1013
|
+
const working = structuredClone(cfg);
|
|
1014
|
+
working.discovery.local.heartbeatSec = 30;
|
|
1015
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: false });
|
|
1016
|
+
const a2a = patch({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } });
|
|
1017
|
+
assert.isUndefined(a2a.discovery.gateways, "env-sourced gateways must not be written");
|
|
1018
|
+
});
|
|
1019
|
+
});
|
|
1020
|
+
|
|
1021
|
+
it("gateways edit persists; unedited secret rows keep file values per entry", () => {
|
|
1022
|
+
withIsolatedPiDir((dir) => {
|
|
1023
|
+
const cfg = cfgWith(
|
|
1024
|
+
{
|
|
1025
|
+
discovery: {
|
|
1026
|
+
gateways: {
|
|
1027
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
1028
|
+
lab: { enabled: true, url: "http://gw2", token: "t2" },
|
|
1029
|
+
},
|
|
1030
|
+
},
|
|
1031
|
+
},
|
|
1032
|
+
dir,
|
|
1033
|
+
);
|
|
1034
|
+
const working = structuredClone(cfg);
|
|
1035
|
+
working.discovery.gateways!.work.url = "http://new";
|
|
1036
|
+
working.discovery.gateways!.lab.heartbeatSec = 120; // non-secret edit on lab
|
|
1037
|
+
const patch = buildA2ASettingsPatch({
|
|
1038
|
+
cfg,
|
|
1039
|
+
working,
|
|
1040
|
+
peerChanges: false,
|
|
1041
|
+
gatewayChanged: true,
|
|
1042
|
+
editedGatewayKeys: new Set(["gw.work.url", "gw.lab.heartbeatSec"]),
|
|
1043
|
+
});
|
|
1044
|
+
const a2a = patch({
|
|
1045
|
+
discovery: {
|
|
1046
|
+
gateways: {
|
|
1047
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
1048
|
+
lab: { enabled: true, url: "http://gw2", token: "t2" },
|
|
1049
|
+
},
|
|
1050
|
+
},
|
|
1051
|
+
});
|
|
1052
|
+
assert.equal(a2a.discovery.gateways.work.url, "http://new");
|
|
1053
|
+
assert.equal(a2a.discovery.gateways.work.token, "t1", "unedited work token kept from file");
|
|
1054
|
+
assert.equal(a2a.discovery.gateways.lab.heartbeatSec, 120, "lab heartbeat edit persists");
|
|
1055
|
+
assert.equal(a2a.discovery.gateways.lab.token, "t2", "unedited lab token kept from file");
|
|
1056
|
+
});
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
it("gateways edit with a NEW entry persists the typed token (panel-add flow)", () => {
|
|
1060
|
+
withIsolatedPiDir((dir) => {
|
|
1061
|
+
const cfg = cfgWith({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } }, dir);
|
|
1062
|
+
const working = structuredClone(cfg);
|
|
1063
|
+
// Entry absent from the file = newly added via the panel (which has no
|
|
1064
|
+
// env source) — its typed token must survive save, NOT be wiped.
|
|
1065
|
+
working.discovery.gateways = {
|
|
1066
|
+
work: { enabled: true, url: "http://gw1", token: "typed-token" },
|
|
1067
|
+
};
|
|
1068
|
+
const patch = buildA2ASettingsPatch({
|
|
1069
|
+
cfg,
|
|
1070
|
+
working,
|
|
1071
|
+
peerChanges: false,
|
|
1072
|
+
gatewayChanged: true,
|
|
1073
|
+
editedGatewayKeys: new Set(), // prompt-driven add never reaches editedKeys
|
|
1074
|
+
});
|
|
1075
|
+
const a2a = patch({ discovery: { local: { enabled: true, heartbeatSec: 15, ttlSec: 60 } } });
|
|
1076
|
+
assert.equal(a2a.discovery.gateways.work.url, "http://gw1");
|
|
1077
|
+
assert.equal(a2a.discovery.gateways.work.token, "typed-token", "typed token must persist");
|
|
1078
|
+
});
|
|
1079
|
+
});
|
|
1080
|
+
|
|
1081
|
+
it("existing-file entry keeps unedited secret rows (env-leak guard preserved)", () => {
|
|
1082
|
+
withIsolatedPiDir((dir) => {
|
|
1083
|
+
const cfg = cfgWith(
|
|
1084
|
+
{
|
|
1085
|
+
discovery: {
|
|
1086
|
+
gateways: {
|
|
1087
|
+
work: { enabled: true, url: "http://gw1", token: "file-token" },
|
|
1088
|
+
},
|
|
1089
|
+
},
|
|
1090
|
+
},
|
|
1091
|
+
dir,
|
|
1092
|
+
);
|
|
1093
|
+
const working = structuredClone(cfg);
|
|
1094
|
+
// The live working token differs (env/override-sourced) but the row was
|
|
1095
|
+
// never edited — the FILE value must win.
|
|
1096
|
+
working.discovery.gateways!.work.token = "env-tok";
|
|
1097
|
+
working.discovery.gateways!.work.heartbeatSec = 90;
|
|
1098
|
+
const patch = buildA2ASettingsPatch({
|
|
1099
|
+
cfg,
|
|
1100
|
+
working,
|
|
1101
|
+
peerChanges: false,
|
|
1102
|
+
gatewayChanged: true,
|
|
1103
|
+
editedGatewayKeys: new Set(["gw.work.heartbeatSec"]),
|
|
1104
|
+
});
|
|
1105
|
+
const a2a = patch({
|
|
1106
|
+
discovery: {
|
|
1107
|
+
gateways: {
|
|
1108
|
+
work: { enabled: true, url: "http://gw1", token: "file-token" },
|
|
1109
|
+
},
|
|
1110
|
+
},
|
|
1111
|
+
});
|
|
1112
|
+
assert.equal(a2a.discovery.gateways.work.heartbeatSec, 90);
|
|
1113
|
+
assert.equal(a2a.discovery.gateways.work.token, "file-token", "unedited secret keeps file value");
|
|
1114
|
+
});
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
it("gateways entry removed by the user is dropped from the file", () => {
|
|
1118
|
+
withIsolatedPiDir((dir) => {
|
|
1119
|
+
const cfg = cfgWith(
|
|
1120
|
+
{
|
|
1121
|
+
discovery: {
|
|
1122
|
+
gateways: {
|
|
1123
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
1124
|
+
lab: { enabled: true, url: "http://gw2", token: "t2" },
|
|
1125
|
+
},
|
|
1126
|
+
},
|
|
1127
|
+
},
|
|
1128
|
+
dir,
|
|
1129
|
+
);
|
|
1130
|
+
const working = structuredClone(cfg);
|
|
1131
|
+
delete working.discovery.gateways!.lab;
|
|
1132
|
+
const patch = buildA2ASettingsPatch({ cfg, working, peerChanges: false, gatewayChanged: true });
|
|
1133
|
+
const a2a = patch({
|
|
1134
|
+
discovery: {
|
|
1135
|
+
gateways: {
|
|
1136
|
+
work: { enabled: true, url: "http://gw1", token: "t1" },
|
|
1137
|
+
lab: { enabled: true, url: "http://gw2", token: "t2" },
|
|
1138
|
+
},
|
|
1139
|
+
},
|
|
1140
|
+
});
|
|
1141
|
+
assert.deepEqual(Object.keys(a2a.discovery.gateways), ["work"], "removed entry gone");
|
|
1142
|
+
});
|
|
1143
|
+
});
|
|
1144
|
+
|
|
1145
|
+
it("peer timeout round-trip is idempotent (settings seconds, no 1000× drift)", () => {
|
|
1146
|
+
withIsolatedPiDir((dir) => {
|
|
1147
|
+
const cfg = cfgWith(
|
|
1148
|
+
{ peers: { bob: { url: "http://b", auth: { type: "none" }, timeout: 120, capabilities: [] } } },
|
|
1149
|
+
dir,
|
|
1150
|
+
);
|
|
1151
|
+
assert.equal(cfg.peers.bob?.timeout, 120000, "loader: seconds → ms");
|
|
1152
|
+
|
|
1153
|
+
// Panel edit (no timeout change) → patch → simulated save → reload.
|
|
1154
|
+
const working = structuredClone(cfg);
|
|
1155
|
+
const next = buildA2ASettingsPatch({ cfg, working, peerChanges: true, gatewayChanged: false })({ peers: {} });
|
|
1156
|
+
assert.equal(next.peers.bob.timeout, 120, "persisted back as seconds");
|
|
1157
|
+
fs.writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ a2a: next }));
|
|
1158
|
+
const cfg2 = loadConfig({ cwd: dir });
|
|
1159
|
+
assert.equal(cfg2.peers.bob?.timeout, 120000);
|
|
1160
|
+
|
|
1161
|
+
// Second cycle must be byte-identical in the settings file.
|
|
1162
|
+
const next2 = buildA2ASettingsPatch({ cfg: cfg2, working: structuredClone(cfg2), peerChanges: true, gatewayChanged: false })({
|
|
1163
|
+
peers: {},
|
|
1164
|
+
});
|
|
1165
|
+
assert.deepEqual(next2.peers, next.peers, "no drift on second save");
|
|
1166
|
+
|
|
1167
|
+
// Panel-added peer (addPeer seeds runtime ms 120000) persists as 120 s.
|
|
1168
|
+
const working3 = structuredClone(cfg2);
|
|
1169
|
+
working3.peers.newp = { url: "http://n", auth: { type: "none" }, timeout: 120000, capabilities: [] };
|
|
1170
|
+
const next3 = buildA2ASettingsPatch({ cfg: cfg2, working: working3, peerChanges: true, gatewayChanged: false })({ peers: {} });
|
|
1171
|
+
assert.equal(next3.peers.newp.timeout, 120, "addPeer seed lands as seconds");
|
|
1172
|
+
});
|
|
1173
|
+
});
|
|
1174
|
+
});
|
|
1175
|
+
|
|
1176
|
+
describe("writeSettingsA2A", () => {
|
|
1177
|
+
it("writes a new a2a key to the global settings path, preserving other keys", () => {
|
|
1178
|
+
withIsolatedPiDir((dir) => {
|
|
1179
|
+
// Place a settings.json in the isolated PI_CODING_AGENT_DIR
|
|
1180
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
1181
|
+
fs.writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ theme: "dark", other: 1 }));
|
|
1182
|
+
const written = writeSettingsA2A({ cwd: dir, patch: (a2a: any) => ({ ...a2a, server: { port: 1234 } }) });
|
|
1183
|
+
assert.equal(written, path.join(dir, "settings.json"));
|
|
1184
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(dir, "settings.json"), "utf-8"));
|
|
1185
|
+
assert.equal(parsed.theme, "dark"); // unrelated keys preserved
|
|
1186
|
+
assert.equal(parsed.other, 1);
|
|
1187
|
+
assert.equal(parsed.a2a.server.port, 1234);
|
|
1188
|
+
});
|
|
1189
|
+
});
|
|
1190
|
+
|
|
1191
|
+
it("prefers an existing GLOBAL settings.json that already has an a2a key over the repo cwd file", () => {
|
|
1192
|
+
withIsolatedPiDir((dir) => {
|
|
1193
|
+
// Repo cwd ships .pi/settings.json with an a2a key; global also has one.
|
|
1194
|
+
// The write must land in the GLOBAL (operator-owned) file — writing
|
|
1195
|
+
// into the repo file would leak operator tokens to a repo-controlled
|
|
1196
|
+
// path (and be stripped on re-read by the injection guard).
|
|
1197
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
1198
|
+
trustCwd(dir);
|
|
1199
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { server: { port: 1 } } }));
|
|
1200
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
1201
|
+
fs.writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ a2a: { server: { port: 2 } } }));
|
|
1202
|
+
const written = writeSettingsA2A({ cwd: dir, patch: (a2a: any) => ({ ...a2a, server: { port: 3 } }) });
|
|
1203
|
+
assert.equal(written, path.join(dir, "settings.json"));
|
|
1204
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(dir, "settings.json"), "utf-8"));
|
|
1205
|
+
assert.equal(parsed.a2a.server.port, 3);
|
|
1206
|
+
// The repo file is untouched.
|
|
1207
|
+
const repo = JSON.parse(fs.readFileSync(path.join(dir, ".pi", "settings.json"), "utf-8"));
|
|
1208
|
+
assert.equal(repo.a2a.server.port, 1);
|
|
1209
|
+
});
|
|
1210
|
+
});
|
|
1211
|
+
|
|
1212
|
+
it("merges the patch onto existing a2a values (server changes keep discovery)", () => {
|
|
1213
|
+
withIsolatedPiDir((dir) => {
|
|
1214
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
1215
|
+
fs.writeFileSync(
|
|
1216
|
+
path.join(dir, "settings.json"),
|
|
1217
|
+
JSON.stringify({ a2a: { server: { port: 1 }, discovery: { mdns: { enabled: true } } } }),
|
|
1218
|
+
);
|
|
1219
|
+
writeSettingsA2A({ cwd: dir, patch: (a2a: any) => ({ ...a2a, server: { port: 9 } }) });
|
|
1220
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(dir, "settings.json"), "utf-8"));
|
|
1221
|
+
assert.equal(parsed.a2a.server.port, 9);
|
|
1222
|
+
assert.isTrue(parsed.a2a.discovery.mdns.enabled); // untouched subtree preserved
|
|
1223
|
+
});
|
|
1224
|
+
});
|
|
1225
|
+
|
|
1226
|
+
it("NEVER writes into the repo-controlled .pi/settings.json — tokens saved by the config panel must not land in a repo path", () => {
|
|
1227
|
+
withIsolatedPiDir((dir) => {
|
|
1228
|
+
fs.mkdirSync(path.join(dir, ".pi"), { recursive: true });
|
|
1229
|
+
trustCwd(dir);
|
|
1230
|
+
fs.writeFileSync(path.join(dir, ".pi", "settings.json"), JSON.stringify({ a2a: { selfIdentity: "proj" } }));
|
|
1231
|
+
const written = writeSettingsA2A({ cwd: dir, patch: (a2a: any) => ({ ...a2a, selfIdentity: "proj2" }) });
|
|
1232
|
+
assert.equal(written, path.join(dir, "settings.json"));
|
|
1233
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(dir, "settings.json"), "utf-8"));
|
|
1234
|
+
assert.equal(parsed.a2a.selfIdentity, "proj2");
|
|
1235
|
+
});
|
|
1236
|
+
});
|
|
1237
|
+
|
|
1238
|
+
it("writes a fresh a2a key to ~/.pi/agent/settings.json, never the orphan ~/.pi/agents path", function (this: any) {
|
|
1239
|
+
// os.homedir() ignores HOME on Windows (uses USERPROFILE) — the test
|
|
1240
|
+
// would target the developer's REAL settings file there. Skip win32;
|
|
1241
|
+
// POSIX behavior is what the fallback relies on.
|
|
1242
|
+
if (process.platform === "win32") this.skip();
|
|
1243
|
+
const home = tmpDir();
|
|
1244
|
+
fs.mkdirSync(path.join(home, ".pi", "agent"), { recursive: true });
|
|
1245
|
+
fs.writeFileSync(path.join(home, ".pi", "agent", "settings.json"), JSON.stringify({ theme: "dark", other: 1 }));
|
|
1246
|
+
const oldHome = process.env.HOME;
|
|
1247
|
+
const oldUserProfile = process.env.USERPROFILE;
|
|
1248
|
+
const oldHomeDrive = process.env.HOMEDRIVE;
|
|
1249
|
+
const oldHomePath = process.env.HOMEPATH;
|
|
1250
|
+
const oldPiDir = process.env.PI_CODING_AGENT_DIR;
|
|
1251
|
+
process.env.HOME = home;
|
|
1252
|
+
delete process.env.USERPROFILE;
|
|
1253
|
+
delete process.env.HOMEDRIVE;
|
|
1254
|
+
delete process.env.HOMEPATH;
|
|
1255
|
+
delete process.env.PI_CODING_AGENT_DIR;
|
|
1256
|
+
try {
|
|
1257
|
+
const written = writeSettingsA2A({ cwd: tmpDir(), patch: (a2a: any) => ({ ...a2a, selfIdentity: "me" }) });
|
|
1258
|
+
assert.equal(written, path.join(home, ".pi", "agent", "settings.json"));
|
|
1259
|
+
assert.isFalse(
|
|
1260
|
+
fs.existsSync(path.join(home, ".pi", "agents", "settings.json")),
|
|
1261
|
+
"must never create the orphan ~/.pi/agents file",
|
|
1262
|
+
);
|
|
1263
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(home, ".pi", "agent", "settings.json"), "utf-8"));
|
|
1264
|
+
assert.equal(parsed.theme, "dark"); // unrelated keys preserved
|
|
1265
|
+
assert.equal(parsed.other, 1);
|
|
1266
|
+
assert.equal(parsed.a2a.selfIdentity, "me");
|
|
1267
|
+
} finally {
|
|
1268
|
+
if (oldHome === undefined) delete process.env.HOME;
|
|
1269
|
+
else process.env.HOME = oldHome;
|
|
1270
|
+
if (oldUserProfile === undefined) delete process.env.USERPROFILE;
|
|
1271
|
+
else process.env.USERPROFILE = oldUserProfile;
|
|
1272
|
+
if (oldHomeDrive === undefined) delete process.env.HOMEDRIVE;
|
|
1273
|
+
else process.env.HOMEDRIVE = oldHomeDrive;
|
|
1274
|
+
if (oldHomePath === undefined) delete process.env.HOMEPATH;
|
|
1275
|
+
else process.env.HOMEPATH = oldHomePath;
|
|
1276
|
+
if (oldPiDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
1277
|
+
else process.env.PI_CODING_AGENT_DIR = oldPiDir;
|
|
1278
|
+
}
|
|
1279
|
+
});
|
|
1280
|
+
});
|
|
1281
|
+
|
|
1282
|
+
describe("cleanHostName", () => {
|
|
1283
|
+
it("strips mDNS suffixes and lowercases", () => {
|
|
1284
|
+
assert.equal(cleanHostName("MBP-Sao.local"), "mbp-sao");
|
|
1285
|
+
assert.equal(cleanHostName("hermes-lxc.LAN."), "hermes-lxc");
|
|
1286
|
+
assert.equal(cleanHostName("Pi-Server"), "pi-server");
|
|
1287
|
+
assert.equal(cleanHostName(""), "pi");
|
|
1288
|
+
assert.equal(cleanHostName(undefined), "pi");
|
|
1289
|
+
});
|
|
1290
|
+
});
|
|
1291
|
+
|
|
1292
|
+
describe("resolvePeer", () => {
|
|
1293
|
+
it("treats a raw URL as a direct peer", () => {
|
|
1294
|
+
const cfg = DEFAULTS();
|
|
1295
|
+
const p = resolvePeer(cfg, "http://example.com:9900");
|
|
1296
|
+
assert.equal(p?.url, "http://example.com:9900");
|
|
1297
|
+
assert.equal(p?.auth.type, "none");
|
|
1298
|
+
});
|
|
1299
|
+
it("resolves a configured peer name", () => {
|
|
1300
|
+
const cfg = DEFAULTS();
|
|
1301
|
+
cfg.peers.alice = { url: "http://a", auth: { type: "none" }, timeout: 1000, capabilities: [] };
|
|
1302
|
+
assert.equal(resolvePeer(cfg, "alice")?.url, "http://a");
|
|
1303
|
+
});
|
|
1304
|
+
it("returns null for unknown name", () => {
|
|
1305
|
+
assert.isNull(resolvePeer(DEFAULTS(), "nope"));
|
|
1306
|
+
});
|
|
1307
|
+
it("auto-attaches the shared token for KNOWN loopback URLs only", () => {
|
|
1308
|
+
const cfg = DEFAULTS();
|
|
1309
|
+
cfg.server.sharedToken = "s3cret";
|
|
1310
|
+
const known = new Set([normUrl("http://127.0.0.1:9911"), normUrl("http://localhost:9910")]);
|
|
1311
|
+
// Known peer URL → token attached
|
|
1312
|
+
const p = resolvePeer(cfg, "http://127.0.0.1:9911", { knownLoopbackUrls: known });
|
|
1313
|
+
assert.equal(p?.auth.type, "bearer");
|
|
1314
|
+
assert.equal((p!.auth as any).token, "s3cret");
|
|
1315
|
+
const p2 = resolvePeer(cfg, "http://localhost:9910", { knownLoopbackUrls: known });
|
|
1316
|
+
assert.equal(p2?.auth.type, "bearer");
|
|
1317
|
+
});
|
|
1318
|
+
it("does NOT attach the shared token to an ARBITRARY loopback URL (prompt-injection guard)", () => {
|
|
1319
|
+
const cfg = DEFAULTS();
|
|
1320
|
+
cfg.server.sharedToken = "s3cret";
|
|
1321
|
+
// No knownLoopbackUrls → arbitrary localhost must get NO token
|
|
1322
|
+
const p = resolvePeer(cfg, "http://localhost:1337");
|
|
1323
|
+
assert.equal(p?.auth.type, "none");
|
|
1324
|
+
// Even with a known set, a DIFFERENT port is not in it
|
|
1325
|
+
const known = new Set([normUrl("http://127.0.0.1:9911")]);
|
|
1326
|
+
const p2 = resolvePeer(cfg, "http://localhost:1337", { knownLoopbackUrls: known });
|
|
1327
|
+
assert.equal(p2?.auth.type, "none");
|
|
1328
|
+
});
|
|
1329
|
+
it("does NOT attach the shared token to non-loopback URLs", () => {
|
|
1330
|
+
const cfg = DEFAULTS();
|
|
1331
|
+
cfg.server.sharedToken = "s3cret";
|
|
1332
|
+
const known = new Set([normUrl("http://10.0.0.5:9911")]);
|
|
1333
|
+
const p = resolvePeer(cfg, "http://10.0.0.5:9911", { knownLoopbackUrls: known });
|
|
1334
|
+
assert.equal(p?.auth.type, "none");
|
|
1335
|
+
});
|
|
1336
|
+
it("does NOT attach a token when no shared token is configured", () => {
|
|
1337
|
+
const known = new Set([normUrl("http://127.0.0.1:9911")]);
|
|
1338
|
+
const p = resolvePeer(DEFAULTS(), "http://127.0.0.1:9911", { knownLoopbackUrls: known });
|
|
1339
|
+
assert.equal(p?.auth.type, "none");
|
|
1340
|
+
});
|
|
1341
|
+
it("treats IPv6 ::1 (bracketed or not) as loopback", () => {
|
|
1342
|
+
const cfg = DEFAULTS();
|
|
1343
|
+
cfg.server.sharedToken = "s3cret";
|
|
1344
|
+
const known = new Set([normUrl("http://[::1]:9911"), normUrl("http://::1:9911")]);
|
|
1345
|
+
assert.equal(resolvePeer(cfg, "http://[::1]:9911", { knownLoopbackUrls: known })?.auth.type, "bearer");
|
|
1346
|
+
// isLoopbackHost itself must accept both forms; unknown-port ::1 still no token
|
|
1347
|
+
const p = resolvePeer(cfg, "http://[::1]:9999");
|
|
1348
|
+
assert.equal(p?.auth.type, "none");
|
|
1349
|
+
});
|
|
1350
|
+
|
|
1351
|
+
it("prefers this session's own peer token over the shared token", () => {
|
|
1352
|
+
const cfg = DEFAULTS();
|
|
1353
|
+
cfg.server.sharedToken = "SHARED";
|
|
1354
|
+
cfg.server.peerTokens = { "session-a": "OWN_TOKEN_A" };
|
|
1355
|
+
cfg.selfIdentity = "session-a";
|
|
1356
|
+
const known = new Set([normUrl("http://127.0.0.1:9911")]);
|
|
1357
|
+
const p = resolvePeer(cfg, "http://127.0.0.1:9911", { knownLoopbackUrls: known });
|
|
1358
|
+
assert.equal(p?.auth.type, "bearer");
|
|
1359
|
+
assert.equal((p!.auth as any).token, "OWN_TOKEN_A", "should present own token, not SHARED");
|
|
1360
|
+
});
|
|
1361
|
+
|
|
1362
|
+
it("falls back to the shared token when selfIdentity has no peer token", () => {
|
|
1363
|
+
const cfg = DEFAULTS();
|
|
1364
|
+
cfg.server.sharedToken = "SHARED";
|
|
1365
|
+
cfg.server.peerTokens = { "session-b": "T_B" };
|
|
1366
|
+
cfg.selfIdentity = "session-a"; // not in peerTokens
|
|
1367
|
+
const known = new Set([normUrl("http://127.0.0.1:9911")]);
|
|
1368
|
+
const p = resolvePeer(cfg, "http://127.0.0.1:9911", { knownLoopbackUrls: known });
|
|
1369
|
+
assert.equal((p!.auth as any).token, "SHARED");
|
|
1370
|
+
});
|
|
1371
|
+
|
|
1372
|
+
it("falls back to the shared token when selfIdentity is empty", () => {
|
|
1373
|
+
const cfg = DEFAULTS();
|
|
1374
|
+
cfg.server.sharedToken = "SHARED";
|
|
1375
|
+
cfg.server.peerTokens = { "session-a": "T_A" };
|
|
1376
|
+
cfg.selfIdentity = "";
|
|
1377
|
+
const known = new Set([normUrl("http://127.0.0.1:9911")]);
|
|
1378
|
+
const p = resolvePeer(cfg, "http://127.0.0.1:9911", { knownLoopbackUrls: known });
|
|
1379
|
+
assert.equal((p!.auth as any).token, "SHARED");
|
|
1380
|
+
});
|
|
1381
|
+
});
|
|
1382
|
+
|
|
1383
|
+
describe("authHeaders", () => {
|
|
1384
|
+
it("builds a Bearer header", () => {
|
|
1385
|
+
const h = authHeaders({ url: "", auth: { type: "bearer", token: "t" }, timeout: 1, capabilities: [] });
|
|
1386
|
+
assert.deepEqual(h, { Authorization: "Bearer t" });
|
|
1387
|
+
});
|
|
1388
|
+
it("builds an ApiKey header", () => {
|
|
1389
|
+
const h = authHeaders({ url: "", auth: { type: "apiKey", token: "k" }, timeout: 1, capabilities: [] });
|
|
1390
|
+
assert.deepEqual(h, { Authorization: "ApiKey k" });
|
|
1391
|
+
});
|
|
1392
|
+
it("omits headers for no auth", () => {
|
|
1393
|
+
assert.deepEqual(authHeaders({ url: "", auth: { type: "none" }, timeout: 1, capabilities: [] }), {});
|
|
1394
|
+
});
|
|
1395
|
+
});
|
|
1396
|
+
});
|