@bakers-build/addon-rex 0.0.0-stage → 0.13.1-next.4
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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/src/index.d.ts +61 -0
- package/dist/src/index.js +192 -0
- package/dist/src/preset.d.ts +4 -0
- package/dist/src/preset.js +7 -0
- package/options.schema.json +39 -0
- package/package.json +34 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 keytec GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bakers-build/addon-rex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
bakers.build Rex integration: executor running each agent in a Rex terminal session.
|
|
4
|
+
|
|
5
|
+
Part of the bakers.build CLI. Install the meta package `@bakers-build/bakers` to get the `bakers` CLI with all addons. Source: https://github.com/bakers-build/bakers-build (bakers-cli/).
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type LaunchWatchOptions } from '@bakers-build/addon-herdr/shared';
|
|
2
|
+
import type { BbExecutor, ExecutorCapabilities, ExecutorPlugin, SpawnedSession, SpawnRunInput } from '@bakers-build/conductor/contract';
|
|
3
|
+
import { type ConductorLogger } from '@bakers-build/core';
|
|
4
|
+
/** Runs `rex <args>`; resolves with stdout, rejects on a non-zero exit. */
|
|
5
|
+
export type RexExec = (args: string[]) => Promise<string>;
|
|
6
|
+
export interface RexExecutorOptions {
|
|
7
|
+
/** Fallback agent command when the conductor supplies none. Default `claude`. */
|
|
8
|
+
command?: string;
|
|
9
|
+
/** As for herdr: `start_timeout_ms` (default 60 s), then `timeout_ms` (4 s). */
|
|
10
|
+
launch_watch?: Omit<LaunchWatchOptions, 'readPane'>;
|
|
11
|
+
}
|
|
12
|
+
/** The oldest `rex` whose `new --cwd --shell none --json` this addon uses. */
|
|
13
|
+
export declare const MIN_REX_VERSION = "0.1.0";
|
|
14
|
+
/** `rex` is not on PATH. */
|
|
15
|
+
export declare class RexNotFound extends Error {
|
|
16
|
+
constructor();
|
|
17
|
+
}
|
|
18
|
+
/** `rex` exit 4: no server answers. */
|
|
19
|
+
export declare class RexServerUnreachable extends Error {
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The real CLI, resolved through PATH. `--autostart=false`: a server the
|
|
24
|
+
* conductor spawned would take the run's sessions down with it.
|
|
25
|
+
*/
|
|
26
|
+
export declare const defaultRex: RexExec;
|
|
27
|
+
/** Rejects unless `rex version` reports at least {@link MIN_REX_VERSION}. */
|
|
28
|
+
export declare function assertRexVersion(rex: RexExec): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* The run's session label, `<identifier>-<runId> <branch>`. Rex labels are
|
|
31
|
+
* free text and may repeat, so the executor only ever acts on session ids.
|
|
32
|
+
*/
|
|
33
|
+
export declare function sessionLabel(identifier: string, runId: number, branch: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Executor hosting each run's agent in its own Rex session. The session lives
|
|
36
|
+
* in the Rex server, so it survives any client detaching. Rex has no
|
|
37
|
+
* workspace notion: the worktree comes from the workspace slot.
|
|
38
|
+
*/
|
|
39
|
+
export declare class RexExecutor implements BbExecutor {
|
|
40
|
+
private readonly options;
|
|
41
|
+
private readonly rex;
|
|
42
|
+
private readonly logger;
|
|
43
|
+
readonly id = "rex";
|
|
44
|
+
constructor(options: RexExecutorOptions, rex?: RexExec, logger?: ConductorLogger);
|
|
45
|
+
capabilities(): ExecutorCapabilities;
|
|
46
|
+
startRun(input: SpawnRunInput): Promise<SpawnedSession>;
|
|
47
|
+
/** No-op: `cleanupRun` ends the program; the transcript is already on disk. */
|
|
48
|
+
stopRun(_branch: string): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Kill the sessions of ONE run, found by the label suffix `-<runId> <branch>`;
|
|
51
|
+
* a sibling run's session is left alone. Best-effort: a failure is logged,
|
|
52
|
+
* never thrown, and a missing match is warned about, since it may leave an
|
|
53
|
+
* agent running.
|
|
54
|
+
*/
|
|
55
|
+
cleanupRun(branch: string, runId: number): Promise<void>;
|
|
56
|
+
/** `rex session kill`; exit 3 means the session is already gone. */
|
|
57
|
+
private kill;
|
|
58
|
+
private commandFor;
|
|
59
|
+
}
|
|
60
|
+
export declare function rexExecutor(options?: RexExecutorOptions): ExecutorPlugin;
|
|
61
|
+
export default rexExecutor;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { rmSync } from 'node:fs';
|
|
2
|
+
import { watchLaunch, writeLaunchScript, } from '@bakers-build/addon-herdr/shared';
|
|
3
|
+
import { ExecError, exec, shellQuote, } from '@bakers-build/core';
|
|
4
|
+
/** The oldest `rex` whose `new --cwd --shell none --json` this addon uses. */
|
|
5
|
+
export const MIN_REX_VERSION = '0.1.0';
|
|
6
|
+
/** `rex` is not on PATH. */
|
|
7
|
+
export class RexNotFound extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(`rex not found on PATH: install Rex ${MIN_REX_VERSION} or later (https://www.superlogical.com/rex/)`);
|
|
10
|
+
this.name = 'RexNotFound';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** `rex` exit 4: no server answers. */
|
|
14
|
+
export class RexServerUnreachable extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super('Rex server unreachable: start it with `rex server install`');
|
|
17
|
+
this.name = 'RexServerUnreachable';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The real CLI, resolved through PATH. `--autostart=false`: a server the
|
|
22
|
+
* conductor spawned would take the run's sessions down with it.
|
|
23
|
+
*/
|
|
24
|
+
export const defaultRex = async (args) => {
|
|
25
|
+
try {
|
|
26
|
+
return await exec('rex', ['--autostart=false', ...args]);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
const cause = (err instanceof ExecError ? err.cause : err);
|
|
30
|
+
if (cause?.code === 'ENOENT')
|
|
31
|
+
throw new RexNotFound();
|
|
32
|
+
if (err instanceof ExecError && err.exitCode === 4) {
|
|
33
|
+
throw new RexServerUnreachable();
|
|
34
|
+
}
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const noopLogger = {
|
|
39
|
+
debug() { },
|
|
40
|
+
info() { },
|
|
41
|
+
warn() { },
|
|
42
|
+
error() { },
|
|
43
|
+
};
|
|
44
|
+
/** Rejects unless `rex version` reports at least {@link MIN_REX_VERSION}. */
|
|
45
|
+
export async function assertRexVersion(rex) {
|
|
46
|
+
const out = await rex(['version']);
|
|
47
|
+
// A snapshot (`0.1.0-snapshot.2531`) counts as its release.
|
|
48
|
+
const found = out.match(/(\d+)\.(\d+)\.(\d+)/)?.[0];
|
|
49
|
+
if (!found) {
|
|
50
|
+
throw new Error(`rex version printed no version: ${out.trim()}`);
|
|
51
|
+
}
|
|
52
|
+
const have = found.split('.').map(Number);
|
|
53
|
+
const need = MIN_REX_VERSION.split('.').map(Number);
|
|
54
|
+
for (let i = 0; i < need.length; i++) {
|
|
55
|
+
if ((have[i] ?? 0) > (need[i] ?? 0))
|
|
56
|
+
return;
|
|
57
|
+
if ((have[i] ?? 0) < (need[i] ?? 0)) {
|
|
58
|
+
throw new Error(`rex ${found} is too old: upgrade Rex to ${MIN_REX_VERSION} or later`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The run's session label, `<identifier>-<runId> <branch>`. Rex labels are
|
|
64
|
+
* free text and may repeat, so the executor only ever acts on session ids.
|
|
65
|
+
*/
|
|
66
|
+
export function sessionLabel(identifier, runId, branch) {
|
|
67
|
+
return `${identifier}-${runId} ${branch}`;
|
|
68
|
+
}
|
|
69
|
+
/** `rex ls --json` prints `{sessions: [{session_id, label}]}`. */
|
|
70
|
+
function parseSessions(output) {
|
|
71
|
+
const list = JSON.parse(output)?.sessions;
|
|
72
|
+
if (!Array.isArray(list))
|
|
73
|
+
return [];
|
|
74
|
+
return list.flatMap((s) => {
|
|
75
|
+
const r = s;
|
|
76
|
+
return typeof r?.session_id === 'string' && typeof r.label === 'string'
|
|
77
|
+
? [{ id: r.session_id, label: r.label }]
|
|
78
|
+
: [];
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Executor hosting each run's agent in its own Rex session. The session lives
|
|
83
|
+
* in the Rex server, so it survives any client detaching. Rex has no
|
|
84
|
+
* workspace notion: the worktree comes from the workspace slot.
|
|
85
|
+
*/
|
|
86
|
+
export class RexExecutor {
|
|
87
|
+
options;
|
|
88
|
+
rex;
|
|
89
|
+
logger;
|
|
90
|
+
id = 'rex';
|
|
91
|
+
constructor(options, rex = defaultRex, logger = noopLogger) {
|
|
92
|
+
this.options = options;
|
|
93
|
+
this.rex = rex;
|
|
94
|
+
this.logger = logger;
|
|
95
|
+
}
|
|
96
|
+
capabilities() {
|
|
97
|
+
return { persistent: true };
|
|
98
|
+
}
|
|
99
|
+
async startRun(input) {
|
|
100
|
+
await assertRexVersion(this.rex);
|
|
101
|
+
let launch;
|
|
102
|
+
let session;
|
|
103
|
+
try {
|
|
104
|
+
// Rex has no env flag; the run's env travels inside the 0600 launch script.
|
|
105
|
+
launch = writeLaunchScript(this.commandFor(input));
|
|
106
|
+
const created = JSON.parse(await this.rex([
|
|
107
|
+
'new',
|
|
108
|
+
sessionLabel(input.ticket.identifier, input.run.id, input.ticket.branchName),
|
|
109
|
+
'--cwd',
|
|
110
|
+
input.workspacePath,
|
|
111
|
+
'--shell',
|
|
112
|
+
'none',
|
|
113
|
+
'--json',
|
|
114
|
+
'--',
|
|
115
|
+
'sh',
|
|
116
|
+
launch.wrapPath,
|
|
117
|
+
]));
|
|
118
|
+
if (typeof created.session_id !== 'string') {
|
|
119
|
+
throw new Error('rex new printed no session_id');
|
|
120
|
+
}
|
|
121
|
+
session = created.session_id;
|
|
122
|
+
const id = session;
|
|
123
|
+
await watchLaunch(launch, {
|
|
124
|
+
...(this.options.launch_watch ?? {}),
|
|
125
|
+
readPane: () => this.rex(['capture', '-s', id, '--trim']),
|
|
126
|
+
});
|
|
127
|
+
return { sessionRef: id };
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
if (launch)
|
|
131
|
+
rmSync(launch.dir, { recursive: true, force: true });
|
|
132
|
+
if (session)
|
|
133
|
+
await this.kill(session).catch(() => { });
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** No-op: `cleanupRun` ends the program; the transcript is already on disk. */
|
|
138
|
+
async stopRun(_branch) { }
|
|
139
|
+
/**
|
|
140
|
+
* Kill the sessions of ONE run, found by the label suffix `-<runId> <branch>`;
|
|
141
|
+
* a sibling run's session is left alone. Best-effort: a failure is logged,
|
|
142
|
+
* never thrown, and a missing match is warned about, since it may leave an
|
|
143
|
+
* agent running.
|
|
144
|
+
*/
|
|
145
|
+
async cleanupRun(branch, runId) {
|
|
146
|
+
try {
|
|
147
|
+
const suffix = `-${runId} ${branch}`;
|
|
148
|
+
const sessions = parseSessions(await this.rex(['ls', '--json'])).filter((s) => s.label.endsWith(suffix));
|
|
149
|
+
if (sessions.length === 0) {
|
|
150
|
+
this.logger.warn({ branch, runId }, 'no Rex session found for this run; its agent may still be running');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const s of sessions)
|
|
154
|
+
await this.kill(s.id);
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
this.logger.warn({ branch, runId, err: String(err) }, 'Rex cleanup failed (best-effort)');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** `rex session kill`; exit 3 means the session is already gone. */
|
|
161
|
+
async kill(session) {
|
|
162
|
+
try {
|
|
163
|
+
await this.rex(['session', 'kill', session]);
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
if (!(err instanceof ExecError && err.exitCode === 3))
|
|
167
|
+
throw err;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
commandFor(input) {
|
|
171
|
+
const env = Object.entries(input.env ?? {}).map(([key, value]) => {
|
|
172
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
|
|
173
|
+
throw new Error(`invalid environment variable name for rex: ${key}`);
|
|
174
|
+
}
|
|
175
|
+
return `${key}=${shellQuote(value)}`;
|
|
176
|
+
});
|
|
177
|
+
const command = input.command ?? this.options.command ?? 'claude';
|
|
178
|
+
return [...env, command].join(' ');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
export function rexExecutor(options = {}) {
|
|
182
|
+
return {
|
|
183
|
+
kind: 'executor',
|
|
184
|
+
id: 'rex',
|
|
185
|
+
requiredModules: [],
|
|
186
|
+
async createExecutor(_config, deps) {
|
|
187
|
+
return new RexExecutor({ ...options, command: options.command ?? 'claude' }, defaultRex, deps.logger);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// The default export lets the config resolver pick the factory without an `export:` key.
|
|
192
|
+
export default rexExecutor;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://bakers.build/schema/addon-rex/options.schema.json",
|
|
4
|
+
"title": "Rex executor options",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"command": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"description": "Fallback agent command when the agent addons supply none.",
|
|
10
|
+
"default": "claude"
|
|
11
|
+
},
|
|
12
|
+
"launch_watch": {
|
|
13
|
+
"type": "object",
|
|
14
|
+
"description": "Launch-watch windows: how long a start may take and how soon an agent exit counts as a failed launch.",
|
|
15
|
+
"properties": {
|
|
16
|
+
"timeout_ms": {
|
|
17
|
+
"type": "integer",
|
|
18
|
+
"minimum": 0,
|
|
19
|
+
"description": "Window after start in which an immediate agent exit fails the launch; 0 skips it.",
|
|
20
|
+
"default": 4000
|
|
21
|
+
},
|
|
22
|
+
"start_timeout_ms": {
|
|
23
|
+
"type": "integer",
|
|
24
|
+
"minimum": 0,
|
|
25
|
+
"description": "How long the launch waits for the wrapper to report the start.",
|
|
26
|
+
"default": 60000
|
|
27
|
+
},
|
|
28
|
+
"poll_ms": {
|
|
29
|
+
"type": "integer",
|
|
30
|
+
"minimum": 0,
|
|
31
|
+
"description": "Polling interval of both windows.",
|
|
32
|
+
"default": 50
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"additionalProperties": false
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"additionalProperties": false
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bakers-build/addon-rex",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.13.1-next.4",
|
|
4
|
+
"description": "bakers.build Rex integration: executor running each agent in a Rex terminal session.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/src/index.js",
|
|
9
|
+
"./preset": "./dist/src/preset.js",
|
|
10
|
+
"./options.schema.json": "./options.schema.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/src",
|
|
14
|
+
"options.schema.json"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/bakers-build/bakers-build.git",
|
|
22
|
+
"directory": "bakers-cli/addons/rex"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@bakers-build/addon-herdr": "^0.13.1-next.4",
|
|
26
|
+
"@bakers-build/addon-workspace-git": "^0.13.1-next.4"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@bakers-build/conductor": "^0.13.1-next.4",
|
|
30
|
+
"@bakers-build/core": "^0.13.1-next.4"
|
|
31
|
+
},
|
|
32
|
+
"bakersBuild": {
|
|
33
|
+
"commit": "9f7275a341fe96e9792bce6cd1bbc7b7afc1dcd0",
|
|
34
|
+
"channel": "next"
|
|
35
|
+
}
|
|
36
|
+
}
|