Repository navigation
Expand file tree
/
Copy pathtasks.ts
More file actions
235 lines (223 loc) · 7.76 KB
/
Copy pathtasks.ts
File metadata and controls
235 lines (223 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { BusContext } from "../context.js";
import { inTransaction } from "../db.js";
import { preview } from "../events.js";
import { ulid } from "../ulid.js";
import { parseJson, rowToTask, type TaskRow } from "../types.js";
import { jsonResult } from "./_shared.js";
const UPDATABLE_STATUS = z.enum([
"in_progress",
"blocked",
"completed",
"failed",
]);
export function registerTaskTools(
server: McpServer,
ctx: BusContext,
agentId: string,
): void {
const getTask = ctx.db.prepare("SELECT * FROM tasks WHERE id = ?");
server.registerTool(
"create_task",
{
title: "Create a task",
description:
"Add a task to the shared list for any agent to claim. Optionally declare " +
"dependencies (task IDs) that must complete before this task can be claimed. " +
"Returns the new task ID.",
inputSchema: {
title: z.string().min(1).describe("Short task title."),
description: z
.string()
.optional()
.describe("Longer description / acceptance criteria."),
depends_on: z
.array(z.string())
.optional()
.describe("Task IDs that must be completed before this can be claimed."),
metadata: z.record(z.unknown()).optional().describe(
F0AC
"Optional metadata."),
},
},
({ title, description, depends_on, metadata }) => {
const now = Date.now();
const taskId = ulid(now);
ctx.db
.prepare(
`INSERT INTO tasks
(id, title, description, status, assignee, depends_on, metadata, result, created_at, updated_at)
VALUES (@id, @title, @description, 'pending', NULL, @depends_on, @metadata, NULL, @now, @now)`,
)
.run({
id: taskId,
title,
description: description ?? null,
depends_on: depends_on ? JSON.stringify(depends_on) : null,
metadata: metadata ? JSON.stringify(metadata) : null,
now,
});
ctx.events.publish({ type: "task_created", task_id: taskId, title });
return jsonResult({ task_id: taskId, created_at: now });
},
);
server.registerTool(
"list_tasks",
{
title: "List tasks",
description:
"Query the shared task list. Filter by status or assignee. Completed tasks " +
"are hidden unless include_completed is true or you filter on status='completed'.",
inputSchema: {
status: z
.enum([
"pending",
"claimed",
"in_progress",
"blocked",
"completed",
"failed",
])
.optional()
.describe("Only return tasks in this status."),
assignee: z
.string()
.optional()
.describe("Only return tasks claimed by this agent ID."),
include_completed: z
.boolean()
.default(false)
.describe("Include completed tasks (ignored if status is set)."),
},
},
({ status, assignee, include_completed }) => {
const clauses: string[] = [];
const params: Record<string, string> = {};
if (status) {
clauses.push("status = @status");
params.status = status;
} else if (!include_completed) {
clauses.push("status != 'completed'");
}
if (assignee) {
clauses.push("assignee = @assignee");
params.assignee = assignee;
}
const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
const rows = ctx.db
.prepare(`SELECT * FROM tasks ${where} ORDER BY updated_at DESC`)
.all(params) as unknown as TaskRow[];
return jsonResult({ tasks: rows.map(rowToTask) });
},
);
server.registerTool(
"claim_task",
{
title: "Claim a task",
description:
"Atomically claim a pending task so no other agent can take it. Fails if " +
"the task is already claimed, doesn't exist, or has unmet dependencies. " +
"On success the task is assigned to you in 'claimed' state.",
inputSchema: {
task_id: z.string().min(1).describe("ID of the task to claim."),
},
},
({ task_id }) => {
const now = Date.now();
// Race-safe: the UPDATE only fires if the row is still pending AND every
// dependency is completed. changes() tells us if we won.
const info = ctx.db
.prepare(
`UPDATE tasks
SET status = 'claimed', assignee = @agent, updated_at = @now
WHERE id = @id
AND status = 'pending'
AND NOT EXISTS (
SELECT 1 FROM json_each(COALESCE(tasks.depends_on, '[]')) dep
WHERE NOT EXISTS (
SELECT 1 FROM tasks t2
WHERE t2.id = dep.value AND t2.status = 'completed'
)
)`,
)
.run({ id: task_id, agent: agentId, now });
if (info.changes === 1) {
const task = rowToTask(getTask.get(task_id) as unknown as TaskRow);
ctx.events.publish({
type: "task_claimed",
task_id,
assignee: agentId,
});
return jsonResult({ claimed: true, task });
}
// Didn't win — explain why.
const row = getTask.get(task_id) as TaskRow | undefined;
if (!row) return jsonResult({ claimed: false, reason: "not_found" });
if (row.status !== "pending")
return jsonResult({ claimed: false, reason: "already_claimed" });
return jsonResult({ claimed: false, reason: "dependencies_pending" });
},
);
server.registerTool(
"update_task_status",
{
title: "Update task status",
description:
"Move a task you've claimed through its lifecycle (in_progress, blocked, " +
"completed, failed). Only the assignee may update it. Attach optional notes " +
"and a result. Completing a task unblocks tasks that depend on it.",
inputSchema: {
task_id: z.string().min(1).describe("ID of the task to update."),
status: UPDATABLE_STATUS.describe("New status."),
notes: z
.string()
.optional()
.describe("Optional progress notes (stored in task metadata)."),
result: z
.string()
.optional()
.describe("Optional result/output, typically set on completion."),
},
},
({ task_id, status, notes, result }) => {
const outcome = inTransaction(ctx.db, () => {
const row = getTask.get(task_id) as TaskRow | undefined;
if (!row) return { updated: false, reason: "not_found" as const };
if (row.assignee !== agentId)
return { updated: false, reason: "not_assignee" as const };
const metadata = parseJson(row.metadata) ?? {};
if (notes !== undefined) metadata.notes = notes;
ctx.db
.prepare(
`UPDATE tasks
SET status = @status,
result = @result,
metadata = @metadata,
updated_at = @now
WHERE id = @id`,
)
.run({
id: task_id,
status,
result: result ?? row.result,
metadata: JSON.stringify(metadata),
now: Date.now(),
});
return {
updated: true as const,
task: rowToTask(getTask.get(task_id) as unknown as TaskRow),
};
});
if (outcome.updated && outcome.task && status === "completed") {
ctx.events.publish({
type: "task_completed",
task_id,
assignee: agentId,
result_preview: outcome.task.result
? preview(outcome.task.result)
: null,
});
}
return jsonResult(outcome);
},
);
}