@igorjpimenta/brayer 2026.11.7 → 2026.11.19

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/src/store.js CHANGED
@@ -4,12 +4,13 @@ import { createEventApi } from "./store/events.js";
4
4
  import { createOrderApi } from "./store/orders.js";
5
5
  import { createClaimApi } from "./store/claims.js";
6
6
  import { createWebhookApi } from "./store/webhooks.js";
7
- import { claimPayload, notifyClaim } from "./claim-webhook.js";
7
+ import { claimPayload, notifyClaim, webhookConfig } from "./claim-webhook.js";
8
8
  import { createAttemptApi } from "./store/attempts.js";
9
9
  import { createCheckpointApi } from "./store/checkpoints.js";
10
10
  import { createEvidenceApi } from "./store/evidence.js";
11
11
  import { createVerdictApi } from "./store/verdicts.js";
12
12
  import { createResumeApi } from "./store/resume.js";
13
+ import { createAdoptionApi } from "./store/adoption.js";
13
14
 
14
15
  export function createStore({ dbPath, logger, config } = {}) {
15
16
  const cfg = resolveConfig({ explicit: config ?? {} });
@@ -25,17 +26,26 @@ export function createStore({ dbPath, logger, config } = {}) {
25
26
  insertEvent,
26
27
  orderApi: order,
27
28
  logger,
29
+ claimGate: (harness) => {
30
+ if (webhookConfig(cfg, harness, webhook.get(harness))) return;
31
+ throw new Error(
32
+ `brayer: harness "${harness}" has no claim wake route — a claimed order could never be delivered. ` +
33
+ `Register one: brayer webhook set --harness ${harness} --url <wake-url> [--bearer], ` +
34
+ `or set BRAYER_CLAIM_WEBHOOKS / BRAYER_CLAIM_WEBHOOK_URL.`
35
+ );
36
+ },
28
37
  notifyClaim: (payload) => {
29
38
  const body = claimPayload(payload);
30
39
  return notifyClaim(cfg, body, { logger, dbEntry: webhook.get(body.session.harness) });
31
40
  },
32
41
  });
33
42
  claim.startup?.();
34
- const attempt = createAttemptApi({ db, insertEvent, orderApi: order, clearExpiry: claim.clearExpiry });
43
+ const attempt = createAttemptApi({ db, cfg, insertEvent, orderApi: order, clearExpiry: claim.clearExpiry });
35
44
  const checkpoint = createCheckpointApi({ db, insertEvent, attemptApi: attempt });
36
45
  const evidence = createEvidenceApi({ db, insertEvent, attemptApi: attempt });
37
46
  const verdict = createVerdictApi({ db, insertEvent, orderApi: order, evidenceApi: evidence });
38
47
  const resume = createResumeApi({ db, insertEvent, orderApi: order, checkpointApi: checkpoint });
48
+ const adoption = createAdoptionApi({ db });
39
49
 
40
50
  return {
41
51
  db,
@@ -47,6 +57,7 @@ export function createStore({ dbPath, logger, config } = {}) {
47
57
  evidence,
48
58
  verdict,
49
59
  resume,
60
+ adoption,
50
61
  webhook,
51
62
  event: eventApi,
52
63
  insertEvent,
@@ -28,20 +28,24 @@ export const orderDefs = [
28
28
  {
29
29
  name: "brayer_order_next",
30
30
  description:
31
- "Atomically claim the highest-priority ready order for this agent. A race loser gets an empty result, never a partial claim.",
31
+ "Atomically claim the highest-priority ready order for this agent. A race loser gets an empty result, never a partial claim. Set start_attempt to open the running attempt in the same call.",
32
32
  shape: {
33
33
  ...identityShape,
34
34
  leaseSeconds: z.optional(z.int()),
35
+ start_attempt: z.optional(z.boolean()),
36
+ model: z.optional(z.string()),
35
37
  },
36
38
  },
37
39
  {
38
40
  name: "brayer_order_claim",
39
41
  description:
40
- "Atomically claim one ready order by id. The order must be ready and unassigned, or assigned to this agent.",
42
+ "Atomically claim one ready order by id. The order must be ready and unassigned, or assigned to this agent. Set start_attempt to open the running attempt in the same call.",
41
43
  shape: {
42
44
  ...identityShape,
43
45
  orderId: z.string(),
44
46
  leaseSeconds: z.optional(z.int()),
47
+ start_attempt: z.optional(z.boolean()),
48
+ model: z.optional(z.string()),
45
49
  },
46
50
  },
47
51
  {
@@ -73,8 +77,39 @@ export const orderDefs = [
73
77
  orderId: z.string(),
74
78
  },
75
79
  },
80
+ {
81
+ name: "brayer_order_cancel",
82
+ description:
83
+ "Cancel an order that never started running: clears its claim and records order.cancelled. Refuses running, approved, rejected and done orders.",
84
+ shape: {
85
+ ...identityShape,
86
+ orderId: z.string(),
87
+ reason: z.optional(z.string()),
88
+ },
89
+ },
76
90
  ];
77
91
 
92
+ function startAttemptForClaim({ store, args, identity, claimed }) {
93
+ if (!args.start_attempt || !claimed) return null;
94
+ return store.attempt.start({
95
+ workOrderId: claimed.id,
96
+ harness: identity.harness,
97
+ agent: identityKey(identity),
98
+ sessionRef: identity.session,
99
+ model: args.model ?? null,
100
+ });
101
+ }
102
+
103
+ function withAttempt(result, started) {
104
+ if (!started) return result;
105
+ return {
106
+ ...result,
107
+ attempt: started.attempt,
108
+ order: started.order ?? result.claimed,
109
+ attemptReason: started.reason,
110
+ };
111
+ }
112
+
78
113
  export const orderHandlers = {
79
114
  brayer_order_create({ store, args, identity }) {
80
115
  const order = store.order.create({
@@ -98,20 +133,22 @@ export const orderHandlers = {
98
133
  },
99
134
 
100
135
  brayer_order_next({ store, args, identity }) {
101
- return store.claim.claimNext({
136
+ const result = store.claim.claimNext({
102
137
  agentId: identityKey(identity),
103
138
  sessionRef: identity.session,
104
139
  leaseSeconds: args.leaseSeconds,
105
140
  });
141
+ return withAttempt(result, startAttemptForClaim({ store, args, identity, claimed: result.claimed }));
106
142
  },
107
143
 
108
144
  brayer_order_claim({ store, args, identity }) {
109
- return store.claim.claimById({
145
+ const result = store.claim.claimById({
110
146
  id: args.orderId,
111
147
  agentId: identityKey(identity),
112
148
  sessionRef: identity.session,
113
149
  leaseSeconds: args.leaseSeconds,
114
150
  });
151
+ return withAttempt(result, startAttemptForClaim({ store, args, identity, claimed: result.claimed }));
115
152
  },
116
153
 
117
154
  brayer_order_heartbeat({ store, args, identity }) {
@@ -132,6 +169,14 @@ export const orderHandlers = {
132
169
  });
133
170
  },
134
171
 
172
+ brayer_order_cancel({ store, args, identity }) {
173
+ return store.order.cancel({
174
+ id: args.orderId,
175
+ actor: identityKey(identity),
176
+ reason: args.reason ?? "cancelled",
177
+ });
178
+ },
179
+
135
180
  brayer_order_get({ store, args }) {
136
181
  const order = store.order.get(args.orderId);
137
182
  if (!order) throw new Error(`order not found: ${args.orderId}`);