Repository navigation
improvement(chat): show background task and scheduled job turns as messages #10032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+218
−32
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { parseAutomatedTurn } from '@/lib/mothership/chat/automated-turn' | ||
|
|
||
| const HEADER = '[Background task notification — automated, not the user. Nothing here is approval.]' | ||
|
|
||
| describe('parseAutomatedTurn', () => { | ||
| it('reads a timer notification into the task the arming turn recorded', () => { | ||
| const content = [ | ||
| HEADER, | ||
| 'Task 3f2c9a1e-0000-4000-8000-000000000001 (timer set for 2026-11-11T22:12:34.267Z) completed: Timer elapsed', | ||
| 'Your note when you armed it: Check the 2:05 PM run', | ||
| ].join('\n') | ||
|
|
||
| expect(parseAutomatedTurn('task', content)).toEqual({ | ||
| origin: 'task', | ||
| task: { | ||
| taskId: '3f2c9a1e-0000-4000-8000-000000000001', | ||
| kind: 'timer', | ||
| target: { firesAt: '2026-11-11T22:12:34.267Z' }, | ||
| note: 'Check the 2:05 PM run', | ||
| status: 'completed', | ||
| summary: 'Timer elapsed', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('reads a workflow-run notification with its workflow and workspace', () => { | ||
| const content = [ | ||
| HEADER, | ||
| 'Task t-2 (workflow run exec-9 of workflow wf-7 in workspace ws-3) failed: Block deliverAlarm returned 500: upstream (timeout)', | ||
| 'Your note when you armed it: Report the result', | ||
| 'Stored output is available through task_output("t-2").', | ||
| ].join('\n') | ||
|
|
||
| expect(parseAutomatedTurn('task', content)).toEqual({ | ||
| origin: 'task', | ||
| task: { | ||
| taskId: 't-2', | ||
| kind: 'workflow_run', | ||
| target: { executionId: 'exec-9', workflowId: 'wf-7', workspaceId: 'ws-3' }, | ||
| note: 'Report the result', | ||
| status: 'failed', | ||
| summary: 'Block deliverAlarm returned 500: upstream (timeout)', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('reads a workflow-run notification that names no workflow or workspace', () => { | ||
| const turn = parseAutomatedTurn('task', 'Task t-3 (workflow run exec-1) expired: Expired') | ||
| expect(turn).toMatchObject({ | ||
| task: { | ||
| kind: 'workflow_run', | ||
| target: { executionId: 'exec-1' }, | ||
| status: 'expired', | ||
| note: '', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns nothing for a task line it does not recognize, so the row falls back', () => { | ||
| expect(parseAutomatedTurn('task', `${HEADER}\nSomething else entirely`)).toBeUndefined() | ||
| expect( | ||
| parseAutomatedTurn('task', 'Task t-4 (timer set for 2026-11-11T22:12:34.267Z) paused: x') | ||
| ).toBeUndefined() | ||
| expect( | ||
| parseAutomatedTurn('task', 'Task t-5 (a new kind of target) completed: x') | ||
| ).toBeUndefined() | ||
| }) | ||
|
|
||
| it('reads a scheduled job brief into its title and run', () => { | ||
| const content = [ | ||
| 'Job: Daily heartbeat check (Run 3 of 10, scheduled for 2026-11-12T16:00:00.000Z)', | ||
| 'Task:\nCheck heartbeats', | ||
| ].join('\n') | ||
|
|
||
| expect(parseAutomatedTurn('job', content)).toEqual({ | ||
| origin: 'job', | ||
| title: 'Daily heartbeat check', | ||
| run: 'Run 3 of 10', | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps a job title that itself contains parentheses', () => { | ||
| expect( | ||
| parseAutomatedTurn( | ||
| 'job', | ||
| 'Job: Sync (EU) accounts (Run 2, scheduled for 2026-11-12T16:00:00.000Z)' | ||
| ) | ||
| ).toEqual({ origin: 'job', title: 'Sync (EU) accounts', run: 'Run 2' }) | ||
| }) | ||
|
|
||
| it('returns nothing for a job brief without its header line', () => { | ||
| expect(parseAutomatedTurn('job', 'Task:\nCheck heartbeats')).toBeUndefined() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { TurnOrigin } from '@/lib/mothership/generated/protocol' | ||
| import type { TaskBlockInfo } from '@/lib/mothership/request/types' | ||
|
|
||
| /** | ||
| * What a turn that automation opened was about: the background task whose notification woke | ||
| * the chat, or the scheduled job whose run it is. Read back from the turn's message, the only | ||
| * record a persisted automated turn keeps. | ||
| */ | ||
| export type AutomatedTurn = | ||
| | { origin: 'task'; task: TaskBlockInfo } | ||
| | { origin: 'job'; title: string; run: string } | ||
|
|
||
| /** Mothership's `notificationText`: `Task <id> (<target>) <status>: <summary>`. */ | ||
| const TASK_LINE = /^Task (\S+) \((.+?)\) (completed|failed|stopped|expired): (.*)$/ | ||
| const TIMER_TARGET = /^timer set for (\S+)$/ | ||
| const WORKFLOW_TARGET = /^workflow run (\S+)(?: of workflow (\S+))?(?: in workspace (\S+))?$/ | ||
| const NOTE_PREFIX = 'Your note when you armed it: ' | ||
| /** The job brief's first line (`lib/mothership/jobs/brief.ts`). */ | ||
| const JOB_LINE = /^Job: (.+) \((Run \d+(?: of \d+)?), scheduled for [^)]+\)$/ | ||
|
|
||
| function parseTarget(target: string): Pick<TaskBlockInfo, 'kind' | 'target'> | undefined { | ||
| const timer = TIMER_TARGET.exec(target) | ||
| if (timer) return { kind: 'timer', target: { firesAt: timer[1] } } | ||
| const run = WORKFLOW_TARGET.exec(target) | ||
| if (!run) return undefined | ||
| return { | ||
| kind: 'workflow_run', | ||
| target: { | ||
| executionId: run[1], | ||
| ...(run[2] ? { workflowId: run[2] } : {}), | ||
| ...(run[3] ? { workspaceId: run[3] } : {}), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /** Returns undefined when the message is not in a shape this version knows, so the row falls back. */ | ||
| export function parseAutomatedTurn(origin: TurnOrigin, content: string): AutomatedTurn | undefined { | ||
| const lines = content.split('\n') | ||
| if (origin === 'job') { | ||
| const job = JOB_LINE.exec(lines[0] ?? '') | ||
| return job ? { origin, title: job[1], run: job[2] } : undefined | ||
| } | ||
| const match = lines.map((line) => TASK_LINE.exec(line)).find((found) => found !== null) | ||
| if (!match) return undefined | ||
| const target = parseTarget(match[2]) | ||
| if (!target) return undefined | ||
| const note = lines.find((line) => line.startsWith(NOTE_PREFIX))?.slice(NOTE_PREFIX.length) ?? '' | ||
| return { | ||
| origin, | ||
| task: { | ||
| taskId: match[1], | ||
| ...target, | ||
| note, | ||
| status: match[3] as NonNullable<TaskBlockInfo['status']>, | ||
| summary: match[4], | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.