gcf-aif-ext 0.23.0 → 0.25.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/GLOSSARY.md +12 -0
- package/README.md +13 -2
- package/commands/gcf.js +232 -28
- package/extension.json +1 -1
- package/lib/agent-permissions.js +15 -0
- package/lib/audit-core.js +5 -0
- package/lib/board.js +24 -4
- package/lib/changes.js +7 -0
- package/lib/commit-files.js +292 -0
- package/lib/config-set.js +29 -0
- package/lib/domain.js +64 -1
- package/lib/git.js +15 -3
- package/lib/integrate.js +12 -6
- package/lib/main-lock.js +24 -0
- package/lib/plan-check.js +117 -6
- package/lib/play.js +769 -0
- package/lib/playtest-list.js +8 -12
- package/lib/safe-path.js +1 -1
- package/lib/session-mark.js +29 -4
- package/lib/shelf.js +1003 -0
- package/lib/transitions.js +8 -1
- package/lib/validate-item.js +26 -0
- package/lib/verify.js +7 -7
- package/lib/work-ops.js +148 -14
- package/package.json +1 -1
- package/schemas/item.schema.json +12 -0
- package/skills/gcf-adopt/SKILL.md +2 -1
- package/skills/gcf-chat/SKILL.md +7 -4
- package/skills/gcf-verdict/SKILL.md +1 -0
- package/templates/agents-block.md +17 -2
- package/templates/config.yml +19 -0
package/lib/play.js
ADDED
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
// Playing a finished card (docs/Play-Plan.md): its build as a snapshot of its own under
|
|
2
|
+
// `.gcf-session/builds/`, which no later run or build touches, and one running player per
|
|
3
|
+
// project. How to build and start the game is the project's (`play:` in gcf/config.yml); the
|
|
4
|
+
// commands are opaque here, so a browser, a phone and a desktop differ only by their words.
|
|
5
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
6
|
+
import { closeSync, existsSync, openSync, readdirSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { lstatSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { cp, mkdir, open, rm } from 'node:fs/promises';
|
|
9
|
+
import net from 'node:net';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import YAML from 'yaml';
|
|
13
|
+
import { configDoc } from './config-set.js';
|
|
14
|
+
import { readConfig } from './gcf-paths.js';
|
|
15
|
+
import { isValidId } from './id.js';
|
|
16
|
+
import { buildLedger } from './ledger.js';
|
|
17
|
+
import { alive, claimMainRun, MAIN_RUN, runHolder, withMainLock } from './main-lock.js';
|
|
18
|
+
import { atomicWrite, renameRetrying } from './safe-path.js';
|
|
19
|
+
import { commitMove } from './unit-ops.js';
|
|
20
|
+
import { failureOf, killTree, runCommand } from './verify.js';
|
|
21
|
+
|
|
22
|
+
export const NOT_SET_UP =
|
|
23
|
+
'how to play is not set up: ai-factory gcf playtest setup --build <cmd> --output <folder> ' +
|
|
24
|
+
'--target <name> --start <cmd> --open <text>';
|
|
25
|
+
|
|
26
|
+
// Whether the start's port answers, and how long a process with no port must live to count.
|
|
27
|
+
const PORT_WAIT_MS = 15000;
|
|
28
|
+
const ALIVE_WAIT_MS = 2000;
|
|
29
|
+
const STOP_WAIT_MS = 15000;
|
|
30
|
+
// A held file on Windows lets go a moment after its process ends.
|
|
31
|
+
const RM = { recursive: true, force: true, maxRetries: 5, retryDelay: 200 };
|
|
32
|
+
|
|
33
|
+
const session = (dir) => path.join(dir, '.gcf-session');
|
|
34
|
+
const buildsOf = (dir) => path.join(session(dir), 'builds');
|
|
35
|
+
const snapshotOf = (dir, id) => path.join(buildsOf(dir), id);
|
|
36
|
+
const factsOf = (dir, id) => path.join(buildsOf(dir), `${id}.json`);
|
|
37
|
+
const attemptOf = (dir, id) => path.join(buildsOf(dir), `${id}.attempt.json`);
|
|
38
|
+
const logOf = (dir, id) => path.join(buildsOf(dir), `${id}.log`);
|
|
39
|
+
const playerFile = (dir) => path.join(session(dir), 'play.json');
|
|
40
|
+
const playerLog = (dir) => path.join(session(dir), 'play.log');
|
|
41
|
+
const leftovers = /^.+\.(?:tmp|old)-(\d+)$/;
|
|
42
|
+
|
|
43
|
+
function readJson(file) {
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A rename from a synchronous path: Windows holds a fresh file for a moment (a scanner). */
|
|
52
|
+
function renameHeld(from, to) {
|
|
53
|
+
for (let attempt = 1; ; attempt++) {
|
|
54
|
+
try {
|
|
55
|
+
return renameSync(from, to);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (!['EPERM', 'EACCES', 'EBUSY'].includes(e.code) || attempt >= 10) throw e;
|
|
58
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A file written whole, from a synchronous path. */
|
|
64
|
+
function writeJsonSync(file, value) {
|
|
65
|
+
const tmp = `${file}.tmp-${process.pid}`;
|
|
66
|
+
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`);
|
|
67
|
+
renameHeld(tmp, file);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const needText = (where, v) => {
|
|
71
|
+
if (typeof v !== 'string' || !v.trim())
|
|
72
|
+
throw new Error(`gcf/config.yml: ${where} must be a command or a text`);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* `play:` of a config, checked; null when the project has none. `output` stays inside the project:
|
|
77
|
+
* it is copied from there, and a path out of it would copy anything.
|
|
78
|
+
* @returns {{build: string, output: string, targets: Record<string, {start: string, open: string,
|
|
79
|
+
* stop?: string}>} | null}
|
|
80
|
+
*/
|
|
81
|
+
export function playOf(config) {
|
|
82
|
+
const play = config.play;
|
|
83
|
+
if (play === undefined || play === null) return null;
|
|
84
|
+
if (typeof play !== 'object' || Array.isArray(play))
|
|
85
|
+
throw new Error('gcf/config.yml: play must map build:, output: and targets:');
|
|
86
|
+
needText('play.build', play.build);
|
|
87
|
+
needOutput(play.output);
|
|
88
|
+
const targets = play.targets ?? {};
|
|
89
|
+
needTargets(targets);
|
|
90
|
+
|
|
91
|
+
return { build: play.build, output: play.output, targets };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function needOutput(output) {
|
|
95
|
+
needText('play.output', output);
|
|
96
|
+
const parts = String(output).split(/[\\/]/);
|
|
97
|
+
if (path.isAbsolute(output) || /^[A-Za-z]:/.test(output) || parts.includes('..'))
|
|
98
|
+
throw new Error(`gcf/config.yml: play.output must lie inside the project: ${output}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function needTargets(targets) {
|
|
102
|
+
if (typeof targets !== 'object' || Array.isArray(targets))
|
|
103
|
+
throw new Error('gcf/config.yml: play.targets must map a name to start: and open:');
|
|
104
|
+
for (const [name, t] of Object.entries(targets)) {
|
|
105
|
+
if (!/^\w[\w-]*$/.test(name)) throw new Error(`a target's name is one word: ${name}`);
|
|
106
|
+
if (typeof t !== 'object' || t === null)
|
|
107
|
+
throw new Error(`gcf/config.yml: play.targets.${name} must map start: and open:`);
|
|
108
|
+
needText(`play.targets.${name}.start`, t.start);
|
|
109
|
+
needText(`play.targets.${name}.open`, t.open);
|
|
110
|
+
if (t.stop !== undefined) needText(`play.targets.${name}.stop`, t.stop);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const projectPlay = async (dir) => {
|
|
115
|
+
const play = playOf(await readConfig(path.join(dir, 'gcf')));
|
|
116
|
+
if (!play) throw new Error(NOT_SET_UP);
|
|
117
|
+
|
|
118
|
+
return play;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const needId = (id) => {
|
|
122
|
+
if (!isValidId(id)) throw new Error(`${id} is no item id`);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** A path as one word of a shell command. */
|
|
126
|
+
const quoted = (p) =>
|
|
127
|
+
process.platform === 'win32' ? `"${p}"` : `"${p.replace(/(["\\$`])/g, '\\$1')}"`;
|
|
128
|
+
|
|
129
|
+
/** The machine's address on its network, for a phone on the same one. */
|
|
130
|
+
function lanAddress() {
|
|
131
|
+
for (const list of Object.values(os.networkInterfaces()))
|
|
132
|
+
for (const a of list ?? []) if (a.family === 'IPv4' && !a.internal) return a.address;
|
|
133
|
+
|
|
134
|
+
return 'localhost';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const fill = (text, { dir, port, lan }) =>
|
|
138
|
+
text
|
|
139
|
+
.replaceAll('{dir}', () => dir)
|
|
140
|
+
.replaceAll('{port}', String(port))
|
|
141
|
+
.replaceAll('{lan}', lan);
|
|
142
|
+
|
|
143
|
+
/** Every file under `dir` with its stamp and size; none when it does not exist. */
|
|
144
|
+
function treeOf(dir) {
|
|
145
|
+
const files = new Map();
|
|
146
|
+
const walk = (at) => {
|
|
147
|
+
for (const e of readdirSync(at, { withFileTypes: true })) {
|
|
148
|
+
const p = path.join(at, e.name);
|
|
149
|
+
if (e.isDirectory()) walk(p);
|
|
150
|
+
else {
|
|
151
|
+
// Its own stamp: a link that points nowhere yet is a file of the build too.
|
|
152
|
+
const s = lstatSync(p);
|
|
153
|
+
files.set(p, { stamp: `${s.mtimeMs}:${s.size}`, size: s.size });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
if (existsSync(dir)) walk(dir);
|
|
159
|
+
|
|
160
|
+
return files;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const refreshed = (before, after) => [...after].some(([p, f]) => before.get(p)?.stamp !== f.stamp);
|
|
164
|
+
|
|
165
|
+
const gitOut = (cwd, args) => {
|
|
166
|
+
const res = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
|
167
|
+
|
|
168
|
+
return res.status === 0 ? res.stdout : null;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/** What git sees changed or new, each path once; gcf/ and the run's own files are no build's. */
|
|
172
|
+
function looseOf(cwd) {
|
|
173
|
+
const out = gitOut(cwd, [
|
|
174
|
+
...['--no-optional-locks', 'status', '--porcelain', '-z', '-uall', '--', '.'],
|
|
175
|
+
...[':(exclude)gcf', ':(exclude)GLOSSARY.md', ':(exclude).gcf-session'],
|
|
176
|
+
]);
|
|
177
|
+
const parts = (out ?? '').split('\0').filter(Boolean);
|
|
178
|
+
const paths = [];
|
|
179
|
+
// A rename or a copy gives its old path as the next part: the new one names it.
|
|
180
|
+
for (let i = 0; i < parts.length; i++) {
|
|
181
|
+
paths.push(parts[i].slice(3));
|
|
182
|
+
if (/^(?:[RC].|.[RC])/.test(parts[i])) i++;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return paths;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The end of a log, as it is: what the operator reads when a command fails. */
|
|
189
|
+
function tailOf(file) {
|
|
190
|
+
try {
|
|
191
|
+
const text = readFileSync(file, 'utf8');
|
|
192
|
+
|
|
193
|
+
return text.trimEnd().split(/\r?\n/).slice(-20).join('\n');
|
|
194
|
+
} catch {
|
|
195
|
+
return '';
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const withTail = (problem, file) => {
|
|
200
|
+
const tail = tailOf(file);
|
|
201
|
+
|
|
202
|
+
return tail ? `${problem}\n${tail}` : problem;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/** Who may be built without `--from`: what waits for play, in the checkout's own code. */
|
|
206
|
+
const PLAYABLE = ['awaiting-verdict', 'unplayed'];
|
|
207
|
+
|
|
208
|
+
/** Why item `id` may not get this build now, or null. A card still in its worktree is `ready`. */
|
|
209
|
+
async function statusProblem(dir, id, from) {
|
|
210
|
+
const item = (await buildLedger(dir)).item(id);
|
|
211
|
+
if (!item) return `${id} is not on the board`;
|
|
212
|
+
const status = item.data.status;
|
|
213
|
+
if (from ? status === 'ready' : PLAYABLE.includes(status)) return null;
|
|
214
|
+
|
|
215
|
+
return `${id} is ${status}: a build of it would be of no use now`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The folder a build by click runs in: nothing may work there meanwhile. A harness run's own build
|
|
220
|
+
* goes under the lock that run holds.
|
|
221
|
+
*/
|
|
222
|
+
async function holdCheckout(dir, id) {
|
|
223
|
+
const holder = runHolder(dir, MAIN_RUN);
|
|
224
|
+
if (holder && process.env.GCF_RUN_OWNER === `run:${holder}`) return { own: true, release: null };
|
|
225
|
+
// Under the main lock: an integrate that passed its check of the run lock ends first, and the
|
|
226
|
+
// next one sees this claim.
|
|
227
|
+
const release = holder ? null : await withMainLock(dir, () => claimMainRun(dir));
|
|
228
|
+
if (!release)
|
|
229
|
+
throw new Error(
|
|
230
|
+
`a run works in the main checkout: a build of ${id} now would take its unfinished work; ` +
|
|
231
|
+
'build when it ends',
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return { own: false, release };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Remove what no card needs any more: a snapshot marked released, the build of a card off the
|
|
239
|
+
* board, leftovers of a dead process. Under the main lock, each `<id>.json` read again: a build
|
|
240
|
+
* that published meanwhile wrote a new one. A held file waits for the next sweep; a card file that
|
|
241
|
+
* cannot be read says nothing of who left the board, so nothing goes by it.
|
|
242
|
+
*/
|
|
243
|
+
async function sweep(dir) {
|
|
244
|
+
const builds = buildsOf(dir);
|
|
245
|
+
if (!existsSync(builds)) return;
|
|
246
|
+
const ledger = await buildLedger(dir).catch(() => null);
|
|
247
|
+
const ids = new Set();
|
|
248
|
+
for (const e of readdirSync(builds)) {
|
|
249
|
+
const left = leftovers.exec(e);
|
|
250
|
+
if (left) {
|
|
251
|
+
// Not awaited: the main lock is held here, and every move would wait for a big one.
|
|
252
|
+
if (!alive(Number(left[1]))) void rm(path.join(builds, e), RM).catch(() => {});
|
|
253
|
+
} else {
|
|
254
|
+
const id = e.replace(/(\.attempt)?\.(json|log)$/, '');
|
|
255
|
+
// A stray `.log` gives an empty id, whose snapshot path is the whole folder.
|
|
256
|
+
if (isValidId(id)) ids.add(id);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
for (const id of ids) {
|
|
261
|
+
const released = readJson(factsOf(dir, id))?.released === true;
|
|
262
|
+
const gone =
|
|
263
|
+
ledger !== null && !ledger.item(id) && !existsSync(path.join(dir, 'gcf', 'work', `${id}.md`));
|
|
264
|
+
if (released || gone) removeBuild(dir, id, { all: gone });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const building = (attempt) => attempt?.building === true && alive(attempt.pid);
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Remove a card's snapshot, its facts last: a removal that failed halfway is repeated by a sweep.
|
|
272
|
+
* Under the main lock it only moves aside: a big one takes long to delete, and every move would
|
|
273
|
+
* wait for it. A live attempt keeps its own file; `all` also takes the log of a card that left.
|
|
274
|
+
*/
|
|
275
|
+
function removeBuild(dir, id, { all = false } = {}) {
|
|
276
|
+
try {
|
|
277
|
+
if (playerOf(dir)?.id === id) stopPlay(dir);
|
|
278
|
+
const target = snapshotOf(dir, id);
|
|
279
|
+
if (existsSync(target)) {
|
|
280
|
+
const old = `${target}.old-${process.pid}`;
|
|
281
|
+
rmSync(old, RM);
|
|
282
|
+
renameHeld(target, old);
|
|
283
|
+
// A failure leaves it to a sweep, once this process is gone.
|
|
284
|
+
void rm(old, RM).catch(() => {});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
rmSync(factsOf(dir, id), { force: true });
|
|
288
|
+
if (!building(readJson(attemptOf(dir, id)))) {
|
|
289
|
+
rmSync(attemptOf(dir, id), { force: true });
|
|
290
|
+
if (all) rmSync(logOf(dir, id), { force: true });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return null;
|
|
294
|
+
} catch (e) {
|
|
295
|
+
return `the build of ${id} stays on the disk for now: ${e.message}`;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The card's play has ended (a verdict, a drop): its player stops and its snapshot goes. After the
|
|
301
|
+
* move's commit, so it never fails the move: the problem comes back as words, and the mark
|
|
302
|
+
* `released` makes a later sweep repeat what a held file refused. A card that a tune sends back
|
|
303
|
+
* stays on the board, and only the mark tells this snapshot from a fresh one.
|
|
304
|
+
* @returns {string | null}
|
|
305
|
+
*/
|
|
306
|
+
export function releaseBuild(projectDir, id) {
|
|
307
|
+
// The id comes from a card's frontmatter, and a recursive delete follows: a slug, nothing else.
|
|
308
|
+
if (!isValidId(id)) return null;
|
|
309
|
+
const dir = path.resolve(projectDir);
|
|
310
|
+
try {
|
|
311
|
+
const facts = readJson(factsOf(dir, id));
|
|
312
|
+
if (facts) writeJsonSync(factsOf(dir, id), { ...facts, released: true });
|
|
313
|
+
else if (existsSync(snapshotOf(dir, id)))
|
|
314
|
+
writeJsonSync(factsOf(dir, id), { id, released: true });
|
|
315
|
+
} catch (e) {
|
|
316
|
+
return `the build of ${id} stays on the disk for now: ${e.message}`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return removeBuild(dir, id);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Put a finished build in its card's place, or throw and leave the old one with its facts. A card
|
|
324
|
+
* whose status moved on meanwhile (a verdict needs no wait for a build) takes no snapshot.
|
|
325
|
+
*/
|
|
326
|
+
async function publish(dir, { id, tmp, facts, from }) {
|
|
327
|
+
const target = snapshotOf(dir, id);
|
|
328
|
+
const old = `${target}.old-${process.pid}`;
|
|
329
|
+
const refused = await withMainLock(dir, async () => {
|
|
330
|
+
const problem = await statusProblem(dir, id, from);
|
|
331
|
+
if (problem) return problem;
|
|
332
|
+
if (playerOf(dir)?.id === id) stopPlay(dir);
|
|
333
|
+
const had = existsSync(target);
|
|
334
|
+
if (had) await renameRetrying(target, old);
|
|
335
|
+
try {
|
|
336
|
+
await renameRetrying(tmp, target);
|
|
337
|
+
await atomicWrite(factsOf(dir, id), `${JSON.stringify(facts, null, 2)}\n`);
|
|
338
|
+
} catch (e) {
|
|
339
|
+
await rm(target, RM).catch(() => {});
|
|
340
|
+
if (had) await renameRetrying(old, target);
|
|
341
|
+
throw e;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return null;
|
|
345
|
+
});
|
|
346
|
+
// Out of the lock: a big snapshot takes a while to delete. A failure leaves it to a sweep.
|
|
347
|
+
await rm(old, RM).catch(() => {});
|
|
348
|
+
|
|
349
|
+
return refused;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Build card `id` with `play.build` and keep the result as its snapshot; the old snapshot stays
|
|
354
|
+
* until a new one has replaced it. `from`: the card's worktree, whose code is still its alone. A
|
|
355
|
+
* failed build is no error: it comes back as `problem`, and the card goes on without one.
|
|
356
|
+
* @returns {Promise<{id: string, ok: boolean, problem?: string, dirtied?: string[]}>}
|
|
357
|
+
*/
|
|
358
|
+
export async function buildItem(projectDir, id, { from = null } = {}) {
|
|
359
|
+
needId(id);
|
|
360
|
+
const dir = path.resolve(projectDir);
|
|
361
|
+
const play = await projectPlay(dir);
|
|
362
|
+
const cwd = from ? path.resolve(from) : dir;
|
|
363
|
+
if (from && cwd !== path.join(session(dir), 'worktrees', id))
|
|
364
|
+
throw new Error(`--from names the worktree of ${id}, not ${from}`);
|
|
365
|
+
const problem = await statusProblem(dir, id, from);
|
|
366
|
+
if (problem) throw new Error(problem);
|
|
367
|
+
const hold = from ? { own: true, release: null } : await holdCheckout(dir, id);
|
|
368
|
+
try {
|
|
369
|
+
return await runBuild(dir, cwd, play, { id, from, own: hold.own });
|
|
370
|
+
} finally {
|
|
371
|
+
await hold.release?.();
|
|
372
|
+
if (hold.release) await mergeWaiting(dir);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* What a worktree run left verified while this click held the checkout: that run has ended, and no
|
|
378
|
+
* other would merge it until the next one starts. A refusal leaves it for that one.
|
|
379
|
+
*/
|
|
380
|
+
async function mergeWaiting(dir) {
|
|
381
|
+
// Here, not on top: integrate reaches this module through the moves it records.
|
|
382
|
+
const { integrate, verifiedBranches } = await import('./integrate.js');
|
|
383
|
+
try {
|
|
384
|
+
if (verifiedBranches(dir).length) await integrate(dir);
|
|
385
|
+
} catch {
|
|
386
|
+
// It must not take the build's answer with it: the next run's integrate reports it.
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function runBuild(dir, cwd, play, { id, from, own }) {
|
|
391
|
+
const started = new Date().toISOString();
|
|
392
|
+
await mkdir(buildsOf(dir), { recursive: true });
|
|
393
|
+
await withMainLock(dir, async () => {
|
|
394
|
+
await sweep(dir);
|
|
395
|
+
const problem = await statusProblem(dir, id, from);
|
|
396
|
+
if (problem) throw new Error(problem);
|
|
397
|
+
const was = readJson(attemptOf(dir, id));
|
|
398
|
+
if (building(was) && was.pid !== process.pid)
|
|
399
|
+
throw new Error(`${id} is being built (pid ${was.pid}): wait until that build ends`);
|
|
400
|
+
writeJsonSync(attemptOf(dir, id), { pid: process.pid, building: true, at: started });
|
|
401
|
+
});
|
|
402
|
+
const failed = (problem) => {
|
|
403
|
+
writeJsonSync(attemptOf(dir, id), { pid: process.pid, problem, at: started });
|
|
404
|
+
|
|
405
|
+
return { id, ok: false, problem, log: logOf(dir, id) };
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
const tmp = `${snapshotOf(dir, id)}.tmp-${process.pid}`;
|
|
409
|
+
try {
|
|
410
|
+
return await buildAndPublish(dir, cwd, play, { id, from, own, tmp, failed });
|
|
411
|
+
} catch (e) {
|
|
412
|
+
return failed(e.message);
|
|
413
|
+
} finally {
|
|
414
|
+
await rm(tmp, RM).catch(() => {});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async function buildAndPublish(dir, cwd, play, { id, from, own, tmp, failed }) {
|
|
419
|
+
const output = path.join(cwd, play.output);
|
|
420
|
+
// The slash: a rule for folders only must match a folder not built yet.
|
|
421
|
+
if (spawnSync('git', ['check-ignore', '-q', `${play.output}/`], { cwd }).status !== 0)
|
|
422
|
+
return failed(`play.output ${play.output} is not ignored by git: add it to .gitignore`);
|
|
423
|
+
const loose = looseOf(cwd);
|
|
424
|
+
const before = treeOf(output);
|
|
425
|
+
const commit = gitOut(cwd, ['rev-parse', '--short', 'HEAD'])?.trim() ?? null;
|
|
426
|
+
const minutes = (await readConfig(path.join(dir, 'gcf'))).dod_timeout_min ?? 0;
|
|
427
|
+
const log = await open(logOf(dir, id), 'w');
|
|
428
|
+
let res;
|
|
429
|
+
try {
|
|
430
|
+
res = await runCommand(play.build, cwd, ['ignore', log.fd, log.fd], minutes * 60000);
|
|
431
|
+
} finally {
|
|
432
|
+
await log.close();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const why = failureOf('play.build', play.build, minutes, res);
|
|
436
|
+
if (why) return failed(withTail(why, logOf(dir, id)));
|
|
437
|
+
const after = treeOf(output);
|
|
438
|
+
// A build that wrote nothing new must not stand in for this commit.
|
|
439
|
+
if (!refreshed(before, after))
|
|
440
|
+
return failed(`the build did not update ${play.output}: no new file there`);
|
|
441
|
+
const dirtied = looseOf(cwd).filter((p) => !loose.includes(p));
|
|
442
|
+
// Its links as they are: made absolute they would point back into `output`, at a later build.
|
|
443
|
+
await cp(output, tmp, { recursive: true, verbatimSymlinks: true });
|
|
444
|
+
const facts = {
|
|
445
|
+
id,
|
|
446
|
+
commit,
|
|
447
|
+
built_at: new Date().toISOString(),
|
|
448
|
+
source: own ? 'run' : 'click',
|
|
449
|
+
dirty: loose.length > 0,
|
|
450
|
+
dirtied,
|
|
451
|
+
size: [...after.values()].reduce((sum, f) => sum + f.size, 0),
|
|
452
|
+
};
|
|
453
|
+
const refused = await publish(dir, { id, tmp, facts, from });
|
|
454
|
+
// Not a failed build: the card has moved on, and nobody waits for this one.
|
|
455
|
+
if (refused) {
|
|
456
|
+
rmSync(attemptOf(dir, id), { force: true });
|
|
457
|
+
|
|
458
|
+
return { id, ok: false, problem: refused, moved_on: true };
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
rmSync(attemptOf(dir, id), { force: true });
|
|
462
|
+
|
|
463
|
+
return { ok: true, ...facts };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** The running player's record as it stands, or null; a dead one reads as none. Reads only. */
|
|
467
|
+
export function playerOf(projectDir) {
|
|
468
|
+
const dir = path.resolve(projectDir);
|
|
469
|
+
const p = readJson(playerFile(dir));
|
|
470
|
+
|
|
471
|
+
// shortcut: a reused pid passes for the player; a check of the process's name, if it happens.
|
|
472
|
+
return p && alive(p.pid) ? p : null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** A target's `stop`: what its start left behind (`adb reverse`) outlives its process. */
|
|
476
|
+
const runStop = (dir, command) =>
|
|
477
|
+
spawnSync(command, {
|
|
478
|
+
cwd: dir,
|
|
479
|
+
shell: true,
|
|
480
|
+
stdio: 'ignore',
|
|
481
|
+
timeout: STOP_WAIT_MS,
|
|
482
|
+
windowsHide: true,
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Stop the player, or only the one playing card `id`, then run its `stop`. A start that launched
|
|
487
|
+
* and ended has no player and no `stop`: what it left is its own.
|
|
488
|
+
* @returns {object | null} what stopped
|
|
489
|
+
*/
|
|
490
|
+
export function stopPlay(projectDir, { id = null } = {}) {
|
|
491
|
+
const dir = path.resolve(projectDir);
|
|
492
|
+
const p = readJson(playerFile(dir));
|
|
493
|
+
if (!p || (id && p.id !== id)) return null;
|
|
494
|
+
if (alive(p.pid)) killTree(p.pid);
|
|
495
|
+
if (p.stop) runStop(dir, p.stop);
|
|
496
|
+
rmSync(playerFile(dir), { force: true });
|
|
497
|
+
|
|
498
|
+
return p;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** The operator's stop: under the lock a start holds, or it misses the player that start makes. */
|
|
502
|
+
export const stopPlayLocked = (projectDir) =>
|
|
503
|
+
withMainLock(path.resolve(projectDir), async () => stopPlay(projectDir));
|
|
504
|
+
|
|
505
|
+
const freePort = () =>
|
|
506
|
+
new Promise((resolve, reject) => {
|
|
507
|
+
const s = net.createServer();
|
|
508
|
+
s.on('error', reject);
|
|
509
|
+
s.listen(0, '127.0.0.1', () => {
|
|
510
|
+
const { port } = /** @type {net.AddressInfo} */ (s.address());
|
|
511
|
+
s.close(() => resolve(port));
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
const answers = (port) =>
|
|
516
|
+
new Promise((resolve) => {
|
|
517
|
+
const c = net.connect({ port, host: 'localhost' });
|
|
518
|
+
c.once('connect', () => {
|
|
519
|
+
c.destroy();
|
|
520
|
+
resolve(true);
|
|
521
|
+
});
|
|
522
|
+
c.once('error', () => resolve(false));
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
/** How a started process turned out: it ended, it answers on its port, or it is merely alive. */
|
|
526
|
+
function outcomeOf(child, port) {
|
|
527
|
+
return new Promise((resolve) => {
|
|
528
|
+
let done = false;
|
|
529
|
+
const end = (r) => {
|
|
530
|
+
done = true;
|
|
531
|
+
resolve(r);
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
child.once('error', (error) => end({ state: 'error', why: error.message }));
|
|
535
|
+
child.once('exit', (status, signal) =>
|
|
536
|
+
end(
|
|
537
|
+
status === 0 ? { state: 'launched' } : { state: 'error', why: `exit ${status ?? signal}` },
|
|
538
|
+
),
|
|
539
|
+
);
|
|
540
|
+
const until = Date.now() + (port ? PORT_WAIT_MS : ALIVE_WAIT_MS);
|
|
541
|
+
const look = async () => {
|
|
542
|
+
if (done) return;
|
|
543
|
+
if (port && (await answers(port))) return done || end({ state: 'running' });
|
|
544
|
+
if (done) return;
|
|
545
|
+
if (Date.now() < until) return void setTimeout(look, 200);
|
|
546
|
+
end(port ? { state: 'silent' } : { state: 'running' });
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
setTimeout(look, port ? 100 : ALIVE_WAIT_MS);
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// A small keeper between this process and the shell. On Windows a child that is not detached dies
|
|
554
|
+
// with its parent (libuv's job object), and what a detached shell starts writes nothing to the log.
|
|
555
|
+
const KEEPER =
|
|
556
|
+
"const c = require('node:child_process').spawn(process.argv[1], " +
|
|
557
|
+
"{ shell: true, stdio: ['ignore', 1, 2], windowsHide: true });" +
|
|
558
|
+
"c.on('error', () => process.exit(1));" +
|
|
559
|
+
"c.on('exit', (status) => process.exit(status ?? 1));";
|
|
560
|
+
|
|
561
|
+
/** Start a command cut loose from this process, its output in the player's log. */
|
|
562
|
+
function launch(dir, command) {
|
|
563
|
+
const log = openSync(playerLog(dir), 'w');
|
|
564
|
+
try {
|
|
565
|
+
const child = spawn(process.execPath, ['-e', KEEPER, command], {
|
|
566
|
+
cwd: dir,
|
|
567
|
+
detached: true,
|
|
568
|
+
stdio: ['ignore', log, log],
|
|
569
|
+
windowsHide: true,
|
|
570
|
+
});
|
|
571
|
+
child.unref();
|
|
572
|
+
|
|
573
|
+
return child;
|
|
574
|
+
} finally {
|
|
575
|
+
closeSync(log);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Write the player's record; a player with none could not be stopped, so it goes. */
|
|
580
|
+
function recordPlayer(dir, record) {
|
|
581
|
+
try {
|
|
582
|
+
writeJsonSync(playerFile(dir), record);
|
|
583
|
+
} catch (e) {
|
|
584
|
+
killTree(record.pid);
|
|
585
|
+
throw e;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Play card `id`'s snapshot on `target`. One player per project: the last one stops first. The
|
|
591
|
+
* process is cut loose from its caller, so a board's restart does not end the game. A process that
|
|
592
|
+
* ends with 0 has launched what it launches: nothing is left to stop.
|
|
593
|
+
*/
|
|
594
|
+
export async function startPlay(projectDir, id, targetName) {
|
|
595
|
+
needId(id);
|
|
596
|
+
const dir = path.resolve(projectDir);
|
|
597
|
+
const play = await projectPlay(dir);
|
|
598
|
+
const target = Object.hasOwn(play.targets, targetName) ? play.targets[targetName] : null;
|
|
599
|
+
if (!target)
|
|
600
|
+
throw new Error(
|
|
601
|
+
`no target ${targetName}: ${Object.keys(play.targets).join(', ') || 'none set up'}`,
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
// Held to the end: a verdict meanwhile waits, or it would miss the player this start records.
|
|
605
|
+
return withMainLock(dir, async () => {
|
|
606
|
+
await sweep(dir);
|
|
607
|
+
const facts = readJson(factsOf(dir, id));
|
|
608
|
+
const snapshot = snapshotOf(dir, id);
|
|
609
|
+
if (!facts || facts.released || !existsSync(snapshot))
|
|
610
|
+
throw new Error(`${id} has no build: ai-factory gcf playtest build ${id}`);
|
|
611
|
+
stopPlay(dir);
|
|
612
|
+
const port = await freePort();
|
|
613
|
+
const lan = lanAddress();
|
|
614
|
+
// `{dir}/game.exe` is one word: Windows' shell takes no name after a closing quote.
|
|
615
|
+
const inCommand = (text) =>
|
|
616
|
+
fill(
|
|
617
|
+
text
|
|
618
|
+
.replace(/"\{dir\}([^"]*)"/g, (_, tail) => quoted(snapshot + tail))
|
|
619
|
+
.replace(/\{dir\}([\\/][^\s"'&|<>;()]*)?/g, (_, tail = '') => quoted(snapshot + tail)),
|
|
620
|
+
{ dir: quoted(snapshot), port, lan },
|
|
621
|
+
);
|
|
622
|
+
const open = fill(target.open, { dir: snapshot, port, lan });
|
|
623
|
+
const child = launch(dir, inCommand(target.start));
|
|
624
|
+
const url = /^https?:\/\//.test(open) ? open : null;
|
|
625
|
+
const stop = target.stop ? { stop: inCommand(target.stop) } : {};
|
|
626
|
+
const started = new Date().toISOString();
|
|
627
|
+
const record = { pid: child.pid, id, target: targetName, port, open, url, started, ...stop };
|
|
628
|
+
// Before the wait: a starter killed in it must not leave a player nobody can stop.
|
|
629
|
+
recordPlayer(dir, record);
|
|
630
|
+
const forget = () => rmSync(playerFile(dir), { force: true });
|
|
631
|
+
const end = await outcomeOf(child, target.start.includes('{port}') ? port : null);
|
|
632
|
+
const undo = () => target.stop && runStop(dir, inCommand(target.stop));
|
|
633
|
+
if (end.state === 'silent') {
|
|
634
|
+
killTree(child.pid);
|
|
635
|
+
forget();
|
|
636
|
+
undo();
|
|
637
|
+
throw new Error(
|
|
638
|
+
withTail(`play.targets.${targetName}.start did not answer on port ${port}`, playerLog(dir)),
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
if (end.state === 'error') {
|
|
643
|
+
forget();
|
|
644
|
+
undo();
|
|
645
|
+
throw new Error(
|
|
646
|
+
withTail(`play.targets.${targetName}.start failed (${end.why})`, playerLog(dir)),
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
if (end.state === 'launched') {
|
|
651
|
+
forget();
|
|
652
|
+
|
|
653
|
+
return { state: 'launched', id, target: targetName, open, url };
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return { state: 'running', ...record };
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const NO_FACTS = {
|
|
661
|
+
commit: null,
|
|
662
|
+
built_at: null,
|
|
663
|
+
source: null,
|
|
664
|
+
dirty: false,
|
|
665
|
+
dirtied: [],
|
|
666
|
+
size: null,
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Why the card's last attempt gave no build, or null. One marked as going by a process now gone was
|
|
671
|
+
* taken by a stop or a crash, and could not say so.
|
|
672
|
+
*/
|
|
673
|
+
function attemptProblem(attempt, going) {
|
|
674
|
+
if (going) return attempt.problem ?? null;
|
|
675
|
+
|
|
676
|
+
return attempt?.building === true
|
|
677
|
+
? 'the last build was stopped before it ended'
|
|
678
|
+
: (attempt?.problem ?? null);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** What a board and a chat show of the card: its snapshot, or the attempt under way or failed. */
|
|
682
|
+
export function buildOf(projectDir, id, status) {
|
|
683
|
+
const dir = path.resolve(projectDir);
|
|
684
|
+
const raw = readJson(factsOf(dir, id));
|
|
685
|
+
const facts = raw?.released || !existsSync(snapshotOf(dir, id)) ? null : raw;
|
|
686
|
+
const attempt = readJson(attemptOf(dir, id));
|
|
687
|
+
const going = building(attempt);
|
|
688
|
+
// A card back at work has no build to offer, unless one goes for it in its worktree right now.
|
|
689
|
+
if (status === 'ready' && !going) return null;
|
|
690
|
+
const problem = attemptProblem(attempt, going);
|
|
691
|
+
if (!facts && !going && !problem) return null;
|
|
692
|
+
|
|
693
|
+
return { ...NO_FACTS, ...facts, ok: Boolean(facts), building: going, problem };
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** The project's targets and running player, read only: null with no `play:`. */
|
|
697
|
+
export function playFacts(projectDir, config) {
|
|
698
|
+
let play;
|
|
699
|
+
try {
|
|
700
|
+
play = playOf(config);
|
|
701
|
+
} catch (e) {
|
|
702
|
+
return { problem: e.message, targets: [], running: null };
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (!play) return null;
|
|
706
|
+
const p = playerOf(projectDir);
|
|
707
|
+
|
|
708
|
+
return {
|
|
709
|
+
problem: null,
|
|
710
|
+
targets: Object.entries(play.targets).map(([name, t]) => ({ name, open: t.open })),
|
|
711
|
+
running: p
|
|
712
|
+
? { id: p.id, target: p.target, open: p.open, url: p.url, started: p.started }
|
|
713
|
+
: null,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** `gcf playtest status`: a dead player's record and what no card needs go, then the facts. */
|
|
718
|
+
export async function playStatus(projectDir) {
|
|
719
|
+
const dir = path.resolve(projectDir);
|
|
720
|
+
await withMainLock(dir, async () => {
|
|
721
|
+
await sweep(dir);
|
|
722
|
+
if (existsSync(playerFile(dir)) && !playerOf(dir)) stopPlay(dir);
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
return playFacts(dir, await readConfig(path.join(dir, 'gcf')));
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* `gcf playtest setup`: how the project builds and starts its game, committed as `gcf(config)`.
|
|
730
|
+
* Only what is given changes. Checked as a whole before it is written.
|
|
731
|
+
* @param {{build?: string, output?: string, target?: string, start?: string, open?: string,
|
|
732
|
+
* stop?: string, removeTarget?: string, commit?: boolean}} opts
|
|
733
|
+
*/
|
|
734
|
+
export async function setupPlay(projectDir, opts) {
|
|
735
|
+
const dir = path.resolve(projectDir);
|
|
736
|
+
const { build, output, target, start, open: words, stop, removeTarget, commit = true } = opts;
|
|
737
|
+
if (target !== undefined && !/^\w[\w-]*$/.test(target))
|
|
738
|
+
throw new Error(`a target's name is one word: ${target}`);
|
|
739
|
+
if (target === undefined && [start, words, stop].some((v) => v !== undefined))
|
|
740
|
+
throw new Error('--start, --open and --stop belong to a --target');
|
|
741
|
+
const file = path.join(dir, 'gcf', 'config.yml');
|
|
742
|
+
const hash = await commitMove(dir, 'gcf(config): play', {
|
|
743
|
+
files: [file],
|
|
744
|
+
commit,
|
|
745
|
+
key: 'config',
|
|
746
|
+
mutate: async () => {
|
|
747
|
+
const doc = configDoc(existsSync(file) ? readFileSync(file, 'utf8') : '');
|
|
748
|
+
const set = (keys, v) => v !== undefined && doc.setIn(['play', ...keys], v);
|
|
749
|
+
if (!YAML.isMap(doc.getIn(['play'], true))) doc.setIn(['play'], doc.createNode({}));
|
|
750
|
+
set(['build'], build);
|
|
751
|
+
set(['output'], output);
|
|
752
|
+
if (target !== undefined) {
|
|
753
|
+
if (!YAML.isMap(doc.getIn(['play', 'targets'], true)))
|
|
754
|
+
doc.setIn(['play', 'targets'], doc.createNode({}));
|
|
755
|
+
if (!YAML.isMap(doc.getIn(['play', 'targets', target], true)))
|
|
756
|
+
doc.setIn(['play', 'targets', target], doc.createNode({}));
|
|
757
|
+
set(['targets', target, 'start'], start);
|
|
758
|
+
set(['targets', target, 'open'], words);
|
|
759
|
+
set(['targets', target, 'stop'], stop);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
if (removeTarget !== undefined) doc.deleteIn(['play', 'targets', removeTarget]);
|
|
763
|
+
playOf(doc.toJS());
|
|
764
|
+
await atomicWrite(file, doc.toString());
|
|
765
|
+
},
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
return { hash, play: playOf(await readConfig(path.join(dir, 'gcf'))) };
|
|
769
|
+
}
|