@bongos/core 1.19.599 → 1.19.600

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/.bongos-core.json CHANGED
@@ -2,22 +2,22 @@
2
2
  "artifact": "bongos-core",
3
3
  "manifest_schema": 1,
4
4
  "generator": "scripts/gds/package-core.js",
5
- "core_version": "1.19.599",
6
- "core_contract": "1.19.599",
7
- "source_commit": "f59ebfca96e7c415cbc1fad82765ad3a5b9928f0",
5
+ "core_version": "1.19.600",
6
+ "core_contract": "1.19.600",
7
+ "source_commit": "8e02f02563986161d1b77b266027bab29d5534aa",
8
8
  "source_ref": "HEAD",
9
- "built_at": "2026-09-08T04:29:29.015Z",
9
+ "built_at": "2026-09-08T04:42:02.346Z",
10
10
  "redaction": {
11
11
  "model": "docs-redacted+functional-verbatim",
12
12
  "docs_redacted": 457,
13
13
  "agent_docs_stubbed": 24,
14
- "functional_verbatim": 2073,
14
+ "functional_verbatim": 2075,
15
15
  "rules": 3,
16
16
  "gate_literals": 3,
17
17
  "gate": "passed"
18
18
  },
19
- "file_count": 2554,
20
- "tree_sha256": "aedc5cdbc47fef15f2a6faed62adffd158b835cfe6b534ff142dc4bef06645ea",
19
+ "file_count": 2556,
20
+ "tree_sha256": "a008ff8e9029ba8be4b741e6ce36012a90a005bad10e76f4fed030cee2404487",
21
21
  "files": [
22
22
  {
23
23
  "path": ".claude/skills/blocker-review/SKILL.md",
@@ -2727,7 +2727,7 @@
2727
2727
  {
2728
2728
  "path": "docs/module-api-changelog.md",
2729
2729
  "mode": "0000644",
2730
- "sha256": "2fc4af2f63b7d20ac659d4e25f0c17a4489e45ab57f8c03e2591063266d5017c"
2730
+ "sha256": "251ae2f82d65d325f220649d865bc5413a3febbf493db6994b6f3e9713968ac8"
2731
2731
  },
2732
2732
  {
2733
2733
  "path": "docs/modules-contract.md",
@@ -5532,7 +5532,7 @@
5532
5532
  {
5533
5533
  "path": "modules/lifecycle/db-goals.js",
5534
5534
  "mode": "0000644",
5535
- "sha256": "e8df5e0da281082f8627d3c985dc1993c3c36e9a9848594fadc8393ea7c128d2"
5535
+ "sha256": "dde23a909ea56f7102d42af9bd5a832449940b00b4bfac7ed779e7938d95885c"
5536
5536
  },
5537
5537
  {
5538
5538
  "path": "modules/lifecycle/db-grade.js",
@@ -5582,7 +5582,7 @@
5582
5582
  {
5583
5583
  "path": "modules/lifecycle/done-when.js",
5584
5584
  "mode": "0000644",
5585
- "sha256": "4e798aff55de6481ea9da3f8a08e68d99902f3eac4749e30d727517ff61176b5"
5585
+ "sha256": "7def4b53d251ecac2e9ce4c7aa2df8cb8814e2105d47417e5d43191884cd8ea1"
5586
5586
  },
5587
5587
  {
5588
5588
  "path": "modules/lifecycle/est-advisory.js",
@@ -5869,6 +5869,11 @@
5869
5869
  "mode": "0000644",
5870
5870
  "sha256": "af00ee4629002c8df2448e2146a24108407a36b062859ee1afee293aa60cb209"
5871
5871
  },
5872
+ {
5873
+ "path": "modules/lifecycle/version-autoclose.js",
5874
+ "mode": "0000644",
5875
+ "sha256": "06e035b8d1545d68a0e629e707ea7f86d48fac5dc8fe0c5e70ec8704508ecf7c"
5876
+ },
5872
5877
  {
5873
5878
  "path": "modules/lifecycle/version-close.js",
5874
5879
  "mode": "0000644",
@@ -7622,12 +7627,12 @@
7622
7627
  {
7623
7628
  "path": "package-lock.json",
7624
7629
  "mode": "0000644",
7625
- "sha256": "89f20f42f441b010cc5a6ea07f38828987932c85779d01278988f85428d3465e"
7630
+ "sha256": "d3456f52cc8555eb1a20f884dc2d32240032138193c5d525ea455d07423e9656"
7626
7631
  },
7627
7632
  {
7628
7633
  "path": "package.json",
7629
7634
  "mode": "0000644",
7630
- "sha256": "8bba17aa2a21a1b48b9bfb7fb50aad31f75df0d63e459b7fc4ef504375c4c80a"
7635
+ "sha256": "107d899ff6ec92f0f2dd863f62ab2a68eaa82c7f60d9e172c243b9b5ff421f52"
7631
7636
  },
7632
7637
  {
7633
7638
  "path": "public-docs/index.html",
@@ -9342,7 +9347,7 @@
9342
9347
  {
9343
9348
  "path": "src/module-api.js",
9344
9349
  "mode": "0000644",
9345
- "sha256": "55821676bf2fdb2f703e9c6137c877f3b5ede7291985bbce9c611f262a2fc48b"
9350
+ "sha256": "fa5a67fefe06f235502a19ee017a75467ab1f5cadf77c2b004edd4109b397ece"
9346
9351
  },
9347
9352
  {
9348
9353
  "path": "src/module-loader/catalog.js",
@@ -9622,7 +9627,7 @@
9622
9627
  {
9623
9628
  "path": "tests/auto_satisfy_criteria.mjs",
9624
9629
  "mode": "0000644",
9625
- "sha256": "d63beea7b680a194d163aa5ad11806c9aa860de93d614195a05a33082e9f8adb"
9630
+ "sha256": "d7ac18d14e8e9e5e7b9e913a76953fe69bfefea4912e77ca30b285647bfcfca8"
9626
9631
  },
9627
9632
  {
9628
9633
  "path": "tests/autonomous_fit_protected_paths.mjs",
@@ -10492,7 +10497,7 @@
10492
10497
  {
10493
10498
  "path": "tests/goal_closure_invariants.mjs",
10494
10499
  "mode": "0000644",
10495
- "sha256": "eace8ebcf39f40683bf9cbcfbc0e554b115e73d1f72894ca57a89e82d119d357"
10500
+ "sha256": "e5f937b9ce3264b6b4bb3a9ca57fa000b2190fe49c3ba6d70acdd9d26f55423c"
10496
10501
  },
10497
10502
  {
10498
10503
  "path": "tests/goal_conflicts.mjs",
@@ -12684,6 +12689,11 @@
12684
12689
  "mode": "0000644",
12685
12690
  "sha256": "808a5fb2e14fb03ded6ed5ea0b3c0a0a7f53bfb885e853acb225f7b439a06ac3"
12686
12691
  },
12692
+ {
12693
+ "path": "tests/version_autoclose.mjs",
12694
+ "mode": "0000644",
12695
+ "sha256": "3a9e1f7ae24aa4d25baaaff53890e4d963f662ae95e5cd69b00da1cd3f6cb012"
12696
+ },
12687
12697
  {
12688
12698
  "path": "tests/version_build_slot.mjs",
12689
12699
  "mode": "0000644",
@@ -1647,5 +1647,7 @@ is load-bearing: the script throws rather than guess if it is missing, and
1647
1647
  landed since 1.19.597 with no explicit bump. run 34186758247. (task 1002620)
1648
1648
  1.19.599 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
1649
1649
  landed since 1.19.598 with no explicit bump. run 34187136306. (task 1002620)
1650
+ 1.19.600 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
1651
+ landed since 1.19.599 with no explicit bump. run 34187881031. (task 1002620)
1650
1652
  ---------------------------------------------------------------------------
1651
1653
  ```
@@ -17,6 +17,9 @@ const { deriveRequiredRank, highestRank } = require('./db-rank-authz.js');
17
17
  const { nonTerminalSql, isTerminalStatus } = require('./task-status');
18
18
  const { maintenanceGoalTitle } = require('./goal-advisory.js');
19
19
  const { SMOKE_NOT_LIKE } = require('./db-shared.js');
20
+ // BV1.R16 (task 1003603) — auto-close, shared with the done-when cascade so the
21
+ // two achievement writers cannot drift apart (ADR 0263 §4).
22
+ const { autoCloseVersionsForGoals } = require('./version-autoclose.js');
20
23
 
21
24
  // -------------------------------------------------------------------------
22
25
  // Goals (ADR 0086 §2 / BV1.R56 — task 1510)
@@ -492,7 +495,20 @@ async function achieveGoalIfComplete({ goalId }, deps = {}) {
492
495
  // the goal simply stays open until its last task lands or is abandoned.
493
496
  if (unfinishedTasks > 0) return { achieved: false, goal, unfinishedTasks };
494
497
  const updated = await setGoalStatus({ goalId, status: 'achieved' }, deps);
495
- return { achieved: true, goal: updated };
498
+
499
+ // BV1.R16 (task 1003603, ADR 0263 §§3–4): the SECOND achievement writer. A goal
500
+ // reaching `achieved` may have been the version's last non-maintenance goal, and
501
+ // that is true whichever door it came through — this manual satisfy path or the
502
+ // ship-time cascade in done-when.js. ADR 0250 D2 already established that these
503
+ // two writers move together or a version closes correctly through one and wrongly
504
+ // through the other; the rule itself lives in ONE place (version-autoclose.js) so
505
+ // "together" is enforced by the require rather than by remembering.
506
+ //
507
+ // Runs on the caller's exec so it sees the flip above, which in the ship path is
508
+ // still uncommitted. Never throws: autoCloseVersionsForGoals catches per version.
509
+ const exec = deps.client || ((deps.pool && process.env.NODE_ENV === 'test') ? deps.pool : pool);
510
+ const versionsClosed = await autoCloseVersionsForGoals(exec, [updated]);
511
+ return { achieved: true, goal: updated, versionsClosed };
496
512
  }
497
513
 
498
514
  // addGoalMember — join. Upserts on the (goal_id, builder_id) PK so a re-join is
@@ -26,6 +26,10 @@
26
26
  const { pool } = require('../../src/module-api');
27
27
  const { nonTerminalSql } = require('./task-status');
28
28
  const { SMOKE_NOT_LIKE } = require('./db-shared.js');
29
+ // BV1.R16 (task 1003603) — auto-close. Dependency-free module, so requiring it
30
+ // here creates no cycle (db-versions.js requires THIS file, which is why the
31
+ // rule could not live beside closeVersion).
32
+ const { autoCloseVersionsForGoals } = require('./version-autoclose.js');
29
33
 
30
34
  // isPendingReview — the ONE derivation of the "met — pending review" auto-flag
31
35
  // (ADR 0086 §6 / BV1.R63): a criterion with >=1 linked task, every one shipped,
@@ -474,7 +478,8 @@ async function autoSatisfyShippedCriteria(exec, { taskId = null } = {}) {
474
478
  // review off docs/architecture.md, whose tasks index list predated 228 and has
475
479
  // been corrected.)
476
480
  const goalIds = [...new Set(criteria.map((c) => c.goal_id).filter((g) => g != null))].map(Number);
477
- if (goalIds.length === 0) return { criteria, goals: [] };
481
+ // No goal achieved, so no version can have just become closable.
482
+ if (goalIds.length === 0) return { criteria, goals: [], versionsClosed: [] };
478
483
  const { rows: goals } = await exec.query(
479
484
  `UPDATE goals g
480
485
  SET status = 'achieved', updated_at = now()
@@ -488,7 +493,16 @@ async function autoSatisfyShippedCriteria(exec, { taskId = null } = {}) {
488
493
  RETURNING g.id, g.title, g.version_id, g.status`,
489
494
  [goalIds]
490
495
  );
491
- return { criteria, goals };
496
+ // BV1.R16 (task 1003603, ADR 0263 §§3–4): a version closes ITSELF once its last
497
+ // non-maintenance goal achieves. On the caller's `exec`, so it sees the flips
498
+ // above that have not committed yet — a check on the pool would never close the
499
+ // version on the ship that actually finished it.
500
+ //
501
+ // `autoCloseVersionsForGoals` catches per version and never rethrows: a version
502
+ // close must not be able to fail a builder's ship. The accepted failure is a
503
+ // version left un-closed, which the reconciler's next unscoped sweep heals.
504
+ const versionsClosed = await autoCloseVersionsForGoals(exec, goals);
505
+ return { criteria, goals, versionsClosed };
492
506
  }
493
507
 
494
508
  // describeAutoClose — the one-line summary of what a close actually closed, or
@@ -0,0 +1,107 @@
1
+ 'use strict';
2
+
3
+ // modules/lifecycle/version-autoclose.js — a version closes ITSELF when its last
4
+ // non-maintenance goal achieves (BV1.R16, task 1003603, goal 1000086,
5
+ // ADR 0250 D5, ADR 0263 §§3–4).
6
+ //
7
+ // WHY THIS FILE EXISTS AT ALL, rather than living beside `closeVersion`.
8
+ // Goal achievement has TWO writers — `db-goals.achieveGoalIfComplete` (the manual
9
+ // `POST /done-when/:id/satisfy` path) and the `done-when.autoSatisfyShippedCriteria`
10
+ // cascade (every ship, every reconciler tick). Both must auto-close, and neither
11
+ // can reach `db-versions.js`: that file already requires `done-when`, so a call
12
+ // the other way is a require cycle. ADR 0263 §4 accepted writing the rule twice
13
+ // for exactly this reason.
14
+ //
15
+ // It is written ONCE instead, here, because this module requires NOTHING but
16
+ // `task-status` — the `task-status.js` precedent, which is dependency-free so a
17
+ // CLI can read it without paying for the pool. Both writers can require it, no
18
+ // cycle exists, and the rule that decides when a VERSION ends has one definition
19
+ // rather than two that must be kept in lockstep by hand. The duplicate the ADR
20
+ // permitted is a cost it did not have to pay.
21
+ //
22
+ // AUTO-CLOSE HAS NOTHING TO DISPOSITION, and that is the whole shape of this
23
+ // feature (ADR 0263 §3). It fires when the last non-maintenance goal achieves —
24
+ // so by construction every non-maintenance goal on the version is already
25
+ // `achieved` or `archived`, and the set needing a decision is EMPTY BY
26
+ // DEFINITION. The disposition map, the refusal that returns the goals, the
27
+ // successor lineage: all of that belongs to EARLY close (R12) and none of it can
28
+ // ever have input here. This file is therefore much smaller than ADR 0250's
29
+ // sentence makes it sound.
30
+
31
+ const { nonTerminalSql } = require('./task-status');
32
+
33
+ // The maintenance goal never holds a version open (ADR 0263 §7). Reads the
34
+ // `is_maintenance` FLAG that R18 (task 1003605, migration core_234) added — never
35
+ // a title, because deciding this by string would let anyone able to name a goal
36
+ // exempt it from the gate that ends a version.
37
+ const NOT_MAINTENANCE = 'NOT g.is_maintenance';
38
+
39
+ // autoCloseVersionIfComplete — flip `versionId` to `shipped` if, and only if, it
40
+ // holds no open non-maintenance goal.
41
+ //
42
+ // RUNS ON THE CALLER'S `exec`, which is the ship transaction's own client. That
43
+ // is load-bearing rather than tidy: the goal that just achieved was flipped in
44
+ // this same uncommitted transaction, so a check on the module pool would not see
45
+ // it and the version would never close on the ship that finished it — the same
46
+ // reason R09's closure check takes an exec.
47
+ //
48
+ // IDEMPOTENT BY `WHERE status = 'building'`. The reconciler's unscoped sweep runs
49
+ // this every tick, so the second and every later run must be a no-op; without the
50
+ // guard each sweep would rewrite `shipped_at` and the version's own record of
51
+ // when it finished would drift forward forever.
52
+ //
53
+ // Returns the closed version row, or null when the version is not ready (the
54
+ // overwhelmingly common answer) — null is not an error.
55
+ async function autoCloseVersionIfComplete(exec, versionId) {
56
+ if (!exec || versionId == null) return null;
57
+ const { rows } = await exec.query(
58
+ `UPDATE versions v
59
+ SET status = 'shipped', shipped_at = now()
60
+ WHERE v.id = $1
61
+ AND v.status = 'building'
62
+ AND NOT EXISTS (
63
+ SELECT 1 FROM goals g
64
+ WHERE g.version_id = v.id
65
+ AND g.status = 'open'
66
+ AND ${NOT_MAINTENANCE}
67
+ )
68
+ RETURNING v.id, v.name, v.track, v.status, v.shipped_at`,
69
+ [versionId]
70
+ );
71
+ return rows[0] ?? null;
72
+ }
73
+
74
+ // autoCloseVersionsForGoals — the call both achievement writers actually make.
75
+ //
76
+ // Takes the goals a cascade just achieved (each carrying `version_id`) and tries
77
+ // their versions, de-duplicated. A cascade can achieve several goals at once and
78
+ // they may sit on different versions, so this is a set rather than a single id.
79
+ //
80
+ // IT MAY NEVER FAIL A SHIP. This is not a preference — it is the established
81
+ // posture of `closeCompletedWorkForShip`, which swallows its own failure on the
82
+ // argument that a criterion close must never fail a ship and the reconciler's
83
+ // sweep retries every tick. A version close is a strictly WIDER write than a
84
+ // criterion close, so it inherits that posture a fortiori: the failure mode we
85
+ // accept is a version left UN-closed, which the next sweep heals, and the one we
86
+ // refuse is a builder's ship rolled back by a bug in version-close. Every throw
87
+ // is caught here, per version, so one bad version cannot stop another closing.
88
+ //
89
+ // Returns the rows that actually closed — usually empty.
90
+ async function autoCloseVersionsForGoals(exec, goals = [], onError = null) {
91
+ const ids = [...new Set((goals || []).map((g) => g && g.version_id).filter((v) => v != null))];
92
+ const closed = [];
93
+ for (const versionId of ids) {
94
+ try {
95
+ const row = await autoCloseVersionIfComplete(exec, versionId);
96
+ if (row) closed.push(row);
97
+ } catch (err) {
98
+ // Reported, never propagated. A silent swallow would make a persistently
99
+ // broken close indistinguishable from "nothing was ready", which is the
100
+ // failure that hides for months.
101
+ if (typeof onError === 'function') onError(versionId, err);
102
+ }
103
+ }
104
+ return closed;
105
+ }
106
+
107
+ module.exports = { autoCloseVersionIfComplete, autoCloseVersionsForGoals, NOT_MAINTENANCE };
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bongos/core",
3
- "version": "1.19.599",
3
+ "version": "1.19.600",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@bongos/core",
9
- "version": "1.19.599",
9
+ "version": "1.19.600",
10
10
  "license": "AGPL-3.0-or-later",
11
11
  "dependencies": {
12
12
  "express": "^4.21.2",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bongos/core",
3
- "version": "1.19.599",
3
+ "version": "1.19.600",
4
4
  "description": "Cloud Bongos — the AI-first build platform core (GDS + platform surfaces + module system), installed as a versioned dependency (ADR 0108).",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "main": "src/platform-server.js",
package/src/module-api.js CHANGED
@@ -55,7 +55,7 @@ const { buildInfo } = require('./build-info');
55
55
  // there. scripts/gds/bump-version.js still rewrites the literal below; it appends
56
56
  // the entry to that file. Look for a version's history there, not here.
57
57
  // ---------------------------------------------------------------------------
58
- const CORE_VERSION = '1.19.599'; // CI auto-patch carrier (ADR 0161); changelog: docs/module-api-changelog.md
58
+ const CORE_VERSION = '1.19.600'; // CI auto-patch carrier (ADR 0161); changelog: docs/module-api-changelog.md
59
59
 
60
60
  // A namespaced logger so a module's log lines are attributable + consistent.
61
61
  // Usage: const log = api.logger('dev-box'); log.info('mounted');
@@ -107,7 +107,12 @@ await test('ASYMMETRY (rollup): the /status rollup is untouched by this task', (
107
107
  await test('EXEC: every write goes through the caller-supplied exec (so a ship txn is atomic)', async () => {
108
108
  const exec = fakeExec({ rows: [CRIT] }, { rows: [{ id: 77, title: 'g', version_id: 'BONGOS-V1', status: 'achieved' }] });
109
109
  const out = await autoSatisfyShippedCriteria(exec, { taskId: 5 });
110
- assert.equal(exec.calls.length, 2, 'one criterion update + one goal cascade');
110
+ // Three since BV1.R16 (task 1003603): the criterion update, the goal cascade,
111
+ // and the version auto-close — which is the point, because the third must ride
112
+ // the SAME exec as the first two or a version would never close on the ship
113
+ // that finished it.
114
+ assert.equal(exec.calls.length, 3, 'criterion update + goal cascade + version auto-close');
115
+ assert.match(exec.calls[2].sql, /UPDATE versions v/, 'and the third is the auto-close, on exec');
111
116
  assert.deepEqual(out.criteria, [CRIT]);
112
117
  assert.deepEqual(out.goals.map((g) => g.id), [77]);
113
118
  const src = readFileSync(new URL('../modules/lifecycle/done-when.js', import.meta.url), 'utf8');
@@ -251,9 +251,15 @@ test('the cascade reads through the exec it was handed, so it sees the ship\'s o
251
251
  test('every statement goes through the caller exec — none reaches the module pool', async () => {
252
252
  const exec = makeExec(world({ criteria: [{ satisfied: false, task_ids: [200] }], tasks: [SHIPPED()] }));
253
253
  await autoSatisfyShippedCriteria(exec, { taskId: 200 });
254
- assert.equal(exec.calls.length, 2, 'the criterion close and the cascade, both on exec');
254
+ // BV1.R16 (task 1003603) added a third: the version auto-close. It belongs in
255
+ // THIS test rather than only in its own, because the property under assertion —
256
+ // nothing reaches the module pool — is exactly what makes auto-close correct on
257
+ // a ship: the goal it keys on was flipped in this uncommitted transaction, and
258
+ // a pool read would not see it.
259
+ assert.equal(exec.calls.length, 3, 'the criterion close, the cascade and the version auto-close, all on exec');
255
260
  assert.match(exec.calls[0].sql, /UPDATE done_when_criteria/);
256
261
  assert.match(exec.calls[1].sql, /UPDATE goals g/);
262
+ assert.match(exec.calls[2].sql, /UPDATE versions v/);
257
263
  });
258
264
 
259
265
  // ---------------------------------------------------------------------------
@@ -0,0 +1,170 @@
1
+ // tests/version_autoclose.mjs — a version closes ITSELF when its last
2
+ // non-maintenance goal achieves (BV1.R16, task 1003603, goal 1000086,
3
+ // ADR 0250 D5, ADR 0263 §§3–4).
4
+ //
5
+ // THE SHAPE, which is smaller than ADR 0250's sentence suggests. Auto-close fires
6
+ // when the last non-maintenance goal achieves — so by construction every
7
+ // non-maintenance goal on the version is already closed, and the set needing a
8
+ // disposition is EMPTY BY DEFINITION. The disposition map, the refusal that
9
+ // returns the goals, the successor lineage: all of that is EARLY close (R12) and
10
+ // none of it can ever have input here.
11
+ //
12
+ // THE TWO PROPERTIES THAT ARE LOAD-BEARING, and the reason this file is not just
13
+ // a happy-path check:
14
+ //
15
+ // 1. It hangs off BOTH achievement writers. `db-goals.achieveGoalIfComplete` is
16
+ // the manual satisfy path; `done-when.autoSatisfyShippedCriteria` is the
17
+ // cascade on every ship and every reconciler tick. ADR 0250 D2 established
18
+ // that these two move together or a version closes correctly through one door
19
+ // and wrongly through the other. ADR 0263 §4 expected the rule to be written
20
+ // TWICE for that reason — a require cycle stops db-versions.js being the
21
+ // shared home. It is written ONCE instead, in a dependency-free module both
22
+ // can require, so "together" is enforced by the import rather than by anyone
23
+ // remembering.
24
+ //
25
+ // 2. It may NEVER fail a ship. A version close is a strictly wider write than a
26
+ // criterion close, and `closeCompletedWorkForShip` already swallows its own
27
+ // failure on the argument that a criterion close must not fail a ship. The
28
+ // accepted failure here is a version left UN-closed, which the next sweep
29
+ // heals; the refused one is a builder's ship rolled back by a bug in
30
+ // version-close.
31
+ //
32
+ // Run: node --test tests/version_autoclose.mjs
33
+
34
+ import assert from 'node:assert/strict';
35
+ import { test } from 'node:test';
36
+ import { readFileSync } from 'node:fs';
37
+ import { createRequire } from 'node:module';
38
+
39
+ process.env.NODE_ENV = 'test';
40
+ const require = createRequire(import.meta.url);
41
+ const { autoCloseVersionIfComplete, autoCloseVersionsForGoals, NOT_MAINTENANCE } =
42
+ require('../modules/lifecycle/version-autoclose.js');
43
+
44
+ const src = (rel) => readFileSync(new URL('../' + rel, import.meta.url), 'utf8');
45
+ const AUTOCLOSE = src('modules/lifecycle/version-autoclose.js');
46
+ const DONE_WHEN = src('modules/lifecycle/done-when.js');
47
+ const DB_GOALS = src('modules/lifecycle/db-goals.js');
48
+
49
+ const recorder = (handler) => {
50
+ const seen = [];
51
+ return { seen, exec: { query: async (sql, params) => { seen.push({ sql, params }); return handler(sql, params); } } };
52
+ };
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // The predicate
56
+ // ---------------------------------------------------------------------------
57
+
58
+ test('the maintenance goal never holds a version open, and it is read by FLAG', () => {
59
+ assert.equal(NOT_MAINTENANCE, 'NOT g.is_maintenance');
60
+ assert.equal(/title/i.test(NOT_MAINTENANCE), false,
61
+ 'deciding this by string would let anyone able to name a goal exempt it from the gate that ends a version');
62
+ });
63
+
64
+ test('the close asks "is any non-maintenance goal still open", and nothing else', async () => {
65
+ const r = recorder(() => ({ rows: [] }));
66
+ await autoCloseVersionIfComplete(r.exec, 'V1');
67
+ const sql = r.seen[0].sql.replace(/\s+/g, ' ');
68
+ assert.match(sql, /UPDATE versions v/);
69
+ assert.match(sql, /NOT EXISTS \( SELECT 1 FROM goals g/);
70
+ assert.match(sql, /g\.status = 'open'/);
71
+ assert.match(sql, /NOT g\.is_maintenance/);
72
+ // No disposition machinery may appear here — see the header.
73
+ assert.equal(/disposition|abandon|roll_forward/i.test(sql), false,
74
+ 'auto-close has nothing to disposition; that is all early close');
75
+ });
76
+
77
+ test('the flip is guarded on building, so a reconciler retry is a no-op', async () => {
78
+ // Without the guard every sweep rewrites shipped_at and the version's own
79
+ // record of when it finished drifts forward forever.
80
+ const r = recorder(() => ({ rows: [] }));
81
+ await autoCloseVersionIfComplete(r.exec, 'V1');
82
+ assert.match(r.seen[0].sql.replace(/\s+/g, ' '), /v\.status = 'building'/);
83
+ });
84
+
85
+ test('a version that is not ready returns null, which is not an error', async () => {
86
+ const r = recorder(() => ({ rows: [] }));
87
+ assert.equal(await autoCloseVersionIfComplete(r.exec, 'V1'), null);
88
+ });
89
+
90
+ test('a missing exec or version id is a no-op, not a throw', async () => {
91
+ assert.equal(await autoCloseVersionIfComplete(null, 'V1'), null);
92
+ const r = recorder(() => ({ rows: [] }));
93
+ assert.equal(await autoCloseVersionIfComplete(r.exec, null), null);
94
+ assert.equal(r.seen.length, 0, 'and it must not issue a query it cannot key');
95
+ });
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // It may never fail a ship
99
+ // ---------------------------------------------------------------------------
100
+
101
+ test('a throwing close is CAUGHT — a ship must never be rolled back by version-close', async () => {
102
+ const boom = { query: async () => { throw new Error('deadlock detected'); } };
103
+ const seen = [];
104
+ const out = await autoCloseVersionsForGoals(boom, [{ version_id: 'V1' }], (v, e) => seen.push(`${v}:${e.message}`));
105
+ assert.deepEqual(out, []);
106
+ assert.deepEqual(seen, ['V1:deadlock detected'],
107
+ 'reported, never propagated — a silent swallow makes a persistently broken close look like "nothing was ready"');
108
+ });
109
+
110
+ test('one bad version does not stop another closing', async () => {
111
+ let n = 0;
112
+ const exec = { query: async () => { n += 1; if (n === 1) throw new Error('nope'); return { rows: [{ id: 'V2', status: 'shipped' }] }; } };
113
+ const out = await autoCloseVersionsForGoals(exec, [{ version_id: 'V1' }, { version_id: 'V2' }], () => {});
114
+ assert.deepEqual(out.map((v) => v.id), ['V2']);
115
+ });
116
+
117
+ test('versions are de-duplicated — a cascade can achieve several goals on one version', async () => {
118
+ const r = recorder(() => ({ rows: [] }));
119
+ await autoCloseVersionsForGoals(r.exec, [{ version_id: 'V1' }, { version_id: 'V1' }, { version_id: 'V2' }]);
120
+ assert.deepEqual(r.seen.map((q) => q.params[0]), ['V1', 'V2']);
121
+ });
122
+
123
+ test('a goal with no version_id is skipped rather than queried as null', async () => {
124
+ const r = recorder(() => ({ rows: [] }));
125
+ assert.deepEqual(await autoCloseVersionsForGoals(r.exec, [{}, null, undefined]), []);
126
+ assert.equal(r.seen.length, 0);
127
+ });
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // BOTH achievement writers, and the reason it is written once
131
+ // ---------------------------------------------------------------------------
132
+
133
+ test('the ship-time cascade auto-closes, on the caller exec', () => {
134
+ assert.match(DONE_WHEN, /const versionsClosed = await autoCloseVersionsForGoals\(exec, goals\)/,
135
+ 'on `exec`, not the pool — the goal it just achieved is uncommitted, and a pool read would never close the version on the ship that finished it');
136
+ assert.match(DONE_WHEN, /return \{ criteria, goals, versionsClosed \}/);
137
+ });
138
+
139
+ test('the MANUAL satisfy path auto-closes too', () => {
140
+ // The failure this prevents: a version that closes correctly through a ship and
141
+ // wrongly through POST /done-when/:id/satisfy.
142
+ assert.match(DB_GOALS, /const versionsClosed = await autoCloseVersionsForGoals\(exec, \[updated\]\)/);
143
+ assert.match(DB_GOALS, /return \{ achieved: true, goal: updated, versionsClosed \}/);
144
+ });
145
+
146
+ test('both writers import the SAME rule — it is not written twice', () => {
147
+ // ADR 0263 §4 expected a deliberate duplicate, because db-versions.js requires
148
+ // done-when.js and so cannot be the shared home. A dependency-free module can
149
+ // be, which makes "the two copies move together" an import rather than a habit.
150
+ for (const [name, code] of [['done-when.js', DONE_WHEN], ['db-goals.js', DB_GOALS]]) {
151
+ assert.match(code, /require\('\.\/version-autoclose\.js'\)/, `${name} must import the shared rule`);
152
+ }
153
+ // And the rule's SQL exists in exactly one file.
154
+ assert.equal(/UPDATE versions v[\s\S]*?NOT EXISTS/.test(DONE_WHEN), false, 'done-when.js must not carry its own copy');
155
+ assert.equal(/UPDATE versions v[\s\S]*?NOT EXISTS/.test(DB_GOALS), false, 'db-goals.js must not carry its own copy');
156
+ assert.match(AUTOCLOSE, /UPDATE versions v/);
157
+ });
158
+
159
+ test('the shared module stays dependency-free, or the cycle comes back', () => {
160
+ // db-versions.js requires done-when.js. If this module ever requires either,
161
+ // the import that makes the single definition possible becomes a cycle.
162
+ const requires = [...AUTOCLOSE.matchAll(/require\('([^']+)'\)/g)].map((m) => m[1]);
163
+ assert.deepEqual(requires, ['./task-status'],
164
+ 'only task-status — the dependency-free precedent that lets both writers share this');
165
+ });
166
+
167
+ test('an empty cascade short-circuits without asking about versions', () => {
168
+ assert.match(DONE_WHEN, /if \(goalIds\.length === 0\) return \{ criteria, goals: \[\], versionsClosed: \[\] \}/,
169
+ 'no goal achieved means no version can have just become closable');
170
+ });