From e77b08358d4764d927d8dea7fddbef72b76b1bb9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 19:51:38 +0200 Subject: [PATCH] fix: address latest orchestration review findings Co-authored-by: codex --- .../threads/ThreadActivityInspector.tsx | 14 ++- .../threadActivityFileNavigation.test.ts | 26 +++++ .../threads/threadActivityFileNavigation.ts | 22 +++++ .../Adapters/AcpAdapterV2.test.ts | 98 +++++++++++++++++++ .../orchestration-v2/Adapters/AcpAdapterV2.ts | 68 +++++++++++-- .../Adapters/CodexAdapterV2.test.ts | 72 ++++++++++++++ .../Adapters/CodexAdapterV2.ts | 16 +-- 7 files changed, 298 insertions(+), 18 deletions(-) create mode 100644 apps/mobile/src/features/threads/threadActivityFileNavigation.test.ts create mode 100644 apps/mobile/src/features/threads/threadActivityFileNavigation.ts diff --git a/apps/mobile/src/features/threads/ThreadActivityInspector.tsx b/apps/mobile/src/features/threads/ThreadActivityInspector.tsx index 67454654a7..66d5c199fb 100644 --- a/apps/mobile/src/features/threads/ThreadActivityInspector.tsx +++ b/apps/mobile/src/features/threads/ThreadActivityInspector.tsx @@ -13,6 +13,7 @@ import { resolveWorkspaceRelativeFilePath } from "../files/filePath"; import { threadEnvironment } from "../../state/threads"; import { useAtomCommand } from "../../state/use-atom-command"; import { useV2ItemSupport } from "../../state/v2-item-support"; +import { buildThreadActivityFileParams } from "./threadActivityFileNavigation"; export function ThreadActivityInspector(props: { readonly activity: ThreadFeedActivity; @@ -93,12 +94,15 @@ export function ThreadActivityInspector(props: { onPress={() => { if (!relativePath) return; void Haptics.selectionAsync(); - router.push( - buildThreadFilesNavigation( - { environmentId: props.environmentId, threadId: row.sourceThreadId }, + navigation.navigate( + "ThreadFile", + buildThreadActivityFileParams({ + environmentId: props.environmentId, + currentThreadId: props.currentThreadId, + activitySourceThreadId: row.sourceThreadId, relativePath, - link.line, - ), + line: link.line, + }), ); }} className="min-h-9 flex-row items-center gap-2 rounded-md border border-neutral-300/50 px-2.5 py-1.5 dark:border-white/[0.1]" diff --git a/apps/mobile/src/features/threads/threadActivityFileNavigation.test.ts b/apps/mobile/src/features/threads/threadActivityFileNavigation.test.ts new file mode 100644 index 0000000000..a8f9343085 --- /dev/null +++ b/apps/mobile/src/features/threads/threadActivityFileNavigation.test.ts @@ -0,0 +1,26 @@ +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { describe, expect, it } from "@effect/vitest"; + +import { buildThreadActivityFileParams } from "./threadActivityFileNavigation"; + +describe("thread activity file navigation", () => { + it("keeps inherited activity file links on the currently selected thread", () => { + const sourceThreadId = ThreadId.make("source-thread"); + const currentThreadId = ThreadId.make("current-thread"); + + const params = buildThreadActivityFileParams({ + environmentId: EnvironmentId.make("environment"), + currentThreadId, + activitySourceThreadId: sourceThreadId, + relativePath: "apps/mobile/src/index.ts", + line: 12, + }); + + expect(params).toEqual({ + environmentId: "environment", + threadId: "current-thread", + path: ["apps", "mobile", "src", "index.ts"], + line: "12", + }); + }); +}); diff --git a/apps/mobile/src/features/threads/threadActivityFileNavigation.ts b/apps/mobile/src/features/threads/threadActivityFileNavigation.ts new file mode 100644 index 0000000000..21dfc44c02 --- /dev/null +++ b/apps/mobile/src/features/threads/threadActivityFileNavigation.ts @@ -0,0 +1,22 @@ +import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; + +interface ThreadActivityFileContext { + readonly environmentId: EnvironmentId; + readonly currentThreadId: ThreadId; + readonly activitySourceThreadId: ThreadId; + readonly relativePath: string; + readonly line?: number | null; +} + +export function buildThreadActivityFileParams(input: ThreadActivityFileContext) { + // Activity provenance may come from a parent thread, but file routes stay scoped + // to the thread whose workspace is currently selected. + return { + environmentId: String(input.environmentId), + threadId: String(input.currentThreadId), + path: input.relativePath.split("/").filter((segment) => segment.length > 0), + ...(Number.isFinite(input.line) && Number(input.line) > 0 + ? { line: String(Math.floor(Number(input.line))) } + : {}), + }; +} diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index e501cc4b22..3cd7e87c91 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -165,6 +165,104 @@ describe("acpPermissionDisposition", () => { assert.equal(acpPermissionDisposition(policy, permissionRequest("read")), "allow"); assert.equal(acpPermissionDisposition(policy, permissionRequest("execute")), "deny"); }); + + it.effect("denies mutations through workspace symlinks that escape the writable roots", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const workspace = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-acp-permission-workspace-", + }); + const outside = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-acp-permission-outside-", + }); + const outsideFile = path.join(outside, "existing.ts"); + yield* fileSystem.writeFileString(outsideFile, "outside"); + yield* fileSystem.symlink(outsideFile, path.join(workspace, "linked-file.ts")); + yield* fileSystem.symlink(outside, path.join(workspace, "linked-directory")); + + const realPolicy = ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: workspace, + approvalPolicy: "never", + sandboxPolicy: { + type: "workspaceWrite", + writableRoots: [], + networkAccess: false, + }, + }); + + assert.equal( + acpPermissionDisposition( + realPolicy, + permissionRequest("edit", [{ path: "linked-file.ts" }]), + ), + "deny", + ); + assert.equal( + acpPermissionDisposition( + realPolicy, + permissionRequest("edit", [{ path: "linked-directory/new-file.ts" }]), + ), + "deny", + "a missing leaf below an escaping directory symlink must not be auto-approved", + ); + assert.equal( + acpPermissionDisposition( + realPolicy, + permissionRequest("edit", [{ path: "linked-directory/../escaped-file.ts" }]), + ), + "deny", + "physical symlink traversal must be resolved before parent segments", + ); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("allows existing and new files beneath canonical writable roots", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const workspace = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-acp-permission-workspace-", + }); + const workspaceLinkParent = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-acp-permission-link-parent-", + }); + const workspaceLink = path.join(workspaceLinkParent, "workspace-link"); + yield* fileSystem.makeDirectory(path.join(workspace, "src"), { recursive: true }); + yield* fileSystem.writeFileString(path.join(workspace, "src", "existing.ts"), "existing"); + yield* fileSystem.symlink(workspace, workspaceLink); + + const realPolicy = ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: workspaceLink, + approvalPolicy: "never", + sandboxPolicy: { + type: "workspaceWrite", + writableRoots: [], + networkAccess: false, + }, + }); + + assert.equal( + acpPermissionDisposition( + realPolicy, + permissionRequest("edit", [{ path: "src/existing.ts" }]), + ), + "allow", + ); + assert.equal( + acpPermissionDisposition( + realPolicy, + permissionRequest("edit", [{ path: "src/generated/new-file.ts" }]), + ), + "allow", + "non-existent descendants of a real in-root ancestor remain writable", + ); + }).pipe(Effect.provide(NodeServices.layer)), + ); }); describe("acpProjectedCommandExitCode", () => { diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts index 8cccbb332f..6426e032c0 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts @@ -1,4 +1,5 @@ // @effect-diagnostics nodeBuiltinImport:off +import * as NodeFS from "node:fs"; import * as NodePath from "node:path"; import { @@ -844,9 +845,9 @@ export type AcpPermissionDisposition = "allow" | "ask" | "deny"; function resolveAcpPermissionPath(path: string, cwd: string | null): string | undefined { const trimmed = path.trim(); if (trimmed.length === 0) return undefined; - if (NodePath.isAbsolute(trimmed)) return NodePath.resolve(trimmed); + if (NodePath.isAbsolute(trimmed)) return trimmed; if (cwd === null || cwd.trim().length === 0) return undefined; - return NodePath.resolve(cwd, trimmed); + return `${cwd}${cwd.endsWith(NodePath.sep) ? "" : NodePath.sep}${trimmed}`; } function acpPathIsWithinRoot(path: string, root: string): boolean { @@ -859,6 +860,50 @@ function acpPathIsWithinRoot(path: string, root: string): boolean { ); } +/** + * Canonicalize a path for an authorization containment check. + * + * `realpath` cannot resolve a file that has not been created yet, so walk up + * to the deepest existing ancestor and append the missing suffix to that + * ancestor's canonical path. This follows symlinked directories while still + * allowing normal writes to new files. If an existing entry cannot be + * canonicalized (for example, a broken symlink), fail closed. + */ +function acpCanonicalPathForContainment(path: string): string | undefined { + // Do not lexically normalize before realpath. For a path such as + // `workspace/link/../file`, the kernel resolves `link` before `..`; an + // eager NodePath.resolve would erase that symlink traversal and could turn + // an outside target into an apparently in-workspace path. + let candidate = path; + const missingSuffix: Array = []; + + while (true) { + try { + return NodePath.resolve(NodeFS.realpathSync.native(candidate), ...missingSuffix); + } catch (cause) { + if ((cause as NodeJS.ErrnoException | undefined)?.code !== "ENOENT") { + return undefined; + } + } + + try { + NodeFS.lstatSync(candidate); + // The entry exists but realpath could not resolve it, as with a broken + // symlink. Treat it as untrusted rather than authorizing its lexical path. + return undefined; + } catch (cause) { + if ((cause as NodeJS.ErrnoException | undefined)?.code !== "ENOENT") { + return undefined; + } + } + + const parent = NodePath.dirname(candidate); + if (parent === candidate) return undefined; + missingSuffix.unshift(NodePath.basename(candidate)); + candidate = parent; + } +} + function acpWorkspaceWriteAllowsMutation( runtimePolicy: ProviderAdapterV2RuntimePolicy, sandboxPolicy: Record, @@ -866,15 +911,21 @@ function acpWorkspaceWriteAllowsMutation( ): boolean { const cwd = typeof runtimePolicy.cwd === "string" && runtimePolicy.cwd.trim().length > 0 - ? NodePath.resolve(runtimePolicy.cwd) + ? (resolveAcpPermissionPath(runtimePolicy.cwd, process.cwd()) ?? null) : null; - const roots = cwd === null ? [] : [cwd]; + const roots: Array = []; + if (cwd !== null) { + const canonicalCwd = acpCanonicalPathForContainment(cwd); + if (canonicalCwd !== undefined) roots.push(canonicalCwd); + } const writableRoots = sandboxPolicy.writableRoots; if (Array.isArray(writableRoots)) { for (const writableRoot of writableRoots) { if (typeof writableRoot !== "string") continue; const resolved = resolveAcpPermissionPath(writableRoot, cwd); - if (resolved !== undefined) roots.push(resolved); + if (resolved === undefined) continue; + const canonicalRoot = acpCanonicalPathForContainment(resolved); + if (canonicalRoot !== undefined) roots.push(canonicalRoot); } } if (roots.length === 0) return false; @@ -885,7 +936,12 @@ function acpWorkspaceWriteAllowsMutation( } for (const location of locations) { const resolved = resolveAcpPermissionPath(location.path, cwd); - if (resolved === undefined || !roots.some((root) => acpPathIsWithinRoot(resolved, root))) { + const canonicalPath = + resolved === undefined ? undefined : acpCanonicalPathForContainment(resolved); + if ( + canonicalPath === undefined || + !roots.some((root) => acpPathIsWithinRoot(canonicalPath, root)) + ) { return false; } } diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 1807be0bb5..2a40c51830 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -347,6 +347,78 @@ describe("CodexAdapterV2 runtime policy", () => { }), ); + it.effect("omits default-mode collaboration settings without the T3 MCP server", () => + Effect.gen(function* () { + const params = yield* buildCodexTurnStartParams({ + nativeThreadId: "native-default-without-t3-mcp", + codexInput: [{ type: "text", text: "implement this task" }], + runtimePolicy: { + runtimeMode: "full-access", + interactionMode: "default", + cwd: null, + }, + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5.4", + }, + hasT3Mcp: false, + }); + + assert.isUndefined(params.collaborationMode); + }), + ); + + it.effect("adds T3 plan-mode developer instructions when the T3 MCP server is attached", () => + Effect.gen(function* () { + const params = yield* buildCodexTurnStartParams({ + nativeThreadId: "native-plan-with-t3-mcp", + codexInput: [{ type: "text", text: "plan this task" }], + runtimePolicy: { + runtimeMode: "full-access", + interactionMode: "plan", + cwd: null, + }, + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5.4", + }, + hasT3Mcp: true, + }); + + assert.equal(params.collaborationMode?.mode, "plan"); + assert.include( + params.collaborationMode?.settings.developer_instructions ?? "", + "request_user_input", + ); + assert.include( + params.collaborationMode?.settings.developer_instructions ?? "", + "preview_status", + ); + }), + ); + + it.effect("keeps Codex in plan mode without referencing unavailable T3 MCP tools", () => + Effect.gen(function* () { + const params = yield* buildCodexTurnStartParams({ + nativeThreadId: "native-plan-without-t3-mcp", + codexInput: [{ type: "text", text: "plan this task" }], + runtimePolicy: { + runtimeMode: "full-access", + interactionMode: "plan", + cwd: null, + }, + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5.4", + }, + hasT3Mcp: false, + }); + + assert.equal(params.collaborationMode?.mode, "plan"); + assert.notProperty(params.collaborationMode?.settings, "developer_instructions"); + }), + ); + it.effect("compiles per-turn Codex model options and cwd from their owning inputs", () => Effect.gen(function* () { const params = yield* buildCodexTurnStartParams({ diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index b092c578a1..7273f8b3b7 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -584,20 +584,22 @@ export function buildCodexTurnStartParams(input: { selectedEffort === undefined ? undefined : yield* decodeTurnReasoningEffort(selectedEffort); const serviceTier = getCodexServiceTierOptionValue(input.modelSelection); const developerInstructions = - input.runtimePolicy.interactionMode === "plan" - ? CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS - : input.hasT3Mcp === true - ? CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS - : undefined; + input.hasT3Mcp !== true + ? undefined + : input.runtimePolicy.interactionMode === "plan" + ? CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS + : CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS; const collaborationMode: CodexSchema.ClientRequest__CollaborationMode | undefined = - developerInstructions === undefined + input.runtimePolicy.interactionMode !== "plan" && developerInstructions === undefined ? undefined : { mode: input.runtimePolicy.interactionMode === "plan" ? "plan" : "default", settings: { model: input.modelSelection.model, reasoning_effort: effort ?? "medium", - developer_instructions: developerInstructions, + ...(developerInstructions === undefined + ? {} + : { developer_instructions: developerInstructions }), }, };