@sleep2agi/commhub-server 0.9.0-preview.58 → 0.9.0-preview.59

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.58",
3
+ "version": "0.9.0-preview.59",
4
4
  "description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/db.ts CHANGED
@@ -130,6 +130,9 @@ for (const col of [
130
130
  // Node skills view — the same session answers skills_list / skill_read on the
131
131
  // rules-file doorbell (node-skills.ts). Sticky, bound-token only, like above.
132
132
  { name: "skills_capable", def: "INTEGER NOT NULL DEFAULT 0" },
133
+ // Project folder view — the same session answers files_list / file_read on the
134
+ // rules-file doorbell (node-files.ts). Sticky, bound-token only, like above.
135
+ { name: "files_capable", def: "INTEGER NOT NULL DEFAULT 0" },
133
136
  ]) {
134
137
  try { db.exec(`ALTER TABLE sessions ADD COLUMN ${col.name} ${col.def}`); } catch {}
135
138
  }
@@ -0,0 +1,344 @@
1
+ // Project folder view — list_node_files / read_node_file ride the rules-file
2
+ // queue + doorbell (ops files_list / file_read).
3
+ //
4
+ // Hub-side contract verified here:
5
+ // 1. report_status { files_capable: true } is sticky and set only by the
6
+ // session's own node token (bound or legacy-unbound, same rule as skills).
7
+ // 2. Both tools reach a session with no nodes row by alias; the pulled
8
+ // request carries the normalized RELATIVE path; results come back through
9
+ // get_rules_file_result.
10
+ // 3. Absolute paths, `..`, `~`, drive letters, backslashes and NUL are refused
11
+ // before any request row exists (the node re-checks; this is the first wall).
12
+ // 4. A node token may not enqueue a files op (it would read another node's
13
+ // disk), and only the token that asked may read a files result.
14
+ // 5. A token bound to node A never pulls or acks node B's files request; an
15
+ // unbound legacy token falls back to its alias exactly as it does for skills.
16
+ // 6. Its own single-flight lane; rules-capable-only sessions are refused.
17
+ // 7. Files acks may carry up to 1 MiB (escaped JSON of a 256 KiB file); rules
18
+ // acks keep their 256 KiB cap.
19
+ //
20
+ // 跑法:cd server && COMMHUB_DB=/tmp/files-transport.db bun test src/node-files-transport.test.ts
21
+ import { afterAll, beforeEach, describe, expect, test } from "bun:test";
22
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
23
+ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
24
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
25
+ import { db } from "./db.js";
26
+ import { registerTools } from "./tools.js";
27
+
28
+ const NET = "net_files_session";
29
+ const OTHER_NET = "net_files_session_other";
30
+ const USER = "u_files_session_owner";
31
+ const SESSION_ALIAS = "files-session-cc";
32
+ const SESSION_RESUME = "cc-files-session-1";
33
+ const PEER_ALIAS = "files-session-peer";
34
+ const NODE_A = "files-node-a";
35
+ const NODE_B = "files-node-b";
36
+ const NODE_A_ID = "node_files_a";
37
+ const NODE_B_ID = "node_files_b";
38
+ const LEGACY_ALIAS = "files-node-legacy";
39
+ const LEGACY_ID = "node_files_legacy";
40
+ const T_SESSION = "tok_files_session_cc";
41
+ const T_PEER = "tok_files_session_peer";
42
+ const T_OTHER_NET_SAME_ALIAS = "tok_files_session_other_net";
43
+ const T_A = "tok_files_node_a";
44
+ const T_B = "tok_files_node_b";
45
+ const T_LEGACY = "tok_files_node_legacy";
46
+
47
+ function cleanup() {
48
+ for (const net of [NET, OTHER_NET]) {
49
+ try { db.run("DELETE FROM node_rules_requests WHERE network_id = ?1", [net]); } catch {}
50
+ try { db.run("DELETE FROM rename_txn WHERE network_id = ?1", [net]); } catch {}
51
+ try { db.run("DELETE FROM sessions WHERE network_id = ?1", [net]); } catch {}
52
+ try { db.run("DELETE FROM nodes WHERE network_id = ?1", [net]); } catch {}
53
+ try { db.run("DELETE FROM api_tokens WHERE network_id = ?1", [net]); } catch {}
54
+ try { db.run("DELETE FROM network_members WHERE network_id = ?1", [net]); } catch {}
55
+ try { db.run("DELETE FROM networks WHERE network_id = ?1", [net]); } catch {}
56
+ }
57
+ try { db.run("DELETE FROM users WHERE user_id = ?1", [USER]); } catch {}
58
+ }
59
+
60
+ function seedWorld() {
61
+ db.run(`INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1, ?2, 'x', 'user', datetime('now'))`, [USER, USER]);
62
+ for (const net of [NET, OTHER_NET]) {
63
+ db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?2, ?3, datetime('now'))`, [net, net, USER]);
64
+ db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, 'owner', datetime('now'))`, [USER, net]);
65
+ }
66
+ const tok = (id: string, net: string, alias: string, bound: string | null = null) => db.run(
67
+ `INSERT INTO api_tokens (token_id, user_id, network_id, scope, name, token_hash, expires_at, revoked_at, bound_node_id) VALUES (?1, ?2, ?3, 'network', ?4, ?5, NULL, NULL, ?6)`,
68
+ [id, USER, net, `node:${alias}`, `hash_${id}`, bound],
69
+ );
70
+ const node = (id: string, alias: string) => db.run(
71
+ `INSERT INTO nodes (node_id, node_name, alias, network_id, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, datetime('now'), datetime('now'))`,
72
+ [id, alias, alias, NET],
73
+ );
74
+ node(NODE_A_ID, NODE_A);
75
+ node(NODE_B_ID, NODE_B);
76
+ node(LEGACY_ID, LEGACY_ALIAS);
77
+ tok(T_SESSION, NET, SESSION_ALIAS);
78
+ tok(T_PEER, NET, PEER_ALIAS);
79
+ tok(T_OTHER_NET_SAME_ALIAS, OTHER_NET, SESSION_ALIAS);
80
+ tok(T_A, NET, NODE_A, NODE_A_ID);
81
+ tok(T_B, NET, NODE_B, NODE_B_ID);
82
+ tok(T_LEGACY, NET, LEGACY_ALIAS, null);
83
+ }
84
+
85
+ type Identity = { net: string; alias: string; isNetworkToken: boolean; tokenId: string };
86
+ const asUser: Identity = { net: NET, alias: USER, isNetworkToken: false, tokenId: "tok_files_session_user" };
87
+ const asUserOtherLogin: Identity = { net: NET, alias: USER, isNetworkToken: false, tokenId: "tok_files_session_user_other_login" };
88
+ const asSession: Identity = { net: NET, alias: SESSION_ALIAS, isNetworkToken: true, tokenId: T_SESSION };
89
+ const asPeer: Identity = { net: NET, alias: PEER_ALIAS, isNetworkToken: true, tokenId: T_PEER };
90
+ const asOtherNetSameAlias: Identity = { net: OTHER_NET, alias: SESSION_ALIAS, isNetworkToken: true, tokenId: T_OTHER_NET_SAME_ALIAS };
91
+ const asA: Identity = { net: NET, alias: NODE_A, isNetworkToken: true, tokenId: T_A };
92
+ const asB: Identity = { net: NET, alias: NODE_B, isNetworkToken: true, tokenId: T_B };
93
+ const asLegacy: Identity = { net: NET, alias: LEGACY_ALIAS, isNetworkToken: true, tokenId: T_LEGACY };
94
+
95
+ async function connect(id: Identity) {
96
+ const server = new McpServer({ name: "files-session-test", version: "1" });
97
+ registerTools(server, undefined, id.net, USER, id.alias, id.isNetworkToken, id.tokenId);
98
+ const client = new Client({ name: "files-session-client", version: "1" });
99
+ const [ct, st] = InMemoryTransport.createLinkedPair();
100
+ await server.connect(st);
101
+ await client.connect(ct);
102
+ const call = async (name: string, args: Record<string, unknown>) => {
103
+ const r: any = await client.callTool({ name, arguments: args });
104
+ const first = r.content?.[0];
105
+ if (!first || first.type !== "text") throw new Error(`no text result from ${name}: ${JSON.stringify(r).slice(0, 300)}`);
106
+ try { return JSON.parse(first.text); } catch { return { raw: first.text, isError: r.isError === true }; }
107
+ };
108
+ const close = async () => { await client.close(); await server.close(); };
109
+ return { call, close };
110
+ }
111
+
112
+ const capable = (alias: string, net = NET) =>
113
+ db.get<{ c: number }>("SELECT files_capable AS c FROM sessions WHERE alias = ?1 AND network_id = ?2", alias, net)?.c;
114
+ const rowCount = () => db.get<{ n: number }>("SELECT COUNT(*) AS n FROM node_rules_requests WHERE network_id = ?1", NET)!.n;
115
+
116
+ async function report(id: Identity, alias: string, resume: string, flags: Record<string, true>) {
117
+ const c = await connect(id);
118
+ try { return await c.call("report_status", { resume_id: resume, alias, status: "idle", agent: "claude-code", ...flags }); }
119
+ finally { await c.close(); }
120
+ }
121
+ const ALL_FLAGS = { rules_file_capable: true, skills_capable: true, files_capable: true } as const;
122
+
123
+ beforeEach(() => { cleanup(); seedWorld(); });
124
+ afterAll(cleanup);
125
+
126
+ describe("files_capable is set only by the session's own node token", () => {
127
+ test("own token sets it; a later report without it does not clear it; a peer cannot set it", async () => {
128
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
129
+ expect(capable(SESSION_ALIAS)).toBe(1);
130
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, {});
131
+ expect(capable(SESSION_ALIAS)).toBe(1);
132
+ const r = await report(asPeer, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
133
+ expect(r).toMatchObject({ ok: false });
134
+ // A user login cannot set it either.
135
+ await report(asUser, PEER_ALIAS, "cc-files-user-report", ALL_FLAGS);
136
+ expect(capable(PEER_ALIAS) ?? 0).toBe(0);
137
+ }, 20_000);
138
+
139
+ test("a stale process whose report is rewritten to a renamed alias does not grant that alias the flag", async () => {
140
+ const NEW_ALIAS = "files-session-renamed";
141
+ db.run(
142
+ `INSERT INTO rename_txn (txn_id, network_id, old_alias, new_alias, status, committed_at) VALUES ('txn_files_rename', ?1, ?2, ?3, 'committed', datetime('now'))`,
143
+ [NET, PEER_ALIAS, NEW_ALIAS],
144
+ );
145
+ // Token still carries the old alias; the hub rewrites the report to NEW_ALIAS.
146
+ const r = await report(asPeer, PEER_ALIAS, "cc-files-renamed", ALL_FLAGS);
147
+ expect(r.ok).toBe(true);
148
+ expect(db.get<{ n: number }>("SELECT COUNT(*) AS n FROM sessions WHERE alias = ?1 AND network_id = ?2", NEW_ALIAS, NET)!.n).toBe(1);
149
+ expect(capable(NEW_ALIAS)).toBe(0);
150
+ }, 20_000);
151
+ });
152
+
153
+ describe("list / read by alias reach a session with no nodes row", () => {
154
+ test("list: pulled request carries the normalized relative path; result comes back", async () => {
155
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
156
+ const u = await connect(asUser);
157
+ const s = await connect(asSession);
158
+ try {
159
+ const enq = await u.call("list_node_files", { alias: SESSION_ALIAS, path: "./src//lib/" });
160
+ expect(enq).toMatchObject({ ok: true, op: "files_list" });
161
+ const pulled = await s.call("get_rules_file_request", {});
162
+ expect(pulled.request).toEqual({ request_id: enq.request_id, op: "files_list", content: "src/lib" });
163
+ const payload = JSON.stringify({ path: "src/lib", entries: [{ name: "a.ts", type: "file", size: 3, mtime: 1 }], truncated: false, total: 1 });
164
+ expect(await s.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", file_name: "files", exists: true, content: payload })).toMatchObject({ ok: true, status: "done" });
165
+ expect(await u.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: true, op: "files_list", status: "done", content: payload });
166
+ } finally { await u.close(); await s.close(); }
167
+ }, 20_000);
168
+
169
+ test("list with no path targets the root (empty content)", async () => {
170
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
171
+ const u = await connect(asUser);
172
+ const s = await connect(asSession);
173
+ try {
174
+ const enq = await u.call("list_node_files", { alias: SESSION_ALIAS });
175
+ expect((await s.call("get_rules_file_request", {})).request).toEqual({ request_id: enq.request_id, op: "files_list", content: "" });
176
+ } finally { await u.close(); await s.close(); }
177
+ }, 20_000);
178
+
179
+ test("read round trip", async () => {
180
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
181
+ const u = await connect(asUser);
182
+ const s = await connect(asSession);
183
+ try {
184
+ const enq = await u.call("read_node_file", { alias: SESSION_ALIAS, path: "docs/README.md" });
185
+ expect(enq).toMatchObject({ ok: true, op: "file_read" });
186
+ expect((await s.call("get_rules_file_request", {})).request).toEqual({ request_id: enq.request_id, op: "file_read", content: "docs/README.md" });
187
+ const payload = JSON.stringify({ path: "docs/README.md", name: "README.md", kind: "text", size: 6, content: "# hi\n" });
188
+ await s.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", file_name: "README.md", exists: true, content: payload });
189
+ expect(await u.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ status: "done", content: payload });
190
+ } finally { await u.close(); await s.close(); }
191
+ }, 20_000);
192
+ });
193
+
194
+ describe("path validation happens before any row is written", () => {
195
+ test("absolute, .., ~, drive letters, backslashes, NUL, and an empty read path are refused", async () => {
196
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
197
+ const u = await connect(asUser);
198
+ try {
199
+ const bad = ["/etc/passwd", "../outside", "a/../../b", "src/..", "~/.ssh/id_rsa", "C:/Windows", "a\\b", "..\\x", "a\u0000b", "x".repeat(1025)];
200
+ for (const path of bad) {
201
+ // Over-long paths are stopped by the zod schema (an MCP input error); the rest by invalid_path.
202
+ const r = await u.call("read_node_file", { alias: SESSION_ALIAS, path });
203
+ expect(r.ok).not.toBe(true);
204
+ if (path.length <= 1024) expect(r).toMatchObject({ ok: false, error: "invalid_path" });
205
+ const l = await u.call("list_node_files", { alias: SESSION_ALIAS, path });
206
+ expect(l.ok).not.toBe(true);
207
+ }
208
+ expect(await u.call("read_node_file", { alias: SESSION_ALIAS, path: "/" })).toMatchObject({ ok: false, error: "invalid_path" });
209
+ expect(await u.call("read_node_file", { alias: SESSION_ALIAS, path: "./" })).toMatchObject({ ok: false, error: "invalid_path" });
210
+ expect(rowCount()).toBe(0);
211
+ } finally { await u.close(); }
212
+ }, 20_000);
213
+ });
214
+
215
+ describe("who may ask, and who may read the answer", () => {
216
+ test("a node token cannot enqueue a files op on another node (or itself)", async () => {
217
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
218
+ const a = await connect(asA);
219
+ try {
220
+ expect(await a.call("list_node_files", { alias: SESSION_ALIAS })).toMatchObject({ ok: false, error: "node_token_cannot_browse_files" });
221
+ expect(await a.call("read_node_file", { node_id: NODE_B_ID, path: ".env" })).toMatchObject({ ok: false, error: "node_token_cannot_browse_files" });
222
+ expect(await a.call("list_node_files", { node_id: NODE_A_ID })).toMatchObject({ ok: false, error: "node_token_cannot_browse_files" });
223
+ expect(rowCount()).toBe(0);
224
+ } finally { await a.close(); }
225
+ }, 20_000);
226
+
227
+ test("only the token that asked can read a files result; node tokens never can", async () => {
228
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
229
+ const u = await connect(asUser);
230
+ const other = await connect(asUserOtherLogin);
231
+ const s = await connect(asSession);
232
+ const a = await connect(asA);
233
+ try {
234
+ const enq = await u.call("read_node_file", { alias: SESSION_ALIAS, path: "notes.txt" });
235
+ await s.call("get_rules_file_request", {});
236
+ await s.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", content: JSON.stringify({ path: "notes.txt", kind: "text", content: "x" }) });
237
+ expect(await u.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: true, status: "done" });
238
+ expect(await other.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: false, error: "request_not_found" });
239
+ expect(await a.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: false, error: "request_not_found" });
240
+ // The serving session itself cannot read the stored result back through the client tool either.
241
+ expect(await s.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: false, error: "request_not_found" });
242
+ } finally { await u.close(); await other.close(); await s.close(); await a.close(); }
243
+ }, 20_000);
244
+
245
+ test("rules/skills results stay readable by any writer of the network (unchanged)", async () => {
246
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
247
+ const u = await connect(asUser);
248
+ const other = await connect(asUserOtherLogin);
249
+ try {
250
+ const enq = await u.call("list_node_skills", { alias: SESSION_ALIAS });
251
+ expect(await other.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ ok: true, status: "pending" });
252
+ } finally { await u.close(); await other.close(); }
253
+ }, 20_000);
254
+ });
255
+
256
+ describe("a token bound to node A cannot touch node B's files request", () => {
257
+ test("B's request: A pulls nothing, A's ack is ignored, B pulls and acks it", async () => {
258
+ const u = await connect(asUser);
259
+ const a = await connect(asA);
260
+ const b = await connect(asB);
261
+ try {
262
+ const enq = await u.call("read_node_file", { node_id: NODE_B_ID, path: "secret-plan.md" });
263
+ expect(enq).toMatchObject({ ok: true });
264
+ expect((await a.call("get_rules_file_request", {})).request).toBeNull();
265
+ expect(await a.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", content: "{\"forged\":true}" })).toMatchObject({ ok: true, ignored: "unknown_or_foreign_request" });
266
+ expect(await u.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ status: "pending" });
267
+ expect((await b.call("get_rules_file_request", {})).request).toEqual({ request_id: enq.request_id, op: "file_read", content: "secret-plan.md" });
268
+ expect(await b.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", content: "{}" })).toMatchObject({ ok: true, status: "done" });
269
+ } finally { await u.close(); await a.close(); await b.close(); }
270
+ }, 20_000);
271
+
272
+ test("same alias in another network neither pulls nor acks; a peer's ack is ignored", async () => {
273
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
274
+ const u = await connect(asUser);
275
+ const other = await connect(asOtherNetSameAlias);
276
+ const peer = await connect(asPeer);
277
+ try {
278
+ const enq = await u.call("list_node_files", { alias: SESSION_ALIAS });
279
+ expect((await other.call("get_rules_file_request", {})).request).toBeNull();
280
+ expect(await other.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", content: "{}" })).toMatchObject({ ignored: "unknown_or_foreign_request" });
281
+ expect(await peer.call("ack_rules_file_request", { request_id: enq.request_id, status: "done", content: "{}" })).toMatchObject({ ignored: "unknown_or_foreign_request" });
282
+ expect(await u.call("get_rules_file_result", { request_id: enq.request_id })).toMatchObject({ status: "pending" });
283
+ } finally { await u.close(); await other.close(); await peer.close(); }
284
+ }, 20_000);
285
+
286
+ test("an unbound legacy token gets what skills gives it: alias fallback pull + ack, and it can set its own flag", async () => {
287
+ const u = await connect(asUser);
288
+ const legacy = await connect(asLegacy);
289
+ try {
290
+ await report(asLegacy, LEGACY_ALIAS, `sdk-${LEGACY_ID}`, ALL_FLAGS);
291
+ expect(capable(LEGACY_ALIAS)).toBe(1);
292
+ const skills = await u.call("list_node_skills", { node_id: LEGACY_ID });
293
+ const files = await u.call("list_node_files", { node_id: LEGACY_ID, path: "src" });
294
+ expect(skills).toMatchObject({ ok: true });
295
+ expect(files).toMatchObject({ ok: true });
296
+ const first = (await legacy.call("get_rules_file_request", {})).request;
297
+ const second = (await legacy.call("get_rules_file_request", {})).request;
298
+ expect([first.request_id, second.request_id].sort()).toEqual([skills.request_id, files.request_id].sort());
299
+ expect(await legacy.call("ack_rules_file_request", { request_id: files.request_id, status: "done", content: "{}" })).toMatchObject({ ok: true, status: "done" });
300
+ expect(await legacy.call("ack_rules_file_request", { request_id: skills.request_id, status: "done", content: "{}" })).toMatchObject({ ok: true, status: "done" });
301
+ } finally { await u.close(); await legacy.close(); }
302
+ }, 20_000);
303
+ });
304
+
305
+ describe("lanes, capability gate and ack size", () => {
306
+ test("files has its own single-flight lane", async () => {
307
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
308
+ const u = await connect(asUser);
309
+ try {
310
+ expect(await u.call("read_node_rules_file", { alias: SESSION_ALIAS })).toMatchObject({ ok: true });
311
+ expect(await u.call("list_node_skills", { alias: SESSION_ALIAS })).toMatchObject({ ok: true });
312
+ const list = await u.call("list_node_files", { alias: SESSION_ALIAS });
313
+ expect(list).toMatchObject({ ok: true });
314
+ expect(await u.call("read_node_file", { alias: SESSION_ALIAS, path: "a.txt" })).toMatchObject({ ok: false, error: "request_in_flight", existing_request_id: list.request_id });
315
+ } finally { await u.close(); }
316
+ }, 20_000);
317
+
318
+ test("rules+skills capable but not files capable → files_target_not_found, no row", async () => {
319
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, { rules_file_capable: true, skills_capable: true });
320
+ const u = await connect(asUser);
321
+ try {
322
+ expect(await u.call("list_node_files", { alias: SESSION_ALIAS })).toMatchObject({ ok: false, error: "files_target_not_found" });
323
+ expect(rowCount()).toBe(0);
324
+ } finally { await u.close(); }
325
+ }, 20_000);
326
+
327
+ test("a files ack may carry up to 1 MiB; a rules ack keeps its 256 KiB cap", async () => {
328
+ await report(asSession, SESSION_ALIAS, SESSION_RESUME, ALL_FLAGS);
329
+ const u = await connect(asUser);
330
+ const s = await connect(asSession);
331
+ try {
332
+ const big = JSON.stringify({ path: "big.txt", kind: "text", content: "\n".repeat(256 * 1024) });
333
+ expect(big.length).toBeGreaterThan(256 * 1024);
334
+ const f = await u.call("read_node_file", { alias: SESSION_ALIAS, path: "big.txt" });
335
+ const r = await u.call("read_node_rules_file", { alias: SESSION_ALIAS });
336
+ await s.call("get_rules_file_request", {});
337
+ await s.call("get_rules_file_request", {});
338
+ expect(await s.call("ack_rules_file_request", { request_id: f.request_id, status: "done", content: big })).toMatchObject({ ok: true, status: "done" });
339
+ expect(await s.call("ack_rules_file_request", { request_id: r.request_id, status: "done", content: "x".repeat(256 * 1024 + 1) })).toMatchObject({ ok: false, error: "content_too_large" });
340
+ const tooBig = await s.call("ack_rules_file_request", { request_id: r.request_id, status: "done", content: "x".repeat(1024 * 1024 + 1) });
341
+ expect(tooBig.ok).not.toBe(true);
342
+ } finally { await u.close(); await s.close(); }
343
+ }, 20_000);
344
+ });
@@ -45,7 +45,7 @@ const GOLDEN_RESPONSE_KEYS = {
45
45
  "process_rss_mb", "process_cpu_pct", "process_uptime_seconds",
46
46
  "process_in_flight_count", "network_id", "registered_at", "updated_at",
47
47
  "node_id", "session_id", "config_path", "channels", "last_seen_at", "model",
48
- "external_schedules", "rules_file_capable", "skills_capable", "runtime", "host", "process_telemetry",
48
+ "external_schedules", "rules_file_capable", "skills_capable", "files_capable", "runtime", "host", "process_telemetry",
49
49
  ],
50
50
  task: [
51
51
  "task_id", "from_node_id", "from_name", "to_node_id", "to_name", "priority",
@@ -19,7 +19,7 @@ export const SESSION_REST_COLUMNS = [
19
19
  "process_rss_mb", "process_cpu_pct", "process_uptime_seconds",
20
20
  "process_in_flight_count", "network_id", "registered_at", "updated_at",
21
21
  "node_id", "session_id", "config_path", "channels", "last_seen_at", "model",
22
- "external_schedules", "rules_file_capable", "skills_capable",
22
+ "external_schedules", "rules_file_capable", "skills_capable", "files_capable",
23
23
  ] as const;
24
24
 
25
25
  export const AUDIT_LOG_REST_COLUMNS = [
package/src/server.ts CHANGED
@@ -1683,6 +1683,7 @@ return Bun.serve({
1683
1683
  // app#225 follow-up — sqlite INTEGER 0/1 → boolean on the wire.
1684
1684
  rules_file_capable: s.rules_file_capable === 1,
1685
1685
  skills_capable: s.skills_capable === 1,
1686
+ files_capable: s.files_capable === 1,
1686
1687
  model: s.model ?? null,
1687
1688
  runtime: normalizeRuntime(s.agent),
1688
1689
  host: {
package/src/tools.ts CHANGED
@@ -721,8 +721,10 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
721
721
  rules_file_capable: z.literal(true).optional(),
722
722
  // Node skills view — same doorbell, ops skills_list / skill_read.
723
723
  skills_capable: z.literal(true).optional(),
724
+ // Project folder view — same doorbell, ops files_list / file_read.
725
+ files_capable: z.literal(true).optional(),
724
726
  },
725
- async ({ resume_id, alias, status, task, output, score, progress, server: srv, hostname: hn, agent: ag, project_dir: pd, version: ver, tmux_name: tmux, node_id, session_id, config_path, channels, model: mdl, node_name: nn, network_id: netId, host, process_telemetry: proc, external_schedules: externalSchedules, config_snapshot: cfgSnap, rules_file_capable: rulesFileCapable, skills_capable: skillsCapable }) => {
727
+ async ({ resume_id, alias, status, task, output, score, progress, server: srv, hostname: hn, agent: ag, project_dir: pd, version: ver, tmux_name: tmux, node_id, session_id, config_path, channels, model: mdl, node_name: nn, network_id: netId, host, process_telemetry: proc, external_schedules: externalSchedules, config_snapshot: cfgSnap, rules_file_capable: rulesFileCapable, skills_capable: skillsCapable, files_capable: filesCapable }) => {
726
728
  const effectiveNetId = getNetworkId(netId);
727
729
  const sessionNetId = effectiveNetId ?? "default";
728
730
  if (!callerTokenIsNetwork || !enforceNetworkId) {
@@ -931,6 +933,9 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
931
933
  if (skillsCapable === true && callerTokenIsNetwork && callerAlias && callerAlias === effectiveAlias) {
932
934
  db.run("UPDATE sessions SET skills_capable = 1 WHERE resume_id = ?1", [resume_id]);
933
935
  }
936
+ if (filesCapable === true && callerTokenIsNetwork && callerAlias && callerAlias === effectiveAlias) {
937
+ db.run("UPDATE sessions SET files_capable = 1 WHERE resume_id = ?1", [resume_id]);
938
+ }
934
939
  if (host || proc) {
935
940
  db.run(
936
941
  `INSERT INTO agent_telemetry (id, network_id, resume_id, alias, hostname, ip, cpu_load_1min, cpu_cores, mem_total_gb, mem_used_gb, mem_avail_gb, disk_total_gb, disk_used_gb, disk_avail_gb, process_rss_bytes, process_rss_mb, process_cpu_pct, process_uptime_seconds, process_in_flight_count, created_at)
@@ -3162,11 +3167,42 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3162
3167
  const SKILL_NAME_RE = /^[A-Za-z0-9._-]{1,64}$/;
3163
3168
  const isSkillsOp = (op: string) => op === "skills_list" || op === "skill_read";
3164
3169
 
3170
+ // Project folder view — files_list / file_read ride the same queue + doorbell.
3171
+ // `content` carries a path RELATIVE to the node's work dir. The hub refuses
3172
+ // absolute paths, `..` segments, backslashes and NUL before any row exists;
3173
+ // the node re-validates, realpath-contains the result in its work dir and
3174
+ // refuses secret-looking files (node-files.ts). Stricter than skills on the
3175
+ // caller: a node token may not browse another node's disk, and only the token
3176
+ // that asked can read the answer.
3177
+ const NODE_FILE_PATH_MAX = 1024;
3178
+ const NODE_FILES_RESULT_MAX_CHARS = 1024 * 1024;
3179
+ const isFilesOp = (op: string) => op === "files_list" || op === "file_read";
3180
+ const normalizeNodeRelPath = (raw: unknown): string | null => {
3181
+ if (raw === undefined || raw === null) return "";
3182
+ if (typeof raw !== "string" || raw.length > NODE_FILE_PATH_MAX) return null;
3183
+ if (raw.includes("\0") || raw.includes("\\")) return null;
3184
+ if (raw.startsWith("/") || raw.startsWith("~") || /^[A-Za-z]:/.test(raw)) return null;
3185
+ const segs = raw.split("/").filter((s) => s !== "" && s !== ".");
3186
+ if (segs.some((s) => s === "..")) return null;
3187
+ return segs.join("/");
3188
+ };
3189
+
3165
3190
  const enqueueRulesFileRequest = (
3166
- op: "read" | "write" | "skills_list" | "skill_read",
3191
+ op: "read" | "write" | "skills_list" | "skill_read" | "files_list" | "file_read",
3167
3192
  a: { node_id?: string; child_node_id?: string; alias?: string; network_id?: string; content?: string },
3168
3193
  ) => {
3169
3194
  const effectiveNetId = getNetworkId(a.network_id);
3195
+ if (isFilesOp(op)) {
3196
+ if (callerTokenIsNetwork) {
3197
+ return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "node_token_cannot_browse_files", message: "a node token cannot browse another node's project folder; use a user login" }) }] };
3198
+ }
3199
+ if (!canWrite(effectiveNetId)) return writeDeniedReply(effectiveNetId, `${op} node files`);
3200
+ const rel = normalizeNodeRelPath(a.content);
3201
+ if (rel === null || (op === "file_read" && rel === "")) {
3202
+ return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "invalid_path", reason: "path must be relative to the node work dir: no leading / or ~, no .. segment, no backslash or NUL, at most 1024 chars" }) }] };
3203
+ }
3204
+ a = { ...a, content: rel };
3205
+ }
3170
3206
  if (!canWrite(effectiveNetId)) return writeDeniedReply(effectiveNetId, isSkillsOp(op) ? `${op} node skills` : `${op} rules file`);
3171
3207
  if (op === "skill_read" && (typeof a.content !== "string" || !SKILL_NAME_RE.test(a.content) || a.content === "." || a.content === "..")) {
3172
3208
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "invalid_skill_name", reason: "name must match [A-Za-z0-9._-]{1,64} and not be . or .." }) }] };
@@ -3197,13 +3233,18 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3197
3233
  node = byAlias;
3198
3234
  } else {
3199
3235
  const session = db.get<{ alias: string; network_id: string | null }>(
3200
- isSkillsOp(op)
3236
+ isFilesOp(op)
3237
+ ? "SELECT alias, network_id FROM sessions WHERE alias = ?1 AND network_id = ?2 AND files_capable = 1 ORDER BY updated_at DESC LIMIT 1"
3238
+ : isSkillsOp(op)
3201
3239
  ? "SELECT alias, network_id FROM sessions WHERE alias = ?1 AND network_id = ?2 AND skills_capable = 1 ORDER BY updated_at DESC LIMIT 1"
3202
3240
  : "SELECT alias, network_id FROM sessions WHERE alias = ?1 AND network_id = ?2 AND rules_file_capable = 1 ORDER BY updated_at DESC LIMIT 1",
3203
3241
  a.alias,
3204
3242
  scopeNet,
3205
3243
  );
3206
3244
  if (!session) {
3245
+ if (isFilesOp(op)) {
3246
+ return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "files_target_not_found", alias: a.alias, message: "no node with this alias in the network, and no session with this alias that can serve its project folder (its channel server may be too old)" }) }] };
3247
+ }
3207
3248
  return isSkillsOp(op)
3208
3249
  ? { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "skills_target_not_found", alias: a.alias, message: "no node with this alias in the network, and no session with this alias that can serve its skills (its channel server may be too old)" }) }] }
3209
3250
  : { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "rules_file_target_not_found", alias: a.alias, message: "no node with this alias in the network, and no session with this alias that can serve rules files (its channel server may be too old)" }) }] };
@@ -3227,7 +3268,9 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3227
3268
  // Separate single-flight lanes: the client opens the rules file and the
3228
3269
  // skills list together; one must not block the other.
3229
3270
  const inFlight = db.get<{ request_id: string; created_at: number; pulled_at: number | null }>(
3230
- isSkillsOp(op)
3271
+ isFilesOp(op)
3272
+ ? "SELECT request_id, created_at, pulled_at FROM node_rules_requests WHERE node_id = ?1 AND status IN ('pending', 'in_progress') AND op IN ('files_list', 'file_read') ORDER BY created_at DESC LIMIT 1"
3273
+ : isSkillsOp(op)
3231
3274
  ? "SELECT request_id, created_at, pulled_at FROM node_rules_requests WHERE node_id = ?1 AND status IN ('pending', 'in_progress') AND op IN ('skills_list', 'skill_read') ORDER BY created_at DESC LIMIT 1"
3232
3275
  : "SELECT request_id, created_at, pulled_at FROM node_rules_requests WHERE node_id = ?1 AND status IN ('pending', 'in_progress') AND op IN ('read', 'write') ORDER BY created_at DESC LIMIT 1",
3233
3276
  nodeId,
@@ -3247,7 +3290,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3247
3290
  const networkId = node.network_id || "default";
3248
3291
  db.run(
3249
3292
  `INSERT INTO node_rules_requests (request_id, node_id, network_id, op, content, status, created_at, created_by_token) VALUES (?1, ?2, ?3, ?4, ?5, 'pending', ?6, ?7)`,
3250
- [requestId, nodeId, networkId, op, op === "write" || op === "skill_read" ? a.content! : null, Date.now(), callerTokenId || "unknown"],
3293
+ [requestId, nodeId, networkId, op, op === "write" || op === "skill_read" || isFilesOp(op) ? a.content! : null, Date.now(), callerTokenId || "unknown"],
3251
3294
  );
3252
3295
  pushEvent(node.alias, { type: "rules_file", request_id: requestId }, networkId);
3253
3296
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: true, request_id: requestId, op }) }] };
@@ -3299,6 +3342,30 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3299
3342
  async ({ node_id, child_node_id, alias, name, network_id }) => enqueueRulesFileRequest("skill_read", { node_id, child_node_id, alias, content: name, network_id }),
3300
3343
  );
3301
3344
 
3345
+ server.tool(
3346
+ "list_node_files",
3347
+ "Ask a node to list one directory level of its work dir (project folder view, read-only). `path` is relative to the work dir (default: the root); absolute paths and .. are refused. Poll get_rules_file_result — content is JSON {path, entries:[{name,type,size?,mtime?,hidden_reason?,no_descend?}], truncated, total}. User logins only.",
3348
+ {
3349
+ ...NODE_ID_ALIAS_FIELDS,
3350
+ alias: z.string().min(1).max(200).optional().describe("Target by alias instead of node_id — resolves to the node row, or to a session that reported files_capable."),
3351
+ path: z.string().max(NODE_FILE_PATH_MAX).optional().describe("Directory relative to the node's work dir; omit or \"\" for the root."),
3352
+ network_id: z.string().max(200).optional(),
3353
+ },
3354
+ async ({ node_id, child_node_id, alias, path: relPath, network_id }) => enqueueRulesFileRequest("files_list", { node_id, child_node_id, alias, content: relPath ?? "", network_id }),
3355
+ );
3356
+
3357
+ server.tool(
3358
+ "read_node_file",
3359
+ "Ask a node for one text file under its work dir (project folder view, read-only, 256 KiB cap; binary / too large → size only; secret-looking files → name only). `path` is relative to the work dir. Poll get_rules_file_result — content is JSON {path, name, kind, size?, mtime?, content?, hidden_reason?}. User logins only.",
3360
+ {
3361
+ ...NODE_ID_ALIAS_FIELDS,
3362
+ alias: z.string().min(1).max(200).optional().describe("Target by alias instead of node_id — resolves to the node row, or to a session that reported files_capable."),
3363
+ path: z.string().min(1).max(NODE_FILE_PATH_MAX).describe("File path relative to the node's work dir."),
3364
+ network_id: z.string().max(200).optional(),
3365
+ },
3366
+ async ({ node_id, child_node_id, alias, path: relPath, network_id }) => enqueueRulesFileRequest("file_read", { node_id, child_node_id, alias, content: relPath, network_id }),
3367
+ );
3368
+
3302
3369
  server.tool(
3303
3370
  "get_rules_file_request",
3304
3371
  "Node pulls its oldest pending rules-file request (called from agent-node when the SSE rules_file doorbell arrives). app#225.",
@@ -3336,7 +3403,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3336
3403
  request: {
3337
3404
  request_id: req.request_id,
3338
3405
  op: req.op,
3339
- ...(req.op === "write" || req.op === "skill_read" ? { content: req.content ?? "" } : {}),
3406
+ ...(req.op === "write" || req.op === "skill_read" || isFilesOp(req.op) ? { content: req.content ?? "" } : {}),
3340
3407
  },
3341
3408
  }),
3342
3409
  }],
@@ -3352,7 +3419,9 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3352
3419
  status: z.enum(["done", "failed"]),
3353
3420
  file_name: z.string().max(64).optional(),
3354
3421
  exists: z.boolean().optional(),
3355
- content: z.string().max(RULES_FILE_MAX_BYTES).optional(),
3422
+ // Files results (JSON listing / a 256 KiB file, escaped) may exceed the rules
3423
+ // cap; the per-op cap is enforced below once the row's op is known.
3424
+ content: z.string().max(NODE_FILES_RESULT_MAX_CHARS).optional(),
3356
3425
  error: z.string().max(2000).optional(),
3357
3426
  },
3358
3427
  async ({ request_id: requestId, status, file_name: fileName, exists, content, error: ackError }) => {
@@ -3365,7 +3434,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3365
3434
  const node = resolveCallerNode();
3366
3435
  const queueKey: string = node ? node.node_id : `session:${callerAlias}`;
3367
3436
  const req = db.get<any>(
3368
- "SELECT request_id, node_id, network_id, status FROM node_rules_requests WHERE request_id = ?1",
3437
+ "SELECT request_id, node_id, network_id, status, op FROM node_rules_requests WHERE request_id = ?1",
3369
3438
  requestId,
3370
3439
  );
3371
3440
  // 跨租户闸:只能 ack 自己的请求;别人的当不存在处理。session 键另外核网络。
@@ -3375,6 +3444,9 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3375
3444
  if (req.status === "done" || req.status === "failed" || req.status === "timeout") {
3376
3445
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: true, ignored: "already_terminal", current_status: req.status }) }] };
3377
3446
  }
3447
+ if (typeof content === "string" && !isFilesOp(req.op) && content.length > RULES_FILE_MAX_BYTES) {
3448
+ return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "content_too_large", max: RULES_FILE_MAX_BYTES }) }] };
3449
+ }
3378
3450
  db.run(
3379
3451
  `UPDATE node_rules_requests SET status = ?1, acked_at = ?2, file_name = ?3, file_exists = ?4, result_content = ?5, error = ?6 WHERE request_id = ?7`,
3380
3452
  [
@@ -3399,11 +3471,13 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
3399
3471
  const effectiveNetId = getNetworkId(clientNetId);
3400
3472
  if (!canWrite(effectiveNetId)) return writeDeniedReply(effectiveNetId, "read rules file result");
3401
3473
  const row = db.get<any>(
3402
- "SELECT request_id, node_id, network_id, op, status, file_name, file_exists, result_content, error, created_at, pulled_at, acked_at FROM node_rules_requests WHERE request_id = ?1",
3474
+ "SELECT request_id, node_id, network_id, op, status, file_name, file_exists, result_content, error, created_at, pulled_at, acked_at, created_by_token FROM node_rules_requests WHERE request_id = ?1",
3403
3475
  requestId,
3404
3476
  );
3405
3477
  // SEC-1:结果行的网络必须等于调用方作用域,否则当不存在。
3406
- if (!row || row.network_id !== (effectiveNetId || "default")) {
3478
+ // Project folder results: only the (user) token that asked may read them.
3479
+ const foreignFilesRow = !!row && isFilesOp(row.op) && (callerTokenIsNetwork || row.created_by_token !== (callerTokenId || "unknown"));
3480
+ if (!row || row.network_id !== (effectiveNetId || "default") || foreignFilesRow) {
3407
3481
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "request_not_found", request_id: requestId }) }] };
3408
3482
  }
3409
3483
  let status: string = row.status;