GeezerVison is showing github.com — sync: port upstream server VCS, keybindings, images, terminal, persis… · aaditagrawal/t3code@dcd00ef
This repository was archived by the owner on Oct 10, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/imageMime.test.ts +57Lines changed: 57 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,63 @@ describe("imageMime", () => { | 32 | 32 | }); | 33 | 33 | }); | 34 | 34 |
| | 35 | + it("rejects payload with characters outside the base64 alphabet", () => { | | 36 | + expect(parseBase64DataUrl("data:image/png;base64,SGVs!bG8=")).toBeNull(); | | 37 | + expect(parseBase64DataUrl("data:image/png;base64,SGVs,bG8=")).toBeNull(); | | 38 | + }); | | 39 | + | | 40 | + it("rejects structurally malformed base64", () => { | | 41 | + // '=' before the trailing padding position | | 42 | + expect(parseBase64DataUrl("data:image/png;base64,AB=CD===")).toBeNull(); | | 43 | + expect(parseBase64DataUrl("data:image/png;base64,SGV=bG8=")).toBeNull(); | | 44 | + // more than two padding characters | | 45 | + expect(parseBase64DataUrl("data:image/png;base64,SGVsbG8=====AAA")).toBeNull(); | | 46 | + // length not a multiple of 4 | | 47 | + expect(parseBase64DataUrl("data:image/png;base64,SGVsbG8")).toBeNull(); | | 48 | + }); | | 49 | + | | 50 | + it("accepts base64 with one or two trailing padding characters", () => { | | 51 | + expect(parseBase64DataUrl("data:image/png;base64,SGVsbA==")).toEqual({ | | 52 | + mimeType: "image/png", | | 53 | + base64: "SGVsbA==", | | 54 | + }); | | 55 | + expect(parseBase64DataUrl("data:image/png;base64,SGVsbG8h")).toEqual({ | | 56 | + mimeType: "image/png", | | 57 | + base64: "SGVsbG8h", | | 58 | + }); | | 59 | + }); | | 60 | + | | 61 | + it("rejects empty and whitespace-only payloads", () => { | | 62 | + expect(parseBase64DataUrl("data:image/png;base64,")).toBeNull(); | | 63 | + expect(parseBase64DataUrl("data:image/png;base64, \r\n")).toBeNull(); | | 64 | + }); | | 65 | + | | 66 | + it("parses a case-insensitive scheme and mime type", () => { | | 67 | + expect(parseBase64DataUrl("DATA:IMAGE/PNG;BASE64,SGVsbG8=")).toEqual({ | | 68 | + mimeType: "image/png", | | 69 | + base64: "SGVsbG8=", | | 70 | + }); | | 71 | + }); | | 72 | + | | 73 | + it("parses a multi-megabyte payload from a deep call stack", () => { | | 74 | + // Regression: matching the payload with a regex borrowed the JS call | | 75 | + // stack, so a ~10 MB image parsed inside fiber execution threw | | 76 | + // "RangeError: Maximum call stack size exceeded". | | 77 | + const dataUrl = `data:image/png;base64,${"A".repeat(14_000_000)}`; | | 78 | + const atDepth = (depth: number): ReturnType<typeof parseBase64DataUrl> => | | 79 | + depth === 0 ? parseBase64DataUrl(dataUrl) : atDepth(depth - 1); | | 80 | + const findMaxDepth = (depth: number): number => { | | 81 | + try { | | 82 | + return findMaxDepth(depth + 1); | | 83 | + } catch { | | 84 | + return depth; | | 85 | + } | | 86 | + }; | | 87 | + const result = atDepth(Math.floor(findMaxDepth(0) * 0.85)); | | 88 | + expect(result?.mimeType).toBe("image/png"); | | 89 | + expect(result?.base64.length).toBe(14_000_000); | | 90 | + }); | | 91 | + | 35 | 92 | it("does not read inherited keys from mime extension map", () => { | 36 | 93 | expect(inferImageExtension({ mimeType: "constructor" })).toBe(".bin"); | 37 | 94 | }); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/imageMime.ts +63-8Lines changed: 63 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,17 +29,43 @@ export const SAFE_IMAGE_FILE_EXTENSIONS = new Set([ | 29 | 29 | ".webp", | 30 | 30 | ]); | 31 | 31 |
| | 32 | +// Whether `code` is a character the base64 payload may contain, aside from | | 33 | +// the whitespace handled separately below. | | 34 | +function isBase64Char(code: number): boolean { | | 35 | + return ( | | 36 | + (code >= 0x61 && code <= 0x7a) || // a-z | | 37 | + (code >= 0x41 && code <= 0x5a) || // A-Z | | 38 | + (code >= 0x30 && code <= 0x39) || // 0-9 | | 39 | + code === 0x2b || // + | | 40 | + code === 0x2f || // / | | 41 | + code === 0x3d // = | | 42 | + ); | | 43 | +} | | 44 | + | | 45 | +function isBase64Whitespace(code: number): boolean { | | 46 | + return code === 0x09 || code === 0x0d || code === 0x0a || code === 0x20; // \t \r \n space | | 47 | +} | | 48 | + | | 49 | +// Data URLs carry the full image payload, so this parser must never run a | | 50 | +// regex across the payload: V8's regex engine borrows the JS call stack, and | | 51 | +// matching a multi-megabyte string from a deep call stack (e.g. inside fiber | | 52 | +// execution) throws "Maximum call stack size exceeded". | 32 | 53 | export function parseBase64DataUrl( | 33 | 54 | dataUrl: string, | 34 | 55 | ): { readonly mimeType: string; readonly base64: string } | null { | 35 | | - const match = /^data:([^,]+),([a-z0-9+/=\r\n ]+)$/i.exec(dataUrl.trim()); | 36 | | - if (!match) return null; | | 56 | + const trimmed = dataUrl.trim(); | | 57 | + if (trimmed.slice(0, 5).toLowerCase() !== "data:") return null; | | 58 | + | | 59 | + const commaIndex = trimmed.indexOf(","); | | 60 | + if (commaIndex === -1) return null; | | 61 | + const header = trimmed.slice(5, commaIndex); | | 62 | + if (header.length === 0) return null; | 37 | 63 |
| 38 | 64 | const headerParts: Array<string> = []; | 39 | | - for (const part of (match[1] ?? "").split(";")) { | 40 | | - const trimmed = part.trim(); | 41 | | - if (trimmed.length > 0) { | 42 | | - headerParts.push(trimmed); | | 65 | + for (const part of header.split(";")) { | | 66 | + const partTrimmed = part.trim(); | | 67 | + if (partTrimmed.length > 0) { | | 68 | + headerParts.push(partTrimmed); | 43 | 69 | } | 44 | 70 | } | 45 | 71 | if (headerParts.length < 2) { | @@ -51,8 +77,37 @@ export function parseBase64DataUrl( | 51 | 77 | } | 52 | 78 |
| 53 | 79 | const mimeType = headerParts[0]?.toLowerCase(); | 54 | | - const base64 = match[2]?.replace(/\s+/g, ""); | 55 | | - if (!mimeType || !base64) return null; | | 80 | + if (!mimeType) return null; | | 81 | + | | 82 | + const payload = trimmed.slice(commaIndex + 1); | | 83 | + const runs: Array<string> = []; | | 84 | + let runStart = -1; | | 85 | + for (let index = 0; index < payload.length; index += 1) { | | 86 | + const code = payload.charCodeAt(index); | | 87 | + if (isBase64Char(code)) { | | 88 | + if (runStart === -1) runStart = index; | | 89 | + continue; | | 90 | + } | | 91 | + if (!isBase64Whitespace(code)) return null; | | 92 | + if (runStart !== -1) { | | 93 | + runs.push(payload.slice(runStart, index)); | | 94 | + runStart = -1; | | 95 | + } | | 96 | + } | | 97 | + if (runStart !== -1) { | | 98 | + runs.push(payload.slice(runStart)); | | 99 | + } | | 100 | + const base64 = runs.length === 1 ? runs[0]! : runs.join(""); | | 101 | + if (base64.length === 0 || base64.length % 4 !== 0) return null; | | 102 | + const firstPad = base64.indexOf("="); | | 103 | + if (firstPad !== -1) { | | 104 | + // '=' is only valid as one or two trailing padding characters; Node's | | 105 | + // decoder would otherwise silently truncate at the first '='. | | 106 | + if (base64.length - firstPad > 2) return null; | | 107 | + for (let index = firstPad; index < base64.length; index += 1) { | | 108 | + if (base64.charCodeAt(index) !== 0x3d) return null; | | 109 | + } | | 110 | + } | 56 | 111 |
| 57 | 112 | return { mimeType, base64 }; | 58 | 113 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/keybindings.test.ts +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -411,6 +411,28 @@ it.layer(NodeServices.layer)("keybindings", (it) => { | 411 | 411 | }).pipe(Effect.provide(makeKeybindingsLayer())), | 412 | 412 | ); | 413 | 413 |
| | 414 | + it.effect("replacing with a rule that already exists elsewhere does not duplicate it", () => | | 415 | + Effect.gen(function* () { | | 416 | + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; | | 417 | + yield* writeKeybindingsConfig(keybindingsConfigPath, [ | | 418 | + { key: "mod+r", command: "script.run-tests.run" }, | | 419 | + { key: "mod+alt+r", command: "script.run-tests.run" }, | | 420 | + ]); | | 421 | + yield* Effect.gen(function* () { | | 422 | + const keybindings = yield* Keybindings.Keybindings; | | 423 | + return yield* keybindings.upsertKeybindingRule({ | | 424 | + key: "mod+alt+r", | | 425 | + command: "script.run-tests.run", | | 426 | + replace: { key: "mod+r", command: "script.run-tests.run" }, | | 427 | + }); | | 428 | + }); | | 429 | + | | 430 | + const persisted = yield* readKeybindingsConfig(keybindingsConfigPath); | | 431 | + const persistedView = persisted.map(({ key, command }) => ({ key, command })); | | 432 | + assert.deepEqual(persistedView, [{ key: "mod+alt+r", command: "script.run-tests.run" }]); | | 433 | + }).pipe(Effect.provide(makeKeybindingsLayer())), | | 434 | + ); | | 435 | + | 414 | 436 | it.effect("removes only the targeted custom keybinding", () => | 415 | 437 | Effect.gen(function* () { | 416 | 438 | const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/keybindings.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -658,7 +658,9 @@ const make = Effect.gen(function* () { | 658 | 658 | const nextConfig = [ | 659 | 659 | ...customConfig.filter((entry) => { | 660 | 660 | if (replaceTarget) { | 661 | | - return !isSameKeybindingRule(entry, replaceTarget); | | 661 | + return ( | | 662 | + !isSameKeybindingRule(entry, replaceTarget) && !isSameKeybindingRule(entry, rule) | | 663 | + ); | 662 | 664 | } | 663 | 665 | return !isSameKeybindingRule(entry, rule); | 664 | 666 | }), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/persistence/ProviderSessionRuntime.ts +15-6Lines changed: 15 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Arr from "effect/Array"; | 1 | 2 | import * as Context from "effect/Context"; | 2 | 3 | import * as Effect from "effect/Effect"; | 3 | 4 | import * as Layer from "effect/Layer"; | @@ -280,18 +281,26 @@ export const make = Effect.gen(function* () { | 280 | 281 | ), | 281 | 282 | ), | 282 | 283 | Effect.flatMap((rows) => | | 284 | + // Skip rows that no longer decode (e.g. written by an older build) | | 285 | + // instead of failing the whole list — one stale row must not disable | | 286 | + // every consumer that enumerates sessions, such as the reaper. | 283 | 287 | Effect.forEach(rows, (row) => | 284 | 288 | decodeRuntimeRow(row).pipe( | 285 | | - Effect.mapError((cause) => | 286 | | - PersistenceDecodeError.fromSchemaError( | 287 | | - "ProviderSessionRuntimeRepository.list:decodeRows", | 288 | | - cause, | 289 | | - { threadId: row.threadId }, | 290 | | - ), | | 289 | + Effect.map(Option.some), | | 290 | + Effect.catch((cause) => | | 291 | + Effect.logWarning("provider.session.runtime.row-skipped", { | | 292 | + threadId: row.threadId, | | 293 | + error: PersistenceDecodeError.fromSchemaError( | | 294 | + "ProviderSessionRuntimeRepository.list:decodeRows", | | 295 | + cause, | | 296 | + { threadId: row.threadId }, | | 297 | + ).message, | | 298 | + }).pipe(Effect.as(Option.none<ProviderSessionRuntime>())), | 291 | 299 | ), | 292 | 300 | ), | 293 | 301 | ), | 294 | 302 | ), | | 303 | + Effect.map((decoded) => Arr.getSomes(decoded)), | 295 | 304 | ); | 296 | 305 |
| 297 | 306 | const deleteByThreadId: ProviderSessionRuntimeRepository["Service"]["deleteByThreadId"] = ( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/persistence/RepositoryErrorCorrelation.test.ts +18-10Lines changed: 18 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -182,7 +182,7 @@ describe("persistence error correlation", () => { | 182 | 182 | }).pipe(Effect.provide(authPairingLinkLayer)), | 183 | 183 | ); | 184 | 184 |
| 185 | | - it.effect("correlates provider runtime SQL and per-row decode failures by thread", () => | | 185 | + it.effect("skips undecodable provider runtime rows and correlates SQL failures by thread", () => | 186 | 186 | Effect.gen(function* () { | 187 | 187 | const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository; | 188 | 188 | const sql = yield* SqlClient.SqlClient; | @@ -215,16 +215,24 @@ describe("persistence error correlation", () => { | 215 | 215 | ) | 216 | 216 | `; | 217 | 217 |
| 218 | | - const decodeError = yield* Effect.flip(runtimes.list()); | 219 | | - assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError); | 220 | | - assert.deepStrictEqual(decodeError.correlation, { threadId }); | 221 | | - assert.equal( | 222 | | - decodeError.message, | 223 | | - `Decode error in ProviderSessionRuntimeRepository.list:decodeRows: ${decodeError.issue}`, | | 218 | + const validThreadId = ThreadId.make("thread-valid"); | | 219 | + yield* runtimes.upsert({ | | 220 | + threadId: validThreadId, | | 221 | + providerName: "codex", | | 222 | + providerInstanceId: null, | | 223 | + adapterKey: "codex", | | 224 | + runtimeMode: "full-access", | | 225 | + status: "running", | | 226 | + lastSeenAt, | | 227 | + resumeCursor: null, | | 228 | + runtimePayload: null, | | 229 | + }); | | 230 | + | | 231 | + const listed = yield* runtimes.list(); | | 232 | + assert.deepStrictEqual( | | 233 | + listed.map((runtime) => runtime.threadId), | | 234 | + [validThreadId], | 224 | 235 | ); | 225 | | - assert.notInclude(decodeError.issue, runtimePayload); | 226 | | - assert.notInclude(decodeError.message, runtimePayload); | 227 | | - assert.notInclude(decodeError.message, lastSeenAt); | 228 | 236 |
| 229 | 237 | yield* sql`DROP TABLE provider_session_runtime`; | 230 | 238 | const sqlFailure = yield* Effect.flip( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,9 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { | 114 | 114 |
| 115 | 115 | const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions")); | 116 | 116 | const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml")); | | 117 | + const mcpOauthLocksTarget = yield* fileSystem.readLink( | | 118 | + path.join(shadowHome, "mcp-oauth-locks"), | | 119 | + ); | 117 | 120 | const modelsCacheExists = yield* fileSystem.exists( | 118 | 121 | path.join(shadowHome, "models_cache.json"), | 119 | 122 | ); | @@ -124,12 +127,45 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { | 124 | 127 |
| 125 | 128 | expect(sessionsTarget).toBe(path.join(sharedHome, "sessions")); | 126 | 129 | expect(configTarget).toBe(path.join(sharedHome, "config.toml")); | | 130 | + expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks")); | 127 | 131 | expect(modelsCacheExists).toBe(false); | 128 | 132 | expect(authLinkResult._tag).toBe("Failure"); | 129 | 133 | expect(authContents).toContain("shadow"); | 130 | 134 | }), | 131 | 135 | ); | 132 | 136 |
| | 137 | + it.effect("replaces Codex-created local MCP OAuth locks with the shared lock directory", () => | | 138 | + Effect.gen(function* () { | | 139 | + const fileSystem = yield* FileSystem.FileSystem; | | 140 | + const path = yield* Path.Path; | | 141 | + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); | | 142 | + const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-"); | | 143 | + const shadowHome = path.join(shadowRoot, "shadow"); | | 144 | + const sharedLocks = path.join(sharedHome, "mcp-oauth-locks"); | | 145 | + const shadowLocks = path.join(shadowHome, "mcp-oauth-locks"); | | 146 | + | | 147 | + yield* writeTextFile(path.join(sharedLocks, "file-store.lock"), ""); | | 148 | + yield* writeTextFile(path.join(shadowLocks, "file-store.lock"), ""); | | 149 | + | | 150 | + const layout = yield* resolveCodexHomeLayout( | | 151 | + decodeCodexSettings({ | | 152 | + homePath: sharedHome, | | 153 | + shadowHomePath: shadowHome, | | 154 | + }), | | 155 | + ); | | 156 | + | | 157 | + yield* materializeCodexShadowHome(layout); | | 158 | + | | 159 | + const locksTarget = yield* fileSystem.readLink(shadowLocks); | | 160 | + const sharedLockExists = yield* fileSystem.exists( | | 161 | + path.join(sharedLocks, "file-store.lock"), | | 162 | + ); | | 163 | + | | 164 | + expect(locksTarget).toBe(sharedLocks); | | 165 | + expect(sharedLockExists).toBe(true); | | 166 | + }), | | 167 | + ); | | 168 | + | 133 | 169 | it.effect("accepts Codex-created shadow-local runtime directories", () => | 134 | 170 | Effect.gen(function* () { | 135 | 171 | const fileSystem = yield* FileSystem.FileSystem; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Drivers/CodexHomeLayout.ts +29-10Lines changed: 29 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,10 +26,12 @@ const KNOWN_SHARED_DIRECTORIES = [ | 26 | 26 | "plugins", | 27 | 27 | "cache", | 28 | 28 | "logs", | | 29 | + "mcp-oauth-locks", | 29 | 30 | ] as const; | 30 | 31 |
| 31 | 32 | const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "models_cache.json"]); | 32 | 33 | const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp"]); | | 34 | +const REPLACEABLE_SHARED_RUNTIME_DIRECTORIES = new Set(["mcp-oauth-locks"]); | 33 | 35 |
| 34 | 36 | function resolveHomePath(path: Path.Path, value: string | undefined): string { | 35 | 37 | const expanded = | @@ -225,16 +227,6 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu | 225 | 227 | linkPath: link, | 226 | 228 | }); | 227 | 229 |
| 228 | | - if (state._tag === "NotSymlink") { | 229 | | - return yield* new CodexShadowHomeEntryConflictError({ | 230 | | - sharedHomePath: input.sharedHomePath, | 231 | | - effectiveHomePath: input.effectiveHomePath, | 232 | | - entryName: input.entryName, | 233 | | - linkPath: link, | 234 | | - targetPath: target, | 235 | | - }); | 236 | | - } | 237 | | - | 238 | 230 | const createLink = input.fileSystem.symlink(target, link).pipe( | 239 | 231 | Effect.catchTags({ | 240 | 232 | PlatformError: (cause) => | @@ -250,6 +242,33 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu | 250 | 242 | }), | 251 | 243 | ); | 252 | 244 |
| | 245 | + if (state._tag === "NotSymlink") { | | 246 | + if (!REPLACEABLE_SHARED_RUNTIME_DIRECTORIES.has(input.entryName)) { | | 247 | + return yield* new CodexShadowHomeEntryConflictError({ | | 248 | + sharedHomePath: input.sharedHomePath, | | 249 | + effectiveHomePath: input.effectiveHomePath, | | 250 | + entryName: input.entryName, | | 251 | + linkPath: link, | | 252 | + targetPath: target, | | 253 | + }); | | 254 | + } | | 255 | + | | 256 | + yield* input.fileSystem.remove(link, { recursive: true }).pipe( | | 257 | + Effect.catchTags({ | | 258 | + PlatformError: (cause) => | | 259 | + new CodexShadowHomeFileSystemError({ | | 260 | + sharedHomePath: input.sharedHomePath, | | 261 | + effectiveHomePath: input.effectiveHomePath, | | 262 | + operation: "remove", | | 263 | + path: link, | | 264 | + entryName: input.entryName, | | 265 | + cause, | | 266 | + }), | | 267 | + }), | | 268 | + ); | | 269 | + return yield* createLink; | | 270 | + } | | 271 | + | 253 | 272 | if (state._tag === "Missing") { | 254 | 273 | return yield* createLink; | 255 | 274 | } | |
|