2026-03-31 17:02:56 -07:00
|
|
|
/// <reference types="bun-types" />
|
|
|
|
|
|
2026-04-05 14:59:03 +09:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, mock, afterAll, spyOn } from "bun:test"
|
|
|
|
|
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
|
2026-03-31 17:02:56 -07:00
|
|
|
import { tmpdir } from "node:os"
|
|
|
|
|
import { join } from "node:path"
|
|
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
|
|
|
|
import { createOpencodeClient, type Project } from "@opencode-ai/sdk"
|
2026-04-05 14:59:03 +09:00
|
|
|
import { readBoulderState, writeBoulderState } from "../../features/boulder-state"
|
|
|
|
|
import { createToolExecuteBeforeHandler } from "./tool-execute-before"
|
2026-03-31 17:02:56 -07:00
|
|
|
|
|
|
|
|
const isCallerOrchestratorMock = mock(async () => true)
|
2026-03-31 17:25:00 -07:00
|
|
|
const collectGitDiffStatsMock = mock(() => ({
|
|
|
|
|
filesChanged: 0,
|
|
|
|
|
insertions: 0,
|
|
|
|
|
deletions: 0,
|
|
|
|
|
}))
|
2026-03-31 17:02:56 -07:00
|
|
|
|
|
|
|
|
mock.module("../../shared/session-utils", () => ({
|
|
|
|
|
isCallerOrchestrator: isCallerOrchestratorMock,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
mock.module("../../shared/git-worktree", () => ({
|
|
|
|
|
collectGitDiffStats: collectGitDiffStatsMock,
|
|
|
|
|
formatFileChanges: mock(() => "No file changes"),
|
|
|
|
|
}))
|
|
|
|
|
|
2026-04-03 23:06:35 +09:00
|
|
|
afterAll(() => { mock.restore() })
|
|
|
|
|
|
2026-03-31 17:02:56 -07:00
|
|
|
const { createToolExecuteAfterHandler } = await import("./tool-execute-after")
|
|
|
|
|
|
2026-04-05 14:59:03 +09:00
|
|
|
type OpencodeClient = ReturnType<typeof createOpencodeClient>
|
|
|
|
|
type SessionGetResult = Awaited<ReturnType<OpencodeClient["session"]["get"]>>
|
|
|
|
|
|
2026-03-31 17:02:56 -07:00
|
|
|
describe("createToolExecuteAfterHandler background launch detection", () => {
|
|
|
|
|
let testDirectory = ""
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
testDirectory = join(tmpdir(), `atlas-background-launch-${crypto.randomUUID()}`)
|
|
|
|
|
|
|
|
|
|
if (!existsSync(testDirectory)) {
|
|
|
|
|
mkdirSync(testDirectory, { recursive: true })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isCallerOrchestratorMock.mockClear()
|
|
|
|
|
collectGitDiffStatsMock.mockClear()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
if (testDirectory && existsSync(testDirectory)) {
|
|
|
|
|
rmSync(testDirectory, { recursive: true, force: true })
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-05 14:59:03 +09:00
|
|
|
function createProject(): Project {
|
|
|
|
|
return {
|
2026-03-31 17:02:56 -07:00
|
|
|
id: "project-1",
|
|
|
|
|
worktree: testDirectory,
|
|
|
|
|
time: {
|
|
|
|
|
created: Date.now(),
|
|
|
|
|
},
|
2026-04-05 14:59:03 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createSessionGetResult(parentID: string | undefined): SessionGetResult {
|
|
|
|
|
return {
|
|
|
|
|
data: {
|
|
|
|
|
parentID,
|
|
|
|
|
},
|
|
|
|
|
error: undefined,
|
|
|
|
|
request: new Request("https://example.com/session"),
|
|
|
|
|
response: new Response(null, { status: 200 }),
|
|
|
|
|
} as SessionGetResult
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createHandler(parentSessionIDs?: Record<string, string | undefined>) {
|
|
|
|
|
const project = createProject()
|
|
|
|
|
const client = createOpencodeClient()
|
|
|
|
|
|
|
|
|
|
if (parentSessionIDs) {
|
|
|
|
|
spyOn(client.session, "get").mockImplementation((input) => Promise.resolve(
|
|
|
|
|
createSessionGetResult(parentSessionIDs[input.path.id]),
|
|
|
|
|
) as never)
|
|
|
|
|
}
|
2026-03-31 17:02:56 -07:00
|
|
|
|
|
|
|
|
const ctx = {
|
2026-04-05 14:59:03 +09:00
|
|
|
client,
|
2026-03-31 17:02:56 -07:00
|
|
|
project,
|
|
|
|
|
directory: testDirectory,
|
|
|
|
|
worktree: testDirectory,
|
|
|
|
|
serverUrl: new URL("https://example.com"),
|
|
|
|
|
$: Bun.$,
|
|
|
|
|
} satisfies PluginInput
|
|
|
|
|
|
|
|
|
|
return createToolExecuteAfterHandler({
|
|
|
|
|
ctx,
|
|
|
|
|
pendingFilePaths: new Map(),
|
|
|
|
|
pendingTaskRefs: new Map(),
|
|
|
|
|
autoCommit: true,
|
|
|
|
|
getState: () => ({ promptFailureCount: 0 }),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("#given a call_omo_agent background launch result", () => {
|
|
|
|
|
describe("#when tool.execute.after handles it", () => {
|
|
|
|
|
it("#then it should treat the launch as still running", async () => {
|
|
|
|
|
const handler = createHandler()
|
|
|
|
|
const output = {
|
|
|
|
|
title: "call_omo_agent",
|
|
|
|
|
output: "Background agent task launched successfully.",
|
|
|
|
|
metadata: {
|
|
|
|
|
sessionId: "ses_child123",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await handler(
|
|
|
|
|
{
|
|
|
|
|
tool: "call_omo_agent",
|
|
|
|
|
sessionID: "ses_parent",
|
|
|
|
|
},
|
|
|
|
|
output,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(output.output).toBe("Background agent task launched successfully.")
|
|
|
|
|
expect(collectGitDiffStatsMock).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-04-05 14:59:03 +09:00
|
|
|
|
|
|
|
|
describe("#when a background task launch belongs to the active boulder task", () => {
|
|
|
|
|
it("#then it should persist the delegated session without transforming the launch output", async () => {
|
|
|
|
|
const sessionID = "ses_parent"
|
|
|
|
|
const childSessionID = "ses_child123"
|
|
|
|
|
const planPath = join(testDirectory, "background-launch-plan.md")
|
|
|
|
|
const project = createProject()
|
|
|
|
|
const client = createOpencodeClient()
|
|
|
|
|
|
|
|
|
|
spyOn(client.session, "get").mockImplementation((input) => Promise.resolve(
|
|
|
|
|
createSessionGetResult(input.path.id === childSessionID ? sessionID : undefined),
|
|
|
|
|
) as never)
|
|
|
|
|
|
|
|
|
|
writeFileSync(planPath, `# Plan
|
|
|
|
|
|
|
|
|
|
## TODOs
|
|
|
|
|
- [ ] 1. Implement auth flow
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
writeBoulderState(testDirectory, {
|
|
|
|
|
active_plan: planPath,
|
|
|
|
|
started_at: "2026-01-02T10:00:00Z",
|
|
|
|
|
session_ids: [sessionID],
|
|
|
|
|
plan_name: "background-launch-plan",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const pendingFilePaths = new Map<string, string>()
|
|
|
|
|
const pendingTaskRefs = new Map()
|
|
|
|
|
const ctx = {
|
|
|
|
|
client,
|
|
|
|
|
project,
|
|
|
|
|
directory: testDirectory,
|
|
|
|
|
worktree: testDirectory,
|
|
|
|
|
serverUrl: new URL("https://example.com"),
|
|
|
|
|
$: Bun.$,
|
|
|
|
|
} satisfies PluginInput
|
|
|
|
|
const beforeHandler = createToolExecuteBeforeHandler({ ctx, pendingFilePaths, pendingTaskRefs })
|
|
|
|
|
const afterHandler = createToolExecuteAfterHandler({
|
|
|
|
|
ctx,
|
|
|
|
|
pendingFilePaths,
|
|
|
|
|
pendingTaskRefs,
|
|
|
|
|
autoCommit: true,
|
|
|
|
|
getState: () => ({ promptFailureCount: 0 }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await beforeHandler(
|
|
|
|
|
{ tool: "task", sessionID, callID: "call-bg-task" },
|
|
|
|
|
{ args: { prompt: "Implement auth flow" } },
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const output = {
|
|
|
|
|
title: "Sisyphus Task",
|
|
|
|
|
output: "Background task launched.\n\nBackground Task ID: bg_123\n\n<task_metadata>\nsession_id: ses_child123\n</task_metadata>",
|
|
|
|
|
metadata: {
|
|
|
|
|
sessionId: childSessionID,
|
|
|
|
|
agent: "sisyphus-junior",
|
|
|
|
|
category: "deep",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await afterHandler(
|
|
|
|
|
{ tool: "task", sessionID, callID: "call-bg-task" },
|
|
|
|
|
output,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(output.output).toContain("Background task launched.")
|
|
|
|
|
expect(collectGitDiffStatsMock).not.toHaveBeenCalled()
|
|
|
|
|
expect(readBoulderState(testDirectory)?.session_ids).toContain(childSessionID)
|
|
|
|
|
expect(readBoulderState(testDirectory)?.task_sessions?.["todo:1"]?.session_id).toBe(childSessionID)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-03-31 17:02:56 -07:00
|
|
|
})
|
|
|
|
|
})
|