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

Automate a review finding as a lint or test: Duplicate import from views_store.ts; merge into one. #17004

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/11516-automate-a-review-finding-as-a-lint-or-t → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/11516-automate-a-review-finding-as-a-lint-or-t was merged into main as 6b46197.

Review

Sign in to review this change.
4 files changed +400 −4
scripts/dup-imports-allow.json added +255
@@ -0,0 +1,255 @@
1 {
2 "src/actions/apps.ts": 1,
3 "src/actions/asks_decide.ts": 1,
4 "src/actions/first_ask.ts": 1,
5 "src/actions/fleet.ts": 1,
6 "src/actions/holds.ts": 1,
7 "src/actions/migrate.ts": 1,
8 "src/actions/pulls.ts": 2,
9 "src/actions/repos.ts": 1,
10 "src/app.ts": 1,
11 "src/apps/runtime_bringup.ts": 1,
12 "src/apps/runtime_deploy.ts": 1,
13 "src/apps/runtime_events.ts": 1,
14 "src/checks/event_handler.ts": 1,
15 "src/checks/gate_run.ts": 1,
16 "src/checks/local_runner.ts": 1,
17 "src/checks/recheck_base.ts": 1,
18 "src/cloud/places_read.ts": 1,
19 "src/core/reconcile.ts": 1,
20 "src/core/talk_work.ts": 1,
21 "src/crew/conversation.ts": 1,
22 "src/crew/dispatch.ts": 2,
23 "src/crew/early.ts": 1,
24 "src/crew/held.ts": 1,
25 "src/crew/vision.ts": 1,
26 "src/fleet/verbs.ts": 2,
27 "src/http/api.ts": 8,
28 "src/http/api/admin.ts": 1,
29 "src/http/api/app.ts": 1,
30 "src/http/api/app_pulls.ts": 1,
31 "src/http/api/conversation_talk.ts": 2,
32 "src/http/api/family.ts": 1,
33 "src/http/api/issues.ts": 3,
34 "src/http/api/maintenance_runs.ts": 1,
35 "src/http/api/pulls.ts": 1,
36 "src/http/api/upstream.ts": 1,
37 "src/http/feed.ts": 1,
38 "src/http/forges_routes.ts": 1,
39 "src/http/mcp.ts": 1,
40 "src/http/mcp/tools/app_data_import.ts": 1,
41 "src/http/mcp/tools/backups.ts": 1,
42 "src/http/mcp/tools/crew.ts": 1,
43 "src/http/mcp/tools/email_verify.ts": 1,
44 "src/http/mcp/tools/flow.ts": 1,
45 "src/http/mcp/tools/migrate_data.ts": 1,
46 "src/http/mcp/tools/pull.ts": 1,
47 "src/http/mcp/tools/system.ts": 1,
48 "src/http/mcp/tools/telemetry_history.ts": 1,
49 "src/http/rp.ts": 1,
50 "src/http/server.ts": 1,
51 "src/http/settings_routes.ts": 1,
52 "src/main.ts": 1,
53 "src/tui/forge.ts": 1,
54 "src/tui/screens/bills.ts": 1,
55 "src/tui/screens/cloud.ts": 1,
56 "src/tui/screens/dashboard.ts": 2,
57 "src/tui/screens/display.ts": 1,
58 "src/tui/screens/external.ts": 1,
59 "src/tui/screens/forges.ts": 2,
60 "src/tui/screens/forks.ts": 1,
61 "src/tui/screens/issue.ts": 2,
62 "src/tui/screens/next.ts": 1,
63 "src/tui/screens/packs.ts": 1,
64 "src/tui/screens/profile.ts": 1,
65 "src/tui/screens/pull.ts": 1,
66 "src/tui/screens/recoverykit.ts": 1,
67 "src/tui/screens/system.ts": 1,
68 "src/tui/screens/waiting.ts": 1,
69 "src/tui/session.ts": 1,
70 "src/web/chrome.ts": 1,
71 "src/web/map_read.ts": 1,
72 "src/web/pages/backups.ts": 2,
73 "src/web/pages/billing.ts": 1,
74 "src/web/pages/board.ts": 2,
75 "src/web/pages/boards-view.ts": 1,
76 "src/web/pages/flow-panel.ts": 1,
77 "src/web/pages/flows.ts": 1,
78 "src/web/pages/following.ts": 1,
79 "src/web/pages/forges.ts": 2,
80 "src/web/pages/map.ts": 1,
81 "src/web/pages/pricing.ts": 1,
82 "src/web/pages/pull.ts": 1,
83 "src/web/pages/repo.ts": 2,
84 "src/web/pages/runs.ts": 1,
85 "src/web/pages/site.ts": 1,
86 "src/web/pages/sources.ts": 1,
87 "src/web/pages/system.ts": 1,
88 "src/web/pages/talk.ts": 3,
89 "src/web/pages/talk_preview.ts": 1,
90 "src/web/pages/thread.ts": 2,
91 "src/web/studio/forks_rows.ts": 1,
92 "src/web/studio/home.ts": 2,
93 "test/account-export.test.ts": 2,
94 "test/agents-md.test.ts": 1,
95 "test/api-forge-view.test.ts": 1,
96 "test/api-reader-marks.test.ts": 1,
97 "test/app-empty-crew.test.ts": 1,
98 "test/app-flags.test.ts": 1,
99 "test/app-releases.test.ts": 2,
100 "test/app-variants.test.ts": 1,
101 "test/apps-build-preview.test.ts": 1,
102 "test/apps-crew.test.ts": 1,
103 "test/apps-demo.test.ts": 1,
104 "test/apps-measure.test.ts": 1,
105 "test/apps-proxy-unavailable.test.ts": 1,
106 "test/apps.test.ts": 1,
107 "test/asks-bulk.test.ts": 1,
108 "test/asks-counts.test.ts": 1,
109 "test/autonomy.test.ts": 2,
110 "test/bets-card-fit.test.ts": 1,
111 "test/bets.test.ts": 2,
112 "test/billing-never-deletes.test.ts": 1,
113 "test/board-pins.test.ts": 1,
114 "test/board-windows.test.ts": 1,
115 "test/boards-query.test.ts": 1,
116 "test/boards-starting.test.ts": 1,
117 "test/builds-rows.test.ts": 1,
118 "test/bundle-uri.test.ts": 1,
119 "test/business-plan.test.ts": 1,
120 "test/changelog-range.test.ts": 1,
121 "test/chapters-page.test.ts": 1,
122 "test/chapters-pick-order.test.ts": 1,
123 "test/chapters.test.ts": 1,
124 "test/check-order-dependent.test.ts": 1,
125 "test/checks-own-first.test.ts": 1,
126 "test/checks-placement.test.ts": 1,
127 "test/child-credit.test.ts": 2,
128 "test/client-list.test.ts": 1,
129 "test/conversation-talk.test.ts": 2,
130 "test/crew-clone-git.test.ts": 1,
131 "test/crew-direction.test.ts": 1,
132 "test/crew-draft.test.ts": 3,
133 "test/crew-interrupted-out.test.ts": 2,
134 "test/crew-interrupted.test.ts": 1,
135 "test/crew-loop.test.ts": 3,
136 "test/crew-network.test.ts": 1,
137 "test/crew-pin-hook.test.ts": 1,
138 "test/crew-retry-parked.test.ts": 1,
139 "test/crew-sandbox-boundary.test.ts": 1,
140 "test/crew-sandbox-escape.test.ts": 1,
141 "test/crew-sandbox-real.test.ts": 2,
142 "test/crew-sandbox.test.ts": 3,
143 "test/crew-switch.test.ts": 1,
144 "test/docs-link.test.ts": 1,
145 "test/error-groups.test.ts": 1,
146 "test/everyday.test.ts": 1,
147 "test/explore-unknown-words.test.ts": 1,
148 "test/explore.test.ts": 1,
149 "test/fleet-graph-href.test.ts": 1,
150 "test/flow-alarm-main-ahead.test.ts": 2,
151 "test/flow-card.test.ts": 1,
152 "test/following-cache.test.ts": 1,
153 "test/following.test.ts": 1,
154 "test/footing.test.ts": 1,
155 "test/forge-archive.test.ts": 1,
156 "test/forge-budget-verdict.test.ts": 1,
157 "test/fork-follow-history.test.ts": 1,
158 "test/fork-follow-word.test.ts": 1,
159 "test/fork-tree-mode.test.ts": 1,
160 "test/git-spawn-failure.test.ts": 1,
161 "test/guests.test.ts": 3,
162 "test/helpers/build-standin.ts": 2,
163 "test/helpers/stranger-builder.ts": 1,
164 "test/home-builders-idle.test.ts": 2,
165 "test/invite-contributors.test.ts": 1,
166 "test/keys.test.ts": 1,
167 "test/knobs.test.ts": 1,
168 "test/land-as-is.test.ts": 1,
169 "test/large-blobs.test.ts": 1,
170 "test/list-limit-shape.test.ts": 1,
171 "test/look-archive.test.ts": 1,
172 "test/machine-rc.test.ts": 2,
173 "test/machine-shell.test.ts": 2,
174 "test/maintenance-last-run.test.ts": 1,
175 "test/mcp-find-tool-queries.test.ts": 1,
176 "test/mcp-org-teams-waiting.test.ts": 1,
177 "test/mcp-releases-card.test.ts": 1,
178 "test/memory-watch.test.ts": 1,
179 "test/mesh-edit.test.ts": 1,
180 "test/migrate-origins.test.ts": 1,
181 "test/next-outsider-offer.test.ts": 1,
182 "test/no-polling.test.ts": 1,
183 "test/notify.test.ts": 1,
184 "test/now-links.test.ts": 1,
185 "test/openai-real-cell.test.ts": 3,
186 "test/order-cloud.test.ts": 1,
187 "test/orgs.test.ts": 1,
188 "test/origin-pull-state.test.ts": 1,
189 "test/origin-read-grant.test.ts": 3,
190 "test/outside-lane.test.ts": 1,
191 "test/packs-up.test.ts": 2,
192 "test/pay-again.test.ts": 1,
193 "test/payments-page.test.ts": 1,
194 "test/phase-profile.test.ts": 1,
195 "test/pinned-one-machine.test.ts": 1,
196 "test/planner-closes-pull.test.ts": 1,
197 "test/preview-looks.test.ts": 1,
198 "test/preview-spec.test.ts": 2,
199 "test/private-app.test.ts": 1,
200 "test/proposal-run.test.ts": 2,
201 "test/recovery-kit.test.ts": 3,
202 "test/release-boot.test.ts": 1,
203 "test/release-keeper-swap.test.ts": 1,
204 "test/release-waits-for-pull-checks.test.ts": 1,
205 "test/render-obj-ply.test.ts": 1,
206 "test/render-subtitles-shift.test.ts": 1,
207 "test/repo-growth-panel.test.ts": 1,
208 "test/repo-transfer.test.ts": 2,
209 "test/request-access-limits.test.ts": 1,
210 "test/resilience.test.ts": 1,
211 "test/review-failed-start.test.ts": 1,
212 "test/review-start-failure.test.ts": 1,
213 "test/run-secrets.test.ts": 1,
214 "test/search-is-palette.test.ts": 1,
215 "test/service-ports.test.ts": 1,
216 "test/settings-runners.test.ts": 1,
217 "test/shards-run.test.ts": 2,
218 "test/signup-password.test.ts": 1,
219 "test/small-machine-off.test.ts": 1,
220 "test/spawn-app-links.test.ts": 4,
221 "test/spawn-cell.test.ts": 1,
222 "test/spawn-self-use.test.ts": 1,
223 "test/starter-crew.test.ts": 1,
224 "test/step-header.test.ts": 1,
225 "test/system-feature-health.test.ts": 1,
226 "test/system-telemetry.test.ts": 1,
227 "test/talk-blocks.test.ts": 2,
228 "test/talk-conversations.test.ts": 1,
229 "test/talk-decisions.test.ts": 1,
230 "test/talk-display.test.ts": 1,
231 "test/talk-live.test.ts": 3,
232 "test/talk-page.test.ts": 1,
233 "test/talk-pane-bar.test.ts": 1,
234 "test/talk-steps.test.ts": 1,
235 "test/test-budget-run.test.ts": 2,
236 "test/tree-window.test.ts": 1,
237 "test/tree.test.ts": 1,
238 "test/tui-about-detach.test.ts": 1,
239 "test/tui-archive.test.ts": 1,
240 "test/tui-ask-data.test.ts": 1,
241 "test/tui-chapter-bar.test.ts": 1,
242 "test/tui-claim-start.test.ts": 1,
243 "test/tui-data.test.ts": 1,
244 "test/tui-flow-gates.test.ts": 1,
245 "test/tui-places.test.ts": 1,
246 "test/tui-transfer-repo.test.ts": 1,
247 "test/upstream-counts.test.ts": 1,
248 "test/upstream_follow.test.ts": 2,
249 "test/vitals.test.ts": 1,
250 "test/wake-budget.test.ts": 1,
251 "test/wave-pulls-cap.test.ts": 1,
252 "test/web.test.ts": 1,
253 "test/webhook-failing.test.ts": 1,
254 "test/week-sprint.test.ts": 1
255 }
scripts/dup-imports.ts added +104
@@ -0,0 +1,104 @@
1 /**
2 * Rule `duplicate-import`: one module is imported on one line (#11516).
3 *
4 * Reviewers asked five times in a week to merge two `import … from "x"` lines
5 * into one. This names them. A `import type` and a value `import` of the same
6 * module are two kinds and may stay apart; a namespace import (`* as`) and a
7 * bare side-effect import cannot merge, so they are not counted.
8 */
9
10 import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
11 import { join } from "node:path";
12
13 export const RULE = "duplicate-import";
14
15 const IMPORT = /^import\s+(type\s+)?([^;]*?)\s+from\s+["']([^"']+)["']/gms;
16
17 /** One line per module imported twice (or more) in the same kind, in `source`. */
18 export function duplicateImports(file: string, source: string): string[] {
19 const seen = new Map<string, number>();
20 const problems: string[] = [];
21 for (const m of source.matchAll(IMPORT)) {
22 const clause = m[2] ?? "";
23 if (clause.includes("* as")) continue;
24 const key = `${m[1] ? "type " : ""}${m[3]}`;
25 const line = source.slice(0, m.index).split("\n").length;
26 const first = seen.get(key);
27 if (first === undefined) seen.set(key, line);
28 else
29 problems.push(
30 `${file}:${line}: ${RULE}: "${m[3]}" is already imported on line ${first}; merge into one import`,
31 );
32 }
33 return problems;
34 }
35
36 function walk(dir: string, out: string[]): void {
37 for (const name of readdirSync(dir)) {
38 if (name === "node_modules" || name.startsWith(".")) continue;
39 const p = join(dir, name);
40 if (statSync(p).isDirectory()) walk(p, out);
41 else if (/\.(ts|tsx)$/.test(name)) out.push(p);
42 }
43 }
44
45 /** Every duplicate import in src/, scripts/ and test/ under `root`. */
46 export function repoDuplicateImports(root = "."): string[] {
47 const files: string[] = [];

A bare catch {} hides every error. Catch only ENOENT, so a read failure cannot let the lint pass with nothing scanned.

48 for (const d of ["src", "scripts", "test"]) {
49 try {
50 walk(join(root, d), files);
51 } catch (e) {
52 // A directory that does not exist is fine; anything else must not pass as "nothing to scan".
53 if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e;
54 }
55 }
56 return files
57 .sort()
58 .flatMap((f) => duplicateImports(f.replace(`${root}/`, ""), readFileSync(f, "utf8")));
59 }
60
61 /** Duplicates per file that predate the rule (`scripts/dup-imports-allow.json`). */
62 export function allowed(root = "."): Record<string, number> {
63 return JSON.parse(readFileSync(join(root, "scripts/dup-imports-allow.json"), "utf8"));
64 }
65
66 /** Count problems per file (the part of each line before the first `:`). */
67 export function counts(problems: readonly string[]): Record<string, number> {
68 const out: Record<string, number> = {};
69 for (const p of problems) {
outdated

The allowlist is by file name only, so any new duplicate in a listed file is accepted. Store a count per file and fail when it rises.

70 const f = p.split(":")[0] as string;
outdated

Failing when a listed file turns clean makes every cleanup pull edit the shared JSON and turns concurrent cleanups into conflicts. Warn here, or compare counts, and do not fail.

71 out[f] = (out[f] ?? 0) + 1;
72 }
73 return out;
74 }
75
76 /**
77 * What fails: a file with more duplicates than its recorded count (zero when
78 * unlisted). Fewer is never a failure; cleaning a file needs no other edit.
79 */
80 export function judge(problems: readonly string[], baseline: Readonly<Record<string, number>>): string[] {
81 const now = counts(problems);
82 const seen = new Map<string, number>();
83 return problems.filter((p) => {
84 const f = p.split(":")[0] as string;
85 const n = (seen.get(f) ?? 0) + 1;
86 seen.set(f, n);
87 // The first `baseline` duplicates of a file are the old ones; later ones are new.
88 return n > (baseline[f] ?? 0) && (now[f] ?? 0) > (baseline[f] ?? 0);
89 });
90 }
91
92 if (import.meta.main) {
93 const problems = repoDuplicateImports();
94 if (process.argv.includes("--write")) {
95 const c = counts(problems);
96 const sorted = Object.fromEntries(Object.keys(c).sort().map((k) => [k, c[k]]));
97 const lines = Object.entries(sorted).map(([k, v]) => ` ${JSON.stringify(k)}: ${v}`);
98 writeFileSync("scripts/dup-imports-allow.json", `{\n${lines.join(",\n")}\n}\n`);
99 process.exit(0);
100 }
101 const failed = judge(problems, allowed());
102 for (const p of failed) console.error(p);
103 process.exit(failed.length ? 1 : 0);
104 }
test/check-slots.test.ts +2 −4
@@ -9,15 +9,13 @@ import { tmpdir } from "node:os";
99 import { join } from "node:path";
1010 import { createApp, type App } from "../src/app.ts";
1111 import { setCheckSlots } from "../src/actions/check_slots.ts";
12 import { createRepo } from "../src/actions/repos.ts";
12 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1313 import { setRole } from "../src/actions/roles.ts";
1414 import { grantHold, holdLine, type SlotView } from "../src/checks/check_slots.ts";
15 import { getRepoById, type Repo } from "../src/core/repos.ts";
15 import { getRepoById, type Repo, writeCheckSlots } from "../src/core/repos.ts";
1616 import { setCapacity } from "../src/core/capacity.ts";
1717 import { createUser, type User } from "../src/core/users.ts";
1818 import { cancelRun, claimRun, createRun, getRun, listActive, type Run } from "../src/core/runs.ts";
19 import { commitToBranch } from "../src/actions/repos.ts";
20 import { writeCheckSlots } from "../src/core/repos.ts";
2119 import { startChecks, type ChecksHandle } from "../src/checks/service.ts";
2220 import { setQuota } from "../src/settings/quotas.ts";
2321 import { readRunLog } from "../src/runs/log.ts";
test/dup-imports.test.ts added +39
@@ -0,0 +1,39 @@
1 // @tree-scan: walks the repository for duplicate imports against its allow-list.
2 import { expect, test } from "bun:test";
3 import { allowed, duplicateImports, judge, repoDuplicateImports } from "../scripts/dup-imports.ts";
4
5 test("two imports of one module are named, with the rule and both lines", () => {
6 const src = `import { a } from "./x.ts";\nimport { b } from "./x.ts";\n`;
7 expect(duplicateImports("f.ts", src)).toEqual([
8 `f.ts:2: duplicate-import: "./x.ts" is already imported on line 1; merge into one import`,
9 ]);
10 });
11
12 test("multi-line imports count, and different modules do not", () => {
13 const src = `import {\n a,\n b,\n} from "./x.ts";\nimport { c } from "./y.ts";\nimport { d } from "./x.ts";\n`;
14 expect(duplicateImports("f.ts", src)).toHaveLength(1);
15 expect(duplicateImports("f.ts", `import { a } from "./x.ts";\nimport { c } from "./y.ts";\n`)).toEqual([]);
16 });
17
18 test("a type import beside a value import, namespaces and side effects are left alone", () => {
19 const src = `import type { A } from "./x.ts";\nimport { a } from "./x.ts";\nimport * as n from "./x.ts";\nimport "./x.ts";\n`;
20 expect(duplicateImports("f.ts", src)).toEqual([]);
21 });
22
23 test("a duplicate beyond a file's recorded count fails; fewer never does", () => {
24 const p = (n: number) => `a.ts:${n}: duplicate-import: "x" is already imported on line 1; merge into one import`;
25 expect(judge([p(2)], {})).toEqual([p(2)]);
26 expect(judge([p(2)], { "a.ts": 1 })).toEqual([]);
27 expect(judge([p(2), p(3)], { "a.ts": 1 })).toEqual([p(3)]);
28 expect(judge([], { "old.ts": 4 })).toEqual([]);
29 expect(judge([p(2)], { "a.ts": 3 })).toEqual([]);
30 });
31
32 test("a missing tree scans empty; any other read error throws", () => {
33 expect(repoDuplicateImports("/nonexistent-root")).toEqual([]);
34 expect(() => repoDuplicateImports("scripts/dup-imports.ts")).toThrow();
35 });
36
37 test("the repository has no duplicate import beyond the allowlist (rule duplicate-import)", () => {
38 expect(judge(repoDuplicateImports(), allowed())).toEqual([]);
39 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs