gcf-aif-ext 0.27.1 → 0.28.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/README.md +3 -1
- package/commands/gcf.js +164 -11
- package/extension.json +1 -1
- package/lib/agent-permissions.js +1 -0
- package/lib/audit-core.js +12 -2
- package/lib/board.js +10 -0
- package/lib/build-queue.js +235 -0
- package/lib/build-waiter.js +5 -0
- package/lib/devices.js +133 -0
- package/lib/domain.js +12 -2
- package/lib/handoff.js +291 -0
- package/lib/main-lock.js +25 -1
- package/lib/play-notes.js +189 -0
- package/lib/play-on.js +42 -0
- package/lib/play.js +609 -128
- package/lib/playtest-list.js +23 -5
- package/lib/validate-item.js +1 -1
- package/lib/verify.js +8 -2
- package/lib/work-ops.js +39 -2
- package/package.json +1 -1
- package/skills/gcf-adopt/SKILL.md +3 -1
- package/skills/gcf-card/SKILL.md +1 -0
- package/skills/gcf-chat/SKILL.md +7 -4
- package/skills/gcf-verdict/SKILL.md +1 -1
- package/templates/agents-block.md +12 -2
package/lib/play.js
CHANGED
|
@@ -22,6 +22,11 @@ import { atomicWrite, renameRetrying } from './safe-path.js';
|
|
|
22
22
|
import { commitMove } from './unit-ops.js';
|
|
23
23
|
import { commandEnv } from './shell-env.js';
|
|
24
24
|
import { failureOf, killTree, runCommand } from './verify.js';
|
|
25
|
+
import { unplayedCloseCommit } from './verdict-fs.js';
|
|
26
|
+
import { CANCELLED, cancelled, sweepTickets, ticketClose, ticketOpen } from './build-queue.js';
|
|
27
|
+
import { readTicket, ticketSet, ticketsOf, waitTurn } from './build-queue.js';
|
|
28
|
+
import { fileURLToPath } from 'node:url';
|
|
29
|
+
import { DEVICE_ID_RE, devicesOf } from './devices.js';
|
|
25
30
|
|
|
26
31
|
export const NOT_SET_UP =
|
|
27
32
|
'how to play is not set up: ai-factory gcf playtest setup --build <cmd> --output <folder> ' +
|
|
@@ -95,43 +100,152 @@ const needText = (where, v) => {
|
|
|
95
100
|
throw new Error(`gcf/config.yml: ${where} must be a command or a text`);
|
|
96
101
|
};
|
|
97
102
|
|
|
103
|
+
/** A build's and a target's name: it goes into file names as it is (docs/Handoff-Plan.md d8). */
|
|
104
|
+
export const NAME_RE = /^\w[\w-]*$/;
|
|
105
|
+
/** The words the core knows in a `plays_on` row; another one is a warning, not a loss. */
|
|
106
|
+
export const VOCAB = {
|
|
107
|
+
device: ['phone', 'tablet', 'tv', 'pc', 'console', 'handheld', 'vr'],
|
|
108
|
+
os: ['android', 'ios', 'tvos', 'windows', 'macos', 'linux', 'xbox', 'ps', 'switch'],
|
|
109
|
+
input: ['touch', 'mouse', 'keyboard', 'gamepad'],
|
|
110
|
+
};
|
|
111
|
+
// Only the fields the core matches or shows: another would be prose posing as mechanics.
|
|
112
|
+
const ROW_KEYS = ['device', 'os', 'os_min', 'model', 'input'];
|
|
113
|
+
const FOR_KEYS = ['device', 'os', 'model'];
|
|
114
|
+
const VERSION_RE = /^\d+(?:\.\d+)*$/;
|
|
115
|
+
|
|
116
|
+
const isMap = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
117
|
+
|
|
118
|
+
/** A word of a row: the vocabulary's, or a warning. */
|
|
119
|
+
function wordOf(where, field, word, warnings) {
|
|
120
|
+
if (typeof word !== 'string' || !word.trim())
|
|
121
|
+
throw new Error(`gcf/config.yml: ${where}.${field} must be a word`);
|
|
122
|
+
if (VOCAB[field] && !VOCAB[field].includes(word))
|
|
123
|
+
warnings.push(`${where}.${field}: ${word} is no word the framework knows`);
|
|
124
|
+
|
|
125
|
+
return word;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A `plays_on` row or a target's `for`, checked; its unknown words go to `warnings`. */
|
|
129
|
+
function rowOf(where, row, keys, warnings) {
|
|
130
|
+
if (!isMap(row)) throw new Error(`gcf/config.yml: ${where} must map ${keys.join(', ')}`);
|
|
131
|
+
const out = {};
|
|
132
|
+
for (const [k, v] of Object.entries(row)) {
|
|
133
|
+
if (!keys.includes(k)) throw new Error(`gcf/config.yml: ${where} has no field ${k}`);
|
|
134
|
+
if (k === 'os_min') {
|
|
135
|
+
out.os_min = String(v);
|
|
136
|
+
if (!VERSION_RE.test(out.os_min))
|
|
137
|
+
throw new Error(`gcf/config.yml: ${where}.os_min must be numbers with dots: ${v}`);
|
|
138
|
+
} else if (k === 'input') out.input = [v].flat().map((w) => wordOf(where, k, w, warnings));
|
|
139
|
+
else out[k] = wordOf(where, k, v, warnings);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** One build's entry: its command, its output, the rows it plays on. */
|
|
146
|
+
function buildEntry(name, b, warnings) {
|
|
147
|
+
const where = `play.builds.${name}`;
|
|
148
|
+
if (!NAME_RE.test(name)) throw new Error(`a build's name is one word: ${name}`);
|
|
149
|
+
if (!isMap(b)) throw new Error(`gcf/config.yml: ${where} must map build:, output: and plays_on:`);
|
|
150
|
+
if (typeof b.build === 'object' && b.build !== null)
|
|
151
|
+
throw new Error(`gcf/config.yml: ${where}.build: profiles come with step 2, give one command`);
|
|
152
|
+
needText(`${where}.build`, b.build);
|
|
153
|
+
needOutput(b.output, `${where}.output`);
|
|
154
|
+
const rows = b.plays_on ?? [];
|
|
155
|
+
if (!Array.isArray(rows)) throw new Error(`gcf/config.yml: ${where}.plays_on must list rows`);
|
|
156
|
+
for (const [i, row] of rows.entries())
|
|
157
|
+
if (row?.device === undefined)
|
|
158
|
+
throw new Error(`gcf/config.yml: ${where}.plays_on[${i}] needs a device`);
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
build: b.build,
|
|
162
|
+
output: b.output,
|
|
163
|
+
plays_on: rows.map((row, i) => rowOf(`${where}.plays_on[${i}]`, row, ROW_KEYS, warnings)),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The builds of `play:`: `builds:` with one entry, or the old `build:`, `output:` as `default`. */
|
|
168
|
+
function buildsIn(play, warnings) {
|
|
169
|
+
if (play.builds === undefined) {
|
|
170
|
+
needText('play.build', play.build);
|
|
171
|
+
needOutput(play.output);
|
|
172
|
+
|
|
173
|
+
return { default: { build: play.build, output: play.output, plays_on: [] } };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (play.build !== undefined || play.output !== undefined)
|
|
177
|
+
throw new Error('gcf/config.yml: play.build and play.output go inside play.builds');
|
|
178
|
+
const builds = play.builds;
|
|
179
|
+
if (!isMap(builds))
|
|
180
|
+
throw new Error('gcf/config.yml: play.builds must map a name to build: and output:');
|
|
181
|
+
const names = Object.keys(builds);
|
|
182
|
+
if (names.length === 0) throw new Error('gcf/config.yml: play.builds names no build');
|
|
183
|
+
if (names.length > 1)
|
|
184
|
+
throw new Error('gcf/config.yml: play.builds: several builds come with step 2, keep one');
|
|
185
|
+
|
|
186
|
+
return Object.fromEntries(names.map((n) => [n, buildEntry(n, builds[n], warnings)]));
|
|
187
|
+
}
|
|
188
|
+
|
|
98
189
|
/**
|
|
99
190
|
* `play:` of a config, checked; null when the project has none. `output` stays inside the project:
|
|
100
|
-
* it is copied from there, and a path out of it would copy anything.
|
|
101
|
-
*
|
|
102
|
-
*
|
|
191
|
+
* it is copied from there, and a path out of it would copy anything. One build in step 1
|
|
192
|
+
* (docs/Handoff-Plan.md d1, d2): `build` and `output` are its own, for a reader of the old form.
|
|
193
|
+
* @returns {{build: string, output: string, builds: Record<string, {build: string, output: string,
|
|
194
|
+
* plays_on: object[]}>, auto_build: string, targets: Record<string, {start: string, open: string,
|
|
195
|
+
* stop?: string, build: string, for?: object, devices?: string, launches?: boolean}>,
|
|
196
|
+
* warnings: string[]} | null}
|
|
103
197
|
*/
|
|
104
198
|
export function playOf(config) {
|
|
105
199
|
const play = config.play;
|
|
106
200
|
if (play === undefined || play === null) return null;
|
|
107
201
|
if (typeof play !== 'object' || Array.isArray(play))
|
|
108
|
-
throw new Error('gcf/config.yml: play must map
|
|
109
|
-
|
|
110
|
-
|
|
202
|
+
throw new Error('gcf/config.yml: play must map builds: and targets:');
|
|
203
|
+
const warnings = [];
|
|
204
|
+
const builds = buildsIn(play, warnings);
|
|
205
|
+
const [only] = Object.keys(builds);
|
|
206
|
+
const auto = play.auto_build ?? only;
|
|
207
|
+
if (!Object.hasOwn(builds, auto))
|
|
208
|
+
throw new Error(`gcf/config.yml: play.auto_build names no build: ${auto}`);
|
|
111
209
|
const targets = play.targets ?? {};
|
|
112
|
-
needTargets(targets);
|
|
210
|
+
needTargets(targets, builds, warnings);
|
|
113
211
|
|
|
114
|
-
return {
|
|
212
|
+
return {
|
|
213
|
+
build: builds[only].build,
|
|
214
|
+
output: builds[only].output,
|
|
215
|
+
builds,
|
|
216
|
+
auto_build: auto,
|
|
217
|
+
targets: Object.fromEntries(
|
|
218
|
+
Object.entries(targets).map(([n, t]) => [n, { ...t, build: t.build ?? only }]),
|
|
219
|
+
),
|
|
220
|
+
warnings,
|
|
221
|
+
};
|
|
115
222
|
}
|
|
116
223
|
|
|
117
|
-
function needOutput(output) {
|
|
118
|
-
needText(
|
|
224
|
+
function needOutput(output, where = 'play.output') {
|
|
225
|
+
needText(where, output);
|
|
119
226
|
const parts = String(output).split(/[\\/]/);
|
|
120
227
|
if (path.isAbsolute(output) || /^[A-Za-z]:/.test(output) || parts.includes('..'))
|
|
121
|
-
throw new Error(`gcf/config.yml:
|
|
228
|
+
throw new Error(`gcf/config.yml: ${where} must lie inside the project: ${output}`);
|
|
122
229
|
}
|
|
123
230
|
|
|
124
|
-
function needTargets(targets) {
|
|
125
|
-
if (
|
|
231
|
+
function needTargets(targets, builds, warnings) {
|
|
232
|
+
if (!isMap(targets))
|
|
126
233
|
throw new Error('gcf/config.yml: play.targets must map a name to start: and open:');
|
|
127
|
-
for (const [name, t] of Object.entries(targets))
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
234
|
+
for (const [name, t] of Object.entries(targets)) needTarget(name, t, builds, warnings);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function needTarget(name, t, builds, warnings) {
|
|
238
|
+
const where = `play.targets.${name}`;
|
|
239
|
+
if (!NAME_RE.test(name)) throw new Error(`a target's name is one word: ${name}`);
|
|
240
|
+
if (!isMap(t)) throw new Error(`gcf/config.yml: ${where} must map start: and open:`);
|
|
241
|
+
needText(`${where}.start`, t.start);
|
|
242
|
+
needText(`${where}.open`, t.open);
|
|
243
|
+
for (const k of ['stop', 'devices']) if (t[k] !== undefined) needText(`${where}.${k}`, t[k]);
|
|
244
|
+
if (t.launches !== undefined && typeof t.launches !== 'boolean')
|
|
245
|
+
throw new Error(`gcf/config.yml: ${where}.launches is true or false`);
|
|
246
|
+
if (t.build !== undefined && !Object.hasOwn(builds, t.build))
|
|
247
|
+
throw new Error(`gcf/config.yml: ${where}.build names no build: ${t.build}`);
|
|
248
|
+
if (t.for !== undefined) rowOf(`${where}.for`, t.for, FOR_KEYS, warnings);
|
|
135
249
|
}
|
|
136
250
|
|
|
137
251
|
const projectPlay = async (dir) => {
|
|
@@ -150,18 +264,20 @@ const quoted = (p) =>
|
|
|
150
264
|
process.platform === 'win32' ? `"${p}"` : `"${p.replace(/(["\\$`])/g, '\\$1')}"`;
|
|
151
265
|
|
|
152
266
|
/** The machine's address on its network, for a phone on the same one. */
|
|
153
|
-
function lanAddress() {
|
|
267
|
+
export function lanAddress() {
|
|
154
268
|
for (const list of Object.values(os.networkInterfaces()))
|
|
155
269
|
for (const a of list ?? []) if (a.family === 'IPv4' && !a.internal) return a.address;
|
|
156
270
|
|
|
157
271
|
return 'localhost';
|
|
158
272
|
}
|
|
159
273
|
|
|
160
|
-
const fill = (text, { dir, port, lan }) =>
|
|
274
|
+
const fill = (text, { dir, port, lan, device = null }) =>
|
|
161
275
|
text
|
|
162
276
|
.replaceAll('{dir}', () => dir)
|
|
163
277
|
.replaceAll('{port}', String(port))
|
|
164
|
-
.replaceAll('{lan}', lan)
|
|
278
|
+
.replaceAll('{lan}', lan)
|
|
279
|
+
// A device's id passed DEVICE_ID_RE: no character of it means anything to a shell.
|
|
280
|
+
.replaceAll('{device}', () => device ?? '{device}');
|
|
165
281
|
|
|
166
282
|
/** Every file under `dir` with its stamp and size; none when it does not exist. */
|
|
167
283
|
function treeOf(dir) {
|
|
@@ -370,6 +486,7 @@ function linkTo(dir, who, key, source) {
|
|
|
370
486
|
* left the board, so nothing goes by it; while a link cannot be read, no snapshot goes.
|
|
371
487
|
*/
|
|
372
488
|
async function sweep(dir) {
|
|
489
|
+
sweepTickets(dir);
|
|
373
490
|
const ledger = await buildLedger(dir).catch(() => null);
|
|
374
491
|
const ids = sweepFolders(dir);
|
|
375
492
|
const links = linksOf(dir);
|
|
@@ -454,6 +571,18 @@ export function releaseBuild(projectDir, id) {
|
|
|
454
571
|
return null;
|
|
455
572
|
}
|
|
456
573
|
|
|
574
|
+
/**
|
|
575
|
+
* Why a finished build may not be put in place now: its ticket was cancelled (under the lock a
|
|
576
|
+
* cancel takes too: a stopped build puts nothing in place, docs/Handoff-Plan.md d6), or its card
|
|
577
|
+
* has moved on.
|
|
578
|
+
*/
|
|
579
|
+
async function refusedNow(dir, who, from, ticket) {
|
|
580
|
+
if (ticket !== null && cancelled(dir, ticket)) return { refused: CANCELLED, stopped: true };
|
|
581
|
+
const problem = who === MAIN ? null : await statusProblem(dir, who, from);
|
|
582
|
+
|
|
583
|
+
return problem ? { refused: problem } : null;
|
|
584
|
+
}
|
|
585
|
+
|
|
457
586
|
/**
|
|
458
587
|
* Put a finished build in place of snapshot `key` and point `who` at it, or throw and leave the old
|
|
459
588
|
* one with its facts. A card whose status moved on meanwhile (a verdict needs no wait for a build)
|
|
@@ -461,14 +590,12 @@ export function releaseBuild(projectDir, id) {
|
|
|
461
590
|
* one is thrown away (d23).
|
|
462
591
|
* @returns {Promise<{refused: string} | {facts: object}>}
|
|
463
592
|
*/
|
|
464
|
-
async function publish(dir, { who, key, tmp, facts, source, from, reuse }) {
|
|
593
|
+
async function publish(dir, { who, key, tmp, facts, source, from, reuse, ticket }) {
|
|
465
594
|
const target = snapOf(dir, key);
|
|
466
595
|
const old = `${target}.old-${process.pid}`;
|
|
467
596
|
const done = await withMainLock(dir, async () => {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
if (problem) return { refused: problem };
|
|
471
|
-
}
|
|
597
|
+
const refused = await refusedNow(dir, who, from, ticket);
|
|
598
|
+
if (refused) return refused;
|
|
472
599
|
|
|
473
600
|
const there = reuse && readJson(snapFactsOf(dir, key));
|
|
474
601
|
if (there && existsSync(target) && held(linksOf(dir), key)) {
|
|
@@ -503,44 +630,119 @@ async function publish(dir, { who, key, tmp, facts, source, from, reuse }) {
|
|
|
503
630
|
* Build card `id` with `play.build`, or link it to the snapshot of the same code another card
|
|
504
631
|
* holds (d23); the old snapshot stays until a new one has replaced it. `from`: the card's worktree,
|
|
505
632
|
* whose code is still its alone. `fresh`: build even when a snapshot of this code is there, and
|
|
506
|
-
* replace it.
|
|
507
|
-
* one.
|
|
633
|
+
* replace it. `wait`: a busy checkout is waited for on a ticket, not refused (docs/Handoff-Plan.md
|
|
634
|
+
* d6); `ticket`: the one a caller opened, and closes. A failed build is no error: it comes back as
|
|
635
|
+
* `problem`, and the card goes on without one.
|
|
508
636
|
* @returns {Promise<{id: string, ok: boolean, problem?: string, dirtied?: string[]}>}
|
|
509
637
|
*/
|
|
510
|
-
export async function buildItem(projectDir, id,
|
|
638
|
+
export async function buildItem(projectDir, id, opts = {}) {
|
|
639
|
+
const { from = null, fresh = false } = opts;
|
|
511
640
|
needId(id);
|
|
512
641
|
const dir = path.resolve(projectDir);
|
|
513
|
-
|
|
642
|
+
await projectPlay(dir);
|
|
514
643
|
const cwd = from ? path.resolve(from) : dir;
|
|
515
644
|
if (from && cwd !== path.join(session(dir), 'worktrees', id))
|
|
516
645
|
throw new Error(`--from names the worktree of ${id}, not ${from}`);
|
|
517
646
|
const problem = await statusProblem(dir, id, from);
|
|
518
647
|
if (problem) throw new Error(problem);
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
await
|
|
524
|
-
if (
|
|
525
|
-
|
|
648
|
+
|
|
649
|
+
return holding(dir, id, { ...opts, job: { build: id, from, fresh } }, async (hold) => {
|
|
650
|
+
// Read again after a wait: the config and the card may have changed meanwhile.
|
|
651
|
+
const play = await projectPlay(dir);
|
|
652
|
+
const again = await statusProblem(dir, id, from);
|
|
653
|
+
if (again) throw new Error(again);
|
|
654
|
+
if (!from) await needCardCode(dir, id);
|
|
655
|
+
|
|
656
|
+
return runBuild(dir, cwd, play, { who: id, from, own: hold.own, fresh, ticket: hold.n });
|
|
657
|
+
});
|
|
526
658
|
}
|
|
527
659
|
|
|
528
660
|
/**
|
|
529
661
|
* Build the checkout as it is, uncommitted edits too (d30): "Play main" after the operator's own
|
|
530
662
|
* changes. A clean tree whose code a card's snapshot already holds takes that one, unless `fresh`.
|
|
531
663
|
*/
|
|
532
|
-
export async function buildMain(projectDir,
|
|
664
|
+
export async function buildMain(projectDir, opts = {}) {
|
|
665
|
+
const { fresh = false } = opts;
|
|
533
666
|
const dir = path.resolve(projectDir);
|
|
534
|
-
|
|
535
|
-
|
|
667
|
+
await projectPlay(dir);
|
|
668
|
+
|
|
669
|
+
return holding(dir, MAIN, { ...opts, job: { build: null, fresh } }, async (hold) => {
|
|
670
|
+
const play = await projectPlay(dir);
|
|
671
|
+
|
|
672
|
+
return runBuild(dir, dir, play, {
|
|
673
|
+
who: MAIN,
|
|
674
|
+
from: null,
|
|
675
|
+
own: hold.own,
|
|
676
|
+
fresh,
|
|
677
|
+
ticket: hold.n,
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Run `fn` holding what a build of `who` needs. A harness run's own build goes under the lock its
|
|
684
|
+
* run holds; without `wait` or `ticket` a busy checkout refuses, as before the queue; with them
|
|
685
|
+
* the build waits its turn on a ticket (lib/build-queue.js). A ticket this opened, this closes.
|
|
686
|
+
*/
|
|
687
|
+
async function holding(dir, who, opts, fn) {
|
|
688
|
+
const { from = null, wait = false, ticket = null, job } = opts;
|
|
689
|
+
if (ownRun(dir) || (!wait && ticket === null)) return holdingNow(dir, who, from, fn);
|
|
690
|
+
const n = ticket ?? (await ticketOpen(dir, job));
|
|
691
|
+
try {
|
|
692
|
+
const got = await waitTurn(dir, n, { from });
|
|
693
|
+
try {
|
|
694
|
+
return await fn({ own: Boolean(from), n });
|
|
695
|
+
} finally {
|
|
696
|
+
await got.release?.();
|
|
697
|
+
// A run that yielded merges its own; a claim of the checkout leaves what waited to this one.
|
|
698
|
+
if (got.release && !got.yielded) await mergeWaiting(dir);
|
|
699
|
+
}
|
|
700
|
+
} finally {
|
|
701
|
+
if (ticket === null) ticketClose(dir, n);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** Whether this process belongs to the main run that holds the checkout: it waits for nobody. */
|
|
706
|
+
const ownRun = (dir) => {
|
|
707
|
+
const holder = runHolder(dir, MAIN_RUN);
|
|
708
|
+
|
|
709
|
+
return holder !== null && process.env.GCF_RUN_OWNER === `run:${holder}`;
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/** The hold as before the queue: a busy checkout refuses. */
|
|
713
|
+
async function holdingNow(dir, who, from, fn) {
|
|
714
|
+
const hold = from ? { own: true, release: null } : await holdCheckout(dir, nameOf(who));
|
|
536
715
|
try {
|
|
537
|
-
return await
|
|
716
|
+
return await fn({ ...hold, n: null });
|
|
538
717
|
} finally {
|
|
539
718
|
await hold.release?.();
|
|
540
719
|
if (hold.release) await mergeWaiting(dir);
|
|
541
720
|
}
|
|
542
721
|
}
|
|
543
722
|
|
|
723
|
+
/**
|
|
724
|
+
* A card's build in the checkout builds the checkout's code: refused when that is no longer the
|
|
725
|
+
* card's (docs/Handoff-Plan.md d7). `.ai-factory/` is left out: the moves commit plans there.
|
|
726
|
+
*/
|
|
727
|
+
async function needCardCode(dir, id) {
|
|
728
|
+
const item = (await buildLedger(dir)).item(id);
|
|
729
|
+
const at = item?.data.status === 'unplayed' ? unplayedCloseCommit(dir, id) : item?.data.since;
|
|
730
|
+
if (!at || gitOut(dir, ['cat-file', '-e', `${at}^{commit}`]) === null) return;
|
|
731
|
+
if (codeTree(dir, 'HEAD') !== codeTree(dir, at))
|
|
732
|
+
throw new Error(`the code of ${id} has left the folder: Build main builds what is there now`);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** The top of a commit's tree without the framework's files and the plans. */
|
|
736
|
+
function codeTree(cwd, rev) {
|
|
737
|
+
const out = gitOut(cwd, ['ls-tree', '-z', rev]) ?? '';
|
|
738
|
+
const skip = [...NOT_CODE, '.ai-factory'];
|
|
739
|
+
|
|
740
|
+
return out
|
|
741
|
+
.split('\0')
|
|
742
|
+
.filter((l) => l && !skip.includes(l.slice(l.indexOf('\t') + 1)))
|
|
743
|
+
.join('\0');
|
|
744
|
+
}
|
|
745
|
+
|
|
544
746
|
/**
|
|
545
747
|
* What a worktree run left verified while this click held the checkout: that run has ended, and no
|
|
546
748
|
* other would merge it until the next one starts. A refusal leaves it for that one.
|
|
@@ -564,7 +766,7 @@ const answerOf = (who, key, facts, source) => ({
|
|
|
564
766
|
source,
|
|
565
767
|
});
|
|
566
768
|
|
|
567
|
-
async function runBuild(dir, cwd, play, { who, from, own, fresh }) {
|
|
769
|
+
async function runBuild(dir, cwd, play, { who, from, own, fresh, ticket = null }) {
|
|
568
770
|
const started = new Date().toISOString();
|
|
569
771
|
await mkdir(buildsOf(dir), { recursive: true });
|
|
570
772
|
await mkdir(snapsOf(dir), { recursive: true });
|
|
@@ -613,7 +815,7 @@ async function runBuild(dir, cwd, play, { who, from, own, fresh }) {
|
|
|
613
815
|
snapsOf(dir),
|
|
614
816
|
`${key}.${who === MAIN ? 'checkout' : `card-${who}`}.tmp-${process.pid}`,
|
|
615
817
|
);
|
|
616
|
-
const at = { who, key, source, loose, from, reuse, tmp, failed };
|
|
818
|
+
const at = { who, key, source, loose, from, reuse, tmp, failed, ticket };
|
|
617
819
|
try {
|
|
618
820
|
return await buildAndPublish(dir, cwd, play, at);
|
|
619
821
|
} catch (e) {
|
|
@@ -623,27 +825,48 @@ async function runBuild(dir, cwd, play, { who, from, own, fresh }) {
|
|
|
623
825
|
}
|
|
624
826
|
}
|
|
625
827
|
|
|
828
|
+
/**
|
|
829
|
+
* `play.build` with its output in the holder's log. A ticket's cancel kills its tree, and the log
|
|
830
|
+
* says so (docs/Handoff-Plan.md d6).
|
|
831
|
+
*/
|
|
832
|
+
async function buildCommand(dir, cwd, play, { who, ticket, minutes }) {
|
|
833
|
+
const log = await open(logOf(dir, who), 'w');
|
|
834
|
+
const run = runCommand(play.build, cwd, ['ignore', log.fd, log.fd], minutes * 60000);
|
|
835
|
+
let stopped = false;
|
|
836
|
+
const watch =
|
|
837
|
+
ticket === null
|
|
838
|
+
? null
|
|
839
|
+
: setInterval(() => {
|
|
840
|
+
if (stopped || !cancelled(dir, ticket)) return;
|
|
841
|
+
stopped = true;
|
|
842
|
+
killTree(run.child.pid);
|
|
843
|
+
}, 300);
|
|
844
|
+
try {
|
|
845
|
+
const res = await run;
|
|
846
|
+
if (stopped) await log.write(`\n${CANCELLED}\n`);
|
|
847
|
+
|
|
848
|
+
return { ...res, stopped };
|
|
849
|
+
} finally {
|
|
850
|
+
clearInterval(watch ?? undefined);
|
|
851
|
+
await log.close();
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
626
855
|
/** Whether git ignores `output`: a build there must not show up as the project's change. */
|
|
627
856
|
const ignored = (cwd, output) =>
|
|
628
857
|
// The slash: a rule for folders only must match a folder not built yet.
|
|
629
858
|
spawnSync('git', ['check-ignore', '-q', `${output}/`], { cwd }).status === 0;
|
|
630
859
|
|
|
631
860
|
async function buildAndPublish(dir, cwd, play, at) {
|
|
632
|
-
const { who, key, source, loose, from, reuse, tmp, failed } = at;
|
|
861
|
+
const { who, key, source, loose, from, reuse, tmp, failed, ticket } = at;
|
|
633
862
|
const output = path.join(cwd, play.output);
|
|
634
863
|
if (!ignored(cwd, play.output))
|
|
635
864
|
return failed(`play.output ${play.output} is not ignored by git: add it to .gitignore`);
|
|
636
865
|
const before = treeOf(output);
|
|
637
866
|
const commit = gitOut(cwd, ['rev-parse', 'HEAD'])?.trim() ?? null;
|
|
638
867
|
const minutes = (await readConfig(path.join(dir, 'gcf'))).dod_timeout_min ?? 0;
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
try {
|
|
642
|
-
res = await runCommand(play.build, cwd, ['ignore', log.fd, log.fd], minutes * 60000);
|
|
643
|
-
} finally {
|
|
644
|
-
await log.close();
|
|
645
|
-
}
|
|
646
|
-
|
|
868
|
+
const res = await buildCommand(dir, cwd, play, { who, ticket, minutes });
|
|
869
|
+
if (res.stopped) return failed(CANCELLED);
|
|
647
870
|
const why = failureOf('play.build', play.build, minutes, res);
|
|
648
871
|
if (why) return failed(withTail(why, logOf(dir, who)));
|
|
649
872
|
const after = treeOf(output);
|
|
@@ -661,7 +884,8 @@ async function buildAndPublish(dir, cwd, play, at) {
|
|
|
661
884
|
dirtied,
|
|
662
885
|
size: [...after.values()].reduce((sum, f) => sum + f.size, 0),
|
|
663
886
|
};
|
|
664
|
-
const done = await publish(dir, { who, key, tmp, facts, source, from, reuse });
|
|
887
|
+
const done = await publish(dir, { who, key, tmp, facts, source, from, reuse, ticket });
|
|
888
|
+
if (done.stopped) return failed(CANCELLED);
|
|
665
889
|
rmSync(attemptOf(dir, who), { force: true });
|
|
666
890
|
// Not a failed build: the card has moved on, and nobody waits for this one.
|
|
667
891
|
if (done.refused) return { id: who, ok: false, problem: done.refused, moved_on: true };
|
|
@@ -800,57 +1024,190 @@ function recordPlayer(dir, record) {
|
|
|
800
1024
|
* Play the snapshot card `id` holds on `target`; `id` null: the checkout's ("Play main"). One
|
|
801
1025
|
* player per project: the last one stops first. The process is cut loose from its caller, so a
|
|
802
1026
|
* board's restart does not end the game. A process that ends with 0 has launched what it launches:
|
|
803
|
-
* nothing is left to stop.
|
|
1027
|
+
* nothing is left to stop. With no build, `wait` or `ticket` builds it on a ticket first and starts
|
|
1028
|
+
* after it, in this process; without them it refuses, as before (docs/Handoff-Plan.md d9).
|
|
804
1029
|
*/
|
|
805
|
-
export async function startPlay(projectDir, id, targetName) {
|
|
1030
|
+
export async function startPlay(projectDir, id, targetName, opts = {}) {
|
|
1031
|
+
const { wait = false, ticket = null, device = null } = opts;
|
|
806
1032
|
if (id !== null) needId(id);
|
|
807
1033
|
const dir = path.resolve(projectDir);
|
|
808
|
-
|
|
1034
|
+
targetOf(await projectPlay(dir), targetName);
|
|
1035
|
+
const how = { target: targetName, device };
|
|
1036
|
+
if ((!wait && ticket === null) || hasBuild(dir, id)) return startNow(dir, id, how);
|
|
1037
|
+
const n = ticket ?? (await ticketOpen(dir, { build: id, start: how }));
|
|
1038
|
+
try {
|
|
1039
|
+
return await buildThenStart(dir, id, how, n);
|
|
1040
|
+
} finally {
|
|
1041
|
+
if (ticket === null) ticketClose(dir, n);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
async function buildThenStart(dir, id, how, n) {
|
|
1046
|
+
const b = await (id === null ? buildMain(dir, { ticket: n }) : buildItem(dir, id, { ticket: n }));
|
|
1047
|
+
if (!b.ok) throw new Error(`the build for this play failed: ${b.problem}`);
|
|
1048
|
+
if (cancelled(dir, n)) throw new Error(CANCELLED);
|
|
1049
|
+
|
|
1050
|
+
return startNow(dir, id, how);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* The device a start plays on (docs/Handoff-Plan.md t8): an id of the target's `devices` answer
|
|
1055
|
+
* that fits a row and is ready. A target whose commands name `{device}` plays on none without one.
|
|
1056
|
+
* @returns {Promise<{id: string, name: string} | null>}
|
|
1057
|
+
*/
|
|
1058
|
+
async function deviceFor(dir, name, target, id) {
|
|
1059
|
+
const needs = [target.start, target.stop ?? ''].some((c) => c.includes('{device}'));
|
|
1060
|
+
if (id === null || id === undefined) {
|
|
1061
|
+
if (needs) throw new Error(`no device: ${name} plays on one (--device <id>)`);
|
|
1062
|
+
|
|
1063
|
+
return null;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
if (!DEVICE_ID_RE.test(id)) throw new Error(`a device id is letters, digits, . : - _: ${id}`);
|
|
1067
|
+
if (!target.devices) return { id, name: id };
|
|
1068
|
+
const r = await devicesOf(dir, name);
|
|
1069
|
+
const d = r.devices.find((x) => x.id === id);
|
|
1070
|
+
if (!d)
|
|
1071
|
+
throw new Error(`no device ${id} on ${name}: ${r.problem ?? r.unavailable ?? 'not found'}`);
|
|
1072
|
+
if (!d.play) throw new Error(`${d.name} cannot play this now: ${d.why ?? d.state}`);
|
|
1073
|
+
|
|
1074
|
+
return { id, name: d.name };
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function targetOf(play, targetName) {
|
|
809
1078
|
const target = Object.hasOwn(play.targets, targetName) ? play.targets[targetName] : null;
|
|
810
1079
|
if (!target)
|
|
811
1080
|
throw new Error(
|
|
812
1081
|
`no target ${targetName}: ${Object.keys(play.targets).join(', ') || 'none set up'}`,
|
|
813
1082
|
);
|
|
814
1083
|
|
|
1084
|
+
return target;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
const hasBuild = (dir, id) => {
|
|
1088
|
+
try {
|
|
1089
|
+
return Boolean(heldKey(dir, id));
|
|
1090
|
+
} catch {
|
|
1091
|
+
return false;
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
/** Whether card `id` (null: the checkout) has a snapshot to play now. */
|
|
1096
|
+
export const hasBuildOf = (projectDir, id) => hasBuild(path.resolve(projectDir), id);
|
|
1097
|
+
|
|
1098
|
+
/** The start itself, on the snapshot there is. */
|
|
1099
|
+
async function startNow(dir, id, { target: targetName, device: deviceId = null }) {
|
|
1100
|
+
const play = await projectPlay(dir);
|
|
1101
|
+
const target = targetOf(play, targetName);
|
|
1102
|
+
const device = await deviceFor(dir, targetName, target, deviceId);
|
|
815
1103
|
// Held to the end: a verdict meanwhile waits, or it would miss the player this start records.
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
{
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
1104
|
+
const started = await withMainLock(dir, () =>
|
|
1105
|
+
startLocked(dir, id, { targetName, target, device }),
|
|
1106
|
+
);
|
|
1107
|
+
|
|
1108
|
+
return started.launching ? awaitLaunch(dir, started) : started;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/** A target's command with the snapshot, the port, the address and the device filled in. */
|
|
1112
|
+
function commandOf(snapshot, at) {
|
|
1113
|
+
// `{dir}/game.exe` is one word: Windows' shell takes no name after a closing quote.
|
|
1114
|
+
return (text) =>
|
|
1115
|
+
fill(
|
|
1116
|
+
text
|
|
1117
|
+
.replace(/"\{dir\}([^"]*)"/g, (_, tail) => quoted(snapshot + tail))
|
|
1118
|
+
.replace(/\{dir\}([\\/][^\s"'&|<>;()]*)?/g, (_, tail = '') => quoted(snapshot + tail)),
|
|
1119
|
+
{ ...at, dir: quoted(snapshot) },
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
/** What a player plays: its build, and the device when it has one. */
|
|
1124
|
+
const playsOn = (target, device) => ({
|
|
1125
|
+
build: target.build,
|
|
1126
|
+
...(device ? { device: device.id, device_name: device.name } : {}),
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
async function startLocked(dir, id, { targetName, target, device }) {
|
|
1130
|
+
await sweep(dir);
|
|
1131
|
+
const key = heldKey(dir, id);
|
|
1132
|
+
const snapshot = snapOf(dir, key);
|
|
1133
|
+
stopPlay(dir);
|
|
1134
|
+
const port = await freePort();
|
|
1135
|
+
const at = { port, lan: lanAddress(), device: device?.id ?? null };
|
|
1136
|
+
const inCommand = commandOf(snapshot, at);
|
|
1137
|
+
// The device's name is for the eyes only: never in a command.
|
|
1138
|
+
const open = fill(target.open, { ...at, dir: snapshot }).replaceAll(
|
|
1139
|
+
'{device_name}',
|
|
1140
|
+
device?.name ?? 'the device',
|
|
1141
|
+
);
|
|
1142
|
+
const child = launch(dir, inCommand(target.start));
|
|
1143
|
+
const exited = exitOf(child);
|
|
1144
|
+
// The address only: words may follow it ("— on a phone: …"), and a tab takes no words.
|
|
1145
|
+
const url = /^https?:\/\/[^\s]+/.exec(open)?.[0] ?? null;
|
|
1146
|
+
const stop = target.stop ? { stop: inCommand(target.stop) } : {};
|
|
1147
|
+
const main = id === null ? { main: true } : {};
|
|
1148
|
+
const on = playsOn(target, device);
|
|
1149
|
+
const record = {
|
|
1150
|
+
pid: child.pid,
|
|
1151
|
+
id,
|
|
1152
|
+
...main,
|
|
1153
|
+
snap: key,
|
|
1154
|
+
target: targetName,
|
|
1155
|
+
...on,
|
|
1156
|
+
port,
|
|
1157
|
+
open,
|
|
1158
|
+
url,
|
|
1159
|
+
};
|
|
1160
|
+
const started = new Date().toISOString();
|
|
1161
|
+
// Before the wait: a starter killed in it must not leave a player nobody can stop.
|
|
1162
|
+
recordPlayer(dir, { ...record, started, ...stop });
|
|
1163
|
+
// It installs and launches, then ends: waited for out of the lock, its record held meanwhile,
|
|
1164
|
+
// so a verdict that takes the snapshot stops it first (dropSnap).
|
|
1165
|
+
if (target.launches) return { launching: true, child, exited, record, targetName };
|
|
1166
|
+
const end = await outcomeOf(child, target.start.includes('{port}') ? port : null);
|
|
1167
|
+
if (end.state === 'running') return { state: 'running', ...record, started, ...stop };
|
|
1168
|
+
|
|
1169
|
+
return settled(dir, end, { child, record, inCommand, target, targetName });
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/** A start that did not stay running: launched, or a failure undone with its log's tail. */
|
|
1173
|
+
function settled(dir, end, { child, record, inCommand, target, targetName }) {
|
|
1174
|
+
rmSync(playerFile(dir), { force: true });
|
|
1175
|
+
if (end.state === 'launched') {
|
|
1176
|
+
const { pid: _pid, port, snap: _snap, ...rest } = record;
|
|
1177
|
+
|
|
1178
|
+
return { state: 'launched', ...rest };
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
if (end.state === 'silent') killTree(child.pid);
|
|
1182
|
+
if (target.stop) runStop(dir, inCommand(target.stop));
|
|
1183
|
+
const why =
|
|
1184
|
+
end.state === 'silent' ? `did not answer on port ${record.port}` : `failed (${end.why})`;
|
|
1185
|
+
throw new Error(withTail(`play.targets.${targetName}.start ${why}`, playerLog(dir)));
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
/** How a process ended: its exit code, or the error that kept it from starting. */
|
|
1189
|
+
function exitOf(child) {
|
|
1190
|
+
return new Promise((resolve) => {
|
|
1191
|
+
child.once('error', (error) => resolve({ status: null, why: error.message }));
|
|
1192
|
+
child.once('exit', (status, signal) => resolve({ status, why: `exit ${status ?? signal}` }));
|
|
851
1193
|
});
|
|
852
1194
|
}
|
|
853
1195
|
|
|
1196
|
+
/** A `launches` start: its exit is the answer, with the tail of its log when it failed. */
|
|
1197
|
+
async function awaitLaunch(dir, { child, exited, record, targetName }) {
|
|
1198
|
+
// Cut loose from this process, yet waited for: an unreferenced child would let it end first.
|
|
1199
|
+
child.ref();
|
|
1200
|
+
const end = await exited;
|
|
1201
|
+
if (readJson(playerFile(dir))?.pid === child.pid) rmSync(playerFile(dir), { force: true });
|
|
1202
|
+
if (end.status === 0) {
|
|
1203
|
+
const { pid: _pid, port: _port, ...rest } = record;
|
|
1204
|
+
|
|
1205
|
+
return { state: 'launched', ...rest };
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
throw new Error(withTail(`play.targets.${targetName}.start failed (${end.why})`, playerLog(dir)));
|
|
1209
|
+
}
|
|
1210
|
+
|
|
854
1211
|
/** The snapshot card `id` holds, or the checkout's for null; none throws what to do. */
|
|
855
1212
|
function heldKey(dir, id) {
|
|
856
1213
|
const link = readJson(linkOf(dir, id ?? MAIN));
|
|
@@ -925,6 +1282,17 @@ export function buildOf(projectDir, id, status, links = linksOf(projectDir)) {
|
|
|
925
1282
|
return heldBuild(dir, id, links);
|
|
926
1283
|
}
|
|
927
1284
|
|
|
1285
|
+
/** The build tickets in their order, as a board shows them (lib/build-queue.js). */
|
|
1286
|
+
export const queueOf = (dir) =>
|
|
1287
|
+
ticketsOf(dir).map((t) => ({
|
|
1288
|
+
ticket: t.n,
|
|
1289
|
+
job: t.job,
|
|
1290
|
+
at: t.at,
|
|
1291
|
+
after: t.after ?? null,
|
|
1292
|
+
building: t.building === true,
|
|
1293
|
+
cancelled: t.cancelled === true,
|
|
1294
|
+
}));
|
|
1295
|
+
|
|
928
1296
|
/**
|
|
929
1297
|
* How the project plays, read only: its `play:` whole (d26), the checkout's snapshot (d30) and the
|
|
930
1298
|
* running player; null with no `play:`.
|
|
@@ -934,9 +1302,9 @@ export function playFacts(projectDir, config) {
|
|
|
934
1302
|
try {
|
|
935
1303
|
play = playOf(config);
|
|
936
1304
|
} catch (e) {
|
|
937
|
-
const none = { build: null, output: null,
|
|
1305
|
+
const none = { build: null, output: null, builds: [], auto_build: null, targets: [] };
|
|
938
1306
|
|
|
939
|
-
return { problem: e.message, ...none };
|
|
1307
|
+
return { problem: e.message, ...none, warnings: [], main: null, running: null, queue: [] };
|
|
940
1308
|
}
|
|
941
1309
|
|
|
942
1310
|
if (!play) return null;
|
|
@@ -947,13 +1315,21 @@ export function playFacts(projectDir, config) {
|
|
|
947
1315
|
problem: null,
|
|
948
1316
|
build: play.build,
|
|
949
1317
|
output: play.output,
|
|
1318
|
+
builds: Object.entries(play.builds).map(([name, b]) => ({ name, ...b })),
|
|
1319
|
+
auto_build: play.auto_build,
|
|
1320
|
+
warnings: play.warnings,
|
|
950
1321
|
targets: Object.entries(play.targets).map(([name, t]) => ({
|
|
951
1322
|
name,
|
|
952
1323
|
start: t.start,
|
|
953
1324
|
open: t.open,
|
|
954
1325
|
stop: t.stop ?? null,
|
|
1326
|
+
build: t.build,
|
|
1327
|
+
for: t.for ?? null,
|
|
1328
|
+
devices: t.devices ?? null,
|
|
1329
|
+
launches: t.launches === true,
|
|
955
1330
|
})),
|
|
956
1331
|
main: heldBuild(dir, MAIN, linksOf(dir)),
|
|
1332
|
+
queue: queueOf(dir),
|
|
957
1333
|
running: p
|
|
958
1334
|
? {
|
|
959
1335
|
id: p.id,
|
|
@@ -979,53 +1355,85 @@ export async function playStatus(projectDir) {
|
|
|
979
1355
|
return playFacts(dir, await readConfig(path.join(dir, 'gcf')));
|
|
980
1356
|
}
|
|
981
1357
|
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
* Only what is given changes. Checked as a whole before it is written.
|
|
985
|
-
* @param {{build?: string, output?: string, target?: string, start?: string, open?: string,
|
|
986
|
-
* stop?: string, removeTarget?: string, commit?: boolean}} opts
|
|
987
|
-
*/
|
|
1358
|
+
const TARGET_FIELDS = ['start', 'words', 'stop', 'for', 'devices', 'launches'];
|
|
1359
|
+
|
|
988
1360
|
/** What a setup may not be: checked before anything is written. */
|
|
989
|
-
function needSetup(dir,
|
|
990
|
-
if (target !== undefined &&
|
|
991
|
-
throw new Error(`a target's name is one word: ${target}`);
|
|
992
|
-
if (target === undefined &&
|
|
993
|
-
throw new Error(
|
|
1361
|
+
function needSetup(dir, o) {
|
|
1362
|
+
if (o.target !== undefined && !NAME_RE.test(o.target))
|
|
1363
|
+
throw new Error(`a target's name is one word: ${o.target}`);
|
|
1364
|
+
if (o.target === undefined && TARGET_FIELDS.some((k) => o[k] !== undefined))
|
|
1365
|
+
throw new Error(
|
|
1366
|
+
'--start, --open, --stop, --for, --devices and --launches belong to a --target',
|
|
1367
|
+
);
|
|
994
1368
|
// Now, not at the first build minutes later (d21).
|
|
995
|
-
if (output !== undefined) needOutput(output);
|
|
996
|
-
if (output !== undefined && !ignored(dir, output))
|
|
997
|
-
throw new Error(`play.output ${output} is not ignored by git: add it to .gitignore`);
|
|
1369
|
+
if (o.output !== undefined) needOutput(o.output);
|
|
1370
|
+
if (o.output !== undefined && !ignored(dir, o.output))
|
|
1371
|
+
throw new Error(`play.output ${o.output} is not ignored by git: add it to .gitignore`);
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* Where `--build`, `--output` and `--plays-on` go (docs/Handoff-Plan.md d2): into the one build of
|
|
1376
|
+
* `builds:`, whatever its name; else into the old `build:` and `output:`, which `--plays-on` turns
|
|
1377
|
+
* into `builds.default`.
|
|
1378
|
+
*/
|
|
1379
|
+
function buildPath(doc, playsOn) {
|
|
1380
|
+
const old = doc.toJS().play ?? {};
|
|
1381
|
+
const names = isMap(old.builds) ? Object.keys(old.builds) : null;
|
|
1382
|
+
if (names?.length === 1) return ['play', 'builds', names[0]];
|
|
1383
|
+
// Several builds: the one to set is named with step 2; playOf refuses play.build beside them.
|
|
1384
|
+
if (playsOn === undefined || old.builds !== undefined) return ['play'];
|
|
1385
|
+
doc.setIn(
|
|
1386
|
+
['play', 'builds'],
|
|
1387
|
+
doc.createNode({ default: { build: old.build, output: old.output } }),
|
|
1388
|
+
);
|
|
1389
|
+
doc.deleteIn(['play', 'build']);
|
|
1390
|
+
doc.deleteIn(['play', 'output']);
|
|
1391
|
+
|
|
1392
|
+
return ['play', 'builds', 'default'];
|
|
998
1393
|
}
|
|
999
1394
|
|
|
1395
|
+
const mapAt = (doc, keys) => {
|
|
1396
|
+
if (!YAML.isMap(doc.getIn(keys, true))) doc.setIn(keys, doc.createNode({}));
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1000
1399
|
/** config.yml's text with `play:` changed as given; what is not given stays. */
|
|
1001
|
-
function playDoc(text,
|
|
1400
|
+
function playDoc(text, o) {
|
|
1002
1401
|
const doc = configDoc(text);
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
set(
|
|
1006
|
-
set(['
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1402
|
+
mapAt(doc, ['play']);
|
|
1403
|
+
const at = buildPath(doc, o.playsOn);
|
|
1404
|
+
const set = (keys, v) => v !== undefined && doc.setIn(keys, doc.createNode(v));
|
|
1405
|
+
set([...at, 'build'], o.build);
|
|
1406
|
+
set([...at, 'output'], o.output);
|
|
1407
|
+
set([...at, 'plays_on'], o.playsOn);
|
|
1408
|
+
if (o.target !== undefined) {
|
|
1409
|
+
const t = ['play', 'targets', o.target];
|
|
1410
|
+
mapAt(doc, ['play', 'targets']);
|
|
1411
|
+
mapAt(doc, t);
|
|
1412
|
+
for (const [k, field] of [['start'], ['words', 'open'], ['stop'], ['for'], ['devices']])
|
|
1413
|
+
set([...t, field ?? k], o[k]);
|
|
1414
|
+
if (o.launches !== undefined) doc.setIn([...t, 'launches'], o.launches);
|
|
1015
1415
|
}
|
|
1016
1416
|
|
|
1017
|
-
if (removeTarget !== undefined) doc.deleteIn(['play', 'targets', removeTarget]);
|
|
1417
|
+
if (o.removeTarget !== undefined) doc.deleteIn(['play', 'targets', o.removeTarget]);
|
|
1018
1418
|
|
|
1019
1419
|
return doc;
|
|
1020
1420
|
}
|
|
1021
1421
|
|
|
1422
|
+
/**
|
|
1423
|
+
* `gcf playtest setup`: how the project builds and starts its game, committed as `gcf(config)`.
|
|
1424
|
+
* Only what is given changes. Checked as a whole before it is written.
|
|
1425
|
+
* @param {{build?: string, output?: string, playsOn?: object[], target?: string, start?: string,
|
|
1426
|
+
* open?: string, stop?: string, for?: object, devices?: string, launches?: boolean,
|
|
1427
|
+
* removeTarget?: string, commit?: boolean}} opts
|
|
1428
|
+
*/
|
|
1022
1429
|
export async function setupPlay(projectDir, opts) {
|
|
1023
1430
|
const dir = path.resolve(projectDir);
|
|
1024
|
-
const {
|
|
1025
|
-
|
|
1431
|
+
const { open: words, commit = true, ...rest } = opts;
|
|
1432
|
+
const o = { ...rest, words };
|
|
1433
|
+
needSetup(dir, o);
|
|
1026
1434
|
const file = path.join(dir, 'gcf', 'config.yml');
|
|
1027
1435
|
const was = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
1028
|
-
const doc = playDoc(was,
|
|
1436
|
+
const doc = playDoc(was, o);
|
|
1029
1437
|
// The same setup again (an agent proposed what is there): nothing to write, nothing to commit.
|
|
1030
1438
|
const same =
|
|
1031
1439
|
JSON.stringify(configDoc(was).toJS()?.play ?? null) === JSON.stringify(doc.toJS().play);
|
|
@@ -1038,7 +1446,7 @@ export async function setupPlay(projectDir, opts) {
|
|
|
1038
1446
|
mutate: async () => {
|
|
1039
1447
|
// Read again under the lock: a move meanwhile may have changed the file.
|
|
1040
1448
|
const now = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
1041
|
-
const next = playDoc(now,
|
|
1449
|
+
const next = playDoc(now, o);
|
|
1042
1450
|
playOf(next.toJS());
|
|
1043
1451
|
await atomicWrite(file, next.toString());
|
|
1044
1452
|
},
|
|
@@ -1046,3 +1454,76 @@ export async function setupPlay(projectDir, opts) {
|
|
|
1046
1454
|
|
|
1047
1455
|
return { hash, play: playOf(await readConfig(path.join(dir, 'gcf'))) };
|
|
1048
1456
|
}
|
|
1457
|
+
|
|
1458
|
+
// The waiter of a `--queue` ticket: a process of its own, so a board's restart does not end it.
|
|
1459
|
+
const WAITER = fileURLToPath(new URL('./build-waiter.js', import.meta.url));
|
|
1460
|
+
// It takes its ticket under the main lock, which a start holds while its port comes up (15 s).
|
|
1461
|
+
const WAITER_START_MS = 90000;
|
|
1462
|
+
|
|
1463
|
+
/** Do ticket `n`'s job: a build, or a build then a start (d9). */
|
|
1464
|
+
function jobOf(dir, job, n) {
|
|
1465
|
+
if (job.start) return startPlay(dir, job.build, job.start.target, { ...job.start, ticket: n });
|
|
1466
|
+
if (job.build === null) return buildMain(dir, { fresh: job.fresh, ticket: n });
|
|
1467
|
+
|
|
1468
|
+
return buildItem(dir, job.build, { from: job.from ?? null, fresh: job.fresh, ticket: n });
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* The waiter's work (lib/build-waiter.js): take ticket `n` as its own, do its job, close it with
|
|
1473
|
+
* what came of it (`.build-<n>.out.json`), which the board reads once the ticket is gone.
|
|
1474
|
+
*/
|
|
1475
|
+
export async function runTicket(projectDir, n) {
|
|
1476
|
+
const dir = path.resolve(projectDir);
|
|
1477
|
+
// Under the lock a cancel takes: a ticket it removed meanwhile is not written back.
|
|
1478
|
+
const t = await withMainLock(dir, async () => ticketSet(dir, n, { pid: process.pid }));
|
|
1479
|
+
if (!t) return null;
|
|
1480
|
+
let outcome;
|
|
1481
|
+
try {
|
|
1482
|
+
const r = await jobOf(dir, t.job, n);
|
|
1483
|
+
outcome =
|
|
1484
|
+
r.ok === false ? { ok: false, problem: r.problem, result: r } : { ok: true, result: r };
|
|
1485
|
+
} catch (e) {
|
|
1486
|
+
outcome = { ok: false, problem: e.message };
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
ticketClose(dir, n, outcome);
|
|
1490
|
+
|
|
1491
|
+
return outcome;
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
/**
|
|
1495
|
+
* `--queue` (docs/Handoff-Plan.md d6): the ticket, and its waiter started cut loose from this
|
|
1496
|
+
* process; the ticket's number at once. What the job cannot be is refused here, not in the waiter.
|
|
1497
|
+
* @param {{build: string | null, from?: string | null, fresh?: boolean, start?: {target: string}}}
|
|
1498
|
+
* job
|
|
1499
|
+
*/
|
|
1500
|
+
export async function queueJob(projectDir, job) {
|
|
1501
|
+
const dir = path.resolve(projectDir);
|
|
1502
|
+
const play = await projectPlay(dir);
|
|
1503
|
+
if (job.start) targetOf(play, job.start.target);
|
|
1504
|
+
if (job.build !== null) {
|
|
1505
|
+
needId(job.build);
|
|
1506
|
+
const problem = await statusProblem(dir, job.build, job.from ?? null);
|
|
1507
|
+
if (problem) throw new Error(problem);
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
const n = await ticketOpen(dir, job);
|
|
1511
|
+
// Not the run's: a waiter that inherited a run's owner would build past its own ticket.
|
|
1512
|
+
const { GCF_RUN_OWNER: _owner, ...env } = process.env;
|
|
1513
|
+
const child = spawn(process.execPath, [WAITER, dir, String(n)], {
|
|
1514
|
+
cwd: dir,
|
|
1515
|
+
detached: true,
|
|
1516
|
+
env,
|
|
1517
|
+
stdio: 'ignore',
|
|
1518
|
+
windowsHide: true,
|
|
1519
|
+
});
|
|
1520
|
+
child.unref();
|
|
1521
|
+
// Until the waiter has taken the ticket: this pid on it dies with this process.
|
|
1522
|
+
for (const until = Date.now() + WAITER_START_MS; Date.now() < until;) {
|
|
1523
|
+
if (readTicket(dir, n)?.pid === child.pid) return { ticket: n };
|
|
1524
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
ticketClose(dir, n, { ok: false, problem: 'the waiter did not start' });
|
|
1528
|
+
throw new Error(`the waiter of ticket ${n} did not start`);
|
|
1529
|
+
}
|