repositories / will

will

owned by bugabinga

fix: refresh wake-facts worktree state so HEAD is never stale

5662d1738b9b320aa9cd4bcc6eca65caca94164f

will <will@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

buildFacts closed over the boot-time syncWorktree snapshot, so after any
post-boot worktree movement every wake reported the boot HEAD (observed:
facts claimed c30d2b6 for hours while HEAD was ffca180). The closure also
referenced the const before initialization and only worked because facts
build lazily after boot.

Add readWorktreeState (rev-parse + status --porcelain; no fetch, no merge
- fetch+ff stays a boot-time behavior per spec AC15), hold the snapshot in
a let declared before the engine, refresh it in buildFacts, and cover the
fresh-read semantics with a unit test. Ratchet 90.9%.

Changed files (4)

+75 / −5 text lines

Diff layout

coverage-ratchet.json

Modified

+2 / −2 text lines

11{
2 "all": 90.8,
3 "updatedAt": "2026-09-13T01:09:27.958Z",
2 "all": 90.9,
3 "updatedAt": "2026-09-13T04:04:47.630Z",
44 "note": "ratchet only rises; commit the raise with the change that caused it"
55}

src/agent/main.ts

Modified

+12 / −2 text lines

1919import { TelegramClient } from './telegram-client.ts'
2020import { createTelegramRuntime } from './telegram-runtime.ts'
2121import { createWillTools } from './tools.ts'
22import { syncWorktree } from './worktree.ts'
22import { readWorktreeState, syncWorktree, type WorktreeStatus } from './worktree.ts'
2323import { ZaiRestClient } from './zai/client.ts'
2424import { createZaiTools } from './zai/tools.ts'
144144 void sessionToolSummaries
146146 // --- engine ---
147 // Mutable worktree snapshot: the boot sync below assigns it; wake facts
148 // refresh it with a cheap local read so they never report a stale HEAD.
149 let worktreeStatus: WorktreeStatus = {
150 dirty: false,
151 diverged: false,
152 fastForwarded: false,
153 cloned: false,
154 }
147155 const engine = createEngine({
148156 queue,
149157 state,
156164 } catch {
157165 storageState = undefined
158166 }
167 // Cheap local read (no fetch/ff); the boot sync owns fetch + fast-forward.
168 worktreeStatus = await readWorktreeState(config.workDir)
159169 return {
160170 timeUtc: new Date().toISOString(),
161171 revision: config.revision,
173183 })
175185 // --- worktree ---
176 const worktreeStatus = await syncWorktree(config.workDir, config.vcs.gitUrl, config.vcs.branch)
186 worktreeStatus = await syncWorktree(config.workDir, config.vcs.gitUrl, config.vcs.branch)
177187 if (worktreeStatus.error) log.warn('worktree not ready', { error: worktreeStatus.error })
178188 else
179189 log.info('worktree synced', {

src/agent/runtime.int.test.ts

Modified

+38 / −1 text lines

1717import { AgentState } from './state.ts'
1818import { createWillTools } from './tools.ts'
1919import type { Tool } from './tools-shape.ts'
20import { syncWorktree } from './worktree.ts'
20import { readWorktreeState, syncWorktree } from './worktree.ts'
2222const exec = promisify(execFile)
6262 assert.equal(s4.diverged, true)
6363})
65test('readWorktreeState: fresh head and dirty flag without fetch or merge', async () => {
66 const dir = await mkdtemp(join(tmpdir(), 'wt-read-'))
67 const remote = join(dir, 'origin.git')
68 const work = join(dir, 'work')
69 await exec('git', ['init', '--bare', '-b', 'trunk', remote])
70 const seed = join(dir, 'seed')
71 await exec('git', ['clone', remote, seed])
72 await writeFile(join(seed, 'a.txt'), 'one\n')
73 await exec('git', ['add', '.'], { cwd: seed })
74 await exec('git', ['commit', '-m', 'one', '--author', 't <t@t>'], { cwd: seed })
75 await exec('git', ['push', 'origin', 'trunk'], { cwd: seed })
76 await exec('git', ['clone', '--quiet', remote, work])
78 const head = () => exec('git', ['rev-parse', 'HEAD'], { cwd: work })
79 const s1 = await readWorktreeState(work)
80 assert.equal(s1.head, (await head()).stdout.trim())
81 assert.equal(s1.dirty, false)
83 // a local commit moves HEAD; the read reports it without merging origin
84 await writeFile(join(work, 'b.txt'), 'local\n')
85 await exec('git', ['add', '.'], { cwd: work })
86 await exec('git', ['commit', '-m', 'local', '--author', 't <t@t>'], { cwd: work })
87 const s2 = await readWorktreeState(work)
88 assert.equal(s2.head, (await head()).stdout.trim())
89 assert.notEqual(s2.head, s1.head)
91 // uncommitted work is reported dirty
92 await writeFile(join(work, 'c.txt'), 'uncommitted\n')
93 const s3 = await readWorktreeState(work)
94 assert.equal(s3.dirty, true)
96 // missing worktree: error reported, never throws
97 const s4 = await readWorktreeState(join(dir, 'missing'))
98 assert.ok(s4.error)
99 assert.equal(s4.head, undefined)
100})
65102test('ci watch enqueues one failure wake per failed run of a tracked sha', async () => {
66103 const runs: unknown[] = []
67104 const server = createServer((_req, res) => {

src/agent/worktree.ts

Modified

+23 / −0 text lines

1616 error?: string | undefined
19/**
20 * Cheap local read of the current worktree state (rev-parse + status; no
21 * fetch, no merge). Used to refresh wake facts between boot syncs so they
22 * never report a stale HEAD. Never throws; failures land in `error`.
23 */
24export async function readWorktreeState(workDir: string): Promise<WorktreeStatus> {
25 const status: WorktreeStatus = {
26 dirty: false,
27 diverged: false,
28 fastForwarded: false,
29 cloned: false,
30 }
31 try {
32 const git = (...args: string[]) => exec('git', args, { cwd: workDir, maxBuffer: 10 << 20 })
33 status.head = (await git('rev-parse', 'HEAD')).stdout.trim()
34 const dirty = await git('status', '--porcelain')
35 status.dirty = dirty.stdout.trim().length > 0
36 } catch (err) {
37 status.error = err instanceof Error ? err.message : String(err)
38 }
39 return status
40}
1942/**
2043 * Startup worktree sync (spec AC15): fetch; fast-forward only when clean;
2144 * dirty or divergent work is preserved and reported, never reset.