@igorjpimenta/winch 2026.11.23-beta.1 → 2026.11.23-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/0014_usage_events.sql +30 -0
- package/.migrations/meta/0012_snapshot.json +2 -2
- package/.migrations/meta/0013_snapshot.json +9 -9
- package/.migrations/meta/0014_snapshot.json +2121 -0
- package/.migrations/meta/_journal.json +7 -0
- package/README.md +26 -6
- package/package.json +1 -1
- package/plugins/README.md +11 -0
- package/plugins/openclaw/winch/README.md +24 -1
- package/plugins/openclaw/winch/index.js +24 -3
- package/plugins/openclaw/winch/package.json +3 -2
- package/plugins/openclaw/winch/usage.js +111 -0
- package/plugins/opencode/winch/hooks.js +16 -2
- package/plugins/opencode/winch/index.js +20 -2
- package/plugins/opencode/winch/package.json +4 -3
- package/plugins/opencode/winch/usage.js +120 -0
- package/src/cli.js +89 -2
- package/src/config.js +7 -0
- package/src/merges.js +147 -12
- package/src/schema.js +30 -0
- package/src/store/artifacts.js +71 -0
- package/src/store/index.js +3 -0
- package/src/store/usage.js +85 -0
- package/src/tools/definitions.js +16 -1
- package/src/tools/index.js +16 -0
- package/src/tools/report.js +59 -4
- package/src/tools/shared.js +9 -0
- package/src/webhook/server.js +57 -0
package/src/tools/report.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// report.js — work reporting and gates: update, done, block, resume.
|
|
2
|
-
import { addDep } from "../deps.js";
|
|
2
|
+
import { addDep, depsResolved } from "../deps.js";
|
|
3
3
|
import { identitySessionKey } from "../identity.js";
|
|
4
4
|
import { ok, err, clampNote } from "./shared.js";
|
|
5
|
+
import { attachArtifact, inferArtifactKind } from "../store/artifacts.js";
|
|
5
6
|
|
|
6
7
|
function checkAttemptLease(store, task, args) {
|
|
7
8
|
if (!args?.attemptId) {
|
|
@@ -40,7 +41,7 @@ export function createReportTools({ store, logger }) {
|
|
|
40
41
|
const noteParts = [];
|
|
41
42
|
if (args?.progressSummary) noteParts.push(`progress: ${clampNote(args.progressSummary, cfg.noteProgressLimit)}`);
|
|
42
43
|
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 (args?.addArtifact) noteParts.push(`artifact: ${clampNote(typeof args.addArtifact === "string" ? args.addArtifact : args.addArtifact?.uri, cfg.noteArtifactLimit)}`);
|
|
44
45
|
if (noteParts.length) {
|
|
45
46
|
store.event.append({
|
|
46
47
|
itemId: task.id,
|
|
@@ -51,6 +52,22 @@ export function createReportTools({ store, logger }) {
|
|
|
51
52
|
note: noteParts.join(" | "),
|
|
52
53
|
});
|
|
53
54
|
}
|
|
55
|
+
if (args?.addArtifact) {
|
|
56
|
+
const raw = args.addArtifact;
|
|
57
|
+
const uri = typeof raw === "string" ? raw : raw?.uri;
|
|
58
|
+
if (uri) {
|
|
59
|
+
const kind = args.addArtifactKind
|
|
60
|
+
?? (typeof raw === "object" && raw.kind ? raw.kind : null)
|
|
61
|
+
?? inferArtifactKind(uri);
|
|
62
|
+
attachArtifact(store, {
|
|
63
|
+
itemId: task.id,
|
|
64
|
+
attemptId: task.attempt_id,
|
|
65
|
+
kind,
|
|
66
|
+
uri: String(uri),
|
|
67
|
+
label: (typeof raw === "object" && raw.label) || null,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
54
71
|
const fresh = store.task.get(task.id);
|
|
55
72
|
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
73
|
} catch (e) {
|
|
@@ -70,12 +87,50 @@ export function createReportTools({ store, logger }) {
|
|
|
70
87
|
try {
|
|
71
88
|
const summary = clampNote(args.resultSummary, cfg.noteResultLimit);
|
|
72
89
|
const artifacts = Array.isArray(args.artifacts) ? args.artifacts.slice(0, cfg.maxArtifacts) : [];
|
|
73
|
-
const
|
|
90
|
+
const hitlKinds = cfg.hitlArtifactKinds ?? [];
|
|
91
|
+
for (const a of artifacts) {
|
|
92
|
+
const uri = typeof a === "string" ? a : a?.uri;
|
|
93
|
+
const kind = (typeof a === "object" && a.kind) || inferArtifactKind(uri);
|
|
94
|
+
if (hitlKinds.includes(kind)) {
|
|
95
|
+
return err(
|
|
96
|
+
`HITL artifact (${kind}) must be attached while in_progress via backlog_update addArtifact — it opens the human review child that gates this close: ${uri}`,
|
|
97
|
+
"hitl_at_done",
|
|
98
|
+
{ taskId: task.id },
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!depsResolved(store, task.id)) {
|
|
103
|
+
const openDeps = store.dep.listOutgoing(task.id)
|
|
104
|
+
.map((e) => e.to_id)
|
|
105
|
+
.filter((id) => {
|
|
106
|
+
const d = store.item.get(id, { includeDeleted: true });
|
|
107
|
+
return d && d.status !== "done" && d.status !== "cancelled";
|
|
108
|
+
});
|
|
109
|
+
return err(
|
|
110
|
+
`${task.id} cannot close: unresolved deps ${openDeps.join(", ")}`,
|
|
111
|
+
"deps_unmet",
|
|
112
|
+
{ taskId: task.id, openDeps },
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const artifactUris = artifacts
|
|
116
|
+
.map((a) => (typeof a === "string" ? a : a?.uri))
|
|
117
|
+
.filter(Boolean);
|
|
118
|
+
const resultSummary = artifactUris.length ? `${summary}\n\nartifacts: ${artifactUris.join(", ")}` : summary;
|
|
74
119
|
const updated = store.task.update(task.id, {
|
|
75
120
|
status: "done", result_summary: resultSummary, actor: agentId, note: "done",
|
|
76
121
|
sessionKey, harness: identity.harness, session: identity.session, agentId,
|
|
77
122
|
});
|
|
78
|
-
for (const
|
|
123
|
+
for (const a of artifacts) {
|
|
124
|
+
const uri = typeof a === "string" ? a : a?.uri;
|
|
125
|
+
if (!uri) continue;
|
|
126
|
+
store.artifact.add({
|
|
127
|
+
itemId: task.id,
|
|
128
|
+
attemptId: updated.attempt_id,
|
|
129
|
+
kind: (typeof a === "object" && a.kind) || inferArtifactKind(uri),
|
|
130
|
+
uri,
|
|
131
|
+
label: (typeof a === "object" && a.label) || null,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
79
134
|
store.task.resumeDependencyWaits();
|
|
80
135
|
// Close upward, one level at a time: a story closes when its last task
|
|
81
136
|
// does, and the epic closes only when its last story does. The epic
|
package/src/tools/shared.js
CHANGED
|
@@ -57,6 +57,15 @@ export function renderReceiverPrompt({ task, story, epic, artifacts }) {
|
|
|
57
57
|
`## Acceptance\n${story?.acceptance ?? "(none recorded)"}`,
|
|
58
58
|
`## Upstream artifacts\n${artifacts.length ? artifacts.map((a) => `- ${a}`).join("\n") : "(none yet)"}`,
|
|
59
59
|
`## Parent\n${epic?.title ?? "(none)"} — ${story?.title ?? "(none)"}`,
|
|
60
|
+
`## PR protocol (mandatory when opening a pull request)\n` +
|
|
61
|
+
`1. PR body MUST contain:\n` +
|
|
62
|
+
` Backlog: <epic-id>\n` +
|
|
63
|
+
` Backlog-Task: <this task id>\n` +
|
|
64
|
+
`2. Attach the PR while in_progress — never at done:\n` +
|
|
65
|
+
` backlog_update({ taskId, addArtifact: "<pr-url>", addArtifactKind: "pr" })\n` +
|
|
66
|
+
` This spawns the human review child and parks this task on it.\n` +
|
|
67
|
+
`3. Never call backlog_done while a HITL child is open — the human merge\n` +
|
|
68
|
+
` resolves the child; merge-detect then closes this task.`,
|
|
60
69
|
];
|
|
61
70
|
return parts.filter(Boolean).join("\n\n");
|
|
62
71
|
}
|
package/src/webhook/server.js
CHANGED
|
@@ -10,6 +10,29 @@ import { eventsSince, lastEventId, toEventMessage, HISTORY_LIMIT } from "./envel
|
|
|
10
10
|
|
|
11
11
|
export const DEFAULT_DISPATCH_PORT = 38293;
|
|
12
12
|
|
|
13
|
+
const USAGE_NUM = (v) => (typeof v === "number" && Number.isFinite(v) ? v : null);
|
|
14
|
+
|
|
15
|
+
// Canonical usage shape from the harness. Accepts snake_case aliases so a
|
|
16
|
+
// plugin need not match our field names exactly. Returns null when nothing
|
|
17
|
+
// numeric/identifiable is present, so the endpoint rejects empty payloads.
|
|
18
|
+
function normalizeUsage(raw) {
|
|
19
|
+
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
20
|
+
const model = typeof raw.model === "string" && raw.model.trim() !== "" ? raw.model.trim() : null;
|
|
21
|
+
const out = {
|
|
22
|
+
model,
|
|
23
|
+
tokensIn: USAGE_NUM(raw.tokensIn ?? raw.tokens_in ?? raw.inputTokens ?? raw.input_tokens),
|
|
24
|
+
tokensOut: USAGE_NUM(raw.tokensOut ?? raw.tokens_out ?? raw.outputTokens ?? raw.output_tokens),
|
|
25
|
+
cacheRead: USAGE_NUM(raw.cacheRead ?? raw.cache_read ?? raw.cachedTokens ?? raw.cached_tokens),
|
|
26
|
+
costUsd: USAGE_NUM(raw.costUsd ?? raw.cost_usd ?? raw.cost),
|
|
27
|
+
toolCalls: USAGE_NUM(raw.toolCalls ?? raw.tool_calls),
|
|
28
|
+
};
|
|
29
|
+
if (out.model === null && out.tokensIn === null && out.tokensOut === null &&
|
|
30
|
+
out.cacheRead === null && out.costUsd === null && out.toolCalls === null) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
13
36
|
export function startDispatchServer({ store, port = null, host = "127.0.0.1", intervalMs = null, logger } = {}) {
|
|
14
37
|
const cfg = store.config ?? {};
|
|
15
38
|
const listenPort = port ?? cfg.dispatchPort ?? DEFAULT_DISPATCH_PORT;
|
|
@@ -172,6 +195,40 @@ export function startDispatchServer({ store, port = null, host = "127.0.0.1", in
|
|
|
172
195
|
}
|
|
173
196
|
return;
|
|
174
197
|
}
|
|
198
|
+
if (req.method === "POST" && url.pathname === "/usage") {
|
|
199
|
+
if (unauthorized(req)) { json(res, 401, { error: "unauthorized" }); return; }
|
|
200
|
+
const body = await readJson(req, res);
|
|
201
|
+
if (body === undefined) return;
|
|
202
|
+
try {
|
|
203
|
+
const sessionKey = typeof body.sessionKey === "string" ? body.sessionKey.trim() : "";
|
|
204
|
+
if (!sessionKey) throw new Error("usage requires sessionKey");
|
|
205
|
+
const usage = normalizeUsage(body.usage);
|
|
206
|
+
if (!usage) throw new Error("usage requires a non-empty usage object");
|
|
207
|
+
const sess = store.db.prepare(
|
|
208
|
+
"SELECT id, item_id, harness, agent_id, external_session_id FROM work_sessions WHERE session_key=?"
|
|
209
|
+
).get(sessionKey);
|
|
210
|
+
if (!sess) { json(res, 404, { error: `no session: ${sessionKey}` }); return; }
|
|
211
|
+
const live = store.db.prepare(
|
|
212
|
+
"SELECT id FROM attempts WHERE item_id=? AND ended_at IS NULL ORDER BY attempt_no DESC LIMIT 1"
|
|
213
|
+
).get(sess.item_id);
|
|
214
|
+
const turnRef = typeof body.turnRef === "string" && body.turnRef.trim() !== "" ? body.turnRef.trim() : null;
|
|
215
|
+
const id = store.usage.record({
|
|
216
|
+
harness: sess.harness,
|
|
217
|
+
agent: sess.agent_id,
|
|
218
|
+
session: sess.external_session_id,
|
|
219
|
+
itemId: sess.item_id,
|
|
220
|
+
sessionId: sess.id,
|
|
221
|
+
attemptId: live?.id ?? null,
|
|
222
|
+
usage,
|
|
223
|
+
source: "api",
|
|
224
|
+
turnRef,
|
|
225
|
+
});
|
|
226
|
+
json(res, 201, { ok: true, id, itemId: sess.item_id, duplicate: id === null });
|
|
227
|
+
} catch (err) {
|
|
228
|
+
json(res, 400, { error: String(err?.message ?? err) });
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
175
232
|
json(res, 404, { error: "not found" });
|
|
176
233
|
});
|
|
177
234
|
|