Skip to content
Merged
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
12 changes: 11 additions & 1 deletion USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,14 @@ step.
the credentials still point to the same account, before it runs. The job keeps running in AWS;
fetch its result here later.
The branch review's card says what it diffs against, by the same rule as the CLI: the branch's
upstream, else the remote's default branch. It reads this from local git only. When the branch has
nothing new since that base, or there is no base at all, the card says so, disables **Start code
review** and offers **Review the whole repo instead** (the full review, with its own guards). It
also counts uncommitted changes, which no review includes: commit them first. If a branch review
still finds nothing, the output drawer shows a neutral **nothing to review** (no job, no upload)
with the same offer.
Before that dialog, a code review checks the repo's own results. If a review of the repo is still
running, it says so and offers **View it →** or **Start another anyway**. If HEAD already has a
finished review, it offers **Open those findings →** first, then **Review it again**. A job
Expand Down Expand Up @@ -1941,7 +1949,9 @@ prism-cli security findings --job <id> --wait # a specific job, waiting if i
that first run says so and makes no change.
3. Builds the scope. By default this is `git diff <base>...HEAD`, where the base is the branch's
upstream, else the remote's default branch, as in the codewatch pre-push review. Only committed
changes are included. `--full` takes `git archive HEAD` instead. An empty diff starts nothing.
changes are included. `--full` takes `git archive HEAD` instead. An empty diff starts nothing
and exits 0, saying `No changes against <base>; nothing to review. To review the whole repo at
HEAD, run with --full.`
4. Uploads to the scan bucket, skipping any upload whose object's ETag already matches. Both scopes
first refresh the code review's own source, `repo/<repo-slug>/latest.zip`, to `git archive
HEAD` and print `Review context: updated ... to HEAD <sha>`. A diff job reads that zip as the
Expand Down
4 changes: 2 additions & 2 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prism-d1/cli",
"version": "1.19.1",
"version": "1.19.2",
"description": "PRISM D1 Velocity CLI — AI development lifecycle assessment tool",
"author": "andklee",
"engines": {
Expand Down
19 changes: 15 additions & 4 deletions cli/src/dashboard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,13 @@ export interface ActionStatus {
cwd?: string;
output?: string;
exitCode?: number | null;
/** When done: ok (exit 0), pending (the action's pendingExit: the job is still running), else failed. */
outcome?: 'ok' | 'pending' | 'failed';
/**
* When done: ok (exit 0), pending (the action's pendingExit: the job is still running), nothing (a branch
* code review that exited 0 and saved no run: its diff was empty, so it started nothing), else failed.
*/
outcome?: 'ok' | 'pending' | 'nothing' | 'failed';
/** The repo's id (from the scan list) for a repo action, so the drawer can offer the next action there. */
repoId?: string | null;
timedOut?: boolean;
startedAt?: string;
finishedAt?: string | null;
Expand All @@ -248,6 +253,9 @@ export interface ActionStatus {
/** Actions that write a Security Agent result file in their repo. */
const RESULT_ACTIONS = new Set<ActionId>(['security-findings', 'security-threats', 'security-pentest-findings', 'security-code-review', 'security-code-review-full', 'security-threat-model', 'security-code-review-force', 'security-code-review-full-force']);

/** Branch code reviews: the only actions that can exit 0 having started nothing (no changes against the base). */
const BRANCH_REVIEW = new Set<ActionId>(['security-code-review', 'security-code-review-force']);

const MAX_OUTPUT = 256 * 1024;
const ALLOWED_KEYS = new Set(['action', 'repo', 'url', 'confirm', 'profile', 'region']);
/** A value the CLI accepts as a profile or region name (aws/connection.ts) that cannot be read as a flag. */
Expand Down Expand Up @@ -341,7 +349,7 @@ export class ActionRunner {
this.busy = true;
const runId = ++this.seq;
const st = this.cur = {
state: 'running' as const, runId, action: id, repo: repo?.name ?? null, command: displayCommand(args, def.program), cwd,
state: 'running' as const, runId, action: id, repo: repo?.name ?? null, repoId: repo?.id ?? null, command: displayCommand(args, def.program), cwd,
buf: '', exitCode: null, timedOut: false, startedAt: now().toISOString(), finishedAt: null,
} as ActionStatus & { buf: string };
const add = (chunk: Buffer | string) => {
Expand Down Expand Up @@ -410,7 +418,10 @@ export class ActionRunner {
process.stderr.write(`prism dashboard: could not read the run ${logSafe(st.action ?? '')} wrote in ${logSafe(wrote.name)}: ${redact((e as Error).message)}\n`);
}
}
st.outcome = code === 0 ? 'ok' : !st.timedOut && code === ACTIONS[st.action!].pendingExit ? 'pending' : 'failed';
// A started job always saves its pending record first, so exit 0 with no run saved is the empty-diff case.
// shortcut: a pending record that could not be written (a warning, exit 0) also reads as "nothing"; give the CLI its own exit code if that ever matters.
const nothing = code === 0 && !!wrote && BRANCH_REVIEW.has(st.action!) && !st.result;
st.outcome = nothing ? 'nothing' : code === 0 ? 'ok' : !st.timedOut && code === ACTIONS[st.action!].pendingExit ? 'pending' : 'failed';
st.state = 'done';
st.finishedAt = (this.d.now ?? (() => new Date()))().toISOString();
this.busy = false;
Expand Down
13 changes: 13 additions & 0 deletions cli/src/dashboard/security-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { readResultEntries } from '../codewatch/results.js';
import { loadSetup, type ResolvedSetup } from '../security/config.js';
import { run } from '../utils/exec.js';
import { listsBucket, PARAM_AGENT_SPACE, PARAM_SCAN_BUCKET } from '../security/setup.js';
import { branchScope, type BranchScope } from '../security/review.js';
import { ACTIONS, displayCommand, redact, type ActionId, type ApiResult, type ScanRepo } from './actions.js';
import { resolveAwsIdentity, type AwsIdentityView, type IdentityDeps } from './aws-identity.js';
import { tildeHome } from './payload.js';
Expand Down Expand Up @@ -412,6 +413,18 @@ export function securityAgentGate(d: Pick<SecurityAgentDeps, 'identity' | 'cache
return r ? { repo: r.id, gate: gateIn(d, r.path, r.name, true) } : null;
}

/**
* GET /api/security-agent/scope?repo=<id>: what a branch code review would
* review in one repo the last full answer named (security/review.ts
* branchScope, the CLI's own rule): its base, whether the diff is empty, and
* how many uncommitted changes it would leave out. Local git only, read-only,
* no AWS call. null when the id is not one of them.
*/
export function securityAgentScope(d: Pick<SecurityAgentDeps, 'identity' | 'cache'>, id: string | null): { repo: string; scope: BranchScope } | null {
const r = id && /^[a-f0-9]{16}$/.test(id) ? KNOWN.get(d)?.get(id) : undefined;
return r ? { repo: r.id, scope: branchScope((a) => run('git', a), r.path) } : null;
}

/** GET /api/security-agent: no AWS call. */
export async function securityAgentView(d: SecurityAgentDeps) {
const repos = await d.repos();
Expand Down
58 changes: 56 additions & 2 deletions cli/src/dashboard/security-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const SA_STEPS = ['AWS connected', 'Security Agent set up', 'Agent space verifie
const SA_POLL_STATES = ['connect', 'profile', 'setup', 'mismatch'];
const SA_POLL_MS = 10000;
let SA_TIMER = null;
/** What a branch review would review, per repo id (GET /api/security-agent/scope, local git): a scope, or null when unknown. */
const SA_SCOPE = new Map();
let SA_SCOPE_LOADING = null;
/** Repo|profile|region keys whose automatic lookup already ran on this page load: step 3 starts once each, never on a refresh. */
const SA_AUTO = new Set();
/** The repo id of the lookup in flight (one at a time), and the last refused request per repo (another action running, a network error). */
Expand Down Expand Up @@ -256,12 +259,62 @@ function saResultsCard(repo) {
el('p', { class: 'note' }, 'Every run, with its findings, is in Security runs above.'));
}
/** The whole-repo review, offered where a branch review has nothing to check (the card, the drawer). Same guards as its own Start. */
function saFullButton(repo) {
const a = actionDef('security-code-review-full');
if (!a || !a.available || SNAPSHOT) return null;
const b = el('button', { type: 'button', class: 'run danger', 'data-full-offer': '' }, 'Review the whole repo instead');
b.addEventListener('click', () => saStart(a, repo));
return b;
}
/** The drawer's offer after a branch review that had nothing to review: that repo, by id, once its setup check passes. */
function saFullOffer(repoId) {
const repo = ((SA && SA.repos) || []).find((r) => r.id === repoId);
return repo && repo.gate && repo.gate.state === 'ready' ? saFullButton(repo) : null;
}
/** Reads the branch review's scope for the picked repo once per full answer (local git only), then redraws. */
async function saScopeLoad(repo) {
if (SNAPSHOT || SA_SCOPE.has(repo.id) || SA_SCOPE_LOADING) return;
SA_SCOPE_LOADING = repo.id;
let r;
try { r = await api('/api/security-agent/scope?repo=' + repo.id); } catch (e) { r = { ok: false }; }
SA_SCOPE_LOADING = null;
SA_SCOPE.set(repo.id, r.ok && r.json && r.json.scope ? r.json.scope : null);
drawSecurity();
}
/**
* The branch review: what it diffs against (the CLI's rule: the upstream, else the remote's default branch),
* and, when that diff is empty or there is no base, a disabled Start with the whole-repo review instead.
* Unknown scope (still loading, or the check failed): the usual Start; the CLI decides.
*/
function saBranchItem(repo) {
const s = SA_SCOPE.get(repo.id) || null;
const why = ['Uploads the repo at HEAD and a patch of your branch to the scan bucket and starts a code review job. The first run in a repo also creates its code review, as the CI gate does.'];
let ctl = [saRun('security-code-review', repo, 'Start code review')], note = null;
if (s) {
const br = s.branch ? txt(s.branch) : 'this commit (no branch checked out)';
if (!s.base) {
note = 'No upstream for ' + br + ' and no default branch on the remote, so there is nothing to diff against. Push the branch with an upstream, or review the whole repo.';
} else {
why.push(s.base.fromUpstream ? 'It diffs against the upstream, ' + txt(s.base.upstream) + '.' : 'No upstream for ' + br + ': it diffs against ' + txt(s.base.upstream) + ', the remote\\'s default branch.');
if (s.empty) note = 'Nothing new on ' + br + ' since ' + txt(s.base.upstream) + '; a branch review would have nothing to check.';
}
why.push(s.dirty ? s.dirty + ' uncommitted change' + (s.dirty === 1 ? '' : 's') + ' here will not be reviewed: commit them first.' : 'Uncommitted changes are never reviewed.');
if (note) ctl = [el('button', { type: 'button', class: 'run danger', disabled: '', 'data-branch-start': '' }, 'Start code review'), saFullButton(repo)];
}
return el('li', { 'data-branch-review': s ? (note ? 'blocked' : 'ok') : 'unknown' }, el('div', { class: 'sa-head' }, el('b', {}, 'Code review of this branch')),
note ? el('div', { class: 'callout', role: 'note' }, note) : null,
el('div', { class: 'fix' }, why.join(' ')), el('div', { class: 'srow-ctl' }, ...ctl));
}
function saStartCard(repo) {
const c = repo.copy || {};
const item = (label, why, ctl) => el('li', {}, el('div', { class: 'sa-head' }, el('b', {}, label)), el('div', { class: 'fix' }, why), el('div', { class: 'srow-ctl' }, ctl));
return saCard('Start new work in ' + txt(repo.name), 'writes to AWS',
el('ul', { class: 'salist' },
item('Code review of this branch', 'Uploads the repo at HEAD and a patch of your branch to the scan bucket and starts a code review job. The first run in a repo also creates its code review, as the CI gate does.', saRun('security-code-review', repo, 'Start code review')),
saBranchItem(repo),
item('Code review of the whole repo', 'Uploads the repo at HEAD and starts a code review job.', saRun('security-code-review-full', repo, 'Start full review')),
item('Threat model', 'Uploads the repo and its scope docs at HEAD, creates a threat model and starts a job.', saRun('security-threat-model', repo, 'Start threat model')),
item('Pentest plan', 'Shows the plan, target and cost. Copy only: run it in a terminal.', copyOnly(txt(c.pentestDryRun), 'terminal')),
Expand Down Expand Up @@ -453,7 +506,7 @@ function saReadyStrip(repo) {
/** Fetches GET /api/security-agent (no AWS call), then draws the page and the nav badge. Runs on every load, whatever the view. */
async function renderSecurity() {
if (!SNAPSHOT) { try { const r = await api('/api/security-agent'); if (r.ok) SA = r.json; } catch (e) { /* keep the last good copy */ } }
if (!SNAPSHOT) { try { const r = await api('/api/security-agent'); if (r.ok) { SA = r.json; SA_SCOPE.clear(); } } catch (e) { /* keep the last good copy */ } }
SA_LOADED = true;
drawSecurity();
}
Expand Down Expand Up @@ -484,6 +537,7 @@ function drawSecurity() {
saAuto(repo);
return;
}
saScopeLoad(repo);
root.append(saReadyStrip(repo));
if (g.precedence) root.append(el('div', { class: 'callout', role: 'note' }, txt(g.precedence)));
for (const line of g.info || []) root.append(el('div', { class: 'callout', role: 'note' }, txt(line)));
Expand Down
8 changes: 7 additions & 1 deletion cli/src/dashboard/setup-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* POST /api/update-check asks the npm registry for the latest prism-cli now (one GET)
* GET /api/security-agent the Security Agent page's data (no AWS call; security-agent.ts)
* GET /api/security-agent/gate?repo=<id> the gate card's re-check for one repo (local files only, no git)
* GET /api/security-agent/scope?repo=<id> what a branch code review would review there (local git only)
* POST /api/security-agent/spaces {repo, refresh?}: the read-only agent-space lookup
* GET /api/security-run?repo=<id>&run=<id> one security run's findings (local result files; security-runs.ts)
* Account ids and ARNs appear only here and in the page, never in `dashboard --json`.
Expand All @@ -19,7 +20,7 @@ import { profileChoices, resolveAwsIdentity, type IdentityDeps } from './aws-ide
import { accountGuardView, setBaseline, type AccountGuardDeps } from './accountguard.js';
import { tildeHome } from './payload.js';
import type { VersionInfo } from './update-check.js';
import { securityAgentGate, securityAgentLookup, securityAgentView, type SecurityAgentDeps } from './security-agent.js';
import { securityAgentGate, securityAgentLookup, securityAgentScope, securityAgentView, type SecurityAgentDeps } from './security-agent.js';
import { securityRunDetail } from './security-runs.js';
import { ApiAnswer } from './server.js';

Expand Down Expand Up @@ -85,6 +86,11 @@ export function setupApi(d: SetupApiDeps) {
const g = d.securityAgent ? securityAgentGate(d.securityAgent, url.searchParams.get('repo')) : null;
return g ?? new ApiAnswer(404, { error: 'not a repo from the last Security Agent answer' });
},
// The branch review's scope in the picked repo: local git only (security/review.ts branchScope).
'/api/security-agent/scope': async (url: URL) => {
const s = d.securityAgent ? securityAgentScope(d.securityAgent, url.searchParams.get('repo')) : null;
return s ?? new ApiAnswer(404, { error: 'not a repo from the last Security Agent answer' });
},
'/api/accountguard': async () => (d.accountGuard ? accountGuardView(d.accountGuard) : null),
// Ids only: the repo id is matched against the scan list and the run id against that repo's result files.
// No such run answers 404 (the page shows Run not found and stops asking); any other failure is retried.
Expand Down
6 changes: 5 additions & 1 deletion cli/src/dashboard/setup-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ function showStatus(s) {
ACTION_PANEL.hidden = false;
// outcome pending: the Security Agent job has no verdict yet (exit 2). That is not a failure, so it is not red.
const pending = s.state === 'done' && s.outcome === 'pending';
const p = s.state === 'running' ? pill('warn', 'running') : s.exitCode === 0 ? pill('ok', 'exit 0')
// outcome nothing: a branch code review found no changes against its base and started nothing. Neither red nor green.
const nothing = s.state === 'done' && s.outcome === 'nothing';
const p = s.state === 'running' ? pill('warn', 'running') : nothing ? pill('info', 'nothing to review') : s.exitCode === 0 ? pill('ok', 'exit 0')
: pending ? pill('warn', 'still running (exit ' + s.exitCode + ')') : pill('error', s.timedOut ? 'timed out' : 'exit ' + s.exitCode);
// replaceChildren would print a null as text: only nodes go in.
OUT.status.replaceChildren(...[p, el('code', {}, txt(s.command)), el('span', { class: 'fix' }, (s.repo ? 'in ' + txt(s.repo) + ' · ' : '') + 'started ' + when(s.startedAt)),
pending ? el('span', { class: 'fix', 'data-pending-hint': '' }, 'The job is still running in AWS; nothing failed. Check back later, or run the same command with --wait.') : null,
nothing ? el('span', { class: 'fix', 'data-nothing-hint': '' }, 'No changes against the branch\\'s base, so no job was started and nothing was uploaded. Uncommitted changes are never reviewed.') : null,
nothing && typeof saFullOffer === 'function' ? saFullOffer(s.repoId) : null,
// A Security Agent action that saved a finished run: the server read its id from the result file.
s.state === 'done' && typeof runDoneBar === 'function' ? runDoneBar(s.result) : null].filter(Boolean));
OUT.pre.textContent = txt(s.output) || (s.state === 'running' ? '(no output yet)' : '(no output)');
Expand Down
Loading
Loading