refactor(core): store background job markers in a SQLite job table (#60491)

This commit is contained in:
Kit Langton
2026-10-30 07:50:16 -07:00
committed by GitHub
Unverified
parent c6f2f814ac
commit 664c8f0990
10 changed files with 524 additions and 81 deletions
+175 -2
View File
@@ -1,8 +1,8 @@
{
"version": "7",
"dialect": "sqlite",
"id": "0ac88673-a402-45a0-b931-e694d59ccefa",
"prevIds": ["e5daa0af-cae7-4057-919a-ebf44dfced0a"],
"id": "4f6e16c6-f070-4545-b9d0-19fcb6e5210c",
"prevIds": ["0ac88673-a402-45a0-b931-e694d59ccefa"],
"ddl": [
{
"name": "account_state",
@@ -28,6 +28,10 @@
"name": "event",
"entityType": "tables"
},
{
"name": "job",
"entityType": "tables"
},
{
"name": "kv",
"entityType": "tables"
@@ -462,6 +466,126 @@
"entityType": "columns",
"table": "event"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "notification_id",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "id",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "session_id",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "child_session_id",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "kind",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "title",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "status",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "output",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "error",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "recovery",
"entityType": "columns",
"table": "job"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "time_created",
"entityType": "columns",
"table": "job"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "time_updated",
"entityType": "columns",
"table": "job"
},
{
"type": "text",
"notNull": false,
@@ -2124,6 +2248,13 @@
"table": "event",
"entityType": "pks"
},
{
"columns": ["notification_id"],
"nameExplicit": false,
"name": "job_pk",
"table": "job",
"entityType": "pks"
},
{
"columns": ["key"],
"nameExplicit": false,
@@ -2241,6 +2372,48 @@
"entityType": "indexes",
"table": "event"
},
{
"columns": [
{
"value": "session_id",
"isExpression": false
}
],
"isUnique": false,
"where": null,
"origin": "manual",
"name": "job_session_idx",
"entityType": "indexes",
"table": "job"
},
{
"columns": [
{
"value": "child_session_id",
"isExpression": false
}
],
"isUnique": false,
"where": null,
"origin": "manual",
"name": "job_child_session_idx",
"entityType": "indexes",
"table": "job"
},
{
"columns": [
{
"value": "id",
"isExpression": false
}
],
"isUnique": false,
"where": null,
"origin": "manual",
"name": "job_id_idx",
"entityType": "indexes",
"table": "job"
},
{
"columns": [
{
+2
View File
@@ -57,6 +57,7 @@ import m54 from "./migration/20261026223005_schedule.js"
import m55 from "./migration/20261029002625_session_part.js"
import m56 from "./migration/20261029073502_session_time_busy.js"
import m57 from "./migration/20261029165540_schedule_checks.js"
import m58 from "./migration/20261030054400_job.js"
export const migrations = [
m00,
@@ -117,4 +118,5 @@ export const migrations = [
m55,
m56,
m57,
m58,
] satisfies DatabaseMigration.Migration[]
@@ -0,0 +1,60 @@
import { Effect } from "effect"
import type { DatabaseMigration } from "../migration.js"
const migration: DatabaseMigration.Migration = {
id: "20261030054400_job",
up(tx) {
return Effect.gen(function* () {
yield* tx.run(`
CREATE TABLE \`job\` (
\`notification_id\` text PRIMARY KEY,
\`id\` text NOT NULL,
\`session_id\` text NOT NULL,
\`child_session_id\` text,
\`kind\` text NOT NULL,
\`title\` text,
\`status\` text NOT NULL,
\`output\` text,
\`error\` text,
\`recovery\` text NOT NULL,
\`time_created\` integer NOT NULL,
\`time_updated\` integer NOT NULL
);
`)
yield* tx.run(`CREATE INDEX \`job_session_idx\` ON \`job\` (\`session_id\`);`)
yield* tx.run(`CREATE INDEX \`job_child_session_idx\` ON \`job\` (\`child_session_id\`);`)
yield* tx.run(`CREATE INDEX \`job_id_idx\` ON \`job\` (\`id\`);`)
yield* tx.run(`
INSERT OR IGNORE INTO \`job\` (
\`notification_id\`, \`id\`, \`session_id\`, \`child_session_id\`, \`kind\`, \`title\`,
\`status\`, \`output\`, \`error\`, \`recovery\`, \`time_created\`, \`time_updated\`
)
SELECT
json_extract(\`value\`, '$.notificationID'),
json_extract(\`value\`, '$.id'),
COALESCE(json_extract(\`value\`, '$.recovery.sessionID'), json_extract(\`value\`, '$.recovery.parentSessionID')),
json_extract(\`value\`, '$.recovery.childSessionID'),
json_extract(\`value\`, '$.recovery.kind'),
COALESCE(
json_extract(\`value\`, '$.recovery.command'),
json_extract(\`value\`, '$.recovery.description'),
json_extract(\`value\`, '$.recovery.path')
),
json_extract(\`value\`, '$.status'),
json_extract(\`value\`, '$.output'),
json_extract(\`value\`, '$.error'),
json_extract(\`value\`, '$.recovery'),
\`time_created\`,
\`time_updated\`
FROM \`kv\`
WHERE \`key\` LIKE 'job.background/%'
AND json_extract(\`value\`, '$.notificationID') IS NOT NULL
AND json_extract(\`value\`, '$.id') IS NOT NULL
AND COALESCE(json_extract(\`value\`, '$.recovery.sessionID'), json_extract(\`value\`, '$.recovery.parentSessionID')) IS NOT NULL;
`)
yield* tx.run(`DELETE FROM \`kv\` WHERE \`key\` LIKE 'job.background/%';`)
})
},
}
export default migration
+19
View File
@@ -68,6 +68,22 @@ const schema: Omit<DatabaseMigration.Migration, "id"> = {
CONSTRAINT \`fk_event_aggregate_id_event_sequence_aggregate_id_fk\` FOREIGN KEY (\`aggregate_id\`) REFERENCES \`event_sequence\`(\`aggregate_id\`) ON DELETE CASCADE
);
`)
yield* tx.run(`
CREATE TABLE \`job\` (
\`notification_id\` text PRIMARY KEY,
\`id\` text NOT NULL,
\`session_id\` text NOT NULL,
\`child_session_id\` text,
\`kind\` text NOT NULL,
\`title\` text,
\`status\` text NOT NULL,
\`output\` text,
\`error\` text,
\`recovery\` text NOT NULL,
\`time_created\` integer NOT NULL,
\`time_updated\` integer NOT NULL
);
`)
yield* tx.run(`
CREATE TABLE \`kv\` (
\`key\` text PRIMARY KEY,
@@ -294,6 +310,9 @@ const schema: Omit<DatabaseMigration.Migration, "id"> = {
`)
yield* tx.run(`CREATE UNIQUE INDEX \`event_aggregate_seq_idx\` ON \`event\` (\`aggregate_id\`,\`seq\`);`)
yield* tx.run(`CREATE INDEX \`event_aggregate_type_seq_idx\` ON \`event\` (\`aggregate_id\`,\`type\`,\`seq\`);`)
yield* tx.run(`CREATE INDEX \`job_session_idx\` ON \`job\` (\`session_id\`);`)
yield* tx.run(`CREATE INDEX \`job_child_session_idx\` ON \`job\` (\`child_session_id\`);`)
yield* tx.run(`CREATE INDEX \`job_id_idx\` ON \`job\` (\`id\`);`)
yield* tx.run(
`CREATE UNIQUE INDEX \`permission_project_action_resource_idx\` ON \`permission\` (\`project_id\`,\`action\`,\`resource\`);`,
)
+99 -27
View File
@@ -1,8 +1,10 @@
export * as Job from "./job.js"
import { asc, eq, or } from "drizzle-orm"
import { Array, Cause, Clock, Context, Deferred, Effect, Exit, Layer, Schema, Scope, SynchronizedRef } from "effect"
import { makeGlobalNode } from "@opencode/util/effect/app-node"
import { KV } from "./kv.js"
import { Database } from "./database/database.js"
import { JobTable } from "./job/sql.js"
import { SessionMessage } from "./session/message.js"
import { SessionSchema } from "./session/schema.js"
@@ -40,7 +42,6 @@ export type Recovery = Background["recovery"]
export type Status = Background["status"]
const decodeBackground = Schema.decodeUnknownResult(Background)
const backgroundPrefix = "job.background/"
const COMPLETED_LIMIT = 25
export type Info = {
@@ -138,6 +139,7 @@ export interface Interface {
readonly background: (id: string) => Effect.Effect<Info | undefined>
readonly backgroundAll: (input: BackgroundAllInput) => Effect.Effect<Info[]>
readonly cancel: (id: string) => Effect.Effect<Info | undefined>
readonly cancelSession: (sessionID: SessionSchema.ID) => Effect.Effect<Info[]>
readonly pendingBackground: Effect.Effect<readonly Background[]>
readonly runningBackground: Effect.Effect<readonly Recovery[]>
readonly completeBackground: (notificationID: SessionMessage.ID) => Effect.Effect<void>
@@ -152,6 +154,26 @@ function snapshot(job: Active): Info {
}
}
function ownerSessionID(recovery: Recovery): SessionSchema.ID {
return recovery.kind === "subagent" ? recovery.parentSessionID : recovery.sessionID
}
function recoveryTitle(job: Active, recovery: Recovery): string | null {
if (job.info.title !== undefined) return job.info.title
if (recovery.kind === "shell") return recovery.command
if (recovery.kind === "subagent") return recovery.description
return recovery.path
}
function matchesSession(job: Active, sessionID: SessionSchema.ID) {
if (job.info.metadata?.sessionID === sessionID) return true
if (job.blockingSessions.has(sessionID)) return true
if (!job.recovery) return false
if (job.recovery.kind === "subagent")
return job.recovery.parentSessionID === sessionID || job.recovery.childSessionID === sessionID
return job.recovery.sessionID === sessionID
}
function errorText(cause: Cause.Cause<unknown>) {
const render = (error: Error): string => {
const message = error.message || error.name || "Unknown error"
@@ -176,14 +198,11 @@ function decrementSession(input: Map<SessionSchema.ID, number>, sessionID: Sessi
}
/**
* Makes one scoped, process-local registry. Explicitly recoverable background
* work also owns a durable notification marker until its notification is admitted.
* Unconsumed results survive the start-to-wait handoff. Foreground block/cancel
* and non-recoverable wait results enter a 25-entry consumed history. Recoverable
* wait results stay available for background registration and acknowledgment.
* Makes one scoped, process-local registry backed by the durable SQLite `job` table for
* backgrounded work until its completion notification is admitted.
*/
export const make = Effect.gen(function* () {
const kv = yield* KV.Service
const db = (yield* Database.Service).db
const state: State = {
jobs: yield* SynchronizedRef.make(new Map()),
scope: yield* Scope.Scope,
@@ -204,14 +223,36 @@ export const make = Effect.gen(function* () {
const persistBackground = Effect.fnUntraced(function* (job: Active) {
if (!job.recovery || !job.info.notificationID) return
yield* kv.set(`${backgroundPrefix}${job.info.notificationID}`, {
const now = yield* Clock.currentTimeMillis
const values = {
notification_id: job.info.notificationID,
id: job.info.id,
notificationID: job.info.notificationID,
recovery: job.recovery,
session_id: ownerSessionID(job.recovery),
child_session_id: job.recovery.kind === "subagent" ? job.recovery.childSessionID : null,
kind: job.recovery.kind,
title: recoveryTitle(job, job.recovery),
status: job.info.status,
...(job.info.output !== undefined ? { output: job.info.output } : {}),
...(job.info.error !== undefined ? { error: job.info.error } : {}),
})
output: job.info.output ?? null,
error: job.info.error ?? null,
recovery: job.recovery,
time_created: job.info.started_at,
time_updated: now,
}
yield* db
.insert(JobTable)
.values(values)
.onConflictDoUpdate({
target: JobTable.notification_id,
set: {
status: values.status,
output: values.output,
error: values.error,
recovery: values.recovery,
time_updated: now,
},
})
.run()
.pipe(Effect.orDie)
})
const settle = Effect.fnUntraced(function* (id: string, scope: Scope.Closeable, exit: Exit.Exit<string, unknown>) {
@@ -316,7 +357,7 @@ export const make = Effect.gen(function* () {
}).pipe(
// Recoverable wait -> background is a supported handoff, even after failure.
Effect.tap((result) =>
result.info.status === "running" || job.recovery ? Effect.void : consume(input.id, job.scope),
result.info?.status === "running" || job.recovery ? Effect.void : consume(input.id, job.scope),
),
)
})
@@ -415,7 +456,7 @@ export const make = Effect.gen(function* () {
return result.map((item) => item.info)
})
const cancel: Interface["cancel"] = Effect.fn("Job.cancel")(function* (id) {
const cancelInternal = Effect.fnUntraced(function* (id: string, persist = true) {
const completed_at = yield* Clock.currentTimeMillis
const result = yield* SynchronizedRef.modifyEffect(
state.jobs,
@@ -432,7 +473,7 @@ export const make = Effect.gen(function* () {
completed_at,
},
}
yield* persistBackground(next)
if (persist) yield* persistBackground(next)
return [
{ info: snapshot(next), done: job.done, scope: job.scope, generation: job.scope },
new Map(jobs).set(id, next),
@@ -445,15 +486,45 @@ export const make = Effect.gen(function* () {
return result.info
})
const cancel: Interface["cancel"] = Effect.fn("Job.cancel")((id) => cancelInternal(id, true))
const cancelSession: Interface["cancelSession"] = Effect.fn("Job.cancelSession")(function* (sessionID) {
const ids = [...(yield* SynchronizedRef.get(state.jobs)).values()]
.filter((job) => job.info.status === "running" && matchesSession(job, sessionID))
.map((job) => job.info.id)
const cancelled = yield* Effect.forEach(ids, (id) => cancelInternal(id, false))
yield* db
.delete(JobTable)
.where(or(eq(JobTable.session_id, sessionID), eq(JobTable.child_session_id, sessionID)))
.run()
.pipe(Effect.orDie)
yield* SynchronizedRef.update(state.jobs, (jobs) => {
const next = new Map(jobs)
for (const [id, job] of jobs) {
if (matchesSession(job, sessionID)) next.delete(id)
}
return next
})
return cancelled.filter((info): info is Info => info !== undefined)
})
const pendingBackground: Interface["pendingBackground"] = Effect.gen(function* () {
const recovered: Background[] = []
let after: string | undefined
do {
const page = yield* kv.scan({ prefix: backgroundPrefix, after })
recovered.push(...Array.filterMap(page.entries, (entry) => decodeBackground(entry.value)))
after = page.next
} while (after)
return recovered
const rows = yield* db
.select()
.from(JobTable)
.orderBy(asc(JobTable.time_created), asc(JobTable.notification_id))
.all()
.pipe(Effect.orDie)
return Array.filterMap(rows, (row) =>
decodeBackground({
id: row.id,
notificationID: row.notification_id,
recovery: row.recovery,
status: row.status,
...(row.output !== null ? { output: row.output } : {}),
...(row.error !== null ? { error: row.error } : {}),
}),
)
}).pipe(Effect.withSpan("Job.pendingBackground"))
const runningBackground: Interface["runningBackground"] = SynchronizedRef.get(state.jobs).pipe(
@@ -468,7 +539,7 @@ export const make = Effect.gen(function* () {
const completeBackground: Interface["completeBackground"] = Effect.fn("Job.completeBackground")((notificationID) =>
SynchronizedRef.updateEffect(state.jobs, (jobs) =>
Effect.gen(function* () {
yield* kv.remove(`${backgroundPrefix}${notificationID}`)
yield* db.delete(JobTable).where(eq(JobTable.notification_id, notificationID)).run().pipe(Effect.orDie)
const entry = [...jobs].find(([, job]) => job.info.notificationID === notificationID)
if (!entry || entry[1].info.status === "running") return jobs
const next = new Map(jobs)
@@ -486,6 +557,7 @@ export const make = Effect.gen(function* () {
background,
backgroundAll,
cancel,
cancelSession,
pendingBackground,
runningBackground,
completeBackground,
@@ -494,4 +566,4 @@ export const make = Effect.gen(function* () {
const layer = Layer.effect(Service, make)
export const node = makeGlobalNode({ service: Service, layer, deps: [KV.node] })
export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node] })
+27
View File
@@ -0,0 +1,27 @@
import { index, sqliteTable, text } from "drizzle-orm/sqlite-core"
import { Timestamps } from "../database/schema.sql.js"
import type { Job } from "../job.js"
import type { SessionMessage } from "../session/message.js"
import type { SessionSchema } from "../session/schema.js"
export const JobTable = sqliteTable(
"job",
{
notification_id: text().$type<SessionMessage.ID>().primaryKey(),
id: text().notNull(),
session_id: text().$type<SessionSchema.ID>().notNull(),
child_session_id: text().$type<SessionSchema.ID>(),
kind: text().notNull(),
title: text(),
status: text().$type<Job.Status>().notNull(),
output: text(),
error: text(),
recovery: text({ mode: "json" }).$type<Job.Recovery>().notNull(),
...Timestamps,
},
(table) => [
index("job_session_idx").on(table.session_id),
index("job_child_session_idx").on(table.child_session_id),
index("job_id_idx").on(table.id),
],
)
+1
View File
@@ -392,6 +392,7 @@ const layer = Layer.effect(
yield* result.get(sessionID)
yield* execution.interrupt(sessionID)
yield* execution.awaitIdle(sessionID)
yield* jobs.cancelSession(sessionID)
yield* transport.close(sessionID)
const children = yield* store.list({ parentID: sessionID })
yield* Effect.forEach(children, (child) => result.remove(child.id), { concurrency: 1, discard: true })
+37 -47
View File
@@ -99,24 +99,21 @@ export const layer = (options?: Options) =>
return true
})
const recoverShell = Effect.fnUntraced(function* (
const restartMessage = (
background: Job.Background,
recovery: Extract<Job.Recovery, { kind: "shell" }>,
) {
recovery: Exclude<Job.Recovery, { kind: "subagent" }>,
) => {
const state = background.status === "running" ? "cancelled" : background.status
const text =
background.status === "running"
? "Command cancelled because the server restarted"
: state === "completed"
? (background.output ?? "Command completed")
: state === "error"
? (background.error ?? "Command failed")
: "Command cancelled"
yield* sessions
.synthetic({
id: background.notificationID,
sessionID: recovery.sessionID,
if (recovery.kind === "shell") {
const text =
background.status === "running"
? "Command cancelled because the server restarted"
: state === "completed"
? (background.output ?? "Command completed")
: state === "error"
? (background.error ?? "Command failed")
: "Command cancelled"
return {
description: recovery.command,
...ShellResult.notification({
jobID: background.id,
@@ -125,45 +122,39 @@ export const layer = (options?: Options) =>
state,
text,
}),
// Restart notices must not revive idle owners of long-lived shells.
// Interrupted executions resume separately after their notices are admitted.
resume: false,
})
.pipe(
Effect.catchTag("Session.NotFoundError", () => Effect.void),
Effect.orDie,
)
yield* jobs.completeBackground(background.notificationID)
})
const recoverImage = Effect.fnUntraced(function* (
background: Job.Background,
recovery: Extract<Job.Recovery, { kind: "image" }>,
) {
}
}
const body =
background.status === "running"
? `Image request for ${recovery.path} was cancelled because the server restarted.`
: background.status === "completed"
: state === "completed"
? (background.output ?? `Image request finished. Saved image: ${recovery.path} (${recovery.model}).`)
: background.status === "error"
: state === "error"
? (background.error ?? `Image request failed for ${recovery.path}.`)
: `Image request for ${recovery.path} was cancelled.`
return {
description: "Image generation",
text: ImageNotice.text({ id: background.id, state, body }),
metadata: {
source: "experimental_image_generate",
jobID: background.id,
state,
path: recovery.path,
},
}
}
const recoverToolJob = Effect.fnUntraced(function* (
background: Job.Background,
recovery: Exclude<Job.Recovery, { kind: "subagent" }>,
) {
yield* sessions
.synthetic({
id: background.notificationID,
sessionID: recovery.sessionID,
description: "Image generation",
text: ImageNotice.text({
id: background.id,
state: background.status === "running" ? "cancelled" : background.status,
body,
}),
metadata: {
source: "experimental_image_generate",
jobID: background.id,
state: background.status === "running" ? "cancelled" : background.status,
path: recovery.path,
},
...restartMessage(background, recovery),
// Restart notices must not revive idle owners of long-lived background tools.
// Interrupted executions resume separately after their notices are admitted.
resume: false,
})
.pipe(
@@ -247,8 +238,7 @@ export const layer = (options?: Options) =>
Effect.fnUntraced(function* (background) {
if ((yield* jobs.get(background.id))?.status === "running") return
const recovery = background.recovery
if (recovery.kind === "shell") return yield* recoverShell(background, recovery)
if (recovery.kind === "image") return yield* recoverImage(background, recovery)
if (recovery.kind !== "subagent") return yield* recoverToolJob(background, recovery)
yield* recoverSubagent(background, recovery, suspended)
}),
{ discard: true },
+86 -2
View File
@@ -1,14 +1,20 @@
import { describe, expect } from "bun:test"
import { Database } from "@opencode/core/database/database"
import jobMigration from "@opencode/core/database/migration/20261030054400_job"
import { Job } from "@opencode/core/job"
import { KV } from "@opencode/core/kv"
import { JobTable } from "@opencode/core/job/sql"
import { KVTable } from "@opencode/core/kv/sql"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { Integration } from "@opencode/core/integration"
import { LayerNode } from "@opencode/util/effect/layer-node"
import { Global } from "@opencode/util/global"
import { eq, like } from "drizzle-orm"
import { Cause, Deferred, Effect, Exit, Fiber, Scope } from "effect"
import { SessionMessage } from "@opencode/core/session/message"
import { SessionSchema } from "@opencode/core/session/schema"
import { testEffect } from "./lib/effect"
const it = testEffect(AppNodeBuilder.build(LayerNode.group([Job.node, KV.node])))
const it = testEffect(AppNodeBuilder.build(LayerNode.group([Job.node, Database.node, Global.node])))
describe("Job", () => {
it.live("tracks process-local work through explicit observation", () =>
@@ -450,4 +456,82 @@ describe("Job", () => {
expect((yield* jobs.get(job.id))?.status).toBe("running")
}),
)
it.live("cancels running session jobs and removes unacknowledged SQLite job rows on cancelSession", () =>
Effect.gen(function* () {
const jobs = yield* Job.Service
const db = (yield* Database.Service).db
const sessionID = SessionSchema.ID.make("ses_cleanup_owner")
const otherSessionID = SessionSchema.ID.make("ses_cleanup_other")
const interrupted = yield* Deferred.make<void>()
const owned = yield* jobs.start({
id: "job_cleanup_owned",
type: "shell",
recovery: { kind: "shell", sessionID, shellID: "job_cleanup_owned", command: "bun run dev" },
run: Effect.never.pipe(Effect.ensuring(Deferred.succeed(interrupted, undefined))),
})
const other = yield* jobs.start({
id: "job_cleanup_other",
type: "shell",
recovery: { kind: "shell", sessionID: otherSessionID, shellID: "job_cleanup_other", command: "bun test" },
run: Effect.succeed("kept"),
})
yield* jobs.background(owned.id)
yield* jobs.background(other.id)
yield* jobs.wait({ id: other.id })
const cancelled = yield* jobs.cancelSession(sessionID)
expect(cancelled).toMatchObject([{ id: owned.id, status: "cancelled" }])
yield* Deferred.await(interrupted).pipe(Effect.timeout("1 second"))
expect(yield* db.select().from(JobTable).where(eq(JobTable.session_id, sessionID)).all()).toEqual([])
const remaining = yield* jobs.pendingBackground
expect(remaining).toMatchObject([{ id: other.id, status: "completed", output: "kept" }])
if (remaining[0]) yield* jobs.completeBackground(remaining[0].notificationID)
}),
)
it.live("migrates legacy kv job.background/* markers into the job table", () =>
Effect.gen(function* () {
const db = (yield* Database.Service).db
const jobs = yield* Job.Service
const notificationID = SessionMessage.ID.create()
const sessionID = SessionSchema.ID.make("ses_migrated")
yield* db.run("DROP TABLE `job`")
yield* db
.insert(KVTable)
.values({
key: `job.background/${notificationID}`,
value: {
id: "shell_legacy",
notificationID,
recovery: {
kind: "shell",
sessionID,
shellID: "shell_legacy",
command: "bun test",
},
status: "completed",
output: "all green",
},
})
.run()
yield* db.transaction((tx) => jobMigration.up(tx))
expect(yield* db.select().from(KVTable).where(like(KVTable.key, "job.background/%")).all()).toEqual([])
expect(yield* jobs.pendingBackground).toMatchObject([
{
id: "shell_legacy",
notificationID,
status: "completed",
output: "all green",
recovery: { kind: "shell", sessionID, shellID: "shell_legacy", command: "bun test" },
},
])
yield* jobs.completeBackground(notificationID)
}),
)
})
+18 -3
View File
@@ -19,6 +19,7 @@ import { Provider } from "@opencode/core/provider"
import { AbsolutePath } from "@opencode/core/schema"
import { Agent } from "@opencode/core/agent"
import { Job } from "@opencode/core/job"
import { JobTable } from "@opencode/core/job/sql"
import { KV } from "@opencode/core/kv"
import { LocationServiceMap } from "@opencode/core/location-service-map"
import { Session } from "@opencode/core/session"
@@ -273,9 +274,23 @@ describe("SubagentTool", () => {
}),
])
// Reproduce a crash after admission but before acknowledgment using the real persisted marker.
const kv = yield* KV.Service
yield* kv.set(`job.background/${marker.notificationID}`, marker)
// Reproduce a crash after admission but before acknowledgment using the real persisted row.
const database = yield* Database.Service
yield* database.db
.insert(JobTable)
.values({
notification_id: marker.notificationID,
id: marker.id,
session_id: parent.id,
child_session_id: outputSessionID(result.metadata),
kind: marker.recovery.kind,
title: "background review",
status: marker.status,
output: marker.output ?? null,
error: marker.error ?? null,
recovery: marker.recovery,
})
.run()
const restart = yield* SessionRestart.Service
yield* restart.resumeSuspendedSessions
yield* sessions.wait(parent.id)