Public: anyone can read this; the crew can change it.

Duplication report: near-duplicate functions and tests clustered into one shape #14654

Closed opened this in answer to
Closed without merging

The report runs, its top clusters are listed below with the measured duplication rate, and one cluster is folded: the fixed-sleep polling loop. The tests that cover the change pass (a 46-file run, then the dupes, gate and honesty suites); I did not run the whole suite.

Notes

stopped closed without shipping deploy ca8f3cd

opened this

The report runs, its top clusters are listed below with the measured duplication rate, and one cluster is folded: the fixed-sleep polling loop. The tests that cover the change pass (a 46-file run, then the dupes, gate and honesty suites); I did not run the whole suite.

What changed

  • Report: bun scripts/dupes.ts reads every function and test body under src/ and test/ as a shape (scripts/dupes_shape.ts: names, strings and numbers taken out). It clusters three or more that are near-duplicates and prints each cluster with the shape its members all hold. It takes about 10 s.
  • Threshold: THRESHOLD is 0.55, the best F1 over 41 pairs I labeled from this repo (scripts/dupes-pairs.json, --tune), not the paper's 0.71. 0.55 gives precision 0.64 and recall 0.58 on those pairs, so treat it as a first tuning. test/dupes.test.ts holds the constant to the best-scoring threshold.
  • Measured rate: 41,891 functions and tests of 40 tokens or more. 441 clusters hold 1,974 of them (4.7%). One implementation each would save about 133k tokens, 2.0% of function tokens. This is close to fallow's 2.13% on the same tree. Nested bodies count in each unit, so the denominator is generous.
  • Top clusters:
    • 16 copies of the boot helper in tui-* tests (176 shared tokens).
    • 30 MCP tool/call/mcp helpers in tests (77 shared tokens).
    • 18 *-timing page tests (128 shared tokens).
    • 15 *-fit tests, "no inline width…" (145 shared tokens).
    • 18 more MCP call helpers (95 shared tokens).
    • 9 src/tui/screens/* screens (packScreen and its siblings, 173 shared tokens).
    • 8 asUser/web fetch helpers (161 shared tokens).
  • Folded: The largest fixed-sleep shape was for (let i = 0; i < N && cond; i++) await Bun.sleep(M), 70 copies across 46 tests. They now call waitFor in test/helpers/poll.ts, which waits the same interval for the same total time and, like the loop, does not throw. pollUntil now sits on it. test-honesty-allow.json dropped by the 70 waits (the committed allowance is 493 files). The 46 changed files pass: 453 pass, 1 skip.
  • Gate note: dupesNote in scripts/gate.ts names an existing function when a pull adds one at or over the threshold. It is a note only. It skips test bodies, since sibling tests read alike by design. It reads the whole tree, about 10 s, and only when the diff adds a function. On this pull it flagged my own test's git helper as 0.75 like test/both-doors-refusal.test.ts#run. I left that duplicate in place.
  • Refactor: gate.ts wrote the base-ref lookup out eleven times. It is now one baseRef.
  • Docs: AGENTS.md says how to run the report and when to use waitFor.

fallow, run once and read-only, off the checks path

  • Dupes: 762 clone groups, 2.13% of tokens. The report agrees on the shape of the duplication, so I ported nothing from it.
  • Dead code: 2,365 unused exports and 433 unused files, mostly noise because fallow does not know the forge's entry points. The gate's unused-exports and unreached rules already cover this.
  • Circular dependencies: fallow found 50. The gate has no cycle rule. I did not port one, because it needs a baseline and is its own change.

Not done

  • The note does not block; that waits until its false-positive rate is measured.
  • The tui boot, MCP call and *-fit clusters are listed but not folded. Each is a one-helper change for its own pull.
  • The while (cond && Date.now() < deadline) await Bun.sleep(M) form (about 50 sites) is not converted, because each leaves a deadline variable behind. Loops with unbounded waits are also left.

Try it: run bun scripts/dupes.ts 15, bun scripts/dupes.ts --tune and bun test test/dupes.test.ts.

🤖 Generated with Claude Code

Closes #14620

commented

The report runs, its top clusters are listed below with the measured duplication rate, and one cluster is folded: the fixed-sleep polling loop. The tests that cover the change pass (a 46-file run, then the dupes, gate and honesty suites); I did not run the whole suite.

What changed

  • Report: bun scripts/dupes.ts reads every function and test body under src/ and test/ as a shape (scripts/dupes_shape.ts: names, strings and numbers taken out). It clusters three or more that are near-duplicates and prints each cluster with the shape its members all hold. It takes about 10 s.
  • Threshold: THRESHOLD is 0.55, the best F1 over 41 pairs I labeled from this repo (scripts/dupes-pairs.json, --tune), not the paper's 0.71. 0.55 gives precision 0.64 and recall 0.58 on those pairs, so treat it as a first tuning. test/dupes.test.ts holds the constant to the best-scoring threshold.
  • Measured rate: 41,891 functions and tests of 40 tokens or more. 441 clusters hold 1,974 of them (4.7%). One implementation each would save about 133k tokens, 2.0% of function tokens. This is close to fallow's 2.13% on the same tree. Nested bodies count in each unit, so the denominator is generous.
  • Top clusters:
    • 16 copies of the boot helper in tui-* tests (176 shared tokens).
    • 30 MCP tool/call/mcp helpers in tests (77 shared tokens).
    • 18 *-timing page tests (128 shared tokens).
    • 15 *-fit tests, "no inline width…" (145 shared tokens).
    • 18 more MCP call helpers (95 shared tokens).
    • 9 src/tui/screens/* screens (packScreen and its siblings, 173 shared tokens).
    • 8 asUser/web fetch helpers (161 shared tokens).
  • Folded: The largest fixed-sleep shape was for (let i = 0; i < N && cond; i++) await Bun.sleep(M), 70 copies across 46 tests. They now call waitFor in test/helpers/poll.ts, which waits the same interval for the same total time and, like the loop, does not throw. pollUntil now sits on it. test-honesty-allow.json dropped by the 70 waits (the committed allowance is 493 files). The 46 changed files pass: 453 pass, 1 skip.
  • Gate note: dupesNote in scripts/gate.ts names an existing function when a pull adds one at or over the threshold. It is a note only. It skips test bodies, since sibling tests read alike by design. It reads the whole tree, about 10 s, and only when the diff adds a function. On this pull it flagged my own test's git helper as 0.75 like test/both-doors-refusal.test.ts#run. I left that duplicate in place.
  • Refactor: gate.ts wrote the base-ref lookup out eleven times. It is now one baseRef.
  • Docs: AGENTS.md says how to run the report and when to use waitFor.

fallow, run once and read-only, off the checks path

  • Dupes: 762 clone groups, 2.13% of tokens. The report agrees on the shape of the duplication, so I ported nothing from it.
  • Dead code: 2,365 unused exports and 433 unused files, mostly noise because fallow does not know the forge's entry points. The gate's unused-exports and unreached rules already cover this.
  • Circular dependencies: fallow found 50. The gate has no cycle rule. I did not port one, because it needs a baseline and is its own change.

Not done

  • The note does not block; that waits until its false-positive rate is measured.
  • The tui boot, MCP call and *-fit clusters are listed but not folded. Each is a one-helper change for its own pull.
  • The while (cond && Date.now() < deadline) await Bun.sleep(M) form (about 50 sites) is not converted, because each leaves a deadline variable behind. Loops with unbounded waits are also left.

Try it: run bun scripts/dupes.ts 15, bun scripts/dupes.ts --tune and bun test test/dupes.test.ts.

🤖 Generated with Claude Code

commented

Declined: I found nothing in this pull to fix, so I pushed nothing. Run log: https://forge.open-platform.sh/trevato/forge/runs/01m7x1v284hzvcexd1eb1tqm8s

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs