Skip to content

Commit e344153

Browse files
authored
fix(antigravity): check account access after installation (pingdotgg#32446)
1 parent 67fdc53 commit e344153

5 files changed

Lines changed: 215 additions & 60 deletions

File tree

‎apps/server/src/provider/providerInstallation.test.ts‎

Lines changed: 133 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
} from "@t3tools/contracts";
88
import * as HostProcess from "@t3tools/shared/HostProcess";
99
import * as Effect from "effect/Effect";
10+
import * as Deferred from "effect/Deferred";
11+
import * as SubscriptionRef from "effect/SubscriptionRef";
1012
import * as FileSystem from "effect/FileSystem";
1113
import * as Layer from "effect/Layer";
1214
import * as Path from "effect/Path";
@@ -15,8 +17,9 @@ import * as Stream from "effect/Stream";
1517
import * as ServerSettings from "../serverSettings.ts";
1618
import * as CodexInstallation from "./CodexInstallation.ts";
1719
import * as AntigravityInstallation from "./AntigravityInstallation.ts";
20+
import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
1821
import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
19-
import { makeProviderInstallation } from "./providerInstallation.ts";
22+
import * as ProviderInstallation from "./providerInstallation.ts";
2023
import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
2124
import * as ProviderRegistry from "./ProviderRegistry.ts";
2225

@@ -60,69 +63,145 @@ const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function*
6063
} = {},
6164
) {
6265
const calls: string[] = [];
66+
const refreshed = yield* Deferred.make<void>();
67+
const observed = yield* Deferred.make<void>();
68+
const listening = yield* Deferred.make<void>();
69+
const installationState = yield* SubscriptionRef.make(state);
6370
let protectedPaths: ReadonlyArray<string> = [];
6471
const configured = input.instance ?? instance();
65-
const router = yield* makeProviderInstallation().pipe(
66-
Effect.provide(
67-
Layer.mergeAll(
68-
ServerSettings.layerTest(input.settings),
69-
Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
70-
getInstance: (id) =>
71-
Effect.succeed(id === configured.instanceId ? configured : undefined),
72-
listInstances: Effect.succeed([configured]),
72+
const dependencies = Layer.mergeAll(
73+
ServerSettings.layerTest(input.settings),
74+
Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
75+
getInstance: (id) => Effect.succeed(id === configured.instanceId ? configured : undefined),
76+
listInstances: Effect.succeed([configured]),
77+
}),
78+
Layer.mock(ProviderRegistry.ProviderRegistry)({
79+
refreshInstance: () =>
80+
Effect.sync(() => {
81+
calls.push("refresh");
82+
return [];
83+
}).pipe(Effect.tap(() => Deferred.succeed(refreshed, undefined))),
84+
}),
85+
Layer.mock(CodexInstallation.CodexInstallation)({
86+
managedDirectory: "/unused-managed-codex",
87+
start: Effect.sync(() => {
88+
calls.push("codex-start");
89+
return { ...state, driver: ProviderDriverKind.make("codex") };
90+
}),
91+
cancel: () =>
92+
Effect.sync(() => {
93+
calls.push("codex-cancel");
94+
return { ...state, driver: ProviderDriverKind.make("codex") };
7395
}),
74-
Layer.mock(ProviderRegistry.ProviderRegistry)({
75-
refreshInstance: () =>
76-
Effect.sync(() => {
77-
calls.push("refresh");
78-
return [];
79-
}),
96+
state: Effect.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
97+
changes: Stream.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
98+
remove: (paths) =>
99+
Effect.sync(() => {
100+
protectedPaths = paths ?? [];
101+
calls.push("codex-remove");
80102
}),
81-
Layer.mock(CodexInstallation.CodexInstallation)({
82-
managedDirectory: "/unused-managed-codex",
83-
start: Effect.sync(() => {
84-
calls.push("codex-start");
85-
return { ...state, driver: ProviderDriverKind.make("codex") };
86-
}),
87-
cancel: () =>
88-
Effect.sync(() => {
89-
calls.push("codex-cancel");
90-
return { ...state, driver: ProviderDriverKind.make("codex") };
91-
}),
92-
state: Effect.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
93-
changes: Stream.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
94-
remove: (paths) =>
95-
Effect.sync(() => {
96-
protectedPaths = paths ?? [];
97-
calls.push("codex-remove");
98-
}),
103+
}),
104+
Layer.mock(AntigravityInstallation.AntigravityInstallation)({
105+
managedDirectory: "/unused-managed-runtime",
106+
start: Effect.sync(() => {
107+
calls.push("start");
108+
return { ...state, operationId: "install-1", phase: "downloading" as const };
109+
}).pipe(Effect.tap((started) => SubscriptionRef.set(installationState, started))),
110+
cancel: () =>
111+
Effect.sync(() => {
112+
calls.push("cancel");
113+
return state;
99114
}),
100-
Layer.mock(AntigravityInstallation.AntigravityInstallation)({
101-
managedDirectory: "/unused-managed-runtime",
102-
start: Effect.sync(() => {
103-
calls.push("start");
104-
return state;
105-
}),
106-
cancel: () =>
107-
Effect.sync(() => {
108-
calls.push("cancel");
109-
return state;
110-
}),
111-
state: Effect.succeed(state),
112-
changes: Stream.succeed(state),
113-
remove: (paths) =>
114-
Effect.sync(() => {
115-
protectedPaths = paths ?? [];
116-
calls.push("remove");
117-
}),
115+
state: Effect.succeed(state),
116+
changes: input.instance?.refreshModels
117+
? SubscriptionRef.changes(installationState).pipe(
118+
Stream.tap(() => Deferred.succeed(listening, undefined)),
119+
Stream.ensuring(Deferred.succeed(observed, undefined)),
120+
)
121+
: Stream.succeed(state),
122+
remove: (paths) =>
123+
Effect.sync(() => {
124+
protectedPaths = paths ?? [];
125+
calls.push("remove");
118126
}),
119-
),
120-
),
127+
}),
121128
);
122-
return { router, calls, protectedPaths: () => protectedPaths };
129+
const context = yield* Layer.build(ProviderInstallation.layer.pipe(Layer.provide(dependencies)));
130+
const connect = Effect.service(ProviderInstallation.ProviderInstallation).pipe(
131+
Effect.provide(context),
132+
);
133+
const router = yield* connect;
134+
return {
135+
router,
136+
connect,
137+
listening,
138+
calls,
139+
refreshed,
140+
observed,
141+
installationState,
142+
protectedPaths: () => protectedPaths,
143+
};
123144
});
124145

125146
describe("provider installation routing", () => {
147+
it.effect.each([
148+
{ phase: "succeeded", enabled: true, checkFails: false, expectedChecks: 1 },
149+
{ phase: "succeeded", enabled: true, checkFails: true, expectedChecks: 1 },
150+
{ phase: "succeeded", enabled: false, checkFails: false, expectedChecks: 0 },
151+
{ phase: "cancelled", enabled: true, checkFails: false, expectedChecks: 0 },
152+
{ phase: "failed", enabled: true, checkFails: false, expectedChecks: 0 },
153+
] as const)(
154+
"checks account access once after installation is $phase, enabled=$enabled, checkFails=$checkFails",
155+
({ phase, enabled, checkFails, expectedChecks }) =>
156+
Effect.gen(function* () {
157+
let checks = 0;
158+
const harness = yield* makeHarness({
159+
instance: Object.assign(instance(), {
160+
enabled,
161+
refreshModels: () =>
162+
Effect.sync(() => {
163+
checks++;
164+
}).pipe(
165+
Effect.andThen(
166+
checkFails
167+
? Effect.fail(
168+
new ProviderDriverError({
169+
driver,
170+
instanceId,
171+
detail: "Sign in required.",
172+
}),
173+
)
174+
: Effect.void,
175+
),
176+
),
177+
}),
178+
});
179+
yield* Effect.gen(function* () {
180+
const first = yield* harness.connect;
181+
const second = yield* harness.connect;
182+
yield* Effect.all([first.start({ instanceId }), second.start({ instanceId })], {
183+
concurrency: "unbounded",
184+
});
185+
yield* Deferred.await(harness.listening);
186+
}).pipe(Effect.scoped);
187+
assert.isFalse(yield* Deferred.isDone(harness.observed));
188+
assert.strictEqual(checks, 0);
189+
yield* SubscriptionRef.set(harness.installationState, {
190+
...state,
191+
operationId: "install-1",
192+
phase,
193+
installedVersion: phase === "succeeded" ? "1.5.1" : null,
194+
});
195+
yield* Deferred.await(harness.observed);
196+
if (expectedChecks > 0) yield* Deferred.await(harness.refreshed);
197+
assert.strictEqual(checks, expectedChecks);
198+
assert.deepEqual(
199+
harness.calls,
200+
expectedChecks > 0 ? ["start", "start", "refresh"] : ["start", "start"],
201+
);
202+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
203+
);
204+
126205
it.effect("keeps external installs manual without hiding shared install status", () =>
127206
Effect.gen(function* () {
128207
const harness = yield* makeHarness({

‎apps/server/src/provider/providerInstallation.ts‎

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import {
33
CodexSettings,
44
ProviderDriverKind,
55
type ProviderInstallCancelInput,
6+
type ProviderInstallState,
67
type ProviderInstanceId,
78
ProviderSetupError,
89
type ProviderSetupInput,
910
} from "@t3tools/contracts";
1011
import { resolveCommandPath } from "@t3tools/shared/shell";
12+
import * as Context from "effect/Context";
1113
import * as Effect from "effect/Effect";
14+
import * as Layer from "effect/Layer";
15+
import * as FileSystem from "effect/FileSystem";
16+
import * as Path from "effect/Path";
17+
import * as Option from "effect/Option";
1218
import * as Schema from "effect/Schema";
1319
import * as Stream from "effect/Stream";
1420

@@ -25,12 +31,34 @@ const hasBinaryPath = Schema.is(Schema.Struct({ binaryPath: Schema.String }));
2531
const decodeCodexSettings = Schema.decodeUnknownEffect(CodexSettings);
2632
const decodeAntigravitySettings = Schema.decodeUnknownEffect(AntigravitySettings);
2733

28-
export const makeProviderInstallation = Effect.fn("makeProviderInstallation")(function* () {
34+
export class ProviderInstallation extends Context.Service<
35+
ProviderInstallation,
36+
{
37+
readonly start: (
38+
input: ProviderSetupInput,
39+
) => Effect.Effect<ProviderInstallState, ProviderSetupError>;
40+
readonly cancel: (
41+
input: ProviderInstallCancelInput,
42+
) => Effect.Effect<ProviderInstallState, ProviderSetupError>;
43+
readonly subscribe: (
44+
input: ProviderSetupInput,
45+
) => Stream.Stream<ProviderInstallState, ProviderSetupError>;
46+
readonly remove: (
47+
input: ProviderSetupInput,
48+
) => Effect.Effect<ProviderInstallState, ProviderSetupError>;
49+
}
50+
>()("t3/provider/providerInstallation") {}
51+
52+
const make = Effect.fn("makeProviderInstallation")(function* () {
2953
const antigravityInstallation = yield* AntigravityInstallation.AntigravityInstallation;
3054
const codexInstallation = yield* CodexInstallation.CodexInstallation;
3155
const instances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
3256
const providers = yield* ProviderRegistry.ProviderRegistry;
3357
const settings = yield* ServerSettings.ServerSettingsService;
58+
const fileSystem = yield* FileSystem.FileSystem;
59+
const path = yield* Path.Path;
60+
const scope = yield* Effect.scope;
61+
const accountCheckOperations = new Map<ProviderInstanceId, string>();
3462

3563
const readEntries = Effect.fn("ProviderInstallation.readEntries")(function* (
3664
instanceId: ProviderInstanceId,
@@ -106,8 +134,47 @@ export const makeProviderInstallation = Effect.fn("makeProviderInstallation")(fu
106134
new ProviderSetupError({ instanceId, operation: error.operation, detail: error.detail });
107135

108136
const start = Effect.fn("ProviderInstallation.start")(function* (input: ProviderSetupInput) {
109-
const { installation } = yield* requireInstance(input.instanceId, "install", true);
110-
return yield* installation.start.pipe(Effect.mapError(failure(input.instanceId)));
137+
const { installation, driver } = yield* requireInstance(input.instanceId, "install", true);
138+
const started = yield* installation.start.pipe(Effect.mapError(failure(input.instanceId)));
139+
if (
140+
driver === ANTIGRAVITY &&
141+
started.operationId !== null &&
142+
accountCheckOperations.get(input.instanceId) !== started.operationId
143+
) {
144+
accountCheckOperations.set(input.instanceId, started.operationId);
145+
// Installation outlives the request and may finish while every client is disconnected.
146+
yield* installation.changes.pipe(
147+
Stream.filter(
148+
(state) =>
149+
state.operationId !== started.operationId ||
150+
state.phase === "succeeded" ||
151+
state.phase === "failed" ||
152+
state.phase === "cancelled",
153+
),
154+
Stream.runHead,
155+
Effect.flatMap(
156+
Effect.fnUntraced(function* (result) {
157+
if (
158+
Option.isNone(result) ||
159+
result.value.operationId !== started.operationId ||
160+
result.value.phase !== "succeeded"
161+
)
162+
return;
163+
const instance = yield* instances.getInstance(input.instanceId);
164+
if (
165+
instance?.driverKind !== ANTIGRAVITY ||
166+
!instance.enabled ||
167+
!instance.refreshModels
168+
)
169+
return;
170+
yield* instance.refreshModels().pipe(Effect.ignore({ log: true }));
171+
yield* providers.refreshInstance(input.instanceId);
172+
}),
173+
),
174+
Effect.forkIn(scope),
175+
);
176+
}
177+
return started;
111178
});
112179

113180
const cancel = Effect.fn("ProviderInstallation.cancel")(function* (
@@ -142,7 +209,10 @@ export const makeProviderInstallation = Effect.fn("makeProviderInstallation")(fu
142209
const binaryPath = entry.config.binaryPath.trim();
143210
const resolved = yield* resolveCommandPath(binaryPath, {
144211
env: yield* mergeProviderInstanceEnvironment(entry.environment),
145-
});
212+
}).pipe(
213+
Effect.provideService(FileSystem.FileSystem, fileSystem),
214+
Effect.provideService(Path.Path, path),
215+
);
146216
return resolved === null ? [binaryPath] : [binaryPath, resolved];
147217
}),
148218
);
@@ -160,3 +230,5 @@ export const makeProviderInstallation = Effect.fn("makeProviderInstallation")(fu
160230

161231
return { start, cancel, subscribe, remove };
162232
});
233+
234+
export const layer = Layer.effect(ProviderInstallation, make());

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
108108
import * as ProjectEnrichmentService from "./project/ProjectEnrichmentService.ts";
109109
import * as NativeAppIconResolver from "./assets/NativeAppIconResolver.ts";
110110
import * as AntigravityInstallation from "./provider/AntigravityInstallation.ts";
111+
import * as ProviderInstallation from "./provider/providerInstallation.ts";
111112
import * as CodexInstallation from "./provider/CodexInstallation.ts";
112113
import * as ProviderInstanceRegistry from "./provider/ProviderInstanceRegistry.ts";
113114
import * as ProviderAdapterRegistry from "./orchestration-v2/ProviderAdapterRegistry.ts";
@@ -666,6 +667,7 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
666667
).pipe(Layer.provide(DelegatedTaskQuestionReactor.layer), Layer.provide(ProjectionStoreV2.layer)),
667668
ProviderUsageLimitsIngestion.layer,
668669
layerProviderInstallationRefresh,
670+
ProviderInstallation.layer,
669671
ReplayMarkers.layer,
670672
Layer.effectDiscard(ProjectSettingsOverrideSweep.sweep),
671673
).pipe(

‎apps/server/src/ws.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import * as AgentSessionImporter from "./project/AgentSessionImporter.ts";
5151
import * as AgentSessionScanner from "./project/AgentSessionScanner.ts";
5252
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
5353
import * as AntigravityInstallation from "./provider/AntigravityInstallation.ts";
54+
import * as ProviderInstallation from "./provider/providerInstallation.ts";
5455
import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";
5556
import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts";
5657
import * as NativeChildWorkQuery from "./orchestration-v2/NativeChildWorkQuery.ts";
@@ -802,6 +803,7 @@ const layerUnusedConfigRpcServices = Layer.mergeAll(
802803
Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({}),
803804
Layer.mock(ProjectStore.ProjectStoreV2)({}),
804805
Layer.mock(ProviderAuthService.ProviderAuthService)({}),
806+
Layer.mock(ProviderInstallation.ProviderInstallation)({}),
805807
Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({}),
806808
Layer.mock(ProviderLatestVersions.ProviderLatestVersions)({}),
807809
Layer.mock(ProviderMaintenanceRunner.ProviderMaintenanceRunner)({}),

‎apps/server/src/ws.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRe
178178
import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator";
179179
import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner.ts";
180180
import * as ProviderAuthService from "./provider/ProviderAuthService.ts";
181-
import { makeProviderInstallation } from "./provider/providerInstallation.ts";
181+
import * as ProviderInstallation from "./provider/providerInstallation.ts";
182182
import * as ServerSelfUpdate from "./cloud/selfUpdate.ts";
183183
import * as NpmRelease from "./cloud/npmRelease.ts";
184184
import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
@@ -1496,7 +1496,7 @@ export const layerWsRpc = (
14961496
yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
14971497
const providerMaintenanceRunner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner;
14981498
const providerAuth = yield* ProviderAuthService.ProviderAuthService;
1499-
const providerInstallation = yield* makeProviderInstallation();
1499+
const providerInstallation = yield* ProviderInstallation.ProviderInstallation;
15001500
const serverSelfUpdate = yield* ServerSelfUpdate.ServerSelfUpdate;
15011501
const npmRelease = yield* NpmRelease.NpmRelease;
15021502
const config = yield* ServerConfig.ServerConfig;

0 commit comments

Comments
 (0)