@hikae/ledger-core 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from "./stage.js";
3
3
  export * from "./pr.js";
4
4
  export * from "./holds.js";
5
5
  export * from "./ports.js";
6
+ export * from "./review.js";
package/dist/index.js CHANGED
@@ -3,3 +3,4 @@ export * from "./stage.js";
3
3
  export * from "./pr.js";
4
4
  export * from "./holds.js";
5
5
  export * from "./ports.js";
6
+ export * from "./review.js";
@@ -0,0 +1,103 @@
1
+ type Row = Record<string, any>;
2
+ export declare const RELEASE_WAITING: Set<string>;
3
+ export declare function applyRelease(row: Row | null, data: Row, now?: string): {
4
+ status: string;
5
+ last_synced_at: string;
6
+ delivery: any;
7
+ } | null;
8
+ export declare function completeRelease(row: Row, release: Row, now?: string): {
9
+ status: string;
10
+ last_synced_at: string;
11
+ delivery: any;
12
+ };
13
+ export declare const needsReview: (before: Row | null, next: Row | null) => boolean;
14
+ export declare function applyReview(row: Row | null, data: Row, now?: string): Row | ({
15
+ last_synced_at: string;
16
+ } & {
17
+ status: "queued";
18
+ last_event: string;
19
+ }) | null;
20
+ export declare function toReviewCard(row: Row): {
21
+ kind: string;
22
+ url: any;
23
+ repository: any;
24
+ pr_number: any;
25
+ status: any;
26
+ priority: any;
27
+ title: any;
28
+ author: any;
29
+ action: any;
30
+ last_error: any;
31
+ updated_at: any;
32
+ added_at: any;
33
+ approved_at: any;
34
+ changes_requested_at: any;
35
+ merged_at: any;
36
+ completed_at: any;
37
+ queue: null;
38
+ };
39
+ export declare const ANSWERABLE: Set<string>;
40
+ export declare const OPTIONS: string[];
41
+ export declare function decisionCard(row: Row | null, history?: unknown[]): {
42
+ history: unknown[];
43
+ summary_ja?: undefined;
44
+ question_ja?: undefined;
45
+ option_labels?: undefined;
46
+ summary_model?: undefined;
47
+ url: any;
48
+ repository: any;
49
+ pr_number: any;
50
+ title: any;
51
+ head: any;
52
+ reason: any;
53
+ asked_at: any;
54
+ held: boolean;
55
+ question: any;
56
+ options: string[];
57
+ } | {
58
+ history: unknown[];
59
+ summary_ja: string;
60
+ question_ja: any;
61
+ option_labels: {
62
+ [k: string]: any;
63
+ };
64
+ summary_model: any;
65
+ url: any;
66
+ repository: any;
67
+ pr_number: any;
68
+ title: any;
69
+ head: any;
70
+ reason: any;
71
+ asked_at: any;
72
+ held: boolean;
73
+ question: any;
74
+ options: string[];
75
+ } | null;
76
+ export declare function answerRow(row: Row | null, { head, choice, custom, }: {
77
+ head?: unknown;
78
+ choice?: unknown;
79
+ custom?: unknown;
80
+ } | undefined, by: string, now?: string): "The row is not waiting for the owner." | "The PR moved; reload." | "Invalid answer." | {
81
+ next: Row;
82
+ record: {
83
+ kind: string;
84
+ url: any;
85
+ repository: any;
86
+ pr_number: any;
87
+ head_sha: string;
88
+ at: string;
89
+ by: string;
90
+ choice: string;
91
+ note: string | null;
92
+ from_status: any;
93
+ to_status: any;
94
+ prior_decision: {
95
+ action: any;
96
+ reason: any;
97
+ summary: any;
98
+ run_url: any;
99
+ } | null;
100
+ evidence_sha256: any;
101
+ };
102
+ };
103
+ export {};
package/dist/review.js ADDED
@@ -0,0 +1,320 @@
1
+ import { ALL_STATUSES, PRE_MERGE, invalidate } from "./pr.js";
2
+ const statuses = new Set(ALL_STATUSES);
3
+ const SCHEMA_VERSION = 1;
4
+ const DIRECT_PRIORITY = 50;
5
+ const SCOPE_PRIORITY = 50;
6
+ export const RELEASE_WAITING = new Set([
7
+ "merged_waiting_release",
8
+ "release_in_progress",
9
+ ]);
10
+ // Applies one release signal to a merged row. Returns the next row, or null when nothing is recorded.
11
+ export function applyRelease(row, data, now = new Date().toISOString()) {
12
+ if (!row ||
13
+ !RELEASE_WAITING.has(row.status) ||
14
+ row.repository !== data.repository.full_name)
15
+ return null;
16
+ const rel = data.release, merge = row.delivery?.merge || {};
17
+ const signal = {
18
+ source: rel.source,
19
+ sha: rel.sha,
20
+ id: rel.id,
21
+ url: rel.url,
22
+ at: now,
23
+ };
24
+ if (rel.state === "success" && rel.sha && rel.sha === merge.merge_commit_sha)
25
+ return completeRelease(row, { ...signal, reason: "deploy-includes-merge" }, now);
26
+ if (row.status === "release_in_progress" &&
27
+ row.delivery?.release?.sha === rel.sha &&
28
+ rel.state !== "success")
29
+ return null;
30
+ return {
31
+ ...row,
32
+ status: "release_in_progress",
33
+ last_synced_at: now,
34
+ delivery: {
35
+ ...(row.delivery || {}),
36
+ release: { ...signal, state: rel.state },
37
+ },
38
+ };
39
+ }
40
+ export function completeRelease(row, release, now = new Date().toISOString()) {
41
+ return {
42
+ ...row,
43
+ status: "completed",
44
+ last_synced_at: now,
45
+ delivery: {
46
+ ...(row.delivery || {}),
47
+ release,
48
+ completed_at: now,
49
+ completion_reason: release.reason,
50
+ },
51
+ };
52
+ }
53
+ // A row needs a review run when it newly became queued, or is queued for a head it has not been queued for.
54
+ export const needsReview = (before, next) => next?.status === "queued" &&
55
+ (before?.status !== "queued" ||
56
+ before?.live?.head_sha !== next.live?.head_sha);
57
+ const live = (row, pr, now) => ({
58
+ ...row,
59
+ live: {
60
+ ...(row.live || {}),
61
+ title: pr.title,
62
+ author: { login: pr.author },
63
+ head_sha: pr.head_sha,
64
+ draft: pr.draft,
65
+ state: pr.state,
66
+ },
67
+ last_synced_at: now,
68
+ });
69
+ // The periodic review sweep needs these to mint a token for the row's repository.
70
+ function ids(row, data) {
71
+ if (Number.isSafeInteger(data.repository?.id))
72
+ row.repository_id = data.repository.id;
73
+ if (Number.isSafeInteger(data.installation_id))
74
+ row.installation_id = data.installation_id;
75
+ return row;
76
+ }
77
+ // A changed head or a return to draft invalidates review evidence, as the local sync's HEAD_CHANGED / DRAFTED do:
78
+ // the row returns to queued and an existing approval awaits re-verification.
79
+ // Applies one event to a row (or null when untracked). Returns the next row, or null when nothing is recorded.
80
+ export function applyReview(row, data, now = new Date().toISOString()) {
81
+ const pr = data.pr, action = data.conclusion;
82
+ if (action === "ci_completed") {
83
+ if (!row || row.live?.head_sha !== pr.head_sha)
84
+ return null;
85
+ // A review in flight may have seen CI pending: record the completion so its publish does not park the row.
86
+ if (row.status === "queued")
87
+ return {
88
+ ...row,
89
+ ci_completed: { at: now, head_sha: pr.head_sha, label: data.label },
90
+ last_synced_at: now,
91
+ };
92
+ if (row.status !== "waiting_ci")
93
+ return null;
94
+ return invalidate({ ...row, last_synced_at: now }, "CI_COMPLETED");
95
+ }
96
+ if (!row) {
97
+ if (pr.state !== "open")
98
+ return null;
99
+ if (action === "review_requested")
100
+ return ids(live({
101
+ schema_version: SCHEMA_VERSION,
102
+ repository: data.repository.full_name,
103
+ pr_number: pr.number,
104
+ url: pr.url,
105
+ status: "queued",
106
+ priority: DIRECT_PRIORITY,
107
+ added_at: now,
108
+ source: "webhook",
109
+ review_request: { active: true, requested_at: now },
110
+ }, pr, now), data);
111
+ if (!data.scope || pr.draft)
112
+ return null;
113
+ return ids(live({
114
+ schema_version: SCHEMA_VERSION,
115
+ repository: data.repository.full_name,
116
+ pr_number: pr.number,
117
+ url: pr.url,
118
+ status: "queued",
119
+ priority: SCOPE_PRIORITY,
120
+ added_at: now,
121
+ source: "webhook",
122
+ scope: data.scope,
123
+ }, pr, now), data);
124
+ }
125
+ let next = live(row, pr, now);
126
+ if (data.scope && !row.scope)
127
+ next.scope = data.scope;
128
+ ids(next, data);
129
+ const headChanged = row.live?.head_sha && row.live.head_sha !== pr.head_sha;
130
+ if (action === "review_requested") {
131
+ next.review_request = {
132
+ ...(row.review_request || {}),
133
+ active: true,
134
+ requested_at: now,
135
+ };
136
+ if (row.status === "closed" && pr.state === "open")
137
+ next = invalidate(next, "REQUESTED");
138
+ }
139
+ else if (action === "review_request_removed") {
140
+ next.review_request = {
141
+ ...(row.review_request || {}),
142
+ active: false,
143
+ removed_at: now,
144
+ };
145
+ }
146
+ else if (action === "closed") {
147
+ if (pr.merged) {
148
+ if (PRE_MERGE.has(row.status))
149
+ next.status = "merged_waiting_release";
150
+ next.delivery = {
151
+ ...(row.delivery || {}),
152
+ merge: {
153
+ merged_at: pr.merged_at || now,
154
+ ...(pr.merge_commit_sha
155
+ ? { merge_commit_sha: pr.merge_commit_sha }
156
+ : {}),
157
+ },
158
+ };
159
+ }
160
+ else if (PRE_MERGE.has(row.status))
161
+ next.status = "closed";
162
+ }
163
+ else if (action === "reopened") {
164
+ if (row.status === "closed")
165
+ next = invalidate(next, "REOPENED");
166
+ }
167
+ else if (action === "converted_to_draft") {
168
+ if (PRE_MERGE.has(row.status) && row.status !== "queued")
169
+ next = invalidate(next, "DRAFTED");
170
+ }
171
+ else if (action === "synchronize" &&
172
+ headChanged &&
173
+ PRE_MERGE.has(row.status) &&
174
+ row.status !== "queued") {
175
+ next = invalidate(next, "HEAD_CHANGED");
176
+ }
177
+ if (!statuses.has(next.status))
178
+ throw new Error("Invalid ledger status: " + next.status);
179
+ return next;
180
+ }
181
+ // Same projection as the local ledger's toCard(), so its Web UI reads these rows unchanged.
182
+ export function toReviewCard(row) {
183
+ return {
184
+ kind: "review",
185
+ url: row.url,
186
+ repository: row.repository,
187
+ pr_number: row.pr_number,
188
+ status: row.status,
189
+ priority: row.priority ?? null,
190
+ title: row.live?.title ?? null,
191
+ author: row.live?.author?.login ?? null,
192
+ action: row.decision?.action ?? null,
193
+ last_error: row.last_error ?? null,
194
+ updated_at: row.last_synced_at ?? null,
195
+ added_at: row.added_at ?? null,
196
+ approved_at: row.approval?.approved_at ?? null,
197
+ changes_requested_at: row.change_request?.submitted_at ?? null,
198
+ merged_at: row.delivery?.merge?.merged_at ?? null,
199
+ completed_at: row.delivery?.completed_at ?? null,
200
+ queue: null,
201
+ };
202
+ }
203
+ // Owner answers. A row the reviewer could not settle (human_required) or sent back (changes_requested) is a pending
204
+ // decision in the local ledger's /api/decisions shape; the owner's answer is recorded as an owner correction.
205
+ export const ANSWERABLE = new Set(["human_required", "changes_requested"]);
206
+ export const OPTIONS = ["Approve", "Request changes", "Retry", "Hold"];
207
+ const held = (row) => row.owner_answer?.choice === "Hold" &&
208
+ row.owner_answer.head_sha === row.live?.head_sha;
209
+ export function decisionCard(row, history = []) {
210
+ if (!row || !ANSWERABLE.has(row.status))
211
+ return null;
212
+ return {
213
+ url: row.url,
214
+ repository: row.repository,
215
+ pr_number: row.pr_number,
216
+ title: row.live?.title ?? "",
217
+ head: row.live?.head_sha ?? null,
218
+ reason: row.decision?.reason || row.status,
219
+ asked_at: row.decision?.reviewed_at || row.last_synced_at || null,
220
+ held: held(row),
221
+ question: row.decision?.summary || row.status,
222
+ options: OPTIONS,
223
+ ...ownerView(row.decision?.owner_view),
224
+ history,
225
+ };
226
+ }
227
+ // The summarizer's Japanese view of a decision (scripts/summarizer.py), only when it kept every machine choice id;
228
+ // otherwise the card carries no view and the UI shows the English question and the ids.
229
+ function ownerView(view) {
230
+ const decision = view?.decisions?.[0];
231
+ if (typeof view?.summary_ja !== "string" ||
232
+ typeof decision?.question_ja !== "string" ||
233
+ !Array.isArray(decision.options))
234
+ return {};
235
+ const labels = Object.fromEntries(decision.options
236
+ .filter((o) => OPTIONS.includes(o?.id) && typeof o.label_ja === "string")
237
+ .map((o) => [
238
+ o.id,
239
+ {
240
+ label: o.label_ja,
241
+ description: typeof o.description_ja === "string" ? o.description_ja : "",
242
+ },
243
+ ]));
244
+ return {
245
+ summary_ja: view.summary_ja,
246
+ question_ja: decision.question_ja,
247
+ option_labels: labels,
248
+ summary_model: view.model ?? null,
249
+ };
250
+ }
251
+ const TARGET = {
252
+ Approve: ["ready_to_approve", "approve"],
253
+ "Request changes": ["changes_requested", "request_changes"],
254
+ };
255
+ // The next row and its history record for one owner answer, or a message string when the answer does not apply.
256
+ export function answerRow(row, { head, choice, custom, } = {}, by, now = new Date().toISOString()) {
257
+ if (!row || !ANSWERABLE.has(row.status))
258
+ return "The row is not waiting for the owner.";
259
+ if (typeof head !== "string" || head !== row.live?.head_sha)
260
+ return "The PR moved; reload.";
261
+ if (typeof choice !== "string" || !choice.trim() || choice.length > 2000)
262
+ return "Invalid answer.";
263
+ const free = custom === true;
264
+ if (!free && !OPTIONS.includes(choice))
265
+ return "Invalid answer.";
266
+ const answer = {
267
+ choice: free ? "Other" : choice,
268
+ note: free ? choice.trim() : null,
269
+ head_sha: head,
270
+ by,
271
+ at: now,
272
+ };
273
+ let next;
274
+ if (!free && TARGET[choice]) {
275
+ const [status, action] = TARGET[choice];
276
+ next = {
277
+ ...row,
278
+ status,
279
+ last_synced_at: now,
280
+ decision: {
281
+ ...(row.decision || {}),
282
+ action,
283
+ reason: "owner-correction",
284
+ corrected_from: row.decision?.action ?? null,
285
+ },
286
+ };
287
+ }
288
+ else if (!free && choice === "Hold") {
289
+ next = { ...row, last_synced_at: now };
290
+ }
291
+ else {
292
+ // Retry, or a free-text answer the next review reads: back to queued for the same head.
293
+ next = invalidate({ ...row, last_synced_at: now }, "OWNER_ANSWERED");
294
+ }
295
+ next.owner_answer = answer;
296
+ const d = row.decision;
297
+ const record = {
298
+ kind: "owner_correction",
299
+ url: row.url,
300
+ repository: row.repository,
301
+ pr_number: row.pr_number,
302
+ head_sha: head,
303
+ at: now,
304
+ by,
305
+ choice: answer.choice,
306
+ note: answer.note,
307
+ from_status: row.status,
308
+ to_status: next.status,
309
+ prior_decision: d
310
+ ? {
311
+ action: d.action ?? null,
312
+ reason: d.reason ?? null,
313
+ summary: d.summary ?? null,
314
+ run_url: d.run_url ?? null,
315
+ }
316
+ : null,
317
+ evidence_sha256: row.evidence_sha256 ?? null,
318
+ };
319
+ return { next, record };
320
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hikae/ledger-core",
3
- "version": "0.0.0",
3
+ "version": "0.2.0",
4
4
  "description": "Pure core shared by the local ledger agent and the remote hikaevi host: model tier policy, issue stage machine, port interfaces.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -46,12 +46,14 @@
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rm -rf dist && tsc -p .",
49
- "test": "npm run build && node --test test/*.test.mjs"
49
+ "test": "npm run build && node --test test/*.test.mjs",
50
+ "prepublishOnly": "npm test"
50
51
  },
51
52
  "devDependencies": {
52
53
  "typescript": "5.9.3"
53
54
  },
54
55
  "publishConfig": {
55
- "access": "public"
56
+ "access": "public",
57
+ "provenance": true
56
58
  }
57
59
  }