@turri-dev/cli 0.2.4-next.efe96fe → 0.2.4-next.f26a225

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 (73) hide show
  1. package/README.md +2 -2
  2. package/dist/chat-skills.d.ts +1 -1
  3. package/dist/chat-skills.js +1 -1
  4. package/dist/chats.d.ts +42 -8
  5. package/dist/chats.js +122 -38
  6. package/dist/cli.js +1 -1
  7. package/dist/config-edit.js +4 -4
  8. package/dist/config.d.ts +48 -11
  9. package/dist/config.js +102 -13
  10. package/dist/extension.d.ts +9 -0
  11. package/dist/extension.js +184 -109
  12. package/dist/genome.js +1 -1
  13. package/dist/host-session.d.ts +6 -5
  14. package/dist/host-session.js +2 -2
  15. package/dist/host.d.ts +2 -0
  16. package/dist/identity.js +2 -2
  17. package/dist/init-config.d.ts +1 -1
  18. package/dist/init-config.js +7 -7
  19. package/dist/launch-agent.d.ts +51 -0
  20. package/dist/launch-agent.js +267 -0
  21. package/dist/{release-cli.js → launch-cli.js} +41 -24
  22. package/dist/launch-session.d.ts +68 -0
  23. package/dist/launch-session.js +217 -0
  24. package/dist/launch-tools.d.ts +28 -0
  25. package/dist/launch-tools.js +69 -0
  26. package/dist/launch-track.d.ts +125 -0
  27. package/dist/launch-track.js +320 -0
  28. package/dist/loop-cli.js +4 -4
  29. package/dist/loop-tools.d.ts +8 -3
  30. package/dist/loop-tools.js +37 -32
  31. package/dist/notice.d.ts +7 -6
  32. package/dist/plain-files.d.ts +2 -2
  33. package/dist/plain-files.js +3 -3
  34. package/dist/regenerate/command-check.d.ts +7 -1
  35. package/dist/regenerate/command-check.js +43 -1
  36. package/dist/regenerate/delivery.d.ts +1 -1
  37. package/dist/regenerate/delivery.js +3 -3
  38. package/dist/regenerate/launch.d.ts +154 -0
  39. package/dist/regenerate/{release.js → launch.js} +195 -51
  40. package/dist/regenerate/ledger.d.ts +32 -1
  41. package/dist/regenerate/lineage.d.ts +2 -0
  42. package/dist/regenerate/lineage.js +8 -0
  43. package/dist/regenerate/loop.d.ts +6 -5
  44. package/dist/regenerate/loop.js +16 -17
  45. package/dist/regenerate/paths.d.ts +2 -2
  46. package/dist/regenerate/paths.js +2 -2
  47. package/dist/self-invocation.js +7 -7
  48. package/dist/sentinel-check.d.ts +3 -3
  49. package/dist/sentinel-check.js +3 -3
  50. package/dist/sentinel.d.ts +18 -12
  51. package/dist/sentinel.js +68 -42
  52. package/docs/README.md +2 -2
  53. package/docs/chats.md +2 -2
  54. package/docs/config.md +9 -9
  55. package/docs/development.md +1 -1
  56. package/docs/headless.md +1 -1
  57. package/docs/hosting.md +14 -13
  58. package/docs/launch.md +67 -0
  59. package/docs/parser.md +1 -1
  60. package/docs/regenerating.md +3 -3
  61. package/docs/sandbox.md +3 -3
  62. package/docs/sentinel.md +24 -12
  63. package/docs/skills-and-gates.md +3 -3
  64. package/docs/using.md +7 -7
  65. package/genome/rules/turri-regeneration.md +1 -1
  66. package/package.json +1 -1
  67. package/dist/regenerate/release.d.ts +0 -121
  68. package/dist/release-agent.d.ts +0 -43
  69. package/dist/release-agent.js +0 -130
  70. package/dist/release-tools.d.ts +0 -44
  71. package/dist/release-tools.js +0 -170
  72. package/docs/deploy.md +0 -48
  73. /package/dist/{release-cli.d.ts → launch-cli.d.ts} +0 -0
@@ -1,12 +1,12 @@
1
1
  import { relative } from "node:path";
2
2
  /**
3
- * How a command-line run that landed code ends: the command that releases it to try.
3
+ * How a command-line run that landed code ends: the command that launches it to try.
4
4
  */
5
5
  function shortPath(buildDir) {
6
6
  const fromHere = relative(process.cwd(), buildDir);
7
7
  return fromHere === "" || fromHere.startsWith("..") ? buildDir : fromHere;
8
8
  }
9
- /** A command-line run has no one to ask: the command that releases what it built. */
9
+ /** A command-line run has no one to ask: the command that launches what it built. */
10
10
  export function tryItCommand(plainFile) {
11
- return `To try it: turri release ${shortPath(plainFile)}`;
11
+ return `To try it: turri launch ${shortPath(plainFile)}`;
12
12
  }
@@ -0,0 +1,154 @@
1
+ import { type ChainEntry } from "./requires.ts";
2
+ /**
3
+ * Launch: a codebase's code, as one of its commits has it, put out in `dist/<module>` for the person and the sentinel
4
+ * to try - the commit a pulse landed, or the codebase's last.
5
+ *
6
+ * A pulse works in `plain_modules/<module>/code` while a launch runs, so what is there is half-written. A commit is not:
7
+ * every unit of work starts from one and ends with one, and both are runnable. So the commit is exported from the
8
+ * module's history - git's own list of what it tracks, and nothing else - without a list of what each stack leaves lying
9
+ * around (`node_modules`, `.venv`, `target`). The working tree is never read, reset or touched: the next pulse may be
10
+ * writing in it.
11
+ *
12
+ * What starts it runs here, on the person's machine, unfenced: a server the person opens in their browser, with their
13
+ * toolchains. The launch agent installs and runs what it needs itself, as any agent with a shell would - nothing is put to
14
+ * the person - and everything started is the harness's to stop: on the next launch, on /halt, and when turri exits.
15
+ */
16
+ /** A codebase's code at a commit, exported - what a launch takes on from. */
17
+ export interface ExportedCode {
18
+ /** The codebase: the module whose `plain_modules/<module>` holds the code. */
19
+ moduleName: string;
20
+ plainFile: string;
21
+ /** `dist/<module>`, in the project: the commit's code. */
22
+ dir: string;
23
+ /** Where the module's code is built: `plain_modules/<module>/code`. */
24
+ buildDir: string;
25
+ /** The commit, whole. */
26
+ commit: string;
27
+ /** The commit, short - as the person and the agents are told it. */
28
+ short: string;
29
+ /** The commit's subject: what the unit of work that landed it did. */
30
+ subject: string;
31
+ /** When, as git says it: "3 minutes ago". */
32
+ when: string;
33
+ /** Work under way in the module - changes nobody committed yet - left out. */
34
+ unfinished: boolean;
35
+ /** The module the spec's code lives in had nothing committed yet: the nearest one below it that had, instead. */
36
+ instead?: string;
37
+ }
38
+ /** The tree of a commit's code: two commits with the same one hold the same code - one landed only tests, say. */
39
+ export declare function codeTreeOf(moduleDir: string, commit: string): string | undefined;
40
+ /** Whether `commit` holds `ancestor`: the same commit, or one built on it. */
41
+ export declare function holdsCommit(moduleDir: string, ancestor: string, commit: string): boolean;
42
+ /**
43
+ * Which codebase to launch for a spec, and at which commit: the tip of its codebase - code accumulates up a requires
44
+ * chain, so the top module holds everything below it - at `commit`, or its last. Before the tip has anything committed,
45
+ * the nearest module below it that has.
46
+ */
47
+ export declare function launchTarget(plainFile: string, options?: {
48
+ inModule?: string;
49
+ commit?: string;
50
+ }): {
51
+ entry: ChainEntry;
52
+ commit: string;
53
+ instead?: string;
54
+ };
55
+ /**
56
+ * Export a codebase's code at a commit - its last, unless `commit` names one - into `dist/<module>`, or `into`
57
+ * (`turri regenerate --build-dir`), replacing whatever was there, the harness's own files (what the code is built from,
58
+ * the agents' notes) left out.
59
+ */
60
+ export declare function exportModule(plainFile: string, options?: {
61
+ inModule?: string;
62
+ into?: string;
63
+ commit?: string;
64
+ }): ExportedCode;
65
+ /**
66
+ * The host codebase a launch with guidance works in: the git repository the project is in - an embedded project's host -
67
+ * or else `fallback`, where turri runs.
68
+ */
69
+ export declare function hostDirOf(plainFile: string, fallback: string): string;
70
+ /** Where a path is, said from the current directory when it is below it. */
71
+ export declare function shortPath(path: string): string;
72
+ /** What was exported, in a line or three. */
73
+ export declare function describeExport(code: ExportedCode): string;
74
+ export interface LaunchedProcess {
75
+ name: string;
76
+ moduleName: string;
77
+ command: string;
78
+ /** Where it runs. */
79
+ dir: string;
80
+ url?: string;
81
+ /** Its output, both streams. */
82
+ log: string;
83
+ pid: number;
84
+ startedAt: number;
85
+ /** Set once it has ended. */
86
+ exited?: {
87
+ code: number | null;
88
+ signal: string | null;
89
+ };
90
+ }
91
+ /**
92
+ * What a launch started, written down as it starts - `.turri/launch/running/<pid>.json` - and taken out as it ends: a turri
93
+ * that dies without stopping what it started leaves it findable, for the next session to stop (`stopLeftBehind`).
94
+ */
95
+ export interface LaunchRecord {
96
+ pid: number;
97
+ /** Its process group: the pid, as it was started as the leader of a group of its own. */
98
+ pgid: number;
99
+ name: string;
100
+ moduleName: string;
101
+ command: string;
102
+ dir: string;
103
+ url?: string;
104
+ startedAt: number;
105
+ /** The turri process that started it. */
106
+ turri: number;
107
+ }
108
+ /**
109
+ * Stop what launches of an earlier turri left running in the project - the codebase's, or every one's - as stopLaunched
110
+ * stops this session's: its process group, politely, then not. Only what turri's own records name, whose turri has ended
111
+ * (another turri still running keeps its own), and only when the process is still the one recorded: a process turri did
112
+ * not start is never stopped. Records of what has ended are taken out. Returns what was stopped.
113
+ */
114
+ export declare function stopLeftBehind(projectDir: string, codebase?: string): Promise<LaunchRecord[]>;
115
+ /** What was stopped of an earlier session's, in a line - for a launch's summary. */
116
+ export declare function describeLeftBehind(stopped: LaunchRecord[]): string;
117
+ /** Everything a launch started, running or ended, oldest first. */
118
+ export declare function listLaunched(): LaunchedProcess[];
119
+ /** The ones still running. */
120
+ export declare function runningLaunched(moduleName?: string): LaunchedProcess[];
121
+ /** Where what a project's launches started write their output: its scratch, not the code in dist. */
122
+ export declare function launchLogDir(projectDir: string): string;
123
+ /** The end of a log, for when something did not come up. */
124
+ export declare function logTail(log: string, lines?: number, chars?: number): string;
125
+ /** Stop one launched process, or every one of a module, or every one: politely, then not. Returns the names stopped. */
126
+ export declare function stopLaunched(which?: {
127
+ name?: string;
128
+ moduleName?: string;
129
+ }): Promise<string[]>;
130
+ /** Stop everything at once, without waiting: for when turri itself is going. */
131
+ export declare function stopAllLaunchedNow(): void;
132
+ /** What holds a port, as lsof says it - or nothing when it cannot say. */
133
+ export declare function holderOf(port: number): string;
134
+ /** How long a server is given to answer at its URL, and to stay up when it has none. */
135
+ export declare const READY_TIMEOUT_MS: number;
136
+ export interface StartResult {
137
+ ok: boolean;
138
+ /** What happened, for the agent and the person. */
139
+ text: string;
140
+ process?: LaunchedProcess;
141
+ }
142
+ /**
143
+ * Start what keeps running, in `dir` - the folder `root` it is named from, or one inside it. A folder that is not there
144
+ * starts nothing: said, with what `root` holds, so the agent can name the right one.
145
+ */
146
+ export declare function startLaunched(input: {
147
+ name: string;
148
+ command: string;
149
+ dir: string;
150
+ moduleName: string;
151
+ logDir: string;
152
+ url?: string;
153
+ root?: string;
154
+ }): Promise<StartResult>;
@@ -1,5 +1,5 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
- import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
2
+ import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
3
3
  import { connect } from "node:net";
4
4
  import { dirname, join, relative } from "node:path";
5
5
  import { HISTORY_DIR } from "./lineage.js";
@@ -10,41 +10,58 @@ import { RECORD_FILE } from "./paths.js";
10
10
  function git(moduleDir, args) {
11
11
  return spawnSync("git", ["--git-dir", join(moduleDir, HISTORY_DIR), "--work-tree", moduleDir, ...args], { cwd: moduleDir, encoding: "utf8" });
12
12
  }
13
- /** The module's last commit, when it has one with code in it. */
14
- function committedCode(moduleDir) {
13
+ /** A commit of the module's history, whole - `HEAD` its last - when it has one with code in it. */
14
+ function committedCode(moduleDir, commit = "HEAD") {
15
15
  if (!existsSync(join(moduleDir, HISTORY_DIR)))
16
16
  return undefined;
17
- const commit = git(moduleDir, ["rev-parse", "--verify", "--quiet", "HEAD"]);
18
- if (commit.status !== 0)
17
+ const found = git(moduleDir, ["rev-parse", "--verify", "--quiet", `${commit}^{commit}`]);
18
+ if (found.status !== 0)
19
19
  return undefined;
20
+ const whole = found.stdout.trim();
20
21
  // A commit with no code folder in it has nothing to run.
21
- if (git(moduleDir, ["rev-parse", "--verify", "--quiet", "HEAD:code"]).status !== 0)
22
+ if (git(moduleDir, ["rev-parse", "--verify", "--quiet", `${whole}:code`]).status !== 0)
22
23
  return undefined;
23
- return commit.stdout.trim();
24
+ return whole;
25
+ }
26
+ /** The tree of a commit's code: two commits with the same one hold the same code - one landed only tests, say. */
27
+ export function codeTreeOf(moduleDir, commit) {
28
+ const tree = git(moduleDir, ["rev-parse", "--verify", "--quiet", `${commit}:code`]);
29
+ return tree.status === 0 ? tree.stdout.trim() : undefined;
30
+ }
31
+ /** Whether `commit` holds `ancestor`: the same commit, or one built on it. */
32
+ export function holdsCommit(moduleDir, ancestor, commit) {
33
+ return git(moduleDir, ["merge-base", "--is-ancestor", ancestor, commit]).status === 0;
24
34
  }
25
35
  /**
26
- * Which module to deploy for a spec: the tip of its codebase - code accumulates up a requires chain, so
27
- * the top module holds everything below it. Before the tip has anything committed, the nearest module
28
- * below it that has.
36
+ * Which codebase to launch for a spec, and at which commit: the tip of its codebase - code accumulates up a requires
37
+ * chain, so the top module holds everything below it - at `commit`, or its last. Before the tip has anything committed,
38
+ * the nearest module below it that has.
29
39
  */
30
- export function deployTarget(plainFile, inModule) {
31
- const { tip, chain } = findCodebaseTip(plainFile, { allowNoFunctionalities: true, ...(inModule !== undefined ? { inModule } : {}) });
40
+ export function launchTarget(plainFile, options = {}) {
41
+ const { tip, chain } = findCodebaseTip(plainFile, { allowNoFunctionalities: true, ...(options.inModule !== undefined ? { inModule: options.inModule } : {}) });
42
+ if (options.commit !== undefined) {
43
+ const commit = committedCode(dirname(tip.buildDir), options.commit);
44
+ if (commit === undefined)
45
+ throw new Error(`${tip.moduleName} has no commit ${options.commit} with code in it: nothing to launch.`);
46
+ return { entry: tip, commit };
47
+ }
32
48
  const candidates = [tip, ...[...chain].reverse().filter((entry) => entry.moduleName !== tip.moduleName)];
33
49
  for (const entry of candidates) {
34
50
  const commit = committedCode(dirname(entry.buildDir));
35
51
  if (commit !== undefined)
36
52
  return { entry, commit, ...(entry.moduleName !== tip.moduleName ? { instead: tip.moduleName } : {}) };
37
53
  }
38
- throw new Error(`Nothing is built yet for ${tip.moduleName}${candidates.length > 1 ? " or the modules it requires" : ""}: there is no code to deploy. Render it first.`);
54
+ throw new Error(`Nothing is built yet for ${tip.moduleName}${candidates.length > 1 ? " or the modules it requires" : ""}: there is no code to launch. Regenerate it first.`);
39
55
  }
40
56
  /**
41
- * Export the module's code at its last commit into `dist/<module>` - or `into`, `turri regenerate --build-dir` -
42
- * replacing whatever was there, the harness's own files (what the code is built from, the agents' notes) left out.
57
+ * Export a codebase's code at a commit - its last, unless `commit` names one - into `dist/<module>`, or `into`
58
+ * (`turri regenerate --build-dir`), replacing whatever was there, the harness's own files (what the code is built from,
59
+ * the agents' notes) left out.
43
60
  */
44
- export function exportModule(plainFile, inModule, into) {
45
- const { entry, commit, instead } = deployTarget(plainFile, inModule);
61
+ export function exportModule(plainFile, options = {}) {
62
+ const { entry, commit, instead } = launchTarget(plainFile, options);
46
63
  const moduleDir = dirname(entry.buildDir);
47
- const dir = into ?? distDirFor(projectDirOf(entry.plainFile), entry.moduleName);
64
+ const dir = options.into ?? distDirFor(projectDirOf(entry.plainFile), entry.moduleName);
48
65
  // Emptied, not removed: the folder may be a mount point - a container's volume - that cannot be removed.
49
66
  mkdirSync(dir, { recursive: true });
50
67
  for (const entry of readdirSync(dir))
@@ -59,10 +76,10 @@ export function exportModule(plainFile, inModule, into) {
59
76
  return {
60
77
  moduleName: entry.moduleName,
61
78
  plainFile: entry.plainFile,
62
- way: "dist",
63
79
  dir,
64
80
  buildDir: entry.buildDir,
65
- commit: short ?? commit.slice(0, 8),
81
+ commit,
82
+ short: short ?? commit.slice(0, 8),
66
83
  subject: subject ?? "",
67
84
  when: when ?? "",
68
85
  unfinished,
@@ -70,7 +87,7 @@ export function exportModule(plainFile, inModule, into) {
70
87
  };
71
88
  }
72
89
  /**
73
- * The host codebase a guided release works in: the git repository the project is in - an embedded project's host -
90
+ * The host codebase a launch with guidance works in: the git repository the project is in - an embedded project's host -
74
91
  * or else `fallback`, where turri runs.
75
92
  */
76
93
  export function hostDirOf(plainFile, fallback) {
@@ -82,34 +99,149 @@ export function shortPath(path) {
82
99
  const fromHere = relative(process.cwd(), path);
83
100
  return fromHere === "" || fromHere.startsWith("..") ? path : fromHere;
84
101
  }
85
- /** What was deployed, in a line or three. */
86
- export function describeDeployment(deployment) {
102
+ /** What was exported, in a line or three. */
103
+ export function describeExport(code) {
87
104
  return [
88
- `Deployed ${deployment.moduleName} to ${shortPath(deployment.dir)} - commit ${deployment.commit}, ${deployment.when}: ${deployment.subject}.`,
89
- ...(deployment.hostDir !== undefined ? [`Its guidance takes it on from there, in ${shortPath(deployment.hostDir)}.`] : []),
90
- ...(deployment.instead !== undefined ? [`${deployment.instead}, the module on top, has nothing built yet - this is ${deployment.moduleName}, the module below it.`] : []),
91
- ...(deployment.unfinished
92
- ? [
93
- deployment.hostDir === undefined
94
- ? "A pulse is at work on the module right now: what it has written so far is not finished, and is left out."
95
- : `A pulse is at work on the module right now: what it has written so far is not finished, and is left out of ${shortPath(deployment.dir)} - but ${shortPath(deployment.buildDir)} holds it, so take the code from ${shortPath(deployment.dir)}, never from there.`,
96
- ]
105
+ `${code.moduleName}'s code at commit ${code.short} (${code.when}: ${code.subject}) is in ${shortPath(code.dir)}.`,
106
+ ...(code.instead !== undefined ? [`${code.instead}, the module on top, has nothing built yet - this is ${code.moduleName}, the module below it.`] : []),
107
+ ...(code.unfinished
108
+ ? [`A pulse is at work on the module right now: what it has written so far is not finished, and is left out of ${shortPath(code.dir)} - ${shortPath(code.buildDir)} holds it, so take the code from ${shortPath(code.dir)}, never from there.`]
97
109
  : []),
98
110
  ].join("\n");
99
111
  }
100
112
  const processes = new Map();
101
- /** Everything deploy started, running or ended, oldest first. */
102
- export function listDeployed() {
113
+ const RUNNING_DIR = "running";
114
+ function writeRecord(logDir, record) {
115
+ try {
116
+ const dir = join(logDir, RUNNING_DIR);
117
+ mkdirSync(dir, { recursive: true });
118
+ const file = join(dir, `${record.pid}.json`);
119
+ writeFileSync(file, `${JSON.stringify(record, null, 2)}\n`);
120
+ return file;
121
+ }
122
+ catch {
123
+ // Unrecorded, it is still stopped by this turri - only a turri that dies leaves it.
124
+ return undefined;
125
+ }
126
+ }
127
+ function dropRecord(file) {
128
+ if (file === undefined)
129
+ return false;
130
+ try {
131
+ unlinkSync(file);
132
+ return true;
133
+ }
134
+ catch {
135
+ return false;
136
+ }
137
+ }
138
+ const alive = (pid) => {
139
+ try {
140
+ process.kill(pid, 0);
141
+ return true;
142
+ }
143
+ catch (error) {
144
+ // Alive, but another user's: it is not one turri started.
145
+ return error.code === "EPERM";
146
+ }
147
+ };
148
+ /**
149
+ * The processes of the group a launch started, as `ps` says them now - none when it has none left, or ps cannot say
150
+ * (Windows): what cannot be told to be turri's is never stopped. Theirs only when the group is the one the record names:
151
+ * its leader started when the record says, or - the leader gone - every one of them started after it. A pid is not given
152
+ * again while a group of that id has a process left.
153
+ */
154
+ function groupOf(record) {
155
+ const listed = spawnSync("ps", ["-A", "-o", "pid=,pgid=,lstart="], { encoding: "utf8", env: { ...process.env, LC_ALL: "C" } });
156
+ if (listed.status !== 0)
157
+ return [];
158
+ const members = [];
159
+ for (const line of listed.stdout.split("\n")) {
160
+ const match = /^\s*(\d+)\s+(\d+)\s+(.+?)\s*$/.exec(line);
161
+ if (match === null || Number(match[2]) !== record.pgid)
162
+ continue;
163
+ members.push({ pid: Number(match[1]), started: Date.parse(match[3]) });
164
+ }
165
+ if (members.length === 0)
166
+ return [];
167
+ const slack = 5000;
168
+ const leader = members.find((member) => member.pid === record.pgid);
169
+ const theirs = leader !== undefined
170
+ ? Number.isFinite(leader.started) && Math.abs(leader.started - record.startedAt) <= slack
171
+ : members.every((member) => Number.isFinite(member.started) && member.started >= record.startedAt - slack);
172
+ return theirs ? members.map((member) => member.pid) : [];
173
+ }
174
+ const groupAlive = (pgid) => {
175
+ try {
176
+ process.kill(-pgid, 0);
177
+ return true;
178
+ }
179
+ catch (error) {
180
+ return error.code === "EPERM";
181
+ }
182
+ };
183
+ /**
184
+ * Stop what launches of an earlier turri left running in the project - the codebase's, or every one's - as stopLaunched
185
+ * stops this session's: its process group, politely, then not. Only what turri's own records name, whose turri has ended
186
+ * (another turri still running keeps its own), and only when the process is still the one recorded: a process turri did
187
+ * not start is never stopped. Records of what has ended are taken out. Returns what was stopped.
188
+ */
189
+ export async function stopLeftBehind(projectDir, codebase) {
190
+ const dir = join(projectDir, ".turri", "launch", RUNNING_DIR);
191
+ let names;
192
+ try {
193
+ names = readdirSync(dir).filter((name) => name.endsWith(".json"));
194
+ }
195
+ catch {
196
+ return [];
197
+ }
198
+ const stopping = [];
199
+ for (const name of names) {
200
+ const file = join(dir, name);
201
+ let record;
202
+ try {
203
+ record = JSON.parse(readFileSync(file, "utf8"));
204
+ }
205
+ catch {
206
+ continue;
207
+ }
208
+ if (codebase !== undefined && record.moduleName !== codebase)
209
+ continue;
210
+ // This turri's own are stopLaunched's; another turri still running keeps its own.
211
+ if (record.turri === process.pid || alive(record.turri))
212
+ continue;
213
+ const group = groupOf(record);
214
+ // Taken by whoever takes the record out first: a sweep at the session's start and one before a launch never both stop it.
215
+ if (!dropRecord(file) || group.length === 0)
216
+ continue;
217
+ signalGroup(record.pgid, "SIGTERM");
218
+ stopping.push(record);
219
+ }
220
+ const deadline = Date.now() + 5000;
221
+ while (Date.now() < deadline && stopping.some((record) => groupAlive(record.pgid)))
222
+ await sleep(100);
223
+ for (const record of stopping)
224
+ if (groupAlive(record.pgid))
225
+ signalGroup(record.pgid, "SIGKILL");
226
+ return stopping;
227
+ }
228
+ /** What was stopped of an earlier session's, in a line - for a launch's summary. */
229
+ export function describeLeftBehind(stopped) {
230
+ const each = stopped.map((record) => `${record.name} (pid ${record.pid}${record.url !== undefined ? `, ${record.url}` : ""})`);
231
+ return `Stopped what an earlier turri session left running of ${[...new Set(stopped.map((record) => record.moduleName))].join(", ")}: ${each.join(", ")}.`;
232
+ }
233
+ /** Everything a launch started, running or ended, oldest first. */
234
+ export function listLaunched() {
103
235
  return [...processes.values()].map((entry) => entry.info);
104
236
  }
105
237
  /** The ones still running. */
106
- export function runningDeployed(moduleName) {
107
- return listDeployed().filter((info) => info.exited === undefined && (moduleName === undefined || info.moduleName === moduleName));
238
+ export function runningLaunched(moduleName) {
239
+ return listLaunched().filter((info) => info.exited === undefined && (moduleName === undefined || info.moduleName === moduleName));
108
240
  }
109
- /** Where a project's deployed processes write their output: its scratch, not the code in dist. */
110
- export function deployLogDir(projectDir) {
241
+ /** Where what a project's launches started write their output: its scratch, not the code in dist. */
242
+ export function launchLogDir(projectDir) {
111
243
  const scratch = join(projectDir, ".turri");
112
- return ensureInScratch(scratch, join(scratch, "deploy"));
244
+ return ensureInScratch(scratch, join(scratch, "launch"));
113
245
  }
114
246
  /** The end of a log, for when something did not come up. */
115
247
  export function logTail(log, lines = 40, chars = 4000) {
@@ -136,8 +268,8 @@ function signalGroup(pid, signal) {
136
268
  }
137
269
  }
138
270
  const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
139
- /** Stop one deployed process, or every one of a module, or every one: politely, then not. Returns the names stopped. */
140
- export async function stopDeployed(which = {}) {
271
+ /** Stop one launched process, or every one of a module, or every one: politely, then not. Returns the names stopped. */
272
+ export async function stopLaunched(which = {}) {
141
273
  const targets = [...processes.values()].filter(({ info }) => info.exited === undefined && (which.name === undefined || info.name === which.name) && (which.moduleName === undefined || info.moduleName === which.moduleName));
142
274
  for (const { info } of targets)
143
275
  signalGroup(info.pid, "SIGTERM");
@@ -150,7 +282,7 @@ export async function stopDeployed(which = {}) {
150
282
  return targets.map(({ info }) => info.name);
151
283
  }
152
284
  /** Stop everything at once, without waiting: for when turri itself is going. */
153
- export function stopAllDeployedNow() {
285
+ export function stopAllLaunchedNow() {
154
286
  for (const { info } of processes.values())
155
287
  if (info.exited === undefined)
156
288
  signalGroup(info.pid, "SIGTERM");
@@ -160,7 +292,7 @@ function stopOnExit() {
160
292
  if (exitHooked)
161
293
  return;
162
294
  exitHooked = true;
163
- process.on("exit", stopAllDeployedNow);
295
+ process.on("exit", stopAllLaunchedNow);
164
296
  }
165
297
  function portOf(url) {
166
298
  if (url.port !== "")
@@ -181,7 +313,7 @@ function portTaken(host, port) {
181
313
  });
182
314
  }
183
315
  /** What holds a port, as lsof says it - or nothing when it cannot say. */
184
- function holderOf(port) {
316
+ export function holderOf(port) {
185
317
  const found = spawnSync("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN"], { encoding: "utf8" });
186
318
  return found.status === 0 ? found.stdout.trim().split("\n").slice(0, 4).join("\n") : "";
187
319
  }
@@ -195,7 +327,7 @@ async function answers(url) {
195
327
  }
196
328
  }
197
329
  /** How long a server is given to answer at its URL, and to stay up when it has none. */
198
- export const READY_TIMEOUT_MS = Number(process.env.TURRI_DEPLOY_READY_TIMEOUT ?? 60) * 1000;
330
+ export const READY_TIMEOUT_MS = Number(process.env.TURRI_LAUNCH_READY_TIMEOUT ?? 60) * 1000;
199
331
  const ALIVE_AFTER_MS = 4000;
200
332
  /**
201
333
  * Start something that keeps running - a server, a frontend's dev server - in `dir`, on this machine,
@@ -219,10 +351,10 @@ function listing(dir) {
219
351
  }
220
352
  }
221
353
  /**
222
- * Start what keeps running, in `dir` - the deployed folder `root`, or one inside it. A folder that is not there starts
223
- * nothing: said, with what the deployed folder holds, so the agent can name the right one.
354
+ * Start what keeps running, in `dir` - the folder `root` it is named from, or one inside it. A folder that is not there
355
+ * starts nothing: said, with what `root` holds, so the agent can name the right one.
224
356
  */
225
- export async function startDeployed(input) {
357
+ export async function startLaunched(input) {
226
358
  let isFolder = false;
227
359
  try {
228
360
  isFolder = statSync(input.dir).isDirectory();
@@ -235,11 +367,11 @@ export async function startDeployed(input) {
235
367
  return {
236
368
  ok: false,
237
369
  text: input.root !== undefined
238
- ? `${input.name} was not started: ${shortPath(input.dir)} ${why}. \`dir\` is relative to the deployed folder, ${shortPath(input.root)} - a folder inside it, e.g. frontend, or none for the deployed folder itself; never the project's path to it. ${shortPath(input.root)} holds: ${listing(input.root)}.`
370
+ ? `${input.name} was not started: ${shortPath(input.dir)} ${why}. \`dir\` is relative to ${shortPath(input.root)} - a folder inside it, e.g. frontend, or none for the folder itself; never the project's path to it. ${shortPath(input.root)} holds: ${listing(input.root)}.`
239
371
  : `${input.name} was not started: ${shortPath(input.dir)} ${why}.`,
240
372
  };
241
373
  }
242
- await stopDeployed({ name: input.name });
374
+ await stopLaunched({ name: input.name });
243
375
  let url;
244
376
  if (input.url !== undefined) {
245
377
  try {
@@ -280,10 +412,22 @@ export async function startDeployed(input) {
280
412
  }
281
413
  const started = { name: input.name, moduleName: input.moduleName, command: input.command, dir: input.dir, log, pid: child.pid, startedAt: Date.now(), ...(input.url !== undefined ? { url: input.url } : {}) };
282
414
  info = started;
415
+ const record = writeRecord(input.logDir, {
416
+ pid: child.pid,
417
+ pgid: child.pid,
418
+ name: input.name,
419
+ moduleName: input.moduleName,
420
+ command: input.command,
421
+ dir: input.dir,
422
+ ...(input.url !== undefined ? { url: input.url } : {}),
423
+ startedAt: started.startedAt,
424
+ turri: process.pid,
425
+ });
283
426
  child.on("exit", (code, signal) => {
284
427
  started.exited = { code, signal };
428
+ dropRecord(record);
285
429
  });
286
- processes.set(input.name, { info: started, child });
430
+ processes.set(input.name, { info: started, child, ...(record !== undefined ? { record } : {}) });
287
431
  stopOnExit();
288
432
  const failed = (why) => {
289
433
  const tail = logTail(log);
@@ -69,8 +69,39 @@ export type LedgerEvent = {
69
69
  } | {
70
70
  kind: "sentinel.checking";
71
71
  module?: string;
72
- replaced?: string;
73
72
  check?: string;
73
+ } | {
74
+ kind: "sentinel.paused";
75
+ module: string;
76
+ launch: string;
77
+ } | {
78
+ kind: "sentinel.resumed";
79
+ module: string;
80
+ launch: string;
81
+ } | {
82
+ kind: "sentinel.refused";
83
+ module?: string;
84
+ check?: string;
85
+ why: string;
86
+ } | {
87
+ kind: "launch.started";
88
+ launch: string;
89
+ commit: string;
90
+ pulse?: string;
91
+ } | {
92
+ kind: "launch.finished";
93
+ launch: string;
94
+ commit: string;
95
+ status: string;
96
+ ms: number;
97
+ } | {
98
+ kind: "launch.left-behind";
99
+ stopped: Array<{
100
+ name: string;
101
+ pid: number;
102
+ module: string;
103
+ url?: string;
104
+ }>;
74
105
  } | {
75
106
  kind: "sentinel.concluded";
76
107
  verdict: string;
@@ -79,6 +79,8 @@ export declare function commitPaths(moduleDir: string, paths: string[], subject:
79
79
  export declare function refreshExcludes(moduleDir: string): void;
80
80
  /** The current commit of a module's history, if it has one. */
81
81
  export declare function head(dir: string): string | undefined;
82
+ /** The module's last commit, whole. */
83
+ export declare function lastCommit(dir: string): string | undefined;
82
84
  /** The module's history, newest first, one line per commit. */
83
85
  export declare function historyLog(dir: string, limit?: number): string;
84
86
  /** One commit in full: its message and what it changed. */
@@ -382,6 +382,14 @@ export function head(dir) {
382
382
  const result = git(moduleDir, ["rev-parse", "--short", "HEAD"]);
383
383
  return result.status === 0 ? result.stdout.trim() : undefined;
384
384
  }
385
+ /** The module's last commit, whole. */
386
+ export function lastCommit(dir) {
387
+ const moduleDir = moduleDirOf(dir);
388
+ if (!existsSync(join(moduleDir, HISTORY_DIR)))
389
+ return undefined;
390
+ const result = git(moduleDir, ["rev-parse", "--verify", "--quiet", "HEAD"]);
391
+ return result.status === 0 ? result.stdout.trim() : undefined;
392
+ }
385
393
  /** The module's history, newest first, one line per commit. */
386
394
  export function historyLog(dir, limit = 30) {
387
395
  const moduleDir = moduleDirOf(dir);
@@ -374,15 +374,16 @@ export declare function createWorkLoop(options: {
374
374
  /** Writes and runs a conformance job's tests. Default: a spec diff's conformance run, the whole suite with it. */
375
375
  runConformance?: JobRunner;
376
376
  /**
377
- * Under `release-after-pulse: true`, what releases a pulse that landed, as its last stage - before the pulse ends, so
378
- * the module's next pulse starts on what is released. Its last reply goes in the pulse's report. Not given: nothing is.
377
+ * A pulse landed: its codebase's commit, once its line is in the notes - the commit what is built on it builds on.
378
+ * Called before the codebase's next pulse starts; what it does - a launch, under launch-after-pulse - runs on its own.
379
379
  */
380
- release?: (input: {
380
+ landed?: (input: {
381
+ job: Job;
381
382
  plainFile: string;
382
383
  moduleName: string;
384
+ commit: string;
383
385
  title: string;
384
- onDelta: (delta: string) => void;
385
- }) => Promise<string>;
386
+ }) => void;
386
387
  }): WorkLoop;
387
388
  /** Why nothing runs in a codebase whose code - and conformance tests, where they are wanted - is built from its spec as it is. */
388
389
  export declare const UP_TO_DATE = "built from the ***plain as it is";