tryharness 0.7.32 → 0.7.34
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 +48 -0
- package/lib/actorRoster.mjs +106 -0
- package/lib/boardAccess.mjs +328 -0
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +228 -0
- package/lib/boardFiles.mjs +584 -0
- package/lib/boardProtocol.json +470 -0
- package/lib/boardProtocol.mjs +27 -0
- package/lib/boardThreads.mjs +153 -0
- package/lib/boardTools.mjs +202 -0
- package/lib/boardWatches.mjs +92 -0
- package/lib/bridge.mjs +272 -0
- package/lib/cards.mjs +5 -83
- package/lib/coordinator.mjs +13 -3
- package/lib/daemon.mjs +809 -5459
- package/lib/goalCommands.mjs +459 -0
- package/lib/harnessModel.mjs +166 -0
- package/lib/mcp-stdio.mjs +8 -2
- package/lib/models.mjs +107 -11
- package/lib/phones.mjs +8 -121
- package/lib/roomPage.mjs +44 -0
- package/lib/runAuthority.mjs +109 -0
- package/lib/runWorker.mjs +503 -0
- package/lib/sessionControl.mjs +788 -0
- package/lib/sessionIndex.mjs +245 -0
- package/lib/store.mjs +28 -9
- package/lib/swarm.mjs +28 -9
- package/lib/taskCommands.mjs +737 -0
- package/lib/taskRuns.mjs +196 -0
- package/lib/threadRuns.mjs +487 -0
- package/package.json +1 -1
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { paths } from "./paths.mjs";
|
|
4
|
+
import { runningPids } from "./runner.mjs";
|
|
5
|
+
import { PROCESS_NAMES, nativeSessionId } from "./swarm.mjs";
|
|
6
|
+
import { nameOf } from "./members.mjs";
|
|
7
|
+
import {
|
|
8
|
+
LIVE_AGENTS,
|
|
9
|
+
focusTerminal,
|
|
10
|
+
hostApp,
|
|
11
|
+
liveSessions,
|
|
12
|
+
opencodeFeed,
|
|
13
|
+
processTable,
|
|
14
|
+
sessionFeed,
|
|
15
|
+
sessionFile,
|
|
16
|
+
workingDirectories,
|
|
17
|
+
} from "./live.mjs";
|
|
18
|
+
import {
|
|
19
|
+
EDITOR_IDS,
|
|
20
|
+
agentCommand,
|
|
21
|
+
editorFolder,
|
|
22
|
+
editorInstall,
|
|
23
|
+
editorName,
|
|
24
|
+
editorRunsOpenTasks,
|
|
25
|
+
editorSessionWorkspace,
|
|
26
|
+
editorTerminalStarts,
|
|
27
|
+
openEditorFolder,
|
|
28
|
+
panelLink,
|
|
29
|
+
placeEnv,
|
|
30
|
+
placeFrom,
|
|
31
|
+
placeKey,
|
|
32
|
+
processEnv,
|
|
33
|
+
resumeLine,
|
|
34
|
+
t3App,
|
|
35
|
+
t3Link,
|
|
36
|
+
t3OpensLinks,
|
|
37
|
+
t3Provider,
|
|
38
|
+
t3Runs,
|
|
39
|
+
t3Thread,
|
|
40
|
+
t3ThreadIn,
|
|
41
|
+
} from "./places.mjs";
|
|
42
|
+
import { detectAgentApps, detectMcpClients } from "./adapters.mjs";
|
|
43
|
+
import { handedToT3, sendToT3, t3Access } from "./t3.mjs";
|
|
44
|
+
|
|
45
|
+
export function createSessionControl({
|
|
46
|
+
store,
|
|
47
|
+
log,
|
|
48
|
+
validAgent,
|
|
49
|
+
opencodeExecutable,
|
|
50
|
+
activeRuns,
|
|
51
|
+
}) {
|
|
52
|
+
const saidKey = (agent, sessionId) => `said:${agent}:${sessionId}`;
|
|
53
|
+
const said = (agent, sessionId) => {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(store.meta(saidKey(agent, sessionId)) || "[]");
|
|
56
|
+
} catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const CODING_AGENTS = ["claude-code", "codex", "pi", "opencode"];
|
|
61
|
+
const SESSION_ID = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
62
|
+
const folderOf = (request) =>
|
|
63
|
+
typeof request.folder === "string" && path.isAbsolute(request.folder)
|
|
64
|
+
? request.folder
|
|
65
|
+
: null;
|
|
66
|
+
|
|
67
|
+
// Where each of the person's sessions runs (places.mjs), recorded when it starts: its hook, plugin or extension sends
|
|
68
|
+
// its environment (board.live.place), else it is read here from the running process. Thirty days of them, per agent.
|
|
69
|
+
const PLACE_MS = 30 * 24 * 3600_000;
|
|
70
|
+
const placeCache = new Map();
|
|
71
|
+
const placesOf = (agentId) => {
|
|
72
|
+
if (!placeCache.has(agentId)) {
|
|
73
|
+
let found = {};
|
|
74
|
+
try {
|
|
75
|
+
found = JSON.parse(store.meta(`places:${agentId}`) || "{}");
|
|
76
|
+
} catch {}
|
|
77
|
+
placeCache.set(agentId, found);
|
|
78
|
+
}
|
|
79
|
+
return placeCache.get(agentId);
|
|
80
|
+
};
|
|
81
|
+
const placedAgents = () => {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(store.meta("places:agents") || "[]");
|
|
84
|
+
} catch {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const notePlace = (agentId, sessionId, place) => {
|
|
89
|
+
if (!place || !SESSION_ID.test(sessionId)) return;
|
|
90
|
+
const places = placesOf(agentId);
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
const before = places[sessionId];
|
|
93
|
+
const next = { ...place, at: before?.at ?? now, seen: now };
|
|
94
|
+
// The same place, already seen today: nothing worth a write.
|
|
95
|
+
if (
|
|
96
|
+
before &&
|
|
97
|
+
JSON.stringify({ ...before, seen: 0 }) ===
|
|
98
|
+
JSON.stringify({ ...next, seen: 0 }) &&
|
|
99
|
+
now - before.seen < 24 * 3600_000
|
|
100
|
+
)
|
|
101
|
+
return;
|
|
102
|
+
const kept = Object.fromEntries(
|
|
103
|
+
Object.entries({ ...places, [sessionId]: next })
|
|
104
|
+
.filter(([, entry]) => now - (entry.seen || 0) < PLACE_MS)
|
|
105
|
+
.sort((a, b) => b[1].seen - a[1].seen)
|
|
106
|
+
.slice(0, 400)
|
|
107
|
+
);
|
|
108
|
+
placeCache.set(agentId, kept);
|
|
109
|
+
store.meta(`places:${agentId}`, JSON.stringify(kept));
|
|
110
|
+
if (!placedAgents().includes(agentId))
|
|
111
|
+
store.meta(
|
|
112
|
+
"places:agents",
|
|
113
|
+
JSON.stringify([...placedAgents(), agentId].slice(-50))
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
/** A run Harness started itself (in its own folders, or a process it runs) is not where the person works. */
|
|
117
|
+
const harnessRun = (folder, pids = []) =>
|
|
118
|
+
Boolean(folder && folder.startsWith(paths.home + path.sep)) ||
|
|
119
|
+
pids.some((pid) => runningPids().includes(pid));
|
|
120
|
+
// Whose turn it is in each of the person's sessions, as its own hooks say: a turn that ended with nothing more to do is
|
|
121
|
+
// the person's, until they type there again; kept while the agent's process runs. No process scan: the buddy reads
|
|
122
|
+
// the sessions waiting for the person with its board poll. Harness's own runs have no place, so never count.
|
|
123
|
+
const turns = new Map();
|
|
124
|
+
const alive = (pid) => {
|
|
125
|
+
try {
|
|
126
|
+
process.kill(pid, 0);
|
|
127
|
+
return true;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return error.code === "EPERM";
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const noteTurn = (agentId, sessionId, request, yours) => {
|
|
133
|
+
if (!SESSION_ID.test(sessionId)) return;
|
|
134
|
+
let place = placesOf(agentId)[sessionId];
|
|
135
|
+
// A session never placed, or placed before it restarted: placed from this hook's process.
|
|
136
|
+
if (!place || (Number.isInteger(place.pid) && !alive(place.pid))) {
|
|
137
|
+
liveCommand(
|
|
138
|
+
{ ...request, sessionId, command: "board.live.place" },
|
|
139
|
+
agentId
|
|
140
|
+
);
|
|
141
|
+
place = placesOf(agentId)[sessionId];
|
|
142
|
+
}
|
|
143
|
+
if (!Number.isInteger(place?.pid) || !alive(place.pid)) return;
|
|
144
|
+
turns.set(`${agentId}:${sessionId}`, {
|
|
145
|
+
agent: agentId,
|
|
146
|
+
sessionId,
|
|
147
|
+
folder: place.folder ?? null,
|
|
148
|
+
pid: place.pid,
|
|
149
|
+
app: place.app ?? null,
|
|
150
|
+
host: place.name ?? null,
|
|
151
|
+
yours,
|
|
152
|
+
at: Date.now(),
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
/** The person's sessions waiting for them, newest first. */
|
|
156
|
+
const yourTurns = () => {
|
|
157
|
+
for (const [key, turn] of turns) if (!alive(turn.pid)) turns.delete(key);
|
|
158
|
+
return [...turns.values()]
|
|
159
|
+
.filter((turn) => turn.yours)
|
|
160
|
+
.sort((a, b) => b.at - a.at)
|
|
161
|
+
.map(({ agent: agentId, sessionId, folder, app, host, at }) => ({
|
|
162
|
+
agent: agentId,
|
|
163
|
+
sessionId,
|
|
164
|
+
folder,
|
|
165
|
+
app,
|
|
166
|
+
host,
|
|
167
|
+
at,
|
|
168
|
+
}));
|
|
169
|
+
};
|
|
170
|
+
// A session says where it runs: its environment, and the agent's process (the hook's parent), read here for the app up
|
|
171
|
+
// its process tree and its terminal. Codex's terminals share one background server, started from wherever Codex first
|
|
172
|
+
// ran: a session there has no app or terminal of its own, and is placed by the Codex terminal working in its folder.
|
|
173
|
+
const placeOfReport = (agentId, folder, request) => {
|
|
174
|
+
const table = processTable();
|
|
175
|
+
const pids = (Array.isArray(request.pids) ? request.pids : [])
|
|
176
|
+
.filter((pid) => Number.isInteger(pid) && pid > 1 && table.has(pid))
|
|
177
|
+
.slice(0, 2);
|
|
178
|
+
if (harnessRun(folder, pids)) return null;
|
|
179
|
+
let env = placeEnv(request.env);
|
|
180
|
+
let host = pids.map((pid) => hostApp(table, pid)).find(Boolean) ?? null;
|
|
181
|
+
let tty = pids.map((pid) => table.get(pid)?.tty).find(Boolean) ?? null;
|
|
182
|
+
// The agent's own process (not the shell a hook ran in), kept with the place: the session runs while it does.
|
|
183
|
+
let agentPid =
|
|
184
|
+
pids.find(
|
|
185
|
+
(pid) => path.basename(table.get(pid).comm) === PROCESS_NAMES[agentId]
|
|
186
|
+
) ?? null;
|
|
187
|
+
if (agentId === "codex" && pids.length && !host && !tty) {
|
|
188
|
+
const clients = [...table]
|
|
189
|
+
.filter(([, row]) => path.basename(row.comm) === "codex" && row.tty)
|
|
190
|
+
.map(([pid]) => pid);
|
|
191
|
+
const cwds = workingDirectories(clients);
|
|
192
|
+
const client = clients
|
|
193
|
+
.filter((pid) => folder && cwds.get(pid) === folder)
|
|
194
|
+
.sort((a, b) => b - a)[0];
|
|
195
|
+
if (!client) return null;
|
|
196
|
+
env = processEnv(client);
|
|
197
|
+
host = hostApp(table, client);
|
|
198
|
+
tty = table.get(client).tty;
|
|
199
|
+
// Codex's shared server outlives its sessions; the terminal's Codex is this one.
|
|
200
|
+
agentPid = client;
|
|
201
|
+
}
|
|
202
|
+
const place = placeFrom({ env, host, tty, folder });
|
|
203
|
+
return place && agentPid
|
|
204
|
+
? { ...place, pid: agentPid, comm: table.get(agentPid).comm }
|
|
205
|
+
: place;
|
|
206
|
+
};
|
|
207
|
+
// A custom harness may say how one of its sessions opens again: a page it serves on this computer, or a command to type.
|
|
208
|
+
const reopenOf = (value) => {
|
|
209
|
+
if (!value || typeof value !== "object") return null;
|
|
210
|
+
const link =
|
|
211
|
+
typeof value.link === "string" &&
|
|
212
|
+
value.link.length <= 2048 &&
|
|
213
|
+
/^http:\/\/(127\.0\.0\.1|localhost)(:\d{1,5})?\/\S*$/.test(value.link)
|
|
214
|
+
? value.link
|
|
215
|
+
: null;
|
|
216
|
+
const command =
|
|
217
|
+
typeof value.command === "string" &&
|
|
218
|
+
value.command.trim() &&
|
|
219
|
+
value.command.length <= 500 &&
|
|
220
|
+
!/[\0-\x1f]/.test(value.command)
|
|
221
|
+
? value.command.trim()
|
|
222
|
+
: null;
|
|
223
|
+
return link || command ? { link, command } : null;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const liveNow = () => {
|
|
227
|
+
const recent = store.recentSessions(200);
|
|
228
|
+
// With the title the index gives it (its first request), when the index has it yet.
|
|
229
|
+
const titleOf = (session) => {
|
|
230
|
+
const title =
|
|
231
|
+
recent.find(
|
|
232
|
+
(row) =>
|
|
233
|
+
row.agent === session.agent &&
|
|
234
|
+
String(row.session_id).includes(session.sessionId)
|
|
235
|
+
)?.title || null;
|
|
236
|
+
// A session opened with a slash command starts with the agent's own markup, not the person's words.
|
|
237
|
+
return title && !title.startsWith("<") ? title : null;
|
|
238
|
+
};
|
|
239
|
+
const linksOk = t3LinksOk();
|
|
240
|
+
const handed = handedToT3();
|
|
241
|
+
// The sessions that named their own process when they started (pi's extension, OpenCode's plugin, hooks).
|
|
242
|
+
const reported = LIVE_AGENTS.flatMap((agentId) =>
|
|
243
|
+
Object.entries(placesOf(agentId))
|
|
244
|
+
.filter(([, place]) => Number.isInteger(place.pid))
|
|
245
|
+
.map(([sessionId, place]) => ({
|
|
246
|
+
agent: agentId,
|
|
247
|
+
sessionId,
|
|
248
|
+
folder: place.folder || null,
|
|
249
|
+
pid: place.pid,
|
|
250
|
+
comm: place.comm,
|
|
251
|
+
at: place.at,
|
|
252
|
+
}))
|
|
253
|
+
);
|
|
254
|
+
const sessions = liveSessions({
|
|
255
|
+
exclude: new Set(runningPids()),
|
|
256
|
+
recent,
|
|
257
|
+
reported,
|
|
258
|
+
})
|
|
259
|
+
// A T3 thread running a turn Harness handed it is Harness's run, shown in its thread.
|
|
260
|
+
.filter((session) => !handed.has(session.t3Thread))
|
|
261
|
+
// A T3 Code that cannot open links is brought forward instead, and messages reach its sessions through their hooks.
|
|
262
|
+
.map((session) => ({
|
|
263
|
+
...session,
|
|
264
|
+
busy:
|
|
265
|
+
session.busy ??
|
|
266
|
+
(turns.has(`${session.agent}:${session.sessionId}`)
|
|
267
|
+
? !turns.get(`${session.agent}:${session.sessionId}`).yours
|
|
268
|
+
: null),
|
|
269
|
+
t3: linksOk ? session.t3 : null,
|
|
270
|
+
title: titleOf(session),
|
|
271
|
+
waiting: said(session.agent, session.sessionId).length,
|
|
272
|
+
}));
|
|
273
|
+
// A running session no hook placed (it started before Harness was set up, or its agent has none): read from its process.
|
|
274
|
+
for (const session of sessions) {
|
|
275
|
+
if (placesOf(session.agent)[session.sessionId]) continue;
|
|
276
|
+
const env = {
|
|
277
|
+
...processEnv(session.pid),
|
|
278
|
+
...(session.entrypoint
|
|
279
|
+
? { CLAUDE_CODE_ENTRYPOINT: session.entrypoint }
|
|
280
|
+
: {}),
|
|
281
|
+
};
|
|
282
|
+
notePlace(
|
|
283
|
+
session.agent,
|
|
284
|
+
session.sessionId,
|
|
285
|
+
placeFrom({
|
|
286
|
+
env,
|
|
287
|
+
host: session.host,
|
|
288
|
+
tty: session.tty,
|
|
289
|
+
folder: session.folder,
|
|
290
|
+
})
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
return sessions;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// An agent's home: the place most of its sessions started in over the last thirty days (ties: the latest), where its
|
|
297
|
+
// sessions open when they are not running.
|
|
298
|
+
let seeded = { at: 0, homes: {} };
|
|
299
|
+
/** Before any session was recorded: what this computer already shows, threads run in T3 Code and the agent started in an editor's terminal. */
|
|
300
|
+
const seedHome = (agentId) => {
|
|
301
|
+
if (Date.now() - seeded.at > 10 * 60_000)
|
|
302
|
+
seeded = { at: Date.now(), homes: {} };
|
|
303
|
+
if (agentId in seeded.homes) return seeded.homes[agentId];
|
|
304
|
+
const command = agentCommand(agentId);
|
|
305
|
+
const seeds = [
|
|
306
|
+
{
|
|
307
|
+
count: t3App()
|
|
308
|
+
? t3Runs(agentId, new Date(Date.now() - PLACE_MS).toISOString())
|
|
309
|
+
: 0,
|
|
310
|
+
place: { kind: "t3", name: "T3 Code" },
|
|
311
|
+
},
|
|
312
|
+
...(command
|
|
313
|
+
? EDITOR_IDS.map((id) => ({
|
|
314
|
+
count: editorTerminalStarts(id, command),
|
|
315
|
+
place: {
|
|
316
|
+
kind: "editor",
|
|
317
|
+
editor: id,
|
|
318
|
+
name: editorName(id),
|
|
319
|
+
surface: "terminal",
|
|
320
|
+
},
|
|
321
|
+
}))
|
|
322
|
+
: []),
|
|
323
|
+
];
|
|
324
|
+
return (seeded.homes[agentId] =
|
|
325
|
+
seeds
|
|
326
|
+
.filter((seed) => seed.count > 0)
|
|
327
|
+
.sort((a, b) => b.count - a.count)[0]?.place ?? null);
|
|
328
|
+
};
|
|
329
|
+
/** A place with the app that opens it now (T3's running build, an editor's install); null when it is not on this computer. */
|
|
330
|
+
const usable = (place) => {
|
|
331
|
+
if (!place) return null;
|
|
332
|
+
if (place.kind === "t3") {
|
|
333
|
+
const t3 = t3App();
|
|
334
|
+
return t3 ? { ...place, app: t3.app } : null;
|
|
335
|
+
}
|
|
336
|
+
if (place.kind === "editor" && !(place.cli && fs.existsSync(place.cli))) {
|
|
337
|
+
const install = editorInstall(place.editor, place.app || "");
|
|
338
|
+
return install ? { ...place, ...install } : place.app ? place : null;
|
|
339
|
+
}
|
|
340
|
+
return place;
|
|
341
|
+
};
|
|
342
|
+
const homeOf = (agentId) => {
|
|
343
|
+
const tally = new Map();
|
|
344
|
+
for (const place of Object.values(placesOf(agentId))) {
|
|
345
|
+
if (!place?.kind || place.kind === "unknown") continue;
|
|
346
|
+
// An agent T3 does not run, started from a shell inside T3 Code, is not one T3 can show (October 16: OpenCode's
|
|
347
|
+
// home was T3, and Open on it landed on Claude Code's T3 thread).
|
|
348
|
+
if (place.kind === "t3" && !t3Provider(agentId)) continue;
|
|
349
|
+
const entry = tally.get(placeKey(place)) ?? { place, count: 0 };
|
|
350
|
+
entry.count += 1;
|
|
351
|
+
if ((place.seen || 0) >= (entry.place.seen || 0)) entry.place = place;
|
|
352
|
+
tally.set(placeKey(place), entry);
|
|
353
|
+
}
|
|
354
|
+
const best = [...tally.values()].sort(
|
|
355
|
+
(a, b) => b.count - a.count || (b.place.seen || 0) - (a.place.seen || 0)
|
|
356
|
+
)[0]?.place;
|
|
357
|
+
return usable(best ?? seedHome(agentId));
|
|
358
|
+
};
|
|
359
|
+
/** Where each agent lives, for Open's labels: coding agents' homes, and Grok Bot's own app. */
|
|
360
|
+
const homes = () => {
|
|
361
|
+
const found = {};
|
|
362
|
+
for (const id of new Set([...CODING_AGENTS, ...placedAgents()])) {
|
|
363
|
+
const home = homeOf(id);
|
|
364
|
+
if (home)
|
|
365
|
+
found[id] = { name: home.name, app: home.app ?? null, kind: home.kind };
|
|
366
|
+
}
|
|
367
|
+
const grok = detectMcpClients().find(
|
|
368
|
+
(client) => client.client === "grok-bot" && client.path
|
|
369
|
+
);
|
|
370
|
+
if (grok)
|
|
371
|
+
found["grok-bot"] = { name: "Grok Bot", app: grok.path, kind: "app" };
|
|
372
|
+
for (const app of detectAgentApps())
|
|
373
|
+
found[app.agent] ??= { name: app.name, app: app.path, kind: "app" };
|
|
374
|
+
return found;
|
|
375
|
+
};
|
|
376
|
+
const t3LinksOk = () => {
|
|
377
|
+
const t3 = t3App();
|
|
378
|
+
return Boolean(t3 && t3OpensLinks(t3.app));
|
|
379
|
+
};
|
|
380
|
+
const T3_OLD =
|
|
381
|
+
"This T3 Code can't be opened at a thread from another app, so it is in front as it is. T3's nightly builds open the exact thread.";
|
|
382
|
+
/** The person's own sessions, not the runs Harness starts in its own folders (their worktrees are not the person's). */
|
|
383
|
+
const ownSession = (row) =>
|
|
384
|
+
!String(row.cwd || "").startsWith(paths.home + path.sep);
|
|
385
|
+
const short = (name) => String(name || "").replace(/ \(.*\)$/, "");
|
|
386
|
+
|
|
387
|
+
/** Whether one of Harness's runs is still working in this session: it is not resumed beside it. */
|
|
388
|
+
const runBusy = (sessionId) =>
|
|
389
|
+
Boolean(sessionId) &&
|
|
390
|
+
[...activeRuns()].some(
|
|
391
|
+
({ id }) => store.getTask(id)?.session_id === sessionId
|
|
392
|
+
);
|
|
393
|
+
/** How a session opens in one place (or the agent itself, with no session); null when that place cannot show it. */
|
|
394
|
+
const placePlan = (agentId, place, sessionId, folder, act) => {
|
|
395
|
+
if (!place) return null;
|
|
396
|
+
const name = short(place.name);
|
|
397
|
+
const busy = runBusy(sessionId)
|
|
398
|
+
? `${nameOf(agentId)} is still working on this${folder ? ` in ${path.basename(folder)}` : ""}; Open it again once it's done to carry on in the same session.`
|
|
399
|
+
: null;
|
|
400
|
+
if (place.kind === "t3") {
|
|
401
|
+
if (!place.app || !t3Provider(agentId)) return null;
|
|
402
|
+
const thread = sessionId ? t3Thread(sessionId) : null;
|
|
403
|
+
if (thread && t3OpensLinks(place.app))
|
|
404
|
+
return { opensIn: "T3 Code", link: t3Link(thread) };
|
|
405
|
+
if (thread) return { opensIn: "T3 Code", app: place.app, note: T3_OLD };
|
|
406
|
+
// No session of T3's (none yet, or one T3 never had): T3 opens on its thread for the folder, so it shows the right
|
|
407
|
+
// workspace; with none there, it comes forward as it is.
|
|
408
|
+
const there = t3OpensLinks(place.app) ? t3Link(t3ThreadIn(folder)) : null;
|
|
409
|
+
const shown = there ? { link: there } : { app: place.app };
|
|
410
|
+
if (!sessionId) return { opensIn: "T3 Code", ...shown };
|
|
411
|
+
// A session T3 never had (a run Harness started in its own folder, say) says so, and can still be resumed in a
|
|
412
|
+
// terminal instead. Until Harness may send into T3, its threads run in that copy: approving it is what moves them
|
|
413
|
+
// into T3 (inT3).
|
|
414
|
+
return {
|
|
415
|
+
opensIn: "T3 Code",
|
|
416
|
+
...shown,
|
|
417
|
+
resumable: true,
|
|
418
|
+
note: harnessRun(folder)
|
|
419
|
+
? `This run happened in Harness's own copy of the repo, which T3 Code doesn't list, so ${there ? "T3 Code opens on its thread for the repository" : "T3 Code is in front as it is"}.${t3Access()?.state === "send" ? "" : " Approve Harness in T3 Code and this thread's next turns run there, where Open finds them."}`
|
|
420
|
+
: `T3 Code doesn't list this session, so ${there ? "it opens on T3's thread for its folder" : "it is in front as it is"}.`,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
if (place.kind === "editor") {
|
|
424
|
+
// Its window (the folder the editor has open around the session's), first when a panel link is to land there.
|
|
425
|
+
const window = editorFolder(place.editor, folder);
|
|
426
|
+
// Not in its panel or a terminal beside a run still working in it: its folder comes up, and the buddy shows the work.
|
|
427
|
+
if (busy && place.app) {
|
|
428
|
+
if (act && window) openEditorFolder(place, window);
|
|
429
|
+
return { opensIn: name, app: place.app, note: busy };
|
|
430
|
+
}
|
|
431
|
+
const link = sessionId
|
|
432
|
+
? panelLink(agentId, place.editor, sessionId)
|
|
433
|
+
: null;
|
|
434
|
+
if (link) {
|
|
435
|
+
if (act && window) openEditorFolder(place, window);
|
|
436
|
+
return { opensIn: name, link };
|
|
437
|
+
}
|
|
438
|
+
if (!place.app) return null;
|
|
439
|
+
const line = sessionId ? resumeLine(agentId, sessionId, folder) : null;
|
|
440
|
+
// The session itself, resumed in the editor's own terminal in a window on its folder: a workspace whose task runs
|
|
441
|
+
// when the window opens (the editor asks once). The command is copied too, should the person not allow it.
|
|
442
|
+
if (line && editorRunsOpenTasks(place.editor)) {
|
|
443
|
+
if (act)
|
|
444
|
+
openEditorFolder(
|
|
445
|
+
place,
|
|
446
|
+
editorSessionWorkspace(agentId, sessionId, folder, window)
|
|
447
|
+
);
|
|
448
|
+
return {
|
|
449
|
+
opensIn: name,
|
|
450
|
+
app: place.app,
|
|
451
|
+
copy: line,
|
|
452
|
+
note: `${name} opens ${path.basename(window || folder)} with this session in its terminal. The first time, it asks you to trust the folder and to allow the task that resumes it; the command is copied too.`,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
// Automatic tasks turned off there: the session's own command is copied to paste in its terminal.
|
|
456
|
+
if (act && window) openEditorFolder(place, window);
|
|
457
|
+
return {
|
|
458
|
+
opensIn: name,
|
|
459
|
+
app: place.app,
|
|
460
|
+
...(line
|
|
461
|
+
? {
|
|
462
|
+
copy: line,
|
|
463
|
+
note: `${name} is up${window ? ` on ${path.basename(window)}` : ""}. The command that resumes this session is copied: paste it in a terminal there.`,
|
|
464
|
+
}
|
|
465
|
+
: {}),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
if (place.kind === "codex-app")
|
|
469
|
+
return agentId === "codex" && sessionId
|
|
470
|
+
? { opensIn: "Codex", link: `codex://threads/${sessionId}` }
|
|
471
|
+
: place.app
|
|
472
|
+
? { opensIn: "Codex", app: place.app }
|
|
473
|
+
: null;
|
|
474
|
+
if (place.kind === "terminal") {
|
|
475
|
+
if (!CODING_AGENTS.includes(agentId)) return null;
|
|
476
|
+
if (busy)
|
|
477
|
+
return place.app
|
|
478
|
+
? { opensIn: name, app: place.app, note: busy }
|
|
479
|
+
: { opensIn: name, note: busy };
|
|
480
|
+
return {
|
|
481
|
+
opensIn: name,
|
|
482
|
+
resume: {
|
|
483
|
+
agent: agentId,
|
|
484
|
+
session: sessionId ?? null,
|
|
485
|
+
cwd: folder ?? null,
|
|
486
|
+
app: place.app ?? null,
|
|
487
|
+
},
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
return place.app ? { opensIn: name, app: place.app } : null;
|
|
491
|
+
};
|
|
492
|
+
/** A running session, where it runs: its T3 thread, its panel, its editor's window, its terminal pane and tab. */
|
|
493
|
+
const runningPlan = (live, act) => {
|
|
494
|
+
const place = placesOf(live.agent)[live.sessionId] ?? null;
|
|
495
|
+
if (
|
|
496
|
+
(live.host && /^T3 Code\b/.test(live.host.name)) ||
|
|
497
|
+
place?.kind === "t3"
|
|
498
|
+
) {
|
|
499
|
+
if (live.t3) return { opensIn: "T3 Code", link: live.t3 };
|
|
500
|
+
return {
|
|
501
|
+
opensIn: "T3 Code",
|
|
502
|
+
app: live.host?.app ?? t3App()?.app ?? null,
|
|
503
|
+
tty: null,
|
|
504
|
+
...(t3Thread(live.sessionId) ? { note: T3_OLD } : {}),
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
if (place?.kind === "editor") {
|
|
508
|
+
const link =
|
|
509
|
+
place.surface === "panel"
|
|
510
|
+
? panelLink(live.agent, place.editor, live.sessionId)
|
|
511
|
+
: null;
|
|
512
|
+
if (link) return { opensIn: short(place.name), link };
|
|
513
|
+
// A session in the editor's terminal: its window. Which terminal tab shows, only the editor itself can pick.
|
|
514
|
+
if (act)
|
|
515
|
+
openEditorFolder(
|
|
516
|
+
usable(place),
|
|
517
|
+
editorFolder(place.editor, live.folder)
|
|
518
|
+
);
|
|
519
|
+
return {
|
|
520
|
+
opensIn: short(place.name),
|
|
521
|
+
app: place.app ?? live.host?.app ?? null,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
if (place?.kind === "codex-app" && live.agent === "codex")
|
|
525
|
+
return { opensIn: "Codex", link: `codex://threads/${live.sessionId}` };
|
|
526
|
+
const opensIn = live.host ? short(live.host.name) : null;
|
|
527
|
+
return {
|
|
528
|
+
opensIn,
|
|
529
|
+
...(act
|
|
530
|
+
? focusTerminal(live, place)
|
|
531
|
+
: live.host
|
|
532
|
+
? { app: live.host.app, tty: live.tty }
|
|
533
|
+
: {}),
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
/**
|
|
537
|
+
* How a session opens, or the agent itself with no session: what the app does next (views/ui.ts openPlace). A running
|
|
538
|
+
* session opens where it runs; one that ended opens in the agent's home, else where it ran, else a terminal resumes
|
|
539
|
+
* it. `act`: the daemon's part happens now (a pane selected, an editor's window brought up); without it, this is only
|
|
540
|
+
* where it would open, for Open's label.
|
|
541
|
+
*/
|
|
542
|
+
const openPlan = (agentId, sessionId, folder, act) => {
|
|
543
|
+
// A T3 Code thread itself (a turn Harness handed to T3) opens there, wherever the agent usually works, and even while
|
|
544
|
+
// the turn runs: T3 runs that one turn and takes what the person types there in turn, so nothing runs twice.
|
|
545
|
+
if (sessionId && t3Thread(sessionId) === sessionId) {
|
|
546
|
+
const plan = placePlan(
|
|
547
|
+
agentId,
|
|
548
|
+
usable({ kind: "t3", name: "T3 Code" }),
|
|
549
|
+
sessionId,
|
|
550
|
+
folder,
|
|
551
|
+
act
|
|
552
|
+
);
|
|
553
|
+
if (plan) return plan;
|
|
554
|
+
}
|
|
555
|
+
// A session one of Harness's runs is working in on this computer is not opened a second time: two processes in one
|
|
556
|
+
// session each finished the job their own way (October 11, two different PRDs from one job). An editor the agent
|
|
557
|
+
// lives in comes up on the run's folder and says it is still working (placePlan); anywhere else the run is followed
|
|
558
|
+
// in its thread, which shows its steps as they come, and Stop. Only a run Harness started holds a session: a task
|
|
559
|
+
// the person's own session claimed through its MCP server is that session at work, opened where it runs.
|
|
560
|
+
const holding = sessionId
|
|
561
|
+
? store.runHoldingSession(agentId, sessionId)
|
|
562
|
+
: null;
|
|
563
|
+
if (holding && [...activeRuns()].some(({ id }) => id === holding.id)) {
|
|
564
|
+
const home = homeOf(agentId);
|
|
565
|
+
const shown =
|
|
566
|
+
home?.kind === "editor"
|
|
567
|
+
? placePlan(agentId, home, sessionId, folder, act)
|
|
568
|
+
: null;
|
|
569
|
+
if (shown?.app && !shown.link && !shown.resume && !shown.copy)
|
|
570
|
+
return shown;
|
|
571
|
+
const thread = holding.parent_id ?? null;
|
|
572
|
+
return {
|
|
573
|
+
opensIn: thread ? "the thread" : null,
|
|
574
|
+
run: holding.id,
|
|
575
|
+
thread,
|
|
576
|
+
note: thread
|
|
577
|
+
? `A thread's run is working in this ${nameOf(agentId)} session right now, so it opens in that thread. Once the run is done, Open carries on in the session itself.`
|
|
578
|
+
: `One of Harness's runs is working in this ${nameOf(agentId)} session right now. Open it again once the run is done.`,
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
const live = sessionId
|
|
582
|
+
? (liveNow().find(
|
|
583
|
+
(row) => row.agent === agentId && row.sessionId === sessionId
|
|
584
|
+
) ?? null)
|
|
585
|
+
: null;
|
|
586
|
+
if (live) return runningPlan(live, act);
|
|
587
|
+
const own = sessionId ? (placesOf(agentId)[sessionId] ?? null) : null;
|
|
588
|
+
const where = folder ?? own?.folder ?? null;
|
|
589
|
+
if (own?.reopen?.link)
|
|
590
|
+
return {
|
|
591
|
+
opensIn: short(own.name) || nameOf(agentId),
|
|
592
|
+
link: own.reopen.link,
|
|
593
|
+
};
|
|
594
|
+
const plan =
|
|
595
|
+
placePlan(agentId, homeOf(agentId), sessionId, where, act) ??
|
|
596
|
+
placePlan(agentId, usable(own), sessionId, where, act);
|
|
597
|
+
if (plan) return plan;
|
|
598
|
+
if (CODING_AGENTS.includes(agentId))
|
|
599
|
+
return {
|
|
600
|
+
opensIn: "Terminal",
|
|
601
|
+
resume: { agent: agentId, session: sessionId, cwd: where, app: null },
|
|
602
|
+
};
|
|
603
|
+
const command = own?.reopen?.command;
|
|
604
|
+
return command
|
|
605
|
+
? {
|
|
606
|
+
opensIn: null,
|
|
607
|
+
copy: command,
|
|
608
|
+
note: "The command that opens this session again is copied: paste it in a terminal.",
|
|
609
|
+
}
|
|
610
|
+
: { opensIn: null };
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const liveCommand = (request, agent) => {
|
|
614
|
+
const sessionId = String(request.sessionId || "");
|
|
615
|
+
if (request.command === "board.live.take") {
|
|
616
|
+
if (!LIVE_AGENTS.includes(agent) || !sessionId)
|
|
617
|
+
return {
|
|
618
|
+
ok: false,
|
|
619
|
+
error: "only a session's own agent takes its messages",
|
|
620
|
+
};
|
|
621
|
+
const messages = said(agent, sessionId);
|
|
622
|
+
if (messages.length) store.meta(saidKey(agent, sessionId), "[]");
|
|
623
|
+
return { ok: true, messages };
|
|
624
|
+
}
|
|
625
|
+
// A session says where it runs (its hook, plugin or extension, or a custom harness, as it starts).
|
|
626
|
+
if (request.command === "board.live.place") {
|
|
627
|
+
if (agent === "user")
|
|
628
|
+
return { ok: false, error: "a session reports its own place" };
|
|
629
|
+
if (!SESSION_ID.test(sessionId))
|
|
630
|
+
return { ok: false, error: "a place needs the session's id" };
|
|
631
|
+
const folder = folderOf(request);
|
|
632
|
+
const place = placeOfReport(agent, folder, request);
|
|
633
|
+
const reopen = reopenOf(request.reopen);
|
|
634
|
+
if (place || (reopen && !harnessRun(folder)))
|
|
635
|
+
notePlace(agent, sessionId, {
|
|
636
|
+
...(place ?? { kind: "unknown", name: nameOf(agent), folder }),
|
|
637
|
+
...(reopen ? { reopen } : {}),
|
|
638
|
+
});
|
|
639
|
+
return { ok: true };
|
|
640
|
+
}
|
|
641
|
+
// A session's turn ended with nothing more to do (its Stop hook, pi's settle, OpenCode's idle): the person's turn.
|
|
642
|
+
if (request.command === "board.live.turn") {
|
|
643
|
+
if (agent === "user")
|
|
644
|
+
return { ok: false, error: "a session reports its own turn" };
|
|
645
|
+
noteTurn(agent, sessionId, request, true);
|
|
646
|
+
return { ok: true };
|
|
647
|
+
}
|
|
648
|
+
if (agent !== "user")
|
|
649
|
+
return {
|
|
650
|
+
ok: false,
|
|
651
|
+
error: "only the person reads or messages their sessions",
|
|
652
|
+
};
|
|
653
|
+
if (request.command === "board.live") {
|
|
654
|
+
// Each coding agent's newest session too, so one not running now still opens where it last worked.
|
|
655
|
+
const last = {};
|
|
656
|
+
for (const row of store.recentSessions(200)) {
|
|
657
|
+
if (
|
|
658
|
+
!LIVE_AGENTS.includes(row.agent) ||
|
|
659
|
+
last[row.agent] ||
|
|
660
|
+
!ownSession(row)
|
|
661
|
+
)
|
|
662
|
+
continue;
|
|
663
|
+
last[row.agent] = {
|
|
664
|
+
sessionId: nativeSessionId(row.agent, row.session_id),
|
|
665
|
+
folder: row.cwd || null,
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
ok: true,
|
|
670
|
+
sessions: liveNow(),
|
|
671
|
+
last,
|
|
672
|
+
homes: homes(),
|
|
673
|
+
t3: t3Access(),
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
// A session's latest messages and steps, from its own file (running or not).
|
|
677
|
+
if (request.command === "board.live.read") {
|
|
678
|
+
const agentId = String(request.to || "");
|
|
679
|
+
if (!LIVE_AGENTS.includes(agentId))
|
|
680
|
+
return {
|
|
681
|
+
ok: false,
|
|
682
|
+
error: "only a coding agent's session can be read here",
|
|
683
|
+
};
|
|
684
|
+
if (agentId === "opencode")
|
|
685
|
+
return {
|
|
686
|
+
ok: true,
|
|
687
|
+
items: opencodeFeed(
|
|
688
|
+
opencodeExecutable || "opencode",
|
|
689
|
+
sessionId,
|
|
690
|
+
Math.min(200, Number(request.limit) || 80)
|
|
691
|
+
),
|
|
692
|
+
};
|
|
693
|
+
const file = sessionFile(
|
|
694
|
+
agentId,
|
|
695
|
+
sessionId,
|
|
696
|
+
folderOf(request),
|
|
697
|
+
store.recentSessions(200)
|
|
698
|
+
);
|
|
699
|
+
if (!file) return { ok: true, items: [] };
|
|
700
|
+
return {
|
|
701
|
+
ok: true,
|
|
702
|
+
items: sessionFeed(
|
|
703
|
+
agentId,
|
|
704
|
+
file,
|
|
705
|
+
Math.min(200, Number(request.limit) || 80)
|
|
706
|
+
),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
// Where a session (or, with none, the agent) opens, for Open's label: nothing happens yet.
|
|
710
|
+
if (
|
|
711
|
+
request.command === "board.live.where" ||
|
|
712
|
+
request.command === "board.live.focus"
|
|
713
|
+
) {
|
|
714
|
+
const agentId = String(request.to || "");
|
|
715
|
+
if (!validAgent(agentId) || (sessionId && !SESSION_ID.test(sessionId)))
|
|
716
|
+
return { ok: false, error: "that session can't be opened" };
|
|
717
|
+
// Open itself: the daemon's part happens now, and the app does the rest.
|
|
718
|
+
return {
|
|
719
|
+
ok: true,
|
|
720
|
+
...openPlan(
|
|
721
|
+
agentId,
|
|
722
|
+
sessionId || null,
|
|
723
|
+
folderOf(request),
|
|
724
|
+
request.command === "board.live.focus"
|
|
725
|
+
),
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
if (request.command === "board.live.say") {
|
|
729
|
+
const text = String(request.text || "")
|
|
730
|
+
.trim()
|
|
731
|
+
.slice(0, 20_000);
|
|
732
|
+
if (!text) return { ok: false, error: "a message needs text" };
|
|
733
|
+
const session = liveNow().find(
|
|
734
|
+
(row) => row.agent === request.to && row.sessionId === sessionId
|
|
735
|
+
);
|
|
736
|
+
if (!session)
|
|
737
|
+
return { ok: false, error: "that session is not running any more" };
|
|
738
|
+
// Kept until the session's own hook takes it: when its turn ends, or the person next types there.
|
|
739
|
+
const keep = (t3) => {
|
|
740
|
+
const messages = [
|
|
741
|
+
...said(session.agent, sessionId),
|
|
742
|
+
{ text, at: new Date().toISOString() },
|
|
743
|
+
];
|
|
744
|
+
store.meta(saidKey(session.agent, sessionId), JSON.stringify(messages));
|
|
745
|
+
return {
|
|
746
|
+
ok: true,
|
|
747
|
+
sent: "hooks",
|
|
748
|
+
session: { ...session, waiting: messages.length },
|
|
749
|
+
...(t3 ? { t3 } : {}),
|
|
750
|
+
};
|
|
751
|
+
};
|
|
752
|
+
// A session in T3 Code, once the person approved Harness there: into its T3 thread, as T3 sends any message.
|
|
753
|
+
const thread =
|
|
754
|
+
session.host && /^T3 Code\b/.test(session.host.name)
|
|
755
|
+
? t3Thread(sessionId)
|
|
756
|
+
: null;
|
|
757
|
+
if (!thread) return keep(null);
|
|
758
|
+
const access = t3Access();
|
|
759
|
+
if (access?.state !== "send")
|
|
760
|
+
return keep(
|
|
761
|
+
!access
|
|
762
|
+
? "not_connected"
|
|
763
|
+
: access.state === "read"
|
|
764
|
+
? "read_only"
|
|
765
|
+
: access.state
|
|
766
|
+
);
|
|
767
|
+
return {
|
|
768
|
+
pending: sendToT3(thread, text).then(
|
|
769
|
+
(delivery) => ({ ok: true, sent: "t3", delivery, session }),
|
|
770
|
+
(error) => {
|
|
771
|
+
log(`T3 Code did not take a message: ${error.message}`);
|
|
772
|
+
return keep(error.code || "failed");
|
|
773
|
+
}
|
|
774
|
+
),
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
return { ok: false, error: `unknown command ${request.command}` };
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
return {
|
|
781
|
+
command: liveCommand,
|
|
782
|
+
placesOf,
|
|
783
|
+
noteTurn,
|
|
784
|
+
yourTurns,
|
|
785
|
+
homeOf,
|
|
786
|
+
folderOf,
|
|
787
|
+
};
|
|
788
|
+
}
|