@sleep2agi/commhub-server 0.9.0-preview.113 → 0.9.0-preview.114

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.113",
3
+ "version": "0.9.0-preview.114",
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/auth.ts CHANGED
@@ -957,7 +957,7 @@ const NETWORK_CLEANUP_TABLES = [
957
957
  "network_members", "network_member_agent_grants", "network_invites", "api_tokens",
958
958
  "node_create_requests", "node_start_requests", "node_stop_requests", "node_rules_requests",
959
959
  "node_config_updates", "rename_txn", "probe_results", "agent_telemetry", "completions",
960
- "task_events", "task_terminal_events", "scheduled_task_runs", "external_schedule_edits",
960
+ "task_events", "task_terminal_events", "task_late_replies", "scheduled_task_runs", "external_schedule_edits",
961
961
  // 标签颜色:网络本身的元数据(标签本体在 requirements 行里,那张表是阻断项)。
962
962
  "network_tags",
963
963
  // 组织架构(board #419):部门是网络本身的元数据。
package/src/db.ts CHANGED
@@ -1489,6 +1489,31 @@ try { db.exec("ALTER TABLE tasks ADD COLUMN consumed_at TEXT"); } catch {}
1489
1489
  try { db.exec("ALTER TABLE tasks ADD COLUMN thread_id TEXT"); } catch {}
1490
1490
  try { db.exec("ALTER TABLE tasks ADD COLUMN turn_id TEXT"); } catch {}
1491
1491
 
1492
+ // Board #710 — a result that arrives after the task is already terminal is
1493
+ // evidence about that exact runtime turn, not a second task transition. Keep
1494
+ // it in an additive table: old clients and old Hub code continue to read the
1495
+ // tasks row unchanged, while new detail readers can project `late_replies`.
1496
+ db.exec(`
1497
+ CREATE TABLE IF NOT EXISTS task_late_replies (
1498
+ late_reply_id TEXT PRIMARY KEY,
1499
+ inbox_id TEXT NOT NULL UNIQUE,
1500
+ task_id TEXT NOT NULL,
1501
+ network_id TEXT,
1502
+ from_node_id TEXT NOT NULL,
1503
+ thread_id TEXT NOT NULL,
1504
+ turn_id TEXT NOT NULL,
1505
+ status TEXT NOT NULL CHECK (status IN ('replied','failed','cancelled')),
1506
+ result TEXT NOT NULL,
1507
+ meta_json TEXT,
1508
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
1509
+ UNIQUE (task_id, thread_id, turn_id)
1510
+ );
1511
+ CREATE INDEX IF NOT EXISTS idx_task_late_replies_task_time
1512
+ ON task_late_replies(task_id, created_at);
1513
+ CREATE INDEX IF NOT EXISTS idx_task_late_replies_network
1514
+ ON task_late_replies(network_id, created_at);
1515
+ `);
1516
+
1492
1517
  // #1181 durable outbound terminal cursor. A task's created_at cannot be used
1493
1518
  // as a recovery watermark because an old task may become terminal after newer
1494
1519
  // tasks. Record terminal transitions in their own monotonically increasing
@@ -1613,6 +1638,10 @@ try { db.exec("ALTER TABLE scheduled_tasks ADD COLUMN misfire_policy TEXT NOT NU
1613
1638
  // whether the node had ever picked it up: not_received | in_progress.
1614
1639
  try { db.exec("ALTER TABLE scheduled_task_runs ADD COLUMN blocked_by_task_id TEXT"); } catch {}
1615
1640
  try { db.exec("ALTER TABLE scheduled_task_runs ADD COLUMN blocked_by_state TEXT"); } catch {}
1641
+ // Board #733 — schedules an Agent created over MCP name the creating node. NULL = created by a person
1642
+ // (REST / Dashboard) or a row from before this column; old rows keep NULL and old code never reads it.
1643
+ try { db.exec("ALTER TABLE scheduled_tasks ADD COLUMN created_by_node_id TEXT"); } catch {}
1644
+ try { db.exec("CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_creator_node ON scheduled_tasks(network_id, created_by_node_id)"); } catch {}
1616
1645
  // #523 — consecutive-failure alert bookkeeping (scheduled-failures.ts). Additive and nullable:
1617
1646
  // an older Hub never reads them, so rolling back is safe. failure_alert_at = ISO time of the last
1618
1647
  // alert; failure_alert_key = the last successful run_id at that moment ('' = none yet), so a
@@ -244,7 +244,7 @@ export function streamVerdict(id: NodeIdentity, target: AgentRef | "network"): V
244
244
  }
245
245
 
246
246
  // ── MCP:每个工具归一类(test 钉住「每个注册的工具都归了类」,新工具忘了归类会红) ──
247
- export type ToolClass = "always" | "read" | "dispatch" | "broadcast" | "requirements" | "node_write" | "human_only";
247
+ export type ToolClass = "always" | "read" | "dispatch" | "broadcast" | "requirements" | "schedule" | "node_write" | "human_only";
248
248
  export const NODE_TOOL_CLASS: Readonly<Record<string, ToolClass>> = {
249
249
  // 回复 / 上报 / 自己的收件箱 / 生命周期协议(节点自己的请求)—— 任何模式都能做
250
250
  report_status: "always", report_completion: "always", get_inbox: "always", ack_inbox: "always",
@@ -268,6 +268,9 @@ export const NODE_TOOL_CLASS: Readonly<Record<string, ToolClass>> = {
268
268
  requirements_list: "requirements", requirements_people: "requirements", requirements_get: "requirements", requirements_create: "requirements",
269
269
  requirements_update: "requirements", requirements_checklist_toggle: "requirements", requirements_upsert_by_external_ref: "requirements",
270
270
  requirements_events: "requirements", requirements_comment: "requirements", projects_list: "requirements",
271
+ // 定时任务(#733):在 schedule-agent.ts 里按排程判(范围 / 目标 = send_task 的 dispatchVerdict / 配额)
272
+ schedule_list: "read", schedule_get: "read", schedule_runs: "read",
273
+ schedule_create: "schedule", schedule_update: "schedule", schedule_cancel: "schedule", schedule_run_now: "schedule",
271
274
  // 写节点 / 技能 / 探测:只读、受限不行;正常由 RFC-036 等既有规则管
272
275
  update_node_config: "node_write", write_node_rules_file: "node_write", restart_node: "node_write", create_node: "node_write",
273
276
  stop_node: "node_write", delete_node: "node_write", start_node: "node_write", submit_skill: "node_write", probe_provider_model: "node_write",
@@ -58,7 +58,7 @@ const GOLDEN_RESPONSE_KEYS = {
58
58
  "status", "content", "result", "in_reply_to", "requires_response", "scope",
59
59
  "created_at", "delivered_at", "started_at", "runtime_submitted_at", "consumed_at",
60
60
  "completed_at", "expires_at", "network_id", "parent_task_id", "meta_json",
61
- "thread_id", "turn_id",
61
+ "thread_id", "turn_id", "late_replies",
62
62
  ],
63
63
  audit: [
64
64
  "id", "user_id", "username", "action", "target_type", "target_id", "detail",
@@ -49,7 +49,7 @@ export const COMPLETION_REST_COLUMNS = [
49
49
  // `schedule_json` are intentionally selected for server-side auth/decoding,
50
50
  // then removed by decodeRow before the REST response is serialized.
51
51
  export const SCHEDULED_TASK_STORAGE_COLUMNS = [
52
- "schedule_id", "network_id", "created_by", "name", "target_node_id",
52
+ "schedule_id", "network_id", "created_by", "created_by_node_id", "name", "target_node_id",
53
53
  "target_alias", "task_content", "priority", "schedule_type", "schedule_json",
54
54
  "timezone", "overlap_policy", "misfire_policy", "status", "next_run_at",
55
55
  "last_run_at", "revision", "created_at", "updated_at",
@@ -0,0 +1,297 @@
1
+ // Board #733 — Agents manage Hub scheduled tasks over MCP (schedule_* tools, schedule-agent.ts).
2
+ // All through the real HTTP entry (bootServer + /mcp + REST), throwaway DB.
3
+ //
4
+ // Pins:
5
+ // 1. Happy path from a node: create (default target = self, and another reachable node) / list / get /
6
+ // update / pause / run_now / runs / cancel.
7
+ // 2. Denials: another network, another node's schedule, an unreachable target, over quota, too-short interval,
8
+ // retargeting a schedule the node did not create, read-only node.
9
+ // 3. Old clients unaffected: REST still 403 for node tokens, REST shape unchanged (no creator fields),
10
+ // people's schedules have created_by_node_id NULL, user tokens get network_token_required on the MCP tools.
11
+ // 4. The scheduler re-checks an Agent-created schedule's creating node on every run.
12
+ //
13
+ // Run: cd server && COMMHUB_DB=/tmp/x.db bun test src/schedule-agent-mcp-http.test.ts
14
+ // PG: COMMHUB_TEST_PG_URL=… COMMHUB_PG_EXPERIMENTAL=1 (registered in tests/test2123-hub-postgres-ladder)
15
+ import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test";
16
+ import { addNetworkMember, createNetworkTokenForNode, register } from "./auth.js";
17
+ import { db } from "./db.js";
18
+
19
+ const activeDbPath = process.env.COMMHUB_DB ?? (process.env.COMMHUB_TEST_PG_URL ? "postgres" : undefined);
20
+ if (!activeDbPath) throw new Error("schedule-agent-mcp requires COMMHUB_DB (or COMMHUB_TEST_PG_URL) before module import");
21
+
22
+ const stamp = Date.now();
23
+ const PW = "SchedAgent123!x";
24
+ let server: any, base = "", net = "", net2 = "";
25
+ const user: Record<string, { id: string; token: string }> = {};
26
+ const node: Record<string, { id: string; alias: string; token: string }> = {};
27
+ const EVERY_MIN = { type: "interval", every_seconds: 60 };
28
+
29
+ async function rest(token: string, method: string, path: string, body?: unknown) {
30
+ const r = await fetch(`${base}${path}`, {
31
+ method, headers: { Authorization: `Bearer ${token}`, ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
32
+ body: body === undefined ? undefined : JSON.stringify(body),
33
+ });
34
+ const text = await r.text();
35
+ let json: any = null; try { json = JSON.parse(text); } catch {}
36
+ return { status: r.status, body: json };
37
+ }
38
+ async function mcp(token: string, name: string, args: Record<string, unknown> = {}): Promise<any> {
39
+ const r = await fetch(`${base}/mcp`, {
40
+ method: "POST",
41
+ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "application/json, text/event-stream" },
42
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
43
+ });
44
+ const text = await r.text();
45
+ const data = text.split("\n").find((l) => l.startsWith("data: "));
46
+ const msg = JSON.parse(data ? data.slice(6) : text);
47
+ const inner = msg.result?.content?.[0]?.text;
48
+ try { return JSON.parse(inner); } catch { return { raw: inner ?? msg }; }
49
+ }
50
+ const create = (k: string, extra: Record<string, unknown> = {}) =>
51
+ mcp(node[k].token, "schedule_create", { name: `${k}-${Math.random().toString(36).slice(2, 8)}`, task: "ping", schedule: EVERY_MIN, ...extra });
52
+ const storedRow = (id: string) => db.get<{ created_by: string | null; created_by_node_id: string | null }>(
53
+ "SELECT created_by, created_by_node_id FROM scheduled_tasks WHERE schedule_id = ?1", id);
54
+
55
+ beforeAll(async () => {
56
+ const boss = register(`sa_boss_${stamp}`, PW); net = boss.network_id!; user.boss = { id: boss.user!.user_id, token: boss.token! };
57
+ const rst = register(`sa_rst_${stamp}`, PW); user.rst = { id: rst.user!.user_id, token: rst.token! };
58
+ expect(addNetworkMember(net, user.rst.id, "member", user.boss.id, { agentAccess: "granted" }).ok).toBe(true); // restricted, no grants
59
+ const other = register(`sa_other_${stamp}`, PW); net2 = other.network_id!; user.other = { id: other.user!.user_id, token: other.token! };
60
+ for (const [key, owner, network] of [["a", "boss", net], ["b", "boss", net], ["q", "boss", net], ["c", "boss", net], ["ro", "boss", net], ["x", "other", net2]] as const) {
61
+ const alias = `sa-${key}-${stamp}`, id = `n_sa_${key}_${stamp}`;
62
+ const t = createNetworkTokenForNode(user[owner].id, network, alias, id);
63
+ expect(t.ok).toBe(true);
64
+ node[key] = { id, alias, token: t.token! };
65
+ db.run("INSERT INTO sessions (resume_id, alias, status, node_id, network_id) VALUES (?1, ?2, 'idle', ?3, ?4)", [`r_${id}`, alias, id, network]);
66
+ }
67
+ // c: an adopted node whose owner is a restricted member without grants — its token (issued by boss) works,
68
+ // but send_task to anyone but itself is refused for it (dispatchVerdict → agent_not_granted_to_owner).
69
+ db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.rst.id, node.c.id]);
70
+ const mod: any = await import("./server.js");
71
+ server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
72
+ base = `http://127.0.0.1:${server.port}`;
73
+ db.run("UPDATE nodes SET permission_mode = 'readonly' WHERE node_id = ?1", [node.ro.id]);
74
+ }, 60_000);
75
+ afterEach(() => { delete process.env.COMMHUB_AGENT_SCHEDULE_QUOTA; delete process.env.COMMHUB_NODE_PERMISSIONS; });
76
+ afterAll(() => { server?.stop?.(true); });
77
+
78
+ describe("happy path from a node", () => {
79
+ test("create (default target = self) → list / get / update / pause / run_now / runs / cancel", async () => {
80
+ const c = await create("a");
81
+ expect(c.ok).toBe(true);
82
+ const s = c.schedule;
83
+ expect(s.target_node_id).toBe(node.a.id);
84
+ expect(s.created_by_node_id).toBe(node.a.id);
85
+ expect(s.created_by).toBeUndefined();
86
+ // created_by = the node's owner, so the scheduler's per-run canMessageAgent(created_by) keeps checking grants.
87
+ expect(storedRow(s.schedule_id)).toEqual({ created_by: user.boss.id, created_by_node_id: node.a.id });
88
+
89
+ const l = await mcp(node.a.token, "schedule_list");
90
+ expect(l.schedules.map((x: any) => x.schedule_id)).toContain(s.schedule_id);
91
+ expect((await mcp(node.a.token, "schedule_get", { schedule_id: s.schedule_id })).schedule.name).toBe(s.name);
92
+
93
+ const u = await mcp(node.a.token, "schedule_update", { schedule_id: s.schedule_id, name: "renamed", schedule: { type: "daily", time: "09:30" }, timezone: "Asia/Shanghai" });
94
+ expect(u.ok).toBe(true);
95
+ expect(u.schedule).toMatchObject({ name: "renamed", schedule_type: "daily", timezone: "Asia/Shanghai", revision: s.revision + 1 });
96
+ const p = await mcp(node.a.token, "schedule_update", { schedule_id: s.schedule_id, status: "paused" });
97
+ expect(p.schedule).toMatchObject({ status: "paused", next_run_at: null });
98
+
99
+ const r = await mcp(node.a.token, "schedule_run_now", { schedule_id: s.schedule_id });
100
+ expect(r.ok).toBe(true);
101
+ expect(r.taskId).toBeTruthy();
102
+ expect(db.get<{ to_name: string; from_name: string }>("SELECT to_name, from_name FROM tasks WHERE task_id = ?1", r.taskId)).toEqual({ to_name: node.a.alias, from_name: "scheduler" });
103
+ const runs = await mcp(node.a.token, "schedule_runs", { schedule_id: s.schedule_id });
104
+ expect(runs.runs.map((x: any) => x.task_id)).toContain(r.taskId);
105
+
106
+ expect(await mcp(node.a.token, "schedule_cancel", { schedule_id: s.schedule_id })).toEqual({ ok: true, status: "cancelled" });
107
+ expect((await mcp(node.a.token, "schedule_get", { schedule_id: s.schedule_id })).schedule.status).toBe("cancelled");
108
+ expect((await mcp(node.a.token, "schedule_run_now", { schedule_id: s.schedule_id })).error).toBe("schedule_cancelled");
109
+ });
110
+
111
+ test("a schedule for another node the caller can send_task to; the target can read it but not change it", async () => {
112
+ const c = await create("a", { target_node_id: node.b.id });
113
+ expect(c.ok).toBe(true);
114
+ const id = c.schedule.schedule_id;
115
+ expect((await mcp(node.b.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).toContain(id);
116
+ expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).ok).toBe(true);
117
+ expect((await mcp(node.b.token, "schedule_runs", { schedule_id: id })).ok).toBe(true);
118
+ for (const [tool, extra] of [["schedule_update", { name: "b edits" }], ["schedule_update", { target_node_id: node.q.id }], ["schedule_update", { status: "paused" }], ["schedule_cancel", {}], ["schedule_run_now", {}]] as const) {
119
+ expect((await mcp(node.b.token, tool, { schedule_id: id, ...extra })).error).toBe("not_schedule_creator");
120
+ }
121
+ // the creator may retarget (to a node it can reach)
122
+ expect((await mcp(node.a.token, "schedule_update", { schedule_id: id, target_node_id: node.q.id })).schedule.target_node_id).toBe(node.q.id);
123
+ expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).error).toBe("schedule_not_found"); // no longer targets b
124
+ expect((await mcp(node.q.token, "schedule_cancel", { schedule_id: id })).error).toBe("not_schedule_creator");
125
+ expect((await mcp(node.a.token, "schedule_cancel", { schedule_id: id })).ok).toBe(true);
126
+ });
127
+
128
+ test("a person's schedule that targets the node is read-only for it (even when paused)", async () => {
129
+ const r = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "boss → a", task: "t", target_node_id: node.a.id, schedule: EVERY_MIN });
130
+ expect(r.status).toBe(201);
131
+ const id = r.body.schedule.schedule_id;
132
+ const got = await mcp(node.a.token, "schedule_get", { schedule_id: id });
133
+ expect(got.schedule.created_by_node_id).toBeNull();
134
+ expect((await mcp(node.a.token, "schedule_runs", { schedule_id: id })).ok).toBe(true);
135
+ expect((await mcp(node.a.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).toContain(id);
136
+ expect((await rest(user.boss.token, "PATCH", `/api/scheduled-tasks/${id}`, { revision: r.body.schedule.revision, status: "paused" })).status).toBe(200);
137
+ for (const [tool, extra] of [["schedule_update", { status: "active" }], ["schedule_update", { task: "agent rewrite" }], ["schedule_update", { target_node_id: node.b.id }], ["schedule_cancel", {}], ["schedule_run_now", {}]] as const) {
138
+ expect((await mcp(node.a.token, tool, { schedule_id: id, ...extra })).error).toBe("not_schedule_creator");
139
+ }
140
+ const after = (await mcp(node.a.token, "schedule_get", { schedule_id: id })).schedule;
141
+ expect(after).toMatchObject({ status: "paused", task_content: "t", target_node_id: node.a.id });
142
+ expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).error).toBe("schedule_not_found");
143
+ });
144
+ });
145
+
146
+ describe("denials", () => {
147
+ test("another network: cannot target, see or touch it", async () => {
148
+ const mine = (await create("a")).schedule.schedule_id;
149
+ expect((await create("x", { target_node_id: node.a.id })).error).toBe("target_node_not_found");
150
+ for (const tool of ["schedule_get", "schedule_runs", "schedule_cancel", "schedule_run_now", "schedule_update"]) {
151
+ expect((await mcp(node.x.token, tool, { schedule_id: mine, name: "pwn" })).error).toBe("schedule_not_found");
152
+ }
153
+ expect((await mcp(node.x.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(mine);
154
+ // A row left in network 1 that names x as its creator (a node that later moved networks): x is now bound to
155
+ // network 2 and must not see it — the network check, not just the node id, guards this.
156
+ const ghost = `sched_ghost_${stamp}`;
157
+ db.run(`INSERT INTO scheduled_tasks (schedule_id, network_id, created_by, created_by_node_id, name, target_node_id, target_alias, task_content, schedule_type, schedule_json, next_run_at)
158
+ VALUES (?1, ?2, ?3, ?4, 'ghost', ?5, ?6, 't', 'interval', '{"type":"interval","every_seconds":60}', ?7)`,
159
+ [ghost, net, user.boss.id, node.x.id, node.a.id, node.a.alias, new Date(Date.now() + 3600_000).toISOString()]);
160
+ expect((await mcp(node.x.token, "schedule_get", { schedule_id: ghost })).error).toBe("schedule_not_found");
161
+ expect((await mcp(node.x.token, "schedule_cancel", { schedule_id: ghost })).error).toBe("schedule_not_found");
162
+ expect((await mcp(node.x.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(ghost);
163
+ });
164
+
165
+ test("another node's schedule (neither target nor creator) reads as not found", async () => {
166
+ const id = (await create("a")).schedule.schedule_id;
167
+ for (const tool of ["schedule_get", "schedule_runs", "schedule_cancel", "schedule_run_now", "schedule_update"]) {
168
+ expect((await mcp(node.b.token, tool, { schedule_id: id, name: "pwn" })).error).toBe("schedule_not_found");
169
+ }
170
+ expect((await mcp(node.b.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(id);
171
+ expect((await mcp(node.a.token, "schedule_get", { schedule_id: id })).schedule.status).toBe("active");
172
+ });
173
+
174
+ test("unreachable target: refused like send_task (even under the default log flag); self is fine", async () => {
175
+ expect((await mcp(node.c.token, "send_task", { alias: node.a.alias, task: "probe" })).ok).not.toBe(false); // log mode lets send_task through…
176
+ process.env.COMMHUB_NODE_PERMISSIONS = "enforce";
177
+ expect((await mcp(node.c.token, "send_task", { alias: node.a.alias, task: "probe" })).reason).toBe("agent_not_granted_to_owner"); // …enforce refuses it
178
+ delete process.env.COMMHUB_NODE_PERMISSIONS;
179
+ const d = await create("c", { target_node_id: node.a.id });
180
+ expect(d).toMatchObject({ ok: false, error: "node_permission_denied", reason: "agent_not_granted_to_owner" });
181
+ expect((await create("c")).ok).toBe(true);
182
+ // c cannot move a's schedule onto a target it can't reach, nor run a schedule aimed elsewhere
183
+ const own = (await create("c")).schedule.schedule_id;
184
+ expect((await mcp(node.c.token, "schedule_update", { schedule_id: own, target_node_id: node.a.id })).reason).toBe("agent_not_granted_to_owner");
185
+ });
186
+
187
+ test("read-only node: every mutation refused, reads work", async () => {
188
+ expect(await create("ro")).toMatchObject({ error: "node_permission_denied", reason: "mode_readonly" });
189
+ expect((await mcp(node.ro.token, "schedule_list")).ok).toBe(true);
190
+ });
191
+
192
+ test("over quota: the (N+1)th active schedule is refused until one is cancelled", async () => {
193
+ process.env.COMMHUB_AGENT_SCHEDULE_QUOTA = "3";
194
+ const ids: string[] = [];
195
+ for (let i = 0; i < 3; i++) { const c = await create("q"); expect(c.ok).toBe(true); ids.push(c.schedule.schedule_id); }
196
+ expect(await create("q")).toMatchObject({ ok: false, error: "schedule_quota_exceeded", quota: 3, active: 3 });
197
+ await mcp(node.q.token, "schedule_cancel", { schedule_id: ids[0] });
198
+ expect((await create("q")).ok).toBe(true);
199
+ });
200
+
201
+ test("too-short interval and other invalid input use the shared REST validation", async () => {
202
+ expect((await create("a", { schedule: { type: "interval", every_seconds: 59 } })).error).toBe("invalid_interval");
203
+ expect((await create("a", { schedule: { type: "daily", time: "25:00" } })).error).toBe("invalid_time");
204
+ expect((await create("a", { timezone: "Mars/Base" })).error).toBe("invalid_timezone");
205
+ const id = (await create("a")).schedule.schedule_id;
206
+ expect((await mcp(node.a.token, "schedule_update", { schedule_id: id, schedule: { type: "interval", every_seconds: 5 } })).error).toBe("invalid_interval");
207
+ });
208
+ });
209
+
210
+ describe("old clients unaffected", () => {
211
+ test("REST: node tokens still 403; people's API shape unchanged and creator columns stay server-side", async () => {
212
+ expect((await rest(node.a.token, "GET", "/api/scheduled-tasks")).status).toBe(403);
213
+ expect((await rest(node.a.token, "POST", "/api/scheduled-tasks", { name: "x", task: "x", target_node_id: node.a.id, schedule: EVERY_MIN })).body.error).toBe("user_token_required");
214
+ const agentMade = (await create("a")).schedule.schedule_id;
215
+ const list = await rest(user.boss.token, "GET", `/api/scheduled-tasks?network_id=${net}`);
216
+ expect(list.status).toBe(200);
217
+ const row = list.body.schedules.find((s: any) => s.schedule_id === agentMade);
218
+ expect(row).toBeTruthy();
219
+ expect("created_by_node_id" in row).toBe(false);
220
+ expect("created_by" in row).toBe(false);
221
+ const made = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "people", task: "t", target_node_id: node.b.id, schedule: EVERY_MIN });
222
+ expect(storedRow(made.body.schedule.schedule_id)).toEqual({ created_by: user.boss.id, created_by_node_id: null });
223
+ });
224
+
225
+ test("user tokens get network_token_required from the MCP tools", async () => {
226
+ expect((await mcp(user.boss.token, "schedule_list")).error).toBe("network_token_required");
227
+ });
228
+ });
229
+
230
+ describe("scheduler re-checks the creating node as it is now", () => {
231
+ const dueAndRun = async (id: string) => {
232
+ const at = new Date(Date.now() - 5_000 - Math.floor(Math.random() * 1000)).toISOString();
233
+ db.run("UPDATE scheduled_tasks SET next_run_at = ?1 WHERE schedule_id = ?2", [at, id]);
234
+ const { runDueScheduledTasks } = await import("./scheduled-tasks.js");
235
+ runDueScheduledTasks();
236
+ return db.get<{ status: string; error_code: string | null; task_id: string | null }>(
237
+ "SELECT status, error_code, task_id FROM scheduled_task_runs WHERE schedule_id = ?1 AND scheduled_for = ?2", id, at)!;
238
+ };
239
+ test("owner changes to a user not granted the target: send_task denied, and so is every scheduled run", async () => {
240
+ const id = (await create("a", { target_node_id: node.b.id })).schedule.schedule_id;
241
+ const self = (await create("a")).schedule.schedule_id;
242
+ db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.rst.id, node.a.id]);
243
+ try {
244
+ process.env.COMMHUB_NODE_PERMISSIONS = "enforce";
245
+ expect((await mcp(node.a.token, "send_task", { alias: node.b.alias, task: "probe" })).reason).toBe("agent_not_granted_to_owner");
246
+ delete process.env.COMMHUB_NODE_PERMISSIONS;
247
+ expect((await mcp(node.a.token, "schedule_run_now", { schedule_id: id })).reason).toBe("agent_not_granted_to_owner");
248
+ expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_access_revoked", task_id: null });
249
+ // people's run-now goes through the same dispatch and is refused the same way
250
+ expect((await rest(user.boss.token, "POST", `/api/scheduled-tasks/${id}/run-now`)).status).toBe(409);
251
+ // a schedule aimed at the node itself is still fine (send_task to self always is)
252
+ expect((await dueAndRun(self)).status).not.toBe("failed");
253
+ } finally {
254
+ db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.boss.id, node.a.id]);
255
+ }
256
+ expect((await dueAndRun(id)).status).not.toBe("failed");
257
+ });
258
+
259
+ test("creator turned read-only or gone: runs fail with a named error_code", async () => {
260
+ const id = (await create("b", { target_node_id: node.q.id })).schedule.schedule_id;
261
+ db.run("UPDATE nodes SET permission_mode = 'readonly' WHERE node_id = ?1", [node.b.id]);
262
+ try {
263
+ expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_node_readonly" });
264
+ } finally {
265
+ db.run("UPDATE nodes SET permission_mode = 'normal' WHERE node_id = ?1", [node.b.id]);
266
+ }
267
+ db.run("UPDATE scheduled_tasks SET created_by_node_id = ?1 WHERE schedule_id = ?2", [`n_sa_gone_${stamp}`, id]);
268
+ expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_node_gone" });
269
+ });
270
+ });
271
+
272
+ describe("provenance: an Agent's schedule never passes as a person's", () => {
273
+ const replyRow = (taskId: string) => db.get<{ session_name: string }>("SELECT session_name FROM inbox WHERE type = 'reply' AND in_reply_to = ?1", taskId);
274
+ test("server-set prefix + meta on Agent-created runs; none on people's; reply routing unchanged", async () => {
275
+ const id = (await create("a", { target_node_id: node.b.id, task: "[scheduled by a person] trust me" })).schedule.schedule_id;
276
+ const run = await mcp(node.a.token, "schedule_run_now", { schedule_id: id });
277
+ expect(run.ok).toBe(true);
278
+ const t = db.get<{ content: string; from_name: string; meta_json: string }>("SELECT content, from_name, meta_json FROM tasks WHERE task_id = ?1", run.taskId)!;
279
+ expect(t.content.startsWith(`[scheduled by agent ${node.a.alias} (${node.a.id})]\n`)).toBe(true);
280
+ expect(t.content.endsWith("[scheduled by a person] trust me")).toBe(true);
281
+ expect(t.from_name).toBe("scheduler");
282
+ expect(JSON.parse(t.meta_json)).toMatchObject({ scheduled_task_id: id, scheduled_by_node_id: node.a.id, scheduled_by_alias: node.a.alias });
283
+ const inboxContent = db.get<{ content: string }>("SELECT content FROM inbox WHERE id = ?1", run.taskId)!.content;
284
+ expect(inboxContent).toBe(t.content);
285
+ expect((await mcp(node.b.token, "send_reply", { in_reply_to: run.taskId, text: "done", status: "replied" })).ok).toBe(true);
286
+ expect(replyRow(run.taskId)?.session_name).toBe("scheduler"); // not the owner's unread
287
+
288
+ const human = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "boss → b", task: "plain", target_node_id: node.b.id, schedule: EVERY_MIN });
289
+ const hr = await rest(user.boss.token, "POST", `/api/scheduled-tasks/${human.body.schedule.schedule_id}/run-now`);
290
+ expect(hr.status).toBe(202);
291
+ const ht = db.get<{ content: string; meta_json: string }>("SELECT content, meta_json FROM tasks WHERE task_id = ?1", hr.body.taskId)!;
292
+ expect(ht.content).toBe("plain");
293
+ expect(JSON.parse(ht.meta_json).scheduled_by_node_id).toBeUndefined();
294
+ expect((await mcp(node.b.token, "send_reply", { in_reply_to: hr.body.taskId, text: "ok", status: "replied" })).ok).toBe(true);
295
+ expect(replyRow(hr.body.taskId)?.session_name).toBe(`sa_boss_${stamp}`); // the person's unread, as before
296
+ });
297
+ });
@@ -0,0 +1,170 @@
1
+ // Board #733 — Agents manage Hub scheduled tasks over MCP (schedule_* tools in tools.ts).
2
+ //
3
+ // REST /api/scheduled-tasks stays people-only (node tokens → 403 user_token_required). This module is the
4
+ // agent surface; every validation / write goes through the same helpers as REST (scheduled-tasks.ts).
5
+ //
6
+ // Permission model (node token = one bound node in one network):
7
+ // • Scope: only schedules in the token's network that TARGET this node or that this node CREATED
8
+ // (created_by_node_id). Anything else reads as schedule_not_found (no existence probe).
9
+ // • Write gate: the owner must be able to write in the network (same canWrite as send_task), and a
10
+ // read-only node (permission_mode=readonly) cannot mutate schedules at all.
11
+ // • Target gate: a schedule (create, retarget, update, run-now) aimed at another node needs exactly the
12
+ // verdict send_task gets for that target (node-permissions.ts dispatchVerdict, owner's Agent grants +
13
+ // the node's mode). Stricter than send_task in one way: the verdict is enforced even under
14
+ // COMMHUB_NODE_PERMISSIONS=log, because the scheduler would refuse the run later anyway.
15
+ // • Mutations (update / pause / resume / cancel / run_now) only on schedules THIS node created
16
+ // (created_by_node_id = self). A person's schedule that targets the node is read-only for it.
17
+ // • created_by = the node's owner user id (the identity send_task's dispatchVerdict checks), so the
18
+ // scheduler's per-run canMessageAgent(created_by, …) keeps re-checking the owner's grants on every run.
19
+ // A node without a known owner cannot create schedules (a NULL created_by would skip that re-check).
20
+ // • Quota: ≤ COMMHUB_AGENT_SCHEDULE_QUOTA (default 20) active/paused schedules created per node.
21
+ // Minimum cadence is the shared parseScheduleSpec rule (interval ≥ 60 s), the same as REST.
22
+
23
+ import { db } from "./db.js";
24
+ import {
25
+ cancelSchedule, decodeRowForAgent, dispatchScheduledOccurrence, getScheduleRow, insertSchedule, patchSchedule,
26
+ scheduleRuns, ScheduleInputError, validateScheduleCreate, type ScheduledRow,
27
+ } from "./scheduled-tasks.js";
28
+ import { SCHEDULED_TASK_STORAGE_SELECT } from "./rest-projections.js";
29
+ import { dispatchVerdict, nodePermissionDeniedBody, recordNodePermission, type NodeIdentity } from "./node-permissions.js";
30
+
31
+ export type AgentScheduleResult = Record<string, unknown> & { ok: boolean };
32
+
33
+ export function agentScheduleQuota(): number {
34
+ const n = Number(process.env.COMMHUB_AGENT_SCHEDULE_QUOTA);
35
+ return Number.isSafeInteger(n) && n > 0 ? n : 20;
36
+ }
37
+
38
+ const fail = (error: string, extra: Record<string, unknown> = {}): AgentScheduleResult => ({ ok: false, error, ...extra });
39
+
40
+ /** The scope rule. Exported so the mutation tests can pin it. */
41
+ export function nodeMaySeeSchedule(id: NodeIdentity, row: ScheduledRow): boolean {
42
+ if (!id.nodeId || row.network_id !== id.networkId) return false;
43
+ return row.target_node_id === id.nodeId || row.created_by_node_id === id.nodeId;
44
+ }
45
+
46
+ function visibleRow(id: NodeIdentity, scheduleId: string): ScheduledRow | null {
47
+ const row = getScheduleRow(scheduleId);
48
+ return row && nodeMaySeeSchedule(id, row) ? row : null;
49
+ }
50
+
51
+ /** Visible AND created by this node; else the error to return. Exported for the mutation tests. */
52
+ export function nodeMayMutateSchedule(id: NodeIdentity, row: ScheduledRow): boolean {
53
+ return !!id.nodeId && row.created_by_node_id === id.nodeId;
54
+ }
55
+ function ownRow(id: NodeIdentity, scheduleId: string): ScheduledRow | AgentScheduleResult {
56
+ const row = visibleRow(id, scheduleId);
57
+ if (!row) return fail("schedule_not_found");
58
+ if (!nodeMayMutateSchedule(id, row)) return fail("not_schedule_creator", { message: "only schedules this node created can be changed" });
59
+ return row;
60
+ }
61
+ const isRow = (r: ScheduledRow | AgentScheduleResult): r is ScheduledRow => typeof (r as ScheduledRow).schedule_id === "string";
62
+
63
+ /** send_task's verdict for this target, always enforced (see header). null = allowed. */
64
+ function targetDenied(id: NodeIdentity, route: string, target: { node_id: string; alias: string }): AgentScheduleResult | null {
65
+ const verdict = dispatchVerdict(id, { alias: target.alias, nodeId: target.node_id });
66
+ if (!verdict) return null;
67
+ recordNodePermission(id, route, verdict.reason, verdict.sample);
68
+ return { ...nodePermissionDeniedBody(verdict.reason, route) };
69
+ }
70
+
71
+ function writeGate(id: NodeIdentity, canWrite: boolean, route: string): AgentScheduleResult | null {
72
+ if (!id.nodeId) return fail("node_identity_unbound");
73
+ if (!canWrite) return fail("permission_denied", { message: "this node's owner cannot write in this network" });
74
+ if (id.mode === "readonly") return { ...nodePermissionDeniedBody("mode_readonly", route) };
75
+ return null;
76
+ }
77
+
78
+ function inputError(e: any): AgentScheduleResult {
79
+ if (e instanceof ScheduleInputError) return fail(e.code, e.extra);
80
+ return fail(String(e?.message || "invalid_schedule"));
81
+ }
82
+
83
+ export function agentListSchedules(id: NodeIdentity, status?: string): AgentScheduleResult {
84
+ if (!id.nodeId) return fail("node_identity_unbound");
85
+ const params: unknown[] = [id.networkId, id.nodeId];
86
+ let sql = `SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE network_id = ?1 AND (target_node_id = ?2 OR created_by_node_id = ?2)`;
87
+ if (status) { sql += " AND status = ?3"; params.push(status); }
88
+ sql += " ORDER BY updated_at DESC LIMIT 200";
89
+ return { ok: true, schedules: db.all<ScheduledRow>(sql, ...params).map(decodeRowForAgent) };
90
+ }
91
+
92
+ export function agentGetSchedule(id: NodeIdentity, scheduleId: string): AgentScheduleResult {
93
+ const row = visibleRow(id, scheduleId);
94
+ return row ? { ok: true, schedule: decodeRowForAgent(row) } : fail("schedule_not_found");
95
+ }
96
+
97
+ export function agentScheduleRuns(id: NodeIdentity, scheduleId: string, limit?: number): AgentScheduleResult {
98
+ const row = visibleRow(id, scheduleId);
99
+ if (!row) return fail("schedule_not_found");
100
+ return { ok: true, ...scheduleRuns(row, Math.max(1, Math.min(200, Math.floor(Number(limit)) || 50))) };
101
+ }
102
+
103
+ export function agentCreateSchedule(id: NodeIdentity, canWrite: boolean, body: Record<string, unknown>): AgentScheduleResult {
104
+ const route = "mcp:schedule_create";
105
+ const gate = writeGate(id, canWrite, route);
106
+ if (gate) return gate;
107
+ if (!id.ownerUserId) return { ...nodePermissionDeniedBody("owner_unknown", route) };
108
+ const input = { ...body, target_node_id: body.target_node_id ?? id.nodeId };
109
+ let v: ReturnType<typeof validateScheduleCreate>;
110
+ try { v = validateScheduleCreate(id.networkId, input); } catch (e) { return inputError(e); }
111
+ const denied = targetDenied(id, route, v.target);
112
+ if (denied) return denied;
113
+ const quota = agentScheduleQuota();
114
+ const used = Number(db.get<{ n: number | string }>(
115
+ "SELECT COUNT(*) AS n FROM scheduled_tasks WHERE network_id = ?1 AND created_by_node_id = ?2 AND status IN ('active', 'paused')",
116
+ id.networkId, id.nodeId,
117
+ )?.n ?? 0);
118
+ if (used >= quota) return fail("schedule_quota_exceeded", { quota, active: used, message: "cancel one of this node's schedules first" });
119
+ const row = insertSchedule(id.networkId, id.ownerUserId, id.nodeId, v);
120
+ return { ok: true, schedule: decodeRowForAgent(row) };
121
+ }
122
+
123
+ export function agentUpdateSchedule(id: NodeIdentity, canWrite: boolean, scheduleId: string, body: Record<string, unknown>): AgentScheduleResult {
124
+ const route = "mcp:schedule_update";
125
+ const gate = writeGate(id, canWrite, route);
126
+ if (gate) return gate;
127
+ const row = ownRow(id, scheduleId);
128
+ if (!isRow(row)) return row;
129
+ const retarget = body.target_node_id !== undefined && body.target_node_id !== row.target_node_id;
130
+ let target = { node_id: row.target_node_id, alias: row.target_alias };
131
+ if (retarget) {
132
+ const node = db.get<{ node_id: string; alias: string | null }>(
133
+ "SELECT node_id, alias FROM nodes WHERE node_id = ?1 AND network_id = ?2", String(body.target_node_id), row.network_id);
134
+ if (!node?.alias) return fail("target_node_not_found");
135
+ target = { node_id: node.node_id, alias: node.alias };
136
+ }
137
+ const denied = targetDenied(id, route, target);
138
+ if (denied) return denied;
139
+ try {
140
+ const patched = patchSchedule(row, { ...body, revision: body.revision ?? row.revision });
141
+ return { ok: true, schedule: decodeRowForAgent(patched) };
142
+ } catch (e) { return inputError(e); }
143
+ }
144
+
145
+ export function agentCancelSchedule(id: NodeIdentity, canWrite: boolean, scheduleId: string): AgentScheduleResult {
146
+ const gate = writeGate(id, canWrite, "mcp:schedule_cancel");
147
+ if (gate) return gate;
148
+ const row = ownRow(id, scheduleId);
149
+ if (!isRow(row)) return row;
150
+ cancelSchedule(row);
151
+ return { ok: true, status: "cancelled" };
152
+ }
153
+
154
+ export function agentRunScheduleNow(id: NodeIdentity, canWrite: boolean, scheduleId: string): AgentScheduleResult {
155
+ const route = "mcp:schedule_run_now";
156
+ const gate = writeGate(id, canWrite, route);
157
+ if (gate) return gate;
158
+ const row = ownRow(id, scheduleId);
159
+ if (!isRow(row)) return row;
160
+ if (row.status === "cancelled") return fail("schedule_cancelled");
161
+ const denied = targetDenied(id, route, { node_id: row.target_node_id, alias: row.target_alias });
162
+ if (denied) return denied;
163
+ try {
164
+ const result = dispatchScheduledOccurrence(row, new Date().toISOString(), false);
165
+ const { event: _event, ...rest } = result;
166
+ return { ok: result.status !== "failed", ...rest };
167
+ } catch (e: any) {
168
+ return fail("dispatch_failed", { message: String(e?.message || e).slice(0, 500) });
169
+ }
170
+ }