Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
lint:
timeout-minutes: 8
runs-on: ubuntu-24.04
permissions:
contents: read
pull-requests: write

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — The whole lint job now gets pull-requests: write, though only the last comment step needs it

Every step in the job runs with a token that can write to PRs, including yarn install lifecycle scripts, nx tasks and PR-authored scripts, and so do push runs, which never comment. Only the comment step needs that scope. Fix: drop pull-requests: write from lint, upload quality-comment.md as an artifact, and post it from a small follow-up job (needs: lint, if: github.event_name == 'pull_request') that alone has pull-requests: write.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

steps:
- name: Checkout
uses: actions/checkout@v6
Expand Down Expand Up @@ -59,6 +62,7 @@ jobs:
run:
node --test scripts/ci/__tests__/ratchet.test.mjs
scripts/ci/__tests__/oxlint-warnings.test.mjs
scripts/ci/__tests__/quality-report.test.mjs
# Also unconditional, and against the PR's own tree. claude-code-review.yml
# runs this file too, but from a base-branch checkout, so a PR that breaks
# what it pins (the AGENTS.md heading every review footer links to) passes
Expand All @@ -69,6 +73,29 @@ jobs:
.github/scripts/code-review/__tests__/review-comments.test.mjs
- name: Escape-hatch ratchet
run: node scripts/ci/ratchet.mjs
# Advisory: the gates are the ratchet above and each package's
# --max-warnings. This only makes the totals visible on the run page.
- name: Code quality report

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — Quality report runs type-aware oxlint on every package, but upstream dist is only built for affected projects

The lint step uses nx affected, so on a PR that touches no package (docs or CI only) common-utils is never built. The report then lints all five packages type-aware with @hyperdx/common-utils/dist/* unresolved. The comment can post counts that don't match what ci:lint gates against each --max-warnings limit. Fix: build common-utils before this step, or report only the packages nx affected linted. Two more risks: it runs under if: always() after a failed install, and the 2-minute timeout covers five type-aware runs.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d342943: the report step builds common-utils first, and its timeout is 3 minutes. Left if: always(): a failed install makes the step fail, which continue-on-error absorbs and which skips the comment.

id: quality
if: always()
continue-on-error: true
timeout-minutes: 3
# `nx affected` above builds dist only for what the PR touches, and
# type-aware lint counts change when common-utils is unbuilt.
run: |
npx nx run @hyperdx/common-utils:ci:build
node scripts/ci/quality-report.mjs --comment "$RUNNER_TEMP/quality-comment.md" >> "$GITHUB_STEP_SUMMARY"
- name: Comment PR with code quality report
# Skip fork and Dependabot PRs: their GITHUB_TOKEN is read-only
if:
always() && steps.quality.outcome == 'success' && github.event_name ==
'pull_request' && github.event.pull_request.head.repo.fork != true &&
github.actor != 'dependabot[bot]'
continue-on-error: true
uses: mshick/add-pr-comment@ec328af66588ab8f77cdeb2c264f14aba45bbf59 # v3.12.0
with:
message-path: ${{ runner.temp }}/quality-comment.md
message-id: code-quality-report
# Also unconditional: the api's own `ci:lint` regenerates the spec but
# never asserts on it, and nx can skip or cache-replay that task.
- name: Check openapi.json is in sync with its sources
Expand Down
2 changes: 1 addition & 1 deletion agent_docs/code_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Code Organization

- **Single Responsibility**: One clear purpose per component/function
- **File Size**: Max 300 lines - refactor when approaching limit
- **File Size**: Max 300 lines - refactor when approaching limit. `yarn ratchet` fails when a file crosses it or an already-larger file grows
- **DRY**: Grep for an existing implementation before adding a type, schema,
helper, or component - see "Before you add a type, schema, or helper" below
(REQUIRED)
Expand Down
23 changes: 22 additions & 1 deletion oxlint/base.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// the top-level config, which is this file (via extends) when run from a
// package and the root one when run from the repo root.
"options": { "typeAware": true, "reportUnusedDisableDirectives": "warn" },
"plugins": ["eslint", "typescript"],
"plugins": ["eslint", "typescript", "import"],
"categories": {
"correctness": "off",
"suspicious": "off",
Expand Down Expand Up @@ -97,6 +97,17 @@
"reportVarsOnlyUsedAsTypes": true,
},
],
// Size and structure. Warnings, so each package's --max-warnings stops
// the count rising without blocking on what is already there.
"max-lines-per-function": [
"warn",
{ "max": 150, "skipBlankLines": true, "skipComments": true },
],
"complexity": ["warn", 20],
"max-depth": ["warn", 4],
"max-params": ["warn", 4],
"no-nested-ternary": "warn",
"import/no-cycle": "warn",
"no-restricted-imports": [
"error",
{
Expand All @@ -110,6 +121,16 @@
],
},
"overrides": [
{
// A describe() callback is one function holding the whole suite.
"files": [
"**/__tests__/**",
"**/*.{test,spec}.{ts,tsx}",
"**/tests/**",
"**/*.stories.tsx",
],
"rules": { "max-lines-per-function": "off" },
},
{
// typescript-eslint's `eslint-recommended` turns these off for TS files
// because tsc reports them, and turns no-var/prefer-* on. Plain JS files
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"dev-task": "DOTENV_CONFIG_PATH=.env.development nodemon --exec 'ts-node' --transpile-only -r tsconfig-paths/register -r dotenv-expand/config -r ./src/telemetry.ts ./src/tasks/index.ts",
"build": "rimraf ./build && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && cp -r ./src/opamp/proto ./build/opamp/",
"build:vercel": "rimraf ./build && tsc -p tsconfig.vercel.json && tsc-alias -p tsconfig.vercel.json && cp -r ./src/opamp/proto ./build/opamp/",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 302 && yarn run -T oxfmt --check 'src/**/*.ts' 'scripts/**/*.ts'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 437 && yarn run -T oxfmt --check 'src/**/*.ts' 'scripts/**/*.ts'",
"lint:fix": "yarn run -T oxlint --fix && yarn run -T oxfmt 'src/**/*.ts' 'scripts/**/*.ts'",
"ci:lint": "yarn lint && yarn tsc --noEmit && yarn lint:openapi",
"ci:unit": "jest --ci --coverage",
Expand Down
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:clickhouse": "NEXT_PUBLIC_IS_LOCAL_MODE=true NEXT_PUBLIC_CLICKHOUSE_BUILD=true next build --webpack && node scripts/prepare-clickhouse-build-export.js",
"run:clickhouse": "test -d out && npx rimraf tmp && mkdir tmp && cp -r out tmp/clickstack && echo 'visit http://localhost:3000/clickstack to start' && npx serve tmp -l 3000 || echo 'run build:clickhouse first'",
"start": "next start",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 569 && yarn run -T oxfmt --check '**/*.{ts,tsx,css,scss}'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 1161 && yarn run -T oxfmt --check '**/*.{ts,tsx,css,scss}'",
"lint:fix": "yarn run -T oxlint --fix && yarn run -T oxfmt '**/*.{ts,tsx,css,scss}'",
"lint:styles": "stylelint **/*/*.{css,scss}",
"ci:lint": "yarn lint && yarn tsc --noEmit && yarn lint:styles --quiet",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"compile:linux": "bun build src/cli.tsx --compile --target=bun-linux-x64 --outfile dist/hdx-linux-x64",
"compile:macos": "bun build src/cli.tsx --compile --target=bun-darwin-arm64 --outfile dist/hdx-darwin-arm64",
"compile:macos-x64": "bun build src/cli.tsx --compile --target=bun-darwin-x64 --outfile dist/hdx-darwin-x64",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 0 && yarn run -T oxfmt --check 'src/**/*.{ts,tsx}'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 74 && yarn run -T oxfmt --check 'src/**/*.{ts,tsx}'",
"ci:build": "tsup",
"ci:lint": "yarn lint && yarn tsc --noEmit",
"ci:unit": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand --ci --passWithNoTests",
Expand Down
2 changes: 1 addition & 1 deletion packages/common-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"build": "tsup && yarn build:types",
"build:types": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"ci:build": "yarn build",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 80 && yarn run -T oxfmt --check 'src/**/*.ts'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 233 && yarn run -T oxfmt --check 'src/**/*.ts'",
"lint:fix": "yarn run -T oxlint --fix && yarn run -T oxfmt 'src/**/*.ts'",
"ci:lint": "yarn lint && yarn tsc --noEmit",
"ci:unit": "jest --ci --coverage",
Expand Down
2 changes: 1 addition & 1 deletion packages/hdx-eval/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"scripts": {
"build": "rimraf ./dist && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"dev": "NODE_OPTIONS=--max-old-space-size=8192 ./scripts/env.sh tsx src/cli.ts",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 0 && yarn run -T oxfmt --check 'src/**/*.ts'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 44 && yarn run -T oxfmt --check 'src/**/*.ts'",
"lint:fix": "yarn run -T oxlint --fix && yarn run -T oxfmt 'src/**/*.ts'",
"ci:lint": "yarn lint && yarn tsc --noEmit",
"ci:unit": "jest --ci --coverage --forceExit",
Expand Down
41 changes: 41 additions & 0 deletions scripts/ci/__tests__/quality-report.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from 'node:assert/strict';
import test from 'node:test';

import { commentBody, lintSection, sizeSection } from '../quality-report.mjs';

test('file size section lists the largest files first', () => {
const out = sizeSection({ 'a.ts': 400, 'b.ts': 900 });
assert.match(out, /2 source files over 300 lines, 700 lines over/);
assert.ok(out.indexOf('b.ts') < out.indexOf('a.ts'));
});

test('lint section totals each rule across packages, most common first', () => {
const out = lintSection({
api: { limit: 5, rules: { complexity: 2, 'max-depth': 3 } },
app: { limit: 9, rules: { complexity: 4 } },
});
assert.match(out, /\| `complexity` \| 2 \| 4 \| 6 \|/);
assert.match(out, /\| `max-depth` \| 3 \| 0 \| 3 \|/);
assert.ok(out.indexOf('complexity') < out.indexOf('max-depth'));
assert.match(out, /api 5\/5, app 4\/9 \(count\/limit\)/);
assert.match(out, /\| \*\*Total\*\* \| 5 \| 4 \|/);
assert.match(out, /\| \*\*Limit\*\* \| 5 \| 9 \|/);
});

test('the PR comment is the totals and a link to the full report', () => {
const out = commentBody(
{ 'a.ts': 400, 'b.ts': 900 },
{ api: { limit: 5, rules: { complexity: 2, 'max-depth': 3 } } },
'https://example.test/runs/1',
);
assert.equal(
out,
[
'## Code quality report',
'- 2 source files over 300 lines, 700 lines over the limit in total',
'- oxlint warnings (count/limit): api 5/5',
'',
'[Full report](https://example.test/runs/1)',
].join('\n'),
);
});
101 changes: 100 additions & 1 deletion scripts/ci/__tests__/ratchet.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import { tmpdir } from 'node:os';
import path from 'node:path';
import test from 'node:test';

import { collectCounts, compare, discoverPackages } from '../ratchet.mjs';
import {
collectCounts,
collectLargeFiles,
compare,
compareSizes,
discoverPackages,
main,
nextSizeBaseline,
} from '../ratchet.mjs';

/** Builds a throwaway workspace: { pkgName: { 'src/a.ts': 'source' } }. */
function fixture(packages, workspaces = ['packages/*']) {
Expand Down Expand Up @@ -172,3 +180,94 @@ test('the committed baseline is not already breached by the real repo', () => {
// Only asserts not-above-baseline: below-baseline is a deliberate warn.
assert.equal(compare(collectCounts(repoRoot), baseline).failed, false);
});

test('collects only non-test source files over the line limit', () => {
const long = 'x;\n'.repeat(301);
const root = fixture({
app: {
'src/big.ts': long,
'src/ok.ts': 'x;\n'.repeat(300),
'src/__tests__/big.test.ts': long,
},
});
assert.deepEqual(collectLargeFiles(root), { 'packages/app/src/big.ts': 301 });
});

test('a new large file or a grown one fails; a shrunk one only warns', () => {
const baseline = { 'a.ts': 500, 'b.ts': 500 };
assert.equal(
compareSizes({ ...baseline, 'new.ts': 301 }, baseline).failed,
true,
);
assert.equal(
compareSizes({ 'a.ts': 501, 'b.ts': 500 }, baseline).failed,
true,
);
const shrunk = compareSizes({ 'a.ts': 400 }, baseline);
assert.equal(shrunk.failed, false);
assert.equal(shrunk.messages.length, 2);
});

test('a moved file points at ratchet:update instead of asking for a split', () => {
const { failed, messages } = compareSizes(
{ 'new/a.ts': 500 },
{ 'a.ts': 500 },
);
assert.equal(failed, true);
assert.match(messages[0], /moved or split from a\.ts/);
});

test('a file that shrank but still exists does not license a new large file', () => {
const baseline = { 'shrunk.ts': 1600 };
const current = { 'new.ts': 1500 };
const exists = file => file === 'shrunk.ts';
assert.match(
compareSizes(current, baseline, exists).messages[0],
/new\.ts: 1500 lines > 300/,
);
assert.deepEqual(nextSizeBaseline(current, baseline, { exists }), {
next: {},
refused: ['new.ts'],
});
});

test('a deleted file covers new files only up to its own line count', () => {
const baseline = { 'old.ts': 1000 };
const current = { 'part1.ts': 600, 'part2.ts': 400, 'extra.ts': 400 };
assert.deepEqual(nextSizeBaseline(current, baseline), {
next: { 'part1.ts': 600, 'part2.ts': 400 },
refused: ['extra.ts'],
});
});

test('update records shrinkage and moves but refuses growth unless allowed', () => {
const baseline = { 'a.ts': 500, 'b.ts': 500, 'old.ts': 900 };
const current = { 'a.ts': 400, 'b.ts': 600, 'moved.ts': 900, 'new.ts': 950 };
assert.deepEqual(nextSizeBaseline(current, baseline), {
next: { 'a.ts': 400, 'b.ts': 500, 'moved.ts': 900 },
refused: ['b.ts', 'new.ts'],
});
assert.deepEqual(nextSizeBaseline(current, baseline, { allowGrowth: true }), {
next: current,
refused: [],
});
});

test('--update exits 1 and leaves a grown file out unless --allow-growth', () => {
const root = fixture({ app: { 'src/big.ts': 'x;\n'.repeat(400) } });
mkdirSync(path.join(root, 'scripts/ci'), { recursive: true });
const sizePath = path.join(root, 'scripts/ci/file-size-baseline.json');
const read = file => JSON.parse(readFileSync(file, 'utf8'));

assert.equal(main(root, ['--update']), 1);
assert.deepEqual(read(sizePath), {});
assert.deepEqual(
read(path.join(root, 'scripts/ci/ratchet-baseline.json')),
collectCounts(root),
);
assert.equal(main(root, []), 1);

assert.equal(main(root, ['--update', '--allow-growth']), 0);
assert.deepEqual(read(sizePath), { 'packages/app/src/big.ts': 400 });
assert.equal(main(root, []), 0);
});
Loading
Loading