Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(muse): a scheduled run is a turn, even though nobody typed its prompt

0xdiego2570a897e52bb3
diff --git a/cli/src/engines/muse/normalizer.spec.ts b/cli/src/engines/muse/normalizer.spec.ts
index ea4f30922..08e7bc9ff 100644
--- a/cli/src/engines/muse/normalizer.spec.ts
+++ b/cli/src/engines/muse/normalizer.spec.ts
@@ -156,7 +156,9 @@ describe('muse device rendering — the three lists the device draws', () => {
* so the device tile spins "Processing" forever and no recap is ever produced.
*/
describe('muse sub-agent lifecycle', () => {
- const rec = (event: unknown) => JSON.stringify({ payload: { event } })
+ // Every real record carries `payload.kind` — the scope the event belongs to. This fixture used to omit
+ // it, which is why a `started` that is NOT a turn (scope `task`) looked identical here to one that is.
+ const rec = (event: unknown, scope = 'run') => JSON.stringify({ payload: { kind: scope, event } })
const call = (callId: string, name: string, args: unknown) =>
rec({ kind: 'assistant_tool_calls_committed', tool_calls: [{ call_id: callId, name, args: JSON.stringify(args) }] })
const result = (callId: string, body: unknown) =>
@@ -193,6 +195,31 @@ describe('muse sub-agent lifecycle', () => {
.toEqual({ id: 'c1', status: 'failed' })
})
+ it('opens a turn for a SCHEDULED run, whose prompt is empty because nobody typed one', () => {
+ // A reminder fires with `payload.kind:'run'`, `started`, `prompt:''`. Requiring a non-empty prompt
+ // meant no turn opened, so the answer belonged to nothing and never reached the device — measured on
+ // a five-minute BTC reminder that ran four times and reported none of them.
+ const n = new MuseNormalizer()
+ const events = feed(n, [
+ rec({ kind: 'started', prompt: '' }),
+ rec({ kind: 'assistant_message_committed', text: 'Báo giá Bitcoin — 14:02' }),
+ rec({ kind: 'terminal', terminal: 'completed', turn_duration_ms: 14000 }),
+ ])
+ const types = events.map((e) => e.type)
+ expect(types).toContain('turn_started')
+ expect(types).toContain('turn_ended')
+ expect(n.turnOpen).toBe(false)
+ })
+
+ it('does NOT take a task lifecycle start for a turn', () => {
+ // `task.started` fires several times inside ONE run (measured: 30 of them across four reminder runs).
+ // Treating them as turns would open and re-open a turn per task and shred the real one.
+ const n = new MuseNormalizer()
+ const events = feed(n, [rec({ kind: 'started', task_id: 't-1' }, 'task')])
+ expect(events).toEqual([])
+ expect(n.turnOpen).toBe(false)
+ })
+
it('releases the turn when muse ends it with children nobody waited on', () => {
// THE BUG: measured 7 spawns / 4 waits in one session. muse ends the main turn regardless, so a row
// nobody closed pinned the tile busy forever with no recap. `terminal` is the backstop.
diff --git a/cli/src/engines/muse/normalizer.ts b/cli/src/engines/muse/normalizer.ts
index c09257aba..ebf412f40 100644
--- a/cli/src/engines/muse/normalizer.ts
+++ b/cli/src/engines/muse/normalizer.ts
@@ -82,15 +82,31 @@ export function clip(value: string, limit: number): string {
return value.length > limit ? `${value.slice(0, limit)}…` : value
}
-/** The `payload.event` of one record, or null for the envelopes that carry no event. */
-export function museEvent(line: string): JsonObject | null {
+/** One record's event plus the SCOPE it belongs to. */
+export interface MuseRecord {
+ /** `payload.kind` — `run`, `task`, `tool_batch_effect`, … */
+ scope: string
+ event: JsonObject
+}
+
+/**
+ * The `payload.event` of one record, or null for the envelopes that carry no event.
+ *
+ * The scope comes with it, and it is load-bearing: `run` and `task` are DIFFERENT lifecycles that share
+ * event names. Both emit `started`, and only a run's is a turn — a `task.started` is scheduler
+ * bookkeeping and several fire inside one run. This used to drop `payload.kind`, which left `started`
+ * ambiguous; the only thing separating the two was that a task carries no prompt. That held right up
+ * until a SCHEDULED run arrived — a real turn whose prompt is empty, because nobody typed anything.
+ */
+export function museEvent(line: string): MuseRecord | null {
let raw: JsonObject | null
try { raw = obj(JSON.parse(line)) } catch { return null }
if (!raw) return null
const payload = obj(raw.payload)
if (!payload) return null
const event = obj(payload.event)
- return event && str(event.kind) ? event : null
+ if (!event || !str(event.kind)) return null
+ return { scope: str(payload.kind), event }
}
/** `workspace_root` from a session's first record — the only thing tying a session to a directory. */
@@ -127,14 +143,22 @@ function resultBody(text: string): JsonObject | null {
}
/** One record → events. `mode: 'replay'` emits `user_message` instead of deriving the turn lifecycle. */
-export function museEventToEvents(event: JsonObject, state: MuseTurnState, mode: 'live' | 'replay'): LiveEvent[] {
+export function museEventToEvents(record: MuseRecord, state: MuseTurnState, mode: 'live' | 'replay'): LiveEvent[] {
+ const { scope, event } = record
const kind = str(event.kind)
const events: LiveEvent[] = []
if (kind === 'started') {
+ // ONLY a run start is a turn. `task.started` fires several times inside a single run — taking it for
+ // a turn would open, and immediately re-open, one per task.
+ if (scope !== 'run') return []
const prompt = str(event.prompt).trim()
- if (!prompt) return []
- if (mode === 'replay') return [{ type: 'user_message', payload: { content: prompt } }]
+ // An EMPTY prompt is normal and still opens the turn: a SCHEDULED run has no typed message, because
+ // the scheduler triggered it rather than a person. Bailing here is why a reminder's output never
+ // reached the device — no turn opened, so the assistant text belonged to nothing and `terminal`
+ // closed a turn that was never there. Replay is the exception: an empty prompt would draw an empty
+ // user bubble, so it contributes no message and simply renders the assistant side.
+ if (mode === 'replay') return prompt ? [{ type: 'user_message', payload: { content: prompt } }] : []
if (state.open) events.push({ type: 'turn_ended', payload: {} })
state.open = true
state.pendingTools.clear()
@@ -275,8 +299,8 @@ export class MuseNormalizer implements EngineNormalizer {
}
ingest(line: string): LiveEvent[] {
- const event = museEvent(line)
- return event ? museEventToEvents(event, this.state, 'live') : []
+ const record = museEvent(line)
+ return record ? museEventToEvents(record, this.state, 'live') : []
}
finishReplay(): LiveEvent[] {
@@ -289,8 +313,8 @@ export function museMessagesToEvents(lines: string[]): SessionEvent[] {
const state = newMuseTurnState()
const events: SessionEvent[] = []
for (const line of lines) {
- const event = museEvent(line)
- if (event) events.push(...museEventToEvents(event, state, 'replay') as SessionEvent[])
+ const record = museEvent(line)
+ if (record) events.push(...museEventToEvents(record, state, 'replay') as SessionEvent[])
}
events.push({ type: 'done', payload: { result: 'success' } })
return events
@@ -301,12 +325,16 @@ export function lastMuseTurnText(lines: string[]): LastTurnText | null {
let userMessage = ''
let assistantText = ''
for (const line of lines) {
- const event = museEvent(line)
- if (!event) continue
+ const record = museEvent(line)
+ if (!record) continue
+ const event = record.event
const kind = str(event.kind)
- if (kind === 'started') {
- const prompt = str(event.prompt).trim()
- if (prompt) { userMessage = prompt; assistantText = '' }
+ if (kind === 'started' && record.scope === 'run') {
+ // Reset on EVERY run start, prompt or not. Keying the reset on a non-empty prompt meant a series of
+ // scheduled runs never reset it, so the recap of the 4th five-minute reminder carried all four
+ // answers glued together instead of the latest one.
+ userMessage = str(event.prompt).trim()
+ assistantText = ''
} else if (kind === 'assistant_message_committed') {
const text = str(event.text).trim()
if (text) assistantText += `${assistantText ? '\n\n' : ''}${text}`