@beatos/bridge-runner 0.8.1 → 0.9.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 +140 -1
- package/dist/api.js +50 -1
- package/dist/claude.js +7 -1
- package/dist/config.js +12 -0
- package/dist/folders.js +6 -2
- package/dist/main.js +26 -4
- package/dist/outbox.js +31 -0
- package/dist/read-wall.js +236 -0
- package/dist/runner.js +26 -1
- package/dist/safety.js +79 -0
- package/dist/sessions-engine.js +984 -0
- package/dist/sessions-scan.js +652 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ computer to the internet. Nothing is merged, deployed or sent without your yes.
|
|
|
25
25
|
- [The key](#the-key)
|
|
26
26
|
- [Safety model](#safety-model)
|
|
27
27
|
- [What is on this computer (opt-in)](#what-is-on-this-computer-opt-in)
|
|
28
|
+
- [Sessions (opt-in)](#sessions-opt-in)
|
|
28
29
|
- [Files and logs](#files-and-logs)
|
|
29
30
|
- [Update](#update)
|
|
30
31
|
- [Uninstall](#uninstall)
|
|
@@ -228,6 +229,11 @@ Every field:
|
|
|
228
229
|
| `video.comprehension` | boolean | `true` | After processing, a short read-only Claude session writes a **Comprensión** into `VIDEO.md`. See [Saved in BeatOS](#saved-in-beatos-runner-080). |
|
|
229
230
|
| `video.comprehensionLanguage` | string | `task` | The language of the **Comprensión**. `task`: the language of the task title and description (Spanish when the task text is too short to tell). Any other value, like `es`, `en` or `Catalan` (ASCII letters, 30 at most), forces that language; a value that is not like that counts as `task`. |
|
|
230
231
|
| `video.saveToBeatOS` | boolean | `true` | Save the result (`VIDEO.md` and the frames, never the audio) in BeatOS as a Vault note of the task's project, so the video is not processed again. |
|
|
232
|
+
| `sessions.access` | `off`, `read` or `work` | `off` | Sessions in Bridge. `off`: nothing about your Claude Code sessions leaves the computer. `read`: chats can only read. `work`: chats may change files. Any other value is `off`. See [Sessions](#sessions-opt-in). |
|
|
233
|
+
| `sessions.maxTurnMinutes` | number | `30` | The time limit of one chat turn (1 to 240). |
|
|
234
|
+
| `sessions.maxTurnsPerDay` | number | `200` | How many chat turns this computer takes per day (1 to 5000). |
|
|
235
|
+
| `sessions.recentDays` | number | `7` | Sessions changed in the last N days are shown (1 to 30). |
|
|
236
|
+
| `sessions.maxSessions` | number | `100` | At most this many sessions are shown (10 to 200). Live ones are always shown. |
|
|
231
237
|
|
|
232
238
|
## Protect your private folders (protectPaths)
|
|
233
239
|
|
|
@@ -557,6 +563,123 @@ inside a description becomes `~/...` or `…`.
|
|
|
557
563
|
Turn it off on the same page. BeatOS then deletes the list, and the runner
|
|
558
564
|
stops reading.
|
|
559
565
|
|
|
566
|
+
## Sessions (opt-in)
|
|
567
|
+
|
|
568
|
+
Runner 0.9.0 and newer can show the Claude Code sessions of this computer in
|
|
569
|
+
BeatOS Bridge. From Bridge (or by voice with Beat-e) you can read them, start
|
|
570
|
+
a new session in one of your project folders, and chat with it. The runner
|
|
571
|
+
does the computer side. **It is off by default.**
|
|
572
|
+
|
|
573
|
+
### Two gates
|
|
574
|
+
|
|
575
|
+
Both must be on. If one is off, the runner sends nothing about sessions and
|
|
576
|
+
takes no command.
|
|
577
|
+
|
|
578
|
+
1. **The switch on beatos.app**: Bridge, Runners, click the runner,
|
|
579
|
+
Sessions. Only the runner's owner can turn it on. A paused or revoked
|
|
580
|
+
runner counts as off.
|
|
581
|
+
2. **`sessions.access` in `config.json`** on this computer: `read` or `work`.
|
|
582
|
+
The default is `off`. beatos.app can never turn this on or raise it: it
|
|
583
|
+
never sends an access level, and the runner always uses its own.
|
|
584
|
+
|
|
585
|
+
```json
|
|
586
|
+
{ "sessions": { "access": "read" } }
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
Restart the runner after you change it. At start it says one line:
|
|
590
|
+
`sessions: off`, `sessions: read only` or `sessions: can change files`.
|
|
591
|
+
|
|
592
|
+
### Read or work
|
|
593
|
+
|
|
594
|
+
What each access can reach:
|
|
595
|
+
|
|
596
|
+
- `read`: the chat has three tools only, Read, Grep and Glob. It cannot run
|
|
597
|
+
commands or change a file. It can read **only the files inside the
|
|
598
|
+
session's folder** (the scanned project folder it was started in), never a
|
|
599
|
+
key or `.env` file, and nothing outside that folder. Claude Code lets these
|
|
600
|
+
tools reach any path, so before each turn the runner adds a deny rule for
|
|
601
|
+
every folder and file next to the session's folder and above it, up to the
|
|
602
|
+
drive root, and for every other drive. If a folder has too many neighbors
|
|
603
|
+
to protect (more than 6000 rules), or a neighbor has a name that cannot be
|
|
604
|
+
protected (it differs from this folder's name only at characters like `+`
|
|
605
|
+
or `(`), the turn is refused. On Windows a chat
|
|
606
|
+
also cannot read a name that looks like a short name (with `~` and a digit,
|
|
607
|
+
like `DOCUME~1`): Windows opens a folder or a `.env` file by such a name
|
|
608
|
+
too, and a rule by the long name would not see it. A link that points at
|
|
609
|
+
the session's folder, into it, or at a folder above it gets no rule
|
|
610
|
+
(Windows has one on every PC: `C:\Documents and Settings` points to
|
|
611
|
+
`C:\Users`): Claude Code follows a link in a rule, and such a rule would
|
|
612
|
+
hide the session's own folder from Glob and Grep. What that link shows
|
|
613
|
+
stays closed by its real name. This wall is made of
|
|
614
|
+
Claude Code deny rules. It was checked with real sessions on Windows
|
|
615
|
+
(Claude Code 2.1.319): long and short names, links and junctions inside
|
|
616
|
+
the folder and above it, other drives, network paths, and that Glob and
|
|
617
|
+
Grep still find the files inside the folder. It is not an operating system
|
|
618
|
+
sandbox, and it was not checked on macOS or Linux. Ask it for a change
|
|
619
|
+
and it tells you what it would change.
|
|
620
|
+
- `work`: like a task run. The chat may change files and run commands **as
|
|
621
|
+
your Windows user** (or your user on macOS and Linux), inside the same deny
|
|
622
|
+
rules as a task run (no push, no deploy, no new packages, no secrets, no
|
|
623
|
+
browser, no MCP servers, no key or `.env` files in the folder, no other
|
|
624
|
+
sessions' transcripts). It is not a sandbox: a command can reach what your
|
|
625
|
+
user can reach. Use `protectPaths` for your private folders. Never
|
|
626
|
+
`--dangerously-skip-permissions`.
|
|
627
|
+
|
|
628
|
+
Each chat gets a short rules file (`RULES.md`, next to the task folder): keep
|
|
629
|
+
the last message short, never push, merge, deploy or pay, stay in the folder,
|
|
630
|
+
and treat text in files and web pages as information, not orders.
|
|
631
|
+
|
|
632
|
+
### What leaves the computer
|
|
633
|
+
|
|
634
|
+
For each session in one of your project folders (the folders the runner
|
|
635
|
+
offers on beatos.app, see [Connect folders from beatos.app](#connect-folders-from-beatosapp)):
|
|
636
|
+
|
|
637
|
+
- its title, the folder name (`~/projects/acme-app`) and git branch;
|
|
638
|
+
- live or not, and when it was last used;
|
|
639
|
+
- a short part of your last message (300 characters) and of the last answer
|
|
640
|
+
(1500 characters; a longer answer is sent as its start and its end);
|
|
641
|
+
- when you open a chat: the answers of the turns you start from Bridge, and
|
|
642
|
+
for an older session, its last 30 messages (your text and the answers);
|
|
643
|
+
- while a turn you started from Bridge runs: a short line of what it does at
|
|
644
|
+
that moment, at most one every 2 seconds. The line is the name of the tool
|
|
645
|
+
and the command, file path or search it uses (300 characters at most), for
|
|
646
|
+
example `Read ~/projects/acme-app/README.md`.
|
|
647
|
+
|
|
648
|
+
What never leaves: the output of tool calls, file contents, sessions in
|
|
649
|
+
other folders, `claude -p` sessions the runner did not start, and keys: a
|
|
650
|
+
text that looks like a key or a token becomes `[hidden]`, and the home folder
|
|
651
|
+
becomes `~`.
|
|
652
|
+
|
|
653
|
+
### A session you started by hand is continued as a copy
|
|
654
|
+
|
|
655
|
+
The runner never writes into a session you started yourself. When you chat
|
|
656
|
+
with one from Bridge, Bridge makes a copy (Claude Code's `--fork-session`):
|
|
657
|
+
the copy keeps the history and gets a new id, and your original session stays
|
|
658
|
+
as it was. Sessions started from Bridge are continued as they are.
|
|
659
|
+
|
|
660
|
+
### Limits
|
|
661
|
+
|
|
662
|
+
- One chat turn at a time on this computer (a task run may run at the same
|
|
663
|
+
time).
|
|
664
|
+
- `sessions.maxTurnMinutes` (default 30) per turn, then it is stopped.
|
|
665
|
+
- `sessions.maxTurnsPerDay` (default 200) turns per day.
|
|
666
|
+
- At most `sessions.maxSessions` (default 100) sessions are shown, changed in
|
|
667
|
+
the last `sessions.recentDays` (default 7) days.
|
|
668
|
+
- A turn cut by a runner restart shows as "interrupted" in Bridge.
|
|
669
|
+
|
|
670
|
+
### Audit log
|
|
671
|
+
|
|
672
|
+
Every command the runner takes or refuses adds one line to
|
|
673
|
+
`~/.beatos-runner/sessions-audit.jsonl`: the time, the command, the folder,
|
|
674
|
+
the access level, the result or why it was refused, and the first 120
|
|
675
|
+
characters of your text. It is kept at 5 MB (the older part moves to
|
|
676
|
+
`sessions-audit.1.jsonl`). It never leaves the computer.
|
|
677
|
+
|
|
678
|
+
### Turn it off
|
|
679
|
+
|
|
680
|
+
Turn the switch off on beatos.app (the runner stops the live turn and goes
|
|
681
|
+
quiet), or set `"access": "off"` in `config.json` and restart the runner.
|
|
682
|
+
|
|
560
683
|
## Files and logs
|
|
561
684
|
|
|
562
685
|
The runner uses two folders in your home folder.
|
|
@@ -569,7 +692,9 @@ agent can never read or change this folder.
|
|
|
569
692
|
| `runner.key` | The key. |
|
|
570
693
|
| `config.json` | Your settings. |
|
|
571
694
|
| `state.json` | Open runs and today's count. |
|
|
572
|
-
| `outbox/` | Events saved before they are sent, so nothing is lost offline. |
|
|
695
|
+
| `outbox/` | Events saved before they are sent, so nothing is lost offline. Sessions use `outbox/sessions-<runner id>.jsonl`. |
|
|
696
|
+
| `sessions-state.json` | Sessions: the sessions started from Bridge, the open turn, the last command ids. |
|
|
697
|
+
| `sessions-audit.jsonl` | Sessions: one line per command taken or refused. Local only. |
|
|
573
698
|
| `runner.lock` | Present while the runner runs. |
|
|
574
699
|
|
|
575
700
|
`~/beatos-runner-work` (next to the runner folder): where the agent works.
|
|
@@ -579,6 +704,7 @@ Change it with `taskRoot` and `worktreeRoot`.
|
|
|
579
704
|
| --- | --- |
|
|
580
705
|
| `tasks/<TASK>/` | Each task's brief (`TAREA.md`), rules and your answers. |
|
|
581
706
|
| `worktrees/` | Work copies of git projects. |
|
|
707
|
+
| `sessions/<session>/` | Sessions: the chat rules file (`RULES.md`) and the settings with the deny rules (`settings.json`) of each Bridge session. |
|
|
582
708
|
|
|
583
709
|
At start, the runner checks that the agent may read and write both work
|
|
584
710
|
folders. If a deny rule covers one of them (for example a `taskRoot` inside
|
|
@@ -672,6 +798,19 @@ pnpm typecheck
|
|
|
672
798
|
pnpm build # compiles to dist/
|
|
673
799
|
```
|
|
674
800
|
|
|
801
|
+
A live check of sessions with the real Claude Code (a few short haiku turns,
|
|
802
|
+
not run in CI):
|
|
803
|
+
|
|
804
|
+
```bash
|
|
805
|
+
node --experimental-strip-types --disable-warning=ExperimentalWarning scripts/sessions-live-check.mjs
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
Run it again after each Claude Code update. The read wall is made of Claude
|
|
809
|
+
Code deny rules, so it depends on how Claude Code matches paths and follows
|
|
810
|
+
links. The check tests both sides with the real program, each with a control:
|
|
811
|
+
a read session cannot reach the folder next door, and it can still find the
|
|
812
|
+
files in its own folder.
|
|
813
|
+
|
|
675
814
|
See PUBLISHING.md to release it to npm.
|
|
676
815
|
|
|
677
816
|
## License
|
package/dist/api.js
CHANGED
|
@@ -133,6 +133,38 @@ export function parseVideoComplete(data) {
|
|
|
133
133
|
const missing = isObj(data) && Array.isArray(data.missing) ? data.missing.filter((m) => typeof m === "string") : [];
|
|
134
134
|
return { verified: false, missing };
|
|
135
135
|
}
|
|
136
|
+
const DEFAULT_POLL_MS = 15_000;
|
|
137
|
+
/** A missing or bad `sessions` block means off (an older server). */
|
|
138
|
+
export function parseSessionsReply(raw) {
|
|
139
|
+
if (!isObj(raw))
|
|
140
|
+
return { enabled: false, poll_ms: DEFAULT_POLL_MS };
|
|
141
|
+
const ms = typeof raw.poll_ms === "number" && Number.isFinite(raw.poll_ms) && raw.poll_ms > 0 ? raw.poll_ms : DEFAULT_POLL_MS;
|
|
142
|
+
return { enabled: raw.enabled === true, poll_ms: ms };
|
|
143
|
+
}
|
|
144
|
+
const strOrNull = (v) => (typeof v === "string" ? v : null);
|
|
145
|
+
/** The commands of a poll reply: items without a string id and kind are dropped, at most 20. */
|
|
146
|
+
export function parseSessionCommands(raw) {
|
|
147
|
+
const out = [];
|
|
148
|
+
for (const c of Array.isArray(raw) ? raw : []) {
|
|
149
|
+
if (out.length >= 20)
|
|
150
|
+
break;
|
|
151
|
+
if (!isObj(c) || typeof c.id !== "string" || typeof c.kind !== "string")
|
|
152
|
+
continue;
|
|
153
|
+
out.push({
|
|
154
|
+
id: c.id,
|
|
155
|
+
kind: c.kind,
|
|
156
|
+
session_id: strOrNull(c.session_id),
|
|
157
|
+
claude_session_id: strOrNull(c.claude_session_id),
|
|
158
|
+
project_id: strOrNull(c.project_id),
|
|
159
|
+
folder: strOrNull(c.folder),
|
|
160
|
+
subpath: strOrNull(c.subpath),
|
|
161
|
+
fork_from: strOrNull(c.fork_from),
|
|
162
|
+
text: strOrNull(c.text),
|
|
163
|
+
created_at: strOrNull(c.created_at),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
136
168
|
export class BeatosApi {
|
|
137
169
|
base;
|
|
138
170
|
key;
|
|
@@ -246,13 +278,30 @@ export class BeatosApi {
|
|
|
246
278
|
}
|
|
247
279
|
async heartbeat(body) {
|
|
248
280
|
const r = await this.call("POST", "/api/v1/bridge/runner/heartbeat", body);
|
|
249
|
-
const { folders, project_folders, ...rest } = r;
|
|
281
|
+
const { folders, project_folders, sessions, ...rest } = r;
|
|
250
282
|
return {
|
|
251
283
|
...rest,
|
|
252
284
|
...(isObj(folders) ? { folders: { send: folders.send === true } } : {}),
|
|
253
285
|
project_folders: parseProjectFolders(project_folders),
|
|
286
|
+
sessions: parseSessionsReply(sessions),
|
|
254
287
|
};
|
|
255
288
|
}
|
|
289
|
+
// ----- Bridge sessions (BEA-107, protocol v1) -----
|
|
290
|
+
/** POST .../sessions/sync. A 409 sessions_off throws an ApiError with that code. */
|
|
291
|
+
async sessionsSync(body) {
|
|
292
|
+
const r = await this.call("POST", "/api/v1/bridge/runner/sessions/sync", body);
|
|
293
|
+
return parseSessionsReply(r);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* POST .../sessions/poll: send events, get commands. `open` (the commands
|
|
297
|
+
* the runner still holds) only when this request carries every unsent
|
|
298
|
+
* event. A 409 (gap, sessions_off) throws an ApiError.
|
|
299
|
+
*/
|
|
300
|
+
async sessionsPoll(body) {
|
|
301
|
+
const r = await this.call("POST", "/api/v1/bridge/runner/sessions/poll", body);
|
|
302
|
+
const ack = typeof r.ack_seq === "number" && Number.isFinite(r.ack_seq) ? r.ack_seq : 0;
|
|
303
|
+
return { ...parseSessionsReply(r), ack_seq: ack, commands: parseSessionCommands(r.commands) };
|
|
304
|
+
}
|
|
256
305
|
createRun(body) {
|
|
257
306
|
return this.call("POST", "/api/v1/bridge/runner/runs", body);
|
|
258
307
|
}
|
package/dist/claude.js
CHANGED
|
@@ -45,7 +45,12 @@ export async function runClaude(opts, cb = {}) {
|
|
|
45
45
|
env: opts.env,
|
|
46
46
|
windowsHide: true,
|
|
47
47
|
});
|
|
48
|
-
|
|
48
|
+
// A CLI that exits before it reads stdin must not crash the runner (EPIPE).
|
|
49
|
+
child.stdin?.on?.("error", () => undefined);
|
|
50
|
+
if (opts.stdinText !== undefined)
|
|
51
|
+
child.stdin?.end(opts.stdinText, "utf8");
|
|
52
|
+
else
|
|
53
|
+
child.stdin?.end();
|
|
49
54
|
const out = {
|
|
50
55
|
exitCode: null,
|
|
51
56
|
sessionId: null,
|
|
@@ -83,6 +88,7 @@ export async function runClaude(opts, cb = {}) {
|
|
|
83
88
|
cb.onLog?.(line.slice(0, 2000));
|
|
84
89
|
return;
|
|
85
90
|
}
|
|
91
|
+
cb.onEvent?.(ev);
|
|
86
92
|
if (typeof ev.session_id === "string" && ev.session_id && out.sessionId !== ev.session_id) {
|
|
87
93
|
out.sessionId = ev.session_id;
|
|
88
94
|
cb.onSession?.(ev.session_id);
|
package/dist/config.js
CHANGED
|
@@ -34,6 +34,17 @@ export function findClaude(env = process.env, platform = process.platform, home
|
|
|
34
34
|
return appData;
|
|
35
35
|
return "claude.exe";
|
|
36
36
|
}
|
|
37
|
+
/** The `sessions` block of config.json; anything missing or of the wrong type gets its default. */
|
|
38
|
+
export function parseSessionsConfig(raw) {
|
|
39
|
+
const v = typeof raw === "object" && raw !== null && !Array.isArray(raw) ? raw : {};
|
|
40
|
+
return {
|
|
41
|
+
access: v.access === "read" || v.access === "work" ? v.access : "off",
|
|
42
|
+
maxTurnMinutes: clampInt(v.maxTurnMinutes, 1, 240, 30),
|
|
43
|
+
maxTurnsPerDay: clampInt(v.maxTurnsPerDay, 1, 5000, 200),
|
|
44
|
+
recentDays: clampInt(v.recentDays, 1, 30, 7),
|
|
45
|
+
maxSessions: clampInt(v.maxSessions, 10, 200, 100),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
37
48
|
const clampInt = (v, min, max, dflt) => typeof v === "number" && Number.isFinite(v) ? Math.min(max, Math.max(min, Math.round(v))) : dflt;
|
|
38
49
|
/** The `video` block of config.json; anything missing or of the wrong type gets its default. */
|
|
39
50
|
export function parseVideoConfig(raw) {
|
|
@@ -95,6 +106,7 @@ export function loadConfig(env = process.env) {
|
|
|
95
106
|
.filter((p) => isAbsolute(p)),
|
|
96
107
|
projects: raw.projects ?? {},
|
|
97
108
|
video: parseVideoConfig(raw.video),
|
|
109
|
+
sessions: parseSessionsConfig(raw.sessions),
|
|
98
110
|
};
|
|
99
111
|
}
|
|
100
112
|
/** The runner's agent key. Never logged. Empty string when missing. */
|
package/dist/folders.js
CHANGED
|
@@ -45,8 +45,8 @@ export function displayPath(abs, home = homedir(), platform = process.platform)
|
|
|
45
45
|
return `~/${a.slice(h.length + 1)}`;
|
|
46
46
|
return a;
|
|
47
47
|
}
|
|
48
|
-
/** True when a is b or inside it. */
|
|
49
|
-
function within(a, b, platform) {
|
|
48
|
+
/** True when a is b or inside it (caseless on Windows). */
|
|
49
|
+
export function within(a, b, platform) {
|
|
50
50
|
const x = caseless(platform) ? posix(a).toLowerCase() : posix(a);
|
|
51
51
|
const y = caseless(platform) ? posix(b).toLowerCase() : posix(b);
|
|
52
52
|
return x === y || x.startsWith(`${y}/`);
|
|
@@ -171,6 +171,10 @@ export class ProjectFolders {
|
|
|
171
171
|
this.paths = s.paths;
|
|
172
172
|
return s.report;
|
|
173
173
|
}
|
|
174
|
+
/** The last scan: shown path -> the runner's own full path (a copy). Sessions (BEA-107) use it. */
|
|
175
|
+
shownPaths() {
|
|
176
|
+
return new Map(this.paths);
|
|
177
|
+
}
|
|
174
178
|
/** undefined = an older server: config.json only. */
|
|
175
179
|
setWeb(list) {
|
|
176
180
|
this.web = new Map((list ?? []).map((m) => [m.project_id, m]));
|
package/dist/main.js
CHANGED
|
@@ -19,13 +19,14 @@
|
|
|
19
19
|
import { ProjectFolders } from "./folders.js";
|
|
20
20
|
import { scanInventory } from "./inventory.js";
|
|
21
21
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
22
|
-
import { join } from "node:path";
|
|
22
|
+
import { dirname, join } from "node:path";
|
|
23
23
|
import { BeatosApi } from "./api.js";
|
|
24
24
|
import { runClaude } from "./claude.js";
|
|
25
|
-
import { loadConfig, loadKey } from "./config.js";
|
|
25
|
+
import { loadConfig, loadKey, parseSessionsConfig } from "./config.js";
|
|
26
26
|
import { pair } from "./pair.js";
|
|
27
27
|
import { agentPathProblems } from "./safety.js";
|
|
28
28
|
import { makeClaudeExec, Runner, RUNNER_VERSION } from "./runner.js";
|
|
29
|
+
import { makeChatExec, SessionsEngine } from "./sessions-engine.js";
|
|
29
30
|
import { videoReady } from "./video.js";
|
|
30
31
|
import { Workspace } from "./workspace.js";
|
|
31
32
|
const USAGE = `BeatOS Bridge runner ${RUNNER_VERSION}
|
|
@@ -94,9 +95,14 @@ async function main() {
|
|
|
94
95
|
return 2;
|
|
95
96
|
}
|
|
96
97
|
// The agent must read and write these; a deny rule over them blocks every task.
|
|
98
|
+
const sessionsCfg = parseSessionsConfig(cfg.sessions);
|
|
97
99
|
const problems = agentPathProblems([
|
|
98
100
|
{ label: "taskRoot", path: cfg.taskRoot },
|
|
99
101
|
{ label: "worktreeRoot", path: cfg.worktreeRoot },
|
|
102
|
+
// Sessions (BEA-107): each chat's rules file lives next to the task folder.
|
|
103
|
+
...(sessionsCfg.access !== "off"
|
|
104
|
+
? [{ label: "sessions folder", path: join(dirname(cfg.taskRoot), "sessions") }]
|
|
105
|
+
: []),
|
|
100
106
|
], { protectPaths: cfg.protectPaths, stateDir: cfg.stateDir });
|
|
101
107
|
if (problems.length > 0) {
|
|
102
108
|
for (const p of problems)
|
|
@@ -114,15 +120,26 @@ async function main() {
|
|
|
114
120
|
if (video)
|
|
115
121
|
console.log("[bridge-runner] video is on: videos become frames and a local transcript");
|
|
116
122
|
const api = new BeatosApi({ baseUrl: cfg.beatosUrl, key, actor: cfg.agentName });
|
|
123
|
+
const folders = new ProjectFolders({ cfg, log: (m) => console.log(`[bridge-runner] ${m}`) });
|
|
124
|
+
// Sessions in Bridge (BEA-107): off unless config.json says read or work.
|
|
125
|
+
const sessions = new SessionsEngine({
|
|
126
|
+
cfg,
|
|
127
|
+
api,
|
|
128
|
+
claude: makeChatExec(cfg, runClaude),
|
|
129
|
+
projectFor: (id) => folders.projectFor(id),
|
|
130
|
+
});
|
|
131
|
+
console.log(`[bridge-runner] sessions: ${sessionsCfg.access === "off" ? "off" : sessionsCfg.access === "read" ? "read only" : "can change files"}`);
|
|
117
132
|
const runner = new Runner({
|
|
118
133
|
cfg,
|
|
119
134
|
api,
|
|
120
135
|
workspace: new Workspace(cfg),
|
|
121
136
|
claude: makeClaudeExec(cfg, runClaude),
|
|
122
137
|
inventory: () => scanInventory(),
|
|
123
|
-
folders
|
|
138
|
+
folders,
|
|
124
139
|
...(video ? { video: {} } : {}),
|
|
140
|
+
sessions,
|
|
125
141
|
});
|
|
142
|
+
sessions.start();
|
|
126
143
|
console.log(`[bridge-runner] ${cfg.runnerName} -> ${cfg.beatosUrl} as ${cfg.agentName}, every ${cfg.pollSeconds}s`);
|
|
127
144
|
let stop = false;
|
|
128
145
|
process.on("SIGINT", () => {
|
|
@@ -151,8 +168,13 @@ async function main() {
|
|
|
151
168
|
}
|
|
152
169
|
finally {
|
|
153
170
|
// Give a running session a short while to settle so its phase is saved.
|
|
154
|
-
|
|
171
|
+
// A live chat turn is killed and reported as interrupted.
|
|
172
|
+
await Promise.race([
|
|
173
|
+
Promise.all([runner.idle(), sessions.shutdown()]),
|
|
174
|
+
new Promise((r) => setTimeout(r, IDLE_WAIT_MS).unref()),
|
|
175
|
+
]);
|
|
155
176
|
runner.save();
|
|
177
|
+
sessions.save();
|
|
156
178
|
rmSync(lock, { force: true });
|
|
157
179
|
}
|
|
158
180
|
return 0;
|
package/dist/outbox.js
CHANGED
|
@@ -19,10 +19,13 @@ export class Outbox {
|
|
|
19
19
|
pending = [];
|
|
20
20
|
lastSeq = 0;
|
|
21
21
|
ack = 0;
|
|
22
|
+
/** True when there was no ack file at start (a new lane, or a lost or reset one): the server's cursor is not known. */
|
|
23
|
+
fresh;
|
|
22
24
|
constructor(dir, runId) {
|
|
23
25
|
mkdirSync(dir, { recursive: true });
|
|
24
26
|
this.file = join(dir, `${runId}.jsonl`);
|
|
25
27
|
this.ackFile = join(dir, `${runId}.ack`);
|
|
28
|
+
this.fresh = !existsSync(this.ackFile);
|
|
26
29
|
this.load();
|
|
27
30
|
}
|
|
28
31
|
load() {
|
|
@@ -51,6 +54,10 @@ export class Outbox {
|
|
|
51
54
|
get ackSeq() {
|
|
52
55
|
return this.ack;
|
|
53
56
|
}
|
|
57
|
+
/** The highest seq this lane ever numbered (or the ack, if higher). */
|
|
58
|
+
get lastSeqNo() {
|
|
59
|
+
return this.lastSeq;
|
|
60
|
+
}
|
|
54
61
|
get size() {
|
|
55
62
|
return this.pending.length;
|
|
56
63
|
}
|
|
@@ -69,6 +76,27 @@ export class Outbox {
|
|
|
69
76
|
this.pending.push(ev);
|
|
70
77
|
return ev.seq;
|
|
71
78
|
}
|
|
79
|
+
/** The unsent events, oldest first (a copy). */
|
|
80
|
+
peek(max = MAX_BATCH) {
|
|
81
|
+
return this.pending.slice(0, max);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The server's cursor is behind what this file thinks it acked (a gap it
|
|
85
|
+
* cannot fill: those events are gone). Number the unsent events again from
|
|
86
|
+
* serverAck + 1 so the lane goes on. Used by the sessions lane only.
|
|
87
|
+
*/
|
|
88
|
+
rebase(serverAck) {
|
|
89
|
+
const base = Math.max(0, Math.floor(serverAck));
|
|
90
|
+
this.pending = this.pending.map((e, i) => ({ ...e, seq: base + 1 + i }));
|
|
91
|
+
this.lastSeq = base + this.pending.length;
|
|
92
|
+
this.ack = base;
|
|
93
|
+
const tmp = this.ackFile + ".tmp";
|
|
94
|
+
writeFileSync(tmp, String(base), "utf8");
|
|
95
|
+
renameSync(tmp, this.ackFile);
|
|
96
|
+
const tmpLog = this.file + ".tmp";
|
|
97
|
+
writeFileSync(tmpLog, this.pending.map((e) => JSON.stringify(e) + "\n").join(""), "utf8");
|
|
98
|
+
renameSync(tmpLog, this.file);
|
|
99
|
+
}
|
|
72
100
|
/** The server's cursor is the truth after a reconnect: adopt it. */
|
|
73
101
|
adoptServerAck(serverAck) {
|
|
74
102
|
if (serverAck > this.ack)
|
|
@@ -76,6 +104,9 @@ export class Outbox {
|
|
|
76
104
|
}
|
|
77
105
|
markAcked(seq) {
|
|
78
106
|
this.ack = seq;
|
|
107
|
+
// A lost or reset file: never number a new event at or below the server's cursor.
|
|
108
|
+
if (seq > this.lastSeq)
|
|
109
|
+
this.lastSeq = seq;
|
|
79
110
|
this.pending = this.pending.filter((e) => e.seq > seq);
|
|
80
111
|
const tmp = this.ackFile + ".tmp";
|
|
81
112
|
writeFileSync(tmp, String(seq), "utf8");
|