@able-autonomy/able 0.0.21 → 0.2.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/able.ts +10 -2
- package/config.ts +11 -0
- package/package.json +1 -1
- package/src/audit/bloat/apply.test.ts +81 -0
- package/src/audit/bloat/apply.ts +49 -0
- package/src/{agents → audit/bloat}/blocks.ts +2 -2
- package/src/{agents → audit/bloat}/checks.ts +3 -3
- package/src/audit/bloat/compare.ts +63 -0
- package/src/{agents → audit/bloat}/findings.ts +11 -7
- package/src/{agents → audit/bloat}/grid.ts +6 -6
- package/src/audit/bloat/index.ts +258 -0
- package/src/{agents → audit/bloat}/load.ts +2 -2
- package/src/{agents → audit/bloat}/picker.ts +4 -4
- package/src/audit/bloat/report.ts +286 -0
- package/src/{agents → audit/bloat}/scan.ts +3 -3
- package/src/audit/bloat/show.ts +79 -0
- package/src/audit/bloat/steps.ts +32 -0
- package/src/{agents → audit/bloat}/types.ts +48 -3
- package/src/audit/index.ts +19 -0
- package/src/commit.ts +2 -1
- package/src/git.ts +43 -2
- package/src/push.ts +2 -1
- package/src/ui.ts +3 -0
- package/src/workspaces/fixture.ts +49 -0
- package/src/workspaces/index.ts +3 -1
- package/src/workspaces/ls.ts +56 -11
- package/src/workspaces/merge.ts +31 -15
- package/src/workspaces/new.ts +17 -15
- package/src/workspaces/overlap.test.ts +53 -0
- package/src/workspaces/overlap.ts +86 -0
- package/src/workspaces/preview.test.ts +81 -0
- package/src/workspaces/preview.ts +162 -0
- package/src/workspaces/refresh.ts +26 -24
- package/src/workspaces/rm.ts +6 -1
- package/src/workspaces/services.ts +2 -2
- package/src/workspaces/state.ts +56 -9
- package/src/workspaces/trunk.test.ts +88 -0
- package/sync.ts +5 -2
- package/src/agents/index.ts +0 -198
- package/src/agents/report.ts +0 -211
- package/src/agents/show.ts +0 -73
- package/src/agents/steps.ts +0 -32
- /package/src/{agents → audit/bloat}/kinds.ts +0 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Step 8, show: a report read back from its file and printed as plain text, narrow enough for a terminal and a chat.
|
|
2
|
+
// `--step <n>` prints one step's full view from the same file, so any step can be looked at again later.
|
|
3
|
+
|
|
4
|
+
import { relative } from 'node:path'
|
|
5
|
+
import { dim } from '../../ui.ts'
|
|
6
|
+
import * as blocks from './blocks.ts'
|
|
7
|
+
import * as checks from './checks.ts'
|
|
8
|
+
import * as grid from './grid.ts'
|
|
9
|
+
import { kinds, pickOrder } from './kinds.ts'
|
|
10
|
+
import * as load from './load.ts'
|
|
11
|
+
import { reportName, tally, type Finding, type Report } from './report.ts'
|
|
12
|
+
import { who } from './steps.ts'
|
|
13
|
+
import { clip, fit, n, render as renderScan } from './scan.ts'
|
|
14
|
+
|
|
15
|
+
/** One step's full view, from the report file. */
|
|
16
|
+
export function step(report: Report, id: string) {
|
|
17
|
+
// In reading order, as the file has them; the report stores them in pick order.
|
|
18
|
+
const checked = { rules: [...report.rules].sort((a, b) => a.span[0] - b.span[0]), missing: [], errors: [] }
|
|
19
|
+
const views: Record<string, () => string> = {
|
|
20
|
+
scan: () => renderScan(report.scan),
|
|
21
|
+
load: () => load.render(report.load),
|
|
22
|
+
blocks: () => blocks.render(report.blocks),
|
|
23
|
+
checks: () => checks.render(report.checks),
|
|
24
|
+
classify: () => grid.byKind(report.blocks, checked),
|
|
25
|
+
}
|
|
26
|
+
return views[id]?.() ?? ` Step ${id} has no view of its own: its result is in the findings below.\n\n${findings(report)}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A finding as two lines: where it is and its words, then its proposal. `mark` comes first, as `show`'s pick box. */
|
|
30
|
+
export function findingLine(r: Finding, mark = '') {
|
|
31
|
+
return [
|
|
32
|
+
` ${mark}${String(r.n).padStart(3)} ${r.confidence.padEnd(4)} ${fit(`${r.path}:${r.line}`, 34).padEnd(34)} "${clip(r.text.replace(/\s+/g, ' '), 46)}"`,
|
|
33
|
+
`${' '.repeat(mark.length + 13)}→ ${r.proposal ? `${clip(r.proposal.target, 40)}: ${clip(r.proposal.change, 64)}` : dim('no proposal yet')}`,
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function findings(report: Report) {
|
|
38
|
+
const out: string[] = []
|
|
39
|
+
const numbered = report.rules.filter((r) => r.n)
|
|
40
|
+
for (const kind of pickOrder) {
|
|
41
|
+
const list = numbered.filter((r) => r.kind === kind)
|
|
42
|
+
if (!list.length) continue
|
|
43
|
+
out.push(` ${kinds[kind].letter} ${kind.toUpperCase()} ${list.length} ${dim(`→ ${kinds[kind].becomes}`)}`)
|
|
44
|
+
for (const r of list) {
|
|
45
|
+
const pick = r.pick === 'yes' ? '[y]' : r.pick === 'no' ? '[n]' : '[ ]'
|
|
46
|
+
const [where, proposal] = findingLine(r, pick)
|
|
47
|
+
out.push(`${where}${r.recommend ? dim(` rec ${r.recommend}`) : ''}`, `${' '.repeat(16)}${dim(clip(r.reason, 92))}`, proposal!)
|
|
48
|
+
}
|
|
49
|
+
out.push('')
|
|
50
|
+
}
|
|
51
|
+
return out.join('\n')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function render(report: Report, path: string, root: string) {
|
|
55
|
+
const out: string[] = []
|
|
56
|
+
const { numbered } = tally(report)
|
|
57
|
+
out.push(
|
|
58
|
+
` REPORT ${reportName(new Date(report.at))
|
|
59
|
+
.replace(/\.json$/, '')
|
|
60
|
+
.replace(/-(\d\d)(\d\d)$/, ' $1:$2')} ${relative(root, path)} ${report.format}`,
|
|
61
|
+
'',
|
|
62
|
+
)
|
|
63
|
+
out.push(' STEPS')
|
|
64
|
+
for (const s of report.steps) out.push(` ${String(s.n).padStart(2)} ${who[s.kind as keyof typeof who]} ${s.id.padEnd(10)} ${s.summary}`)
|
|
65
|
+
|
|
66
|
+
out.push('', ` WORDS AT START, BY FOLDER (host files; tokens are about 1.3 x words)`)
|
|
67
|
+
out.push(` ${'folder'.padEnd(24)} ${'now'.padStart(6)} ${'after'.padStart(6)} ${'saved'.padStart(6)}`)
|
|
68
|
+
for (const f of report.impact.folders)
|
|
69
|
+
out.push(` ${clip(f.folder === '.' ? '(root)' : f.folder, 24).padEnd(24)} ${n(f.before).padStart(6)} ${n(f.after).padStart(6)} ${`-${n(f.before - f.after)}`.padStart(6)}`)
|
|
70
|
+
|
|
71
|
+
const kept = report.impact.byKind.keep
|
|
72
|
+
out.push('', ` FINDINGS ${numbered} to pick${kept ? `, ${kept.rules} rules kept` : ''} [y] yes [n] no [ ] not picked yet`, '')
|
|
73
|
+
out.push(findings(report))
|
|
74
|
+
|
|
75
|
+
out.push(' AGENTS.md (vendor-neutral; converting is version 2)')
|
|
76
|
+
for (const a of report.agentsMd) out.push(` ${a.path.padEnd(24)} ${a.blockers.length ? a.blockers.join('; ') : 'would convert: AGENTS.md, plus a CLAUDE.md of one line, @AGENTS.md'}`)
|
|
77
|
+
if (!report.agentsMd.length) out.push(' no CLAUDE.md files')
|
|
78
|
+
return out.join('\n')
|
|
79
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The steps of a report, as data: `able audit bloat status` walks them, the skill follows them, a dashboard page will show them.
|
|
2
|
+
|
|
3
|
+
export type StepKind = 'mechanical' | 'agent' | 'operator'
|
|
4
|
+
|
|
5
|
+
export interface Step {
|
|
6
|
+
n: number
|
|
7
|
+
/** The step's one name, also its command: `able audit bloat <id>`. */
|
|
8
|
+
id: string
|
|
9
|
+
kind: StepKind
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The letter each kind of step shows as in `status` and `show`. */
|
|
13
|
+
export const who: Record<StepKind, string> = { mechanical: 'M', agent: 'A', operator: 'O' }
|
|
14
|
+
|
|
15
|
+
export const steps: Step[] = [
|
|
16
|
+
{ n: 1, id: 'scan', kind: 'mechanical' },
|
|
17
|
+
{ n: 2, id: 'load', kind: 'mechanical' },
|
|
18
|
+
{ n: 3, id: 'blocks', kind: 'mechanical' },
|
|
19
|
+
{ n: 4, id: 'checks', kind: 'mechanical' },
|
|
20
|
+
{ n: 5, id: 'classify', kind: 'agent' },
|
|
21
|
+
{ n: 6, id: 'propose', kind: 'agent' },
|
|
22
|
+
{ n: 7, id: 'report', kind: 'mechanical' },
|
|
23
|
+
{ n: 8, id: 'show', kind: 'mechanical' },
|
|
24
|
+
{ n: 9, id: 'pick', kind: 'operator' },
|
|
25
|
+
{ n: 10, id: 'apply', kind: 'agent' },
|
|
26
|
+
{ n: 11, id: 'compare', kind: 'mechanical' },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
/** The folder reports are written to, under the host's journal. One name, so renaming the diagnostic is one line. */
|
|
30
|
+
export const reportsDir = 'bloat'
|
|
31
|
+
/** Where reports were written before the diagnostic was named bloat (as `able agents`): still read, never written. */
|
|
32
|
+
export const legacyReportsDir = 'agents'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The shapes a
|
|
1
|
+
// The shapes a report is made of, types only and importing nothing, so api/ and web/ can read a report file typed by the CLI
|
|
2
2
|
// that wrote it without type-checking the CLI's code. Each step's module re-exports its own.
|
|
3
3
|
|
|
4
4
|
/** The ten kinds a rule can be (kinds.ts holds their letters and meanings). */
|
|
@@ -148,10 +148,15 @@ export interface Finding extends Rule {
|
|
|
148
148
|
line: number
|
|
149
149
|
words: number
|
|
150
150
|
pick?: 'yes' | 'no'
|
|
151
|
+
/** Step 10: where a yes pick went, recorded by `able audit bloat apply --record` once the agent has made the change. */
|
|
152
|
+
applied?: { at: string; target: string }
|
|
153
|
+
/** Lineage: the id of the finding in the report before that this rule came from, when it was applied and moved here
|
|
154
|
+
* (its words found again in the file it was applied to). */
|
|
155
|
+
from?: string
|
|
151
156
|
}
|
|
152
157
|
|
|
153
|
-
export interface
|
|
154
|
-
format: 'able.agents@1' | 'able.agents@2'
|
|
158
|
+
export interface Report {
|
|
159
|
+
format: 'able.agents@1' | 'able.agents@2' | 'able.bloat.report@1'
|
|
155
160
|
at: string
|
|
156
161
|
steps: { n: number; id: string; kind: string; summary: string }[]
|
|
157
162
|
scan: Entry[]
|
|
@@ -169,3 +174,43 @@ export interface Run {
|
|
|
169
174
|
/** Each CLAUDE.md as a vendor-neutral AGENTS.md: what would convert, and what stands in the way (version 2 converts). */
|
|
170
175
|
agentsMd: { path: string; blockers: string[] }[]
|
|
171
176
|
}
|
|
177
|
+
|
|
178
|
+
// compare.ts
|
|
179
|
+
/** Step 11: a report against the one after it, once picks were applied in between. */
|
|
180
|
+
export interface Comparison {
|
|
181
|
+
/** Words at start an agent in each folder loads: before, predicted after the applied picks, and actual. */
|
|
182
|
+
folders: { folder: string; before: number; predicted: number; actual: number | null }[]
|
|
183
|
+
/** Each applied finding: gone from where it was, or still there; and the rule it became, when the new report found
|
|
184
|
+
* its words again in the file it went to (its `from`). */
|
|
185
|
+
applied: { n: number; id: string; kind: Kind; path: string; target: string; gone: boolean; became?: { id: string; kind: Kind; path: string }; state: string }[]
|
|
186
|
+
/** Findings of the old report that are gone though nobody picked them yes: a rule lost, or its words changed. */
|
|
187
|
+
unpicked: { n: number; id: string; kind: Kind; path: string; text: string }[]
|
|
188
|
+
/** Yes picks that are gone though nothing recorded where they went (`able audit bloat apply --record` was missed). */
|
|
189
|
+
unrecorded: { n: number; id: string; kind: Kind; path: string; text: string }[]
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** `able audit bloat compare --json`: the comparison and the two reports' names. */
|
|
193
|
+
export interface Compared {
|
|
194
|
+
c: Comparison
|
|
195
|
+
names: { old: string; next: string }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** One report in the diagnostic's history (`able audit bloat history`): what the report picker shows of it, and a point on
|
|
199
|
+
* the trend charts. Worked out from the report file, so it never disagrees with it. */
|
|
200
|
+
export interface HistoryLine {
|
|
201
|
+
/** The report's name, its file's: `2026-11-18-1449`. */
|
|
202
|
+
name: string
|
|
203
|
+
/** Its short handle, easy to say and type: six letters and digits from its name (report.ts, reportCode). */
|
|
204
|
+
code: string
|
|
205
|
+
/** Its file, from the root, and the format it was written in. */
|
|
206
|
+
file: string
|
|
207
|
+
format: Report['format']
|
|
208
|
+
at: string
|
|
209
|
+
files: number
|
|
210
|
+
rules: number
|
|
211
|
+
/** Host words an agent starting at the root loads, now and with every proposal applied. */
|
|
212
|
+
start: { before: number; after: number }
|
|
213
|
+
/** Rules per kind. */
|
|
214
|
+
byKind: Partial<Record<Kind, number>>
|
|
215
|
+
picks: { numbered: number; yes: number; no: number; open: number }
|
|
216
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// able audit: the Audit module (knowledge/product/modules/audit.md). On-demand diagnostics, each writing a frozen
|
|
2
|
+
// report the operator picks from, with history and compare.
|
|
3
|
+
//
|
|
4
|
+
// able audit lists the audits
|
|
5
|
+
// able audit bloat <step> … the bloat diagnostic (bloat/index.ts); cli/able.ts rewrites `able bloat` and `able agents` to it
|
|
6
|
+
|
|
7
|
+
const audits: Record<string, { about: string; run: () => Promise<unknown> }> = {
|
|
8
|
+
bloat: { about: 'where each rule in the agent files belongs', run: () => import('./bloat/index.ts') },
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const name = process.argv[3] ?? ''
|
|
12
|
+
if (Object.hasOwn(audits, name)) await audits[name]!.run()
|
|
13
|
+
else {
|
|
14
|
+
const list = Object.entries(audits).map(([n, a]) => ` able audit ${n} <step> ${a.about}`)
|
|
15
|
+
console.error(['usage: able audit <audit> <step> …', ...list].join('\n'))
|
|
16
|
+
process.exit(name ? 1 : 0)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export {}
|
package/src/commit.ts
CHANGED
|
@@ -14,13 +14,14 @@ import { runChecks } from './checks.ts'
|
|
|
14
14
|
import { failOnMerge, git, gitLoud } from './git.ts'
|
|
15
15
|
import { status, working } from './status.ts'
|
|
16
16
|
import { bold, cancel, confirm, dim, fail, green } from './ui.ts'
|
|
17
|
-
import { current } from './workspaces/state.ts'
|
|
17
|
+
import { current, refuseInPreview } from './workspaces/state.ts'
|
|
18
18
|
|
|
19
19
|
const argv = process.argv.slice(3)
|
|
20
20
|
const [msg, ...named] = argv.filter((a) => !a.startsWith('--'))
|
|
21
21
|
if (!msg) fail('usage: able commit "<message>" [<path>…] [--dry] [--yes]')
|
|
22
22
|
|
|
23
23
|
const at = await current()
|
|
24
|
+
refuseInPreview(at, 'able commit')
|
|
24
25
|
const root = at.path
|
|
25
26
|
failOnMerge(root)
|
|
26
27
|
|
package/src/git.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// The git calls every command shares: capture output, stream it, or stay quiet; and the questions commands keep
|
|
2
2
|
// asking (dirty, unpushed, behind, merge in progress) answered once.
|
|
3
3
|
|
|
4
|
+
import { existsSync, statSync } from 'node:fs'
|
|
4
5
|
import { dirname, resolve } from 'node:path'
|
|
5
|
-
import { dim, fail, red } from './ui.ts'
|
|
6
|
+
import { dim, fail, red, when, yellow } from './ui.ts'
|
|
6
7
|
|
|
7
8
|
const spawn = (cwd: string, args: string[], stdio: 'inherit' | 'ignore') => Bun.spawnSync(['git', '-C', cwd, ...args], { stdout: stdio, stderr: stdio })
|
|
8
9
|
|
|
@@ -32,7 +33,47 @@ export const dirtyCount = (cwd: string) => lines(cwd, 'status', '--porcelain').l
|
|
|
32
33
|
/** Commits reachable from HEAD and from no remote ref: what exists only here. */
|
|
33
34
|
export const unpushedCount = (cwd: string) => count(cwd, 'HEAD', '--not', '--remotes')
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
/** When `origin/<trunk>` was last fetched here: FETCH_HEAD's time, else the remote ref's last move. */
|
|
37
|
+
export function lastFetched(cwd: string, trunk: string) {
|
|
38
|
+
const head = resolve(cwd, git(cwd, 'rev-parse', '--git-path', 'FETCH_HEAD'))
|
|
39
|
+
const at = existsSync(head) ? statSync(head).mtime : new Date(git(cwd, 'log', '-g', '-1', '--format=%cI', `refs/remotes/origin/${trunk}`) || NaN)
|
|
40
|
+
return Number.isNaN(at.getTime()) ? 'never' : when(at)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fetches `origin/<trunk>`, the trunk every command means. When origin can't be reached it says so and carries on
|
|
45
|
+
* with the copy it has; a command that must push then calls `needOrigin`. Returns whether the fetch worked.
|
|
46
|
+
*/
|
|
47
|
+
export function fetchTrunk(cwd: string, trunk: string) {
|
|
48
|
+
if (gitQuiet(cwd, 'fetch', '--quiet', 'origin', trunk)) return true
|
|
49
|
+
console.log(yellow(` couldn't reach origin; using origin/${trunk} as of ${lastFetched(cwd, trunk)}`))
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Fails when the fetch couldn't reach origin and the command is about to push. */
|
|
54
|
+
export const needOrigin = (fetched: boolean, trunk: string) => fetched || fail(`couldn't reach origin/${trunk}, and this has to push; nothing pushed`)
|
|
55
|
+
|
|
56
|
+
/** Commits on `local <trunk>` that `origin/<trunk>` doesn't have, oldest first, as `<hash> <subject>`. */
|
|
57
|
+
// A missing `local <trunk>` makes git fail, and `lines` then answers no commits.
|
|
58
|
+
export const unpushedOnTrunk = (cwd: string, trunk: string, notIn?: string) =>
|
|
59
|
+
lines(cwd, 'log', '--reverse', '--format=%h %s', `origin/${trunk}..refs/heads/${trunk}`, ...(notIn ? ['--not', notIn] : []))
|
|
60
|
+
|
|
61
|
+
/** Commits on `local <trunk>`, not on `origin/<trunk>`, that HEAD took in through a merge: off its first-parent line, so
|
|
62
|
+
* a branch's own commits never count, even when local <trunk> was fast-forwarded onto them. `%h %s` each, oldest first. */
|
|
63
|
+
export function carriedFromLocal(cwd: string, trunk: string) {
|
|
64
|
+
const fromLocal = new Set(lines(cwd, 'rev-list', `origin/${trunk}..refs/heads/${trunk}`))
|
|
65
|
+
const own = new Set(lines(cwd, 'rev-list', '--first-parent', `origin/${trunk}..HEAD`))
|
|
66
|
+
return lines(cwd, 'log', '--reverse', '--format=%H %h %s', `origin/${trunk}..HEAD`)
|
|
67
|
+
.map((l) => [l.slice(0, l.indexOf(' ')), l.slice(l.indexOf(' ') + 1)] as const)
|
|
68
|
+
.filter(([c]) => fromLocal.has(c) && !own.has(c))
|
|
69
|
+
.map(([, shown]) => shown)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Names the commits on `local <trunk>` a command left out, and how to bring them in. */
|
|
73
|
+
export function warnLeftOut(left: string[], what: string, hint: string) {
|
|
74
|
+
if (!left.length) return
|
|
75
|
+
console.log(`${yellow(`\n ${left.length} commit(s) ${what}`)}\n${left.map((l) => dim(` ${l}`)).join('\n')}\n ${dim(hint)}`)
|
|
76
|
+
}
|
|
36
77
|
|
|
37
78
|
export const failOnMerge = (cwd: string) => git(cwd, 'rev-parse', '-q', '--verify', 'MERGE_HEAD') && fail('a merge is in progress; finish it (commit) or abort it first')
|
|
38
79
|
|
package/src/push.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import { gitLoud } from './git.ts'
|
|
4
4
|
import { fail, green } from './ui.ts'
|
|
5
|
-
import { current } from './workspaces/state.ts'
|
|
5
|
+
import { current, refuseInPreview } from './workspaces/state.ts'
|
|
6
6
|
|
|
7
7
|
const at = await current()
|
|
8
|
+
refuseInPreview(at, 'able push')
|
|
8
9
|
if (at.isWorkspace) fail('a workspace is merged with: able ws merge')
|
|
9
10
|
if (!at.branch || at.branch === 'HEAD') fail('not on a branch')
|
|
10
11
|
if (!gitLoud(at.path, 'push', '--follow-tags', 'origin', at.branch)) fail(`push of ${at.branch} failed`)
|
package/src/ui.ts
CHANGED
|
@@ -15,6 +15,9 @@ export const red = paint(31)
|
|
|
15
15
|
export const green = paint(32)
|
|
16
16
|
export const yellow = paint(33)
|
|
17
17
|
|
|
18
|
+
/** A moment as the CLI prints it: `19 Nov 2026, 13:37`. */
|
|
19
|
+
export const when = (at: Date | string) => new Date(at).toLocaleString('en-GB', { dateStyle: 'medium', timeStyle: 'short' })
|
|
20
|
+
|
|
18
21
|
/** Prints the message, reports the error and exits 1. */
|
|
19
22
|
export function fail(message: string): never {
|
|
20
23
|
status({ state: 'error', msg: message })
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A scratch host for the workspace tests: a bare `origin`, a main checkout cloned from it with an `able.config.ts`,
|
|
2
|
+
// and a way to run `able` inside any checkout of it. Everything lives in one temporary folder.
|
|
3
|
+
|
|
4
|
+
import { mkdtempSync, mkdirSync, realpathSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { tmpdir } from 'node:os'
|
|
6
|
+
import { dirname, join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
const able = join(import.meta.dir, '../../able.ts')
|
|
9
|
+
|
|
10
|
+
export const sh = (cwd: string, ...cmd: string[]) => {
|
|
11
|
+
const r = Bun.spawnSync(cmd, { cwd, stdout: 'pipe', stderr: 'pipe', env: { ...process.env, GIT_AUTHOR_NAME: 't', GIT_AUTHOR_EMAIL: 't@t', GIT_COMMITTER_NAME: 't', GIT_COMMITTER_EMAIL: 't@t' } })
|
|
12
|
+
return { code: r.exitCode, out: `${r.stdout}${r.stderr}`.replace(/\x1b\[[0-9;]*m|\x1b\][^\x1b]*\x1b\\/g, '') }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const git = (cwd: string, ...args: string[]) => {
|
|
16
|
+
const r = sh(cwd, 'git', ...args)
|
|
17
|
+
if (r.code !== 0) throw new Error(`git ${args.join(' ')}: ${r.out}`)
|
|
18
|
+
return r.out.trim()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const run = (cwd: string, ...args: string[]) => sh(cwd, process.execPath, able, ...args)
|
|
22
|
+
|
|
23
|
+
export function write(root: string, path: string, text: string) {
|
|
24
|
+
mkdirSync(dirname(join(root, path)), { recursive: true })
|
|
25
|
+
writeFileSync(join(root, path), text)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const commit = (root: string, path: string, text: string, message = `change ${path}`) => {
|
|
29
|
+
write(root, path, text)
|
|
30
|
+
git(root, 'add', '--', path)
|
|
31
|
+
git(root, 'commit', '--quiet', '-m', message)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A fresh host: `origin` (bare), `main` (its clone, on `main`, pushed), workspaces in `ws/`. */
|
|
35
|
+
export function host() {
|
|
36
|
+
const dir = realpathSync(mkdtempSync(join(tmpdir(), 'able-ws-')))
|
|
37
|
+
const origin = join(dir, 'origin.git')
|
|
38
|
+
const main = join(dir, 'main')
|
|
39
|
+
git(dir, 'init', '--quiet', '--bare', '--initial-branch=main', origin)
|
|
40
|
+
git(dir, 'clone', '--quiet', origin, main)
|
|
41
|
+
git(main, 'checkout', '--quiet', '-B', 'main')
|
|
42
|
+
write(main, 'able.config.ts', `export default { trunk: 'main', journal: 'journal', workspaces: { dir: '../ws', ports: { step: 100, max: 9 } } }\n`)
|
|
43
|
+
write(main, '.gitignore', '.able/\n')
|
|
44
|
+
write(main, 'README.md', 'one\ntwo\nthree\n')
|
|
45
|
+
git(main, 'add', '-A')
|
|
46
|
+
git(main, 'commit', '--quiet', '-m', 'start')
|
|
47
|
+
git(main, 'push', '--quiet', '-u', 'origin', 'main')
|
|
48
|
+
return { dir, origin, main, ws: (slug: string) => join(dir, 'ws', slug) }
|
|
49
|
+
}
|
package/src/workspaces/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// able ws: the Workspaces module (knowledge/product/modules/workspaces.md).
|
|
2
2
|
//
|
|
3
3
|
// able ws new <slug> [--no-fetch] a worktree on feat/<slug> from the trunk, with the host's copy and onCreate
|
|
4
|
-
// able ws ls [--json] every checkout: offset, services, tree, resources held
|
|
4
|
+
// able ws ls [--json] every checkout: offset, services, tree, resources held; overlap between workspaces
|
|
5
5
|
// able ws current [--json] the checkout this runs in: slug, branch, path, port offset, main checkout
|
|
6
6
|
// able ws refresh merges the trunk into this workspace
|
|
7
7
|
// able ws merge ["message"] [--yes] [--keep-open] merges this workspace into the trunk as `Merge feat/<slug>`
|
|
8
8
|
// able ws rm <slug> [--force] stops and removes a workspace, deleting its branch once merged
|
|
9
|
+
// able ws preview [<slug>…] rebuilds `all`: origin's trunk plus the open workspaces' committed work
|
|
9
10
|
// able ws start | stop [<service>…] the workspace's services, in dependency order
|
|
10
11
|
// able ws status the services running in this checkout
|
|
11
12
|
// able ws take <resource> moves a shared resource to this checkout
|
|
@@ -21,6 +22,7 @@ const commands: Record<string, () => Promise<unknown>> = {
|
|
|
21
22
|
rm: () => import('./rm.ts'),
|
|
22
23
|
refresh: () => import('./refresh.ts'),
|
|
23
24
|
merge: () => import('./merge.ts'),
|
|
25
|
+
preview: () => import('./preview.ts'),
|
|
24
26
|
take: () => import('./take.ts'),
|
|
25
27
|
current: async () => console.log(JSON.stringify(await (await import('./state.ts')).current(), null, args.includes('--json') ? undefined : 2)),
|
|
26
28
|
start: async () => (await import('./services.ts')).start(names),
|
package/src/workspaces/ls.ts
CHANGED
|
@@ -1,25 +1,48 @@
|
|
|
1
1
|
// able ws ls: the main checkout and every workspace, read live: offset, services, tree, shared resources held.
|
|
2
2
|
// A service counts as running when Able started it or its `ready` check answers; a resource's holders come from its
|
|
3
3
|
// `holder` command when the host gives one, otherwise from Able's record.
|
|
4
|
-
//
|
|
4
|
+
// Under the table, the overlap between open workspaces' committed tips (overlap.ts), against `origin/<trunk>` as last
|
|
5
|
+
// fetched; `ls` itself doesn't fetch. With --json, only the workspaces and the preview, oldest first and the preview
|
|
6
|
+
// last, for a host's own tools (84): slug, branch, path, portOffset, createdAt, `dirty`, `unpushed`, `url` (from
|
|
7
|
+
// `workspaces.url`), `overlap` (each `{ with, conflicts, shared }`) and the preview's `build`. `--no-overlap` leaves
|
|
8
|
+
// the overlap out, for a caller that needs only which checkouts there are: it is most of the git work. Where it can't
|
|
9
|
+
// be worked out (git older than 2.38, no `origin/<trunk>`) the list still prints, the overlap said to be not shown.
|
|
5
10
|
|
|
6
11
|
import { existsSync } from 'node:fs'
|
|
7
12
|
import { resolve } from 'node:path'
|
|
8
|
-
import { loadConfig } from '../../config.ts'
|
|
9
|
-
import { dirtyCount, mainRoot, unpushedCount } from '../git.ts'
|
|
13
|
+
import { loadConfig, urlOf } from '../../config.ts'
|
|
14
|
+
import { dirtyCount, lastFetched, mainRoot, unpushedCount } from '../git.ts'
|
|
10
15
|
import { shellFor, type Place } from '../run.ts'
|
|
11
|
-
import { bold, dim, red, yellow } from '../ui.ts'
|
|
16
|
+
import { bold, dim, red, when, yellow } from '../ui.ts'
|
|
17
|
+
import { overlapUnavailable, overlaps } from './overlap.ts'
|
|
12
18
|
import { answers } from './services.ts'
|
|
13
|
-
import {
|
|
19
|
+
import { byAge, holderOf, openOf, PREVIEW, readState, running, workspaces } from './state.ts'
|
|
14
20
|
|
|
15
21
|
const main = mainRoot()
|
|
16
22
|
const state = await readState(main)
|
|
23
|
+
const config = await loadConfig(main)
|
|
24
|
+
const all = (await workspaces(main, state)).sort(byAge)
|
|
25
|
+
const open = openOf(all)
|
|
26
|
+
const base = `origin/${config.trunk}`
|
|
27
|
+
const skip = process.argv.includes('--no-overlap') || !open.length
|
|
28
|
+
const noOverlap = skip ? undefined : overlapUnavailable(main, config.trunk)
|
|
29
|
+
const overlap =
|
|
30
|
+
skip || noOverlap
|
|
31
|
+
? {}
|
|
32
|
+
: overlaps(
|
|
33
|
+
main,
|
|
34
|
+
config.trunk,
|
|
35
|
+
open.map((w) => ({ slug: w.slug, tip: w.branch })),
|
|
36
|
+
)
|
|
17
37
|
if (process.argv.includes('--json')) {
|
|
18
|
-
|
|
38
|
+
const facts = (w: (typeof all)[number]) => (existsSync(w.path) ? { dirty: dirtyCount(w.path), unpushed: w.branch ? unpushedCount(w.path) : 0 } : {})
|
|
39
|
+
const url = (w: (typeof all)[number]) => (config.workspaces?.url ? { url: urlOf(config.workspaces.url, w.slug, w.portOffset) } : {})
|
|
40
|
+
const build = (w: (typeof all)[number]) => (w.kind ? { build: state.workspaces[PREVIEW]?.build } : {})
|
|
41
|
+
console.log(JSON.stringify(all.map((w) => ({ ...w, ...facts(w), ...url(w), ...build(w), overlap: overlap[w.slug] ?? [] }))))
|
|
19
42
|
process.exit(0)
|
|
20
43
|
}
|
|
21
|
-
const rows =
|
|
22
|
-
const { services = {}, resources = {} } =
|
|
44
|
+
const rows: Place[] = [{ slug: 'main', path: main, portOffset: 0, main }, ...all.map((w) => ({ slug: w.slug, path: w.path, portOffset: w.portOffset, main }))]
|
|
45
|
+
const { services = {}, resources = {} } = config.workspaces ?? {}
|
|
23
46
|
const home = rows[0]
|
|
24
47
|
|
|
25
48
|
const asked = (c: NonNullable<(typeof resources)[string]['holder']>, at: Place) => {
|
|
@@ -49,22 +72,44 @@ const upCount = async (w: Place) => {
|
|
|
49
72
|
}
|
|
50
73
|
const ups = await Promise.all(rows.map(upCount))
|
|
51
74
|
|
|
52
|
-
|
|
75
|
+
const width = Math.max(16, ...rows.map((w) => w.slug.length))
|
|
76
|
+
console.log(`\n${bold(` ${'CHECKOUT'.padEnd(width)} OFFSET SERVICES TREE`)}`)
|
|
53
77
|
rows.forEach((w, i) => {
|
|
54
78
|
const up = ups[i] ?? 0
|
|
55
79
|
let tree = yellow('directory missing')
|
|
56
80
|
if (existsSync(w.path)) {
|
|
57
81
|
const dirty = dirtyCount(w.path)
|
|
58
82
|
const ahead = unpushedCount(w.path)
|
|
59
|
-
|
|
83
|
+
const built = w.slug === PREVIEW ? state.workspaces[PREVIEW]?.build : undefined
|
|
84
|
+
tree = [
|
|
85
|
+
dirty ? yellow(`${dirty} dirty`) : dim('clean'),
|
|
86
|
+
built ? dim(`preview of ${built.included.map((i) => i.slug).join(', ') || `${base} alone`}, built ${when(built.at)}`) : ahead ? yellow(`${ahead} unpushed`) : dim('0 unpushed'),
|
|
87
|
+
].join(dim(' · '))
|
|
60
88
|
}
|
|
61
89
|
const holds = Object.entries(holders)
|
|
62
90
|
.filter(([, slugs]) => slugs.includes(w.slug))
|
|
63
91
|
.map(([name, slugs]) => (slugs.length > 1 ? red(name) : name))
|
|
64
92
|
const column = up ? `${up} running`.padEnd(10) : dim('stopped'.padEnd(10))
|
|
65
|
-
console.log(` ${w.slug.padEnd(
|
|
93
|
+
console.log(` ${w.slug.padEnd(width)} ${String(w.portOffset).padEnd(7)} ${column} ${tree}${holds.length ? bold(` holds ${holds.join(' + ')}`) : ''}`)
|
|
66
94
|
})
|
|
67
95
|
for (const [name, slugs] of Object.entries(holders))
|
|
68
96
|
if (slugs.length > 1) console.log(red(`\n ${name} is held by ${slugs.join(' and ')} at once: able ws take ${name} in the one that should hold it`))
|
|
69
97
|
if (rows.length === 1) console.log(dim('\n no workspaces: able ws new <slug>'))
|
|
98
|
+
|
|
99
|
+
// Each workspace against the trunk, and each pair once: under the older of the two.
|
|
100
|
+
const rank = (slug: string) => open.findIndex((w) => w.slug === slug)
|
|
101
|
+
const found = open.flatMap((w) => (overlap[w.slug] ?? []).filter((o) => o.with === base || rank(o.with) > rank(w.slug)).map((o) => ({ slug: w.slug, ...o })))
|
|
102
|
+
if (noOverlap) console.log(`\n${bold(' OVERLAP')} ${dim(`not shown: ${noOverlap}`)}`)
|
|
103
|
+
else if (!skip) {
|
|
104
|
+
console.log(`\n${bold(' OVERLAP')} ${dim(`committed tips, against ${base} as of ${lastFetched(main, config.trunk)}`)}`)
|
|
105
|
+
if (!found.length) console.log(dim(' none'))
|
|
106
|
+
const pairWidth = Math.max(...found.map((o) => `${o.slug} × ${o.with}`.length), 0)
|
|
107
|
+
const list = (files: string[]) => files.slice(0, 4).join(', ') + (files.length > 4 ? dim(` +${files.length - 4}`) : '')
|
|
108
|
+
for (const o of found) {
|
|
109
|
+
const pair = `${o.slug} × ${o.with}`.padEnd(pairWidth)
|
|
110
|
+
if (o.conflicts.length) console.log(` ${pair} ${red(`${o.conflicts.length} conflict${o.conflicts.length > 1 ? 's' : ''}`)} ${list(o.conflicts)}`)
|
|
111
|
+
const quiet = o.shared.filter((f) => !o.conflicts.includes(f))
|
|
112
|
+
if (quiet.length) console.log(` ${o.conflicts.length ? ''.padEnd(pairWidth) : pair} ${yellow(`${quiet.length} shared`)} ${list(quiet)}`)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
70
115
|
console.log()
|
package/src/workspaces/merge.ts
CHANGED
|
@@ -8,13 +8,16 @@
|
|
|
8
8
|
// `Merge feat/<slug>`, which is how `able sync` finds it. A message commits what is still dirty first. The main checkout takes no part; afterwards it is
|
|
9
9
|
// fast-forwarded when it is on the trunk and nothing of its own is in the way.
|
|
10
10
|
//
|
|
11
|
+
// It never carries commits that are only on the main checkout's `local <trunk>`: a branch holding any (merged in before
|
|
12
|
+
// `able ws refresh` took `origin/<trunk>` only) is refused, with them named, until they are pushed on their own.
|
|
13
|
+
//
|
|
11
14
|
// A conflict stops it with the files named: resolve them here, commit, and run it again. A push refused because the
|
|
12
15
|
// trunk moved meanwhile is the same: run it again, and it merges the newer trunk in.
|
|
13
16
|
|
|
14
17
|
import { existsSync, readFileSync } from 'node:fs'
|
|
15
18
|
import { join, relative } from 'node:path'
|
|
16
19
|
import { runChecks, runMergeChecks } from '../checks.ts'
|
|
17
|
-
import { count, dirtyCount, fetchTrunk, git, gitLoud, gitQuiet, lines, mergeOrFail } from '../git.ts'
|
|
20
|
+
import { carriedFromLocal, count, dirtyCount, fetchTrunk, git, gitLoud, gitQuiet, lines, mergeOrFail, needOrigin, unpushedOnTrunk } from '../git.ts'
|
|
18
21
|
import { status, working } from '../status.ts'
|
|
19
22
|
import { bold, cancel, confirm, dim, fail, green, yellow } from '../ui.ts'
|
|
20
23
|
import { inWorkspace } from './state.ts'
|
|
@@ -22,7 +25,7 @@ import { inWorkspace } from './state.ts'
|
|
|
22
25
|
const args = process.argv.slice(4)
|
|
23
26
|
const msg = args.find((a) => !a.startsWith('-') && a !== '')
|
|
24
27
|
|
|
25
|
-
const { ws, config } = await inWorkspace()
|
|
28
|
+
const { ws, config } = await inWorkspace('able ws merge')
|
|
26
29
|
const { trunk } = config
|
|
27
30
|
const dirty = dirtyCount(ws.path)
|
|
28
31
|
if (dirty && !msg) fail(`${dirty} uncommitted file(s); commit them (able commit), or pass a message to commit them all at once`)
|
|
@@ -31,27 +34,34 @@ if (dirty && !msg) fail(`${dirty} uncommitted file(s); commit them (able commit)
|
|
|
31
34
|
const keepOpen = args.includes('--keep-open')
|
|
32
35
|
const brief = join(ws.path, config.journal, 'workspaces', `${ws.slug}.md`)
|
|
33
36
|
if (!keepOpen && existsSync(brief) && !readFileSync(brief, 'utf8').includes('\n## Ledger\n'))
|
|
34
|
-
fail(`${relative(ws.path, brief)} has no ## Ledger; close the workspace first (the able-close
|
|
37
|
+
fail(`${relative(ws.path, brief)} has no ## Ledger; close the workspace first (the able-ws-close skill), then merge, or land what is done with --keep-open`)
|
|
35
38
|
|
|
36
39
|
const step = working(`merge ${ws.slug}`)
|
|
37
|
-
step(`fetching
|
|
38
|
-
fetchTrunk(ws.path, trunk)
|
|
40
|
+
step(`fetching origin/${trunk}`)
|
|
41
|
+
needOrigin(fetchTrunk(ws.path, trunk), trunk)
|
|
42
|
+
const carried = carriedFromLocal(ws.path, trunk)
|
|
43
|
+
if (carried.length)
|
|
44
|
+
fail(
|
|
45
|
+
`${ws.branch} holds ${carried.length} commit(s) from local ${trunk} that origin/${trunk} doesn't have; they would leave inside this merge:\n${carried
|
|
46
|
+
.map((c) => ` ${c}`)
|
|
47
|
+
.join('\n')}\n ${dim(`push them from the main checkout first (able push), then merge again`)}`,
|
|
48
|
+
)
|
|
39
49
|
const ahead = count(ws.path, `origin/${trunk}..HEAD`)
|
|
40
50
|
const behind = count(ws.path, `HEAD..origin/${trunk}`)
|
|
41
51
|
|
|
42
|
-
console.log(`\n ${bold(`Merge ${ws.branch} into
|
|
43
|
-
console.log(` ${ahead} commit(s) to merge${dirty ? `, +${dirty} uncommitted → "${msg}"` : ''};
|
|
52
|
+
console.log(`\n ${bold(`Merge ${ws.branch} into origin/${trunk}`)}`)
|
|
53
|
+
console.log(` ${ahead} commit(s) to merge${dirty ? `, +${dirty} uncommitted → "${msg}"` : ''}; origin/${trunk} is ${behind} commit(s) ahead${behind ? ', merged in first' : ''}`)
|
|
44
54
|
if (!ahead && !dirty) {
|
|
45
55
|
console.log(dim(' nothing to merge\n'))
|
|
46
56
|
status({ state: 'done', msg: `merge ${ws.slug}: nothing to merge` })
|
|
47
57
|
process.exit(0)
|
|
48
58
|
}
|
|
49
|
-
if (!args.includes('--yes') && !confirm(`Merge
|
|
59
|
+
if (!args.includes('--yes') && !confirm(`Merge origin/${trunk} in, run the checks, and push to origin/${trunk}?`)) cancel('aborted; nothing pushed')
|
|
50
60
|
|
|
51
61
|
const commitAll = (message: string) => gitLoud(ws.path, 'add', '-A') && gitLoud(ws.path, 'commit', '--quiet', '-m', message)
|
|
52
62
|
if (dirty && msg && !commitAll(msg)) fail('commit failed')
|
|
53
63
|
if (behind) {
|
|
54
|
-
step(`merging
|
|
64
|
+
step(`merging origin/${trunk} in`)
|
|
55
65
|
mergeOrFail(ws.path, `origin/${trunk}`, ws.branch, 'resolve them here, commit the merge, then run able ws merge again')
|
|
56
66
|
}
|
|
57
67
|
|
|
@@ -65,14 +75,14 @@ step(`pushing ${ws.branch}`)
|
|
|
65
75
|
if (!gitLoud(ws.path, 'push', '--quiet', '-u', 'origin', ws.branch)) fail(`push of ${ws.branch} failed`)
|
|
66
76
|
|
|
67
77
|
// The branch already holds the trunk, so this merge cannot conflict.
|
|
68
|
-
step(`pushing the merge to
|
|
78
|
+
step(`pushing the merge to origin/${trunk}`)
|
|
69
79
|
if (!gitLoud(ws.path, 'checkout', '--quiet', '--detach', `origin/${trunk}`) || !gitLoud(ws.path, 'merge', '--quiet', '--no-ff', '-m', `Merge ${ws.branch}`, ws.branch))
|
|
70
80
|
fail('could not build the merge commit')
|
|
71
81
|
const merged = git(ws.path, 'rev-parse', 'HEAD')
|
|
72
82
|
const pushed = gitLoud(ws.path, 'push', '--quiet', 'origin', `HEAD:${trunk}`)
|
|
73
83
|
gitLoud(ws.path, 'checkout', '--quiet', ws.branch)
|
|
74
|
-
if (!pushed) fail(
|
|
75
|
-
console.log(`\n ${green('✓')} ${bold(ws.branch)} is on
|
|
84
|
+
if (!pushed) fail(`origin/${trunk} moved meanwhile; run it again to merge it in`)
|
|
85
|
+
console.log(`\n ${green('✓')} ${bold(ws.branch)} is on origin/${trunk}`)
|
|
76
86
|
|
|
77
87
|
// `--ff-only` either moves the main checkout to the merge or refuses without touching it.
|
|
78
88
|
const onTrunk = git(ws.main, 'rev-parse', '--abbrev-ref', 'HEAD') === trunk
|
|
@@ -80,9 +90,15 @@ const synced = onTrunk && gitQuiet(ws.main, 'merge', '--quiet', '--ff-only', mer
|
|
|
80
90
|
console.log(
|
|
81
91
|
synced
|
|
82
92
|
? ` ${green('✓')} main checkout fast-forwarded`
|
|
83
|
-
: yellow(
|
|
93
|
+
: yellow(
|
|
94
|
+
!onTrunk
|
|
95
|
+
? ` main checkout not updated: it is not on ${trunk}; pull it there: git pull --ff-only`
|
|
96
|
+
: unpushedOnTrunk(ws.main, trunk).length
|
|
97
|
+
? ` main checkout not updated: local ${trunk} has commits origin/${trunk} doesn't; put them on top of it there: git pull --rebase`
|
|
98
|
+
: ` main checkout not updated: its own changes are in the way; pull it there: git pull --ff-only`,
|
|
99
|
+
),
|
|
84
100
|
)
|
|
85
101
|
console.log(
|
|
86
|
-
keepOpen ? `\n ${dim('the workspace stays open: keep working here, and close it (able-close
|
|
102
|
+
keepOpen ? `\n ${dim('the workspace stays open: keep working here, and close it (able-ws-close) before its last merge')}\n` : `\n ${dim('next, from the main checkout:')} able ws rm ${ws.slug}\n`,
|
|
87
103
|
)
|
|
88
|
-
status({ state: 'done', msg: `merge: ${ws.branch} is on
|
|
104
|
+
status({ state: 'done', msg: `merge: ${ws.branch} is on origin/${trunk}` })
|