authorgravatar for julius0216@outlook.comJulius Marminge <julius0216@outlook.com> 2026-02-26 09:04:10-08:00
committergravatar for jmarminge@gmail.comJulius Marminge <jmarminge@gmail.com> 2026-02-26 13:20:36-08:00
log7ad5d03948a27e64bb11f6862d2f23333a0a2bd1
tree91c75ffa9bda352366e9794f2b6be5c5df1eb4ea
parent6cf03d987c28af7fdc7867d63f791720a0464a27

fix bad decoding


2 files changed, 17 insertions(+), 30 deletions(-)

.plans/16c-pr89-remediation-checklist.md+2-2
......@@ -360,8 +360,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
360360 - Threads: PRRT_kwDORLtfbc5wiaR-
361361 - Audit note: Decode and SQL errors still collapsed into one persistence error kind.
362362
363- [ ] `C049` `JSON.stringify(cause)` returns `undefined` for `undefined`, functions, or symbols, violating the `string` return type. Consider coercing the result to a string (e.g., `String(JSON.stringify(cause))`) or adding a fallback.
364 - Status: `TODO`
363- [x] `C049` `JSON.stringify(cause)` returns `undefined` for `undefined`, functions, or symbols, violating the `string` return type. Consider coercing the result to a string (e.g., `String(JSON.stringify(cause))`) or adding a fallback.
364 - Status: `DONE`
365365 - Verdict: `valid`
366366 - Severity: `Low`
367367 - Area: `Other`
apps/server/src/provider/Layers/ProviderService.ts+15-28
......@@ -22,7 +22,7 @@ import {
2222 type ProviderRuntimeEvent,
2323 type ProviderSession,
2424} from "@t3tools/contracts";
25import { Effect, Layer, Option, PubSub, Queue, Ref, Schema, Stream } from "effect";
25import { Effect, Layer, Option, PubSub, Queue, Ref, Schema, SchemaIssue, Stream } from "effect";
2626
2727import { ProviderSessionNotFoundError, ProviderValidationError } from "../Errors.ts";
2828import { ProviderAdapterRegistry } from "../Services/ProviderAdapterRegistry.ts";
......@@ -54,30 +54,21 @@ function toValidationError(
5454 });
5555}
5656
57function decodeIssueMessage(cause: unknown): string {
58 if (cause instanceof Error && cause.message.length > 0) {
59 return cause.message;
60 }
61 try {
62 return JSON.stringify(cause);
63 } catch {
64 return "Invalid provider input.";
65 }
66}
67
68function decodeInputOrValidationError<S extends Schema.Top>(input: {
57const decodeInputOrValidationError = <S extends Schema.Top>(input: {
6958 readonly operation: string;
7059 readonly schema: S;
7160 readonly payload: unknown;
72}): Effect.Effect<Schema.Schema.Type<S>, ProviderValidationError> {
73 try {
74 return Effect.succeed(
75 Schema.decodeUnknownSync(input.schema as never)(input.payload) as Schema.Schema.Type<S>,
76 );
77 } catch (cause) {
78 return Effect.fail(toValidationError(input.operation, decodeIssueMessage(cause), cause));
79 }
80}
61}) =>
62 Schema.decodeUnknownEffect(input.schema)(input.payload).pipe(
63 Effect.mapError(
64 (schemaError) =>
65 new ProviderValidationError({
66 operation: input.operation,
67 issue: SchemaIssue.makeFormatterDefault()(schemaError.issue),
68 cause: schemaError,
69 }),
70 ),
71 );
8172
8273function toRuntimeStatus(session: ProviderSession): "starting" | "running" | "stopped" | "error" {
8374 switch (session.status) {
......@@ -265,9 +256,7 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
265256 provider: existing.provider,
266257 threadId: input.binding.threadId,
267258 providerThreadId: existingProviderThreadId,
268 ...(existing.resumeCursor !== undefined
269 ? { resumeCursor: existing.resumeCursor }
270 : {}),
259 ...(existing.resumeCursor !== undefined ? { resumeCursor: existing.resumeCursor } : {}),
271260 });
272261 if (existing.sessionId !== input.staleSessionId) {
273262 yield* setAlias(input.staleSessionId, existing.sessionId);
......@@ -310,9 +299,7 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
310299 provider: resumed.provider,
311300 threadId: input.binding.threadId,
312301 providerThreadId: resumedProviderThreadId,
313 ...(resumed.resumeCursor !== undefined
314 ? { resumeCursor: resumed.resumeCursor }
315 : {}),
302 ...(resumed.resumeCursor !== undefined ? { resumeCursor: resumed.resumeCursor } : {}),
316303 });
317304
318305 if (resumed.sessionId !== input.staleSessionId) {