Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/storage/src/__tests__/runtime-policy-stores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3331,6 +3331,60 @@ describe('runtime policy stores', () => {
});
});

test('clears a stale onboarding intent when its connection id conflicts with the catalog', async () => {
await withInteractiveRoot(async ({ root, capability }) => {
const owner = await tryAcquireInteractiveRootOwner(capability);
assert.ok(owner);
if (!owner) return;
try {
const stores = await openInteractiveRuntimePolicyStoresForWrite(owner.lease);
const connection = await createConnection(
stores,
0,
connectionDraft('openai', 'openai', 'Stale onboarding'),
);
await writeFile(
join(root, 'runtime-policy-onboarding.json'),
`${JSON.stringify({
schemaVersion: 1,
connectionId: '11111111-1111-4111-8111-111111111111',
providerType: connection.providerType,
suppliedSecret: null,
enabledModelIds: connection.enabledModelIds,
discovery: {
models: [{ id: 'gpt-5' }],
source: 'fetched',
fetchedAt: 1_800_000_000_000,
},
invalidateLastTest: false,
})}\n`,
);
} finally {
await owner.close();
}

const successor = await tryAcquireInteractiveRootOwner(capability);
assert.ok(successor);
if (!successor) return;
try {
await openInteractiveRuntimePolicyStoresForWrite(successor.lease);
assert.equal(existsSync(join(root, 'runtime-policy-onboarding.json')), false);
} finally {
await successor.close();
}

const reopened = await tryAcquireInteractiveRootOwner(capability);
assert.ok(reopened);
if (!reopened) return;
try {
await openInteractiveRuntimePolicyStoresForWrite(reopened.lease);
assert.equal(existsSync(join(root, 'runtime-policy-onboarding.json')), false);
} finally {
await reopened.close();
}
});
});

test('interactive OAuth login commits only against its frozen connection and credential basis', async () => {
await withInteractiveOwner(async ({ root, stores }) => {
const claude = await createConnection(
Expand Down
13 changes: 13 additions & 0 deletions packages/storage/src/runtime-policy/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,11 @@ export class RuntimePolicyCoordinator {
await clearConnectionOnboardingIntent(root);
this.onboardingRecoveryRequired = false;
} catch (error) {
if (isObsoleteConnectionOnboardingIntent(error)) {
await clearConnectionOnboardingIntent(root);
this.onboardingRecoveryRequired = false;
return;
}
if (isCommitOutcomeUnknown(error)) throw error;
throw commitOutcomeUnknown('Connection onboarding recovery did not converge', error);
}
Expand Down Expand Up @@ -1391,6 +1396,14 @@ function isCommitOutcomeUnknown(error: unknown): error is RuntimePolicyStoreErro
return error instanceof RuntimePolicyStoreError && error.code === 'commit_outcome_unknown';
}

function isObsoleteConnectionOnboardingIntent(error: unknown): boolean {
return (
error instanceof RuntimePolicyStoreError &&
error.code === 'invalid_document' &&
error.message === 'Onboarding intent conflicts with the connection id'
);
}

function commonSemanticConnectionBasis(
prepared: PreparedConnectionMaterial,
): CommonSemanticConnectionBasis {
Expand Down