@nanobpm/nano-workforce 0.46.0 → 0.46.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.46.1](https://github.com/nanobpm/nano-workforce/compare/v0.46.0...v0.46.1) (2026-08-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **trial-merge:** durable "needs attention" resolution + robust escalation key ([#131](https://github.com/nanobpm/nano-workforce/issues/131)) ([71200e8](https://github.com/nanobpm/nano-workforce/commit/71200e814a8c035a79f1eec37303781c70b6e6c0))
7
+
1
8
  # [0.46.0](https://github.com/nanobpm/nano-workforce/compare/v0.45.0...v0.46.0) (2026-08-12)
2
9
 
3
10
 
package/app/plan.test.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  // planner could revise forever. `positiveIntEnv` must fall back to the default on any value that
6
6
  // is not a positive integer, so the loop is always bounded.
7
7
  import { test } from "node:test";
8
- import { assertEquals, assertThrows } from "#test-assert";
8
+ import { assertEquals, assertRejects, assertThrows } from "#test-assert";
9
9
  import { positiveIntEnv } from "./plan.ts";
10
10
 
11
11
  const KEY = "NANO_PLAN_REVIEW_ROUNDS_TEST";
@@ -303,6 +303,64 @@ test("answerTaskEscalation is a no-op when no open escalation matches the correl
303
303
  assertEquals(r.ok, false);
304
304
  });
305
305
 
306
+ // Red/green regression (PR #131 suppressed advisory, app/plan.ts:455).
307
+ //
308
+ // Clearing a trial-merge wave's "Needs attention" row (`resolveTrialMergeAttention`)
309
+ // is a best-effort cosmetic cleanup, but it must be RETRIABLE: if it ran only AFTER
310
+ // the escalation was committed as `answered` and the resume message was published,
311
+ // a transient DB error there would 500 the whole answer flow while the escalation is
312
+ // already answered/resumed — a retry then 404s (no open escalation) and the red row
313
+ // is pinned forever (the very failure the insert-first ordering elsewhere avoids).
314
+ // The fix runs the idempotent resolution BEFORE the commit/publish, so a failure
315
+ // leaves the escalation OPEN and nothing is orphaned — the caller can safely retry.
316
+ test("answerTaskEscalation stays retriable (escalation open, no orphaned resume) when clearing 'Needs attention' fails", async () => {
317
+ const stores = escalationStores([
318
+ {
319
+ id: 1,
320
+ plan_key: "owner/repo#9",
321
+ task_id: "trial-merge-wave-0",
322
+ corr_key: "owner/repo#9:trial-merge-wave-0",
323
+ question: "Q",
324
+ status: "open",
325
+ answer: null,
326
+ },
327
+ ]);
328
+ stores.plan_trial_merges = {
329
+ rows: [{ id: 100, plan_key: "owner/repo#9", wave: 0, resolved: 0 }],
330
+ key: "id",
331
+ };
332
+ const base = memData(stores);
333
+ // Inject a transient failure in the trial-merge audit table's `update` only.
334
+ const data = {
335
+ table: (name: string, key: string) => {
336
+ const t = base.table(name, key);
337
+ if (name === "plan_trial_merges") {
338
+ return { ...t, update: () => Promise.reject(new Error("transient DB error")) };
339
+ }
340
+ return t;
341
+ },
342
+ } as any;
343
+
344
+ const published: any[] = [];
345
+ const engine = {
346
+ publishMessage: (m: any) => {
347
+ published.push(m);
348
+ return Promise.resolve();
349
+ },
350
+ } as any;
351
+
352
+ await assertRejects(() =>
353
+ answerTaskEscalation(data, engine, "owner/repo#9:trial-merge-wave-0", "proceed")
354
+ );
355
+
356
+ // Escalation must remain OPEN so a retry can recover (never committed as answered).
357
+ const esc = stores.plan_escalations.rows.find((x: any) => x.id === 1) as any;
358
+ assertEquals(esc.status, "open");
359
+ assertEquals(esc.answer, null);
360
+ // No orphaned resume message was published.
361
+ assertEquals(published.length, 0);
362
+ });
363
+
306
364
  test("currentPlanReviewEpoch counts answered plan-review escalations only", async () => {
307
365
  const stores = {
308
366
  plan_review_escalations: {
package/app/plan.ts CHANGED
@@ -13,6 +13,7 @@ import type { DataLayer, EngineClient } from "@nanobpm/urban";
13
13
  import { blackboardUrl, mintBlackboardToken, renderCoordinationBrief } from "./blackboard.ts";
14
14
  import { clearExclusions } from "./mergeExclusion.ts";
15
15
  import { clearTaskDeltas } from "./taskDelta.ts";
16
+ import { resolveTrialMergeAttention, trialMergeWaveFromTaskId } from "./trialMerge.ts";
16
17
 
17
18
  /** The BPMN process this module drives (resources/processes/plan-fanout.bpmn). */
18
19
  export const PLAN_PROCESS_ID = "plan-fanout";
@@ -433,6 +434,20 @@ export async function answerTaskEscalation(
433
434
  .sort((a, b) => b.id - a.id)[0];
434
435
  if (!open) return { ok: false, reason: "no open escalation" };
435
436
  const ts = now();
437
+ // A trial-merge escalation (task_id `trial-merge-wave-<wave>`) leaves an
438
+ // append-only red audit row in `plan_trial_merges`. Answering it clears that
439
+ // row from the page's "Needs attention" tab — including a "proceed" override
440
+ // that records no re-run row (a re-run would supersede it, but a proceed would
441
+ // not, pinning the red row forever).
442
+ //
443
+ // Resolve it FIRST, before the escalation is committed as answered and the
444
+ // resume message is published. `resolveTrialMergeAttention` is idempotent, so
445
+ // if this throws (e.g. a transient DB error) the escalation is still open and
446
+ // the whole operation retries cleanly. Running it AFTER the commit/publish
447
+ // would make a failure here unrecoverable: the escalation is already answered,
448
+ // a retry 404s (no open escalation), and the red row is pinned forever.
449
+ const trialWave = trialMergeWaveFromTaskId(open.task_id);
450
+ if (trialWave != null) await resolveTrialMergeAttention(data, open.plan_key, trialWave);
436
451
  await planEscalations(data).update(open.id, { answer, status: "answered", answered_at: ts });
437
452
  // Mirror onto the task row so a re-dispatched agent (and the UI) sees the answer.
438
453
  for (const t of await planTasks(data).find({ plan_key: open.plan_key, task_id: open.task_id })) {
@@ -1,6 +1,12 @@
1
1
  import { test } from "node:test";
2
- import { assertEquals } from "#test-assert";
3
- import { shouldRunTrialMerge, trialMergeDecision } from "./trialMerge.ts";
2
+ import { assertEquals, assertRejects } from "#test-assert";
3
+ import {
4
+ recordTrialMergeAudit,
5
+ resolveTrialMergeAttention,
6
+ shouldRunTrialMerge,
7
+ trialMergeDecision,
8
+ trialMergeWaveFromTaskId,
9
+ } from "./trialMerge.ts";
4
10
 
5
11
  test("trialMergeDecision only escalates clean-merge suite failures", () => {
6
12
  assertEquals(trialMergeDecision("clean"), "proceed");
@@ -14,3 +20,89 @@ test("shouldRunTrialMerge skips lone heads and mergify queues", () => {
14
20
  assertEquals(shouldRunTrialMerge(2, { land: { method: "mergify-queue" } }), false);
15
21
  assertEquals(shouldRunTrialMerge(2, { land: { method: "gh-merge" } }), true);
16
22
  });
23
+
24
+ // In-memory `plan_trial_merges` table backing the audit-resolution tests.
25
+ function memData() {
26
+ const rows: any[] = [];
27
+ let nextId = 1;
28
+ const table = {
29
+ async insert(row: any) {
30
+ const r = { id: nextId++, ...row };
31
+ rows.push(r);
32
+ return r.id;
33
+ },
34
+ async find(where: any = {}) {
35
+ return rows.filter((r) => Object.entries(where).every(([k, v]) => r[k] === v)).map((r) => ({ ...r }));
36
+ },
37
+ async update(id: any, patch: any) {
38
+ const r = rows.find((x) => x.id === id);
39
+ if (r) Object.assign(r, patch);
40
+ },
41
+ };
42
+ const data = { table: () => table } as any;
43
+ return { data, rows, table };
44
+ }
45
+
46
+ test("recordTrialMergeAudit supersedes prior rows for the same wave", async () => {
47
+ const { data, rows } = memData();
48
+ // Wave 1 fails, then re-runs clean; wave 2 is independent.
49
+ const first = await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "suite-failed" });
50
+ await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 2, result: "suite-failed" });
51
+ const rerun = await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "clean" });
52
+
53
+ const byId = (id: number) => rows.find((r) => r.id === id);
54
+ assertEquals(byId(first).resolved, 1, "the superseded wave-1 red row is resolved");
55
+ assertEquals(byId(rerun).resolved, 0, "the fresh wave-1 row stays unresolved");
56
+ // The unrelated wave-2 row is untouched (still needs attention).
57
+ assertEquals(rows.filter((r) => r.wave === 2)[0].resolved, 0);
58
+ });
59
+
60
+ test("recordTrialMergeAudit updates a re-reporting job in place without superseding", async () => {
61
+ const { data, rows } = memData();
62
+ const id = await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "suite-failed", jobKey: "j1" });
63
+ const again = await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "clean", jobKey: "j1" });
64
+ assertEquals(again, id, "the same job_key updates its row in place");
65
+ assertEquals(rows.length, 1, "no duplicate/supersede row is created");
66
+ assertEquals(rows[0].result, "clean");
67
+ assertEquals(rows[0].resolved, 0);
68
+ });
69
+
70
+ test("recordTrialMergeAudit keeps the wave flagged if the superseding insert fails", async () => {
71
+ const { data, rows, table } = memData();
72
+ await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "suite-failed" });
73
+ // Simulate a crash/failure on the superseding insert. The new row must be
74
+ // inserted BEFORE prior rows are resolved, so a failure here must not leave
75
+ // the wave with zero unresolved rows (which would silently clear "Needs
76
+ // attention").
77
+ table.insert = async () => {
78
+ throw new Error("insert failed");
79
+ };
80
+ await assertRejects(() => recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "clean" }));
81
+ const unresolved = rows.filter((r) => r.wave === 1 && r.resolved !== 1);
82
+ assertEquals(unresolved.length, 1, "the wave still has an unresolved row after the failed insert");
83
+ });
84
+
85
+ test("resolveTrialMergeAttention clears every unresolved row for the wave", async () => {
86
+ const { data, rows } = memData();
87
+ await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 1, result: "suite-failed" });
88
+ await recordTrialMergeAudit(data, { planKey: "o/r#1", wave: 3, result: "suite-failed" });
89
+ const cleared = await resolveTrialMergeAttention(data, "o/r#1", 1);
90
+ assertEquals(cleared, 1);
91
+ assertEquals(rows.filter((r) => r.wave === 1)[0].resolved, 1);
92
+ assertEquals(rows.filter((r) => r.wave === 3)[0].resolved, 0, "another wave is untouched");
93
+ // Idempotent: a second call resolves nothing new.
94
+ assertEquals(await resolveTrialMergeAttention(data, "o/r#1", 1), 0);
95
+ });
96
+
97
+ test("trialMergeWaveFromTaskId parses only trial-merge task ids", () => {
98
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-2"), 2);
99
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-0"), 0);
100
+ assertEquals(trialMergeWaveFromTaskId("some-feature-task"), null);
101
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-x"), null);
102
+ // Empty suffix must not silently map to wave 0 (Number("") === 0).
103
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-"), null);
104
+ // Non-integer / signed / whitespace suffixes are rejected.
105
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-1.5"), null);
106
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-+2"), null);
107
+ assertEquals(trialMergeWaveFromTaskId("trial-merge-wave-12"), 12);
108
+ });
package/app/trialMerge.ts CHANGED
@@ -28,6 +28,7 @@ export interface TrialMergeAuditRow {
28
28
  failing: string | null;
29
29
  summary: string | null;
30
30
  job_key: string | null;
31
+ resolved: number;
31
32
  created_at: string;
32
33
  updated_at: string;
33
34
  }
@@ -46,6 +47,17 @@ export function trialMergeTaskId(wave: number): string {
46
47
  return `${TRIAL_MERGE_TASK_PREFIX}${Math.max(0, Math.trunc(wave))}`;
47
48
  }
48
49
 
50
+ /** Inverse of {@link trialMergeTaskId}: the wave a trial-merge escalation
51
+ * `task_id` refers to, or `null` when `taskId` is not a trial-merge escalation
52
+ * (e.g. an ordinary feature escalation). */
53
+ export function trialMergeWaveFromTaskId(taskId: string): number | null {
54
+ if (!taskId.startsWith(TRIAL_MERGE_TASK_PREFIX)) return null;
55
+ const suffix = taskId.slice(TRIAL_MERGE_TASK_PREFIX.length);
56
+ if (!/^\d+$/.test(suffix)) return null;
57
+ const wave = Number(suffix);
58
+ return Number.isInteger(wave) && wave >= 0 ? wave : null;
59
+ }
60
+
49
61
  const auditTable = (data: DataLayer) => data.table<TrialMergeAuditRow>("plan_trial_merges", "id");
50
62
 
51
63
  function jsonOrNull(v: unknown): string | null {
@@ -76,6 +88,8 @@ export async function recordTrialMergeAudit(
76
88
  if (jobKey) {
77
89
  const existing = (await table.find({ plan_key: row.planKey, job_key: jobKey })).sort((a, b) => b.id - a.id)[0];
78
90
  if (existing) {
91
+ // Same job re-reporting (a retry before its wait subscription opened):
92
+ // update in place — it is the same logical attempt, not a supersede.
79
93
  await table.update(existing.id, {
80
94
  result: row.result,
81
95
  heads: jsonOrNull(row.heads),
@@ -87,16 +101,57 @@ export async function recordTrialMergeAudit(
87
101
  return existing.id;
88
102
  }
89
103
  }
90
- return Number(await table.insert({
91
- plan_key: row.planKey,
92
- wave: row.wave,
93
- result: row.result,
94
- heads: jsonOrNull(row.heads),
95
- conflicts: jsonOrNull(row.conflicts),
96
- failing: jsonOrNull(row.failing),
97
- summary: row.summary ?? null,
98
- job_key: jobKey,
99
- created_at: ts,
100
- updated_at: ts,
101
- }));
104
+ // A fresh audit row for this wave supersedes every prior row for the same
105
+ // (plan_key, wave): those are now history, so mark them resolved. Without this
106
+ // the append-only log leaves an old red row in the page's "Needs attention"
107
+ // tab forever, even after the wave was re-run clean (issue: the tab never
108
+ // cleared). The newly-inserted row defaults `resolved = 0`, so a still-red
109
+ // latest attempt keeps showing until it too is superseded or answered.
110
+ //
111
+ // Insert the new (unresolved) row FIRST, then resolve the older rows — never
112
+ // the reverse. Resolving priors before the insert would leave the wave with
113
+ // zero unresolved rows if the insert (or the process) failed in between,
114
+ // silently clearing the "Needs attention" tab. Insert-first guarantees the
115
+ // wave always has at least one unresolved row through the transition.
116
+ const id = Number(
117
+ await table.insert({
118
+ plan_key: row.planKey,
119
+ wave: row.wave,
120
+ result: row.result,
121
+ heads: jsonOrNull(row.heads),
122
+ conflicts: jsonOrNull(row.conflicts),
123
+ failing: jsonOrNull(row.failing),
124
+ summary: row.summary ?? null,
125
+ job_key: jobKey,
126
+ resolved: 0,
127
+ created_at: ts,
128
+ updated_at: ts,
129
+ }),
130
+ );
131
+ for (const prior of await table.find({ plan_key: row.planKey, wave: row.wave })) {
132
+ if (prior.id !== id && prior.resolved !== 1) await table.update(prior.id, { resolved: 1, updated_at: ts });
133
+ }
134
+ return id;
135
+ }
136
+
137
+ /** Mark every trial-merge audit row for `(planKey, wave)` resolved, so the epic
138
+ * page's "Needs attention" tab stops surfacing it. Called when the wave's trial
139
+ * escalation is answered — including a "proceed" override that records no
140
+ * re-run row and so would otherwise leave the old red row pinned forever.
141
+ * Returns the number of rows newly resolved. */
142
+ export async function resolveTrialMergeAttention(
143
+ data: DataLayer,
144
+ planKey: string,
145
+ wave: number,
146
+ ): Promise<number> {
147
+ const table = auditTable(data);
148
+ const ts = now();
149
+ let resolved = 0;
150
+ for (const r of await table.find({ plan_key: planKey, wave })) {
151
+ if (r.resolved !== 1) {
152
+ await table.update(r.id, { resolved: 1, updated_at: ts });
153
+ resolved++;
154
+ }
155
+ }
156
+ return resolved;
102
157
  }
@@ -0,0 +1,44 @@
1
+ -- Durable "needs attention" resolution for the trial-merge audit log (issue: the
2
+ -- epic page's "Needs attention" tab never cleared).
3
+ --
4
+ -- `plan_trial_merges` is an append-only audit trail: a re-run after a suite
5
+ -- failure INSERTs a fresh row but never supersedes the old red one, so a
6
+ -- `merge-conflict`/`suite-failed` row stayed in "Needs attention" forever even
7
+ -- after the escalation was answered and the wave re-run clean. Add an explicit
8
+ -- `resolved` flag so the page can hide history, and backfill it for existing
9
+ -- rows. Going forward `recordTrialMergeAudit` marks prior same-(plan,wave) rows
10
+ -- resolved on each new insert (supersede-on-insert).
11
+ --
12
+ -- NB: the migration runner wraps each file in its own transaction — this file
13
+ -- must NOT contain BEGIN/COMMIT.
14
+
15
+ ALTER TABLE plan_trial_merges ADD COLUMN resolved INTEGER NOT NULL DEFAULT 0;
16
+
17
+ -- Backfill 1 (supersede): any audit row that has a NEWER row (higher id) for the
18
+ -- same (plan_key, wave) is superseded history — resolve it. The newest row per
19
+ -- wave stays unresolved so a still-red latest attempt keeps showing.
20
+ UPDATE plan_trial_merges
21
+ SET resolved = 1
22
+ WHERE EXISTS (
23
+ SELECT 1 FROM plan_trial_merges AS newer
24
+ WHERE newer.plan_key = plan_trial_merges.plan_key
25
+ AND newer.wave = plan_trial_merges.wave
26
+ AND newer.id > plan_trial_merges.id
27
+ );
28
+
29
+ -- Backfill 2 (answered): a red (needs-attention) row whose trial escalation has
30
+ -- already been answered is resolved — even if no re-run row was ever recorded
31
+ -- (e.g. the operator answered "proceed"/override). The trial escalation's
32
+ -- task_id is 'trial-merge-wave-<wave>' (see app/trialMerge.ts trialMergeTaskId).
33
+ UPDATE plan_trial_merges
34
+ SET resolved = 1
35
+ WHERE result IN ('merge-conflict', 'suite-failed')
36
+ AND EXISTS (
37
+ SELECT 1 FROM plan_escalations AS e
38
+ WHERE e.plan_key = plan_trial_merges.plan_key
39
+ AND e.task_id = 'trial-merge-wave-' || plan_trial_merges.wave
40
+ AND e.status = 'answered'
41
+ );
42
+
43
+ CREATE INDEX IF NOT EXISTS idx_plan_trial_merges_attention
44
+ ON plan_trial_merges(plan_key, resolved, result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/nano-workforce",
3
- "version": "0.46.0",
3
+ "version": "0.46.1",
4
4
  "description": "Nano Workforce — an Agent Graph Orchestration application for Agentic SDLC: durable BPMN processes that coordinate a graph of AI agents across the software delivery lifecycle.",
5
5
  "type": "module",
6
6
  "main": "main.ts",
@@ -273,12 +273,18 @@
273
273
  "source": "app",
274
274
  "table": "plan_trial_merges",
275
275
  "orderBy": { "field": "created_at", "dir": "desc" },
276
- "filter": [{ "field": "result", "in": ["merge-conflict", "suite-failed"] }]
276
+ "filter": [
277
+ { "field": "result", "in": ["merge-conflict", "suite-failed"] },
278
+ { "field": "resolved", "in": [0] }
279
+ ]
277
280
  },
278
281
  "tabs": [
279
282
  {
280
283
  "label": "Needs attention",
281
- "filter": [{ "field": "result", "in": ["merge-conflict", "suite-failed"] }]
284
+ "filter": [
285
+ { "field": "result", "in": ["merge-conflict", "suite-failed"] },
286
+ { "field": "resolved", "in": [0] }
287
+ ]
282
288
  },
283
289
  {
284
290
  "label": "Clean",
@@ -2,7 +2,7 @@
2
2
  <bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" id="Definitions_nano_workforce_plan" targetNamespace="http://nanobpm.io/nano-workforce">
3
3
  <bpmn:message id="Message_featureEscalationAnswered" name="feature-escalation-answered">
4
4
  <bpmn:extensionElements>
5
- <zeebe:subscription correlationKey="=planKey + &#34;:&#34; + task.id" />
5
+ <zeebe:subscription correlationKey="=escalationCorrKey" />
6
6
  </bpmn:extensionElements>
7
7
  </bpmn:message>
8
8
  <bpmn:message id="Message_planEscalationAnswered" name="plan-escalation-answered">
@@ -36,6 +36,7 @@ interface In extends Record<string, unknown> {
36
36
  }
37
37
  interface Out extends Record<string, unknown> {
38
38
  escalationId: number;
39
+ escalationCorrKey: string;
39
40
  }
40
41
 
41
42
  // A non-blank trimmed string, else undefined. A blank question/PR must not reach
@@ -106,7 +107,14 @@ const handler: AppJobHandler<In, Out> = async (job, app) => {
106
107
  }
107
108
 
108
109
  await refreshOpenTaskEscalation(app.data, planKey);
109
- return { escalationId };
110
+ // Emit the correlation key as a single scalar so the downstream
111
+ // `feature-escalation-answered` catch subscribes on `=escalationCorrKey`
112
+ // (freshly set, in scope) rather than re-deriving `=planKey + ":" + task.id`.
113
+ // The concatenation form errored to an empty, unmatchable key whenever `task`
114
+ // was not a Map at subscription-open, parking the token forever (see the
115
+ // engine incident fix). The value is identical to `featureCorrKey(planKey,
116
+ // taskId)`, so the app's answer-publish path still matches.
117
+ return { escalationId, escalationCorrKey: corrKey };
110
118
  };
111
119
 
112
120
  export default handler;