@nanobpm/nano-workforce 0.210.1 → 0.211.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/CHANGELOG.md +12 -0
- package/app/abandon.test.ts +31 -0
- package/app/abandon.ts +8 -2
- package/app/cliExtension.test.ts +40 -0
- package/app/conformance.test.ts +17 -0
- package/app/loopTermination.test.ts +4 -0
- package/app/migration139.test.ts +115 -0
- package/app/prTrackingReadModel.test.ts +175 -0
- package/app/prTrackingReadModel.ts +152 -0
- package/app/retro.test.ts +11 -0
- package/app/service.ts +11 -3
- package/app/terminalReaderBehaviour.test.ts +44 -0
- package/build-info.json +4 -4
- package/db/migrations/139_pull_requests_backfill_merged_not_abandoned.sql +40 -0
- package/main.ts +6 -0
- package/openapi.yaml +123 -0
- package/package.json +2 -2
- package/test/trackingViews.ts +16 -12
- package/workers/reconverge/worker.test.ts +137 -8
- package/workers/reconverge/worker.ts +53 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## [0.211.0](https://github.com/nanobpm/nano-workforce/compare/v0.210.2...v0.211.0) (2026-11-14)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* **openapi:** add x-cli command projection to every operation ([#963](https://github.com/nanobpm/nano-workforce/issues/963)) ([4ba5c8c](https://github.com/nanobpm/nano-workforce/commit/4ba5c8c61e6d5bfceaff3883c1719795797eb9a4)), closes [#961](https://github.com/nanobpm/nano-workforce/issues/961) [#961](https://github.com/nanobpm/nano-workforce/issues/961)
|
|
6
|
+
|
|
7
|
+
## [0.210.2](https://github.com/nanobpm/nano-workforce/compare/v0.210.1...v0.210.2) (2026-11-14)
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **tracking:** merged PRs no longer derive 'abandoned' when their run was terminated ([#960](https://github.com/nanobpm/nano-workforce/issues/960)) ([56f0ff4](https://github.com/nanobpm/nano-workforce/commit/56f0ff4c4a3a0d8a3cf68745b1f7b9bf539987da)), closes [nano-bpm#1579](https://github.com/nanobpm/nano-bpm/issues/1579) [#959](https://github.com/nanobpm/nano-workforce/issues/959)
|
|
12
|
+
|
|
1
13
|
## [0.210.1](https://github.com/nanobpm/nano-workforce/compare/v0.210.0...v0.210.1) (2026-11-12)
|
|
2
14
|
|
|
3
15
|
### Bug Fixes
|
package/app/abandon.test.ts
CHANGED
|
@@ -134,6 +134,37 @@ test("abandonStatusForToken derives abandoned from the row status", async () =>
|
|
|
134
134
|
assertEquals(await abandonStatusForToken(data, "nope"), undefined);
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
+
test("abandonStatusForToken aborts a MERGED PR whose owning run was terminated (owner_terminated)", async () => {
|
|
138
|
+
// derived_status reports a landed PR as `merged` even when its run was terminated (#959), so the
|
|
139
|
+
// cancellation signal rides the separate owner_terminated column — terminating a redundant
|
|
140
|
+
// post-merge round must still tell its agent to stop.
|
|
141
|
+
const data = memData();
|
|
142
|
+
await data.table("pull_requests", "pr_key").insert({
|
|
143
|
+
pr_key: "o/r#4",
|
|
144
|
+
abandon_token: "landed-term",
|
|
145
|
+
status: "merged",
|
|
146
|
+
merged_at: "t",
|
|
147
|
+
owner_terminated: 1,
|
|
148
|
+
});
|
|
149
|
+
await data.table("pull_requests", "pr_key").insert({
|
|
150
|
+
pr_key: "o/r#5",
|
|
151
|
+
abandon_token: "landed-live",
|
|
152
|
+
status: "merged",
|
|
153
|
+
merged_at: "t",
|
|
154
|
+
owner_terminated: null,
|
|
155
|
+
});
|
|
156
|
+
assertEquals(await abandonStatusForToken(data, "landed-term"), {
|
|
157
|
+
prKey: "o/r#4",
|
|
158
|
+
status: "merged",
|
|
159
|
+
abandoned: true,
|
|
160
|
+
});
|
|
161
|
+
assertEquals(await abandonStatusForToken(data, "landed-live"), {
|
|
162
|
+
prKey: "o/r#5",
|
|
163
|
+
status: "merged",
|
|
164
|
+
abandoned: false,
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
137
168
|
test("abandonStatusForToken fails CLOSED on a non-string derived status", async () => {
|
|
138
169
|
// A malformed/missing derived_status must never be reported as `abandoned:false`
|
|
139
170
|
// (that would let a cancelled run proceed with irreversible side effects). It throws
|
package/app/abandon.ts
CHANGED
|
@@ -24,12 +24,15 @@
|
|
|
24
24
|
import type { DataLayer } from "@nanobpm/urban";
|
|
25
25
|
import { publicBaseUrl } from "./blackboard.ts";
|
|
26
26
|
import { trackingTargetFor } from "./instanceTracking.ts";
|
|
27
|
+
import { OWNER_TERMINATED_COLUMN } from "./prTrackingReadModel.ts";
|
|
27
28
|
|
|
28
29
|
/** The one derived-status value meaning a PR is terminally abandoned — the run must not be worked on
|
|
29
30
|
* further. Two disjoint producers surface a row here, and both are non-completion terminals that must
|
|
30
31
|
* stop a servicing agent:
|
|
31
32
|
* 1. an explicit **cancel** of a live convergence/merge run (Urban's cancel primitive terminates
|
|
32
|
-
* the instance; the `instanceTracking` `onTerminated.set` edge derives `abandoned` on read)
|
|
33
|
+
* the instance; the `instanceTracking` `onTerminated.set` edge derives `abandoned` on read) —
|
|
34
|
+
* but ONLY when the PR did NOT land: the merge-aware derivation (app/prTrackingReadModel.ts,
|
|
35
|
+
* issue #959) folds a terminated-but-MERGED PR to `merged`, never `abandoned`, and
|
|
33
36
|
* 2. **`abandonClosedPr`** reconciling a wave-member PR that was **closed on GitHub without
|
|
34
37
|
* merging** (#352) — for both `pull_requests` and its `plan_tasks` — by writing the base row.
|
|
35
38
|
* Convergence/merge terminal states `converged`/`merged` are NOT abandonment. In either abandoned
|
|
@@ -116,7 +119,10 @@ export async function abandonStatusForToken(
|
|
|
116
119
|
);
|
|
117
120
|
}
|
|
118
121
|
const status = rawStatus;
|
|
119
|
-
|
|
122
|
+
// `derived_status` reports a landed PR as `merged` even when its run was terminated (issue #959),
|
|
123
|
+
// so termination is read from its own column: a terminated owner ALWAYS aborts the agent.
|
|
124
|
+
const ownerTerminated = Boolean(row[OWNER_TERMINATED_COLUMN]);
|
|
125
|
+
return { prKey: row.pr_key, status, abandoned: isAbandoned(status) || ownerTerminated };
|
|
120
126
|
}
|
|
121
127
|
|
|
122
128
|
/** The instruction block appended (verbatim, via `appendPrompt`) to each side-effecting agent's
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Authoring guard for the `x-cli` CLI command-projection extension (issue #961, nano-ide#617).
|
|
2
|
+
//
|
|
3
|
+
// `d8ctl nwf …` projects every `openapi.yaml` operation into a CLI command from its `x-cli`. The
|
|
4
|
+
// spec is parsed by the SAME `@nanobpm/urban` walker the runtime uses (`parseSpec` +
|
|
5
|
+
// `collectOperations`), which validates `x-cli` (unknown keys, kebab-case words, duplicate command
|
|
6
|
+
// paths) and exposes it as `op.cli` — no second parser to drift from it.
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { test } from "node:test";
|
|
11
|
+
import { collectOperations, parseSpec } from "@nanobpm/urban/toolkit";
|
|
12
|
+
import { assert } from "#test-assert";
|
|
13
|
+
|
|
14
|
+
const SPEC_PATH = join(dirname(dirname(fileURLToPath(import.meta.url))), "openapi.yaml");
|
|
15
|
+
const ops = () => collectOperations(parseSpec(readFileSync(SPEC_PATH, "utf8")));
|
|
16
|
+
|
|
17
|
+
test("every operation declares x-cli (a deliberate command or an explicit exclude)", () => {
|
|
18
|
+
const missing = ops().filter((op) => op.cli === undefined).map((op) => op.operationId);
|
|
19
|
+
assert(missing.length === 0, `operations without x-cli: ${missing.join(", ")}`);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("every x-mcp-excluded operation is operator-audience or CLI-excluded", () => {
|
|
23
|
+
const leaked = ops()
|
|
24
|
+
.filter((op) => op.mcpExcluded && !(op.cli?.exclude === true || op.cli?.audience === "operator"))
|
|
25
|
+
.map((op) => op.operationId);
|
|
26
|
+
assert(leaked.length === 0, `x-mcp-excluded but CLI-visible to agents: ${leaked.join(", ")}`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("the /hooks/* side channels are excluded from the CLI", () => {
|
|
30
|
+
const hooks = ops().filter((op) => op.path.startsWith("/hooks/"));
|
|
31
|
+
assert(hooks.length > 0, "expected /hooks/* operations");
|
|
32
|
+
const exposed = hooks.filter((op) => op.cli?.exclude !== true).map((op) => op.operationId);
|
|
33
|
+
assert(exposed.length === 0, `hook side channels exposed as CLI commands: ${exposed.join(", ")}`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("dispatchDeliveryGraph is an operator-audience command", () => {
|
|
37
|
+
const op = ops().find((o) => o.operationId === "dispatchDeliveryGraph");
|
|
38
|
+
assert(op?.cli?.audience === "operator", "dispatchDeliveryGraph must be audience: operator");
|
|
39
|
+
assert(op?.cli?.command.join(" ") === "graph dispatch", "dispatchDeliveryGraph command");
|
|
40
|
+
});
|
package/app/conformance.test.ts
CHANGED
|
@@ -126,6 +126,23 @@ test("hasDeliveredImplementation: true iff at least one PR landed", async () =>
|
|
|
126
126
|
assert(hasDeliveredImplementation(await gatherConformance(data, PLAN)));
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
+
// Issue #959: a PR that MERGED but whose loop was terminated afterwards (operator cancel, out-of-band
|
|
130
|
+
// merge reconcile, restart cleanup) derived `abandoned` off the `onTerminated` edge — and conformance
|
|
131
|
+
// then refused to count it as landed. The merge-aware derivation folds it to `merged` (merged_at set),
|
|
132
|
+
// so a merged-then-terminated PR IS landed. Model the terminated divergence by seeding the base row at
|
|
133
|
+
// its frozen transient (`converging`) WITH `merged_at` set: the tracking view must report `merged`.
|
|
134
|
+
test("hasDeliveredImplementation: a merged-then-terminated PR (frozen base + merged_at) counts as landed (#959)", async () => {
|
|
135
|
+
const { data, stores } = memData();
|
|
136
|
+
seedPlan(stores);
|
|
137
|
+
seedTask(stores, { id: 1, task_index: 0, task_id: "t1", status: "opened", pr_key: "acme/widgets#10" });
|
|
138
|
+
// Base status frozen at the in-flight transient, but the PR landed (merged_at set) before termination.
|
|
139
|
+
stores["pull_requests"] = [{ pr_key: "acme/widgets#10", status: "converging", merged_at: "2026-11-01T00:00:00Z" }];
|
|
140
|
+
assertEquals(await hasDeliveredImplementationForPlan(data, PLAN), true);
|
|
141
|
+
const d = await gatherConformance(data, PLAN);
|
|
142
|
+
assertEquals(hasDeliveredImplementation(d), true);
|
|
143
|
+
assertEquals(d.slices.find((s) => s.taskId === "t1")?.landed, true);
|
|
144
|
+
});
|
|
145
|
+
|
|
129
146
|
test("hasDeliveredImplementationForPlan: matches the digest without a blackboard scan", async () => {
|
|
130
147
|
const { data, stores } = memData();
|
|
131
148
|
seedPlan(stores);
|
|
@@ -410,3 +410,7 @@ test("PR#944 round 6: a successor-owned converging row is NEVER demoted by a red
|
|
|
410
410
|
assertEquals(stores.pull_requests[0].process_key, "succ");
|
|
411
411
|
assertEquals(stores.pull_requests[0].retire_pending_key, "merge-1", "the pending retirement is left for retryPendingRetirements");
|
|
412
412
|
});
|
|
413
|
+
|
|
414
|
+
test("#959 isOrphanedPr: an audit-only landing (derived merged, base merging, no merged_at) is never orphaned", () => {
|
|
415
|
+
assert(!isOrphanedPr({ status: "merging", derived_status: "merged", termination_reason: null, merged_at: null }));
|
|
416
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Migration guard for 139_pull_requests_backfill_merged_not_abandoned.sql (issue #959): the backfill
|
|
2
|
+
// that re-derives the 23 merged-but-terminated PRs so they show as `merged`, not `abandoned`.
|
|
3
|
+
//
|
|
4
|
+
// Applies the migration to a real in-memory SQLite DB over a pre-existing `pull_requests` + `merges`
|
|
5
|
+
// shape and asserts the contract:
|
|
6
|
+
// * a row with a `merged` audit outcome but a non-`merged` base status (the loop terminated before
|
|
7
|
+
// `merged_at` was stamped) is settled to `status='merged'` + `merged_at` — so the merge-aware
|
|
8
|
+
// derivation reports `merged` however the instance ended;
|
|
9
|
+
// * a row already settled `merged` is left untouched (idempotent);
|
|
10
|
+
// * a row with NO `merged` audit outcome (a genuinely abandoned/closed-unmerged run) is NOT
|
|
11
|
+
// flipped — it stays at its frozen transient so the derivation still reports `abandoned`;
|
|
12
|
+
// * re-running the migration is a no-op (forward-only, idempotent).
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
import { DatabaseSync } from "node:sqlite";
|
|
15
|
+
import { test } from "node:test";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import { assertEquals } from "#test-assert";
|
|
18
|
+
|
|
19
|
+
const MIGRATION_139 = fileURLToPath(
|
|
20
|
+
new URL("../db/migrations/139_pull_requests_backfill_merged_not_abandoned.sql", import.meta.url),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
function migratedDb(): DatabaseSync {
|
|
24
|
+
const db = new DatabaseSync(":memory:");
|
|
25
|
+
db.exec(
|
|
26
|
+
`CREATE TABLE pull_requests (
|
|
27
|
+
pr_key TEXT PRIMARY KEY,
|
|
28
|
+
repo TEXT NOT NULL,
|
|
29
|
+
number INTEGER NOT NULL,
|
|
30
|
+
status TEXT NOT NULL,
|
|
31
|
+
process_key TEXT,
|
|
32
|
+
merged_at TEXT,
|
|
33
|
+
updated_at TEXT
|
|
34
|
+
);
|
|
35
|
+
CREATE TABLE merges (
|
|
36
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
37
|
+
pr_key TEXT NOT NULL,
|
|
38
|
+
outcome TEXT NOT NULL,
|
|
39
|
+
method TEXT,
|
|
40
|
+
detail TEXT,
|
|
41
|
+
at TEXT NOT NULL
|
|
42
|
+
);`,
|
|
43
|
+
);
|
|
44
|
+
return db;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function addPr(db: DatabaseSync, prKey: string, status: string, mergedAt: string | null = null): void {
|
|
48
|
+
db.prepare(
|
|
49
|
+
"INSERT INTO pull_requests (pr_key, repo, number, status, process_key, merged_at, updated_at) VALUES (?, 'o/r', 1, ?, 'PI-1', ?, 't0')",
|
|
50
|
+
).run(prKey, status, mergedAt);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function addMerge(db: DatabaseSync, prKey: string, outcome: string, at: string): void {
|
|
54
|
+
db.prepare("INSERT INTO merges (pr_key, outcome, method, detail, at) VALUES (?, ?, 'squash', 'd', ?)").run(prKey, outcome, at);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function row(db: DatabaseSync, prKey: string): { status: string; merged_at: string | null } {
|
|
58
|
+
return db.prepare("SELECT status, merged_at FROM pull_requests WHERE pr_key = ?").get(prKey) as {
|
|
59
|
+
status: string;
|
|
60
|
+
merged_at: string | null;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const SQL = () => readFileSync(MIGRATION_139, "utf8");
|
|
65
|
+
|
|
66
|
+
test("139 backfills a merged-but-terminated PR (frozen transient + merged audit) to status='merged' + merged_at", () => {
|
|
67
|
+
const db = migratedDb();
|
|
68
|
+
// The #959 case: PR landed (merged audit row) but the loop was terminated before merged_at was set,
|
|
69
|
+
// leaving the base status frozen at its in-flight transient.
|
|
70
|
+
addPr(db, "o/r#landed", "converging", null);
|
|
71
|
+
addMerge(db, "o/r#landed", "merged", "2026-11-01T02:03:04Z");
|
|
72
|
+
db.exec(SQL());
|
|
73
|
+
const r = row(db, "o/r#landed");
|
|
74
|
+
assertEquals(r.status, "merged");
|
|
75
|
+
assertEquals(r.merged_at, "2026-11-01T02:03:04Z", "merged_at is stamped from the earliest merged audit row");
|
|
76
|
+
db.close();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("139 leaves an already-merged row untouched (idempotent, never overwrites merged_at)", () => {
|
|
80
|
+
const db = migratedDb();
|
|
81
|
+
addPr(db, "o/r#already", "merged", "2026-10-30T00:00:00Z");
|
|
82
|
+
addMerge(db, "o/r#already", "merged", "2026-10-30T00:00:00Z");
|
|
83
|
+
db.exec(SQL());
|
|
84
|
+
const r = row(db, "o/r#already");
|
|
85
|
+
assertEquals(r.status, "merged");
|
|
86
|
+
assertEquals(r.merged_at, "2026-10-30T00:00:00Z");
|
|
87
|
+
db.close();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("139 does NOT flip a closed-unmerged / genuinely-abandoned PR (no merged audit outcome)", () => {
|
|
91
|
+
const db = migratedDb();
|
|
92
|
+
// A PR closed without merging: terminated → abandoned, NO merged audit row. Must stay un-merged.
|
|
93
|
+
addPr(db, "o/r#closed", "converging", null);
|
|
94
|
+
addMerge(db, "o/r#closed", "abandoned", "2026-11-01T00:00:00Z");
|
|
95
|
+
// A still-in-flight PR with no merge record at all.
|
|
96
|
+
addPr(db, "o/r#inflight", "waiting_merge", null);
|
|
97
|
+
db.exec(SQL());
|
|
98
|
+
assertEquals(row(db, "o/r#closed").status, "converging", "no merged outcome → not flipped to merged");
|
|
99
|
+
assertEquals(row(db, "o/r#closed").merged_at, null);
|
|
100
|
+
assertEquals(row(db, "o/r#inflight").status, "waiting_merge");
|
|
101
|
+
assertEquals(row(db, "o/r#inflight").merged_at, null);
|
|
102
|
+
db.close();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("139 is idempotent — a second run is a no-op", () => {
|
|
106
|
+
const db = migratedDb();
|
|
107
|
+
addPr(db, "o/r#landed", "waiting_merge", null);
|
|
108
|
+
addMerge(db, "o/r#landed", "merged", "2026-11-01T02:03:04Z");
|
|
109
|
+
db.exec(SQL());
|
|
110
|
+
const first = row(db, "o/r#landed");
|
|
111
|
+
db.exec(SQL());
|
|
112
|
+
const second = row(db, "o/r#landed");
|
|
113
|
+
assertEquals(second, first);
|
|
114
|
+
db.close();
|
|
115
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Issue #959 — the canonical "merged beats terminated" effective-status derivation for a PR.
|
|
2
|
+
//
|
|
3
|
+
// THE DEFECT: `pull_requests__tracking.derived_status` mapped ANY terminated run to `abandoned`
|
|
4
|
+
// without consulting the PR's merge state, so the 23 November PRs that MERGED but whose loops were
|
|
5
|
+
// terminated afterwards (operator cancel, out-of-band merge reconcile, restart cleanup) all read
|
|
6
|
+
// "abandoned". These tests pin the fixed semantics against the REAL derivation SQL
|
|
7
|
+
// (`pullRequestTrackingReadModel`, app/prTrackingReadModel.ts) compiled to an in-memory SQLite VIEW
|
|
8
|
+
// over the SAME canonical `urban_instance_state` projection the framework feeds:
|
|
9
|
+
//
|
|
10
|
+
// terminated + merged PR → `merged` (landed wins over terminated)
|
|
11
|
+
// terminated + closed-unmerged PR → `abandoned` (no merged_at → the onTerminated edge)
|
|
12
|
+
// terminated + open PR → `abandoned`
|
|
13
|
+
// live (non-terminated) run → base `status` passes through
|
|
14
|
+
//
|
|
15
|
+
// and the downstream consequence: conformance/retro count a merged-then-terminated PR as LANDED.
|
|
16
|
+
import { DatabaseSync } from "node:sqlite";
|
|
17
|
+
import { test } from "node:test";
|
|
18
|
+
import { registerCanonicalProjections, readModelRegistry } from "@nanobpm/urban";
|
|
19
|
+
import { assert, assertEquals } from "#test-assert";
|
|
20
|
+
import {
|
|
21
|
+
pullRequestTrackingReadModel,
|
|
22
|
+
registerPullRequestTrackingReadModel,
|
|
23
|
+
} from "./prTrackingReadModel.ts";
|
|
24
|
+
|
|
25
|
+
// The projection NAME→physical-table mapping the VIEW's `EXISTS` resolves through.
|
|
26
|
+
registerCanonicalProjections();
|
|
27
|
+
|
|
28
|
+
/** A minimal in-memory DB: the real `pull_requests` base columns the derivation reads (`status`,
|
|
29
|
+
* `process_key`, `merged_at`), the canonical `_urban_instance_state` projection, and the
|
|
30
|
+
* app-owned tracking VIEW compiled from the ONE declaration. */
|
|
31
|
+
function viewDb(): DatabaseSync {
|
|
32
|
+
const db = new DatabaseSync(":memory:");
|
|
33
|
+
db.exec(
|
|
34
|
+
`CREATE TABLE pull_requests (pr_key TEXT PRIMARY KEY, status TEXT, process_key TEXT, merged_at TEXT);
|
|
35
|
+
CREATE TABLE merges (id INTEGER PRIMARY KEY, pr_key TEXT, outcome TEXT, method TEXT, detail TEXT, at TEXT);
|
|
36
|
+
CREATE TABLE _urban_instance_state (process_instance_key TEXT PRIMARY KEY, state TEXT, waiting_on_human INTEGER NOT NULL DEFAULT 0, updated_at TEXT);`,
|
|
37
|
+
);
|
|
38
|
+
db.exec(pullRequestTrackingReadModel.viewDdl());
|
|
39
|
+
return db;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function addPr(
|
|
43
|
+
db: DatabaseSync,
|
|
44
|
+
prKey: string,
|
|
45
|
+
opts: { status: string; processKey: string | null; mergedAt: string | null },
|
|
46
|
+
): void {
|
|
47
|
+
db.prepare("INSERT INTO pull_requests (pr_key, status, process_key, merged_at) VALUES (?, ?, ?, ?)").run(
|
|
48
|
+
prKey,
|
|
49
|
+
opts.status,
|
|
50
|
+
opts.processKey,
|
|
51
|
+
opts.mergedAt,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function terminate(db: DatabaseSync, processKey: string): void {
|
|
56
|
+
db.prepare(
|
|
57
|
+
"INSERT INTO _urban_instance_state (process_instance_key, state, waiting_on_human, updated_at) VALUES (?, 'TERMINATED', 0, 't')",
|
|
58
|
+
).run(processKey);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function derivedStatus(db: DatabaseSync, prKey: string): string {
|
|
62
|
+
const r = db.prepare("SELECT derived_status FROM pull_requests__tracking WHERE pr_key = ?").get(prKey) as {
|
|
63
|
+
derived_status: string;
|
|
64
|
+
};
|
|
65
|
+
return r.derived_status;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
test("terminated instance + merged PR (merged_at set) derives `merged`, not `abandoned`", () => {
|
|
69
|
+
const db = viewDb();
|
|
70
|
+
// The #959 case: the loop was terminated AFTER the PR landed; base status frozen at its transient.
|
|
71
|
+
addPr(db, "o/r#merged", { status: "converging", processKey: "514213", mergedAt: "2026-11-01T00:00:00Z" });
|
|
72
|
+
terminate(db, "514213");
|
|
73
|
+
assertEquals(derivedStatus(db, "o/r#merged"), "merged");
|
|
74
|
+
db.close();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("terminated instance + closed-unmerged PR (no merged_at) derives `abandoned`", () => {
|
|
78
|
+
const db = viewDb();
|
|
79
|
+
addPr(db, "o/r#closed", { status: "converging", processKey: "222", mergedAt: null });
|
|
80
|
+
terminate(db, "222");
|
|
81
|
+
assertEquals(derivedStatus(db, "o/r#closed"), "abandoned");
|
|
82
|
+
db.close();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("terminated instance + open PR (no merged_at) derives `abandoned`", () => {
|
|
86
|
+
const db = viewDb();
|
|
87
|
+
addPr(db, "o/r#open", { status: "waiting_review", processKey: "333", mergedAt: null });
|
|
88
|
+
terminate(db, "333");
|
|
89
|
+
assertEquals(derivedStatus(db, "o/r#open"), "abandoned");
|
|
90
|
+
db.close();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("a live (non-terminated) in-flight run passes its base status through unchanged", () => {
|
|
94
|
+
const db = viewDb();
|
|
95
|
+
addPr(db, "o/r#live", { status: "converging", processKey: "444", mergedAt: null });
|
|
96
|
+
db.prepare(
|
|
97
|
+
"INSERT INTO _urban_instance_state (process_instance_key, state, waiting_on_human, updated_at) VALUES ('444', 'ACTIVE', 0, 't')",
|
|
98
|
+
).run();
|
|
99
|
+
assertEquals(derivedStatus(db, "o/r#live"), "converging");
|
|
100
|
+
db.close();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("a merged PR whose instance COMPLETED normally (no terminated edge) reads base `merged`", () => {
|
|
104
|
+
const db = viewDb();
|
|
105
|
+
addPr(db, "o/r#done", { status: "merged", processKey: "555", mergedAt: "2026-11-01T00:00:00Z" });
|
|
106
|
+
db.prepare(
|
|
107
|
+
"INSERT INTO _urban_instance_state (process_instance_key, state, waiting_on_human, updated_at) VALUES ('555', 'COMPLETED', 0, 't')",
|
|
108
|
+
).run();
|
|
109
|
+
assertEquals(derivedStatus(db, "o/r#done"), "merged");
|
|
110
|
+
db.close();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a landed PR with no instance-state row (null process_key) still derives merged off merged_at", () => {
|
|
114
|
+
const db = viewDb();
|
|
115
|
+
// merged_at set but the instance row is gone/null: the merged WHEN keys on merged_at, not the join.
|
|
116
|
+
addPr(db, "o/r#nullkey", { status: "converging", processKey: null, mergedAt: "2026-11-01T00:00:00Z" });
|
|
117
|
+
assertEquals(derivedStatus(db, "o/r#nullkey"), "merged");
|
|
118
|
+
db.close();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a landed PR whose instance-state row was pruned still derives merged off merged_at", () => {
|
|
122
|
+
const db = viewDb();
|
|
123
|
+
addPr(db, "o/r#pruned", { status: "waiting_merge", processKey: "777", mergedAt: "2026-11-01T00:00:00Z" });
|
|
124
|
+
assertEquals(derivedStatus(db, "o/r#pruned"), "merged");
|
|
125
|
+
db.close();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("a run terminated between pr.merge's audit and pr.mark-merged (merged_at NULL) derives merged", () => {
|
|
129
|
+
const db = viewDb();
|
|
130
|
+
addPr(db, "o/r#gap", { status: "merging", processKey: "888", mergedAt: null });
|
|
131
|
+
db.prepare("INSERT INTO merges (pr_key, outcome, method, detail, at) VALUES ('o/r#gap', 'merged', 'squash', '', 't')").run();
|
|
132
|
+
terminate(db, "888");
|
|
133
|
+
assertEquals(derivedStatus(db, "o/r#gap"), "merged");
|
|
134
|
+
db.close();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a non-merged audit outcome does not fold a terminated run to merged", () => {
|
|
138
|
+
const db = viewDb();
|
|
139
|
+
addPr(db, "o/r#rc", { status: "merging", processKey: "889", mergedAt: null });
|
|
140
|
+
db.prepare("INSERT INTO merges (pr_key, outcome, method, detail, at) VALUES ('o/r#rc', 'reconverge', 'x', '', 't')").run();
|
|
141
|
+
terminate(db, "889");
|
|
142
|
+
assertEquals(derivedStatus(db, "o/r#rc"), "abandoned");
|
|
143
|
+
db.close();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("owner_terminated exposes termination separately: set for a terminated merged PR, NULL for a live one", () => {
|
|
147
|
+
const db = viewDb();
|
|
148
|
+
addPr(db, "o/r#mt", { status: "merged", processKey: "901", mergedAt: "t" });
|
|
149
|
+
terminate(db, "901");
|
|
150
|
+
addPr(db, "o/r#ml", { status: "merged", processKey: "902", mergedAt: "t" });
|
|
151
|
+
const get = (k: string) =>
|
|
152
|
+
(db.prepare("SELECT derived_status, owner_terminated FROM pull_requests__tracking WHERE pr_key = ?").get(k) as {
|
|
153
|
+
derived_status: string;
|
|
154
|
+
owner_terminated: number | null;
|
|
155
|
+
});
|
|
156
|
+
assertEquals(get("o/r#mt").derived_status, "merged");
|
|
157
|
+
assertEquals(get("o/r#mt").owner_terminated, 1);
|
|
158
|
+
assertEquals(get("o/r#ml").owner_terminated, null);
|
|
159
|
+
db.close();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// The whole fix hinges on the app-owned model claiming the EXACT view name urban's provisioner would
|
|
163
|
+
// otherwise manage, so the framework defers to it. Pin that contract: the model is named
|
|
164
|
+
// `pull_requests__tracking`, registers into the process-wide registry (idempotently), and the
|
|
165
|
+
// registration is what `main.ts` invokes before `runFromEnv`.
|
|
166
|
+
test("the app-owned model is named pull_requests__tracking and registers idempotently", () => {
|
|
167
|
+
assertEquals(pullRequestTrackingReadModel.decl.name, "pull_requests__tracking");
|
|
168
|
+
assertEquals(pullRequestTrackingReadModel.decl.baseTable, "pull_requests");
|
|
169
|
+
registerPullRequestTrackingReadModel();
|
|
170
|
+
const registered = readModelRegistry.get("pull_requests__tracking");
|
|
171
|
+
assert(registered, "the model must be registered so the framework defers to it");
|
|
172
|
+
assertEquals(registered.decl.name, "pull_requests__tracking");
|
|
173
|
+
// Idempotent: re-registering the identical definition is a no-op (never throws).
|
|
174
|
+
registerPullRequestTrackingReadModel();
|
|
175
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// nano-workforce — the ONE canonical "what is this PR's effective status" derivation (issue #959).
|
|
2
|
+
//
|
|
3
|
+
// THE DEFECT. `derived_status` came from the `instanceTracking` `onTerminated.set` edge, which maps
|
|
4
|
+
// **any** terminated run to `abandoned` without consulting the PR's merge state. But a run is
|
|
5
|
+
// terminated AFTER its PR merged in several legitimate ways — an operator cancels a loop that is
|
|
6
|
+
// redundant after a merge, an out-of-band merge (Mergify / a manual click) is reconciled, a restart
|
|
7
|
+
// or upgrade cleans up. Every one of those was recorded "abandoned", which made the status
|
|
8
|
+
// untrustworthy (an operator can't tell dropped work from shipped work) and skewed retro/conformance
|
|
9
|
+
// stats (`LANDED_PR_STATUSES` excludes `abandoned`, so merged PRs didn't count as landed).
|
|
10
|
+
//
|
|
11
|
+
// THE FIX (the defect class, not the instance). `abandoned` must mean "terminated AND the PR did not
|
|
12
|
+
// merge". When the PR landed — recorded on the base row's `merged_at` (set by `pr.mark-merged` or the
|
|
13
|
+
// out-of-band `retireTerminalReviewWait`) — the effective status is `merged` REGARDLESS of how the
|
|
14
|
+
// instance ended. This module owns that derivation ONCE, as a `defineReadModel` over the SAME
|
|
15
|
+
// canonical `urban_instance_state` projection the framework's tracking view uses, plus the base row's
|
|
16
|
+
// own `merged_at` column.
|
|
17
|
+
//
|
|
18
|
+
// HOW IT SHIPS. The model is named `pull_requests__tracking` — the EXACT managed-VIEW name urban's
|
|
19
|
+
// `instanceTracking` provisioner would otherwise create for the `pull_requests` binding
|
|
20
|
+
// (`instanceTrackingReadModelTarget`). Registering it into the process-wide `readModelRegistry` makes
|
|
21
|
+
// the framework's mount DEFER to this app-owned derivation: `provisionInstanceTrackingViews` refuses
|
|
22
|
+
// to clobber a registered read model of the same name (it logs and skips), and `mountWorkers`
|
|
23
|
+
// (`readModelRegistry.ensureViews`) — which runs BEFORE `mountInstanceTracking` — applies this VIEW.
|
|
24
|
+
// So every reader that already routes through `pull_requests__tracking.derived_status` (the abandon
|
|
25
|
+
// check, conformance, retro, the PR list, the read model) picks up the merge-aware edge with NO
|
|
26
|
+
// per-reader change — the "one canonical derivation, no per-reader logic" the issue asks for.
|
|
27
|
+
//
|
|
28
|
+
// PRECEDENCE (merged > terminated > waiting-human > base). The merged `WHEN` is emitted FIRST so a
|
|
29
|
+
// landed PR reads `merged` even when its instance was terminated; a terminated-unmerged run still
|
|
30
|
+
// reads `abandoned`; a waiting-on-human edge (none for pull_requests today, but kept for parity with
|
|
31
|
+
// the framework's derivation) still applies to a live, unmerged run; otherwise the worker-owned base
|
|
32
|
+
// `status` passes through.
|
|
33
|
+
|
|
34
|
+
import {
|
|
35
|
+
and,
|
|
36
|
+
caseWhen,
|
|
37
|
+
col,
|
|
38
|
+
defineReadModel,
|
|
39
|
+
type Expr,
|
|
40
|
+
eq,
|
|
41
|
+
exists,
|
|
42
|
+
isNotNull,
|
|
43
|
+
lit,
|
|
44
|
+
not,
|
|
45
|
+
or,
|
|
46
|
+
pcol,
|
|
47
|
+
type ReadModel,
|
|
48
|
+
readModelRegistry,
|
|
49
|
+
when,
|
|
50
|
+
} from "@nanobpm/urban";
|
|
51
|
+
import { trackingBindingFor, trackingTargetFor } from "./instanceTracking.ts";
|
|
52
|
+
|
|
53
|
+
const TABLE = "pull_requests";
|
|
54
|
+
|
|
55
|
+
/** The engine-truth terminal lifecycle state the terminal edge keys on (mirrors urban's
|
|
56
|
+
* `TERMINATED_STATE`). Duplicated as a literal so this module derives from the public DSL primitives
|
|
57
|
+
* (`exists`/`pcol`) without reaching into urban's internals. */
|
|
58
|
+
const TERMINATED = "TERMINATED";
|
|
59
|
+
|
|
60
|
+
/** `EXISTS(urban_instance_state WHERE process_instance_key = base.<keyField> AND state='TERMINATED')`
|
|
61
|
+
* — the SAME terminal edge the framework's tracking derivation uses, keyed off the binding's
|
|
62
|
+
* `keyField` (`process_key`) so it can never drift from the reconciler's notion of "which column
|
|
63
|
+
* holds the engine instance key". */
|
|
64
|
+
function terminatedEdge(keyField: string): Expr {
|
|
65
|
+
return exists(
|
|
66
|
+
"urban_instance_state",
|
|
67
|
+
and(eq(pcol("process_instance_key"), col(keyField)), eq(pcol("state"), lit(TERMINATED))),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** `EXISTS(urban_open_user_tasks WHERE process_instance_key = base.<keyField>)` — the wait-on-human
|
|
72
|
+
* edge, kept for parity with the framework's derivation (a binding that adds `onWaitingHuman` later
|
|
73
|
+
* still derives it correctly). */
|
|
74
|
+
function waitingHumanEdge(keyField: string): Expr {
|
|
75
|
+
return exists("urban_open_user_tasks", eq(pcol("process_instance_key"), col(keyField)));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The landed signal: the base row's `merged_at` stamp OR a durable `merges.outcome='merged'` audit
|
|
79
|
+
* row. `pr.merge` writes the audit BEFORE the separate `pr.mark-merged` task stamps `merged_at`, so a
|
|
80
|
+
* run terminated between the two still landed — the audit alone must be sufficient. Neither signal
|
|
81
|
+
* depends on a retained instance-state row (a pruned/null `process_key` still reads `merged`). */
|
|
82
|
+
function landedEdge(): Expr {
|
|
83
|
+
return or(
|
|
84
|
+
isNotNull(col("merged_at")),
|
|
85
|
+
exists("merges", and(eq(pcol("pr_key"), col("pr_key")), eq(pcol("outcome"), lit("merged")))),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The merge-aware effective-status derivation for the `pull_requests` binding:
|
|
90
|
+
*
|
|
91
|
+
* CASE
|
|
92
|
+
* WHEN <merged_at set OR merged audit row> THEN 'merged' -- landed wins, however the run ended
|
|
93
|
+
* WHEN <terminated> THEN <onTerminated> -- terminated-unmerged = abandoned
|
|
94
|
+
* WHEN <waiting-on-human> AND NOT <terminated> THEN <onWaitingHuman>
|
|
95
|
+
* ELSE base.<statusField> -- the worker-owned transient
|
|
96
|
+
* END
|
|
97
|
+
*
|
|
98
|
+
* The merged `WHEN` precedes the terminated one so a landed PR is `merged` no matter how its run
|
|
99
|
+
* ended; the terminated-unmerged branch preserves the manifest's `onTerminated.set.status` value
|
|
100
|
+
* (`abandoned`) rather than hard-coding it, so the manifest stays the source of truth for the
|
|
101
|
+
* not-landed terminal. */
|
|
102
|
+
export function derivePrEffectiveStatusExpr(): Expr {
|
|
103
|
+
const binding = trackingBindingFor(TABLE);
|
|
104
|
+
const statusField = binding.statusField ?? "status";
|
|
105
|
+
const keyField = binding.keyField;
|
|
106
|
+
const terminated = terminatedEdge(keyField);
|
|
107
|
+
|
|
108
|
+
const whens = [
|
|
109
|
+
// Landed beats terminated: a merged PR is `merged` even when its loop was terminated afterwards.
|
|
110
|
+
when(landedEdge(), lit("merged")),
|
|
111
|
+
];
|
|
112
|
+
const terminalStatus = binding.onTerminated?.set?.[statusField];
|
|
113
|
+
if (typeof terminalStatus === "string") {
|
|
114
|
+
whens.push(when(terminated, lit(terminalStatus)));
|
|
115
|
+
}
|
|
116
|
+
const waitingStatus = binding.onWaitingHuman?.set?.[statusField];
|
|
117
|
+
if (typeof waitingStatus === "string") {
|
|
118
|
+
whens.push(when(and(waitingHumanEdge(keyField), not(terminated)), lit(waitingStatus)));
|
|
119
|
+
}
|
|
120
|
+
return caseWhen(whens, col(statusField));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The derived column exposing "this PR's owning run was terminated" SEPARATELY from the effective
|
|
124
|
+
* status. `derived_status` deliberately reports a landed PR as `merged` even when its run was
|
|
125
|
+
* terminated, so it can no longer carry the cancellation signal on its own — the abort protocol
|
|
126
|
+
* (`abandonStatusForToken`) reads THIS column too, so terminating a redundant post-merge round still
|
|
127
|
+
* tells its agent to stop. `1` when terminated, otherwise `NULL`. */
|
|
128
|
+
export const OWNER_TERMINATED_COLUMN = "owner_terminated";
|
|
129
|
+
|
|
130
|
+
/** `CASE WHEN <terminated> THEN 1 END` over the same terminal edge `derived_status` uses. */
|
|
131
|
+
export function deriveOwnerTerminatedExpr(): Expr {
|
|
132
|
+
return caseWhen([when(terminatedEdge(trackingBindingFor(TABLE).keyField), lit(1))], lit(null));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The app-owned `pull_requests__tracking` read model. `selectBaseColumns: true` (the default) so the
|
|
136
|
+
* VIEW re-exports `pull_requests.*` plus the derived `derived_status` — byte-compatible with the
|
|
137
|
+
* framework's own tracking VIEW shape, so every existing reader keeps working unchanged. */
|
|
138
|
+
export const pullRequestTrackingReadModel: ReadModel = defineReadModel({
|
|
139
|
+
name: trackingTargetFor(TABLE).view,
|
|
140
|
+
baseTable: TABLE,
|
|
141
|
+
derive: {
|
|
142
|
+
[trackingTargetFor(TABLE).statusColumn]: derivePrEffectiveStatusExpr(),
|
|
143
|
+
[OWNER_TERMINATED_COLUMN]: deriveOwnerTerminatedExpr(),
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
/** Register the merge-aware tracking VIEW into the process-wide `readModelRegistry` so the framework
|
|
148
|
+
* provisions THIS derivation (and defers its own). Call ONCE at boot, BEFORE `runFromEnv` mounts
|
|
149
|
+
* workers/instance-tracking. Idempotent: re-registering the identical definition is a no-op. */
|
|
150
|
+
export function registerPullRequestTrackingReadModel(): void {
|
|
151
|
+
readModelRegistry.register(pullRequestTrackingReadModel);
|
|
152
|
+
}
|
package/app/retro.test.ts
CHANGED
|
@@ -140,6 +140,17 @@ test("isPlanComplete: true when every task is settled (terminal PR or skipped/bl
|
|
|
140
140
|
assertEquals(await isPlanComplete(data, PLAN), true);
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
// Issue #959: a PR that MERGED but whose loop was terminated afterwards derived `abandoned` off the
|
|
144
|
+
// `onTerminated` edge. Retro must still treat it as the landed terminal it is. Model the terminated
|
|
145
|
+
// divergence by seeding the base row at its frozen in-flight transient WITH `merged_at` set: the
|
|
146
|
+
// merge-aware tracking view folds it to `merged`, so the plan reads complete (not stuck-in-flight).
|
|
147
|
+
test("isPlanComplete: a merged-then-terminated PR (frozen base + merged_at) is terminal/landed (#959)", async () => {
|
|
148
|
+
const { data, stores } = memData();
|
|
149
|
+
seedTask(stores, { id: "t1", status: "opened", pr_key: "acme/widgets#10" });
|
|
150
|
+
stores["pull_requests"] = [{ pr_key: "acme/widgets#10", status: "converging", merged_at: "2026-11-01T00:00:00Z" }];
|
|
151
|
+
assertEquals(await isPlanComplete(data, PLAN), true);
|
|
152
|
+
});
|
|
153
|
+
|
|
143
154
|
test("isPlanComplete: an empty plan has nothing to retrospect", async () => {
|
|
144
155
|
const { data } = memData();
|
|
145
156
|
assertEquals(await isPlanComplete(data, PLAN), false);
|