The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { expect, it } from "@effect/vitest"; |
| 4 | import { ProviderInstanceId } from "@t3tools/contracts"; |
| 5 | import { McpProviderSessions } from "@t3tools/provider-core/server/McpProviderSessions"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import { McpServer } from "effect/ai"; |
| 9 | |
| 10 | import { AssetSigner } from "../assets/AssetSigner.ts"; |
| 11 | import { ServerSecretStore } from "../auth/ServerSecretStore.ts"; |
| 12 | import { ServerConfig } from "../config.ts"; |
| 13 | import { DeviceService } from "../device/DeviceService.ts"; |
| 14 | import { ServerEnvironment } from "../environment/ServerEnvironment.ts"; |
| 15 | import { GitManager } from "../git/GitManager.ts"; |
| 16 | import { GitWorkflowService } from "../git/GitWorkflowService.ts"; |
| 17 | import * as McpHttpServer from "../mcp/McpHttpServer.ts"; |
| 18 | import { PreviewAutomationBroker } from "../mcp/PreviewAutomationBroker.ts"; |
| 19 | import { |
| 20 | CLAUDE_PROVIDER, |
| 21 | makeClaudeQueryOptions, |
| 22 | } from "../orchestration-v2/Adapters/ClaudeAdapterV2.ts"; |
| 23 | import { OrchestratorV2 } from "../orchestration-v2/Orchestrator.ts"; |
| 24 | import { ProviderAdapterRegistryV2 } from "../orchestration-v2/ProviderAdapterRegistry.ts"; |
| 25 | import { ThreadIssueService } from "../orchestration-v2/ThreadIssueService.ts"; |
| 26 | import { ThreadLaunchService } from "../orchestration-v2/ThreadLaunchService.ts"; |
| 27 | import { ThreadManagementService } from "../orchestration-v2/ThreadManagementService.ts"; |
| 28 | import { ThreadSearch } from "../orchestration-v2/ThreadSearch.ts"; |
| 29 | import { PeerForwarding } from "../peer/PeerForwarding.ts"; |
| 30 | import { PeerLinkRequests } from "../peer/PeerLinkRequests.ts"; |
| 31 | import { PeerLinks } from "../peer/PeerLinks.ts"; |
| 32 | import { RemoteDelegation } from "../peer/RemoteDelegation.ts"; |
| 33 | import { PreviewManager } from "../preview/Manager.ts"; |
| 34 | import { PreviewBrowser } from "../preview/PreviewBrowser.ts"; |
| 35 | import { ManagedProjectFolders } from "../project/ManagedProjectFolders.ts"; |
| 36 | import { ProjectCloneTracker } from "../project/ProjectCloneTracker.ts"; |
| 37 | import { ProjectService } from "../project/ProjectService.ts"; |
| 38 | import { ProjectSetupScriptRunner } from "../project/ProjectSetupScriptRunner.ts"; |
| 39 | import { IssueService } from "../issue/IssueService.ts"; |
| 40 | import { PullRequestReviewGuideService } from "../pullRequest/PullRequestReviewGuideService.ts"; |
| 41 | import { PullRequestService } from "../pullRequest/PullRequestService.ts"; |
| 42 | import { ScheduledTaskService } from "../scheduledTasks/ScheduledTaskService.ts"; |
| 43 | import { SecretRequests } from "../secrets/SecretRequests.ts"; |
| 44 | import { ServerSettingsService } from "../serverSettings.ts"; |
| 45 | import { SourceControlRepositoryService } from "../sourceControl/SourceControlRepositoryService.ts"; |
| 46 | import { GitVcsDriver } from "../vcs/GitVcsDriver.ts"; |
| 47 | import { VcsStatusBroadcaster } from "../vcs/VcsStatusBroadcaster.ts"; |
| 48 | import { buildCodexAdditionalContext } from "./CodexDeveloperInstructions.ts"; |
| 49 | import { DocumentText } from "./DocumentText.ts"; |
| 50 | import { EnvironmentThemeService } from "../environmentTheme.ts"; |
| 51 | import { ProviderRegistry } from "./ProviderRegistry.ts"; |
| 52 | import budget from "./agentContextBudget.json" with { type: "json" }; |
| 53 | |
| 54 | // Registration only stores tool definitions, so the handlers' services are never called. |
| 55 | const toolHandlerServices = Layer.mergeAll( |
| 56 | Layer.mock(AssetSigner)({}), |
| 57 | Layer.mock(DeviceService)({}), |
| 58 | Layer.mock(DocumentText)({}), |
| 59 | Layer.mock(EnvironmentThemeService)({}), |
| 60 | Layer.mock(GitVcsDriver)({}), |
| 61 | Layer.mock(GitWorkflowService)({}), |
| 62 | Layer.mock(IssueService)({}), |
| 63 | Layer.mock(ManagedProjectFolders)({ namedProjectsRoot: "" }), |
| 64 | Layer.mock(McpProviderSessions)({}), |
| 65 | Layer.mock(OrchestratorV2)({}), |
| 66 | Layer.mock(PeerForwarding)({}), |
| 67 | Layer.mock(PeerLinkRequests)({}), |
| 68 | Layer.mock(PeerLinks)({}), |
| 69 | Layer.mock(PreviewAutomationBroker)({}), |
| 70 | Layer.mock(PreviewBrowser)({}), |
| 71 | Layer.mock(PreviewManager)({}), |
| 72 | Layer.mock(ProjectCloneTracker)({}), |
| 73 | Layer.mock(ProjectService)({}), |
| 74 | Layer.mock(ProjectSetupScriptRunner)({}), |
| 75 | Layer.mock(ProviderAdapterRegistryV2)({}), |
| 76 | Layer.mock(ProviderRegistry)({}), |
| 77 | Layer.mock(PullRequestReviewGuideService)({}), |
| 78 | Layer.mock(PullRequestService)({}), |
| 79 | Layer.mock(GitManager)({}), |
| 80 | Layer.mock(RemoteDelegation)({}), |
| 81 | Layer.mock(ScheduledTaskService)({}), |
| 82 | Layer.mock(SecretRequests)({}), |
| 83 | ServerConfig.layerTest(process.cwd(), { prefix: "t3-agent-context-budget-" }), |
| 84 | Layer.mock(ServerEnvironment)({}), |
| 85 | Layer.mock(ServerSecretStore)({}), |
| 86 | Layer.mock(ServerSettingsService)({}), |
| 87 | Layer.mock(SourceControlRepositoryService)({}), |
| 88 | Layer.mock(ThreadIssueService)({}), |
| 89 | Layer.mock(ThreadLaunchService)({}), |
| 90 | Layer.mock(ThreadManagementService)({}), |
| 91 | Layer.mock(ThreadSearch)({}), |
| 92 | Layer.mock(VcsStatusBroadcaster)({}), |
| 93 | ).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); |
| 94 | |
| 95 | const utf8Bytes = (text: string) => Buffer.byteLength(text, "utf8"); |
| 96 | |
| 97 | const claudeSystemAppend = () => { |
| 98 | const { systemPrompt } = makeClaudeQueryOptions({ |
| 99 | modelSelection: { |
| 100 | instanceId: ProviderInstanceId.make(CLAUDE_PROVIDER), |
| 101 | model: "claude-opus-5-5", |
| 102 | }, |
| 103 | nativeThreadId: "context-budget", |
| 104 | resume: false, |
| 105 | cwd: null, |
| 106 | mcpServers: { "t3-code": { type: "http", url: "http://127.0.0.1/mcp" } }, |
| 107 | }); |
| 108 | return typeof systemPrompt === "object" && "append" in systemPrompt ? systemPrompt.append : ""; |
| 109 | }; |
| 110 | |
| 111 | const codexAdditionalContext = () => |
| 112 | Object.values( |
| 113 | buildCodexAdditionalContext( |
| 114 | { model: "gpt-6.1-sol", modelName: "GPT-6.1 Sol", reasoningEffort: "high" }, |
| 115 | { browser: true, device: true }, |
| 116 | ), |
| 117 | ) |
| 118 | .map((entry) => entry.value) |
| 119 | .join(""); |
| 120 | |
| 121 | const layerAdvertisedTools = McpHttpServer.layerTools.pipe( |
| 122 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 123 | Layer.provide(toolHandlerServices), |
| 124 | ); |
| 125 | |
| 126 | // Claude Code loads these tools up front instead of behind ToolSearch, outside this budget. |
| 127 | it.effect("only request_secret and send_to_user ask Claude Code to load them up front", () => |
| 128 | Effect.gen(function* () { |
| 129 | const mcp = yield* McpServer.McpServer; |
| 130 | const alwaysLoad = mcp.tools |
| 131 | .filter(({ tool }) => tool._meta?.["anthropic/alwaysLoad"] !== undefined) |
| 132 | .map(({ tool }) => [tool.name, tool._meta]); |
| 133 | expect(alwaysLoad).toEqual([ |
| 134 | ["request_secret", { "anthropic/alwaysLoad": true }], |
| 135 | ["send_to_user", { "anthropic/alwaysLoad": true }], |
| 136 | ]); |
| 137 | }).pipe(Effect.provide(layerAdvertisedTools)), |
| 138 | ); |
| 139 | |
| 140 | // CI rejects a PR that raises any value above main's agentContextBudget.json, so growth |
| 141 | // needs the agent-context-increase label. Lower the values when the text shrinks. |
| 142 | it.effect("agent context matches the committed budget", () => |
| 143 | Effect.gen(function* () { |
| 144 | const mcp = yield* McpServer.McpServer; |
| 145 | const measured = { |
| 146 | claudeSystemAppendBytes: utf8Bytes(claudeSystemAppend()), |
| 147 | codexAdditionalContextBytes: utf8Bytes(codexAdditionalContext()), |
| 148 | // The model sees these fields. outputSchema and annotations are protocol metadata. |
| 149 | mcpToolListJsonBytes: utf8Bytes( |
| 150 | JSON.stringify( |
| 151 | mcp.tools.map(({ tool }) => ({ |
| 152 | name: tool.name, |
| 153 | description: tool.description, |
| 154 | inputSchema: tool.inputSchema, |
| 155 | })), |
| 156 | ), |
| 157 | ), |
| 158 | }; |
| 159 | |
| 160 | expect( |
| 161 | measured, |
| 162 | "Set agentContextBudget.json to the measured values. Before you cut text to fit, run the evals that cover it (evals/README.md).", |
| 163 | ).toEqual(budget); |
| 164 | }).pipe(Effect.provide(layerAdvertisedTools)), |
| 165 | ); |