@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.
Files changed (43) hide show
  1. package/able.ts +10 -2
  2. package/config.ts +11 -0
  3. package/package.json +1 -1
  4. package/src/audit/bloat/apply.test.ts +81 -0
  5. package/src/audit/bloat/apply.ts +49 -0
  6. package/src/{agents → audit/bloat}/blocks.ts +2 -2
  7. package/src/{agents → audit/bloat}/checks.ts +3 -3
  8. package/src/audit/bloat/compare.ts +63 -0
  9. package/src/{agents → audit/bloat}/findings.ts +11 -7
  10. package/src/{agents → audit/bloat}/grid.ts +6 -6
  11. package/src/audit/bloat/index.ts +258 -0
  12. package/src/{agents → audit/bloat}/load.ts +2 -2
  13. package/src/{agents → audit/bloat}/picker.ts +4 -4
  14. package/src/audit/bloat/report.ts +286 -0
  15. package/src/{agents → audit/bloat}/scan.ts +3 -3
  16. package/src/audit/bloat/show.ts +79 -0
  17. package/src/audit/bloat/steps.ts +32 -0
  18. package/src/{agents → audit/bloat}/types.ts +48 -3
  19. package/src/audit/index.ts +19 -0
  20. package/src/commit.ts +2 -1
  21. package/src/git.ts +43 -2
  22. package/src/push.ts +2 -1
  23. package/src/ui.ts +3 -0
  24. package/src/workspaces/fixture.ts +49 -0
  25. package/src/workspaces/index.ts +3 -1
  26. package/src/workspaces/ls.ts +56 -11
  27. package/src/workspaces/merge.ts +31 -15
  28. package/src/workspaces/new.ts +17 -15
  29. package/src/workspaces/overlap.test.ts +53 -0
  30. package/src/workspaces/overlap.ts +86 -0
  31. package/src/workspaces/preview.test.ts +81 -0
  32. package/src/workspaces/preview.ts +162 -0
  33. package/src/workspaces/refresh.ts +26 -24
  34. package/src/workspaces/rm.ts +6 -1
  35. package/src/workspaces/services.ts +2 -2
  36. package/src/workspaces/state.ts +56 -9
  37. package/src/workspaces/trunk.test.ts +88 -0
  38. package/sync.ts +5 -2
  39. package/src/agents/index.ts +0 -198
  40. package/src/agents/report.ts +0 -211
  41. package/src/agents/show.ts +0 -73
  42. package/src/agents/steps.ts +0 -32
  43. /package/src/{agents → audit/bloat}/kinds.ts +0 -0
@@ -7,21 +7,25 @@
7
7
  // the cut carries it. Already on origin as it is here: nothing to publish. Nowhere: refused, unless `--no-brief`.
8
8
  // Running it again after a failure picks up where it stopped: a brief committed but not pushed is pushed, since the
9
9
  // only unpushed changes it allows are the brief's own. `--brief`, the old way to ask for this, is still accepted.
10
+ //
11
+ // Other unpushed commits on `local <trunk>` are not in the new workspace, which is cut from `origin/<trunk>`: a warning
12
+ // names them. Only when publishing the brief would push them along is that a refusal.
10
13
 
11
- import { copyFileSync, existsSync, mkdirSync } from 'node:fs'
14
+ import { existsSync, mkdirSync } from 'node:fs'
12
15
  import { dirname, join, relative, resolve } from 'node:path'
13
16
  import { loadConfig, trailerArgs } from '../../config.ts'
14
17
  import { runChecks } from '../checks.ts'
15
- import { count, failOnMerge, fetchTrunk, git, gitLoud, gitQuiet, lines, mainRoot } from '../git.ts'
18
+ import { count, failOnMerge, fetchTrunk, git, gitLoud, gitQuiet, lines, mainRoot, needOrigin, unpushedOnTrunk, warnLeftOut } from '../git.ts'
16
19
  import { runCommand } from '../run.ts'
17
20
  import { status, working } from '../status.ts'
18
21
  import { bold, dim, fail, green, yellow } from '../ui.ts'
19
- import { branchOf, readState, workspaces, writeState } from './state.ts'
22
+ import { branchOf, copyFiles, freeOffset, PREVIEW, readState, workspaces, writeState } from './state.ts'
20
23
 
21
24
  const args = process.argv.slice(4)
22
25
  const slug = args.find((a) => !a.startsWith('-'))
23
26
  if (!slug) fail('usage: able ws new <slug> [--no-brief] [--no-fetch]')
24
27
  if (!/^[a-z0-9][a-z0-9-]*$/.test(slug)) fail(`invalid name '${slug}': lowercase letters, digits and dashes only`)
28
+ if (slug === PREVIEW || slug === 'main') fail(`'${slug}' is reserved: ${slug === PREVIEW ? 'it is the combined preview (able ws preview)' : 'it is the main checkout'}`)
25
29
 
26
30
  const main = mainRoot()
27
31
  const config = await loadConfig(main)
@@ -35,14 +39,13 @@ if (existsSync(path)) fail(`${path} already exists; remove it first`)
35
39
  if (git(main, 'rev-parse', '--verify', '--quiet', branch)) fail(`branch '${branch}' already exists`)
36
40
 
37
41
  // Without `ports` every workspace sits at offset 0 and only one runs services at a time (88, 94).
38
- const taken = new Set(Object.values(state.workspaces).map((w) => w.portOffset))
39
- const { ports } = ws
40
- const portOffset = ports ? (Array.from({ length: ports.max }, (_, i) => (i + 1) * ports.step).find((o) => !taken.has(o)) ?? fail('no free port offset; remove a workspace first')) : 0
42
+ const portOffset = freeOffset(state, ws.ports)
41
43
 
42
44
  const step = working(`new ${slug}`)
45
+ let fetched = true
43
46
  if (!args.includes('--no-fetch')) {
44
- step(`fetching ${config.trunk}`)
45
- fetchTrunk(main, config.trunk)
47
+ step(`fetching origin/${config.trunk}`)
48
+ fetched = fetchTrunk(main, config.trunk)
46
49
  }
47
50
 
48
51
  const { trunk, journal } = config
@@ -60,7 +63,8 @@ if (differs) {
60
63
  // Unpushed work other than the brief would leave with it. The brief's own commit (a run that stopped before its
61
64
  // push) is fine: this run pushes it.
62
65
  const others = lines(main, 'diff', '--name-only', `origin/${trunk}...${trunk}`).filter((f) => f !== file)
63
- if (others.length) fail(`${trunk} has unpushed changes that would leave with the brief:\n${git(main, 'log', '--oneline', `origin/${trunk}..${trunk}`)}`)
66
+ if (others.length) fail(`local ${trunk} has unpushed changes that would leave with the brief:\n${git(main, 'log', '--oneline', `origin/${trunk}..${trunk}`)}`)
67
+ needOrigin(fetched, trunk)
64
68
  if (git(main, 'status', '--porcelain', '--', file)) {
65
69
  const at = { slug: 'main', path: main, portOffset: 0, main }
66
70
  if (!runChecks(config, [file], at)) fail('a check failed; nothing committed')
@@ -69,7 +73,8 @@ if (differs) {
69
73
  if (count(main, `origin/${trunk}..${trunk}`) && !gitLoud(main, 'push', '--quiet', 'origin', trunk)) fail(`push of ${trunk} failed`)
70
74
  }
71
75
 
72
- // Cut from origin's trunk: the main checkout's own branch may lag, and is the operator's to move.
76
+ // Cut from origin's trunk: the main checkout's own branch may lag or lead, and is the operator's to move.
77
+ const left = differs ? [] : unpushedOnTrunk(main, trunk)
73
78
  const start = git(main, 'rev-parse', '--verify', '--quiet', `origin/${config.trunk}`) ? `origin/${config.trunk}` : config.trunk
74
79
 
75
80
  const started = Date.now()
@@ -81,11 +86,7 @@ if (added.exitCode !== 0) fail(`worktree add failed: ${added.stderr.toString().t
81
86
  state.workspaces[slug] = { portOffset, createdAt: new Date().toISOString() }
82
87
  await writeState(main, state)
83
88
 
84
- for (const rel of ws.copy ?? []) {
85
- if (!existsSync(join(main, rel))) continue
86
- mkdirSync(dirname(join(path, rel)), { recursive: true })
87
- copyFileSync(join(main, rel), join(path, rel))
88
- }
89
+ copyFiles(main, path, ws.copy)
89
90
 
90
91
  if (ws.onCreate?.length) step('onCreate commands')
91
92
  const failed = (ws.onCreate ?? []).filter((c) => !runCommand(c, { slug, path, portOffset, main }))
@@ -95,6 +96,7 @@ console.log(`\n${green('✓')} ${bold(slug)} ${dim(path)} ${dim(`in ${((Date.now
95
96
  console.log(` ${'branch'.padEnd(12)} ${branch} ${dim(`from ${start}`)}`)
96
97
  console.log(` ${'port offset'.padEnd(12)} ${portOffset}`)
97
98
  console.log(` ${'brief'.padEnd(12)} ${brief || yellow(`none: ${config.trunk} has no ${config.journal}/workspaces/${slug}.md`)}`)
99
+ warnLeftOut(left, `on local ${trunk} not in the new workspace: it is cut from origin/${trunk}, which doesn't have them`, 'push them (able push), then able ws refresh inside it, to include them')
98
100
  if (failed.length) console.log(yellow(`\n ${failed.length} onCreate command(s) failed; run them inside the workspace`))
99
101
  console.log(`\n ${dim(`cd ${relative(process.cwd(), path)}`)}\n`)
100
102
  status({ state: 'done', msg: `new: ${slug} is ready${failed.length ? `, ${failed.length} onCreate command(s) failed` : ''}` })
@@ -0,0 +1,53 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { commit, git, host, run } from './fixture.ts'
3
+
4
+ test('ws ls reports conflicts and shared files between open workspaces, writing nothing to the repository', () => {
5
+ const h = host()
6
+ for (const slug of ['one', 'two', 'three']) expect(run(h.main, 'ws', 'new', slug, '--no-brief').code).toBe(0)
7
+ commit(h.ws('one'), 'README.md', 'one\nTWO by one\nthree\n')
8
+ commit(h.ws('two'), 'README.md', 'one\nTWO by two\nthree\n')
9
+ commit(h.ws('one'), 'shared.txt', 'a\n')
10
+ commit(h.ws('three'), 'shared.txt', 'a\n')
11
+ commit(h.ws('three'), 'alone.txt', 'x\n')
12
+ // An uncommitted edit is not a committed tip, so it never counts.
13
+ commit(h.ws('two'), 'loose.txt', 'x\n')
14
+ Bun.write(`${h.ws('three')}/loose.txt`, 'y\n')
15
+
16
+ const before = git(h.main, 'count-objects', '-v')
17
+ const refs = git(h.main, 'for-each-ref')
18
+ const json = JSON.parse(run(h.main, 'ws', 'ls', '--json').out) as { slug: string; dirty: number; overlap: { with: string; conflicts: string[]; shared: string[] }[] }[]
19
+ expect(git(h.main, 'count-objects', '-v')).toBe(before)
20
+ expect(git(h.main, 'for-each-ref')).toBe(refs)
21
+
22
+ const of = (slug: string) => json.find((w) => w.slug === slug)?.overlap ?? []
23
+ expect(of('one')).toEqual([
24
+ { with: 'two', conflicts: ['README.md'], shared: ['README.md'] },
25
+ { with: 'three', conflicts: [], shared: ['shared.txt'] },
26
+ ])
27
+ expect(of('two')).toEqual([{ with: 'one', conflicts: ['README.md'], shared: ['README.md'] }])
28
+ expect(json.find((w) => w.slug === 'three')?.dirty).toBe(1)
29
+
30
+ // Against origin/main: a change origin/main makes to the same line conflicts with both.
31
+ commit(h.main, 'README.md', 'one\nTWO by main\nthree\n')
32
+ git(h.main, 'push', '--quiet', 'origin', 'main')
33
+ const after = JSON.parse(run(h.main, 'ws', 'ls', '--json').out) as typeof json
34
+ expect(after.find((w) => w.slug === 'one')?.overlap[0]).toEqual({ with: 'origin/main', conflicts: ['README.md'], shared: [] })
35
+
36
+ const table = run(h.main, 'ws', 'ls').out
37
+ expect(table).toContain('OVERLAP')
38
+ expect(table).toMatch(/one × two\s+1 conflict\s+README\.md/)
39
+ expect(table).toMatch(/one × three\s+1 shared\s+shared\.txt/)
40
+ })
41
+
42
+ test('ws ls still lists the workspaces when there is no origin/main to compare against', () => {
43
+ const h = host()
44
+ expect(run(h.main, 'ws', 'new', 'one', '--no-brief').code).toBe(0)
45
+ git(h.main, 'update-ref', '-d', 'refs/remotes/origin/main')
46
+ const table = run(h.main, 'ws', 'ls')
47
+ expect(table.code).toBe(0)
48
+ expect(table.out).toContain('one')
49
+ expect(table.out).toContain('not shown: no origin/main to compare against')
50
+ const json = run(h.main, 'ws', 'ls', '--json')
51
+ expect(json.code).toBe(0)
52
+ expect(JSON.parse(json.out)[0].overlap).toEqual([])
53
+ })
@@ -0,0 +1,86 @@
1
+ // Overlap between open workspaces (principle 4: shown, never resolved for the operator). Committed tips only: each
2
+ // workspace's tip against `origin/<trunk>`, and every pair against each other.
3
+ //
4
+ // conflicts `git merge-tree --write-tree`: the files a merge of the two would stop on
5
+ // shared files the files both change, from `git diff --name-only origin/<trunk>...<tip>` of each
6
+ //
7
+ // Nothing is written to the repository: merge-tree's trees and blobs go to a throwaway object directory, with the
8
+ // repository's own as an alternate, and no ref, index or worktree is touched.
9
+
10
+ import { mkdtempSync, rmSync } from 'node:fs'
11
+ import { tmpdir } from 'node:os'
12
+ import { join, resolve } from 'node:path'
13
+ import { git, lines } from '../git.ts'
14
+ import { fail } from '../ui.ts'
15
+
16
+ /** What one workspace overlaps with: `with` is another workspace's slug, or `origin/<trunk>`. */
17
+ export type Overlap = { with: string; conflicts: string[]; shared: string[] }
18
+
19
+ /** Why git can't run `git merge-tree --write-tree` (it needs 2.38), or nothing when it can. */
20
+ function mergeTreeTooOld() {
21
+ const [major = 0, minor = 0] = (git('.', '--version').match(/(\d+)\.(\d+)/) ?? []).slice(1).map(Number)
22
+ if (major < 2 || (major === 2 && minor < 38)) return `git ${major}.${minor} is too old: overlap and the preview need git 2.38 or newer (git merge-tree --write-tree)`
23
+ }
24
+
25
+ export function requireMergeTree() {
26
+ const old = mergeTreeTooOld()
27
+ if (old) fail(old)
28
+ }
29
+
30
+ /** Why overlap can't be worked out here (git too old, no `origin/<trunk>`), or nothing when it can. `able ws ls`
31
+ * skips it then, with the reason, and lists the checkouts anyway. */
32
+ export function overlapUnavailable(main: string, trunk: string) {
33
+ return mergeTreeTooOld() ?? (git(main, 'rev-parse', '--verify', '--quiet', `origin/${trunk}^{commit}`) ? undefined : `no origin/${trunk} to compare against`)
34
+ }
35
+
36
+ /** Where merge-tree writes when nothing may reach the repository: a scratch object directory, removed by `done`. */
37
+ export type Scratch = { env: Record<string, string>; done: () => void }
38
+
39
+ /** A scratch object directory with the repository's own objects as an alternate, so merge-tree can read them all. */
40
+ export function scratchObjects(main: string): Scratch {
41
+ const dir = mkdtempSync(join(tmpdir(), 'able-merge-tree-'))
42
+ const objects = resolve(main, git(main, 'rev-parse', '--git-path', 'objects'))
43
+ return { env: { GIT_OBJECT_DIRECTORY: dir, GIT_ALTERNATE_OBJECT_DIRECTORIES: objects }, done: () => rmSync(dir, { recursive: true, force: true }) }
44
+ }
45
+
46
+ /** `git merge-tree --write-tree a b`: the merged tree, and the files it conflicts on (none: a clean merge). Its
47
+ * objects go to `scratch` when given, else into the repository (the preview checks its tree out). */
48
+ export function mergeTree(main: string, a: string, b: string, scratch?: Scratch) {
49
+ const r = Bun.spawnSync(['git', '-C', main, 'merge-tree', '--write-tree', '--name-only', '--no-messages', a, b], {
50
+ stdout: 'pipe',
51
+ stderr: 'pipe',
52
+ env: { ...process.env, ...scratch?.env },
53
+ })
54
+ // 0: clean, 1: conflicts (the tree's id, then one line per conflicted file); anything else is an error.
55
+ if (r.exitCode !== 0 && r.exitCode !== 1) fail(`git merge-tree ${a} ${b}: ${r.stderr.toString().trim()}`)
56
+ const [tree = '', ...files] = r.stdout.toString().split('\n').filter(Boolean)
57
+ return { tree, conflicts: r.exitCode === 0 ? [] : [...new Set(files)] }
58
+ }
59
+
60
+ /** Every open workspace's overlap, by slug: against `origin/<trunk>` first, then against each other workspace.
61
+ * A tip is any ref: the workspace's branch. */
62
+ export function overlaps(main: string, trunk: string, tips: { slug: string; tip: string }[]): Record<string, Overlap[]> {
63
+ const base = `origin/${trunk}`
64
+ const scratch = scratchObjects(main)
65
+ const conflicts = (a: string, b: string) => mergeTree(main, a, b, scratch).conflicts
66
+ try {
67
+ const changed = Object.fromEntries(tips.map((t) => [t.slug, new Set(lines(main, 'diff', '--name-only', `${base}...${t.tip}`))]))
68
+ const out: Record<string, Overlap[]> = Object.fromEntries(tips.map((t) => [t.slug, []]))
69
+ for (const t of tips) {
70
+ const c = conflicts(base, t.tip)
71
+ if (c.length) out[t.slug]?.push({ with: base, conflicts: c, shared: [] })
72
+ }
73
+ tips.forEach((a, i) => {
74
+ for (const b of tips.slice(i + 1)) {
75
+ const shared = [...(changed[a.slug] ?? [])].filter((f) => changed[b.slug]?.has(f)).sort()
76
+ const c = conflicts(a.tip, b.tip)
77
+ if (!shared.length && !c.length) continue
78
+ out[a.slug]?.push({ with: b.slug, conflicts: c, shared })
79
+ out[b.slug]?.push({ with: a.slug, conflicts: c, shared })
80
+ }
81
+ })
82
+ return out
83
+ } finally {
84
+ scratch.done()
85
+ }
86
+ }
@@ -0,0 +1,81 @@
1
+ // The preview slot: `all`, rebuilt from origin/main and the open workspaces' committed tips, never answering as main.
2
+
3
+ import { expect, test } from 'bun:test'
4
+ import { existsSync, readFileSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { commit, git, host, run, write } from './fixture.ts'
7
+
8
+ test('ws preview combines the open workspaces, leaves out and names a conflicting one, and is a checkout of its own', () => {
9
+ const h = host()
10
+ for (const slug of ['one', 'two', 'three']) expect(run(h.main, 'ws', 'new', slug, '--no-brief').code).toBe(0)
11
+ commit(h.ws('one'), 'one.txt', 'from one\n')
12
+ commit(h.ws('two'), 'two.txt', 'from two\n')
13
+ commit(h.ws('one'), 'README.md', 'one\nTWO by one\nthree\n')
14
+ commit(h.ws('three'), 'README.md', 'one\nTWO by three\nthree\n')
15
+ write(h.ws('two'), 'uncommitted.txt', 'never in the preview\n')
16
+
17
+ const r = run(h.main, 'ws', 'preview')
18
+ expect(r.code).toBe(0)
19
+ expect(r.out).toMatch(/\+ one/)
20
+ expect(r.out).toMatch(/\+ two/)
21
+ expect(r.out).toMatch(/− three .* left out: conflicts with one/)
22
+ const all = h.ws('all')
23
+ expect(readFileSync(join(all, 'one.txt'), 'utf8')).toBe('from one\n')
24
+ expect(readFileSync(join(all, 'two.txt'), 'utf8')).toBe('from two\n')
25
+ expect(existsSync(join(all, 'uncommitted.txt'))).toBe(false)
26
+ // Detached, and no branch or ref of its own.
27
+ expect(git(all, 'rev-parse', '--abbrev-ref', 'HEAD')).toBe('HEAD')
28
+ expect(git(h.main, 'for-each-ref', '--format=%(refname)')).not.toContain('all')
29
+
30
+ // Its own checkout: slug, offset, and never main.
31
+ const at = JSON.parse(run(all, 'ws', 'current', '--json').out)
32
+ expect(at.slug).toBe('all')
33
+ expect(at.portOffset).toBeGreaterThan(0)
34
+ const offsets = (JSON.parse(run(h.main, 'ws', 'ls', '--json').out) as { slug: string; portOffset: number; kind?: string }[]).map((w) => w.portOffset)
35
+ expect(new Set(offsets).size).toBe(offsets.length)
36
+
37
+ // Read-only.
38
+ for (const cmd of [['commit', 'x', '--yes'], ['push'], ['ws', 'merge', '--yes'], ['ws', 'refresh']]) {
39
+ const refused = run(all, ...cmd)
40
+ expect(refused.code).not.toBe(0)
41
+ expect(refused.out).toContain('read-only')
42
+ }
43
+
44
+ // Named only, and a rebuild picks up new commits.
45
+ commit(h.ws('two'), 'two.txt', 'two, again\n')
46
+ expect(run(h.main, 'ws', 'preview', 'two').code).toBe(0)
47
+ expect(readFileSync(join(all, 'two.txt'), 'utf8')).toBe('two, again\n')
48
+ expect(existsSync(join(all, 'one.txt'))).toBe(false)
49
+
50
+ // Edits in the preview stop the rebuild, each named with its workspace.
51
+ write(all, 'two.txt', 'edited in the preview\n')
52
+ write(all, 'stray.txt', 'x\n')
53
+ const stopped = run(h.main, 'ws', 'preview')
54
+ expect(stopped.code).not.toBe(0)
55
+ expect(stopped.out).toMatch(/two\.txt\s+two/)
56
+ expect(stopped.out).toMatch(/stray\.txt\s+no workspace/)
57
+
58
+ // Reserved, and removable like any checkout.
59
+ expect(run(h.main, 'ws', 'new', 'all', '--no-brief').out).toContain('reserved')
60
+ expect(run(h.main, 'ws', 'rm', 'all', '--force').code).toBe(0)
61
+ expect(existsSync(all)).toBe(false)
62
+ })
63
+
64
+ test("a hook that rewrites a tracked file doesn't stop the next rebuild, and a preview whose folder is gone can be removed", () => {
65
+ const h = host()
66
+ expect(run(h.main, 'ws', 'new', 'one', '--no-brief').code).toBe(0)
67
+ commit(h.ws('one'), 'one.txt', 'from one\n')
68
+ // As an install rewrites a lockfile the combined tree disagrees with.
69
+ write(h.main, 'able.config.ts', `export default { trunk: 'main', journal: 'journal', workspaces: { dir: '../ws', ports: { step: 100, max: 9 }, onPreview: ['echo rewritten >> README.md'] } }\n`)
70
+
71
+ const first = run(h.main, 'ws', 'preview')
72
+ expect(first.code).toBe(0)
73
+ expect(first.out).toContain('put back what the hooks changed in tracked files: README.md')
74
+ expect(git(h.ws('all'), 'status', '--porcelain')).toBe('')
75
+ expect(run(h.main, 'ws', 'preview').code).toBe(0)
76
+
77
+ // Deleted by hand: `ws rm all` still clears its entry and frees its offset.
78
+ git(h.main, 'worktree', 'remove', '--force', h.ws('all'))
79
+ expect(run(h.main, 'ws', 'rm', 'all').code).toBe(0)
80
+ expect((JSON.parse(run(h.main, 'ws', 'ls', '--json').out) as { slug: string }[]).map((w) => w.slug)).toEqual(['one'])
81
+ })
@@ -0,0 +1,162 @@
1
+ // able ws preview [<slug>…]: the combined preview, from the main checkout. One checkout, slug `all`, in
2
+ // `workspaces.dir`, rebuilt on request as `origin/<trunk>` plus the open workspaces' committed work: every open one,
3
+ // oldest first, or the ones named. Combination is a view, not a branch: nothing in it is merged anywhere.
4
+ //
5
+ // 1. fetch, then chain `git merge-tree --write-tree` over `origin/<trunk>` and each workspace's committed tip,
6
+ // `git commit-tree` each step; a workspace that conflicts is left out, and named with whom it conflicts
7
+ // 2. in `all`: stop its services, check the commit out (detached), run `onCreate` when a lockfile changed since the
8
+ // last build (or on the first), then `onPreview` (Able's resets its Postgres), put back any tracked file the
9
+ // hooks rewrote, record the build, then start its services
10
+ //
11
+ // `all` has its own entry in `.able/workspaces.json` (`kind: 'preview'`, its offset, its last build), so its ports,
12
+ // database and host are its own. It is created on first use. Edits in it stop the rebuild: each changed file is
13
+ // named with the workspace that changes it, where the edit belongs.
14
+
15
+ import { existsSync, mkdirSync } from 'node:fs'
16
+ import { dirname, relative, resolve } from 'node:path'
17
+ import { loadConfig, urlOf } from '../../config.ts'
18
+ import { fetchTrunk, git, gitLoud, gitQuiet, lines, mainRoot } from '../git.ts'
19
+ import { runCommand, type Place } from '../run.ts'
20
+ import { status, working } from '../status.ts'
21
+ import { bold, dim, fail, green, red, yellow } from '../ui.ts'
22
+ import { mergeTree, requireMergeTree, scratchObjects } from './overlap.ts'
23
+ import { start, stop } from './services.ts'
24
+ import { copyFiles, current, freeOffset, openWorkspaces, PREVIEW, readState, workspaces, writeState, type Build } from './state.ts'
25
+
26
+ const LOCKFILES = /(^|\/)(bun\.lockb?|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|uv\.lock|poetry\.lock|Cargo\.lock|Gemfile\.lock|go\.sum)$/
27
+
28
+ const named = process.argv.slice(4).filter((a) => !a.startsWith('-'))
29
+ const main = mainRoot()
30
+ if (resolve((await current()).path) !== resolve(main)) fail('able ws preview runs in the main checkout')
31
+ const config = await loadConfig(main)
32
+ const { trunk } = config
33
+ const wsc = config.workspaces ?? fail('able.config.ts has no workspaces')
34
+ requireMergeTree()
35
+
36
+ const began = Date.now()
37
+ const step = working('preview')
38
+ step(`fetching origin/${trunk}`)
39
+ fetchTrunk(main, trunk)
40
+ const base = `origin/${trunk}`
41
+ const baseCommit = git(main, 'rev-parse', '--verify', '--quiet', `${base}^{commit}`) || fail(`no ${base}; push ${trunk} to origin first`)
42
+
43
+ const state = await readState(main)
44
+ const open = await openWorkspaces(main, state)
45
+ const unknown = named.filter((n) => !open.some((w) => w.slug === n))
46
+ if (unknown.length) fail(`no open workspace ${unknown.map((n) => `'${n}'`).join(', ')}; see able ws ls`)
47
+ const chosen = named.length ? open.filter((w) => named.includes(w.slug)) : open
48
+
49
+ const path = resolve(main, wsc.dir, PREVIEW)
50
+ const exists = (await workspaces(main, state)).some((w) => w.kind === 'preview' && existsSync(w.path))
51
+
52
+ // Edits in the preview belong in a workspace: name it for each file, and stop.
53
+ if (exists) {
54
+ const edited = lines(path, 'status', '--porcelain', '--untracked-files=all').map((l) => l.slice(3))
55
+ if (edited.length) {
56
+ const changes = Object.fromEntries(open.map((w) => [w.slug, new Set(lines(main, 'diff', '--name-only', `${base}...${w.branch}`))]))
57
+ const owner = (f: string) =>
58
+ open
59
+ .filter((w) => changes[w.slug]?.has(f))
60
+ .map((w) => w.slug)
61
+ .join(', ') || 'no workspace'
62
+ fail(
63
+ `${edited.length} uncommitted file(s) in the preview; it is rebuilt from committed work only, so make each change in its workspace:\n${edited
64
+ .map((f) => ` ${f.padEnd(40)} ${dim(owner(f))}`)
65
+ .join('\n')}\n ${dim(`then discard them (git -C ${relative(process.cwd(), path)} checkout -- . && git -C ${relative(process.cwd(), path)} clean -fd) and rebuild`)}`,
66
+ )
67
+ }
68
+ }
69
+
70
+ // ── The combined commit ─────────────────────────────────────────────────────────────────────────
71
+
72
+ step('combining')
73
+ let head = baseCommit
74
+ const included: Build['included'] = []
75
+ const left: Build['left'] = []
76
+ const scratch = scratchObjects(main)
77
+ try {
78
+ for (const w of chosen) {
79
+ const tip = git(main, 'rev-parse', w.branch)
80
+ const { tree, conflicts } = mergeTree(main, head, tip)
81
+ if (!conflicts.length) {
82
+ head = git(main, 'commit-tree', tree, '-p', head, '-p', tip, '-m', `Preview: ${w.branch} at ${tip.slice(0, 7)}`) || fail(`git commit-tree failed for ${w.slug}`)
83
+ included.push({ slug: w.slug, tip })
84
+ continue
85
+ }
86
+ // With whom: origin/<trunk>, then each workspace already in; failing both, the combination of them.
87
+ const against = [{ slug: base, tip: baseCommit }, ...included].filter((o) => mergeTree(main, o.tip, tip, scratch).conflicts.length).map((o) => o.slug)
88
+ left.push({ slug: w.slug, tip, with: against.length ? against : included.map((i) => i.slug) })
89
+ }
90
+ } finally {
91
+ scratch.done()
92
+ }
93
+
94
+ // ── The checkout ────────────────────────────────────────────────────────────────────────────────
95
+
96
+ const entry = state.workspaces[PREVIEW]
97
+ const portOffset = entry?.portOffset ?? freeOffset(state, wsc.ports)
98
+ const at: Place = { slug: PREVIEW, path, portOffset, main }
99
+ if (!exists) {
100
+ step('adding the preview checkout')
101
+ gitQuiet(main, 'worktree', 'prune')
102
+ if (existsSync(path)) fail(`${path} exists and is not the preview; remove it first`)
103
+ mkdirSync(dirname(path), { recursive: true })
104
+ const added = Bun.spawnSync(['git', '-C', main, 'worktree', 'add', '--detach', path, head], { stdout: 'pipe', stderr: 'pipe' })
105
+ if (added.exitCode !== 0) fail(`worktree add failed: ${added.stderr.toString().trim()}`)
106
+ copyFiles(main, path, wsc.copy)
107
+ state.workspaces[PREVIEW] = { portOffset, createdAt: new Date().toISOString(), kind: 'preview', path }
108
+ await writeState(main, state)
109
+ } else {
110
+ step('stopping its services')
111
+ await stop([], at)
112
+ if (!gitLoud(path, 'checkout', '--quiet', '--detach', head)) fail('could not check the combined commit out in the preview')
113
+ }
114
+
115
+ const previous = entry?.build?.commit
116
+ const installs = !exists || !previous || lines(main, 'diff', '--name-only', previous, head).some((f) => LOCKFILES.test(f))
117
+ const failed = new Set<string>()
118
+ if (installs && wsc.onCreate?.length) {
119
+ step('onCreate (a lockfile changed)')
120
+ for (const c of wsc.onCreate) if (!runCommand(c, at)) failed.add('onCreate')
121
+ }
122
+ if (wsc.onPreview?.length) {
123
+ step('onPreview')
124
+ for (const c of wsc.onPreview) if (!runCommand(c, at)) failed.add('onPreview')
125
+ }
126
+ // The hooks may rewrite tracked files (an install updating a lockfile the combined tree disagrees with). The preview
127
+ // holds committed work only, so put them back, or the next rebuild would refuse over edits nobody made.
128
+ const rewritten = lines(path, 'diff', '--name-only', 'HEAD')
129
+ if (rewritten.length) gitQuiet(path, 'checkout', '--quiet', 'HEAD', '--', '.')
130
+
131
+ // Recorded before the services start, so a service that fails to come up still leaves the record of what is checked
132
+ // out. Re-read: stopping services moves resource holders in the same file.
133
+ const after = await readState(main)
134
+ const build: Build = { at: new Date().toISOString(), commit: head, base: baseCommit, included, left }
135
+ // Written above on the first build, or there from an earlier one.
136
+ after.workspaces[PREVIEW] = { ...after.workspaces[PREVIEW]!, build }
137
+ await writeState(main, after)
138
+
139
+ const services = Object.keys(wsc.services ?? {})
140
+ if (services.length && !failed.size) {
141
+ step('starting its services')
142
+ await start([], at)
143
+ }
144
+
145
+ // ── What went in ────────────────────────────────────────────────────────────────────────────────
146
+
147
+ const short = (c: string) => c.slice(0, 7)
148
+ const width = Math.max(12, ...chosen.map((w) => w.slug.length + 2))
149
+ console.log(
150
+ `\n${green('✓')} ${bold(PREVIEW)} ${dim(relative(process.cwd(), path))} ${dim(`built in ${((Date.now() - began) / 1000).toFixed(1)}s, ${new Date(build.at).toLocaleTimeString('en-GB')}`)}\n`,
151
+ )
152
+ console.log(` ${'from'.padEnd(width)} ${base} ${dim(short(baseCommit))}`)
153
+ for (const i of included) console.log(` ${green('+')} ${i.slug.padEnd(width - 2)} ${dim(short(i.tip))}`)
154
+ for (const l of left) console.log(` ${red('−')} ${l.slug.padEnd(width - 2)} ${dim(short(l.tip))} ${red('left out')}: conflicts with ${l.with.join(', ')}`)
155
+ if (!chosen.length) console.log(dim(` no open workspaces: the preview is ${base} alone`))
156
+ if (wsc.url) console.log(`\n ${'url'.padEnd(width)} ${urlOf(wsc.url, PREVIEW, portOffset)}`)
157
+ console.log(` ${'port offset'.padEnd(width)} ${portOffset}`)
158
+ if (rewritten.length) console.log(dim(`\n put back what the hooks changed in tracked files: ${rewritten.join(', ')}`))
159
+ if (failed.size) console.log(yellow(`\n ${[...failed].join(' and ')} failed; see above, then run it again`))
160
+ else if (!services.length) console.log(dim(`\n able.config.ts names no services: start the app in ${relative(process.cwd(), path)} yourself`))
161
+ console.log()
162
+ status({ state: 'done', msg: `preview: ${included.length} workspace(s) in${left.length ? `, ${left.length} left out` : ''}` })
@@ -1,37 +1,39 @@
1
1
  // able ws refresh: catches this workspace up with the trunk, from inside it (decision 81).
2
2
  //
3
- // A worktree shares the main checkout's repository, so it sees the main checkout's own trunk as well as origin's. It
4
- // merges whichever holds the other (both, origin's first, when they have diverged), so work committed on the trunk
5
- // reaches a workspace without being pushed first. Refuses on uncommitted files.
3
+ // The trunk is `origin/<trunk>`, after a fetch: only what origin holds reaches a workspace. Commits on the main
4
+ // checkout's `local <trunk>` that aren't pushed are left out and named, so they never leave the machine inside this
5
+ // workspace's merge. Refuses on uncommitted files.
6
6
 
7
- import { count, dirtyCount, fetchTrunk, git, gitQuiet, mergeOrFail } from '../git.ts'
7
+ import { carriedFromLocal, count, dirtyCount, fetchTrunk, git, mergeOrFail, unpushedOnTrunk, warnLeftOut } from '../git.ts'
8
8
  import { status, working } from '../status.ts'
9
- import { bold, dim, fail, green, yellow } from '../ui.ts'
9
+ import { bold, dim, fail, green } from '../ui.ts'
10
10
  import { inWorkspace } from './state.ts'
11
11
 
12
- const { ws, config } = await inWorkspace()
12
+ const { ws, config } = await inWorkspace('able ws refresh')
13
13
  const { trunk } = config
14
14
  const dirty = dirtyCount(ws.path)
15
15
  if (dirty) fail(`${dirty} uncommitted file(s); commit them first, so the merge stays separable from your work`)
16
16
 
17
17
  const step = working(`refresh ${ws.slug}`)
18
- step(`fetching ${trunk}`)
18
+ step(`fetching origin/${trunk}`)
19
19
  fetchTrunk(ws.path, trunk)
20
- const contains = (outer: string, inner: string) => gitQuiet(ws.path, 'merge-base', '--is-ancestor', inner, outer)
21
- const local = git(ws.path, 'rev-parse', '--verify', '--quiet', trunk) ? trunk : ''
22
- const remote = git(ws.path, 'rev-parse', '--verify', '--quiet', `origin/${trunk}`) ? `origin/${trunk}` : ''
23
- const sources = !local ? [remote] : !remote || contains(local, remote) ? [local] : contains(remote, local) ? [remote] : [remote, local]
20
+ const remote = `origin/${trunk}`
21
+ if (!git(ws.path, 'rev-parse', '--verify', '--quiet', remote)) fail(`no ${remote}; push ${trunk} to origin first`)
24
22
 
25
- for (const source of sources.filter(Boolean)) {
26
- const behind = count(ws.path, `HEAD..${source}`)
27
- if (!behind) {
28
- console.log(dim(` ${source}: already merged`))
29
- continue
30
- }
31
- const unpushed = source === trunk && remote ? count(ws.path, `${remote}..${trunk}`) : 0
32
- console.log(` ${bold(source)}: ${behind} commit(s)${unpushed ? yellow(`; ${unpushed} not on origin yet, they reach it when this workspace is merged`) : ''}`)
33
- step(`merging ${source}`)
34
- mergeOrFail(ws.path, source, ws.branch, 'resolve them here and commit the merge, or abort it with git merge --abort')
35
- }
36
- console.log(`\n${green('✓')} ${bold(ws.branch)} holds ${trunk}\n`)
37
- status({ state: 'done', msg: `refresh: ${ws.branch} holds ${trunk}` })
23
+ const behind = count(ws.path, `HEAD..${remote}`)
24
+ if (behind) {
25
+ console.log(` ${bold(remote)}: ${behind} commit(s)`)
26
+ step(`merging ${remote}`)
27
+ mergeOrFail(ws.path, remote, ws.branch, 'resolve them here and commit the merge, or abort it with git merge --abort')
28
+ } else console.log(dim(` ${remote}: already merged`))
29
+
30
+ const left = unpushedOnTrunk(ws.path, trunk, 'HEAD')
31
+ warnLeftOut(left, `on local ${trunk} not included: they aren't on ${remote}, and only what origin holds reaches a workspace`, 'push them from the main checkout (able push), then refresh again')
32
+ // Taken in before the trunk rule: already in the branch, and `able ws merge` refuses them until origin has them.
33
+ warnLeftOut(
34
+ carriedFromLocal(ws.path, trunk),
35
+ `from local ${trunk} already in ${ws.branch}, not on ${remote}: able ws merge refuses them`,
36
+ 'push them from the main checkout (able push) before merging',
37
+ )
38
+ console.log(`\n${green('✓')} ${bold(ws.branch)} holds ${remote}\n`)
39
+ status({ state: 'done', msg: `refresh: ${ws.branch} holds ${remote}${left.length ? `; ${left.length} unpushed on local ${trunk} left out` : ''}` })
@@ -23,7 +23,8 @@ const ws = (await workspaces(main)).find((w) => w.slug === slug) ?? fail(`no wor
23
23
 
24
24
  if (!args.includes('--force') && existsSync(ws.path)) {
25
25
  const dirty = dirtyCount(ws.path)
26
- const ahead = unpushedCount(ws.path)
26
+ // A checkout without a branch (the preview) holds builds of other checkouts' work: only uncommitted edits are its own.
27
+ const ahead = ws.branch ? unpushedCount(ws.path) : 0
27
28
  const blockers = [dirty && `${dirty} uncommitted file(s)`, ahead && `${ahead} unpushed commit(s)`].filter(Boolean)
28
29
  if (blockers.length) fail(`${red('refusing to remove')} ${bold(slug)}: this work exists nowhere else: ${blockers.join(', ')}\n ${dim('push it, or --force to discard')}`)
29
30
  }
@@ -44,6 +45,10 @@ delete state.workspaces[slug]
44
45
  await writeState(main, state)
45
46
  console.log(`\n${green('✓')} removed ${bold(slug)}`)
46
47
 
48
+ if (!ws.branch) {
49
+ console.log()
50
+ process.exit(0)
51
+ }
47
52
  fetchTrunk(main, config.trunk)
48
53
  if (git(main, 'branch', '--merged', `origin/${config.trunk}`, '--list', ws.branch)) {
49
54
  gitQuiet(main, 'branch', '-D', ws.branch)
@@ -55,8 +55,8 @@ async function ready(s: Service, at: Place, timeoutMs = 120_000): Promise<boolea
55
55
  return false
56
56
  }
57
57
 
58
- export async function start(names: string[]) {
59
- const at = await current()
58
+ export async function start(names: string[], place?: Place) {
59
+ const at = place ?? (await current())
60
60
  const config = await loadConfig(at.path)
61
61
  const services = config.workspaces?.services ?? fail('able.config.ts has no workspaces.services')
62
62
  // Without `ports`, every checkout uses the same ports: only one runs services at a time (88, 94).