Skip to content

Commit 782125f

Browse files
authored
feat(app): show where automations run and which session they wake (#66393)
1 parent c39ad3b commit 782125f

22 files changed

Lines changed: 1431 additions & 10 deletions

File tree

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
import type { AutomationInfo } from "@opencode/client/promise"
2+
import { expect, test } from "@playwright/test"
3+
import { project, provider, REMOTE_SERVER, seed, SERVER, sessionHref } from "../utils/app"
4+
import { mockServers } from "../utils/mock-server"
5+
import { mockWorkspace } from "../utils/workspace"
6+
7+
test("inspects event and timer automations and manages the selected row", async ({ page }) => {
8+
await page.setViewportSize({ width: 1440, height: 900 })
9+
const directory = "C:/OpenCode/Atlas"
10+
const now = Date.UTC(2026, 10, 17, 9, 10)
11+
12+
const base = {
13+
projectID: "proj_atlas",
14+
directory: `${directory}/release`,
15+
tz: "UTC",
16+
sessionID: "ses_release",
17+
status: "active",
18+
runCount: 3,
19+
time: { created: now, updated: now },
20+
} as const
21+
22+
const automations: AutomationInfo[] = [
23+
{
24+
...base,
25+
id: "aut_deploy",
26+
name: "Release health watch",
27+
trigger: { type: "event", server: "deployments", event: "deployment.finished", args: { environment: "staging" } },
28+
action: {
29+
type: "code",
30+
code: 'console.log(`Deployment status: ${event.data.status}`)\nif (event.data.status === "failed")\n return { send: { prompt: "Review the failed deployment." } }',
31+
},
32+
lastRunAt: now,
33+
lastOutcome: "fired",
34+
lastOutput: "Deployment status: succeeded",
35+
},
36+
{
37+
...base,
38+
id: "aut_brief",
39+
name: "Morning release brief",
40+
trigger: { type: "cron", cron: "0 9 * * 1-5" },
41+
action: { type: "prompt", prompt: "Summarize release activity since yesterday.", to: { session: "ses_brief" } },
42+
nextDueAt: now + 86_400_000,
43+
},
44+
{
45+
...base,
46+
id: "aut_smoke",
47+
name: "Nightly smoke test",
48+
trigger: { type: "cron", cron: "0 2 * * *" },
49+
action: { type: "shell", command: "bun run test:smoke --reporter=dot" },
50+
nextDueAt: now + 2 * 86_400_000,
51+
lastRunAt: now,
52+
lastOutcome: "fired",
53+
lastOutput: "Exit code: 0\n42 pass, 0 fail",
54+
},
55+
{
56+
...base,
57+
id: "aut_docs",
58+
name: "Documentation review",
59+
status: "paused",
60+
trigger: { type: "every", every: "1 day" },
61+
action: { type: "prompt", prompt: "Review the documentation changes.", to: { subagent: "reviewer" } },
62+
},
63+
]
64+
65+
await mockWorkspace(page, {
66+
name: "Atlas",
67+
directory,
68+
project: { name: "Atlas (sample)" },
69+
hostname: "release-runner-03",
70+
commands: [{ name: "automation" }],
71+
sessions: [
72+
{ id: "ses_release", title: "Release planning" },
73+
{ id: "ses_brief", title: "Release notes" },
74+
],
75+
automations,
76+
seed: {
77+
theme: { id: "oc-2", scheme: "dark" },
78+
settings: { general: { automations: true }, appearance: { tabLayout: "vertical", sidebarOpen: true } },
79+
},
80+
})
81+
await page.goto(sessionHref("ses_release"))
82+
const nav = page.getByRole("button", { name: "Automations", exact: true })
83+
await nav.click()
84+
await expect(page.getByRole("button", { name: /Morning release brief/ })).toContainText("Weekdays at 9:00 AM")
85+
const detail = page.getByRole("article")
86+
await page.getByRole("button", { name: /Release health watch/ }).click()
87+
await expect(detail.getByRole("heading", { name: "Release health watch" })).toBeVisible()
88+
await expect(detail.getByText("deployment.finished from deployments", { exact: true })).toBeVisible()
89+
await expect(
90+
detail
91+
.getByRole("term")
92+
.filter({ hasText: /^Runs on$/ })
93+
.locator("+ dd"),
94+
).toHaveText("release-runner-03")
95+
await expect(detail.getByText('environment = "staging"', { exact: true })).toBeVisible()
96+
await expect(detail.getByText("Next run")).toHaveCount(0)
97+
await expect(
98+
detail.getByText("Wakes the creating session only when the code returns send", { exact: false }),
99+
).toBeVisible()
100+
await expect(detail.getByText(`${directory}/release`, { exact: true })).toBeVisible()
101+
await expect(detail.getByText("Deployment status: succeeded", { exact: true })).toBeVisible()
102+
await page.screenshot({ path: test.info().outputPath("automation-inspector.png"), animations: "disabled" })
103+
await detail.getByRole("button", { name: "Pause", exact: true }).click()
104+
await expect(nav).toContainText("2")
105+
await detail.getByRole("button", { name: "Resume", exact: true }).click()
106+
await expect(nav).toContainText("3")
107+
await detail.getByRole("button", { name: "Delete", exact: true }).click()
108+
await page.getByRole("dialog").getByRole("button", { name: "Delete", exact: true }).click()
109+
await expect(page.getByRole("button", { name: /Release health watch/ })).toHaveCount(0)
110+
await expect(detail.getByText("Next run")).toBeVisible()
111+
await expect(detail.getByRole("button", { name: "Release planning" })).toBeVisible()
112+
await expect(detail.getByRole("button", { name: "Release notes" })).toBeVisible()
113+
await page.getByRole("button", { name: /Nightly smoke test/ }).click()
114+
await expect(detail.getByText("bun run test:smoke --reporter=dot", { exact: true })).toBeVisible()
115+
await expect(detail.getByText("Doesn’t wake a session", { exact: false })).toBeVisible()
116+
await page.getByRole("button", { name: /Documentation review/ }).click()
117+
await expect(detail.getByText("Send a prompt to a new reviewer subagent")).toBeVisible()
118+
await page.getByRole("button", { name: "New automation" }).click()
119+
await expect(page.locator('[data-component="composer-editor"]')).toHaveText(/\/automation/)
120+
})
121+
122+
test("hides every automation entry point while the experimental setting is off", async ({ page }) => {
123+
await mockWorkspace(page, { name: "Off", commands: [{ name: "automation" }] })
124+
await page.goto("/automations")
125+
await expect(page.getByText("Turn on Automations in Settings → Experimental.")).toBeVisible()
126+
await expect(page.getByRole("button", { name: "Automations", exact: true })).toHaveCount(0)
127+
await expect(page.getByRole("button", { name: "New automation" })).toHaveCount(0)
128+
})
129+
130+
test("keeps the entry for an empty server and withdraws New automation when the server removes its command", async ({
131+
page,
132+
}) => {
133+
let enabled = true
134+
135+
const { push, directory } = await mockWorkspace(page, {
136+
name: "Empty",
137+
commands: () => (enabled ? [{ name: "automation" }] : []),
138+
seed: { settings: { general: { automations: true } } },
139+
})
140+
141+
await page.goto("/automations")
142+
await expect(page.getByRole("heading", { name: "No automations yet" })).toBeVisible()
143+
enabled = false
144+
await push([
145+
{ id: "evt_automation_disabled", created: 1, type: "command.updated", location: { directory }, data: {} },
146+
])
147+
await expect(page.getByRole("button", { name: "New automation" })).toHaveCount(0)
148+
await expect(page.getByRole("heading", { name: "No automations yet" })).toBeVisible()
149+
await expect(page.getByRole("button", { name: "Automations", exact: true })).toBeVisible()
150+
})
151+
152+
test("manages a watch outside open locations and opens the recipient instead of its last viewed child", async ({
153+
page,
154+
}) => {
155+
const directory = "/projects/repo"
156+
const docs = `${directory}/docs`
157+
await mockWorkspace(page, {
158+
name: "Repo",
159+
directory,
160+
commands: (dir) => (dir === docs ? [{ name: "automation" }] : []),
161+
automations: [
162+
{
163+
id: "aut_docs",
164+
projectID: "proj_repo",
165+
directory: docs,
166+
name: "Review docs",
167+
tz: "UTC",
168+
trigger: { type: "every", every: "1 day" },
169+
action: { type: "prompt", prompt: "Review new documentation.", to: { session: "ses_parent" } },
170+
status: "active",
171+
runCount: 0,
172+
time: { created: 1, updated: 1 },
173+
},
174+
],
175+
sessions: [
176+
{ id: "ses_parent", title: "Docs planning" },
177+
{ id: "ses_child", title: "Research notes", parentID: "ses_parent" },
178+
],
179+
seed: {
180+
settings: { general: { automations: true } },
181+
storage: {
182+
"opencode.window.browser.dat:tabs": [
183+
{
184+
type: "session",
185+
sessionId: "ses_parent",
186+
server: SERVER,
187+
routeSessionId: "ses_child",
188+
routeParentId: "ses_parent",
189+
},
190+
],
191+
},
192+
},
193+
})
194+
await page.goto(sessionHref("ses_child"))
195+
await expect(page.getByRole("heading", { name: "Research notes", exact: true })).toBeVisible()
196+
await page.getByRole("button", { name: "Automations", exact: true }).click()
197+
const detail = page.getByRole("article")
198+
await expect(detail.getByRole("heading", { name: "Review docs" })).toBeVisible()
199+
await expect(detail.getByText(docs, { exact: true })).toBeVisible()
200+
await expect(page.getByRole("button", { name: "New automation" })).toBeVisible()
201+
await detail.getByRole("button", { name: /Docs planning|ses_parent/ }).click()
202+
await expect(page).toHaveURL(sessionHref("ses_parent"))
203+
await expect(page.locator('[data-component="composer-editor"]')).toBeEditable()
204+
})
205+
206+
test("keeps identically keyed automations on separate servers and starts the draft in the row’s saved directory", async ({
207+
page,
208+
}) => {
209+
const servers = [SERVER, REMOTE_SERVER] as const
210+
const creates: unknown[] = []
211+
const commands: unknown[] = []
212+
await mockServers(
213+
page,
214+
Object.fromEntries(
215+
servers.map((server) => {
216+
const name = server === SERVER ? "Local" : "Remote"
217+
const directory = `/projects/${name.toLowerCase()}`
218+
219+
const row: AutomationInfo = {
220+
id: "aut_brief",
221+
name: `${name} brief`,
222+
projectID: "proj_shared",
223+
directory: `${directory}/worktrees/docs`,
224+
status: "active",
225+
runCount: 0,
226+
trigger: { type: "at", at: "2027-01-04T09:00" },
227+
tz: "UTC",
228+
action: { type: "prompt", prompt: "Review the release notes.", to: { session: "ses_notes" } },
229+
time: { created: 1, updated: 1 },
230+
}
231+
232+
return [
233+
server,
234+
{
235+
directory,
236+
project: project({ id: row.projectID, directory, name: `${name} project` }),
237+
provider: provider(),
238+
pageMessages: () => ({ items: [] }),
239+
onSessionCreate: (body: { location?: unknown }) => void creates.push({ server, location: body.location }),
240+
onCommand: (input: { body: unknown }) => void commands.push({ server, body: input.body }),
241+
sessions: [],
242+
commands: [{ name: "automation" }],
243+
automations: [row],
244+
},
245+
]
246+
}),
247+
),
248+
)
249+
await seed(page, {
250+
settings: { general: { automations: true } },
251+
servers: [REMOTE_SERVER],
252+
usedProjects: { local: ["proj_shared"], [REMOTE_SERVER]: ["proj_shared"] },
253+
})
254+
await page.goto("/automations")
255+
await page.getByRole("button", { name: /Remote brief/ }).click()
256+
const detail = page.getByRole("article")
257+
await expect(detail.getByText("Remote project", { exact: true })).toBeVisible()
258+
await expect(detail.getByText("Created by")).toHaveCount(0)
259+
await expect(detail.getByRole("button", { name: "ses_notes" })).toBeVisible()
260+
const directory = "/projects/remote/worktrees/docs"
261+
await expect(detail.getByText(directory, { exact: true })).toBeVisible()
262+
await page.getByRole("button", { name: "New automation" }).click()
263+
const editor = page.locator('[data-component="composer-editor"]')
264+
await expect(editor).toHaveText(/\/automation/)
265+
await editor.fill("/automation Review the release notes on Friday")
266+
await editor.press("Enter")
267+
await expect.poll(() => creates).toEqual([{ server: REMOTE_SERVER, location: { directory } }])
268+
await expect
269+
.poll(() => commands)
270+
.toMatchObject([
271+
{ server: REMOTE_SERVER, body: { name: "automation", text: "Review the release notes on Friday" } },
272+
])
273+
})

‎packages/app/e2e/utils/mock-api.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ const Group = HttpApiGroup.make("mock")
242242
.add(HttpApiEndpoint.get("usageLimits", "/api/experimental/usage/limits", { success: Json }))
243243
.add(HttpApiEndpoint.get("flagList", "/api/experimental/flag", { success: Json }))
244244
.add(HttpApiEndpoint.get("jobList", "/api/experimental/job", { success: Json }))
245+
.add(
246+
HttpApiEndpoint.get("automationList", "/api/experimental/automation", {
247+
query: Schema.Struct({ sessionID: Schema.optional(Schema.String) }),
248+
success: Json,
249+
}),
250+
)
251+
.add(
252+
HttpApiEndpoint.post("automationPause", "/api/experimental/automation/:automationID/pause", {
253+
params: Schema.Struct({ automationID: Schema.String }),
254+
success: Json,
255+
error: [MockNotFound],
256+
}),
257+
)
258+
.add(
259+
HttpApiEndpoint.post("automationResume", "/api/experimental/automation/:automationID/resume", {
260+
params: Schema.Struct({ automationID: Schema.String }),
261+
success: Json,
262+
error: [MockNotFound],
263+
}),
264+
)
265+
.add(
266+
HttpApiEndpoint.delete("automationDelete", "/api/experimental/automation/:automationID", {
267+
params: Schema.Struct({ automationID: Schema.String }),
268+
success: NoContent,
269+
error: [MockNotFound],
270+
}),
271+
)
245272
.add(
246273
HttpApiEndpoint.get("worktreeList", "/api/worktree", {
247274
query: Schema.Struct({ projectID: Schema.String }),

‎packages/app/e2e/utils/mock-server.ts‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Page } from "@playwright/test"
2-
import type { OpenCodeEvent, SessionMessageInfo } from "@opencode/client/promise"
2+
import type { OpenCodeEvent, AutomationInfo, SessionMessageInfo } from "@opencode/client/promise"
33
import { Permission } from "@opencode/schema/permission"
44
import { artifactKind, artifactMime } from "@opencode/util/artifact"
55
import { Model } from "@opencode/schema/model"
@@ -151,7 +151,7 @@ export interface MockServerConfig {
151151
// Title of created sessions (default: the request's title, else "New session").
152152
createdSessionTitle?: string
153153
// Slash commands served by `/api/command`.
154-
commands?: Resolvable<unknown[]>
154+
commands?: unknown[] | ((directory: string) => unknown[])
155155
// Records POST /api/session/:id/command (204). Without it, commands answer 501 MockUnsupported.
156156
onCommand?: (input: { sessionID: string; body: unknown }) => void
157157
fileList?: (path: string) => unknown[] | Promise<unknown[]>
@@ -168,6 +168,8 @@ export interface MockServerConfig {
168168
generate?: (input: { sessionID: string; prompt: string }) => { text: string } | Promise<{ text: string }>
169169
onInboxChange?: (input: { sessionID: string; inboxID: string; action: "cancel" | "steer" | "queue" }) => void
170170
onBackground?: (input: { sessionID: string }) => void
171+
// Serves `/api/experimental/automation*`; defaults to an empty list.
172+
automations?: AutomationInfo[]
171173
// Serves the session-scoped persistent PTY routes and their connect WebSockets (`e2e/utils/persistent-pty.ts`).
172174
// Without it, the server has a working daemon and no terminals; `capability: false` makes the routes answer 503.
173175
persistentPty?: MockPersistentPtyConfig
@@ -924,10 +926,14 @@ function mockHandlers(
924926
),
925927
credentialRemove: () => noContent,
926928
command: (ctx) =>
927-
Effect.sync(() => ({
928-
location: location(config, requestDirectory(config, ctx.request)),
929-
data: resolve(config.commands ?? []),
930-
})),
929+
Effect.sync(() => {
930+
const directory = requestDirectory(config, ctx.request)
931+
932+
return {
933+
location: location(config, directory),
934+
data: Predicate.isFunction(config.commands) ? config.commands(directory) : config.commands ?? [],
935+
}
936+
}),
931937
skill: () => Effect.sync(() => ({ location: location(config), data: resolve(config.skills ?? []) })),
932938
plugin: () => Effect.sync(() => ({ location: location(config), data: resolve(config.plugins ?? []) })),
933939
mcp: (ctx) =>
@@ -972,6 +978,45 @@ function mockHandlers(
972978
Effect.sync(() => ({ location: location(config, requestDirectory(config, ctx.request)), data: {} })),
973979
// No scenario runs background jobs (Code Mode scripts or subagent work), so none is running.
974980
jobList: () => Effect.succeed({ data: [] }),
981+
automationList: (ctx) =>
982+
Effect.sync(() => {
983+
const list = config.automations ?? []
984+
const filtered = ctx.query.sessionID ? list.filter((item) => item.sessionID === ctx.query.sessionID) : list
985+
986+
return { data: filtered }
987+
}),
988+
automationPause: (ctx) =>
989+
Effect.suspend(() => {
990+
const list = config.automations ?? []
991+
const index = list.findIndex((item) => item.id === ctx.params.automationID)
992+
993+
if (index === -1) return Effect.fail(new MockNotFound({ message: `Automation not found: ${ctx.params.automationID}` }))
994+
const updated: AutomationInfo = { ...list[index]!, status: "paused", time: { ...list[index]!.time, updated: Date.now() } }
995+
list[index] = updated
996+
997+
return Effect.succeed({ data: updated })
998+
}),
999+
automationResume: (ctx) =>
1000+
Effect.suspend(() => {
1001+
const list = config.automations ?? []
1002+
const index = list.findIndex((item) => item.id === ctx.params.automationID)
1003+
1004+
if (index === -1) return Effect.fail(new MockNotFound({ message: `Automation not found: ${ctx.params.automationID}` }))
1005+
const updated: AutomationInfo = { ...list[index]!, status: "active", time: { ...list[index]!.time, updated: Date.now() } }
1006+
list[index] = updated
1007+
1008+
return Effect.succeed({ data: updated })
1009+
}),
1010+
automationDelete: (ctx) =>
1011+
Effect.suspend(() => {
1012+
const list = config.automations ?? []
1013+
const index = list.findIndex((item) => item.id === ctx.params.automationID)
1014+
1015+
if (index === -1) return Effect.fail(new MockNotFound({ message: `Automation not found: ${ctx.params.automationID}` }))
1016+
list.splice(index, 1)
1017+
1018+
return noContent
1019+
}),
9751020
worktreeList: () =>
9761021
Effect.sync(() => {
9771022
if (config.worktrees) return resolve(config.worktrees)

0 commit comments

Comments
 (0)