megalens-review 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,9 +32,11 @@ npx megalens-review install # downloads the pinned Codex CLI and checks
32
32
  MEGALENS_TOKEN=... npx megalens-review run --tier standard --target range --base <base-sha> --head <head-sha> --out findings.json
33
33
  ```
34
34
 
35
+ Size limit: one review takes at most 250 changed files, 40,000 changed lines (added plus removed), 3 MiB of changed text, and 512 KiB for any one file, for Standard and Deep alike, and for pull request, range and uncommitted targets. It is measured on the exact diff that is sent to the model (after the engine's own exclusions: agent config and credential-shaped files), so lock files, build and vendor folders and binary files all count, and no `.gitattributes` setting can lower the count. A bigger change is refused (exit 3) before any gateway call or model run, with 0 credits; a review the Action already opened is closed as failed with reason `too_large`. Message: "This change is too large for one review (N files, about M lines). Split it into smaller pull requests, or review one folder." The numbers live in `src/limits.mjs`.
36
+
35
37
  Commands: `run`, `status <review-id>`, `result <review-id>`, `cancel <review-id>`, `install`, `verify`.
36
38
 
37
- Exit codes: `0` complete (findings may be empty), `2` partial, `3` refused by the gateway (no credits, limits, sign-in), `4` local error or failed review. A failed or partial review is never reported as a clean one.
39
+ Exit codes: `0` complete (findings may be empty), `2` partial, `3` refused (no credits, limits, sign-in, change too large), `4` local error or failed review. A failed or partial review is never reported as a clean one.
38
40
 
39
41
  ## Support
40
42
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "megalens-review",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "MegaLens review engine: runs a pinned code review on your pull request diff in a sandbox and writes the findings as JSON. Used by the MegaLens GitHub Action.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
package/src/cli.mjs CHANGED
@@ -17,7 +17,7 @@ megalens-review install # download and verify the pinned codex-cli (the onl
17
17
  megalens-review verify # re-check the installed codex-cli against PINNED.json
18
18
 
19
19
  Auth by environment: MEGALENS_TOKEN (a MegaLens token, or the review key the GitHub Action passes).
20
- Exit codes: 0 complete, 2 partial, 3 refused by the gateway, 4 local error or failed review.`
20
+ Exit codes: 0 complete, 2 partial, 3 refused (gateway, limits, change too large), 4 local error or failed review.`
21
21
 
22
22
  function emit(doc, out, io) {
23
23
  const text = JSON.stringify(doc, null, 2) + '\n'
@@ -128,7 +128,11 @@ export async function main(argv, { tiers, codexBin, pinned, modes, unsafeNoIsola
128
128
  return exit
129
129
  } catch (e) {
130
130
  if (e instanceof RefusedError) {
131
- io.stderr((e.code === 'mode_unavailable' || e.code === 'tier_unavailable') ? `${e.message}\n` : `refused by the MegaLens gateway: ${e.message}\n`)
131
+ if (e.code === 'too_large' && outPath) {
132
+ // refused:true so the Action treats it as a refusal; the message travels in notes.
133
+ try { writeFileSync(outPath, JSON.stringify(failureDoc({ reviewId: e.detail?.reviewId ?? undefined, tier: runTier, reason: 'too_large', refused: true, notes: [e.message], target: e.detail?.target ?? null }), null, 2) + '\n', { mode: 0o600 }) } catch { /* the exit code still says it */ }
134
+ }
135
+ io.stderr((e.code === 'mode_unavailable' || e.code === 'tier_unavailable' || e.code === 'too_large') ? `${e.message}\n` : `refused by the MegaLens gateway: ${e.message}\n`)
132
136
  return EXIT.REFUSED
133
137
  }
134
138
  if (e instanceof SandboxUnusableError) {
package/src/engine.mjs CHANGED
@@ -17,6 +17,7 @@ import { nestedSandboxWorks, resolveIsolation, wrapCommand } from './isolation.m
17
17
  import { judgeShortRun } from './short-run.mjs'
18
18
  import { BRIDGE_PORT, CHILD_PREFIX, startCredentialProxy } from './credential-proxy.mjs'
19
19
  import { DOUBLE_TIER_ENABLED, TIERS, ctxPrompt, isTier, planPasses } from './tiers.mjs'
20
+ import { measureDiff, overLimit, tooLargeMessage } from './limits.mjs'
20
21
  import { buildWorkspace, closeTarget, collectChanges, resolveTarget, snapshotHash } from './workspace.mjs'
21
22
 
22
23
  const round2 = (n) => Math.round(n * 100) / 100
@@ -80,14 +81,32 @@ export async function prepare({ tiers, codexBin, pinned, modes, unsafeNoIsolatio
80
81
  }
81
82
  }
82
83
  const target = resolveTarget({ cwd, kind: targetKind, base, head })
84
+ // Read the target once, build the workspace from that one read, and measure the diff that is actually sent. All local: nothing
85
+ // has been created at the gateway and no model has run, so a change that is too big ends here for 0 credits.
83
86
  let changes
84
87
  let snapshot
88
+ let ws
85
89
  try {
86
90
  changes = collectChanges(target)
87
91
  snapshot = snapshotHash(target, changes)
92
+ ws = buildWorkspace(target, changes)
88
93
  } catch (e) {
89
94
  closeTarget(target)
90
95
  throw e
96
+ } finally {
97
+ closeTarget(target) // the scratch repo has done its job
98
+ }
99
+ const size = measureDiff(ws.diffText)
100
+ if (overLimit(size)) {
101
+ ws.cleanup()
102
+ if (auth.mode === 'gateway' && auth.preReview) {
103
+ // The Action already opened a review for this run: close it as failed so it is charged 0 credits.
104
+ try { await createGatewayClient({ apiUrl: env.MEGALENS_API_URL || DEFAULT_API_URL, token: auth.token, fetchImpl, env }).complete(auth.preReview.id, 'failed', 'too_large') } catch { /* the server also closes on expiry */ }
105
+ }
106
+ throw new RefusedError(tooLargeMessage(size.files, size.lines), 'too_large', {
107
+ reviewId: auth.mode === 'gateway' && auth.preReview ? auth.preReview.id : null,
108
+ target: { kind: target.kind, base: target.base, head: target.head, snapshot_sha256: snapshot },
109
+ })
91
110
  }
92
111
  const plan = planPasses(tier, { hasFirstReview: !!firstReview })
93
112
  const jobTimeout = Number.isFinite(timeoutMin) && timeoutMin > 0 ? timeoutMin : TIERS[tier].timeoutMin
@@ -117,17 +136,11 @@ export async function prepare({ tiers, codexBin, pinned, modes, unsafeNoIsolatio
117
136
  reviewId = `rv_${randomBytes(9).toString('base64url')}`
118
137
  }
119
138
  } catch (e) {
120
- // A refused or failed create must not leave the scratch repository (it holds every changed file) in the temp dir.
121
- closeTarget(target)
139
+ // A refused or failed create must not leave the workspace (it holds every changed file) in the temp dir.
140
+ ws.cleanup()
122
141
  throw e
123
142
  }
124
143
 
125
- let ws
126
- try {
127
- ws = buildWorkspace(target, changes)
128
- } finally {
129
- closeTarget(target) // the scratch repo has done its job
130
- }
131
144
  const notes = []
132
145
  if (changes.excluded.length) notes.push(`${changes.excluded.length} changed file(s) were not sent for review (agent config or credential-shaped)`)
133
146
  if (changes.baseSkipped) notes.push(`${changes.baseSkipped} unchanged file(s) too large to copy were left out of the snapshot`)
@@ -381,13 +394,14 @@ async function executeInner({ job, reviewKey, auth, env, fetchImpl, signal }) {
381
394
  }
382
395
 
383
396
  /** The document for a run that could not start or was refused before any pass. Never `complete`. */
384
- export function failureDoc({ reviewId = 'rv_none', tier, reason, refused = false, notes = null }) {
397
+ export function failureDoc({ reviewId = 'rv_none', tier, reason, refused = false, notes = null, target = null }) {
385
398
  return {
386
399
  schema: SCHEMA,
387
400
  review_id: reviewId,
388
401
  tier,
389
402
  status: 'failed',
390
- target: { kind: 'uncommitted', base: null, head: null, snapshot_sha256: '0'.repeat(64) },
403
+ ...(reason === 'too_large' ? { reason } : {}),
404
+ target: target ?? { kind: 'uncommitted', base: null, head: null, snapshot_sha256: '0'.repeat(64) },
391
405
  passes: [{ name: refused ? 'gateway' : 'engine', model: '-', effort: '-', status: 'failed', minutes: 0, reason }],
392
406
  findings: [],
393
407
  dropped_by_quiet_mode: 0,
@@ -16,10 +16,11 @@ export function exitCodeForStatus(status, refused = false) {
16
16
 
17
17
  /** Thrown for any refusal by the gateway (auth, quota, spend cap, concurrency). */
18
18
  export class RefusedError extends Error {
19
- constructor(message, code = 'refused') {
19
+ constructor(message, code = 'refused', detail = null) {
20
20
  super(message)
21
21
  this.name = 'RefusedError'
22
22
  this.code = code
23
+ this.detail = detail // e.g. { reviewId, target } for a too_large refusal, so the findings document can name them
23
24
  }
24
25
  }
25
26
 
package/src/limits.mjs ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Size limits for one review. The ONE place they are set (README documents them).
3
+ * Measured 21 Nov 2026: 136-138 files / 22-30K lines worked (partial) on Standard; 263+ files / 42K+ lines (1.1-1.4M input tokens) failed.
4
+ * Deep runs the same limits: the code pins no larger context for Deep (tiers.mjs), so it gets no larger allowance.
5
+ *
6
+ * The count is taken from the diff that is SENT to the model (the review workspace's own diff, after the engine's real exclusions),
7
+ * never from a separate git listing, so no repo attribute (.gitattributes, linguist, -diff) can lower it.
8
+ *
9
+ * Bytes: a minified one-line file is 1 line but megabytes. 40,000 lines at the observed 50-80 bytes a line is 2-3 MB, and the
10
+ * failed runs were about 4+ MB of changed text, so the total cap is 3 MiB (above the measured working range, below the failing one).
11
+ * One file may not be more than 512 KiB of diff (a normal source file is far smaller; the engine copies up to 2 MiB per file).
12
+ */
13
+ export const MAX_CHANGED_FILES = 250
14
+ export const MAX_CHANGED_LINES = 40_000
15
+ export const MAX_CHANGED_BYTES = 3 * 1024 * 1024
16
+ export const MAX_FILE_DIFF_BYTES = 512 * 1024
17
+
18
+ /**
19
+ * Measure the unified diff the model is given. Every `diff --git` section is a file (binary files too); lines are the
20
+ * added and removed lines inside hunks; bytes are the diff text itself, in total and for the biggest single file.
21
+ * @returns {{files:number, lines:number, bytes:number, maxFileBytes:number}}
22
+ */
23
+ export function measureDiff(diffText) {
24
+ const text = String(diffText ?? '')
25
+ let files = 0
26
+ let lines = 0
27
+ let maxFileBytes = 0
28
+ let inHunk = false
29
+ let sectionBytes = 0
30
+ const close = () => { if (sectionBytes > maxFileBytes) maxFileBytes = sectionBytes }
31
+ let pos = 0
32
+ while (pos < text.length) {
33
+ let nl = text.indexOf('\n', pos)
34
+ if (nl < 0) nl = text.length
35
+ const len = nl - pos + 1
36
+ if (text.startsWith('diff --git ', pos)) {
37
+ close()
38
+ files += 1
39
+ sectionBytes = 0
40
+ inHunk = false
41
+ } else if (text.startsWith('@@', pos)) {
42
+ inHunk = true
43
+ } else if (inHunk) {
44
+ const c = text.charCodeAt(pos)
45
+ if (c === 43 || c === 45) lines += 1 // '+' or '-'
46
+ }
47
+ sectionBytes += Buffer.byteLength(text.slice(pos, nl)) + 1
48
+ pos += len
49
+ }
50
+ close()
51
+ return { files, lines, bytes: Buffer.byteLength(text), maxFileBytes }
52
+ }
53
+
54
+ /** True when a measured change is over any limit. */
55
+ export function overLimit(m) {
56
+ return m.files > MAX_CHANGED_FILES || m.lines > MAX_CHANGED_LINES || m.bytes > MAX_CHANGED_BYTES || m.maxFileBytes > MAX_FILE_DIFF_BYTES
57
+ }
58
+
59
+ export function tooLargeMessage(files, lines) {
60
+ return `This change is too large for one review (${files} files, about ${lines} lines). Split it into smaller pull requests, or review one folder.`
61
+ }