@rucher/cli 0.2.0-beta.4 → 0.2.0-beta.5
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 -4
- package/dist/cli.mjs +878 -915
- package/package.json +2 -2
package/dist/cli.mjs
CHANGED
|
@@ -3,9 +3,9 @@ import { defineCommand, runMain } from "citty";
|
|
|
3
3
|
import { existsSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { basename, dirname, join, resolve } from "node:path";
|
|
5
5
|
import tab from "@bomb.sh/tab/citty";
|
|
6
|
-
import { CarnetError,
|
|
7
|
-
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { CarnetError, LEGEND, NOTES_HEADER, NOTE_KINDS, SIMILAR_PREFIX, SUBAGENT_RULES, WORKSPACE_FILE, addWorkspaceRepo, ambiguousWorkspaceContext, appendPromptFile, archiveWorkspaceSessions, claudeCode, createWorkspace, definitionInstructions, deleteWorkspaceNotes, dropWorkspace, formatClock, handleSessionEnd, handleSessionStart, handleToolUse, handleUserPromptSubmit, hostById, isWorkspaceName, listWorkspaces, loadWorkspace, memoryHalfLife, moveSession, openCarnet, openResolvedCarnet, openWorkspaceCarnet, removeWorkspaceRepo, renderRunDirectory, renderSections, repoNameFor, repoPathFor, requireWorkspace, resolveWorkspace, shortId, workspaceExists, workspaceLabel, workspaceRepoNames, workspaceSessionCounts } from "@rucher/carnet";
|
|
8
7
|
import { styleText } from "node:util";
|
|
8
|
+
import { spawnSync } from "node:child_process";
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
import { createInterface } from "node:readline/promises";
|
|
11
11
|
import { serveCarnet } from "@rucher/carnet/mcp";
|
|
@@ -27,22 +27,22 @@ function readTypedWords({ typedWords }) {
|
|
|
27
27
|
const typed = typedWords.slice(0, -1);
|
|
28
28
|
const passthrough = typed.indexOf("--");
|
|
29
29
|
const own = passthrough === -1 ? typed : typed.slice(0, passthrough);
|
|
30
|
-
const flag = own.
|
|
30
|
+
const flag = own.lastIndexOf("--ws");
|
|
31
31
|
return {
|
|
32
|
-
|
|
33
|
-
words: own.filter((_, index) => !(index > 0 &&
|
|
32
|
+
workspace: flag === -1 ? void 0 : own[flag + 1],
|
|
33
|
+
words: own.filter((_, index) => !(index > 0 && own[index - 1] === "--ws"))
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function inWorkspace(context) {
|
|
37
37
|
return {
|
|
38
38
|
cwd: context.cwd,
|
|
39
39
|
env: context.env,
|
|
40
|
-
|
|
40
|
+
workspace: readTypedWords(context).workspace
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
43
|
function suggestSessions(context, complete) {
|
|
44
44
|
quietly(() => {
|
|
45
|
-
const carnet = openCarnet(
|
|
45
|
+
const carnet = openCarnet(inWorkspace(context));
|
|
46
46
|
try {
|
|
47
47
|
const typed = readTypedWords(context).words;
|
|
48
48
|
const sessions = carnet.sessions.list({ includeTemp: true });
|
|
@@ -59,20 +59,20 @@ function suggestSessions(context, complete) {
|
|
|
59
59
|
}
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
-
function
|
|
62
|
+
function suggestWorkspaces(context, complete) {
|
|
63
63
|
quietly(() => {
|
|
64
|
-
for (const
|
|
64
|
+
for (const workspace of listWorkspaces(context.env)) complete(workspace.name, workspace.runDirectory);
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
function suggestRepos(context, complete) {
|
|
68
68
|
quietly(() => {
|
|
69
|
-
for (const repo of
|
|
69
|
+
for (const repo of resolveWorkspace(inWorkspace(context)).workspace?.repos ?? []) complete(repo.path, repo.name);
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
function suggestRepoNames(context, complete) {
|
|
73
73
|
quietly(() => {
|
|
74
|
-
const {
|
|
75
|
-
for (const repo of
|
|
74
|
+
const { workspace } = resolveWorkspace(inWorkspace(context));
|
|
75
|
+
for (const repo of workspace ? workspaceRepoNames(workspace) : []) complete(repo, "");
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
function suggestNoteKinds(complete) {
|
|
@@ -80,7 +80,7 @@ function suggestNoteKinds(complete) {
|
|
|
80
80
|
}
|
|
81
81
|
function suggestNotes(context, complete) {
|
|
82
82
|
quietly(() => {
|
|
83
|
-
const carnet = openCarnet(
|
|
83
|
+
const carnet = openCarnet(inWorkspace(context));
|
|
84
84
|
try {
|
|
85
85
|
for (const note of carnet.notes.list()) complete(String(note.id), note.text.slice(0, 60));
|
|
86
86
|
} finally {
|
|
@@ -105,119 +105,119 @@ function processContext() {
|
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
107
|
const sessions = (complete) => suggestSessions(processContext(), complete);
|
|
108
|
-
const
|
|
108
|
+
const workspaces = (complete) => suggestWorkspaces(processContext(), complete);
|
|
109
109
|
const repos = (complete) => suggestRepos(processContext(), complete);
|
|
110
110
|
const directories = (complete) => suggestDirectories(processContext(), complete);
|
|
111
111
|
const repoNames = (complete) => suggestRepoNames(processContext(), complete);
|
|
112
112
|
const noteKinds = (complete) => suggestNoteKinds(complete);
|
|
113
113
|
const notes = (complete) => suggestNotes(processContext(), complete);
|
|
114
|
-
const
|
|
114
|
+
const workspace = { ws: workspaces };
|
|
115
115
|
const completionConfig = { subCommands: {
|
|
116
116
|
status: { options: {
|
|
117
117
|
session: sessions,
|
|
118
|
-
...
|
|
118
|
+
...workspace
|
|
119
119
|
} },
|
|
120
120
|
log: { options: {
|
|
121
121
|
session: sessions,
|
|
122
|
-
...
|
|
122
|
+
...workspace
|
|
123
123
|
} },
|
|
124
|
-
new: { options:
|
|
125
|
-
tmp: { options:
|
|
124
|
+
new: { options: workspace },
|
|
125
|
+
tmp: { options: workspace },
|
|
126
126
|
resume: {
|
|
127
127
|
args: { session: sessions },
|
|
128
|
-
options:
|
|
128
|
+
options: workspace
|
|
129
129
|
},
|
|
130
130
|
fork: {
|
|
131
131
|
args: { session: sessions },
|
|
132
|
-
options:
|
|
132
|
+
options: workspace
|
|
133
133
|
},
|
|
134
134
|
sessions: {
|
|
135
|
-
options:
|
|
135
|
+
options: workspace,
|
|
136
136
|
subCommands: {
|
|
137
|
-
list: { options:
|
|
137
|
+
list: { options: workspace },
|
|
138
138
|
merge: {
|
|
139
139
|
args: {
|
|
140
140
|
from: sessions,
|
|
141
141
|
into: sessions
|
|
142
142
|
},
|
|
143
|
-
options:
|
|
143
|
+
options: workspace
|
|
144
144
|
},
|
|
145
145
|
move: {
|
|
146
146
|
args: { session: sessions },
|
|
147
147
|
options: {
|
|
148
|
-
to:
|
|
149
|
-
...
|
|
148
|
+
to: workspaces,
|
|
149
|
+
...workspace
|
|
150
150
|
}
|
|
151
151
|
},
|
|
152
152
|
rename: {
|
|
153
153
|
args: { session: sessions },
|
|
154
|
-
options:
|
|
154
|
+
options: workspace
|
|
155
155
|
},
|
|
156
156
|
promote: {
|
|
157
157
|
args: { session: sessions },
|
|
158
|
-
options:
|
|
158
|
+
options: workspace
|
|
159
159
|
},
|
|
160
160
|
demote: {
|
|
161
161
|
args: { session: sessions },
|
|
162
|
-
options:
|
|
162
|
+
options: workspace
|
|
163
163
|
},
|
|
164
164
|
drop: {
|
|
165
165
|
args: { session: sessions },
|
|
166
|
-
options:
|
|
166
|
+
options: workspace
|
|
167
167
|
},
|
|
168
|
-
clean: { options:
|
|
168
|
+
clean: { options: workspace }
|
|
169
169
|
}
|
|
170
170
|
},
|
|
171
171
|
notes: {
|
|
172
172
|
options: {
|
|
173
173
|
kind: noteKinds,
|
|
174
174
|
repo: repoNames,
|
|
175
|
-
...
|
|
175
|
+
...workspace
|
|
176
176
|
},
|
|
177
177
|
subCommands: {
|
|
178
178
|
list: { options: {
|
|
179
179
|
kind: noteKinds,
|
|
180
180
|
repo: repoNames,
|
|
181
|
-
...
|
|
181
|
+
...workspace
|
|
182
182
|
} },
|
|
183
183
|
show: {
|
|
184
184
|
args: { id: notes },
|
|
185
|
-
options:
|
|
185
|
+
options: workspace
|
|
186
186
|
},
|
|
187
187
|
add: {
|
|
188
188
|
args: { kind: noteKinds },
|
|
189
189
|
options: {
|
|
190
190
|
repo: repoNames,
|
|
191
|
-
...
|
|
191
|
+
...workspace
|
|
192
192
|
}
|
|
193
193
|
},
|
|
194
194
|
drop: {
|
|
195
195
|
args: { id: notes },
|
|
196
196
|
options: {
|
|
197
197
|
"replaced-by": notes,
|
|
198
|
-
...
|
|
198
|
+
...workspace
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
},
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
args: {
|
|
207
|
-
options:
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
203
|
+
ws: {
|
|
204
|
+
options: workspace,
|
|
205
|
+
subCommands: {
|
|
206
|
+
drop: { args: { name: workspaces } },
|
|
207
|
+
show: { options: workspace },
|
|
208
|
+
add: {
|
|
209
|
+
args: { path: directories },
|
|
210
|
+
options: workspace
|
|
211
|
+
},
|
|
212
|
+
remove: {
|
|
213
|
+
args: { path: repos },
|
|
214
|
+
options: workspace
|
|
215
|
+
},
|
|
216
|
+
sync: { options: workspace },
|
|
217
|
+
edit: { options: workspace },
|
|
218
|
+
path: { options: workspace }
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
221
|
} };
|
|
222
222
|
async function setupCompletion(main) {
|
|
223
223
|
const subCommands = await (typeof main.subCommands === "function" ? main.subCommands() : main.subCommands);
|
|
@@ -239,165 +239,137 @@ async function setupCompletion(main) {
|
|
|
239
239
|
};
|
|
240
240
|
}
|
|
241
241
|
// package.json
|
|
242
|
-
var version = "0.2.0-beta.
|
|
243
|
-
// src/
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return {
|
|
247
|
-
bold: style("bold"),
|
|
248
|
-
dim: style("dim"),
|
|
249
|
-
yellow: style("yellow"),
|
|
250
|
-
red: style("red"),
|
|
251
|
-
cyan: style("cyan")
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
// src/group-option.ts
|
|
255
|
-
const groupArgument = { group: {
|
|
256
|
-
type: "string",
|
|
257
|
-
alias: "g",
|
|
258
|
-
description: "Group name (default: from RUCHER_GROUP, the workspace or the repo you are in)"
|
|
259
|
-
} };
|
|
260
|
-
function announceGroup(io, resolution) {
|
|
261
|
-
if (!resolution.group) return;
|
|
262
|
-
if (resolution.origin === "workspace") io.stderr(`group ${resolution.group.name} (from its workspace)\n`);
|
|
263
|
-
else if (resolution.origin === "repo" && resolution.repo) io.stderr(`group ${resolution.group.name} (from ${resolution.repo.name})\n`);
|
|
264
|
-
}
|
|
265
|
-
function warnNoGroup(carnet, io) {
|
|
266
|
-
if (!carnet.group) io.stderr("no group here: pass -g after the subcommand\n");
|
|
267
|
-
}
|
|
268
|
-
function openCliCarnet(io, group) {
|
|
269
|
-
const { carnet, resolution } = openResolvedCarnet({
|
|
270
|
-
cwd: io.cwd,
|
|
271
|
-
env: io.env,
|
|
272
|
-
group
|
|
273
|
-
});
|
|
274
|
-
announceGroup(io, resolution);
|
|
275
|
-
return carnet;
|
|
276
|
-
}
|
|
277
|
-
function currentGroup(io, group) {
|
|
278
|
-
const resolution = resolveGroup({
|
|
279
|
-
cwd: io.cwd,
|
|
280
|
-
env: io.env,
|
|
281
|
-
group
|
|
282
|
-
});
|
|
283
|
-
const found = requireGroup(resolution.group);
|
|
284
|
-
announceGroup(io, resolution);
|
|
285
|
-
return found;
|
|
286
|
-
}
|
|
287
|
-
// src/commands/group.ts
|
|
288
|
-
function carnetErrors(io, run) {
|
|
242
|
+
var version = "0.2.0-beta.5";
|
|
243
|
+
// src/commands/hook.ts
|
|
244
|
+
const host = claudeCode;
|
|
245
|
+
function runHook(io, run, hookEventName) {
|
|
289
246
|
try {
|
|
290
|
-
|
|
247
|
+
run();
|
|
291
248
|
} catch (error) {
|
|
292
|
-
if (error instanceof CarnetError)
|
|
293
|
-
|
|
294
|
-
|
|
249
|
+
if (error instanceof CarnetError && error.code === "NO_WORKSPACE") return 0;
|
|
250
|
+
if (error instanceof CarnetError && error.code === "WORKSPACE_AMBIGUOUS") {
|
|
251
|
+
if (hookEventName) {
|
|
252
|
+
io.stderr(`rucher: ${error.message}\n`);
|
|
253
|
+
const additionalContext = ambiguousWorkspaceContext(error.message);
|
|
254
|
+
io.stdout(`${JSON.stringify({ hookSpecificOutput: {
|
|
255
|
+
hookEventName,
|
|
256
|
+
additionalContext
|
|
257
|
+
} })}\n`);
|
|
258
|
+
}
|
|
259
|
+
return 0;
|
|
295
260
|
}
|
|
296
|
-
|
|
261
|
+
io.stderr(`rucher hook: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
297
262
|
}
|
|
263
|
+
return 0;
|
|
298
264
|
}
|
|
299
|
-
function
|
|
300
|
-
const
|
|
301
|
-
|
|
265
|
+
function withCarnet$1(payload, io, use) {
|
|
266
|
+
const carnet = openCarnet({
|
|
267
|
+
cwd: payload.cwd ?? io.cwd,
|
|
268
|
+
env: io.env,
|
|
269
|
+
sessionId: payload.session_id
|
|
270
|
+
});
|
|
271
|
+
try {
|
|
272
|
+
const known = payload.session_id ? carnet.sessions.get(payload.session_id) : void 0;
|
|
273
|
+
if (!carnet.workspace && !io.env.RUCHER_SESSION_ID && !known) return;
|
|
274
|
+
use(carnet);
|
|
275
|
+
} finally {
|
|
276
|
+
carnet.close();
|
|
277
|
+
}
|
|
302
278
|
}
|
|
303
|
-
function
|
|
304
|
-
return
|
|
279
|
+
function sessionStart(stdinText, io) {
|
|
280
|
+
return runHook(io, () => {
|
|
281
|
+
const payload = host.parseSessionPayload(stdinText);
|
|
282
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
283
|
+
const { additionalContext, notices } = handleSessionStart(carnet, payload, {
|
|
284
|
+
host: host.id,
|
|
285
|
+
processId: host.processId(io.env),
|
|
286
|
+
RUCHER_PURGE_DAYS: io.env.RUCHER_PURGE_DAYS,
|
|
287
|
+
RUCHER_HOME: io.env.RUCHER_HOME,
|
|
288
|
+
RUCHER_SESSION_ID: io.env.RUCHER_SESSION_ID,
|
|
289
|
+
RUCHER_SESSION_KIND: io.env.RUCHER_SESSION_KIND,
|
|
290
|
+
RUCHER_SESSION_NAME: io.env.RUCHER_SESSION_NAME,
|
|
291
|
+
RUCHER_PROMPT_VERSION: io.env.RUCHER_PROMPT_VERSION,
|
|
292
|
+
RUCHER_MEMORY_MAX_CHARACTERS: io.env.RUCHER_MEMORY_MAX_CHARACTERS,
|
|
293
|
+
RUCHER_MEMORY_HALF_LIFE_DAYS: io.env.RUCHER_MEMORY_HALF_LIFE_DAYS
|
|
294
|
+
});
|
|
295
|
+
for (const notice of notices) io.stderr(`${notice}\n`);
|
|
296
|
+
if (additionalContext) {
|
|
297
|
+
const hookSpecificOutput = {
|
|
298
|
+
hookEventName: "SessionStart",
|
|
299
|
+
additionalContext
|
|
300
|
+
};
|
|
301
|
+
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}, "SessionStart");
|
|
305
305
|
}
|
|
306
|
-
function
|
|
307
|
-
return
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
return
|
|
306
|
+
function subagentStart(stdinText, io) {
|
|
307
|
+
return runHook(io, () => {
|
|
308
|
+
const payload = host.parseSubagentPayload(stdinText);
|
|
309
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
310
|
+
carnet.agents.pairSubagent(payload.session_id, payload.agent_id);
|
|
311
|
+
});
|
|
312
|
+
if (host.subagentInheritsContext(payload)) return;
|
|
313
|
+
const hookSpecificOutput = {
|
|
314
|
+
hookEventName: "SubagentStart",
|
|
315
|
+
additionalContext: SUBAGENT_RULES
|
|
316
|
+
};
|
|
317
|
+
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
313
318
|
});
|
|
314
319
|
}
|
|
315
|
-
function
|
|
316
|
-
return
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
return 0;
|
|
320
|
+
function subagentStop(stdinText, io) {
|
|
321
|
+
return runHook(io, () => {
|
|
322
|
+
const payload = host.parseSubagentPayload(stdinText);
|
|
323
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
324
|
+
carnet.agents.stopSubagent(payload.session_id, payload.agent_id, host.subagentNames(payload));
|
|
325
|
+
});
|
|
322
326
|
});
|
|
323
327
|
}
|
|
324
|
-
function
|
|
325
|
-
return
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
});
|
|
328
|
+
function userPromptSubmit(stdinText, io) {
|
|
329
|
+
return runHook(io, () => {
|
|
330
|
+
const payload = host.parsePromptPayload(stdinText);
|
|
331
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
332
|
+
handleUserPromptSubmit(carnet, host, payload);
|
|
333
|
+
});
|
|
334
|
+
}, "UserPromptSubmit");
|
|
331
335
|
}
|
|
332
|
-
function
|
|
333
|
-
return
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
336
|
+
function postToolUse(stdinText, io) {
|
|
337
|
+
return runHook(io, () => {
|
|
338
|
+
const payload = host.parseToolPayload(stdinText);
|
|
339
|
+
if (payload.agent_id || !payload.path) return;
|
|
340
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
341
|
+
const additionalContext = handleToolUse(carnet, payload, {
|
|
342
|
+
RUCHER_MEMORY_MAX_CHARACTERS: io.env.RUCHER_MEMORY_MAX_CHARACTERS,
|
|
343
|
+
RUCHER_MEMORY_HALF_LIFE_DAYS: io.env.RUCHER_MEMORY_HALF_LIFE_DAYS
|
|
344
|
+
});
|
|
345
|
+
if (!additionalContext) return;
|
|
346
|
+
const hookSpecificOutput = {
|
|
347
|
+
hookEventName: "PostToolUse",
|
|
348
|
+
additionalContext
|
|
349
|
+
};
|
|
350
|
+
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
351
|
+
});
|
|
338
352
|
});
|
|
339
353
|
}
|
|
340
|
-
function
|
|
341
|
-
return
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
354
|
+
function sessionEnd(stdinText, io) {
|
|
355
|
+
return runHook(io, () => {
|
|
356
|
+
const payload = host.parseSessionEndPayload(stdinText);
|
|
357
|
+
withCarnet$1(payload, io, (carnet) => {
|
|
358
|
+
handleSessionEnd(carnet, payload);
|
|
359
|
+
});
|
|
345
360
|
});
|
|
346
361
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
...io.env,
|
|
357
|
-
RUCHER_EDIT_FILE: file
|
|
358
|
-
}
|
|
359
|
-
}).status ?? 1;
|
|
362
|
+
// src/color.ts
|
|
363
|
+
function paint(io) {
|
|
364
|
+
const style = (name) => (text) => io.color ? styleText(name, text, { validateStream: false }) : text;
|
|
365
|
+
return {
|
|
366
|
+
bold: style("bold"),
|
|
367
|
+
dim: style("dim"),
|
|
368
|
+
yellow: style("yellow"),
|
|
369
|
+
red: style("red"),
|
|
370
|
+
cyan: style("cyan")
|
|
360
371
|
};
|
|
361
372
|
}
|
|
362
|
-
function invalidGroupFile(name, io) {
|
|
363
|
-
try {
|
|
364
|
-
loadGroup(name, io.env);
|
|
365
|
-
return null;
|
|
366
|
-
} catch (error) {
|
|
367
|
-
return error instanceof Error ? error.message : String(error);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
async function groupEdit(io, options = {}) {
|
|
371
|
-
let group;
|
|
372
|
-
let file;
|
|
373
|
-
try {
|
|
374
|
-
group = currentGroup(io, options.group);
|
|
375
|
-
file = join(group.source, options.json ? GROUP_FILE : sourceInstructions(group));
|
|
376
|
-
} catch (error) {
|
|
377
|
-
if (error instanceof CarnetError) {
|
|
378
|
-
io.stderr(`error: ${error.message}\n`);
|
|
379
|
-
return 1;
|
|
380
|
-
}
|
|
381
|
-
throw error;
|
|
382
|
-
}
|
|
383
|
-
const original = readFileSync(file, "utf8");
|
|
384
|
-
const openEditor = options.openEditor ?? openEditorFor(io);
|
|
385
|
-
for (;;) {
|
|
386
|
-
if (openEditor(file) !== 0) {
|
|
387
|
-
io.stderr(`error: ${editorCommand(io.env)} exited with an error, nothing synced\n`);
|
|
388
|
-
return 1;
|
|
389
|
-
}
|
|
390
|
-
const problem = options.json ? invalidGroupFile(group.name, io) : null;
|
|
391
|
-
if (!problem) break;
|
|
392
|
-
io.stderr(`error: ${GROUP_FILE} is invalid: ${problem}\n`);
|
|
393
|
-
if (!options.confirm || !await options.confirm("Edit it again? [Y/n] ")) {
|
|
394
|
-
writeFileSync(file, original);
|
|
395
|
-
io.stderr(`restored the previous ${GROUP_FILE}\n`);
|
|
396
|
-
return 1;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return groupSync(io, { group: group.name });
|
|
400
|
-
}
|
|
401
373
|
// src/session.ts
|
|
402
374
|
var CliError = class extends Error {
|
|
403
375
|
hint;
|
|
@@ -419,394 +391,27 @@ function findSession(carnet, reference) {
|
|
|
419
391
|
function resolveSession(carnet, reference) {
|
|
420
392
|
if (reference) return findSession(carnet, reference);
|
|
421
393
|
const session = carnet.sessions.default();
|
|
422
|
-
if (!session) throw new CliError("No session in this
|
|
394
|
+
if (!session) throw new CliError("No session in this workspace yet", "start a Claude Code session inside the workspace");
|
|
423
395
|
return session;
|
|
424
396
|
}
|
|
425
|
-
// src/commands/
|
|
426
|
-
function
|
|
427
|
-
const
|
|
428
|
-
|
|
397
|
+
// src/commands/log.ts
|
|
398
|
+
function formatEvents(events, io) {
|
|
399
|
+
const colors = paint(io);
|
|
400
|
+
if (events.length === 0) return colors.dim("no events yet");
|
|
401
|
+
return events.map((event) => {
|
|
402
|
+
return [
|
|
403
|
+
colors.dim(formatClock(new Date(event.at))),
|
|
404
|
+
colors.bold(event.type.padEnd(9)),
|
|
405
|
+
event.agent ?? "",
|
|
406
|
+
event.link ?? event.note ?? "",
|
|
407
|
+
event.commit ? colors.cyan(event.commit.slice(0, 7)) : ""
|
|
408
|
+
].filter(Boolean).join(" ");
|
|
409
|
+
}).join("\n");
|
|
429
410
|
}
|
|
430
|
-
function
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
session.kind,
|
|
435
|
-
session.status,
|
|
436
|
-
repoPathFor(carnet.group, session.cwd) ?? session.repo ?? "",
|
|
437
|
-
lastActive(session.lastActiveAt, now),
|
|
438
|
-
open ? `${open} agent${open > 1 ? "s" : ""}` : "",
|
|
439
|
-
session.purpose ?? ""
|
|
440
|
-
];
|
|
441
|
-
}
|
|
442
|
-
function sessionsOnce(carnet, io, options = {}) {
|
|
443
|
-
const colors = paint(io);
|
|
444
|
-
const sessions = carnet.sessions.list({
|
|
445
|
-
includeTemp: options.all,
|
|
446
|
-
includeArchived: options.all
|
|
447
|
-
});
|
|
448
|
-
warnNoGroup(carnet, io);
|
|
449
|
-
if (sessions.length === 0) {
|
|
450
|
-
const hint = options.all ? "" : " (temp and archived ones are hidden, use --all)";
|
|
451
|
-
if (carnet.group) io.stdout(`${colors.dim(`no active session yet${hint}`)}\n`);
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const now = options.now ?? /* @__PURE__ */ new Date();
|
|
455
|
-
const rows = sessions.map((session) => ({
|
|
456
|
-
session,
|
|
457
|
-
cells: row(carnet, session, now)
|
|
458
|
-
}));
|
|
459
|
-
const widths = rows.reduce((current, { cells }) => cells.map((cell, column) => Math.max(current[column] ?? 0, cell.length)), []);
|
|
460
|
-
const lines = rows.map(({ session, cells }) => {
|
|
461
|
-
return cells.map((cell, column) => {
|
|
462
|
-
const padded = column < cells.length - 1 ? cell.padEnd(widths[column] ?? 0) : cell;
|
|
463
|
-
if (column === 0) return colors.bold(padded);
|
|
464
|
-
if (column === 1 && session.kind === "temp") return colors.dim(padded);
|
|
465
|
-
if (column === 2 && session.status === "archived") return colors.dim(padded);
|
|
466
|
-
return padded;
|
|
467
|
-
}).join(" ").trimEnd();
|
|
468
|
-
});
|
|
469
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
470
|
-
}
|
|
471
|
-
function sessionsMerge(carnet, fromReference, intoReference, io) {
|
|
472
|
-
const from = findSession(carnet, fromReference);
|
|
473
|
-
const into = findSession(carnet, intoReference);
|
|
474
|
-
const result = carnet.sessions.merge(from.id, into.id);
|
|
475
|
-
const label = (session) => session.name ?? session.id.slice(0, 8);
|
|
476
|
-
const lines = [
|
|
477
|
-
`merged ${label(from)} into ${label(result.session)}`,
|
|
478
|
-
` agents moved: ${result.movedAgents.join(", ") || "none"}`,
|
|
479
|
-
...result.closedAgents.length ? [` agents closed (name already used): ${result.closedAgents.join(", ")}`] : [],
|
|
480
|
-
` events moved: ${result.movedEvents}`,
|
|
481
|
-
` table line moved: ${result.movedTable ? "yes" : "no"}`
|
|
482
|
-
];
|
|
483
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
484
|
-
}
|
|
485
|
-
function label(session) {
|
|
486
|
-
return session.name ?? shortId(session.id);
|
|
487
|
-
}
|
|
488
|
-
function groupNamed(name, io) {
|
|
489
|
-
if (!groupExists(name, io.env)) throw new CliError(`No group named "${name}"`, "see rucher groups list");
|
|
490
|
-
return loadGroup(name, io.env);
|
|
491
|
-
}
|
|
492
|
-
function sessionsMove(carnet, reference, groupReference, io, options = {}) {
|
|
493
|
-
const session = findSession(carnet, reference);
|
|
494
|
-
const target = groupNamed(groupReference, io).name;
|
|
495
|
-
const result = moveSession(carnet, session.id, target, options);
|
|
496
|
-
const into = result.into ? ` into ${label(result.into)}` : "";
|
|
497
|
-
const lines = [
|
|
498
|
-
`${options.dryRun ? "would move" : "moved"} ${label(session)} from ${groupLabel(carnet.group)} to ${target}${into}`,
|
|
499
|
-
` agents moved: ${result.movedAgents.join(", ") || "none"}`,
|
|
500
|
-
...result.skippedAgents.length ? [` agents left out (name already used): ${result.skippedAgents.join(", ")}`] : [],
|
|
501
|
-
` events moved: ${result.movedEvents}`,
|
|
502
|
-
` table line moved: ${result.movedTable ? "yes" : "no"}`,
|
|
503
|
-
...result.keptNotes ? [` notes kept in ${groupLabel(carnet.group)}: ${result.keptNotes}`] : []
|
|
504
|
-
];
|
|
505
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
506
|
-
}
|
|
507
|
-
function sessionsRename(carnet, reference, name, io) {
|
|
508
|
-
const session = carnet.sessions.rename(findSession(carnet, reference).id, name);
|
|
509
|
-
io.stdout(`renamed to ${session.name}; rucher resume ${session.name} gives Claude the same title\n`);
|
|
510
|
-
}
|
|
511
|
-
function sessionsPromote(carnet, reference, name, io) {
|
|
512
|
-
const session = carnet.sessions.promote(findSession(carnet, reference).id, name);
|
|
513
|
-
io.stdout(`${label(session)} is now active\n`);
|
|
514
|
-
}
|
|
515
|
-
function sessionsDemote(carnet, reference, io) {
|
|
516
|
-
const session = carnet.sessions.demote(findSession(carnet, reference).id);
|
|
517
|
-
io.stdout(`${label(session)} is now temp, cleaned by rucher sessions clean once idle\n`);
|
|
518
|
-
}
|
|
519
|
-
function purgeTranscripts(session, io) {
|
|
520
|
-
const files = hostById(session.host).transcriptFiles(session, io.env);
|
|
521
|
-
for (const file of files) {
|
|
522
|
-
rmSync(file, {
|
|
523
|
-
recursive: true,
|
|
524
|
-
force: true
|
|
525
|
-
});
|
|
526
|
-
io.stdout(` deleted ${file}\n`);
|
|
527
|
-
}
|
|
528
|
-
if (files.length === 0) io.stdout(" no transcript found\n");
|
|
529
|
-
}
|
|
530
|
-
function sessionsDrop(carnet, reference, io, options = {}) {
|
|
531
|
-
const session = findSession(carnet, reference);
|
|
532
|
-
if (options.purge && session.status === "running") throw new CliError(`Session ${label(session)} is running`, "drop it with --purge once it has ended");
|
|
533
|
-
carnet.sessions.drop(session.id);
|
|
534
|
-
io.stdout(`archived ${label(session)}\n`);
|
|
535
|
-
if (options.purge) purgeTranscripts(session, io);
|
|
536
|
-
}
|
|
537
|
-
const UNIT_MILLISECONDS = {
|
|
538
|
-
m: 6e4,
|
|
539
|
-
h: 36e5,
|
|
540
|
-
d: 864e5
|
|
541
|
-
};
|
|
542
|
-
function parseDuration(value) {
|
|
543
|
-
const match = /^(\d+(?:\.\d+)?)([mhd]?)$/.exec(value.trim());
|
|
544
|
-
if (!match) throw new CliError(`Invalid duration "${value}"`, "use a number of days, or 30m, 12h, 7d");
|
|
545
|
-
return Number(match[1]) * (UNIT_MILLISECONDS[match[2] || "d"] ?? 0);
|
|
546
|
-
}
|
|
547
|
-
function processIsAlive(processId) {
|
|
548
|
-
try {
|
|
549
|
-
process.kill(processId, 0);
|
|
550
|
-
return true;
|
|
551
|
-
} catch (error) {
|
|
552
|
-
return error.code === "EPERM";
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
function sessionsClean(carnet, io, options = {}) {
|
|
556
|
-
const olderThan = parseDuration(options.olderThan ?? "7d");
|
|
557
|
-
if (!options.dryRun) for (const session of carnet.sessions.resetDead(options.isAlive ?? processIsAlive)) io.stdout(`${label(session)} was marked running but its process is gone: now idle\n`);
|
|
558
|
-
const stale = carnet.sessions.staleTemp(olderThan);
|
|
559
|
-
if (stale.length === 0) {
|
|
560
|
-
io.stdout(`no temp session idle for more than ${options.olderThan ?? "7d"}\n`);
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
|
-
for (const session of stale) {
|
|
564
|
-
if (options.dryRun) {
|
|
565
|
-
io.stdout(`would archive ${label(session)}${options.purge ? " and delete its transcript" : ""}\n`);
|
|
566
|
-
continue;
|
|
567
|
-
}
|
|
568
|
-
carnet.sessions.drop(session.id);
|
|
569
|
-
io.stdout(`archived ${label(session)}\n`);
|
|
570
|
-
if (options.purge) purgeTranscripts(session, io);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
// src/commands/groups.ts
|
|
574
|
-
function plural(count, word) {
|
|
575
|
-
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
576
|
-
}
|
|
577
|
-
function groupsList(io) {
|
|
578
|
-
const colors = paint(io);
|
|
579
|
-
const groups = listGroups(io.env);
|
|
580
|
-
if (groups.length === 0) {
|
|
581
|
-
io.stdout(`${colors.dim("no group yet, create one with `rucher groups create <name>`")}\n`);
|
|
582
|
-
return 0;
|
|
583
|
-
}
|
|
584
|
-
const carnet = openGroupCarnet(null, { env: io.env });
|
|
585
|
-
const sessions = groupSessionCounts(carnet.db);
|
|
586
|
-
carnet.close();
|
|
587
|
-
const width = Math.max(...groups.map((group) => group.name.length));
|
|
588
|
-
const lines = groups.map((group) => `${colors.bold(group.name.padEnd(width))} ${plural(group.repos.length, "repo")} ${plural(sessions.get(group.name) ?? 0, "session")} ${colors.dim(group.workspace)}`);
|
|
589
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
590
|
-
return 0;
|
|
591
|
-
}
|
|
592
|
-
function groupsCreate(name, io, options = {}) {
|
|
593
|
-
const colors = paint(io);
|
|
594
|
-
const group = createGroup(name, {
|
|
595
|
-
repos: options.repos?.map((repo) => resolve(io.cwd, repo)),
|
|
596
|
-
env: io.env
|
|
597
|
-
});
|
|
598
|
-
const result = renderWorkspace(group, { env: io.env });
|
|
599
|
-
const lines = [
|
|
600
|
-
`${colors.bold(group.name)} created`,
|
|
601
|
-
` source: ${group.source}`,
|
|
602
|
-
` workspace: ${group.workspace}`,
|
|
603
|
-
` repos: ${group.repos.length ? group.repos.map((repo) => repo.path).join(", ") : "none yet, add them with `rucher group add <path> -g " + group.name + "`"}`,
|
|
604
|
-
...result.written.map((path) => ` written ${path}`),
|
|
605
|
-
"",
|
|
606
|
-
"Accept the Claude Code trust prompt in its first session: until then the workspace settings are ignored, and rucher passes the repos with --add-dir.",
|
|
607
|
-
`Next: \`rucher group edit -g ${group.name}\` for its instructions, then \`rucher new <session> -g ${group.name}\`.`
|
|
608
|
-
];
|
|
609
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
610
|
-
return 0;
|
|
611
|
-
}
|
|
612
|
-
function liveSessions(carnet, isAlive) {
|
|
613
|
-
return carnet.sessions.list({ includeTemp: true }).filter((session) => session.status === "running" && session.hostPid !== null && isAlive(session.hostPid));
|
|
614
|
-
}
|
|
615
|
-
function refuseLive(carnet, action, isAlive) {
|
|
616
|
-
const live = liveSessions(carnet, isAlive);
|
|
617
|
-
if (live.length) throw new CliError(`${plural(live.length, "session")} of ${carnet.group?.name} still running: ${live.map((session) => session.name ?? session.id.slice(0, 8)).join(", ")}`, `end them before you ${action} the group`);
|
|
618
|
-
}
|
|
619
|
-
function groupsRename(from, to, io, options = {}) {
|
|
620
|
-
const carnet = openGroupCarnet(loadGroup(from, io.env), { env: io.env });
|
|
621
|
-
try {
|
|
622
|
-
refuseLive(carnet, "rename", options.isAlive ?? processIsAlive);
|
|
623
|
-
const group = renameGroup(from, to, io.env);
|
|
624
|
-
try {
|
|
625
|
-
renameGroupRows(carnet.db, from, to);
|
|
626
|
-
} catch (error) {
|
|
627
|
-
renameGroup(to, from, io.env);
|
|
628
|
-
throw error;
|
|
629
|
-
}
|
|
630
|
-
io.stdout(`renamed ${from} to ${to}; its workspace stays at ${group.workspace}\n`);
|
|
631
|
-
return 0;
|
|
632
|
-
} finally {
|
|
633
|
-
carnet.close();
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
function groupsDrop(name, io, options = {}) {
|
|
637
|
-
const carnet = openGroupCarnet(loadGroup(name, io.env), { env: io.env });
|
|
638
|
-
try {
|
|
639
|
-
refuseLive(carnet, "drop", options.isAlive ?? processIsAlive);
|
|
640
|
-
const at = (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
|
|
641
|
-
const archived = archiveGroupSessions(carnet.db, name, at);
|
|
642
|
-
const deletedNotes = options.purge ? deleteGroupNotes(carnet.db, name) : 0;
|
|
643
|
-
const group = dropGroup(name, {
|
|
644
|
-
purge: options.purge,
|
|
645
|
-
env: io.env
|
|
646
|
-
});
|
|
647
|
-
const lines = [
|
|
648
|
-
`dropped ${name}: source ${group.source} removed`,
|
|
649
|
-
` sessions archived: ${archived}`,
|
|
650
|
-
options.purge ? ` notes deleted: ${deletedNotes}; workspace ${group.workspace} removed` : ` notes and workspace kept; recreate the group under the same name to see them again`
|
|
651
|
-
];
|
|
652
|
-
io.stdout(`${lines.join("\n")}\n`);
|
|
653
|
-
return 0;
|
|
654
|
-
} finally {
|
|
655
|
-
carnet.close();
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
function groupsErrors(io, run) {
|
|
659
|
-
try {
|
|
660
|
-
return run();
|
|
661
|
-
} catch (error) {
|
|
662
|
-
if (error instanceof CliError) {
|
|
663
|
-
io.stderr(`error: ${error.message}${error.hint ? `\n ${error.hint}` : ""}\n`);
|
|
664
|
-
return 1;
|
|
665
|
-
}
|
|
666
|
-
if (error instanceof CarnetError) {
|
|
667
|
-
io.stderr(`error: ${error.message}\n`);
|
|
668
|
-
return 1;
|
|
669
|
-
}
|
|
670
|
-
throw error;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
// src/commands/hook.ts
|
|
674
|
-
const host = claudeCode;
|
|
675
|
-
function runHook(io, run, hookEventName) {
|
|
676
|
-
try {
|
|
677
|
-
run();
|
|
678
|
-
} catch (error) {
|
|
679
|
-
if (error instanceof CarnetError && error.code === "NO_GROUP") return 0;
|
|
680
|
-
if (error instanceof CarnetError && error.code === "GROUP_AMBIGUOUS") {
|
|
681
|
-
if (hookEventName) {
|
|
682
|
-
io.stderr(`rucher: ${error.message}\n`);
|
|
683
|
-
const additionalContext = ambiguousGroupContext(error.message);
|
|
684
|
-
io.stdout(`${JSON.stringify({ hookSpecificOutput: {
|
|
685
|
-
hookEventName,
|
|
686
|
-
additionalContext
|
|
687
|
-
} })}\n`);
|
|
688
|
-
}
|
|
689
|
-
return 0;
|
|
690
|
-
}
|
|
691
|
-
io.stderr(`rucher hook: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
692
|
-
}
|
|
693
|
-
return 0;
|
|
694
|
-
}
|
|
695
|
-
function withCarnet$1(payload, io, use) {
|
|
696
|
-
const carnet = openCarnet({
|
|
697
|
-
cwd: payload.cwd ?? io.cwd,
|
|
698
|
-
env: io.env,
|
|
699
|
-
sessionId: payload.session_id
|
|
700
|
-
});
|
|
701
|
-
try {
|
|
702
|
-
const known = payload.session_id ? carnet.sessions.get(payload.session_id) : void 0;
|
|
703
|
-
if (!carnet.group && !io.env.RUCHER_SESSION_ID && !known) return;
|
|
704
|
-
use(carnet);
|
|
705
|
-
} finally {
|
|
706
|
-
carnet.close();
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
function sessionStart(stdinText, io) {
|
|
710
|
-
return runHook(io, () => {
|
|
711
|
-
const payload = host.parseSessionPayload(stdinText);
|
|
712
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
713
|
-
const { additionalContext, notices } = handleSessionStart(carnet, payload, {
|
|
714
|
-
host: host.id,
|
|
715
|
-
processId: host.processId(io.env),
|
|
716
|
-
RUCHER_PURGE_DAYS: io.env.RUCHER_PURGE_DAYS,
|
|
717
|
-
RUCHER_HOME: io.env.RUCHER_HOME,
|
|
718
|
-
RUCHER_SESSION_ID: io.env.RUCHER_SESSION_ID,
|
|
719
|
-
RUCHER_SESSION_KIND: io.env.RUCHER_SESSION_KIND,
|
|
720
|
-
RUCHER_SESSION_NAME: io.env.RUCHER_SESSION_NAME,
|
|
721
|
-
RUCHER_PROMPT_VERSION: io.env.RUCHER_PROMPT_VERSION,
|
|
722
|
-
RUCHER_MEMORY_MAX_CHARACTERS: io.env.RUCHER_MEMORY_MAX_CHARACTERS,
|
|
723
|
-
RUCHER_MEMORY_HALF_LIFE_DAYS: io.env.RUCHER_MEMORY_HALF_LIFE_DAYS
|
|
724
|
-
});
|
|
725
|
-
for (const notice of notices) io.stderr(`${notice}\n`);
|
|
726
|
-
if (additionalContext) {
|
|
727
|
-
const hookSpecificOutput = {
|
|
728
|
-
hookEventName: "SessionStart",
|
|
729
|
-
additionalContext
|
|
730
|
-
};
|
|
731
|
-
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
}, "SessionStart");
|
|
735
|
-
}
|
|
736
|
-
function subagentStart(stdinText, io) {
|
|
737
|
-
return runHook(io, () => {
|
|
738
|
-
const payload = host.parseSubagentPayload(stdinText);
|
|
739
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
740
|
-
carnet.agents.pairSubagent(payload.session_id, payload.agent_id);
|
|
741
|
-
});
|
|
742
|
-
if (host.subagentInheritsContext(payload)) return;
|
|
743
|
-
const hookSpecificOutput = {
|
|
744
|
-
hookEventName: "SubagentStart",
|
|
745
|
-
additionalContext: SUBAGENT_RULES
|
|
746
|
-
};
|
|
747
|
-
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
748
|
-
});
|
|
749
|
-
}
|
|
750
|
-
function subagentStop(stdinText, io) {
|
|
751
|
-
return runHook(io, () => {
|
|
752
|
-
const payload = host.parseSubagentPayload(stdinText);
|
|
753
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
754
|
-
carnet.agents.stopSubagent(payload.session_id, payload.agent_id, host.subagentNames(payload));
|
|
755
|
-
});
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
function userPromptSubmit(stdinText, io) {
|
|
759
|
-
return runHook(io, () => {
|
|
760
|
-
const payload = host.parsePromptPayload(stdinText);
|
|
761
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
762
|
-
handleUserPromptSubmit(carnet, host, payload);
|
|
763
|
-
});
|
|
764
|
-
}, "UserPromptSubmit");
|
|
765
|
-
}
|
|
766
|
-
function postToolUse(stdinText, io) {
|
|
767
|
-
return runHook(io, () => {
|
|
768
|
-
const payload = host.parseToolPayload(stdinText);
|
|
769
|
-
if (payload.agent_id || !payload.path) return;
|
|
770
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
771
|
-
const additionalContext = handleToolUse(carnet, payload, {
|
|
772
|
-
RUCHER_MEMORY_MAX_CHARACTERS: io.env.RUCHER_MEMORY_MAX_CHARACTERS,
|
|
773
|
-
RUCHER_MEMORY_HALF_LIFE_DAYS: io.env.RUCHER_MEMORY_HALF_LIFE_DAYS
|
|
774
|
-
});
|
|
775
|
-
if (!additionalContext) return;
|
|
776
|
-
const hookSpecificOutput = {
|
|
777
|
-
hookEventName: "PostToolUse",
|
|
778
|
-
additionalContext
|
|
779
|
-
};
|
|
780
|
-
io.stdout(`${JSON.stringify({ hookSpecificOutput })}\n`);
|
|
781
|
-
});
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
function sessionEnd(stdinText, io) {
|
|
785
|
-
return runHook(io, () => {
|
|
786
|
-
const payload = host.parseSessionEndPayload(stdinText);
|
|
787
|
-
withCarnet$1(payload, io, (carnet) => {
|
|
788
|
-
handleSessionEnd(carnet, payload);
|
|
789
|
-
});
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
// src/commands/log.ts
|
|
793
|
-
function formatEvents(events, io) {
|
|
794
|
-
const colors = paint(io);
|
|
795
|
-
if (events.length === 0) return colors.dim("no events yet");
|
|
796
|
-
return events.map((event) => {
|
|
797
|
-
return [
|
|
798
|
-
colors.dim(formatClock(new Date(event.at))),
|
|
799
|
-
colors.bold(event.type.padEnd(9)),
|
|
800
|
-
event.agent ?? "",
|
|
801
|
-
event.link ?? event.note ?? "",
|
|
802
|
-
event.commit ? colors.cyan(event.commit.slice(0, 7)) : ""
|
|
803
|
-
].filter(Boolean).join(" ");
|
|
804
|
-
}).join("\n");
|
|
805
|
-
}
|
|
806
|
-
function formatDay(date) {
|
|
807
|
-
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
808
|
-
const day = String(date.getDate()).padStart(2, "0");
|
|
809
|
-
return `${date.getFullYear()}-${month}-${day}`;
|
|
411
|
+
function formatDay(date) {
|
|
412
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
413
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
414
|
+
return `${date.getFullYear()}-${month}-${day}`;
|
|
810
415
|
}
|
|
811
416
|
function markdownEvent(event) {
|
|
812
417
|
const parts = [`${formatClock(new Date(event.at))} **${event.type}**`];
|
|
@@ -833,13 +438,48 @@ function formatEventsMarkdown(events, title) {
|
|
|
833
438
|
return lines.join("\n");
|
|
834
439
|
}
|
|
835
440
|
function sessionTitle(carnet, session) {
|
|
836
|
-
return `${
|
|
441
|
+
return `${workspaceLabel(carnet.workspace)} · ${session.name ?? session.id.slice(0, 8)}`;
|
|
837
442
|
}
|
|
838
443
|
function logOnce(carnet, io, options) {
|
|
839
444
|
const session = resolveSession(carnet, options.session);
|
|
840
445
|
const events = carnet.events.read(session.id, options.count, options.agents);
|
|
841
446
|
io.stdout(`${options.md ? formatEventsMarkdown(events, sessionTitle(carnet, session)) : formatEvents(events, io)}\n`);
|
|
842
447
|
}
|
|
448
|
+
// src/workspace-option.ts
|
|
449
|
+
const workspaceArgument = { ws: {
|
|
450
|
+
type: "string",
|
|
451
|
+
description: "Workspace name (default: from RUCHER_WS, the run directory or the repo you are in)"
|
|
452
|
+
} };
|
|
453
|
+
function forwardWorkspace({ args }) {
|
|
454
|
+
if (args.ws) process.env.RUCHER_WS = args.ws;
|
|
455
|
+
}
|
|
456
|
+
function announceWorkspace(io, resolution) {
|
|
457
|
+
if (!resolution.workspace) return;
|
|
458
|
+
if (resolution.origin === "run-directory") io.stderr(`workspace ${resolution.workspace.name} (from its run directory)\n`);
|
|
459
|
+
else if (resolution.origin === "repo" && resolution.repo) io.stderr(`workspace ${resolution.workspace.name} (from ${resolution.repo.name})\n`);
|
|
460
|
+
}
|
|
461
|
+
function warnNoWorkspace(carnet, io) {
|
|
462
|
+
if (!carnet.workspace) io.stderr("no workspace here: pass --ws\n");
|
|
463
|
+
}
|
|
464
|
+
function openCliCarnet(io, workspace) {
|
|
465
|
+
const { carnet, resolution } = openResolvedCarnet({
|
|
466
|
+
cwd: io.cwd,
|
|
467
|
+
env: io.env,
|
|
468
|
+
workspace
|
|
469
|
+
});
|
|
470
|
+
announceWorkspace(io, resolution);
|
|
471
|
+
return carnet;
|
|
472
|
+
}
|
|
473
|
+
function currentWorkspace(io, workspace) {
|
|
474
|
+
const resolution = resolveWorkspace({
|
|
475
|
+
cwd: io.cwd,
|
|
476
|
+
env: io.env,
|
|
477
|
+
workspace
|
|
478
|
+
});
|
|
479
|
+
const found = requireWorkspace(resolution.workspace);
|
|
480
|
+
announceWorkspace(io, resolution);
|
|
481
|
+
return found;
|
|
482
|
+
}
|
|
843
483
|
// src/commands/notes.ts
|
|
844
484
|
const KIND_TITLES = {
|
|
845
485
|
dead_end: "Dead ends",
|
|
@@ -888,7 +528,7 @@ function markdown(carnet, notes) {
|
|
|
888
528
|
});
|
|
889
529
|
return `## ${KIND_TITLES[kind]}\n\n${lines.join("\n")}`;
|
|
890
530
|
});
|
|
891
|
-
return `# Memory: ${
|
|
531
|
+
return `# Memory: ${workspaceLabel(carnet.workspace)}\n\n${sections.join("\n\n") || "No notes."}\n`;
|
|
892
532
|
}
|
|
893
533
|
function notesList(carnet, io, options = {}) {
|
|
894
534
|
const notes = options.stale ? carnet.notes.stale(halfLife(io)) : carnet.notes.list({
|
|
@@ -903,9 +543,9 @@ function notesList(carnet, io, options = {}) {
|
|
|
903
543
|
return;
|
|
904
544
|
}
|
|
905
545
|
const colors = paint(io);
|
|
906
|
-
|
|
546
|
+
warnNoWorkspace(carnet, io);
|
|
907
547
|
if (notes.length === 0) {
|
|
908
|
-
if (carnet.
|
|
548
|
+
if (carnet.workspace) io.stdout(`${colors.dim(options.stale ? "no note to review" : "no notes")}\n`);
|
|
909
549
|
return;
|
|
910
550
|
}
|
|
911
551
|
const blocks = grouped(notes).map(([kind, kindNotes]) => `${colors.bold(KIND_TITLES[kind])}\n${kindNotes.map((note) => noteLine(io, note)).join("\n")}`);
|
|
@@ -975,13 +615,13 @@ function splitPassthrough(rawArgs) {
|
|
|
975
615
|
function positionals(own) {
|
|
976
616
|
return own.filter((argument) => !argument.startsWith("-"));
|
|
977
617
|
}
|
|
978
|
-
function launchEnv(
|
|
618
|
+
function launchEnv(workspace, session, promptVersion, kind) {
|
|
979
619
|
return {
|
|
980
620
|
RUCHER_SESSION_ID: session.id,
|
|
981
621
|
RUCHER_PROMPT_VERSION: promptVersion,
|
|
982
622
|
RUCHER_SESSION_NAME: session.name ?? "",
|
|
983
623
|
CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD: "1",
|
|
984
|
-
...
|
|
624
|
+
...workspace ? { RUCHER_WS: workspace.name } : {},
|
|
985
625
|
...kind ? { RUCHER_SESSION_KIND: kind } : {}
|
|
986
626
|
};
|
|
987
627
|
}
|
|
@@ -991,41 +631,41 @@ function planNew(carnet, io, options) {
|
|
|
991
631
|
id: randomUUID(),
|
|
992
632
|
kind: options.kind,
|
|
993
633
|
name: options.name,
|
|
994
|
-
cwd: carnet.
|
|
995
|
-
repo: repoNameFor(carnet.
|
|
634
|
+
cwd: carnet.workspace?.runDirectory ?? io.cwd,
|
|
635
|
+
repo: repoNameFor(carnet.workspace, io.cwd)
|
|
996
636
|
});
|
|
997
637
|
const host = hostById(session.host);
|
|
998
|
-
if (carnet.
|
|
638
|
+
if (carnet.workspace) renderRunDirectory(carnet.workspace, {
|
|
999
639
|
env: io.env,
|
|
1000
640
|
host: session.host
|
|
1001
641
|
});
|
|
1002
|
-
const prompt = appendPromptFile(carnet.
|
|
642
|
+
const prompt = appendPromptFile(carnet.workspace, session, io.env);
|
|
1003
643
|
return {
|
|
1004
644
|
...host.launchCommand(session.id, {
|
|
1005
645
|
name: session.name,
|
|
1006
646
|
appendPromptFile: prompt.path,
|
|
1007
|
-
directories: repoDirectories(carnet.
|
|
647
|
+
directories: repoDirectories(carnet.workspace),
|
|
1008
648
|
passthrough: options.passthrough
|
|
1009
649
|
}),
|
|
1010
|
-
cwd: carnet.
|
|
1011
|
-
env: launchEnv(carnet.
|
|
650
|
+
cwd: carnet.workspace?.runDirectory ?? io.cwd,
|
|
651
|
+
env: launchEnv(carnet.workspace, session, prompt.version),
|
|
1012
652
|
sessionId: session.id
|
|
1013
653
|
};
|
|
1014
654
|
}
|
|
1015
|
-
function repoDirectories(
|
|
1016
|
-
return (
|
|
655
|
+
function repoDirectories(workspace) {
|
|
656
|
+
return (workspace?.repos ?? []).map((repo) => repo.path).filter((path) => existsSync(path));
|
|
1017
657
|
}
|
|
1018
|
-
function
|
|
1019
|
-
if (!session.
|
|
1020
|
-
return carnet.
|
|
658
|
+
function sessionWorkspace(carnet, session, env) {
|
|
659
|
+
if (!session.workspace || !isWorkspaceName(session.workspace)) return null;
|
|
660
|
+
return carnet.workspace?.name === session.workspace ? carnet.workspace : loadWorkspace(session.workspace, env);
|
|
1021
661
|
}
|
|
1022
662
|
function prepareSession(carnet, session, env) {
|
|
1023
|
-
const
|
|
1024
|
-
if (
|
|
663
|
+
const workspace = sessionWorkspace(carnet, session, env);
|
|
664
|
+
if (workspace) renderRunDirectory(workspace, {
|
|
1025
665
|
env,
|
|
1026
666
|
host: session.host
|
|
1027
667
|
});
|
|
1028
|
-
return
|
|
668
|
+
return workspace;
|
|
1029
669
|
}
|
|
1030
670
|
function sessionDirectory(session) {
|
|
1031
671
|
if (session.cwd && existsSync(session.cwd)) return session.cwd;
|
|
@@ -1034,17 +674,17 @@ function sessionDirectory(session) {
|
|
|
1034
674
|
function planResume(carnet, io, reference, passthrough = []) {
|
|
1035
675
|
const session = findSession(carnet, reference);
|
|
1036
676
|
if (session.status === "archived") io.stderr(`warning: session ${reference} is archived\n`);
|
|
1037
|
-
const
|
|
677
|
+
const workspace = prepareSession(carnet, session, io.env);
|
|
1038
678
|
const cwd = sessionDirectory(session);
|
|
1039
|
-
const prompt = appendPromptFile(
|
|
679
|
+
const prompt = appendPromptFile(workspace, session, io.env);
|
|
1040
680
|
return {
|
|
1041
681
|
...hostById(session.host).resumeCommand(session, {
|
|
1042
682
|
appendPromptFile: prompt.path,
|
|
1043
|
-
directories: repoDirectories(
|
|
683
|
+
directories: repoDirectories(workspace),
|
|
1044
684
|
passthrough
|
|
1045
685
|
}),
|
|
1046
686
|
cwd,
|
|
1047
|
-
env: launchEnv(
|
|
687
|
+
env: launchEnv(workspace, session, prompt.version),
|
|
1048
688
|
sessionId: session.id
|
|
1049
689
|
};
|
|
1050
690
|
}
|
|
@@ -1052,19 +692,19 @@ function planFork(carnet, io, reference, options = {}) {
|
|
|
1052
692
|
if (options.active && !options.name) throw new CliError("An active fork needs a name", "rucher fork <session> <name> --active");
|
|
1053
693
|
if (options.name && carnet.sessions.byName(options.name)) throw new CliError(`A session named "${options.name}" already exists`);
|
|
1054
694
|
const parent = findSession(carnet, reference);
|
|
1055
|
-
const
|
|
695
|
+
const workspace = prepareSession(carnet, parent, io.env);
|
|
1056
696
|
const cwd = sessionDirectory(parent);
|
|
1057
697
|
const kind = options.active ? "active" : "temp";
|
|
1058
698
|
return {
|
|
1059
699
|
...hostById(parent.host).forkCommand(parent, {
|
|
1060
700
|
name: options.name,
|
|
1061
|
-
appendPromptFile: appendPromptFile(
|
|
1062
|
-
directories: repoDirectories(
|
|
701
|
+
appendPromptFile: appendPromptFile(workspace, parent, io.env).path,
|
|
702
|
+
directories: repoDirectories(workspace),
|
|
1063
703
|
passthrough: options.passthrough
|
|
1064
704
|
}),
|
|
1065
705
|
cwd,
|
|
1066
706
|
env: {
|
|
1067
|
-
...launchEnv(
|
|
707
|
+
...launchEnv(workspace, parent, parent.promptVersion ?? "", kind),
|
|
1068
708
|
RUCHER_SESSION_NAME: options.name ?? ""
|
|
1069
709
|
},
|
|
1070
710
|
parentId: parent.id,
|
|
@@ -1122,54 +762,202 @@ function pickerLines(sessions, now = /* @__PURE__ */ new Date()) {
|
|
|
1122
762
|
const widths = (rows[0] ?? []).map((_, column) => Math.max(...rows.map((row) => (row[column] ?? "").length)));
|
|
1123
763
|
return rows.map((row) => row.map((cell, column) => column < row.length - 1 ? cell.padEnd(widths[column] ?? 0) : cell).join(" "));
|
|
1124
764
|
}
|
|
1125
|
-
function hasFzf() {
|
|
1126
|
-
return spawnSync("fzf", ["--version"], { stdio: "ignore" }).status === 0;
|
|
765
|
+
function hasFzf() {
|
|
766
|
+
return spawnSync("fzf", ["--version"], { stdio: "ignore" }).status === 0;
|
|
767
|
+
}
|
|
768
|
+
function pickWithFzf(sessions, lines) {
|
|
769
|
+
const result = spawnSync("fzf", [
|
|
770
|
+
"--with-nth=2..",
|
|
771
|
+
"--no-sort",
|
|
772
|
+
"--prompt=session> "
|
|
773
|
+
], {
|
|
774
|
+
input: lines.join("\n"),
|
|
775
|
+
stdio: [
|
|
776
|
+
"pipe",
|
|
777
|
+
"pipe",
|
|
778
|
+
"inherit"
|
|
779
|
+
],
|
|
780
|
+
encoding: "utf8"
|
|
781
|
+
});
|
|
782
|
+
return sessions[Number(result.stdout?.trim().split(/\s+/)[0]) - 1];
|
|
783
|
+
}
|
|
784
|
+
async function pickFromList(sessions, lines) {
|
|
785
|
+
process.stderr.write(`${lines.join("\n")}\n`);
|
|
786
|
+
const prompt = createInterface({
|
|
787
|
+
input: process.stdin,
|
|
788
|
+
output: process.stderr
|
|
789
|
+
});
|
|
790
|
+
try {
|
|
791
|
+
const answer = await prompt.question("session number> ");
|
|
792
|
+
return sessions[Number(answer.trim()) - 1];
|
|
793
|
+
} finally {
|
|
794
|
+
prompt.close();
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
async function pickSession(carnet) {
|
|
798
|
+
const sessions = pickerSessions(carnet);
|
|
799
|
+
if (sessions.length === 0) throw new CliError("No session to resume in this workspace", "rucher new <name>");
|
|
800
|
+
if (!process.stdin.isTTY) throw new CliError("No session given", "rucher resume <name, id or id prefix>");
|
|
801
|
+
const lines = pickerLines(sessions);
|
|
802
|
+
const picked = hasFzf() ? pickWithFzf(sessions, lines) : await pickFromList(sessions, lines);
|
|
803
|
+
if (!picked) throw new CliError("No session picked");
|
|
804
|
+
return picked;
|
|
805
|
+
}
|
|
806
|
+
// src/commands/serve.ts
|
|
807
|
+
function serve(io) {
|
|
808
|
+
serveCarnet({
|
|
809
|
+
cwd: io.cwd,
|
|
810
|
+
env: io.env,
|
|
811
|
+
sessionId: claudeCode.sessionIdFromEnvironment(io.env)
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
// src/commands/sessions.ts
|
|
815
|
+
function lastActive(at, now) {
|
|
816
|
+
const date = new Date(at);
|
|
817
|
+
return date.toDateString() === now.toDateString() ? formatClock(date) : date.toISOString().slice(0, 10);
|
|
818
|
+
}
|
|
819
|
+
function row(carnet, session, now) {
|
|
820
|
+
const open = carnet.agents.list(session.id).length;
|
|
821
|
+
return [
|
|
822
|
+
session.name ?? session.id.slice(0, 8),
|
|
823
|
+
session.kind,
|
|
824
|
+
session.status,
|
|
825
|
+
repoPathFor(carnet.workspace, session.cwd) ?? session.repo ?? "",
|
|
826
|
+
lastActive(session.lastActiveAt, now),
|
|
827
|
+
open ? `${open} agent${open > 1 ? "s" : ""}` : "",
|
|
828
|
+
session.purpose ?? ""
|
|
829
|
+
];
|
|
830
|
+
}
|
|
831
|
+
function sessionsOnce(carnet, io, options = {}) {
|
|
832
|
+
const colors = paint(io);
|
|
833
|
+
const sessions = carnet.sessions.list({
|
|
834
|
+
includeTemp: options.all,
|
|
835
|
+
includeArchived: options.all
|
|
836
|
+
});
|
|
837
|
+
warnNoWorkspace(carnet, io);
|
|
838
|
+
if (sessions.length === 0) {
|
|
839
|
+
const hint = options.all ? "" : " (temp and archived ones are hidden, use --all)";
|
|
840
|
+
if (carnet.workspace) io.stdout(`${colors.dim(`no active session yet${hint}`)}\n`);
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
const now = options.now ?? /* @__PURE__ */ new Date();
|
|
844
|
+
const rows = sessions.map((session) => ({
|
|
845
|
+
session,
|
|
846
|
+
cells: row(carnet, session, now)
|
|
847
|
+
}));
|
|
848
|
+
const widths = rows.reduce((current, { cells }) => cells.map((cell, column) => Math.max(current[column] ?? 0, cell.length)), []);
|
|
849
|
+
const lines = rows.map(({ session, cells }) => {
|
|
850
|
+
return cells.map((cell, column) => {
|
|
851
|
+
const padded = column < cells.length - 1 ? cell.padEnd(widths[column] ?? 0) : cell;
|
|
852
|
+
if (column === 0) return colors.bold(padded);
|
|
853
|
+
if (column === 1 && session.kind === "temp") return colors.dim(padded);
|
|
854
|
+
if (column === 2 && session.status === "archived") return colors.dim(padded);
|
|
855
|
+
return padded;
|
|
856
|
+
}).join(" ").trimEnd();
|
|
857
|
+
});
|
|
858
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
859
|
+
}
|
|
860
|
+
function sessionsMerge(carnet, fromReference, intoReference, io) {
|
|
861
|
+
const from = findSession(carnet, fromReference);
|
|
862
|
+
const into = findSession(carnet, intoReference);
|
|
863
|
+
const result = carnet.sessions.merge(from.id, into.id);
|
|
864
|
+
const label = (session) => session.name ?? session.id.slice(0, 8);
|
|
865
|
+
const lines = [
|
|
866
|
+
`merged ${label(from)} into ${label(result.session)}`,
|
|
867
|
+
` agents moved: ${result.movedAgents.join(", ") || "none"}`,
|
|
868
|
+
...result.closedAgents.length ? [` agents closed (name already used): ${result.closedAgents.join(", ")}`] : [],
|
|
869
|
+
` events moved: ${result.movedEvents}`,
|
|
870
|
+
` table line moved: ${result.movedTable ? "yes" : "no"}`
|
|
871
|
+
];
|
|
872
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
873
|
+
}
|
|
874
|
+
function label(session) {
|
|
875
|
+
return session.name ?? shortId(session.id);
|
|
876
|
+
}
|
|
877
|
+
function workspaced(name, io) {
|
|
878
|
+
if (!workspaceExists(name, io.env)) throw new CliError(`No workspace named "${name}"`, "see rucher ws list");
|
|
879
|
+
return loadWorkspace(name, io.env);
|
|
880
|
+
}
|
|
881
|
+
function sessionsMove(carnet, reference, workspaceReference, io, options = {}) {
|
|
882
|
+
const session = findSession(carnet, reference);
|
|
883
|
+
const target = workspaced(workspaceReference, io).name;
|
|
884
|
+
const result = moveSession(carnet, session.id, target, options);
|
|
885
|
+
const into = result.into ? ` into ${label(result.into)}` : "";
|
|
886
|
+
const lines = [
|
|
887
|
+
`${options.dryRun ? "would move" : "moved"} ${label(session)} from ${workspaceLabel(carnet.workspace)} to ${target}${into}`,
|
|
888
|
+
` agents moved: ${result.movedAgents.join(", ") || "none"}`,
|
|
889
|
+
...result.skippedAgents.length ? [` agents left out (name already used): ${result.skippedAgents.join(", ")}`] : [],
|
|
890
|
+
` events moved: ${result.movedEvents}`,
|
|
891
|
+
` table line moved: ${result.movedTable ? "yes" : "no"}`,
|
|
892
|
+
...result.keptNotes ? [` notes kept in ${workspaceLabel(carnet.workspace)}: ${result.keptNotes}`] : []
|
|
893
|
+
];
|
|
894
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
895
|
+
}
|
|
896
|
+
function sessionsRename(carnet, reference, name, io) {
|
|
897
|
+
const session = carnet.sessions.rename(findSession(carnet, reference).id, name);
|
|
898
|
+
io.stdout(`renamed to ${session.name}; rucher resume ${session.name} gives Claude the same title\n`);
|
|
899
|
+
}
|
|
900
|
+
function sessionsPromote(carnet, reference, name, io) {
|
|
901
|
+
const session = carnet.sessions.promote(findSession(carnet, reference).id, name);
|
|
902
|
+
io.stdout(`${label(session)} is now active\n`);
|
|
903
|
+
}
|
|
904
|
+
function sessionsDemote(carnet, reference, io) {
|
|
905
|
+
const session = carnet.sessions.demote(findSession(carnet, reference).id);
|
|
906
|
+
io.stdout(`${label(session)} is now temp, cleaned by rucher sessions clean once idle\n`);
|
|
907
|
+
}
|
|
908
|
+
function purgeTranscripts(session, io) {
|
|
909
|
+
const files = hostById(session.host).transcriptFiles(session, io.env);
|
|
910
|
+
for (const file of files) {
|
|
911
|
+
rmSync(file, {
|
|
912
|
+
recursive: true,
|
|
913
|
+
force: true
|
|
914
|
+
});
|
|
915
|
+
io.stdout(` deleted ${file}\n`);
|
|
916
|
+
}
|
|
917
|
+
if (files.length === 0) io.stdout(" no transcript found\n");
|
|
1127
918
|
}
|
|
1128
|
-
function
|
|
1129
|
-
const
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
input: lines.join("\n"),
|
|
1135
|
-
stdio: [
|
|
1136
|
-
"pipe",
|
|
1137
|
-
"pipe",
|
|
1138
|
-
"inherit"
|
|
1139
|
-
],
|
|
1140
|
-
encoding: "utf8"
|
|
1141
|
-
});
|
|
1142
|
-
return sessions[Number(result.stdout?.trim().split(/\s+/)[0]) - 1];
|
|
919
|
+
function sessionsDrop(carnet, reference, io, options = {}) {
|
|
920
|
+
const session = findSession(carnet, reference);
|
|
921
|
+
if (options.purge && session.status === "running") throw new CliError(`Session ${label(session)} is running`, "drop it with --purge once it has ended");
|
|
922
|
+
carnet.sessions.drop(session.id);
|
|
923
|
+
io.stdout(`archived ${label(session)}\n`);
|
|
924
|
+
if (options.purge) purgeTranscripts(session, io);
|
|
1143
925
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
926
|
+
const UNIT_MILLISECONDS = {
|
|
927
|
+
m: 6e4,
|
|
928
|
+
h: 36e5,
|
|
929
|
+
d: 864e5
|
|
930
|
+
};
|
|
931
|
+
function parseDuration(value) {
|
|
932
|
+
const match = /^(\d+(?:\.\d+)?)([mhd]?)$/.exec(value.trim());
|
|
933
|
+
if (!match) throw new CliError(`Invalid duration "${value}"`, "use a number of days, or 30m, 12h, 7d");
|
|
934
|
+
return Number(match[1]) * (UNIT_MILLISECONDS[match[2] || "d"] ?? 0);
|
|
935
|
+
}
|
|
936
|
+
function processIsAlive(processId) {
|
|
1150
937
|
try {
|
|
1151
|
-
|
|
1152
|
-
return
|
|
1153
|
-
}
|
|
1154
|
-
|
|
938
|
+
process.kill(processId, 0);
|
|
939
|
+
return true;
|
|
940
|
+
} catch (error) {
|
|
941
|
+
return error.code === "EPERM";
|
|
1155
942
|
}
|
|
1156
943
|
}
|
|
1157
|
-
|
|
1158
|
-
const
|
|
1159
|
-
if (
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
944
|
+
function sessionsClean(carnet, io, options = {}) {
|
|
945
|
+
const olderThan = parseDuration(options.olderThan ?? "7d");
|
|
946
|
+
if (!options.dryRun) for (const session of carnet.sessions.resetDead(options.isAlive ?? processIsAlive)) io.stdout(`${label(session)} was marked running but its process is gone: now idle\n`);
|
|
947
|
+
const stale = carnet.sessions.staleTemp(olderThan);
|
|
948
|
+
if (stale.length === 0) {
|
|
949
|
+
io.stdout(`no temp session idle for more than ${options.olderThan ?? "7d"}\n`);
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
for (const session of stale) {
|
|
953
|
+
if (options.dryRun) {
|
|
954
|
+
io.stdout(`would archive ${label(session)}${options.purge ? " and delete its transcript" : ""}\n`);
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
carnet.sessions.drop(session.id);
|
|
958
|
+
io.stdout(`archived ${label(session)}\n`);
|
|
959
|
+
if (options.purge) purgeTranscripts(session, io);
|
|
960
|
+
}
|
|
1173
961
|
}
|
|
1174
962
|
// src/commands/watch-keys.ts
|
|
1175
963
|
const INITIAL_WATCH_STATE = {
|
|
@@ -1310,7 +1098,7 @@ function sessionName(session) {
|
|
|
1310
1098
|
function sessionLabel(carnet, session, columns) {
|
|
1311
1099
|
const name = sessionName(session);
|
|
1312
1100
|
const room = columns - Array.from(name).length - 3;
|
|
1313
|
-
return room > 1 ? `${name} · ${truncate(
|
|
1101
|
+
return room > 1 ? `${name} · ${truncate(workspaceLabel(carnet.workspace), room)}` : name;
|
|
1314
1102
|
}
|
|
1315
1103
|
function sessionHeader(session, io) {
|
|
1316
1104
|
return paint(io).cyan(truncate(sessionName(session), io.columns));
|
|
@@ -1387,89 +1175,285 @@ function renderStatusView(carnet, io, options) {
|
|
|
1387
1175
|
return sessionBlock(session, view, io);
|
|
1388
1176
|
});
|
|
1389
1177
|
const colors = paint(io);
|
|
1390
|
-
const separator = " - ";
|
|
1391
|
-
const
|
|
1392
|
-
return [
|
|
1393
|
-
`${colors.dim(time)}${separator}${colors.cyan(
|
|
1394
|
-
"",
|
|
1395
|
-
blocks.join("\n\n"),
|
|
1178
|
+
const separator = " - ";
|
|
1179
|
+
const workspace = truncate(workspaceLabel(carnet.workspace), io.columns - time.length - 3);
|
|
1180
|
+
return [
|
|
1181
|
+
`${colors.dim(time)}${separator}${colors.cyan(workspace)}`,
|
|
1182
|
+
"",
|
|
1183
|
+
blocks.join("\n\n"),
|
|
1184
|
+
"",
|
|
1185
|
+
colors.dim(LEGEND)
|
|
1186
|
+
].join("\n");
|
|
1187
|
+
}
|
|
1188
|
+
function statusOnce(carnet, io, options) {
|
|
1189
|
+
io.stdout(`${renderStatusView(carnet, io, options)}\n`);
|
|
1190
|
+
}
|
|
1191
|
+
function dropSelected(carnet, options, index, reason) {
|
|
1192
|
+
const session = resolveSession(carnet, options.session);
|
|
1193
|
+
const item = carnet.notes.activity(session.id)[index];
|
|
1194
|
+
if (!item) return "no note selected";
|
|
1195
|
+
try {
|
|
1196
|
+
carnet.notes.drop(item.note, {
|
|
1197
|
+
reason,
|
|
1198
|
+
session: session.id
|
|
1199
|
+
});
|
|
1200
|
+
return null;
|
|
1201
|
+
} catch (error) {
|
|
1202
|
+
return error instanceof Error ? error.message : String(error);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
function noteCount(carnet, options) {
|
|
1206
|
+
try {
|
|
1207
|
+
return carnet.notes.activity(resolveSession(carnet, options.session).id).length;
|
|
1208
|
+
} catch {
|
|
1209
|
+
return 0;
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
const CLEAR = "\x1B[H\x1B[2J";
|
|
1213
|
+
const HIDE_CURSOR = "\x1B[?25l";
|
|
1214
|
+
const SHOW_CURSOR = "\x1B[?25h";
|
|
1215
|
+
function statusWatch(carnet, io, options) {
|
|
1216
|
+
return new Promise((resolve) => {
|
|
1217
|
+
let last = "";
|
|
1218
|
+
const interactive = options.keys !== void 0 && !options.all;
|
|
1219
|
+
let state = INITIAL_WATCH_STATE;
|
|
1220
|
+
const repaint = () => {
|
|
1221
|
+
let text;
|
|
1222
|
+
try {
|
|
1223
|
+
text = renderStatusView(carnet, io, {
|
|
1224
|
+
...options,
|
|
1225
|
+
interaction: interactive ? state : void 0
|
|
1226
|
+
});
|
|
1227
|
+
} catch (error) {
|
|
1228
|
+
text = paint(io).dim(error instanceof Error ? error.message : String(error));
|
|
1229
|
+
}
|
|
1230
|
+
if (text === last) return;
|
|
1231
|
+
last = text;
|
|
1232
|
+
io.stdout(`${CLEAR}${text}\n`);
|
|
1233
|
+
};
|
|
1234
|
+
const timer = setInterval(() => {
|
|
1235
|
+
if (state.reason === null) repaint();
|
|
1236
|
+
}, options.intervalMilliseconds);
|
|
1237
|
+
let stopped = false;
|
|
1238
|
+
const stop = () => {
|
|
1239
|
+
if (stopped) return;
|
|
1240
|
+
stopped = true;
|
|
1241
|
+
clearInterval(timer);
|
|
1242
|
+
options.keys?.close();
|
|
1243
|
+
io.stdout(SHOW_CURSOR);
|
|
1244
|
+
resolve();
|
|
1245
|
+
};
|
|
1246
|
+
options.keys?.onKey((key) => {
|
|
1247
|
+
const result = pressKey(state, key, interactive ? noteCount(carnet, options) : 0);
|
|
1248
|
+
state = result.state;
|
|
1249
|
+
if (result.action?.type === "quit") return stop();
|
|
1250
|
+
if (result.action?.type === "drop") state = {
|
|
1251
|
+
...state,
|
|
1252
|
+
message: dropSelected(carnet, options, result.action.index, result.action.reason)
|
|
1253
|
+
};
|
|
1254
|
+
repaint();
|
|
1255
|
+
});
|
|
1256
|
+
options.signal?.addEventListener("abort", stop, { once: true });
|
|
1257
|
+
io.stdout(HIDE_CURSOR);
|
|
1258
|
+
repaint();
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
// src/commands/ws.ts
|
|
1262
|
+
function carnetErrors(io, run) {
|
|
1263
|
+
try {
|
|
1264
|
+
return run();
|
|
1265
|
+
} catch (error) {
|
|
1266
|
+
if (error instanceof CarnetError) {
|
|
1267
|
+
io.stderr(`error: ${error.message}\n`);
|
|
1268
|
+
return 1;
|
|
1269
|
+
}
|
|
1270
|
+
throw error;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
function rendered(workspace, io) {
|
|
1274
|
+
const result = renderRunDirectory(workspace, { env: io.env });
|
|
1275
|
+
return [...result.written.map((path) => `written ${path}`), ...result.removed.map((path) => `removed ${path}`)];
|
|
1276
|
+
}
|
|
1277
|
+
function withChanges(line, changes) {
|
|
1278
|
+
return `${line}${changes.length ? ` (${changes.join(", ")})` : ""}\n`;
|
|
1279
|
+
}
|
|
1280
|
+
function workspaceShow(io, options = {}) {
|
|
1281
|
+
return carnetErrors(io, () => {
|
|
1282
|
+
const colors = paint(io);
|
|
1283
|
+
const workspace = currentWorkspace(io, options.workspace);
|
|
1284
|
+
const repos = workspace.repos.length ? workspace.repos.map((repo) => ` ${repo.name} ${colors.dim(repo.path)}`).join("\n") : ` ${colors.dim("none")}`;
|
|
1285
|
+
io.stdout(`${colors.bold(workspace.name)}\ndefinition: ${workspace.definition}\nrun directory: ${workspace.runDirectory}\nrepos:\n${repos}\n`);
|
|
1286
|
+
return 0;
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
function workspaceAdd(repo, io, options = {}) {
|
|
1290
|
+
return carnetErrors(io, () => {
|
|
1291
|
+
const found = currentWorkspace(io, options.workspace);
|
|
1292
|
+
const path = resolve(io.cwd, repo);
|
|
1293
|
+
const workspace = addWorkspaceRepo(found.name, path, io.env);
|
|
1294
|
+
io.stdout(withChanges(`added ${basename(path)} to ${workspace.name}`, rendered(workspace, io)));
|
|
1295
|
+
return 0;
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
function workspaceRemove(repo, io, options = {}) {
|
|
1299
|
+
return carnetErrors(io, () => {
|
|
1300
|
+
const found = currentWorkspace(io, options.workspace);
|
|
1301
|
+
const workspace = removeWorkspaceRepo(found.name, resolve(io.cwd, repo), io.env);
|
|
1302
|
+
io.stdout(withChanges(`removed ${basename(repo)} from ${workspace.name}`, rendered(workspace, io)));
|
|
1303
|
+
return 0;
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
function workspaceSync(io, options = {}) {
|
|
1307
|
+
return carnetErrors(io, () => {
|
|
1308
|
+
const workspace = currentWorkspace(io, options.workspace);
|
|
1309
|
+
const changes = rendered(workspace, io);
|
|
1310
|
+
io.stdout(changes.length ? `${[`synced ${workspace.name}`, ...changes.map((change) => ` ${change}`)].join("\n")}\n` : `${workspace.name} already in sync\n`);
|
|
1311
|
+
return 0;
|
|
1312
|
+
});
|
|
1313
|
+
}
|
|
1314
|
+
function workspacePath(io, options = {}) {
|
|
1315
|
+
return carnetErrors(io, () => {
|
|
1316
|
+
const workspace = currentWorkspace(io, options.workspace);
|
|
1317
|
+
io.stdout(`${options.definition ? workspace.definition : workspace.runDirectory}\n`);
|
|
1318
|
+
return 0;
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
function editorCommand(env) {
|
|
1322
|
+
return env.VISUAL || env.EDITOR || "vi";
|
|
1323
|
+
}
|
|
1324
|
+
function openEditorFor(io) {
|
|
1325
|
+
return (file) => {
|
|
1326
|
+
return spawnSync(`${editorCommand(io.env)} "$RUCHER_EDIT_FILE"`, {
|
|
1327
|
+
shell: true,
|
|
1328
|
+
stdio: "inherit",
|
|
1329
|
+
env: {
|
|
1330
|
+
...io.env,
|
|
1331
|
+
RUCHER_EDIT_FILE: file
|
|
1332
|
+
}
|
|
1333
|
+
}).status ?? 1;
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
function invalidWorkspaceFile(name, io) {
|
|
1337
|
+
try {
|
|
1338
|
+
loadWorkspace(name, io.env);
|
|
1339
|
+
return null;
|
|
1340
|
+
} catch (error) {
|
|
1341
|
+
return error instanceof Error ? error.message : String(error);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
async function workspaceEdit(io, options = {}) {
|
|
1345
|
+
let workspace;
|
|
1346
|
+
let file;
|
|
1347
|
+
try {
|
|
1348
|
+
workspace = currentWorkspace(io, options.workspace);
|
|
1349
|
+
file = join(workspace.definition, options.json ? WORKSPACE_FILE : definitionInstructions(workspace));
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
if (error instanceof CarnetError) {
|
|
1352
|
+
io.stderr(`error: ${error.message}\n`);
|
|
1353
|
+
return 1;
|
|
1354
|
+
}
|
|
1355
|
+
throw error;
|
|
1356
|
+
}
|
|
1357
|
+
const original = readFileSync(file, "utf8");
|
|
1358
|
+
const openEditor = options.openEditor ?? openEditorFor(io);
|
|
1359
|
+
for (;;) {
|
|
1360
|
+
if (openEditor(file) !== 0) {
|
|
1361
|
+
io.stderr(`error: ${editorCommand(io.env)} exited with an error, nothing synced\n`);
|
|
1362
|
+
return 1;
|
|
1363
|
+
}
|
|
1364
|
+
const problem = options.json ? invalidWorkspaceFile(workspace.name, io) : null;
|
|
1365
|
+
if (!problem) break;
|
|
1366
|
+
io.stderr(`error: ${WORKSPACE_FILE} is invalid: ${problem}\n`);
|
|
1367
|
+
if (!options.confirm || !await options.confirm("Edit it again? [Y/n] ")) {
|
|
1368
|
+
writeFileSync(file, original);
|
|
1369
|
+
io.stderr(`restored the previous ${WORKSPACE_FILE}\n`);
|
|
1370
|
+
return 1;
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
return workspaceSync(io, { workspace: workspace.name });
|
|
1374
|
+
}
|
|
1375
|
+
function plural(count, word) {
|
|
1376
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
1377
|
+
}
|
|
1378
|
+
function workspaceList(io) {
|
|
1379
|
+
const colors = paint(io);
|
|
1380
|
+
const workspaces = listWorkspaces(io.env);
|
|
1381
|
+
if (workspaces.length === 0) {
|
|
1382
|
+
io.stdout(`${colors.dim("no workspace yet, create one with `rucher ws create <name>`")}\n`);
|
|
1383
|
+
return 0;
|
|
1384
|
+
}
|
|
1385
|
+
const carnet = openWorkspaceCarnet(null, { env: io.env });
|
|
1386
|
+
const sessions = workspaceSessionCounts(carnet.db);
|
|
1387
|
+
carnet.close();
|
|
1388
|
+
const width = Math.max(...workspaces.map((workspace) => workspace.name.length));
|
|
1389
|
+
const lines = workspaces.map((workspace) => `${colors.bold(workspace.name.padEnd(width))} ${plural(workspace.repos.length, "repo")} ${plural(sessions.get(workspace.name) ?? 0, "session")} ${colors.dim(workspace.runDirectory)}`);
|
|
1390
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
1391
|
+
return 0;
|
|
1392
|
+
}
|
|
1393
|
+
function workspaceCreate(name, io, options = {}) {
|
|
1394
|
+
const colors = paint(io);
|
|
1395
|
+
const workspace = createWorkspace(name, {
|
|
1396
|
+
repos: options.repos?.map((repo) => resolve(io.cwd, repo)),
|
|
1397
|
+
env: io.env
|
|
1398
|
+
});
|
|
1399
|
+
const result = renderRunDirectory(workspace, { env: io.env });
|
|
1400
|
+
const lines = [
|
|
1401
|
+
`${colors.bold(workspace.name)} created`,
|
|
1402
|
+
` definition: ${workspace.definition}`,
|
|
1403
|
+
` run directory: ${workspace.runDirectory}`,
|
|
1404
|
+
` repos: ${workspace.repos.length ? workspace.repos.map((repo) => repo.path).join(", ") : "none yet, add them with `rucher ws add <path> --ws " + workspace.name + "`"}`,
|
|
1405
|
+
...result.written.map((path) => ` written ${path}`),
|
|
1396
1406
|
"",
|
|
1397
|
-
|
|
1398
|
-
|
|
1407
|
+
"Accept the Claude Code trust prompt in its first session: until then the run directory settings are ignored, and rucher passes the repos with --add-dir.",
|
|
1408
|
+
`Next: \`rucher ws edit --ws ${workspace.name}\` for its instructions, then \`rucher new <session> --ws ${workspace.name}\`.`
|
|
1409
|
+
];
|
|
1410
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
1411
|
+
return 0;
|
|
1399
1412
|
}
|
|
1400
|
-
function
|
|
1401
|
-
|
|
1413
|
+
function liveSessions(carnet, isAlive) {
|
|
1414
|
+
return carnet.sessions.list({ includeTemp: true }).filter((session) => session.status === "running" && session.hostPid !== null && isAlive(session.hostPid));
|
|
1402
1415
|
}
|
|
1403
|
-
function
|
|
1404
|
-
const
|
|
1405
|
-
|
|
1406
|
-
|
|
1416
|
+
function refuseLive(carnet, action, isAlive) {
|
|
1417
|
+
const live = liveSessions(carnet, isAlive);
|
|
1418
|
+
if (live.length) throw new CliError(`${plural(live.length, "session")} of ${carnet.workspace?.name} still running: ${live.map((session) => session.name ?? session.id.slice(0, 8)).join(", ")}`, `end them before you ${action} the workspace`);
|
|
1419
|
+
}
|
|
1420
|
+
function workspaceDrop(name, io, options = {}) {
|
|
1421
|
+
const carnet = openWorkspaceCarnet(loadWorkspace(name, io.env), { env: io.env });
|
|
1407
1422
|
try {
|
|
1408
|
-
carnet
|
|
1409
|
-
|
|
1410
|
-
|
|
1423
|
+
refuseLive(carnet, "drop", options.isAlive ?? processIsAlive);
|
|
1424
|
+
const at = (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
|
|
1425
|
+
const archived = archiveWorkspaceSessions(carnet.db, name, at);
|
|
1426
|
+
const deletedNotes = options.purge ? deleteWorkspaceNotes(carnet.db, name) : 0;
|
|
1427
|
+
const workspace = dropWorkspace(name, {
|
|
1428
|
+
purge: options.purge,
|
|
1429
|
+
env: io.env
|
|
1411
1430
|
});
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1431
|
+
const lines = [
|
|
1432
|
+
`dropped ${name}: definition ${workspace.definition} removed`,
|
|
1433
|
+
` sessions archived: ${archived}`,
|
|
1434
|
+
options.purge ? ` notes deleted: ${deletedNotes}; run directory ${workspace.runDirectory} removed` : ` notes and run directory kept; recreate the workspace under the same name to see them again`
|
|
1435
|
+
];
|
|
1436
|
+
io.stdout(`${lines.join("\n")}\n`);
|
|
1437
|
+
return 0;
|
|
1438
|
+
} finally {
|
|
1439
|
+
carnet.close();
|
|
1415
1440
|
}
|
|
1416
1441
|
}
|
|
1417
|
-
function
|
|
1442
|
+
function workspaceErrors(io, run) {
|
|
1418
1443
|
try {
|
|
1419
|
-
return
|
|
1420
|
-
} catch {
|
|
1421
|
-
|
|
1444
|
+
return run();
|
|
1445
|
+
} catch (error) {
|
|
1446
|
+
if (error instanceof CliError) {
|
|
1447
|
+
io.stderr(`error: ${error.message}${error.hint ? `\n ${error.hint}` : ""}\n`);
|
|
1448
|
+
return 1;
|
|
1449
|
+
}
|
|
1450
|
+
if (error instanceof CarnetError) {
|
|
1451
|
+
io.stderr(`error: ${error.message}\n`);
|
|
1452
|
+
return 1;
|
|
1453
|
+
}
|
|
1454
|
+
throw error;
|
|
1422
1455
|
}
|
|
1423
1456
|
}
|
|
1424
|
-
const CLEAR = "\x1B[H\x1B[2J";
|
|
1425
|
-
const HIDE_CURSOR = "\x1B[?25l";
|
|
1426
|
-
const SHOW_CURSOR = "\x1B[?25h";
|
|
1427
|
-
function statusWatch(carnet, io, options) {
|
|
1428
|
-
return new Promise((resolve) => {
|
|
1429
|
-
let last = "";
|
|
1430
|
-
const interactive = options.keys !== void 0 && !options.all;
|
|
1431
|
-
let state = INITIAL_WATCH_STATE;
|
|
1432
|
-
const repaint = () => {
|
|
1433
|
-
let text;
|
|
1434
|
-
try {
|
|
1435
|
-
text = renderStatusView(carnet, io, {
|
|
1436
|
-
...options,
|
|
1437
|
-
interaction: interactive ? state : void 0
|
|
1438
|
-
});
|
|
1439
|
-
} catch (error) {
|
|
1440
|
-
text = paint(io).dim(error instanceof Error ? error.message : String(error));
|
|
1441
|
-
}
|
|
1442
|
-
if (text === last) return;
|
|
1443
|
-
last = text;
|
|
1444
|
-
io.stdout(`${CLEAR}${text}\n`);
|
|
1445
|
-
};
|
|
1446
|
-
const timer = setInterval(() => {
|
|
1447
|
-
if (state.reason === null) repaint();
|
|
1448
|
-
}, options.intervalMilliseconds);
|
|
1449
|
-
let stopped = false;
|
|
1450
|
-
const stop = () => {
|
|
1451
|
-
if (stopped) return;
|
|
1452
|
-
stopped = true;
|
|
1453
|
-
clearInterval(timer);
|
|
1454
|
-
options.keys?.close();
|
|
1455
|
-
io.stdout(SHOW_CURSOR);
|
|
1456
|
-
resolve();
|
|
1457
|
-
};
|
|
1458
|
-
options.keys?.onKey((key) => {
|
|
1459
|
-
const result = pressKey(state, key, interactive ? noteCount(carnet, options) : 0);
|
|
1460
|
-
state = result.state;
|
|
1461
|
-
if (result.action?.type === "quit") return stop();
|
|
1462
|
-
if (result.action?.type === "drop") state = {
|
|
1463
|
-
...state,
|
|
1464
|
-
message: dropSelected(carnet, options, result.action.index, result.action.reason)
|
|
1465
|
-
};
|
|
1466
|
-
repaint();
|
|
1467
|
-
});
|
|
1468
|
-
options.signal?.addEventListener("abort", stop, { once: true });
|
|
1469
|
-
io.stdout(HIDE_CURSOR);
|
|
1470
|
-
repaint();
|
|
1471
|
-
});
|
|
1472
|
-
}
|
|
1473
1457
|
// src/io.ts
|
|
1474
1458
|
function processIo(options = {}) {
|
|
1475
1459
|
const tty = process.stdout.isTTY === true;
|
|
@@ -1558,13 +1542,13 @@ const statusCommand = defineCommand({
|
|
|
1558
1542
|
default: "1",
|
|
1559
1543
|
description: "Refresh interval in seconds with --watch"
|
|
1560
1544
|
},
|
|
1561
|
-
...
|
|
1545
|
+
...workspaceArgument,
|
|
1562
1546
|
...colorArgument
|
|
1563
1547
|
},
|
|
1564
1548
|
async run({ args }) {
|
|
1565
1549
|
const io = processIo({ color: args.color });
|
|
1566
1550
|
try {
|
|
1567
|
-
const carnet = openCliCarnet(io, args.
|
|
1551
|
+
const carnet = openCliCarnet(io, args.ws);
|
|
1568
1552
|
const options = {
|
|
1569
1553
|
session: args.session,
|
|
1570
1554
|
all: args.all
|
|
@@ -1611,13 +1595,13 @@ const logCommand = defineCommand({
|
|
|
1611
1595
|
default: false,
|
|
1612
1596
|
description: "Agent events only, without session starts and ends"
|
|
1613
1597
|
},
|
|
1614
|
-
...
|
|
1598
|
+
...workspaceArgument,
|
|
1615
1599
|
...colorArgument
|
|
1616
1600
|
},
|
|
1617
1601
|
run({ args }) {
|
|
1618
1602
|
const io = processIo({ color: args.color });
|
|
1619
1603
|
try {
|
|
1620
|
-
logOnce(openCliCarnet(io, args.
|
|
1604
|
+
logOnce(openCliCarnet(io, args.ws), io, {
|
|
1621
1605
|
session: args.session,
|
|
1622
1606
|
count: Number(args.count) || 20,
|
|
1623
1607
|
md: args.md,
|
|
@@ -1631,7 +1615,7 @@ const logCommand = defineCommand({
|
|
|
1631
1615
|
const sessionsListCommand = defineCommand({
|
|
1632
1616
|
meta: {
|
|
1633
1617
|
name: "list",
|
|
1634
|
-
description: "List the named sessions of the
|
|
1618
|
+
description: "List the named sessions of the workspace"
|
|
1635
1619
|
},
|
|
1636
1620
|
args: {
|
|
1637
1621
|
all: {
|
|
@@ -1639,13 +1623,13 @@ const sessionsListCommand = defineCommand({
|
|
|
1639
1623
|
default: false,
|
|
1640
1624
|
description: "Include temp and archived sessions"
|
|
1641
1625
|
},
|
|
1642
|
-
...
|
|
1626
|
+
...workspaceArgument,
|
|
1643
1627
|
...colorArgument
|
|
1644
1628
|
},
|
|
1645
1629
|
run({ args }) {
|
|
1646
1630
|
const io = processIo({ color: args.color });
|
|
1647
1631
|
try {
|
|
1648
|
-
sessionsOnce(openCliCarnet(io, args.
|
|
1632
|
+
sessionsOnce(openCliCarnet(io, args.ws), io, { all: args.all });
|
|
1649
1633
|
} catch (error) {
|
|
1650
1634
|
fail(io, error);
|
|
1651
1635
|
}
|
|
@@ -1667,12 +1651,12 @@ const sessionsMergeCommand = defineCommand({
|
|
|
1667
1651
|
description: "Session that keeps everything (name, id or id prefix)",
|
|
1668
1652
|
required: true
|
|
1669
1653
|
},
|
|
1670
|
-
...
|
|
1654
|
+
...workspaceArgument
|
|
1671
1655
|
},
|
|
1672
1656
|
run({ args }) {
|
|
1673
1657
|
const io = processIo();
|
|
1674
1658
|
try {
|
|
1675
|
-
const carnet = openCliCarnet(io, args.
|
|
1659
|
+
const carnet = openCliCarnet(io, args.ws);
|
|
1676
1660
|
sessionsMerge(carnet, args.from, args.into, io);
|
|
1677
1661
|
carnet.close();
|
|
1678
1662
|
} catch (error) {
|
|
@@ -1697,11 +1681,11 @@ const sessionsRenameCommand = defineCommand({
|
|
|
1697
1681
|
description: "New name",
|
|
1698
1682
|
required: true
|
|
1699
1683
|
},
|
|
1700
|
-
...
|
|
1684
|
+
...workspaceArgument
|
|
1701
1685
|
},
|
|
1702
1686
|
run({ args }) {
|
|
1703
1687
|
const io = processIo();
|
|
1704
|
-
withCarnet(io, args.
|
|
1688
|
+
withCarnet(io, args.ws, (carnet) => sessionsRename(carnet, args.session, args.name, io));
|
|
1705
1689
|
}
|
|
1706
1690
|
});
|
|
1707
1691
|
const sessionsPromoteCommand = defineCommand({
|
|
@@ -1716,11 +1700,11 @@ const sessionsPromoteCommand = defineCommand({
|
|
|
1716
1700
|
description: "Name, when the session has none",
|
|
1717
1701
|
required: false
|
|
1718
1702
|
},
|
|
1719
|
-
...
|
|
1703
|
+
...workspaceArgument
|
|
1720
1704
|
},
|
|
1721
1705
|
run({ args }) {
|
|
1722
1706
|
const io = processIo();
|
|
1723
|
-
withCarnet(io, args.
|
|
1707
|
+
withCarnet(io, args.ws, (carnet) => sessionsPromote(carnet, args.session, args.name, io));
|
|
1724
1708
|
}
|
|
1725
1709
|
});
|
|
1726
1710
|
const sessionsDemoteCommand = defineCommand({
|
|
@@ -1730,11 +1714,11 @@ const sessionsDemoteCommand = defineCommand({
|
|
|
1730
1714
|
},
|
|
1731
1715
|
args: {
|
|
1732
1716
|
session: sessionReference,
|
|
1733
|
-
...
|
|
1717
|
+
...workspaceArgument
|
|
1734
1718
|
},
|
|
1735
1719
|
run({ args }) {
|
|
1736
1720
|
const io = processIo();
|
|
1737
|
-
withCarnet(io, args.
|
|
1721
|
+
withCarnet(io, args.ws, (carnet) => sessionsDemote(carnet, args.session, io));
|
|
1738
1722
|
}
|
|
1739
1723
|
});
|
|
1740
1724
|
const purgeArgument = { purge: {
|
|
@@ -1750,40 +1734,40 @@ const sessionsDropCommand = defineCommand({
|
|
|
1750
1734
|
args: {
|
|
1751
1735
|
session: sessionReference,
|
|
1752
1736
|
...purgeArgument,
|
|
1753
|
-
...
|
|
1737
|
+
...workspaceArgument
|
|
1754
1738
|
},
|
|
1755
1739
|
run({ args }) {
|
|
1756
1740
|
const io = processIo();
|
|
1757
|
-
withCarnet(io, args.
|
|
1741
|
+
withCarnet(io, args.ws, (carnet) => sessionsDrop(carnet, args.session, io, { purge: args.purge }));
|
|
1758
1742
|
}
|
|
1759
1743
|
});
|
|
1760
1744
|
const sessionsMoveCommand = defineCommand({
|
|
1761
1745
|
meta: {
|
|
1762
1746
|
name: "move",
|
|
1763
|
-
description: "Move a session, its agents, table line and journal to another
|
|
1747
|
+
description: "Move a session, its agents, table line and journal to another workspace"
|
|
1764
1748
|
},
|
|
1765
1749
|
args: {
|
|
1766
1750
|
session: sessionReference,
|
|
1767
1751
|
to: {
|
|
1768
1752
|
type: "string",
|
|
1769
1753
|
required: true,
|
|
1770
|
-
description: "Target
|
|
1754
|
+
description: "Target workspace name"
|
|
1771
1755
|
},
|
|
1772
1756
|
merge: {
|
|
1773
1757
|
type: "boolean",
|
|
1774
1758
|
default: false,
|
|
1775
|
-
description: "Merge into the session the target
|
|
1759
|
+
description: "Merge into the session the target workspace already has under this id or name"
|
|
1776
1760
|
},
|
|
1777
1761
|
"dry-run": {
|
|
1778
1762
|
type: "boolean",
|
|
1779
1763
|
default: false,
|
|
1780
1764
|
description: "Show what would move"
|
|
1781
1765
|
},
|
|
1782
|
-
...
|
|
1766
|
+
...workspaceArgument
|
|
1783
1767
|
},
|
|
1784
1768
|
run({ args }) {
|
|
1785
1769
|
const io = processIo();
|
|
1786
|
-
withCarnet(io, args.
|
|
1770
|
+
withCarnet(io, args.ws, (carnet) => sessionsMove(carnet, args.session, args.to, io, {
|
|
1787
1771
|
merge: args.merge,
|
|
1788
1772
|
dryRun: args["dry-run"]
|
|
1789
1773
|
}));
|
|
@@ -1806,11 +1790,11 @@ const sessionsCleanCommand = defineCommand({
|
|
|
1806
1790
|
default: false,
|
|
1807
1791
|
description: "Show what would be archived"
|
|
1808
1792
|
},
|
|
1809
|
-
...
|
|
1793
|
+
...workspaceArgument
|
|
1810
1794
|
},
|
|
1811
1795
|
run({ args }) {
|
|
1812
1796
|
const io = processIo();
|
|
1813
|
-
withCarnet(io, args.
|
|
1797
|
+
withCarnet(io, args.ws, (carnet) => sessionsClean(carnet, io, {
|
|
1814
1798
|
olderThan: args["older-than"],
|
|
1815
1799
|
purge: args.purge,
|
|
1816
1800
|
dryRun: args["dry-run"]
|
|
@@ -1820,9 +1804,10 @@ const sessionsCleanCommand = defineCommand({
|
|
|
1820
1804
|
const sessionsCommand = defineCommand({
|
|
1821
1805
|
meta: {
|
|
1822
1806
|
name: "sessions",
|
|
1823
|
-
description: "List and manage the sessions of the
|
|
1807
|
+
description: "List and manage the sessions of the workspace"
|
|
1824
1808
|
},
|
|
1825
|
-
args: { ...
|
|
1809
|
+
args: { ...workspaceArgument },
|
|
1810
|
+
setup: forwardWorkspace,
|
|
1826
1811
|
default: "list",
|
|
1827
1812
|
subCommands: {
|
|
1828
1813
|
list: sessionsListCommand,
|
|
@@ -1835,9 +1820,9 @@ const sessionsCommand = defineCommand({
|
|
|
1835
1820
|
clean: sessionsCleanCommand
|
|
1836
1821
|
}
|
|
1837
1822
|
});
|
|
1838
|
-
function withCarnet(io,
|
|
1823
|
+
function withCarnet(io, workspace, run) {
|
|
1839
1824
|
try {
|
|
1840
|
-
const carnet = openCliCarnet(io,
|
|
1825
|
+
const carnet = openCliCarnet(io, workspace);
|
|
1841
1826
|
try {
|
|
1842
1827
|
run(carnet);
|
|
1843
1828
|
} finally {
|
|
@@ -1860,7 +1845,7 @@ const noteFilterArguments = {
|
|
|
1860
1845
|
const notesListCommand = defineCommand({
|
|
1861
1846
|
meta: {
|
|
1862
1847
|
name: "list",
|
|
1863
|
-
description: "Active notes of the
|
|
1848
|
+
description: "Active notes of the workspace, by kind"
|
|
1864
1849
|
},
|
|
1865
1850
|
args: {
|
|
1866
1851
|
all: {
|
|
@@ -1884,12 +1869,12 @@ const notesListCommand = defineCommand({
|
|
|
1884
1869
|
default: false,
|
|
1885
1870
|
description: "Markdown output, a view only"
|
|
1886
1871
|
},
|
|
1887
|
-
...
|
|
1872
|
+
...workspaceArgument,
|
|
1888
1873
|
...colorArgument
|
|
1889
1874
|
},
|
|
1890
1875
|
run({ args }) {
|
|
1891
1876
|
const io = processIo({ color: args.color });
|
|
1892
|
-
withCarnet(io, args.
|
|
1877
|
+
withCarnet(io, args.ws, (carnet) => notesList(carnet, io, {
|
|
1893
1878
|
all: args.all,
|
|
1894
1879
|
kind: args.kind,
|
|
1895
1880
|
repo: args.repo,
|
|
@@ -1910,12 +1895,12 @@ const notesShowCommand = defineCommand({
|
|
|
1910
1895
|
description: "Note number",
|
|
1911
1896
|
required: true
|
|
1912
1897
|
},
|
|
1913
|
-
...
|
|
1898
|
+
...workspaceArgument,
|
|
1914
1899
|
...colorArgument
|
|
1915
1900
|
},
|
|
1916
1901
|
run({ args }) {
|
|
1917
1902
|
const io = processIo({ color: args.color });
|
|
1918
|
-
withCarnet(io, args.
|
|
1903
|
+
withCarnet(io, args.ws, (carnet) => notesShow(carnet, io, parseNoteId(args.id)));
|
|
1919
1904
|
}
|
|
1920
1905
|
});
|
|
1921
1906
|
const notesAddCommand = defineCommand({
|
|
@@ -1938,12 +1923,12 @@ const notesAddCommand = defineCommand({
|
|
|
1938
1923
|
type: "string",
|
|
1939
1924
|
description: "Repo the note applies to (repeat for several)"
|
|
1940
1925
|
},
|
|
1941
|
-
...
|
|
1926
|
+
...workspaceArgument,
|
|
1942
1927
|
...colorArgument
|
|
1943
1928
|
},
|
|
1944
1929
|
run({ args, rawArgs }) {
|
|
1945
1930
|
const io = processIo({ color: args.color });
|
|
1946
|
-
withCarnet(io, args.
|
|
1931
|
+
withCarnet(io, args.ws, (carnet) => notesAdd(carnet, io, {
|
|
1947
1932
|
kind: args.kind,
|
|
1948
1933
|
text: args.text,
|
|
1949
1934
|
repos: repoList(rawArgs)
|
|
@@ -1970,12 +1955,12 @@ const notesDropCommand = defineCommand({
|
|
|
1970
1955
|
type: "string",
|
|
1971
1956
|
description: "Number of the note that replaces it"
|
|
1972
1957
|
},
|
|
1973
|
-
...
|
|
1958
|
+
...workspaceArgument
|
|
1974
1959
|
},
|
|
1975
1960
|
run({ args }) {
|
|
1976
1961
|
const io = processIo();
|
|
1977
1962
|
const replacement = args["replaced-by"];
|
|
1978
|
-
withCarnet(io, args.
|
|
1963
|
+
withCarnet(io, args.ws, (carnet) => notesDrop(carnet, io, parseNoteId(args.id), {
|
|
1979
1964
|
reason: args.reason,
|
|
1980
1965
|
replacedBy: replacement === void 0 ? void 0 : parseNoteId(replacement)
|
|
1981
1966
|
}));
|
|
@@ -1984,12 +1969,13 @@ const notesDropCommand = defineCommand({
|
|
|
1984
1969
|
const notesCommand = defineCommand({
|
|
1985
1970
|
meta: {
|
|
1986
1971
|
name: "notes",
|
|
1987
|
-
description: "The
|
|
1972
|
+
description: "The workspace's memory: learnings, decisions and dead ends"
|
|
1988
1973
|
},
|
|
1989
1974
|
args: {
|
|
1990
1975
|
...noteFilterArguments,
|
|
1991
|
-
...
|
|
1976
|
+
...workspaceArgument
|
|
1992
1977
|
},
|
|
1978
|
+
setup: forwardWorkspace,
|
|
1993
1979
|
default: "list",
|
|
1994
1980
|
subCommands: {
|
|
1995
1981
|
list: notesListCommand,
|
|
@@ -1998,14 +1984,14 @@ const notesCommand = defineCommand({
|
|
|
1998
1984
|
drop: notesDropCommand
|
|
1999
1985
|
}
|
|
2000
1986
|
});
|
|
2001
|
-
async function launch(io,
|
|
1987
|
+
async function launch(io, workspace, plan) {
|
|
2002
1988
|
try {
|
|
2003
|
-
const carnet = openCliCarnet(io,
|
|
1989
|
+
const carnet = openCliCarnet(io, workspace);
|
|
2004
1990
|
const launchPlan = await plan(carnet);
|
|
2005
1991
|
startLaunch(carnet, launchPlan);
|
|
2006
1992
|
carnet.close();
|
|
2007
1993
|
const exitCode = spawnLaunch(launchPlan, io.env);
|
|
2008
|
-
const after =
|
|
1994
|
+
const after = openWorkspaceCarnet(carnet.workspace, { env: io.env });
|
|
2009
1995
|
finishLaunch(after, launchPlan);
|
|
2010
1996
|
after.close();
|
|
2011
1997
|
process.exitCode = exitCode;
|
|
@@ -2014,12 +2000,12 @@ async function launch(io, group, plan) {
|
|
|
2014
2000
|
}
|
|
2015
2001
|
}
|
|
2016
2002
|
const passthroughHelp = "Arguments after -- are passed to Claude Code";
|
|
2017
|
-
function
|
|
2003
|
+
function withoutWorkspaceOption(own) {
|
|
2018
2004
|
const kept = [];
|
|
2019
2005
|
for (let index = 0; index < own.length; index += 1) {
|
|
2020
2006
|
const argument = own[index] ?? "";
|
|
2021
|
-
if (argument === "
|
|
2022
|
-
else if (!argument.startsWith("--
|
|
2007
|
+
if (argument === "--ws") index += 1;
|
|
2008
|
+
else if (!argument.startsWith("--ws=")) kept.push(argument);
|
|
2023
2009
|
}
|
|
2024
2010
|
return kept;
|
|
2025
2011
|
}
|
|
@@ -2034,13 +2020,13 @@ const newCommand = defineCommand({
|
|
|
2034
2020
|
description: "Session name",
|
|
2035
2021
|
required: true
|
|
2036
2022
|
},
|
|
2037
|
-
...
|
|
2023
|
+
...workspaceArgument
|
|
2038
2024
|
},
|
|
2039
2025
|
async run({ args, rawArgs }) {
|
|
2040
2026
|
const { own, passthrough } = splitPassthrough(rawArgs);
|
|
2041
|
-
await launch(processIo(), args.
|
|
2027
|
+
await launch(processIo(), args.ws, (carnet) => planNew(carnet, processIo(), {
|
|
2042
2028
|
kind: "active",
|
|
2043
|
-
name: positionals(
|
|
2029
|
+
name: positionals(withoutWorkspaceOption(own))[0],
|
|
2044
2030
|
passthrough
|
|
2045
2031
|
}));
|
|
2046
2032
|
}
|
|
@@ -2056,13 +2042,13 @@ const tmpCommand = defineCommand({
|
|
|
2056
2042
|
description: "Optional session name",
|
|
2057
2043
|
required: false
|
|
2058
2044
|
},
|
|
2059
|
-
...
|
|
2045
|
+
...workspaceArgument
|
|
2060
2046
|
},
|
|
2061
2047
|
async run({ args, rawArgs }) {
|
|
2062
2048
|
const { own, passthrough } = splitPassthrough(rawArgs);
|
|
2063
|
-
await launch(processIo(), args.
|
|
2049
|
+
await launch(processIo(), args.ws, (carnet) => planNew(carnet, processIo(), {
|
|
2064
2050
|
kind: "temp",
|
|
2065
|
-
name: positionals(
|
|
2051
|
+
name: positionals(withoutWorkspaceOption(own))[0],
|
|
2066
2052
|
passthrough
|
|
2067
2053
|
}));
|
|
2068
2054
|
}
|
|
@@ -2078,13 +2064,13 @@ const resumeCommand = defineCommand({
|
|
|
2078
2064
|
description: "Session name, id or id prefix",
|
|
2079
2065
|
required: false
|
|
2080
2066
|
},
|
|
2081
|
-
...
|
|
2067
|
+
...workspaceArgument
|
|
2082
2068
|
},
|
|
2083
2069
|
async run({ args, rawArgs }) {
|
|
2084
2070
|
const { own, passthrough } = splitPassthrough(rawArgs);
|
|
2085
2071
|
const io = processIo();
|
|
2086
|
-
await launch(io, args.
|
|
2087
|
-
const reference = positionals(
|
|
2072
|
+
await launch(io, args.ws, async (carnet) => {
|
|
2073
|
+
const reference = positionals(withoutWorkspaceOption(own))[0] ?? (await pickSession(carnet)).id;
|
|
2088
2074
|
return planResume(carnet, io, reference, passthrough);
|
|
2089
2075
|
});
|
|
2090
2076
|
}
|
|
@@ -2110,42 +2096,114 @@ const forkCommand = defineCommand({
|
|
|
2110
2096
|
default: false,
|
|
2111
2097
|
description: "Make the new session durable (needs a name)"
|
|
2112
2098
|
},
|
|
2113
|
-
...
|
|
2099
|
+
...workspaceArgument
|
|
2114
2100
|
},
|
|
2115
2101
|
async run({ args, rawArgs }) {
|
|
2116
2102
|
const { own, passthrough } = splitPassthrough(rawArgs);
|
|
2117
|
-
const [reference, name] = positionals(
|
|
2103
|
+
const [reference, name] = positionals(withoutWorkspaceOption(own));
|
|
2118
2104
|
const io = processIo();
|
|
2119
|
-
await launch(io, args.
|
|
2105
|
+
await launch(io, args.ws, (carnet) => planFork(carnet, io, reference ?? "", {
|
|
2120
2106
|
name,
|
|
2121
2107
|
active: own.includes("--active"),
|
|
2122
2108
|
passthrough
|
|
2123
2109
|
}));
|
|
2124
2110
|
}
|
|
2125
2111
|
});
|
|
2126
|
-
|
|
2112
|
+
async function askYes(question) {
|
|
2113
|
+
const { createInterface } = await import("node:readline/promises");
|
|
2114
|
+
const prompt = createInterface({
|
|
2115
|
+
input: process.stdin,
|
|
2116
|
+
output: process.stderr
|
|
2117
|
+
});
|
|
2118
|
+
try {
|
|
2119
|
+
return !/^n/i.test((await prompt.question(question)).trim());
|
|
2120
|
+
} finally {
|
|
2121
|
+
prompt.close();
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
function commaList(value) {
|
|
2125
|
+
return value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
2126
|
+
}
|
|
2127
|
+
const wsCommand = defineCommand({
|
|
2127
2128
|
meta: {
|
|
2128
|
-
name: "
|
|
2129
|
-
description: "The
|
|
2129
|
+
name: "ws",
|
|
2130
|
+
description: "The workspaces: create, list, show, add or remove repos, render, edit instructions"
|
|
2130
2131
|
},
|
|
2132
|
+
args: { ...workspaceArgument },
|
|
2133
|
+
setup: forwardWorkspace,
|
|
2134
|
+
default: "list",
|
|
2131
2135
|
subCommands: {
|
|
2136
|
+
list: defineCommand({
|
|
2137
|
+
meta: {
|
|
2138
|
+
name: "list",
|
|
2139
|
+
description: "Workspaces with their repo and session counts"
|
|
2140
|
+
},
|
|
2141
|
+
args: { ...colorArgument },
|
|
2142
|
+
run({ args }) {
|
|
2143
|
+
process.exitCode = workspaceList(processIo({ color: args.color }));
|
|
2144
|
+
}
|
|
2145
|
+
}),
|
|
2146
|
+
create: defineCommand({
|
|
2147
|
+
meta: {
|
|
2148
|
+
name: "create",
|
|
2149
|
+
description: "Create a workspace and render its run directory"
|
|
2150
|
+
},
|
|
2151
|
+
args: {
|
|
2152
|
+
name: {
|
|
2153
|
+
type: "positional",
|
|
2154
|
+
description: "Workspace name: a-z, 0-9 and -",
|
|
2155
|
+
required: true
|
|
2156
|
+
},
|
|
2157
|
+
repos: {
|
|
2158
|
+
type: "string",
|
|
2159
|
+
description: "Comma-separated repo paths"
|
|
2160
|
+
},
|
|
2161
|
+
...colorArgument
|
|
2162
|
+
},
|
|
2163
|
+
run({ args }) {
|
|
2164
|
+
const io = processIo({ color: args.color });
|
|
2165
|
+
process.exitCode = workspaceErrors(io, () => workspaceCreate(args.name, io, { repos: commaList(args.repos) }));
|
|
2166
|
+
}
|
|
2167
|
+
}),
|
|
2168
|
+
drop: defineCommand({
|
|
2169
|
+
meta: {
|
|
2170
|
+
name: "drop",
|
|
2171
|
+
description: "Archive the sessions of a workspace and remove its definition, keeping its notes"
|
|
2172
|
+
},
|
|
2173
|
+
args: {
|
|
2174
|
+
name: {
|
|
2175
|
+
type: "positional",
|
|
2176
|
+
description: "Workspace name",
|
|
2177
|
+
required: true
|
|
2178
|
+
},
|
|
2179
|
+
purge: {
|
|
2180
|
+
type: "boolean",
|
|
2181
|
+
default: false,
|
|
2182
|
+
description: "Also delete its notes and its run directory"
|
|
2183
|
+
}
|
|
2184
|
+
},
|
|
2185
|
+
run({ args }) {
|
|
2186
|
+
const io = processIo();
|
|
2187
|
+
process.exitCode = workspaceErrors(io, () => workspaceDrop(args.name, io, { purge: args.purge }));
|
|
2188
|
+
}
|
|
2189
|
+
}),
|
|
2132
2190
|
show: defineCommand({
|
|
2133
2191
|
meta: {
|
|
2134
2192
|
name: "show",
|
|
2135
|
-
description: "Name,
|
|
2193
|
+
description: "Name, definition, run directory and repos of the workspace"
|
|
2136
2194
|
},
|
|
2137
2195
|
args: {
|
|
2138
|
-
...
|
|
2196
|
+
...workspaceArgument,
|
|
2139
2197
|
...colorArgument
|
|
2140
2198
|
},
|
|
2141
2199
|
run({ args }) {
|
|
2142
|
-
process.exitCode =
|
|
2200
|
+
process.exitCode = workspaceShow(processIo({ color: args.color }), { workspace: args.ws });
|
|
2143
2201
|
}
|
|
2144
2202
|
}),
|
|
2145
2203
|
add: defineCommand({
|
|
2146
2204
|
meta: {
|
|
2147
2205
|
name: "add",
|
|
2148
|
-
description: "Add a repo path to the
|
|
2206
|
+
description: "Add a repo path to the workspace, then render its run directory"
|
|
2149
2207
|
},
|
|
2150
2208
|
args: {
|
|
2151
2209
|
path: {
|
|
@@ -2153,17 +2211,17 @@ const groupCommand = defineCommand({
|
|
|
2153
2211
|
description: "Repo path",
|
|
2154
2212
|
required: true
|
|
2155
2213
|
},
|
|
2156
|
-
...
|
|
2214
|
+
...workspaceArgument,
|
|
2157
2215
|
...colorArgument
|
|
2158
2216
|
},
|
|
2159
2217
|
run({ args }) {
|
|
2160
|
-
process.exitCode =
|
|
2218
|
+
process.exitCode = workspaceAdd(args.path, processIo({ color: args.color }), { workspace: args.ws });
|
|
2161
2219
|
}
|
|
2162
2220
|
}),
|
|
2163
2221
|
remove: defineCommand({
|
|
2164
2222
|
meta: {
|
|
2165
2223
|
name: "remove",
|
|
2166
|
-
description: "Remove a repo from the
|
|
2224
|
+
description: "Remove a repo from the workspace, then render its run directory"
|
|
2167
2225
|
},
|
|
2168
2226
|
args: {
|
|
2169
2227
|
path: {
|
|
@@ -2171,42 +2229,42 @@ const groupCommand = defineCommand({
|
|
|
2171
2229
|
description: "Repo path or name",
|
|
2172
2230
|
required: true
|
|
2173
2231
|
},
|
|
2174
|
-
...
|
|
2232
|
+
...workspaceArgument,
|
|
2175
2233
|
...colorArgument
|
|
2176
2234
|
},
|
|
2177
2235
|
run({ args }) {
|
|
2178
|
-
process.exitCode =
|
|
2236
|
+
process.exitCode = workspaceRemove(args.path, processIo({ color: args.color }), { workspace: args.ws });
|
|
2179
2237
|
}
|
|
2180
2238
|
}),
|
|
2181
2239
|
sync: defineCommand({
|
|
2182
2240
|
meta: {
|
|
2183
2241
|
name: "sync",
|
|
2184
|
-
description: "Render the
|
|
2242
|
+
description: "Render the workspace's run directory (instruction copies, generated settings)"
|
|
2185
2243
|
},
|
|
2186
2244
|
args: {
|
|
2187
|
-
...
|
|
2245
|
+
...workspaceArgument,
|
|
2188
2246
|
...colorArgument
|
|
2189
2247
|
},
|
|
2190
2248
|
run({ args }) {
|
|
2191
|
-
process.exitCode =
|
|
2249
|
+
process.exitCode = workspaceSync(processIo({ color: args.color }), { workspace: args.ws });
|
|
2192
2250
|
}
|
|
2193
2251
|
}),
|
|
2194
2252
|
edit: defineCommand({
|
|
2195
2253
|
meta: {
|
|
2196
2254
|
name: "edit",
|
|
2197
|
-
description: "Open the instructions (or
|
|
2255
|
+
description: "Open the instructions (or workspace.json with --json) in $EDITOR, then sync"
|
|
2198
2256
|
},
|
|
2199
2257
|
args: {
|
|
2200
2258
|
json: {
|
|
2201
2259
|
type: "boolean",
|
|
2202
2260
|
default: false,
|
|
2203
|
-
description: "Edit
|
|
2261
|
+
description: "Edit workspace.json instead"
|
|
2204
2262
|
},
|
|
2205
|
-
...
|
|
2263
|
+
...workspaceArgument
|
|
2206
2264
|
},
|
|
2207
2265
|
async run({ args }) {
|
|
2208
|
-
process.exitCode = await
|
|
2209
|
-
|
|
2266
|
+
process.exitCode = await workspaceEdit(processIo(), {
|
|
2267
|
+
workspace: args.ws,
|
|
2210
2268
|
json: args.json,
|
|
2211
2269
|
confirm: askYes
|
|
2212
2270
|
});
|
|
@@ -2215,130 +2273,30 @@ const groupCommand = defineCommand({
|
|
|
2215
2273
|
path: defineCommand({
|
|
2216
2274
|
meta: {
|
|
2217
2275
|
name: "path",
|
|
2218
|
-
description: "Print the
|
|
2276
|
+
description: "Print the run directory path (or the definition with --def), for cd \"$(...)\""
|
|
2219
2277
|
},
|
|
2220
2278
|
args: {
|
|
2221
|
-
|
|
2279
|
+
def: {
|
|
2222
2280
|
type: "boolean",
|
|
2223
2281
|
default: false,
|
|
2224
|
-
description: "The
|
|
2282
|
+
description: "The definition folder instead"
|
|
2225
2283
|
},
|
|
2226
|
-
|
|
2284
|
+
run: {
|
|
2227
2285
|
type: "boolean",
|
|
2228
2286
|
default: false,
|
|
2229
|
-
description: "The
|
|
2287
|
+
description: "The run directory (default)"
|
|
2230
2288
|
},
|
|
2231
|
-
...
|
|
2289
|
+
...workspaceArgument
|
|
2232
2290
|
},
|
|
2233
2291
|
run({ args }) {
|
|
2234
|
-
process.exitCode =
|
|
2235
|
-
|
|
2236
|
-
|
|
2292
|
+
process.exitCode = workspacePath(processIo(), {
|
|
2293
|
+
workspace: args.ws,
|
|
2294
|
+
definition: args.def
|
|
2237
2295
|
});
|
|
2238
2296
|
}
|
|
2239
2297
|
})
|
|
2240
2298
|
}
|
|
2241
2299
|
});
|
|
2242
|
-
async function askYes(question) {
|
|
2243
|
-
const { createInterface } = await import("node:readline/promises");
|
|
2244
|
-
const prompt = createInterface({
|
|
2245
|
-
input: process.stdin,
|
|
2246
|
-
output: process.stderr
|
|
2247
|
-
});
|
|
2248
|
-
try {
|
|
2249
|
-
return !/^n/i.test((await prompt.question(question)).trim());
|
|
2250
|
-
} finally {
|
|
2251
|
-
prompt.close();
|
|
2252
|
-
}
|
|
2253
|
-
}
|
|
2254
|
-
function commaList(value) {
|
|
2255
|
-
return value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
2256
|
-
}
|
|
2257
|
-
const groupsCommand = defineCommand({
|
|
2258
|
-
meta: {
|
|
2259
|
-
name: "groups",
|
|
2260
|
-
description: "The groups of this machine"
|
|
2261
|
-
},
|
|
2262
|
-
default: "list",
|
|
2263
|
-
subCommands: {
|
|
2264
|
-
list: defineCommand({
|
|
2265
|
-
meta: {
|
|
2266
|
-
name: "list",
|
|
2267
|
-
description: "Groups with their repo and session counts"
|
|
2268
|
-
},
|
|
2269
|
-
args: { ...colorArgument },
|
|
2270
|
-
run({ args }) {
|
|
2271
|
-
process.exitCode = groupsList(processIo({ color: args.color }));
|
|
2272
|
-
}
|
|
2273
|
-
}),
|
|
2274
|
-
create: defineCommand({
|
|
2275
|
-
meta: {
|
|
2276
|
-
name: "create",
|
|
2277
|
-
description: "Create a group and render its workspace"
|
|
2278
|
-
},
|
|
2279
|
-
args: {
|
|
2280
|
-
name: {
|
|
2281
|
-
type: "positional",
|
|
2282
|
-
description: "Group name: a-z, 0-9 and -",
|
|
2283
|
-
required: true
|
|
2284
|
-
},
|
|
2285
|
-
repos: {
|
|
2286
|
-
type: "string",
|
|
2287
|
-
description: "Comma-separated repo paths"
|
|
2288
|
-
},
|
|
2289
|
-
...colorArgument
|
|
2290
|
-
},
|
|
2291
|
-
run({ args }) {
|
|
2292
|
-
const io = processIo({ color: args.color });
|
|
2293
|
-
process.exitCode = groupsErrors(io, () => groupsCreate(args.name, io, { repos: commaList(args.repos) }));
|
|
2294
|
-
}
|
|
2295
|
-
}),
|
|
2296
|
-
rename: defineCommand({
|
|
2297
|
-
meta: {
|
|
2298
|
-
name: "rename",
|
|
2299
|
-
description: "Rename a group with no running session; its workspace stays"
|
|
2300
|
-
},
|
|
2301
|
-
args: {
|
|
2302
|
-
from: {
|
|
2303
|
-
type: "positional",
|
|
2304
|
-
description: "Current name",
|
|
2305
|
-
required: true
|
|
2306
|
-
},
|
|
2307
|
-
to: {
|
|
2308
|
-
type: "positional",
|
|
2309
|
-
description: "New name",
|
|
2310
|
-
required: true
|
|
2311
|
-
}
|
|
2312
|
-
},
|
|
2313
|
-
run({ args }) {
|
|
2314
|
-
const io = processIo();
|
|
2315
|
-
process.exitCode = groupsErrors(io, () => groupsRename(args.from, args.to, io));
|
|
2316
|
-
}
|
|
2317
|
-
}),
|
|
2318
|
-
drop: defineCommand({
|
|
2319
|
-
meta: {
|
|
2320
|
-
name: "drop",
|
|
2321
|
-
description: "Archive the sessions of a group and remove its source, keeping its notes"
|
|
2322
|
-
},
|
|
2323
|
-
args: {
|
|
2324
|
-
name: {
|
|
2325
|
-
type: "positional",
|
|
2326
|
-
description: "Group name",
|
|
2327
|
-
required: true
|
|
2328
|
-
},
|
|
2329
|
-
purge: {
|
|
2330
|
-
type: "boolean",
|
|
2331
|
-
default: false,
|
|
2332
|
-
description: "Also delete its notes and its workspace"
|
|
2333
|
-
}
|
|
2334
|
-
},
|
|
2335
|
-
run({ args }) {
|
|
2336
|
-
const io = processIo();
|
|
2337
|
-
process.exitCode = groupsErrors(io, () => groupsDrop(args.name, io, { purge: args.purge }));
|
|
2338
|
-
}
|
|
2339
|
-
})
|
|
2340
|
-
}
|
|
2341
|
-
});
|
|
2342
2300
|
const hookCommand = defineCommand({
|
|
2343
2301
|
meta: {
|
|
2344
2302
|
name: "hook",
|
|
@@ -2410,12 +2368,18 @@ const serveCommand = defineCommand({
|
|
|
2410
2368
|
serve(processIo());
|
|
2411
2369
|
}
|
|
2412
2370
|
});
|
|
2371
|
+
function refuseRemovedOption(rawArgs) {
|
|
2372
|
+
const removed = /^(-g|--group)(=|$)/;
|
|
2373
|
+
const option = splitPassthrough([...rawArgs]).own.find((argument) => removed.test(argument));
|
|
2374
|
+
if (option) fail(processIo(), new CliError(`Unknown option ${option}`, "use --ws <name>"));
|
|
2375
|
+
}
|
|
2413
2376
|
const main = defineCommand({
|
|
2414
2377
|
meta: {
|
|
2415
2378
|
name: "rucher",
|
|
2416
2379
|
version,
|
|
2417
2380
|
description: "Status, journal and sessions of your coding agents (Claude Code today)"
|
|
2418
2381
|
},
|
|
2382
|
+
setup: ({ rawArgs }) => refuseRemovedOption(rawArgs),
|
|
2419
2383
|
subCommands: {
|
|
2420
2384
|
status: statusCommand,
|
|
2421
2385
|
log: logCommand,
|
|
@@ -2425,8 +2389,7 @@ const main = defineCommand({
|
|
|
2425
2389
|
fork: forkCommand,
|
|
2426
2390
|
sessions: sessionsCommand,
|
|
2427
2391
|
notes: notesCommand,
|
|
2428
|
-
|
|
2429
|
-
groups: groupsCommand,
|
|
2392
|
+
ws: wsCommand,
|
|
2430
2393
|
hook: hookCommand,
|
|
2431
2394
|
serve: serveCommand
|
|
2432
2395
|
}
|