🪴 fix: Restore Native Workspace Selection (#15771)

This commit is contained in:
Danny Avila 2026-09-09 02:37:52 -04:00 • committed by GitHub
parent a8087ce454
commit 2c07e28311
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 86 additions and 16 deletions

View file

@ -119,14 +119,40 @@ describe('useCodeWorkspace', () => {
expect(result.current.state).toBe('unavailable');
});
it.each([false, undefined])('rejects non-stateful worker support: %s', (statefulWorkspace) => {
const statuses = mockStatus();
statuses[0].data.statefulWorkspace = statefulWorkspace;
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
const { result } = renderHook(() => useCodeWorkspace(conversation([selection])));
it.each([false, undefined])(
'selects native roots without runtime sessions: %s',
(statefulWorkspace) => {
const statuses = mockStatus();
statuses[0].data.statefulWorkspace = statefulWorkspace;
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
const { result } = renderHook(() => useCodeWorkspace(conversation([selection])));
expect(result.current.state).toBe('ready');
expect(result.current.selections).toEqual([selection]);
expect(result.current.resolveSelections([selection])).toEqual([selection]);
},
);
it('automatically selects a sole native root without runtime sessions', () => {
mockStatus()[0].data.statefulWorkspace = false;
const { result } = renderHook(() => useCodeWorkspace(conversation()));
expect(result.current.state).toBe('ready');
expect(result.current.selections).toEqual([
{ environmentId: 'personal-vm', workspaceId: 'project-a' },
]);
});
it.each(['offline', 'starting'])('rejects a native worker that is %s', (status) => {
Object.assign(mockStatus()[0].data, { status, statefulWorkspace: false });
const { result } = renderHook(() => useCodeWorkspace(conversation()));
expect(result.current.state).toBe('unavailable');
expect(result.current.selections).toBeUndefined();
expect(result.current.resolveSelections([selection])).toBeUndefined();
});
it('rejects a ready native worker without advertised roots', () => {
Object.assign(mockStatus()[0].data, { statefulWorkspace: false, workspaces: undefined });
const { result } = renderHook(() => useCodeWorkspace(conversation()));
expect(result.current.state).toBe('unsupported');
expect(result.current.selections).toBeUndefined();
});
it('requires an explicit choice when several workspaces are advertised', () => {

View file

@ -70,12 +70,7 @@ function resolveEnvironmentSelection({
stored?: CodeWorkspaceSelection;
hasStoredSelections: boolean;
}): CodeWorkspaceSelection | undefined {
if (
status?.status !== 'ready' ||
status.statefulWorkspace !== true ||
status.environmentId !== environment.id
)
return undefined;
if (status?.status !== 'ready' || status.environmentId !== environment.id) return undefined;
if (stored != null && workspaces.some(({ id }) => id === stored.workspaceId)) {
return { environmentId: environment.id, workspaceId: stored.workspaceId };
}
@ -160,7 +155,6 @@ export default function useCodeWorkspace(
else if (
status.isError ||
status.data?.status !== 'ready' ||
status.data.statefulWorkspace !== true ||
status.data.environmentId !== environment.id
) {
state = 'unavailable';

View file

@ -283,7 +283,7 @@ describe('resolveCodeExecutionWorkspaceContext', () => {
delete process.env.TEST_CODE_CAPABILITY_TOKEN;
});
function workspaceStatus(workspaces: unknown[]): Response {
function workspaceStatus(workspaces: unknown[], statefulWorkspace: boolean = true): Response {
return new Response(
JSON.stringify({
protocolVersion: 1,
@ -292,7 +292,7 @@ describe('resolveCodeExecutionWorkspaceContext', () => {
ready: true,
leaseExpiresInMs: 45_000,
capabilities: {
statefulWorkspace: true,
statefulWorkspace,
sandboxProfile: 'native-srt',
runtimes: ['bash'],
workspaceTools: {
@ -333,6 +333,55 @@ describe('resolveCodeExecutionWorkspaceContext', () => {
});
});
it('admits native workspace tools without enabling programmatic runtime execution', async () => {
jest
.spyOn(globalThis, 'fetch')
.mockResolvedValue(workspaceStatus([{ id: 'docs', operations: ['read_file'] }], false));
const resolved = await resolveCodeExecutionWorkspaceContext({
context,
requestedSelections: [{ environmentId: 'personal', workspaceId: 'docs' }],
environments,
getAppConfig,
});
expect(resolved.codeWorkspace).toEqual({
environmentId: 'personal',
workspaceId: 'docs',
operations: ['read_file'],
});
expect(await supportsProgrammaticCodeExecution(context, environments, getAppConfig)).toBe(
false,
);
expect(await supportsProgrammaticCodeExecution(resolved, environments, getAppConfig)).toBe(
false,
);
});
it.each([false, true])(
'fails closed without native workspace capabilities (ready: %s)',
async (ready) => {
jest.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(
JSON.stringify({
protocolVersion: 1,
workerId: 'worker',
online: true,
ready,
leaseExpiresInMs: 45_000,
capabilities: { statefulWorkspace: false, sandboxProfile: 'native-srt', runtimes: [] },
}),
),
);
await expect(
resolveCodeExecutionWorkspaceContext({
context,
requestedSelections: [{ environmentId: 'personal', workspaceId: 'docs' }],
environments,
getAppConfig,
}),
).rejects.toMatchObject({ reason: ready ? 'unsupported' : 'worker_unavailable' });
},
);
it.each(['worker', 'replacement', undefined])(
'pins deployment worker identity: %s',
async (workerId) => {

View file

@ -138,7 +138,8 @@ export async function resolveCodeExecutionWorkspaceContext({
);
throw new CodeWorkspaceSelectionError('worker_unavailable');
}
if (status.status !== 'ready' || status.statefulWorkspace !== true) {
/** Named workspace tools enforce their roots independently of runtime sessions. */
if (status.status !== 'ready') {
throw new CodeWorkspaceSelectionError('worker_unavailable');
}
if (!status.workspaces || !status.operations) {