@able-autonomy/able 0.0.13 → 0.0.14
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/able.ts +1 -1
- package/config.ts +29 -0
- package/package.json +1 -1
- package/src/workspaces/agent.ts +119 -0
- package/src/workspaces/index.ts +4 -0
package/able.ts
CHANGED
|
@@ -143,7 +143,7 @@ if (delegate) {
|
|
|
143
143
|
}
|
|
144
144
|
if (command !== 'gh') {
|
|
145
145
|
console.error(
|
|
146
|
-
"usage: able gh <gh arguments> run gh as Able's GitHub App\n able sync [--dry-run] file follow-ups and write ended weeks' records\n able config [--json] print the host's able.config.ts\n able commit \"<message>\" [<path>…] a pathspec commit after the touched folders' checks\n able push pushes this branch with its tags\n able upgrade makes bunx run the latest published able\n able ws <new|ls|current|refresh|merge|rm|start|stop|status|take> workspaces",
|
|
146
|
+
"usage: able gh <gh arguments> run gh as Able's GitHub App\n able sync [--dry-run] file follow-ups and write ended weeks' records\n able config [--json] print the host's able.config.ts\n able commit \"<message>\" [<path>…] a pathspec commit after the touched folders' checks\n able push pushes this branch with its tags\n able upgrade makes bunx run the latest published able\n able ws <new|ls|current|refresh|merge|rm|start|stop|status|take|build|attach> workspaces",
|
|
147
147
|
)
|
|
148
148
|
process.exit(command ? 1 : 0)
|
|
149
149
|
}
|
package/config.ts
CHANGED
|
@@ -54,6 +54,33 @@ export interface AbleConfig {
|
|
|
54
54
|
/** The `git commit` flags for the host's trailers. */
|
|
55
55
|
export const trailerArgs = (config: AbleConfig) => (config.commit?.trailers ?? []).flatMap((t) => ['--trailer', t])
|
|
56
56
|
|
|
57
|
+
// The keys Able reads, mirroring the interfaces above; `*` is a name the host picks. A host without the package has no
|
|
58
|
+
// types, so a mistyped key fails here rather than being ignored.
|
|
59
|
+
type Shape = { [key: string]: Shape | true }
|
|
60
|
+
const shape: Shape = {
|
|
61
|
+
checks: { commit: true, merge: true },
|
|
62
|
+
commit: { trailers: true },
|
|
63
|
+
workspaces: {
|
|
64
|
+
dir: true,
|
|
65
|
+
ports: { step: true, max: true },
|
|
66
|
+
copy: true,
|
|
67
|
+
onCreate: true,
|
|
68
|
+
onRemove: true,
|
|
69
|
+
services: { '*': { start: true, stop: true, ready: true, needs: true, cwd: true } },
|
|
70
|
+
resources: { '*': { take: true, auto: true, holder: true } },
|
|
71
|
+
},
|
|
72
|
+
trunk: true,
|
|
73
|
+
journal: true,
|
|
74
|
+
knowledge: true,
|
|
75
|
+
}
|
|
76
|
+
const unknownKeys = (value: unknown, s: Shape, at = ''): string[] =>
|
|
77
|
+
value && typeof value === 'object' && !Array.isArray(value)
|
|
78
|
+
? Object.entries(value).flatMap(([key, v]) => {
|
|
79
|
+
const sub = s[key] ?? s['*']
|
|
80
|
+
return sub === undefined ? [`${at}${key}`] : sub === true ? [] : unknownKeys(v, sub, `${at}${key}.`)
|
|
81
|
+
})
|
|
82
|
+
: []
|
|
83
|
+
|
|
57
84
|
/** Types and editor help for `able.config.ts`; returns the config unchanged. */
|
|
58
85
|
export const defineConfig = (config: AbleConfig): AbleConfig => config
|
|
59
86
|
|
|
@@ -68,6 +95,8 @@ export async function loadConfig(root: string): Promise<AbleConfig> {
|
|
|
68
95
|
const path = join(root, 'able.config.ts')
|
|
69
96
|
if (!existsSync(path)) fail(`no able.config.ts at ${root}. Run Able's onboarding to set this project up.`)
|
|
70
97
|
const config = (await import(path)).default as Partial<AbleConfig> | undefined
|
|
98
|
+
const unknown = unknownKeys(config, shape)
|
|
99
|
+
if (unknown.length) fail(`${path}: unknown key(s) ${unknown.join(', ')}`)
|
|
71
100
|
if (config?.workspaces && typeof config.workspaces.dir !== 'string') fail(`${path}: workspaces needs a dir`)
|
|
72
101
|
const missing = (['trunk', 'journal'] as const).filter((key) => typeof config?.[key] !== 'string' || !config[key])
|
|
73
102
|
if (missing.length) fail(`${path} is missing ${missing.join(', ')}`)
|
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// able ws build <slug> ["message"]: runs a headless builder (`claude -p`) in a workspace, in the background. With no
|
|
2
|
+
// message it starts a session on "build the brief <journal>/workspaces/<slug>.md"; with one it resumes the saved session.
|
|
3
|
+
// able ws attach [<slug>]: follows that session live while the builder runs, then opens it as a normal `claude` session.
|
|
4
|
+
//
|
|
5
|
+
// The session id and pid live in the workspace's `.able/builder.json`. The conversation itself is Claude's own
|
|
6
|
+
// transcript, `~/.claude/projects/<path>/<id>.jsonl`, so there is no second log. A builder started some other way is
|
|
7
|
+
// found as the workspace's newest headless session.
|
|
8
|
+
|
|
9
|
+
import { spawn } from 'node:child_process'
|
|
10
|
+
import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
11
|
+
import { homedir } from 'node:os'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import { loadConfig } from '../../config.ts'
|
|
14
|
+
import { bold, dim, fail, green } from '../ui.ts'
|
|
15
|
+
import { current, workspaces, type Workspace } from './state.ts'
|
|
16
|
+
|
|
17
|
+
type Builder = { sessionId: string; pid: number; startedAt: string }
|
|
18
|
+
|
|
19
|
+
const savedPath = (ws: Workspace) => join(ws.path, '.able', 'builder.json')
|
|
20
|
+
const alive = (pid: number) => Bun.spawnSync(['kill', '-0', String(pid)], { stdout: 'ignore', stderr: 'ignore' }).exitCode === 0
|
|
21
|
+
// A `claude` started from inside another Claude session counts itself that session's child; the builder is its own.
|
|
22
|
+
const cleanEnv = () => Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('CLAUDE')))
|
|
23
|
+
const transcriptDir = (ws: Workspace) => join(homedir(), '.claude', 'projects', realpathSync(ws.path).replace(/[^a-zA-Z0-9]/g, '-'))
|
|
24
|
+
const transcript = (ws: Workspace, id: string) => join(transcriptDir(ws), `${id}.jsonl`)
|
|
25
|
+
|
|
26
|
+
async function target(slug?: string) {
|
|
27
|
+
const at = await current()
|
|
28
|
+
const name = slug ?? (at.isWorkspace ? at.slug : undefined)
|
|
29
|
+
if (!name) fail('usage: able ws build <slug> ["message"] | able ws attach [<slug>]')
|
|
30
|
+
const ws = (await workspaces(at.main)).find((w) => w.slug === name)
|
|
31
|
+
if (!ws) fail(`no workspace '${name}'`)
|
|
32
|
+
return ws
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const readSaved = (ws: Workspace): Builder | undefined => (existsSync(savedPath(ws)) ? JSON.parse(readFileSync(savedPath(ws), 'utf8')) : undefined)
|
|
36
|
+
|
|
37
|
+
/** The newest headless session in the workspace, for a builder Able didn't start. */
|
|
38
|
+
function newestHeadless(ws: Workspace) {
|
|
39
|
+
const dir = transcriptDir(ws)
|
|
40
|
+
if (!existsSync(dir)) return
|
|
41
|
+
return readdirSync(dir)
|
|
42
|
+
.filter((f) => f.endsWith('.jsonl'))
|
|
43
|
+
.map((f) => ({ id: f.slice(0, -'.jsonl'.length), at: statSync(join(dir, f)).mtimeMs, head: readFileSync(join(dir, f), 'utf8').slice(0, 20_000) }))
|
|
44
|
+
.filter((s) => s.head.includes('"entrypoint":"sdk-cli"'))
|
|
45
|
+
.sort((a, b) => b.at - a.at)[0]?.id
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function build(slug: string | undefined, message: string | undefined) {
|
|
49
|
+
const ws = await target(slug)
|
|
50
|
+
const saved = readSaved(ws)
|
|
51
|
+
if (saved && alive(saved.pid)) fail(`the builder is still running in ${ws.slug} (pid ${saved.pid}); able ws attach ${ws.slug}`)
|
|
52
|
+
const resumeId = message ? (saved?.sessionId ?? newestHeadless(ws)) : undefined
|
|
53
|
+
if (message && !resumeId) fail(`no builder session in ${ws.slug} to resume; start one with able ws build ${ws.slug}`)
|
|
54
|
+
const sessionId = resumeId ?? crypto.randomUUID()
|
|
55
|
+
const prompt = message ?? `build the brief ${(await loadConfig(ws.path)).journal}/workspaces/${ws.slug}.md`
|
|
56
|
+
|
|
57
|
+
mkdirSync(join(ws.path, '.able'), { recursive: true })
|
|
58
|
+
const out = openSync(join(ws.path, '.able', 'builder.log'), 'a')
|
|
59
|
+
const session = resumeId ? ['--resume', resumeId] : ['--session-id', sessionId]
|
|
60
|
+
const child = spawn('claude', ['-p', prompt, ...session, '--permission-mode', 'bypassPermissions'], {
|
|
61
|
+
cwd: ws.path,
|
|
62
|
+
env: cleanEnv(),
|
|
63
|
+
detached: true,
|
|
64
|
+
stdio: ['ignore', out, out],
|
|
65
|
+
})
|
|
66
|
+
child.unref()
|
|
67
|
+
closeSync(out)
|
|
68
|
+
if (!child.pid) fail('claude did not start; is it on PATH?')
|
|
69
|
+
await Bun.write(savedPath(ws), `${JSON.stringify({ sessionId, pid: child.pid, startedAt: new Date().toISOString() } satisfies Builder, null, 2)}\n`)
|
|
70
|
+
console.log(`\n ${green('✓')} builder ${resumeId ? 'resumed' : 'started'} in ${bold(ws.slug)} ${dim(`session ${sessionId}`)}\n ${dim(`follow it: able ws attach ${ws.slug}`)}\n`)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One line per thing the builder said (💬) or ran (🔧). */
|
|
74
|
+
function lines(record: string) {
|
|
75
|
+
const entry = JSON.parse(record)
|
|
76
|
+
if (entry.type !== 'assistant' || !Array.isArray(entry.message?.content)) return []
|
|
77
|
+
return entry.message.content.flatMap((part: { type: string; text?: string; name?: string; input?: Record<string, unknown> }) => {
|
|
78
|
+
if (part.type === 'text' && part.text?.trim()) return [`💬 ${part.text.trim()}`]
|
|
79
|
+
if (part.type !== 'tool_use') return []
|
|
80
|
+
const detail = String(part.input?.description ?? part.input?.command ?? part.input?.file_path ?? '').split('\n')[0] ?? ''
|
|
81
|
+
return [dim(`🔧 ${part.name} ${detail.slice(0, 110)}`)]
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function follow(file: string, pid: number) {
|
|
86
|
+
let seen = 0
|
|
87
|
+
const show = (all: string[]) => all.flatMap(lines).forEach((l) => console.log(l))
|
|
88
|
+
const records = () => (existsSync(file) ? readFileSync(file, 'utf8').split('\n') : [])
|
|
89
|
+
const first = records()
|
|
90
|
+
// The whole line count minus the last, possibly partial, record.
|
|
91
|
+
seen = Math.max(0, first.length - 1)
|
|
92
|
+
show(first.slice(0, seen).slice(-40))
|
|
93
|
+
while (alive(pid)) {
|
|
94
|
+
await Bun.sleep(500)
|
|
95
|
+
const now = records()
|
|
96
|
+
const complete = Math.max(0, now.length - 1)
|
|
97
|
+
show(now.slice(seen, complete))
|
|
98
|
+
seen = complete
|
|
99
|
+
}
|
|
100
|
+
show(records().slice(seen))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function attach(slug: string | undefined) {
|
|
104
|
+
const ws = await target(slug)
|
|
105
|
+
const saved = readSaved(ws)
|
|
106
|
+
const sessionId = saved?.sessionId ?? newestHeadless(ws)
|
|
107
|
+
if (!sessionId) fail(`no builder session in ${ws.slug}; start one with able ws build ${ws.slug}`)
|
|
108
|
+
if (saved && alive(saved.pid)) {
|
|
109
|
+
console.log(`\n ${bold(ws.slug)} builder running ${dim('ctrl-c leaves it running')}\n`)
|
|
110
|
+
process.on('SIGINT', () => {
|
|
111
|
+
console.log(`\n ${dim(`left; the builder runs on. able ws attach ${ws.slug} to come back`)}\n`)
|
|
112
|
+
process.exit(0)
|
|
113
|
+
})
|
|
114
|
+
await follow(transcript(ws, sessionId), saved.pid)
|
|
115
|
+
console.log(`\n ${green('✓')} builder finished; opening its session\n`)
|
|
116
|
+
}
|
|
117
|
+
const claude = Bun.spawnSync(['claude', '--resume', sessionId], { cwd: ws.path, env: cleanEnv(), stdio: ['inherit', 'inherit', 'inherit'] })
|
|
118
|
+
process.exit(claude.exitCode ?? 0)
|
|
119
|
+
}
|
package/src/workspaces/index.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
// able ws start | stop [<service>…] the workspace's services, in dependency order
|
|
10
10
|
// able ws status the services running in this checkout
|
|
11
11
|
// able ws take <resource> moves a shared resource to this checkout
|
|
12
|
+
// able ws build <slug> ["message"] a headless builder on the brief, or the saved session resumed with a message
|
|
13
|
+
// able ws attach [<slug>] follows the builder live, then opens its session
|
|
12
14
|
|
|
13
15
|
const [sub = '', ...args] = process.argv.slice(3)
|
|
14
16
|
const names = args.filter((a) => !a.startsWith('-'))
|
|
@@ -24,6 +26,8 @@ const commands: Record<string, () => Promise<unknown>> = {
|
|
|
24
26
|
start: async () => (await import('./services.ts')).start(names),
|
|
25
27
|
stop: async () => (await import('./services.ts')).stop(names),
|
|
26
28
|
status: async () => (await import('./services.ts')).status(),
|
|
29
|
+
build: async () => (await import('./agent.ts')).build(names[0], names[1]),
|
|
30
|
+
attach: async () => (await import('./agent.ts')).attach(names[0]),
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
const command = commands[sub]
|