@tablation/crew 0.133.1 → 0.134.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +145 -14
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -9632,7 +9632,14 @@ function heldByLiveOtherShip(t, a) {
9632
9632
  const holder = ticketHolderShipId(t, a.contract);
9633
9633
  if (!holder || holder === a.myShipId) return false;
9634
9634
  const owner = a.ships.find((s) => s.id === holder);
9635
- return shipIsAlive(owner, a.now ?? Date.now());
9635
+ const now = a.now ?? Date.now();
9636
+ return shipIsAlive(owner, now) || shipEngagedOn(owner, t, now);
9637
+ }
9638
+ var ENGAGED_TRUST_MS = 6 * 60 * 60 * 1e3;
9639
+ function shipEngagedOn(ship, t, now) {
9640
+ if (!ship?.engaged || ship.engaged_ticket_id !== t.id) return false;
9641
+ const since2 = ship.engaged_since ? Date.parse(ship.engaged_since) : NaN;
9642
+ return Number.isFinite(since2) && now - since2 <= ENGAGED_TRUST_MS;
9636
9643
  }
9637
9644
  function shipName(ships, id) {
9638
9645
  if (!id) return "an unknown ship";
@@ -10110,6 +10117,15 @@ async function enforceHandoff(i) {
10110
10117
  return { kind: "failed", why };
10111
10118
  }
10112
10119
  }
10120
+ function lostTicketReason(t, contract, myShipId) {
10121
+ if (closedStatuses(contract).includes(t.status)) return `the ticket is ${t.status}`;
10122
+ const holder = ticketHolderShipId(t, contract);
10123
+ const st = contract.statuses;
10124
+ if (holder && myShipId && holder !== myShipId && [st.building, st.approved].includes(t.status)) {
10125
+ return "another ship holds the ticket now";
10126
+ }
10127
+ return null;
10128
+ }
10113
10129
  async function guardRunHandoff(route2, ship, tracker, role, workingId, log, since2) {
10114
10130
  const replayed = await replayRoutePending(route2, ship, tracker, log, role);
10115
10131
  if (!workingId || role !== "dev" && role !== "design" && role !== "qa") return null;
@@ -10122,6 +10138,11 @@ async function guardRunHandoff(route2, ship, tracker, role, workingId, log, sinc
10122
10138
  if (!resolved) return null;
10123
10139
  const { target, cfg } = resolved;
10124
10140
  checkoutDir = target.dir;
10141
+ const lost = lostTicketReason(t, tracker.contract, (await tracker.claimAffinity())?.shipId);
10142
+ if (lost && role !== "qa") {
10143
+ log.warn(`${t.issue_id}: run ended without a push \u2014 ${lost}`, { ticket: t.issue_id, step: "handoff" });
10144
+ return { kind: "unverified", why: lost };
10145
+ }
10125
10146
  if (role === "qa") {
10126
10147
  await retireRunWorktree(tracker, target.dir, cfg, t, role, seatId, ship.name, log);
10127
10148
  return null;
@@ -10462,6 +10483,69 @@ async function post(target, path, body) {
10462
10483
  if (!res.ok) throw new AgentLogHttpError(`${path}: ${res.status} ${res.statusText}`, res.status);
10463
10484
  return await res.json();
10464
10485
  }
10486
+ var RUNNING_OUTCOME = "running";
10487
+ async function startAgentRunRow(target, s) {
10488
+ try {
10489
+ const entry = await post(target, "/agents/log", {
10490
+ agentId: target.agentId,
10491
+ ticketReference: s.ticketReference,
10492
+ outcome: RUNNING_OUTCOME,
10493
+ startedAt: s.startedAt,
10494
+ source: "client",
10495
+ client: "crew",
10496
+ model: s.model,
10497
+ promptVersion: s.promptVersion,
10498
+ promptSha: s.promptSha,
10499
+ runId: s.runId,
10500
+ harness: s.harness,
10501
+ provider: s.provider
10502
+ });
10503
+ return entry.id;
10504
+ } catch {
10505
+ return void 0;
10506
+ }
10507
+ }
10508
+ function finishedRowBody(run4, workItem) {
10509
+ const values = {
10510
+ ticket_reference: run4.ticketReference,
10511
+ outcome: run4.outcome,
10512
+ finished_at: run4.finishedAt,
10513
+ model: run4.model,
10514
+ input_tokens: run4.tokensIn,
10515
+ output_tokens: run4.tokensOut,
10516
+ cache_read_tokens: run4.cacheReadTokens,
10517
+ cache_write_tokens: run4.cacheWriteTokens,
10518
+ cost_usd: run4.costUsd,
10519
+ num_turns: run4.numTurns,
10520
+ prefix_tokens: run4.prefixTokens,
10521
+ prompt_version: run4.promptVersion,
10522
+ prompt_sha: run4.promptSha,
10523
+ harness: run4.harness,
10524
+ provider: run4.provider,
10525
+ work_item_table: workItem.workItemTable,
10526
+ work_item_record_id: workItem.workItemRecordId
10527
+ };
10528
+ return Object.fromEntries(Object.entries(values).filter(([, v]) => v !== void 0));
10529
+ }
10530
+ async function updateStartedRow(target, rowId, body) {
10531
+ try {
10532
+ const model = await request(
10533
+ target,
10534
+ "GET",
10535
+ `/data-models/agent_log?workspaceId=${encodeURIComponent(target.workspaceId)}`
10536
+ );
10537
+ const have = await columnsOf(target, model.id);
10538
+ const values = Object.fromEntries(Object.entries(body).filter(([column]) => have.has(column)));
10539
+ await request(target, "PATCH", `/data-models/${model.id}/records/${rowId}`, values);
10540
+ return true;
10541
+ } catch (e) {
10542
+ if (!(e instanceof AgentLogHttpError)) throw new AgentLogUnreachableError(e.message);
10543
+ if (e.status === 404) return false;
10544
+ if (e.status >= 500) throw new AgentLogUnreachableError(e.message);
10545
+ e.row = true;
10546
+ throw e;
10547
+ }
10548
+ }
10465
10549
  async function reportAgentRun(target, run4) {
10466
10550
  let ticketRecordId;
10467
10551
  if (target.tickets && run4.ticketReference) {
@@ -10492,20 +10576,23 @@ async function reportAgentRun(target, run4) {
10492
10576
  const { runId, ...legacyBody } = { ...body, runId: run4.runId };
10493
10577
  const workItem = target.tickets && ticketRecordId ? { workItemTable: target.tickets.issuesModelId, workItemRecordId: ticketRecordId } : {};
10494
10578
  let entry;
10495
- try {
10496
- entry = await post(target, "/agents/log", { ...body, ...runId ? { runId } : {}, ...workItem, harness: run4.harness, provider: run4.provider });
10497
- } catch (e) {
10498
- if (!(e instanceof AgentLogHttpError)) throw new AgentLogUnreachableError(e.message);
10499
- if (e.status >= 500) throw new AgentLogUnreachableError(e.message);
10500
- if (e.status !== 400 || !run4.harness && !run4.provider && !ticketRecordId && !runId) {
10501
- e.row = true;
10502
- throw e;
10503
- }
10579
+ if (run4.rowId && await updateStartedRow(target, run4.rowId, finishedRowBody(run4, workItem))) entry = { id: run4.rowId };
10580
+ if (!entry) {
10504
10581
  try {
10505
- entry = await post(target, "/agents/log", legacyBody);
10506
- } catch (e2) {
10507
- if (e2 instanceof AgentLogHttpError && e2.status < 500) e2.row = true;
10508
- throw e2;
10582
+ entry = await post(target, "/agents/log", { ...body, ...runId ? { runId } : {}, ...workItem, harness: run4.harness, provider: run4.provider });
10583
+ } catch (e) {
10584
+ if (!(e instanceof AgentLogHttpError)) throw new AgentLogUnreachableError(e.message);
10585
+ if (e.status >= 500) throw new AgentLogUnreachableError(e.message);
10586
+ if (e.status !== 400 || !run4.harness && !run4.provider && !ticketRecordId && !runId) {
10587
+ e.row = true;
10588
+ throw e;
10589
+ }
10590
+ try {
10591
+ entry = await post(target, "/agents/log", legacyBody);
10592
+ } catch (e2) {
10593
+ if (e2 instanceof AgentLogHttpError && e2.status < 500) e2.row = true;
10594
+ throw e2;
10595
+ }
10509
10596
  }
10510
10597
  }
10511
10598
  for (const cycle of run4.cycles) {
@@ -12965,6 +13052,7 @@ async function finishRun(id, obs, emit, startedAt, end, agentLogKey, hintMoved)
12965
13052
  model: id.model,
12966
13053
  // CREW-2980: under `auth: workspace` the endpoint's rows are the accounting; the run row
12967
13054
  // keeps turns only, and `reportAgentRun` sums those rows into the ticket counters.
13055
+ ...id.agentLogRowId ? { rowId: id.agentLogRowId } : {},
12968
13056
  ...id.workspaceRunId ? { runId: id.workspaceRunId } : {
12969
13057
  tokensIn: result?.inputTokens,
12970
13058
  tokensOut: result?.outputTokens,
@@ -13113,6 +13201,26 @@ function spawnAgent(plan, emit, hintMoved) {
13113
13201
  } catch {
13114
13202
  }
13115
13203
  }
13204
+ const startedRow = plan.workspaceRunId && plan.agentLog ? startAgentRunRow(plan.agentLog, {
13205
+ runId: plan.workspaceRunId,
13206
+ ticketReference: plan.ticket ?? void 0,
13207
+ startedAt,
13208
+ model: plan.model,
13209
+ harness: id.harness,
13210
+ provider: id.provider,
13211
+ promptVersion: plan.promptVersion,
13212
+ promptSha: plan.promptSha
13213
+ }).then((rowId) => {
13214
+ if (!rowId) return;
13215
+ id.agentLogRowId = rowId;
13216
+ const sc = readRunSidecar(plan.streamPath);
13217
+ if (sc) {
13218
+ try {
13219
+ writeFileSync20(pidPathFor(plan.streamPath), JSON.stringify({ ...sc, agentLogRowId: rowId }));
13220
+ } catch {
13221
+ }
13222
+ }
13223
+ }) : Promise.resolve();
13116
13224
  const obs = new RunObserver(id, adapter, eventsSink, emit.cycle);
13117
13225
  const meta = { pid: child.pid ?? null, ...deadline ? { deadline } : {} };
13118
13226
  obs.onClaim = () => writeRunMeta(id, startedAt, obs.liveTicket, meta);
@@ -13144,6 +13252,7 @@ function spawnAgent(plan, emit, hintMoved) {
13144
13252
  if (limitTimer && !killed && !obs.sawResult && msUntilDeadline(deadline) <= 0) {
13145
13253
  killed = { limitMinutes: plan.maxRunMinutes ?? 0, elapsedMs: Date.now() - Date.parse(spawnedAt) };
13146
13254
  }
13255
+ await startedRow;
13147
13256
  const { ms: ms3, handoffPending } = await finishRun(
13148
13257
  id,
13149
13258
  obs,
@@ -17915,6 +18024,21 @@ async function resolveOne(tracker, t, seat, approvedStatus, buildingStatus, affi
17915
18024
  throw e;
17916
18025
  }
17917
18026
  }
18027
+ async function claimStillHeld(tracker, t, buildingStatus, affinity) {
18028
+ if (!tracker.ticket) return null;
18029
+ let now;
18030
+ try {
18031
+ now = await tracker.ticket(t.id);
18032
+ } catch {
18033
+ return null;
18034
+ }
18035
+ const holder = affinity ? now[affinity.column] : null;
18036
+ if (affinity && typeof holder === "string" && holder && holder !== affinity.shipId) {
18037
+ return `${t.issue_id} is held by another ship now`;
18038
+ }
18039
+ if (now.status !== buildingStatus) return `${t.issue_id} is at ${now.status}, not ${buildingStatus}`;
18040
+ return null;
18041
+ }
17918
18042
 
17919
18043
  // src/repo-refresh.ts
17920
18044
  import { existsSync as existsSync38, mkdirSync as mkdirSync26, readFileSync as readFileSync36, writeFileSync as writeFileSync29 } from "fs";
@@ -25156,6 +25280,13 @@ async function runRoleAgentLocked(role, decision, ctx, hooks, held) {
25156
25280
  emit.emit(`${role} skipped this cycle \u2014 every candidate was already claimed elsewhere`);
25157
25281
  return;
25158
25282
  }
25283
+ if (role !== "qa") {
25284
+ const lost = await claimStillHeld(tracker, result.ticket, claimStatuses(role, contract).building, await tracker.claimAffinity());
25285
+ if (lost) {
25286
+ emit.warn(`${role} run refused: ${lost}`, { step: "select", role });
25287
+ return;
25288
+ }
25289
+ }
25159
25290
  if (result.claimed) {
25160
25291
  const idx = decision.tickets.findIndex((t) => t.id === result.ticket.id);
25161
25292
  if (idx >= 0) decision.tickets[idx] = result.ticket;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tablation/crew",
3
- "version": "0.133.1",
3
+ "version": "0.134.0",
4
4
  "description": "A standing team of headless agents that picks work off a Tablation board, does it on a machine you control, and reports back.",
5
5
  "type": "module",
6
6
  "license": "MIT",