@agentage/cli 0.53.0 → 0.55.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 +9 -11
- package/dist/commands/daemon/daemon.d.ts +3 -3
- package/dist/commands/daemon/daemon.js +21 -8
- package/dist/commands/daemon/installer.js +4 -7
- package/dist/commands/extension/build.js +5 -3
- package/dist/commands/extension/configure.js +2 -1
- package/dist/commands/extension/create-install.js +3 -2
- package/dist/commands/extension/install-once.d.ts +2 -0
- package/dist/commands/extension/install-once.js +5 -5
- package/dist/commands/extension/kit-tool.d.ts +1 -1
- package/dist/commands/extension/lifecycle.d.ts +0 -2
- package/dist/commands/extension/lifecycle.js +4 -4
- package/dist/commands/extension/package-build.d.ts +10 -0
- package/dist/commands/extension/package-build.js +28 -9
- package/dist/commands/extension/release-guard.d.ts +8 -1
- package/dist/commands/extension/release-guard.js +39 -16
- package/dist/commands/extension/release.js +35 -19
- package/dist/commands/extension/setup-install.js +2 -4
- package/dist/commands/extension/up-app.d.ts +9 -0
- package/dist/commands/extension/up-app.js +59 -0
- package/dist/commands/extension/up-build.d.ts +0 -1
- package/dist/commands/extension/up-build.js +3 -9
- package/dist/commands/extension/up-local.d.ts +3 -0
- package/dist/commands/extension/up-local.js +30 -11
- package/dist/commands/extension/up-reload.d.ts +9 -1
- package/dist/commands/extension/up-reload.js +58 -17
- package/dist/commands/extension/up-run.d.ts +2 -0
- package/dist/commands/extension/up-run.js +80 -38
- package/dist/commands/extension/up-watch.d.ts +2 -2
- package/dist/commands/extension/up-watch.js +21 -11
- package/dist/commands/extension/up.js +6 -4
- package/dist/commands/status/status-daemon.d.ts +12 -0
- package/dist/commands/status/status-daemon.js +22 -0
- package/dist/commands/status/status.d.ts +2 -0
- package/dist/commands/status/status.js +18 -4
- package/dist/core/loader/manifest.js +1 -2
- package/dist/lib/daemon/daemon-client.js +3 -6
- package/dist/lib/daemon/daemon-probe.d.ts +1 -0
- package/dist/lib/daemon/daemon-probe.js +7 -2
- package/dist/lib/daemon/host-client.d.ts +8 -5
- package/dist/lib/daemon/host-client.js +19 -16
- package/dist/lib/fs/file-lock.js +6 -0
- package/dist/lib/proc/start-time.d.ts +2 -2
- package/dist/lib/proc/start-time.js +12 -10
- package/package.json +7 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AppBuilder } from '@agentage/extension/tool';
|
|
2
|
+
import { type LoadKitTool } from './kit-tool.js';
|
|
3
|
+
import { type Run } from './up-reload.js';
|
|
4
|
+
export declare const APP_OUT: string;
|
|
5
|
+
export declare const hasAppPart: (dir: string) => boolean;
|
|
6
|
+
export declare const startAppBuilder: (dir: string, tool?: LoadKitTool) => Promise<AppBuilder | null>;
|
|
7
|
+
export declare const appIntoNext: (dir: string) => void;
|
|
8
|
+
export declare const buildAppOnce: (dir: string, builder: AppBuilder) => Promise<boolean>;
|
|
9
|
+
export declare const appAndReload: (run: Run, builder: AppBuilder) => Promise<void>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { cpSync, existsSync, rmSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { isRecord } from '../../lib/fs/json-file.js';
|
|
4
|
+
import { messageOf } from '../../lib/json-output.js';
|
|
5
|
+
import { loadKitTool } from './kit-tool.js';
|
|
6
|
+
import { packageOf } from './package-build.js';
|
|
7
|
+
import { NEXT } from './up-build.js';
|
|
8
|
+
import {} from './up-reload.js';
|
|
9
|
+
// The app part's build output (kit APP_DIR); the shell imports it, the child never reads it.
|
|
10
|
+
export const APP_OUT = join('dist', 'app');
|
|
11
|
+
export const hasAppPart = (dir) => {
|
|
12
|
+
const agentage = packageOf(dir)['agentage'];
|
|
13
|
+
const parts = isRecord(agentage) ? agentage['parts'] : undefined;
|
|
14
|
+
return isRecord(parts) && parts['app'] === true;
|
|
15
|
+
};
|
|
16
|
+
// A warm esbuild context for the whole up; null (with one line) when it cannot start.
|
|
17
|
+
export const startAppBuilder = async (dir, tool = loadKitTool) => {
|
|
18
|
+
if (!hasAppPart(dir))
|
|
19
|
+
return null;
|
|
20
|
+
try {
|
|
21
|
+
return await (await tool()).appBuilder(dir);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
console.error(`app no app build: ${messageOf(err)}`);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
// A full reload swaps dist.next for dist whole: dist.next/app must be the current app build.
|
|
29
|
+
export const appIntoNext = (dir) => {
|
|
30
|
+
const next = join(dir, NEXT, 'app');
|
|
31
|
+
rmSync(next, { recursive: true, force: true });
|
|
32
|
+
if (existsSync(join(dir, APP_OUT)))
|
|
33
|
+
cpSync(join(dir, APP_OUT), next, { recursive: true });
|
|
34
|
+
};
|
|
35
|
+
// One build of src/app into dist/app and dist.next/app; false (one line) when it failed.
|
|
36
|
+
export const buildAppOnce = async (dir, builder) => {
|
|
37
|
+
try {
|
|
38
|
+
await builder.rebuild();
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
console.error(`app build failed, no reload: ${messageOf(err)}`);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
appIntoNext(dir);
|
|
45
|
+
return true;
|
|
46
|
+
};
|
|
47
|
+
// A save in src/app: build it, then a reload of the app part only (the child keeps running).
|
|
48
|
+
export const appAndReload = async (run, builder) => {
|
|
49
|
+
const since = Date.now();
|
|
50
|
+
if (!(await buildAppOnce(run.dir, builder)))
|
|
51
|
+
return;
|
|
52
|
+
const answer = await run.host
|
|
53
|
+
.reload(run.id, 'app')
|
|
54
|
+
.catch((err) => ({ error: messageOf(err) }));
|
|
55
|
+
const ms = Date.now() - since;
|
|
56
|
+
if ('error' in answer)
|
|
57
|
+
return console.error(`app ${run.id} reload failed: ${answer.error}`);
|
|
58
|
+
console.log(`app ${run.id} reloaded in ${ms} ms (the child keeps running)`);
|
|
59
|
+
};
|
|
@@ -9,7 +9,6 @@ export type UpMode = {
|
|
|
9
9
|
script: string | null;
|
|
10
10
|
};
|
|
11
11
|
export declare const NEXT = "dist.next";
|
|
12
|
-
export declare const buildScriptOf: (dir: string) => string | null;
|
|
13
12
|
export declare const upModeOf: (dir: string) => UpMode;
|
|
14
13
|
export declare const seedNext: (dir: string) => void;
|
|
15
14
|
export declare const stageOf: (dir: string) => string;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync,
|
|
2
|
+
import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, rmSync, symlinkSync, unlinkSync, } from 'node:fs';
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { basename, join, resolve } from 'node:path';
|
|
5
5
|
import { getConfigDir } from '../../lib/fs/config.js';
|
|
6
|
-
import {} from './package-build.js';
|
|
6
|
+
import { buildScriptOf } from './package-build.js';
|
|
7
7
|
// The live tree the daemon runs is dist; the watcher writes only dist.next (the daemon copies it).
|
|
8
8
|
export const NEXT = 'dist.next';
|
|
9
9
|
const TSC = /^tsc(?:\s+-p\s+(\S+))?$/;
|
|
10
|
-
export const buildScriptOf = (dir) => {
|
|
11
|
-
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
12
|
-
const script = pkg.scripts?.['build'];
|
|
13
|
-
return typeof script === 'string' ? script : null;
|
|
14
|
-
};
|
|
15
10
|
// The folder's own typescript, run as `node typescript/bin/tsc` (no .bin shim, none on Windows).
|
|
16
11
|
const tscOf = (dir) => {
|
|
17
12
|
try {
|
|
@@ -51,8 +46,7 @@ export const removeStage = (dir) => {
|
|
|
51
46
|
unlinkSync(modules);
|
|
52
47
|
rmSync(stageOf(dir), { recursive: true, force: true });
|
|
53
48
|
};
|
|
54
|
-
// A copy
|
|
55
|
-
// No .env*, coverage, .git or dist*; a `../` path in the build script resolves from the cache.
|
|
49
|
+
// A copy with node_modules linked, no .env*, coverage, .git or dist*: never the live dist.
|
|
56
50
|
export const prepareStage = (dir) => {
|
|
57
51
|
const stage = stageOf(dir);
|
|
58
52
|
removeStage(dir);
|
|
@@ -13,6 +13,9 @@ export declare const writeRecord: (dir: string, record: UpRecord) => void;
|
|
|
13
13
|
export declare const removeRecord: (dir: string) => void;
|
|
14
14
|
export declare const stopOrphan: (record: UpRecord | null) => void;
|
|
15
15
|
export declare const refuseRunning: (dir: string) => UpRecord | null;
|
|
16
|
+
export declare const selfMark: () => ProcessMark;
|
|
17
|
+
export declare const claimFolder: (dir: string, nonce: string, id: string) => Promise<UpRecord | null>;
|
|
18
|
+
export declare const rewriteRecord: (dir: string, record: UpRecord) => Promise<void>;
|
|
16
19
|
export declare const untilStopped: (dir: string, nonce: string, ended: Promise<string>, everyMs?: number) => Promise<string>;
|
|
17
20
|
export type LocalDown = 'stopped' | 'stale' | 'none';
|
|
18
21
|
export declare const stopLocal: (dir: string, timeoutMs?: number) => Promise<{
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
|
|
2
2
|
import { join, resolve } from 'node:path';
|
|
3
|
-
import { isMark, proven, stopProven } from '../../lib/proc/start-time.js';
|
|
3
|
+
import { isMark, markOf, proven, stopProven } from '../../lib/proc/start-time.js';
|
|
4
|
+
import { withFileLock } from '../../lib/fs/file-lock.js';
|
|
4
5
|
import { isRecord, readJson, writeTextAtomic } from '../../lib/fs/json-file.js';
|
|
5
6
|
import { CliError } from '../../lib/json-output.js';
|
|
6
7
|
import { waitFor } from '../../lib/wait.js';
|
|
@@ -45,28 +46,46 @@ export const refuseRunning = (dir) => {
|
|
|
45
46
|
return (stopOrphan(record), record);
|
|
46
47
|
throw new CliError('already_running', `extension up runs for ${dir} (pid ${record.up.pid}): agentage extension down`);
|
|
47
48
|
};
|
|
48
|
-
//
|
|
49
|
-
|
|
49
|
+
// A closed terminal (SIGHUP) stops as Ctrl-C does: the watch goes, the row stays for the next up.
|
|
50
|
+
const STOPS = ['SIGINT', 'SIGTERM', 'SIGHUP'];
|
|
51
|
+
// This up: pid and start time; where the start time cannot be read (Windows) it is never proven.
|
|
52
|
+
export const selfMark = () => markOf(process.pid) ?? { pid: process.pid, started: 'unknown' };
|
|
53
|
+
// Two ups of one folder at once: the check and the claim are one step under the record's lock.
|
|
54
|
+
export const claimFolder = (dir, nonce, id) => withFileLock(recordPath(dir), () => {
|
|
55
|
+
const before = refuseRunning(dir);
|
|
56
|
+
writeRecord(dir, { up: selfMark(), nonce, id, added: before?.added === true });
|
|
57
|
+
return before;
|
|
58
|
+
});
|
|
59
|
+
// A record that is no longer this up's (down ran, or a second up took it) stops the start; locked as down is.
|
|
60
|
+
export const rewriteRecord = (dir, record) => withFileLock(recordPath(dir), () => {
|
|
61
|
+
if (readRecord(dir)?.nonce !== record.nonce)
|
|
62
|
+
throw new CliError('cancelled', `the record of this up for ${dir} changed while it started (extension down, or another up)`);
|
|
63
|
+
writeRecord(dir, record);
|
|
64
|
+
});
|
|
65
|
+
// Ends on a signal (the row stays), a removed or rewritten record (`down`) or a dead watch.
|
|
50
66
|
export const untilStopped = (dir, nonce, ended, everyMs = 300) => new Promise((done) => {
|
|
67
|
+
const on = STOPS.map((name) => [name, () => finish(name)]);
|
|
51
68
|
const finish = (why) => {
|
|
52
69
|
clearInterval(timer);
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
for (const [name, handler] of on)
|
|
71
|
+
process.off(name, handler);
|
|
55
72
|
done(why);
|
|
56
73
|
};
|
|
57
|
-
const onInt = () => finish('SIGINT');
|
|
58
|
-
const onTerm = () => finish('SIGTERM');
|
|
59
74
|
const timer = setInterval(() => readRecord(dir)?.nonce === nonce || finish('down'), everyMs);
|
|
60
|
-
|
|
61
|
-
|
|
75
|
+
for (const [name, handler] of on)
|
|
76
|
+
process.once(name, handler);
|
|
62
77
|
void ended.then(finish);
|
|
63
78
|
});
|
|
64
79
|
// Removing the record is the stop: the up of that pid sees it and exits; no up is signalled.
|
|
65
80
|
export const stopLocal = async (dir, timeoutMs = 10_000) => {
|
|
66
|
-
const record =
|
|
81
|
+
const record = await withFileLock(recordPath(dir), () => {
|
|
82
|
+
const found = readRecord(dir);
|
|
83
|
+
if (found)
|
|
84
|
+
removeRecord(dir);
|
|
85
|
+
return found;
|
|
86
|
+
});
|
|
67
87
|
if (!record)
|
|
68
88
|
return { down: 'none', record };
|
|
69
|
-
removeRecord(dir);
|
|
70
89
|
if (!proven(record.up))
|
|
71
90
|
return (stopOrphan(record), { down: 'stale', record });
|
|
72
91
|
if (!(await waitFor(() => !proven(record.up), timeoutMs)))
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { AppBuilder } from '@agentage/extension/tool';
|
|
1
2
|
import { type HostClient } from '../../lib/daemon/host-client.js';
|
|
3
|
+
import { type Code } from '../../lib/json-output.js';
|
|
2
4
|
import { type Npm } from './package-build.js';
|
|
3
5
|
import { type UpMode } from './up-build.js';
|
|
4
6
|
import { type Stoppable, type TscBuilt } from './up-watch.js';
|
|
@@ -7,10 +9,16 @@ export interface Run {
|
|
|
7
9
|
id: string;
|
|
8
10
|
host: HostClient;
|
|
9
11
|
npm: Npm;
|
|
12
|
+
fatal?: (why: string, code?: Code) => void;
|
|
13
|
+
failed?: number;
|
|
14
|
+
builtAt?: number;
|
|
15
|
+
app?: AppBuilder | null;
|
|
10
16
|
}
|
|
17
|
+
export declare const editedSince: (dir: string, since?: number, sub?: string) => boolean;
|
|
18
|
+
export declare const RELOAD_STRIKES = 3;
|
|
11
19
|
type Task = () => Promise<void>;
|
|
12
20
|
export declare const lane: () => ((task: Task) => Promise<void>);
|
|
13
|
-
export declare const reload: (
|
|
21
|
+
export declare const reload: (run: Run, since: number) => Promise<void>;
|
|
14
22
|
export declare const stageAndReload: (run: Run) => Promise<void>;
|
|
15
23
|
export declare const tscReloads: (run: Run, inLane: (task: Task) => Promise<void>) => {
|
|
16
24
|
onChange: () => undefined;
|
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
import { cpSync, existsSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
1
|
+
import { cpSync, existsSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join, sep } from 'node:path';
|
|
3
3
|
import {} from '../../lib/daemon/host-client.js';
|
|
4
4
|
import { messageOf } from '../../lib/json-output.js';
|
|
5
5
|
import {} from './package-build.js';
|
|
6
|
+
import { appAndReload } from './up-app.js';
|
|
6
7
|
import { NEXT, stagedBuild } from './up-build.js';
|
|
7
8
|
import { coalesced, runRest, watchFolder, watchTsc, } from './up-watch.js';
|
|
9
|
+
// A save between the start of the running build and the end of the first watch build.
|
|
10
|
+
export const editedSince = (dir, since, sub = 'src') => {
|
|
11
|
+
const src = join(dir, sub);
|
|
12
|
+
if (since === undefined || !existsSync(src))
|
|
13
|
+
return false;
|
|
14
|
+
return readdirSync(src, { recursive: true, withFileTypes: true }).some((f) => f.isFile() && statSync(join(f.parentPath, f.name)).mtimeMs >= since);
|
|
15
|
+
};
|
|
16
|
+
// A daemon that refuses the call three times in a row will not take the next save either.
|
|
17
|
+
export const RELOAD_STRIKES = 3;
|
|
18
|
+
const failedReload = (run, why) => {
|
|
19
|
+
console.error(`reload ${run.id} failed: ${why}`);
|
|
20
|
+
run.failed = (run.failed ?? 0) + 1;
|
|
21
|
+
if (run.failed >= RELOAD_STRIKES)
|
|
22
|
+
run.fatal?.(`the reload failed ${run.failed} times in a row`, 'daemon_unavailable');
|
|
23
|
+
};
|
|
8
24
|
// One lane for every write into dist.next and every reload: two of them never overlap.
|
|
9
25
|
export const lane = () => {
|
|
10
26
|
let tail = Promise.resolve();
|
|
@@ -15,16 +31,20 @@ export const lane = () => {
|
|
|
15
31
|
};
|
|
16
32
|
};
|
|
17
33
|
// One line per reload: reloaded and the time, or the daemon's refusal (the last good build runs).
|
|
18
|
-
export const reload = async (
|
|
34
|
+
export const reload = async (run, since) => {
|
|
35
|
+
const { host, id } = run;
|
|
19
36
|
const answer = await host.reload(id).catch((err) => ({ error: messageOf(err) }));
|
|
20
37
|
const ms = Date.now() - since;
|
|
21
38
|
if ('error' in answer)
|
|
22
|
-
return
|
|
39
|
+
return failedReload(run, answer.error);
|
|
40
|
+
run.failed = 0;
|
|
23
41
|
if (answer.reloaded)
|
|
24
42
|
return console.log(`reload ${id} reloaded: true in ${ms} ms`);
|
|
25
43
|
console.log(`reload ${id} reloaded: false, refused: ${answer.refused ?? 'no reason'} (${ms} ms)`);
|
|
26
44
|
console.log(` ${id} the last good build runs`);
|
|
27
45
|
};
|
|
46
|
+
// npm or the script's own tool cannot run (127: command not found): no later save builds either.
|
|
47
|
+
const MISSING = /did not start|exited 127$/;
|
|
28
48
|
// A build that fails prints the compiler's words (its own stderr) and sends no reload.
|
|
29
49
|
export const stageAndReload = async (run) => {
|
|
30
50
|
const since = Date.now();
|
|
@@ -32,7 +52,10 @@ export const stageAndReload = async (run) => {
|
|
|
32
52
|
await stagedBuild(run.dir, run.npm);
|
|
33
53
|
}
|
|
34
54
|
catch (err) {
|
|
35
|
-
|
|
55
|
+
console.error(`build failed, no reload: ${messageOf(err)}`);
|
|
56
|
+
if (MISSING.test(messageOf(err)))
|
|
57
|
+
run.fatal?.('the build tool is missing (npm ci installs it)');
|
|
58
|
+
return;
|
|
36
59
|
}
|
|
37
60
|
await reload(run, since);
|
|
38
61
|
};
|
|
@@ -44,8 +67,7 @@ const bundleUi = async (run, rest) => {
|
|
|
44
67
|
cpSync(join(run.dir, 'dist', 'ui'), join(run.dir, NEXT, 'ui'), { recursive: true, force: true });
|
|
45
68
|
console.log(`ui bundled into dist/ui in ${Date.now() - since} ms (no reload)`);
|
|
46
69
|
};
|
|
47
|
-
// A reload only after
|
|
48
|
-
// that ends during a reload gives one more reload after it.
|
|
70
|
+
// A reload only after a clean tsc build, never during one; a build during a reload gives one more.
|
|
49
71
|
export const tscReloads = (run, inLane) => {
|
|
50
72
|
const state = { building: false, pending: false, since: 0 };
|
|
51
73
|
const go = coalesced(() => inLane(async () => {
|
|
@@ -58,22 +80,47 @@ export const tscReloads = (run, inLane) => {
|
|
|
58
80
|
state.building = false;
|
|
59
81
|
if (errors > 0)
|
|
60
82
|
console.error(`build ${errors} error(s), no reload`);
|
|
61
|
-
|
|
83
|
+
// The first watch build is the running one, unless a save came in while it built.
|
|
84
|
+
const changed = !first || editedSince(run.dir, run.builtAt);
|
|
85
|
+
if (errors === 0 && changed)
|
|
62
86
|
Object.assign(state, { pending: true, since: Date.now() - ms });
|
|
63
87
|
if (state.pending)
|
|
64
88
|
void go();
|
|
65
89
|
};
|
|
66
90
|
return { onChange: () => void (state.building = true), onBuilt };
|
|
67
91
|
};
|
|
92
|
+
const inApp = (file) => file === 'app' || file.startsWith(`app${sep}`);
|
|
93
|
+
// src/app: its own watch, in the lane of every dist.next write, never a child restart.
|
|
94
|
+
const appWatch = (run, inLane) => {
|
|
95
|
+
const app = run.app;
|
|
96
|
+
if (!app)
|
|
97
|
+
return [];
|
|
98
|
+
const go = coalesced(() => inLane(() => appAndReload(run, app)));
|
|
99
|
+
const watcher = watchFolder(join(run.dir, 'src', 'app'), go);
|
|
100
|
+
// A save between the start of up and this watch (the page may be open already) is built now.
|
|
101
|
+
if (editedSince(run.dir, run.builtAt, join('src', 'app')))
|
|
102
|
+
void go();
|
|
103
|
+
return [watcher];
|
|
104
|
+
};
|
|
68
105
|
const watchWithTsc = (run, mode, onDied) => {
|
|
69
106
|
const inLane = lane();
|
|
70
107
|
const tsc = watchTsc(run.dir, mode, { ...tscReloads(run, inLane), onDied });
|
|
108
|
+
const app = appWatch(run, inLane);
|
|
71
109
|
if (!mode.rest)
|
|
72
|
-
return { watchers: [tsc], tscPid: tsc.pid };
|
|
110
|
+
return { watchers: [tsc, ...app], tscPid: tsc.pid };
|
|
73
111
|
const rest = mode.rest;
|
|
74
112
|
const bundle = coalesced(() => inLane(() => bundleUi(run, rest)));
|
|
75
113
|
const ui = watchFolder(join(run.dir, 'src', 'ui'), bundle);
|
|
76
|
-
return { watchers: [tsc, ui], tscPid: tsc.pid };
|
|
114
|
+
return { watchers: [tsc, ui, ...app], tscPid: tsc.pid };
|
|
115
|
+
};
|
|
116
|
+
const watchStaged = (run, script) => {
|
|
117
|
+
const inLane = lane();
|
|
118
|
+
const app = appWatch(run, inLane);
|
|
119
|
+
if (!script)
|
|
120
|
+
return { watchers: app };
|
|
121
|
+
const skip = run.app ? inApp : undefined;
|
|
122
|
+
const go = coalesced(() => inLane(() => stageAndReload(run)));
|
|
123
|
+
return { watchers: [watchFolder(join(run.dir, 'src'), go, undefined, skip), ...app] };
|
|
77
124
|
};
|
|
78
125
|
export const startWatch = (run, mode, onDied) => {
|
|
79
126
|
if (mode.kind === 'tsc')
|
|
@@ -82,11 +129,5 @@ export const startWatch = (run, mode, onDied) => {
|
|
|
82
129
|
console.error('watch no build script: a change in src/ is not built');
|
|
83
130
|
if (!existsSync(join(run.dir, 'src')))
|
|
84
131
|
console.error('watch no src/ folder: nothing to watch');
|
|
85
|
-
|
|
86
|
-
return { watchers: [] };
|
|
87
|
-
return {
|
|
88
|
-
watchers: [
|
|
89
|
-
watchFolder(join(run.dir, 'src'), coalesced(() => stageAndReload(run))),
|
|
90
|
-
],
|
|
91
|
-
};
|
|
132
|
+
return watchStaged(run, mode.script);
|
|
92
133
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AppBuilder } from '@agentage/extension/tool';
|
|
1
2
|
import { type HostClient } from '../../lib/daemon/host-client.js';
|
|
2
3
|
import { type Npm, type PackageBuild } from './package-build.js';
|
|
3
4
|
export interface LocalUpDeps {
|
|
@@ -8,6 +9,7 @@ export interface LocalUpDeps {
|
|
|
8
9
|
}>;
|
|
9
10
|
npm: Npm;
|
|
10
11
|
stopped: (dir: string, nonce: string, ended: Promise<string>) => Promise<string>;
|
|
12
|
+
app?: (dir: string) => Promise<AppBuilder | null>;
|
|
11
13
|
}
|
|
12
14
|
export declare const localUpDeps: LocalUpDeps;
|
|
13
15
|
export interface LocalUp {
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
3
2
|
import { markOf } from '../../lib/proc/start-time.js';
|
|
4
|
-
import {
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { isRecord } from '../../lib/fs/json-file.js';
|
|
5
5
|
import { ensureDaemon } from '../../lib/daemon/daemon-client.js';
|
|
6
6
|
import { hostClient } from '../../lib/daemon/host-client.js';
|
|
7
7
|
import { setExit } from '../../lib/exit-code.js';
|
|
8
8
|
import { CliError } from '../../lib/json-output.js';
|
|
9
|
-
import { installFolder } from './install-once.js';
|
|
9
|
+
import { folderRow, installFolder } from './install-once.js';
|
|
10
10
|
import { loadKitTool } from './kit-tool.js';
|
|
11
|
-
import { buildPackage, npmIn } from './package-build.js';
|
|
11
|
+
import { buildPackage, npmIn, packageOf } from './package-build.js';
|
|
12
|
+
import { buildAppOnce, startAppBuilder } from './up-app.js';
|
|
12
13
|
import { removeStage, seedNext, upModeOf } from './up-build.js';
|
|
13
|
-
import {
|
|
14
|
+
import { claimFolder, rewriteRecord, selfMark, untilStopped } from './up-local.js';
|
|
14
15
|
import { stageAndReload, startWatch } from './up-reload.js';
|
|
15
16
|
export const localUpDeps = {
|
|
16
17
|
build: (dir) => buildPackage(dir),
|
|
@@ -21,20 +22,14 @@ export const localUpDeps = {
|
|
|
21
22
|
npm: npmIn,
|
|
22
23
|
stopped: (dir, nonce, ended) => untilStopped(dir, nonce, ended),
|
|
23
24
|
};
|
|
24
|
-
const real = (path) => (existsSync(path) ? realpathSync(path) : path);
|
|
25
25
|
// Checked before the daemon starts: a folder that is no extension package starts nothing.
|
|
26
26
|
const idOf = (dir) => {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
throw new CliError('invalid_argument', `no package.json in ${dir}: run it in an extension package folder`);
|
|
30
|
-
const pkg = JSON.parse(readFileSync(file, 'utf8'));
|
|
31
|
-
const id = pkg.agentage?.id;
|
|
27
|
+
const agentage = packageOf(dir)['agentage'];
|
|
28
|
+
const id = isRecord(agentage) ? agentage['id'] : undefined;
|
|
32
29
|
if (typeof id !== 'string')
|
|
33
30
|
throw new CliError('invalid_argument', `${dir}: no agentage.id`);
|
|
34
31
|
return id;
|
|
35
32
|
};
|
|
36
|
-
// The daemon runs this folder already (a create or an earlier up): its live dist is not rebuilt.
|
|
37
|
-
const runsHere = async (host, id, dir) => (await host.list()).some((row) => row.id === id && real(row.source) === real(dir));
|
|
38
33
|
// Problems do not stop a local run; they are printed, as `extension build` would refuse them.
|
|
39
34
|
const warnProblems = async ({ report }) => {
|
|
40
35
|
if (report.conforms)
|
|
@@ -56,9 +51,12 @@ const stopLine = (id, stoppedBy, added) => {
|
|
|
56
51
|
if (added)
|
|
57
52
|
console.log(` agentage extension down removes it (its data stays)`);
|
|
58
53
|
};
|
|
59
|
-
// The first build: in the folder while no child runs it, else staged
|
|
54
|
+
// The first build: in the folder while no child runs it (a create or an earlier up), else staged.
|
|
55
|
+
// The app goes into dist/app first, so dist.next starts with it (a full reload swaps dist whole).
|
|
60
56
|
const firstBuild = async (run, mode, build) => {
|
|
61
|
-
if (
|
|
57
|
+
if (run.app)
|
|
58
|
+
await buildAppOnce(run.dir, run.app);
|
|
59
|
+
if (!(await folderRow(run.host, run.id, run.dir))) {
|
|
62
60
|
await warnProblems(await build(run.dir));
|
|
63
61
|
return seedNext(run.dir);
|
|
64
62
|
}
|
|
@@ -66,56 +64,100 @@ const firstBuild = async (run, mode, build) => {
|
|
|
66
64
|
if (mode.kind === 'tsc' || mode.script)
|
|
67
65
|
await stageAndReload(run);
|
|
68
66
|
};
|
|
69
|
-
// A watch that ends by itself
|
|
67
|
+
// A watch that ends by itself, a missing build tool or a reload that keeps failing stops up.
|
|
70
68
|
const deathOf = () => {
|
|
71
69
|
let died = () => undefined;
|
|
70
|
+
let reason = '';
|
|
71
|
+
let code = 'build_failed';
|
|
72
72
|
const ended = new Promise((done) => (died = done));
|
|
73
|
-
const onDied = (why) => {
|
|
74
|
-
|
|
73
|
+
const onDied = (why, as = 'build_failed') => {
|
|
74
|
+
if (reason)
|
|
75
|
+
return;
|
|
76
|
+
[reason, code] = [why, as];
|
|
77
|
+
console.error(`watch ${why}; extension up stops (run it again)`);
|
|
75
78
|
died('watch died');
|
|
76
79
|
};
|
|
77
|
-
|
|
80
|
+
const failure = () => new CliError(code, `extension up stopped: ${reason}`);
|
|
81
|
+
return { ended, onDied, failure, died: () => reason !== '' };
|
|
78
82
|
};
|
|
79
|
-
|
|
83
|
+
// Ctrl-C is 130 and a closed terminal 129; a watch that died fails, so --json gives the error.
|
|
84
|
+
const exitFor = (stoppedBy, failure) => {
|
|
80
85
|
if (stoppedBy === 'SIGINT')
|
|
81
86
|
setExit(130);
|
|
87
|
+
if (stoppedBy === 'SIGHUP')
|
|
88
|
+
setExit(129);
|
|
82
89
|
if (stoppedBy === 'watch died')
|
|
83
|
-
|
|
90
|
+
throw failure();
|
|
84
91
|
};
|
|
85
92
|
// D12: build, the folder as a daemon row, a watch that builds to dist.next and reloads that child.
|
|
86
93
|
export const runLocalUp = async (folder = '.', deps = localUpDeps, json = false) => {
|
|
87
94
|
const dir = resolve(folder);
|
|
88
|
-
const before = refuseRunning(dir);
|
|
89
95
|
const id = idOf(dir);
|
|
96
|
+
const nonce = randomUUID();
|
|
97
|
+
const before = await claimFolder(dir, nonce, id);
|
|
90
98
|
const { port, host } = await deps.daemon();
|
|
91
99
|
const run = { dir, id, host, npm: deps.npm };
|
|
92
100
|
const mode = upModeOf(dir);
|
|
93
101
|
// A stage of an earlier up may hold an old package.json: the first staged build copies it again.
|
|
94
102
|
removeStage(dir);
|
|
103
|
+
run.app = await (deps.app ?? startAppBuilder)(dir);
|
|
95
104
|
try {
|
|
96
|
-
return await upAndWatch(run, mode, deps, { port, json, before });
|
|
105
|
+
return await upAndWatch(run, mode, deps, { port, json, before, nonce });
|
|
97
106
|
}
|
|
98
107
|
finally {
|
|
99
108
|
removeStage(dir);
|
|
109
|
+
await run.app?.dispose();
|
|
100
110
|
}
|
|
101
111
|
};
|
|
112
|
+
// The fatal channel is wired before the first build: a second up with no build tool stops too.
|
|
102
113
|
const upAndWatch = async (run, mode, deps, ctx) => {
|
|
114
|
+
const death = deathOf();
|
|
115
|
+
run.fatal = death.onDied;
|
|
116
|
+
run.builtAt = Date.now();
|
|
103
117
|
await firstBuild(run, mode, deps.build);
|
|
118
|
+
if (death.died())
|
|
119
|
+
throw death.failure();
|
|
104
120
|
const { row, already } = await installFolder(run.host, run.dir);
|
|
105
121
|
const added = !already || ctx.before?.added === true;
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const stoppedBy = await
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
122
|
+
const up = {
|
|
123
|
+
id: row.id,
|
|
124
|
+
version: row.version,
|
|
125
|
+
url: urlOf(ctx.port, row.id),
|
|
126
|
+
port: ctx.port,
|
|
127
|
+
added,
|
|
128
|
+
};
|
|
129
|
+
const stoppedBy = await watchUntilStopped(run, mode, { deps, ctx, death, up });
|
|
130
|
+
stopLine(up.id, stoppedBy, added);
|
|
131
|
+
exitFor(stoppedBy, death.failure);
|
|
132
|
+
return { ...up, stoppedBy };
|
|
133
|
+
};
|
|
134
|
+
// No record is left for down to read, so a row this up added goes with the cancelled start.
|
|
135
|
+
const cancelled = async (run, up, err) => {
|
|
136
|
+
if (up.added && err instanceof CliError && err.code === 'cancelled') {
|
|
137
|
+
if (await folderRow(run.host, up.id, run.dir))
|
|
138
|
+
await run.host.remove(up.id, false);
|
|
139
|
+
}
|
|
140
|
+
throw err;
|
|
141
|
+
};
|
|
142
|
+
// A record down removed meanwhile, or one that cannot be written, still stops the tsc it started.
|
|
143
|
+
const watchUntilStopped = async (run, mode, w) => {
|
|
144
|
+
const { watchers, tscPid } = startWatch(run, mode, w.death.onDied);
|
|
145
|
+
try {
|
|
146
|
+
const tsc = markOf(tscPid) ?? undefined;
|
|
147
|
+
const { nonce } = w.ctx;
|
|
148
|
+
await rewriteRecord(run.dir, {
|
|
149
|
+
up: selfMark(),
|
|
150
|
+
nonce,
|
|
151
|
+
id: w.up.id,
|
|
152
|
+
added: w.up.added,
|
|
153
|
+
...(tsc && { tsc }),
|
|
154
|
+
}).catch((err) => cancelled(run, w.up, err));
|
|
155
|
+
ready(w.up, w.ctx.json);
|
|
156
|
+
return await w.deps.stopped(run.dir, nonce, w.death.ended);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
for (const watcher of watchers)
|
|
160
|
+
watcher.stop();
|
|
161
|
+
}
|
|
119
162
|
};
|
|
120
|
-
|
|
121
|
-
const selfMark = () => markOf(process.pid) ?? { pid: process.pid, started: 'unknown' };
|
|
163
|
+
const urlOf = (port, id) => `http://127.0.0.1:${port}/api/${id}/`;
|
|
@@ -2,7 +2,7 @@ import { type UpMode } from './up-build.js';
|
|
|
2
2
|
export interface Stoppable {
|
|
3
3
|
stop: () => void;
|
|
4
4
|
}
|
|
5
|
-
export declare const watchFolder: (dir: string, onChange: () => void, debounceMs?: number) => Stoppable;
|
|
5
|
+
export declare const watchFolder: (dir: string, onChange: () => void, debounceMs?: number, skip?: (file: string) => boolean) => Stoppable;
|
|
6
6
|
export declare const coalesced: (run: () => Promise<void>) => (() => Promise<void>);
|
|
7
7
|
export interface TscBuilt {
|
|
8
8
|
errors: number;
|
|
@@ -20,4 +20,4 @@ export interface TscWatch extends Stoppable {
|
|
|
20
20
|
export declare const watchTsc: (dir: string, mode: Extract<UpMode, {
|
|
21
21
|
kind: "tsc";
|
|
22
22
|
}>, events: TscEvents) => TscWatch;
|
|
23
|
-
export declare const runRest: (dir: string, rest: string) => Promise<boolean>;
|
|
23
|
+
export declare const runRest: (dir: string, rest: string, timeoutMs?: number) => Promise<boolean>;
|
|
@@ -4,20 +4,30 @@ import { delimiter, join } from 'node:path';
|
|
|
4
4
|
import { createInterface } from 'node:readline';
|
|
5
5
|
import { childEnv } from '../../lib/child-env.js';
|
|
6
6
|
import { messageOf } from '../../lib/json-output.js';
|
|
7
|
+
import { deadline, how, STEP_MS } from './package-build.js';
|
|
7
8
|
import { NEXT } from './up-build.js';
|
|
8
9
|
// A recursive watch of one folder of the working copy; saves inside the debounce are one change.
|
|
9
|
-
|
|
10
|
+
// `skip`: a changed path (relative to dir) that is not this watch's change (src/app has its own).
|
|
11
|
+
export const watchFolder = (dir, onChange, debounceMs = 200, skip = () => false) => {
|
|
10
12
|
if (!existsSync(dir))
|
|
11
13
|
return { stop: () => undefined };
|
|
12
14
|
let timer;
|
|
13
|
-
|
|
15
|
+
// Linux recursive watches lose a file an editor saved by rename (seen as `change`): re-armed each time.
|
|
16
|
+
const fire = () => {
|
|
17
|
+
watcher.close();
|
|
18
|
+
watcher = arm();
|
|
19
|
+
onChange();
|
|
20
|
+
};
|
|
21
|
+
const arm = () => watch(dir, { recursive: true }, (_event, file) => {
|
|
22
|
+
if (file && skip(file))
|
|
23
|
+
return;
|
|
14
24
|
clearTimeout(timer);
|
|
15
|
-
timer = setTimeout(
|
|
25
|
+
timer = setTimeout(fire, debounceMs);
|
|
16
26
|
});
|
|
27
|
+
let watcher = arm();
|
|
17
28
|
return { stop: () => (clearTimeout(timer), watcher.close()) };
|
|
18
29
|
};
|
|
19
|
-
// One run at a time
|
|
20
|
-
// A run that throws prints one line: the watch goes on.
|
|
30
|
+
// One run at a time, one more after a change during it; a throw prints one line, the watch goes on.
|
|
21
31
|
export const coalesced = (run) => {
|
|
22
32
|
let busy = false;
|
|
23
33
|
let again = false;
|
|
@@ -40,7 +50,6 @@ export const coalesced = (run) => {
|
|
|
40
50
|
};
|
|
41
51
|
const FOUND = /Found (\d+) errors?\./;
|
|
42
52
|
const CHANGE = /File change detected/;
|
|
43
|
-
const exitWhy = (code, signal) => code === null ? `exited by signal ${signal ?? 'unknown'}` : `exited ${code}`;
|
|
44
53
|
// The warm watch of D13: noEmitOnError, out to dist.next; each compiler error line goes to stderr.
|
|
45
54
|
export const watchTsc = (dir, mode, events) => {
|
|
46
55
|
const args = ['-p', mode.config, '--watch', '--preserveWatchOutput', '--noEmitOnError'];
|
|
@@ -51,8 +60,8 @@ export const watchTsc = (dir, mode, events) => {
|
|
|
51
60
|
windowsHide: true,
|
|
52
61
|
});
|
|
53
62
|
let stopping = false;
|
|
54
|
-
child.once('error', (err) => stopping || events.onDied(`did not start: ${err.message}`));
|
|
55
|
-
child.once('exit', (code, signal) => stopping || events.onDied(
|
|
63
|
+
child.once('error', (err) => stopping || events.onDied(`tsc did not start: ${err.message}`));
|
|
64
|
+
child.once('exit', (code, signal) => stopping || events.onDied(`tsc ${how(code, signal)}`));
|
|
56
65
|
readBuilds(child.stdout, events);
|
|
57
66
|
return { pid: child.pid, stop: () => void ((stopping = true), child.kill()) };
|
|
58
67
|
};
|
|
@@ -72,10 +81,11 @@ const readBuilds = (out, events) => {
|
|
|
72
81
|
});
|
|
73
82
|
};
|
|
74
83
|
// The rest of the build script (the widget bundle) runs in the folder; it writes dist/ui itself.
|
|
75
|
-
export const runRest = (dir, rest) => new Promise((done) => {
|
|
84
|
+
export const runRest = (dir, rest, timeoutMs = STEP_MS) => new Promise((done) => {
|
|
76
85
|
const bin = join(dir, 'node_modules', '.bin');
|
|
77
86
|
const env = { ...childEnv(), PATH: `${bin}${delimiter}${process.env['PATH'] ?? ''}` };
|
|
78
87
|
const child = spawn(rest, { cwd: dir, env, shell: true, stdio: ['ignore', 2, 2] });
|
|
79
|
-
child
|
|
80
|
-
child.once('
|
|
88
|
+
const clock = deadline(child, timeoutMs, 1_000);
|
|
89
|
+
child.once('error', () => (clock.stop(), done(false)));
|
|
90
|
+
child.once('exit', (code) => (clock.stop(), done(code === 0)));
|
|
81
91
|
});
|