@kevlns/art-workshop 0.3.3 → 0.3.4
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/lib/analysis-worker.mjs +2 -1
- package/lib/lifecycle.mjs +2 -3
- package/lib/process-identity.mjs +14 -0
- package/lib/service.mjs +2 -13
- package/lib/task-worker.mjs +2 -0
- package/lib/tasks.mjs +10 -8
- package/lib/worker-process.mjs +17 -0
- package/package.json +1 -1
package/lib/analysis-worker.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {processIdentity} from './process-identity.mjs';
|
|
1
2
|
import {existsSync} from 'node:fs';
|
|
2
3
|
import {join} from 'node:path';
|
|
3
4
|
import {readJson,writeJson,hash} from './shared.mjs';
|
|
@@ -10,7 +11,7 @@ import {extractText} from './bl.mjs';
|
|
|
10
11
|
const file=process.argv[2],task=readJson(file);
|
|
11
12
|
const persist=()=>{task.heartbeat=new Date().toISOString();writeJson(file,task);};
|
|
12
13
|
const cancelled=()=>existsSync(file+'.cancel')||existsSync(join(task.root,'shutdown.json'));
|
|
13
|
-
task.workerPid=process.pid;task.status='running';persist();const heartbeat=setInterval(persist,1000);
|
|
14
|
+
task.workerPid=process.pid;task.workerIdentity=processIdentity(process.pid);task.status='running';persist();const heartbeat=setInterval(persist,1000);
|
|
14
15
|
let responseIndex=0;
|
|
15
16
|
try{
|
|
16
17
|
const analyzer=task.objectKind==='task'?analyzeTaskReferences:analyzeStyle;
|
package/lib/lifecycle.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {workerAlive} from './worker-process.mjs';
|
|
1
2
|
/** Execution completion is separate from proof that all side effects have stopped. */
|
|
2
3
|
export const RUN_STATES = Object.freeze([
|
|
3
4
|
"queued",
|
|
@@ -19,9 +20,7 @@ export function analysisLocallyStopped(task) {
|
|
|
19
20
|
!Number.isFinite(Date.parse(task.finishedAt)) ||
|
|
20
21
|
!Number.isSafeInteger(task.workerPid) || task.workerPid <= 0 ||
|
|
21
22
|
task.plan || task.promptId || Object.keys(task.remoteExecutions ?? {}).length) return false;
|
|
22
|
-
|
|
23
|
-
return error.code === 'ESRCH';
|
|
24
|
-
}
|
|
23
|
+
return !workerAlive(task);
|
|
25
24
|
}
|
|
26
25
|
export function executionSettled(task) {
|
|
27
26
|
if (analysisLocallyStopped(task)) return true;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {execFileSync} from 'node:child_process';
|
|
2
|
+
export function processIdentity(pid) {
|
|
3
|
+
if(!Number.isSafeInteger(pid)||pid<1)return null;
|
|
4
|
+
try {
|
|
5
|
+
if(process.platform==='win32') {
|
|
6
|
+
const script=`$p=Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}"; if($p){[pscustomobject]@{pid=$p.ProcessId;exe=$p.ExecutablePath;created=$p.CreationDate.ToUniversalTime().ToString('o');command=$p.CommandLine}|ConvertTo-Json -Compress}`;
|
|
7
|
+
const output=execFileSync('powershell.exe',['-NoProfile','-NonInteractive','-Command',script],{encoding:'utf8',windowsHide:true}).trim();
|
|
8
|
+
return output?JSON.parse(output):null;
|
|
9
|
+
}
|
|
10
|
+
const created=execFileSync('ps',['-p',String(pid),'-o','lstart='],{encoding:'utf8'}).trim();
|
|
11
|
+
const command=execFileSync('ps',['-p',String(pid),'-o','args='],{encoding:'utf8'}).trim();
|
|
12
|
+
return created?{pid,created,command}:null;
|
|
13
|
+
}catch{return null;}
|
|
14
|
+
}
|
package/lib/service.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {processIdentity} from './process-identity.mjs';
|
|
2
|
+
export {processIdentity} from './process-identity.mjs';
|
|
1
3
|
import {homedir} from 'node:os';
|
|
2
4
|
import {spawn,execFileSync} from 'node:child_process';
|
|
3
5
|
import {openSync,closeSync,existsSync,readdirSync} from 'node:fs';
|
|
@@ -7,19 +9,6 @@ import {writeJson,hash} from './shared.mjs';
|
|
|
7
9
|
import {http,sleep,locked,stateRead,alive} from './runtime.mjs';
|
|
8
10
|
import {inspectEnvironment,workflowError} from './diagnostics.mjs';
|
|
9
11
|
|
|
10
|
-
export function processIdentity(pid) {
|
|
11
|
-
if(!Number.isSafeInteger(pid)||pid<1)return null;
|
|
12
|
-
try {
|
|
13
|
-
if(process.platform==='win32') {
|
|
14
|
-
const script=`$p=Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}"; if($p){[pscustomobject]@{pid=$p.ProcessId;exe=$p.ExecutablePath;created=$p.CreationDate.ToUniversalTime().ToString('o');command=$p.CommandLine}|ConvertTo-Json -Compress}`;
|
|
15
|
-
const output=execFileSync('powershell.exe',['-NoProfile','-NonInteractive','-Command',script],{encoding:'utf8',windowsHide:true}).trim();
|
|
16
|
-
return output?JSON.parse(output):null;
|
|
17
|
-
}
|
|
18
|
-
const created=execFileSync('ps',['-p',String(pid),'-o','lstart='],{encoding:'utf8'}).trim();
|
|
19
|
-
const command=execFileSync('ps',['-p',String(pid),'-o','args='],{encoding:'utf8'}).trim();
|
|
20
|
-
return created?{pid,created,command}:null;
|
|
21
|
-
}catch{return null;}
|
|
22
|
-
}
|
|
23
12
|
export async function stopIdentity(identity,{timeout=10000}={}) {
|
|
24
13
|
const current=processIdentity(identity.pid);
|
|
25
14
|
if(!current)return;
|
package/lib/task-worker.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {processIdentity} from './process-identity.mjs';
|
|
1
2
|
import { existsSync, readdirSync, renameSync, readFileSync } from "node:fs";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { readJson, writeJson, hash } from "./shared.mjs";
|
|
@@ -27,6 +28,7 @@ const persist = () => {
|
|
|
27
28
|
writeJson(file, task);
|
|
28
29
|
};
|
|
29
30
|
task.workerPid = process.pid;
|
|
31
|
+
task.workerIdentity = processIdentity(process.pid);
|
|
30
32
|
task.status = "running";
|
|
31
33
|
persist();
|
|
32
34
|
const heartbeat = setInterval(persist, 1000);
|
package/lib/tasks.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {workerAlive} from './worker-process.mjs';
|
|
1
2
|
import { spawn } from "node:child_process";
|
|
2
3
|
import { mkdirSync, openSync, closeSync, existsSync, rmSync } from "node:fs";
|
|
3
4
|
import { join } from "node:path";
|
|
@@ -51,7 +52,7 @@ export function taskView(task) {
|
|
|
51
52
|
canResume:
|
|
52
53
|
terminal(task.status) &&
|
|
53
54
|
task.status !== "succeeded" &&
|
|
54
|
-
!
|
|
55
|
+
!workerAlive(task) &&
|
|
55
56
|
Object.values(task.remoteExecutions ?? {}).some((receipt) =>
|
|
56
57
|
["accepted", "completed"].includes(receipt.state),
|
|
57
58
|
) &&
|
|
@@ -95,7 +96,7 @@ export function taskStatus(root, taskId) {
|
|
|
95
96
|
if (
|
|
96
97
|
!terminal(task.status) &&
|
|
97
98
|
Date.now() - Date.parse(task.heartbeat ?? task.createdAt) > 10000 &&
|
|
98
|
-
!
|
|
99
|
+
!workerAlive(task)
|
|
99
100
|
) {
|
|
100
101
|
task.status = "unknown";
|
|
101
102
|
task.stopConfirmed = false;
|
|
@@ -266,17 +267,17 @@ export async function cancelTask(root, taskId, { wait = true } = {}) {
|
|
|
266
267
|
if (terminal(task.status)) {
|
|
267
268
|
if (['style-analysis', 'task-reference-analysis'].includes(task.kind) && task.finishedAt) {
|
|
268
269
|
const deadline = Date.now() + 5000;
|
|
269
|
-
while (
|
|
270
|
+
while (workerAlive(task) && Date.now() < deadline) await sleep(50);
|
|
270
271
|
const latest = taskStatus(root, taskId);
|
|
271
272
|
if (executionSettled(latest)) return latest;
|
|
272
273
|
}
|
|
273
|
-
if (
|
|
274
|
+
if (workerAlive(task))
|
|
274
275
|
throw new Error("工作进程尚未退出,停止结果待确认");
|
|
275
276
|
if (!task.endpoint || !task.promptId)
|
|
276
277
|
throw new Error("没有可核验的后端任务身份,停止结果未知;保留资源");
|
|
277
278
|
await cancelPrompt(task.endpoint, task.promptId);
|
|
278
279
|
const latest = taskStatus(root, taskId);
|
|
279
|
-
if (
|
|
280
|
+
if (workerAlive(latest) || latest.promptId !== task.promptId)
|
|
280
281
|
throw new Error("执行身份已变化,请重新查询");
|
|
281
282
|
latest.promptId = null;
|
|
282
283
|
latest.unsettled = false;
|
|
@@ -327,7 +328,7 @@ export async function shutdown(settings, { closeServices = true } = {}) {
|
|
|
327
328
|
// A final heartbeat may precede worker exit by a few milliseconds.
|
|
328
329
|
const exitDeadline = Date.now() + 5000;
|
|
329
330
|
while (
|
|
330
|
-
tasks(root).some((t) =>
|
|
331
|
+
tasks(root).some((t) => workerAlive(t)) &&
|
|
331
332
|
Date.now() < exitDeadline
|
|
332
333
|
)
|
|
333
334
|
await sleep(50);
|
|
@@ -335,7 +336,7 @@ export async function shutdown(settings, { closeServices = true } = {}) {
|
|
|
335
336
|
if (
|
|
336
337
|
tasks(root).some(
|
|
337
338
|
(t) =>
|
|
338
|
-
!executionSettled(taskStatus(root, t.taskId)) ||
|
|
339
|
+
!executionSettled(taskStatus(root, t.taskId)) || workerAlive(t),
|
|
339
340
|
)
|
|
340
341
|
)
|
|
341
342
|
throw new Error("任务尚未停止,保留服务:" + failures.join("; "));
|
|
@@ -401,7 +402,7 @@ export async function resumeTask(root, taskId) {
|
|
|
401
402
|
return locked(root, "control", async () => {
|
|
402
403
|
const task = taskStatus(root, taskId),
|
|
403
404
|
file = taskFile(root, taskId);
|
|
404
|
-
if (
|
|
405
|
+
if (workerAlive(task) || !terminal(task.status))
|
|
405
406
|
throw new Error("原工作进程仍在执行,不能恢复");
|
|
406
407
|
validatePlan(task.plan);
|
|
407
408
|
if (executionSettled(task) && task.status === "succeeded")
|
|
@@ -440,6 +441,7 @@ export async function resumeTask(root, taskId) {
|
|
|
440
441
|
task.finishedAt = null;
|
|
441
442
|
task.heartbeat = new Date().toISOString();
|
|
442
443
|
task.workerPid = null;
|
|
444
|
+
task.workerIdentity = null;
|
|
443
445
|
rmSync(file + ".cancel", { force: true });
|
|
444
446
|
if (gate) rmSync(join(root, "shutdown.json"), { force: true });
|
|
445
447
|
writeJson(file, task);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {processIdentity} from './process-identity.mjs';
|
|
2
|
+
import {hash} from './shared.mjs';
|
|
3
|
+
|
|
4
|
+
// A recycled PID is not evidence that an old task is still running.
|
|
5
|
+
// Unreadable identities remain live: uncertainty never authorizes termination.
|
|
6
|
+
export function workerAlive(task, inspect = processIdentity) {
|
|
7
|
+
const pid = task.workerPid;
|
|
8
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) return false;
|
|
9
|
+
try { process.kill(pid, 0); } catch (error) { return error.code !== 'ESRCH'; }
|
|
10
|
+
const current = inspect(pid);
|
|
11
|
+
if (!current) return true;
|
|
12
|
+
if (task.workerIdentity) return hash(current) === hash(task.workerIdentity);
|
|
13
|
+
// Historical receipts have no identity. A process born after the completed
|
|
14
|
+
// task cannot be its worker; allow one second for Unix ps time precision.
|
|
15
|
+
const finished = Date.parse(task.finishedAt), created = Date.parse(current.created);
|
|
16
|
+
return !(Number.isFinite(finished) && Number.isFinite(created) && created > finished + 1000);
|
|
17
|
+
}
|