|
1 | 1 | import * as NodeURL from "node:url"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import { describe, expect, it } from "@effect/vitest"; |
4 | | -import type { PermissionRuleset } from "@opencode-ai/sdk/v2"; |
5 | 4 | import { |
6 | 5 | CommandId, |
7 | 6 | EnvironmentId, |
@@ -30,23 +29,12 @@ import { McpSchema, McpServer } from "effect/ai"; |
30 | 29 | import { CodexOrchestratorReplayHarness } from "../orchestration-v2/Adapters/CodexAdapterV2.testkit.ts"; |
31 | 30 | import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts"; |
32 | 31 | import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; |
33 | | -import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts"; |
34 | 32 | import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; |
35 | 33 | import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; |
36 | 34 | import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
37 | 35 | import * as ServerSettings from "../serverSettings.ts"; |
38 | 36 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
39 | | -import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
40 | 37 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
41 | | -import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; |
42 | | -import { OpenCodeAdapterV2Driver } from "@t3tools/provider-opencode/server"; |
43 | | -import * as OpenCodeRuntime from "@t3tools/provider-opencode/server/OpenCodeRuntime"; |
44 | | -import { |
45 | | - OPENCODE_DEFAULT_INSTANCE_ID, |
46 | | - OPENCODE_PROVIDER, |
47 | | - openCodeToolDecision, |
48 | | -} from "@t3tools/provider-opencode/testing"; |
49 | | -import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; |
50 | 38 | import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; |
51 | 39 | import * as ProviderReplayHarness from "../orchestration-v2/testkit/ProviderReplayHarness.ts"; |
52 | 40 | import { |
@@ -655,283 +643,4 @@ describe("orchestrator MCP toolkit", () => { |
655 | 643 | }), |
656 | 644 | ), |
657 | 645 | ); |
658 | | - // A delegated reviewer pinned to a read-only OpenCode agent redirected shell |
659 | | - // output into /tmp: the child's full access overrode the agent's denies. |
660 | | - it.live("delegate_task runs an OpenCode review agent without the tools it denies", () => |
661 | | - Effect.scoped( |
662 | | - Effect.gen(function* () { |
663 | | - const cwd = yield* checkpointWorkspace("delegated-opencode-review"); |
664 | | - const instanceId = ProviderInstanceId.make(OPENCODE_DEFAULT_INSTANCE_ID); |
665 | | - const model = "anthropic/claude-sonnet"; |
666 | | - // The agent as OpenCode resolves it from opencode.json: the user's |
667 | | - // global allow, then the agent's own rules. |
668 | | - const review: PermissionRuleset = [ |
669 | | - { permission: "*", pattern: "*", action: "allow" }, |
670 | | - { permission: "*", pattern: "*", action: "deny" }, |
671 | | - { permission: "read", pattern: "*", action: "allow" }, |
672 | | - { permission: "glob", pattern: "*", action: "allow" }, |
673 | | - { permission: "grep", pattern: "*", action: "allow" }, |
674 | | - { permission: "list", pattern: "*", action: "allow" }, |
675 | | - { permission: "edit", pattern: "*", action: "deny" }, |
676 | | - { permission: "bash", pattern: "*", action: "deny" }, |
677 | | - { permission: "task", pattern: "*", action: "deny" }, |
678 | | - ]; |
679 | | - // Another session on the same OpenCode server answered "always" for shell. |
680 | | - const approved: PermissionRuleset = [{ permission: "bash", pattern: "*", action: "allow" }]; |
681 | | - const sessions = new Map< |
682 | | - string, |
683 | | - { readonly id: string; readonly permission?: PermissionRuleset } |
684 | | - >(); |
685 | | - let markParentStarted!: () => void; |
686 | | - const parentStarted = new Promise<void>((resolve) => { |
687 | | - markParentStarted = resolve; |
688 | | - }); |
689 | | - let childPrompted!: (prompt: { |
690 | | - readonly agent: unknown; |
691 | | - readonly variant: unknown; |
692 | | - readonly ran: ReadonlyArray<string>; |
693 | | - }) => void; |
694 | | - const childPrompt = new Promise<Parameters<typeof childPrompted>[0]>((resolve) => { |
695 | | - childPrompted = resolve; |
696 | | - }); |
697 | | - const openCodeClient = { |
698 | | - event: { |
699 | | - subscribe: async (_input?: unknown, options?: { readonly signal?: AbortSignal }) => ({ |
700 | | - stream: { |
701 | | - [Symbol.asyncIterator]: () => ({ |
702 | | - next: () => |
703 | | - new Promise<IteratorResult<never>>((resolve) => { |
704 | | - const done = () => resolve({ done: true, value: undefined }); |
705 | | - if (options?.signal?.aborted) return done(); |
706 | | - options?.signal?.addEventListener("abort", done, { once: true }); |
707 | | - }), |
708 | | - }), |
709 | | - }, |
710 | | - }), |
711 | | - }, |
712 | | - app: { |
713 | | - agents: async () => ({ |
714 | | - data: [{ name: "review", mode: "primary", options: {}, permission: review }], |
715 | | - }), |
716 | | - }, |
717 | | - session: { |
718 | | - create: async (input: { readonly permission?: PermissionRuleset }) => { |
719 | | - const nativeSession = { |
720 | | - id: `native-${sessions.size + 1}`, |
721 | | - permission: input.permission ?? [], |
722 | | - time: { created: 1, updated: 1 }, |
723 | | - }; |
724 | | - sessions.set(nativeSession.id, nativeSession); |
725 | | - return { data: nativeSession }; |
726 | | - }, |
727 | | - get: async ({ sessionID }: { readonly sessionID: string }) => ({ |
728 | | - data: sessions.get(sessionID), |
729 | | - }), |
730 | | - update: async (input: { |
731 | | - readonly sessionID: string; |
732 | | - readonly permission: PermissionRuleset; |
733 | | - }) => { |
734 | | - const existing = sessions.get(input.sessionID)!; |
735 | | - // OpenCode appends updated rules after the session's own. |
736 | | - const nativeSession = { |
737 | | - ...existing, |
738 | | - permission: [...(existing.permission ?? []), ...input.permission], |
739 | | - }; |
740 | | - sessions.set(input.sessionID, nativeSession); |
741 | | - return { data: nativeSession }; |
742 | | - }, |
743 | | - promptAsync: async (input: { |
744 | | - readonly sessionID: string; |
745 | | - readonly agent?: string; |
746 | | - readonly variant?: string; |
747 | | - }) => { |
748 | | - if (input.agent === undefined) { |
749 | | - markParentStarted(); |
750 | | - return { data: true }; |
751 | | - } |
752 | | - const rules = [...review, ...(sessions.get(input.sessionID)?.permission ?? [])]; |
753 | | - // What OpenCode would spawn or write for the reviewer's redirect, |
754 | | - // an edit, a nested task, a T3 MCP write, and the read it needs. |
755 | | - const ran = ( |
756 | | - [ |
757 | | - ["bash", "git diff > /tmp/review.diff"], |
758 | | - ["write", "notes.md"], |
759 | | - ["task", "general"], |
760 | | - ["t3-code_delegate_task", "*"], |
761 | | - ["read", "src/index.ts"], |
762 | | - ] as const |
763 | | - ).flatMap(([tool, pattern]) => |
764 | | - openCodeToolDecision(rules, approved, tool, pattern) === "allow" ? [tool] : [], |
765 | | - ); |
766 | | - childPrompted({ agent: input.agent, variant: input.variant, ran }); |
767 | | - return { data: true }; |
768 | | - }, |
769 | | - abort: async () => ({ data: true }), |
770 | | - children: async () => ({ data: [] }), |
771 | | - }, |
772 | | - }; |
773 | | - const layerRegistry = ProviderAdapterRegistry.layerFromDrivers({ |
774 | | - drivers: [OpenCodeAdapterV2Driver], |
775 | | - configMap: { |
776 | | - [instanceId]: { |
777 | | - driver: OPENCODE_PROVIDER, |
778 | | - config: { serverUrl: "http://test.invalid" }, |
779 | | - }, |
780 | | - }, |
781 | | - }).pipe( |
782 | | - Layer.provide( |
783 | | - Layer.mergeAll( |
784 | | - Layer.succeed(OpenCodeRuntime.OpenCodeRuntime, { |
785 | | - connectToOpenCodeServer: () => |
786 | | - Effect.succeed({ |
787 | | - url: "http://test.invalid", |
788 | | - version: "test", |
789 | | - exitCode: null, |
790 | | - external: true, |
791 | | - }), |
792 | | - createOpenCodeSdkClient: () => openCodeClient, |
793 | | - } as unknown as OpenCodeRuntime.OpenCodeRuntime["Service"]), |
794 | | - TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer)), |
795 | | - NodeServices.layer, |
796 | | - IdAllocator.layer, |
797 | | - Layer.succeed( |
798 | | - ProviderEventLoggers.ProviderEventLoggers, |
799 | | - ProviderEventLoggers.NoOpProviderEventLoggers, |
800 | | - ), |
801 | | - ), |
802 | | - ), |
803 | | - ); |
804 | | - // OpenCode is off until its instance is enabled. |
805 | | - const layerSettings = ServerSettings.layerTest({ |
806 | | - providerInstances: { [instanceId]: { driver: OPENCODE_PROVIDER, enabled: true } }, |
807 | | - }); |
808 | | - const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( |
809 | | - { name: "delegated-opencode-review" }, |
810 | | - layerRegistry, |
811 | | - ); |
812 | | - const layerOrchestration = Layer.merge( |
813 | | - layerOrchestrator, |
814 | | - ThreadManagementService.layer.pipe( |
815 | | - Layer.provide(layerOrchestrator), |
816 | | - Layer.provide(layerSettings), |
817 | | - Layer.provide( |
818 | | - CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), |
819 | | - ), |
820 | | - ), |
821 | | - ); |
822 | | - const layerTest = layerOrchestratorToolkit.pipe( |
823 | | - Layer.provideMerge(McpServer.McpServer.layer), |
824 | | - Layer.provideMerge(layerOrchestration), |
825 | | - Layer.provide(layerRegistry.pipe(Layer.provide(McpProviderSessions.layer))), |
826 | | - Layer.provide( |
827 | | - ProviderRegistryMock.layer([ |
828 | | - makeProviderSnapshot({ |
829 | | - instanceId, |
830 | | - driver: ProviderDriverKind.make(OPENCODE_PROVIDER), |
831 | | - model, |
832 | | - optionDescriptors: [ |
833 | | - { |
834 | | - id: "variant", |
835 | | - label: "Variant", |
836 | | - type: "select", |
837 | | - options: [{ id: "high", label: "High" }], |
838 | | - }, |
839 | | - { |
840 | | - id: "agent", |
841 | | - label: "Agent", |
842 | | - type: "select", |
843 | | - options: [{ id: "review", label: "Review" }], |
844 | | - }, |
845 | | - ], |
846 | | - }), |
847 | | - ]), |
848 | | - ), |
849 | | - Layer.provide(layerSettings), |
850 | | - Layer.provide(layerUnusedScheduledTaskStub), |
851 | | - Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
852 | | - Layer.provideMerge( |
853 | | - SecretRequests.layer.pipe( |
854 | | - Layer.provide(layerMemorySecretStore), |
855 | | - Layer.provide(layerOrchestration), |
856 | | - ), |
857 | | - ), |
858 | | - Layer.provide(NodeServices.layer), |
859 | | - ); |
860 | | - |
861 | | - yield* Effect.gen(function* () { |
862 | | - const orchestrator = yield* Orchestrator.OrchestratorV2; |
863 | | - const server = yield* McpServer.McpServer; |
864 | | - const selection = { instanceId, model } satisfies ModelSelection; |
865 | | - yield* orchestrator.dispatch({ |
866 | | - type: "thread.create", |
867 | | - createdBy: "user", |
868 | | - creationSource: "web", |
869 | | - commandId: CommandId.make("command:opencode-review-parent:create"), |
870 | | - threadId: parentThreadId, |
871 | | - projectId, |
872 | | - title: "OpenCode review parent", |
873 | | - modelSelection: selection, |
874 | | - runtimeMode: "full-access", |
875 | | - interactionMode: "default", |
876 | | - branch: null, |
877 | | - worktreePath: cwd, |
878 | | - }); |
879 | | - yield* orchestrator.dispatch({ |
880 | | - type: "message.dispatch", |
881 | | - createdBy: "user", |
882 | | - creationSource: "web", |
883 | | - commandId: CommandId.make("command:opencode-review-parent:start"), |
884 | | - threadId: parentThreadId, |
885 | | - messageId: MessageId.make("message:opencode-review-parent:start"), |
886 | | - text: parentPrompt, |
887 | | - attachments: [], |
888 | | - modelSelection: selection, |
889 | | - dispatchMode: { type: "start_immediately" }, |
890 | | - }); |
891 | | - yield* Effect.promise(() => parentStarted); |
892 | | - |
893 | | - const invocation: McpInvocationContext.McpInvocationScope = { |
894 | | - environmentId: EnvironmentId.make("environment:opencode-review"), |
895 | | - requestNamespace: "mcp-provider-session-opencode-review-parent", |
896 | | - thread: { |
897 | | - threadId: parentThreadId, |
898 | | - providerSessionId: "mcp-provider-session-opencode-review-parent", |
899 | | - providerInstanceId: instanceId, |
900 | | - }, |
901 | | - client: undefined, |
902 | | - capabilities: new Set(["orchestration"]), |
903 | | - issuedAt: 1, |
904 | | - }; |
905 | | - const delegated = yield* server |
906 | | - .callTool({ |
907 | | - name: "delegate_task", |
908 | | - arguments: { |
909 | | - task: "Review the change and report findings.", |
910 | | - role: "review", |
911 | | - target: { |
912 | | - providerInstanceId: instanceId, |
913 | | - model, |
914 | | - options: [ |
915 | | - { id: "variant", value: "high" }, |
916 | | - { id: "agent", value: "review" }, |
917 | | - ], |
918 | | - }, |
919 | | - mode: "async", |
920 | | - clientRequestId: "delegate-opencode-review-1", |
921 | | - }, |
922 | | - }) |
923 | | - .pipe( |
924 | | - Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
925 | | - Effect.provideService(McpSchema.McpServerClient, client), |
926 | | - ); |
927 | | - expect(delegated.isError).not.toBe(true); |
928 | | - expect(yield* Effect.promise(() => childPrompt)).toEqual({ |
929 | | - agent: "review", |
930 | | - variant: "high", |
931 | | - ran: ["read"], |
932 | | - }); |
933 | | - }).pipe(Effect.provide(layerTest)); |
934 | | - }), |
935 | | - ), |
936 | | - ); |
937 | 646 | }); |
0 commit comments