@infodb/p3 0.8.0 → 0.10.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 +4 -2
- package/dist/web/assets/{Outputs-CQi0oba1.js → Outputs-B4Tr7JzU.js} +1 -1
- package/dist/web/assets/{RequestStatus-grC0F_SQ.js → RequestStatus-DGaoQoIa.js} +1 -1
- package/dist/web/assets/{Tabs-BJRGzidj.js → Tabs-D16MqQxu.js} +1 -1
- package/dist/web/assets/{TagInput-cIIwXAHA.js → TagInput-pSYxm4bz.js} +1 -1
- package/dist/web/assets/{TaskDrawer-CSZ7Z0JC.js → TaskDrawer-1Ev3RpRV.js} +1 -1
- package/dist/web/assets/{_requestId-Z3CBhTIL.js → _requestId-BKhn019m.js} +1 -1
- package/dist/web/assets/_requestId-Btpvuu7-.js +30 -0
- package/dist/web/assets/{appCtx-DCiGJFAe.js → appCtx-D_Iaoh2e.js} +1 -1
- package/dist/web/assets/{board-DyfGX5h0.js → board-DeHdxbN4.js} +1 -1
- package/dist/web/assets/{directTask-Dk3H5Q2B.js → directTask-C1NWDkZp.js} +1 -1
- package/dist/web/assets/{done-Bhxqy7bH.js → done-BFaHGt03.js} +1 -1
- package/dist/web/assets/{index-Bt13tvIX.js → index-CDWcKy1L.js} +3 -3
- package/dist/web/assets/{new-BRsIUNIb.js → new-ChFzBoL2.js} +1 -1
- package/dist/web/assets/{preload-helper-B40dBmwL.js → preload-helper-hSId95YD.js} +1 -1
- package/dist/web/assets/{release-notes-C3FdiM36.js → release-notes-BWNDFDkH.js} +1 -1
- package/dist/web/assets/{requestData-w6fhCYmh.js → requestData-B_320uxL.js} +1 -1
- package/dist/web/assets/{routes-QNgQHe48.js → routes-BBD464IS.js} +1 -1
- package/dist/web/assets/{settings-CoKJahEe.js → settings-D7PPrGjT.js} +1 -1
- package/dist/web/index.html +3 -3
- package/docs/architecture.md +95 -6
- package/docs/development.md +21 -5
- package/docs/how-to-use.md +99 -1
- package/docs/installation.md +30 -12
- package/docs/publishing.md +5 -1
- package/lib/cli/args.js +5 -0
- package/lib/cli/help.js +10 -1
- package/lib/cli/main.js +12 -3
- package/lib/cli/preflightCommands.js +98 -3
- package/lib/cli/project.js +31 -8
- package/lib/core/context.js +1 -1
- package/lib/core/preflight/codexEngine.js +63 -220
- package/lib/core/preflight/codexImage.js +49 -19
- package/lib/core/preflight/docker.js +149 -0
- package/lib/core/preflight/instructions.js +263 -0
- package/lib/core/preflight/owner.js +118 -0
- package/lib/core/preflight/render-image/Dockerfile +1 -1
- package/lib/core/preflight/service.js +516 -84
- package/lib/core/preflight/uiRender.js +1 -1
- package/lib/core/projectRegistry.js +2 -1
- package/lib/core/store/dataWatcher.js +122 -0
- package/lib/core/store/normalize.js +9 -1
- package/lib/core/store/preflightStore.js +20 -2
- package/lib/core/store/projectFile.js +6 -1
- package/lib/core/store/workspace.js +20 -37
- package/lib/server/api/manifest.js +10 -1
- package/lib/server/api/routes.js +18 -5
- package/lib/server/server.js +3 -1
- package/lib/shared/directTask.js +2 -2
- package/lib/shared/preflightRunner.js +18 -0
- package/lib/shared/scopeView.js +3 -2
- package/package.json +2 -2
- package/skills/p3/SKILL.md +45 -7
- package/dist/web/assets/_requestId-C8WYk2kC.js +0 -30
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
1
|
+
import { lstat, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { isAbsolute, join, relative } from "node:path";
|
|
4
4
|
import { createHash, randomBytes } from "node:crypto";
|
|
5
5
|
import {} from "../../shared/types.js";
|
|
6
6
|
import { normalizeTaskId, REQUEST_ID } from "../../shared/refs.js";
|
|
7
|
-
import { newRequestId, nowIso, writeFileAtomic } from "../store/fsutil.js";
|
|
7
|
+
import { newRequestId, nowIso, withFileLock, writeFileAtomic } from "../store/fsutil.js";
|
|
8
8
|
import { ConflictError, NotFoundError } from "../store/collection.js";
|
|
9
9
|
import { supersedeCandidates } from "../supersede.js";
|
|
10
10
|
import { hierarchyNow, reopenAncestors } from "../requestStatus.js";
|
|
@@ -12,6 +12,9 @@ import { archivedRequestIds, inheritFrom, withHierarchyLock } from "../hierarchy
|
|
|
12
12
|
import { childBody, childrenVersion, draftProblems, parseSplit, splitMarker } from "./split.js";
|
|
13
13
|
import { redact } from "../redact.js";
|
|
14
14
|
import { renderCarryover, selectCarryover } from "./carryover.js";
|
|
15
|
+
import { LOCAL_INSTRUCTIONS_FILE, LOCAL_OUTPUTS, localInstructions } from "./instructions.js";
|
|
16
|
+
import { renderManuscripts } from "./uiRender.js";
|
|
17
|
+
import { repoTargetProblem } from "../../shared/workTarget.js";
|
|
15
18
|
import { refScope } from "../repoRefs.js";
|
|
16
19
|
import { checkRepoTarget, planWorkstreams, workstreamPatch } from "../workTargets.js";
|
|
17
20
|
import { hideConfigPath } from "../userConfig.js";
|
|
@@ -20,6 +23,7 @@ import { applyUiShortage, IMAGE_TYPES, loadUiMaterials, readUiImage, UI_DIR, UiM
|
|
|
20
23
|
import { compareFamily, compareInput, inputHash, normalizeInput, renderRequestSnapshot, runInput, StaleInputError } from "./input.js";
|
|
21
24
|
import { changedLabel } from "../../shared/preflightInput.js";
|
|
22
25
|
import { PREFLIGHT_CANCELLED } from "../../shared/directTask.js";
|
|
26
|
+
import { currentOwner, ownerState, systemProbe } from "./owner.js";
|
|
23
27
|
/** 始められない理由をそのまま失敗にするエンジン(run の記録とログに残すため) */
|
|
24
28
|
const failing = (name, message) => ({
|
|
25
29
|
name,
|
|
@@ -58,15 +62,23 @@ export class PulseInputError extends Error {
|
|
|
58
62
|
export class PreflightService {
|
|
59
63
|
ws;
|
|
60
64
|
engines;
|
|
65
|
+
renderOpts;
|
|
66
|
+
ownerOpts;
|
|
61
67
|
active = new Map();
|
|
62
68
|
/** 最後に分かった既定のエンジン */
|
|
63
69
|
lastDefault;
|
|
64
70
|
/**
|
|
65
71
|
* defaultEngine: 決まった名前か、読むたびに今の既定を返す関数(serve: 個人設定の preflight.engine を Preflight を始めるたびに読む)
|
|
66
72
|
*/
|
|
67
|
-
constructor(ws, engines, defaultEngine
|
|
73
|
+
constructor(ws, engines, defaultEngine,
|
|
74
|
+
/** 手元のエージェントの取り込みで画面イメージを描くとき: パスの対応(codex.pathMap)と、テスト用の描画の差し替え */
|
|
75
|
+
renderOpts = { pathMap: async () => [] },
|
|
76
|
+
/** 実行しているプロセスの判定(テストが差し替える。owner.ts) */
|
|
77
|
+
ownerOpts = {}) {
|
|
68
78
|
this.ws = ws;
|
|
69
79
|
this.engines = engines;
|
|
80
|
+
this.renderOpts = renderOpts;
|
|
81
|
+
this.ownerOpts = ownerOpts;
|
|
70
82
|
if (typeof defaultEngine === "string") {
|
|
71
83
|
this.lastDefault = defaultEngine;
|
|
72
84
|
this.loadDefault = async () => defaultEngine;
|
|
@@ -120,6 +132,311 @@ export class PreflightService {
|
|
|
120
132
|
return note;
|
|
121
133
|
}
|
|
122
134
|
async start(requestId, opts = {}) {
|
|
135
|
+
return this.locked(requestId, async () => {
|
|
136
|
+
const fixed = await this.fix(requestId);
|
|
137
|
+
// 既定のエンジンは始めるたびに個人設定から決める。読めなければ、この run を設定のエラーとして失敗にする
|
|
138
|
+
// (古い設定や Mock に落とさない。serve とほかの操作は続ける)
|
|
139
|
+
let engine;
|
|
140
|
+
if (opts.engine)
|
|
141
|
+
engine = this.engines[opts.engine];
|
|
142
|
+
else {
|
|
143
|
+
try {
|
|
144
|
+
engine = this.engines[await this.currentDefault()];
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
const message = `個人設定を読めないので Preflight を始めません(Docker は起動していません): ${hideConfigPath(e)}`;
|
|
148
|
+
engine = failing(this.lastDefault, message);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!engine)
|
|
152
|
+
throw new Error(`engine ${opts.engine} は利用できません`);
|
|
153
|
+
// 始めるときの設定に決める(この run の間は個人設定が変わっても使い続ける)
|
|
154
|
+
if (engine.snapshot) {
|
|
155
|
+
const from = engine;
|
|
156
|
+
engine = await from.snapshot().catch((e) => failing(from.name, e.message));
|
|
157
|
+
}
|
|
158
|
+
const opened = await this.open(fixed, engine.name, opts.actor);
|
|
159
|
+
const abort = new AbortController();
|
|
160
|
+
// 別のプロセス(CLI・別の serve)からの中断の依頼(cancel-request.json)を見て止める
|
|
161
|
+
const watch = setInterval(() => {
|
|
162
|
+
void this.ws.preflight.cancelRequest(requestId, opened.run.id).then((r) => {
|
|
163
|
+
if (r && !abort.signal.aborted) {
|
|
164
|
+
this.log(opened.run, { kind: "info", text: `中断を頼まれたので止めます${r.by ? `(${r.by})` : ""}` });
|
|
165
|
+
abort.abort();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}, CANCEL_POLL_MS);
|
|
169
|
+
watch.unref();
|
|
170
|
+
const done = this.execute(engine, opened, abort.signal).finally(() => clearInterval(watch));
|
|
171
|
+
this.active.set(opened.run.id, { run: opened.run, abort, done });
|
|
172
|
+
return opened.run;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 手元の対話エージェント(Claude Code など)に評価を頼む準備: 評価対象・参照先・親子・添付・メモ・前回の結果を通常の Preflight と
|
|
177
|
+
* 同じ方法で固定し、指示のファイル(instructions.md)を run のフォルダに書いて、評価を待つ run(engine: local・running)を残して返す。
|
|
178
|
+
* モデル・Codex・Docker は使わない。取り込みは complete、やめるのは cancelLocal(別のプロセスからでもよい)
|
|
179
|
+
*/
|
|
180
|
+
async prepare(requestId, opts = {}) {
|
|
181
|
+
return this.locked(requestId, async () => {
|
|
182
|
+
const opened = await this.open(await this.fix(requestId), "local", opts.actor);
|
|
183
|
+
const { run, runDir } = opened;
|
|
184
|
+
run.local = { before: opened.before };
|
|
185
|
+
await this.extractAttachments(run, runDir);
|
|
186
|
+
const instructions = join(runDir, LOCAL_INSTRUCTIONS_FILE);
|
|
187
|
+
await writeFileAtomic(instructions, localInstructions(this.job(opened)));
|
|
188
|
+
this.log(run, { kind: "info", text: `手元のエージェントの評価を待っています(指示: ${LOCAL_INSTRUCTIONS_FILE}。取り込みは p3 preflight ${requestId} --complete --run ${run.id})` });
|
|
189
|
+
await this.ws.preflight.save(run);
|
|
190
|
+
this.ws.emit({ type: "changed", kind: "preflight", request: requestId });
|
|
191
|
+
return { run, runDir, instructions, outputs: LOCAL_OUTPUTS.map((o) => join(runDir, o)) };
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 手元のエージェントが書いた結果を取り込む。指定した run(engine: local で評価を待っているもの)の成果物だけを確かめ、通常の Preflight と
|
|
196
|
+
* 同じ取り込み(評価・案・分割・根拠・画面資料)で完了にする。成果物に問題があれば何も確定せず LocalResultError(直して同じ run で
|
|
197
|
+
* やり直せる)。取り込み済みなら何もせずに返す(already)
|
|
198
|
+
*/
|
|
199
|
+
async complete(requestId, runId) {
|
|
200
|
+
return this.locked(requestId, async () => {
|
|
201
|
+
const run = await this.localRun(requestId, runId);
|
|
202
|
+
if (run.status === "completed")
|
|
203
|
+
return { run, already: true };
|
|
204
|
+
if (run.status !== "running") {
|
|
205
|
+
throw new LocalRunError(`${runId} は${run.error === PREFLIGHT_CANCELLED ? "取り消し済み" : "失敗した run"}です。評価し直すなら p3 preflight ${requestId} --prepare から`);
|
|
206
|
+
}
|
|
207
|
+
const runDir = this.ws.preflight.runDir(requestId, runId);
|
|
208
|
+
const refs = this.ws.refs();
|
|
209
|
+
const problems = await localProblems(this.ws, run, runDir);
|
|
210
|
+
if (problems.length) {
|
|
211
|
+
run.local = { ...run.local, rejected: (run.local?.rejected ?? 0) + 1 };
|
|
212
|
+
for (const p of problems)
|
|
213
|
+
this.log(run, { kind: "error", text: `取り込めません: ${p}` });
|
|
214
|
+
await this.ws.preflight.save(run);
|
|
215
|
+
throw new LocalResultError(problems);
|
|
216
|
+
}
|
|
217
|
+
// 画面イメージ: 原稿があるときだけ描画のコンテナを使う。描けなくても評価は残す(取り込みで不足として扱う)
|
|
218
|
+
await renderManuscripts(runDir, { pathMap: await this.renderOpts.pathMap().catch(() => []), log: (e) => this.log(run, e), render: this.renderOpts.render });
|
|
219
|
+
await this.ingest(run, runDir, refs);
|
|
220
|
+
run.status = "completed";
|
|
221
|
+
await this.finishLocked(run, run.local.before);
|
|
222
|
+
return { run, already: false };
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* run を中断する(p3 preflight --cancel --run・画面の「中止」)。最新でなくてもよい。成果物・ログ・履歴は消さない。
|
|
227
|
+
* - local: 評価を待つのをやめる(cancelLocal)
|
|
228
|
+
* - このプロセスで実行中: 止めて、終わるまで少し待つ
|
|
229
|
+
* - 実行していたプロセスが終わっていると確かめられる: その場で中断にする(T001 の回復と同じ)
|
|
230
|
+
* - ほかのプロセスで動いている: 中断を頼む(cancel-request.json。そのプロセスが見て止める)。止まるまで少し待ち、
|
|
231
|
+
* 止まらなければ requested(中断を頼んだ。まだ running)。acknowledgeStopped は断る(動いているので)
|
|
232
|
+
* - 動いているか確かめられない(以前の run・別の環境): 中断を頼んだうえで、acknowledgeStopped(人が止まっていることを確かめた)が
|
|
233
|
+
* あるときだけ中断にする。無ければ requested と、確かめて中断にする方法を返す(確かめずに中断したことにしない)
|
|
234
|
+
* 完了した run は断る(CancelRefusedError)。中断・失敗済みなら already。プロセスやコンテナを kill しない
|
|
235
|
+
*/
|
|
236
|
+
async cancelRun(requestId, runId, opts = {}) {
|
|
237
|
+
if (!/^PF-[\w-]+$/.test(runId))
|
|
238
|
+
throw new NotFoundError(`run の ID の形ではありません: ${runId}`);
|
|
239
|
+
this.ws.requests.require(requestId);
|
|
240
|
+
const found = await this.ws.preflight.get(requestId, runId);
|
|
241
|
+
if (!found)
|
|
242
|
+
throw new NotFoundError(`${requestId} に ${runId} はありません`);
|
|
243
|
+
if (found.engine === "local") {
|
|
244
|
+
if (opts.acknowledgeStopped)
|
|
245
|
+
throw new CancelRefusedError("not_needed", `${runId} は手元のエージェントの run です(--acknowledge-stopped は要りません)`);
|
|
246
|
+
const r = await this.cancelLocal(requestId, runId);
|
|
247
|
+
return { state: r.already ? "already" : "cancelled", run: r.run, message: r.already ? `${runId} は取り消し済みです` : `${runId} を取り消しました(成果物のファイルは run のフォルダに残します)` };
|
|
248
|
+
}
|
|
249
|
+
if (found.status === "completed")
|
|
250
|
+
throw new CancelRefusedError("completed", `${runId} は完了しています(中断にはしません)`);
|
|
251
|
+
if (found.status !== "running")
|
|
252
|
+
return { state: "already", run: found, message: `${runId} は既に終わっています(${found.error ?? found.status})` };
|
|
253
|
+
const waitMs = opts.waitMs ?? CANCEL_WAIT_MS;
|
|
254
|
+
const settled = async () => {
|
|
255
|
+
const until = Date.now() + waitMs;
|
|
256
|
+
for (;;) {
|
|
257
|
+
const now = await this.ws.preflight.get(requestId, runId);
|
|
258
|
+
if (now && now.status !== "running")
|
|
259
|
+
return now;
|
|
260
|
+
if (Date.now() >= until)
|
|
261
|
+
return undefined;
|
|
262
|
+
await new Promise((r) => setTimeout(r, Math.min(200, waitMs)));
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
// このプロセスで実行中
|
|
266
|
+
const mine = this.active.get(runId);
|
|
267
|
+
if (mine) {
|
|
268
|
+
if (opts.acknowledgeStopped)
|
|
269
|
+
throw new CancelRefusedError("alive", `${runId} はこの serve で実行中です(--acknowledge-stopped は使えません。そのまま中止できます)`);
|
|
270
|
+
mine.abort.abort();
|
|
271
|
+
await Promise.race([mine.done, new Promise((r) => setTimeout(r, waitMs))]);
|
|
272
|
+
const now = await settled();
|
|
273
|
+
return now ? { state: "cancelled", run: now, message: `${runId} を中止しました` } : { state: "requested", run: (await this.ws.preflight.get(requestId, runId)) ?? found, message: `${runId} を止めています(まだ終わっていません)` };
|
|
274
|
+
}
|
|
275
|
+
const owner = ownerState(found.owner, this.probe);
|
|
276
|
+
if (owner.state === "dead") {
|
|
277
|
+
await this.locked(requestId, async () => {
|
|
278
|
+
await this.ws.requests.reload(requestId);
|
|
279
|
+
await this.recoverRequest(requestId);
|
|
280
|
+
});
|
|
281
|
+
const now = (await this.ws.preflight.get(requestId, runId)) ?? found;
|
|
282
|
+
return { state: now.status === "running" ? "requested" : "cancelled", run: now, message: `${runId} を中断にしました(${owner.reason})` };
|
|
283
|
+
}
|
|
284
|
+
if (owner.state === "alive" && opts.acknowledgeStopped) {
|
|
285
|
+
throw new CancelRefusedError("alive", `${runId} は別のプロセスで動いています(--acknowledge-stopped は使えません。--cancel だけで中断を頼めます)`);
|
|
286
|
+
}
|
|
287
|
+
if (owner.state === "unknown" && opts.acknowledgeStopped) {
|
|
288
|
+
let done = false;
|
|
289
|
+
await this.locked(requestId, async () => {
|
|
290
|
+
const run = await this.ws.preflight.get(requestId, runId);
|
|
291
|
+
if (!run || run.status !== "running")
|
|
292
|
+
return;
|
|
293
|
+
await this.ws.requests.reload(requestId);
|
|
294
|
+
run.status = "failed";
|
|
295
|
+
run.error = `中断しました: 止まっていることを人が確かめました(${owner.reason})`;
|
|
296
|
+
run.interrupted = { reason: owner.reason, at: nowIso(), acknowledgedBy: opts.actor ?? "unknown" };
|
|
297
|
+
run.ui = withCurrent({ status: "missing", reason: "評価が終わらなかったので、画面資料を取り込んでいません", targets: [], items: [], problems: [] }, run);
|
|
298
|
+
this.log(run, { kind: "error", text: `${run.error}${opts.actor ? `(${opts.actor})` : ""}` });
|
|
299
|
+
await this.finishLocked(run, run.before);
|
|
300
|
+
done = true;
|
|
301
|
+
});
|
|
302
|
+
const now = (await this.ws.preflight.get(requestId, runId)) ?? found;
|
|
303
|
+
return done
|
|
304
|
+
? { state: "cancelled", run: now, message: `${runId} を中断にしました(止まっていることを確かめた記録を残しました。プロセスやコンテナは止めていません)` }
|
|
305
|
+
: { state: "already", run: now, message: `${runId} は既に終わっています` };
|
|
306
|
+
}
|
|
307
|
+
// 動いている・確かめられない: 中断を頼む(そのプロセスが見て止める)
|
|
308
|
+
await this.ws.preflight.requestCancel(requestId, runId, opts.actor);
|
|
309
|
+
this.ws.emit({ type: "changed", kind: "preflight", request: requestId });
|
|
310
|
+
const now = await settled();
|
|
311
|
+
if (now)
|
|
312
|
+
return { state: "cancelled", run: now, message: `${runId} を中止しました` };
|
|
313
|
+
const current = (await this.ws.preflight.get(requestId, runId)) ?? found;
|
|
314
|
+
return {
|
|
315
|
+
state: "requested",
|
|
316
|
+
run: current,
|
|
317
|
+
...(owner.state === "unknown" ? { acknowledgeable: true } : {}),
|
|
318
|
+
message: owner.state === "alive"
|
|
319
|
+
? `${runId} の中断を頼みました。実行しているプロセスが止めるまで running のままです`
|
|
320
|
+
: `${runId} の中断を頼みました。動いているか確かめられません(${owner.reason})。止まっていることを確かめたら、p3 preflight ${requestId} --cancel --run ${runId} --acknowledge-stopped で中断にできます(プロセスやコンテナは止めません)`,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/** 手元のエージェントの評価を待つ run をやめる(取り込まない。成果物のファイルは残す)。取り消し済みなら何もしない */
|
|
324
|
+
async cancelLocal(requestId, runId) {
|
|
325
|
+
return this.locked(requestId, async () => {
|
|
326
|
+
const run = await this.localRun(requestId, runId);
|
|
327
|
+
if (run.status === "completed")
|
|
328
|
+
throw new LocalRunError(`${runId} は取り込み済みです(取り消せません)`);
|
|
329
|
+
if (run.status === "failed")
|
|
330
|
+
return { run, already: true };
|
|
331
|
+
run.status = "failed";
|
|
332
|
+
run.error = PREFLIGHT_CANCELLED;
|
|
333
|
+
run.ui = withCurrent({ status: "missing", reason: "評価が終わらなかったので、画面資料を取り込んでいません", targets: [], items: [], problems: [] }, run);
|
|
334
|
+
this.log(run, { kind: "error", text: run.error });
|
|
335
|
+
await this.finishLocked(run, run.local.before);
|
|
336
|
+
return { run, already: false };
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
async localRun(requestId, runId) {
|
|
340
|
+
if (!/^PF-[\w-]+$/.test(runId))
|
|
341
|
+
throw new NotFoundError(runId);
|
|
342
|
+
this.ws.requests.require(requestId);
|
|
343
|
+
const run = await this.ws.preflight.get(requestId, runId);
|
|
344
|
+
if (!run)
|
|
345
|
+
throw new NotFoundError(`${requestId}/${runId}`);
|
|
346
|
+
if (run.engine !== "local" || !run.local)
|
|
347
|
+
throw new LocalRunError(`${runId} は手元のエージェントの run ではありません(engine: ${run.engine})`);
|
|
348
|
+
return run;
|
|
349
|
+
}
|
|
350
|
+
/** 同じ要望の Preflight の開始・準備・取り込み・取消を、プロセスを跨いで1つずつにする */
|
|
351
|
+
locked(requestId, fn) {
|
|
352
|
+
if (!REQUEST_ID.test(requestId))
|
|
353
|
+
throw new NotFoundError(requestId);
|
|
354
|
+
return withFileLock(join(this.ws.requestsDir, `.preflight-${requestId}.lock`), fn, {
|
|
355
|
+
timeoutMs: 10_000,
|
|
356
|
+
busyMessage: `${requestId} の Preflight を別の操作が変えています。少し待ってからやり直してください`,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
get probe() {
|
|
360
|
+
return this.ownerOpts.probe ?? systemProbe;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* 同じ要望の Preflight が動いているか(ロックの中で呼ぶ)。先に、実行していたプロセスが終わっていると確かめられる run を中断にする
|
|
364
|
+
* (recoverRequest)。このプロセスで実行中、手元のエージェントの評価を待っている、ほかのプロセス(serve・CLI)で実行中、
|
|
365
|
+
* 動いているか確かめられない(以前の run・別の環境)なら、その理由。時間がたっただけでは始めない(生きている別のプロセスと重ねない)
|
|
366
|
+
*/
|
|
367
|
+
async busy(requestId) {
|
|
368
|
+
if ([...this.active.values()].some((a) => a.run.requestId === requestId))
|
|
369
|
+
return `${requestId} の preflight は実行中です`;
|
|
370
|
+
await this.recoverRequest(requestId);
|
|
371
|
+
const last = this.ws.requests.get(requestId)?.lastPreflight;
|
|
372
|
+
if (!last)
|
|
373
|
+
return undefined;
|
|
374
|
+
const run = await this.ws.preflight.get(requestId, last);
|
|
375
|
+
if (run?.status !== "running")
|
|
376
|
+
return undefined;
|
|
377
|
+
if (run.engine === "local") {
|
|
378
|
+
return `${requestId} の preflight ${last} は手元のエージェントの評価を待っています(取り込む: p3 preflight ${requestId} --complete --run ${last} / やめる: p3 preflight ${requestId} --cancel --run ${last})`;
|
|
379
|
+
}
|
|
380
|
+
const owner = ownerState(run.owner, this.probe);
|
|
381
|
+
if (owner.state === "alive")
|
|
382
|
+
return `${requestId} の preflight ${last} が別のプロセスで実行中です(やめるなら p3 preflight ${requestId} --cancel --run ${last})`;
|
|
383
|
+
return (`${requestId} の preflight ${last} が実行中のまま残っています。動いていないと確かめられないので始めません(${owner.reason})。` +
|
|
384
|
+
`止まっていることを確かめたら p3 preflight ${requestId} --cancel --run ${last} --acknowledge-stopped で中断にでき、始め直せます`);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* 実行していたプロセスが終わっていると確かめられる、running のまま残った run(codex / mock。最新でなくても)を中断(failed)にする。
|
|
388
|
+
* ロックの中で呼ぶ。手元のエージェントの run(local)・このプロセスで実行中の run・確かめられない run は変えない。中断にした run の ID
|
|
389
|
+
*/
|
|
390
|
+
async recoverRequest(requestId) {
|
|
391
|
+
const done = [];
|
|
392
|
+
for (const run of await this.ws.preflight.list(requestId)) {
|
|
393
|
+
if (run.status !== "running" || run.engine === "local" || this.active.has(run.id))
|
|
394
|
+
continue;
|
|
395
|
+
const owner = ownerState(run.owner, this.probe);
|
|
396
|
+
if (owner.state !== "dead")
|
|
397
|
+
continue;
|
|
398
|
+
run.status = "failed";
|
|
399
|
+
run.error = `中断しました: ${owner.reason}(serve や CLI が途中で止まった可能性があります)`;
|
|
400
|
+
run.interrupted = { reason: owner.reason, at: nowIso() };
|
|
401
|
+
run.ui = withCurrent({ status: "missing", reason: "評価が終わらなかったので、画面資料を取り込んでいません", targets: [], items: [], problems: [] }, run);
|
|
402
|
+
this.log(run, { kind: "error", text: run.error });
|
|
403
|
+
await this.finishLocked(run, run.before);
|
|
404
|
+
done.push(run.id);
|
|
405
|
+
}
|
|
406
|
+
return done;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* プロジェクトを開いたとき(serve の起動・--all-projects で開いたとき)に、全部の要望の running のまま残った run を確かめ、
|
|
410
|
+
* 実行していたプロセスが終わっていると確かめられるものを中断にする。1件の失敗で止めない(理由をログに残し、ほかは続ける)
|
|
411
|
+
*/
|
|
412
|
+
async recover(opts = {}) {
|
|
413
|
+
const log = opts.log ?? ((m) => console.error(m));
|
|
414
|
+
const recovered = [];
|
|
415
|
+
const errors = [];
|
|
416
|
+
for (const req of this.ws.requests.list()) {
|
|
417
|
+
try {
|
|
418
|
+
// ロックを取る前に、候補(running の codex / mock で、終わっていると確かめられる)があるかだけを見る
|
|
419
|
+
const runs = await this.ws.preflight.list(req.id);
|
|
420
|
+
const candidates = runs.filter((r) => r.status === "running" && r.engine !== "local" && !this.active.has(r.id) && ownerState(r.owner, this.probe).state === "dead");
|
|
421
|
+
if (!candidates.length)
|
|
422
|
+
continue;
|
|
423
|
+
recovered.push(...(await this.locked(req.id, async () => {
|
|
424
|
+
await this.ws.requests.reload(req.id);
|
|
425
|
+
return this.recoverRequest(req.id);
|
|
426
|
+
})).map((id) => `${req.id}/${id}`));
|
|
427
|
+
}
|
|
428
|
+
catch (e) {
|
|
429
|
+
const m = `[preflight] ${this.ws.dataDir}: ${req.id} の残った run を確かめられません: ${e.message}`;
|
|
430
|
+
errors.push(m);
|
|
431
|
+
log(m);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
for (const r of recovered)
|
|
435
|
+
log(`[preflight] ${this.ws.dataDir}: 実行していたプロセスが終わっていた run を中断にしました: ${r}`);
|
|
436
|
+
return { recovered, errors };
|
|
437
|
+
}
|
|
438
|
+
/** 評価対象を固定する(ディスクから読み直す)。同じ要望の Preflight が動いていれば PreflightBusyError */
|
|
439
|
+
async fix(requestId) {
|
|
123
440
|
// 評価対象はディスクから読み直して固定する(監視していない外部の編集も取り込む)。この後に変わっても、この run は固定した内容で評価する
|
|
124
441
|
await this.ws.requests.reload(requestId);
|
|
125
442
|
const current = this.ws.requests.require(requestId);
|
|
@@ -133,30 +450,15 @@ export class PreflightService {
|
|
|
133
450
|
...(parent ? { parent: { id: parent.id, hash: inputHash(parent) } } : {}),
|
|
134
451
|
children: kids.map((c) => ({ id: c.id, hash: inputHash(c) })),
|
|
135
452
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
try {
|
|
146
|
-
engine = this.engines[await this.currentDefault()];
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
const message = `個人設定を読めないので Preflight を始めません(Docker は起動していません): ${hideConfigPath(e)}`;
|
|
150
|
-
engine = failing(this.lastDefault, message);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (!engine)
|
|
154
|
-
throw new Error(`engine ${opts.engine} は利用できません`);
|
|
155
|
-
// 始めるときの設定に決める(この run の間は個人設定が変わっても使い続ける)
|
|
156
|
-
if (engine.snapshot) {
|
|
157
|
-
const from = engine;
|
|
158
|
-
engine = await from.snapshot().catch((e) => failing(from.name, e.message));
|
|
159
|
-
}
|
|
453
|
+
const busy = await this.busy(requestId);
|
|
454
|
+
if (busy)
|
|
455
|
+
throw new PreflightBusyError(busy);
|
|
456
|
+
return { request, input, parent, kids };
|
|
457
|
+
}
|
|
458
|
+
/** run を作る: 前回の結果とメモ・参照先・添付を固定し、要望を評価中にする(エンジンによらず同じ) */
|
|
459
|
+
async open(fixed, engine, actor) {
|
|
460
|
+
const { request, input, parent, kids } = fixed;
|
|
461
|
+
const requestId = request.id;
|
|
160
462
|
const stamp = nowIso().replace(/[-:]/g, "").replace("T", "-").slice(0, 15);
|
|
161
463
|
const id = `PF-${stamp}-${randomBytes(2).toString("hex")}`;
|
|
162
464
|
// 前回の結果とその後のメモ。開始の時点で選んで固定する(実行中に足されたメモは次回に回る)
|
|
@@ -167,10 +469,10 @@ export class PreflightService {
|
|
|
167
469
|
const run = {
|
|
168
470
|
id,
|
|
169
471
|
requestId,
|
|
170
|
-
engine
|
|
472
|
+
engine,
|
|
171
473
|
status: "running",
|
|
172
474
|
startedAt: nowIso(),
|
|
173
|
-
startedBy:
|
|
475
|
+
startedBy: actor,
|
|
174
476
|
previous: carry.previous?.id,
|
|
175
477
|
notes: carry.notes.length ? carry.notes.map((n) => n.id) : undefined,
|
|
176
478
|
log: [],
|
|
@@ -178,13 +480,18 @@ export class PreflightService {
|
|
|
178
480
|
scope: refs.map(refScope),
|
|
179
481
|
ui: { status: "generating", targets: [], items: [], problems: [] },
|
|
180
482
|
input,
|
|
483
|
+
before: request.status,
|
|
181
484
|
};
|
|
485
|
+
// 実行しているプロセスの記録(codex / mock)。手元のエージェントの run はプロセスを跨いで待つので記録しない
|
|
486
|
+
const owner = engine === "local" ? undefined : currentOwner();
|
|
487
|
+
if (owner)
|
|
488
|
+
run.owner = owner;
|
|
182
489
|
const runDir = this.ws.preflight.runDir(requestId, id);
|
|
183
490
|
// 添付は始める時点のものを run の中にコピーして固定する。途中で変わったら始めない(原本は残るので、そのまま再試行できる)
|
|
184
491
|
try {
|
|
185
|
-
const
|
|
186
|
-
if (
|
|
187
|
-
run.attachments =
|
|
492
|
+
const fixedAttachments = await this.ws.attachments.snapshot(requestId, runDir);
|
|
493
|
+
if (fixedAttachments.length)
|
|
494
|
+
run.attachments = fixedAttachments;
|
|
188
495
|
}
|
|
189
496
|
catch (e) {
|
|
190
497
|
await rm(runDir, { recursive: true, force: true });
|
|
@@ -223,18 +530,27 @@ export class PreflightService {
|
|
|
223
530
|
await writeFileAtomic(join(runDir, "request.md"), renderRequestSnapshot(request.id, input));
|
|
224
531
|
// ステータスを動かすのはタスク化する前の要望だけ。進行中・完了の要望で再評価しても状態は変えない
|
|
225
532
|
const before = request.status;
|
|
226
|
-
|
|
227
|
-
await this.ws.requests.update(requestId, early ? { status: "preflight", lastPreflight: id } : { lastPreflight: id }, { actor: opts.actor });
|
|
533
|
+
await this.ws.requests.update(requestId, isEarly(before) ? { status: "preflight", lastPreflight: id } : { lastPreflight: id }, { actor });
|
|
228
534
|
this.ws.emit({ type: "changed", kind: "request", request: requestId });
|
|
229
|
-
const abort = new AbortController();
|
|
230
|
-
const after = (ok) => (early ? (ok ? "reviewed" : before === "reviewed" ? "reviewed" : "draft") : before);
|
|
231
535
|
const family = {
|
|
232
536
|
...(parent ? { parent: { id: parent.id, title: parent.title, body: parent.body } } : {}),
|
|
233
537
|
...(kids.length ? { children: kids.map((c) => ({ id: c.id, title: c.title, status: c.status })) } : {}),
|
|
234
538
|
};
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
539
|
+
return { run, request, runDir, carried, refs, family, before };
|
|
540
|
+
}
|
|
541
|
+
/** エンジンに渡す仕事(始めたときに固定したもの) */
|
|
542
|
+
job(o) {
|
|
543
|
+
return {
|
|
544
|
+
project: this.ws.project,
|
|
545
|
+
// 始めたときに固定した要望(実行中に本文が変わっても、request.md・run.input と同じものを渡す)
|
|
546
|
+
request: o.request,
|
|
547
|
+
refs: o.refs,
|
|
548
|
+
dataDir: this.ws.dataDir,
|
|
549
|
+
runDir: o.runDir,
|
|
550
|
+
carryover: o.carried,
|
|
551
|
+
attachments: o.run.attachments ?? [],
|
|
552
|
+
...o.family,
|
|
553
|
+
};
|
|
238
554
|
}
|
|
239
555
|
/** 実行中の run の完了を待つ(CLI 用)。完了後の run を返す */
|
|
240
556
|
async wait(requestId, runId) {
|
|
@@ -278,45 +594,16 @@ export class PreflightService {
|
|
|
278
594
|
return `data:${data}`;
|
|
279
595
|
return p.startsWith("/workspace") ? p : "(参照先の外)";
|
|
280
596
|
}
|
|
281
|
-
async execute(engine,
|
|
597
|
+
async execute(engine, opened, signal) {
|
|
598
|
+
const { run, runDir, refs } = opened;
|
|
282
599
|
try {
|
|
283
600
|
await this.extractAttachments(run, runDir);
|
|
284
|
-
const outcome = await engine.run(
|
|
285
|
-
project: this.ws.project,
|
|
286
|
-
// 始めたときに固定した要望(実行中に本文が変わっても、request.md・run.input と同じものを渡す)
|
|
287
|
-
request,
|
|
288
|
-
refs,
|
|
289
|
-
dataDir: this.ws.dataDir,
|
|
290
|
-
runDir,
|
|
291
|
-
carryover,
|
|
292
|
-
attachments: run.attachments ?? [],
|
|
293
|
-
...family,
|
|
294
|
-
}, (e) => this.log(run, e), signal);
|
|
601
|
+
const outcome = await engine.run(this.job(opened), (e) => this.log(run, e), signal);
|
|
295
602
|
// 書いてよい範囲の外が変更されていないか確かめる
|
|
296
603
|
const stray = outcome.changed.filter((p) => !p.startsWith(`${runDir}/`));
|
|
297
604
|
for (const p of stray)
|
|
298
605
|
this.log(run, { kind: "error", text: `想定外の変更: ${this.shownPath(p, refs)}(確認して必要なら元に戻してください)` });
|
|
299
|
-
|
|
300
|
-
run.drafts = await this.ws.preflight.loadDrafts(run.requestId, run.id);
|
|
301
|
-
// 子の案(分割の run): 確かめて、問題があれば残す(採用しない)
|
|
302
|
-
const children = await this.ws.preflight.loadChildren(run.requestId, run.id);
|
|
303
|
-
if (children) {
|
|
304
|
-
run.children = children;
|
|
305
|
-
const problems = draftProblems(run, this.ws.tasksOf(run.requestId).list().map((t) => t.id));
|
|
306
|
-
if (problems.length)
|
|
307
|
-
run.draftProblems = problems;
|
|
308
|
-
for (const p of problems)
|
|
309
|
-
this.log(run, { kind: "error", text: `子の案の問題: ${p}` });
|
|
310
|
-
}
|
|
311
|
-
// 画面資料: 確かめて取り込む。不正なものは除いて理由を残す(評価は消さない)。必要な画面イメージが足りなければ go を clarify に
|
|
312
|
-
run.ui = applyUiShortage(run.assessment, await loadUiMaterials(runDir, run.id, [...run.drafts, ...(run.children ?? [])].map((d) => d.key)));
|
|
313
|
-
for (const p of run.ui.problems)
|
|
314
|
-
this.log(run, { kind: "error", text: `画面資料を取り込めません: ${p}` });
|
|
315
|
-
if (run.ui.status === "missing")
|
|
316
|
-
this.log(run, { kind: "info", text: `画面資料が足りません: ${run.ui.reason}` });
|
|
317
|
-
run.ui = withCurrent(run.ui, run);
|
|
318
|
-
if (run.ui.verdictAdjusted)
|
|
319
|
-
this.log(run, { kind: "info", text: "必要な画面イメージが足りないので、判定を go から clarify にしました" });
|
|
606
|
+
await this.ingest(run, runDir, refs);
|
|
320
607
|
run.status = "completed";
|
|
321
608
|
}
|
|
322
609
|
catch (e) {
|
|
@@ -327,19 +614,72 @@ export class PreflightService {
|
|
|
327
614
|
this.log(run, { kind: "error", text: run.error });
|
|
328
615
|
}
|
|
329
616
|
finally {
|
|
330
|
-
run.
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
617
|
+
await this.finish(run, opened.before);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/** 実行の終わり(execute): 要望ごとのロックの中で終える。ロックを取れなくても記録は残す(終わった状態を上書きしない規則は同じ) */
|
|
621
|
+
async finish(run, before) {
|
|
622
|
+
try {
|
|
623
|
+
await this.locked(run.requestId, () => this.finishLocked(run, before));
|
|
624
|
+
}
|
|
625
|
+
catch (e) {
|
|
626
|
+
if (!this.active.has(run.id))
|
|
627
|
+
return; // 終えた後に失敗した
|
|
628
|
+
console.error(`[preflight] ${run.requestId}/${run.id}: ロックを取れないまま終えます: ${e.message}`);
|
|
629
|
+
await this.finishLocked(run, before);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/** run のフォルダの成果物(評価・案・子の案・画面資料)を取り込む。評価が読めなければ例外 */
|
|
633
|
+
async ingest(run, runDir, refs) {
|
|
634
|
+
run.assessment = parseAssessment(await readFile(join(runDir, "assessment.json"), "utf8"), refs);
|
|
635
|
+
run.drafts = await this.ws.preflight.loadDrafts(run.requestId, run.id);
|
|
636
|
+
// 子の案(分割の run): 確かめて、問題があれば残す(採用しない)
|
|
637
|
+
const children = await this.ws.preflight.loadChildren(run.requestId, run.id);
|
|
638
|
+
if (children) {
|
|
639
|
+
run.children = children;
|
|
640
|
+
const problems = draftProblems(run, this.ws.tasksOf(run.requestId).list().map((t) => t.id));
|
|
641
|
+
if (problems.length)
|
|
642
|
+
run.draftProblems = problems;
|
|
643
|
+
for (const p of problems)
|
|
644
|
+
this.log(run, { kind: "error", text: `子の案の問題: ${p}` });
|
|
645
|
+
}
|
|
646
|
+
// 画面資料: 確かめて取り込む。不正なものは除いて理由を残す(評価は消さない)。必要な画面イメージが足りなければ go を clarify に
|
|
647
|
+
run.ui = applyUiShortage(run.assessment, await loadUiMaterials(runDir, run.id, [...run.drafts, ...(run.children ?? [])].map((d) => d.key)));
|
|
648
|
+
for (const p of run.ui.problems)
|
|
649
|
+
this.log(run, { kind: "error", text: `画面資料を取り込めません: ${p}` });
|
|
650
|
+
if (run.ui.status === "missing")
|
|
651
|
+
this.log(run, { kind: "info", text: `画面資料が足りません: ${run.ui.reason}` });
|
|
652
|
+
run.ui = withCurrent(run.ui, run);
|
|
653
|
+
if (run.ui.verdictAdjusted)
|
|
654
|
+
this.log(run, { kind: "info", text: "必要な画面イメージが足りないので、判定を go から clarify にしました" });
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* run を終える(ロックの中で呼ぶ): 記録を保存し、評価中にした要望を評価済み(失敗なら元)へ戻す。
|
|
658
|
+
* ファイルの run が既に終わっていれば(別のプロセスが中断にした・取り消した)、その状態を上書きしない。
|
|
659
|
+
* 要望のステータスは、その run が最新(lastPreflight)のときだけ戻す(古い run の終わりで、新しい run と要望の状態を巻き戻さない)。
|
|
660
|
+
* 始める前の状態(before)が分からない run は、要望のステータスを変えない
|
|
661
|
+
*/
|
|
662
|
+
async finishLocked(run, before) {
|
|
663
|
+
const onDisk = await this.ws.preflight.get(run.requestId, run.id);
|
|
664
|
+
if (onDisk && onDisk.status !== "running") {
|
|
339
665
|
this.active.delete(run.id);
|
|
340
|
-
this.ws.emit({ type: "preflight-status", runId: run.id, status:
|
|
341
|
-
|
|
666
|
+
this.ws.emit({ type: "preflight-status", runId: run.id, status: onDisk.status });
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
run.finishedAt = nowIso();
|
|
670
|
+
await this.ws.preflight.save(run);
|
|
671
|
+
// 評価中にした要望だけ、終わったら評価済み(失敗なら元)へ戻す。実行中に人や AI が変えたステータス
|
|
672
|
+
// (進行中に戻した・完了済にしたなど)や、評価中にしていない要望(進行中・対応済など)は上書きしない
|
|
673
|
+
await this.ws.requests.reload(run.requestId).catch(() => undefined);
|
|
674
|
+
const req = this.ws.requests.get(run.requestId);
|
|
675
|
+
const status = before ? statusAfter(before, run.status === "completed") : undefined;
|
|
676
|
+
if (status && req?.lastPreflight === run.id && req.status === "preflight" && status !== "preflight") {
|
|
677
|
+
await this.ws.requests.update(run.requestId, { status });
|
|
342
678
|
}
|
|
679
|
+
// 保存が終わってから実行中の一覧から外す(先に外すと wait() が保存前のファイルを読む)
|
|
680
|
+
this.active.delete(run.id);
|
|
681
|
+
this.ws.emit({ type: "preflight-status", runId: run.id, status: run.status });
|
|
682
|
+
this.ws.emit({ type: "changed", kind: "request", request: run.requestId });
|
|
343
683
|
}
|
|
344
684
|
/**
|
|
345
685
|
* 最新の Preflight(lastPreflight)と、指定した run の今の状態。要望と run.json をディスクから読むだけで、読み込んだ内容
|
|
@@ -490,7 +830,15 @@ export class PreflightService {
|
|
|
490
830
|
}
|
|
491
831
|
/** run に今の要望との比較(inputMatch)を添える(画面・CLI・API の表示用。run.json には書かない) */
|
|
492
832
|
async withInputMatch(requestId, run) {
|
|
493
|
-
return { ...run, inputMatch: await this.inputMatch(requestId, run) };
|
|
833
|
+
return { ...run, inputMatch: await this.inputMatch(requestId, run), ...(run.status === "running" ? { liveness: this.liveness(run) } : {}) };
|
|
834
|
+
}
|
|
835
|
+
/** running の run を実行しているプロセスの今の状態(画面の「中止」の出し分け。書かない)。local は待っている(waiting) */
|
|
836
|
+
liveness(run) {
|
|
837
|
+
if (run.engine === "local")
|
|
838
|
+
return { state: "waiting" };
|
|
839
|
+
if (this.active.has(run.id))
|
|
840
|
+
return { state: "alive" };
|
|
841
|
+
return ownerState(run.owner, this.probe);
|
|
494
842
|
}
|
|
495
843
|
/** タスク化の前に見せるもの: 採用する案と、置き換えられる既存タスク、評価対象が今の要望と同じか(ファイルは変えない) */
|
|
496
844
|
async previewMaterialize(requestId, runId) {
|
|
@@ -783,6 +1131,90 @@ export class PreflightService {
|
|
|
783
1131
|
};
|
|
784
1132
|
}
|
|
785
1133
|
}
|
|
1134
|
+
/** 中断の依頼を見る間隔(実行しているプロセス)と、頼んだ側が止まるのを待つ時間 */
|
|
1135
|
+
const CANCEL_POLL_MS = 1_000;
|
|
1136
|
+
const CANCEL_WAIT_MS = 5_000;
|
|
1137
|
+
/** 中断できない(完了した run・動いている run への確認の操作など)。何も変えていない */
|
|
1138
|
+
export class CancelRefusedError extends Error {
|
|
1139
|
+
code;
|
|
1140
|
+
constructor(code, message) {
|
|
1141
|
+
super(message);
|
|
1142
|
+
this.code = code;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
/** ステータスを動かすのはタスク化する前の要望だけ */
|
|
1146
|
+
const isEarly = (s) => s === "draft" || s === "preflight" || s === "reviewed";
|
|
1147
|
+
/** Preflight を終えたときの要望のステータス(評価中にしたものだけ。成功なら評価済み、失敗なら元) */
|
|
1148
|
+
function statusAfter(before, ok) {
|
|
1149
|
+
return isEarly(before) ? (ok ? "reviewed" : before === "reviewed" ? "reviewed" : "draft") : before;
|
|
1150
|
+
}
|
|
1151
|
+
/** 手元のエージェントの run ではない・もう待っていない(使い方の誤り) */
|
|
1152
|
+
export class LocalRunError extends Error {
|
|
1153
|
+
}
|
|
1154
|
+
/** 手元のエージェントの成果物を取り込めない(何も確定していない。直して同じ run でやり直せる) */
|
|
1155
|
+
export class LocalResultError extends Error {
|
|
1156
|
+
problems;
|
|
1157
|
+
constructor(problems) {
|
|
1158
|
+
super(`成果物を取り込めません(何も確定していません。直してから同じ run でもう一度 --complete):\n${problems.map((p) => `- ${p}`).join("\n")}`);
|
|
1159
|
+
this.problems = problems;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* 手元のエージェントの成果物を取り込む前に確かめる(問題の一覧。無ければ空): 成果物がシンボリックリンクでない(run の外を読ませない)、
|
|
1164
|
+
* 評価が JSON として読める、タスク案と子の案の形(キー・題・依存・作業先・混在)
|
|
1165
|
+
*/
|
|
1166
|
+
async function localProblems(ws, run, runDir) {
|
|
1167
|
+
const problems = [];
|
|
1168
|
+
const link = async (rel) => (await lstat(join(runDir, rel)).catch(() => undefined))?.isSymbolicLink();
|
|
1169
|
+
for (const rel of ["assessment.json", "drafts", "children", UI_DIR, `${UI_DIR}/src`, `${UI_DIR}/index.json`]) {
|
|
1170
|
+
if (await link(rel))
|
|
1171
|
+
problems.push(`${rel} がシンボリックリンクです(run のフォルダの中にファイルとして書いてください)`);
|
|
1172
|
+
}
|
|
1173
|
+
for (const dir of ["drafts", "children"]) {
|
|
1174
|
+
if (await link(dir))
|
|
1175
|
+
continue;
|
|
1176
|
+
const files = await readdir(join(runDir, dir)).catch(() => []);
|
|
1177
|
+
for (const f of files)
|
|
1178
|
+
if (await link(`${dir}/${f}`))
|
|
1179
|
+
problems.push(`${dir}/${f} がシンボリックリンクです`);
|
|
1180
|
+
}
|
|
1181
|
+
if (problems.length)
|
|
1182
|
+
return problems;
|
|
1183
|
+
const text = await readFile(join(runDir, "assessment.json"), "utf8").catch(() => undefined);
|
|
1184
|
+
if (text === undefined)
|
|
1185
|
+
problems.push("assessment.json がありません");
|
|
1186
|
+
else {
|
|
1187
|
+
try {
|
|
1188
|
+
parseAssessment(text);
|
|
1189
|
+
}
|
|
1190
|
+
catch (e) {
|
|
1191
|
+
problems.push(e.message);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
const drafts = await ws.preflight.loadDrafts(run.requestId, run.id);
|
|
1195
|
+
const children = await ws.preflight.loadChildren(run.requestId, run.id);
|
|
1196
|
+
if (children)
|
|
1197
|
+
problems.push(...draftProblems({ drafts, children }, ws.tasksOf(run.requestId).list().map((t) => t.id)));
|
|
1198
|
+
const keys = drafts.map((d) => d.key);
|
|
1199
|
+
for (const k of new Set(keys.filter((k, i) => keys.indexOf(k) !== i)))
|
|
1200
|
+
problems.push(`タスク案のキーが重なっています: ${k}`);
|
|
1201
|
+
for (const d of drafts) {
|
|
1202
|
+
if (!/^D\d+$/.test(d.key))
|
|
1203
|
+
problems.push(`タスク案のキーの形が違います: ${d.key}(D1, D2, ...)`);
|
|
1204
|
+
if (!d.title.trim())
|
|
1205
|
+
problems.push(`${d.key}: 題がありません`);
|
|
1206
|
+
for (const dep of d.dependsOn) {
|
|
1207
|
+
if (dep === d.key)
|
|
1208
|
+
problems.push(`${d.key}: 自分に依存しています`);
|
|
1209
|
+
else if (!keys.includes(dep))
|
|
1210
|
+
problems.push(`${d.key}: 依存先の ${dep} というタスク案はありません`);
|
|
1211
|
+
}
|
|
1212
|
+
const repo = repoTargetProblem(d.repo, ws.project);
|
|
1213
|
+
if (repo)
|
|
1214
|
+
problems.push(`${d.key}: ${repo}`);
|
|
1215
|
+
}
|
|
1216
|
+
return problems;
|
|
1217
|
+
}
|
|
786
1218
|
/** 固定した添付の画像を、現状(添付)の画面資料として足す(対象のタスク案は無し) */
|
|
787
1219
|
function withCurrent(ui, run) {
|
|
788
1220
|
const current = (run.attachments ?? [])
|