Skip to content

Commit f8fad59

Browse files
authored
Merge branch 'pingdotgg:main' into feat/diff-panel-loc-totals
2 parents 0563b84 + e77f42c commit f8fad59

50 files changed

Lines changed: 1190 additions & 291 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ jobs:
864864
echo "Deploying hosted web app for $channel_name channel."
865865
deployment_url="$(
866866
vp dlx [email protected] deploy \
867+
--archive=tgz \
867868
--prod \
868869
--skip-domain \
869870
--yes \

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ import * as BrowserSession from "./BrowserSession.ts";
2222
import * as PreviewManager from "./Manager.ts";
2323

2424
describe("fitPictureInPictureContentSize", () => {
25-
it("fits landscape and portrait viewports without letterboxing the window", () => {
26-
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([480, 270]);
27-
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 9 / 16)).toEqual([240, 427]);
25+
it("preserves the PiP content area across aspect-ratio changes", () => {
26+
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([523, 294]);
27+
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 9 / 16)).toEqual([294, 523]);
28+
});
29+
30+
it("does not collapse toward the minimum size when orientation changes repeatedly", () => {
31+
const portrait = PreviewManager.fitPictureInPictureContentSize([523, 294], 9 / 16);
32+
const landscape = PreviewManager.fitPictureInPictureContentSize(portrait, 16 / 9);
33+
34+
expect(portrait).toEqual([294, 523]);
35+
expect(landscape).toEqual([523, 294]);
2836
});
2937
});
3038

@@ -1065,7 +1073,7 @@ describe("PreviewManager", () => {
10651073
skipTransformProcessType: true,
10661074
});
10671075
expect(pictureInPictureWindow.setAspectRatio.mock.calls).toEqual([[0], [1280 / 720]]);
1068-
expect(pictureInPictureWindow.setContentSize).toHaveBeenCalledWith(480, 270, false);
1076+
expect(pictureInPictureWindow.setContentSize).toHaveBeenCalledWith(523, 294, false);
10691077
expect(pictureInPictureWindow.setAspectRatio.mock.invocationCallOrder[0]).toBeLessThan(
10701078
pictureInPictureWindow.setContentSize.mock.invocationCallOrder[0] ?? 0,
10711079
);

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480;
104104
const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320;
105105
const PICTURE_IN_PICTURE_MIN_WIDTH = 240;
106106
const PICTURE_IN_PICTURE_MIN_HEIGHT = 160;
107+
const PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON = 0.002;
107108
const DIAGNOSTIC_BUFFER_LIMIT = 200;
108109
const MAX_ARTIFACT_SITE_SLUG_LENGTH = 80;
109110
const AGENT_CURSOR_MOVE_MS = 160;
@@ -164,8 +165,8 @@ export const fitPictureInPictureContentSize = (
164165
): readonly [width: number, height: number] => {
165166
const currentWidth = Math.max(1, current[0] ?? PICTURE_IN_PICTURE_INITIAL_WIDTH);
166167
const currentHeight = Math.max(1, current[1] ?? PICTURE_IN_PICTURE_INITIAL_HEIGHT);
167-
let width =
168-
currentWidth / currentHeight > aspectRatio ? currentHeight * aspectRatio : currentWidth;
168+
const currentArea = currentWidth * currentHeight;
169+
let width = Math.sqrt(currentArea * aspectRatio);
169170
let height = width / aspectRatio;
170171
const minimumScale = Math.max(
171172
1,
@@ -2074,7 +2075,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20742075
tabId,
20752076
);
20762077
const aspectRatio = frame.width / frame.height;
2077-
if (previousAspectRatio !== aspectRatio) {
2078+
if (
2079+
previousAspectRatio === undefined ||
2080+
Math.abs(previousAspectRatio - aspectRatio) > PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON
2081+
) {
20782082
yield* attempt(
20792083
{
20802084
operation: "pictureInPicture.setAspectRatio",

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const invocation = {
2323
providerInstanceId: ProviderInstanceId.make("codex"),
2424
capabilities: new Set(["preview"] as const),
2525
issuedAt: 1,
26-
expiresAt: Number.MAX_SAFE_INTEGER,
2726
};
2827
const client = McpSchema.McpServerClient.of({
2928
clientId: 1,

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ const makeMcpAuthMiddleware = McpSessionRegistry.McpSessionRegistry.pipe(
7474
? authorization.slice("Bearer ".length).trim()
7575
: "";
7676
const invocation = yield* registry.resolve(token);
77-
if (!invocation) return unauthorized;
77+
if (!invocation) {
78+
// Without this the only symptom of a dead credential is the agent
79+
// quietly losing the whole `t3-code` toolkit for the rest of its
80+
// session, with nothing on the server to explain why.
81+
yield* Effect.logWarning("rejected MCP request with an unusable credential", {
82+
reason: token.length === 0 ? "missing_bearer_token" : "unknown_or_expired_token",
83+
});
84+
return unauthorized;
85+
}
7886
return yield* httpEffect.pipe(
7987
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
8088
Effect.map(normalizeMcpHttpResponse),

‎apps/server/src/mcp/McpInvocationContext.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ it.effect("reports the scoped credential context when preview capability is unav
1717
providerInstanceId: ProviderInstanceId.make("codex"),
1818
capabilities: new Set(),
1919
issuedAt: 1,
20-
expiresAt: 2,
2120
};
2221

2322
return Effect.gen(function* () {

‎apps/server/src/mcp/McpInvocationContext.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export interface McpInvocationScope {
1616
readonly providerInstanceId: ProviderInstanceId;
1717
readonly capabilities: ReadonlySet<McpCapability>;
1818
readonly issuedAt: number;
19-
readonly expiresAt: number;
2019
}
2120

2221
export class McpInvocationContext extends Context.Service<

‎apps/server/src/mcp/McpSessionRegistry.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
2323
McpSessionRegistry.__testing
2424
.make({
2525
now,
26-
idleTimeoutMs: 100,
27-
maximumLifetimeMs: 1_000,
26+
livenessWindowMs: 100,
2827
})
2928
.pipe(
3029
Effect.provideService(HttpServer.HttpServer, httpServer),
@@ -75,7 +74,7 @@ it.effect("builds MCP endpoints from the bound server host", () =>
7574
}),
7675
);
7776

78-
it.effect("expires credentials after inactivity", () =>
77+
it.effect("expires credentials once their session stops showing signs of life", () =>
7978
Effect.gen(function* () {
8079
let timestamp = 1_000;
8180
const registry = yield* makeRegistry(() => timestamp);
@@ -88,3 +87,43 @@ it.effect("expires credentials after inactivity", () =>
8887
expect(yield* registry.resolve(token)).toBeUndefined();
8988
}),
9089
);
90+
91+
it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
92+
Effect.gen(function* () {
93+
let timestamp = 1_000;
94+
const registry = yield* makeRegistry(() => timestamp);
95+
const threadId = ThreadId.make("thread-3");
96+
const issued = yield* registry.issue({
97+
threadId,
98+
providerInstanceId: ProviderInstanceId.make("claude"),
99+
});
100+
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
101+
102+
// Well past the liveness window in total, but each turn reports in before
103+
// it lapses — this is the long-session case that used to lose the toolkit.
104+
for (let turn = 0; turn < 10; turn += 1) {
105+
timestamp += 99;
106+
yield* registry.touch(threadId);
107+
}
108+
109+
expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
110+
}),
111+
);
112+
113+
it.effect("does not keep credentials of other threads alive", () =>
114+
Effect.gen(function* () {
115+
let timestamp = 1_000;
116+
const registry = yield* makeRegistry(() => timestamp);
117+
const issued = yield* registry.issue({
118+
threadId: ThreadId.make("thread-4"),
119+
providerInstanceId: ProviderInstanceId.make("codex"),
120+
});
121+
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
122+
123+
timestamp += 99;
124+
yield* registry.touch(ThreadId.make("thread-unrelated"));
125+
timestamp += 2;
126+
127+
expect(yield* registry.resolve(token)).toBeUndefined();
128+
}),
129+
);

‎apps/server/src/mcp/McpSessionRegistry.ts‎

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ export interface McpCredentialRequest {
1818

1919
export interface McpIssuedCredential {
2020
readonly config: McpProviderSession.McpProviderSessionConfig;
21-
readonly expiresAt: number;
2221
}
2322

2423
export interface McpSessionRegistryShape {
2524
readonly issue: (request: McpCredentialRequest) => Effect.Effect<McpIssuedCredential>;
2625
readonly resolve: (
2726
rawToken: string,
2827
) => Effect.Effect<McpInvocationContext.McpInvocationScope | undefined>;
28+
/**
29+
* Records a sign of life for every credential bound to `threadId`. Provider
30+
* turns call this so that a session which is plainly alive keeps its
31+
* credential even when it goes a long time without touching an MCP tool.
32+
*/
33+
readonly touch: (threadId: ThreadId) => Effect.Effect<void>;
2934
readonly revokeProviderSession: (providerSessionId: string) => Effect.Effect<void>;
3035
readonly revokeThread: (threadId: ThreadId) => Effect.Effect<void>;
3136
readonly revokeAll: Effect.Effect<void>;
@@ -39,21 +44,33 @@ export class McpSessionRegistry extends Context.Service<
3944
interface CredentialRecord {
4045
readonly tokenHash: string;
4146
readonly scope: McpInvocationContext.McpInvocationScope;
42-
readonly lastUsedAt: number;
47+
readonly lastAliveAt: number;
4348
}
4449

4550
interface RegistryState {
4651
readonly records: ReadonlyMap<string, CredentialRecord>;
4752
}
4853

4954
export interface McpSessionRegistryOptions {
50-
readonly idleTimeoutMs?: number;
51-
readonly maximumLifetimeMs?: number;
55+
readonly livenessWindowMs?: number;
5256
readonly now?: () => number;
5357
}
5458

55-
const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;
56-
const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000;
59+
/**
60+
* How long a credential outlives the last sign of life from its provider
61+
* session.
62+
*
63+
* Liveness is refreshed both by MCP traffic and by `touch` on every provider
64+
* turn, so a session that is still doing work never expires no matter how long
65+
* it goes between browser tool calls. This window therefore only bounds
66+
* credentials whose session died without a clean stop — the normal paths
67+
* (`stopSession`, `stopAll`) revoke eagerly and do not wait for it.
68+
*
69+
* The bound matters because `/mcp` is mounted outside the environment auth
70+
* stack and is reachable on whatever host the server binds to, so this token is
71+
* the only thing guarding the preview toolkit on a remote-reachable server.
72+
*/
73+
const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000;
5774

5875
const bytesToHex = (bytes: Uint8Array): string =>
5976
Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
@@ -80,8 +97,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
8097
const httpServer = yield* HttpServer.HttpServer;
8198
const state = yield* SynchronizedRef.make<RegistryState>({ records: new Map() });
8299
const currentTimeMillis = options.now ? Effect.sync(options.now) : Clock.currentTimeMillis;
83-
const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
84-
const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;
100+
const livenessWindowMs = options.livenessWindowMs ?? DEFAULT_LIVENESS_WINDOW_MS;
85101
const endpoint =
86102
httpServer.address._tag === "TcpAddress"
87103
? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp`
@@ -92,11 +108,10 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
92108
.digest("SHA-256", new TextEncoder().encode(token))
93109
.pipe(Effect.map(bytesToHex), Effect.orDie);
94110

95-
const pruneExpired = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
111+
const pruneDead = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
96112
const next = new Map(
97113
Array.from(records).filter(
98-
([, record]) =>
99-
timestamp <= record.scope.expiresAt && timestamp - record.lastUsedAt <= idleTimeoutMs,
114+
([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs,
100115
),
101116
);
102117
return next.size === records.size ? records : next;
@@ -108,19 +123,17 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
108123
const providerSessionId = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
109124
const rawToken = yield* crypto.randomBytes(32).pipe(Effect.map(tokenFromBytes), Effect.orDie);
110125
const tokenHash = yield* hashToken(rawToken);
111-
const expiresAt = issuedAt + maximumLifetimeMs;
112126
const scope: McpInvocationContext.McpInvocationScope = {
113127
environmentId,
114128
threadId: ThreadId.make(request.threadId),
115129
providerSessionId,
116130
providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),
117131
capabilities: new Set(["preview"]),
118132
issuedAt,
119-
expiresAt,
120133
};
121134
yield* SynchronizedRef.update(state, ({ records }) => {
122-
const next = new Map(pruneExpired(records, issuedAt));
123-
next.set(tokenHash, { tokenHash, scope, lastUsedAt: issuedAt });
135+
const next = new Map(pruneDead(records, issuedAt));
136+
next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt });
124137
return { records: next };
125138
});
126139
return {
@@ -132,7 +145,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
132145
endpoint,
133146
authorizationHeader: `Bearer ${rawToken}`,
134147
},
135-
expiresAt,
136148
};
137149
},
138150
);
@@ -143,16 +155,32 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
143155
const tokenHash = yield* hashToken(rawToken);
144156
const timestamp = yield* currentTimeMillis;
145157
return yield* SynchronizedRef.modify(state, ({ records }) => {
146-
const current = pruneExpired(records, timestamp);
158+
const current = pruneDead(records, timestamp);
147159
const record = current.get(tokenHash);
148160
if (!record) return [undefined, { records: current }] as const;
149161
const next = new Map(current);
150-
next.set(tokenHash, { ...record, lastUsedAt: timestamp });
162+
next.set(tokenHash, { ...record, lastAliveAt: timestamp });
151163
return [record.scope, { records: next }] as const;
152164
});
153165
},
154166
);
155167

168+
const touch: McpSessionRegistryShape["touch"] = Effect.fn("McpSessionRegistry.touch")(
169+
function* (threadId) {
170+
const timestamp = yield* currentTimeMillis;
171+
yield* SynchronizedRef.update(state, ({ records }) => {
172+
const current = pruneDead(records, timestamp);
173+
const next = new Map(current);
174+
for (const [tokenHash, record] of current) {
175+
if (record.scope.threadId === threadId) {
176+
next.set(tokenHash, { ...record, lastAliveAt: timestamp });
177+
}
178+
}
179+
return { records: next };
180+
});
181+
},
182+
);
183+
156184
const revokeWhere = (predicate: (record: CredentialRecord) => boolean) =>
157185
SynchronizedRef.update(state, ({ records }) => ({
158186
records: new Map(Array.from(records).filter(([, record]) => !predicate(record))),
@@ -161,6 +189,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
161189
return McpSessionRegistry.of({
162190
issue,
163191
resolve,
192+
touch,
164193
revokeProviderSession: Effect.fn("McpSessionRegistry.revokeProviderSession")(
165194
function* (providerSessionId) {
166195
yield* revokeWhere((record) => record.scope.providerSessionId === providerSessionId);
@@ -202,6 +231,13 @@ export const issueActiveMcpCredential = (
202231
.pipe(Effect.andThen(activeMcpSessionRegistry.issue(request)))
203232
: Effect.sync((): McpIssuedCredential | undefined => undefined);
204233

234+
/**
235+
* Refreshes the liveness of a thread's MCP credential. Called on every provider
236+
* turn so an active session is never mistaken for an abandoned one.
237+
*/
238+
export const touchActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>
239+
activeMcpSessionRegistry ? activeMcpSessionRegistry.touch(threadId) : Effect.void;
240+
205241
export const revokeActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>
206242
activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeThread(threadId) : Effect.void;
207243

‎apps/server/src/mcp/PreviewAutomationBroker.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const scope = {
3131
providerInstanceId: ProviderInstanceId.make("codex"),
3232
capabilities: new Set(["preview"] as const),
3333
issuedAt: 1,
34-
expiresAt: Number.MAX_SAFE_INTEGER,
3534
};
3635

3736
const makeHost = (overrides: Partial<PreviewAutomationHost> = {}): PreviewAutomationHost => ({

0 commit comments

Comments
 (0)