@vimhead.dev/norn-cli 0.1.0-tip.37310387847.1 → 0.1.0-tip.43698584363.1

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.
@@ -0,0 +1,103 @@
1
+ // src/internal/executor-startup.ts
2
+ import { createHash } from "node:crypto";
3
+ import { readFile } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+
6
+ // ../core/src/atomic-files.ts
7
+ import { randomUUID } from "node:crypto";
8
+ import { chmod, mkdir, rename, rm, stat, writeFile } from "node:fs/promises";
9
+ import { dirname } from "node:path";
10
+
11
+ // ../core/src/errors.ts
12
+ function isNodeError(error) {
13
+ return error instanceof Error && "code" in error;
14
+ }
15
+
16
+ // ../core/src/atomic-files.ts
17
+ async function writeJsonAtomically(path, value) {
18
+ await writeTextAtomically(path, `${JSON.stringify(value, null, 2)}
19
+ `);
20
+ }
21
+ async function writeTextAtomically(path, content) {
22
+ await writeBytesAtomically(path, Buffer.from(content, "utf8"));
23
+ }
24
+ async function writeBytesAtomically(path, content) {
25
+ await mkdir(dirname(path), { recursive: true });
26
+ let existingMode;
27
+ try {
28
+ existingMode = (await stat(path)).mode & 511;
29
+ } catch (error) {
30
+ if (!isNodeError(error) || error.code !== "ENOENT") throw error;
31
+ }
32
+ const tmpPath = `${path}.${randomUUID()}.tmp`;
33
+ try {
34
+ await writeFile(tmpPath, content, { mode: existingMode });
35
+ if (existingMode !== void 0) await chmod(tmpPath, existingMode);
36
+ await rename(tmpPath, path);
37
+ } catch (error) {
38
+ await rm(tmpPath, { force: true }).catch(() => void 0);
39
+ throw error;
40
+ }
41
+ }
42
+
43
+ // src/internal/executor-startup.ts
44
+ var EXECUTOR_START_TIMEOUT_MS = 6e4;
45
+ var NornExecutorStartup = class {
46
+ resultPath;
47
+ diagnosticsPath;
48
+ constructor(input) {
49
+ const request = input.request;
50
+ const identity = request.type === "run" ? request.id : request.requestId ?? request.createdAt;
51
+ const attempt = createHash("sha256").update(`${request.type}:${identity}`).digest("hex");
52
+ this.resultPath = join(input.runRoot, "executor", `${attempt}.json`);
53
+ this.diagnosticsPath = join(input.runRoot, "executor", `${attempt}.log`);
54
+ }
55
+ async readResult() {
56
+ try {
57
+ const result = JSON.parse(
58
+ await readFile(this.resultPath, "utf8")
59
+ );
60
+ if (result.status !== "starting" && result.status !== "ready" && result.status !== "failed" || typeof result.at !== "string" || Number.isNaN(Date.parse(result.at)) || result.status === "failed" && typeof result.error !== "string")
61
+ throw new Error(`Invalid executor startup result: ${this.resultPath}`);
62
+ return result;
63
+ } catch (error) {
64
+ if (isNodeError(error) && error.code === "ENOENT") return void 0;
65
+ throw error;
66
+ }
67
+ }
68
+ async begin() {
69
+ await writeJsonAtomically(this.resultPath, {
70
+ status: "starting",
71
+ at: (/* @__PURE__ */ new Date()).toISOString()
72
+ });
73
+ }
74
+ async acknowledge() {
75
+ const result = await this.readResult();
76
+ if (result?.status === "failed") throw new Error(result.error);
77
+ await writeJsonAtomically(this.resultPath, {
78
+ status: "ready",
79
+ at: (/* @__PURE__ */ new Date()).toISOString()
80
+ });
81
+ }
82
+ async fail(error) {
83
+ await writeJsonAtomically(this.resultPath, {
84
+ status: "failed",
85
+ at: (/* @__PURE__ */ new Date()).toISOString(),
86
+ error
87
+ });
88
+ }
89
+ };
90
+ var NornExecutorStartupError = class extends Error {
91
+ constructor(details) {
92
+ super(
93
+ `Executor startup failed for run ${details.runId}: ${details.reason}. Diagnostics: ${details.diagnosticsPath}`
94
+ );
95
+ this.details = details;
96
+ }
97
+ details;
98
+ };
99
+ export {
100
+ EXECUTOR_START_TIMEOUT_MS,
101
+ NornExecutorStartup,
102
+ NornExecutorStartupError
103
+ };
@@ -22,6 +22,7 @@ export type NornRunResumeRequest = {
22
22
  };
23
23
  export declare function writeRunLaunchRequest(runRoot: string, request: NornRunLaunchRequest): Promise<void>;
24
24
  export declare function readRunLaunchRequest(runRoot: string): Promise<NornRunLaunchRequest>;
25
+ export declare function readOptionalRunLaunchRequest(runRoot: string): Promise<NornRunLaunchRequest | undefined>;
25
26
  export declare function writeRunResumeRequest(runRoot: string, request: NornRunResumeRequest): Promise<void>;
26
27
  export declare function readRunResumeRequest(runRoot: string): Promise<NornRunResumeRequest>;
27
28
  export declare function readOptionalRunResumeRequest(runRoot: string): Promise<NornRunResumeRequest | undefined>;
@@ -52,6 +52,14 @@ async function readRunLaunchRequest(runRoot) {
52
52
  JSON.parse(await readFile(join(runRoot, LAUNCH_REQUEST_FILE_NAME), "utf8"))
53
53
  );
54
54
  }
55
+ async function readOptionalRunLaunchRequest(runRoot) {
56
+ try {
57
+ return await readRunLaunchRequest(runRoot);
58
+ } catch (error) {
59
+ if (isNodeError(error) && error.code === "ENOENT") return void 0;
60
+ throw error;
61
+ }
62
+ }
55
63
  async function writeRunResumeRequest(runRoot, request) {
56
64
  const path = join(runRoot, RESUME_REQUEST_FILE_NAME);
57
65
  const stagedPath = `${path}.${randomUUID2()}.tmp`;
@@ -123,6 +131,7 @@ export {
123
131
  RESUME_START_GRACE_MS,
124
132
  clearRunResumeRequest,
125
133
  matchesRunResumeRequest,
134
+ readOptionalRunLaunchRequest,
126
135
  readOptionalRunResumeRequest,
127
136
  readRunLaunchRequest,
128
137
  readRunResumeRequest,
@@ -2,7 +2,8 @@
2
2
  import { readFile } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { runCurrentRoot } from "./run-store.js";
5
- import { RUN_STATE_FILE_NAME } from "./run-state.js";
5
+ import { getRunInfo, RUN_STATE_FILE_NAME } from "./run-state.js";
6
+ import { readOptionalRunLaunchRequest } from "./launch-request.js";
6
7
 
7
8
  // ../core/src/errors.ts
8
9
  function isNodeError(error) {
@@ -18,11 +19,17 @@ import {
18
19
  var LEGACY_RUN_STATE_FILE_NAME = "runtime-state.json";
19
20
  async function readRunMetrics(runRoot) {
20
21
  const currentRoot = runCurrentRoot(runRoot);
21
- const [state, events] = await Promise.all([
22
- readRunStateSnapshot(currentRoot),
23
- readManifestEvents(join(currentRoot, "manifest.json"))
24
- ]);
25
- return calculateRunMetrics(events, state, /* @__PURE__ */ new Date());
22
+ try {
23
+ const [state, events] = await Promise.all([
24
+ readRunStateSnapshot(currentRoot),
25
+ readManifestEvents(join(currentRoot, "manifest.json"))
26
+ ]);
27
+ return calculateRunMetrics(events, state, /* @__PURE__ */ new Date());
28
+ } catch (error) {
29
+ if (!isNodeError(error) || error.code !== "ENOENT" || !await readOptionalRunLaunchRequest(runRoot))
30
+ throw error;
31
+ return calculateRunMetrics([], await getRunInfo(runRoot), /* @__PURE__ */ new Date());
32
+ }
26
33
  }
27
34
  function calculateRunMetrics(events, state, now) {
28
35
  const startedAt = state.startedAt ?? firstEventTime(events) ?? now.toISOString();
@@ -193,7 +193,16 @@ function parseRunLeaseOwner(value) {
193
193
  };
194
194
  }
195
195
  function isRunLeaseStale(owner) {
196
- return Date.now() - Date.parse(owner.heartbeatAt) > HEARTBEAT_TTL_MS;
196
+ if (Date.now() - Date.parse(owner.heartbeatAt) > HEARTBEAT_TTL_MS)
197
+ return true;
198
+ if (owner.pid > 0) {
199
+ try {
200
+ process.kill(owner.pid, 0);
201
+ } catch (error) {
202
+ if (isNodeError(error) && error.code === "ESRCH") return true;
203
+ }
204
+ }
205
+ return false;
197
206
  }
198
207
  async function isRunLeaseDirectoryStale(lockRoot) {
199
208
  try {
@@ -10,10 +10,15 @@ function isNodeError(error) {
10
10
  // src/internal/run-state.ts
11
11
  import {
12
12
  readRunLaunchRequest,
13
+ readOptionalRunLaunchRequest,
13
14
  readOptionalRunResumeRequest,
14
15
  RESUME_START_GRACE_MS
15
16
  } from "./launch-request.js";
16
17
  import { getRunLeaseHealth } from "./run-lease.js";
18
+ import {
19
+ EXECUTOR_START_TIMEOUT_MS,
20
+ NornExecutorStartup
21
+ } from "./executor-startup.js";
17
22
 
18
23
  // ../core/src/atomic-files.ts
19
24
  import { randomUUID } from "node:crypto";
@@ -299,6 +304,17 @@ async function getRunInfo(runRoot) {
299
304
  if (state.version === RUN_STATE_VERSION && pendingResume.status === "rejected")
300
305
  throw pendingResume.error;
301
306
  const resumeRequest = state.version === RUN_STATE_VERSION && pendingResume.status === "fulfilled" ? pendingResume.request : void 0;
307
+ const launchRequest = state.status === "running" ? await readOptionalRunLaunchRequest(runRoot) : void 0;
308
+ const startup = launchRequest ? await new NornExecutorStartup({
309
+ runRoot,
310
+ request: launchRequest
311
+ }).readResult() : void 0;
312
+ const startupFailure = startup?.status === "failed" ? {
313
+ workflowId: state.entrypointWorkflowId,
314
+ error: startup.error,
315
+ failedAt: startup.at
316
+ } : void 0;
317
+ const isStartupOverdue = startup?.status === "starting" && Date.now() - Date.parse(startup.at) >= EXECUTOR_START_TIMEOUT_MS;
302
318
  return {
303
319
  version: state.version,
304
320
  id: state.id,
@@ -310,13 +326,13 @@ async function getRunInfo(runRoot) {
310
326
  },
311
327
  entrypointWorkflowId: state.entrypointWorkflowId,
312
328
  currentWorkflowId: state.current?.workflowId,
313
- status: resumeRequest ? "running" : state.status,
314
- health: resumeRequest ? await getPendingResumeHealth(runRoot, resumeRequest.createdAt) : await runHealth(runRoot, state.status),
329
+ status: startupFailure ? "failed" : resumeRequest ? "running" : state.status,
330
+ health: startupFailure || isStartupOverdue ? "unhealthy" : resumeRequest ? await getPendingResumeHealth(runRoot, resumeRequest) : await runHealth(runRoot, state.status),
315
331
  interruption: resumeRequest ? void 0 : runInterruption(state),
316
332
  outcome: runOutcome(state),
317
- failed: runFailure(state),
333
+ failed: startupFailure ?? runFailure(state),
318
334
  startedAt: state.startedAt,
319
- updatedAt: state.updatedAt
335
+ updatedAt: startupFailure?.failedAt ?? state.updatedAt
320
336
  };
321
337
  } catch (error) {
322
338
  if (!isNodeError(error) || error.code !== "ENOENT") throw error;
@@ -345,9 +361,15 @@ function runRootCandidates(sessionCwd, run) {
345
361
  candidates.push(resolve(sessionCwd, ".norn", "runs", run));
346
362
  return Array.from(new Set(candidates));
347
363
  }
348
- async function getPendingResumeHealth(runRoot, requestedAt) {
364
+ async function getPendingResumeHealth(runRoot, request) {
365
+ const startup = await new NornExecutorStartup({
366
+ runRoot,
367
+ request
368
+ }).readResult();
369
+ if (startup?.status === "failed" || startup?.status === "starting" && Date.now() - Date.parse(startup.at) >= EXECUTOR_START_TIMEOUT_MS)
370
+ return "unhealthy";
349
371
  const health = await getRunLeaseHealth(runRoot);
350
- return health === "healthy" || Date.now() - Date.parse(requestedAt) < RESUME_START_GRACE_MS ? "healthy" : "unhealthy";
372
+ return health === "healthy" || Date.now() - Date.parse(request.createdAt) < RESUME_START_GRACE_MS ? "healthy" : "unhealthy";
351
373
  }
352
374
  async function runHealth(runRoot, status) {
353
375
  if (status !== "running") return "healthy";
@@ -363,6 +385,15 @@ async function readNornRunInfo(runRoot) {
363
385
  }
364
386
  async function getLaunchedRunInfo(runRoot) {
365
387
  const launchRequest = await readRunLaunchRequest(runRoot);
388
+ const startup = await new NornExecutorStartup({
389
+ runRoot,
390
+ request: launchRequest
391
+ }).readResult();
392
+ const failure = startup?.status === "failed" ? {
393
+ workflowId: launchRequest.workflowId,
394
+ error: startup.error,
395
+ failedAt: startup.at
396
+ } : void 0;
366
397
  return {
367
398
  version: launchRequest.version,
368
399
  id: launchRequest.id,
@@ -374,10 +405,11 @@ async function getLaunchedRunInfo(runRoot) {
374
405
  },
375
406
  entrypointWorkflowId: launchRequest.workflowId,
376
407
  currentWorkflowId: launchRequest.workflowId,
377
- status: "running",
378
- health: "healthy",
408
+ status: failure ? "failed" : "running",
409
+ health: failure || Date.now() - Date.parse(launchRequest.createdAt) >= EXECUTOR_START_TIMEOUT_MS ? "unhealthy" : "healthy",
410
+ failed: failure,
379
411
  startedAt: launchRequest.createdAt,
380
- updatedAt: launchRequest.createdAt
412
+ updatedAt: failure?.failedAt ?? launchRequest.createdAt
381
413
  };
382
414
  }
383
415
  async function readRunStateJson(currentRoot) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vimhead.dev/norn-cli",
3
- "version": "0.1.0-tip.37310387847.1",
3
+ "version": "0.1.0-tip.43698584363.1",
4
4
  "description": "Harness-agnostic runtime for agent-driven and code-driven workflows, built primarily for agents.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,10 +40,10 @@
40
40
  "@vimhead.dev/norn-core": "0.0.0"
41
41
  },
42
42
  "dependencies": {
43
- "@earendil-works/pi-coding-agent": "1.0.3",
43
+ "@earendil-works/pi-coding-agent": "1.5.2",
44
+ "@vimhead.dev/norn": "0.1.0-tip.43698584363.1",
44
45
  "jiti": "^2.7.0",
45
- "typebox": "^1.3.34",
46
- "@vimhead.dev/norn": "0.1.0-tip.37310387847.1"
46
+ "typebox": "^1.4.7"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "node ../../scripts/build-package.ts"