GeezerVison is showing github.com — fix(server): Claude task lists survive a server restart by TonybynMp4 · Pull Request #18398 · pingdotgg/t3code
fix(server): Claude task lists survive a server restart
#18398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.ts
(2)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
fix(server): Claude task lists survive a server restart
The Claude adapter kept each session's task list only in memory, while
Claude persists it per session on disk. After a server restart or a new
CLI process resuming the session, a TaskUpdate on an older task was
dropped and a TaskCreate replaced the drawer with just the new task.
The first task tool result a process sees for a session now starts from
Claude's stored list (<config dir>/tasks/<list id>/<id>.json, list id
being CLAUDE_CODE_TASK_LIST_ID or the session id). A missing or
unreadable store falls back to the old in-memory behavior.
Co-Authored-By: Claude Opus 5.5 <[email protected] >
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit fe6df54201146295e243501ce7655a2e38169f51
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -4010,7 +4010,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
}
const makeTaskToolHarness = Effect.gen(function* () {
const harness = yield* makeWakeHarness;
// Claude's on-disk task store; tests seed it to stand in for a list Claude
// kept from an earlier server process.
const configDir = yield* (yield* FileSystem.FileSystem).makeTempDirectoryScoped({
prefix: "t3-claude-tasks-",
});
const harness = yield* makeWakeHarnessWithOptions({
environment: { CLAUDE_CONFIG_DIR: configDir },
});
let sequence = 600;
let turn = 0;
const nextUuid = () => `00000000-0000-4000-8000-${String(++sequence).padStart(12, "0")}`;
Expand Down
Expand Up
@@ -4046,7 +4053,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
harness.events.flatMap((event) =>
event.type === "plan.updated" && event.plan.kind === "todo_list" ? [event.plan] : [],
);
return { startTurn, call, finishTurn, plans };
return { configDir, startTurn, call, finishTurn, plans };
});
const createTask = (id: string, subject: string): TaskToolCall => ({
Expand Down
Expand Up
@@ -4208,6 +4215,63 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);
it.effect("starts from the task list Claude kept for the session before a restart", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const harness = yield* makeTaskToolHarness;
const tasksDir = `${harness.configDir}/tasks/${WAKE_NATIVE_SESSION}`;
yield* fileSystem.makeDirectory(tasksDir, { recursive: true });
const task = (id: string, subject: string, status: string, metadata?: object) =>
fileSystem.writeFileString(
`${tasksDir}/${id}.json`,
JSON.stringify({
id,
subject,
description: "",
status,
blocks: [],
blockedBy: [],
metadata,
}),
);
yield* task("10", "Ship", "pending");
yield* task("1", "Inspect", "completed");
// Claude already wrote the TaskUpdate below before T3 sees its result.
yield* task("2", "Implement", "completed");
yield* task("3", "Internal", "pending", { _internal: true });
yield* fileSystem.writeFileString(`${tasksDir}/4.json`, "{");
yield* fileSystem.writeFileString(`${tasksDir}/.highwatermark`, "10");
yield* fileSystem.writeFileString(`${tasksDir}/.lock`, "");
yield* harness.startTurn();
yield* harness.call(updateTask({ taskId: "2", status: "completed" }));
yield* harness.call(createTask("11", "Verify"));
yield* harness.finishTurn();
assert.deepEqual(
harness.plans().map((plan) => plan.steps),
[
[
{ id: "task-1", text: "Inspect", status: "completed" },
{ id: "task-2", text: "Implement", status: "completed" },
{ id: "task-10", text: "Ship", status: "pending" },
],
[
{ id: "task-1", text: "Inspect", status: "completed" },
{ id: "task-2", text: "Implement", status: "completed" },
{ id: "task-10", text: "Ship", status: "pending" },
{ id: "task-11", text: "Verify", status: "pending" },
],
],
);
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
),
),
);
it.effect.each(["success false", "error result", "unknown id"] as const)(
"does not emit a plan update for TaskUpdate with %s",
(reason) =>
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -3308,6 +3308,64 @@ function applyClaudeTaskTool(
return new Map(tasks).set(id, { text, status });
}
const CLAUDE_TASK_TOOL_NAMES = new Set(["TaskCreate", "TaskUpdate", "TaskList"]);
const CLAUDE_TASK_FILE_NAME = /^\d+\.json$/;
const decodeClaudeTaskFile = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
/**
* Reads the task list Claude persisted for a session, in id order. Claude
* keeps it at `<config dir>/tasks/<list id>/<task id>.json`, where the list id
* is `CLAUDE_CODE_TASK_LIST_ID` or else the session id, so a list started
* before a server restart is still the one later task tools edit. The format
* is Claude's and undocumented: anything missing or unreadable reads as an
* empty list rather than failing the turn.
*/
const readClaudeTaskList = Effect.fnUntraced(
function* (input: {
readonly fileSystem: FileSystem.FileSystem;
readonly path: Path.Path;
readonly environment: NodeJS.ProcessEnv;
readonly cwd: string | null | undefined;
readonly nativeThreadId: string;
}) {
const { fileSystem, path } = input;
const configDirOverride = input.environment.CLAUDE_CONFIG_DIR?.trim();
// The CLI resolves a relative config dir against its own cwd.
const configDir = configDirOverride
? path.resolve(input.cwd ?? ".", configDirOverride)
: path.join(yield* HostProcess.HomeDirectory, ".claude");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
const listId = input.environment.CLAUDE_CODE_TASK_LIST_ID?.trim() || input.nativeThreadId;
const directory = path.join(configDir, "tasks", listId.replace(/[^a-zA-Z0-9_-]/g, "-"));
const names = (yield* fileSystem.readDirectory(directory)).filter((name) =>
CLAUDE_TASK_FILE_NAME.test(name),
);
const tasks = yield* Effect.forEach(names, (name) =>
fileSystem.readFileString(path.join(directory, name)).pipe(
Effect.map((contents) => {
const task = Option.getOrUndefined(decodeClaudeTaskFile(contents));
const id = claudeTaskString(task, "id");
const text = claudeTaskString(task, "subject");
const status = claudeTaskStatus(claudeTaskField(task, "status"));
// Claude hides `_internal` tasks from TaskList; so do we.
const internal = claudeTaskField(claudeTaskField(task, "metadata"), "_internal");
return id === undefined || text === undefined || status === undefined || internal
? []
: [{ id, text, status }];
}),
Effect.orElseSucceed(() => []),
),
);
const list: ClaudeTaskList = new Map(
tasks
.flat()
.toSorted((left, right) => Number(left.id) - Number(right.id))
.map(({ id, text, status }) => [id, { text, status }]),
);
return list;
},
Effect.orElseSucceed((): ClaudeTaskList => new Map()),
);
function claudeTaskSteps(tasks: ClaudeTaskList): ReadonlyArray<OrchestrationV2PlanStep> {
return [...tasks].map(([id, task]) => ({ id: `task-${id}`, ...task }));
}
Expand Down
Expand Up
@@ -7391,12 +7449,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
// A subagent's task tools must not replace the parent's list.
if (parentToolUseId === null && !isClaudeToolResultError(toolResult)) {
const taskLists = yield* Ref.get(taskListsByNativeThread);
const taskList = applyClaudeTaskTool(
taskLists.get(liveQuery.nativeThreadId) ?? new Map(),
toolCall.toolName,
claudeNativeToolInputValue(toolCall.input),
claudeNativeToolOutputValue(output),
);
const knownTaskList = taskLists.get(liveQuery.nativeThreadId);
// This process has not seen the session's list yet (a server
// restart, or a resumed session): start from Claude's own copy.
// It already includes this call, so it is news even when
// applying the call changes nothing.
const storedTaskList =
knownTaskList === undefined && CLAUDE_TASK_TOOL_NAMES.has(toolCall.toolName)
? yield* readClaudeTaskList({
fileSystem,
path,
environment: adapterOptions.environment,
cwd: context.input.runtimePolicy.cwd,
nativeThreadId: liveQuery.nativeThreadId,
})
: undefined;
const taskList =
applyClaudeTaskTool(
knownTaskList ?? storedTaskList ?? new Map(),
toolCall.toolName,
claudeNativeToolInputValue(toolCall.input),
claudeNativeToolOutputValue(output),
) ?? (storedTaskList?.size ? storedTaskList : undefined);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
if (taskList !== undefined) {
yield* Ref.set(
taskListsByNativeThread,
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.