@staff0rd/assist 0.367.2 → 0.369.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.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { Command } from "commander";
6
6
  // package.json
7
7
  var package_default = {
8
8
  name: "@staff0rd/assist",
9
- version: "0.367.2",
9
+ version: "0.369.0",
10
10
  type: "module",
11
11
  main: "dist/index.js",
12
12
  bin: {
@@ -963,7 +963,7 @@ function createPool() {
963
963
  return pool;
964
964
  }
965
965
  async function initSchema(pool) {
966
- await ensureSchema((sql10) => pool.query(sql10));
966
+ await ensureSchema((sql11) => pool.query(sql11));
967
967
  const orm = makeOrmFromPool(pool);
968
968
  await seedNewsFeeds(orm);
969
969
  await runUsagePeakCleanup(orm);
@@ -3881,8 +3881,8 @@ async function buildDump(tables, copyOut) {
3881
3881
  // src/commands/backlog/dump/copyTableOut.ts
3882
3882
  import { to as copyTo } from "pg-copy-streams";
3883
3883
  async function copyTableOut(client, table) {
3884
- const sql10 = `COPY ${table.name} (${table.columns.join(", ")}) TO STDOUT`;
3885
- const stream = client.query(copyTo(sql10));
3884
+ const sql11 = `COPY ${table.name} (${table.columns.join(", ")}) TO STDOUT`;
3885
+ const stream = client.query(copyTo(sql11));
3886
3886
  const chunks = [];
3887
3887
  for await (const chunk of stream) {
3888
3888
  chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
@@ -5234,6 +5234,7 @@ function buildAuthoredPhasePrompt(item, phaseNumber, phase) {
5234
5234
  const confirmSuffix = needsConfirmation ? " and the user confirms" : "";
5235
5235
  return [
5236
5236
  ...buildContextLines(item, phaseNumber, phase),
5237
+ ...buildJiraStartedLines(item, phaseNumber),
5237
5238
  "",
5238
5239
  "Focus ONLY on this phase. Do not work on other phases.",
5239
5240
  "If you need to modify backlog items, run `assist backlog --help` to discover available commands.",
@@ -5262,6 +5263,15 @@ function buildContextLines(item, phaseNumber, phase) {
5262
5263
  formatTasks(phase)
5263
5264
  ];
5264
5265
  }
5266
+ function buildJiraStartedLines(item, phaseNumber) {
5267
+ if (phaseNumber !== 1 || !item.jiraKey) {
5268
+ return [];
5269
+ }
5270
+ return [
5271
+ "",
5272
+ `As your first step, before any implementation, run \`/jira started ${item.jiraKey}\` to assign the issue to yourself and transition it to In Progress.`
5273
+ ];
5274
+ }
5265
5275
  function buildManualCheckLines(manualChecks) {
5266
5276
  if (manualChecks.length > 0) {
5267
5277
  return [
@@ -6311,8 +6321,12 @@ function createFallbackHandler(routes3, htmlHandler2, extra) {
6311
6321
  import { eq as eq12 } from "drizzle-orm";
6312
6322
 
6313
6323
  // src/commands/backlog/loadItemSummaries.ts
6314
- import { asc as asc5, eq as eq11 } from "drizzle-orm";
6324
+ import { asc as asc5, eq as eq11, ne, sql as sql5 } from "drizzle-orm";
6315
6325
  async function loadItemSummaries(orm, origin) {
6326
+ const incompleteCounts = orm.select({
6327
+ itemId: itemSubtasks.itemId,
6328
+ count: sql5`count(*)::int`.as("incomplete_subtasks")
6329
+ }).from(itemSubtasks).where(ne(itemSubtasks.status, "done")).groupBy(itemSubtasks.itemId).as("incomplete_counts");
6316
6330
  const rows = await orm.select({
6317
6331
  id: items.id,
6318
6332
  origin: items.origin,
@@ -6320,8 +6334,9 @@ async function loadItemSummaries(orm, origin) {
6320
6334
  name: items.name,
6321
6335
  status: items.status,
6322
6336
  starred: items.starred,
6323
- jiraKey: items.jiraKey
6324
- }).from(items).where(origin === void 0 ? void 0 : eq11(items.origin, origin)).orderBy(asc5(items.id));
6337
+ jiraKey: items.jiraKey,
6338
+ incompleteSubtasks: incompleteCounts.count
6339
+ }).from(items).leftJoin(incompleteCounts, eq11(incompleteCounts.itemId, items.id)).where(origin === void 0 ? void 0 : eq11(items.origin, origin)).orderBy(asc5(items.id));
6325
6340
  return rows.map((row) => ({
6326
6341
  id: row.id,
6327
6342
  origin: row.origin,
@@ -6329,7 +6344,8 @@ async function loadItemSummaries(orm, origin) {
6329
6344
  name: row.name,
6330
6345
  status: row.status,
6331
6346
  starred: row.starred,
6332
- jiraKey: row.jiraKey ?? void 0
6347
+ jiraKey: row.jiraKey ?? void 0,
6348
+ incompleteSubtasks: row.incompleteSubtasks ?? 0
6333
6349
  }));
6334
6350
  }
6335
6351
 
@@ -6484,6 +6500,17 @@ async function parseStarBody(req) {
6484
6500
  return JSON.parse(await readBody(req));
6485
6501
  }
6486
6502
 
6503
+ // src/commands/backlog/resolveRewindPlan.ts
6504
+ function resolveRewindPlan(item) {
6505
+ return [...resolvePlan(item), buildReviewPhase()];
6506
+ }
6507
+
6508
+ // src/commands/backlog/web/withReviewPhase.ts
6509
+ function withReviewPhase(item) {
6510
+ if (!item.plan || item.plan.length === 0) return item;
6511
+ return { ...item, plan: resolveRewindPlan(item) };
6512
+ }
6513
+
6487
6514
  // src/commands/backlog/web/shared.ts
6488
6515
  async function listItems(req, res) {
6489
6516
  applyCwdFromReq(req);
@@ -6501,7 +6528,7 @@ async function findItemOr404(res, id2) {
6501
6528
  }
6502
6529
  async function getItemById(res, id2) {
6503
6530
  const result = await findItemOr404(res, id2);
6504
- if (result) respondJson(res, 200, result.item);
6531
+ if (result) respondJson(res, 200, withReviewPhase(result.item));
6505
6532
  }
6506
6533
  async function deleteItem2(res, id2) {
6507
6534
  const result = await findItemOr404(res, id2);
@@ -6575,12 +6602,33 @@ async function patchSubtaskStatus(req, res, id2, idx) {
6575
6602
  respondJson(res, 200, await loadItem(orm, id2));
6576
6603
  }
6577
6604
 
6578
- // src/commands/backlog/web/rewindItemPhase.ts
6579
- import { eq as eq16 } from "drizzle-orm";
6580
-
6581
- // src/commands/backlog/resolveRewindPlan.ts
6582
- function resolveRewindPlan(item) {
6583
- return [...resolvePlan(item), buildReviewPhase()];
6605
+ // src/commands/backlog/rewindItemToPhase.ts
6606
+ async function rewindItemToPhase(orm, item, phaseNumber, reason) {
6607
+ const plan2 = resolveRewindPlan(item);
6608
+ if (phaseNumber < 1 || phaseNumber > plan2.length) {
6609
+ return {
6610
+ ok: false,
6611
+ error: `Phase ${phaseNumber} does not exist. Valid range: 1\u2013${plan2.length}.`
6612
+ };
6613
+ }
6614
+ const currentPhase = item.currentPhase ?? 1;
6615
+ if (phaseNumber >= currentPhase) {
6616
+ return {
6617
+ ok: false,
6618
+ error: `Phase ${phaseNumber} is not earlier than the current phase (${currentPhase}).`
6619
+ };
6620
+ }
6621
+ const phaseName = plan2[phaseNumber - 1].name;
6622
+ await appendComment(
6623
+ orm,
6624
+ item.id,
6625
+ `Rewound to phase ${phaseNumber} (${phaseName}): ${reason}`,
6626
+ { phase: phaseNumber }
6627
+ );
6628
+ await updateCurrentPhase(orm, item.id, phaseNumber);
6629
+ await updateStatus(orm, item.id, "in-progress");
6630
+ writeSignal("rewind", { itemId: item.id, targetPhase: phaseNumber - 1 });
6631
+ return { ok: true, phaseName };
6584
6632
  }
6585
6633
 
6586
6634
  // src/commands/backlog/web/rewindItemPhase.ts
@@ -6589,32 +6637,13 @@ async function rewindItemPhase(req, res, id2) {
6589
6637
  const result = await findItemOr404(res, id2);
6590
6638
  if (!result) return;
6591
6639
  const { orm, item } = result;
6592
- const plan2 = resolveRewindPlan(item);
6593
- const error = validateRewind(item, plan2, phase);
6594
- if (error) {
6595
- respondJson(res, 400, { error });
6640
+ const outcome = await rewindItemToPhase(orm, item, phase, reason);
6641
+ if (!outcome.ok) {
6642
+ respondJson(res, 400, { error: outcome.error });
6596
6643
  return;
6597
6644
  }
6598
- const phaseName = plan2[phase - 1].name;
6599
- await appendComment(
6600
- orm,
6601
- id2,
6602
- `Rewound to phase ${phase} (${phaseName}): ${reason}`,
6603
- { phase }
6604
- );
6605
- await orm.update(items).set({ currentPhase: phase, status: "in-progress" }).where(eq16(items.id, id2));
6606
6645
  respondJson(res, 200, await loadItem(orm, id2));
6607
6646
  }
6608
- function validateRewind(item, plan2, phase) {
6609
- if (phase < 1 || phase > plan2.length) {
6610
- return `Phase ${phase} does not exist. Valid range: 1\u2013${plan2.length}.`;
6611
- }
6612
- const currentPhase = item.currentPhase ?? 1;
6613
- if (phase >= currentPhase) {
6614
- return `Phase ${phase} is not earlier than the current phase (${currentPhase}).`;
6615
- }
6616
- return void 0;
6617
- }
6618
6647
 
6619
6648
  // src/commands/backlog/web/handleItemRoute.ts
6620
6649
  var id = (match, group = 1) => Number.parseInt(match[group], 10);
@@ -7725,7 +7754,7 @@ async function web2(options2) {
7725
7754
 
7726
7755
  // src/commands/backlog/associate-jira/index.ts
7727
7756
  import chalk61 from "chalk";
7728
- import { eq as eq17 } from "drizzle-orm";
7757
+ import { eq as eq16 } from "drizzle-orm";
7729
7758
 
7730
7759
  // src/commands/jira/fetchIssue.ts
7731
7760
  import { execSync as execSync25 } from "child_process";
@@ -7767,7 +7796,7 @@ async function associateJira(id2, key, options2) {
7767
7796
  const { orm } = found;
7768
7797
  const itemId = found.item.id;
7769
7798
  if (options2.clear) {
7770
- await orm.update(items).set({ jiraKey: null }).where(eq17(items.id, itemId));
7799
+ await orm.update(items).set({ jiraKey: null }).where(eq16(items.id, itemId));
7771
7800
  console.log(chalk61.green(`Cleared Jira association on item #${itemId}.`));
7772
7801
  return;
7773
7802
  }
@@ -7786,7 +7815,7 @@ async function associateJira(id2, key, options2) {
7786
7815
  const parsed = fetchIssue(key, "summary");
7787
7816
  const fields = parsed?.fields;
7788
7817
  const summary = fields?.summary;
7789
- await orm.update(items).set({ jiraKey: key }).where(eq17(items.id, itemId));
7818
+ await orm.update(items).set({ jiraKey: key }).where(eq16(items.id, itemId));
7790
7819
  console.log(
7791
7820
  chalk61.green(`Associated ${key} with item #${itemId}.`),
7792
7821
  summary ? chalk61.dim(`(${summary})`) : ""
@@ -7986,8 +8015,8 @@ async function readStdinBuffer() {
7986
8015
  import { finished } from "stream/promises";
7987
8016
  import { from as copyFrom } from "pg-copy-streams";
7988
8017
  async function copyTableIn(client, table, data) {
7989
- const sql10 = `COPY ${table.name} (${table.columns.join(", ")}) FROM STDIN`;
7990
- const stream = client.query(copyFrom(sql10));
8018
+ const sql11 = `COPY ${table.name} (${table.columns.join(", ")}) FROM STDIN`;
8019
+ const stream = client.query(copyFrom(sql11));
7991
8020
  stream.end(data);
7992
8021
  await finished(stream);
7993
8022
  }
@@ -8084,22 +8113,22 @@ function registerImportCommand(cmd) {
8084
8113
  import chalk68 from "chalk";
8085
8114
 
8086
8115
  // src/commands/backlog/insertPhaseAt.ts
8087
- import { count as count2, eq as eq19 } from "drizzle-orm";
8116
+ import { count as count2, eq as eq18 } from "drizzle-orm";
8088
8117
 
8089
8118
  // src/commands/backlog/shiftPhasesUp.ts
8090
- import { and as and6, desc as desc3, eq as eq18, gte } from "drizzle-orm";
8119
+ import { and as and6, desc as desc3, eq as eq17, gte } from "drizzle-orm";
8091
8120
  async function shiftPhasesUp(db, itemId, fromIdx) {
8092
- const toShift = await db.select({ idx: planPhases.idx }).from(planPhases).where(and6(eq18(planPhases.itemId, itemId), gte(planPhases.idx, fromIdx))).orderBy(desc3(planPhases.idx));
8121
+ const toShift = await db.select({ idx: planPhases.idx }).from(planPhases).where(and6(eq17(planPhases.itemId, itemId), gte(planPhases.idx, fromIdx))).orderBy(desc3(planPhases.idx));
8093
8122
  for (const p of toShift) {
8094
- await db.update(planTasks).set({ phaseIdx: p.idx + 1 }).where(and6(eq18(planTasks.itemId, itemId), eq18(planTasks.phaseIdx, p.idx)));
8095
- await db.update(planPhases).set({ idx: p.idx + 1 }).where(and6(eq18(planPhases.itemId, itemId), eq18(planPhases.idx, p.idx)));
8123
+ await db.update(planTasks).set({ phaseIdx: p.idx + 1 }).where(and6(eq17(planTasks.itemId, itemId), eq17(planTasks.phaseIdx, p.idx)));
8124
+ await db.update(planPhases).set({ idx: p.idx + 1 }).where(and6(eq17(planPhases.itemId, itemId), eq17(planPhases.idx, p.idx)));
8096
8125
  }
8097
8126
  }
8098
8127
 
8099
8128
  // src/commands/backlog/insertPhaseAt.ts
8100
8129
  async function insertPhaseAt(orm, itemId, phaseIdx, name, tasks, manualChecks, currentPhase) {
8101
8130
  await orm.transaction(async (tx) => {
8102
- const [row] = await tx.select({ cnt: count2() }).from(planPhases).where(eq19(planPhases.itemId, itemId));
8131
+ const [row] = await tx.select({ cnt: count2() }).from(planPhases).where(eq18(planPhases.itemId, itemId));
8103
8132
  const phaseCount = row?.cnt ?? 0;
8104
8133
  await shiftPhasesUp(tx, itemId, phaseIdx);
8105
8134
  await tx.insert(planPhases).values({ itemId, idx: phaseIdx, name, manualChecks });
@@ -8108,17 +8137,17 @@ async function insertPhaseAt(orm, itemId, phaseIdx, name, tasks, manualChecks, c
8108
8137
  }
8109
8138
  if (currentPhase !== void 0 && currentPhase - 1 >= phaseIdx) {
8110
8139
  const atReviewSlot = currentPhase - 1 >= phaseCount;
8111
- await tx.update(items).set({ currentPhase: atReviewSlot ? phaseIdx + 1 : currentPhase + 1 }).where(eq19(items.id, itemId));
8140
+ await tx.update(items).set({ currentPhase: atReviewSlot ? phaseIdx + 1 : currentPhase + 1 }).where(eq18(items.id, itemId));
8112
8141
  }
8113
8142
  });
8114
8143
  }
8115
8144
 
8116
8145
  // src/commands/backlog/insertSubtask.ts
8117
- import { sql as sql5 } from "drizzle-orm";
8146
+ import { sql as sql6 } from "drizzle-orm";
8118
8147
  async function insertSubtask(orm, itemId, title, description) {
8119
8148
  await orm.insert(itemSubtasks).values({
8120
8149
  itemId,
8121
- idx: sql5`(SELECT COALESCE(MAX(${itemSubtasks.idx}) + 1, 0) FROM ${itemSubtasks} WHERE ${itemSubtasks.itemId} = ${itemId})`,
8150
+ idx: sql6`(SELECT COALESCE(MAX(${itemSubtasks.idx}) + 1, 0) FROM ${itemSubtasks} WHERE ${itemSubtasks.itemId} = ${itemId})`,
8122
8151
  title,
8123
8152
  description: description ?? null,
8124
8153
  status: "todo"
@@ -8242,9 +8271,9 @@ import chalk70 from "chalk";
8242
8271
 
8243
8272
  // src/commands/backlog/resolveInsertPosition.ts
8244
8273
  import chalk69 from "chalk";
8245
- import { count as count3, eq as eq20 } from "drizzle-orm";
8274
+ import { count as count3, eq as eq19 } from "drizzle-orm";
8246
8275
  async function resolveInsertPosition(orm, itemId, position) {
8247
- const [row] = await orm.select({ cnt: count3() }).from(planPhases).where(eq20(planPhases.itemId, itemId));
8276
+ const [row] = await orm.select({ cnt: count3() }).from(planPhases).where(eq19(planPhases.itemId, itemId));
8248
8277
  const phaseCount = row?.cnt ?? 0;
8249
8278
  if (position === void 0) return phaseCount;
8250
8279
  const pos = Number.parseInt(position, 10);
@@ -8368,9 +8397,9 @@ function hasCycle(adjacency, fromId, toId) {
8368
8397
  }
8369
8398
 
8370
8399
  // src/commands/backlog/loadDependencyGraph.ts
8371
- import { eq as eq21 } from "drizzle-orm";
8400
+ import { eq as eq20 } from "drizzle-orm";
8372
8401
  async function loadDependencyGraph(orm) {
8373
- const rows = await orm.select({ itemId: links.itemId, targetId: links.targetId }).from(links).where(eq21(links.type, "depends-on"));
8402
+ const rows = await orm.select({ itemId: links.itemId, targetId: links.targetId }).from(links).where(eq20(links.type, "depends-on"));
8374
8403
  const graph = /* @__PURE__ */ new Map();
8375
8404
  for (const { itemId, targetId } of rows) {
8376
8405
  const bucket = graph.get(itemId);
@@ -8433,7 +8462,7 @@ async function link(fromId, toId, opts) {
8433
8462
 
8434
8463
  // src/commands/backlog/unlink.ts
8435
8464
  import chalk74 from "chalk";
8436
- import { and as and7, eq as eq22 } from "drizzle-orm";
8465
+ import { and as and7, eq as eq21 } from "drizzle-orm";
8437
8466
  async function unlink(fromId, toId) {
8438
8467
  const fromNum = Number.parseInt(fromId, 10);
8439
8468
  const toNum = Number.parseInt(toId, 10);
@@ -8451,7 +8480,7 @@ async function unlink(fromId, toId) {
8451
8480
  console.log(chalk74.yellow(`No link from #${fromId} to #${toId} found.`));
8452
8481
  return;
8453
8482
  }
8454
- await orm.delete(links).where(and7(eq22(links.itemId, fromNum), eq22(links.targetId, toNum)));
8483
+ await orm.delete(links).where(and7(eq21(links.itemId, fromNum), eq21(links.targetId, toNum)));
8455
8484
  console.log(chalk74.green(`Removed link from #${fromId} to #${toId}.`));
8456
8485
  }
8457
8486
 
@@ -8467,7 +8496,7 @@ function registerLinkCommands(cmd) {
8467
8496
 
8468
8497
  // src/commands/backlog/move-repo/index.ts
8469
8498
  import chalk76 from "chalk";
8470
- import { eq as eq24 } from "drizzle-orm";
8499
+ import { eq as eq23 } from "drizzle-orm";
8471
8500
 
8472
8501
  // src/commands/backlog/move-repo/confirmMove.ts
8473
8502
  import chalk75 from "chalk";
@@ -8482,9 +8511,9 @@ async function confirmMove(cnt, oldOrigin, newOrigin) {
8482
8511
  }
8483
8512
 
8484
8513
  // src/commands/backlog/move-repo/countByOrigin.ts
8485
- import { count as count4, eq as eq23 } from "drizzle-orm";
8514
+ import { count as count4, eq as eq22 } from "drizzle-orm";
8486
8515
  async function countByOrigin(orm, origin) {
8487
- const [{ cnt }] = await orm.select({ cnt: count4() }).from(items).where(eq23(items.origin, origin));
8516
+ const [{ cnt }] = await orm.select({ cnt: count4() }).from(items).where(eq22(items.origin, origin));
8488
8517
  return cnt;
8489
8518
  }
8490
8519
 
@@ -8527,7 +8556,7 @@ async function moveRepo(oldOriginRaw, newOriginRaw, options2 = {}) {
8527
8556
  console.log(chalk76.yellow("Move cancelled; no changes made."));
8528
8557
  return;
8529
8558
  }
8530
- await orm.update(items).set({ origin: newOrigin }).where(eq24(items.origin, oldOrigin));
8559
+ await orm.update(items).set({ origin: newOrigin }).where(eq23(items.origin, oldOrigin));
8531
8560
  console.log(
8532
8561
  chalk76.green(
8533
8562
  `Moved ${pluralItems(cnt)} from "${oldOrigin}" to "${newOrigin}".`
@@ -8707,47 +8736,24 @@ function registerRefineCommand(cmd) {
8707
8736
 
8708
8737
  // src/commands/backlog/rewindPhase.ts
8709
8738
  import chalk80 from "chalk";
8710
- function validateRewind2(item, plan2, phaseNumber) {
8711
- if (phaseNumber < 1 || phaseNumber > plan2.length) {
8712
- return `Phase ${phaseNumber} does not exist. Valid range: 1\u2013${plan2.length}.`;
8713
- }
8714
- const currentPhase = item.currentPhase ?? 1;
8715
- if (phaseNumber >= currentPhase) {
8716
- return `Phase ${phaseNumber} is not earlier than the current phase (${currentPhase}).`;
8717
- }
8718
- return void 0;
8719
- }
8720
8739
  async function rewindPhase(id2, phase, opts) {
8721
8740
  const phaseNumber = Number.parseInt(phase, 10);
8722
- const phaseIndex = phaseNumber - 1;
8723
8741
  const { orm } = await getReady();
8724
8742
  const item = await loadItem(orm, Number.parseInt(id2, 10));
8725
8743
  if (!item) {
8726
8744
  console.log(chalk80.red(`Item #${id2} not found.`));
8727
8745
  return;
8728
8746
  }
8729
- const plan2 = resolveRewindPlan(item);
8730
- const error = validateRewind2(item, plan2, phaseNumber);
8731
- if (error) {
8732
- console.log(chalk80.red(error));
8747
+ const result = await rewindItemToPhase(orm, item, phaseNumber, opts.reason);
8748
+ if (!result.ok) {
8749
+ console.log(chalk80.red(result.error));
8733
8750
  process.exitCode = 1;
8734
8751
  return;
8735
8752
  }
8736
- const phaseName = plan2[phaseIndex].name;
8737
- await appendComment(
8738
- orm,
8739
- item.id,
8740
- `Rewound to phase ${phaseNumber} (${phaseName}): ${opts.reason}`,
8741
- { phase: phaseNumber }
8742
- );
8743
- await setCurrentPhase(id2, phaseNumber);
8744
- await setStatus(id2, "in-progress");
8745
- writeSignal("rewind", {
8746
- itemId: Number.parseInt(id2, 10),
8747
- targetPhase: phaseIndex
8748
- });
8749
8753
  console.log(
8750
- chalk80.green(`Rewound item #${id2} to phase ${phaseNumber} (${phaseName}).`)
8754
+ chalk80.green(
8755
+ `Rewound item #${id2} to phase ${phaseNumber} (${result.phaseName}).`
8756
+ )
8751
8757
  );
8752
8758
  }
8753
8759
 
@@ -8896,10 +8902,10 @@ async function start(id2) {
8896
8902
 
8897
8903
  // src/commands/backlog/stop/index.ts
8898
8904
  import chalk88 from "chalk";
8899
- import { and as and8, eq as eq25 } from "drizzle-orm";
8905
+ import { and as and8, eq as eq24 } from "drizzle-orm";
8900
8906
  async function stop() {
8901
8907
  const { orm } = await getReady();
8902
- const stopped = await orm.update(items).set({ status: "todo", currentPhase: 1 }).where(and8(eq25(items.status, "in-progress"), eq25(items.origin, getOrigin()))).returning({ id: items.id, name: items.name });
8908
+ const stopped = await orm.update(items).set({ status: "todo", currentPhase: 1 }).where(and8(eq24(items.status, "in-progress"), eq24(items.origin, getOrigin()))).returning({ id: items.id, name: items.name });
8903
8909
  if (stopped.length === 0) {
8904
8910
  console.log(chalk88.yellow("No in-progress items to stop."));
8905
8911
  return;
@@ -9008,13 +9014,13 @@ async function findSubtask(id2, index3) {
9008
9014
  }
9009
9015
 
9010
9016
  // src/commands/backlog/updateSubtask.ts
9011
- import { and as and9, eq as eq26 } from "drizzle-orm";
9017
+ import { and as and9, eq as eq25 } from "drizzle-orm";
9012
9018
  async function updateSubtask(orm, itemId, idx, fields) {
9013
9019
  const set = {};
9014
9020
  if (fields.title !== void 0) set.title = fields.title;
9015
9021
  if (fields.description !== void 0) set.description = fields.description;
9016
9022
  if (fields.status !== void 0) set.status = fields.status;
9017
- const [row] = await orm.update(itemSubtasks).set(set).where(and9(eq26(itemSubtasks.itemId, itemId), eq26(itemSubtasks.idx, idx))).returning({ title: itemSubtasks.title });
9023
+ const [row] = await orm.update(itemSubtasks).set(set).where(and9(eq25(itemSubtasks.itemId, itemId), eq25(itemSubtasks.idx, idx))).returning({ title: itemSubtasks.title });
9018
9024
  return row?.title;
9019
9025
  }
9020
9026
 
@@ -9067,16 +9073,16 @@ async function editSubtask(id2, index3, options2) {
9067
9073
  import chalk94 from "chalk";
9068
9074
 
9069
9075
  // src/commands/backlog/deleteSubtask.ts
9070
- import { and as and10, asc as asc6, eq as eq27 } from "drizzle-orm";
9076
+ import { and as and10, asc as asc6, eq as eq26 } from "drizzle-orm";
9071
9077
  async function deleteSubtask(orm, itemId, idx) {
9072
- const [row] = await orm.delete(itemSubtasks).where(and10(eq27(itemSubtasks.itemId, itemId), eq27(itemSubtasks.idx, idx))).returning({ title: itemSubtasks.title });
9078
+ const [row] = await orm.delete(itemSubtasks).where(and10(eq26(itemSubtasks.itemId, itemId), eq26(itemSubtasks.idx, idx))).returning({ title: itemSubtasks.title });
9073
9079
  if (!row) return void 0;
9074
- const remaining = await orm.select({ idx: itemSubtasks.idx }).from(itemSubtasks).where(eq27(itemSubtasks.itemId, itemId)).orderBy(asc6(itemSubtasks.idx));
9080
+ const remaining = await orm.select({ idx: itemSubtasks.idx }).from(itemSubtasks).where(eq26(itemSubtasks.itemId, itemId)).orderBy(asc6(itemSubtasks.idx));
9075
9081
  for (let i = 0; i < remaining.length; i++) {
9076
9082
  const oldIdx = remaining[i].idx;
9077
9083
  if (oldIdx === i) continue;
9078
9084
  await orm.update(itemSubtasks).set({ idx: i }).where(
9079
- and10(eq27(itemSubtasks.itemId, itemId), eq27(itemSubtasks.idx, oldIdx))
9085
+ and10(eq26(itemSubtasks.itemId, itemId), eq26(itemSubtasks.idx, oldIdx))
9080
9086
  );
9081
9087
  }
9082
9088
  return row.title;
@@ -9143,20 +9149,20 @@ function registerSubtaskCommands(cmd) {
9143
9149
 
9144
9150
  // src/commands/backlog/movePhase.ts
9145
9151
  import chalk96 from "chalk";
9146
- import { count as count6, eq as eq30 } from "drizzle-orm";
9152
+ import { count as count6, eq as eq29 } from "drizzle-orm";
9147
9153
 
9148
9154
  // src/commands/backlog/reorderPhaseRows.ts
9149
- import { and as and12, asc as asc8, eq as eq29 } from "drizzle-orm";
9155
+ import { and as and12, asc as asc8, eq as eq28 } from "drizzle-orm";
9150
9156
 
9151
9157
  // src/commands/backlog/reindexPhases.ts
9152
- import { and as and11, asc as asc7, count as count5, eq as eq28 } from "drizzle-orm";
9158
+ import { and as and11, asc as asc7, count as count5, eq as eq27 } from "drizzle-orm";
9153
9159
  async function reindexPhases(db, itemId) {
9154
- const remaining = await db.select({ idx: planPhases.idx }).from(planPhases).where(eq28(planPhases.itemId, itemId)).orderBy(asc7(planPhases.idx));
9160
+ const remaining = await db.select({ idx: planPhases.idx }).from(planPhases).where(eq27(planPhases.itemId, itemId)).orderBy(asc7(planPhases.idx));
9155
9161
  for (let i = 0; i < remaining.length; i++) {
9156
9162
  const oldIdx = remaining[i].idx;
9157
9163
  if (oldIdx === i) continue;
9158
- await db.update(planTasks).set({ phaseIdx: i }).where(and11(eq28(planTasks.itemId, itemId), eq28(planTasks.phaseIdx, oldIdx)));
9159
- await db.update(planPhases).set({ idx: i }).where(and11(eq28(planPhases.itemId, itemId), eq28(planPhases.idx, oldIdx)));
9164
+ await db.update(planTasks).set({ phaseIdx: i }).where(and11(eq27(planTasks.itemId, itemId), eq27(planTasks.phaseIdx, oldIdx)));
9165
+ await db.update(planPhases).set({ idx: i }).where(and11(eq27(planPhases.itemId, itemId), eq27(planPhases.idx, oldIdx)));
9160
9166
  }
9161
9167
  }
9162
9168
  async function adjustCurrentPhase(db, item, removedIdx) {
@@ -9164,24 +9170,24 @@ async function adjustCurrentPhase(db, item, removedIdx) {
9164
9170
  if (currentPhase === void 0) return;
9165
9171
  const currentIdx = currentPhase - 1;
9166
9172
  if (removedIdx < currentIdx) {
9167
- await db.update(items).set({ currentPhase: currentPhase - 1 }).where(eq28(items.id, item.id));
9173
+ await db.update(items).set({ currentPhase: currentPhase - 1 }).where(eq27(items.id, item.id));
9168
9174
  return;
9169
9175
  }
9170
9176
  if (removedIdx !== currentIdx) return;
9171
- const [row] = await db.select({ cnt: count5() }).from(planPhases).where(eq28(planPhases.itemId, item.id));
9177
+ const [row] = await db.select({ cnt: count5() }).from(planPhases).where(eq27(planPhases.itemId, item.id));
9172
9178
  const cnt = row?.cnt ?? 0;
9173
- await db.update(items).set({ currentPhase: cnt === 0 ? null : Math.min(currentPhase, cnt) }).where(eq28(items.id, item.id));
9179
+ await db.update(items).set({ currentPhase: cnt === 0 ? null : Math.min(currentPhase, cnt) }).where(eq27(items.id, item.id));
9174
9180
  }
9175
9181
 
9176
9182
  // src/commands/backlog/reorderPhaseRows.ts
9177
9183
  async function reorderPhaseRows(orm, itemId, fromIdx, toIdx) {
9178
9184
  await orm.transaction(async (tx) => {
9179
- const [phaseRow] = await tx.select({ name: planPhases.name, manualChecks: planPhases.manualChecks }).from(planPhases).where(and12(eq29(planPhases.itemId, itemId), eq29(planPhases.idx, fromIdx)));
9180
- const tasks = await tx.select({ idx: planTasks.idx, task: planTasks.task }).from(planTasks).where(and12(eq29(planTasks.itemId, itemId), eq29(planTasks.phaseIdx, fromIdx))).orderBy(asc8(planTasks.idx));
9185
+ const [phaseRow] = await tx.select({ name: planPhases.name, manualChecks: planPhases.manualChecks }).from(planPhases).where(and12(eq28(planPhases.itemId, itemId), eq28(planPhases.idx, fromIdx)));
9186
+ const tasks = await tx.select({ idx: planTasks.idx, task: planTasks.task }).from(planTasks).where(and12(eq28(planTasks.itemId, itemId), eq28(planTasks.phaseIdx, fromIdx))).orderBy(asc8(planTasks.idx));
9181
9187
  await tx.delete(planTasks).where(
9182
- and12(eq29(planTasks.itemId, itemId), eq29(planTasks.phaseIdx, fromIdx))
9188
+ and12(eq28(planTasks.itemId, itemId), eq28(planTasks.phaseIdx, fromIdx))
9183
9189
  );
9184
- await tx.delete(planPhases).where(and12(eq29(planPhases.itemId, itemId), eq29(planPhases.idx, fromIdx)));
9190
+ await tx.delete(planPhases).where(and12(eq28(planPhases.itemId, itemId), eq28(planPhases.idx, fromIdx)));
9185
9191
  await reindexPhases(tx, itemId);
9186
9192
  await shiftPhasesUp(tx, itemId, toIdx);
9187
9193
  await tx.insert(planPhases).values({
@@ -9222,7 +9228,7 @@ async function movePhase(id2, from, to) {
9222
9228
  if (!found) return;
9223
9229
  const { orm, item } = found;
9224
9230
  const itemId = item.id;
9225
- const [row] = await orm.select({ cnt: count6() }).from(planPhases).where(eq30(planPhases.itemId, itemId));
9231
+ const [row] = await orm.select({ cnt: count6() }).from(planPhases).where(eq29(planPhases.itemId, itemId));
9226
9232
  const phaseCount = row?.cnt ?? 0;
9227
9233
  const fromIdx = toIndex(from, phaseCount);
9228
9234
  if (fromIdx === void 0) return;
@@ -9238,22 +9244,22 @@ async function movePhase(id2, from, to) {
9238
9244
  import chalk98 from "chalk";
9239
9245
 
9240
9246
  // src/commands/backlog/applyPhaseUpdate.ts
9241
- import { and as and13, eq as eq31 } from "drizzle-orm";
9247
+ import { and as and13, eq as eq30 } from "drizzle-orm";
9242
9248
  async function applyPhaseUpdate(orm, itemId, phaseIdx, fields) {
9243
9249
  await orm.transaction(async (tx) => {
9244
9250
  if (fields.name) {
9245
9251
  await tx.update(planPhases).set({ name: fields.name }).where(
9246
- and13(eq31(planPhases.itemId, itemId), eq31(planPhases.idx, phaseIdx))
9252
+ and13(eq30(planPhases.itemId, itemId), eq30(planPhases.idx, phaseIdx))
9247
9253
  );
9248
9254
  }
9249
9255
  if (fields.manualCheck) {
9250
9256
  await tx.update(planPhases).set({ manualChecks: JSON.stringify(fields.manualCheck) }).where(
9251
- and13(eq31(planPhases.itemId, itemId), eq31(planPhases.idx, phaseIdx))
9257
+ and13(eq30(planPhases.itemId, itemId), eq30(planPhases.idx, phaseIdx))
9252
9258
  );
9253
9259
  }
9254
9260
  if (fields.task) {
9255
9261
  await tx.delete(planTasks).where(
9256
- and13(eq31(planTasks.itemId, itemId), eq31(planTasks.phaseIdx, phaseIdx))
9262
+ and13(eq30(planTasks.itemId, itemId), eq30(planTasks.phaseIdx, phaseIdx))
9257
9263
  );
9258
9264
  if (fields.task.length) {
9259
9265
  await tx.insert(planTasks).values(
@@ -9271,14 +9277,14 @@ async function applyPhaseUpdate(orm, itemId, phaseIdx, fields) {
9271
9277
 
9272
9278
  // src/commands/backlog/findPhase.ts
9273
9279
  import chalk97 from "chalk";
9274
- import { and as and14, count as count7, eq as eq32 } from "drizzle-orm";
9280
+ import { and as and14, count as count7, eq as eq31 } from "drizzle-orm";
9275
9281
  async function findPhase(id2, phase) {
9276
9282
  const found = await findOneItem(id2);
9277
9283
  if (!found) return void 0;
9278
9284
  const { orm, item } = found;
9279
9285
  const itemId = item.id;
9280
9286
  const phaseIdx = Number.parseInt(phase, 10) - 1;
9281
- const [row] = await orm.select({ cnt: count7() }).from(planPhases).where(and14(eq32(planPhases.itemId, itemId), eq32(planPhases.idx, phaseIdx)));
9287
+ const [row] = await orm.select({ cnt: count7() }).from(planPhases).where(and14(eq31(planPhases.itemId, itemId), eq31(planPhases.idx, phaseIdx)));
9282
9288
  if (!row || row.cnt === 0) {
9283
9289
  console.log(
9284
9290
  chalk97.red(`Phase ${phaseIdx + 1} not found on item #${itemId}.`)
@@ -9449,16 +9455,16 @@ function registerUpdatePhaseCommand(cmd) {
9449
9455
 
9450
9456
  // src/commands/backlog/removePhase.ts
9451
9457
  import chalk99 from "chalk";
9452
- import { and as and15, eq as eq33 } from "drizzle-orm";
9458
+ import { and as and15, eq as eq32 } from "drizzle-orm";
9453
9459
  async function removePhase(id2, phase) {
9454
9460
  const found = await findPhase(id2, phase);
9455
9461
  if (!found) return;
9456
9462
  const { item, orm, itemId, phaseIdx } = found;
9457
9463
  await orm.transaction(async (tx) => {
9458
9464
  await tx.delete(planTasks).where(
9459
- and15(eq33(planTasks.itemId, itemId), eq33(planTasks.phaseIdx, phaseIdx))
9465
+ and15(eq32(planTasks.itemId, itemId), eq32(planTasks.phaseIdx, phaseIdx))
9460
9466
  );
9461
- await tx.delete(planPhases).where(and15(eq33(planPhases.itemId, itemId), eq33(planPhases.idx, phaseIdx)));
9467
+ await tx.delete(planPhases).where(and15(eq32(planPhases.itemId, itemId), eq32(planPhases.idx, phaseIdx)));
9462
9468
  await reindexPhases(tx, itemId);
9463
9469
  await adjustCurrentPhase(tx, item, phaseIdx);
9464
9470
  });
@@ -9469,7 +9475,7 @@ async function removePhase(id2, phase) {
9469
9475
 
9470
9476
  // src/commands/backlog/update/index.ts
9471
9477
  import chalk101 from "chalk";
9472
- import { eq as eq34 } from "drizzle-orm";
9478
+ import { eq as eq33 } from "drizzle-orm";
9473
9479
 
9474
9480
  // src/commands/backlog/update/applyAcMutations.ts
9475
9481
  function hasAcMutations(options2) {
@@ -9549,7 +9555,7 @@ async function update(id2, options2) {
9549
9555
  if (!built) return;
9550
9556
  const { orm } = found;
9551
9557
  const itemId = found.item.id;
9552
- await orm.update(items).set(built.set).where(eq34(items.id, itemId));
9558
+ await orm.update(items).set(built.set).where(eq33(items.id, itemId));
9553
9559
  console.log(chalk101.green(`Updated ${built.fields} on item #${itemId}.`));
9554
9560
  }
9555
9561
 
@@ -13259,9 +13265,9 @@ function registerGithub(program2) {
13259
13265
  }
13260
13266
 
13261
13267
  // src/commands/handover/countPendingHandovers.ts
13262
- import { and as and16, eq as eq35, isNull, sql as sql6 } from "drizzle-orm";
13268
+ import { and as and16, eq as eq34, isNull, sql as sql7 } from "drizzle-orm";
13263
13269
  async function countPendingHandovers(orm, origin) {
13264
- const [row] = await orm.select({ count: sql6`count(*)::int` }).from(handovers).where(and16(eq35(handovers.origin, origin), isNull(handovers.recalledAt)));
13270
+ const [row] = await orm.select({ count: sql7`count(*)::int` }).from(handovers).where(and16(eq34(handovers.origin, origin), isNull(handovers.recalledAt)));
13265
13271
  return row?.count ?? 0;
13266
13272
  }
13267
13273
 
@@ -13401,13 +13407,13 @@ async function load2(options2 = {}) {
13401
13407
  }
13402
13408
 
13403
13409
  // src/commands/handover/listPendingHandovers.ts
13404
- import { and as and17, desc as desc4, eq as eq36, isNull as isNull2 } from "drizzle-orm";
13410
+ import { and as and17, desc as desc4, eq as eq35, isNull as isNull2 } from "drizzle-orm";
13405
13411
  async function listPendingHandovers(orm, origin) {
13406
13412
  return orm.select({
13407
13413
  id: handovers.id,
13408
13414
  summary: handovers.summary,
13409
13415
  createdAt: handovers.createdAt
13410
- }).from(handovers).where(and17(eq36(handovers.origin, origin), isNull2(handovers.recalledAt))).orderBy(desc4(handovers.createdAt), desc4(handovers.id));
13416
+ }).from(handovers).where(and17(eq35(handovers.origin, origin), isNull2(handovers.recalledAt))).orderBy(desc4(handovers.createdAt), desc4(handovers.id));
13411
13417
  }
13412
13418
 
13413
13419
  // src/commands/handover/printPendingHandovers.ts
@@ -13420,17 +13426,17 @@ async function printPendingHandovers() {
13420
13426
  }
13421
13427
 
13422
13428
  // src/commands/handover/recallHandover.ts
13423
- import { and as and18, desc as desc5, eq as eq37, isNull as isNull3 } from "drizzle-orm";
13429
+ import { and as and18, desc as desc5, eq as eq36, isNull as isNull3 } from "drizzle-orm";
13424
13430
  async function recallHandover(orm, origin, id2) {
13425
13431
  const [row] = await orm.select().from(handovers).where(
13426
13432
  and18(
13427
- eq37(handovers.origin, origin),
13433
+ eq36(handovers.origin, origin),
13428
13434
  isNull3(handovers.recalledAt),
13429
- ...id2 === void 0 ? [] : [eq37(handovers.id, id2)]
13435
+ ...id2 === void 0 ? [] : [eq36(handovers.id, id2)]
13430
13436
  )
13431
13437
  ).orderBy(desc5(handovers.createdAt), desc5(handovers.id)).limit(1);
13432
13438
  if (!row) return void 0;
13433
- await orm.update(handovers).set({ recalledAt: /* @__PURE__ */ new Date() }).where(eq37(handovers.id, row.id));
13439
+ await orm.update(handovers).set({ recalledAt: /* @__PURE__ */ new Date() }).where(eq36(handovers.id, row.id));
13434
13440
  return row.content;
13435
13441
  }
13436
13442
 
@@ -19458,8 +19464,8 @@ function filterToSql(filter) {
19458
19464
  // src/commands/seq/fetchSeqData.ts
19459
19465
  function buildDataParams(filter, count8, from, to) {
19460
19466
  const sqlFilter = filterToSql(filter);
19461
- const sql10 = `select @Timestamp, @Level, @Exception, @Message from stream where ${sqlFilter} order by @Timestamp desc limit ${count8}`;
19462
- const params = new URLSearchParams({ q: sql10 });
19467
+ const sql11 = `select @Timestamp, @Level, @Exception, @Message from stream where ${sqlFilter} order by @Timestamp desc limit ${count8}`;
19468
+ const params = new URLSearchParams({ q: sql11 });
19463
19469
  if (from) params.set("rangeStartUtc", from);
19464
19470
  if (to) params.set("rangeEndUtc", to);
19465
19471
  return params;
@@ -19719,26 +19725,26 @@ import chalk186 from "chalk";
19719
19725
  // src/commands/sql/loadConnections.ts
19720
19726
  function loadConnections3() {
19721
19727
  const raw = loadGlobalConfigRaw();
19722
- const sql10 = raw.sql;
19723
- return sql10?.connections ?? [];
19728
+ const sql11 = raw.sql;
19729
+ return sql11?.connections ?? [];
19724
19730
  }
19725
19731
  function saveConnections3(connections) {
19726
19732
  const raw = loadGlobalConfigRaw();
19727
- const sql10 = raw.sql ?? {};
19728
- sql10.connections = connections;
19729
- raw.sql = sql10;
19733
+ const sql11 = raw.sql ?? {};
19734
+ sql11.connections = connections;
19735
+ raw.sql = sql11;
19730
19736
  saveGlobalConfig(raw);
19731
19737
  }
19732
19738
  function getDefaultConnection2() {
19733
19739
  const raw = loadGlobalConfigRaw();
19734
- const sql10 = raw.sql;
19735
- return sql10?.defaultConnection;
19740
+ const sql11 = raw.sql;
19741
+ return sql11?.defaultConnection;
19736
19742
  }
19737
19743
  function setDefaultConnection2(name) {
19738
19744
  const raw = loadGlobalConfigRaw();
19739
- const sql10 = raw.sql ?? {};
19740
- sql10.defaultConnection = name;
19741
- raw.sql = sql10;
19745
+ const sql11 = raw.sql ?? {};
19746
+ sql11.defaultConnection = name;
19747
+ raw.sql = sql11;
19742
19748
  saveGlobalConfig(raw);
19743
19749
  }
19744
19750
 
@@ -19809,9 +19815,9 @@ function resolveConnection3(name) {
19809
19815
  }
19810
19816
 
19811
19817
  // src/commands/sql/sqlConnect.ts
19812
- import sql7 from "mssql";
19818
+ import sql8 from "mssql";
19813
19819
  async function sqlConnect(conn) {
19814
- return await sql7.connect({
19820
+ return await sql8.connect({
19815
19821
  server: conn.server,
19816
19822
  port: conn.port,
19817
19823
  user: conn.user,
@@ -19874,11 +19880,11 @@ var MUTATION_PATTERN = new RegExp(
19874
19880
  `\\b(${MUTATION_KEYWORDS.join("|")})\\b`,
19875
19881
  "i"
19876
19882
  );
19877
- function stripComments(sql10) {
19878
- return sql10.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/--[^\n]*/g, " ");
19883
+ function stripComments(sql11) {
19884
+ return sql11.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/--[^\n]*/g, " ");
19879
19885
  }
19880
- function isMutation(sql10) {
19881
- const stripped = stripComments(sql10);
19886
+ function isMutation(sql11) {
19887
+ const stripped = stripComments(sql11);
19882
19888
  if (MUTATION_PATTERN.test(stripped)) return true;
19883
19889
  return /\bSELECT\b[\s\S]+\bINTO\s+\w/i.test(stripped);
19884
19890
  }
@@ -21992,13 +21998,13 @@ function logSpawnedSession(session) {
21992
21998
  }
21993
21999
 
21994
22000
  // src/shared/db/recordPhaseActiveMs.ts
21995
- import { sql as sql8 } from "drizzle-orm";
22001
+ import { sql as sql9 } from "drizzle-orm";
21996
22002
  async function recordPhaseActiveMs(db, itemId, phaseIdx, activeMs) {
21997
22003
  if (activeMs <= 0) return;
21998
22004
  await db.insert(phaseUsage).values({ itemId, phaseIdx, activeMs }).onConflictDoUpdate({
21999
22005
  target: [phaseUsage.itemId, phaseUsage.phaseIdx],
22000
22006
  set: {
22001
- activeMs: sql8`${phaseUsage.activeMs} + excluded.active_ms`
22007
+ activeMs: sql9`${phaseUsage.activeMs} + excluded.active_ms`
22002
22008
  }
22003
22009
  });
22004
22010
  }
@@ -22141,15 +22147,15 @@ function makeStatusChangeHandler(dismiss, notify2, reuseForRun) {
22141
22147
  }
22142
22148
 
22143
22149
  // src/shared/db/recordPhaseTokens.ts
22144
- import { sql as sql9 } from "drizzle-orm";
22150
+ import { sql as sql10 } from "drizzle-orm";
22145
22151
  async function recordPhaseTokens(db, itemId, phaseIdx, totalIn, totalOut) {
22146
22152
  await db.insert(phaseUsage).values({ itemId, phaseIdx, lastTotalIn: totalIn, lastTotalOut: totalOut }).onConflictDoUpdate({
22147
22153
  target: [phaseUsage.itemId, phaseUsage.phaseIdx],
22148
22154
  set: {
22149
- tokensDown: sql9`${phaseUsage.tokensDown} + CASE WHEN ${phaseUsage.lastTotalIn} IS NULL THEN 0 ELSE GREATEST(excluded.last_total_in - ${phaseUsage.lastTotalIn}, 0) END`,
22150
- tokensUp: sql9`${phaseUsage.tokensUp} + CASE WHEN ${phaseUsage.lastTotalOut} IS NULL THEN 0 ELSE GREATEST(excluded.last_total_out - ${phaseUsage.lastTotalOut}, 0) END`,
22151
- lastTotalIn: sql9`excluded.last_total_in`,
22152
- lastTotalOut: sql9`excluded.last_total_out`
22155
+ tokensDown: sql10`${phaseUsage.tokensDown} + CASE WHEN ${phaseUsage.lastTotalIn} IS NULL THEN 0 ELSE GREATEST(excluded.last_total_in - ${phaseUsage.lastTotalIn}, 0) END`,
22156
+ tokensUp: sql10`${phaseUsage.tokensUp} + CASE WHEN ${phaseUsage.lastTotalOut} IS NULL THEN 0 ELSE GREATEST(excluded.last_total_out - ${phaseUsage.lastTotalOut}, 0) END`,
22157
+ lastTotalIn: sql10`excluded.last_total_in`,
22158
+ lastTotalOut: sql10`excluded.last_total_out`
22153
22159
  }
22154
22160
  });
22155
22161
  }
@@ -22191,10 +22197,10 @@ function applyReviewPause(session, activity2) {
22191
22197
  }
22192
22198
 
22193
22199
  // src/shared/db/getPhaseActiveMs.ts
22194
- import { and as and19, eq as eq38 } from "drizzle-orm";
22200
+ import { and as and19, eq as eq37 } from "drizzle-orm";
22195
22201
  async function getPhaseActiveMs(db, itemId, phaseIdx) {
22196
22202
  const [row] = await db.select({ activeMs: phaseUsage.activeMs }).from(phaseUsage).where(
22197
- and19(eq38(phaseUsage.itemId, itemId), eq38(phaseUsage.phaseIdx, phaseIdx))
22203
+ and19(eq37(phaseUsage.itemId, itemId), eq37(phaseUsage.phaseIdx, phaseIdx))
22198
22204
  );
22199
22205
  return row?.activeMs ?? 0;
22200
22206
  }