feat(atlas): persist preferred task session reuse

This commit is contained in:
HaD0Yun
2026-03-17 17:25:46 +09:00
parent 1070b9170f
commit 5c6194372e
14 changed files with 665 additions and 18 deletions
+1
View File
@@ -1,3 +1,4 @@
export * from "./types"
export * from "./constants"
export * from "./storage"
export * from "./top-level-task"
+130
View File
@@ -11,8 +11,11 @@ import {
getPlanName,
createBoulderState,
findPrometheusPlans,
getTaskSessionState,
upsertTaskSessionState,
} from "./storage"
import type { BoulderState } from "./types"
import { readCurrentTopLevelTask } from "./top-level-task"
describe("boulder-state", () => {
const TEST_DIR = join(tmpdir(), "boulder-state-test-" + Date.now())
@@ -134,6 +137,24 @@ describe("boulder-state", () => {
expect(result?.session_ids).toEqual(["session-1", "session-2"])
expect(result?.plan_name).toBe("my-plan")
})
test("should default task_sessions to empty object when missing from JSON", () => {
// given - boulder.json without task_sessions field
const boulderFile = join(SISYPHUS_DIR, "boulder.json")
writeFileSync(boulderFile, JSON.stringify({
active_plan: "/path/to/plan.md",
started_at: "2026-01-01T00:00:00Z",
session_ids: ["session-1"],
plan_name: "plan",
}))
// when
const result = readBoulderState(TEST_DIR)
// then
expect(result).not.toBeNull()
expect(result!.task_sessions).toEqual({})
})
})
describe("writeBoulderState", () => {
@@ -249,6 +270,115 @@ describe("boulder-state", () => {
})
})
describe("task session state", () => {
test("should persist and read preferred session for a top-level plan task", () => {
// given - existing boulder state
const state: BoulderState = {
active_plan: "/plan.md",
started_at: "2026-01-02T10:00:00Z",
session_ids: ["session-1"],
plan_name: "plan",
}
writeBoulderState(TEST_DIR, state)
// when
upsertTaskSessionState(TEST_DIR, {
taskKey: "todo:1",
taskLabel: "1",
taskTitle: "Implement auth flow",
sessionId: "ses_task_123",
agent: "sisyphus-junior",
category: "deep",
})
const result = getTaskSessionState(TEST_DIR, "todo:1")
// then
expect(result).not.toBeNull()
expect(result?.session_id).toBe("ses_task_123")
expect(result?.task_title).toBe("Implement auth flow")
expect(result?.agent).toBe("sisyphus-junior")
expect(result?.category).toBe("deep")
})
test("should overwrite preferred session for the same top-level plan task", () => {
// given - existing boulder state with prior preferred session
const state: BoulderState = {
active_plan: "/plan.md",
started_at: "2026-01-02T10:00:00Z",
session_ids: ["session-1"],
plan_name: "plan",
task_sessions: {
"todo:1": {
task_key: "todo:1",
task_label: "1",
task_title: "Implement auth flow",
session_id: "ses_old",
updated_at: "2026-01-02T10:00:00Z",
},
},
}
writeBoulderState(TEST_DIR, state)
// when
upsertTaskSessionState(TEST_DIR, {
taskKey: "todo:1",
taskLabel: "1",
taskTitle: "Implement auth flow",
sessionId: "ses_new",
})
const result = getTaskSessionState(TEST_DIR, "todo:1")
// then
expect(result?.session_id).toBe("ses_new")
})
})
describe("readCurrentTopLevelTask", () => {
test("should return the first unchecked top-level task in TODOs", () => {
// given - plan with nested and top-level unchecked tasks
const planPath = join(TEST_DIR, "current-task-plan.md")
writeFileSync(planPath, `# Plan
## TODOs
- [x] 1. Finished task
- [ ] nested acceptance checkbox
- [ ] 2. Current task
## Final Verification Wave
- [ ] F1. Final review
`)
// when
const result = readCurrentTopLevelTask(planPath)
// then
expect(result).not.toBeNull()
expect(result?.key).toBe("todo:2")
expect(result?.title).toBe("Current task")
})
test("should fall back to final-wave task when implementation tasks are complete", () => {
// given - plan with only final-wave work remaining
const planPath = join(TEST_DIR, "final-wave-current-task-plan.md")
writeFileSync(planPath, `# Plan
## TODOs
- [x] 1. Finished task
## Final Verification Wave
- [ ] F1. Final review
`)
// when
const result = readCurrentTopLevelTask(planPath)
// then
expect(result).not.toBeNull()
expect(result?.key).toBe("final-wave:f1")
expect(result?.title).toBe("Final review")
})
})
describe("getPlanProgress", () => {
test("should count completed and uncompleted checkboxes", () => {
// given - plan file with checkboxes
+48 -1
View File
@@ -6,7 +6,7 @@
import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync } from "node:fs"
import { dirname, join, basename } from "node:path"
import type { BoulderState, PlanProgress } from "./types"
import type { BoulderState, PlanProgress, TaskSessionState } from "./types"
import { BOULDER_DIR, BOULDER_FILE, PROMETHEUS_PLANS_DIR } from "./constants"
export function getBoulderFilePath(directory: string): string {
@@ -29,6 +29,9 @@ export function readBoulderState(directory: string): BoulderState | null {
if (!Array.isArray(parsed.session_ids)) {
parsed.session_ids = []
}
if (!parsed.task_sessions || typeof parsed.task_sessions !== "object" || Array.isArray(parsed.task_sessions)) {
parsed.task_sessions = {}
}
return parsed as BoulderState
} catch {
return null
@@ -82,6 +85,50 @@ export function clearBoulderState(directory: string): boolean {
}
}
export function getTaskSessionState(directory: string, taskKey: string): TaskSessionState | null {
const state = readBoulderState(directory)
if (!state?.task_sessions) {
return null
}
return state.task_sessions[taskKey] ?? null
}
export function upsertTaskSessionState(
directory: string,
input: {
taskKey: string
taskLabel: string
taskTitle: string
sessionId: string
agent?: string
category?: string
},
): BoulderState | null {
const state = readBoulderState(directory)
if (!state) {
return null
}
const taskSessions = state.task_sessions ?? {}
taskSessions[input.taskKey] = {
task_key: input.taskKey,
task_label: input.taskLabel,
task_title: input.taskTitle,
session_id: input.sessionId,
...(input.agent !== undefined ? { agent: input.agent } : {}),
...(input.category !== undefined ? { category: input.category } : {}),
updated_at: new Date().toISOString(),
}
state.task_sessions = taskSessions
if (writeBoulderState(directory, state)) {
return state
}
return null
}
/**
* Find Prometheus plan files for this project.
* Prometheus stores plans at: {project}/.sisyphus/plans/{name}.md
@@ -0,0 +1,77 @@
import { existsSync, readFileSync } from "node:fs"
import type { TopLevelTaskRef } from "./types"
const TODO_HEADING_PATTERN = /^##\s+TODOs\b/i
const FINAL_VERIFICATION_HEADING_PATTERN = /^##\s+Final Verification Wave\b/i
const SECOND_LEVEL_HEADING_PATTERN = /^##\s+/
const UNCHECKED_CHECKBOX_PATTERN = /^(\s*)[-*]\s*\[\s*\]\s*(.+)$/
const TODO_TASK_PATTERN = /^(\d+)\.\s+(.+)$/
const FINAL_WAVE_TASK_PATTERN = /^(F\d+)\.\s+(.+)$/i
type PlanSection = "todo" | "final-wave" | "other"
function buildTaskRef(
section: "todo" | "final-wave",
taskLabel: string,
): TopLevelTaskRef | null {
const pattern = section === "todo" ? TODO_TASK_PATTERN : FINAL_WAVE_TASK_PATTERN
const match = taskLabel.match(pattern)
if (!match) {
return null
}
const rawLabel = match[1]
const title = match[2].trim()
return {
key: `${section}:${rawLabel.toLowerCase()}`,
section,
label: rawLabel,
title,
}
}
export function readCurrentTopLevelTask(planPath: string): TopLevelTaskRef | null {
if (!existsSync(planPath)) {
return null
}
try {
const content = readFileSync(planPath, "utf-8")
const lines = content.split(/\r?\n/)
let section: PlanSection = "other"
for (const line of lines) {
if (SECOND_LEVEL_HEADING_PATTERN.test(line)) {
section = TODO_HEADING_PATTERN.test(line)
? "todo"
: FINAL_VERIFICATION_HEADING_PATTERN.test(line)
? "final-wave"
: "other"
}
const uncheckedTaskMatch = line.match(UNCHECKED_CHECKBOX_PATTERN)
if (!uncheckedTaskMatch) {
continue
}
if (uncheckedTaskMatch[1].length > 0) {
continue
}
if (section !== "todo" && section !== "final-wave") {
continue
}
const taskRef = buildTaskRef(section, uncheckedTaskMatch[2].trim())
if (taskRef) {
return taskRef
}
}
return null
} catch {
return null
}
}
+30
View File
@@ -18,6 +18,8 @@ export interface BoulderState {
agent?: string
/** Absolute path to the git worktree root where work happens */
worktree_path?: string
/** Preferred reusable subagent sessions keyed by current top-level plan task */
task_sessions?: Record<string, TaskSessionState>
}
export interface PlanProgress {
@@ -28,3 +30,31 @@ export interface PlanProgress {
/** Whether all tasks are done */
isComplete: boolean
}
export interface TaskSessionState {
/** Stable identifier for the current top-level plan task (e.g. todo:1 / final-wave:F1) */
task_key: string
/** Original task label from the plan file */
task_label: string
/** Full task title from the plan file */
task_title: string
/** Preferred reusable subagent session */
session_id: string
/** Agent associated with the task session, when known */
agent?: string
/** Category associated with the task session, when known */
category?: string
/** Last update timestamp */
updated_at: string
}
export interface TopLevelTaskRef {
/** Stable identifier for the current top-level plan task */
key: string
/** Task section in the Prometheus plan */
section: "todo" | "final-wave"
/** Original label token (e.g. 1 / F1) */
label: string
/** Full task title extracted from the checkbox line */
title: string
}