fix(code): never lose a local task's initial prompt - #3396
Conversation
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
|
Reviews (1): Last reviewed commit: "fix(code): never lose a local task's ini..." | Re-trigger Greptile |
| auth, | ||
| logResult, | ||
| ); | ||
| if (reconnected) { | ||
| await this.resendPendingPromptIfNeeded( | ||
| taskId, | ||
| convertStoredEntriesToEvents(logResult.rawEntries), | ||
| ); | ||
| } |
There was a problem hiding this comment.
When reconnectToLocalSession subscribes to live events before this replay check runs, a delayed live session/prompt echo can start clearing the durable prompt while resendPendingPromptIfNeeded still sees the old replayed log without that echo. Because the clear is fire-and-forget, the durable read can still return the prompt and send it again, causing the local task to process the initial prompt twice.
The initial prompt for a local task was only held in memory, so if the agent hadn't produced its first response yet and the user looked away (app backgrounded, reloaded, crashed, or a transient connect failure exhausted its silent retries), the prompt was lost and the task never started. Persist the prompt durably in the workspace-server task_metadata table keyed by task id the moment the task run is created, re-send it on resume when the agent hasn't consumed it (detected via the session/prompt echo in the replayed log), and clear it once consumed. clearSessionError now falls back to the durable copy so Retry and auto-retry recover after a reload wiped the in-memory session. Cloud tasks are unaffected — they self-fetch their prompt server-side. Generated-By: PostHog Code Task-Id: a4d03ce4-bd03-46db-8893-8a110e54e865
f4d9f22 to
e4d21d3
Compare
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
|
This PR has had no activity for 7 days and has been marked stale. We are moving to the monorepo and tightening PR staleness in preparation, so it will be closed in 7 days if no further activity occurs. |
|
Closing stale PRs ahead of Friday's monorepo migration. If this is a mistake, rebase and reopen for review. |
Problem
When creating a local task, the initial prompt was only ever held in memory. If the agent hadn't produced its first response yet and the user looked away — the app was backgrounded, reloaded, crashed, or a transient connect/
sendPromptfailure silently exhausted its retries — the prompt was lost and the task never started.Root cause (
packages/core/src/sessions/sessionService.ts):createNewLocalSessionstored the prompt only on the in-memory zustand session and delivered it viasendPrompt; the session store has no persistence.client.createTaskRunwas called with no options, so the deliverable prompt never reached the server either.task.latest_run.idstill exists, so the resume path (routeLocalConnect→resume-existing→reconnectToLocalSession) never re-sent the prompt.Fix
Persist the prompt durably in the workspace-server
task_metadataSQLite table, keyed by task id, and wire re-send/clear into the local session flow:pendingInitialPromptcolumn ontask_metadata+ migration0017.WorkspaceMetadataServiceset/get/clear passthroughs, Zod schemas, and one-line tRPC forwards.sessionService:ContentBlock[]) right after the task run is created, before spawning the agent — never aborting creation on a persistence failure.resendPendingPromptIfNeededre-sends the stored prompt unless the replayed log already contains thesession/promptecho (the agent's "received it" signal, preventing double-send).handleSessionEvent, independent of in-memory state.clearSessionErrorfalls back to the durable copy so Retry and the silent auto-retry path recover after a reload.Cloud tasks are unaffected — they self-fetch their prompt server-side and never enter
createNewLocalSession.Testing
pnpm typecheck(core, workspace-server, host-router): clean.noRestrictedImports.task-metadata-repository.test.ts(round-trip / clear / non-clobbering) andinitialPromptPersistence.test.ts(echo-detection, resend-once, clear-on-echo, durable retry fallback).Created with PostHog Code