@k-electron/k-bench 0.3.0 → 0.4.1
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 +13 -24
- package/dist/js/src/server/app.js +37 -8
- package/dist/js/src/server/attachments.js +38 -0
- package/dist/js/src/server/claude.js +146 -8
- package/dist/js/src/server/config.js +1 -0
- package/dist/js/src/server/directories.js +6 -3
- package/dist/js/src/server/reconciler.js +13 -3
- package/dist/js/src/server/server.js +13 -1
- package/dist/js/src/server/service.js +274 -26
- package/dist/js/src/server/store.js +183 -20
- package/dist/js/src/server/tmux.js +131 -26
- package/dist/js/src/shared/launch-options.js +9 -0
- package/dist/js/src/shared/session-roles.js +11 -0
- package/dist/web/assets/index-BtYgPWZ6.js +74 -0
- package/dist/web/assets/index-CWZyOJf0.css +1 -0
- package/dist/web/index.html +2 -2
- package/package.json +5 -6
- package/dist/web/assets/index-BStYcuMO.css +0 -1
- package/dist/web/assets/index-QWsANvSc.js +0 -74
|
@@ -2,8 +2,11 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { mkdirSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { ROLE_LAUNCH_OPTIONS } from "../shared/launch-options.js";
|
|
6
|
+
import { availableName } from "./naming.js";
|
|
5
7
|
/**
|
|
6
8
|
* Migrations run in order on startup; `PRAGMA user_version` records how many have run.
|
|
9
|
+
* Each is SQL, or a function for a change SQL can't make, and runs in its own transaction.
|
|
7
10
|
* Append only. Per ADR-0002 nothing here records process IDs or running/Stopped status.
|
|
8
11
|
*/
|
|
9
12
|
const MIGRATIONS = [
|
|
@@ -39,8 +42,87 @@ const MIGRATIONS = [
|
|
|
39
42
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
40
43
|
options TEXT NOT NULL
|
|
41
44
|
);
|
|
45
|
+
`,
|
|
46
|
+
// A Session's role: 'prime' or 'worker' for a Workspace's two fixed Claude Sessions, null
|
|
47
|
+
// for an Other Session. A Workspace has at most one of each.
|
|
48
|
+
`
|
|
49
|
+
ALTER TABLE sessions ADD COLUMN role TEXT;
|
|
50
|
+
CREATE UNIQUE INDEX sessions_one_per_role ON sessions (workspace_id, role)
|
|
51
|
+
WHERE role IS NOT NULL;
|
|
52
|
+
`,
|
|
53
|
+
// When each Workspace was created (milliseconds), so the folder picker can open next to the
|
|
54
|
+
// newest. Workspaces from before this get their row order, which sorts them before any new one.
|
|
55
|
+
`
|
|
56
|
+
ALTER TABLE workspaces ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0;
|
|
57
|
+
UPDATE workspaces SET created_at = rowid;
|
|
58
|
+
`,
|
|
59
|
+
// Every existing Workspace gets a Prime and a Worker, Stopped, each on a fresh conversation.
|
|
60
|
+
addPrimeAndWorker,
|
|
61
|
+
// A Session's place in the owner's order of its Workspace's Other Sessions (the Prime and
|
|
62
|
+
// Worker are listed first whatever theirs). Existing Sessions keep their creation order;
|
|
63
|
+
// the Prime and Worker the previous migration added get positions too, which is harmless.
|
|
64
|
+
// The default keeps inserts that don't name a position working.
|
|
65
|
+
`
|
|
66
|
+
ALTER TABLE sessions ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
|
|
67
|
+
UPDATE sessions SET position = (
|
|
68
|
+
SELECT COUNT(*) FROM sessions earlier
|
|
69
|
+
WHERE earlier.workspace_id = sessions.workspace_id
|
|
70
|
+
AND (earlier.created_at < sessions.created_at
|
|
71
|
+
OR (earlier.created_at = sessions.created_at AND earlier.rowid < sessions.rowid))
|
|
72
|
+
);
|
|
73
|
+
`,
|
|
74
|
+
// The Host's one record of the Open Workspace, the one the main view shows on every
|
|
75
|
+
// device; null when none has been opened. The id check keeps it to one row.
|
|
76
|
+
`
|
|
77
|
+
CREATE TABLE open_workspace (
|
|
78
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
79
|
+
workspace_id TEXT REFERENCES workspaces(id) ON DELETE SET NULL
|
|
80
|
+
);
|
|
81
|
+
`,
|
|
82
|
+
// Each Workspace's right Pane: its tabs (a JSON list of Other Session ids, in order) and
|
|
83
|
+
// the one shown, null for the Worker. A Workspace with no row shows the Worker, no tabs.
|
|
84
|
+
`
|
|
85
|
+
CREATE TABLE right_panes (
|
|
86
|
+
workspace_id TEXT PRIMARY KEY REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
87
|
+
shown TEXT,
|
|
88
|
+
tabs TEXT NOT NULL
|
|
89
|
+
);
|
|
42
90
|
`,
|
|
43
91
|
];
|
|
92
|
+
/**
|
|
93
|
+
* Records a Prime and a Worker for every Workspace. Existing Sessions stay as Other Sessions;
|
|
94
|
+
* one already named "Prime" or "Worker" is renamed first, to "Prime (earlier)" for example,
|
|
95
|
+
* so the names stay unique and the upgrade never fails.
|
|
96
|
+
*/
|
|
97
|
+
function addPrimeAndWorker(db) {
|
|
98
|
+
const roles = [
|
|
99
|
+
{ role: "prime", name: "Prime" },
|
|
100
|
+
{ role: "worker", name: "Worker" },
|
|
101
|
+
];
|
|
102
|
+
const workspaces = db.prepare("SELECT id FROM workspaces").all();
|
|
103
|
+
const namesIn = db.prepare("SELECT name, created_at AS createdAt FROM sessions WHERE workspace_id = ?");
|
|
104
|
+
const rename = db.prepare("UPDATE sessions SET name = ? WHERE workspace_id = ? AND name = ?");
|
|
105
|
+
const insert = db.prepare(`INSERT INTO sessions
|
|
106
|
+
(id, workspace_id, name, created_at, kind, role, conversation_id, launch_options)
|
|
107
|
+
VALUES (?, ?, ?, ?, 'claude', ?, ?, ?)`);
|
|
108
|
+
for (const workspace of workspaces) {
|
|
109
|
+
const rows = namesIn.all(workspace.id);
|
|
110
|
+
const taken = new Set(rows.map((row) => row.name));
|
|
111
|
+
// Dated just before the Workspace's first Session, so they are listed first, as in a
|
|
112
|
+
// new Workspace.
|
|
113
|
+
const first = Math.min(Date.now(), ...rows.map((row) => row.createdAt));
|
|
114
|
+
for (const { name } of roles) {
|
|
115
|
+
if (!taken.has(name))
|
|
116
|
+
continue;
|
|
117
|
+
const earlier = availableName(`${name} (earlier)`, taken);
|
|
118
|
+
rename.run(earlier, workspace.id, name);
|
|
119
|
+
taken.add(earlier);
|
|
120
|
+
}
|
|
121
|
+
for (const [index, { role, name }] of roles.entries()) {
|
|
122
|
+
insert.run(randomUUID(), workspace.id, name, first - roles.length + index, role, randomUUID(), JSON.stringify(ROLE_LAUNCH_OPTIONS[role]));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
44
126
|
function sessionRow(raw) {
|
|
45
127
|
return {
|
|
46
128
|
...raw,
|
|
@@ -60,7 +142,11 @@ export class Store {
|
|
|
60
142
|
for (let version = row.user_version; version < MIGRATIONS.length; version++) {
|
|
61
143
|
this.db.exec("BEGIN");
|
|
62
144
|
try {
|
|
63
|
-
|
|
145
|
+
const migration = MIGRATIONS[version];
|
|
146
|
+
if (typeof migration === "function")
|
|
147
|
+
migration(this.db);
|
|
148
|
+
else
|
|
149
|
+
this.db.exec(migration);
|
|
64
150
|
this.db.exec(`PRAGMA user_version = ${version + 1}`);
|
|
65
151
|
this.db.exec("COMMIT");
|
|
66
152
|
}
|
|
@@ -73,14 +159,27 @@ export class Store {
|
|
|
73
159
|
close() {
|
|
74
160
|
this.db.close();
|
|
75
161
|
}
|
|
162
|
+
/** Runs `change` in one transaction: every write in it is kept, or none is. */
|
|
163
|
+
transaction(change) {
|
|
164
|
+
this.db.exec("BEGIN");
|
|
165
|
+
try {
|
|
166
|
+
const result = change();
|
|
167
|
+
this.db.exec("COMMIT");
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
this.db.exec("ROLLBACK");
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
76
175
|
listWorkspaces() {
|
|
77
176
|
return this.db
|
|
78
|
-
.prepare("SELECT id, name, folder, position FROM workspaces ORDER BY position")
|
|
177
|
+
.prepare("SELECT id, name, folder, position, created_at AS createdAt FROM workspaces ORDER BY position")
|
|
79
178
|
.all();
|
|
80
179
|
}
|
|
81
180
|
getWorkspace(id) {
|
|
82
181
|
return this.db
|
|
83
|
-
.prepare("SELECT id, name, folder, position FROM workspaces WHERE id = ?")
|
|
182
|
+
.prepare("SELECT id, name, folder, position, created_at AS createdAt FROM workspaces WHERE id = ?")
|
|
84
183
|
.get(id);
|
|
85
184
|
}
|
|
86
185
|
addWorkspace(input) {
|
|
@@ -88,10 +187,23 @@ export class Store {
|
|
|
88
187
|
const next = this.db
|
|
89
188
|
.prepare("SELECT COALESCE(MAX(position), -1) + 1 AS position FROM workspaces")
|
|
90
189
|
.get();
|
|
190
|
+
const createdAt = Date.now();
|
|
91
191
|
this.db
|
|
92
|
-
.prepare("INSERT INTO workspaces (id, name, folder, position) VALUES (?, ?, ?, ?)")
|
|
93
|
-
.run(id, input.name, input.folder, next.position);
|
|
94
|
-
return {
|
|
192
|
+
.prepare("INSERT INTO workspaces (id, name, folder, position, created_at) VALUES (?, ?, ?, ?, ?)")
|
|
193
|
+
.run(id, input.name, input.folder, next.position, createdAt);
|
|
194
|
+
return {
|
|
195
|
+
id,
|
|
196
|
+
name: input.name,
|
|
197
|
+
folder: input.folder,
|
|
198
|
+
position: next.position,
|
|
199
|
+
createdAt,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/** The most recently created Workspace, if there is one. */
|
|
203
|
+
newestWorkspace() {
|
|
204
|
+
return this.db
|
|
205
|
+
.prepare("SELECT id, name, folder, position, created_at AS createdAt FROM workspaces ORDER BY created_at DESC, rowid DESC LIMIT 1")
|
|
206
|
+
.get();
|
|
95
207
|
}
|
|
96
208
|
updateWorkspace(id, changes) {
|
|
97
209
|
if (changes.name !== undefined) {
|
|
@@ -112,29 +224,35 @@ export class Store {
|
|
|
112
224
|
/** Gives each Workspace the position of its index in `ids`, in one transaction. */
|
|
113
225
|
setWorkspaceOrder(ids) {
|
|
114
226
|
const update = this.db.prepare("UPDATE workspaces SET position = ? WHERE id = ?");
|
|
115
|
-
this.
|
|
116
|
-
try {
|
|
227
|
+
this.transaction(() => {
|
|
117
228
|
ids.forEach((id, index) => {
|
|
118
229
|
update.run(index, id);
|
|
119
230
|
});
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/** Gives each of a Workspace's Sessions the position of its index in `ids`, in one transaction. */
|
|
234
|
+
setSessionOrder(workspaceId, ids) {
|
|
235
|
+
const update = this.db.prepare("UPDATE sessions SET position = ? WHERE id = ? AND workspace_id = ?");
|
|
236
|
+
this.transaction(() => {
|
|
237
|
+
ids.forEach((id, index) => {
|
|
238
|
+
update.run(index, id, workspaceId);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
126
241
|
}
|
|
242
|
+
/** The Prime, then the Worker, then the Other Sessions in the owner's order. */
|
|
127
243
|
listSessions(workspaceId) {
|
|
128
244
|
const rows = this.db
|
|
129
|
-
.prepare(`SELECT id, workspace_id AS workspaceId, name, kind,
|
|
245
|
+
.prepare(`SELECT id, workspace_id AS workspaceId, name, kind, role,
|
|
130
246
|
conversation_id AS conversationId, launch_options AS launchOptions
|
|
131
|
-
FROM sessions WHERE workspace_id = ?
|
|
247
|
+
FROM sessions WHERE workspace_id = ?
|
|
248
|
+
ORDER BY CASE role WHEN 'prime' THEN 0 WHEN 'worker' THEN 1 ELSE 2 END,
|
|
249
|
+
position, rowid`)
|
|
132
250
|
.all(workspaceId);
|
|
133
251
|
return rows.map(sessionRow);
|
|
134
252
|
}
|
|
135
253
|
getSession(id) {
|
|
136
254
|
const row = this.db
|
|
137
|
-
.prepare(`SELECT s.id, s.workspace_id AS workspaceId, s.name, s.kind,
|
|
255
|
+
.prepare(`SELECT s.id, s.workspace_id AS workspaceId, s.name, s.kind, s.role,
|
|
138
256
|
s.conversation_id AS conversationId,
|
|
139
257
|
s.launch_options AS launchOptions, w.folder
|
|
140
258
|
FROM sessions s JOIN workspaces w ON w.id = s.workspace_id WHERE s.id = ?`)
|
|
@@ -154,6 +272,44 @@ export class Store {
|
|
|
154
272
|
ON CONFLICT (id) DO UPDATE SET options = excluded.options`)
|
|
155
273
|
.run(JSON.stringify(options));
|
|
156
274
|
}
|
|
275
|
+
/** The Workspace last opened, if one has been and it still exists. */
|
|
276
|
+
getOpenWorkspaceId() {
|
|
277
|
+
const row = this.db
|
|
278
|
+
.prepare("SELECT workspace_id AS workspaceId FROM open_workspace WHERE id = 1")
|
|
279
|
+
.get();
|
|
280
|
+
return row?.workspaceId ?? null;
|
|
281
|
+
}
|
|
282
|
+
setOpenWorkspaceId(workspaceId) {
|
|
283
|
+
this.db
|
|
284
|
+
.prepare(`INSERT INTO open_workspace (id, workspace_id) VALUES (1, ?)
|
|
285
|
+
ON CONFLICT (id) DO UPDATE SET workspace_id = excluded.workspace_id`)
|
|
286
|
+
.run(workspaceId);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Records the Claude conversation a Claude Session is now on, in place: no history is kept.
|
|
290
|
+
* A shell Session, or one not stored yet, is left alone.
|
|
291
|
+
*/
|
|
292
|
+
setConversationId(id, conversationId) {
|
|
293
|
+
this.db
|
|
294
|
+
.prepare(`UPDATE sessions SET conversation_id = ?
|
|
295
|
+
WHERE id = ? AND kind = 'claude' AND conversation_id IS NOT ?`)
|
|
296
|
+
.run(conversationId, id, conversationId);
|
|
297
|
+
}
|
|
298
|
+
/** What the Workspace's right Pane holds: the Worker and no tabs until it is set. */
|
|
299
|
+
getRightPane(workspaceId) {
|
|
300
|
+
const row = this.db
|
|
301
|
+
.prepare("SELECT shown, tabs FROM right_panes WHERE workspace_id = ?")
|
|
302
|
+
.get(workspaceId);
|
|
303
|
+
return row
|
|
304
|
+
? { shown: row.shown, tabs: JSON.parse(row.tabs) }
|
|
305
|
+
: { shown: null, tabs: [] };
|
|
306
|
+
}
|
|
307
|
+
setRightPane(workspaceId, rightPane) {
|
|
308
|
+
this.db
|
|
309
|
+
.prepare(`INSERT INTO right_panes (workspace_id, shown, tabs) VALUES (?, ?, ?)
|
|
310
|
+
ON CONFLICT (workspace_id) DO UPDATE SET shown = excluded.shown, tabs = excluded.tabs`)
|
|
311
|
+
.run(workspaceId, rightPane.shown, JSON.stringify(rightPane.tabs));
|
|
312
|
+
}
|
|
157
313
|
renameSession(id, name) {
|
|
158
314
|
this.db.prepare("UPDATE sessions SET name = ? WHERE id = ?").run(name, id);
|
|
159
315
|
}
|
|
@@ -164,16 +320,23 @@ export class Store {
|
|
|
164
320
|
const conversationId = input.conversationId ?? null;
|
|
165
321
|
const launchOptions = conversationId === null ? null : (input.launchOptions ?? null);
|
|
166
322
|
const kind = conversationId === null ? "shell" : "claude";
|
|
323
|
+
const role = input.role ?? null;
|
|
324
|
+
// A new Session goes last in its Workspace's order.
|
|
325
|
+
const next = this.db
|
|
326
|
+
.prepare("SELECT COALESCE(MAX(position), -1) + 1 AS position FROM sessions WHERE workspace_id = ?")
|
|
327
|
+
.get(input.workspaceId);
|
|
167
328
|
this.db
|
|
168
329
|
.prepare(`INSERT INTO sessions
|
|
169
|
-
(id, workspace_id, name, created_at, kind, conversation_id, launch_options
|
|
170
|
-
|
|
171
|
-
|
|
330
|
+
(id, workspace_id, name, created_at, kind, role, conversation_id, launch_options,
|
|
331
|
+
position)
|
|
332
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
333
|
+
.run(input.id, input.workspaceId, input.name, Date.now(), kind, role, conversationId, launchOptions && JSON.stringify(launchOptions), next.position);
|
|
172
334
|
return {
|
|
173
335
|
id: input.id,
|
|
174
336
|
workspaceId: input.workspaceId,
|
|
175
337
|
name: input.name,
|
|
176
338
|
kind,
|
|
339
|
+
role,
|
|
177
340
|
conversationId,
|
|
178
341
|
launchOptions,
|
|
179
342
|
};
|
|
@@ -2,7 +2,7 @@ import { execFile, spawn } from "node:child_process";
|
|
|
2
2
|
import { userInfo } from "node:os";
|
|
3
3
|
import { promisify } from "node:util";
|
|
4
4
|
import * as pty from "node-pty";
|
|
5
|
-
import { claudeCommand } from "./claude.js";
|
|
5
|
+
import { ACTIVITY_AT_OPTION, ACTIVITY_CHANNEL, ACTIVITY_OPTION, CONVERSATION_CHANNEL, CONVERSATION_OPTION, claudeCommand, } from "./claude.js";
|
|
6
6
|
const execFileAsync = promisify(execFile);
|
|
7
7
|
/** Every Session is a tmux session named `kb-<session id>` on the instance's socket. */
|
|
8
8
|
const PREFIX = "kb-";
|
|
@@ -10,8 +10,44 @@ const PREFIX = "kb-";
|
|
|
10
10
|
const BELL_OPTION = "@kb-bell";
|
|
11
11
|
/** The tmux `wait-for` channel signalled whenever a Bell may have been raised or cleared. */
|
|
12
12
|
const BELL_CHANNEL = "kb-bell-changed";
|
|
13
|
-
/** How long a
|
|
14
|
-
const
|
|
13
|
+
/** How long a channel watch waits before trying again after tmux failed it unexpectedly. */
|
|
14
|
+
const WATCH_RETRY_MS = 1_000;
|
|
15
|
+
const ACTIVITIES = ["working", "waiting", "idle"];
|
|
16
|
+
/**
|
|
17
|
+
* A Session's state from what the activity hooks wrote: Idle while it is empty, before any
|
|
18
|
+
* hook has run, or when it isn't a state at all.
|
|
19
|
+
*/
|
|
20
|
+
function activityOf(written) {
|
|
21
|
+
return ACTIVITIES.find((activity) => activity === written) ?? "idle";
|
|
22
|
+
}
|
|
23
|
+
/** The tmux format for a Session's state, as the activity hooks wrote it. */
|
|
24
|
+
const ACTIVITY_NOW = `#{${ACTIVITY_OPTION}}`;
|
|
25
|
+
/** A tmux format that is true while a Session's state is `activity`. */
|
|
26
|
+
function activityIs(activity) {
|
|
27
|
+
return `#{==:${ACTIVITY_NOW},${activity}}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The tmux target for the Session's one pane. The trailing colon names the Session's current
|
|
31
|
+
* window: without it tmux 3.8 finds no window for a format and prints an empty value, without
|
|
32
|
+
* failing.
|
|
33
|
+
*/
|
|
34
|
+
function paneOf(id) {
|
|
35
|
+
return `=${PREFIX}${id}:`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The `set-option` arguments that set the pane option `option` on `pane` to the tmux format
|
|
39
|
+
* `format`, expanded as it is set.
|
|
40
|
+
*/
|
|
41
|
+
function setPaneOption(pane, option, format) {
|
|
42
|
+
return ["set-option", "-p", "-F", "-t", pane, option, format];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The `set-option` arguments that make the state on `pane` `activity` while the tmux format
|
|
46
|
+
* `test` is true, and leave it alone otherwise.
|
|
47
|
+
*/
|
|
48
|
+
function setActivityIf(pane, test, activity) {
|
|
49
|
+
return setPaneOption(pane, ACTIVITY_OPTION, `#{?${test},${activity},${ACTIVITY_NOW}}`);
|
|
50
|
+
}
|
|
15
51
|
/** The owner's login shell. */
|
|
16
52
|
function ownerShell() {
|
|
17
53
|
return userInfo().shell || "/bin/zsh";
|
|
@@ -92,8 +128,8 @@ function serverConfiguration() {
|
|
|
92
128
|
*/
|
|
93
129
|
export class TmuxDriver {
|
|
94
130
|
socket;
|
|
95
|
-
/**
|
|
96
|
-
|
|
131
|
+
/** Channel watches, which a new tmux server must wake (see `watchBells`). */
|
|
132
|
+
watches = new Set();
|
|
97
133
|
/** The program a Claude Session runs (`InstanceConfig.claudeProgram`). */
|
|
98
134
|
claudeProgram;
|
|
99
135
|
constructor(socket, claudeProgram = "claude") {
|
|
@@ -148,8 +184,8 @@ export class TmuxDriver {
|
|
|
148
184
|
"--",
|
|
149
185
|
...command,
|
|
150
186
|
]);
|
|
151
|
-
// This may have started the tmux server, which a
|
|
152
|
-
for (const watch of this.
|
|
187
|
+
// This may have started the tmux server, which a channel watch idles without.
|
|
188
|
+
for (const watch of this.watches)
|
|
153
189
|
watch.serverStarted();
|
|
154
190
|
}
|
|
155
191
|
/**
|
|
@@ -171,11 +207,30 @@ export class TmuxDriver {
|
|
|
171
207
|
* when there are no Bells to change.
|
|
172
208
|
*/
|
|
173
209
|
watchBells(onChange) {
|
|
174
|
-
|
|
210
|
+
return this.watchChannel(BELL_CHANNEL, onChange);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Calls `onChange` soon after Claude Code in any Claude Session reports starting a
|
|
214
|
+
* conversation (and now and then when none did), until stopped; `listSessions` then has
|
|
215
|
+
* it. Like the Bells, reports are missed only while no tmux server runs.
|
|
216
|
+
*/
|
|
217
|
+
watchConversations(onChange) {
|
|
218
|
+
return this.watchChannel(CONVERSATION_CHANNEL, onChange);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Calls `onChange` soon after Claude Code in any Claude Session reports a new state (and
|
|
222
|
+
* now and then when none did), until stopped; `listSessions` then has it. Like the Bells,
|
|
223
|
+
* reports are missed only while no tmux server runs.
|
|
224
|
+
*/
|
|
225
|
+
watchActivity(onChange) {
|
|
226
|
+
return this.watchChannel(ACTIVITY_CHANNEL, onChange);
|
|
227
|
+
}
|
|
228
|
+
watchChannel(channel, onChange) {
|
|
229
|
+
const watch = new ChannelWatcher([...this.baseArgs(), "wait-for", channel], this.env(), {
|
|
175
230
|
onChange,
|
|
176
|
-
onStop: () => this.
|
|
231
|
+
onStop: () => this.watches.delete(watch),
|
|
177
232
|
});
|
|
178
|
-
this.
|
|
233
|
+
this.watches.add(watch);
|
|
179
234
|
watch.start();
|
|
180
235
|
return watch;
|
|
181
236
|
}
|
|
@@ -188,14 +243,19 @@ export class TmuxDriver {
|
|
|
188
243
|
throw error;
|
|
189
244
|
}
|
|
190
245
|
}
|
|
191
|
-
/**
|
|
246
|
+
/**
|
|
247
|
+
* The Sessions whose shell is currently alive on this socket, with their Bells, states and
|
|
248
|
+
* conversations. The state and conversation are pane options; a session's format reads them,
|
|
249
|
+
* and its last output, from the session's one window and pane.
|
|
250
|
+
*/
|
|
192
251
|
async listSessions() {
|
|
193
252
|
let stdout;
|
|
194
253
|
try {
|
|
195
254
|
stdout = await this.run([
|
|
196
255
|
"list-sessions",
|
|
197
256
|
"-F",
|
|
198
|
-
|
|
257
|
+
// The conversation goes last: it is read as the rest of the line.
|
|
258
|
+
`#{session_name}\t#{${BELL_OPTION}}\t#{${ACTIVITY_OPTION}}\t#{${ACTIVITY_AT_OPTION}}\t#{window_activity}\t#{${CONVERSATION_OPTION}}`,
|
|
199
259
|
]);
|
|
200
260
|
}
|
|
201
261
|
catch (error) {
|
|
@@ -207,25 +267,66 @@ export class TmuxDriver {
|
|
|
207
267
|
.split("\n")
|
|
208
268
|
.filter((line) => line.startsWith(PREFIX))
|
|
209
269
|
.map((line) => {
|
|
210
|
-
const [name = "", bell = ""] = line.split("
|
|
211
|
-
return {
|
|
270
|
+
const [name = "", bell = "", activity = "", at = "", output = "", ...conversation] = line.split("\t");
|
|
271
|
+
return {
|
|
272
|
+
id: name.slice(PREFIX.length),
|
|
273
|
+
bell: bell !== "",
|
|
274
|
+
activity: activityOf(activity),
|
|
275
|
+
lastChange: Math.max(Number(at) || 0, Number(output) || 0),
|
|
276
|
+
conversation: conversation.join("\t"),
|
|
277
|
+
};
|
|
212
278
|
});
|
|
213
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Makes a Waiting Claude Session Working, for a key the owner sent it, which is how they
|
|
282
|
+
* answer or deny what it asked (see Waiting in the glossary). Any other state is left
|
|
283
|
+
* alone. It is one tmux call, sent as the key arrives, so it reads the state the key
|
|
284
|
+
* answered: a Waiting that a hook writes after it, for the next prompt, stays. It signals
|
|
285
|
+
* the activity channel only when the Session was Waiting.
|
|
286
|
+
*/
|
|
287
|
+
async endWaiting(id) {
|
|
288
|
+
const pane = paneOf(id);
|
|
289
|
+
const isWaiting = activityIs("waiting");
|
|
290
|
+
await this.run(["if-shell", "-F", "-t", pane, isWaiting, `wait-for -S ${ACTIVITY_CHANNEL}`],
|
|
291
|
+
// Stamped before the state is written, while it still says Waiting.
|
|
292
|
+
setPaneOption(pane, ACTIVITY_AT_OPTION, `#{?${isWaiting},${nowInSeconds()},#{${ACTIVITY_AT_OPTION}}}`), setActivityIf(pane, isWaiting, "working"));
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Makes a Working Claude Session Idle if it has printed nothing, and had no state written,
|
|
296
|
+
* since `quietSince` (epoch seconds), and returns the state it is in after. Any other state
|
|
297
|
+
* is left alone. It is one tmux call, so a hook or the owner's key that writes the state
|
|
298
|
+
* after it was last read is never overwritten. Returns undefined when the Session has gone.
|
|
299
|
+
*/
|
|
300
|
+
async decayWorking(id, quietSince) {
|
|
301
|
+
const pane = paneOf(id);
|
|
302
|
+
const quiet = [
|
|
303
|
+
activityIs("working"),
|
|
304
|
+
`#{e|<=:#{window_activity},${quietSince}}`,
|
|
305
|
+
`#{e|<=:#{${ACTIVITY_AT_OPTION}},${quietSince}}`,
|
|
306
|
+
].reduce((all, test) => `#{&&:${all},${test}}`);
|
|
307
|
+
try {
|
|
308
|
+
const stdout = await this.run(setActivityIf(pane, quiet, "idle"), [
|
|
309
|
+
"display-message",
|
|
310
|
+
"-p",
|
|
311
|
+
"-t",
|
|
312
|
+
pane,
|
|
313
|
+
ACTIVITY_NOW,
|
|
314
|
+
]);
|
|
315
|
+
return activityOf(stdout.trim());
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
if (isNoSuchSession(error))
|
|
319
|
+
return undefined;
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
214
323
|
/**
|
|
215
324
|
* The size the Session's shell sees: that of its tmux pane's terminal. tmux resizes that
|
|
216
325
|
* terminal some time after the window (at most once every 250ms per tmux pane), so the
|
|
217
326
|
* window's size can be ahead of it. tmux names the terminal; `stty` reads its size.
|
|
218
327
|
*/
|
|
219
328
|
async shellSize(id) {
|
|
220
|
-
const tmuxPaneTty = (await this.run([
|
|
221
|
-
"display-message",
|
|
222
|
-
"-p",
|
|
223
|
-
"-t",
|
|
224
|
-
// The trailing colon names the Session's current window. Without it tmux 3.8
|
|
225
|
-
// finds no window and prints an empty value, without failing.
|
|
226
|
-
`=${PREFIX}${id}:`,
|
|
227
|
-
"#{pane_tty}",
|
|
228
|
-
])).trim();
|
|
329
|
+
const tmuxPaneTty = (await this.run(["display-message", "-p", "-t", paneOf(id), "#{pane_tty}"])).trim();
|
|
229
330
|
// `-f` is the macOS spelling (the Host is a Mac); it prints "<rows> <cols>".
|
|
230
331
|
const { stdout } = await execFileAsync("stty", ["-f", tmuxPaneTty, "size"]);
|
|
231
332
|
const [rows, cols] = stdout.trim().split(" ").map(Number);
|
|
@@ -254,11 +355,11 @@ export class TmuxDriver {
|
|
|
254
355
|
}
|
|
255
356
|
}
|
|
256
357
|
/**
|
|
257
|
-
* Waits on
|
|
358
|
+
* Waits on a channel with a `tmux wait-for` client, again and again. tmux keeps a
|
|
258
359
|
* signal sent while nobody is waiting for the next wait, so none is lost between two waits.
|
|
259
360
|
* With no tmux server to wait on, it idles until told one has started.
|
|
260
361
|
*/
|
|
261
|
-
class
|
|
362
|
+
class ChannelWatcher {
|
|
262
363
|
args;
|
|
263
364
|
env;
|
|
264
365
|
onChange;
|
|
@@ -325,7 +426,7 @@ class BellWatcher {
|
|
|
325
426
|
}
|
|
326
427
|
else {
|
|
327
428
|
// tmux failed the wait some other way, such as its server being killed under it.
|
|
328
|
-
this.retry = setTimeout(() => this.start(),
|
|
429
|
+
this.retry = setTimeout(() => this.start(), WATCH_RETRY_MS);
|
|
329
430
|
this.retry.unref();
|
|
330
431
|
}
|
|
331
432
|
}
|
|
@@ -340,6 +441,10 @@ class BellWatcher {
|
|
|
340
441
|
return this.stopping;
|
|
341
442
|
}
|
|
342
443
|
}
|
|
444
|
+
/** The time now in epoch seconds, as tmux keeps it. */
|
|
445
|
+
export function nowInSeconds() {
|
|
446
|
+
return Math.floor(Date.now() / 1000);
|
|
447
|
+
}
|
|
343
448
|
function stderrOf(error) {
|
|
344
449
|
return String(error.stderr ?? "");
|
|
345
450
|
}
|
|
@@ -12,3 +12,12 @@ export const DEFAULT_LAUNCH_OPTIONS = {
|
|
|
12
12
|
advisor: "default",
|
|
13
13
|
focusView: false,
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* The Launch options k-bench sets for a Workspace's Prime and Worker, by role. They are
|
|
17
|
+
* passed on every launch in place of whatever is stored with the Session, so a change here
|
|
18
|
+
* reaches every Prime and Worker at its next launch.
|
|
19
|
+
*/
|
|
20
|
+
export const ROLE_LAUNCH_OPTIONS = {
|
|
21
|
+
prime: { model: "opus", effort: "high", advisor: "off", focusView: true },
|
|
22
|
+
worker: { model: "sonnet", effort: "high", advisor: "opus", focusView: true },
|
|
23
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Sorts a Workspace's Sessions into its Prime, its Worker and its Other Sessions. */
|
|
2
|
+
export function sessionsByRole(sessions) {
|
|
3
|
+
const prime = sessions.find((s) => s.role === "prime");
|
|
4
|
+
const worker = sessions.find((s) => s.role === "worker");
|
|
5
|
+
return {
|
|
6
|
+
prime,
|
|
7
|
+
worker,
|
|
8
|
+
primeAndWorker: [prime, worker].filter((s) => s !== undefined),
|
|
9
|
+
others: sessions.filter((s) => s.role === null),
|
|
10
|
+
};
|
|
11
|
+
}
|