@knihal/pcskit 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 +202 -0
- package/README.md +138 -2
- package/dist/chunk-ZSPASUUI.js +4200 -0
- package/dist/cli.js +648 -0
- package/dist/server-2ZV4YT53.js +301 -0
- package/dist/server-C5RT3W4G.js +282 -0
- package/package.json +68 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
CONNECT_CLIENTS,
|
|
4
|
+
HOOK_EVENTS,
|
|
5
|
+
PcsError,
|
|
6
|
+
addNote,
|
|
7
|
+
addProject,
|
|
8
|
+
addTask,
|
|
9
|
+
autostartStatus,
|
|
10
|
+
buildExport,
|
|
11
|
+
checkExport,
|
|
12
|
+
claudeHooksStatus,
|
|
13
|
+
clientConfigPath,
|
|
14
|
+
compareSnapshots,
|
|
15
|
+
connectClient,
|
|
16
|
+
createAnchor,
|
|
17
|
+
createHandoff,
|
|
18
|
+
currentSession,
|
|
19
|
+
deinitProject,
|
|
20
|
+
describePolicy,
|
|
21
|
+
disconnectClient,
|
|
22
|
+
endSession,
|
|
23
|
+
exitCodeFor,
|
|
24
|
+
formatArgv,
|
|
25
|
+
formatDoctor,
|
|
26
|
+
formatExportMarkdown,
|
|
27
|
+
formatLogLine,
|
|
28
|
+
getStatus,
|
|
29
|
+
getTaskEvidence,
|
|
30
|
+
getTestRun,
|
|
31
|
+
handleHook,
|
|
32
|
+
initConfig,
|
|
33
|
+
initProject,
|
|
34
|
+
installAgentInstructions,
|
|
35
|
+
installAutostart,
|
|
36
|
+
installClaudeHooks,
|
|
37
|
+
isHookEvent,
|
|
38
|
+
linkEvidence,
|
|
39
|
+
listEvents,
|
|
40
|
+
listNotes,
|
|
41
|
+
listSessions,
|
|
42
|
+
listSnapshots,
|
|
43
|
+
listTasks,
|
|
44
|
+
listTestRuns,
|
|
45
|
+
parseHookPayload,
|
|
46
|
+
parseTargets,
|
|
47
|
+
removeProject,
|
|
48
|
+
requestStop,
|
|
49
|
+
requireInitializedRoot,
|
|
50
|
+
resolveNote,
|
|
51
|
+
resume,
|
|
52
|
+
runDoctor,
|
|
53
|
+
runTest,
|
|
54
|
+
serverEntry,
|
|
55
|
+
serverKey,
|
|
56
|
+
startSession,
|
|
57
|
+
startWatcher,
|
|
58
|
+
takeSnapshot,
|
|
59
|
+
takeoverPrompt,
|
|
60
|
+
uninstallAgentInstructions,
|
|
61
|
+
uninstallAutostart,
|
|
62
|
+
uninstallClaudeHooks,
|
|
63
|
+
updateTask,
|
|
64
|
+
verifyAnchor,
|
|
65
|
+
verifyProject,
|
|
66
|
+
watcherState,
|
|
67
|
+
workspaceStatus
|
|
68
|
+
} from "./chunk-ZSPASUUI.js";
|
|
69
|
+
|
|
70
|
+
// src/cli/index.ts
|
|
71
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
72
|
+
import { Command } from "commander";
|
|
73
|
+
function safe(s) {
|
|
74
|
+
return s.replace(/[\u0000-\u001f\u007f-\u009f]/g, "?");
|
|
75
|
+
}
|
|
76
|
+
function formatTask(t) {
|
|
77
|
+
return `#${t.id} [${t.status}] ${safe(t.title)}`;
|
|
78
|
+
}
|
|
79
|
+
function formatSession(s) {
|
|
80
|
+
const state = s.ended_at ? `ended ${s.ended_at} (${s.end_reason})` : "ACTIVE";
|
|
81
|
+
return `#${s.id} ${safe(s.agent)} started ${s.started_at} ${state}`;
|
|
82
|
+
}
|
|
83
|
+
function short(hash) {
|
|
84
|
+
return hash ? hash.slice(0, 7) : "(none)";
|
|
85
|
+
}
|
|
86
|
+
function formatSnapshotLine(s, failedMessage) {
|
|
87
|
+
return s ? `Snapshot #${s.id} (${s.kind}): HEAD ${short(s.head)}, ${s.files.length} changed file(s)${s.truncated ? " [truncated]" : ""}` : failedMessage;
|
|
88
|
+
}
|
|
89
|
+
function integrityLine(i) {
|
|
90
|
+
return i.ok ? `OK (${i.count} events, hash chain and snapshots verified)` : `INTEGRITY FAILURE${i.brokenAt !== void 0 ? ` at event #${i.brokenAt}` : ""}: ${i.reason}`;
|
|
91
|
+
}
|
|
92
|
+
function formatStatus(s) {
|
|
93
|
+
const { project, git, integrity } = s;
|
|
94
|
+
const lines = [
|
|
95
|
+
`Project: ${safe(project.name)}`,
|
|
96
|
+
`Root: ${safe(project.root)}`,
|
|
97
|
+
`Branch: ${safe(git.branch)}`,
|
|
98
|
+
git.head ? `HEAD: ${git.head.hash.slice(0, 7)} ${safe(git.head.subject)} (${git.head.date})` : "HEAD: (no commits yet)",
|
|
99
|
+
s.session ? `Session: #${s.session.id} ${safe(s.session.agent)} (since ${s.session.started_at})` : "Session: (none active)",
|
|
100
|
+
`Watcher: ${s.watcher.running ? `running (pid ${s.watcher.pid})` : s.watcher.stale ? "not running (previous one died)" : "not running"}`,
|
|
101
|
+
`Log: ${integrityLine(integrity)}`
|
|
102
|
+
];
|
|
103
|
+
lines.push("", `Open tasks (${s.openTasks.length}):`);
|
|
104
|
+
if (s.openTasks.length === 0) lines.push(" (none)");
|
|
105
|
+
for (const t of s.openTasks) lines.push(` ${formatTask(t)}`);
|
|
106
|
+
lines.push("", `Changed files (${git.changed.length}):`);
|
|
107
|
+
if (git.changed.length === 0) lines.push(" (working tree clean)");
|
|
108
|
+
for (const f of git.changed) {
|
|
109
|
+
const tag = f.untracked ? "??" : `${f.index}${f.worktree}`.replace(/ /g, "_");
|
|
110
|
+
lines.push(` ${tag} ${safe(f.path)}${f.sensitive ? " [sensitive: content never included]" : ""}`);
|
|
111
|
+
}
|
|
112
|
+
return lines.join("\n");
|
|
113
|
+
}
|
|
114
|
+
function fileTag(f) {
|
|
115
|
+
if (f.sensitive) return " [sensitive: name only]";
|
|
116
|
+
if (f.kind === "missing") return " [deleted]";
|
|
117
|
+
if (f.note === "too-large") return " [too large to hash]";
|
|
118
|
+
return "";
|
|
119
|
+
}
|
|
120
|
+
function formatComparison(c) {
|
|
121
|
+
const lines = [
|
|
122
|
+
`Snapshot #${c.from} -> #${c.to} (all of this was observed by PCS)`,
|
|
123
|
+
`Branch: ${safe(c.branch.from)}${c.branch.changed ? ` -> ${safe(c.branch.to)}` : " (unchanged)"}`,
|
|
124
|
+
`HEAD: ${short(c.head.from)}${c.head.changed ? ` -> ${short(c.head.to)}` : " (unchanged)"}`
|
|
125
|
+
];
|
|
126
|
+
if (c.incomplete) lines.push("Note: a snapshot hit the file cap; lists below may be incomplete.");
|
|
127
|
+
if (c.historyNote) lines.push(`Note: ${c.historyNote}`);
|
|
128
|
+
lines.push("", `Commits made (${c.commits.length}):`);
|
|
129
|
+
if (c.commits.length === 0) lines.push(" (none)");
|
|
130
|
+
for (const k of c.commits) lines.push(` ${short(k.hash)} ${safe(k.subject)}`);
|
|
131
|
+
if (c.committedFiles.length > 0) {
|
|
132
|
+
lines.push("", `Files changed by those commits (${c.committedFiles.length}):`);
|
|
133
|
+
for (const f of c.committedFiles) lines.push(` ${safe(f.status)} ${safe(f.path)}`);
|
|
134
|
+
}
|
|
135
|
+
lines.push("", `New uncommitted changes (${c.added.length}):`);
|
|
136
|
+
if (c.added.length === 0) lines.push(" (none)");
|
|
137
|
+
for (const f of c.added) lines.push(` ${safe(f.path)}${fileTag(f)}`);
|
|
138
|
+
lines.push("", `Changed again since (${c.modified.length}):`);
|
|
139
|
+
if (c.modified.length === 0) lines.push(" (none)");
|
|
140
|
+
for (const m of c.modified) {
|
|
141
|
+
const why = [m.contentChanged ? "content" : "", m.statusChanged ? "git status" : ""].filter(Boolean).join(" + ");
|
|
142
|
+
lines.push(` ${safe(m.path)} (${why})${fileTag(m.after)}`);
|
|
143
|
+
}
|
|
144
|
+
lines.push("", `No longer differ from HEAD (committed, reverted or stashed) (${c.resolved.length}):`);
|
|
145
|
+
if (c.resolved.length === 0) lines.push(" (none)");
|
|
146
|
+
for (const f of c.resolved) lines.push(` ${safe(f.path)}`);
|
|
147
|
+
lines.push("", `Unchanged between snapshots: ${c.unchanged}`);
|
|
148
|
+
return lines.join("\n");
|
|
149
|
+
}
|
|
150
|
+
var program = new Command();
|
|
151
|
+
program.name("pcs").description("Project Continuity System").version("0.1.0");
|
|
152
|
+
program.command("init").description("Register the current git project with PCS").action(async () => {
|
|
153
|
+
const { project, alreadyInitialized } = await initProject(process.cwd());
|
|
154
|
+
console.log(
|
|
155
|
+
alreadyInitialized ? `Already initialized: ${safe(project.name)}` : `Initialized PCS for ${safe(project.name)} (.pcs/)`
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
program.command("status").description("Show the actual project state from git, plus session and open tasks").option("--json", "output JSON").action(async (opts) => {
|
|
159
|
+
const s = await getStatus(process.cwd());
|
|
160
|
+
console.log(opts.json ? JSON.stringify(s, null, 2) : formatStatus(s));
|
|
161
|
+
if (!s.integrity.ok) process.exitCode = 2;
|
|
162
|
+
});
|
|
163
|
+
program.command("verify").description("Verify the event log and snapshots are untampered (optionally against a signed anchor)").option("--anchor <file>", "also check the log against a file made by `pcs anchor`").action(async (opts) => {
|
|
164
|
+
if (opts.anchor) {
|
|
165
|
+
const { integrity, anchor } = await verifyAnchor(process.cwd(), opts.anchor);
|
|
166
|
+
if (!integrity.ok) {
|
|
167
|
+
console.error(integrityLine(integrity));
|
|
168
|
+
process.exitCode = 2;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (!anchor.ok) {
|
|
172
|
+
console.error(`Anchor check FAILED: ${anchor.reason}`);
|
|
173
|
+
process.exitCode = 2;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
console.log(`Integrity ${integrityLine(integrity)}`);
|
|
177
|
+
console.log(`Anchor OK: head #${anchor.anchor?.last_id} (${anchor.anchor?.count} events) from ${anchor.anchor?.created_at}.`);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const r = await verifyProject(process.cwd());
|
|
181
|
+
if (r.ok) {
|
|
182
|
+
console.log(`Integrity ${integrityLine(r)}`);
|
|
183
|
+
} else {
|
|
184
|
+
console.error(integrityLine(r));
|
|
185
|
+
process.exitCode = 2;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
var config = program.command("config").description("Policy: extra redaction, sensitive paths, test allowlist, caps");
|
|
189
|
+
config.command("show").description("Show the effective policy and which config files are in use").action(async () => {
|
|
190
|
+
const root = await requireInitializedRoot(process.cwd());
|
|
191
|
+
const d = describePolicy(root);
|
|
192
|
+
console.log(`Global config: ${d.files.global}${d.sources.includes(d.files.global) ? "" : " (none)"}`);
|
|
193
|
+
console.log(`Project config: ${d.files.project}${d.sources.includes(d.files.project) ? "" : " (none)"}`);
|
|
194
|
+
console.log(`Test allowlist: ${d.policy.testAllow.length === 0 ? "none (any command)" : d.policy.testAllow.map((l) => l.map((t) => t.join(" ")).join(", ")).join(" AND ")}`);
|
|
195
|
+
console.log(`Caps: note ${d.policy.maxNoteChars} chars, test output ${d.policy.maxTestOutputChars} chars`);
|
|
196
|
+
});
|
|
197
|
+
config.command("init").description("Write a sample .pcs/config.json to edit").action(async () => {
|
|
198
|
+
const root = await requireInitializedRoot(process.cwd());
|
|
199
|
+
console.log(`Wrote ${initConfig(root)}`);
|
|
200
|
+
});
|
|
201
|
+
program.command("anchor").description("Write a signed anchor of the log head; keep it outside the project to detect truncation or rewrites").option("-o, --out <file>", "write the anchor to a file instead of stdout").action(async (opts) => {
|
|
202
|
+
const a = await createAnchor(process.cwd());
|
|
203
|
+
const json = `${JSON.stringify(a, null, 2)}
|
|
204
|
+
`;
|
|
205
|
+
if (opts.out) {
|
|
206
|
+
writeFileSync(opts.out, json);
|
|
207
|
+
console.log(`Anchor written to ${opts.out} (head #${a.last_id}, ${a.count} events). Store it outside this project.`);
|
|
208
|
+
} else {
|
|
209
|
+
process.stdout.write(json);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
var session = program.command("session").description("Track which AI agent is working");
|
|
213
|
+
session.command("start").description("Start a session (a session left open is marked interrupted) and snapshot the repo").requiredOption("--agent <name>", "agent name, e.g. claude, gpt, codex").action(async (opts) => {
|
|
214
|
+
const { session: s, interrupted, snapshot: snapshot2 } = await startSession(process.cwd(), opts.agent);
|
|
215
|
+
if (interrupted) {
|
|
216
|
+
console.log(`Previous session #${interrupted.id} (${safe(interrupted.agent)}) was never ended; marked interrupted.`);
|
|
217
|
+
}
|
|
218
|
+
console.log(`Started session #${s.id} (${safe(s.agent)})`);
|
|
219
|
+
console.log(formatSnapshotLine(snapshot2, "Snapshot failed (recorded in the log); session started anyway."));
|
|
220
|
+
});
|
|
221
|
+
session.command("end").description("End the active session and snapshot the repo").option("--reason <reason>", "completed | limit | switched", "completed").option("--note <text>", "short note, e.g. where you stopped").action(async (opts) => {
|
|
222
|
+
const s = await endSession(process.cwd(), opts);
|
|
223
|
+
console.log(`Ended session #${s.id} (${safe(s.agent)}): ${s.end_reason}`);
|
|
224
|
+
console.log(formatSnapshotLine(s.snapshot, "Snapshot failed (recorded in the log)."));
|
|
225
|
+
});
|
|
226
|
+
session.command("current").description("Show the active session").action(async () => {
|
|
227
|
+
const s = await currentSession(process.cwd());
|
|
228
|
+
console.log(s ? formatSession(s) : "(no active session)");
|
|
229
|
+
});
|
|
230
|
+
session.command("list").description("List recent sessions").option("--limit <n>", "how many", "20").option("--json", "output JSON").action(async (opts) => {
|
|
231
|
+
const list = await listSessions(process.cwd(), Number(opts.limit));
|
|
232
|
+
if (opts.json) console.log(JSON.stringify(list, null, 2));
|
|
233
|
+
else if (list.length === 0) console.log("(no sessions)");
|
|
234
|
+
else for (const s of list) console.log(formatSession(s));
|
|
235
|
+
});
|
|
236
|
+
var snapshot = program.command("snapshot").description("Verified snapshots of the repo state");
|
|
237
|
+
snapshot.command("take", { isDefault: true }).description("Record a snapshot now (git state + content hashes of changed files)").action(async () => {
|
|
238
|
+
const s = await takeSnapshot(process.cwd());
|
|
239
|
+
console.log(formatSnapshotLine(s, ""));
|
|
240
|
+
});
|
|
241
|
+
snapshot.command("list").description("List recent snapshots").option("--limit <n>", "how many", "20").option("--json", "output JSON").action(async (opts) => {
|
|
242
|
+
const list = await listSnapshots(process.cwd(), Number(opts.limit));
|
|
243
|
+
if (opts.json) console.log(JSON.stringify(list, null, 2));
|
|
244
|
+
else if (list.length === 0) console.log("(no snapshots)");
|
|
245
|
+
else {
|
|
246
|
+
for (const s of list) {
|
|
247
|
+
const sess = s.session_id === null ? "no session" : `session #${s.session_id}`;
|
|
248
|
+
console.log(`#${s.id} ${s.kind} ${s.taken_at} ${safe(s.branch)} @ ${short(s.head)} ${s.fileCount} file(s) ${sess}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
snapshot.command("diff [from] [to]").description("Compare two snapshots (default: the last two) and show commits made in between").option("--session <id>", "compare the first and last snapshot of a session").option("--json", "output JSON").action(async (from, to, opts) => {
|
|
253
|
+
const c = await compareSnapshots(process.cwd(), {
|
|
254
|
+
from: from === void 0 ? void 0 : Number(from),
|
|
255
|
+
to: to === void 0 ? void 0 : Number(to),
|
|
256
|
+
session: opts.session === void 0 ? void 0 : Number(opts.session)
|
|
257
|
+
});
|
|
258
|
+
console.log(opts.json ? JSON.stringify(c, null, 2) : formatComparison(c));
|
|
259
|
+
});
|
|
260
|
+
var task = program.command("task").description("Track tasks (status changes are recorded as reported events)");
|
|
261
|
+
task.command("add <title...>").description("Add a task").option("--agent <name>", "who is reporting this (default: active session's agent, else user)").action(async (title, opts) => {
|
|
262
|
+
const t = await addTask(process.cwd(), title.join(" "), opts.agent);
|
|
263
|
+
console.log(`Added ${formatTask(t)}`);
|
|
264
|
+
});
|
|
265
|
+
task.command("list").description("List tasks").option("--status <status>", "todo | in_progress | done | blocked").option("--json", "output JSON").action(async (opts) => {
|
|
266
|
+
const tasks = await listTasks(process.cwd(), opts.status);
|
|
267
|
+
const evidence2 = await getTaskEvidence(process.cwd());
|
|
268
|
+
if (opts.json) console.log(JSON.stringify(tasks, null, 2));
|
|
269
|
+
else if (tasks.length === 0) console.log("(no tasks)");
|
|
270
|
+
else {
|
|
271
|
+
for (const t of tasks) {
|
|
272
|
+
const e = evidence2[t.id];
|
|
273
|
+
console.log(formatTask(t) + (e ? ` [evidence: run #${e.run_id} ${e.outcome}]` : ""));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
task.command("update <id>").description("Change a task's status").requiredOption("--status <status>", "todo | in_progress | done | blocked").option("--note <text>", "short note about the change").option("--agent <name>", "who is reporting this (default: active session's agent, else user)").action(async (id, opts) => {
|
|
278
|
+
const t = await updateTask(process.cwd(), Number(id), {
|
|
279
|
+
status: opts.status,
|
|
280
|
+
note: opts.note,
|
|
281
|
+
source: opts.agent
|
|
282
|
+
});
|
|
283
|
+
console.log(`Updated ${formatTask(t)}`);
|
|
284
|
+
});
|
|
285
|
+
function safeText(s) {
|
|
286
|
+
return s.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "?");
|
|
287
|
+
}
|
|
288
|
+
function formatCommand(argv) {
|
|
289
|
+
return safe(formatArgv(argv));
|
|
290
|
+
}
|
|
291
|
+
function describeExit(r) {
|
|
292
|
+
if (r.outcome === "timeout") return "timed out";
|
|
293
|
+
if (r.exit_code !== null) return `exit ${r.exit_code}`;
|
|
294
|
+
return r.signal ? `signal ${safe(r.signal)}` : "no exit code";
|
|
295
|
+
}
|
|
296
|
+
program.command("test").description("Run a command and record its result as verified evidence (put the command after --)").argument("<command...>", "command and arguments, e.g. -- npm test").option("--task <id>", "link this run to a task as evidence").option("--timeout <seconds>", "kill the command after this long", "600").option("--agent <name>", "who links the evidence (default: active session's agent, else user)").action(async (command, opts) => {
|
|
297
|
+
const { run: r, linkedTask } = await runTest(process.cwd(), command, {
|
|
298
|
+
...opts.task !== void 0 ? { taskId: Number(opts.task) } : {},
|
|
299
|
+
...opts.agent !== void 0 ? { agent: opts.agent } : {},
|
|
300
|
+
timeoutMs: Math.round(Number(opts.timeout) * 1e3),
|
|
301
|
+
onOutput: (text, stream) => {
|
|
302
|
+
(stream === "stderr" ? process.stderr : process.stdout).write(text);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
console.log(
|
|
306
|
+
`
|
|
307
|
+
Run #${r.id} ${r.outcome.toUpperCase()} (${describeExit(r)}, ${(r.duration_ms / 1e3).toFixed(1)}s): recorded as verified evidence`
|
|
308
|
+
);
|
|
309
|
+
console.log(
|
|
310
|
+
`Repo state: HEAD ${short(r.head)}, fingerprint ${r.tree_fingerprint ? r.tree_fingerprint.slice(0, 12) : "(not observed)"}`
|
|
311
|
+
);
|
|
312
|
+
if (r.output_truncated) console.log("Note: only the last part of the output was stored.");
|
|
313
|
+
if (linkedTask !== null) console.log(`Linked to task #${linkedTask}`);
|
|
314
|
+
if (r.outcome !== "passed") process.exitCode = 1;
|
|
315
|
+
});
|
|
316
|
+
var evidence = program.command("evidence").description("Verified test runs and the tasks they back");
|
|
317
|
+
evidence.command("list").description("List recent test runs").option("--limit <n>", "how many", "20").option("--json", "output JSON").action(async (opts) => {
|
|
318
|
+
const list = await listTestRuns(process.cwd(), Number(opts.limit));
|
|
319
|
+
if (opts.json) console.log(JSON.stringify(list, null, 2));
|
|
320
|
+
else if (list.length === 0) console.log("(no test runs)");
|
|
321
|
+
else {
|
|
322
|
+
for (const r of list) {
|
|
323
|
+
const sess = r.session_id === null ? "no session" : `session #${r.session_id}`;
|
|
324
|
+
const code = r.exit_code === null ? "-" : String(r.exit_code);
|
|
325
|
+
console.log(
|
|
326
|
+
`#${r.id} ${r.outcome.padEnd(7)} exit ${code} ${(r.duration_ms / 1e3).toFixed(1)}s ${r.ts} @ ${short(r.head)} ${sess} ${formatCommand(r.argv)}`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
evidence.command("show <id>").description("Show one test run with its stored output").option("--json", "output JSON").action(async (id, opts) => {
|
|
332
|
+
const r = await getTestRun(process.cwd(), Number(id));
|
|
333
|
+
if (opts.json) {
|
|
334
|
+
console.log(JSON.stringify(r, null, 2));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
console.log(`Run #${r.id}: ${r.outcome.toUpperCase()} (${describeExit(r)}, ${(r.duration_ms / 1e3).toFixed(1)}s) [verified: PCS ran it]`);
|
|
338
|
+
console.log(`Command: ${formatCommand(r.argv)}${r.cwd ? ` (in ${safe(r.cwd)})` : ""}`);
|
|
339
|
+
console.log(`Started: ${r.ts} HEAD ${short(r.head)} fingerprint ${r.tree_fingerprint ? r.tree_fingerprint.slice(0, 12) : "(not observed)"}`);
|
|
340
|
+
console.log(`Output (${r.output_truncated ? "last part of " : ""}${r.output_bytes} bytes, redacted):`);
|
|
341
|
+
console.log(safeText(r.output));
|
|
342
|
+
});
|
|
343
|
+
evidence.command("link <task> <run>").description("Point a task at a test run as evidence").option("--agent <name>", "who is linking (default: active session's agent, else user)").action(async (task2, run, opts) => {
|
|
344
|
+
await linkEvidence(process.cwd(), Number(task2), Number(run), opts.agent);
|
|
345
|
+
console.log(`Linked run #${Number(run)} to task #${Number(task2)}`);
|
|
346
|
+
});
|
|
347
|
+
function formatNote(n) {
|
|
348
|
+
const state = n.resolved_at ? ` (resolved by ${safe(n.resolved_by ?? "?")} ${n.resolved_at})` : "";
|
|
349
|
+
return `#${n.id} [${n.kind}] ${safe(n.source)}: ${safe(n.text)}${state}`;
|
|
350
|
+
}
|
|
351
|
+
var note = program.command("note").description("Record what the next agent must know (stored as reported claims in the event log)");
|
|
352
|
+
note.command("add <kind> <text...>").description("kind: task (what we are building) | next (an exact next step) | avoid (do not touch, and why) | issue (known problem)").option("--agent <name>", "who is reporting this (default: active session's agent, else user)").action(async (kind, text, opts) => {
|
|
353
|
+
const n = await addNote(process.cwd(), kind, text.join(" "), opts.agent);
|
|
354
|
+
console.log(`Added ${formatNote(n)}`);
|
|
355
|
+
});
|
|
356
|
+
note.command("list").description("List notes (unresolved by default)").option("--kind <kind>", "task | next | avoid | issue").option("--all", "include resolved notes").option("--json", "output JSON").action(async (opts) => {
|
|
357
|
+
const list = await listNotes(process.cwd(), {
|
|
358
|
+
...opts.kind !== void 0 ? { kind: opts.kind } : {},
|
|
359
|
+
...opts.all ? { all: true } : {}
|
|
360
|
+
});
|
|
361
|
+
if (opts.json) console.log(JSON.stringify(list, null, 2));
|
|
362
|
+
else if (list.length === 0) console.log("(no notes)");
|
|
363
|
+
else for (const n of list) console.log(formatNote(n));
|
|
364
|
+
});
|
|
365
|
+
note.command("resolve <id>").description("Mark a next/avoid/issue note as no longer applicable").option("--agent <name>", "who is resolving it (default: active session's agent, else user)").action(async (id, opts) => {
|
|
366
|
+
const n = await resolveNote(process.cwd(), Number(id), opts.agent);
|
|
367
|
+
console.log(`Resolved ${formatNote(n)}`);
|
|
368
|
+
});
|
|
369
|
+
program.command("handoff").description("Write a handoff for the next agent (.pcs/HANDOFF.md): verified, reported and inferred facts kept apart").option("--session <id>", "whose work to hand off (default: the latest session)").option("--out <file>", "write here instead of .pcs/HANDOFF.md").option("--stdout", "print the handoff instead of writing a file").option("--json", "print the structured handoff as JSON instead of writing a file").action(async (opts) => {
|
|
370
|
+
const toTerminal = opts.stdout === true || opts.json === true;
|
|
371
|
+
const { handoff, markdown, path } = await createHandoff(process.cwd(), {
|
|
372
|
+
...opts.session !== void 0 ? { session: Number(opts.session) } : {},
|
|
373
|
+
...opts.out !== void 0 ? { out: opts.out } : {},
|
|
374
|
+
noFile: toTerminal
|
|
375
|
+
});
|
|
376
|
+
if (opts.json) console.log(JSON.stringify(handoff, null, 2));
|
|
377
|
+
else if (opts.stdout) console.log(safeText(markdown));
|
|
378
|
+
else {
|
|
379
|
+
console.log(`Handoff written to ${safe(path ?? "")} (${Buffer.byteLength(markdown)} bytes)`);
|
|
380
|
+
console.log(
|
|
381
|
+
`Completed: ${handoff.tasks.verified.length} verified, ${handoff.tasks.reportedOnly.length + handoff.tasks.stale.length} not verified. In progress: ${handoff.tasks.inProgress.length}. Unresolved issues: ${handoff.issues.length}.`
|
|
382
|
+
);
|
|
383
|
+
if (!handoff.originalTask) console.log('Tip: record the original task with `pcs note add task "..."`.');
|
|
384
|
+
if (handoff.nextSteps.length === 0) console.log('Tip: record exact next steps with `pcs note add next "..."`.');
|
|
385
|
+
}
|
|
386
|
+
if (!handoff.integrity.ok) process.exitCode = 2;
|
|
387
|
+
});
|
|
388
|
+
program.command("resume").description("Pick up the latest handoff: check it, snapshot the repo, print the brief and record the pickup").option("--agent <name>", "who is picking up; starts a session for them (default: the active session's agent)").option("--json", "output JSON").option("--prompt", "wrap the brief in a ready-to-paste prompt for an agent that has no PCS tools").action(async (opts) => {
|
|
389
|
+
const r = await resume(process.cwd(), opts.agent !== void 0 ? { agent: opts.agent } : {});
|
|
390
|
+
if (opts.json) console.log(JSON.stringify(r, null, 2));
|
|
391
|
+
else if (opts.prompt) console.log(takeoverPrompt(safeText(r.brief)));
|
|
392
|
+
else console.log(safeText(r.brief));
|
|
393
|
+
if (!r.checks.integrity.ok) process.exitCode = 2;
|
|
394
|
+
});
|
|
395
|
+
program.command("watch").description("Watch the project and snapshot it when files or HEAD change, so a stopped agent's last work is recorded").option("--stop", "ask the running watcher to stop").option("--status", "show whether a watcher is running").option("--debounce <seconds>", "quiet time before a snapshot is taken (default 2)").option("--stalled <minutes>", "report a stalled session after this much inactivity (default 10)").option("--project <dir>", "project directory (default: the current directory); used by the autostart entry").action(async (opts) => {
|
|
396
|
+
const cwd = opts.project ?? process.cwd();
|
|
397
|
+
if (opts.status) {
|
|
398
|
+
const s = await watcherState(cwd);
|
|
399
|
+
console.log(
|
|
400
|
+
s.running ? `Watcher running (pid ${s.pid}, since ${s.startedAt}).` : s.stale ? "Watcher is not running (a previous one died; its lock is stale)." : "Watcher is not running."
|
|
401
|
+
);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (opts.stop) {
|
|
405
|
+
const r = await requestStop(cwd);
|
|
406
|
+
console.log(r.requested ? "Stop requested; the watcher will exit within a few seconds." : "No watcher is running.");
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const num = (v, unit, name) => {
|
|
410
|
+
if (v === void 0) return void 0;
|
|
411
|
+
const n = Number(v);
|
|
412
|
+
if (!Number.isFinite(n) || n <= 0) throw new PcsError("INVALID_INPUT", `${name} must be a positive number.`);
|
|
413
|
+
return Math.round(n * unit);
|
|
414
|
+
};
|
|
415
|
+
const debounceMs = num(opts.debounce, 1e3, "--debounce");
|
|
416
|
+
const stalledMs = num(opts.stalled, 6e4, "--stalled");
|
|
417
|
+
const w = await startWatcher(cwd, {
|
|
418
|
+
...debounceMs !== void 0 ? { debounceMs } : {},
|
|
419
|
+
...stalledMs !== void 0 ? { stalledMs } : {},
|
|
420
|
+
onLog: (line) => console.log(`[pcs watch] ${safe(line)}`)
|
|
421
|
+
});
|
|
422
|
+
const quit = () => void w.stop("interrupted by user");
|
|
423
|
+
process.once("SIGINT", quit);
|
|
424
|
+
process.once("SIGTERM", quit);
|
|
425
|
+
await w.ready;
|
|
426
|
+
console.log("Press Ctrl+C or run `pcs watch --stop` to stop.");
|
|
427
|
+
await w.done;
|
|
428
|
+
});
|
|
429
|
+
program.command("log").description("Show recent events from the evidence log").option("-n, --limit <n>", "how many of the newest events (default 20)", "20").option("--type <type>", "only this event type; * is a wildcard, e.g. test.*").option("--provenance <p>", "only verified, reported or inferred").option("--after <id>", "only events after this id").option("--json", "output JSON").action(async (opts) => {
|
|
430
|
+
const rows = await listEvents(process.cwd(), {
|
|
431
|
+
limit: Number(opts.limit),
|
|
432
|
+
...opts.type !== void 0 ? { type: opts.type } : {},
|
|
433
|
+
...opts.provenance !== void 0 ? { provenance: opts.provenance } : {},
|
|
434
|
+
...opts.after !== void 0 ? { afterId: Number(opts.after) } : {}
|
|
435
|
+
});
|
|
436
|
+
if (opts.json) console.log(JSON.stringify(rows, null, 2));
|
|
437
|
+
else if (rows.length === 0) console.log("No events.");
|
|
438
|
+
else for (const e of rows) console.log(safeText(formatLogLine(e)));
|
|
439
|
+
});
|
|
440
|
+
program.command("export").description("Write the evidence log as an audit trail (JSON or Markdown) that can be checked without pcskit").option("--format <fmt>", "json or md (default json)", "json").option("-o, --out <file>", "write to a file instead of stdout").option("--from <id>", "first event id to include").option("--to <id>", "last event id to include").option("--check <file>", "re-check a JSON export file instead of exporting").action(async (opts) => {
|
|
441
|
+
if (opts.check) {
|
|
442
|
+
const r = checkExport(readFileSync(opts.check, "utf8"));
|
|
443
|
+
if (r.ok) console.log(`Export OK: ${r.events} events${r.partial ? " (partial export)" : ""} chain correctly.`);
|
|
444
|
+
else {
|
|
445
|
+
console.error(`Export check FAILED${r.brokenAt !== void 0 ? ` at #${r.brokenAt}` : ""}: ${r.reason}`);
|
|
446
|
+
process.exitCode = 2;
|
|
447
|
+
}
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (opts.format !== "json" && opts.format !== "md") throw new PcsError("INVALID_INPUT", "--format must be json or md.");
|
|
451
|
+
const x = await buildExport(process.cwd(), {
|
|
452
|
+
...opts.from !== void 0 ? { from: Number(opts.from) } : {},
|
|
453
|
+
...opts.to !== void 0 ? { to: Number(opts.to) } : {}
|
|
454
|
+
});
|
|
455
|
+
const text = opts.format === "md" ? formatExportMarkdown(x) : `${JSON.stringify(x, null, 2)}
|
|
456
|
+
`;
|
|
457
|
+
if (opts.out) {
|
|
458
|
+
writeFileSync(opts.out, text, { mode: 384 });
|
|
459
|
+
console.log(`Wrote ${x.events.length} events to ${safe(opts.out)}${x.integrity.ok ? "" : " (the log FAILED its integrity check; see the file)"}.`);
|
|
460
|
+
} else process.stdout.write(text);
|
|
461
|
+
if (!x.integrity.ok) process.exitCode = 2;
|
|
462
|
+
});
|
|
463
|
+
program.command("doctor").description("Check the environment and this project, and say how to fix what is wrong").option("--json", "output JSON").option("--fix", "also restrict .pcs to your user when it is open to others").action(async (opts) => {
|
|
464
|
+
const r = await runDoctor(process.cwd(), {}, { fix: opts.fix === true });
|
|
465
|
+
console.log(opts.json ? JSON.stringify(r, null, 2) : safeText(formatDoctor(r)));
|
|
466
|
+
if (!r.ok) process.exitCode = 1;
|
|
467
|
+
});
|
|
468
|
+
program.command("deinit").description("Remove everything PCS added to this project (the .pcs folder, hooks, instruction blocks, login entry)").option("--yes", "actually do it; without this only the plan is shown").action(async (opts) => {
|
|
469
|
+
const r = await deinitProject(process.cwd(), { yes: opts.yes === true });
|
|
470
|
+
console.log(r.dryRun ? "This would remove:" : "Removed:");
|
|
471
|
+
for (const x of r.removed) console.log(` - ${x}`);
|
|
472
|
+
console.log("Left alone:");
|
|
473
|
+
for (const x of r.kept) console.log(` - ${x}`);
|
|
474
|
+
if (r.dryRun) console.log("\nNothing was changed. Run `pcs deinit --yes` to do it.");
|
|
475
|
+
});
|
|
476
|
+
var autostart = program.command("autostart").description("Start the watcher automatically at login (Task Scheduler, systemd user unit or launchd agent)");
|
|
477
|
+
autostart.command("install").description("Run `pcs watch` for this project at every login, and start it now").action(async () => {
|
|
478
|
+
const r = await installAutostart(process.cwd());
|
|
479
|
+
console.log(`Installed ${r.name} (${r.platform}${r.file ? `, ${safe(r.file)}` : ""}).${r.note ? ` ${r.note}` : ""}`);
|
|
480
|
+
});
|
|
481
|
+
autostart.command("uninstall").description("Remove the login entry for this project and stop its watcher").action(async () => {
|
|
482
|
+
const r = await uninstallAutostart(process.cwd());
|
|
483
|
+
console.log(r.removed ? `Removed ${r.name}.` : `Nothing to remove (${r.name} was not installed).`);
|
|
484
|
+
});
|
|
485
|
+
autostart.command("status").description("Show whether the login entry for this project is installed").action(async () => {
|
|
486
|
+
const r = await autostartStatus(process.cwd());
|
|
487
|
+
console.log(`${r.installed ? "Installed" : "Not installed"}: ${r.name} (${r.platform}).`);
|
|
488
|
+
});
|
|
489
|
+
var workspace = program.command("workspace").description("Work across several projects: each keeps its own .pcs, this only lists them");
|
|
490
|
+
workspace.command("add [path]").description("Register an initialized project (default: the current one)").option("--name <name>", "short name used to refer to it (default: the folder name)").action(async (path, opts) => {
|
|
491
|
+
const r = await addProject(path ?? process.cwd(), opts.name);
|
|
492
|
+
console.log(r.added ? `Registered ${safe(r.entry.name)} (${safe(r.entry.root)})` : `Already registered as ${safe(r.entry.name)}.`);
|
|
493
|
+
});
|
|
494
|
+
workspace.command("remove <project>").description("Unregister a project by name or path (its .pcs is not touched)").action((project) => {
|
|
495
|
+
const r = removeProject(project);
|
|
496
|
+
console.log(`Unregistered ${safe(r.name)}. Its .pcs data was left in place.`);
|
|
497
|
+
});
|
|
498
|
+
workspace.command("list").description("Show every registered project with its current state").option("--json", "output JSON").action(async (opts) => {
|
|
499
|
+
const rows = await workspaceStatus();
|
|
500
|
+
if (opts.json) return console.log(JSON.stringify(rows, null, 2));
|
|
501
|
+
if (rows.length === 0) return console.log("No projects registered. Run `pcs workspace add` inside an initialized project.");
|
|
502
|
+
for (const p of rows) {
|
|
503
|
+
if (!p.ok) {
|
|
504
|
+
console.log(`${safe(p.name).padEnd(20)} UNREADABLE ${safe(p.error ?? "")} (${safe(p.root)})`);
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
const flags = [
|
|
508
|
+
p.integrity ? null : "INTEGRITY FAILURE",
|
|
509
|
+
p.session ? `agent: ${p.session.agent}` : "no active session",
|
|
510
|
+
`${p.changedFiles} changed`,
|
|
511
|
+
p.watcher === "running" ? "watcher on" : null,
|
|
512
|
+
p.lastHandoff ? `handoff ${p.lastHandoff.slice(0, 16)}Z` : "no handoff"
|
|
513
|
+
].filter(Boolean);
|
|
514
|
+
console.log(`${safe(p.name).padEnd(20)} ${safe(p.branch ?? "")} @ ${p.head ?? "-"} ${safe(flags.join(" \xC2\xB7 "))}`);
|
|
515
|
+
}
|
|
516
|
+
if (rows.some((p) => p.ok && !p.integrity)) process.exitCode = 2;
|
|
517
|
+
});
|
|
518
|
+
program.command("dashboard").description("Open a read-only local dashboard (127.0.0.1 only, access token in the URL)").option("--port <n>", "port (default 7777; 0 = any free port)").option("--workspace", "show every project registered with `pcs workspace add`, with a picker").option("--open", "open the dashboard in your browser").action(async (opts) => {
|
|
519
|
+
const port = opts.port === void 0 ? void 0 : Number(opts.port);
|
|
520
|
+
if (port !== void 0 && (!Number.isInteger(port) || port < 0 || port > 65535)) {
|
|
521
|
+
throw new PcsError("INVALID_INPUT", "--port must be an integer between 0 and 65535.");
|
|
522
|
+
}
|
|
523
|
+
const { startDashboard } = await import("./server-2ZV4YT53.js");
|
|
524
|
+
const d = await startDashboard({ project: process.cwd(), ...port !== void 0 ? { port } : {}, ...opts.workspace ? { workspace: true } : {} });
|
|
525
|
+
console.log(`PCS dashboard (read-only, this machine only):
|
|
526
|
+
${d.url}
|
|
527
|
+
Press Ctrl+C to stop.`);
|
|
528
|
+
if (opts.open) {
|
|
529
|
+
const { spawn } = await import("child_process");
|
|
530
|
+
const [cmd, args] = process.platform === "win32" ? ["cmd", ["/c", "start", "", d.url]] : process.platform === "darwin" ? ["open", [d.url]] : ["xdg-open", [d.url]];
|
|
531
|
+
spawn(cmd, args, { stdio: "ignore", detached: true }).on("error", () => void 0).unref();
|
|
532
|
+
}
|
|
533
|
+
const quit = () => void d.close().then(() => process.exit(0));
|
|
534
|
+
process.once("SIGINT", quit);
|
|
535
|
+
process.once("SIGTERM", quit);
|
|
536
|
+
});
|
|
537
|
+
program.command("mcp").description("Run PCS as an MCP server over stdio, so agents can call pcs tools themselves").option("--project <dir>", "project directory (default: the current directory)").option("--agent <name>", "default agent name recorded on writes").option("--workspace", "serve every project registered with `pcs workspace add` (tools take a `project` argument)").option("--allow-test", "also expose pcs_test, which runs commands on this machine (off by default)").option("--read-only", "expose only tools that cannot change anything (status, verify, lists); overrides --allow-test").action(async (opts) => {
|
|
538
|
+
const project = opts.project ?? process.cwd();
|
|
539
|
+
if (!opts.workspace) await getStatus(project);
|
|
540
|
+
const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
|
|
541
|
+
const { createMcpServer } = await import("./server-C5RT3W4G.js");
|
|
542
|
+
const server = createMcpServer({
|
|
543
|
+
project,
|
|
544
|
+
...opts.agent !== void 0 ? { agent: opts.agent } : {},
|
|
545
|
+
...opts.allowTest && !opts.readOnly ? { allowTest: true } : {},
|
|
546
|
+
...opts.readOnly ? { readOnly: true } : {},
|
|
547
|
+
...opts.workspace ? { workspace: true } : {}
|
|
548
|
+
});
|
|
549
|
+
await server.connect(new StdioServerTransport());
|
|
550
|
+
console.error(`pcs mcp: serving ${safe(project)}${opts.readOnly ? " (read-only)" : opts.allowTest ? " (pcs_test enabled)" : ""}`);
|
|
551
|
+
});
|
|
552
|
+
async function readStdin(maxBytes = 1e6, timeoutMs = 2e3) {
|
|
553
|
+
if (process.stdin.isTTY) return "";
|
|
554
|
+
return new Promise((resolve) => {
|
|
555
|
+
const chunks = [];
|
|
556
|
+
let size = 0;
|
|
557
|
+
let finished = false;
|
|
558
|
+
const done = () => {
|
|
559
|
+
if (finished) return;
|
|
560
|
+
finished = true;
|
|
561
|
+
clearTimeout(timer);
|
|
562
|
+
process.stdin.pause();
|
|
563
|
+
process.stdin.unref?.();
|
|
564
|
+
resolve(Buffer.concat(chunks).toString("utf8"));
|
|
565
|
+
};
|
|
566
|
+
const timer = setTimeout(done, timeoutMs);
|
|
567
|
+
process.stdin.on("data", (c) => {
|
|
568
|
+
size += c.length;
|
|
569
|
+
if (size > maxBytes) return done();
|
|
570
|
+
chunks.push(c);
|
|
571
|
+
});
|
|
572
|
+
process.stdin.on("end", done);
|
|
573
|
+
process.stdin.on("error", done);
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
program.command("hook <event>").description("Handle an agent lifecycle hook: session-start, pre-compact or session-end (reads the hook JSON on stdin; installed by `pcs hooks install`)").option("--agent <name>", "agent name recorded for the session", "claude-code").action(async (event, opts) => {
|
|
577
|
+
if (!isHookEvent(event)) {
|
|
578
|
+
throw new PcsError("INVALID_INPUT", `Unknown hook event "${safe(event)}". Use: ${HOOK_EVENTS.join(", ")}.`);
|
|
579
|
+
}
|
|
580
|
+
const result = await handleHook(event, parseHookPayload(await readStdin()), { agent: opts.agent });
|
|
581
|
+
if (result.output) process.stdout.write(`${result.output}
|
|
582
|
+
`);
|
|
583
|
+
if (result.warning) {
|
|
584
|
+
console.error(`pcs: ${result.warning}`);
|
|
585
|
+
process.exitCode = 2;
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
var hooks = program.command("hooks").description("Install PCS hooks into Claude Code so sessions start, checkpoint and end without the agent's help");
|
|
589
|
+
hooks.command("install").description("Add SessionStart, PreCompact and SessionEnd hooks to Claude Code's settings (idempotent; other settings are kept)").option("--scope <scope>", "local (default, git-ignored), project (shared with the team) or user (every project)", "local").option("--agent <name>", "agent name for sessions; use the same one in `pcs mcp --agent`", "claude-code").option("--command <exe>", "how the hook runs pcs (default: pcs); e.g. 'node D:/pcs/dist/cli.js'").action(async (opts) => {
|
|
590
|
+
const r = await installClaudeHooks(process.cwd(), {
|
|
591
|
+
scope: opts.scope,
|
|
592
|
+
agent: opts.agent,
|
|
593
|
+
...opts.command !== void 0 ? { command: opts.command } : {}
|
|
594
|
+
});
|
|
595
|
+
const names = (l) => l.length > 0 ? l.join(", ") : "none";
|
|
596
|
+
console.log(`Claude Code settings: ${safe(r.file)} (scope ${r.scope})`);
|
|
597
|
+
console.log(`Added: ${names(r.added)}. Updated: ${names(r.updated)}. Already installed: ${names(r.unchanged)}.`);
|
|
598
|
+
console.log(`Sessions are recorded as agent "${r.agent}". Start the MCP server with the same name: pcs mcp --agent ${r.agent}`);
|
|
599
|
+
console.log("Restart Claude Code for the hooks to take effect.");
|
|
600
|
+
});
|
|
601
|
+
hooks.command("uninstall").description("Remove the PCS hooks (only those; other hooks and settings stay)").option("--scope <scope>", "local (default), project or user", "local").action(async (opts) => {
|
|
602
|
+
const r = await uninstallClaudeHooks(process.cwd(), { scope: opts.scope });
|
|
603
|
+
console.log(r.removed.length > 0 ? `Removed ${r.removed.join(", ")} from ${safe(r.file)}.` : `No PCS hooks in ${safe(r.file)}.`);
|
|
604
|
+
});
|
|
605
|
+
hooks.command("status").description("Show where PCS hooks are installed").action(async () => {
|
|
606
|
+
for (const s of await claudeHooksStatus(process.cwd())) {
|
|
607
|
+
const state = s.installed.length > 0 ? s.installed.join(", ") : s.exists ? "not installed" : "no settings file";
|
|
608
|
+
console.log(`${s.scope.padEnd(8)} ${state} (${safe(s.file)})`);
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
var agents = program.command("agents").description("Add or remove the PCS instruction block in CLAUDE.md, AGENTS.md and .cursor/rules for agents without hooks");
|
|
612
|
+
agents.command("install").description("Write (or refresh) a marked PCS block telling agents to resume at start and hand off at the end").option("--target <list>", "claude, agents, cursor or all (comma-separated); default: the files that exist, else AGENTS.md").action(async (opts) => {
|
|
613
|
+
const parsed = parseTargets(opts.target);
|
|
614
|
+
for (const r of await installAgentInstructions(process.cwd(), { targets: parsed })) {
|
|
615
|
+
console.log(`${r.status.padEnd(9)} ${safe(r.file)}`);
|
|
616
|
+
}
|
|
617
|
+
console.log("These files are part of your repository: review and commit them if you want the team to share them.");
|
|
618
|
+
});
|
|
619
|
+
agents.command("uninstall").description("Remove the PCS block from the instruction files (a file that held only that block is deleted)").action(async () => {
|
|
620
|
+
const results = await uninstallAgentInstructions(process.cwd());
|
|
621
|
+
if (results.length === 0) console.log("No instruction files found.");
|
|
622
|
+
for (const r of results) console.log(`${r.status.padEnd(11)} ${safe(r.file)}`);
|
|
623
|
+
});
|
|
624
|
+
program.command("connect <client>").description("Register the PCS MCP server in an app's config: claude-desktop, cursor or claude-code").option("--global", "cursor: use ~/.cursor/mcp.json instead of this project's .cursor/mcp.json").option("--portable", "write plain `pcs` as the command (needs pcs on the app's PATH) instead of absolute paths").option("--allow-test", "also expose pcs_test, which runs commands on this machine (off by default)").option("--read-only", "expose only tools that cannot change anything").option("--remove", "remove the PCS entry again").option("--print", "show the entry without writing anything").action(async (client, opts) => {
|
|
625
|
+
if (!CONNECT_CLIENTS.includes(client)) {
|
|
626
|
+
throw new PcsError("INVALID_INPUT", `Unknown client "${safe(client)}". Use one of: ${CONNECT_CLIENTS.join(", ")}.`);
|
|
627
|
+
}
|
|
628
|
+
const c = client;
|
|
629
|
+
const o = {
|
|
630
|
+
...opts.global ? { global: true } : {},
|
|
631
|
+
...opts.portable ? { portable: true } : {},
|
|
632
|
+
...opts.allowTest ? { allowTest: true } : {},
|
|
633
|
+
...opts.readOnly ? { readOnly: true } : {}
|
|
634
|
+
};
|
|
635
|
+
if (opts.print) {
|
|
636
|
+
const root = await requireInitializedRoot(process.cwd());
|
|
637
|
+
console.log(JSON.stringify({ mcpServers: { [serverKey(c, root, o)]: serverEntry(root, o) } }, null, 2));
|
|
638
|
+
console.log(`File: ${safe(clientConfigPath(c, root, o))}`);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const r = opts.remove ? await disconnectClient(c, process.cwd(), o) : await connectClient(c, process.cwd(), o);
|
|
642
|
+
console.log(`${r.status.padEnd(9)} ${safe(r.file)} (${safe(r.key)})`);
|
|
643
|
+
if (!opts.remove) console.log(c === "claude-desktop" ? "Restart Claude Desktop fully (quit from the tray), then check that the pcs tools appear." : c === "cursor" ? "Reload Cursor, then open Settings > MCP and check that the server is green." : "Run /mcp in Claude Code and approve the project server.");
|
|
644
|
+
});
|
|
645
|
+
program.parseAsync().catch((err) => {
|
|
646
|
+
console.error(`pcs: ${err instanceof Error ? safe(err.message) : "unexpected error"}`);
|
|
647
|
+
process.exit(exitCodeFor(err));
|
|
648
|
+
});
|