@igorjpimenta/brayer 0.0.0-stage → 2026.10.29-beta.1
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_initial.sql +133 -0
- package/.migrations/0001_awesome_ben_parker.sql +8 -0
- package/.migrations/meta/0000_snapshot.json +931 -0
- package/.migrations/meta/0001_snapshot.json +983 -0
- package/.migrations/meta/_journal.json +20 -0
- package/LICENSE +21 -0
- package/README.md +261 -2
- package/bin/brayer.js +7 -0
- package/package.json +43 -4
- package/plugins/openclaw/brayer/index.js +119 -0
- package/plugins/openclaw/brayer/meta.js +23 -0
- package/plugins/openclaw/brayer/openclaw.plugin.json +14 -0
- package/plugins/openclaw/brayer/package.json +37 -0
- package/plugins/openclaw/brayer/session.js +30 -0
- package/plugins/openclaw/brayer/webhook.js +125 -0
- package/plugins/opencode/brayer/hooks.js +17 -0
- package/plugins/opencode/brayer/identity.js +11 -0
- package/plugins/opencode/brayer/index.js +5 -0
- package/plugins/opencode/brayer/package.json +25 -0
- package/plugins/shared/contract.js +193 -0
- package/plugins/wake/brayer/bin/brayer-wake.js +7 -0
- package/plugins/wake/brayer/index.js +125 -0
- package/plugins/wake/brayer/package.json +29 -0
- package/src/claim-webhook.js +66 -0
- package/src/cli.js +164 -0
- package/src/config.js +63 -0
- package/src/db.js +24 -0
- package/src/events-ws.js +121 -0
- package/src/evidence.js +42 -0
- package/src/identity.js +32 -0
- package/src/ids.js +17 -0
- package/src/mcp.js +114 -0
- package/src/schema.js +168 -0
- package/src/store/attempts.js +104 -0
- package/src/store/checkpoints.js +76 -0
- package/src/store/claims.js +139 -0
- package/src/store/events.js +24 -0
- package/src/store/evidence.js +67 -0
- package/src/store/orders.js +70 -0
- package/src/store/resume.js +38 -0
- package/src/store/rows.js +118 -0
- package/src/store/util.js +3 -0
- package/src/store/verdicts.js +64 -0
- package/src/store/webhooks.js +56 -0
- package/src/store.js +53 -0
- package/src/tools/attempts.js +75 -0
- package/src/tools/handoff.js +120 -0
- package/src/tools/index.js +28 -0
- package/src/tools/orders.js +146 -0
- package/src/tools/shared.js +7 -0
- package/src/tools/verdicts.js +46 -0
- package/src/version.js +5 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { now } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export function createResumeApi({ db, insertEvent, orderApi, checkpointApi }) {
|
|
4
|
+
const resumeApi = {
|
|
5
|
+
resume({ workOrderId, actor = "operator", reason = "resumed" } = {}) {
|
|
6
|
+
if (!workOrderId) throw new Error("workOrderId is required");
|
|
7
|
+
const t = now();
|
|
8
|
+
db.exec("BEGIN IMMEDIATE");
|
|
9
|
+
try {
|
|
10
|
+
const order = orderApi.get(workOrderId);
|
|
11
|
+
if (!order) throw new Error(`order not found: ${workOrderId}`);
|
|
12
|
+
if (order.status !== "rejected" && order.status !== "blocked") {
|
|
13
|
+
throw new Error(`resume requires rejected or blocked, got ${order.status}`);
|
|
14
|
+
}
|
|
15
|
+
db.prepare(`
|
|
16
|
+
UPDATE work_orders
|
|
17
|
+
SET status='ready', assignee=NULL, claim_expires_at=NULL, lease_seconds=NULL,
|
|
18
|
+
last_heartbeat=NULL, updated_at=?
|
|
19
|
+
WHERE id=?
|
|
20
|
+
`).run(t, workOrderId);
|
|
21
|
+
insertEvent({
|
|
22
|
+
workOrderId, fromStatus: order.status, toStatus: "ready", kind: "order.resumed",
|
|
23
|
+
actor, note: reason, at: t,
|
|
24
|
+
});
|
|
25
|
+
db.exec("COMMIT");
|
|
26
|
+
return {
|
|
27
|
+
order: orderApi.get(workOrderId),
|
|
28
|
+
checkpoint: checkpointApi.latest({ workOrderId }),
|
|
29
|
+
};
|
|
30
|
+
} catch (err) {
|
|
31
|
+
db.exec("ROLLBACK");
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return resumeApi;
|
|
38
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { jsonIn } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export function rowOrder(r) {
|
|
4
|
+
if (!r) return null;
|
|
5
|
+
return {
|
|
6
|
+
id: r.id,
|
|
7
|
+
title: r.title,
|
|
8
|
+
goal: r.goal,
|
|
9
|
+
constraints: r.constraints,
|
|
10
|
+
acceptance: r.acceptance,
|
|
11
|
+
status: r.status,
|
|
12
|
+
priority: r.priority,
|
|
13
|
+
workspace: r.workspace,
|
|
14
|
+
repo: r.repo,
|
|
15
|
+
branch: r.branch,
|
|
16
|
+
budgetTokens: r.budget_tokens,
|
|
17
|
+
budgetUsd: r.budget_usd,
|
|
18
|
+
modelPolicy: jsonIn(r.model_policy),
|
|
19
|
+
approvalMode: r.approval_mode,
|
|
20
|
+
parentId: r.parent_id,
|
|
21
|
+
assignee: r.assignee,
|
|
22
|
+
claimExpiresAt: r.claim_expires_at,
|
|
23
|
+
leaseSeconds: r.lease_seconds,
|
|
24
|
+
lastHeartbeat: r.last_heartbeat,
|
|
25
|
+
attemptCount: r.attempt_count,
|
|
26
|
+
createdAt: r.created_at,
|
|
27
|
+
updatedAt: r.updated_at,
|
|
28
|
+
closedAt: r.closed_at,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function rowAttempt(r) {
|
|
33
|
+
if (!r) return null;
|
|
34
|
+
return {
|
|
35
|
+
id: r.id,
|
|
36
|
+
workOrderId: r.work_order_id,
|
|
37
|
+
harness: r.harness,
|
|
38
|
+
agent: r.agent,
|
|
39
|
+
sessionRef: r.session_ref,
|
|
40
|
+
model: r.model,
|
|
41
|
+
status: r.status,
|
|
42
|
+
startedAt: r.started_at,
|
|
43
|
+
endedAt: r.ended_at,
|
|
44
|
+
tokensIn: r.tokens_in,
|
|
45
|
+
tokensOut: r.tokens_out,
|
|
46
|
+
cacheRead: r.cache_read,
|
|
47
|
+
costUsd: r.cost_usd,
|
|
48
|
+
toolCalls: r.tool_calls,
|
|
49
|
+
error: r.error,
|
|
50
|
+
checkpointId: r.checkpoint_id,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function rowCheckpoint(r) {
|
|
55
|
+
if (!r) return null;
|
|
56
|
+
return {
|
|
57
|
+
id: r.id,
|
|
58
|
+
workOrderId: r.work_order_id,
|
|
59
|
+
attemptId: r.attempt_id,
|
|
60
|
+
kind: r.kind,
|
|
61
|
+
goal: r.goal,
|
|
62
|
+
state: jsonIn(r.state_json),
|
|
63
|
+
summary: r.summary,
|
|
64
|
+
confidence: r.confidence,
|
|
65
|
+
resumeRecipe: r.resume_recipe,
|
|
66
|
+
treeHash: r.tree_hash,
|
|
67
|
+
createdAt: r.created_at,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function rowEvidence(r) {
|
|
72
|
+
if (!r) return null;
|
|
73
|
+
return {
|
|
74
|
+
id: r.id,
|
|
75
|
+
workOrderId: r.work_order_id,
|
|
76
|
+
attemptId: r.attempt_id,
|
|
77
|
+
kind: r.kind,
|
|
78
|
+
uri: r.uri,
|
|
79
|
+
sha256: r.sha256,
|
|
80
|
+
summary: r.summary,
|
|
81
|
+
command: r.command,
|
|
82
|
+
exitCode: r.exit_code,
|
|
83
|
+
status: r.status,
|
|
84
|
+
verifier: r.verifier,
|
|
85
|
+
createdAt: r.created_at,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function rowVerdict(r) {
|
|
90
|
+
if (!r) return null;
|
|
91
|
+
return {
|
|
92
|
+
id: r.id,
|
|
93
|
+
workOrderId: r.work_order_id,
|
|
94
|
+
attemptId: r.attempt_id,
|
|
95
|
+
decision: r.decision,
|
|
96
|
+
reason: r.reason,
|
|
97
|
+
actor: r.actor,
|
|
98
|
+
policy: r.policy,
|
|
99
|
+
evidenceIds: jsonIn(r.evidence_ids),
|
|
100
|
+
createdAt: r.created_at,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function rowEvent(r) {
|
|
105
|
+
if (!r) return null;
|
|
106
|
+
return {
|
|
107
|
+
id: r.id,
|
|
108
|
+
workOrderId: r.work_order_id,
|
|
109
|
+
attemptId: r.attempt_id,
|
|
110
|
+
fromStatus: r.from_status,
|
|
111
|
+
toStatus: r.to_status,
|
|
112
|
+
kind: r.kind,
|
|
113
|
+
actor: r.actor,
|
|
114
|
+
note: r.note,
|
|
115
|
+
sessionRef: r.session_ref,
|
|
116
|
+
at: r.at,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { newVerdictId } from "../ids.js";
|
|
2
|
+
import { jsonOut, now } from "./util.js";
|
|
3
|
+
import { rowVerdict } from "./rows.js";
|
|
4
|
+
|
|
5
|
+
export function createVerdictApi({ db, insertEvent, orderApi, evidenceApi }) {
|
|
6
|
+
const verdictApi = {
|
|
7
|
+
list({ workOrderId } = {}) {
|
|
8
|
+
const sql = workOrderId
|
|
9
|
+
? "SELECT * FROM verdicts WHERE work_order_id=? ORDER BY created_at ASC, rowid ASC"
|
|
10
|
+
: "SELECT * FROM verdicts ORDER BY created_at ASC, rowid ASC LIMIT 200";
|
|
11
|
+
return db.prepare(sql).all(...(workOrderId ? [workOrderId] : [])).map(rowVerdict);
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
issue({
|
|
15
|
+
workOrderId,
|
|
16
|
+
attemptId = null,
|
|
17
|
+
decision,
|
|
18
|
+
reason = null,
|
|
19
|
+
actor = "system",
|
|
20
|
+
policy = null,
|
|
21
|
+
evidenceIds = null,
|
|
22
|
+
} = {}) {
|
|
23
|
+
if (!workOrderId) throw new Error("workOrderId is required");
|
|
24
|
+
if (!["approve", "reject", "escalate", "rollback"].includes(decision)) throw new Error(`invalid verdict decision: ${decision}`);
|
|
25
|
+
const t = now();
|
|
26
|
+
db.exec("BEGIN IMMEDIATE");
|
|
27
|
+
try {
|
|
28
|
+
const order = orderApi.get(workOrderId);
|
|
29
|
+
if (!order) throw new Error(`order not found: ${workOrderId}`);
|
|
30
|
+
if (order.status !== "review" && order.status !== "blocked") {
|
|
31
|
+
throw new Error(`verdict requires review or blocked, got ${order.status}`);
|
|
32
|
+
}
|
|
33
|
+
if (decision === "approve" && evidenceApi.list({ workOrderId }).length === 0) {
|
|
34
|
+
throw new Error("approve requires at least one evidence row");
|
|
35
|
+
}
|
|
36
|
+
const resolvedAttemptId = attemptId ?? db.prepare(
|
|
37
|
+
"SELECT id FROM attempts WHERE work_order_id=? ORDER BY started_at DESC LIMIT 1"
|
|
38
|
+
).get(workOrderId)?.id ?? null;
|
|
39
|
+
|
|
40
|
+
const nextStatus = decision === "approve" ? "approved"
|
|
41
|
+
: decision === "reject" ? "rejected"
|
|
42
|
+
: decision === "escalate" ? "blocked"
|
|
43
|
+
: "ready";
|
|
44
|
+
const id = newVerdictId();
|
|
45
|
+
db.prepare(`
|
|
46
|
+
INSERT INTO verdicts(id, work_order_id, attempt_id, decision, reason, actor, policy, evidence_ids, created_at)
|
|
47
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
48
|
+
`).run(id, workOrderId, resolvedAttemptId, decision, reason, actor, policy, jsonOut(evidenceIds), t);
|
|
49
|
+
db.prepare("UPDATE work_orders SET status=?, updated_at=? WHERE id=?").run(nextStatus, t, workOrderId);
|
|
50
|
+
insertEvent({
|
|
51
|
+
workOrderId, attemptId: resolvedAttemptId, fromStatus: order.status, toStatus: nextStatus, kind: "verdict.issued",
|
|
52
|
+
actor, note: reason ?? decision, at: t,
|
|
53
|
+
});
|
|
54
|
+
db.exec("COMMIT");
|
|
55
|
+
return { verdict: verdictApi.list({ workOrderId }).at(-1), order: orderApi.get(workOrderId) };
|
|
56
|
+
} catch (err) {
|
|
57
|
+
db.exec("ROLLBACK");
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return verdictApi;
|
|
64
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { now } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export function createWebhookApi({ db }) {
|
|
4
|
+
const webhookApi = {
|
|
5
|
+
upsert({ harness, url, bearer = null, timeoutMs = null }) {
|
|
6
|
+
const h = typeof harness === "string" ? harness.trim() : "";
|
|
7
|
+
if (!h) throw new Error("webhook target requires a harness");
|
|
8
|
+
let parsed;
|
|
9
|
+
try {
|
|
10
|
+
parsed = new URL(url);
|
|
11
|
+
} catch {
|
|
12
|
+
throw new Error(`invalid webhook url: ${JSON.stringify(url)}`);
|
|
13
|
+
}
|
|
14
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
15
|
+
throw new Error(`webhook url must be http(s): ${JSON.stringify(url)}`);
|
|
16
|
+
}
|
|
17
|
+
if (timeoutMs != null && (!Number.isInteger(timeoutMs) || timeoutMs <= 0)) {
|
|
18
|
+
throw new Error(`webhook timeoutMs must be a positive integer, got ${JSON.stringify(timeoutMs)}`);
|
|
19
|
+
}
|
|
20
|
+
const t = now();
|
|
21
|
+
db.prepare(`
|
|
22
|
+
INSERT INTO webhook_targets(harness, url, bearer, timeout_ms, created_at, updated_at)
|
|
23
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
24
|
+
ON CONFLICT(harness) DO UPDATE SET url=excluded.url, bearer=excluded.bearer,
|
|
25
|
+
timeout_ms=excluded.timeout_ms, updated_at=excluded.updated_at
|
|
26
|
+
`).run(h, parsed.toString(), bearer ?? null, timeoutMs ?? null, t, t);
|
|
27
|
+
return webhookApi.get(h);
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
get(harness) {
|
|
31
|
+
const row = db.prepare("SELECT * FROM webhook_targets WHERE harness=?").get(harness);
|
|
32
|
+
if (!row) return null;
|
|
33
|
+
return {
|
|
34
|
+
harness: row.harness,
|
|
35
|
+
url: row.url,
|
|
36
|
+
bearer: row.bearer,
|
|
37
|
+
timeoutMs: row.timeout_ms,
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
list() {
|
|
42
|
+
return db.prepare("SELECT * FROM webhook_targets ORDER BY harness ASC").all().map((row) => ({
|
|
43
|
+
harness: row.harness,
|
|
44
|
+
url: row.url,
|
|
45
|
+
bearer: row.bearer,
|
|
46
|
+
timeoutMs: row.timeout_ms,
|
|
47
|
+
}));
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
remove({ harness }) {
|
|
51
|
+
return db.prepare("DELETE FROM webhook_targets WHERE harness=?").run(harness).changes > 0;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return webhookApi;
|
|
56
|
+
}
|
package/src/store.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { openDatabase } from "./db.js";
|
|
2
|
+
import { resolveConfig } from "./config.js";
|
|
3
|
+
import { createEventApi } from "./store/events.js";
|
|
4
|
+
import { createOrderApi } from "./store/orders.js";
|
|
5
|
+
import { createClaimApi } from "./store/claims.js";
|
|
6
|
+
import { createWebhookApi } from "./store/webhooks.js";
|
|
7
|
+
import { claimPayload, notifyClaim } from "./claim-webhook.js";
|
|
8
|
+
import { createAttemptApi } from "./store/attempts.js";
|
|
9
|
+
import { createCheckpointApi } from "./store/checkpoints.js";
|
|
10
|
+
import { createEvidenceApi } from "./store/evidence.js";
|
|
11
|
+
import { createVerdictApi } from "./store/verdicts.js";
|
|
12
|
+
import { createResumeApi } from "./store/resume.js";
|
|
13
|
+
|
|
14
|
+
export function createStore({ dbPath, logger, config } = {}) {
|
|
15
|
+
const cfg = resolveConfig({ explicit: config ?? {} });
|
|
16
|
+
const finalPath = dbPath || cfg.dbPath;
|
|
17
|
+
const { sqlite: db } = openDatabase({ dbPath: finalPath, logger });
|
|
18
|
+
|
|
19
|
+
const { insertEvent, eventApi } = createEventApi({ db });
|
|
20
|
+
const order = createOrderApi({ db, insertEvent });
|
|
21
|
+
const webhook = createWebhookApi({ db });
|
|
22
|
+
const claim = createClaimApi({
|
|
23
|
+
db,
|
|
24
|
+
cfg,
|
|
25
|
+
insertEvent,
|
|
26
|
+
orderApi: order,
|
|
27
|
+
notifyClaim: (payload) => {
|
|
28
|
+
const body = claimPayload(payload);
|
|
29
|
+
return notifyClaim(cfg, body, { logger, dbEntry: webhook.get(body.session.harness) });
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const attempt = createAttemptApi({ db, insertEvent, orderApi: order });
|
|
33
|
+
const checkpoint = createCheckpointApi({ db, insertEvent, attemptApi: attempt });
|
|
34
|
+
const evidence = createEvidenceApi({ db, insertEvent, attemptApi: attempt });
|
|
35
|
+
const verdict = createVerdictApi({ db, insertEvent, orderApi: order, evidenceApi: evidence });
|
|
36
|
+
const resume = createResumeApi({ db, insertEvent, orderApi: order, checkpointApi: checkpoint });
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
db,
|
|
40
|
+
config: cfg,
|
|
41
|
+
order,
|
|
42
|
+
claim,
|
|
43
|
+
attempt,
|
|
44
|
+
checkpoint,
|
|
45
|
+
evidence,
|
|
46
|
+
verdict,
|
|
47
|
+
resume,
|
|
48
|
+
webhook,
|
|
49
|
+
event: eventApi,
|
|
50
|
+
insertEvent,
|
|
51
|
+
close() { db.close(); },
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
import { identityKey } from "../identity.js";
|
|
3
|
+
import { identityShape } from "./shared.js";
|
|
4
|
+
|
|
5
|
+
export const attemptDefs = [
|
|
6
|
+
{
|
|
7
|
+
name: "brayer_attempt_start",
|
|
8
|
+
description:
|
|
9
|
+
"Open an attempt on an order this agent holds. One live attempt per order; the order moves claimed → running.",
|
|
10
|
+
shape: {
|
|
11
|
+
...identityShape,
|
|
12
|
+
orderId: z.string(),
|
|
13
|
+
model: z.optional(z.string()),
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: "brayer_attempt_finish",
|
|
18
|
+
description:
|
|
19
|
+
"Close a running attempt. finished moves the order to review; failed returns it to the ready queue.",
|
|
20
|
+
shape: {
|
|
21
|
+
...identityShape,
|
|
22
|
+
attemptId: z.string(),
|
|
23
|
+
status: z.enum(["finished", "failed"]),
|
|
24
|
+
summary: z.optional(z.string()),
|
|
25
|
+
tokensIn: z.optional(z.int()),
|
|
26
|
+
tokensOut: z.optional(z.int()),
|
|
27
|
+
cacheRead: z.optional(z.int()),
|
|
28
|
+
costUsd: z.optional(z.number()),
|
|
29
|
+
toolCalls: z.optional(z.int()),
|
|
30
|
+
error: z.optional(z.string()),
|
|
31
|
+
checkpointId: z.optional(z.string()),
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "brayer_attempt_get",
|
|
36
|
+
description: "Read one attempt. Read-only.",
|
|
37
|
+
shape: {
|
|
38
|
+
...identityShape,
|
|
39
|
+
attemptId: z.string(),
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
export const attemptHandlers = {
|
|
45
|
+
brayer_attempt_start({ store, args, identity }) {
|
|
46
|
+
return store.attempt.start({
|
|
47
|
+
workOrderId: args.orderId,
|
|
48
|
+
harness: identity.harness,
|
|
49
|
+
agent: identityKey(identity),
|
|
50
|
+
sessionRef: identity.session,
|
|
51
|
+
model: args.model ?? null,
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
brayer_attempt_finish({ store, args }) {
|
|
56
|
+
return store.attempt.finish({
|
|
57
|
+
attemptId: args.attemptId,
|
|
58
|
+
status: args.status,
|
|
59
|
+
summary: args.summary ?? null,
|
|
60
|
+
tokensIn: args.tokensIn ?? 0,
|
|
61
|
+
tokensOut: args.tokensOut ?? 0,
|
|
62
|
+
cacheRead: args.cacheRead ?? 0,
|
|
63
|
+
costUsd: args.costUsd ?? 0,
|
|
64
|
+
toolCalls: args.toolCalls ?? 0,
|
|
65
|
+
error: args.error ?? null,
|
|
66
|
+
checkpointId: args.checkpointId ?? null,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
brayer_attempt_get({ store, args }) {
|
|
71
|
+
const attempt = store.attempt.get(args.attemptId);
|
|
72
|
+
if (!attempt) throw new Error(`attempt not found: ${args.attemptId}`);
|
|
73
|
+
return { attempt };
|
|
74
|
+
},
|
|
75
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
import { storeFile } from "../evidence.js";
|
|
3
|
+
import { identityKey } from "../identity.js";
|
|
4
|
+
import { identityShape } from "./shared.js";
|
|
5
|
+
|
|
6
|
+
export const handoffDefs = [
|
|
7
|
+
{
|
|
8
|
+
name: "brayer_checkpoint_write",
|
|
9
|
+
description:
|
|
10
|
+
"Write the structured handoff for a running attempt. The state is an object, not a transcript; treeHash pins the workspace the state describes.",
|
|
11
|
+
shape: {
|
|
12
|
+
...identityShape,
|
|
13
|
+
orderId: z.string(),
|
|
14
|
+
attemptId: z.string(),
|
|
15
|
+
kind: z.optional(z.enum(["auto", "manual", "pre_compact", "stop", "error"])),
|
|
16
|
+
goal: z.optional(z.string()),
|
|
17
|
+
state: z.unknown(),
|
|
18
|
+
summary: z.optional(z.string()),
|
|
19
|
+
confidence: z.optional(z.number()),
|
|
20
|
+
resumeRecipe: z.optional(z.string()),
|
|
21
|
+
treeHash: z.string(),
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "brayer_checkpoint_get",
|
|
26
|
+
description:
|
|
27
|
+
"Read a checkpoint by id, or the latest checkpoint for an order. Read-only.",
|
|
28
|
+
shape: {
|
|
29
|
+
...identityShape,
|
|
30
|
+
checkpointId: z.optional(z.string()),
|
|
31
|
+
orderId: z.optional(z.string()),
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "brayer_evidence_attach",
|
|
36
|
+
description:
|
|
37
|
+
"Attach typed evidence to a running attempt. Pass a local path and Brayer stores a content-addressed blob; or pass uri + sha256 for an existing blob.",
|
|
38
|
+
shape: {
|
|
39
|
+
...identityShape,
|
|
40
|
+
orderId: z.string(),
|
|
41
|
+
attemptId: z.string(),
|
|
42
|
+
kind: z.enum(["diff", "test", "lint", "screenshot", "artifact", "log"]),
|
|
43
|
+
path: z.optional(z.string()),
|
|
44
|
+
uri: z.optional(z.string()),
|
|
45
|
+
sha256: z.optional(z.string()),
|
|
46
|
+
summary: z.optional(z.string()),
|
|
47
|
+
command: z.optional(z.string()),
|
|
48
|
+
exitCode: z.optional(z.int()),
|
|
49
|
+
status: z.optional(z.string()),
|
|
50
|
+
verifier: z.optional(z.string()),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "brayer_evidence_list",
|
|
55
|
+
description: "List evidence for an order or an attempt. Read-only.",
|
|
56
|
+
shape: {
|
|
57
|
+
...identityShape,
|
|
58
|
+
orderId: z.optional(z.string()),
|
|
59
|
+
attemptId: z.optional(z.string()),
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
export const handoffHandlers = {
|
|
65
|
+
brayer_checkpoint_write({ store, args, identity }) {
|
|
66
|
+
const checkpoint = store.checkpoint.write({
|
|
67
|
+
workOrderId: args.orderId,
|
|
68
|
+
attemptId: args.attemptId,
|
|
69
|
+
kind: args.kind ?? "manual",
|
|
70
|
+
goal: args.goal ?? null,
|
|
71
|
+
state: args.state,
|
|
72
|
+
summary: args.summary ?? null,
|
|
73
|
+
confidence: args.confidence ?? null,
|
|
74
|
+
resumeRecipe: args.resumeRecipe ?? null,
|
|
75
|
+
treeHash: args.treeHash,
|
|
76
|
+
actor: identityKey(identity),
|
|
77
|
+
});
|
|
78
|
+
return { checkpoint };
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
brayer_checkpoint_get({ store, args }) {
|
|
82
|
+
if (args.checkpointId) {
|
|
83
|
+
const checkpoint = store.checkpoint.get(args.checkpointId);
|
|
84
|
+
if (!checkpoint) throw new Error(`checkpoint not found: ${args.checkpointId}`);
|
|
85
|
+
return { checkpoint };
|
|
86
|
+
}
|
|
87
|
+
if (!args.orderId) throw new Error("checkpointId or orderId is required");
|
|
88
|
+
return { checkpoint: store.checkpoint.latest({ workOrderId: args.orderId }) };
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
brayer_evidence_attach({ store, args, identity }) {
|
|
92
|
+
let uri = args.uri;
|
|
93
|
+
let sha256 = args.sha256;
|
|
94
|
+
if (args.path) {
|
|
95
|
+
const stored = storeFile({ root: store.config.blobsPath, filePath: args.path });
|
|
96
|
+
uri = stored.uri;
|
|
97
|
+
sha256 = stored.sha256;
|
|
98
|
+
}
|
|
99
|
+
if (!uri || !sha256) throw new Error("path or uri + sha256 is required");
|
|
100
|
+
const evidence = store.evidence.attach({
|
|
101
|
+
workOrderId: args.orderId,
|
|
102
|
+
attemptId: args.attemptId,
|
|
103
|
+
kind: args.kind,
|
|
104
|
+
uri,
|
|
105
|
+
sha256,
|
|
106
|
+
summary: args.summary ?? null,
|
|
107
|
+
command: args.command ?? null,
|
|
108
|
+
exitCode: args.exitCode ?? null,
|
|
109
|
+
status: args.status ?? null,
|
|
110
|
+
verifier: args.verifier ?? null,
|
|
111
|
+
actor: identityKey(identity),
|
|
112
|
+
});
|
|
113
|
+
return { evidence };
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
brayer_evidence_list({ store, args }) {
|
|
117
|
+
if (!args.orderId && !args.attemptId) throw new Error("orderId or attemptId is required");
|
|
118
|
+
return { evidence: store.evidence.list({ workOrderId: args.orderId, attemptId: args.attemptId }) };
|
|
119
|
+
},
|
|
120
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { attemptDefs, attemptHandlers } from "./attempts.js";
|
|
2
|
+
import { handoffDefs, handoffHandlers } from "./handoff.js";
|
|
3
|
+
import { orderDefs, orderHandlers } from "./orders.js";
|
|
4
|
+
import { verdictDefs, verdictHandlers } from "./verdicts.js";
|
|
5
|
+
|
|
6
|
+
const TOOL_DEFS = [
|
|
7
|
+
...orderDefs,
|
|
8
|
+
...attemptDefs,
|
|
9
|
+
...handoffDefs,
|
|
10
|
+
...verdictDefs,
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const HANDLERS = {
|
|
14
|
+
...orderHandlers,
|
|
15
|
+
...attemptHandlers,
|
|
16
|
+
...handoffHandlers,
|
|
17
|
+
...verdictHandlers,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function createToolHandlers(store) {
|
|
21
|
+
async function handle(name, args, identity) {
|
|
22
|
+
if (!Object.hasOwn(HANDLERS, name)) throw new Error(`unknown tool: ${name}`);
|
|
23
|
+
const handler = HANDLERS[name];
|
|
24
|
+
return handler({ store, args, identity });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return { TOOL_DEFS, handle };
|
|
28
|
+
}
|