forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor: address codebase audit findings #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { | ||
| ApprovalRequestId, | ||
| EventId, | ||
| RuntimeItemId, | ||
| ThreadId, | ||
| TurnId, | ||
| type ProviderApprovalDecision, | ||
| type ProviderRuntimeEvent, | ||
| type ProviderSession, | ||
| type ProviderTurnStartResult, | ||
| type ProviderUserInputAnswers, | ||
| } from "@t3tools/contracts"; | ||
| import { it, vi } from "@effect/vitest"; | ||
| import { Effect, Layer, Stream } from "effect"; | ||
|
|
||
| import { AmpServerManager } from "../../ampServerManager.ts"; | ||
| import { AmpAdapter } from "../Services/AmpAdapter.ts"; | ||
| import { makeAmpAdapterLive } from "./AmpAdapter.ts"; | ||
| import { ServerSettingsService } from "../../serverSettings.ts"; | ||
|
|
||
| const asThreadId = (value: string): ThreadId => ThreadId.makeUnsafe(value); | ||
| const asTurnId = (value: string): TurnId => TurnId.makeUnsafe(value); | ||
| const asEventId = (value: string): EventId => EventId.makeUnsafe(value); | ||
| const asItemId = (value: string): RuntimeItemId => RuntimeItemId.makeUnsafe(value); | ||
|
|
||
| class FakeAmpManager extends AmpServerManager { | ||
| public startSessionImpl = vi.fn(async (threadId: ThreadId): Promise<ProviderSession> => { | ||
| const now = new Date().toISOString(); | ||
| return { | ||
| provider: "amp", | ||
| status: "ready", | ||
| runtimeMode: "full-access", | ||
| threadId, | ||
| cwd: process.cwd(), | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| resumeCursor: { sessionId: `session-${threadId}` }, | ||
| } as unknown as ProviderSession; | ||
| }); | ||
|
|
||
| public sendTurnImpl = vi.fn( | ||
| async (threadId: ThreadId): Promise<ProviderTurnStartResult> => ({ | ||
| threadId, | ||
| turnId: asTurnId(`turn-${threadId}`), | ||
| }), | ||
| ); | ||
|
|
||
| public interruptTurnImpl = vi.fn(async (): Promise<void> => undefined); | ||
| public respondToRequestImpl = vi.fn(async (): Promise<void> => undefined); | ||
| public respondToUserInputImpl = vi.fn(async (): Promise<void> => undefined); | ||
| public readThreadImpl = vi.fn(async (threadId: ThreadId) => ({ threadId, turns: [] })); | ||
| public rollbackThreadImpl = vi.fn(async (threadId: ThreadId) => ({ threadId, turns: [] })); | ||
| public stopAllImpl = vi.fn(() => undefined); | ||
|
|
||
| override startSession(input: { threadId: ThreadId }): Promise<ProviderSession> { | ||
| return this.startSessionImpl(input.threadId); | ||
| } | ||
|
|
||
| override sendTurn(input: { threadId: ThreadId }): Promise<ProviderTurnStartResult> { | ||
| return this.sendTurnImpl(input.threadId); | ||
| } | ||
|
|
||
| override interruptTurn(_threadId: ThreadId): Promise<void> { | ||
| return this.interruptTurnImpl(); | ||
| } | ||
|
|
||
| override respondToRequest( | ||
| _threadId: ThreadId, | ||
| _requestId: ApprovalRequestId, | ||
| _decision: ProviderApprovalDecision, | ||
| ): Promise<void> { | ||
| return this.respondToRequestImpl(); | ||
| } | ||
|
|
||
| override respondToUserInput( | ||
| _threadId: ThreadId, | ||
| _requestId: ApprovalRequestId, | ||
| _answers: ProviderUserInputAnswers, | ||
| ): Promise<void> { | ||
| return this.respondToUserInputImpl(); | ||
| } | ||
|
|
||
| override readThread(threadId: ThreadId) { | ||
| return this.readThreadImpl(threadId); | ||
| } | ||
|
|
||
| override rollbackThread(threadId: ThreadId) { | ||
| return this.rollbackThreadImpl(threadId); | ||
| } | ||
|
|
||
| override stopSession(_threadId: ThreadId): void {} | ||
|
|
||
| override listSessions(): ProviderSession[] { | ||
| return []; | ||
| } | ||
|
|
||
| override hasSession(_threadId: ThreadId): boolean { | ||
| return false; | ||
| } | ||
|
|
||
| override stopAll(): void { | ||
| this.stopAllImpl(); | ||
| } | ||
| } | ||
|
|
||
| const manager = new FakeAmpManager(); | ||
| const layer = it.layer( | ||
| makeAmpAdapterLive({ manager }).pipe(Layer.provideMerge(ServerSettingsService.layerTest())), | ||
| ); | ||
|
|
||
| layer("AmpAdapterLive", (it) => { | ||
| it.effect("delegates session startup to the manager", () => | ||
| Effect.gen(function* () { | ||
| manager.startSessionImpl.mockClear(); | ||
| const adapter = yield* AmpAdapter; | ||
|
|
||
| const session = yield* adapter.startSession({ | ||
| threadId: asThreadId("thread-1"), | ||
| runtimeMode: "full-access", | ||
| }); | ||
|
|
||
| assert.equal(session.provider, "amp"); | ||
| assert.equal(manager.startSessionImpl.mock.calls[0]?.[0], asThreadId("thread-1")); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("rejects attachments until AMP attachment wiring exists", () => | ||
| Effect.gen(function* () { | ||
| const adapter = yield* AmpAdapter; | ||
| const result = yield* adapter | ||
| .sendTurn({ | ||
| threadId: asThreadId("thread-attachments"), | ||
| input: "hello", | ||
| attachments: [{ id: "attachment-1" }] as never, | ||
| }) | ||
| .pipe(Effect.result); | ||
|
|
||
| assert.equal(result._tag, "Failure"); | ||
| if (result._tag !== "Failure") { | ||
| return; | ||
| } | ||
| assert.equal(result.failure._tag, "ProviderAdapterValidationError"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("forwards manager runtime events through the adapter stream", () => | ||
| Effect.gen(function* () { | ||
| const adapter = yield* AmpAdapter; | ||
|
|
||
| const event = { | ||
| type: "content.delta", | ||
| eventId: asEventId("evt-amp-delta"), | ||
| provider: "amp", | ||
| createdAt: new Date().toISOString(), | ||
| threadId: asThreadId("thread-1"), | ||
| turnId: asTurnId("turn-1"), | ||
| itemId: asItemId("item-1"), | ||
| payload: { | ||
| streamKind: "assistant_text", | ||
| delta: "hello", | ||
| }, | ||
| } as unknown as ProviderRuntimeEvent; | ||
|
|
||
| // Emit first — the event is buffered in the unbounded queue via the | ||
| // listener that was registered during layer construction. | ||
| manager.emit("event", event); | ||
|
|
||
| // Now consume the head. Since the queue already has an item, this | ||
| // resolves immediately without a race condition. | ||
| const received = yield* Stream.runHead(adapter.streamEvents); | ||
|
|
||
| assert.equal(received._tag, "Some"); | ||
| if (received._tag !== "Some") { | ||
| return; | ||
| } | ||
| assert.equal(received.value.type, "content.delta"); | ||
| if (received.value.type !== "content.delta") { | ||
| return; | ||
| } | ||
| assert.equal(received.value.payload.delta, "hello"); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configured AMP path is read and then dropped.
Line 67 computes the AMP binary from server settings, but Line 70 still starts the session with the original
input. That makesproviders.amp.binaryPathineffective, so users who configure a non-default AMP executable will still launch the default path.🤖 Prompt for AI Agents