@infodb/p3 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +56 -2
- package/bin/p3.mjs +18 -0
- package/dist/web/apple-touch-icon.png +0 -0
- package/dist/web/assets/Outputs-Ni3pNuGU.js +1 -0
- package/dist/web/assets/RequestStatus-K8TZwpur.js +1 -0
- package/dist/web/assets/TagInput-BICKVH2x.js +1 -0
- package/dist/web/assets/TaskDrawer-BkDIstIN.js +3 -0
- package/dist/web/assets/_requestId-B5O_ytaq.js +1 -0
- package/dist/web/assets/_requestId-C66puEMO.js +30 -0
- package/dist/web/assets/action-CMjATXqv.js +1 -0
- package/dist/web/assets/board-CbfDjkls.js +1 -0
- package/dist/web/assets/copy-B1ZbihKL.js +2 -0
- package/dist/web/assets/done-CPQbCFHg.js +1 -0
- package/dist/web/assets/draft-dT3gLDIt.js +1 -0
- package/dist/web/assets/format-DuK6P1hb.js +1 -0
- package/dist/web/assets/index-BUcs2J6N.js +12 -0
- package/dist/web/assets/index-C3DkqwJ9.css +2 -0
- package/dist/web/assets/link-_s8ndNKS.js +1 -0
- package/dist/web/assets/new-T60RB7de.js +1 -0
- package/dist/web/assets/preload-helper-dXqN7N-I.js +1 -0
- package/dist/web/assets/release-notes-DxrPhDbt.js +3 -0
- package/dist/web/assets/requestData-D8ZiSZss.js +1 -0
- package/dist/web/assets/rolldown-runtime-hePW80VL.js +1 -0
- package/dist/web/assets/routes-D9ZxXoLk.js +2 -0
- package/dist/web/assets/search-CJ8MBKTz.js +1 -0
- package/dist/web/assets/settings-DsHVyqzJ.js +1 -0
- package/dist/web/assets/supersede-CLXlhCq0.js +1 -0
- package/dist/web/assets/ui-Cvke4y4X.js +1 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/favicon.svg +12 -0
- package/dist/web/index.html +23 -0
- package/docs/architecture.md +588 -0
- package/docs/chatgpt-sign-in.md +163 -0
- package/docs/development.md +194 -0
- package/docs/how-to-use.md +524 -0
- package/docs/installation.md +275 -0
- package/docs/publishing.md +65 -0
- package/package.json +86 -3
- package/skills/p3/SKILL.md +95 -0
- package/src/cli/args.ts +179 -0
- package/src/cli/chatgpt.ts +74 -0
- package/src/cli/codex.ts +28 -0
- package/src/cli/format.ts +9 -0
- package/src/cli/help.ts +99 -0
- package/src/cli/main.ts +267 -0
- package/src/cli/output.ts +99 -0
- package/src/cli/preflightCommands.ts +237 -0
- package/src/cli/project.ts +501 -0
- package/src/cli/readOnly.ts +36 -0
- package/src/cli/releaseNotesCommands.ts +139 -0
- package/src/cli/render.ts +60 -0
- package/src/cli/requestCommands.ts +170 -0
- package/src/cli/showView.ts +110 -0
- package/src/cli/taskCommands.ts +167 -0
- package/src/cli/userConfig.ts +58 -0
- package/src/core/chatgpt.ts +33 -0
- package/src/core/context.ts +57 -0
- package/src/core/machineSettings.ts +207 -0
- package/src/core/machineSettingsEdit.ts +156 -0
- package/src/core/ops.ts +114 -0
- package/src/core/preflight/carryover.ts +133 -0
- package/src/core/preflight/codexEngine.ts +702 -0
- package/src/core/preflight/codexImage.ts +98 -0
- package/src/core/preflight/diagnose.ts +124 -0
- package/src/core/preflight/engine.ts +92 -0
- package/src/core/preflight/input.ts +65 -0
- package/src/core/preflight/launch.ts +86 -0
- package/src/core/preflight/launcher.mjs +75 -0
- package/src/core/preflight/mock-ui.html +24 -0
- package/src/core/preflight/mock-ui.png +0 -0
- package/src/core/preflight/mockEngine.ts +145 -0
- package/src/core/preflight/render-image/Dockerfile +23 -0
- package/src/core/preflight/render-image/render.mjs +53 -0
- package/src/core/preflight/render.ts +336 -0
- package/src/core/preflight/service.ts +690 -0
- package/src/core/preflight/uiMaterials.ts +272 -0
- package/src/core/preflight/uiRender.ts +84 -0
- package/src/core/preflight/wireframe.ts +69 -0
- package/src/core/preflight/xlsx.ts +328 -0
- package/src/core/redact.ts +31 -0
- package/src/core/releaseNotes/generate.ts +271 -0
- package/src/core/releaseNotes/material.ts +114 -0
- package/src/core/releaseNotes/mockSummarizer.ts +28 -0
- package/src/core/releaseNotes/service.ts +106 -0
- package/src/core/releaseNotes/store.ts +216 -0
- package/src/core/repoAdmin.ts +81 -0
- package/src/core/repoRefs.ts +90 -0
- package/src/core/requestOps.ts +140 -0
- package/src/core/requestStatus.ts +58 -0
- package/src/core/store/attachments.ts +228 -0
- package/src/core/store/collection.ts +282 -0
- package/src/core/store/frontmatter.ts +22 -0
- package/src/core/store/fsutil.ts +90 -0
- package/src/core/store/locate.ts +181 -0
- package/src/core/store/normalize.ts +155 -0
- package/src/core/store/noteStore.ts +56 -0
- package/src/core/store/preflightStore.ts +136 -0
- package/src/core/store/projectFile.ts +101 -0
- package/src/core/store/workspace.ts +328 -0
- package/src/core/supersede.ts +74 -0
- package/src/core/userConfig.ts +180 -0
- package/src/core/userTheme.ts +22 -0
- package/src/core/workTargets.ts +120 -0
- package/src/server/api/chatgptRoutes.ts +48 -0
- package/src/server/api/local.ts +33 -0
- package/src/server/api/manifest.ts +119 -0
- package/src/server/api/releaseNoteRoutes.ts +78 -0
- package/src/server/api/repoRoutes.ts +67 -0
- package/src/server/api/routes.ts +374 -0
- package/src/server/api/userThemeRoutes.ts +62 -0
- package/src/server/server.ts +49 -0
- package/src/shared/dates.ts +12 -0
- package/src/shared/preflightInput.ts +27 -0
- package/src/shared/refs.ts +55 -0
- package/src/shared/repos.ts +113 -0
- package/src/shared/scopeView.ts +48 -0
- package/src/shared/tags.ts +27 -0
- package/src/shared/types.ts +700 -0
- package/src/shared/workTarget.ts +44 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { repoNameProblem, type CodeMapEntry, type RepoRef } from "../../shared/repos.ts";
|
|
2
|
+
import { isTaskStatus, RELATION_TYPES, REQUEST_STATUSES, type ProjectConfig, type Relation, type RepoOutput, type Request, type RequestStatus, type Task, type Workstream } from "../../shared/types.ts";
|
|
3
|
+
|
|
4
|
+
const str = (v: unknown): string | undefined => (v === undefined || v === null || v === "" ? undefined : String(v));
|
|
5
|
+
const num = (v: unknown): number | undefined => (v === undefined || v === null || v === "" ? undefined : Number(v));
|
|
6
|
+
const strs = (v: unknown): string[] => (Array.isArray(v) ? v.map(String) : typeof v === "string" ? [v] : []);
|
|
7
|
+
/** YAML は日付を Date にしてしまうので YYYY-MM-DD に戻す */
|
|
8
|
+
const date = (v: unknown): string | undefined => (v instanceof Date ? v.toISOString().slice(0, 10) : str(v));
|
|
9
|
+
const iso = (v: unknown): string => (v instanceof Date ? v.toISOString() : (str(v) ?? new Date(0).toISOString()));
|
|
10
|
+
|
|
11
|
+
export function normalizeRelations(v: unknown): Relation[] {
|
|
12
|
+
if (!Array.isArray(v)) return [];
|
|
13
|
+
return v
|
|
14
|
+
.filter((r): r is Record<string, unknown> => !!r && typeof r === "object")
|
|
15
|
+
.filter((r) => typeof r.type === "string" && r.type in RELATION_TYPES && typeof r.target === "string")
|
|
16
|
+
.map((r) => ({
|
|
17
|
+
type: r.type as Relation["type"],
|
|
18
|
+
target: String(r.target),
|
|
19
|
+
note: str(r.note),
|
|
20
|
+
origin: str(r.origin) as Relation["origin"],
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 要望 requestId のタスクとして読む(request / ref は場所から決まる) */
|
|
25
|
+
export const normalizeTaskIn =
|
|
26
|
+
(requestId: string) =>
|
|
27
|
+
(d: Record<string, unknown>, body: string, id: string): Task => ({
|
|
28
|
+
id,
|
|
29
|
+
request: requestId,
|
|
30
|
+
ref: `${requestId}/${id}`,
|
|
31
|
+
title: str(d.title) ?? id,
|
|
32
|
+
// 3つ以外(古い設定の列など)は未着手として読む
|
|
33
|
+
status: isTaskStatus(d.status) ? d.status : "todo",
|
|
34
|
+
labels: strs(d.labels),
|
|
35
|
+
areas: strs(d.areas),
|
|
36
|
+
outputs: strs(d.outputs),
|
|
37
|
+
// 作業先(null / "" / 無いは作業先なし)
|
|
38
|
+
repo: str(d.repo) ?? null,
|
|
39
|
+
...(Array.isArray(d.repoOutputs) ? { repoOutputs: normalizeRepoOutputs(d.repoOutputs) } : {}),
|
|
40
|
+
startedAt: d.startedAt ? iso(d.startedAt) : undefined,
|
|
41
|
+
doneAt: d.doneAt ? iso(d.doneAt) : undefined,
|
|
42
|
+
checkedAt: d.checkedAt ? iso(d.checkedAt) : undefined,
|
|
43
|
+
record: str(d.record)?.trim() || undefined,
|
|
44
|
+
relations: normalizeRelations(d.relations),
|
|
45
|
+
rev: num(d.rev) ?? 1,
|
|
46
|
+
createdAt: iso(d.createdAt),
|
|
47
|
+
updatedAt: iso(d.updatedAt),
|
|
48
|
+
updatedBy: str(d.updatedBy),
|
|
49
|
+
body,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const TASK_KEY_ORDER: (keyof Task)[] = [
|
|
53
|
+
"id", "title", "status", "repo", "labels", "areas", "outputs", "repoOutputs", "record",
|
|
54
|
+
"relations", "rev", "createdAt", "startedAt", "doneAt", "checkedAt", "updatedAt", "updatedBy",
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
export function normalizeRequest(d: Record<string, unknown>, body: string, id: string): Request {
|
|
58
|
+
const status = str(d.status) as RequestStatus;
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
title: str(d.title) ?? id,
|
|
62
|
+
status: REQUEST_STATUSES.includes(status) ? status : "draft",
|
|
63
|
+
areas: strs(d.areas),
|
|
64
|
+
due: date(d.due),
|
|
65
|
+
milestone: str(d.milestone),
|
|
66
|
+
tags: strs(d.tags),
|
|
67
|
+
...(d.workstreams && typeof d.workstreams === "object" ? { workstreams: normalizeWorkstreams(d.workstreams) } : {}),
|
|
68
|
+
lastPreflight: str(d.lastPreflight),
|
|
69
|
+
rev: num(d.rev) ?? 1,
|
|
70
|
+
createdAt: iso(d.createdAt),
|
|
71
|
+
updatedAt: iso(d.updatedAt),
|
|
72
|
+
updatedBy: str(d.updatedBy),
|
|
73
|
+
body,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const REQUEST_KEY_ORDER: (keyof Request)[] = [
|
|
78
|
+
"id", "title", "status", "due", "milestone", "tags", "areas", "workstreams", "lastPreflight",
|
|
79
|
+
"rev", "createdAt", "updatedAt", "updatedBy",
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
export function normalizeProject(d: Record<string, unknown>): ProjectConfig {
|
|
83
|
+
const preflight = (d.preflight ?? {}) as Record<string, unknown>;
|
|
84
|
+
return {
|
|
85
|
+
name: str(d.name) ?? "p3 project",
|
|
86
|
+
description: str(d.description) ?? "",
|
|
87
|
+
repos: normalizeRepos(d.repos),
|
|
88
|
+
milestones: Array.isArray(d.milestones)
|
|
89
|
+
? (d.milestones as unknown[])
|
|
90
|
+
.filter((m): m is Record<string, unknown> => !!m && typeof m === "object" && !!(m as Record<string, unknown>).id)
|
|
91
|
+
.map((m) => ({ id: String(m.id), title: str(m.title) ?? String(m.id), due: date(m.due), done: m.done === true ? true : undefined }))
|
|
92
|
+
: [],
|
|
93
|
+
branchPattern: str(d.branchPattern) ?? "req/{id}-{slug}",
|
|
94
|
+
preflight: { instructions: str(preflight.instructions) ?? "" },
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* project.yaml の repos。名前をキーにした対応(推奨)か、name を持つ項目の並び。名前が使えない項目・重複は読まない
|
|
100
|
+
* (書き戻すときも p3 repo で変えた項目のほかは触らないので、ファイルからは消えない)。
|
|
101
|
+
* map は「パス: 役割」か「パス: { role, entry }」の対応、または { path, role, entry } の並び
|
|
102
|
+
*/
|
|
103
|
+
export function normalizeRepos(v: unknown): RepoRef[] {
|
|
104
|
+
const entries: [string, Record<string, unknown>][] = Array.isArray(v)
|
|
105
|
+
? v.filter((x): x is Record<string, unknown> => !!x && typeof x === "object").map((x) => [String(x.name ?? ""), x])
|
|
106
|
+
: v && typeof v === "object"
|
|
107
|
+
? Object.entries(v as Record<string, unknown>).map(([k, x]) => [k, x && typeof x === "object" ? (x as Record<string, unknown>) : { role: x }])
|
|
108
|
+
: [];
|
|
109
|
+
const out: RepoRef[] = [];
|
|
110
|
+
for (const [name, x] of entries) {
|
|
111
|
+
if (repoNameProblem(name) || out.some((r) => r.name === name)) continue;
|
|
112
|
+
out.push({
|
|
113
|
+
name,
|
|
114
|
+
role: str(x.role) ?? "",
|
|
115
|
+
readFirst: x.readFirst === true,
|
|
116
|
+
writable: x.writable === true,
|
|
117
|
+
map: normalizeCodeMap(x.map),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function normalizeCodeMap(v: unknown): CodeMapEntry[] {
|
|
124
|
+
const one = (path: string, x: unknown): CodeMapEntry | undefined => {
|
|
125
|
+
if (!path) return undefined;
|
|
126
|
+
if (typeof x === "string") return { path, role: x };
|
|
127
|
+
if (x && typeof x === "object") {
|
|
128
|
+
const o = x as Record<string, unknown>;
|
|
129
|
+
return { path, role: str(o.role) ?? "", ...(str(o.entry) ? { entry: str(o.entry) } : {}) };
|
|
130
|
+
}
|
|
131
|
+
return { path, role: "" };
|
|
132
|
+
};
|
|
133
|
+
const list = Array.isArray(v)
|
|
134
|
+
? v.map((x) => (x && typeof x === "object" ? one(String((x as Record<string, unknown>).path ?? ""), x) : undefined))
|
|
135
|
+
: v && typeof v === "object"
|
|
136
|
+
? Object.entries(v as Record<string, unknown>).map(([k, x]) => one(k, x))
|
|
137
|
+
: [];
|
|
138
|
+
return list.filter((x): x is CodeMapEntry => !!x);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** リポジトリごとの成果物。repo と output のどちらかが無い項目は読まない */
|
|
142
|
+
export function normalizeRepoOutputs(v: unknown): RepoOutput[] {
|
|
143
|
+
return (Array.isArray(v) ? v : [])
|
|
144
|
+
.filter((x): x is Record<string, unknown> => !!x && typeof x === "object")
|
|
145
|
+
.flatMap((x) => (str(x.repo) && str(x.output) ? [{ repo: str(x.repo)!, output: str(x.output)! }] : []));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function normalizeWorkstreams(v: object): Record<string, Workstream> {
|
|
149
|
+
const out: Record<string, Workstream> = {};
|
|
150
|
+
for (const [name, w] of Object.entries(v as Record<string, unknown>)) {
|
|
151
|
+
const o = w && typeof w === "object" ? (w as Record<string, unknown>) : {};
|
|
152
|
+
if (!repoNameProblem(name) && str(o.branch)) out[name] = { branch: str(o.branch)! };
|
|
153
|
+
}
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { randomBytes } from "node:crypto";
|
|
4
|
+
import type { PreflightNote } from "../../shared/types.ts";
|
|
5
|
+
import { parseFrontmatter, stringifyFrontmatter } from "./frontmatter.ts";
|
|
6
|
+
import { createExclusive, nowIso } from "./fsutil.ts";
|
|
7
|
+
|
|
8
|
+
export const NOTE_ID = /^N-\d{8}-\d{6}-[0-9a-f]{4}$/;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* requests/<要望ID>/notes/N-....md Preflight へのメモ(1件1ファイル。書き足すだけで、直さない)
|
|
12
|
+
*
|
|
13
|
+
* 誰がいつ書いたかを frontmatter に残す。ファイルを分けるので、複数人が同時に書いても衝突しない。
|
|
14
|
+
*/
|
|
15
|
+
export class NoteStore {
|
|
16
|
+
/** requestsDir: .p3/requests */
|
|
17
|
+
constructor(readonly requestsDir: string) {}
|
|
18
|
+
|
|
19
|
+
dir(requestId: string) {
|
|
20
|
+
return join(this.requestsDir, requestId, "notes");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async add(requestId: string, body: string, actor?: string): Promise<PreflightNote> {
|
|
24
|
+
const text = body.trim();
|
|
25
|
+
if (!text) throw new Error("メモが空です");
|
|
26
|
+
for (;;) {
|
|
27
|
+
const createdAt = nowIso();
|
|
28
|
+
const stamp = createdAt.replace(/[-:]/g, "").replace("T", "-").slice(0, 15);
|
|
29
|
+
const note: PreflightNote = { id: `N-${stamp}-${randomBytes(2).toString("hex")}`, requestId, createdAt, createdBy: actor, body: text };
|
|
30
|
+
const file = stringifyFrontmatter({ id: note.id, createdAt, createdBy: actor }, text);
|
|
31
|
+
if (await createExclusive(join(this.dir(requestId), `${note.id}.md`), file)) return note;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 古い順(作成日時、同じなら ID)。読めないファイルは invalid に理由を返す(黙って捨てない) */
|
|
36
|
+
async list(requestId: string): Promise<{ notes: PreflightNote[]; invalid: { file: string; error: string }[] }> {
|
|
37
|
+
const files = (await readdir(this.dir(requestId)).catch(() => [] as string[])).filter((f) => f.endsWith(".md"));
|
|
38
|
+
const notes: PreflightNote[] = [];
|
|
39
|
+
const invalid: { file: string; error: string }[] = [];
|
|
40
|
+
for (const f of files) {
|
|
41
|
+
const id = f.slice(0, -3);
|
|
42
|
+
try {
|
|
43
|
+
if (!NOTE_ID.test(id)) throw new Error("ファイル名が N-YYYYMMDD-HHMMSS-xxxx.md ではありません");
|
|
44
|
+
const { data, body } = parseFrontmatter(await readFile(join(this.dir(requestId), f), "utf8"));
|
|
45
|
+
if (!body.trim()) throw new Error("本文が空です");
|
|
46
|
+
const createdAt = typeof data.createdAt === "string" ? data.createdAt : "";
|
|
47
|
+
if (!createdAt) throw new Error("createdAt がありません");
|
|
48
|
+
notes.push({ id, requestId, createdAt, createdBy: typeof data.createdBy === "string" ? data.createdBy : undefined, body: body.trim() });
|
|
49
|
+
} catch (e) {
|
|
50
|
+
invalid.push({ file: `notes/${f}`, error: (e as Error).message });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
notes.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
54
|
+
return { notes, invalid };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { readdir, readFile, mkdir, rm, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { PreflightRun, ProposedTask, RunPulse } from "../../shared/types.ts";
|
|
4
|
+
import { parseFrontmatter, stringifyFrontmatter } from "./frontmatter.ts";
|
|
5
|
+
import { writeFileAtomic } from "./fsutil.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* requests/<要望ID>/preflight/<RUN-ID>/
|
|
9
|
+
* run.json 評価結果・ログ・文脈(機械向け)
|
|
10
|
+
* drafts/D1.md ... 細分化されたタスク案(1案 = 1ファイル。人や AI が直接編集できる)
|
|
11
|
+
*/
|
|
12
|
+
export class PreflightStore {
|
|
13
|
+
/** requestsDir: .p3/requests */
|
|
14
|
+
constructor(readonly requestsDir: string) {}
|
|
15
|
+
|
|
16
|
+
runDir(requestId: string, runId: string) {
|
|
17
|
+
return join(this.requestsDir, requestId, "preflight", runId);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async save(run: PreflightRun): Promise<void> {
|
|
21
|
+
const { drafts, ...meta } = run;
|
|
22
|
+
await writeFileAtomic(join(this.runDir(run.requestId, run.id), "run.json"), JSON.stringify(meta, null, 2) + "\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async saveDrafts(requestId: string, runId: string, drafts: ProposedTask[]): Promise<void> {
|
|
26
|
+
const dir = join(this.runDir(requestId, runId), "drafts");
|
|
27
|
+
await rm(dir, { recursive: true, force: true });
|
|
28
|
+
await mkdir(dir, { recursive: true });
|
|
29
|
+
for (const d of drafts) await this.saveDraft(requestId, runId, d);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async saveDraft(requestId: string, runId: string, d: ProposedTask): Promise<void> {
|
|
33
|
+
const { description, ...meta } = d;
|
|
34
|
+
await writeFileAtomic(
|
|
35
|
+
join(this.runDir(requestId, runId), "drafts", `${d.key}.md`),
|
|
36
|
+
stringifyFrontmatter({ ...meta, include: d.include }, description),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async loadDrafts(requestId: string, runId: string): Promise<ProposedTask[]> {
|
|
41
|
+
const dir = join(this.runDir(requestId, runId), "drafts");
|
|
42
|
+
const files = await readdir(dir).catch(() => [] as string[]);
|
|
43
|
+
const drafts: ProposedTask[] = [];
|
|
44
|
+
for (const f of files.filter((f) => f.endsWith(".md"))) {
|
|
45
|
+
const { data, body } = parseFrontmatter(await readFile(join(dir, f), "utf8"));
|
|
46
|
+
drafts.push({
|
|
47
|
+
key: String(data.key ?? f.replace(/\.md$/, "")),
|
|
48
|
+
title: String(data.title ?? ""),
|
|
49
|
+
description: body.trim(),
|
|
50
|
+
areas: Array.isArray(data.areas) ? data.areas.map(String) : [],
|
|
51
|
+
dependsOn: Array.isArray(data.dependsOn) ? data.dependsOn.map(String) : [],
|
|
52
|
+
supersedes: Array.isArray(data.supersedes) ? data.supersedes.map(String) : [],
|
|
53
|
+
include: data.include !== false,
|
|
54
|
+
...("repo" in data ? { repo: data.repo === null || data.repo === "" || data.repo === "none" ? null : String(data.repo) } : {}),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return drafts.sort((a, b) => a.key.localeCompare(b.key, undefined, { numeric: true }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async get(requestId: string, runId: string): Promise<PreflightRun | undefined> {
|
|
61
|
+
try {
|
|
62
|
+
const meta = JSON.parse(await readFile(join(this.runDir(requestId, runId), "run.json"), "utf8"));
|
|
63
|
+
return { ...meta, drafts: await this.loadDrafts(requestId, runId) };
|
|
64
|
+
} catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** run の今の状態だけ(run.json の状態と日時。案・ログは返さない)。ファイルは変えない */
|
|
70
|
+
async pulse(requestId: string, runId: string): Promise<RunPulse> {
|
|
71
|
+
const file = join(this.runDir(requestId, runId), "run.json");
|
|
72
|
+
const info = await stat(file).catch((e: NodeJS.ErrnoException) => e);
|
|
73
|
+
if (info instanceof Error) {
|
|
74
|
+
if (info.code !== "ENOENT") return { id: runId, state: "broken" };
|
|
75
|
+
const dir = await stat(this.runDir(requestId, runId)).catch(() => undefined);
|
|
76
|
+
return { id: runId, state: dir?.isDirectory() ? "pending" : "missing" };
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const meta = JSON.parse(await readFile(file, "utf8")) as Partial<PreflightRun>;
|
|
80
|
+
if (!meta || typeof meta !== "object" || typeof meta.status !== "string") return { id: runId, state: "broken" };
|
|
81
|
+
return {
|
|
82
|
+
id: runId,
|
|
83
|
+
state: "ready",
|
|
84
|
+
status: meta.status,
|
|
85
|
+
...(meta.startedAt ? { startedAt: meta.startedAt } : {}),
|
|
86
|
+
...(meta.finishedAt ? { finishedAt: meta.finishedAt } : {}),
|
|
87
|
+
version: `${info.mtimeMs}:${info.size}`,
|
|
88
|
+
};
|
|
89
|
+
} catch {
|
|
90
|
+
return { id: runId, state: "broken" };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async list(requestId: string): Promise<PreflightRun[]> {
|
|
95
|
+
const ids = await readdir(join(this.requestsDir, requestId, "preflight")).catch(() => [] as string[]);
|
|
96
|
+
const runs = await Promise.all(ids.filter((i) => i.startsWith("PF-")).map((i) => this.get(requestId, i)));
|
|
97
|
+
return newestFirst(runs.filter((r): r is PreflightRun => !!r));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* run を新しい順に並べる。基本は開始時刻だが、時計は戻ることがある(WSL2 の時刻の補正など)ので、
|
|
103
|
+
* 前回を引き継いだ run(previous)は、引き継いだ相手より必ず新しいとする。
|
|
104
|
+
*/
|
|
105
|
+
export function newestFirst<T extends Pick<PreflightRun, "id" | "startedAt" | "previous">>(runs: T[]): T[] {
|
|
106
|
+
const byTime = [...runs].sort((a, b) => b.startedAt.localeCompare(a.startedAt) || b.id.localeCompare(a.id));
|
|
107
|
+
// previous の鎖の長さ(引き継いだ回数)。鎖でつながる run どうしは、長い方が新しい
|
|
108
|
+
const ids = new Map(runs.map((r) => [r.id, r]));
|
|
109
|
+
const depth = new Map<string, number>();
|
|
110
|
+
const chain = (r: T, seen = new Set<string>()): number => {
|
|
111
|
+
if (depth.has(r.id)) return depth.get(r.id)!;
|
|
112
|
+
const prev = r.previous && !seen.has(r.id) ? ids.get(r.previous) : undefined;
|
|
113
|
+
const d = prev ? chain(prev, seen.add(r.id)) + 1 : 0;
|
|
114
|
+
depth.set(r.id, d);
|
|
115
|
+
return d;
|
|
116
|
+
};
|
|
117
|
+
const root = (r: T): string => {
|
|
118
|
+
let cur = r;
|
|
119
|
+
for (const seen = new Set<string>(); cur.previous && ids.has(cur.previous) && !seen.has(cur.id); ) {
|
|
120
|
+
seen.add(cur.id);
|
|
121
|
+
cur = ids.get(cur.previous)!;
|
|
122
|
+
}
|
|
123
|
+
return cur.id;
|
|
124
|
+
};
|
|
125
|
+
// 時刻順の並びで各鎖が占める位置はそのままにし、その位置に鎖の run を引き継いだ順(新しい方から)で入れ直す
|
|
126
|
+
const groups = new Map<string, T[]>();
|
|
127
|
+
for (const r of byTime) (groups.get(root(r)) ?? groups.set(root(r), []).get(root(r))!).push(r);
|
|
128
|
+
for (const g of groups.values()) g.sort((a, b) => chain(b) - chain(a));
|
|
129
|
+
const next = new Map([...groups].map(([k]) => [k, 0]));
|
|
130
|
+
return byTime.map((r) => {
|
|
131
|
+
const k = root(r);
|
|
132
|
+
const i = next.get(k)!;
|
|
133
|
+
next.set(k, i + 1);
|
|
134
|
+
return groups.get(k)![i];
|
|
135
|
+
});
|
|
136
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import YAML from "yaml";
|
|
3
|
+
import type { RepoRef } from "../../shared/repos.ts";
|
|
4
|
+
import type { ProjectConfig } from "../../shared/types.ts";
|
|
5
|
+
import { writeFileAtomic } from "./fsutil.ts";
|
|
6
|
+
|
|
7
|
+
/** 設定の保存(画面・p3 config)で書き換える項目。repos は p3 repo だけが項目ごとに書く(writeRepoEntry) */
|
|
8
|
+
const ORDER: (keyof ProjectConfig)[] = ["name", "description", "milestones", "branchPattern", "preflight"];
|
|
9
|
+
/** ファイルの中の並び */
|
|
10
|
+
const RANK: (keyof ProjectConfig)[] = ["name", "description", "repos", "milestones", "branchPattern", "preflight"];
|
|
11
|
+
|
|
12
|
+
/** project.yaml に書く形。既定値のままの項目や空の項目は省き、人が読み書きしやすく保つ */
|
|
13
|
+
function toPlain(cfg: ProjectConfig): Partial<Record<keyof ProjectConfig, unknown>> {
|
|
14
|
+
return {
|
|
15
|
+
name: cfg.name,
|
|
16
|
+
description: cfg.description || undefined,
|
|
17
|
+
milestones: cfg.milestones.length
|
|
18
|
+
? cfg.milestones.map((m) => Object.fromEntries(Object.entries({ id: m.id, title: m.title, due: m.due, done: m.done || undefined }).filter(([, v]) => v !== undefined)))
|
|
19
|
+
: undefined,
|
|
20
|
+
branchPattern: cfg.branchPattern,
|
|
21
|
+
preflight: cfg.preflight.instructions ? { instructions: cfg.preflight.instructions } : undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* project.yaml を書く。既存のファイルがあれば YAML の文書として更新し、人が書いたコメントを残す。
|
|
27
|
+
*/
|
|
28
|
+
export async function writeProjectFile(path: string, cfg: ProjectConfig): Promise<void> {
|
|
29
|
+
const text = await readFile(path, "utf8").catch(() => "");
|
|
30
|
+
const doc = text.trim() ? YAML.parseDocument(text) : new YAML.Document({});
|
|
31
|
+
const plain = toPlain(cfg);
|
|
32
|
+
doc.delete("statuses"); // 以前は列を設定できた。今は未着手 / 作業中 / 完了で固定
|
|
33
|
+
dropLegacy(doc);
|
|
34
|
+
for (const key of ORDER) {
|
|
35
|
+
const value = plain[key];
|
|
36
|
+
if (value === undefined) doc.delete(key);
|
|
37
|
+
else doc.set(key, doc.createNode(value));
|
|
38
|
+
}
|
|
39
|
+
sortKeys(doc);
|
|
40
|
+
await writeFileAtomic(path, doc.toString({ lineWidth: 0 }));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 以前のプロジェクトの docs(どの保存でも消す)。今は参照先の主なフォルダ(入口)で示す */
|
|
44
|
+
function dropLegacy(doc: YAML.Document) {
|
|
45
|
+
doc.delete("docs");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 後から足した項目も決まった順に並べる(コメントは項目に付いているので一緒に動く) */
|
|
49
|
+
function sortKeys(doc: YAML.Document) {
|
|
50
|
+
if (!YAML.isMap(doc.contents)) return;
|
|
51
|
+
const rank = (k: unknown) => {
|
|
52
|
+
const i = RANK.indexOf(String(YAML.isScalar(k) ? k.value : k) as keyof ProjectConfig);
|
|
53
|
+
return i < 0 ? RANK.length : i;
|
|
54
|
+
};
|
|
55
|
+
doc.contents.items.sort((x, y) => rank(x.key) - rank(y.key));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** project.yaml の repos の1件の形(既定のままの項目は省く) */
|
|
59
|
+
function repoPlain(r: RepoRef): Record<string, unknown> {
|
|
60
|
+
const map = r.map.length
|
|
61
|
+
? Object.fromEntries(r.map.map((m) => [m.path, m.entry ? { role: m.role, entry: m.entry } : m.role]))
|
|
62
|
+
: undefined;
|
|
63
|
+
return Object.fromEntries(
|
|
64
|
+
Object.entries({ role: r.role || undefined, readFirst: r.readFirst || undefined, writable: r.writable || undefined, map }).filter(
|
|
65
|
+
([, v]) => v !== undefined,
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* project.yaml の repos の1件だけを書く(null で消す)。ほかの参照先・項目・コメントは変えない。
|
|
72
|
+
* repos が無ければ作る(名前をキーにした対応で書く)
|
|
73
|
+
*/
|
|
74
|
+
export async function writeRepoEntry(path: string, name: string, entry: RepoRef | null): Promise<void> {
|
|
75
|
+
const text = await readFile(path, "utf8").catch(() => "");
|
|
76
|
+
const doc = text.trim() ? YAML.parseDocument(text) : new YAML.Document({});
|
|
77
|
+
if (doc.errors.length) throw new Error(`project.yaml が壊れています: ${path}(${doc.errors[0].message.split("\n")[0]})`);
|
|
78
|
+
const repos = doc.get("repos");
|
|
79
|
+
if (repos !== undefined && !YAML.isMap(repos)) {
|
|
80
|
+
throw new Error(`project.yaml の repos は「名前: 定義」の対応で書いてください(今の形では p3 repo で書き換えられません): ${path}`);
|
|
81
|
+
}
|
|
82
|
+
dropLegacy(doc);
|
|
83
|
+
if (entry === null) {
|
|
84
|
+
doc.deleteIn(["repos", name]);
|
|
85
|
+
if (YAML.isMap(doc.get("repos")) && !(doc.get("repos") as YAML.YAMLMap).items.length) doc.delete("repos");
|
|
86
|
+
} else {
|
|
87
|
+
if (!YAML.isMap(doc.get("repos"))) doc.set("repos", doc.createNode({}));
|
|
88
|
+
const node = doc.getIn(["repos", name]);
|
|
89
|
+
const plain = repoPlain(entry);
|
|
90
|
+
if (YAML.isMap(node)) {
|
|
91
|
+
// 既にあれば項目ごとに書く(人が書いたコメントや、ここで扱わない項目を残す)
|
|
92
|
+
node.delete("mapFile"); // 以前の構成の説明。今は主なフォルダの入口で示す
|
|
93
|
+
for (const key of ["role", "readFirst", "writable", "map"]) {
|
|
94
|
+
if (plain[key] === undefined) node.delete(key);
|
|
95
|
+
else node.set(key, doc.createNode(plain[key]));
|
|
96
|
+
}
|
|
97
|
+
} else doc.setIn(["repos", name], doc.createNode(plain));
|
|
98
|
+
}
|
|
99
|
+
sortKeys(doc);
|
|
100
|
+
await writeFileAtomic(path, doc.toString({ lineWidth: 0 }));
|
|
101
|
+
}
|