Open History to see the last commit
1// Unknown paths run every job; recognized packages pull in their dependents.
2// scripts/check-changed.ts shares these decisions so local checks match CI.
3const fs = require("node:fs");
4const path = require("node:path");
5
6const root = path.resolve(__dirname, "../..");
7
8const INERT = [
9 /^docs\//,
10 /^\.(agents|cursor|vscode|devcontainer|macroscope|repos)\//,
11 /^[^/]+\.md$/,
12 /^LICENSE$/,
13];
14
15// Shared test setup and vendored mobile dependencies affect every package.
16const RUNS_EVERYTHING = [
17 /^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv))\.ts$/,
18 /^apps\/mobile\/deps\//,
19];
20
21// Includes cross-package reads absent from package.json dependencies.
22const EXTRA_OWNERS = [
23 // Vite configs and tsconfigs of these apps import scripts/lib directly.
24 [/^scripts\/lib\//, ["@t3tools/desktop", "@t3tools/mobile", "@t3tools/web", "t3"]],
25 // scripts/build-desktop-artifact.ts reads the GNOME extension manifest.
26 [/^apps\/desktop\/gnome-extension\//, ["@t3tools/scripts"]],
27 [/^native\/browser-secret\//, ["@t3tools/desktop"]],
28 [/^native\/kde-snap-shot\//, ["@t3tools/desktop"]],
29 [/^native\/libghostty-vt\//, ["@t3tools/web"]],
30 // apps/server/src/cloud/ConnectReachability.test.ts checks the guide's headings.
31 [/^docs\/user\/t3-connect-troubleshooting\.md$/, ["t3"]],
32];
33const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
34
35// These tests read other packages without declaring dependencies.
36const TEST_ONLY_DEPS = {
37 // Evals import the server's source to test its real model-facing text.
38 "@t3tools/evals": ["t3"],
39 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
40 "@t3tools/scripts": [
41 "@t3tools/desktop",
42 "@t3tools/mobile",
43 "@t3tools/web",
44 "t3",
45 "@t3tools/provider-cursor",
46 ],
47};
48// The server bundles the web build without importing it.
49const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
50
51const MOBILE_NATIVE =
52 /^apps\/mobile\/.*\.(swift|kt|kts)$|^apps\/mobile\/(\.swiftlint\.yml|detekt\.yml|\.editorconfig|Brewfile)$|^scripts\/mobile-native-static-check\.ts$|^package\.json$|^\.github\/workflows\/ci\.yml$|^\.github\/scripts\/ci-changes\.cjs$|^\.github\/actions\/ci-changes\//;
53
54// Real-Electron check that agent preview tabs paint with the app window hidden.
55// Electron is pinned in the desktop manifest, so its upgrades rerun it too.
56const DESKTOP_PREVIEW =
57 /^apps\/desktop\/src\/preview\/|^apps\/desktop\/scripts\/verify-background-preview|^apps\/desktop\/package\.json$|^\.github\/workflows\/ci\.yml$|^\.github\/scripts\/ci-changes\.cjs$|^\.github\/actions\/ci-changes\//;
58
59// Startup, migration, settlement, and sidebar paths that decide whether an upgraded user's
60// threads show after launch. Build runs scripts/smoke-desktop-app.ts for them, so they do not
61// first fail in the release launch check.
62const DESKTOP_LAUNCH = [
63 /^apps\/desktop\/src\/((main|boot|compileCache|serverCompileCache)\.ts$|(app|backend|window)\/)/,
64 /^apps\/desktop\/src\/electron\/(ElectronApp|ElectronProtocol|ElectronWindow)\.ts$/,
65 /^apps\/web\/src\/((main|AppRoot|appInit)\.tsx|(bootstrap|router)\.ts)$/,
66 /^apps\/web\/src\/.*sidebar/i,
67 /^apps\/server\/src\/(bin|binMain|server|serverRuntimeStartup)\.ts$/,
68 /^apps\/server\/src\/persistence\/Migrations/,
69 /^apps\/server\/src\/orchestration-v2\/((ThreadSettlementService|ProjectionStore)\.ts$|legacy\/)/,
70 /^packages\/contracts\/src\/settings\.ts$/,
71 /^scripts\/(smoke-desktop-app\.ts|fixtures\/desktop-launch\/)/,
72];
73
74const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
75
76function readWorkspace() {
77 const patterns = [];
78 let inPackages = false;
79 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
80 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
81 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
82 if (entry) patterns.push(entry[1]);
83 }
84
85 const dirs = patterns.flatMap((pattern) => {
86 if (!pattern.endsWith("/*")) return [pattern];
87 const parent = pattern.slice(0, -2);
88 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
89 });
90
91 const packages = [];
92 for (const dir of dirs) {
93 const manifestPath = path.join(root, dir, "package.json");
94 if (!fs.existsSync(manifestPath)) continue;
95 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
96 const deps = {
97 ...manifest.dependencies,
98 ...manifest.devDependencies,
99 ...manifest.peerDependencies,
100 };
101 packages.push({
102 name: manifest.name,
103 dir,
104 hasTest: Boolean(manifest.scripts?.test),
105 deps: Object.keys(deps).filter(
106 (dep) =>
107 String(deps[dep]).startsWith("workspace:") &&
108 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
109 ),
110 });
111 }
112 return packages;
113}
114
115// [] means inert; null means unknown and requires every job.
116function ownersOf(file, packages) {
117 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
118 if (INERT.some((pattern) => pattern.test(file))) return extra;
119 // A manifest can change dependencies or versions other packages read.
120 if (path.posix.basename(file) === "package.json") return null;
121 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
122 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
123 if (RUST.test(file)) return ["@t3tools/scripts", ...extra];
124
125 const owner = packages
126 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
127 .sort((a, b) => b.dir.length - a.dir.length)[0];
128 if (owner) return [owner.name, ...extra];
129 return extra.length > 0 ? extra : null;
130}
131
132function decide(files, packages, full) {
133 const all = new Set(packages.map((pkg) => pkg.name));
134 let affected = new Set();
135 let everything = full || files === null;
136 let rust = false;
137
138 for (const file of files ?? []) {
139 if (RUST.test(file)) rust = true;
140 const owners = ownersOf(file, packages);
141 if (owners === null) {
142 everything = true;
143 console.error(`Runs everything: ${file}`);
144 } else {
145 for (const owner of owners) affected.add(owner);
146 }
147 }
148
149 // Release smoke reads scripts/ itself, not what the scripts tests read.
150 let scriptsChanged = everything;
151 if (everything) {
152 affected = all;
153 } else {
154 let grew = true;
155 while (grew) {
156 grew = false;
157 for (const pkg of packages) {
158 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
159 affected.add(pkg.name);
160 grew = true;
161 }
162 }
163 }
164 scriptsChanged = affected.has("@t3tools/scripts");
165 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
166 if (deps.some((dep) => affected.has(dep))) affected.add(name);
167 }
168 }
169
170 const tested = packages.filter(
171 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
172 );
173 const has = (name) => affected.has(name);
174 const desktopLaunch =
175 everything ||
176 files.some(
177 (file) => !/\.test\.tsx?$/.test(file) && DESKTOP_LAUNCH.some((pattern) => pattern.test(file)),
178 );
179
180 return {
181 affected: [...affected].sort(),
182 outputs: {
183 typecheck: affected.size > 0,
184 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web") || desktopLaunch,
185 desktop_launch: desktopLaunch,
186 test: tested.length > 0,
187 // Empty means every package the Test job covers.
188 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
189 test_web: has("@t3tools/web"),
190 test_server: has("t3"),
191 rust: everything || rust,
192 release_smoke: scriptsChanged,
193 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
194 desktop_preview: files === null || files.some((file) => DESKTOP_PREVIEW.test(file)),
195 },
196 };
197}
198
199function main() {
200 const args = process.argv.slice(2);
201 const pathsIndex = args.indexOf("--paths");
202 const files =
203 pathsIndex === -1
204 ? null
205 : fs
206 .readFileSync(args[pathsIndex + 1], "utf8")
207 .split("\n")
208 .filter(Boolean);
209 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
210
211 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
212 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
213}
214
215module.exports = { readWorkspace, decide };
216
217if (require.main === module) main();