@roomi-fields/grillhouse 0.44.0 → 0.45.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.
Files changed (62) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CHANGELOG.md +24 -0
  3. package/cadre/templates/projet/.claude/settings.json +5 -0
  4. package/cadre/templates/projet/CLAUDE.md +15 -2
  5. package/cadre/templates/projet/docs/agents/issue-tracker.md +1 -1
  6. package/charte.md +3 -2
  7. package/dist/temporal/circuit/client.d.ts +7 -0
  8. package/dist/temporal/circuit/client.js +12 -2
  9. package/dist/temporal/circuit/client.js.map +1 -1
  10. package/dist/temporal/commande.js +5 -1
  11. package/dist/temporal/commande.js.map +1 -1
  12. package/dist/temporal/installation.d.ts +21 -1
  13. package/dist/temporal/installation.js +103 -12
  14. package/dist/temporal/installation.js.map +1 -1
  15. package/dist/temporal/tickets/activites.js +3 -1
  16. package/dist/temporal/tickets/activites.js.map +1 -1
  17. package/docs/ARCHITECTURE.md +204 -68
  18. package/docs/INTERFACE.md +23 -15
  19. package/package.json +1 -1
  20. package/scripts/activites.mjs +59 -43
  21. package/scripts/aiguillage.mjs +1 -0
  22. package/scripts/attente-de-la-machine.mjs +312 -0
  23. package/scripts/avancement.mjs +38 -31
  24. package/scripts/bilan.mjs +30 -5
  25. package/scripts/circuit.mjs +10 -7
  26. package/scripts/codes-de-sortie.mjs +1 -1
  27. package/scripts/coeurs-dedies.mjs +427 -0
  28. package/scripts/composants-affectes.mjs +3 -1
  29. package/scripts/consommation-des-passes.mjs +382 -0
  30. package/scripts/consommation.mjs +45 -0
  31. package/scripts/copies-finies.mjs +2 -2
  32. package/scripts/depart-du-ticket.mjs +3 -1
  33. package/scripts/disque-des-copies.mjs +1 -1
  34. package/scripts/echantillonneur-de-la-passe.mjs +103 -0
  35. package/scripts/enveloppe/enveloppe.mjs +1 -11
  36. package/scripts/enveloppe/travail-de-la-copie.mjs +3 -2
  37. package/scripts/grillhouse-maj.mjs +8 -4
  38. package/scripts/integration/file.mjs +10 -4
  39. package/scripts/jointure.d.ts +20 -9
  40. package/scripts/langages/cargo/commandes.mjs +8 -8
  41. package/scripts/langages/python/commandes.mjs +7 -6
  42. package/scripts/langages/typescript/tests-de-nuit.mjs +17 -4
  43. package/scripts/langages/typescript/tests.mjs +15 -6
  44. package/scripts/langages/typescript/verifier.mjs +23 -17
  45. package/scripts/langages/typescript/vitest-config/base.mjs +3 -3
  46. package/scripts/langages/typescript/vitest-config/machine.setup.mjs +7 -7
  47. package/scripts/mesure/base-de-passe.mjs +1 -1
  48. package/scripts/migrations/0.44.0-seuil-des-codes-de-tickets.mjs +47 -0
  49. package/scripts/migrations.mjs +2 -0
  50. package/scripts/passe-de-la-machine.mjs +456 -31
  51. package/scripts/places-de-la-machine.mjs +508 -61
  52. package/scripts/plafond-de-la-machine.d.mts +47 -2
  53. package/scripts/plafond-de-la-machine.mjs +197 -5
  54. package/scripts/preparation.mjs +1 -0
  55. package/scripts/regles-des-places.d.mts +40 -0
  56. package/scripts/regles-des-places.mjs +91 -0
  57. package/scripts/retirer-les-copies-finies.mjs +1 -1
  58. package/scripts/seances.mjs +4 -3
  59. package/scripts/services-du-noyau.mjs +3 -3
  60. package/scripts/sous-commandes.mjs +6 -7
  61. package/scripts/tableau.mjs +121 -112
  62. package/scripts/verrou-de-machine.mjs +0 -829
@@ -3,8 +3,8 @@
3
3
  // number, and as the job server of GNU make shares its slots between every make of one build.
4
4
  // Each session of an agent (testwriter, developer, reviewer…) takes a place at its start and gives
5
5
  // it back at its end; a circuit that waits for a question, or between two sessions, holds none.
6
- // Each pass takes one too (scripts/passe-de-la-machine.mjs: a targeted pass, and a heavy one
7
- // besides the exclusive lock of the machine, scripts/verrou-de-machine.mjs). Witness: 2026-11-02,
6
+ // A pass takes no place: it takes its part of the budget of the machine (below), which alone
7
+ // bounds it. A place is a session of an agent or a circuit; a part is a pass. Witness: 2026-11-02,
8
8
  // load 39 on 12 cores with five circuits at once (grillhouse-9kq).
9
9
  //
10
10
  // - The places live on the Temporal server, in the one workflow of the machine
@@ -13,13 +13,12 @@
13
13
  // the memory as `make -l`'s, the spacing of the starts) and frees the place of a process gone. A
14
14
  // synchronous caller reads and takes them by ./places-du-serveur.mjs, in a process of its own.
15
15
  // - The server is required on the workstation: one that does not answer stops the taking, named.
16
- // In the CI (CI set) no place is taken. A trial machine (GRILLHOUSE_VERROU_MACHINE, the base
17
- // configuration of the tests) takes its places on the server GRILLHOUSE_PLACES_D_ESSAI names,
18
- // and none without it, so that no test takes a place of the user's machine.
19
- // - A pass or a session that holds a place carries its mark to its children
20
- // (GRILLHOUSE_PLACE_TENUE): a pass they play takes no second place. In the envelope of an agent
21
- // (GRILLHOUSE_ENVELOPPE set, ./enveloppe/paquets-du-plan.mjs) no place is taken, as no lock is
22
- // (./verrou-de-machine.mjs): the place of the session, taken out of it by the circuit, covers its
16
+ // In the CI (CI set) no place is taken. A trial machine (GRILLHOUSE_PARTS_DE_LA_MACHINE, the
17
+ // base configuration of the tests) takes its places on the server GRILLHOUSE_PLACES_D_ESSAI
18
+ // names, and none without it, so that no test takes a place of the user's machine.
19
+ // - A session that holds a place carries its mark to its children (GRILLHOUSE_PLACE_TENUE). In
20
+ // the envelope of an agent (GRILLHOUSE_ENVELOPPE set, ./enveloppe/paquets-du-plan.mjs) no place
21
+ // and no part is taken: the place of the session, taken out of it by the circuit, covers its
23
22
  // commands, and the envelope passes on its mark; a number of process there means nothing on
24
23
  // the machine.
25
24
  // - The ceiling is a configuration of the poste (`plafondDe` of ./plafond-de-la-machine.mjs:
@@ -27,23 +26,75 @@
27
26
  // reads again at each judgement; two starts are spaced by ESPACEMENT_MS
28
27
  // (GRILLHOUSE_ESPACEMENT_MS names another, in ms), which the workflow reads when it starts.
29
28
  // - A wait says its reason (place, rank, load, memory or spacing) when it begins and every 30 s,
30
- // and is bounded as the wait for the lock of the machine, GRILLHOUSE_ATTENTE_MACHINE_MS: at the
31
- // bound, « à repasser » (75), the request withdrawn.
29
+ // and is bounded, GRILLHOUSE_ATTENTE_MACHINE_MS: at the bound, « à repasser » (75), the request
30
+ // withdrawn.
32
31
  // - A wait keeps the rank of its arrival, as the queue of a merge train keeps its order: the rank
33
32
  // is drawn when the circuit is launched (`prendreUnRang`) and kept from one session to the next.
34
33
  // - The number of workers of the tests of a session is `travailleursDe`: max(1, cores ÷ places),
35
34
  // passed to the tool of each language (GRILLHOUSE_TRAVAILLEURS).
35
+ //
36
+ // The parts of the passes, as Slurm reserves the cores and the memory of a job and waits while
37
+ // they lack, and as the job server of GNU make shares its slots between every make of a build:
38
+ // - The budget of the machine (its hardware less the reserve of the user,
39
+ // ./plafond-de-la-machine.mjs) is shared in parts, which every pass of the machine takes, whatever
40
+ // repository it plays in: the heavy pass of a circuit, `npm run verifier`, a targeted
41
+ // `npm test`, a slice of the CI on a machine of ours, the night. A pass starts when its part
42
+ // fits in what the parts held leave of the budget and the machine admits it (the load under the
43
+ // cores, 4 GB free: `admissionDePart` of ./regles-des-places.mjs); else it waits, says what it
44
+ // waits for when it begins and every 30 s, keeps its rank (the measure of the frame before a
45
+ // project's, then the order of arrival) and stops at « à repasser » (75) at the bound of the wait.
46
+ // - The state is shared by the machine, one file per pass in the directory of the user's state
47
+ // (`${XDG_STATE_HOME:-~/.local/state}/grillhouse/parts`; GRILLHOUSE_PARTS_DE_LA_MACHINE names
48
+ // another, which the base configuration of the tests gives to each test file): the part held
49
+ // or waited for, its project, ticket, kind, rank and, once it names it, its progress directory,
50
+ // which the board reads (`lireLesParts`). A pass whose process is gone frees its part.
51
+ // - A part holds precise threads (`fils` of its file): the passes choose them on the topology of the
52
+ // system (./coeurs-dedies.mjs `choisirLesCoeurs`), none held by another. A measure of time
53
+ // (`mesure`) takes dedicated cores, physical cores whole (its sibling threads with them), the
54
+ // number its project declares (`grillhouse.mesure`) or the default part of the budget, and the
55
+ // other passes go on on the cores left; it waits for its cores, keeping its rank, then for the
56
+ // rest of them (their load, out of it, under 10 %), within the bound of the wait.
57
+ // - A pass that holds a part carries its mark to its children (GRILLHOUSE_PART_TENUE): a pass they
58
+ // play takes no second part, and the workers of its tools are the cores of its part
59
+ // (GRILLHOUSE_TRAVAILLEURS).
60
+ import { randomUUID } from 'node:crypto';
36
61
  import { spawnSync } from 'node:child_process';
37
- import { availableParallelism } from 'node:os';
62
+ import { mkdirSync, readdirSync, readFileSync, rmdirSync, rmSync } from 'node:fs';
63
+ import { availableParallelism, homedir } from 'node:os';
38
64
  import path from 'node:path';
39
65
  import { departDe } from './mesure/base-de-passe.mjs';
66
+ import { ecrireAtomique } from './mesure/avancement.mjs';
40
67
  import { A_REPASSER } from './codes-de-sortie.mjs';
68
+ import {
69
+ AttenteDeLaMachine,
70
+ attendreDeLaPlace,
71
+ borneDe,
72
+ racineDeLaCopie,
73
+ racineDuDepot,
74
+ rangDeLaMesure,
75
+ } from './attente-de-la-machine.mjs';
76
+ import {
77
+ budgetEnFils,
78
+ chargeDesCoeursDuSysteme,
79
+ choisirLesCoeurs,
80
+ coeursDeLaMesureDe,
81
+ filsDeLaMesure,
82
+ lireLaTopologie,
83
+ mesureDeclareeDe,
84
+ SEUIL_DE_CHARGE,
85
+ } from './coeurs-dedies.mjs';
41
86
  import { paquetsDeLEnveloppe } from './enveloppe/paquets-du-plan.mjs';
42
87
  import { ecrireLaTrace, traceDesPlaces } from './journal-des-etats.mjs';
43
- import { mesureDeLaMachine } from './mesure-de-la-machine.mjs';
44
- import { plafondDe } from './plafond-de-la-machine.mjs';
45
- import { VARIABLE_JETON } from './regles-des-places.mjs';
46
- import { AttenteDeLaMachine, avecLaMachine, borneDe, racineDuDepot } from './verrou-de-machine.mjs';
88
+ import { mesureDeLaMachine, processusVivant } from './mesure-de-la-machine.mjs';
89
+ import {
90
+ budgetDe,
91
+ partDe,
92
+ partDeclareeDe,
93
+ plafondDe,
94
+ reserveDe,
95
+ } from './plafond-de-la-machine.mjs';
96
+ import { duree, refusDePart, VARIABLE_JETON } from './regles-des-places.mjs';
97
+ import { attendreUnPeu, lacherVerrou, prendreVerrou } from './verrou-de-fichier.mjs';
47
98
 
48
99
  // The spacing and the jeton of the night are rules (./regles-des-places.mjs), published here too.
49
100
  export {
@@ -53,10 +104,24 @@ export {
53
104
  VARIABLE_JETON,
54
105
  } from './regles-des-places.mjs';
55
106
  export const VARIABLE_TRAVAILLEURS = 'GRILLHOUSE_TRAVAILLEURS';
107
+
108
+ /**
109
+ * The command <commande> placed under `nice` then `ionice`, so a session or a pass yields the CPU
110
+ * and the disk to the rest of the machine (its command kept whole at the end).
111
+ * @param {string[]} commande
112
+ * @returns {string[]}
113
+ */
114
+ export function sousNiceEtIonice(commande) {
115
+ return ['nice', '-n', '10', 'ionice', '-c', '2', '-n', '7', ...commande];
116
+ }
56
117
  /** The mark of a place held, which the children of its holder inherit. */
57
118
  export const VARIABLE_TENUE = 'GRILLHOUSE_PLACE_TENUE';
58
119
  /** The measure of the machine a trial fixes, in JSON `{ coeurs, charge, memoireLibre, memoireTotale }`. */
59
120
  export const VARIABLE_MESURE_D_ESSAI = 'GRILLHOUSE_MESURE_D_ESSAI';
121
+ /** The directory of the state of the parts a trial names, in place of the user's. */
122
+ export const VARIABLE_PARTS = 'GRILLHOUSE_PARTS_DE_LA_MACHINE';
123
+ /** The mark of a part held, which the children of its holder inherit. */
124
+ export const VARIABLE_PART_TENUE = 'GRILLHOUSE_PART_TENUE';
60
125
  // The server of the places of a trial machine.
61
126
  const VARIABLE_ESSAI = 'GRILLHOUSE_PLACES_D_ESSAI';
62
127
 
@@ -108,7 +173,7 @@ export function surLeServeur(env) {
108
173
  if (env.CI || paquetsDeLEnveloppe(env)) {
109
174
  return null;
110
175
  }
111
- if (env.GRILLHOUSE_VERROU_MACHINE) {
176
+ if (env[VARIABLE_PARTS]) {
112
177
  return env[VARIABLE_ESSAI] ? { ...env, TEMPORAL_ADDRESS: env[VARIABLE_ESSAI] } : null;
113
178
  }
114
179
  return env;
@@ -287,20 +352,432 @@ export function avecUnePlace(seance, options) {
287
352
  }
288
353
 
289
354
  /**
290
- * Plays the pass <passe> on a place of the machine, as a command a person or an agent runs (`npm
291
- * test`): its exit code, or, a wait that stops (its bound, 75; a server that does not answer, 1),
292
- * its reason said and its code. <passe> receives the environment of its children, with the mark
293
- * of the place.
294
- * @param {(env: Record<string, string | undefined>) => number} passe
295
- * @param {{ env?: Record<string, string | undefined>, dire?: (l: string) => void }} [o]
355
+ * The directory of the state of the parts under <env>: GRILLHOUSE_PARTS_DE_LA_MACHINE, else
356
+ * `grillhouse/parts` under the state directory of the user (`XDG_STATE_HOME`, else
357
+ * `~/.local/state`), common to every project of the machine.
358
+ * @param {Record<string, string | undefined>} [env]
359
+ * @returns {string}
360
+ */
361
+ export function dossierDesParts(env = process.env) {
362
+ if (env[VARIABLE_PARTS]) {
363
+ return env[VARIABLE_PARTS];
364
+ }
365
+ const etat = env.XDG_STATE_HOME || path.join(env.HOME || homedir(), '.local', 'state');
366
+ return path.join(etat, 'grillhouse', 'parts');
367
+ }
368
+
369
+ /**
370
+ * The state of a pass in its file of the parts: `etat` « tient » (its part is held) or « attend »
371
+ * (`raison`: what it waits for), and what the board reads besides (`depot`, `copie`, `depuis`,
372
+ * `dossier` its progress directory, `depasse` what it consumes beyond its part).
373
+ * @typedef {{ id: string, pid: number, depart: any, projet: string, ticket: string | null,
374
+ * sorte: string, rang: number, coeurs: number, memoire: number, etat: string, raison?: string,
375
+ * depot?: string, copie?: string, depuis?: number, dossier?: string | null,
376
+ * depasse?: string | null }} PartDUnePasse
377
+ */
378
+
379
+ /**
380
+ * Writes the state of the pass <passe> in its file of <dossier>, whole or not at all. The file of a
381
+ * pass is one: a new state replaces it.
382
+ * @param {string} dossier
383
+ * @param {{ id: string } & Record<string, unknown>} passe
384
+ */
385
+ export function poserLaPart(dossier, passe) {
386
+ ecrireAtomique(path.join(dossier, `${passe.id}.json`), passe);
387
+ }
388
+
389
+ /**
390
+ * Gives back the part of the pass <id>: its file goes, and the directory when it is empty. A part
391
+ * that is not there changes nothing.
392
+ * @param {string} dossier
393
+ * @param {string} id
394
+ */
395
+ export function rendreLaPart(dossier, id) {
396
+ rmSync(path.join(dossier, `${id}.json`), { force: true });
397
+ try {
398
+ rmdirSync(dossier);
399
+ } catch {
400
+ // other parts stand there, or the directory is gone
401
+ }
402
+ }
403
+
404
+ /**
405
+ * The parts of <dossier> as the machine holds them: `prises` (the passes that hold their part)
406
+ * and `attentes` (those that wait, in the order of their rank). A pass whose process is gone
407
+ * (`vivant`, `processusVivant` by default: its pid and its start) frees its part, its file
408
+ * removed. A directory that is not there holds none.
409
+ * @param {string} dossier
410
+ * @param {{ vivant?: (p: PartDUnePasse) => boolean }} [o]
411
+ * @returns {{ prises: PartDUnePasse[], attentes: PartDUnePasse[] }}
412
+ */
413
+ export function lireLesParts(dossier, { vivant = processusVivant } = {}) {
414
+ let noms;
415
+ try {
416
+ noms = readdirSync(dossier).filter(n => n.endsWith('.json'));
417
+ } catch {
418
+ return { prises: [], attentes: [] };
419
+ }
420
+ const prises = [];
421
+ const attentes = [];
422
+ for (const nom of noms) {
423
+ let p;
424
+ try {
425
+ p = JSON.parse(readFileSync(path.join(dossier, nom), 'utf8'));
426
+ } catch {
427
+ continue;
428
+ }
429
+ if (!p || typeof p.id !== 'string') {
430
+ continue;
431
+ }
432
+ if (!vivant(p)) {
433
+ rmSync(path.join(dossier, nom), { force: true });
434
+ continue;
435
+ }
436
+ (p.etat === 'attend' ? attentes : prises).push(p);
437
+ }
438
+ return { prises, attentes: attentes.sort((a, b) => a.rang - b.rang) };
439
+ }
440
+
441
+ /**
442
+ * The machine a pass is judged on under <env>: the one a trial fixes (GRILLHOUSE_MESURE_D_ESSAI),
443
+ * else the real one.
444
+ * @param {Record<string, string | undefined>} env
445
+ */
446
+ const machineDe = env => mesureDEssai(env) ?? mesureDeLaMachine();
447
+
448
+ /**
449
+ * The budget of the machine under <env>: its hardware (the one a trial fixes, else the real one)
450
+ * less the reserve of the user.
451
+ * @param {Record<string, string | undefined>} [env]
452
+ * @returns {{ coeurs: number, memoire: number }}
453
+ * @throws {Error} naming the variable of the reserve when it is faulty or leaves nothing
454
+ */
455
+ export function budgetDeLaPasse(env = process.env) {
456
+ const m = machineDe(env);
457
+ return budgetDe({ coeurs: m.coeurs, memoireTotale: m.memoireTotale }, reserveDe(env, { coeurs: m.coeurs }));
458
+ }
459
+
460
+ /**
461
+ * The part of a pass of the project at <racine> under <env>: the one the key `grillhouse.part` of
462
+ * its main tree's `package.json` declares, else the default part of the budget.
463
+ * @param {{ racine: string, env?: Record<string, string | undefined> }} o
464
+ * @returns {{ coeurs: number, memoire: number }}
465
+ * @throws {AttenteDeLaMachine} naming the key (code 1) when its value is faulty, before any work
466
+ */
467
+ export function partDeLaPasse({ racine, env = process.env }) {
468
+ const { declaree, faute } = partDeclareeDe(packageDuDepot(racine));
469
+ if (faute) {
470
+ throw new AttenteDeLaMachine(`✗ ${faute}`, 1);
471
+ }
472
+ return partDe({ declaree, budget: budgetDeLaPasse(env) });
473
+ }
474
+
475
+ // The `package.json` of the main tree of the repository at <racine>; empty without one (a
476
+ // repository without a package.json declares neither part nor measure).
477
+ function packageDuDepot(racine) {
478
+ try {
479
+ return JSON.parse(readFileSync(path.join(racineDuDepot(racine), 'package.json'), 'utf8'));
480
+ } catch {
481
+ return {};
482
+ }
483
+ }
484
+
485
+ /**
486
+ * The cores a measure of time of the project at <racine> asks for, in threads: the key
487
+ * `grillhouse.mesure` of its main tree's `package.json`, else the default part of the budget.
488
+ * @param {{ racine: string, budget: { coeurs: number } }} o
489
+ * @returns {number}
490
+ * @throws {AttenteDeLaMachine} naming the key (code 1) when its value is faulty, before any work
491
+ */
492
+ function coeursDeLaMesure({ racine, budget }) {
493
+ const { declaree, faute } = mesureDeclareeDe(packageDuDepot(racine));
494
+ if (faute) {
495
+ throw new AttenteDeLaMachine(`✗ ${faute}`, 1);
496
+ }
497
+ return coeursDeLaMesureDe({ declaree, budget });
498
+ }
499
+
500
+ const RANGS = { 1: 'mesure du cadre', 2: "mesure d'un projet" };
501
+ // The rank of a pass among the parts: the kind of its measure first (the frame's before a
502
+ // project's), then its time of arrival, so that a smaller rank passes first.
503
+ const ECHELLE_DES_RANGS = 1e14;
504
+
505
+ // Plays <f> holding the short lock of the admission of <dossier>, so that two passes never judge
506
+ // the same free budget at once.
507
+ function sousLAdmission(dossier, f) {
508
+ mkdirSync(dossier, { recursive: true });
509
+ const verrou = path.join(dossier, '.admission');
510
+ prendreVerrou(verrou, { attendre: true });
511
+ try {
512
+ return f();
513
+ } finally {
514
+ lacherVerrou(verrou);
515
+ }
516
+ }
517
+
518
+ /**
519
+ * Takes the part of a pass of the project at <racine>, waiting for it, saying what it waits for
520
+ * (`dire`) when it begins and every <rappelMs>, and returns it. Room first on the disks the pass
521
+ * writes on (`attendreDeLaPlace`), then the part: it is taken once it fits in what the parts held
522
+ * leave of the budget and the load and the memory of the machine admit it, the waits of a smaller
523
+ * rank passing first. Its state is one file of the parts (`poserLaPart`), « attend » then
524
+ * « tient ». The night (VARIABLE_JETON) and a pass that asks for the <entiere> budget take the
525
+ * whole budget: they play alone. A <mesure> of time takes dedicated cores (`fils`, whole physical
526
+ * cores), waits for them and for their rest (<chargeDesCoeurs>: the load of the threads it gives,
527
+ * null without a reading; <topologie>, the topology of the system, for a trial). Where no part is taken (the envelope of an agent, a part already
528
+ * held: GRILLHOUSE_PART_TENUE) the part returned holds nothing.
529
+ * @param {{ env?: Record<string, string | undefined>, racine?: string, ticket?: string | null,
530
+ * sorte?: string, entiere?: boolean, borneMs?: number, rappelMs?: number,
531
+ * dire?: (l: string) => void, maintenant?: () => number, dormir?: (ms: number) => void,
532
+ * libre?: (d: string) => number, occupe?: (d: string) => string,
533
+ * occupeLaCopie?: (r: string) => string }} [o]
534
+ * @returns {{ id: string | null, part: { coeurs: number, memoire: number } | null,
535
+ * env: Record<string, string | undefined>, rendre: () => void, raison: string | null }}
536
+ * `env`, the environment of its holder's children, with the mark of the part and the workers of
537
+ * its tools; `raison`, why no part is held, null when one is
538
+ * @throws {AttenteDeLaMachine} at the bound of the wait (code 75), or on a faulty key or reserve
539
+ * (code 1), its reason named
540
+ */
541
+ export function prendreUnePart({
542
+ env = process.env,
543
+ racine = process.cwd(),
544
+ ticket,
545
+ sorte,
546
+ entiere = false,
547
+ mesure = false,
548
+ topologie,
549
+ chargeDesCoeurs = chargeDesCoeursDuSysteme,
550
+ borneMs = borneDe(env),
551
+ rappelMs = RAPPEL_MS,
552
+ dire = l => process.stdout.write(`${l}\n`),
553
+ maintenant = Date.now,
554
+ dormir = attendreUnPeu,
555
+ ...disques
556
+ } = {}) {
557
+ const sans = raison => ({ id: null, part: null, fils: null, env, rendre: () => {}, raison });
558
+ if (paquetsDeLEnveloppe(env)) {
559
+ return sans("dans l'enveloppe d'un agent");
560
+ }
561
+ if (env[VARIABLE_PART_TENUE]) {
562
+ return sans('sous la part de son parent');
563
+ }
564
+ // The caller names who the pass is, else its environment does (GRILLHOUSE_VERIFICATION_TICKET,
565
+ // GRILLHOUSE_VERIFICATION_SORTE, empty as absent), else « Vérification » and no ticket.
566
+ ticket = ticket ?? (env.GRILLHOUSE_VERIFICATION_TICKET || null);
567
+ sorte = sorte ?? (env.GRILLHOUSE_VERIFICATION_SORTE || 'Vérification');
568
+ const dossier = dossierDesParts(env);
569
+ let budget;
570
+ let part;
571
+ // The precise threads of the budget and the topology they are chosen on.
572
+ let budgetFils;
573
+ let topo;
574
+ try {
575
+ budget = budgetDeLaPasse(env);
576
+ topo = topologie ?? lireLaTopologie({ nombreDeFils: machineDe(env).coeurs });
577
+ budgetFils = budgetEnFils({ topologie: topo, budget: budget.coeurs });
578
+ if (mesure) {
579
+ const demande = coeursDeLaMesure({ racine, budget });
580
+ let fils = filsDeLaMesure({ budget: budgetFils, topologie: topo, demande });
581
+ if (fils === null || !topo.lisible) {
582
+ // No whole core in the budget, or a topology that does not read: each thread is a core.
583
+ topo = { lisible: false, coeurs: topo.coeurs.flatMap(c => c.fils.map(f => ({ fils: [f], noeud: c.noeud }))) };
584
+ fils = filsDeLaMesure({ budget: budgetFils, topologie: topo, demande });
585
+ dire('la topologie des cœurs ne se lit pas : chaque fil compte pour un cœur.');
586
+ }
587
+ part = { coeurs: fils, memoire: Math.floor((budget.memoire * fils) / budget.coeurs) };
588
+ } else {
589
+ part = entiere || env[VARIABLE_JETON] ? budget : partDeLaPasse({ racine, env });
590
+ }
591
+ } catch (e) {
592
+ throw e instanceof AttenteDeLaMachine ? e : new AttenteDeLaMachine(`✗ ${e.message}`, 1);
593
+ }
594
+ const depot = racineDuDepot(racine);
595
+ const rangDeLaPasse = rangDeLaMesure(racine);
596
+ const moi = {
597
+ id: randomUUID(),
598
+ pid: process.pid,
599
+ depart: departDe(process.pid),
600
+ projet: path.basename(depot),
601
+ depot,
602
+ copie: racineDeLaCopie(racine),
603
+ ticket,
604
+ sorte,
605
+ rang: rangDeLaPasse * ECHELLE_DES_RANGS + maintenant(),
606
+ coeurs: part.coeurs,
607
+ memoire: part.memoire,
608
+ dediee: mesure,
609
+ depuis: maintenant(),
610
+ };
611
+ let tenus = null;
612
+ try {
613
+ attendreDeLaPlace({
614
+ env,
615
+ dire,
616
+ borneMs,
617
+ rappelMs,
618
+ racine,
619
+ maintenant,
620
+ dormir,
621
+ ...disques,
622
+ inscrire: raison => {
623
+ poserLaPart(dossier, { ...moi, etat: 'attend', raison });
624
+ return () => {};
625
+ },
626
+ });
627
+ const debut = maintenant();
628
+ let dit = null;
629
+ for (;;) {
630
+ const refus = sousLAdmission(dossier, () => {
631
+ const { prises, attentes } = lireLesParts(dossier);
632
+ let r = refusDePart(
633
+ { prises, attentes },
634
+ { id: moi.id, rang: moi.rang, part, budget, mesurer: () => machineDe(env), dediee: mesure }
635
+ );
636
+ let fils = null;
637
+ if (r === null) {
638
+ // The count fits: the precise threads, none held by another pass (a measure's whole
639
+ // cores, a free core cut by an ordinary pass's threads does not give one).
640
+ fils = choisirLesCoeurs({
641
+ budget: budgetFils,
642
+ topologie: topo,
643
+ tenus: prises.filter(p => p.id !== moi.id).flatMap(p => p.fils ?? []),
644
+ demande: part.coeurs,
645
+ dedies: mesure,
646
+ });
647
+ if (fils === null) {
648
+ r = mesure
649
+ ? `cœurs dédiés : ${part.coeurs} demandés, aucun cœur entier libre`
650
+ : `cœurs : ${part.coeurs} demandés, pas assez de fils libres`;
651
+ }
652
+ }
653
+ poserLaPart(
654
+ dossier,
655
+ r === null ? { ...moi, fils, etat: 'tient' } : { ...moi, etat: 'attend', raison: r }
656
+ );
657
+ tenus = r === null ? fils : null;
658
+ return r;
659
+ });
660
+ if (refus === null) {
661
+ break;
662
+ }
663
+ const t = maintenant();
664
+ if (t - debut >= borneMs) {
665
+ throw new AttenteDeLaMachine(
666
+ `⏱ attente de la part bornée à ${duree(borneMs)} (GRILLHOUSE_ATTENTE_MACHINE_MS), ${RANGS[rangDeLaPasse]} : ${refus} ; à repasser (code ${A_REPASSER}).`,
667
+ A_REPASSER
668
+ );
669
+ }
670
+ if (dit === null) {
671
+ dire(
672
+ `⏳ ${RANGS[rangDeLaPasse]}, la passe attend sa part (${part.coeurs} cœurs) : ${refus} ; ${duree(borneMs)} au plus.`
673
+ );
674
+ } else if (t - dit >= rappelMs) {
675
+ dire(`⏳ la passe attend sa part depuis ${duree(t - debut)} : ${refus}.`);
676
+ }
677
+ if (dit === null || t - dit >= rappelMs) {
678
+ dit = t;
679
+ }
680
+ dormir(Math.max(1, Math.min(200, borneMs - (maintenant() - debut))));
681
+ }
682
+ // The rest of the dedicated cores: a measure goes once their load, out of it, is under the
683
+ // threshold, within the bound of the wait; without a reading it does not judge that load.
684
+ if (mesure) {
685
+ let ditCharge = null;
686
+ for (;;) {
687
+ const charge = chargeDesCoeurs(tenus);
688
+ if (charge === null) {
689
+ dire('la charge des cœurs dédiés ne se lit pas : la mesure ne la juge pas.');
690
+ break;
691
+ }
692
+ if (charge < SEUIL_DE_CHARGE) {
693
+ break;
694
+ }
695
+ const t = maintenant();
696
+ const pct = `${Math.round(charge * 100)} %`;
697
+ if (t - debut >= borneMs) {
698
+ throw new AttenteDeLaMachine(
699
+ `⏱ attente du repos des cœurs dédiés bornée à ${duree(borneMs)} (GRILLHOUSE_ATTENTE_MACHINE_MS), ${RANGS[rangDeLaPasse]} : charge de ${pct} des cœurs dédiés hors la mesure, ${Math.round(SEUIL_DE_CHARGE * 100)} % au plus ; à repasser (code ${A_REPASSER}).`,
700
+ A_REPASSER
701
+ );
702
+ }
703
+ if (ditCharge === null || t - ditCharge >= rappelMs) {
704
+ dire(`⏳ les cœurs dédiés ne sont pas au repos : charge de ${pct} hors la mesure, ${Math.round(SEUIL_DE_CHARGE * 100)} % au plus.`);
705
+ ditCharge = t;
706
+ }
707
+ dormir(Math.max(1, Math.min(200, borneMs - (maintenant() - debut))));
708
+ }
709
+ }
710
+ if (dit !== null) {
711
+ dire(`✓ part prise après ${duree(maintenant() - debut)}.`);
712
+ }
713
+ } catch (e) {
714
+ rendreLaPart(dossier, moi.id);
715
+ throw e;
716
+ }
717
+ const {
718
+ GRILLHOUSE_VERIFICATION_SORTE: _sorte,
719
+ GRILLHOUSE_VERIFICATION_TICKET: _ticket,
720
+ ...enfant
721
+ } = env;
722
+ return {
723
+ id: moi.id,
724
+ part,
725
+ fils: tenus,
726
+ dediee: mesure,
727
+ budgetFils,
728
+ topologie: topo,
729
+ env: {
730
+ ...enfant,
731
+ [VARIABLE_PART_TENUE]: moi.id,
732
+ [VARIABLE_TRAVAILLEURS]: String(part.coeurs),
733
+ },
734
+ rendre: () => rendreLaPart(dossier, moi.id),
735
+ raison: null,
736
+ };
737
+ }
738
+
739
+ /**
740
+ * Names the progress directory <dossier> of the pass that holds the part its environment
741
+ * <env> marks (GRILLHOUSE_PART_TENUE), in its file of the parts, where the board reads it; a
742
+ * function that takes it out. Nothing without a part held (the envelope of an agent).
743
+ * @param {{ env?: Record<string, string | undefined>, dossier: string }} o
744
+ * @returns {() => void}
745
+ */
746
+ export function nommerLAvancement({ env = process.env, dossier }) {
747
+ const id = env[VARIABLE_PART_TENUE];
748
+ if (!id) {
749
+ return () => {};
750
+ }
751
+ const f = path.join(dossierDesParts(env), `${id}.json`);
752
+ const ecrire = d => {
753
+ try {
754
+ const p = JSON.parse(readFileSync(f, 'utf8'));
755
+ ecrireAtomique(f, { ...p, dossier: d });
756
+ return true;
757
+ } catch {
758
+ return false;
759
+ }
760
+ };
761
+ return ecrire(dossier) ? () => void ecrire(null) : () => {};
762
+ }
763
+
764
+ /**
765
+ * Plays <passe> holding a part of the machine, given back at its end, whether it returns or
766
+ * throws: its exit code, or, a wait that stops (its bound, 75; a faulty key, 1), its reason said
767
+ * and its code. <passe> receives the environment of its children and the part (null where none is
768
+ * held); the pass is a command a person runs (`npm test`) and the process itself.
769
+ * @param {(env: Record<string, string | undefined>, part: { coeurs: number, memoire: number } | null) => number} passe
770
+ * @param {{ env?: Record<string, string | undefined>, racine?: string,
771
+ * dire?: (l: string) => void }} [o]
296
772
  * @returns {number}
297
773
  */
298
- export function surUnePlace(
774
+ export function surUnePart(
299
775
  passe,
300
- { env = process.env, dire = l => process.stderr.write(`${l}\n`) } = {}
776
+ { env = process.env, racine = process.cwd(), dire = l => process.stderr.write(`${l}\n`) } = {}
301
777
  ) {
778
+ let prise;
302
779
  try {
303
- return avecUnePlace(place => passe(place.env), { env, dire });
780
+ prise = prendreUnePart({ env, racine, dire, sorte: 'Tests' });
304
781
  } catch (e) {
305
782
  if (!(e instanceof AttenteDeLaMachine)) {
306
783
  throw e;
@@ -308,41 +785,11 @@ export function surUnePlace(
308
785
  dire(e.message);
309
786
  return e.code;
310
787
  }
311
- }
312
-
313
- /**
314
- * Plays the heavy measure <mesure> on a place of the machine, then under its exclusive lock
315
- * (`avecLaMachine` of ./verrou-de-machine.mjs), and returns what it returns: the place first, so
316
- * that a pass that waits for its place holds no lock. <mesure> receives the environment of its
317
- * children, which carries the marks of both.
318
- * @param {(env: Record<string, string | undefined>) => T} mesure
319
- * @param {Parameters<typeof avecLaMachine>[1] & { rang?: number }} [options] those of the lock,
320
- * whose `env`, `ticket`, `dire`, `borneMs` and `rappelMs` the place reads too, and `rang`, the
321
- * rank of the circuit of the pass
322
- * @returns {T}
323
- * @template T
324
- */
325
- export function avecLaMachineEtUnePlace(mesure, options = {}) {
326
- const { env = process.env, ticket, rang, dire, borneMs, rappelMs, ...verrou } = options;
327
- return avecUnePlace(
328
- place =>
329
- avecLaMachine(mesure, {
330
- ...verrou,
331
- env: place.env,
332
- ...(ticket === undefined ? {} : { ticket }),
333
- ...(dire ? { dire } : {}),
334
- ...(borneMs === undefined ? {} : { borneMs }),
335
- ...(rappelMs === undefined ? {} : { rappelMs }),
336
- }),
337
- {
338
- env,
339
- ticket: ticket ?? null,
340
- ...(rang === undefined ? {} : { rang }),
341
- ...(dire ? { dire } : {}),
342
- ...(borneMs === undefined ? {} : { borneMs }),
343
- ...(rappelMs === undefined ? {} : { rappelMs }),
344
- }
345
- );
788
+ try {
789
+ return passe(prise.env, prise.part);
790
+ } finally {
791
+ prise.rendre();
792
+ }
346
793
  }
347
794
 
348
795
  /**