@imdeadpool/guardex 8.4.15 → 8.4.16
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/CONTRIBUTING.md +2 -33
- package/README.md +7 -174
- package/SECURITY.md +1 -1
- package/package.json +6 -39
- package/scripts/prepublish-bump-version.js +11 -1
- package/src/agents/start.js +1 -1
- package/src/cli/commands/branch.js +7 -7
- package/src/cli/commands/claude.js +6 -13
- package/src/cli/commands/pr.js +2 -2
- package/src/cli/commands/release.js +7 -1
- package/src/cli/commands/review.js +2 -2
- package/src/cli/commands/worktree-copy-ignored.js +1 -1
- package/src/cli/commands/worktree-estimate.js +2 -2
- package/src/cli/commands/worktree-hooks.js +1 -1
- package/src/cli/commands/worktree-navigation.js +3 -3
- package/src/cli/shared-locks.js +1 -1
- package/src/context.js +8 -1
- package/src/finish/index.js +1 -1
- package/src/finish/post-branch-finish-cleanup.js +2 -2
- package/src/finish/review-gate.js +3 -3
- package/src/finish/review-receipt.js +5 -5
- package/src/{github-api.js → github/api.js} +2 -2
- package/src/mcp/collect.js +2 -2
- package/src/pr-review.js +2 -1178
- package/src/pr-value.js +2 -111
- package/src/provider-binary.js +2 -122
- package/src/{pr.js → pull-request/index.js} +5 -5
- package/src/{pr-lens-publish.js → pull-request/lens-publish.js} +2 -2
- package/src/{pr-lens.js → pull-request/lens.js} +2 -2
- package/src/{pr-respond.js → pull-request/respond.js} +8 -8
- package/src/pull-request/value.js +111 -0
- package/src/{review-fix.js → review/fix.js} +2 -2
- package/src/review/provider-binary.js +122 -0
- package/src/review/pull-request.js +1178 -0
- package/src/{worktree-copy-ignored.js → worktree/copy-ignored.js} +3 -3
- package/src/{worktree-hook-worker.js → worktree/hook-worker.js} +1 -1
- package/src/{worktree-hooks.js → worktree/hooks.js} +5 -5
- package/src/{worktree-navigation.js → worktree/navigation.js} +2 -2
- package/src/{worktree-picker.js → worktree/picker.js} +1 -1
- package/src/{worktree-quota.js → worktree/quota.js} +1 -1
- package/src/{worktree-start.js → worktree/start.js} +4 -4
- package/src/{worktree-tasks.js → worktree/tasks.js} +6 -6
- /package/src/{shared-git-state.js → git/shared-state.js} +0 -0
- /package/src/{pr-checks.js → pull-request/checks.js} +0 -0
- /package/src/{review-diff-context.js → review/diff-context.js} +0 -0
- /package/src/{review-diff.js → review/diff.js} +0 -0
package/src/pr-value.js
CHANGED
|
@@ -1,111 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const AREAS = {
|
|
4
|
-
product: 'Product & user experience',
|
|
5
|
-
reliability: 'Reliability & security',
|
|
6
|
-
performance: 'Performance & cost',
|
|
7
|
-
maintenance: 'Maintainability & developer experience'
|
|
8
|
-
};
|
|
9
|
-
const IMPACTS = { benefit: 'Benefit', 'trade-off': 'Trade-off', neutral: 'Neutral' };
|
|
10
|
-
const WEIGHTS = { none: 0, limited: 5, notable: 10, material: 25, exceptional: 50 };
|
|
11
|
-
const BASELINE = 50;
|
|
12
|
-
|
|
13
|
-
const reviewValueInstructions = [
|
|
14
|
-
'Also add an advisory top-level "value" alongside "findings", even for clean diffs.',
|
|
15
|
-
'Estimate the net repository impact of merging this diff as written vs. not merging, not finding count or diff size. Do not return a numeric score; GitGuardex calculates it from a 50 neutral baseline.',
|
|
16
|
-
`"value":{"drivers":[{"area":"${Object.keys(AREAS).join('|')}","impact":"${Object.keys(IMPACTS).join('|')}","weight":"${Object.keys(WEIGHTS).join('|')}","reason":"what changed and who or what it affects","evidence":{"path":"new/file","line":12}}],"improvements":[]}`,
|
|
17
|
-
'Use none (0) only for neutral, limited (5) for a localized change, notable (10) for a meaningful effect on one workflow, material (25) for a substantial effect on an important or widespread workflow, exceptional (50) only for clear repository-wide impact. Use the same anchors for benefits and costs.',
|
|
18
|
-
'Give 1–3 distinct drivers. Do not split one outcome across areas or cherry-pick benefits while omitting a visible cost. Cite a real right-side line from a diff hunk for each driver. Return "value":null if the visible evidence cannot support the evaluation.',
|
|
19
|
-
'Use plain text, up to 200 characters per reason or improvement. Do not invent benchmark results, test passes, or adoption. Give at most two actionable improvements tied to the diff, highest impact first. Omit generic advice, repeated findings, and predicted point gains.'
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
function plainText(value) {
|
|
23
|
-
if (typeof value !== 'string') return '';
|
|
24
|
-
const clean = value.replace(/[\s\p{Cc}\p{Cf}]+/gu, ' ').trim();
|
|
25
|
-
return clean.length <= 200 ? clean : '';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function normalizeReviewValue(value, lineMap) {
|
|
29
|
-
const { drivers, improvements } = value ?? {};
|
|
30
|
-
if (!Array.isArray(drivers) || drivers.length < 1 || drivers.length > 3) return null;
|
|
31
|
-
const normalized = [];
|
|
32
|
-
const cited = new Set();
|
|
33
|
-
const reasons = new Set();
|
|
34
|
-
for (const driver of drivers) {
|
|
35
|
-
const { area, impact, weight, reason, evidence } = driver ?? {};
|
|
36
|
-
if (typeof area !== 'string' || !Object.hasOwn(AREAS, area)) return null;
|
|
37
|
-
if (typeof impact !== 'string' || !Object.hasOwn(IMPACTS, impact)) return null;
|
|
38
|
-
if (typeof weight !== 'string' || !Object.hasOwn(WEIGHTS, weight)) return null;
|
|
39
|
-
if ((impact === 'neutral') !== (weight === 'none')) return null;
|
|
40
|
-
const clean = plainText(reason);
|
|
41
|
-
const { path, line } = evidence ?? {};
|
|
42
|
-
if (
|
|
43
|
-
!clean ||
|
|
44
|
-
typeof path !== 'string' ||
|
|
45
|
-
!Number.isInteger(line) ||
|
|
46
|
-
line < 1 ||
|
|
47
|
-
!lineMap.get(path)?.has(line)
|
|
48
|
-
)
|
|
49
|
-
return null;
|
|
50
|
-
const key = JSON.stringify([impact, path, line]);
|
|
51
|
-
const reasonKey = clean.toLowerCase();
|
|
52
|
-
if (cited.has(key) || reasons.has(reasonKey)) return null;
|
|
53
|
-
cited.add(key);
|
|
54
|
-
reasons.add(reasonKey);
|
|
55
|
-
const points = impact === 'trade-off' ? -WEIGHTS[weight] : WEIGHTS[weight];
|
|
56
|
-
normalized.push({ area, impact, reason: clean, evidence: { path, line }, points });
|
|
57
|
-
}
|
|
58
|
-
const actions = new Set();
|
|
59
|
-
if (Array.isArray(improvements)) {
|
|
60
|
-
for (const improvement of improvements) {
|
|
61
|
-
const clean = plainText(improvement);
|
|
62
|
-
if (clean) actions.add(clean);
|
|
63
|
-
if (actions.size === 2) break;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const net = normalized.reduce((sum, driver) => sum + driver.points, BASELINE);
|
|
67
|
-
return {
|
|
68
|
-
score: Math.max(0, Math.min(100, net)),
|
|
69
|
-
drivers: normalized,
|
|
70
|
-
improvements: [...actions]
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function escapeValueText(text) {
|
|
75
|
-
// Model data is plain text, not Markdown, mentions, or hidden metadata.
|
|
76
|
-
return text.replace(/[&<>@\\[\]_*\x60~!|:.#$()+-]/g, (char) => `&#${char.charCodeAt(0)};`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function reviewValueLines(value, truncated, reviewDiffPath) {
|
|
80
|
-
if (truncated) return ['**PR value: unavailable** — diff is incomplete.'];
|
|
81
|
-
if (!value) return ['**PR value: unavailable** — no supported estimate for this diff.'];
|
|
82
|
-
return [
|
|
83
|
-
'### PR impact',
|
|
84
|
-
'',
|
|
85
|
-
`**PR value: ${value.score}/100** · Neutral baseline **${BASELINE}**`,
|
|
86
|
-
'',
|
|
87
|
-
'GitGuardex adds the estimated impact of each cited change to the baseline (bounded to 0–100). Diff-only estimate, not a merge verdict.',
|
|
88
|
-
'',
|
|
89
|
-
'| Impact | Points | Evidence and rationale |',
|
|
90
|
-
'| :--- | ---: | :--- |',
|
|
91
|
-
...value.drivers.map(({ area, impact, reason, evidence, points }) => {
|
|
92
|
-
const label = escapeValueText(`${evidence.path}:${evidence.line}`);
|
|
93
|
-
const hash = createHash('sha256').update(evidence.path).digest('hex');
|
|
94
|
-
const source = reviewDiffPath
|
|
95
|
-
? `[${label}](${reviewDiffPath}#diff-${hash}R${evidence.line})`
|
|
96
|
-
: label;
|
|
97
|
-
const pointsLabel = points > 0 ? `+${points}` : String(points);
|
|
98
|
-
return `| **${IMPACTS[impact]}** · ${AREAS[area]} | **${pointsLabel}** | ${escapeValueText(reason)} — ${source} |`;
|
|
99
|
-
}),
|
|
100
|
-
...(value.improvements.length
|
|
101
|
-
? [
|
|
102
|
-
'',
|
|
103
|
-
'**Suggested next steps**',
|
|
104
|
-
'',
|
|
105
|
-
...value.improvements.map((action, i) => `${i + 1}. ${escapeValueText(action)}`)
|
|
106
|
-
]
|
|
107
|
-
: [])
|
|
108
|
-
];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
module.exports = { normalizeReviewValue, reviewValueInstructions, reviewValueLines };
|
|
1
|
+
// Compatibility for the in-flight PR impact chart lane; implementation is here:
|
|
2
|
+
module.exports = require('./pull-request/value');
|
package/src/provider-binary.js
CHANGED
|
@@ -1,122 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const path = require('node:path');
|
|
4
|
-
|
|
5
|
-
const AGENTS = new Set(['claude', 'codex']);
|
|
6
|
-
const CODEX_REVIEW_EFFORTS = new Set(['low', 'medium', 'high', 'xhigh']);
|
|
7
|
-
const DEFAULT_CODEX_REVIEW_EFFORT = 'medium';
|
|
8
|
-
|
|
9
|
-
/** Keep bounded code-assist runs independent of a user's interactive effort. */
|
|
10
|
-
function codexReviewEffort(env = process.env) {
|
|
11
|
-
const requested = String(env.GUARDEX_REVIEW_CODEX_EFFORT || '').trim().toLowerCase();
|
|
12
|
-
return CODEX_REVIEW_EFFORTS.has(requested) ? requested : DEFAULT_CODEX_REVIEW_EFFORT;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function cueShimDir(env = process.env) {
|
|
16
|
-
const configHome = env.XDG_CONFIG_HOME && String(env.XDG_CONFIG_HOME).trim()
|
|
17
|
-
? String(env.XDG_CONFIG_HOME).trim()
|
|
18
|
-
: path.join(os.homedir(), '.config');
|
|
19
|
-
return path.resolve(configHome, 'cue', 'shims');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function isExecutableFile(candidate) {
|
|
23
|
-
try {
|
|
24
|
-
const stat = fs.statSync(candidate);
|
|
25
|
-
return stat.isFile() && (stat.mode & 0o111) !== 0;
|
|
26
|
-
} catch (_error) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function smallTextFile(candidate) {
|
|
32
|
-
try {
|
|
33
|
-
const stat = fs.statSync(candidate);
|
|
34
|
-
if (!stat.isFile() || stat.size >= 64_000) return '';
|
|
35
|
-
return fs.readFileSync(candidate, 'utf8');
|
|
36
|
-
} catch (_error) {
|
|
37
|
-
return '';
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function isCueAgentShim(content, provider) {
|
|
42
|
-
const text = String(content || '');
|
|
43
|
-
return text.includes('cue')
|
|
44
|
-
&& new RegExp(`\\blaunch\\s+${provider}\\b`).test(text);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isCodexGuard(content) {
|
|
48
|
-
const text = String(content || '');
|
|
49
|
-
return text.includes('codex-guard') && text.includes('find_real_codex');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function providerOverride(provider, env = process.env) {
|
|
53
|
-
const key = provider === 'claude' ? 'GUARDEX_REVIEW_CLAUDE_BIN' : 'GUARDEX_REVIEW_CODEX_BIN';
|
|
54
|
-
const explicit = String(env[key] || '').trim();
|
|
55
|
-
if (explicit) return explicit;
|
|
56
|
-
|
|
57
|
-
if (provider === 'claude') {
|
|
58
|
-
for (const fallbackKey of ['CUE_REAL_CLAUDE', 'CLAUDE_CODE_EXECPATH']) {
|
|
59
|
-
const candidate = String(env[fallbackKey] || '').trim();
|
|
60
|
-
if (candidate && isExecutableFile(candidate)) return candidate;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (provider === 'codex') {
|
|
65
|
-
const candidate = String(env.CUE_REAL_CODEX || '').trim();
|
|
66
|
-
if (candidate && isExecutableFile(candidate)) return candidate;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return '';
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function findRealProviderBin(provider, env = process.env) {
|
|
73
|
-
if (!AGENTS.has(provider)) return '';
|
|
74
|
-
const dirs = String(env.PATH || '').split(path.delimiter).filter(Boolean);
|
|
75
|
-
const shimDir = cueShimDir(env);
|
|
76
|
-
for (const dir of dirs) {
|
|
77
|
-
if (path.resolve(dir) === shimDir) continue;
|
|
78
|
-
const candidate = path.join(dir, provider);
|
|
79
|
-
if (!isExecutableFile(candidate)) continue;
|
|
80
|
-
const content = smallTextFile(candidate);
|
|
81
|
-
if (content && isCueAgentShim(content, provider)) continue;
|
|
82
|
-
if (provider === 'codex' && content && isCodexGuard(content)) continue;
|
|
83
|
-
return candidate;
|
|
84
|
-
}
|
|
85
|
-
return '';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function resolveProviderBin(provider, env = process.env) {
|
|
89
|
-
const normalized = String(provider || '').trim().toLowerCase();
|
|
90
|
-
const override = providerOverride(normalized, env);
|
|
91
|
-
if (override) return override;
|
|
92
|
-
return findRealProviderBin(normalized, env) || normalized;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function withProviderStderr(agentQuiet, execute) {
|
|
96
|
-
if (!agentQuiet) return execute('inherit');
|
|
97
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gitguardex-provider-'));
|
|
98
|
-
let fd;
|
|
99
|
-
try {
|
|
100
|
-
// Don't pipe stderr: verbose diagnostics must not consume the stdout
|
|
101
|
-
// buffer budget or cause spawnSync to terminate a successful provider.
|
|
102
|
-
fd = fs.openSync(path.join(dir, 'stderr'), 'wx+', 0o600);
|
|
103
|
-
const result = execute(fd);
|
|
104
|
-
if (result.status === 0) return result;
|
|
105
|
-
const size = fs.fstatSync(fd).size;
|
|
106
|
-
const tail = Buffer.alloc(Math.min(size, 2000));
|
|
107
|
-
const read = fs.readSync(fd, tail, 0, tail.length, size - tail.length);
|
|
108
|
-
return { ...result, stderr: tail.toString('utf8', 0, read) };
|
|
109
|
-
} finally {
|
|
110
|
-
if (fd !== undefined) fs.closeSync(fd);
|
|
111
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
module.exports = {
|
|
116
|
-
codexReviewEffort,
|
|
117
|
-
cueShimDir,
|
|
118
|
-
findRealProviderBin,
|
|
119
|
-
isCueAgentShim,
|
|
120
|
-
resolveProviderBin,
|
|
121
|
-
withProviderStderr,
|
|
122
|
-
};
|
|
1
|
+
// Compatibility for the in-flight PR impact chart lane.
|
|
2
|
+
module.exports = require('./review/provider-binary');
|
|
@@ -7,16 +7,16 @@
|
|
|
7
7
|
// these helpers expose the same primitives to the JS CLI so `gx pr ...` can be
|
|
8
8
|
// driven without invoking the shell flow.
|
|
9
9
|
|
|
10
|
-
const _ctx = require('
|
|
11
|
-
const { run } = require('
|
|
10
|
+
const _ctx = require('../context');
|
|
11
|
+
const { run } = require('../core/runtime');
|
|
12
12
|
const {
|
|
13
13
|
resolveRepoRoot,
|
|
14
14
|
currentBranchName,
|
|
15
15
|
hasOriginRemote,
|
|
16
16
|
detectDefaultBaseBranch,
|
|
17
|
-
} = require('
|
|
18
|
-
const { repoApiPath } = require('
|
|
19
|
-
const { FAILING_CONCLUSIONS, summarizeStatusCheckRollup } = require('./
|
|
17
|
+
} = require('../git');
|
|
18
|
+
const { repoApiPath } = require('../github/api');
|
|
19
|
+
const { FAILING_CONCLUSIONS, summarizeStatusCheckRollup } = require('./checks');
|
|
20
20
|
|
|
21
21
|
const BILLING_BLOCKED_JOB_MESSAGE = /^The job was not started because recent account payments have failed or your spending limit needs to be increased\. Please check the ['"]Billing & plans['"] section in your settings\.?$/i;
|
|
22
22
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const fs = require('node:fs');
|
|
2
2
|
const path = require('node:path');
|
|
3
|
-
const { GH_BIN } = require('
|
|
4
|
-
const { createRun, writeRunFile, completeRun } = require('
|
|
3
|
+
const { GH_BIN } = require('../context');
|
|
4
|
+
const { createRun, writeRunFile, completeRun } = require('../storage/run-artifacts');
|
|
5
5
|
|
|
6
6
|
// PR Lens 0.6.1 / graph schema 0.2.0. Keep inference in our existing tool-free
|
|
7
7
|
// reviewer; only validation and rendering run in the external CLI (no model API).
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
|
|
20
20
|
'use strict';
|
|
21
21
|
|
|
22
|
-
const { GH_BIN } = require('
|
|
23
|
-
const { run } = require('
|
|
24
|
-
const { repoNameWithOwner } = require('
|
|
25
|
-
const { worktreePathForBranch } = require('
|
|
26
|
-
const { gitRun, currentBranchName } = require('
|
|
27
|
-
const { runReviewFix } = require('
|
|
28
|
-
const { runPrReview, MARKER } = require('
|
|
29
|
-
const { getPrHeadOid } = require('./
|
|
22
|
+
const { GH_BIN } = require('../context');
|
|
23
|
+
const { run } = require('../core/runtime');
|
|
24
|
+
const { repoNameWithOwner } = require('../github/api');
|
|
25
|
+
const { worktreePathForBranch } = require('../agents/inspect');
|
|
26
|
+
const { gitRun, currentBranchName } = require('../git');
|
|
27
|
+
const { runReviewFix } = require('../review/fix');
|
|
28
|
+
const { runPrReview, MARKER } = require('../review/pull-request');
|
|
29
|
+
const { getPrHeadOid } = require('./index');
|
|
30
30
|
|
|
31
31
|
const TOOL_PREFIX = '[gitguardex]';
|
|
32
32
|
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const { createHash } = require('node:crypto');
|
|
2
|
+
|
|
3
|
+
const AREAS = {
|
|
4
|
+
product: 'Product & user experience',
|
|
5
|
+
reliability: 'Reliability & security',
|
|
6
|
+
performance: 'Performance & cost',
|
|
7
|
+
maintenance: 'Maintainability & developer experience'
|
|
8
|
+
};
|
|
9
|
+
const IMPACTS = { benefit: 'Benefit', 'trade-off': 'Trade-off', neutral: 'Neutral' };
|
|
10
|
+
const WEIGHTS = { none: 0, limited: 5, notable: 10, material: 25, exceptional: 50 };
|
|
11
|
+
const BASELINE = 50;
|
|
12
|
+
|
|
13
|
+
const reviewValueInstructions = [
|
|
14
|
+
'Also add an advisory top-level "value" alongside "findings", even for clean diffs.',
|
|
15
|
+
'Estimate the net repository impact of merging this diff as written vs. not merging, not finding count or diff size. Do not return a numeric score; GitGuardex calculates it from a 50 neutral baseline.',
|
|
16
|
+
`"value":{"drivers":[{"area":"${Object.keys(AREAS).join('|')}","impact":"${Object.keys(IMPACTS).join('|')}","weight":"${Object.keys(WEIGHTS).join('|')}","reason":"what changed and who or what it affects","evidence":{"path":"new/file","line":12}}],"improvements":[]}`,
|
|
17
|
+
'Use none (0) only for neutral, limited (5) for a localized change, notable (10) for a meaningful effect on one workflow, material (25) for a substantial effect on an important or widespread workflow, exceptional (50) only for clear repository-wide impact. Use the same anchors for benefits and costs.',
|
|
18
|
+
'Give 1–3 distinct drivers. Do not split one outcome across areas or cherry-pick benefits while omitting a visible cost. Cite a real right-side line from a diff hunk for each driver. Return "value":null if the visible evidence cannot support the evaluation.',
|
|
19
|
+
'Use plain text, up to 200 characters per reason or improvement. Do not invent benchmark results, test passes, or adoption. Give at most two actionable improvements tied to the diff, highest impact first. Omit generic advice, repeated findings, and predicted point gains.'
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
function plainText(value) {
|
|
23
|
+
if (typeof value !== 'string') return '';
|
|
24
|
+
const clean = value.replace(/[\s\p{Cc}\p{Cf}]+/gu, ' ').trim();
|
|
25
|
+
return clean.length <= 200 ? clean : '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function normalizeReviewValue(value, lineMap) {
|
|
29
|
+
const { drivers, improvements } = value ?? {};
|
|
30
|
+
if (!Array.isArray(drivers) || drivers.length < 1 || drivers.length > 3) return null;
|
|
31
|
+
const normalized = [];
|
|
32
|
+
const cited = new Set();
|
|
33
|
+
const reasons = new Set();
|
|
34
|
+
for (const driver of drivers) {
|
|
35
|
+
const { area, impact, weight, reason, evidence } = driver ?? {};
|
|
36
|
+
if (typeof area !== 'string' || !Object.hasOwn(AREAS, area)) return null;
|
|
37
|
+
if (typeof impact !== 'string' || !Object.hasOwn(IMPACTS, impact)) return null;
|
|
38
|
+
if (typeof weight !== 'string' || !Object.hasOwn(WEIGHTS, weight)) return null;
|
|
39
|
+
if ((impact === 'neutral') !== (weight === 'none')) return null;
|
|
40
|
+
const clean = plainText(reason);
|
|
41
|
+
const { path, line } = evidence ?? {};
|
|
42
|
+
if (
|
|
43
|
+
!clean ||
|
|
44
|
+
typeof path !== 'string' ||
|
|
45
|
+
!Number.isInteger(line) ||
|
|
46
|
+
line < 1 ||
|
|
47
|
+
!lineMap.get(path)?.has(line)
|
|
48
|
+
)
|
|
49
|
+
return null;
|
|
50
|
+
const key = JSON.stringify([impact, path, line]);
|
|
51
|
+
const reasonKey = clean.toLowerCase();
|
|
52
|
+
if (cited.has(key) || reasons.has(reasonKey)) return null;
|
|
53
|
+
cited.add(key);
|
|
54
|
+
reasons.add(reasonKey);
|
|
55
|
+
const points = impact === 'trade-off' ? -WEIGHTS[weight] : WEIGHTS[weight];
|
|
56
|
+
normalized.push({ area, impact, reason: clean, evidence: { path, line }, points });
|
|
57
|
+
}
|
|
58
|
+
const actions = new Set();
|
|
59
|
+
if (Array.isArray(improvements)) {
|
|
60
|
+
for (const improvement of improvements) {
|
|
61
|
+
const clean = plainText(improvement);
|
|
62
|
+
if (clean) actions.add(clean);
|
|
63
|
+
if (actions.size === 2) break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const net = normalized.reduce((sum, driver) => sum + driver.points, BASELINE);
|
|
67
|
+
return {
|
|
68
|
+
score: Math.max(0, Math.min(100, net)),
|
|
69
|
+
drivers: normalized,
|
|
70
|
+
improvements: [...actions]
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function escapeValueText(text) {
|
|
75
|
+
// Model data is plain text, not Markdown, mentions, or hidden metadata.
|
|
76
|
+
return text.replace(/[&<>@\\[\]_*\x60~!|:.#$()+-]/g, (char) => `&#${char.charCodeAt(0)};`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function reviewValueLines(value, truncated, reviewDiffPath) {
|
|
80
|
+
if (truncated) return ['**PR value: unavailable** — diff is incomplete.'];
|
|
81
|
+
if (!value) return ['**PR value: unavailable** — no supported estimate for this diff.'];
|
|
82
|
+
return [
|
|
83
|
+
'### PR impact',
|
|
84
|
+
'',
|
|
85
|
+
`**PR value: ${value.score}/100** · Neutral baseline **${BASELINE}**`,
|
|
86
|
+
'',
|
|
87
|
+
'GitGuardex adds the estimated impact of each cited change to the baseline (bounded to 0–100). Diff-only estimate, not a merge verdict.',
|
|
88
|
+
'',
|
|
89
|
+
'| Impact | Points | Evidence and rationale |',
|
|
90
|
+
'| :--- | ---: | :--- |',
|
|
91
|
+
...value.drivers.map(({ area, impact, reason, evidence, points }) => {
|
|
92
|
+
const label = escapeValueText(`${evidence.path}:${evidence.line}`);
|
|
93
|
+
const hash = createHash('sha256').update(evidence.path).digest('hex');
|
|
94
|
+
const source = reviewDiffPath
|
|
95
|
+
? `[${label}](${reviewDiffPath}#diff-${hash}R${evidence.line})`
|
|
96
|
+
: label;
|
|
97
|
+
const pointsLabel = points > 0 ? `+${points}` : String(points);
|
|
98
|
+
return `| **${IMPACTS[impact]}** · ${AREAS[area]} | **${pointsLabel}** | ${escapeValueText(reason)} — ${source} |`;
|
|
99
|
+
}),
|
|
100
|
+
...(value.improvements.length
|
|
101
|
+
? [
|
|
102
|
+
'',
|
|
103
|
+
'**Suggested next steps**',
|
|
104
|
+
'',
|
|
105
|
+
...value.improvements.map((action, i) => `${i + 1}. ${escapeValueText(action)}`)
|
|
106
|
+
]
|
|
107
|
+
: [])
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = { normalizeReviewValue, reviewValueInstructions, reviewValueLines };
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
// re-review with a fresh run — a model must not grade its own homework;
|
|
13
13
|
// - no push, no merge: the finish flow still owns those.
|
|
14
14
|
|
|
15
|
-
const { run } = require('
|
|
16
|
-
const { gitRun, currentBranchName, readProtectedBranches } = require('
|
|
15
|
+
const { run } = require('../core/runtime');
|
|
16
|
+
const { gitRun, currentBranchName, readProtectedBranches } = require('../git');
|
|
17
17
|
const { codexReviewEffort, resolveProviderBin, withProviderStderr } = require('./provider-binary');
|
|
18
18
|
|
|
19
19
|
const DEFAULT_FIX_TIMEOUT_MS = 15 * 60 * 1000;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
const os = require('node:os');
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
|
|
5
|
+
const AGENTS = new Set(['claude', 'codex']);
|
|
6
|
+
const CODEX_REVIEW_EFFORTS = new Set(['low', 'medium', 'high', 'xhigh']);
|
|
7
|
+
const DEFAULT_CODEX_REVIEW_EFFORT = 'medium';
|
|
8
|
+
|
|
9
|
+
/** Keep bounded code-assist runs independent of a user's interactive effort. */
|
|
10
|
+
function codexReviewEffort(env = process.env) {
|
|
11
|
+
const requested = String(env.GUARDEX_REVIEW_CODEX_EFFORT || '').trim().toLowerCase();
|
|
12
|
+
return CODEX_REVIEW_EFFORTS.has(requested) ? requested : DEFAULT_CODEX_REVIEW_EFFORT;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cueShimDir(env = process.env) {
|
|
16
|
+
const configHome = env.XDG_CONFIG_HOME && String(env.XDG_CONFIG_HOME).trim()
|
|
17
|
+
? String(env.XDG_CONFIG_HOME).trim()
|
|
18
|
+
: path.join(os.homedir(), '.config');
|
|
19
|
+
return path.resolve(configHome, 'cue', 'shims');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isExecutableFile(candidate) {
|
|
23
|
+
try {
|
|
24
|
+
const stat = fs.statSync(candidate);
|
|
25
|
+
return stat.isFile() && (stat.mode & 0o111) !== 0;
|
|
26
|
+
} catch (_error) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function smallTextFile(candidate) {
|
|
32
|
+
try {
|
|
33
|
+
const stat = fs.statSync(candidate);
|
|
34
|
+
if (!stat.isFile() || stat.size >= 64_000) return '';
|
|
35
|
+
return fs.readFileSync(candidate, 'utf8');
|
|
36
|
+
} catch (_error) {
|
|
37
|
+
return '';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isCueAgentShim(content, provider) {
|
|
42
|
+
const text = String(content || '');
|
|
43
|
+
return text.includes('cue')
|
|
44
|
+
&& new RegExp(`\\blaunch\\s+${provider}\\b`).test(text);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isCodexGuard(content) {
|
|
48
|
+
const text = String(content || '');
|
|
49
|
+
return text.includes('codex-guard') && text.includes('find_real_codex');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function providerOverride(provider, env = process.env) {
|
|
53
|
+
const key = provider === 'claude' ? 'GUARDEX_REVIEW_CLAUDE_BIN' : 'GUARDEX_REVIEW_CODEX_BIN';
|
|
54
|
+
const explicit = String(env[key] || '').trim();
|
|
55
|
+
if (explicit) return explicit;
|
|
56
|
+
|
|
57
|
+
if (provider === 'claude') {
|
|
58
|
+
for (const fallbackKey of ['CUE_REAL_CLAUDE', 'CLAUDE_CODE_EXECPATH']) {
|
|
59
|
+
const candidate = String(env[fallbackKey] || '').trim();
|
|
60
|
+
if (candidate && isExecutableFile(candidate)) return candidate;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (provider === 'codex') {
|
|
65
|
+
const candidate = String(env.CUE_REAL_CODEX || '').trim();
|
|
66
|
+
if (candidate && isExecutableFile(candidate)) return candidate;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function findRealProviderBin(provider, env = process.env) {
|
|
73
|
+
if (!AGENTS.has(provider)) return '';
|
|
74
|
+
const dirs = String(env.PATH || '').split(path.delimiter).filter(Boolean);
|
|
75
|
+
const shimDir = cueShimDir(env);
|
|
76
|
+
for (const dir of dirs) {
|
|
77
|
+
if (path.resolve(dir) === shimDir) continue;
|
|
78
|
+
const candidate = path.join(dir, provider);
|
|
79
|
+
if (!isExecutableFile(candidate)) continue;
|
|
80
|
+
const content = smallTextFile(candidate);
|
|
81
|
+
if (content && isCueAgentShim(content, provider)) continue;
|
|
82
|
+
if (provider === 'codex' && content && isCodexGuard(content)) continue;
|
|
83
|
+
return candidate;
|
|
84
|
+
}
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function resolveProviderBin(provider, env = process.env) {
|
|
89
|
+
const normalized = String(provider || '').trim().toLowerCase();
|
|
90
|
+
const override = providerOverride(normalized, env);
|
|
91
|
+
if (override) return override;
|
|
92
|
+
return findRealProviderBin(normalized, env) || normalized;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function withProviderStderr(agentQuiet, execute) {
|
|
96
|
+
if (!agentQuiet) return execute('inherit');
|
|
97
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gitguardex-provider-'));
|
|
98
|
+
let fd;
|
|
99
|
+
try {
|
|
100
|
+
// Don't pipe stderr: verbose diagnostics must not consume the stdout
|
|
101
|
+
// buffer budget or cause spawnSync to terminate a successful provider.
|
|
102
|
+
fd = fs.openSync(path.join(dir, 'stderr'), 'wx+', 0o600);
|
|
103
|
+
const result = execute(fd);
|
|
104
|
+
if (result.status === 0) return result;
|
|
105
|
+
const size = fs.fstatSync(fd).size;
|
|
106
|
+
const tail = Buffer.alloc(Math.min(size, 2000));
|
|
107
|
+
const read = fs.readSync(fd, tail, 0, tail.length, size - tail.length);
|
|
108
|
+
return { ...result, stderr: tail.toString('utf8', 0, read) };
|
|
109
|
+
} finally {
|
|
110
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
111
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {
|
|
116
|
+
codexReviewEffort,
|
|
117
|
+
cueShimDir,
|
|
118
|
+
findRealProviderBin,
|
|
119
|
+
isCueAgentShim,
|
|
120
|
+
resolveProviderBin,
|
|
121
|
+
withProviderStderr,
|
|
122
|
+
};
|