@bongos/core 1.19.935 → 1.19.937

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.bongos-core.json +74 -39
  2. package/clients/bongos-client/README.md +1 -1
  3. package/clients/bongos-client/bongos-client.global.js +2 -0
  4. package/clients/bongos-client/index.cjs +2 -0
  5. package/clients/bongos-client/index.d.ts +6 -3
  6. package/clients/bongos-client/index.mjs +2 -0
  7. package/docs/api/openapi.json +103 -2
  8. package/docs/api-reference.md +6 -5
  9. package/docs/copy-inventory.md +36 -34
  10. package/docs/copy-registry.json +56 -38
  11. package/docs/module-api-changelog.md +4 -0
  12. package/modules/government/board.js +35 -0
  13. package/modules/government/db.js +41 -1
  14. package/modules/government/migrations/government_018_passed_items_index.sql +31 -0
  15. package/modules/government/routes/government.js +7 -0
  16. package/modules/hall-ui/public/board-room.css +6 -0
  17. package/modules/hall-ui/public/board-room.js +31 -0
  18. package/modules/hall-ui/public/ideas.css +27 -0
  19. package/modules/hall-ui/public/ideas.html +7 -0
  20. package/modules/hall-ui/public/ideas.js +99 -3
  21. package/modules/ideas/grades.js +43 -14
  22. package/modules/ideas/inbox.js +18 -0
  23. package/modules/ideas/migrations/ideas_009_recommendation.sql +62 -0
  24. package/modules/ideas/migrations/ideas_010_goal_ratification.sql +103 -0
  25. package/modules/ideas/module.json +1 -0
  26. package/modules/ideas/projection.js +45 -2
  27. package/modules/ideas/ratify.js +288 -1
  28. package/modules/ideas/routes/inbox.js +72 -1
  29. package/modules/ideas/routes/ratify-goal.js +163 -0
  30. package/modules/ideas/templates.js +16 -4
  31. package/modules/lifecycle/goal-from-idea.js +131 -0
  32. package/modules/lifecycle/lifecycle.js +14 -0
  33. package/package-lock.json +2 -2
  34. package/package.json +1 -1
  35. package/src/module-api.js +1 -1
  36. package/tests/government_board_view.mjs +5 -1
  37. package/tests/government_seed.mjs +1 -0
  38. package/tests/idea_develop_spark.mjs +6 -1
  39. package/tests/idea_goal_ratification.mjs +553 -0
  40. package/tests/idea_recommendation.mjs +381 -0
@@ -0,0 +1,62 @@
1
+ -- ideas_009_recommendation.sql — a Full Idea carries a RECOMMENDATION: goal or
2
+ -- criterion (task 1004092 / BV2.R13, spec R22 in
3
+ -- docs/specs/bongos-v2-ideator-surface.md; spec decision 9).
4
+ --
5
+ -- WHAT THIS IS, AND WHAT IT IS NOT. It is the ideator's answer to "how big did
6
+ -- this turn out to be" — a goal (a gravity well of its own) or a criterion (a
7
+ -- ring inside an existing one). It is NOT a decision. The owner's recorded
8
+ -- reasoning: scope is the thing you learn last, so the person who had the
9
+ -- thought writes down what they think it is and the board, reading it, decides.
10
+ -- Nothing in the system may branch on this column as if it were an outcome —
11
+ -- the ratification half (BV2.R17) reads what the BOARD chose, and this column
12
+ -- only travels alongside it so voters can see what was proposed.
13
+ --
14
+ -- WHY A COLUMN AND NOT A SIXTH TEMPLATE FIELD. The five questions are prose an
15
+ -- author writes and the completeness scorer measures; this is a closed
16
+ -- two-value vocabulary that nothing scores. Putting it in `field_values` would
17
+ -- make it a scored field (`completeness.js` walks every declared field), would
18
+ -- change what a Full Idea costs to file, and would have to be declared once per
19
+ -- vintage — while the recommendation is the same question under any template.
20
+ -- It is a property OF the grade row, beside `completeness_score`, not one of
21
+ -- the answers inside it.
22
+ --
23
+ -- NULLABLE, AND THAT IS PERMANENT. Every Full Idea filed before today has no
24
+ -- recommendation, and inventing one for them would put words in an author's
25
+ -- mouth — the never-invent-a-field rule (the Ideator pack) is exactly about
26
+ -- this. So NULL means "the author did not say", every reader must render that
27
+ -- honestly, and there is no backfill. The hall composer requires a choice
28
+ -- going forward (that is where the question is actually asked); the API keeps
29
+ -- it optional, because a reader that can handle NULL for the historical rows
30
+ -- can handle NULL for a terse CLI filing too, and a required body field would
31
+ -- 400 every already-shipped client against an upgraded instance.
32
+ --
33
+ -- Text + CHECK rather than a Postgres enum TYPE, matching
34
+ -- `ideas_grades_grade_chk` one column over: the vocabulary is pinned closed
35
+ -- today (an unknown recommendation is refused, never silently stored) and
36
+ -- widening it later is a one-line constraint change in a subsequent ideas_*
37
+ -- migration rather than an enum-type migration.
38
+ --
39
+ -- Module migrations are ADDITIVE and NAMESPACED (ADR 0083 §Decision #5): this
40
+ -- touches only `ideas_grades`, a table this module owns. There is no
41
+ -- down-migration. Idempotent: safe to re-run.
42
+
43
+ BEGIN;
44
+
45
+ ALTER TABLE ideas_grades
46
+ ADD COLUMN IF NOT EXISTS recommendation text;
47
+
48
+ -- Added separately and guarded, because ADD COLUMN IF NOT EXISTS skips the
49
+ -- whole clause on a re-run — an inline CONSTRAINT would then never be created
50
+ -- on an instance where the column already landed without it.
51
+ DO $$
52
+ BEGIN
53
+ IF NOT EXISTS (
54
+ SELECT 1 FROM pg_constraint WHERE conname = 'ideas_grades_recommendation_chk'
55
+ ) THEN
56
+ ALTER TABLE ideas_grades
57
+ ADD CONSTRAINT ideas_grades_recommendation_chk
58
+ CHECK (recommendation IS NULL OR recommendation IN ('goal', 'criterion'));
59
+ END IF;
60
+ END $$;
61
+
62
+ COMMIT;
@@ -0,0 +1,103 @@
1
+ -- ideas_010_goal_ratification.sql — the durable record of a Full Idea having
2
+ -- been ratified INTO A GOAL (task 1004096 / BV2.R17, spec R23 in
3
+ -- docs/specs/bongos-v2-ideator-surface.md; spec decision 9).
4
+ --
5
+ -- WHAT THIS TABLE IS FOR. The ratification writes a goal and a done-when
6
+ -- criterion into the LIFECYCLE tables and flips the idea to `promoted`. Those
7
+ -- rows are the outcome; none of them records the ACT. Afterwards the goal looks
8
+ -- exactly like a goal someone scoped in a planning session, and the idea looks
9
+ -- exactly like an idea that became a task. Nothing left says who ratified it,
10
+ -- under which board decision, with what scope, or what the author had
11
+ -- recommended — and "a thought is never lost between having it and learning what
12
+ -- it caused" (criterion `ideator-nothing-is-lost`) is not met by an outcome with
13
+ -- no provenance. This is the `criterion_proposal_audit` argument (migration 042)
14
+ -- applied to the goal half.
15
+ --
16
+ -- `idea_id` IS UNIQUE, AND THAT IS THE IDEMPOTENCY. The transaction also refuses
17
+ -- an idea whose status is no longer `open`, which catches the ordinary second
18
+ -- attempt; this constraint is what catches the two-concurrent-ratifications race
19
+ -- the status check cannot, because both would read `open` before either wrote.
20
+ -- The second INSERT trips 23505 and its whole transaction rolls back, so the
21
+ -- outcome is one goal, never two.
22
+ --
23
+ -- `recommendation_at_filing` IS PROVENANCE, NOT AN INPUT. It stores what the
24
+ -- AUTHOR recommended (ideas_009) beside what the BOARD chose, so the two can be
25
+ -- compared after the fact — how often does the board agree with the ideator? —
26
+ -- without that ever having influenced the ratification. NULL is first-class and
27
+ -- permanent here for the same reason it is on ideas_grades: every Full Idea
28
+ -- filed before BV2.R13 shipped carries no recommendation, there is no backfill,
29
+ -- and inventing one would put a scope opinion in an author's mouth. Nothing may
30
+ -- branch on this column.
31
+ --
32
+ -- FOREIGN KEYS POINT AT CORE TABLES, which is allowed and is what the module's
33
+ -- own precedent does: ideas_002 references `tasks(id)`, ideas_005 references
34
+ -- `builders(id)`. The namespacing rule (ADR 0083 §Decision #5) is about which
35
+ -- tables a module may CREATE and ALTER, not about what its own rows may point
36
+ -- at.
37
+ --
38
+ -- NONE OF THE FOUR FKs CASCADES, and that is the deliberate part. An audit row
39
+ -- exists to say what happened; a cascade would delete the record of a
40
+ -- ratification along with the goal it produced, which is precisely when someone
41
+ -- wants to read it. `criterion_proposal_audit` (migration 042) takes the same
42
+ -- posture on the same reasoning. The consequence is accepted: a hard DELETE of a
43
+ -- ratified goal is refused while its audit row stands, and that refusal is
44
+ -- correct — goals are archived, not deleted.
45
+ --
46
+ -- `scope_modules` is stored as text[] matching `goals.scope_modules`, because
47
+ -- WHICH WALL the goal was given is part of what was decided — a ratification
48
+ -- that admitted a protected module is an Archon act and the row has to say so
49
+ -- even after someone later widens or narrows the live goal.
50
+ --
51
+ -- Module migrations are ADDITIVE and NAMESPACED. There is no down-migration.
52
+ -- Idempotent: safe to re-run.
53
+
54
+ BEGIN;
55
+
56
+ CREATE TABLE IF NOT EXISTS ideas_goal_ratifications (
57
+ id bigserial PRIMARY KEY,
58
+ idea_id bigint NOT NULL REFERENCES idea_inbox(id),
59
+ goal_id bigint NOT NULL REFERENCES goals(id),
60
+ criterion_id bigint NOT NULL REFERENCES done_when_criteria(id),
61
+ board_item_id bigint,
62
+ target_version text NOT NULL,
63
+ scope_modules text[] NOT NULL DEFAULT '{}',
64
+ recommendation_at_filing text,
65
+ proposed_by bigint REFERENCES builders(id),
66
+ ratified_by bigint NOT NULL REFERENCES builders(id),
67
+ notes_md text NOT NULL DEFAULT '',
68
+ ratified_at timestamptz NOT NULL DEFAULT now()
69
+ );
70
+
71
+ -- One ratification per idea — see the header. Added separately and guarded so it
72
+ -- still lands on an instance where the table was created before this clause
73
+ -- existed.
74
+ DO $$
75
+ BEGIN
76
+ IF NOT EXISTS (
77
+ SELECT 1 FROM pg_constraint WHERE conname = 'ideas_goal_ratifications_idea_uniq'
78
+ ) THEN
79
+ ALTER TABLE ideas_goal_ratifications
80
+ ADD CONSTRAINT ideas_goal_ratifications_idea_uniq UNIQUE (idea_id);
81
+ END IF;
82
+ END $$;
83
+
84
+ -- Mirrors ideas_grades_recommendation_chk one table over: the vocabulary is
85
+ -- closed, and NULL means "the author did not say".
86
+ DO $$
87
+ BEGIN
88
+ IF NOT EXISTS (
89
+ SELECT 1 FROM pg_constraint WHERE conname = 'ideas_goal_ratifications_recommendation_chk'
90
+ ) THEN
91
+ ALTER TABLE ideas_goal_ratifications
92
+ ADD CONSTRAINT ideas_goal_ratifications_recommendation_chk
93
+ CHECK (recommendation_at_filing IS NULL OR recommendation_at_filing IN ('goal', 'criterion'));
94
+ END IF;
95
+ END $$;
96
+
97
+ -- "What did this goal come from" — the read the goal surface will want when it
98
+ -- shows a goal's origin. The idea direction is already served by the unique
99
+ -- constraint's index.
100
+ CREATE INDEX IF NOT EXISTS ideas_goal_ratifications_goal_idx
101
+ ON ideas_goal_ratifications (goal_id);
102
+
103
+ COMMIT;
@@ -10,6 +10,7 @@
10
10
  "contributes": {
11
11
  "routes": [
12
12
  "inbox",
13
+ "ratify-goal",
13
14
  "blockers"
14
15
  ],
15
16
  "pollers": [
@@ -80,6 +80,40 @@ const TEMPLATE_KEY_MAX = 64;
80
80
  // payload gets a structured 400 instead of a constraint-violation 500.
81
81
  const FIELD_VALUES_MAX = 32768;
82
82
 
83
+ // -------------------------------------------------------------------------
84
+ // The recommendation (task 1004092 / BV2.R13, spec R22)
85
+ //
86
+ // What the author thinks their Full Idea turned out to be: a goal (a gravity
87
+ // well of its own) or a criterion (a ring inside an existing one). A CLOSED
88
+ // vocabulary, mirroring ideas_grades_recommendation_chk so an unknown value is
89
+ // a named 400 at the route rather than a constraint-violation 500 in the
90
+ // driver.
91
+ //
92
+ // NULL IS A FIRST-CLASS VALUE AND ALWAYS WILL BE — "the author did not say".
93
+ // Every Full Idea filed before this shipped has no recommendation, and there is
94
+ // no backfill, because inventing one would put a scope opinion in someone's
95
+ // mouth. Every reader must render the absence honestly; nothing may substitute
96
+ // a default.
97
+ //
98
+ // IT IS A RECOMMENDATION, NEVER A DECISION (spec decision 9). The ideator
99
+ // recommends; the board decides. Nothing may branch on this column as if it
100
+ // were the outcome — BV2.R17's ratification reads what the board chose.
101
+ const VALID_RECOMMENDATIONS = ['goal', 'criterion'];
102
+
103
+ // The one predicate. `undefined` and `null` both mean "not given" and are
104
+ // legal; anything else must be a declared member. Pure, so a route can refuse
105
+ // before any write and a test can exercise it with no harness.
106
+ function isValidRecommendation(v) {
107
+ return v === null || v === undefined || VALID_RECOMMENDATIONS.includes(v);
108
+ }
109
+
110
+ // Normalise a request-supplied value to what the column stores. Written once
111
+ // because three routes (capture, develop, resubmit) all take the same field and
112
+ // all have to turn "absent" into NULL rather than into the string 'undefined'.
113
+ function normalizeRecommendation(v) {
114
+ return v === undefined || v === '' ? null : v;
115
+ }
116
+
83
117
  // The grade half of an idea read: the COALESCE above, plus the template-shaped
84
118
  // store and the score. `g.*` columns are NULL for an ungraded idea, which is
85
119
  // exactly right — it has no template and no score, it just has a default grade.
@@ -89,16 +123,22 @@ const FIELD_VALUES_MAX = 32768;
89
123
  // hall detail view (R153) and the R147 credit split all want it. NULL for the
90
124
  // overwhelming majority of rows — a quick idea and a self-authored Full Idea were
91
125
  // never developed by anyone — which is the honest answer, not a missing one.
126
+ //
127
+ // `recommendation` (ideas_009, task 1004092 / BV2.R13) rides along for the same
128
+ // reason developed_by does: it is part of a Full Idea's identity from filing
129
+ // onwards, and the board item, the detail read and the author's own trail all
130
+ // want it without a second query. NULL for every quick idea and for every Full
131
+ // Idea filed before the question existed — the honest answer, not a missing one.
92
132
  const GRADE_COLS_Q = `COALESCE(g.grade, '${DEFAULT_GRADE}') AS grade,
93
133
  g.template_key, g.field_values, g.completeness_score,
94
- g.developed_by, g.developed_at`;
134
+ g.developed_by, g.developed_at, g.recommendation`;
95
135
  const IDEA_JOIN_GRADE = `LEFT JOIN ideas_grades g ON g.idea_id = i.id`;
96
136
 
97
137
  // The same projection for a row that provably has no grade row yet (a just-
98
138
  // INSERTed idea), so INSERT…RETURNING does not need a second round trip to
99
139
  // return the same shape every other read returns.
100
140
  function ungradedProjection() {
101
- return { grade: DEFAULT_GRADE, template_key: null, field_values: null, completeness_score: null, developed_by: null, developed_at: null };
141
+ return { grade: DEFAULT_GRADE, template_key: null, field_values: null, completeness_score: null, developed_by: null, developed_at: null, recommendation: null };
102
142
  }
103
143
 
104
144
  module.exports = {
@@ -110,6 +150,9 @@ module.exports = {
110
150
  TEMPLATE_KEY_RE,
111
151
  TEMPLATE_KEY_MAX,
112
152
  FIELD_VALUES_MAX,
153
+ VALID_RECOMMENDATIONS,
154
+ isValidRecommendation,
155
+ normalizeRecommendation,
113
156
  GRADE_COLS_Q,
114
157
  IDEA_JOIN_GRADE,
115
158
  ungradedProjection,
@@ -13,7 +13,9 @@
13
13
  // Archon ratifies it into a version's scope. Keeping it beside the triage
14
14
  // writes made the two look like the same kind of act. They are not.
15
15
 
16
- const { pool } = require('../../src/module-api');
16
+ const api = require('../../src/module-api');
17
+ const { pool } = api;
18
+ const { templateFor, fieldOrder } = require('./templates');
17
19
 
18
20
  // -------------------------------------------------------------------------
19
21
  // Criterion-proposal ratification (V3.R31 / #252)
@@ -203,9 +205,294 @@ async function ratifyCriterionProposal(ideaId, ratifierId, { notesMd = '' } = {}
203
205
  }
204
206
  }
205
207
 
208
+ // -------------------------------------------------------------------------
209
+ // THE GOAL HALF (task 1004096 / BV2.R17, spec R23, criterion
210
+ // `ideator-full-idea-shapes-the-space`)
211
+ //
212
+ // The criterion half above turns a `criterion-proposal` — an idea filed as JSON,
213
+ // explicitly to become a ring inside an existing well — into a
214
+ // `done_when_criteria` row. This half turns a FULL IDEA, filed as five prose
215
+ // answers by someone who was thinking rather than scoping, into a GOAL: a
216
+ // gravity well of its own, with the "what does a successful implementation look
217
+ // like" answer as its first done-when criterion.
218
+ //
219
+ // THE TWO HALVES ARE NOT SYMMETRICAL, and reading them as if they were is the
220
+ // mistake to avoid:
221
+ //
222
+ // • THE SUBJECT DIFFERS. The criterion half parses `body_md` as JSON the
223
+ // proposer wrote FOR ratification. This half reads a graded Full Idea's
224
+ // `field_values` — prose written to express a thought, which nobody wrote
225
+ // with a goal row in mind. So the shape has to be DERIVED, and which answer
226
+ // becomes which part of the goal is a decision recorded below.
227
+ //
228
+ // • THE AUTHORITY DIFFERS. A criterion proposal is one Metic's suggestion and
229
+ // one ratifier's judgement; there is no board in it. A Full Idea has already
230
+ // been through the Board Room (`government.openBoardWindowForIdea` fires when
231
+ // its grade lands), and spec decision 9 is that the ideator RECOMMENDS while
232
+ // the BOARD DECIDES. So this half is GATED on a passed sitting, and the
233
+ // author's own `recommendation` is read for the audit row only.
234
+ //
235
+ // • NOBODY IS PAID HERE. The criterion half awards KARMA_RATIFY_DELTA to the
236
+ // proposer, because nothing else ever will. A Full Idea's author was already
237
+ // paid at the board CLOSE — karma by `awardAuthorRatifyKarma` and credit by
238
+ // `awardIdeaGovernancePassCredit`, both inside the close transaction. Paying
239
+ // again on the ratification would pay twice for one thought, and the second
240
+ // payment would be invisible beside the first. This is why there is no karma
241
+ // INSERT below, and it is a decision rather than an omission.
242
+
243
+ // WHICH ANSWER BECOMES WHICH PART OF THE GOAL, per template vintage.
244
+ //
245
+ // THIS IS RATIFICATION POLICY, NOT TEMPLATE SHAPE, which is why it lives here
246
+ // and not as two more keys inside the declarations in templates.js. Those
247
+ // declarations are FROZEN — a vintage is never edited, because `substanceMin`
248
+ // feeds `completeness_score` and that feeds credits — and "this field supplies a
249
+ // goal's done-when" is a statement about what ratification does with an answer,
250
+ // not about what the question is or what it costs to answer it.
251
+ //
252
+ // `base-v1` IS DELIBERATELY ABSENT, and that is the honest answer rather than a
253
+ // gap. Its five questions are Purpose / Big Picture / Why did you have it / Why
254
+ // does it matter / How does it fit — an argument for a thought, with no answer
255
+ // anywhere in it describing a finished implementation. A goal needs a done-when
256
+ // criterion to be achievable at all (ADR 0263's close gate reads them, so a goal
257
+ // born without one holds its version open forever), and there is nothing in a
258
+ // base-v1 filing to write one FROM. Picking its closest-sounding field would be
259
+ // inventing a success criterion on the author's behalf — the never-invent-a-field
260
+ // rule, and the one place in this file where it bites. A base-v1 Full Idea is
261
+ // refused with TEMPLATE_HAS_NO_GOAL_SHAPE, naming the vintage; its author can
262
+ // resubmit against the current questions, which is what the resubmit path is for.
263
+ //
264
+ // ADDING A VINTAGE MEANS ADDING AN ENTRY HERE. Nothing falls back — an unmapped
265
+ // template refuses, rather than guessing at a key that happens to exist.
266
+ const GOAL_SHAPE_BY_TEMPLATE = Object.freeze({
267
+ // The five questions a Full Idea is asked today (BASE_V2_TEMPLATE). The
268
+ // mapping is the one the proof task (BV2.R25) states: scope from "scope it as
269
+ // best as you can here", done-when from "what does a successful
270
+ // implementation of the idea look like".
271
+ 'base-v2': Object.freeze({ doneWhenField: 'success' }),
272
+ });
273
+
274
+ // composeGoalFromFullIdea — the five answers → the goal's prose + its first
275
+ // criterion. PURE: no DB, no clock, no doorway, so the route can refuse before
276
+ // opening a transaction and a test can exercise every branch with no harness.
277
+ //
278
+ // EVERY ANSWER LANDS IN THE DESCRIPTION, including the one that also becomes the
279
+ // criterion. The done-when is a one-line gate a version-close reads; the
280
+ // description is what a builder opening the goal actually reads, and dropping
281
+ // the success answer from it to avoid "duplication" would take the clearest
282
+ // statement of the goal's purpose out of the one place someone looks for it.
283
+ //
284
+ // ORDER COMES FROM `fieldOrder`, never from Object.keys(field_values) — jsonb
285
+ // does not preserve key order, so iterating the stored row shows the five
286
+ // answers in whatever order Postgres chose (templates.js' own header says so).
287
+ // LABELS come from the declaration too, so a v1 row would render under v1
288
+ // labels if a v1 shape were ever declared.
289
+ function composeGoalFromFullIdea({ ideaId, templateKey, fieldValues } = {}) {
290
+ const template = templateFor(templateKey);
291
+ if (!template) return { ok: false, reason: 'unknown_template', template_key: templateKey ?? null };
292
+ const shape = Object.hasOwn(GOAL_SHAPE_BY_TEMPLATE, templateKey) ? GOAL_SHAPE_BY_TEMPLATE[templateKey] : null;
293
+ if (!shape) return { ok: false, reason: 'template_has_no_goal_shape', template_key: templateKey };
294
+ if (!fieldValues || typeof fieldValues !== 'object' || Array.isArray(fieldValues)) {
295
+ return { ok: false, reason: 'field_values_missing' };
296
+ }
297
+
298
+ const doneWhen = fieldValues[shape.doneWhenField];
299
+ if (typeof doneWhen !== 'string' || doneWhen.trim() === '') {
300
+ // A Full Idea cannot normally reach here with a blank required field —
301
+ // validateFieldValues refuses that at capture. It is checked anyway because
302
+ // the alternative is a goal whose only criterion is an empty string, which
303
+ // the close gate would read as satisfiable by nothing.
304
+ return { ok: false, reason: 'done_when_answer_missing', field: shape.doneWhenField };
305
+ }
306
+
307
+ const sections = [];
308
+ for (const key of fieldOrder(template)) {
309
+ const field = template.fields.find((f) => f.key === key);
310
+ const value = fieldValues[key];
311
+ // A field the stored row simply does not carry is SKIPPED, not rendered as an
312
+ // empty heading. Only required fields are guaranteed present, and a heading
313
+ // with nothing under it reads as a question the author declined to answer
314
+ // rather than one the template added later.
315
+ if (typeof value !== 'string' || value.trim() === '') continue;
316
+ sections.push(`### ${field.label}\n\n${value.trim()}`);
317
+ }
318
+ if (sections.length === 0) return { ok: false, reason: 'field_values_empty' };
319
+
320
+ // The provenance line is FIRST and is prose, not a machine key: the durable
321
+ // machine record is the ideas_goal_ratifications row. This is so a builder
322
+ // opening the goal learns, without asking anyone, that its scope is somebody's
323
+ // thought and where to go and read it whole.
324
+ const origin = `> Ratified from Full Idea [#${ideaId}](/idea/${ideaId}) — its five answers, as written.`;
325
+ return {
326
+ ok: true,
327
+ description: [origin, ...sections].join('\n\n'),
328
+ criterionMd: doneWhen.trim(),
329
+ doneWhenField: shape.doneWhenField,
330
+ };
331
+ }
332
+
333
+ // ratifyFullIdeaAsGoal — the whole act, in one transaction.
334
+ //
335
+ // a. lock the idea + its grade row e. write the ideas_goal_ratifications row
336
+ // b. check the board carried it f. COMMIT
337
+ // c. compose the goal's prose
338
+ // d. create the goal + its criterion (through the `lifecycle` port)
339
+ //
340
+ // WHAT THE CALLER DECIDES AND WHAT IT DOES NOT. `targetVersion`, `scopeModules`
341
+ // and `admitReason` are the BOARD'S choices, stated by the ratifier: no prose
342
+ // answer can supply which version a goal belongs to or which modules it may
343
+ // touch, and guessing either would be the system deciding scope — the exact
344
+ // thing spec decision 9 puts in the board's hands. What the caller may NOT do is
345
+ // decide whether the create is allowed: `actorRank` is passed through to the
346
+ // lifecycle port, which applies every gate POST /goals applies.
347
+ //
348
+ // THE AUTHOR'S `recommendation` IS READ AND NEVER BRANCHED ON. It is copied into
349
+ // the audit row beside what the board chose, so "how often does the board agree
350
+ // with the ideator" is answerable later. A ratification of an idea whose author
351
+ // recommended 'criterion' is NOT refused here — the board may well have decided
352
+ // the thought was bigger than its author thought, and refusing would make the
353
+ // recommendation a decision, which is the one thing it is not.
354
+ async function ratifyFullIdeaAsGoal(
355
+ ideaId, ratifierId,
356
+ { targetVersion, scopeModules = [], admitReason = null, actorRank = null, notesMd = '' } = {}
357
+ ) {
358
+ const id = Number(ideaId);
359
+ if (!Number.isFinite(id)) throw { code: 'BAD_ID' };
360
+ const rid = Number(ratifierId);
361
+ if (!Number.isFinite(rid)) throw { code: 'BAD_RATIFIER' };
362
+ if (!targetVersion || typeof targetVersion !== 'string') throw { code: 'TARGET_VERSION_REQUIRED' };
363
+
364
+ // BOTH PORTS RESOLVE BEFORE THE TRANSACTION OPENS, and both FAIL CLOSED. An
365
+ // instance without the lifecycle module cannot hold goals at all; an instance
366
+ // without government has no board, and "the board decided" cannot be true
367
+ // where there is no board. A missing authority system is a denial, never an
368
+ // admission (the requirePermission posture).
369
+ const lifecycle = api.resolveOptional && api.resolveOptional('lifecycle');
370
+ if (!lifecycle || typeof lifecycle.createRatifiedGoal !== 'function') {
371
+ throw { code: 'NO_LIFECYCLE', detail: 'This instance cannot create goals — the lifecycle module is not mounted.' };
372
+ }
373
+ const government = api.resolveOptional && api.resolveOptional('government');
374
+ if (!government || typeof government.boardPassForIdea !== 'function') {
375
+ throw { code: 'NO_BOARD', detail: 'This instance has no Board Room, so a Full Idea cannot be ratified.' };
376
+ }
377
+
378
+ const client = await pool.connect();
379
+ try {
380
+ await client.query('BEGIN');
381
+
382
+ // a. Read + lock the idea and its grade row together. LEFT JOIN, the module's
383
+ // standing rule: an idea with no grade row reads as 'quick', and that is a
384
+ // refusal below rather than a missing row here. FOR UPDATE names the idea
385
+ // table explicitly — a bare FOR UPDATE over an outer join is not lockable
386
+ // in Postgres, and locking the idea is what serialises two ratifiers.
387
+ const { rows: ideaRows } = await client.query(
388
+ `SELECT i.id, i.title, i.kind, i.status, i.captured_by,
389
+ COALESCE(g.grade, 'quick') AS grade,
390
+ g.template_key, g.field_values, g.recommendation
391
+ FROM idea_inbox i
392
+ LEFT JOIN ideas_grades g ON g.idea_id = i.id
393
+ WHERE i.id = $1
394
+ FOR UPDATE OF i`,
395
+ [id]
396
+ );
397
+ if (ideaRows.length === 0) throw { code: 'IDEA_NOT_FOUND' };
398
+ const idea = ideaRows[0];
399
+ if (idea.grade !== 'full') throw { code: 'NOT_A_FULL_IDEA', grade: idea.grade };
400
+ if (idea.status !== 'open') throw { code: 'IDEA_NOT_OPEN', status: idea.status };
401
+
402
+ // b. The board gate. Spec decision 9 in one statement: without a sitting that
403
+ // closed `passed`, this is a ratifier's preference and not a decision.
404
+ // Read through the port, outside its own transaction (a closed item is
405
+ // terminal, so there is nothing here for a lock to protect).
406
+ const pass = await government.boardPassForIdea(id);
407
+ if (!pass) throw { code: 'BOARD_HAS_NOT_PASSED' };
408
+
409
+ // c. Compose. Pure, and its refusals are the caller's 400s.
410
+ const composed = composeGoalFromFullIdea({
411
+ ideaId: idea.id, templateKey: idea.template_key, fieldValues: idea.field_values,
412
+ });
413
+ if (!composed.ok) throw { code: 'CANNOT_COMPOSE_GOAL', reason: composed.reason, ...composed };
414
+
415
+ // d. The goal + its first criterion, through the port, on THIS client.
416
+ // `criterionRef` is derived from the idea, so UNIQUE (version_id,
417
+ // criterion_id) is a second guard against a double ratification onto the
418
+ // same version — the `proposal-<idea_id>` trick, one half over.
419
+ //
420
+ // created_by IS THE RATIFIER, NOT THE AUTHOR, and that is a security
421
+ // decision rather than a credit one. createGoal makes created_by the
422
+ // goal's owner AND inserts a 'lead' membership row, and goal leads author
423
+ // tasks inside the scope wall. Filing an idea is open to every rank, so
424
+ // owning the goal your idea became would turn "have a thought that the
425
+ // board likes" into a grant of authority nobody reviewed. The author's
426
+ // claim on the thought is preserved where claims belong — the audit row
427
+ // below, and the origin line in the description.
428
+ const made = await lifecycle.createRatifiedGoal({
429
+ versionId: targetVersion,
430
+ title: idea.title,
431
+ description: composed.description,
432
+ scopeModules,
433
+ createdBy: rid,
434
+ criterionMd: composed.criterionMd,
435
+ criterionRef: `idea-${idea.id}`,
436
+ admitReason,
437
+ actorRank,
438
+ }, { client });
439
+ if (!made.ok) throw { code: made.code, status: made.status, ...made };
440
+
441
+ // e. Mark the idea promoted. promoted_to_task_id stays NULL — a goal is not
442
+ // a task, the same reading the criterion half takes.
443
+ await client.query(
444
+ `UPDATE idea_inbox
445
+ SET status = 'promoted',
446
+ promoted_to_task_id = NULL,
447
+ reviewed_at = now()
448
+ WHERE id = $1`,
449
+ [idea.id]
450
+ );
451
+
452
+ // f. The audit row — who ratified what, under which decision, with what the
453
+ // author had recommended beside it.
454
+ const { rows: auditRows } = await client.query(
455
+ `INSERT INTO ideas_goal_ratifications
456
+ (idea_id, goal_id, criterion_id, board_item_id, target_version,
457
+ scope_modules, recommendation_at_filing, proposed_by, ratified_by, notes_md)
458
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
459
+ RETURNING id, ratified_at`,
460
+ [
461
+ idea.id, made.goal.id, made.criterion.id, pass.item_id, targetVersion,
462
+ Array.isArray(scopeModules) ? scopeModules : [],
463
+ idea.recommendation ?? null,
464
+ idea.captured_by, rid, notesMd || '',
465
+ ]
466
+ );
467
+
468
+ await client.query('COMMIT');
469
+ return {
470
+ goal: made.goal,
471
+ criterion: made.criterion,
472
+ admitted: made.admitted,
473
+ audit_id: auditRows[0].id,
474
+ ratified_at: auditRows[0].ratified_at,
475
+ board_item_id: pass.item_id,
476
+ // Reported back so a caller can see what the author had recommended and
477
+ // what the board did with it, without a second read. NULL stays NULL.
478
+ recommendation_at_filing: idea.recommendation ?? null,
479
+ done_when_from: composed.doneWhenField,
480
+ proposer_id: idea.captured_by,
481
+ };
482
+ } catch (err) {
483
+ await client.query('ROLLBACK').catch(() => {});
484
+ throw err;
485
+ } finally {
486
+ client.release();
487
+ }
488
+ }
489
+
206
490
  module.exports = {
207
491
  KARMA_RATIFY_DELTA,
208
492
  KARMA_RATIFY_REASON,
209
493
  parseProposalBody,
210
494
  ratifyCriterionProposal,
495
+ GOAL_SHAPE_BY_TEMPLATE,
496
+ composeGoalFromFullIdea,
497
+ ratifyFullIdeaAsGoal,
211
498
  };