Repository navigation
ensure draft chat spec is updated when update_current_session tool is called #1342
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ export interface UseDraftAgentSpecResult { | |
| updateAgentSpec: (update: AgentSpecUpdate) => void; | ||
| flushAgentSpec: () => Promise<void>; | ||
| adoptAgentSpec: (request: { agentSpec: AgentSpec; updatedAt?: string }) => void; | ||
| /** Re-read the stored spec after the backend changed it (e.g. the agent edited its own session). */ | ||
| reloadAgentSpec: () => Promise<void>; | ||
| takeTurnHeaderTimestamp: () => Promise<string | undefined>; | ||
| } | ||
|
|
||
|
|
@@ -223,6 +225,21 @@ export function useDraftAgentSpec({ | |
| [], | ||
| ); | ||
|
|
||
| const reloadAgentSpec = useCallback(async () => { | ||
| if (draftBridge == null || draftSessionId == null) { | ||
| return; | ||
| } | ||
| try { | ||
| const loaded = await draftBridge.getDraftAgentSpec(draftSessionId); | ||
| if (loaded == null || activeDraftIdRef.current !== draftSessionId) { | ||
| return; | ||
| } | ||
| adoptAgentSpec({ agentSpec: loaded }); | ||
| } catch (error) { | ||
| onError?.(error); | ||
| } | ||
| }, [adoptAgentSpec, draftBridge, draftSessionId, onError]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In-flight spec sync can clobber agent updatesMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 81d66c2. Configure here. |
||
|
|
||
| const takeTurnHeaderTimestamp = useCallback(async () => { | ||
| await flushPendingSpecSyncNow(); | ||
| const updatedAt = lastUpdatedAtRef.current; | ||
|
|
@@ -265,6 +282,7 @@ export function useDraftAgentSpec({ | |
| updateAgentSpec, | ||
| flushAgentSpec: flushPendingSpecSyncNow, | ||
| adoptAgentSpec, | ||
| reloadAgentSpec, | ||
| takeTurnHeaderTimestamp, | ||
| }), | ||
| [ | ||
|
|
@@ -276,6 +294,7 @@ export function useDraftAgentSpec({ | |
| specError, | ||
| flushPendingSpecSyncNow, | ||
| adoptAgentSpec, | ||
| reloadAgentSpec, | ||
| takeTurnHeaderTimestamp, | ||
| updateAgentSpec, | ||
| ], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import { | |
| collectPendingToolResponses, | ||
| derivePendingMcpAuth, | ||
| deriveSandboxId, | ||
| findLatestSessionSpecUpdate, | ||
| } from './collectPending.js'; | ||
| import { | ||
| buildUserMessageContent, | ||
|
|
@@ -156,6 +157,40 @@ function useTrueForgeAgentRuntimeImpl( | |
| const pendingMcpAuth = useMemo(() => derivePendingMcpAuth(messages), [messages]); | ||
| const sandboxId = useMemo(() => deriveSandboxId(messages), [messages]); | ||
|
|
||
| const sessionSpecUpdateId = useMemo(() => findLatestSessionSpecUpdate(messages), [messages]); | ||
| const handledSessionSpecUpdateIdRef = useRef<string | undefined>(undefined); | ||
| // Gate reloads on "has this session run in this mount", not on the current | ||
| // isRunning flag. Stream teardown often applies the tool result and clears | ||
| // isRunning in the same batch; marking the id handled while skipping reload | ||
| // would drop that update permanently. | ||
| const sawRunningInSessionRef = useRef(false); | ||
| const trackedSessionIdRef = useRef(sessionId); | ||
| if (trackedSessionIdRef.current !== sessionId) { | ||
| trackedSessionIdRef.current = sessionId; | ||
| sawRunningInSessionRef.current = false; | ||
| handledSessionSpecUpdateIdRef.current = undefined; | ||
| } | ||
| const reloadAgentSpec = draftSpec.reloadAgentSpec; | ||
| useEffect(() => { | ||
| if (isRunning) { | ||
| sawRunningInSessionRef.current = true; | ||
| } | ||
| }, [isRunning]); | ||
| useEffect(() => { | ||
| if (sessionSpecUpdateId === undefined || sessionSpecUpdateId === handledSessionSpecUpdateIdRef.current) { | ||
| return; | ||
| } | ||
| // Calls already in loaded history are reflected in the spec loaded with the session. | ||
| if (!sawRunningInSessionRef.current) { | ||
| handledSessionSpecUpdateIdRef.current = sessionSpecUpdateId; | ||
| return; | ||
| } | ||
| handledSessionSpecUpdateIdRef.current = sessionSpecUpdateId; | ||
| if (agent.mode === 'draft') { | ||
| void reloadAgentSpec(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paused turns skip the spec reloadMedium Severity The reload effect treats any Reviewed by Cursor Bugbot for commit 81d66c2. Configure here. |
||
| }, [agent.mode, reloadAgentSpec, sessionSpecUpdateId]); | ||
|
|
||
| const downloadSandboxFile = useCallback( | ||
| async ({ turnId, path }: { turnId: string; path: string }) => { | ||
| if (server.downloadSandboxFile == null) { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overlapping reloads can apply a stale spec
Medium Severity
reloadAgentSpecdoes not ignore stale in-flight fetches. Each finishedupdate_current_sessionstarts a new GET, andadoptAgentSpecalways applies whichever response returns last. A slower first fetch can overwrite a later spec, so the draft UI can miss the most recent agent edit.Additional Locations (1)
packages/assistant-ui-runtime/src/useTrueForgeAgentRuntime.ts#L178-L191Reviewed by Cursor Bugbot for commit 81d66c2. Configure here.