@igorjpimenta/winch 0.0.0-stage → 2026.11.22-beta.3
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/.migrations/0000_next_katie_power.sql +99 -0
- package/.migrations/0001_add-events-kind.sql +8 -0
- package/.migrations/0002_drop-session-budgets.sql +1 -0
- package/.migrations/0003_pink_siren.sql +8 -0
- package/.migrations/0004_salty_trauma.sql +1 -0
- package/.migrations/0005_classy_havok.sql +1 -0
- package/.migrations/0006_recursive_items.sql +265 -0
- package/.migrations/0007_unify_statuses.sql +35 -0
- package/.migrations/0008_core_dispatch.sql +61 -0
- package/.migrations/0009_webhooks.sql +17 -0
- package/.migrations/0010_dispatch_consumer_id.sql +1 -0
- package/.migrations/0011_dispatch_delivery_leases.sql +53 -0
- package/.migrations/0012_dispatch_fencing_audit.sql +32 -0
- package/.migrations/meta/0000_snapshot.json +658 -0
- package/.migrations/meta/0001_snapshot.json +665 -0
- package/.migrations/meta/0002_snapshot.json +604 -0
- package/.migrations/meta/0003_snapshot.json +656 -0
- package/.migrations/meta/0004_snapshot.json +664 -0
- package/.migrations/meta/0005_snapshot.json +671 -0
- package/.migrations/meta/0006_snapshot.json +1089 -0
- package/.migrations/meta/0007_unify_statuses_snapshot.json +1097 -0
- package/.migrations/meta/0008_snapshot.json +1382 -0
- package/.migrations/meta/0009_snapshot.json +1428 -0
- package/.migrations/meta/0010_snapshot.json +1435 -0
- package/.migrations/meta/0011_snapshot.json +1670 -0
- package/.migrations/meta/0012_snapshot.json +1900 -0
- package/.migrations/meta/_journal.json +97 -0
- package/README.md +480 -2
- package/bin/winch.js +4 -0
- package/package.json +36 -4
- package/plugins/README.md +304 -0
- package/plugins/openclaw/winch/README.md +71 -0
- package/plugins/openclaw/winch/dispatch.js +236 -0
- package/plugins/openclaw/winch/index.js +139 -0
- package/plugins/openclaw/winch/meta.js +35 -0
- package/plugins/openclaw/winch/openclaw.plugin.json +43 -0
- package/plugins/openclaw/winch/package.json +37 -0
- package/plugins/opencode/winch/dispatch.js +228 -0
- package/plugins/opencode/winch/hooks.js +22 -0
- package/plugins/opencode/winch/identity.js +34 -0
- package/plugins/opencode/winch/index.js +198 -0
- package/plugins/opencode/winch/package.json +26 -0
- package/src/cli.js +434 -0
- package/src/config.js +181 -0
- package/src/db.js +39 -0
- package/src/deps.js +70 -0
- package/src/dispatch-server.js +204 -0
- package/src/dispatch-webhook.js +221 -0
- package/src/dispatch.js +258 -0
- package/src/events-ws.js +269 -0
- package/src/identity.js +140 -0
- package/src/ids.js +18 -0
- package/src/mcp-http.js +208 -0
- package/src/mcp.js +107 -0
- package/src/merges.js +150 -0
- package/src/paths.js +15 -0
- package/src/ready-queue.js +77 -0
- package/src/release.js +96 -0
- package/src/reserved.js +23 -0
- package/src/schema.js +302 -0
- package/src/store/admission.js +314 -0
- package/src/store/artifacts.js +24 -0
- package/src/store/attempts.js +22 -0
- package/src/store/claims.js +182 -0
- package/src/store/core.js +225 -0
- package/src/store/delivery.js +137 -0
- package/src/store/deps.js +62 -0
- package/src/store/dispatch-events.js +38 -0
- package/src/store/epics.js +31 -0
- package/src/store/events.js +21 -0
- package/src/store/gates.js +85 -0
- package/src/store/index.js +135 -0
- package/src/store/items.js +303 -0
- package/src/store/stories.js +37 -0
- package/src/store/sweep.js +77 -0
- package/src/store/tasks.js +190 -0
- package/src/store/webhook.js +197 -0
- package/src/tools/accept-wake.js +30 -0
- package/src/tools/assign.js +75 -0
- package/src/tools/claim.js +88 -0
- package/src/tools/definitions.js +371 -0
- package/src/tools/delegate.js +147 -0
- package/src/tools/index.js +44 -0
- package/src/tools/plan.js +262 -0
- package/src/tools/read.js +103 -0
- package/src/tools/report.js +190 -0
- package/src/tools/shared.js +62 -0
- package/src/watcher.js +105 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// plan.js — create and reshape the plan: plan, append, drop, restore.
|
|
2
|
+
import { addDep } from "../deps.js";
|
|
3
|
+
import { ok, err } from "./shared.js";
|
|
4
|
+
|
|
5
|
+
export function createPlanTools({ store, logger }) {
|
|
6
|
+
return {
|
|
7
|
+
async backlog_plan(args, identity, cfg) {
|
|
8
|
+
const epicIn = args?.epic;
|
|
9
|
+
const storiesIn = args?.stories;
|
|
10
|
+
if (!epicIn?.title || !Array.isArray(storiesIn) || storiesIn.length === 0) {
|
|
11
|
+
return err("backlog_plan: epic.title and at least one story are required", "rejected");
|
|
12
|
+
}
|
|
13
|
+
// Validate keys and dependency direction before writing anything:
|
|
14
|
+
// backward-only edges make the plan acyclic without a graph search,
|
|
15
|
+
// and a bad key must not leave half a plan behind.
|
|
16
|
+
const flat = [];
|
|
17
|
+
storiesIn.forEach((s, si) => {
|
|
18
|
+
(s?.tasks ?? []).forEach((t, ti) => {
|
|
19
|
+
flat.push({ storyIndex: si, taskIndex: ti, key: t?.key ?? `s${si}t${ti}`, dependsOn: t?.dependsOn ?? [], title: t?.title });
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
const keyIndex = new Map();
|
|
23
|
+
for (let i = 0; i < flat.length; i++) {
|
|
24
|
+
if (keyIndex.has(flat[i].key)) return err(`backlog_plan: duplicate task key: ${flat[i].key}`, "rejected");
|
|
25
|
+
keyIndex.set(flat[i].key, i);
|
|
26
|
+
}
|
|
27
|
+
for (const item of flat) {
|
|
28
|
+
for (const dep of item.dependsOn) {
|
|
29
|
+
if (!keyIndex.has(dep)) return err(`backlog_plan: unknown dependsOn key: ${dep} (in ${item.key})`, "rejected");
|
|
30
|
+
if (keyIndex.get(dep) >= keyIndex.get(item.key)) {
|
|
31
|
+
return err(`backlog_plan: dependsOn must point backward: ${item.key} -> ${dep}`, "rejected");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
let epic = null;
|
|
36
|
+
const createdIds = [];
|
|
37
|
+
try {
|
|
38
|
+
epic = store.epic.create({
|
|
39
|
+
title: epicIn.title,
|
|
40
|
+
description: epicIn.description,
|
|
41
|
+
acceptance: epicIn.acceptance,
|
|
42
|
+
status: "backlog",
|
|
43
|
+
priority: epicIn.priority ?? 50,
|
|
44
|
+
createdBy: identity.agent,
|
|
45
|
+
});
|
|
46
|
+
createdIds.push(epic.id);
|
|
47
|
+
const idByKey = new Map();
|
|
48
|
+
const stories = storiesIn.map((s, si) => {
|
|
49
|
+
const story = store.story.create({
|
|
50
|
+
epicId: epic.id,
|
|
51
|
+
title: s.title,
|
|
52
|
+
description: s.description,
|
|
53
|
+
acceptance: s.acceptance,
|
|
54
|
+
status: "backlog",
|
|
55
|
+
priority: s.priority ?? epic.priority,
|
|
56
|
+
capabilities: s.capabilities,
|
|
57
|
+
});
|
|
58
|
+
createdIds.push(story.id);
|
|
59
|
+
const tasks = (s.tasks ?? []).map((t, ti) => {
|
|
60
|
+
const key = t.key ?? `s${si}t${ti}`;
|
|
61
|
+
const task = store.task.create({
|
|
62
|
+
storyId: story.id,
|
|
63
|
+
title: t.title,
|
|
64
|
+
description: t.description,
|
|
65
|
+
status: "backlog",
|
|
66
|
+
executionKind: t.executionKind ?? "manual",
|
|
67
|
+
executionArgs: t.executionArgs,
|
|
68
|
+
capabilities: t.capabilities ?? s.capabilities,
|
|
69
|
+
});
|
|
70
|
+
createdIds.push(task.id);
|
|
71
|
+
idByKey.set(key, task.id);
|
|
72
|
+
return { id: task.id, key, title: task.title };
|
|
73
|
+
});
|
|
74
|
+
return { id: story.id, title: story.title, acceptance: story.acceptance ?? null, tasks };
|
|
75
|
+
});
|
|
76
|
+
let dependencies = 0;
|
|
77
|
+
for (const item of flat) {
|
|
78
|
+
for (const dep of item.dependsOn) {
|
|
79
|
+
const res = addDep(store, idByKey.get(item.key), idByKey.get(dep));
|
|
80
|
+
if (!res.ok) throw new Error(`dependency ${item.key} -> ${dep} rejected: ${res.code}`);
|
|
81
|
+
dependencies++;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
logger?.info?.(`backlog: ${identity.harness}:${identity.agent} planned ${epic.id} (${flat.length} tasks)`);
|
|
85
|
+
return ok(
|
|
86
|
+
JSON.stringify({ epic: { id: epic.id, title: epic.title }, stories, dependencies }),
|
|
87
|
+
{ status: "ok", agentId: identity.agent, epicId: epic.id, tasks: flat.length }
|
|
88
|
+
);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
// The epic is the root; deleting it cascades everything written under it.
|
|
91
|
+
if (epic?.id) {
|
|
92
|
+
for (const id of createdIds) store.db.prepare("DELETE FROM item_events WHERE item_id=?").run(id);
|
|
93
|
+
store.db.prepare("DELETE FROM items WHERE id=?").run(epic.id);
|
|
94
|
+
}
|
|
95
|
+
logger?.warn?.(`backlog_plan failed: ${e?.message}`);
|
|
96
|
+
return err(`backlog_plan failed: ${e?.message}`, "error");
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
async backlog_append(args, identity, cfg) {
|
|
101
|
+
let epic = args?.epicId ? store.epic.get(args.epicId) : null;
|
|
102
|
+
const story = args?.storyId ? store.story.get(args.storyId) : null;
|
|
103
|
+
if (args?.epicId && !epic) return err(`backlog_append: epic not found: ${args.epicId}`, "not_found");
|
|
104
|
+
if (args?.storyId && !story) return err(`backlog_append: story not found: ${args.storyId}`, "not_found");
|
|
105
|
+
if (story && !epic) epic = store.epic.get(story.epic_id) ?? null;
|
|
106
|
+
if (epic && story && story.epic_id !== epic.id) {
|
|
107
|
+
return err(`backlog_append: story ${story.id} does not belong to epic ${epic.id}`, "rejected");
|
|
108
|
+
}
|
|
109
|
+
if (args?.stories?.length && !epic) return err("backlog_append: epicId is required to add stories", "rejected");
|
|
110
|
+
if (args?.tasks?.length && !story) return err("backlog_append: storyId is required to add tasks", "rejected");
|
|
111
|
+
if (!args?.stories?.length && !args?.tasks?.length) {
|
|
112
|
+
return err("backlog_append: pass stories[] or tasks[]", "rejected");
|
|
113
|
+
}
|
|
114
|
+
// Appending after a close is a review finding: reopen the containers so
|
|
115
|
+
// the new work can actually be claimed.
|
|
116
|
+
if (epic && (epic.status === "done" || epic.status === "cancelled")) {
|
|
117
|
+
store.epic.update(epic.id, { status: "in_progress" });
|
|
118
|
+
}
|
|
119
|
+
if (story && (story.status === "done" || story.status === "cancelled")) {
|
|
120
|
+
store.story.update(story.id, { status: story.assignee_agent ? "ready" : "backlog" });
|
|
121
|
+
}
|
|
122
|
+
const created = { stories: [], tasks: [] };
|
|
123
|
+
const idByKey = new Map();
|
|
124
|
+
const actor = `${identity.harness}:${identity.agent}`;
|
|
125
|
+
try {
|
|
126
|
+
(args.stories ?? []).forEach((st, si) => {
|
|
127
|
+
if (!st?.title) throw new Error("every story needs a title");
|
|
128
|
+
const row = store.story.create({
|
|
129
|
+
epicId: epic.id,
|
|
130
|
+
title: st.title,
|
|
131
|
+
description: st.description,
|
|
132
|
+
acceptance: st.acceptance,
|
|
133
|
+
status: "backlog",
|
|
134
|
+
priority: st.priority ?? epic.priority,
|
|
135
|
+
capabilities: st.capabilities,
|
|
136
|
+
});
|
|
137
|
+
created.stories.push({ id: row.id, title: row.title });
|
|
138
|
+
(st.tasks ?? []).forEach((t, ti) => {
|
|
139
|
+
if (!t?.title) throw new Error("every task needs a title");
|
|
140
|
+
const key = t.key ?? `s${si}t${ti}`;
|
|
141
|
+
if (idByKey.has(key)) throw new Error(`duplicate key: ${key}`);
|
|
142
|
+
idByKey.set(key, { task: null, deferred: { storyId: row.id, t, si, ti } });
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
(args.tasks ?? []).forEach((t, i) => {
|
|
146
|
+
if (!t?.title) throw new Error("every task needs a title");
|
|
147
|
+
const key = t.key ?? `a${i}`;
|
|
148
|
+
if (idByKey.has(key)) throw new Error(`duplicate key: ${key}`);
|
|
149
|
+
idByKey.set(key, { task: null, deferred: { storyId: story.id, t, si: -1, ti: i } });
|
|
150
|
+
});
|
|
151
|
+
// Create every task after the stories exist, then resolve edges, so a
|
|
152
|
+
// bad dependency key rolls the whole call back instead of half-writing.
|
|
153
|
+
for (const [key, entry] of idByKey) {
|
|
154
|
+
const { storyId, t } = entry.deferred;
|
|
155
|
+
const parent = store.story.get(storyId);
|
|
156
|
+
const row = store.task.create({
|
|
157
|
+
storyId,
|
|
158
|
+
title: t.title,
|
|
159
|
+
description: t.description,
|
|
160
|
+
status: "backlog",
|
|
161
|
+
executionKind: t.executionKind ?? "manual",
|
|
162
|
+
executionArgs: t.executionArgs,
|
|
163
|
+
capabilities: t.capabilities ?? parent?.capabilities,
|
|
164
|
+
assigned: t.assigned ?? null,
|
|
165
|
+
assigneeHarness: t.assigned ? identity.harness : null,
|
|
166
|
+
});
|
|
167
|
+
entry.task = row;
|
|
168
|
+
created.tasks.push({ id: row.id, key, title: row.title, storyId, status: row.status, assigned: row.assignee_agent });
|
|
169
|
+
}
|
|
170
|
+
for (const [key, entry] of idByKey) {
|
|
171
|
+
for (const dep of entry.deferred.t.dependsOn ?? []) {
|
|
172
|
+
const target = idByKey.get(dep)?.task?.id ?? (store.itemKindOf(dep) ? dep : null);
|
|
173
|
+
if (!target) throw new Error(`unknown dependsOn: ${dep}`);
|
|
174
|
+
const res = addDep(store, entry.task.id, target);
|
|
175
|
+
if (!res.ok) throw new Error(`dependency ${key} -> ${dep} rejected: ${res.code}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// task.appended tells a dispatcher this work arrived after the plan;
|
|
179
|
+
// task.created (from plan) stays planner noise.
|
|
180
|
+
for (const t of created.tasks) {
|
|
181
|
+
store.event.append({
|
|
182
|
+
itemId: t.id, fromStatus: t.status, toStatus: t.status, kind: "task.appended",
|
|
183
|
+
actor, note: t.title.slice(0, 120),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return ok(
|
|
187
|
+
JSON.stringify({ ok: true, epicId: epic?.id ?? null, stories: created.stories, tasks: created.tasks }),
|
|
188
|
+
{ status: "ok", agentId: identity.agent, stories: created.stories.length, tasks: created.tasks.length }
|
|
189
|
+
);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
for (const t of created.tasks) {
|
|
192
|
+
store.db.prepare("DELETE FROM item_events WHERE item_id=?").run(t.id);
|
|
193
|
+
store.db.prepare("DELETE FROM items WHERE id=?").run(t.id);
|
|
194
|
+
}
|
|
195
|
+
for (const st of created.stories) {
|
|
196
|
+
store.db.prepare("DELETE FROM item_events WHERE item_id=?").run(st.id);
|
|
197
|
+
store.db.prepare("DELETE FROM items WHERE id=?").run(st.id);
|
|
198
|
+
}
|
|
199
|
+
logger?.warn?.(`backlog_append failed: ${e?.message}`);
|
|
200
|
+
return err(`backlog_append failed: ${e?.message}`, "error");
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
async backlog_drop(args, identity, cfg) {
|
|
205
|
+
const id = args?.id;
|
|
206
|
+
if (!id) return err("backlog_drop: id is required", "rejected");
|
|
207
|
+
const kind = store.itemKindOf(id);
|
|
208
|
+
const row = kind ? store.item.get(id, { includeDeleted: true }) : null;
|
|
209
|
+
if (!kind || !row) return err(`backlog_drop: not found: ${id}`, "not_found");
|
|
210
|
+
const tree = store.item.tree(id, { includeDeleted: true });
|
|
211
|
+
const tasks = tree.filter((r) => r.kind === "task");
|
|
212
|
+
// Dropping is reversible, but it must never hide work someone has done:
|
|
213
|
+
// the guard covers every descendant, not just the row named in the call.
|
|
214
|
+
const claimed = tasks.filter((t) => t.attempt_count > 0 || (t.status !== "backlog" && t.status !== "ready" && t.status !== "cancelled"));
|
|
215
|
+
if (claimed.length > 0) {
|
|
216
|
+
return err(
|
|
217
|
+
`${id} cannot be dropped: ${claimed.length} task(s) under it were already claimed (${claimed.map((t) => t.id).join(", ")}).`,
|
|
218
|
+
"not_droppable",
|
|
219
|
+
{ id, tasks: claimed.map((t) => t.id) }
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
const actor = `${identity.harness}:${identity.agent}`;
|
|
224
|
+
const result = store.item.drop(id, { reason: args?.reason ?? null, actor });
|
|
225
|
+
store.task.resumeDependencyWaits();
|
|
226
|
+
logger?.info?.(`backlog: ${actor} dropped ${id} (${tasks.length} task(s), soft)`);
|
|
227
|
+
return ok(JSON.stringify({ ok: true, dropped: { kind, id, reversible: true }, tasks: tasks.map((t) => t.id) }), {
|
|
228
|
+
status: "ok", agentId: identity.agent, level: kind, id,
|
|
229
|
+
});
|
|
230
|
+
} catch (e) {
|
|
231
|
+
logger?.warn?.(`backlog_drop failed: ${e?.message}`);
|
|
232
|
+
return err(`backlog_drop failed: ${e?.message}`, "error");
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
async backlog_restore(args, identity, cfg) {
|
|
237
|
+
const id = args?.id;
|
|
238
|
+
if (!id) return err("backlog_restore: id is required", "rejected");
|
|
239
|
+
const kind = store.itemKindOf(id);
|
|
240
|
+
const row = kind ? store.item.get(id, { includeDeleted: true }) : null;
|
|
241
|
+
if (!kind || !row) return err(`backlog_restore: not found: ${id}`, "not_found");
|
|
242
|
+
if (row.deleted_at == null && row.status !== "cancelled") {
|
|
243
|
+
return err(`${id} is ${row.status}, not dropped.`, "not_dropped");
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const actor = `${identity.harness}:${identity.agent}`;
|
|
247
|
+
const tree = store.item.tree(id, { includeDeleted: true });
|
|
248
|
+
// Restore top-down so a child never outlives its cancelled parent.
|
|
249
|
+
for (const r of tree) {
|
|
250
|
+
if (r.deleted_at != null || r.status === "cancelled") store.item.restore(r.id, { actor });
|
|
251
|
+
}
|
|
252
|
+
logger?.info?.(`backlog: ${actor} restored ${id} (${tree.filter((r) => r.kind === "task").length} task(s))`);
|
|
253
|
+
return ok(JSON.stringify({ ok: true, restored: { kind, id }, tasks: tree.filter((r) => r.kind === "task").map((r) => r.id) }), {
|
|
254
|
+
status: "ok", agentId: identity.agent, level: kind, id,
|
|
255
|
+
});
|
|
256
|
+
} catch (e) {
|
|
257
|
+
logger?.warn?.(`backlog_restore failed: ${e?.message}`);
|
|
258
|
+
return err(`backlog_restore failed: ${e?.message}`, "error");
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// read.js — one read tool, any level: epic, story or task. The row's `kind`
|
|
2
|
+
// selects the level.
|
|
3
|
+
import { depsResolved } from "../deps.js";
|
|
4
|
+
import { ok, err, collectStoryArtifacts, renderReceiverPrompt } from "./shared.js";
|
|
5
|
+
|
|
6
|
+
export function createReadTools({ store, logger }) {
|
|
7
|
+
const taskView = (t) => ({
|
|
8
|
+
id: t.id,
|
|
9
|
+
title: t.title,
|
|
10
|
+
description: t.description ?? null,
|
|
11
|
+
status: t.status,
|
|
12
|
+
assigned: t.assignee_agent ?? null,
|
|
13
|
+
capabilities: t.capabilities?.length ? t.capabilities : null,
|
|
14
|
+
result_summary: t.result_summary ?? null,
|
|
15
|
+
artifacts: store.artifact.listFor(t.id).map((a) => a.uri),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
async backlog_get(args, identity, cfg) {
|
|
20
|
+
const id = args?.id;
|
|
21
|
+
if (!id) return err("backlog_get: id is required", "rejected");
|
|
22
|
+
try {
|
|
23
|
+
const kind = store.itemKindOf(id);
|
|
24
|
+
if (!kind) return err(`backlog_get: not found: ${id}`, "not_found");
|
|
25
|
+
|
|
26
|
+
if (kind === "epic") {
|
|
27
|
+
const epic = store.epic.get(id);
|
|
28
|
+
const stories = store.item.list({ kind: "story", parentId: epic.id }).map((s) => ({
|
|
29
|
+
id: s.id, title: s.title, description: s.description ?? null, acceptance: s.acceptance ?? null,
|
|
30
|
+
status: s.status, priority: s.priority,
|
|
31
|
+
tasks: store.item.tree(s.id).filter((r) => r.kind === "task").map(taskView),
|
|
32
|
+
}));
|
|
33
|
+
const all = stories.flatMap((s) => s.tasks);
|
|
34
|
+
const count = (st) => all.filter((t) => t.status === st).length;
|
|
35
|
+
return ok(
|
|
36
|
+
JSON.stringify({
|
|
37
|
+
level: "epic",
|
|
38
|
+
epic: { id: epic.id, title: epic.title, description: epic.description, acceptance: epic.acceptance, status: epic.status, priority: epic.priority },
|
|
39
|
+
stories,
|
|
40
|
+
summary: { tasks: all.length, backlog: count("backlog"), ready: count("ready"), in_progress: count("in_progress"), blocked: count("blocked"), review: count("review"), done: count("done"), cancelled: count("cancelled") },
|
|
41
|
+
}),
|
|
42
|
+
{ status: "ok", agentId: identity.agent, level: "epic", id: epic.id }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (kind === "story") {
|
|
47
|
+
const story = store.story.get(id);
|
|
48
|
+
const epic = story.epic_id ? store.epic.get(story.epic_id) : null;
|
|
49
|
+
return ok(
|
|
50
|
+
JSON.stringify({
|
|
51
|
+
level: "story",
|
|
52
|
+
story: { id: story.id, title: story.title, description: story.description, acceptance: story.acceptance, status: story.status, priority: story.priority },
|
|
53
|
+
epic: epic ? { id: epic.id, title: epic.title, acceptance: epic.acceptance, status: epic.status } : null,
|
|
54
|
+
tasks: store.item.tree(story.id).filter((r) => r.kind === "task").map(taskView),
|
|
55
|
+
artifacts: collectStoryArtifacts(store, story.id),
|
|
56
|
+
}),
|
|
57
|
+
{ status: "ok", agentId: identity.agent, level: "story", id: story.id }
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const task = store.task.get(id);
|
|
62
|
+
const story = task.story_id ? store.story.get(task.story_id) : null;
|
|
63
|
+
const epic = task.epic_id ? store.epic.get(task.epic_id) : null;
|
|
64
|
+
const artifacts = story ? collectStoryArtifacts(store, story.id) : [];
|
|
65
|
+
const requiredCaps = Array.isArray(task.capabilities) && task.capabilities.length > 0
|
|
66
|
+
? task.capabilities
|
|
67
|
+
: Array.isArray(story?.capabilities) && story.capabilities.length > 0 ? story.capabilities : [];
|
|
68
|
+
const offered = Array.isArray(args?.capabilities) ? args.capabilities : [];
|
|
69
|
+
const deps = store.dep.listOutgoing(task.id).map((e) => {
|
|
70
|
+
const item = store.item.get(e.to_id);
|
|
71
|
+
return { kind: e.to_kind, id: e.to_id, status: item?.status ?? null };
|
|
72
|
+
});
|
|
73
|
+
const blockers = [];
|
|
74
|
+
if (task.status !== "ready") blockers.push(`task is ${task.status}`);
|
|
75
|
+
if (!story) blockers.push("story missing");
|
|
76
|
+
else if (story.status !== "ready" && story.status !== "in_progress") blockers.push(`story is ${story.status}`);
|
|
77
|
+
if (!epic) blockers.push("epic missing");
|
|
78
|
+
if (!depsResolved(store, task.id)) blockers.push("task dependencies unresolved");
|
|
79
|
+
if (task.assignee_agent !== identity.agent) blockers.push(`assigned to ${task.assignee_agent ?? "nobody"}`);
|
|
80
|
+
if (offered.length > 0 && requiredCaps.length > 0 && !offered.some((c) => requiredCaps.includes(c))) {
|
|
81
|
+
blockers.push(`requires [${requiredCaps.join(", ")}]`);
|
|
82
|
+
}
|
|
83
|
+
return ok(
|
|
84
|
+
JSON.stringify({
|
|
85
|
+
level: "task",
|
|
86
|
+
task,
|
|
87
|
+
story: story ? { id: story.id, title: story.title, description: story.description, acceptance: story.acceptance, status: story.status, priority: story.priority } : null,
|
|
88
|
+
epic: epic ? { id: epic.id, title: epic.title, description: epic.description, acceptance: epic.acceptance, status: epic.status, priority: epic.priority } : null,
|
|
89
|
+
artifacts,
|
|
90
|
+
dependencies: deps,
|
|
91
|
+
readiness: { ready: blockers.length === 0, blockers, requiresCapabilities: requiredCaps, assignedTo: task.assignee_agent ?? null },
|
|
92
|
+
recentEvents: store.event.listFor(task.id).slice(-10).map((e) => ({ id: e.id, kind: e.kind, actor: e.actor, note: e.note, at: e.created_at })).reverse(),
|
|
93
|
+
brief: story ? renderReceiverPrompt({ task, story, epic, artifacts }) : null,
|
|
94
|
+
}),
|
|
95
|
+
{ status: "ok", agentId: identity.agent, level: "task", id: task.id }
|
|
96
|
+
);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
logger?.warn?.(`backlog_get failed: ${e?.message}`);
|
|
99
|
+
return err(`backlog_get failed: ${e?.message}`, "error");
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// report.js — work reporting and gates: update, done, block, resume.
|
|
2
|
+
import { addDep } from "../deps.js";
|
|
3
|
+
import { identitySessionKey } from "../identity.js";
|
|
4
|
+
import { ok, err, clampNote } from "./shared.js";
|
|
5
|
+
|
|
6
|
+
function checkAttemptLease(store, task, args) {
|
|
7
|
+
if (!args?.attemptId) {
|
|
8
|
+
const live = task?.attempt_id ? store.attempt.get(task.attempt_id) : null;
|
|
9
|
+
if ((live?.lease_epoch ?? 0) > 0) {
|
|
10
|
+
return err(`${task.id} is wake-owned; pass attemptId and leaseEpoch from claim or recovery.`, "stale_claim", { taskId: task.id });
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const attempt = store.attempt.get(args.attemptId);
|
|
15
|
+
if (!attempt || attempt.item_id !== task.id || attempt.ended_at != null) {
|
|
16
|
+
return err(`${task.id} is not running attempt ${args.attemptId}; stop and re-read the task.`, "stale_claim", { taskId: task.id, attemptId: args.attemptId });
|
|
17
|
+
}
|
|
18
|
+
if ((attempt.lease_epoch ?? 0) > 0 && args?.leaseEpoch !== attempt.lease_epoch) {
|
|
19
|
+
return err(`${task.id} attempt ${args.attemptId} is fenced by lease epoch ${attempt.lease_epoch}; stop and re-read the task.`, "stale_claim", { taskId: task.id, attemptId: args.attemptId });
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createReportTools({ store, logger }) {
|
|
25
|
+
return {
|
|
26
|
+
async backlog_update(args, identity, cfg) {
|
|
27
|
+
const agentId = identity.agent;
|
|
28
|
+
const task = store.task.get(args?.taskId);
|
|
29
|
+
if (!task) return err(`backlog_update: not found: ${args?.taskId}`, "not_found");
|
|
30
|
+
if (task.status !== "in_progress") return err(`${task.id} is ${task.status}, not in_progress — claim it with backlog_claim first.`, "not_claimed", { taskId: task.id });
|
|
31
|
+
if (task.assignee_agent !== agentId) return err(`${task.id} is assigned to ${task.assignee_agent}, not ${agentId}.`, "not_assignee", { taskId: task.id, assigned: task.assignee_agent });
|
|
32
|
+
try {
|
|
33
|
+
const stale = checkAttemptLease(store, task, args);
|
|
34
|
+
if (stale) return stale;
|
|
35
|
+
const wantHeartbeat = args?.heartbeat !== false;
|
|
36
|
+
if (wantHeartbeat) {
|
|
37
|
+
const beat = store.task.heartbeat(task.id, agentId, args?.attemptId ?? null, args?.leaseEpoch ?? null);
|
|
38
|
+
if (beat === null) return err(`${task.id} is not running attempt ${args?.attemptId}; stop and re-read the task.`, "stale_claim", { taskId: task.id, attemptId: args?.attemptId ?? null });
|
|
39
|
+
}
|
|
40
|
+
const noteParts = [];
|
|
41
|
+
if (args?.progressSummary) noteParts.push(`progress: ${clampNote(args.progressSummary, cfg.noteProgressLimit)}`);
|
|
42
|
+
if (args?.comment) noteParts.push(`comment: ${clampNote(args.comment, cfg.noteProgressLimit)}`);
|
|
43
|
+
if (args?.addArtifact) noteParts.push(`artifact: ${clampNote(args.addArtifact, cfg.noteArtifactLimit)}`);
|
|
44
|
+
if (noteParts.length) {
|
|
45
|
+
store.event.append({
|
|
46
|
+
itemId: task.id,
|
|
47
|
+
sessionId: task.session_id,
|
|
48
|
+
fromStatus: task.status,
|
|
49
|
+
toStatus: task.status,
|
|
50
|
+
actor: agentId,
|
|
51
|
+
note: noteParts.join(" | "),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const fresh = store.task.get(task.id);
|
|
55
|
+
return ok(JSON.stringify({ ok: true, task: { id: fresh.id, status: fresh.status, last_heartbeat: fresh.last_heartbeat, attempt_count: fresh.attempt_count } }), { status: "ok", taskId: task.id, agentId });
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return err(`backlog_update failed: ${e?.message}`, "error");
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async backlog_done(args, identity, cfg) {
|
|
62
|
+
const agentId = identity.agent;
|
|
63
|
+
const sessionKey = identitySessionKey(identity);
|
|
64
|
+
const task = store.task.get(args?.taskId);
|
|
65
|
+
if (!task) return err(`backlog_done: not found: ${args?.taskId}`, "not_found");
|
|
66
|
+
if (task.status !== "in_progress") return err(`${task.id} is ${task.status}, not in_progress.`, "not_claimed", { taskId: task.id });
|
|
67
|
+
if (task.assignee_agent !== agentId) return err(`${task.id} is assigned to ${task.assignee_agent}, not ${agentId}.`, "not_assignee", { taskId: task.id, assigned: task.assignee_agent });
|
|
68
|
+
const stale = checkAttemptLease(store, task, args);
|
|
69
|
+
if (stale) return stale;
|
|
70
|
+
try {
|
|
71
|
+
const summary = clampNote(args.resultSummary, cfg.noteResultLimit);
|
|
72
|
+
const artifacts = Array.isArray(args.artifacts) ? args.artifacts.slice(0, cfg.maxArtifacts) : [];
|
|
73
|
+
const resultSummary = artifacts.length ? `${summary}\n\nartifacts: ${artifacts.join(", ")}` : summary;
|
|
74
|
+
const updated = store.task.update(task.id, {
|
|
75
|
+
status: "done", result_summary: resultSummary, actor: agentId, note: "done",
|
|
76
|
+
sessionKey, harness: identity.harness, session: identity.session, agentId,
|
|
77
|
+
});
|
|
78
|
+
for (const uri of artifacts) store.artifact.add({ itemId: task.id, attemptId: updated.attempt_id, kind: "link", uri });
|
|
79
|
+
store.task.resumeDependencyWaits();
|
|
80
|
+
// Close upward, one level at a time: a story closes when its last task
|
|
81
|
+
// does, and the epic closes only when its last story does. The epic
|
|
82
|
+
// row is what wakes its creator to review the delivery.
|
|
83
|
+
const actor = `${identity.harness}:${identity.agent}`;
|
|
84
|
+
const story = task.story_id ? store.story.get(task.story_id) : null;
|
|
85
|
+
let storyClosed = false;
|
|
86
|
+
if (story && story.status !== "done" && story.status !== "cancelled") {
|
|
87
|
+
const openTasks = store.item.tree(story.id).filter((r) => r.kind === "task" && r.status !== "done" && r.status !== "cancelled").length;
|
|
88
|
+
if (openTasks === 0) {
|
|
89
|
+
store.story.update(story.id, { status: "done", actor, note: story.id });
|
|
90
|
+
storyClosed = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const epicId = task.epic_id;
|
|
94
|
+
if (storyClosed && epicId) {
|
|
95
|
+
const epic = store.epic.get(epicId);
|
|
96
|
+
const openStories = store.item.tree(epicId).filter((r) => r.kind === "story" && r.status !== "done" && r.status !== "cancelled").length;
|
|
97
|
+
// One marker per close cycle, not per epic: a review that appends
|
|
98
|
+
// work reopens the epic, and its second close must wake the planner
|
|
99
|
+
// again or the fix is never confirmed.
|
|
100
|
+
if (openStories === 0) {
|
|
101
|
+
store.epic.update(epicId, { status: "done", actor, note: epicId });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
logger?.info?.(`backlog: ${agentId} completed ${task.id}`);
|
|
105
|
+
return ok(JSON.stringify({ ok: true, task: { id: updated.id, status: updated.status, result_summary: updated.result_summary } }), { status: "ok", taskId: task.id, agentId });
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return err(`backlog_done failed: ${e?.message}`, "error");
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
async backlog_block(args, identity, cfg) {
|
|
112
|
+
const agentId = identity.agent;
|
|
113
|
+
const task = store.task.get(args?.taskId);
|
|
114
|
+
if (!task) return err(`backlog_block: not found: ${args?.taskId}`, "not_found");
|
|
115
|
+
if (task.assignee_agent && task.assignee_agent !== agentId) return err(`${task.id} is assigned to ${task.assignee_agent}, not ${agentId}.`, "not_assignee", { taskId: task.id });
|
|
116
|
+
if (task.status !== "in_progress" && task.status !== "ready") return err(`${task.id} is ${task.status}; blocking applies to ready/in_progress only.`, "wrong_status", { taskId: task.id });
|
|
117
|
+
const stale = checkAttemptLease(store, task, args);
|
|
118
|
+
if (stale) return stale;
|
|
119
|
+
try {
|
|
120
|
+
const reason = clampNote(args.reason, cfg.noteReasonLimit);
|
|
121
|
+
let updated;
|
|
122
|
+
let depResult = null;
|
|
123
|
+
if (args?.gateType) {
|
|
124
|
+
// Human gates open through openGate — the one path that stamps
|
|
125
|
+
// gate.opened rows — never the generic update.
|
|
126
|
+
updated = store.task.openGate({
|
|
127
|
+
id: task.id, gateType: args.gateType, reason, actor: agentId,
|
|
128
|
+
harness: identity.harness, session: identity.session,
|
|
129
|
+
});
|
|
130
|
+
if (!updated) return err(`${task.id} is ${store.task.get(task.id)?.status ?? "gone"}; gating applies to ready/in_progress only.`, "wrong_status", { taskId: task.id });
|
|
131
|
+
if (args?.blockedBy) {
|
|
132
|
+
const r = addDep(store, task.id, args.blockedBy.id);
|
|
133
|
+
depResult = r.ok ? "added" : `failed:${r.code}`;
|
|
134
|
+
if (r.ok) {
|
|
135
|
+
store.event.append({
|
|
136
|
+
itemId: task.id, fromStatus: "blocked", toStatus: "blocked",
|
|
137
|
+
actor: agentId, note: `dep: ${task.id} depends on ${args.blockedBy.id}`,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} else if (args?.blockedBy) {
|
|
142
|
+
// A dependency wait returns to ready on its own; a plain manual
|
|
143
|
+
// block stays blocked until backlog_resume.
|
|
144
|
+
const r = addDep(store, task.id, args.blockedBy.id);
|
|
145
|
+
if (!r.ok) return err(`backlog_block: dependency rejected: ${r.code}`, "error", { taskId: task.id });
|
|
146
|
+
depResult = "added";
|
|
147
|
+
updated = store.task.blockOnDeps({
|
|
148
|
+
id: task.id, childId: args.blockedBy.id, actor: agentId,
|
|
149
|
+
note: `blocked: ${reason}; dep ${args.blockedBy.id}`,
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
updated = store.task.update(task.id, {
|
|
153
|
+
status: "blocked", actor: agentId, note: `blocked: ${reason}`,
|
|
154
|
+
sessionKey: identitySessionKey(identity), harness: identity.harness, session: identity.session, agentId,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
logger?.warn?.(`backlog: ${agentId} blocked ${task.id} (${reason})`);
|
|
158
|
+
return ok(JSON.stringify({ ok: true, task: { id: updated.id, status: updated.status }, dep: depResult }), { status: "ok", taskId: task.id, agentId, dep: depResult });
|
|
159
|
+
} catch (e) {
|
|
160
|
+
return err(`backlog_block failed: ${e?.message}`, "error");
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
async backlog_resume(args, identity, cfg) {
|
|
165
|
+
// The actor is the caller, never the caller's claim about who acts: a
|
|
166
|
+
// mismatched actor would mint another key's approval on the trail.
|
|
167
|
+
if (args?.actor != null && args.actor !== identity.agent) {
|
|
168
|
+
return err(`backlog_resume: refused actor ${JSON.stringify(args.actor)} — approvals record the caller (${identity.agent}).`, "actor_mismatch", { taskId: args?.taskId ?? null });
|
|
169
|
+
}
|
|
170
|
+
const actor = identity.agent;
|
|
171
|
+
const task = store.task.resumeGate({
|
|
172
|
+
id: args?.taskId, actor, harness: identity.harness, session: identity.session,
|
|
173
|
+
});
|
|
174
|
+
if (!task) {
|
|
175
|
+
// Name the common false alarm: the task was gated before but the gate
|
|
176
|
+
// has since dissolved (e.g. an unmerged close requeued it).
|
|
177
|
+
const current = args?.taskId ? store.task.get(args.taskId) : null;
|
|
178
|
+
const lastGate = current && store.event.listFor(current.id).slice().reverse().find(
|
|
179
|
+
(e) => typeof e.note === "string" && (e.note.startsWith("gate open:") || e.note === "gate resume")
|
|
180
|
+
);
|
|
181
|
+
if (lastGate && lastGate.note.startsWith("gate open:")) {
|
|
182
|
+
return err(`backlog_resume: ${current.id} was gated but the gate has dissolved (status ${current.status}) — nothing to resume; re-gate with backlog_block if approval is still needed.`, "gate_dissolved", { taskId: current.id, taskStatus: current.status });
|
|
183
|
+
}
|
|
184
|
+
return err(`backlog_resume: not resumable: ${args?.taskId}`, "not_resumable", { taskId: args?.taskId ?? null });
|
|
185
|
+
}
|
|
186
|
+
logger?.info?.(`backlog: ${actor} resumed ${task.id}`);
|
|
187
|
+
return ok(JSON.stringify({ ok: true, task: { id: task.id, status: task.status, gate_type: task.gate_type, assigned: task.assignee_agent } }), { status: "ok", taskId: task.id, actor });
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// shared.js — the pieces every tool handler needs: result shapes, note
|
|
2
|
+
// clamping, and the store-derived handoff text.
|
|
3
|
+
|
|
4
|
+
export function ok(text, details) {
|
|
5
|
+
return { content: [{ type: "text", text }], details };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function err(text, reason, extra = {}) {
|
|
9
|
+
return {
|
|
10
|
+
content: [{ type: "text", text }],
|
|
11
|
+
details: { status: "rejected", reason, ...extra },
|
|
12
|
+
isError: true,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Tool handlers carry internal `details`; MCP results do not. Keep the
|
|
17
|
+
// protocol error bit without leaking bookkeeping into the content.
|
|
18
|
+
export function toMcpResult(res) {
|
|
19
|
+
return res?.isError
|
|
20
|
+
? { content: res.content, isError: true }
|
|
21
|
+
: { content: res.content };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function clampNote(s, max = 2000) {
|
|
25
|
+
if (typeof s !== "string") return null;
|
|
26
|
+
return s.length > max ? s.slice(0, max) + "…" : s;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Artifacts upstream tasks recorded on this story: addArtifact events,
|
|
30
|
+
// explicit artifact rows, and done-result artifact lists, deduped.
|
|
31
|
+
export function collectStoryArtifacts(store, storyId) {
|
|
32
|
+
const out = new Set();
|
|
33
|
+
const story = store.item.get(storyId);
|
|
34
|
+
if (!story) return [];
|
|
35
|
+
const tasks = store.item.tree(storyId).filter((r) => r.kind === "task");
|
|
36
|
+
for (const t of tasks) {
|
|
37
|
+
for (const e of store.event.listFor(t.id)) {
|
|
38
|
+
if (typeof e.note === "string" && e.note.startsWith("artifact:")) {
|
|
39
|
+
out.add(e.note.slice("artifact:".length).trim());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const a of store.artifact.listFor(t.id)) out.add(a.uri);
|
|
43
|
+
const m = t.result_summary ? t.result_summary.match(/\nartifacts: (.+)$/) : null;
|
|
44
|
+
if (m) for (const a of m[1].split(",")) out.add(a.trim());
|
|
45
|
+
}
|
|
46
|
+
return [...out].filter(Boolean);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Rendered exclusively from store rows (task/story/epic/artifact events).
|
|
50
|
+
// No session history, call params, or conversation content can enter this
|
|
51
|
+
// string — that is the handoff contract: a receiver prompt is a projection
|
|
52
|
+
// of the store, nothing else.
|
|
53
|
+
export function renderReceiverPrompt({ task, story, epic, artifacts }) {
|
|
54
|
+
const parts = [
|
|
55
|
+
`# Task: ${task.title}`,
|
|
56
|
+
task.description ? `## Context\n${task.description}` : null,
|
|
57
|
+
`## Acceptance\n${story?.acceptance ?? "(none recorded)"}`,
|
|
58
|
+
`## Upstream artifacts\n${artifacts.length ? artifacts.map((a) => `- ${a}`).join("\n") : "(none yet)"}`,
|
|
59
|
+
`## Parent\n${epic?.title ?? "(none)"} — ${story?.title ?? "(none)"}`,
|
|
60
|
+
];
|
|
61
|
+
return parts.filter(Boolean).join("\n\n");
|
|
62
|
+
}
|