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
Comment thread
jordan-simonovski marked this conversation as resolved.
Resolved
id: quality
if: always()

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 — if: always() keeps the report running after the run is cancelled

The workflow sets concurrency.cancel-in-progress: true, so a new push cancels the old run. always() still runs on cancellation, so the cancelled run keeps rebuilding common-utils and linting every package for up to 3 minutes, holding the runner. The comment step at line 90 has the same problem. Fix: use !cancelled() in both conditions.

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

continue-on-error: true
timeout-minutes: 3

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 'advisory' quality report can still time out and fail the whole lint job

continue-on-error and the step's 3-minute timeout-minutes do not protect against the job's timeout-minutes: 8 (line 12). If the job runs past 8 minutes, GitHub fails the job, not just the step. This step rebuilds common-utils and then runs a second type-aware oxlint over all five packages, after ci:lint has already linted them (on push runs, every package via run-many). That can add up to 3 minutes to the job. If the limit is hit, the advisory report turns the lint job red, and on push runs it also skips Save nx computation cache (line 105). Fix: raise the job timeout by the step's budget, or move the report into its own job so it cannot affect the gating job.

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

# `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 @@ -107,7 +107,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 173 && yarn run -T oxfmt --check 'src/**/*.ts' 'scripts/**/*.ts'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 309 && 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 192 && yarn run -T oxfmt --check '**/*.{ts,tsx,css,scss}'",
"lint": "node ../../scripts/ci/oxlint-warnings.mjs --max-warnings 785 && 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
13 changes: 13 additions & 0 deletions scripts/ci/__tests__/oxlint-warnings.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import test from 'node:test';
import {
collectWarnings,
formatStylish,
intersectLines,
newWarnings,
parseAddedLines,
parseArgs,
Expand Down Expand Up @@ -188,3 +189,15 @@ test('formatStylish prints per-file problems and a total', () => {
assert.match(out, /✖ 4 problems \(1 error, 3 warnings\)\n$/);
assert.equal(formatStylish([], ROOT), '');
});

test('intersectLines keeps only lines new against both merge parents', () => {
const ours = new Map([
['/repo/a.ts', new Set([1, 2, 3])],
['/repo/from-main.ts', new Set([5])],
]);
const theirs = new Map([['/repo/a.ts', new Set([2, 9])]]);
assert.deepEqual(
intersectLines(ours, theirs),
new Map([['/repo/a.ts', new Set([2])]]),
);
});
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