Repository navigation
feat(harness): reopen Assistant runtimes paused [SAP-3292] #1000
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@sapiom/harness": patch | ||
| --- | ||
|
|
||
| Bind Assistant requests to the current Studio lifecycle and restore newly started runtimes paused. Reopening Studio does not automatically recover earlier work; an explicit message enables execution. Include lifecycle changes in ordered Studio state updates and fence both managed engines during shutdown. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { expect, it, vi } from "vitest"; | ||
| import type { AssistantLifecycle } from "../shared/assistant-session.js"; | ||
| import { | ||
| parseAssistantState, | ||
| type AssistantStateSnapshot, | ||
| } from "../shared/assistant-state.js"; | ||
| import { createAssistantStateProjection } from "./assistant-state-projection.js"; | ||
|
|
||
| const state: AssistantLifecycle = { | ||
| version: 1, | ||
| harnessSessionId: "studio-a", | ||
| revision: 2, | ||
| lifecycle: "ended", | ||
| execution: "paused", | ||
| updatedAt: 10, | ||
| }; | ||
| const runtime: AssistantStateSnapshot = { | ||
| hostInstanceId: "host-a", | ||
| authorityRevision: "grant-a", | ||
| revision: 45, | ||
| enabled: true, | ||
| sessions: [], | ||
| }; | ||
| it("orders lifecycle-only updates with observations and clears disabled projections", () => { | ||
| let changedHost!: () => void, changedLifecycle!: () => void; | ||
| let native = runtime; | ||
| let rows = [state]; | ||
| const stopHost = vi.fn(), | ||
| stopLifecycle = vi.fn(), | ||
| publish = vi.fn(); | ||
| const projection = createAssistantStateProjection( | ||
| { | ||
| getAssistantState: () => native, | ||
| subscribeAssistantState: (fn) => { | ||
| changedHost = fn; | ||
| return stopHost; | ||
| }, | ||
| }, | ||
| { | ||
| snapshot: () => rows, | ||
| subscribe: (fn) => { | ||
| changedLifecycle = fn; | ||
| return stopLifecycle; | ||
| }, | ||
| }, | ||
| publish, | ||
| ); | ||
| const initial = projection.get(); | ||
| expect(parseAssistantState(initial)).toEqual(initial); | ||
| rows = [{ ...state, revision: 3, lifecycle: "open" }]; | ||
| changedLifecycle(); | ||
| const resumed = publish.mock.lastCall![0] as AssistantStateSnapshot; | ||
| expect(resumed.revision).toBeGreaterThan(initial.revision); | ||
| expect(resumed.lifecycles?.[0]?.lifecycle).toBe("open"); | ||
| native = { | ||
| ...native, | ||
| enabled: false, | ||
| authorityRevision: "grant-b", | ||
| revision: 46, | ||
| }; | ||
| changedHost(); | ||
| const revoked = publish.mock.lastCall![0] as AssistantStateSnapshot; | ||
| expect(revoked.revision).toBeGreaterThan(resumed.revision); | ||
| expect(revoked.lifecycles).toEqual([]); | ||
| expect(parseAssistantState(revoked)).toEqual(revoked); | ||
| projection.dispose(); | ||
| expect(stopHost).toHaveBeenCalledOnce(); | ||
| expect(stopLifecycle).toHaveBeenCalledOnce(); | ||
| }); | ||
| it.each([ | ||
| [{ ...state, lease: "private" }], | ||
| [{ ...state, execution: "enabled" }], | ||
| [{ ...state, revision: -1 }], | ||
| [{ ...state, harnessSessionId: "../private" }], | ||
| [state, state], | ||
| null, | ||
| ])( | ||
| "rejects malformed or contradictory lifecycle projection %j", | ||
| (lifecycles) => { | ||
| expect(parseAssistantState({ ...runtime, lifecycles })).toBeNull(); | ||
| }, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import type { AssistantStateSnapshot } from "../shared/assistant-state.js"; | ||
| import type { AssistantLifecycleCoordinator } from "./assistant-lifecycle.js"; | ||
| import type { OpenCodeHost } from "./opencode-host.js"; | ||
|
|
||
| /** One ordering clock for runtime observations and durable lifecycle changes. */ | ||
| export function createAssistantStateProjection( | ||
| host: Pick<OpenCodeHost, "getAssistantState" | "subscribeAssistantState">, | ||
| lifecycle: Pick<AssistantLifecycleCoordinator, "snapshot" | "subscribe">, | ||
| publish: (snapshot: AssistantStateSnapshot) => void, | ||
| ) { | ||
| let revision = 0; | ||
| const get = (): AssistantStateSnapshot => { | ||
| // Reading access may synchronously revoke it and notify host subscribers. | ||
| const runtime = host.getAssistantState(); | ||
| return { | ||
| ...runtime, | ||
| revision, | ||
| lifecycles: runtime.enabled ? lifecycle.snapshot() : [], | ||
| }; | ||
| }; | ||
| const changed = () => { | ||
| revision++; | ||
| publish(get()); | ||
| }; | ||
| const stopHost = host.subscribeAssistantState(changed); | ||
| const stopLifecycle = lifecycle.subscribe(changed); | ||
| return { | ||
| get, | ||
| dispose: () => { | ||
| stopHost(); | ||
| stopLifecycle(); | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,6 +197,8 @@ import { | |
| import { IngestCredentialRegistry } from "../core/ingest-credentials.js"; | ||
| import { AssistantAccess } from "../core/assistant-access.js"; | ||
| import { AssistantSessionStore } from "../core/assistant-session-store.js"; | ||
| import { AssistantLifecycleCoordinator } from "../core/assistant-lifecycle.js"; | ||
| import { createAssistantStateProjection } from "../core/assistant-state-projection.js"; | ||
| import { OpenCodeAssociations } from "../core/opencode-association.js"; | ||
| import { OpenCodeHost, type HostedOpenCode } from "../core/opencode-host.js"; | ||
| import { AssistantRecordStore } from "../core/assistant-record-store.js"; | ||
|
|
@@ -3566,10 +3568,15 @@ export const startServer = async ( | |
| stateRoot: statePaths.root, | ||
| authorize: authorizeAssistantWorkspace, | ||
| }); | ||
| const getAssistantState = () => openCodeHost.getAssistantState(); | ||
| const unsubscribeAssistant = openCodeHost.subscribeAssistantState(() => | ||
| bus.publish({ type: "assistant.state", snapshot: getAssistantState() }), | ||
| const openCodeAssociations = new OpenCodeAssociations(assistantSessions); | ||
| const assistantLifecycle = new AssistantLifecycleCoordinator({ store: assistantSessions, host: openCodeHost, associations: openCodeAssociations }); | ||
|
Contributor
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. 🔴 Closed sessions retain Assistant runtimes Closing a Studio session never calls Learn moreThe lifecycle coordinator owns the fence that invalidates leases, retires the native Assistant process, and persists Example: Alice opens Assistant, receives a lease, then closes the Studio tab. The DELETE request closes the Terminal, but the mounted pane can still send a leased Assistant request. The expected result is immediate rejection and retirement of both runtimes. Recommended fix: Add one close orchestration boundary used by the REST deletion route. It must call Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| // Hydrate lifecycle headers without launching runtimes. A corrupt entry fails | ||
| // closed when selected rather than preventing unrelated sessions from loading. | ||
| await Promise.allSettled(sessionManager.list().map((session) => assistantLifecycle.describe(session.id))); | ||
| const assistantProjection = createAssistantStateProjection(openCodeHost, assistantLifecycle, (snapshot) => | ||
| bus.publish({ type: "assistant.state", snapshot }), | ||
| ); | ||
| const getAssistantState = assistantProjection.get; | ||
| const app: Express = express(); | ||
| app.disable("x-powered-by"); | ||
| app.use("/opencode-runtime", openCodeBridge.router); | ||
|
|
@@ -3585,8 +3592,12 @@ export const startServer = async ( | |
| getEnvironment: () => assistantAccess.get()?.environment ?? null, | ||
| loadSystemPrompt: options.loadSystemPrompt, | ||
| }), | ||
| new OpenCodeAssociations(assistantSessions), | ||
| new OpenCodeFinalResponse({ onPersisted: async (hosted) => { await recordCaptures.get(hosted)?.checkpoint(); } }), | ||
| openCodeAssociations, | ||
| new OpenCodeFinalResponse({ | ||
| assertCurrent: (hosted) => assistantLifecycle.assertRuntime(hosted, true), | ||
| onPersisted: async (hosted) => { await recordCaptures.get(hosted)?.checkpoint(); }, | ||
| }), | ||
| assistantLifecycle, | ||
| ), | ||
| ); | ||
|
|
||
|
|
@@ -4321,11 +4332,14 @@ export const startServer = async ( | |
|
|
||
| credentialStoreObserver?.close(); | ||
| unsubscribeCredentialChanges(); | ||
| // Fence both engines synchronously. Native ownership cleanup can wait on | ||
| // process proof and must not leave Terminal admission open during that wait. | ||
| assistantLifecycle.beginShutdown(); | ||
| sessionManager.beginShutdown(); | ||
| assistantAccess.close(); | ||
| await settle(() => openCodeHost.close()); | ||
| unsubscribeAssistant(); | ||
| const nativeClosing = settle(() => openCodeHost.close()); | ||
| assistantProjection.dispose(); | ||
| openCodeBridge.close(); | ||
| await settle(() => sessionManager.beginShutdown()); | ||
| const bootstrapClosing = settle(() => projectBootstrap?.close()); | ||
| const registrationClosing = settle(() => createdAgentRegistration.close()); | ||
| coordinatorActive = false; | ||
|
|
@@ -4353,6 +4367,7 @@ export const startServer = async ( | |
| // socket if an adapter misses its own exit acknowledgement. | ||
| const SHUTDOWN_KILL_TIMEOUT_MS = 5_000; | ||
| const killsSettled = Promise.all([ | ||
| nativeClosing, | ||
| settle(() => sessionManager.killAll()), | ||
| settle(() => taskManager.killAll()), | ||
| ]).then(() => {}); | ||
|
|
||
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.
🔍 Lifecycle projection has no consumer
The server publishes
lifecycles, but the SPA never reads them. Mounted panes therefore cannot react to ordered lifecycle changes without another request.Was this helpful? React with 👍 or 👎 to provide feedback.