-
Notifications
You must be signed in to change notification settings - Fork 0
fix(acp): settle sessions before agent removal #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import type { PendingSessionInputRecord } from "@argos/shared/types/agent-interface"; | ||
|
|
||
| /** | ||
| * Settlement for session ownership changes (delete / move / agent removal). | ||
| * | ||
| * Mirrors the upstream DeepChat fix for "allow uninstall while disabled" | ||
| * (ThinkInAIXYZ/deepchat#2188), re-implemented natively for Argos' daemon-owned | ||
| * session architecture: | ||
| * | ||
| * 1. Discard queue-mode pending inputs — they belong to no turn yet and would | ||
| * otherwise leak onto the next owner. Steer-mode inputs are kept on purpose: | ||
| * they are conversation facts, and a cancelled run cannot claim them because | ||
| * `cancelGeneration` suppresses the pending-input drain. | ||
| * 2. Cancel an active generation and wait (bounded) for the session status to | ||
| * leave `generating` — cancellation settles asynchronously, so proceeding | ||
| * immediately would race the runtime. | ||
| * 3. Purge durable ACP bindings (`acp_sessions` rows) best-effort so the ACP | ||
| * uninstall guard becomes accurate after the sessions are gone. | ||
| */ | ||
|
|
||
| export interface SettleSessionHost { | ||
| getSession(sessionId: string): Promise<{ status?: string | null } | null>; | ||
| listPendingInputs(sessionId: string): Promise<PendingSessionInputRecord[]>; | ||
| deletePendingInput(sessionId: string, itemId: string): Promise<void>; | ||
| cancelGeneration(sessionId: string): Promise<void>; | ||
| purgeAcpSessionData?(sessionId: string): Promise<void>; | ||
| } | ||
|
|
||
| export interface SettleSessionOptions { | ||
| /** Max time to wait for a cancelled generation to settle. Default 10s. */ | ||
| timeoutMs?: number; | ||
| /** Poll interval while waiting for settlement. Default 100ms. */ | ||
| pollIntervalMs?: number; | ||
| /** Injectable delay for tests. */ | ||
| delay?: (ms: number) => Promise<void>; | ||
| } | ||
|
|
||
| export interface SettleSessionResult { | ||
| cancelled: boolean; | ||
| discardedQueueInputIds: string[]; | ||
| } | ||
|
|
||
| const DEFAULT_TIMEOUT_MS = 10_000; | ||
| const DEFAULT_POLL_INTERVAL_MS = 100; | ||
|
|
||
| export async function settleSessionForOwnershipChange( | ||
| sessionId: string, | ||
| host: SettleSessionHost, | ||
| options: SettleSessionOptions = {}, | ||
| ): Promise<SettleSessionResult> { | ||
| const delay = options.delay ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))); | ||
| const discardedQueueInputIds = await discardQueueInputs(sessionId, host); | ||
|
|
||
| let cancelled = false; | ||
| if ((await currentStatus(sessionId, host)) === "generating") { | ||
| cancelled = true; | ||
| await host.cancelGeneration(sessionId); | ||
| await waitForSettle(sessionId, host, delay, options); | ||
| } | ||
|
|
||
| // Fail-closed: a durable binding that cannot be purged would leave the ACP | ||
| // uninstall guard stuck with no conversation left to retry against, so the | ||
| // ownership change must abort instead of proceeding. | ||
| await host.purgeAcpSessionData?.(sessionId); | ||
|
|
||
| return { cancelled, discardedQueueInputIds }; | ||
| } | ||
|
|
||
| async function currentStatus(sessionId: string, host: SettleSessionHost): Promise<string | null> { | ||
| // Propagate read failures: treating a transient error as "not generating" | ||
| // could skip cancellation and race a still-running turn. | ||
| const session = await host.getSession(sessionId); | ||
| return session?.status ?? null; | ||
| } | ||
|
|
||
| async function discardQueueInputs(sessionId: string, host: SettleSessionHost): Promise<string[]> { | ||
| // Fail-closed: if queued inputs cannot be enumerated, the ownership change | ||
| // must stop — leaked inputs would drain under the new owner. | ||
| const inputs = await host.listPendingInputs(sessionId); | ||
| const discarded: string[] = []; | ||
| for (const input of inputs) { | ||
| if (input.mode !== "queue") continue; | ||
| await host.deletePendingInput(sessionId, input.id); | ||
| discarded.push(input.id); | ||
| } | ||
| return discarded; | ||
| } | ||
|
|
||
| async function waitForSettle( | ||
| sessionId: string, | ||
| host: SettleSessionHost, | ||
| delay: (ms: number) => Promise<void>, | ||
| options: SettleSessionOptions, | ||
| ): Promise<void> { | ||
| const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS; | ||
| const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS; | ||
| const deadline = Date.now() + timeoutMs; | ||
| while (Date.now() < deadline) { | ||
| await delay(pollIntervalMs); | ||
| if ((await currentStatus(sessionId, host)) !== "generating") { | ||
| return; | ||
| } | ||
| } | ||
| throw new Error(`Session ${sessionId} did not stop before ownership change.`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Failures to list or delete queued inputs are swallowed, so a move continues as though cleanup succeeded. Moving a session preserves its pending-input rows; therefore, any queue input left behind remains attached to the same session ID and can be consumed by the newly assigned agent even though it was intended for the previous agent. The move should stop when queued inputs cannot be enumerated or removed.
Prompt To Fix With AI