Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Read-only model-abuse preflight for the pr-reviewer pipeline.
*
* The preflight reads public PR metadata and diffs through `gh`, then sends the
* complete title/description/diff of each external PR to the dedicated local
* model-abuse boundary. It never checks out or executes a contributor branch.
* Only generic, validated safety metadata crosses into the ordinary code-review
* stage; flagged content and classifier output do not.
*/
import { createHash } from 'node:crypto';
import { execGh, ensureForgeReachable } from './github.js';
import { getSelfLogin } from './prWatcher.js';
import { resolveForgeExecOptions } from './forgeExecOptions.js';
import { createGithubActorTrust } from './forgeActorTrust.js';
import { getOriginInfo } from '../lib/gitRemote.js';
import { githubApiHost, githubRepoSpec } from '../lib/workTracker.js';
import { mapWithConcurrency } from '../lib/mapWithConcurrency.js';
import {
MODEL_ABUSE_GUARD,
MODEL_ABUSE_GUARD_MAX_INPUT_CHARS,
LINKED_ISSUE_MAX_COUNT,
LINKED_ISSUE_STANDARD_BODY_MAX_CHARS,
linkedIssueIntentContent,
linkedIssueIntentFingerprint,
normalizeLinkedIssues,
} from '../lib/modelAbuseGuard.js';
// The screened surface and its fingerprint have ONE definition, because the
// coordinator recomputes the same value from a fresh forge read before it acts.
// Never rebuild either shape here.
import { screenedPullRequestContent, screenedPullRequestFingerprint } from '../lib/prReviewContent.js';
import { screenUntrustedContent } from './untrustedContent.js';
import { safeJSONParse } from '../lib/fileUtils.js';
export const SECURITY_SCAN_MAX_OPEN_PRS = 200;
export const SECURITY_SCAN_MAX_DIFF_CHARS = MODEL_ABUSE_GUARD_MAX_INPUT_CHARS;
export const SECURITY_SCAN_MAX_REPORT_CHARS = 100_000;
const MAX_LINKED_ISSUES = 50;
const failure = (code, extra = {}) => ({ ok: false, passed: false, code, ...extra });
const isHeadRefOid = (value) => typeof value === 'string' && /^[a-f0-9]{40}$/i.test(value);
const safeText = (value, max) => typeof value === 'string' ? value.slice(0, max) : '';
const ELIGIBILITY_LOOKUP_CONCURRENCY = 4;
function issueNumbersFromText(value, repoFullName) {
if (typeof value !== 'string' || !repoFullName) return [];
const repo = String(repoFullName).toLowerCase();
const numbers = new Set();
// GitHub's closing/reference syntax is the useful signal here. Limit
// repository-qualified references to this repository so a contributor's
// unrelated cross-repo issue cannot become an eligibility fact.
const referencePattern = /(?:\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?|relate[sd]?\s+to|ref(?:s)?|part\s+of)\s+)?(?:(?:[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\s*)?#(\d+)/gi;
let match;
while ((match = referencePattern.exec(value)) && numbers.size <= MAX_LINKED_ISSUES) {
const qualified = match[0].match(/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\s*#\d+$/i)?.[1];
if (qualified && qualified.toLowerCase() !== repo) continue;
const number = Number(match[1]);
if (Number.isInteger(number) && number > 0) numbers.add(number);
}
return [...numbers].sort((a, b) => a - b);
}
export function extractLinkedIssueNumbers(pr, repoFullName) {
return [...new Set([
...issueNumbersFromText(pr?.title, repoFullName),
...issueNumbersFromText(pr?.body, repoFullName),
])].sort((a, b) => a - b).slice(0, MAX_LINKED_ISSUES + 1);
}
/**
* Resolve the issue facts that admit a PR, plus the intent evidence a reviewer
* needs to answer "does this diff do what the issue asked?". Only OPEN linked
* issues contribute intent — a closed issue is not a live requirement, and the
* pre-action recheck recomputes the fingerprint from the same open set.
*/
async function resolveEligibilityFacts(pr, repoFullName, readIssue) {
const linkedIssueNumbers = extractLinkedIssueNumbers(pr, repoFullName);
if (linkedIssueNumbers.length === 0) {
return {
facts: {
linkedIssueNumbers: [],
openLinkedIssueNumbers: [],
openerAssignedIssueNumbers: [],
issueLookupComplete: true,
intentFingerprint: null,
},
linkedIssues: [],
inputComplete: true,
};
}
if (linkedIssueNumbers.length > MAX_LINKED_ISSUES) {
return { facts: { issueLookupComplete: false }, linkedIssues: [], inputComplete: false };
}
const openLinkedIssueNumbers = [];
const openerAssignedIssueNumbers = [];
const openIssues = [];
let issueLookupComplete = true;
for (const issueNumber of linkedIssueNumbers) {
const issue = await readIssue(issueNumber);
if (!issue || issue.number !== issueNumber) {
issueLookupComplete = false;
continue;
}
const isIssue = !issue.pull_request;
if (isIssue && String(issue.state).toLowerCase() === 'open') {
openLinkedIssueNumbers.push(issueNumber);
openIssues.push({ number: issueNumber, title: issue.title, body: issue.body });
const assignedLogins = Array.isArray(issue.assignees)
? issue.assignees.map((assignee) => String(assignee?.login || '').toLowerCase()).filter(Boolean)
: [];
if (assignedLogins.includes(String(pr.authorLogin).toLowerCase())) {
openerAssignedIssueNumbers.push(issueNumber);
}
}
}
const linkedIssues = normalizeLinkedIssues(openIssues);
return {
facts: {
linkedIssueNumbers,
openLinkedIssueNumbers,
openerAssignedIssueNumbers,
issueLookupComplete,
intentFingerprint: linkedIssueIntentFingerprint(linkedIssues),
},
linkedIssues,
inputComplete: openIssues.length <= LINKED_ISSUE_MAX_COUNT && !linkedIssues.some(issue => issue.truncated),
};
}
/**
* Assess the complete current contribution, then re-read after inference. Both
* automated merge queues use this boundary; the caller supplies forge readers
* so every read retains the owning app's account and host.
*
* stillCurrent performs no inference and can be called again after a review or
* another network action. A verdict never authorizes a changed head, mutable
* description, linked intent, or CI observation.
*/
export async function assessPullRequestForAction({ pr, diff, repoFullName, readPr, readDiff, readIssue }) {
const fingerprint = screenedPullRequestFingerprint(pr, diff);
if (!fingerprint || !isHeadRefOid(pr?.headRefOid) || typeof diff !== 'string' || !diff.trim()) {
return failure('security-action-evidence-unavailable');
}
const readIntent = (row) => resolveEligibilityFacts(
{ ...row, authorLogin: row.author?.login }, repoFullName, readIssue,
);
const intent = await readIntent(pr);
if (!intent.inputComplete || !intent.facts.issueLookupComplete) return failure('security-action-intent-unavailable');
const content = [screenedPullRequestContent(pr, diff), linkedIssueIntentContent(intent.linkedIssues)]
.filter(Boolean).join('\n\n');
if (content.length > SECURITY_SCAN_MAX_DIFF_CHARS) return failure('security-action-input-too-large');
const screened = await screenUntrustedContent({ content, source: 'github-pr' });
if (screened?.ok !== true || screened.screening?.safe !== true) {
// `code`/`message` on a screen failure are server-authored, never model prose.
return failure('security-action-assessment-withheld', {
...(screened?.code ? { cause: screened.code } : {}),
...(screened?.message ? { message: screened.message } : {}),
});
}
const actionState = (row) => JSON.stringify({
state: row.state, isDraft: row.isDraft, author: row.author?.login,
baseRefName: row.baseRefName, baseRefOid: row.baseRefOid,
mergeable: row.mergeable, mergeStateStatus: row.mergeStateStatus,
checks: row.statusCheckRollup,
});
const stillCurrent = async () => {
const [current, currentDiff] = await Promise.all([readPr(), readDiff()]);
if (!current || typeof currentDiff !== 'string'
|| screenedPullRequestFingerprint(current, currentDiff) !== fingerprint
|| actionState(current) !== actionState(pr)) return false;
const currentIntent = await readIntent(current);
return currentIntent.inputComplete && currentIntent.facts.issueLookupComplete
&& JSON.stringify(currentIntent.facts) === JSON.stringify(intent.facts);
};
if (!await stillCurrent()) return failure('security-action-evidence-changed');
return { ok: true, stillCurrent };
}
// These closures stay local to an intake; target/pipeline records never carry
// credential-bearing execution options, including when callers spread them.
const forgeReader = ({ cwd, env }) => args => execGh(args, undefined, { cwd, env });
const issueReader = (runGh, hostname, repoFullName) => async number => {
const raw = await runGh(['api', '--hostname', hostname, `repos/${repoFullName}/issues/${number}`]).catch(() => null);
return safeJSONParse(raw, null);
};
/**
* The three facts that decide which of a repo's open PRs pr-reviewer may target:
* the `gh` repo selector, the default branch it reviews against, and the login
* whose own PRs are excluded. Split out of `listExternalOpenPullRequests` so a
* caller that already HAS the PR rows — the PRs/MRs tab, deciding whether to
* offer its per-row Review action — can answer "is this row reviewable?" from
* the same source of truth instead of re-listing every PR or re-deriving the
* rule and drifting from it.
*/
export async function resolvePrReviewerTargetScope(app) {
const forgeExec = await resolveForgeExecOptions(app?.repoPath, { forgeAccount: app?.forgeAccount });
return readPrReviewerTargetScope(app, forgeExec);
}
async function readPrReviewerTargetScope(app, forgeExec) {
const runGh = forgeReader(forgeExec);
const origin = await getOriginInfo(app?.repoPath).catch(() => null);
const repoSpec = githubRepoSpec(origin);
if (!repoSpec) return failure('security-scan-not-a-github-repo');
const forge = await ensureForgeReachable('pr-reviewer security scan', {
hostname: githubApiHost(origin.host),
...(forgeExec.customEnv ? { env: forgeExec.customEnv } : {}),
});
if (!forge.ok) return failure('security-scan-forge-unreachable');
const defaultBranch = await runGh([
'repo', 'view', repoSpec, '--json', 'defaultBranchRef', '-q', '.defaultBranchRef.name',
]).catch(() => null);
if (!defaultBranch?.trim()) return failure('security-scan-default-branch-unresolved');
const selfLogin = await getSelfLogin(githubApiHost(origin.host), { cwd: forgeExec.cwd, env: forgeExec.env });
if (!selfLogin) return failure('security-scan-self-login-unavailable');
return {
ok: true,
repoSpec,
repoFullName: origin.fullName,
hostname: githubApiHost(origin.host),
defaultBranch: defaultBranch.trim(),
selfLogin,
actorTrust: await createGithubActorTrust({ runGh, host: githubApiHost(origin.host), repoFullName: origin.fullName, currentUser: selfLogin }),
};
}
/**
* Whether one already-listed PR row is a pr-reviewer target, per a resolved
* scope. Mirrors exactly what `listExternalOpenPullRequests` keeps — open
* against the default branch, opened by someone other than the signed-in
* account — so a UI gated on this and the route's authoritative check agree.
*/
export async function isReviewablePullRequest(scope, pullRequest) {
const author = pullRequest?.author;
return scope?.ok === true
&& pullRequest?.baseBranch === scope.defaultBranch
&& typeof author === 'string'
&& author.length > 0
&& author.toLowerCase() !== String(scope.selfLogin).toLowerCase()
&& !(await scope.actorTrust?.isTrusted(author));
}
export async function listExternalOpenPullRequests(app) {
const forgeExec = await resolveForgeExecOptions(app?.repoPath, { forgeAccount: app?.forgeAccount });
return readExternalOpenPullRequests(app, forgeExec);
}
async function readExternalOpenPullRequests(app, forgeExec) {
const runGh = forgeReader(forgeExec);
const scope = await readPrReviewerTargetScope(app, forgeExec);
if (!scope.ok) return scope;
const { repoSpec, repoFullName, hostname, defaultBranch, actorTrust } = scope;
const raw = await runGh([
'pr', 'list', '--repo', repoSpec,
'--base', defaultBranch, '--state', 'open',
'--limit', String(SECURITY_SCAN_MAX_OPEN_PRS),
'--json', 'number,author,url,headRefOid,updatedAt,title,body',
]).catch(() => null);
if (raw === null) return failure('security-scan-pr-list-failed');
const parsed = safeJSONParse(raw, null);
if (!Array.isArray(parsed)) return failure('security-scan-pr-list-unreadable');
if (parsed.length >= SECURITY_SCAN_MAX_OPEN_PRS) return failure('security-scan-too-many-open-prs');
const listedPrs = parsed.map((pr) => ({
number: pr?.number,
authorLogin: pr?.author?.login,
headRefOid: isHeadRefOid(pr?.headRefOid) ? pr.headRefOid : null,
updatedAt: pr?.updatedAt || null,
url: typeof pr?.url === 'string' ? pr.url : '',
title: typeof pr?.title === 'string' ? pr.title : null,
body: typeof pr?.body === 'string' ? pr.body : '',
}));
if (listedPrs.some((pr) => (
!Number.isInteger(pr.number)
|| pr.number < 1
|| typeof pr.authorLogin !== 'string'
|| !pr.authorLogin
|| typeof pr.title !== 'string'
))) {
return failure('security-scan-pr-list-unreadable');
}
const trusted = await mapWithConcurrency(listedPrs, ELIGIBILITY_LOOKUP_CONCURRENCY, pr => actorTrust.isTrusted(pr.authorLogin));
const externalPrs = listedPrs.filter((_, index) => !trusted[index]);
const readIssue = issueReader(runGh, hostname, repoFullName);
const prs = await mapWithConcurrency(externalPrs, ELIGIBILITY_LOOKUP_CONCURRENCY, async (pr) => {
const { facts, linkedIssues, inputComplete } = await resolveEligibilityFacts(pr, repoFullName, readIssue);
return { ...pr, eligibilityFacts: facts, linkedIssues, inputComplete };
});
return {
ok: true,
repoSpec,
repoFullName,
defaultBranch: defaultBranch.trim(),
prs,
};
}
/**
* Return a stable identity for the public PR set that was scanned. A new head
* commit produces a new identity, while an unresolved result for the same set
* does not cause a scheduler to assume that content was safely screened.
*/
export function securityScanFingerprint(target) {
if (!target?.ok || !Array.isArray(target.prs)) return null;
if (target.prs.some((pr) => !Number.isInteger(pr?.number) || !isHeadRefOid(pr.headRefOid))) return null;
const identity = {
repoFullName: target.repoFullName || null,
defaultBranch: target.defaultBranch || null,
prs: target.prs
.map((pr) => ({ number: pr.number, headRefOid: pr.headRefOid }))
.sort((a, b) => a.number - b.number),
eligibilityFacts: target.prs
.map((pr) => ({ number: pr.number, facts: pr.eligibilityFacts || null }))
.sort((a, b) => a.number - b.number),
};
return createHash('sha256').update(JSON.stringify(identity)).digest('hex');
}
const reportChars = (reports) => JSON.stringify(reports).length;
const formatSecurityFindings = (findings) => findings.map((finding) => (
`${finding.severity} — ${finding.location}: ${finding.reason}`
)).join('\n');
const structuralVerdict = (findings) => ({
ok: true,
safe: false,
findings: findings.map((finding) => ({
severity: 'blocking',
category: finding.category,
location: 'external-content',
reason: `${finding.path}:${finding.line} — ${finding.detail}`,
})),
model: 'Deterministic hidden-content checks (structural diff gate)',
layers: { deterministic: 'blocked', classifier: 'not-run', verdict: 'validated' },
});
const reportFor = (pr, diff, commits, verdict) => ({
number: pr.number,
url: pr.url,
headRefOid: pr.headRefOid,
contentFingerprint: screenedPullRequestFingerprint(pr, diff, commits),
updatedAt: pr.updatedAt,
passed: verdict.safe === true,
safe: verdict.safe === true,
findings: verdict.safe === true ? 'No model-abuse findings.' : formatSecurityFindings(verdict.findings || []),
securityFindings: Array.isArray(verdict.findings) ? verdict.findings : [],
guardId: verdict.guardId || MODEL_ABUSE_GUARD.id,
guardModel: verdict.model || MODEL_ABUSE_GUARD.name,
guardRevision: verdict.revision ?? null,
layers: verdict.layers || null,
chunkCount: Number.isInteger(verdict.chunkCount) ? verdict.chunkCount : null,
minBenignScore: Number.isFinite(verdict.minBenignScore) ? verdict.minBenignScore : null,
});
/**
* Commit messages are screened content, but they cannot ride the bulk listing:
* GitHub prices `commits` on `gh pr list` at the query's LIMIT rather than its
* hit count, so at the 200-PR cap it trips the 500,000 potential-node ceiling
* and the whole listing is refused. Read the log per PR, in the scan.
*/
async function fetchPullRequestCommits(number, repoSpec, runGh) {
const raw = await runGh([
'pr', 'view', String(number), '--repo', repoSpec, '--json', 'commits',
]).catch(() => null);
const parsed = safeJSONParse(raw, null);
return Array.isArray(parsed?.commits) ? parsed.commits : null;
}
/**
* Scan every currently-open external PR in order. The complete input is sent
* to the dedicated classifier, not a promptable chat endpoint. Any unavailable
* or malformed verdict fails closed; reports collected before that point remain
* generic and are useful to the human-facing status view only.
*/
export async function runPrReviewerSecurityScan({ app, target = null, largeInputFallback = null } = {}) {
const forgeExec = await resolveForgeExecOptions(app?.repoPath, { forgeAccount: app?.forgeAccount });
const runGh = forgeReader(forgeExec);
const resolvedTarget = target || await readExternalOpenPullRequests(app, forgeExec);
if (!resolvedTarget.ok) return resolvedTarget;
const scanKey = securityScanFingerprint(resolvedTarget);
if (!scanKey) return failure('security-scan-target-unidentifiable');
const requiresLargeInputFallback = resolvedTarget.prs.some(pr =>
pr.linkedIssues?.some(issue => issue.body.length > LINKED_ISSUE_STANDARD_BODY_MAX_CHARS));
if (requiresLargeInputFallback && (!largeInputFallback?.providerId || !largeInputFallback?.model)) {
return failure(largeInputFallback ? 'security-scan-large-input-fallback-unconfigured' : 'security-scan-linked-issue-too-large', { scanKey });
}
const reviewedPrs = [];
const reviewInputs = [];
let hasFindings = false;
// Loaded at the call site so the widely-imported preflight module does not
// statically instantiate the diff scanner (import-budget #6156).
const { scanDiffForHiddenContent, STRUCTURAL_HIDDEN_CONTENT_CATEGORIES } = await import('../lib/diffHiddenContentScan.js');
const structuralCategorySet = new Set(STRUCTURAL_HIDDEN_CONTENT_CATEGORIES);
const structuralFindingsFrom = (diff) => scanDiffForHiddenContent(diff)
.filter((finding) => structuralCategorySet.has(finding.category));
// Named by the verdicts, not the module constant: a scan that ran only the
// deterministic layer must not be recorded as classified by Prompt Guard.
let guardModel = MODEL_ABUSE_GUARD.name;
let guardRevision = MODEL_ABUSE_GUARD.revision;
for (const pr of resolvedTarget.prs) {
if (pr.inputComplete === false || pr.linkedIssues?.some(issue => issue.truncated)) return failure('security-scan-linked-issue-too-large', { reviewedPrs, scanKey });
const [diff, commits] = await Promise.all([
runGh(['pr', 'diff', String(pr.number), '--repo', resolvedTarget.repoSpec]).catch(() => null),
fetchPullRequestCommits(pr.number, resolvedTarget.repoSpec, runGh),
]);
if (diff === null) return failure('security-scan-diff-unavailable', { reviewedPrs, scanKey });
// Fail closed on an unreadable commit log: screening a PR as though it had
// no commit messages is the hidden-content gap the log was added to close.
if (commits === null) return failure('security-scan-pr-commits-unreadable', { reviewedPrs, scanKey });
if (typeof diff !== 'string' || diff.length > SECURITY_SCAN_MAX_DIFF_CHARS) {
return failure('security-scan-diff-too-large', { reviewedPrs, scanKey });
}
if (!diff.trim()) return failure('security-scan-empty-diff', { reviewedPrs, scanKey });
// The linked issue's own text is contributor-authored public content, and it
// is about to be quoted to a downstream reviewer as the requirement the diff
// is judged against. It crosses the boundary in the same pass as the PR — an
// injected issue body flags the PR rather than reaching a reviewer as the
// thing it is supposed to trust. The verdict is one status for the whole
// PR, so a second classifier subprocess per PR would buy nothing: findings
// are deliberately generic either way. Freshness stays anchored on both
// halves separately — the diff by `contentFingerprint`, the issue text by
// `eligibilityFacts.intentFingerprint`.
const intentContent = linkedIssueIntentContent(pr.linkedIssues);
const prContent = screenedPullRequestContent(pr, diff, commits);
const content = intentContent ? `${prContent}\n\n${intentContent}` : prContent;
if (content.length > SECURITY_SCAN_MAX_DIFF_CHARS) {
return failure('security-scan-input-too-large', { reviewedPrs, scanKey });
}
// Structural findings (symlink out of the tree, new gitlink, non-media
// binary, inline script in markup) are not visible as text a model-abuse
// classifier can score, so they short-circuit before the classifier run.
// Title/body/commit injection still goes through the full screen when the
// diff itself is structurally clean.
const structural = structuralFindingsFrom(diff);
const screened = structural.length
? { screening: structuralVerdict(structural) }
: await screenUntrustedContent({ content, source: 'github-pr' });
const verdict = screened.screening || screened;
if (!verdict.ok) return failure(verdict.code || 'security-scan-verdict-unavailable', { reviewedPrs, scanKey, ...(typeof verdict.message === 'string' ? { message: verdict.message } : {}) });
guardModel = verdict.model || guardModel;
guardRevision = verdict.revision ?? null;
const report = reportFor(pr, diff, commits, verdict);
reviewedPrs.push(report);
if (reportChars(reviewedPrs) > SECURITY_SCAN_MAX_REPORT_CHARS) {
return failure('security-scan-report-too-large', { reviewedPrs, scanKey });
}
if (!report.safe) hasFindings = true;
else reviewInputs.push({
number: pr.number,
title: pr.title,
body: pr.body,
authorLogin: pr.authorLogin,
url: pr.url,
headSha: pr.headRefOid,
baseRefName: resolvedTarget.defaultBranch,
eligibilityFacts: pr.eligibilityFacts,
linkedIssues: pr.linkedIssues || [],
behindBy: null,
files: [],
additions: 0,
deletions: 0,
diff,
});
}
return {
ok: true,
passed: !hasFindings,
code: hasFindings ? 'security-scan-findings' : 'security-scan-passed',
guardId: MODEL_ABUSE_GUARD.id,
guardModel,
guardRevision,
repoFullName: resolvedTarget.repoFullName,
defaultBranch: resolvedTarget.defaultBranch,
scanKey,
reviewedPrs,
reports: reviewedPrs,
reviewInputs,
usedLargeInputFallback: reviewInputs.some(input =>
input.linkedIssues.some(issue => issue.body.length > LINKED_ISSUE_STANDARD_BODY_MAX_CHARS)),
};
}
export function summarizeSecurityScanReport(report) {
return {
number: report?.number || null,
safe: report?.safe === true,
findingCount: Array.isArray(report?.securityFindings) ? report.securityFindings.length : 0,
guardId: safeText(report?.guardId, 100) || MODEL_ABUSE_GUARD.id,
};
}