Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

chore(architecture): warn when oversized source files grow

vastsa4e4c40474cbf
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0eaac8e81..1c56952d0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -53,6 +53,9 @@ jobs:
ARCHITECTURE_BASE: ${{ github.event.pull_request.base.sha || github.event.before || 'HEAD^' }}
run: node scripts/check-architecture.mjs
+ - name: Unit-test the architecture checks
+ run: node --test scripts/architecture-growth.test.mjs
+
- name: Unit-test the Pi patch checks
run: node --test scripts/pi-patch-hash.test.mjs
diff --git a/AGENTS.md b/AGENTS.md
index d8393a19a..e1621c6b7 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -1,6 +1,6 @@
# AGENTS.md
-Policy-Sync: 2026-11-17.1
+Policy-Sync: 2026-11-24.1
Mandatory rules for AI coding agents working in PI-Desktop.
@@ -425,19 +425,23 @@ New work must not continuously increase architectural entropy.
Known hotspots — treat as **SHRINK OR STAY STABLE**:
```text
+packages/agent-runtime/src/runtime.ts
+crates/host-core/src/rpc/mod.rs
+crates/host-core/src/sessions.rs
+crates/host-core/src/tools/mod.rs
+apps/desktop/electron/main/plugin-runtime.ts
+apps/desktop/src/components/Sidebar.tsx
apps/desktop/electron/main/index.ts
apps/desktop/src/stores/app-store.ts
-apps/desktop/src/components/ChatTranscript.tsx
-apps/desktop/src/components/Composer.tsx
-crates/host-core/src/plugins.rs
-crates/host-core/src/db.rs
-crates/host-core/src/providers.rs
-crates/host-core/src/plans.rs
```
Do not use a historical God Module as the default place for new
functionality, and do not solve one God Module by creating another.
+`node scripts/check-architecture.mjs` flags any production TS / TSX
+file above 800 LOC or Rust file above 1000 LOC that grows relative to
+its base; see `docs/architecture/README.md`.
+
Split by real domain / responsibility / ownership / lifecycle, not
arbitrary line count. Guidance:
diff --git a/CLAUDE.md b/CLAUDE.md
index 8af386214..5404fa559 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1,6 +1,6 @@
# CLAUDE.md
-Policy-Sync: 2026-11-17.1
+Policy-Sync: 2026-11-24.1
Instructions for Claude Code CLI and Claude Cowork on PI-Desktop.
@@ -168,17 +168,17 @@ Database / schema changes need migration, schema version bump, upgrade compatibi
Do not pile new logic into God Modules. Prefer shrink-or-stay-stable for:
```text
+packages/agent-runtime/src/runtime.ts
+crates/host-core/src/rpc/mod.rs
+crates/host-core/src/sessions.rs
+crates/host-core/src/tools/mod.rs
+apps/desktop/electron/main/plugin-runtime.ts
+apps/desktop/src/components/Sidebar.tsx
apps/desktop/electron/main/index.ts
apps/desktop/src/stores/app-store.ts
-apps/desktop/src/components/ChatTranscript.tsx
-apps/desktop/src/components/Composer.tsx
-crates/host-core/src/plugins.rs
-crates/host-core/src/db.rs
-crates/host-core/src/providers.rs
-crates/host-core/src/plans.rs
```
-New logic belongs in the domain module that owns the state or process boundary. Facades stay for compatibility only.
+New logic belongs in the domain module that owns the state or process boundary. Facades stay for compatibility only. `node scripts/check-architecture.mjs` flags oversized files that grow (see `docs/architecture/README.md`).
Size guidance:
diff --git a/docs/adr/0235-domain-facades-and-architecture-budgets.md b/docs/adr/0235-domain-facades-and-architecture-budgets.md
index 70b023ca4..efaa6fabc 100644
--- a/docs/adr/0235-domain-facades-and-architecture-budgets.md
+++ b/docs/adr/0235-domain-facades-and-architecture-budgets.md
@@ -25,7 +25,8 @@ growing again.
test surface.
4. Enforce source-size budgets with `node scripts/check-architecture.mjs`, which
protects the facade limits, rejects newly oversized TypeScript files, and
- requires a documented reason for oversized Rust files.
+ requires a documented reason for oversized Rust files. It also warns when an
+ existing oversized production file grows relative to the comparison base.
5. Run the focused Biome lint, architecture checker, Rust formatting, and
workspace Clippy checks in CI alongside build, typecheck, and test.
diff --git a/docs/architecture/README.md b/docs/architecture/README.md
index def3c0c3a..e15cda757 100644
--- a/docs/architecture/README.md
+++ b/docs/architecture/README.md
@@ -15,6 +15,17 @@ The checker enforces these limits:
- Every Rust source file above 1000 LOC has a reasoned entry under rust in
allowlist.json.
+The checker also reports oversized-file growth. A production TypeScript or
+TSX file above 800 LOC, or a Rust file above 1000 LOC, may shrink or keep its
+size but should not grow past the size it had at the merge base with
+`ARCHITECTURE_BASE` (CI passes the pull-request base). Tests, fixtures, E2E
+sources, `.d.ts` files, i18n locales, and changelog catalogs are excluded, and
+files that did not exist at the base are covered by the new-file rules above.
+Growth is reported as a CI warning annotation and does not fail the check;
+setting `ARCHITECTURE_STRICT_GROWTH=1` turns it into a failure. A path listed
+with a reason under `growth` in allowlist.json is exempt. The rules live in
+scripts/architecture-growth.mjs with unit tests beside them.
+
The Rust entries currently document legacy modules that were outside the
follow-up split scope. New Rust domain files are expected to remain below the
limit. An allowlist entry records an explicit debt boundary; it does not raise
diff --git a/docs/spec/02-architecture/03-repo-structure.md b/docs/spec/02-architecture/03-repo-structure.md
index e917f5fd7..c8f3cbd1e 100644
--- a/docs/spec/02-architecture/03-repo-structure.md
+++ b/docs/spec/02-architecture/03-repo-structure.md
@@ -78,7 +78,8 @@ belongs in the domain module that owns its state or process boundary.
Source budgets are reported and enforced by
[`scripts/check-architecture.mjs`](../../architecture/README.md). Its
-allowlist records only existing extraction debt with a reason.
+allowlist records only existing extraction debt with a reason. The checker also
+warns when a production source file that is already above its size budget grows.
## 2. Package responsibilities
diff --git a/scripts/architecture-growth.mjs b/scripts/architecture-growth.mjs
new file mode 100644
index 000000000..929a0a6bb
--- /dev/null
+++ b/scripts/architecture-growth.mjs
@@ -0,0 +1,57 @@
+// Pure size-ratchet rules used by scripts/check-architecture.mjs.
+//
+// A production TypeScript or Rust source file above its size budget may stay
+// at its current size or shrink, but must not grow past the size it had at the
+// comparison base. Newly added files are governed by the new-file and Rust
+// allowlist rules in check-architecture.mjs instead.
+
+export const sizeBudgets = Object.freeze({
+ typescript: 800,
+ rust: 1_000,
+});
+
+const testSegments = new Set(["test", "tests", "__tests__", "e2e", "fixtures"]);
+
+export function isTestSourcePath(filePath) {
+ const segments = filePath.split("/");
+ const name = segments[segments.length - 1] ?? "";
+ if (segments.slice(0, -1).some((segment) => testSegments.has(segment))) return true;
+ if (/\.(test|spec)\.[cm]?[jt]sx?$/.test(name)) return true;
+ return name === "tests.rs" || name.endsWith("_tests.rs") || name.endsWith("_test.rs");
+}
+
+// Declarative data grows with product copy and release notes; it is exempt
+// from source-size budgets per AGENTS.md section 7.
+export function isDeclarativeDataPath(filePath) {
+ return (
+ filePath.startsWith("packages/i18n/src/locales/") ||
+ /^packages\/shared\/src\/changelog-[A-Za-z-]+\.ts$/.test(filePath)
+ );
+}
+
+export function sizeBudgetFor(filePath) {
+ if (filePath.endsWith(".d.ts")) return null;
+ if (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) return sizeBudgets.typescript;
+ if (filePath.endsWith(".rs")) return sizeBudgets.rust;
+ return null;
+}
+
+/**
+ * @param {object} input
+ * @param {Array<{ path: string, loc: number }>} input.metrics current source sizes
+ * @param {(path: string) => number | null} input.baseLocFor size at the base, or null when absent
+ * @param {ReadonlySet<string>} [input.exempt] extra paths exempt from the ratchet
+ * @returns {Array<{ path: string, loc: number, baseLoc: number, budget: number }>}
+ */
+export function findGrowthViolations({ metrics, baseLocFor, exempt = new Set() }) {
+ const violations = [];
+ for (const { path, loc } of metrics) {
+ const budget = sizeBudgetFor(path);
+ if (budget === null || loc <= budget) continue;
+ if (exempt.has(path) || isTestSourcePath(path) || isDeclarativeDataPath(path)) continue;
+ const baseLoc = baseLocFor(path);
+ if (baseLoc === null) continue;
+ if (loc > Math.max(baseLoc, budget)) violations.push({ path, loc, baseLoc, budget });
+ }
+ return violations;
+}
diff --git a/scripts/architecture-growth.test.mjs b/scripts/architecture-growth.test.mjs
new file mode 100644
index 000000000..2bc3d98e4
--- /dev/null
+++ b/scripts/architecture-growth.test.mjs
@@ -0,0 +1,99 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+
+import {
+ findGrowthViolations,
+ isDeclarativeDataPath,
+ isTestSourcePath,
+ sizeBudgetFor,
+} from "./architecture-growth.mjs";
+
+function check(metrics, baseSizes, exempt = []) {
+ return findGrowthViolations({
+ metrics,
+ baseLocFor: (path) => (path in baseSizes ? baseSizes[path] : null),
+ exempt: new Set(exempt),
+ });
+}
+
+test("an oversized file that grows past its base size is rejected", () => {
+ assert.deepEqual(
+ check([{ path: "packages/a/src/runtime.ts", loc: 1_205 }], {
+ "packages/a/src/runtime.ts": 1_200,
+ }),
+ [{ path: "packages/a/src/runtime.ts", loc: 1_205, baseLoc: 1_200, budget: 800 }],
+ );
+ assert.equal(
+ check([{ path: "crates/x/src/rpc/mod.rs", loc: 2_001 }], { "crates/x/src/rpc/mod.rs": 2_000 })
+ .length,
+ 1,
+ );
+});
+
+test("an oversized file may keep its size or shrink", () => {
+ assert.deepEqual(
+ check(
+ [
+ { path: "packages/a/src/same.ts", loc: 1_200 },
+ { path: "packages/a/src/smaller.tsx", loc: 900 },
+ { path: "crates/x/src/smaller.rs", loc: 1_500 },
+ ],
+ {
+ "packages/a/src/same.ts": 1_200,
+ "packages/a/src/smaller.tsx": 1_000,
+ "crates/x/src/smaller.rs": 1_800,
+ },
+ ),
+ [],
+ );
+});
+
+test("a file crossing its budget from below is rejected", () => {
+ assert.deepEqual(check([{ path: "apps/d/src/Panel.tsx", loc: 801 }], { "apps/d/src/Panel.tsx": 790 }), [
+ { path: "apps/d/src/Panel.tsx", loc: 801, baseLoc: 790, budget: 800 },
+ ]);
+});
+
+test("growth within the budget is allowed", () => {
+ assert.deepEqual(
+ check(
+ [
+ { path: "apps/d/src/Panel.tsx", loc: 800 },
+ { path: "crates/x/src/lib.rs", loc: 1_000 },
+ ],
+ { "apps/d/src/Panel.tsx": 100, "crates/x/src/lib.rs": 10 },
+ ),
+ [],
+ );
+});
+
+test("new files, tests, declarative data, exemptions, and unbudgeted files are skipped", () => {
+ const metrics = [
+ { path: "packages/a/src/runtime.test.ts", loc: 9_000 },
+ { path: "crates/x/src/db/tests.rs", loc: 3_000 },
+ { path: "crates/x/src/rpc/modes_rpc_tests.rs", loc: 3_000 },
+ { path: "scripts/e2e/composer-paste.tsx", loc: 2_000 },
+ { path: "packages/shared/src/changelog-en.ts", loc: 9_000 },
+ { path: "packages/i18n/src/locales/en/index.ts", loc: 9_000 },
+ { path: "packages/s/src/types.d.ts", loc: 9_000 },
+ { path: "scripts/big.mjs", loc: 9_000 },
+ { path: "apps/d/src/Allowed.ts", loc: 9_000 },
+ ];
+ const baseSizes = Object.fromEntries(metrics.map(({ path }) => [path, 1]));
+ metrics.push({ path: "apps/d/src/New.tsx", loc: 5_000 });
+ assert.deepEqual(check(metrics, baseSizes, ["apps/d/src/Allowed.ts"]), []);
+});
+
+test("path classification", () => {
+ assert.equal(isTestSourcePath("apps/desktop/test/foo.mjs"), true);
+ assert.equal(isTestSourcePath("packages/a/src/x.spec.tsx"), true);
+ assert.equal(isTestSourcePath("packages/a/src/contest.ts"), false);
+ assert.equal(isTestSourcePath("crates/host-core/src/tools/mod.rs"), false);
+ assert.equal(sizeBudgetFor("a/b.tsx"), 800);
+ assert.equal(sizeBudgetFor("a/b.rs"), 1_000);
+ assert.equal(sizeBudgetFor("a/b.d.ts"), null);
+ assert.equal(sizeBudgetFor("a/b.mjs"), null);
+ assert.equal(isDeclarativeDataPath("packages/shared/src/changelog-zh-CN.ts"), true);
+ assert.equal(isDeclarativeDataPath("packages/i18n/src/locales/ko/index.ts"), true);
+ assert.equal(isDeclarativeDataPath("apps/desktop/electron/main/models-dev-catalog.ts"), false);
+});
diff --git a/scripts/check-architecture.mjs b/scripts/check-architecture.mjs
index 32f18cbd1..9a9c3525b 100644
--- a/scripts/check-architecture.mjs
+++ b/scripts/check-architecture.mjs
@@ -4,6 +4,8 @@ import { execFileSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { extname, resolve } from "node:path";
+import { findGrowthViolations } from "./architecture-growth.mjs";
+
const root = process.cwd();
const sourceRoots = ["apps", "packages", "crates", "scripts"];
const sourceExtensions = new Set([".js", ".jsx", ".mjs", ".ts", ".tsx", ".rs"]);
@@ -120,6 +122,7 @@ function readAllowlist() {
return {
typescript: value.typescript || {},
rust: value.rust || {},
+ growth: value.growth || {},
};
}
@@ -134,6 +137,33 @@ function formatEntry(entry) {
return entry.path + " (" + entry.loc + " LOC)";
}
+function mergeBaseFor(base) {
+ if (!base) return null;
+ try {
+ return git(["merge-base", base, "HEAD"]);
+ } catch {
+ return null;
+ }
+}
+
+function baseLocReader(revision) {
+ if (!revision) return () => null;
+ const tracked = new Set(
+ git(["ls-tree", "-r", "--name-only", revision]).split("\n").filter(Boolean),
+ );
+ return (filePath) => {
+ if (!tracked.has(filePath)) return null;
+ const text = execFileSync("git", ["show", revision + ":" + filePath], {
+ cwd: root,
+ encoding: "utf8",
+ maxBuffer: 64 * 1024 * 1024,
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ const newlineCount = (text.match(/\n/g) || []).length;
+ return newlineCount + (text.length > 0 && !text.endsWith("\n") ? 1 : 0);
+ };
+}
+
const { base: requestedBase } = parseArgs();
const base = revisionExists(requestedBase) ? requestedBase : fallbackBase();
if (requestedBase && requestedBase !== base) {
@@ -199,6 +229,31 @@ for (const entry of largeNewTypeScript) {
}
}
+const growthBase = mergeBaseFor(base);
+const growthExempt = new Set(
+ Object.keys(allowlist.growth).filter((path) => allowlistReason(allowlist.growth, path)),
+);
+const growthViolations = findGrowthViolations({
+ metrics,
+ baseLocFor: baseLocReader(growthBase),
+ exempt: growthExempt,
+});
+// Oversized-file growth is reported, not blocking, unless strict mode is on.
+// Replaying recent merges showed a hard gate would reject about half of all
+// pull requests, including small bug fixes, so it starts as a visible warning.
+const strictGrowth = process.env.ARCHITECTURE_STRICT_GROWTH === "1";
+const growthMessages = growthViolations.map(
+ (entry) =>
+ formatEntry(entry) +
+ " grew from " +
+ entry.baseLoc +
+ " LOC while above the " +
+ entry.budget +
+ "-LOC budget; move new logic into a domain module or shrink the file" +
+ " (see docs/architecture/README.md)",
+);
+if (strictGrowth) failures.push(...growthMessages);
+
console.log("Architecture metrics");
console.log("Source files: " + metrics.length);
console.log("Total LOC: " + metrics.reduce((total, entry) => total + entry.loc, 0));
@@ -240,6 +295,15 @@ console.log(
" New TS/TSX files checked: " +
(base ? addedFiles.length : "skipped; no usable base"),
);
+console.log(
+ " Oversized-file growth checked against: " +
+ (growthBase ? growthBase.slice(0, 12) : "skipped; no usable base"),
+);
+console.log(
+ " Oversized files that grew: " +
+ (growthBase ? growthViolations.length : "n/a") +
+ (strictGrowth ? " (strict)" : " (warning only)"),
+);
if (largeNewTypeScript.length > 0) {
console.log("");
@@ -247,6 +311,19 @@ if (largeNewTypeScript.length > 0) {
for (const entry of largeNewTypeScript) console.log(" " + formatEntry(entry));
}
+if (!strictGrowth && growthViolations.length > 0) {
+ console.warn("");
+ console.warn("Oversized files grew (warning)");
+ for (const [index, message] of growthMessages.entries()) {
+ console.warn(" " + message);
+ if (process.env.GITHUB_ACTIONS === "true") {
+ console.log(
+ "::warning file=" + growthViolations[index].path + ",title=Oversized file grew::" + message,
+ );
+ }
+ }
+}
+
if (failures.length > 0) {
console.error("");
console.error("Architecture check failed");