mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
142 lines
5.5 KiB
TypeScript
142 lines
5.5 KiB
TypeScript
import { expect } from "bun:test"
|
|
import { Agent } from "@opencode/core/agent"
|
|
import { Automation } from "@opencode/core/automation"
|
|
import { AutomationTable } from "@opencode/core/automation/sql"
|
|
import { Database } from "@opencode/core/database/database"
|
|
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
|
import { Instance } from "@opencode/core/instance/service"
|
|
import { Permission } from "@opencode/core/permission"
|
|
import { PersistentPty } from "@opencode/core/persistent-pty"
|
|
import { Session } from "@opencode/core/session"
|
|
import { SessionExecution } from "@opencode/core/session/execution"
|
|
import { SessionMessage } from "@opencode/core/session/message"
|
|
import { SessionInboxTable } from "@opencode/core/session/sql"
|
|
import { AbsolutePath } from "@opencode/schema/schema"
|
|
import { Tool } from "@opencode/schema/tool"
|
|
import { LayerNode } from "@opencode/util/effect/layer-node"
|
|
import { eq } from "drizzle-orm"
|
|
import { Clock, Effect, Fiber, Layer, Schedule } from "effect"
|
|
import path from "node:path"
|
|
import { offlineModels } from "./fixture/models"
|
|
import { disabledPersistentPtyLayer } from "./fixture/persistent-pty"
|
|
import { tmpdirScoped } from "./fixture/tmpdir"
|
|
import { testEffect } from "./lib/effect"
|
|
|
|
const it = testEffect(
|
|
AppNodeBuilder.build(LayerNode.group([Automation.node, Database.node, Instance.node, Session.node]), [
|
|
offlineModels,
|
|
PersistentPty.node.replace(disabledPersistentPtyLayer),
|
|
SessionExecution.node.replace(
|
|
Layer.mock(SessionExecution.Service, { wake: () => Effect.die("Shell actions must not wake a session") }),
|
|
),
|
|
]),
|
|
)
|
|
|
|
const command = "echo automation-output; exit 7"
|
|
const trigger = { type: "every", every: "1 hour" } as const
|
|
|
|
it.live("approves the fixed command once and records a failed exit without waking a session", () =>
|
|
Effect.gen(function* () {
|
|
const directory = AbsolutePath.make((yield* tmpdirScoped()).path)
|
|
yield* Effect.promise(() =>
|
|
Bun.write(
|
|
path.join(directory, "opencode.json"),
|
|
JSON.stringify({ experimental: { automations: true }, permission: { shell: "ask" } }),
|
|
),
|
|
)
|
|
const sessions = yield* Session.Service
|
|
const session = yield* sessions.create({ location: { directory } })
|
|
const instances = yield* Instance.Service
|
|
const automations = yield* Automation.Service
|
|
const invocation = Permission.invocation({
|
|
sessionID: session.id,
|
|
agent: Agent.defaultID,
|
|
messageID: SessionMessage.ID.make("msg_automation_shell"),
|
|
id: Tool.CallID.make("automation_create"),
|
|
})
|
|
const create = yield* automations
|
|
.create({ name: "shell test", sessionID: session.id, trigger, action: { type: "shell", command } }, invocation)
|
|
.pipe(Effect.forkChild)
|
|
|
|
yield* Effect.gen(function* () {
|
|
const permission = yield* Permission.Service
|
|
const asks = yield* permission
|
|
.forSession(session.id)
|
|
.pipe(
|
|
Effect.repeat({ until: (asks) => asks.length > 0, schedule: Schedule.spaced("10 millis") }),
|
|
Effect.timeout("10 seconds"),
|
|
)
|
|
expect(asks).toMatchObject([
|
|
{
|
|
action: "shell",
|
|
resources: ["echo automation-output", "exit 7"],
|
|
metadata: { automation: "shell test", trigger, unattended: true, command },
|
|
source: invocation.source,
|
|
},
|
|
])
|
|
expect(asks[0]?.save).toBeUndefined()
|
|
for (const ask of asks) yield* permission.reply({ requestID: ask.id, reply: "once" })
|
|
}).pipe(instances.provide(session))
|
|
|
|
const created = yield* Fiber.join(create)
|
|
const database = yield* Database.Service
|
|
yield* database.db
|
|
.update(AutomationTable)
|
|
.set({ next_due_at: yield* Clock.currentTimeMillis })
|
|
.where(eq(AutomationTable.id, created.id))
|
|
.run()
|
|
yield* automations.tick
|
|
const ran = yield* automations
|
|
.get(created.id)
|
|
.pipe(
|
|
Effect.repeat({ until: (row) => row.lastOutput !== undefined, schedule: Schedule.spaced("10 millis") }),
|
|
Effect.timeout("10 seconds"),
|
|
)
|
|
expect(ran).toMatchObject({
|
|
status: "active",
|
|
runCount: 1,
|
|
lastOutcome: "failed",
|
|
lastOutput: expect.stringMatching(/^Exit code: 7\s+automation-output/),
|
|
})
|
|
expect(yield* database.db.select().from(SessionInboxTable).all()).toEqual([])
|
|
}),
|
|
)
|
|
|
|
it.live("rejects a shell action that changes to a denied external directory", () =>
|
|
Effect.gen(function* () {
|
|
const directory = AbsolutePath.make((yield* tmpdirScoped()).path)
|
|
yield* Effect.promise(() =>
|
|
Bun.write(
|
|
path.join(directory, "opencode.json"),
|
|
JSON.stringify({
|
|
experimental: { automations: true },
|
|
permission: { shell: "allow", external_directory: "deny" },
|
|
}),
|
|
),
|
|
)
|
|
const sessions = yield* Session.Service
|
|
const session = yield* sessions.create({ location: { directory } })
|
|
const automations = yield* Automation.Service
|
|
const error = yield* automations
|
|
.create(
|
|
{
|
|
name: "outside",
|
|
sessionID: session.id,
|
|
trigger,
|
|
action: { type: "shell", command: "cd ..; echo outside" },
|
|
},
|
|
Permission.invocation({
|
|
sessionID: session.id,
|
|
agent: Agent.defaultID,
|
|
messageID: SessionMessage.ID.make("msg_external_directory"),
|
|
id: Tool.CallID.make("automation_create"),
|
|
}),
|
|
)
|
|
.pipe(Effect.flip)
|
|
expect(error).toMatchObject({
|
|
_tag: "Permission.BlockedError",
|
|
permission: "external_directory",
|
|
})
|
|
expect(yield* automations.list()).toEqual([])
|
|
}),
|
|
)
|