Skip to content

feat(findings): copied-type-cast flags a cast onto a same-name copy of the value's type - #490

Merged
byronwall merged 4 commits into
mainfrom
claude/copied-type-cast
Nov 28, 2026
Merged

byronwall merged 4 commits into
mainfrom
claude/copied-type-cast

Conversation

@byronwall

@byronwall byronwall commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

srcly now reports a new finding, copied-type-cast: a cast such as fetchDoc(id) as Promise<DocDetail> onto a type that has the same name as the value's type but is a different declaration. The cast is what keeps tsc from comparing the two copies. This is the other half of duplicate-type (#483): that rule finds the copied declaration, this one finds the cast that hides it, also when the original is a library type such as a Prisma enum.

What you'll see

  • A new warning in findings: srcly findings --rule copied-type-cast, srcly report's checks line (srcly copied-type casts (<tsconfig>)), the findings queue and the code viewer.
  • The message names both declarations: `t.status as RunStatus` casts the `RunStatus` from .prisma/client/index.d.ts:424 to a different `RunStatus`, declared in app/src/services/ai/ai-runs.queries.ts:4.

How it decides

The rule needs the type checker, so a small Node script (copied_type_casts.mjs) runs with the project's own typescript package once per tsconfig (the same configs and the same cap of 6 that tsc gets). A cast (x as T or <T>x, not as const) is reported when:

  • the operand's type and T are named types (a type alias, interface, class or enum) with the same name and no shared declaration; for Promise<A> as Promise<B> the payloads are compared;
  • at least one of the two is declared in the project, so DOM's and Node's ReadableStream stay out.

The target name is the one written in the cast, so type Other = Doc; x as Other is not a Doc copy. An enum member stands for its enum. Anonymous object types, type parameters, test files and .d.ts files are left out. Project references are read from their sources, so an unbuilt reference does not read as a clean 0. A tsconfig the rule cannot check (no node_modules/typescript, over the cap, no Node) makes srcly findings --rule copied-type-cast read N/A with the reason. The cache key covers every TS and JSON file in the repository, because the checker reads files outside the scanned folder.

Proof

Visual Notes d59937fe (app/, 1,427 files, one tsconfig, 6 to 10 s for the rule), with srcly findings:

src/components/DocumentSidePanel.tsx:42 copied-type-cast `fetchDoc(id) as Promise<DocDetail>` casts the `DocDetail` from app/src/services/docs.queries.ts:10 to a different `DocDetail`, declared in app/src/components/DocumentSidePanel.tsx:25. …
src/components/DocumentSidePanel.tsx:122 copied-type-cast `fetchDocLatest(id) as Promise<DocDetail>` …
src/components/command-palette/palette-search.queries.ts:64 copied-type-cast `task.status as TaskStatus` casts the `TaskStatus` from .prisma/client/index.d.ts:479 to a different `TaskStatus`, declared in app/src/services/tasks/tasks.types.ts:9. …
src/routes/docs/[id].tsx:48 copied-type-cast `fetchDoc(id) as Promise<DocDetail>` …
src/routes/docs/[id].tsx:81 copied-type-cast `fetchDocLatest(id) as Promise<DocDetail>` …
src/server/people/memory-store.ts:59 copied-type-cast `i.kind as InteractionKind` casts the `InteractionKind` from .prisma/client/index.d.ts:619 to a different `InteractionKind`, declared in app/src/lib/people/types.ts:23. …
src/services/ai/ai-chat.queries.ts:66 copied-type-cast `t.status as ChatThreadStatus` …
src/services/ai/ai-chat.queries.ts:74 copied-type-cast `t.messages[0].role as ChatMessageRole` …
src/services/ai/ai-chat.queries.ts:109 copied-type-cast `thread.status as ChatThreadStatus` …
src/services/ai/ai-chat.queries.ts:117 copied-type-cast `m.role as ChatMessageRole` …
src/services/ai/ai-runs.queries.ts:86 copied-type-cast `r.status as RunStatus` …
src/services/ai/ai-runs.queries.ts:123 copied-type-cast `run.status as RunStatus` …

copied-type-cast: 12
duplicate-type: 5

10 of 10 of the copies the TASK-0017 measurement found are reported: DocDetail x4, ChatThreadStatus/ChatMessageRole x4 in ai-chat.queries.ts, RunStatus x2 in ai-runs.queries.ts. Every other hit, read by hand:

  • TaskStatus (palette-search.queries.ts:64) and InteractionKind (memory-store.ts:59): real copies. TASK_STATUSES and INTERACTION_KINDS restate Prisma's TaskStatus and InteractionKind enums value for value, under the same names. The measurement missed them because the checker folds (typeof X)[number] to a nameless union. The rule reads the name written in the cast.
  • The measurement's 6 other same-name hits are gone: 4 anonymous object types (regroup() as { members: … } and the like, and the dagre g.node(id) cast), and 2 ReadableStream casts between DOM's and Node's declarations, which are both library types.

So the 12 hits are 12 real copies. On srcly itself (6 tsconfigs) the rule reports 0.

Tests: server/tests/test_copied_type_casts.py runs the real checker on small projects: true cases (a Promise payload copy, a union alias, a narrowed enum value), false cases (the same declaration under another name, an alias with another name, a parenthesized target, a wrapping Promise alias, an anonymous shape, as const, a lib type, a one-member enum, a type parameter), a test file scanned alone, one file as the scan root, a change outside the scanned folder that changes the cache signature, a broken extends that reads N/A, two overlapping tsconfigs that give one finding, an unbuilt project reference, and skipped configs that make the count N/A. scripts/check.sh server passes (656 tests).

Codex review (gpt-6.2-sol), 3 rounds:

  • Round 1 (FAIL, 2 P1, 3 P2): stale cache for scoped scans, a missing extends base read as 0, enum members and type parameters read as copies, one-file roots checked nothing, configs over the cap left the count complete. All fixed.
  • Round 2 (FAIL, 2 P1, 3 P2): the cache and background jobs could still reuse a result after a change outside the tsconfig folder; an unbuilt project reference read as 0; overlapping tsconfigs reported one cast twice; the checker's folded alias named the wrong type; a test file scanned alone was reported. All fixed except the type Id = number half of the alias finding (declined below).
  • Round 3 (FAIL, 2 P1, 3 P2): fixed after the round, without a 4th review: a Promise alias that wraps its argument, disableSourceOfProjectReferenceRedirect, a parenthesized target, and one file loaded through a symlink and a relative path. Still open: changes under node_modules (below), and inline import() targets.

Round 3's architecture note: have the TypeScript runner return a project snapshot with canonical declaration ids, the files and resolution inputs it read, and a coverage status, and use that one snapshot to dedupe findings and to decide cache and job reuse.

Survey moves: server only, from this PR's corpus edits to server/app/services/findings/ (jobs.py now calls runner.tools_signature, which plans the tools and reads every tool's signature()). Boundary filesystem −6 and frontier external-code −54 net, over 12 traces that all stay partial. scan_codebase and cache.fingerprint gain 1 boundary and 6 frontiers each (the new read_jsonc file read and the planning calls behind tools_signature), and Finding gains 1 frontier. The traces of 6 findings classes and helpers (BuiltinTsTool, ReachabilityTool, RuffTool, MissingTool, EslintTool, find_up) lose 1 boundary and 11 frontiers each. They now end at the job list (self._jobs.append) and no longer list the cache-write, fix and listing stops: their downstream slice falls from about 600 to about 330 nodes, with the budget not exhausted. resolve_auto_base and load_side lose 1 boundary each, and build_compare loses 1 frontier. I reproduced the exact −6/−54 locally with survey-blockers.ts --every 5 at f0fd3da8 and at the head, and diffed per trace. No outcome count, error or trace time moves. Why the slice shrinks (instead of adding the job path to the cache path) is not explained yet; it is a follow-up for the engine.

Limits

  • It compares named types only, so a cast between two unions, arrays (as Doc[]) or anonymous shapes is not checked. Promise<…> is the only wrapper it unwraps.
  • It skips compare snapshots, which have no node_modules, like tsc does.

Open findings

  • P1 · diff · server/app/services/findings/copied_type_casts.py:122 — A change to a declaration under node_modules (prisma generate renaming an enum) does not change the cache signature, so a cached result can stay stale until a source file changes. Open: the reuse key would need the files the checker resolved (round 3's architecture note).
  • P2 · diff · server/app/services/findings/copied_type_casts.mjs:68 — A target written as an inline import('./view').Other falls back to the checker's folded type, so an alias with another name can read as a copy. Open: rare spelling; resolve the import() qualifier.
  • P2 · diff · server/app/services/findings/copied_type_casts.mjs:68 — Two separate type Id = number declarations are not reported: the operand's type is folded to number and has no name. Declined: catching it needs the producer's declaration, not the checker's type.

🤖 Generated with Claude Code

byronwall and others added 4 commits November 28, 2026 00:35
…f the value's type

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gs, resolves enum members, checks one-file roots

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s project references to sources, dedupes across tsconfigs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…arentheses, forces source redirects, sees symlinked files as one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@byronwall
byronwall enabled auto-merge (squash) November 28, 2026 06:12
@byronwall
byronwall merged commit 9dfcb7f into main Nov 28, 2026
11 of 13 checks passed
@byronwall
byronwall deleted the claude/copied-type-cast branch November 28, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant