Skip to content

Commit 59e3945

Browse files
t3dotggclaude
andcommitted
fix: address remaining review pushback on #5561
Follow-ups stacked on the connection-resilience RFC (#5561): - supervisor: the retry ladder was non-monotonic after the first rung was raised to 3s ([3s, 2s, 4s, ...] retried the second failure faster than the first). Drop the stale 2s rung: [3s, 4s, 8s, 16s]. - observability: clamp oversized strings nested inside arrays/objects too (e.g. error stacks preserved by normalizeJsonValue), not just top-level values, so the trace-retention win holds. Adds a unit test. - shell: move the command-resolution cache into the Effect environment as a Context.Reference (same idiom as HostProcessPlatform) so tests and embedders can provide an isolated instance, per the Effect Service Conventions check. Also document the 30s negative-caching window. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 5ab7cf2 commit 59e3945

5 files changed

Lines changed: 95 additions & 23 deletions

File tree

‎packages/client-runtime/src/connection/supervisor.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ describe("EnvironmentSupervisor", () => {
358358
);
359359
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
360360

361-
for (const [index, delay] of [3_000, 2_000, 4_000, 8_000, 16_000, 16_000].entries()) {
361+
for (const [index, delay] of [3_000, 4_000, 8_000, 16_000, 16_000, 16_000].entries()) {
362362
yield* TestClock.adjust(delay);
363363
yield* eventuallyState(
364364
supervisor.state,
@@ -741,7 +741,7 @@ describe("EnvironmentSupervisor", () => {
741741

742742
expect(secondFailure.retryAt).not.toBeNull();
743743

744-
yield* TestClock.adjust("1 second");
744+
yield* TestClock.adjust("3 seconds");
745745
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
746746

747747
yield* TestClock.adjust("1 second");

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import * as RpcSession from "../rpc/session.ts";
2929
import { safeErrorLogAttributes } from "../errors/safeLog.ts";
3030
import * as ConnectionWakeups from "./wakeups.ts";
3131

32-
const RETRY_DELAYS_MS = [3_000, 2_000, 4_000, 8_000, 16_000] as const;
32+
const RETRY_DELAYS_MS = [3_000, 4_000, 8_000, 16_000] as const;
3333
const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds";
3434
const CONNECTION_PROBE_TIMEOUT = "3 seconds";
3535
const MOBILE_CONNECTION_PROBE_TIMEOUT = "3 seconds";

‎packages/shared/src/observability.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
makeTraceSink,
2222
type TraceRecord,
2323
type TraceSinkFlushStats,
24+
truncateTraceAttributes,
2425
} from "./observability.ts";
2526

2627
describe("errorTag", () => {
@@ -111,6 +112,31 @@ const makeTestLayer = (tracePath: string) =>
111112

112113
const nodeServicesIt = it.layer(NodeServices.layer);
113114

115+
describe("truncateTraceAttributes", () => {
116+
it("clamps oversized strings at any depth without mutating the input", () => {
117+
const stack = "s".repeat(2_000);
118+
const attributes = {
119+
"db.query.text": "q".repeat(2_000),
120+
short: "ok",
121+
error: { name: "Error", stack, nested: ["a".repeat(2_000)] },
122+
};
123+
const truncated = truncateTraceAttributes(attributes);
124+
125+
assert.equal((truncated["db.query.text"] as string).length, 200 + "…[truncated]".length);
126+
assert.equal(truncated["short"], "ok");
127+
const error = truncated["error"] as { stack: string; nested: Array<string> };
128+
assert.equal(error.stack.length, 500 + "…[truncated]".length);
129+
assert.equal(error.nested[0]?.length, 500 + "…[truncated]".length);
130+
// Input is untouched: the live span's attributes are shared.
131+
assert.equal(attributes.error.stack, stack);
132+
});
133+
134+
it("returns the same reference when nothing exceeds the limits", () => {
135+
const attributes = { short: "ok", nested: { fine: "also ok" } };
136+
assert.equal(truncateTraceAttributes(attributes), attributes);
137+
});
138+
});
139+
114140
describe("observability", () => {
115141
it("normalizes circular arrays, maps, and sets without recursing forever", () => {
116142
const array: Array<unknown> = ["alpha"];

‎packages/shared/src/observability.ts‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,52 @@ const TRACE_ATTRIBUTE_TRUNCATED_LENGTH = 200;
253253
const TRACE_ATTRIBUTE_TRUNCATION_SUFFIX = "…[truncated]";
254254
const ALWAYS_TRUNCATED_TRACE_ATTRIBUTES: ReadonlySet<string> = new Set(["db.query.text"]);
255255

256+
// Clamps strings nested inside already-normalized attribute values (arrays and
257+
// plain objects from normalizeJsonValue, e.g. an Error's `stack`). Returns the
258+
// input reference when nothing was clamped.
259+
function truncateNestedValue(value: unknown): unknown {
260+
if (typeof value === "string") {
261+
return value.length <= TRACE_ATTRIBUTE_MAX_LENGTH
262+
? value
263+
: `${value.slice(0, TRACE_ATTRIBUTE_MAX_LENGTH)}${TRACE_ATTRIBUTE_TRUNCATION_SUFFIX}`;
264+
}
265+
if (Array.isArray(value)) {
266+
const truncated = value.map(truncateNestedValue);
267+
return truncated.some((entry, index) => entry !== value[index]) ? truncated : value;
268+
}
269+
if (isPlainObject(value)) {
270+
let truncated: Record<string, unknown> | undefined;
271+
for (const [key, entry] of Object.entries(value)) {
272+
const next = truncateNestedValue(entry);
273+
if (next === entry) continue;
274+
truncated ??= { ...value };
275+
truncated[key] = next;
276+
}
277+
return truncated ?? value;
278+
}
279+
return value;
280+
}
281+
256282
/**
257283
* Clamps oversized attribute values on the serialized trace record so the file
258-
* sink stays small. Returns a new record when anything was clamped; never
284+
* sink stays small, including strings nested inside arrays and objects (e.g.
285+
* error stacks). Returns a new record when anything was clamped; never
259286
* mutates the input (the live span's attributes are shared with other tracers).
260287
*/
261-
function truncateTraceAttributes(attributes: TraceAttributes): TraceAttributes {
288+
export function truncateTraceAttributes(attributes: TraceAttributes): TraceAttributes {
262289
let truncated: Record<string, unknown> | undefined;
263290
for (const [key, value] of Object.entries(attributes)) {
264-
if (typeof value !== "string") continue;
265-
const maxLength = ALWAYS_TRUNCATED_TRACE_ATTRIBUTES.has(key)
266-
? TRACE_ATTRIBUTE_TRUNCATED_LENGTH
267-
: TRACE_ATTRIBUTE_MAX_LENGTH;
268-
if (value.length <= maxLength) continue;
291+
if (typeof value === "string" && ALWAYS_TRUNCATED_TRACE_ATTRIBUTES.has(key)) {
292+
if (value.length <= TRACE_ATTRIBUTE_TRUNCATED_LENGTH) continue;
293+
truncated ??= { ...attributes };
294+
truncated[key] =
295+
`${value.slice(0, TRACE_ATTRIBUTE_TRUNCATED_LENGTH)}${TRACE_ATTRIBUTE_TRUNCATION_SUFFIX}`;
296+
continue;
297+
}
298+
const next = truncateNestedValue(value);
299+
if (next === value) continue;
269300
truncated ??= { ...attributes };
270-
truncated[key] = `${value.slice(0, maxLength)}${TRACE_ATTRIBUTE_TRUNCATION_SUFFIX}`;
301+
truncated[key] = next;
271302
}
272303
return truncated ?? attributes;
273304
}

‎packages/shared/src/shell.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,29 +498,43 @@ function resolveCommandCandidates(
498498
// (platform, PATH, PATHEXT, command) for a short window: repeat scans hit the
499499
// cache while any change to the search environment invalidates immediately.
500500
// Explicit-path resolution is never cached - callers probe paths they have
501-
// just written (e.g. managed binary installs).
501+
// just written (e.g. managed binary installs). A "not-found" outcome is also
502+
// cached for the TTL, so a just-installed binary can stay invisible for up to
503+
// 30s unless resolved by explicit path.
502504
// TTL expiry uses the monotonic clock (Clock.currentTimeNanos) so backward
503505
// wall-clock adjustments cannot keep expired entries alive.
504506
const COMMAND_RESOLUTION_CACHE_TTL_NANOS = 30_000_000_000n;
505507
const COMMAND_RESOLUTION_CACHE_MAX_ENTRIES = 512;
506508
const COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR = String.fromCharCode(0);
507-
const commandResolutionCache = new Map<
508-
string,
509-
{ readonly resolvedPath: string | null; readonly expiresAtNanos: bigint }
510-
>();
509+
510+
interface CommandResolutionCacheEntry {
511+
readonly resolvedPath: string | null;
512+
readonly expiresAtNanos: bigint;
513+
}
514+
515+
// The cache lives in the Effect environment (like HostProcessPlatform above)
516+
// so tests and embedders can provide an isolated instance; the default is a
517+
// single process-wide map shared by all consumers.
518+
export const CommandResolutionCache = Context.Reference<Map<string, CommandResolutionCacheEntry>>(
519+
"@t3tools/shared/shell/CommandResolutionCache",
520+
{
521+
defaultValue: () => new Map(),
522+
},
523+
);
511524

512525
function cacheCommandResolution(
526+
cache: Map<string, CommandResolutionCacheEntry>,
513527
cacheKey: string,
514528
resolvedPath: string | null,
515529
nowNanos: bigint,
516530
): void {
517-
if (commandResolutionCache.size >= COMMAND_RESOLUTION_CACHE_MAX_ENTRIES) {
518-
const oldestKey = commandResolutionCache.keys().next().value;
531+
if (cache.size >= COMMAND_RESOLUTION_CACHE_MAX_ENTRIES) {
532+
const oldestKey = cache.keys().next().value;
519533
if (oldestKey !== undefined) {
520-
commandResolutionCache.delete(oldestKey);
534+
cache.delete(oldestKey);
521535
}
522536
}
523-
commandResolutionCache.set(cacheKey, {
537+
cache.set(cacheKey, {
524538
resolvedPath,
525539
expiresAtNanos: nowNanos + COMMAND_RESOLUTION_CACHE_TTL_NANOS,
526540
});
@@ -577,8 +591,9 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
577591
const cacheKey = [platform, pathValue, windowsPathExtensions.join(";"), command].join(
578592
COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR,
579593
);
594+
const cache = yield* CommandResolutionCache;
580595
const nowNanos = yield* Clock.currentTimeNanos;
581-
const cached = commandResolutionCache.get(cacheKey);
596+
const cached = cache.get(cacheKey);
582597
if (cached !== undefined && cached.expiresAtNanos > nowNanos) {
583598
if (cached.resolvedPath === null) {
584599
return yield* new CommandResolutionError({ command, reason: "not-found" });
@@ -598,12 +613,12 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
598613
for (const candidate of commandCandidates) {
599614
const candidatePath = path.join(pathEntry, candidate);
600615
if (yield* isExecutableFile(candidatePath, platform, windowsPathExtensions)) {
601-
cacheCommandResolution(cacheKey, candidatePath, nowNanos);
616+
cacheCommandResolution(cache, cacheKey, candidatePath, nowNanos);
602617
return candidatePath;
603618
}
604619
}
605620
}
606-
cacheCommandResolution(cacheKey, null, nowNanos);
621+
cacheCommandResolution(cache, cacheKey, null, nowNanos);
607622
return yield* new CommandResolutionError({ command, reason: "not-found" });
608623
});
609624

0 commit comments

Comments
 (0)