@rarog/server 1.1.2-beta.1 → 1.1.2-beta.2
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/node_modules/@rarog/shared/package.json +1 -1
- package/node_modules/@rarog/shared/src/decode.js +43 -1
- package/node_modules/@rarog/shared/src/notify-copy.js +1 -0
- package/node_modules/@rarog/shared/src/protocol/context.js +57 -0
- package/node_modules/@rarog/shared/src/protocol/coordination.js +42 -0
- package/node_modules/@rarog/shared/src/protocol/dialog-live.js +117 -0
- package/node_modules/@rarog/shared/src/protocol/files.js +1 -1
- package/node_modules/@rarog/shared/src/protocol/inbox.js +6 -0
- package/node_modules/@rarog/shared/src/protocol/presence.js +47 -0
- package/node_modules/@rarog/shared/src/protocol.js +14 -146
- package/node_modules/@rarog/shared/src/rest.js +16 -1
- package/node_modules/@rarog/shared/src/sentences/push.js +6 -0
- package/node_modules/@rarog/shared/src/sentences/rest.js +9 -0
- package/node_modules/@rarog/shared/src/sentences/restart.js +1 -0
- package/node_modules/@rarog/shared/src/sentences/session.js +24 -0
- package/package.json +2 -2
- package/plugin/rarog/.claude-plugin/plugin.json +4 -0
- package/plugin/rarog/bin/rarog-coordinator +7 -0
- package/plugin/rarog/hooks/hooks.json +18 -0
- package/plugin/rarog/hooks/recipient.mjs +95 -0
- package/plugin/rarog/lib/coordinator.mjs +1340 -0
- package/plugin/rarog/lib/threads.mjs +350 -0
- package/plugin/rarog/skills/coordinator/SKILL.md +111 -0
- package/plugin/rarog/skills/coordinator/check-and-merge.md +46 -0
- package/plugin/rarog/skills/coordinator/close-thread.md +15 -0
- package/plugin/rarog/skills/coordinator/role.md +61 -0
- package/plugin/rarog/skills/coordinator/send-back.md +39 -0
- package/plugin/rarog/skills/coordinator/start-thread.md +43 -0
- package/server/src/bootstrap/coordination.js +49 -0
- package/server/src/bootstrap/coordinator-start.js +27 -0
- package/server/src/bootstrap/inbox.js +4 -0
- package/server/src/bootstrap/push.js +24 -1
- package/server/src/companion/catalog.js +1 -1
- package/server/src/companion/index.js +2 -4
- package/server/src/config/flags.js +4 -0
- package/server/src/config-env.js +15 -0
- package/server/src/config.js +17 -3
- package/server/src/coordination/merge-events.js +86 -0
- package/server/src/coordination/merge-prompt.js +244 -0
- package/server/src/coordination/registry.js +289 -0
- package/server/src/coordination/state.js +94 -0
- package/server/src/coordination/summary.js +422 -0
- package/server/src/coordination/thread-file.js +245 -0
- package/server/src/coordination/thread-git.js +110 -0
- package/server/src/coordination/watch.js +237 -0
- package/server/src/demo/content/coordinator.js +89 -0
- package/server/src/demo/content/en.js +57 -1
- package/server/src/demo/content/pl.js +57 -1
- package/server/src/demo/inbox.js +12 -0
- package/server/src/demo/mock-herdr.js +3 -2
- package/server/src/demo/review-routes.js +2 -2
- package/server/src/demo/seeds.js +20 -2
- package/server/src/drivers/herdr/live-dialog.js +412 -0
- package/server/src/drivers/herdr/live-step.js +261 -0
- package/server/src/drivers/herdr/pause-gate.js +60 -0
- package/server/src/drivers/herdr/returned-prompt.js +4 -1
- package/server/src/drivers/herdr/screen-fallback.js +50 -0
- package/server/src/drivers/herdr/start-dialog-gate.js +4 -0
- package/server/src/drivers/herdr-driver.js +88 -66
- package/server/src/files/refusals.js +9 -2
- package/server/src/files/session-gate.js +5 -4
- package/server/src/git-branch-diff.js +194 -0
- package/server/src/herdr/argv.js +34 -3
- package/server/src/herdr/audit.js +32 -1
- package/server/src/herdr/background/cli-shapes.js +19 -0
- package/server/src/herdr/background/queued-prompt.js +46 -0
- package/server/src/herdr/background/work.js +12 -0
- package/server/src/herdr/client.js +22 -46
- package/server/src/herdr/commands/catalog.js +11 -7
- package/server/src/herdr/commands/discover.js +33 -1
- package/server/src/herdr/commands/index.js +3 -2
- package/server/src/herdr/dialogs/index.js +9 -0
- package/server/src/herdr/dialogs/layer1/hook-list/hook-list.js +4 -56
- package/server/src/herdr/dialogs/live/model.js +220 -0
- package/server/src/herdr/dialogs/live/policy.js +108 -0
- package/server/src/herdr/dialogs/live/view.js +261 -0
- package/server/src/herdr/dialogs/screen/form.js +4 -3
- package/server/src/herdr/dialogs/screen/signatures.js +23 -0
- package/server/src/herdr/engines.js +96 -0
- package/server/src/herdr/history-steps.js +9 -4
- package/server/src/herdr/identity.js +20 -5
- package/server/src/herdr/jsonl-follow.js +20 -1
- package/server/src/herdr/plugin-copy.js +281 -0
- package/server/src/herdr/session-index.js +19 -0
- package/server/src/herdr/supervisor.js +16 -13
- package/server/src/herdr/types.js +1 -0
- package/server/src/inbox/card.js +31 -8
- package/server/src/inbox/heads.js +1 -0
- package/server/src/inbox/inbox.js +21 -4
- package/server/src/inbox/removals.js +10 -1
- package/server/src/index.js +37 -5
- package/server/src/notify/content.js +27 -0
- package/server/src/notify/index.js +7 -3
- package/server/src/notify/live-activity-state.js +14 -5
- package/server/src/notify/policy.js +49 -11
- package/server/src/notify/ready-to-merge.js +148 -0
- package/server/src/project-root.js +233 -0
- package/server/src/routes/sessions.js +68 -2
- package/server/src/sessions/branch-diff.js +57 -0
- package/server/src/sessions/cli-registry.js +17 -3
- package/server/src/sessions/companion-scope.js +2 -1
- package/server/src/sessions/consent-cards.js +49 -2
- package/server/src/sessions/conversation-lists.js +4 -1
- package/server/src/sessions/coordinator-start.js +172 -0
- package/server/src/sessions/demo-modules.js +1 -0
- package/server/src/sessions/demo-seeds.js +100 -0
- package/server/src/sessions/driver-events.js +61 -11
- package/server/src/sessions/herdr-lifecycle.js +11 -4
- package/server/src/sessions/launch-gate.js +36 -11
- package/server/src/sessions/live-dialog.js +69 -0
- package/server/src/sessions/manager.js +124 -282
- package/server/src/sessions/owner-turn.js +36 -0
- package/server/src/sessions/seen-marks.js +31 -12
- package/server/src/sessions/session-dialog.js +82 -0
- package/server/src/sessions/session-git.js +1 -0
- package/server/src/sessions/session-scope.js +6 -5
- package/server/src/sessions/session-take-back.js +5 -0
- package/server/src/sessions/session.js +72 -7
- package/server/src/sessions/state-machine.js +2 -0
- package/server/src/sessions/state-sentences.js +87 -30
- package/server/src/sessions/waiting-line.js +49 -0
- package/server/src/transcript-import.js +20 -2
- package/server/src/version.js +4 -0
- /package/server/src/{companion/asks-owner.js → asks-owner.js} +0 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coordination fields of the session summaries: `projectRoot` and `coordination`, laid over a summary from memory
|
|
3
|
+
* ({@link SessionCoordination.overlay}), and the reads that keep that memory fresh: the main checkout of each session
|
|
4
|
+
* directory, the thread files of each project seen, the git facts of each thread session and the coordinator the
|
|
5
|
+
* registry holds. When a read or a session event changes what a summary carries, the sessions it touches get
|
|
6
|
+
* `session-changed` through the port the manager binds. The thread state itself is the rule in `state.ts`; the Merge
|
|
7
|
+
* events come from the transcript entries of the live coordinator and of its threads the followers pass in
|
|
8
|
+
* (`merge-events.ts`). It writes no file.
|
|
9
|
+
*/
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { areaDiskOf, inExcludedOnDisk, inWorkAreaOnDisk } from '../config/work-area.js';
|
|
12
|
+
import { DIRECT_DISK } from '../folder-gate/disk.js';
|
|
13
|
+
import { hasBackgroundWork } from '../herdr/background/index.js';
|
|
14
|
+
import { ProjectRoots } from '../project-root.js';
|
|
15
|
+
import { MergeEvents } from './merge-events.js';
|
|
16
|
+
import { threadCoordination } from './state.js';
|
|
17
|
+
import { readProjectThreads } from './thread-file.js';
|
|
18
|
+
import { ThreadGitFacts } from './thread-git.js';
|
|
19
|
+
import { CoordinationWatcher } from './watch.js';
|
|
20
|
+
/** A closed coordinator, or one whose process is gone, holds no role. */
|
|
21
|
+
const NOT_LIVE = new Set(['done', 'resumable']);
|
|
22
|
+
/**
|
|
23
|
+
* The main checkout a session in `cwd` counts for: the project root of a worktree session, else the real path of
|
|
24
|
+
* `cwd`. A Launch of a coordinator stores its project under the same string.
|
|
25
|
+
*/
|
|
26
|
+
export function projectOfSession(roots, cwd, disk = DIRECT_DISK) {
|
|
27
|
+
const root = roots.peek(cwd);
|
|
28
|
+
if (root !== undefined)
|
|
29
|
+
return root;
|
|
30
|
+
try {
|
|
31
|
+
return disk.sync('realpath', { path: cwd });
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return path.resolve(cwd);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class SessionCoordination {
|
|
38
|
+
#registry;
|
|
39
|
+
#roots;
|
|
40
|
+
#disk;
|
|
41
|
+
#area;
|
|
42
|
+
#git;
|
|
43
|
+
#watcher;
|
|
44
|
+
#pluginOf;
|
|
45
|
+
#now;
|
|
46
|
+
#port = null;
|
|
47
|
+
#projects = new Map();
|
|
48
|
+
/** Conversation → its thread file and project, over every project read. */
|
|
49
|
+
#threadOf = new Map();
|
|
50
|
+
/** Session id → the project whose coordinator role it held when last seen. */
|
|
51
|
+
#holding = new Map();
|
|
52
|
+
#watched = new Set();
|
|
53
|
+
/** Session id → the merge state its last summary carried, so `since` stays while the state stays. */
|
|
54
|
+
#previous = new Map();
|
|
55
|
+
/** Session id → its last `coordination`, to tell whether a state event changed it. */
|
|
56
|
+
#shown = new Map();
|
|
57
|
+
/** Thread sessions whose first git read was asked. */
|
|
58
|
+
#gitAsked = new Set();
|
|
59
|
+
#dirty = new Set();
|
|
60
|
+
#merges;
|
|
61
|
+
#flushQueued = false;
|
|
62
|
+
#closed = false;
|
|
63
|
+
constructor(opts) {
|
|
64
|
+
this.#registry = opts.registry;
|
|
65
|
+
this.#disk = opts.disk ?? DIRECT_DISK;
|
|
66
|
+
this.#area = { roots: opts.roots, home: opts.home };
|
|
67
|
+
this.#roots = new ProjectRoots({
|
|
68
|
+
roots: opts.roots, home: opts.home, disk: this.#disk, changed: (cwd) => { this.#cwdChanged(cwd); },
|
|
69
|
+
});
|
|
70
|
+
this.#git = new ThreadGitFacts({ run: opts.run, disk: this.#disk });
|
|
71
|
+
this.#pluginOf = opts.pluginOf ?? (() => false);
|
|
72
|
+
this.#now = opts.now ?? (() => Date.now());
|
|
73
|
+
this.#merges = new MergeEvents(this.#now);
|
|
74
|
+
const onChange = (main) => { this.#read(main); };
|
|
75
|
+
this.#watcher = opts.watcher?.(onChange) ?? new CoordinationWatcher({ onChange });
|
|
76
|
+
}
|
|
77
|
+
bind(port) {
|
|
78
|
+
this.#port = port;
|
|
79
|
+
}
|
|
80
|
+
/** The coordination fields over a summary the manager builds; answers from memory and starts the reads it lacks. */
|
|
81
|
+
overlay(summary, session) {
|
|
82
|
+
if (this.#closed)
|
|
83
|
+
return;
|
|
84
|
+
const { projectRoot, coordination } = this.#fieldsOf(session);
|
|
85
|
+
if (projectRoot !== undefined)
|
|
86
|
+
summary.projectRoot = projectRoot;
|
|
87
|
+
if (coordination !== undefined)
|
|
88
|
+
summary.coordination = coordination;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* An event of the manager's stream. A state change moves the role of a coordinator and the merge state of a
|
|
92
|
+
* thread, which a `state` frame does not carry, so the sessions whose `coordination` changed get `session-changed`;
|
|
93
|
+
* a thread also reads its git facts again, because a write in its worktree that moved neither `HEAD` nor the index
|
|
94
|
+
* is seen by no watcher.
|
|
95
|
+
*/
|
|
96
|
+
noteEvent(event) {
|
|
97
|
+
if (this.#closed || (event.type !== 'state' && event.type !== 'git-status'))
|
|
98
|
+
return;
|
|
99
|
+
const session = this.#port?.session(event.sessionId);
|
|
100
|
+
if (session === undefined)
|
|
101
|
+
return;
|
|
102
|
+
this.#refreshGit(session);
|
|
103
|
+
if (event.type === 'state' && this.#shown.get(session.id) !== JSON.stringify(this.#fieldsOf(session).coordination)) {
|
|
104
|
+
this.#markDirty(session.id);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** The git watcher saw a change in these session directories. */
|
|
108
|
+
gitChanged(cwds) {
|
|
109
|
+
if (this.#closed)
|
|
110
|
+
return;
|
|
111
|
+
this.#roots.forget(cwds);
|
|
112
|
+
const changed = new Set(cwds);
|
|
113
|
+
for (const session of this.#port?.sessions() ?? [])
|
|
114
|
+
if (changed.has(session.cwd))
|
|
115
|
+
this.#refreshGit(session);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The session's pane runs another conversation (`/clear`, `/resume`); only its `engineSessionId` changed, which no
|
|
119
|
+
* frame carries. The facts of the previous conversation go, and the session's summary is built again: that asks the
|
|
120
|
+
* registry about the new conversation, so a coordinator that lost its role sends its threads fresh summaries too.
|
|
121
|
+
*/
|
|
122
|
+
conversationChanged(sessionId) {
|
|
123
|
+
if (this.#closed)
|
|
124
|
+
return;
|
|
125
|
+
this.#previous.delete(sessionId);
|
|
126
|
+
this.#shown.delete(sessionId);
|
|
127
|
+
this.#gitAsked.delete(sessionId);
|
|
128
|
+
this.#git.forget(sessionId);
|
|
129
|
+
this.#markDirty(sessionId);
|
|
130
|
+
}
|
|
131
|
+
/** The session left the manager: it holds no role, and its thread memory goes. */
|
|
132
|
+
forget(sessionId) {
|
|
133
|
+
this.#registry.ended(sessionId);
|
|
134
|
+
this.#released(sessionId);
|
|
135
|
+
this.#previous.delete(sessionId);
|
|
136
|
+
this.#shown.delete(sessionId);
|
|
137
|
+
this.#gitAsked.delete(sessionId);
|
|
138
|
+
this.#git.forget(sessionId);
|
|
139
|
+
}
|
|
140
|
+
/** `checked` of the thread file of the session, from memory; `undefined` for no thread or no checked commit. */
|
|
141
|
+
checkedOf(sessionId) {
|
|
142
|
+
const conversation = this.#port?.session(sessionId)?.engineSessionId;
|
|
143
|
+
return (conversation === undefined ? undefined : this.#threadOf.get(conversation)?.thread.checked) ?? undefined;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Whether a message from another session in the transcript of `conversation` is a prompt bubble: for a thread of a
|
|
147
|
+
* live coordinator, whose work arrives that way, and for a live coordinator, whose threads report that way. Answers
|
|
148
|
+
* from memory, synchronously, at every line a follower reads.
|
|
149
|
+
*/
|
|
150
|
+
crossSessionPrompts(conversation) {
|
|
151
|
+
if (this.#closed)
|
|
152
|
+
return false;
|
|
153
|
+
const found = this.#threadOf.get(conversation);
|
|
154
|
+
if (found !== undefined && this.#registry.coordinatorOf(found.main) !== undefined)
|
|
155
|
+
return true;
|
|
156
|
+
return this.#coordinatorRunning(conversation) !== undefined;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* One entry a follower read from the transcript of `conversation`, live, in file order. A live coordinator's entries
|
|
160
|
+
* count: a Merge prompt makes its thread `merging`, the end of its turn ends that. So do a thread's: a message whose
|
|
161
|
+
* sender process the CLI process registry lists for its live coordinator's conversation sends it back.
|
|
162
|
+
*/
|
|
163
|
+
transcriptEntry(conversation, entry) {
|
|
164
|
+
if (this.#closed)
|
|
165
|
+
return;
|
|
166
|
+
const found = this.#threadOf.get(conversation);
|
|
167
|
+
if (found !== undefined) {
|
|
168
|
+
const pids = (one) => this.#port?.cliPids(one) ?? null;
|
|
169
|
+
if (this.#merges.threadEntry(found.main, found.thread.name, entry, pids))
|
|
170
|
+
this.#threadTouched(found.thread);
|
|
171
|
+
}
|
|
172
|
+
const project = this.#coordinatorRunning(conversation);
|
|
173
|
+
if (project === undefined)
|
|
174
|
+
return;
|
|
175
|
+
const threads = this.#projects.get(project)?.threads ?? [];
|
|
176
|
+
for (const name of this.#merges.coordinatorEntry(project, conversation, entry, threads)) {
|
|
177
|
+
this.#threadTouched(threads.find((thread) => thread.name === name));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/** The project whose live coordinator runs `conversation` now. */
|
|
181
|
+
#coordinatorRunning(conversation) {
|
|
182
|
+
for (const [sessionId, project] of this.#holding) {
|
|
183
|
+
if (this.#registry.coordinatorOf(project) !== sessionId)
|
|
184
|
+
continue;
|
|
185
|
+
if (this.#port?.session(sessionId)?.engineSessionId === conversation)
|
|
186
|
+
return project;
|
|
187
|
+
}
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
/** The sessions running the thread's conversation get fresh summaries. */
|
|
191
|
+
#threadTouched(thread) {
|
|
192
|
+
if (thread === undefined)
|
|
193
|
+
return;
|
|
194
|
+
for (const session of this.#port?.sessions() ?? []) {
|
|
195
|
+
if (session.engineSessionId === thread.session)
|
|
196
|
+
this.#markDirty(session.id);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
close() {
|
|
200
|
+
this.#closed = true;
|
|
201
|
+
this.#watcher.close();
|
|
202
|
+
}
|
|
203
|
+
#fieldsOf(session) {
|
|
204
|
+
const projectRoot = this.#roots.peek(session.cwd);
|
|
205
|
+
const project = projectOfSession({ peek: () => projectRoot }, session.cwd, this.#disk);
|
|
206
|
+
if (!this.#projects.has(project))
|
|
207
|
+
this.#read(project);
|
|
208
|
+
this.#observe(session, project);
|
|
209
|
+
let coordination;
|
|
210
|
+
if (this.#registry.coordinatorOf(project) === session.id) {
|
|
211
|
+
const open = (this.#projects.get(project)?.threads ?? []).filter((thread) => thread.merged === null).length;
|
|
212
|
+
coordination = { role: 'coordinator', threads: open };
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const found = session.engineSessionId === undefined ? undefined : this.#threadOf.get(session.engineSessionId);
|
|
216
|
+
if (found !== undefined)
|
|
217
|
+
coordination = this.#threadRole(session, found.main, found.thread);
|
|
218
|
+
else
|
|
219
|
+
this.#previous.delete(session.id);
|
|
220
|
+
}
|
|
221
|
+
this.#shown.set(session.id, JSON.stringify(coordination));
|
|
222
|
+
return {
|
|
223
|
+
...(projectRoot === undefined ? {} : { projectRoot }),
|
|
224
|
+
...(coordination === undefined ? {} : { coordination }),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
#inArea(main) {
|
|
228
|
+
try {
|
|
229
|
+
const area = areaDiskOf(this.#disk);
|
|
230
|
+
return !inExcludedOnDisk(main, this.#area.roots, this.#area.home, area)
|
|
231
|
+
&& inWorkAreaOnDisk(main, this.#area.roots, this.#area.home, area);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Asks the registry whether the session is its project's coordinator; a changed holder touches the project. */
|
|
238
|
+
#observe(session, project) {
|
|
239
|
+
if (NOT_LIVE.has(session.state))
|
|
240
|
+
this.#registry.ended(session.id);
|
|
241
|
+
else {
|
|
242
|
+
this.#registry.observe({
|
|
243
|
+
sessionId: session.id, project, conversation: session.engineSessionId, plugin: this.#pluginOf(session.id),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
const held = this.#registry.coordinatorOf(project) === session.id ? project : undefined;
|
|
247
|
+
const before = this.#holding.get(session.id);
|
|
248
|
+
if (held === before)
|
|
249
|
+
return;
|
|
250
|
+
if (held === undefined) {
|
|
251
|
+
this.#released(session.id);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.#released(session.id);
|
|
255
|
+
this.#holding.set(session.id, held);
|
|
256
|
+
if (session.engineSessionId !== undefined)
|
|
257
|
+
this.#merges.started(held, session.engineSessionId);
|
|
258
|
+
this.#holderChanged(held, session.id);
|
|
259
|
+
}
|
|
260
|
+
#released(sessionId) {
|
|
261
|
+
const project = this.#holding.get(sessionId);
|
|
262
|
+
if (project === undefined)
|
|
263
|
+
return;
|
|
264
|
+
this.#holding.delete(sessionId);
|
|
265
|
+
this.#merges.ended(project);
|
|
266
|
+
this.#holderChanged(project, sessionId);
|
|
267
|
+
}
|
|
268
|
+
/** The project gained or lost its coordinator: the watcher follows, and its threads get fresh summaries. */
|
|
269
|
+
#holderChanged(project, holder) {
|
|
270
|
+
// A project outside the work area, or in an excluded folder, is never watched nor read.
|
|
271
|
+
const live = this.#registry.coordinatorOf(project) !== undefined && this.#inArea(project);
|
|
272
|
+
if (live && !this.#watched.has(project)) {
|
|
273
|
+
this.#watched.add(project);
|
|
274
|
+
this.#watcher.watch(project);
|
|
275
|
+
// Thread files written while nobody watched.
|
|
276
|
+
this.#read(project);
|
|
277
|
+
}
|
|
278
|
+
else if (!live && this.#watched.delete(project)) {
|
|
279
|
+
this.#watcher.unwatch(project);
|
|
280
|
+
}
|
|
281
|
+
for (const session of this.#port?.sessions() ?? []) {
|
|
282
|
+
if (session.id === holder)
|
|
283
|
+
continue;
|
|
284
|
+
const conversation = session.engineSessionId;
|
|
285
|
+
if (conversation !== undefined && this.#threadOf.get(conversation)?.main === project)
|
|
286
|
+
this.#markDirty(session.id);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
#threadRole(session, main, thread) {
|
|
290
|
+
if (!this.#gitAsked.has(session.id))
|
|
291
|
+
this.#refreshGit(session);
|
|
292
|
+
const git = this.#git.get(session.id);
|
|
293
|
+
const coordinatorId = this.#registry.coordinatorOf(main);
|
|
294
|
+
const coordinator = coordinatorId === undefined ? undefined : this.#port?.session(coordinatorId);
|
|
295
|
+
const role = threadCoordination(thread.name, {
|
|
296
|
+
file: {
|
|
297
|
+
...(thread.checked === null ? {} : { checked: thread.checked }),
|
|
298
|
+
...(thread.merged === null ? {} : { merged: thread.merged }),
|
|
299
|
+
},
|
|
300
|
+
session: { state: session.state, backgroundWork: hasBackgroundWork(session.backgroundTasks) },
|
|
301
|
+
git: {
|
|
302
|
+
...(git?.head == null ? {} : { head: git.head }),
|
|
303
|
+
...(git?.clean == null ? {} : { clean: git.clean }),
|
|
304
|
+
commits: git?.commits ?? 0, files: git?.files ?? 0,
|
|
305
|
+
},
|
|
306
|
+
...(coordinator === undefined ? {} : { coordinator: { id: coordinator.id, state: coordinator.state } }),
|
|
307
|
+
events: this.#merges.eventsOf(main, thread.name),
|
|
308
|
+
}, new Date(this.#now()).toISOString(), this.#previous.get(session.id));
|
|
309
|
+
// Unknown counts (no branch, a failed read, no read yet) give no merge state rather than a made-up 0, except for
|
|
310
|
+
// `merged`: a merged branch has no commits over the main branch, so its counts are 0 even once it is deleted.
|
|
311
|
+
if (role.merge !== undefined && role.merge.state !== 'merged' && (git?.commits == null || git.files == null)) {
|
|
312
|
+
delete role.merge;
|
|
313
|
+
}
|
|
314
|
+
if (role.merge === undefined)
|
|
315
|
+
this.#previous.delete(session.id);
|
|
316
|
+
else
|
|
317
|
+
this.#previous.set(session.id, role.merge);
|
|
318
|
+
return role;
|
|
319
|
+
}
|
|
320
|
+
/** Reads the git facts of a thread session again; a change sends its summary. */
|
|
321
|
+
#refreshGit(session) {
|
|
322
|
+
const conversation = session.engineSessionId;
|
|
323
|
+
const found = conversation === undefined ? undefined : this.#threadOf.get(conversation);
|
|
324
|
+
if (found === undefined)
|
|
325
|
+
return;
|
|
326
|
+
this.#gitAsked.add(session.id);
|
|
327
|
+
void this.#git.refresh(session.id, found.main, found.thread).then((changed) => {
|
|
328
|
+
if (changed)
|
|
329
|
+
this.#markDirty(session.id);
|
|
330
|
+
}, () => undefined);
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Reads the thread files of a project; a read asked while one runs runs once more after it. A project outside the
|
|
334
|
+
* work area, or in an excluded folder, is never read: it holds no threads.
|
|
335
|
+
*/
|
|
336
|
+
#read(main) {
|
|
337
|
+
if (this.#closed)
|
|
338
|
+
return;
|
|
339
|
+
let project = this.#projects.get(main);
|
|
340
|
+
if (project === undefined) {
|
|
341
|
+
project = { threads: [], reading: false, again: false };
|
|
342
|
+
this.#projects.set(main, project);
|
|
343
|
+
}
|
|
344
|
+
if (!this.#inArea(main))
|
|
345
|
+
return;
|
|
346
|
+
if (project.reading) {
|
|
347
|
+
project.again = true;
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const entry = project;
|
|
351
|
+
entry.reading = true;
|
|
352
|
+
void (async () => {
|
|
353
|
+
try {
|
|
354
|
+
do {
|
|
355
|
+
entry.again = false;
|
|
356
|
+
const read = await readProjectThreads(main, this.#disk);
|
|
357
|
+
if (this.#closed)
|
|
358
|
+
return;
|
|
359
|
+
this.#settle(main, entry, read.ok ? read.threads : []);
|
|
360
|
+
} while (entry.again);
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
// `readProjectThreads` answers a refusal instead of throwing; nothing to keep from an unexpected throw.
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
entry.reading = false;
|
|
367
|
+
}
|
|
368
|
+
})();
|
|
369
|
+
}
|
|
370
|
+
/** New thread files of a project: the index follows, and the sessions they name get fresh summaries. */
|
|
371
|
+
#settle(main, project, threads) {
|
|
372
|
+
const before = project.threads;
|
|
373
|
+
project.threads = threads;
|
|
374
|
+
if (JSON.stringify(before) === JSON.stringify(threads))
|
|
375
|
+
return;
|
|
376
|
+
const touched = new Set();
|
|
377
|
+
for (const thread of before) {
|
|
378
|
+
touched.add(thread.session);
|
|
379
|
+
if (this.#threadOf.get(thread.session)?.main === main)
|
|
380
|
+
this.#threadOf.delete(thread.session);
|
|
381
|
+
}
|
|
382
|
+
for (const thread of threads) {
|
|
383
|
+
touched.add(thread.session);
|
|
384
|
+
this.#threadOf.set(thread.session, { main, thread });
|
|
385
|
+
if (thread.merged !== null)
|
|
386
|
+
this.#merges.merged(main, thread.name);
|
|
387
|
+
}
|
|
388
|
+
const holder = this.#registry.coordinatorOf(main);
|
|
389
|
+
if (holder !== undefined)
|
|
390
|
+
this.#markDirty(holder);
|
|
391
|
+
for (const session of this.#port?.sessions() ?? []) {
|
|
392
|
+
if (session.engineSessionId === undefined || !touched.has(session.engineSessionId))
|
|
393
|
+
continue;
|
|
394
|
+
this.#markDirty(session.id);
|
|
395
|
+
this.#refreshGit(session);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/** The main checkout of a session directory changed: its sessions get fresh summaries. */
|
|
399
|
+
#cwdChanged(cwd) {
|
|
400
|
+
for (const session of this.#port?.sessions() ?? [])
|
|
401
|
+
if (session.cwd === cwd)
|
|
402
|
+
this.#markDirty(session.id);
|
|
403
|
+
}
|
|
404
|
+
/** Sends the summaries of touched sessions once, after the code that touched them, never inside a summary build. */
|
|
405
|
+
#markDirty(sessionId) {
|
|
406
|
+
if (this.#closed)
|
|
407
|
+
return;
|
|
408
|
+
this.#dirty.add(sessionId);
|
|
409
|
+
if (this.#flushQueued)
|
|
410
|
+
return;
|
|
411
|
+
this.#flushQueued = true;
|
|
412
|
+
queueMicrotask(() => {
|
|
413
|
+
this.#flushQueued = false;
|
|
414
|
+
const ids = [...this.#dirty];
|
|
415
|
+
this.#dirty.clear();
|
|
416
|
+
if (this.#closed)
|
|
417
|
+
return;
|
|
418
|
+
for (const id of ids)
|
|
419
|
+
this.#port?.emit(id);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader of the coordinator's thread files, `<main checkout>/.rarog/threads/<slug>.md`, in the strict format the
|
|
3
|
+
* coordinator plugin writes (`plugin/rarog/lib/threads.mjs`, same keys and limits). Reads go through the work-area
|
|
4
|
+
* disk port, so a closed guarded folder gives a refusal instead of a consent window. A file that does not match the
|
|
5
|
+
* format exactly is no thread, and its session stays an ordinary one. It writes nothing and runs no git.
|
|
6
|
+
*/
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { DIRECT_DISK } from '../folder-gate/disk.js';
|
|
9
|
+
/** The coordinator's directory in the main checkout, and the thread files in it. */
|
|
10
|
+
export const COORDINATION_DIR = '.rarog';
|
|
11
|
+
export const THREADS_DIR = 'threads';
|
|
12
|
+
/** Prefix of a thread session's name in front of the thread name. */
|
|
13
|
+
export const THREAD_SESSION_PREFIX = '[A] ';
|
|
14
|
+
export const THREAD_FILE_MAX_BYTES = 16 * 1024;
|
|
15
|
+
export const THREAD_NAME_MAX = 24;
|
|
16
|
+
/** The slug is the file name and the branch; a leading hyphen would read as an option of git. */
|
|
17
|
+
export const THREAD_SLUG_RE = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
18
|
+
/** ASCII letters, digits and hyphens; at most four words separated by single spaces. */
|
|
19
|
+
const THREAD_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9-]*(?: [A-Za-z0-9-]+){0,3}$/;
|
|
20
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
21
|
+
const SHA_RE = /^[0-9a-f]{40}$/;
|
|
22
|
+
/** The keys of the block, in the order the plugin writes them; any other key is skipped. */
|
|
23
|
+
export const THREAD_KEYS = ['format', 'name', 'session', 'branch', 'worktree', 'base', 'checked', 'merged'];
|
|
24
|
+
const THREAD_REQUIRED = ['format', 'name', 'session', 'branch', 'worktree', 'base'];
|
|
25
|
+
const KEY_LINE = /^([a-z]+):(?: (.*))?$/;
|
|
26
|
+
export function isThreadName(name) {
|
|
27
|
+
return name.length <= THREAD_NAME_MAX && THREAD_NAME_RE.test(name);
|
|
28
|
+
}
|
|
29
|
+
export function threadsDirOf(main) {
|
|
30
|
+
return path.join(main, COORDINATION_DIR, THREADS_DIR);
|
|
31
|
+
}
|
|
32
|
+
const errnoOf = (error) => (typeof error === 'object' && error !== null && typeof error.code === 'string'
|
|
33
|
+
? error.code : 'error');
|
|
34
|
+
/** The block between the two `---` lines: known keys only, the rest checked for shape and repetition. */
|
|
35
|
+
function parseBlock(text) {
|
|
36
|
+
if (/[\r\0]/u.test(text))
|
|
37
|
+
return { reason: 'contains a carriage return or NUL' };
|
|
38
|
+
const lines = text.split('\n');
|
|
39
|
+
if (lines[0] !== '---')
|
|
40
|
+
return { reason: 'does not start with ---' };
|
|
41
|
+
const end = lines.indexOf('---', 1);
|
|
42
|
+
if (end < 0)
|
|
43
|
+
return { reason: 'no closing ---' };
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const fields = {};
|
|
46
|
+
for (const line of lines.slice(1, end)) {
|
|
47
|
+
const match = KEY_LINE.exec(line);
|
|
48
|
+
if (match === null)
|
|
49
|
+
return { reason: `line is not "key: value": ${JSON.stringify(line.slice(0, 40))}` };
|
|
50
|
+
const key = match[1] ?? '';
|
|
51
|
+
if (seen.has(key))
|
|
52
|
+
return { reason: `repeated key ${key}` };
|
|
53
|
+
seen.add(key);
|
|
54
|
+
if (THREAD_KEYS.includes(key))
|
|
55
|
+
fields[key] = match[2] ?? '';
|
|
56
|
+
}
|
|
57
|
+
return { fields };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The format of one thread file, without the disk: `text` is the whole file, `slug` its name without `.md`. The
|
|
61
|
+
* worktree must be `<main>/.claude/worktrees/<slug>` for the main checkout the file names; {@link readThreadFile}
|
|
62
|
+
* checks that this main checkout is `main` and that the path has no symbolic link.
|
|
63
|
+
*/
|
|
64
|
+
export function parseThreadText(text, slug) {
|
|
65
|
+
if (Buffer.byteLength(text, 'utf8') > THREAD_FILE_MAX_BYTES)
|
|
66
|
+
return { reason: 'larger than 16 KiB' };
|
|
67
|
+
if (!THREAD_SLUG_RE.test(slug))
|
|
68
|
+
return { reason: 'file name is not a thread slug' };
|
|
69
|
+
const parsed = parseBlock(text);
|
|
70
|
+
if ('reason' in parsed)
|
|
71
|
+
return parsed;
|
|
72
|
+
const f = parsed.fields;
|
|
73
|
+
for (const key of THREAD_REQUIRED)
|
|
74
|
+
if (f[key] === undefined)
|
|
75
|
+
return { reason: `missing key ${key}` };
|
|
76
|
+
const name = f.name ?? '';
|
|
77
|
+
const session = f.session ?? '';
|
|
78
|
+
const worktree = f.worktree ?? '';
|
|
79
|
+
const base = f.base ?? '';
|
|
80
|
+
if (f.format !== '1')
|
|
81
|
+
return { reason: `format ${JSON.stringify(f.format)} is not 1` };
|
|
82
|
+
if (!isThreadName(name))
|
|
83
|
+
return { reason: `name ${JSON.stringify(name)} is outside the thread name alphabet` };
|
|
84
|
+
if (!UUID_RE.test(session))
|
|
85
|
+
return { reason: 'session is not a lowercase UUID' };
|
|
86
|
+
if (f.branch !== slug)
|
|
87
|
+
return { reason: `branch ${JSON.stringify(f.branch)} differs from the file name` };
|
|
88
|
+
const suffix = path.join('.claude', 'worktrees', slug);
|
|
89
|
+
const underMain = worktree.endsWith(`${path.sep}${suffix}`);
|
|
90
|
+
if (!path.isAbsolute(worktree) || path.normalize(worktree) !== worktree || !underMain) {
|
|
91
|
+
return { reason: `worktree is not <main checkout>/${suffix}` };
|
|
92
|
+
}
|
|
93
|
+
if (!SHA_RE.test(base))
|
|
94
|
+
return { reason: 'base is not a 40-character SHA' };
|
|
95
|
+
const checked = f.checked ?? '';
|
|
96
|
+
const merged = f.merged ?? '';
|
|
97
|
+
for (const [key, value] of [['checked', checked], ['merged', merged]]) {
|
|
98
|
+
if (value !== '' && !SHA_RE.test(value))
|
|
99
|
+
return { reason: `${key} is neither empty nor a 40-character SHA` };
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
thread: {
|
|
103
|
+
slug, name, session, branch: slug, worktree, base,
|
|
104
|
+
checked: checked === '' ? null : checked, merged: merged === '' ? null : merged,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Why `target` cannot be used as a path inside `root`, or `null` when it can: every existing component below `root`
|
|
110
|
+
* must be a real file or directory (a symbolic link anywhere refuses), and the real path of the deepest existing one
|
|
111
|
+
* must stay inside the real path of `root`. Components that do not exist (a removed worktree) are fine.
|
|
112
|
+
*/
|
|
113
|
+
async function pathProblem(disk, root, target) {
|
|
114
|
+
const rel = path.relative(root, target);
|
|
115
|
+
if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel))
|
|
116
|
+
return `${target} is not inside ${root}`;
|
|
117
|
+
let current = root;
|
|
118
|
+
let existing = root;
|
|
119
|
+
for (const part of rel.split(path.sep)) {
|
|
120
|
+
current = path.join(current, part);
|
|
121
|
+
let stat;
|
|
122
|
+
try {
|
|
123
|
+
stat = await disk.call('lstat', { path: current });
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (errnoOf(error) === 'ENOENT')
|
|
127
|
+
break;
|
|
128
|
+
return `cannot inspect ${current} (${errnoOf(error)})`;
|
|
129
|
+
}
|
|
130
|
+
if (stat.isSymbolicLink())
|
|
131
|
+
return `${current} is a symbolic link`;
|
|
132
|
+
existing = current;
|
|
133
|
+
}
|
|
134
|
+
const realRoot = await disk.call('realpath', { path: root });
|
|
135
|
+
const realExisting = await disk.call('realpath', { path: existing });
|
|
136
|
+
if (realExisting !== realRoot && !realExisting.startsWith(`${realRoot}${path.sep}`)) {
|
|
137
|
+
return `${existing} resolves to ${realExisting}, outside ${realRoot}`;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* One thread file of the project in `main`: read without following a symbolic link, at most 16 KiB, then
|
|
143
|
+
* {@link parseThreadText}. The main checkout the worktree path names must resolve to the same directory as `main`
|
|
144
|
+
* (the plugin writes it as git gives it, the server may know it by its real path), and the worktree path below it
|
|
145
|
+
* must pass {@link pathProblem}.
|
|
146
|
+
*/
|
|
147
|
+
export async function readThreadFile(main, slug, disk = DIRECT_DISK) {
|
|
148
|
+
const file = path.join(threadsDirOf(main), `${slug}.md`);
|
|
149
|
+
let text;
|
|
150
|
+
try {
|
|
151
|
+
const head = await disk.call('readHead', { path: file, maxBytes: THREAD_FILE_MAX_BYTES, followSymlink: false });
|
|
152
|
+
if (head === null)
|
|
153
|
+
return { reason: 'not a regular file' };
|
|
154
|
+
if (head.truncated)
|
|
155
|
+
return { reason: 'larger than 16 KiB' };
|
|
156
|
+
text = head.bytes.toString('utf8');
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
const code = errnoOf(error);
|
|
160
|
+
return { reason: code === 'ELOOP' ? 'a symbolic link' : `cannot read (${code})` };
|
|
161
|
+
}
|
|
162
|
+
const parsed = parseThreadText(text, slug);
|
|
163
|
+
if ('reason' in parsed)
|
|
164
|
+
return parsed;
|
|
165
|
+
const fileMain = parsed.thread.worktree.slice(0, -path.join('.claude', 'worktrees', slug).length - 1);
|
|
166
|
+
try {
|
|
167
|
+
if (fileMain !== main) {
|
|
168
|
+
const [realFileMain, realMain] = await Promise.all([
|
|
169
|
+
disk.call('realpath', { path: fileMain }), disk.call('realpath', { path: main }),
|
|
170
|
+
]);
|
|
171
|
+
if (realFileMain !== realMain)
|
|
172
|
+
return { reason: `worktree is not inside the main checkout ${main}` };
|
|
173
|
+
}
|
|
174
|
+
const problem = await pathProblem(disk, fileMain, parsed.thread.worktree);
|
|
175
|
+
if (problem !== null)
|
|
176
|
+
return { reason: `worktree: ${problem}` };
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return { reason: `worktree: cannot resolve (${errnoOf(error)})` };
|
|
180
|
+
}
|
|
181
|
+
return parsed;
|
|
182
|
+
}
|
|
183
|
+
/** Whether `dir` is a plain directory: `null` when absent, a reason when it is anything else or cannot be read. */
|
|
184
|
+
async function plainDirectory(disk, dir) {
|
|
185
|
+
try {
|
|
186
|
+
const stat = await disk.call('lstat', { path: dir });
|
|
187
|
+
return stat.isDirectory() ? true : `${dir} is not a plain directory`;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
return errnoOf(error) === 'ENOENT' ? false : `cannot inspect ${dir} (${errnoOf(error)})`;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Every thread of the project in `main`. No `.rarog` or no `threads` directory: no threads. Either one as a symbolic
|
|
195
|
+
* link or another kind of entry, or a directory that cannot be listed: `ok: false`. A `.md` file that is no thread
|
|
196
|
+
* goes to `rejected`; so do both files when two of them carry the same name or session, since a Merge prompt and a
|
|
197
|
+
* session must each find one thread. Other entries (the writer's temporary files, `checks`) are not read.
|
|
198
|
+
*/
|
|
199
|
+
export async function readProjectThreads(main, disk = DIRECT_DISK) {
|
|
200
|
+
for (const dir of [path.join(main, COORDINATION_DIR), threadsDirOf(main)]) {
|
|
201
|
+
const plain = await plainDirectory(disk, dir);
|
|
202
|
+
if (plain === false)
|
|
203
|
+
return { ok: true, threads: [], rejected: [] };
|
|
204
|
+
if (typeof plain === 'string')
|
|
205
|
+
return { ok: false, reason: plain };
|
|
206
|
+
}
|
|
207
|
+
let names;
|
|
208
|
+
try {
|
|
209
|
+
names = (await disk.call('readdir', { path: threadsDirOf(main), encoding: 'utf8' }))
|
|
210
|
+
.map((entry) => String(entry.name)).filter((name) => name.endsWith('.md')).sort();
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
return { ok: false, reason: `cannot list ${threadsDirOf(main)} (${errnoOf(error)})` };
|
|
214
|
+
}
|
|
215
|
+
const found = [];
|
|
216
|
+
const rejected = [];
|
|
217
|
+
for (const file of names) {
|
|
218
|
+
const slug = file.slice(0, -'.md'.length);
|
|
219
|
+
const read = THREAD_SLUG_RE.test(slug)
|
|
220
|
+
? await readThreadFile(main, slug, disk) : { reason: 'file name is not a thread slug' };
|
|
221
|
+
if ('thread' in read)
|
|
222
|
+
found.push(read.thread);
|
|
223
|
+
else
|
|
224
|
+
rejected.push({ file, reason: read.reason });
|
|
225
|
+
}
|
|
226
|
+
const count = (key, value) => found.filter((t) => t[key] === value).length;
|
|
227
|
+
const threads = [];
|
|
228
|
+
for (const thread of found) {
|
|
229
|
+
const file = `${thread.slug}.md`;
|
|
230
|
+
if (count('name', thread.name) > 1)
|
|
231
|
+
rejected.push({ file, reason: `name "${thread.name}" in another file too` });
|
|
232
|
+
else if (count('session', thread.session) > 1)
|
|
233
|
+
rejected.push({ file, reason: 'session in another file too' });
|
|
234
|
+
else
|
|
235
|
+
threads.push(thread);
|
|
236
|
+
}
|
|
237
|
+
return { ok: true, threads, rejected };
|
|
238
|
+
}
|
|
239
|
+
export function threadOfSession(threads, session) {
|
|
240
|
+
return threads.find((thread) => thread.session === session);
|
|
241
|
+
}
|
|
242
|
+
/** The thread with exactly this name (the name of a Merge prompt). */
|
|
243
|
+
export function threadNamed(threads, name) {
|
|
244
|
+
return threads.find((thread) => thread.name === name);
|
|
245
|
+
}
|