bareloop 0.54.1 → 0.56.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 +122 -0
- package/bareloop.context.md +67 -6
- package/package.json +3 -3
- package/src/authorflow.js +217 -238
- package/src/authoring.js +22 -6
- package/src/authorjob.js +138 -30
- package/src/authorreadout.js +15 -4
- package/src/authorrun.js +13 -4
- package/src/authorscout.js +9 -6
- package/src/behaviour.js +14 -0
- package/src/blockasks.js +43 -0
- package/src/cardschedule.js +42 -0
- package/src/checkblock.js +772 -0
- package/src/checklist.js +128 -0
- package/src/checkpick.js +294 -0
- package/src/checksuggest.js +28 -0
- package/src/checkwords.js +27 -0
- package/src/cli.js +1 -1
- package/src/config.js +24 -1
- package/src/declaredclose.js +30 -1
- package/src/destpoint.js +151 -0
- package/src/draftspend.js +85 -0
- package/src/facts.js +8 -1
- package/src/factwords.js +14 -1
- package/src/fitcheck.js +5 -3
- package/src/job.js +81 -1
- package/src/jobblock.js +9 -0
- package/src/labelscan.js +11 -0
- package/src/launcher.js +3 -2
- package/src/mcpchecks.js +52 -0
- package/src/mcppick.js +36 -0
- package/src/mcpsources.js +77 -0
- package/src/mcpuse.js +97 -0
- package/src/natural.js +23 -0
- package/src/pagemap.js +20 -5
- package/src/panel/authorroutes.js +105 -18
- package/src/panel/authorsession.js +293 -44
- package/src/panel/index.html +873 -41
- package/src/panel/jobgrid.js +117 -0
- package/src/panel/jobplan.js +103 -35
- package/src/panel/scheduler.js +80 -13
- package/src/panel/schedules.js +189 -28
- package/src/panel/schedulestart.js +44 -13
- package/src/panel/server.js +471 -39
- package/src/panel/settingsroutes.js +133 -3
- package/src/panel/status.js +19 -1
- package/src/panel/tickets.js +44 -0
- package/src/pickrecord.js +163 -0
- package/src/placerule.js +355 -0
- package/src/plan.js +31 -2
- package/src/planrun.js +71 -15
- package/src/prcheckout.js +259 -0
- package/src/promptregisters.js +11 -0
- package/src/questionbox.js +112 -0
- package/src/readshim.js +3 -2
- package/src/revises.js +56 -0
- package/src/rowedit.js +115 -0
- package/src/rubricstep.js +265 -132
- package/src/run.js +19 -2
- package/src/runexport.js +2 -0
- package/src/runlist.js +3 -2
- package/src/schedulewords.js +46 -4
- package/src/source.js +81 -7
- package/src/stepmcp.js +86 -0
- package/src/ticket.js +113 -0
- package/src/ticketfilter.js +76 -0
- package/src/ticketlabels.js +79 -0
- package/src/ticketmcp.js +111 -0
- package/src/ticketnext.js +35 -0
- package/src/ticketrepo.js +98 -0
- package/src/ticketschedule.js +32 -0
- package/src/ticketshape.js +45 -0
- package/src/tools.js +9 -0
- package/src/trigger.js +14 -2
- package/src/triggerclock.js +7 -3
- package/src/triggerfiles.js +19 -11
- package/src/triggertickets.js +244 -0
- package/src/userrun.js +191 -10
- package/src/validate.js +3 -1
- package/src/worktree.js +43 -1
- package/types/authorflow.d.ts +86 -93
- package/types/authorjob.d.ts +35 -4
- package/types/authorreadout.d.ts +4 -3
- package/types/authorscout.d.ts +4 -2
- package/types/behaviour.d.ts +12 -0
- package/types/blockasks.d.ts +37 -0
- package/types/cardschedule.d.ts +24 -0
- package/types/checkblock.d.ts +361 -0
- package/types/checklist.d.ts +134 -0
- package/types/checkpick.d.ts +160 -0
- package/types/checksuggest.d.ts +30 -0
- package/types/checkwords.d.ts +14 -0
- package/types/config.d.ts +7 -0
- package/types/declaredclose.d.ts +4 -0
- package/types/destpoint.d.ts +89 -0
- package/types/draftspend.d.ts +37 -0
- package/types/factwords.d.ts +7 -0
- package/types/fitcheck.d.ts +7 -1
- package/types/job.d.ts +13 -0
- package/types/jobblock.d.ts +9 -0
- package/types/labelscan.d.ts +7 -0
- package/types/launcher.d.ts +2 -2
- package/types/mcpchecks.d.ts +8 -0
- package/types/mcppick.d.ts +33 -0
- package/types/mcpsources.d.ts +51 -0
- package/types/mcpuse.d.ts +15 -0
- package/types/natural.d.ts +2 -0
- package/types/pagemap.d.ts +6 -1
- package/types/panel/authorroutes.d.ts +11 -2
- package/types/panel/authorsession.d.ts +69 -3
- package/types/panel/jobgrid.d.ts +405 -0
- package/types/panel/jobplan.d.ts +55 -11
- package/types/panel/scheduler.d.ts +7 -2
- package/types/panel/schedules.d.ts +124 -4
- package/types/panel/schedulestart.d.ts +21 -3
- package/types/panel/server.d.ts +432 -10
- package/types/panel/settingsroutes.d.ts +4 -2
- package/types/panel/status.d.ts +16 -2
- package/types/panel/tickets.d.ts +61 -0
- package/types/pickrecord.d.ts +100 -0
- package/types/placerule.d.ts +194 -0
- package/types/plan.d.ts +1 -0
- package/types/prcheckout.d.ts +151 -0
- package/types/questionbox.d.ts +87 -0
- package/types/readshim.d.ts +4 -3
- package/types/revises.d.ts +23 -0
- package/types/rowedit.d.ts +32 -0
- package/types/rubricstep.d.ts +100 -48
- package/types/run.d.ts +27 -1
- package/types/runlist.d.ts +6 -3
- package/types/schedulewords.d.ts +29 -2
- package/types/source.d.ts +40 -4
- package/types/stepmcp.d.ts +40 -0
- package/types/ticket.d.ts +114 -0
- package/types/ticketfilter.d.ts +34 -0
- package/types/ticketlabels.d.ts +44 -0
- package/types/ticketmcp.d.ts +84 -0
- package/types/ticketnext.d.ts +33 -0
- package/types/ticketrepo.d.ts +118 -0
- package/types/ticketschedule.d.ts +21 -0
- package/types/ticketshape.d.ts +37 -0
- package/types/tools.d.ts +1 -0
- package/types/trigger.d.ts +9 -1
- package/types/triggerclock.d.ts +6 -2
- package/types/triggerfiles.d.ts +13 -5
- package/types/triggertickets.d.ts +110 -0
- package/types/userrun.d.ts +50 -0
- package/types/validate.d.ts +1 -0
- package/types/worktree.d.ts +19 -0
package/src/triggerfiles.js
CHANGED
|
@@ -22,25 +22,33 @@ import {
|
|
|
22
22
|
import { basename, dirname, join } from 'node:path';
|
|
23
23
|
import { createHash } from 'node:crypto';
|
|
24
24
|
import { appendRow, readLedger, withoutReleased } from './triggerledger.js';
|
|
25
|
+
import { compareNatural } from './natural.js';
|
|
25
26
|
|
|
26
|
-
/** @typedef {{id:string, kind:'baseline'|'arrived'|'ran'|'dropped', sha:string, name:string, size:number, at:number, sig?:string, path?:string, why?:string, of?:string}} SeenRow */
|
|
27
|
+
/** @typedef {{id:string, kind:'baseline'|'arrived'|'ran'|'dropped'|'unreadable'|'looked', sha:string, name:string, size:number, at:number, sig?:string, path?:string, why?:string, of?:string}} SeenRow */
|
|
27
28
|
/** @typedef {{path:string, sha:string, size:number}} ToRun */
|
|
28
29
|
/** @typedef {{path:string, why:string}} Refused */
|
|
29
30
|
|
|
30
31
|
/** Consecutive polls with the same size and mtime before a file is taken. (Two matches in a row = taken on the third sighting.) */
|
|
31
32
|
export const STABLE_POLLS = 2;
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
// `unreadable` / `looked` are the ticket look's own rows (src/triggertickets.js): a look that could not read, and the first look that read after one
|
|
35
|
+
const SEEN_KINDS = ['baseline', 'arrived', 'ran', 'dropped', 'unreadable', 'looked'];
|
|
34
36
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
* The seen-kind rows of the job's ledger that still count (a released `ran` does not). `sig` (the ticket look only) = just the rows of that signing; absent = every row.
|
|
39
|
+
* @param {string} file @param {string} [sig] @returns {SeenRow[]}
|
|
40
|
+
*/
|
|
41
|
+
export function readSeen(file, sig) {
|
|
42
|
+
return /** @type {SeenRow[]} */ (withoutReleased(readLedger(file).rows).filter((r) => SEEN_KINDS.includes(r.kind) && (sig === undefined || r.sig === sig)));
|
|
38
43
|
}
|
|
39
44
|
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The ONE appender here: write-once by live id (a second row with the same id is not written). With `sig` (the ticket look) the id is unique within that signing and the row is stamped with it.
|
|
47
|
+
* @param {string} file @param {SeenRow} row @param {string} [sig] @returns {boolean} written
|
|
48
|
+
*/
|
|
49
|
+
export function appendSeen(file, row, sig) {
|
|
50
|
+
if (readSeen(file, sig).some((r) => r.id === row.id)) return false;
|
|
51
|
+
appendRow(file, sig === undefined ? row : { ...row, sig });
|
|
44
52
|
return true;
|
|
45
53
|
}
|
|
46
54
|
/** Rows of one sha, file order. @param {SeenRow[]} rows @param {string} sha */
|
|
@@ -79,13 +87,13 @@ export function recordDropped(ledger, item, why, at, sig) {
|
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
/**
|
|
82
|
-
* The queue: arrived and neither ran nor dropped, oldest arrival first. Identical arrival times: by name, then by sha (deterministic).
|
|
90
|
+
* The queue: arrived and neither ran nor dropped, oldest arrival first. Identical arrival times: by name (numbers as numbers, `#9` before `#10`), then by sha (deterministic).
|
|
83
91
|
* @param {SeenRow[]} rows @returns {(SeenRow & {path:string})[]}
|
|
84
92
|
*/
|
|
85
93
|
export function queueOf(rows) {
|
|
86
94
|
const cmp = (/** @type {string} */ x, /** @type {string} */ y) => (x < y ? -1 : x > y ? 1 : 0);
|
|
87
95
|
return /** @type {any} */ (rows.filter((r) => waiting(rows, r)))
|
|
88
|
-
.sort((x, y) => x.at - y.at ||
|
|
96
|
+
.sort((x, y) => x.at - y.at || compareNatural(x.name, y.name) || cmp(x.sha, y.sha));
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
/**
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// borrowed-from: fwdloop src/trigger-tickets.js@v0.29.0 (adapted: bareloop's ticketmcp.js / ticketshape.js / triggerfiles.js; an ISSUES list only)
|
|
2
|
+
// MCP + tickets piece 3 (ITEM33-BUILD): the ticket trigger. Proven first in the R6 POC (.claude/scratch/mcp-tickets/r6/live.mjs, fwd F103/F104/F94b).
|
|
3
|
+
//
|
|
4
|
+
// This file only says WHAT is new on a ticket list: `lookTickets` is ONE $0 look through the person's own MCP server, started with `allow` = only the
|
|
5
|
+
// signed `list_issues` tool, and it writes nothing. The ledger rows (baseline / arrived / ran / unreadable / looked) are the scheduler's, written by the functions
|
|
6
|
+
// below, the same rows as the file trigger's (src/triggerfiles.js): a ticket is `sha: ticket:<number>` (a re-arrival by a watched change: `ticket:<number>:<k>`),
|
|
7
|
+
// `name: #<number>`, `size: 0`, so `queueOf` and `recordDropped` work on it unchanged. Every row carries `sig`; the ids are unique within one signing.
|
|
8
|
+
import { createMCPBridge, discoverServers } from 'bare-agent/mcp';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
configForServer, remoteRefusal, mcpStartOpts, startRefusal, firstLine, isDeviceLogin, mcpToolName,
|
|
12
|
+
} from './ticketmcp.js';
|
|
13
|
+
import { parseTicketList, isTicketNumber, TICKET_MAX } from './ticketshape.js';
|
|
14
|
+
import { appendSeen, queueOf, readSeen } from './triggerfiles.js';
|
|
15
|
+
|
|
16
|
+
/** A look every 15 minutes, fixed (item 8). */
|
|
17
|
+
export const TICKET_LOOK_MS = 15 * 60_000;
|
|
18
|
+
/** The tool the look reads with: the signed `mcp.tools` must contain it (a schedule on a ticket job is refused at sign time otherwise). */
|
|
19
|
+
export const LOOK_TOOL = 'list_issues';
|
|
20
|
+
/** The most pages one look follows (a flood bigger than this is `unreadable`, never silently cut). */
|
|
21
|
+
const MAX_PAGES = 20;
|
|
22
|
+
|
|
23
|
+
/** @param {unknown} reply */
|
|
24
|
+
const textOf = (reply) => (typeof reply === 'string' ? reply : (Array.isArray(reply) ? reply : /** @type {any} */ (reply)?.content ?? []).map((/** @type {any} */ c) => c?.text ?? '').join(''));
|
|
25
|
+
/** @param {any} i */
|
|
26
|
+
const labelsOf = (i) => (Array.isArray(i?.labels) ? i.labels : []).map((/** @type {any} */ l) => (typeof l === 'string' ? l : l?.name)).filter((/** @type {unknown} */ n) => typeof n === 'string').sort();
|
|
27
|
+
/** @param {any} i the typed values of the watched fields */
|
|
28
|
+
export const watchedOf = (i) => ({ state: String(i?.state ?? '').toLowerCase(), labels: labelsOf(i) });
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Why a ticket changed, in words, only for the watched fields; '' = nothing changed. @param {{state: string, labels: string[]}} prev @param {{state: string, labels: string[]}} cur @param {string[]} watch
|
|
32
|
+
*/
|
|
33
|
+
export function reasonFor(prev, cur, watch) {
|
|
34
|
+
const r = [];
|
|
35
|
+
if (watch.includes('state') && prev.state !== cur.state) r.push(`state ${prev.state}→${cur.state}`);
|
|
36
|
+
if (watch.includes('labels')) {
|
|
37
|
+
for (const l of cur.labels) if (!prev.labels.includes(l)) r.push(`label added: ${l}`);
|
|
38
|
+
for (const l of prev.labels) if (!cur.labels.includes(l)) r.push(`label removed: ${l}`);
|
|
39
|
+
}
|
|
40
|
+
return r.join('; ');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* One look. `seen` null = the first look after signing (the first page is the starting point, nothing runs). Otherwise newest first with the cursor, stopping at a seen
|
|
45
|
+
* number; with watched fields the walk goes on past it only as far as the oldest tracked ticket, comparing each tracked ticket's watched values with `last`.
|
|
46
|
+
* Every failure is `{ok:false, error}` naming its cause, at $0; none reads as "0 new".
|
|
47
|
+
* `cut` (first look only) = the highest ticket number the job's first green run read: the first look goes on past a page whose tickets are all above it, so every ticket
|
|
48
|
+
* that came in after that run started is seen; `recordLook` then baselines the ones up to `cut` and lets the rest arrive (fwd F114f).
|
|
49
|
+
* @param {{listUrl: string, mcp: {server: string, tool?: string}, configPath?: string, seen: Set<number>|null, last: Map<number, {state: string, labels: string[]}>, watch: string[], cut?: number, perPage?: number, timeoutMs?: number}} o
|
|
50
|
+
* @returns {Promise<{ok: true, first: boolean, cut?: number, ignored?: number[], found: Array<{number: number, state: string, labels: string[], createdAt: string|null}>, changed: Array<{number: number, reason: string, state: string, labels: string[]}>, pages: number} | {ok: false, error: string}>}
|
|
51
|
+
*/
|
|
52
|
+
export async function lookTickets({
|
|
53
|
+
listUrl, mcp, configPath: explicitConfig, seen, last, watch, cut, perPage = 10, timeoutMs = 90000,
|
|
54
|
+
}) {
|
|
55
|
+
const parsed = parseTicketList(listUrl);
|
|
56
|
+
if (!parsed.ok) return parsed;
|
|
57
|
+
if (parsed.kind !== 'issue') return { ok: false, error: `${listUrl} is a pull request list; the new-ticket look reads an issues list` };
|
|
58
|
+
const { owner, repo } = parsed;
|
|
59
|
+
const { server } = mcp;
|
|
60
|
+
const tool = mcp.tool ?? LOOK_TOOL;
|
|
61
|
+
const { file: configPath, remote } = configForServer(server, explicitConfig);
|
|
62
|
+
if (!discoverServers([configPath]).has(server)) return { ok: false, error: `${server} is not in ~/.mcp.json` };
|
|
63
|
+
if (remote) return { ok: false, error: remoteRefusal(server) };
|
|
64
|
+
const first = seen === null;
|
|
65
|
+
let bridge;
|
|
66
|
+
try {
|
|
67
|
+
bridge = await createMCPBridge({ configPaths: [configPath], ...mcpStartOpts(), timeout: timeoutMs, callTimeout: timeoutMs, confirmServer: () => true, allow: { [server]: [tool] } });
|
|
68
|
+
const failed = bridge.errors.find((/** @type {any} */ e) => e.server === server);
|
|
69
|
+
if (failed) return { ok: false, error: `${server} did not start: ${firstLine(failed.error)}` };
|
|
70
|
+
if (bridge.missing?.[0]) return { ok: false, error: `${server} does not offer ${bridge.missing[0].tool}` };
|
|
71
|
+
const list = bridge.tools.find((/** @type {any} */ t) => t.name === mcpToolName(server, tool));
|
|
72
|
+
if (!list) return { ok: false, error: `${server} does not offer ${tool}` };
|
|
73
|
+
|
|
74
|
+
/** @param {Record<string, unknown>} args @returns {Promise<{issues: any[], pageInfo?: any}|{error: string}>} one page; a page with no list is asked for once more (F103) */
|
|
75
|
+
const page = async (args) => {
|
|
76
|
+
let cause = 'no ticket list in the answer';
|
|
77
|
+
for (let n = 0; n < 2; n++) {
|
|
78
|
+
let text;
|
|
79
|
+
try { text = textOf(await list.execute(args)); } catch (e) { cause = `${server}: ${tool} failed: ${firstLine(e)}`; continue; }
|
|
80
|
+
if (isDeviceLogin(text)) return { error: 'GitHub token unavailable — is pass locked?' };
|
|
81
|
+
let o;
|
|
82
|
+
try { o = JSON.parse(text); } catch { o = null; }
|
|
83
|
+
if (o && Array.isArray(o.issues)) return o;
|
|
84
|
+
cause = text.trim() === '' ? 'no ticket list in the answer (it was empty)' : `no ticket list in the answer: ${firstLine(text)}`;
|
|
85
|
+
if (/^\S*Could not|error/i.test(text)) break; // an error text will not change by asking again
|
|
86
|
+
}
|
|
87
|
+
return { error: cause };
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const found = [];
|
|
91
|
+
/** @type {number[]} */
|
|
92
|
+
const ignored = [];
|
|
93
|
+
/** @type {Array<{number: number, reason: string, state: string, labels: string[]}>} */
|
|
94
|
+
const changed = [];
|
|
95
|
+
const tracked = new Set(first ? [] : last.keys());
|
|
96
|
+
const oldestTracked = tracked.size > 0 ? Math.min(...tracked) : Infinity;
|
|
97
|
+
const stillNeeded = new Set(tracked);
|
|
98
|
+
let after;
|
|
99
|
+
let pages = 0;
|
|
100
|
+
let hitSeen = false;
|
|
101
|
+
// a ticket is new only if its number is higher than the highest seen (closed seen tickets leave an OPEN-only list; older open ones are not new)
|
|
102
|
+
const maxSeen = first ? 0 : Math.max(0, ...seen);
|
|
103
|
+
for (;;) {
|
|
104
|
+
const args = {
|
|
105
|
+
owner, repo, orderBy: 'CREATED_AT', direction: 'DESC', perPage, ...(watch.length === 0 ? { state: 'OPEN' } : {}), fields: ['number', 'created_at', 'state', 'labels'], ...(after ? { after } : {}),
|
|
106
|
+
};
|
|
107
|
+
// eslint-disable-next-line no-await-in-loop
|
|
108
|
+
const r = await page(args);
|
|
109
|
+
if ('error' in r) return { ok: false, error: r.error };
|
|
110
|
+
pages++;
|
|
111
|
+
if (pages === 1 && r.issues.length === 0) return { ok: false, error: `the list at ${listUrl} came back empty; an empty list is unreadable, not "no new tickets"` };
|
|
112
|
+
let lowest = Infinity;
|
|
113
|
+
for (const i of r.issues) {
|
|
114
|
+
const n = i.number;
|
|
115
|
+
if (!Number.isInteger(n)) continue;
|
|
116
|
+
// a number over the one cap is ignored, never a crash; the look records one reason (fwd 9.7)
|
|
117
|
+
if (!isTicketNumber(n)) { ignored.push(n); continue; }
|
|
118
|
+
lowest = Math.min(lowest, n);
|
|
119
|
+
const rec = { number: n, ...watchedOf(i), createdAt: typeof i.created_at === 'string' ? i.created_at : null };
|
|
120
|
+
if (first) { found.push(rec); continue; }
|
|
121
|
+
if (seen.has(n)) {
|
|
122
|
+
hitSeen = true;
|
|
123
|
+
const prev = last.get(n);
|
|
124
|
+
stillNeeded.delete(n);
|
|
125
|
+
if (prev && watch.length > 0) {
|
|
126
|
+
const reason = reasonFor(prev, rec, watch);
|
|
127
|
+
if (reason) changed.push({ number: n, reason, state: rec.state, labels: rec.labels });
|
|
128
|
+
}
|
|
129
|
+
} else if (!hitSeen && n > maxSeen) found.push(rec);
|
|
130
|
+
}
|
|
131
|
+
if (first && (cut === undefined || lowest <= cut)) break;
|
|
132
|
+
const done = (hitSeen || lowest <= maxSeen) && (watch.length === 0 || stillNeeded.size === 0 || lowest < oldestTracked);
|
|
133
|
+
if (done || !r.pageInfo?.hasNextPage || !r.pageInfo?.endCursor) break;
|
|
134
|
+
if (pages >= MAX_PAGES) return { ok: false, error: `more than ${MAX_PAGES} pages of new tickets in one look` };
|
|
135
|
+
after = r.pageInfo.endCursor;
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
ok: true, first, ...(first && cut !== undefined ? { cut } : {}), ...(ignored.length ? { ignored } : {}), found, changed: changed.sort((a, b) => a.number - b.number), pages,
|
|
139
|
+
};
|
|
140
|
+
} catch (e) {
|
|
141
|
+
return { ok: false, error: startRefusal(server, e) };
|
|
142
|
+
} finally {
|
|
143
|
+
try { await bridge?.close(); } catch { /* already closed */ }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ---- the ledger rows (THE writers; only the scheduler calls them) ----------------------------------------------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
/** The numbers a signing has a baseline or an arrived row for. @param {string} ledger @param {string} sig */
|
|
150
|
+
export function seenNumbers(ledger, sig) {
|
|
151
|
+
return new Set(readSeen(ledger, sig).filter((r) => (r.kind === 'baseline' || r.kind === 'arrived') && Number.isInteger(/** @type {any} */ (r).number)).map((r) => /** @type {any} */ (r).number));
|
|
152
|
+
}
|
|
153
|
+
/** Last-seen watched values per number: the newest baseline/arrived row's. @param {string} ledger @param {string} sig @returns {Map<number, {state: string, labels: string[]}>} */
|
|
154
|
+
export function lastSeen(ledger, sig) {
|
|
155
|
+
const m = new Map();
|
|
156
|
+
for (const r of /** @type {any[]} */ (readSeen(ledger, sig))) if ((r.kind === 'baseline' || r.kind === 'arrived') && r.watch) m.set(r.number, r.watch);
|
|
157
|
+
return m;
|
|
158
|
+
}
|
|
159
|
+
/** Has this signing had its first look (its baseline written)? @param {string} ledger @param {string} sig */
|
|
160
|
+
export const hasBaseline = (ledger, sig) => readSeen(ledger, sig).some((r) => r.kind === 'baseline');
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The cause of the newest look when it could not read, else null. The newest look row decides: an `unreadable` row is the problem until a later look reads
|
|
164
|
+
* (its `looked`, `baseline` or `arrived` row). @param {import('./triggerfiles.js').SeenRow[]} rows @returns {{at: number, why: string}|null}
|
|
165
|
+
*/
|
|
166
|
+
export function lookProblemOf(rows) {
|
|
167
|
+
const last = /** @type {any[]} */ (rows).findLast((r) => r.kind === 'unreadable' || r.kind === 'looked' || r.kind === 'baseline' || r.kind === 'arrived');
|
|
168
|
+
return last?.kind === 'unreadable' ? { at: last.at, why: String(last.why ?? 'it could not read the list') } : null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Write what a look found, once. First look: a `baseline` row per ticket on the page (never runs). Later: an `arrived` row per new ticket, oldest first (its arrival
|
|
173
|
+
* time is this clock), and one `arrived` row per watched change with its `reason`. A look that could not read writes one `unreadable` row with its cause and NO `baseline`.
|
|
174
|
+
* @param {string} ledger @param {string} sig @param {Awaited<ReturnType<typeof lookTickets>>} look @param {number} at the scheduler's clock
|
|
175
|
+
* @returns {{arrived: number[], unreadable: string|null}}
|
|
176
|
+
*/
|
|
177
|
+
export function recordLook(ledger, sig, look, at) {
|
|
178
|
+
/** @param {string} id @param {string} kind @param {string} sha @param {number} number @param {{state: string, labels: string[]}} w @param {Record<string, unknown>} [more] */
|
|
179
|
+
const ticketRow = (id, kind, sha, number, w, more = {}) => appendSeen(ledger, /** @type {any} */ ({
|
|
180
|
+
id, kind, sha, name: `#${number}`, path: `#${number}`, size: 0, at, number, watch: { state: w.state, labels: w.labels }, ...more,
|
|
181
|
+
}), sig);
|
|
182
|
+
if (!look.ok) {
|
|
183
|
+
appendSeen(ledger, /** @type {any} */ ({ id: `unreadable:${at}`, kind: 'unreadable', sha: `unreadable:${at}`, name: '', size: 0, at, why: look.error }), sig);
|
|
184
|
+
return { arrived: [], unreadable: look.error };
|
|
185
|
+
}
|
|
186
|
+
// a look that read, after one that could not: one `looked` row ends the problem even when nothing is new; ticket numbers over the cap were ignored by the look, ONE reason is recorded on the same row
|
|
187
|
+
const note = look.ignored?.length ? `ignored ${look.ignored.length} ticket number${look.ignored.length === 1 ? '' : 's'} over ${TICKET_MAX.toLocaleString('en-US')}: ${look.ignored.slice(0, 5).join(', ')}` : null;
|
|
188
|
+
if (note !== null || lookProblemOf(readSeen(ledger, sig))) appendSeen(ledger, /** @type {any} */ ({ id: `looked:${at}`, kind: 'looked', sha: `looked:${at}`, name: '', size: 0, at, ...(note !== null ? { why: note } : {}) }), sig);
|
|
189
|
+
const arrived = [];
|
|
190
|
+
if (look.first) {
|
|
191
|
+
for (const i of [...look.found].reverse()) {
|
|
192
|
+
if (look.cut !== undefined && i.number > look.cut) {
|
|
193
|
+
// above the number the first green run read = it came in after that run began, so it runs once the schedule is on
|
|
194
|
+
ticketRow(`arrived:ticket:${i.number}`, 'arrived', `ticket:${i.number}`, i.number, i);
|
|
195
|
+
arrived.push(i.number);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
ticketRow(`baseline:ticket:${i.number}`, 'baseline', `ticket:${i.number}`, i.number, i);
|
|
199
|
+
}
|
|
200
|
+
return { arrived, unreadable: null };
|
|
201
|
+
}
|
|
202
|
+
for (const i of [...look.found].reverse()) {
|
|
203
|
+
ticketRow(`arrived:ticket:${i.number}`, 'arrived', `ticket:${i.number}`, i.number, i);
|
|
204
|
+
arrived.push(i.number);
|
|
205
|
+
}
|
|
206
|
+
for (const c of look.changed) {
|
|
207
|
+
const rows = readSeen(ledger, sig);
|
|
208
|
+
if (queueOf(rows).some((q) => /** @type {any} */ (q).number === c.number)) {
|
|
209
|
+
// already waiting: the queued run reads the ticket as it is when it starts, so no second run. Advance the last-seen value (a `baseline` row, same writer) so the
|
|
210
|
+
// next look does not re-fire this same change once that run has started; a LATER change still differs from it and arrives once.
|
|
211
|
+
const b = rows.filter((r) => r.kind === 'baseline' && /** @type {any} */ (r).number === c.number).length;
|
|
212
|
+
ticketRow(`baseline:ticket:${c.number}:${b}`, 'baseline', `ticket:${c.number}`, c.number, c);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
// k = how many `arrived` rows this number already has: `arrived:ticket:<n>` (the first arrival) and `arrived:ticket:<n>:<k>` (a watched change) never share an id
|
|
216
|
+
const k = rows.filter((r) => r.kind === 'arrived' && /** @type {any} */ (r).number === c.number).length;
|
|
217
|
+
ticketRow(`arrived:ticket:${c.number}:${k}`, 'arrived', `ticket:${c.number}:${k}`, c.number, c, { reason: c.reason });
|
|
218
|
+
arrived.push(c.number);
|
|
219
|
+
}
|
|
220
|
+
return { arrived, unreadable: null };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The consuming row of a ticket's turn. Called ONLY by the starter's `beforeSpawn` hook, after busy, monthly and wouldAccept said yes (T16), exactly as for a file.
|
|
225
|
+
* @param {string} ledger @param {string} sig @param {{sha: string, name: string}} item @param {number} at
|
|
226
|
+
*/
|
|
227
|
+
export const recordTicketRan = (ledger, sig, item, at) => appendSeen(ledger, { id: `ran:${item.sha}`, kind: 'ran', sha: item.sha, name: item.name, size: 0, at }, sig);
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* "Run it anyway" (ITEM33-BUILD piece 4, borrowed-from fwdloop trigger-tickets.js@v0.29.0 `recordAnyway`): queue a ticket as a NEW arrival with the person's reason and `anyway: true`
|
|
231
|
+
* (the starter then says `--anyway`, so the job's filter never skips it). ONE `arrived` row; a ticket that is already in the queue is refused, never a second run.
|
|
232
|
+
* @param {string} ledger @param {string} sig @param {{number: number, reason: string}} a @param {number} at
|
|
233
|
+
* @returns {{ok: true} | {ok: false, say: string}}
|
|
234
|
+
*/
|
|
235
|
+
export function recordAnyway(ledger, sig, a, at) {
|
|
236
|
+
const rows = readSeen(ledger, sig);
|
|
237
|
+
if (queueOf(rows).some((q) => /** @type {any} */ (q).number === a.number)) return { ok: false, say: `Ticket #${a.number} is already queued. Nothing more was queued.` };
|
|
238
|
+
const n = rows.filter((r) => r.kind === 'arrived' && /** @type {any} */ (r).number === a.number).length;
|
|
239
|
+
const watch = lastSeen(ledger, sig).get(a.number);
|
|
240
|
+
appendSeen(ledger, /** @type {any} */ ({
|
|
241
|
+
id: `arrived:ticket:${a.number}:${n}`, kind: 'arrived', sha: `ticket:${a.number}:${n}`, name: `#${a.number}`, path: `#${a.number}`, size: 0, at, number: a.number, ...(watch ? { watch } : {}), reason: a.reason, anyway: true,
|
|
242
|
+
}), sig);
|
|
243
|
+
return { ok: true };
|
|
244
|
+
}
|