Skip to content

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

2.7 KB
Raw Blame
/** Malware scan report lifecycle shared by CoS completion and Brain links. */

import { join } from 'path';
import { PATHS, ensureDir, tryReadFile } from '../lib/fileUtils.js';

const REPORT_ID_RE = /^[a-f0-9-]{36}$/i;
const VERDICTS = new Set(['CLEAN', 'CAUTION', 'DANGEROUS']);

export function getScanReportsDirectory() {
  return join(PATHS.brain, 'scans');
}

export function reportPathForId(reportId) {
  return REPORT_ID_RE.test(reportId || '') ? join(getScanReportsDirectory(), `${reportId}.md`) : null;
}

export function extractScanVerdict(report) {
  const match = typeof report === 'string'
    ? report.match(/^#{1,6}\s*(?:final\s+)?verdict\s*:?\s*(CLEAN|CAUTION|DANGEROUS)\b/im)
    : null;
  return match && VERDICTS.has(match[1]) ? match[1] : null;
}

export async function prepareScanReportDirectory() {
  await ensureDir(getScanReportsDirectory());
}

export async function getScanReport(reportId) {
  const path = reportPathForId(reportId);
  return path ? tryReadFile(path) : null;
}

export async function finalizeMalwareScan({ agentId, task, success }) {
  const scan = task?.metadata?.malwareScan;
  if (!scan?.linkId || !scan?.reportId) return null;

  const report = success ? await getScanReport(scan.reportId) : null;
  const verdict = extractScanVerdict(report);
  const status = success && report ? 'completed' : 'failed';
  const completedAt = new Date().toISOString();
  // Keep report links inside the SPA. Opening the raw markdown API response
  // navigates a Home Screen install away from PortOS.
  const reportUrl = `/brain/links/${scan.linkId}/scan-report`;
  // This module sits on every agent-completion path. Load the Brain/Review
  // writers only for an actual scan so ordinary tasks do not pull those service
  // graphs into the completion hot path.
  const [brain, agents, review] = await Promise.all([
    import('./brain.js'),
    import('./cosAgentLifecycle.js'),
    import('./review.js')
  ]);

  await brain.updateLink(scan.linkId, {
    malwareScan: { reportId: scan.reportId, taskId: task.id, status, verdict, completedAt }
  });
  await agents.updateAgent(agentId, { metadata: { malwareScan: { linkId: scan.linkId, reportUrl, status, verdict } } });

  if (report) {
    await review.createItem({
      type: verdict === 'DANGEROUS' ? 'alert' : 'briefing',
      title: `${verdict || 'UNKNOWN'} malware scan: ${task.description}`,
      description: 'Open the attached markdown report for the complete static-analysis findings.',
      metadata: {
        referenceId: `malware-scan:${scan.reportId}`,
        category: 'content-review',
        actionKind: 'content.review',
        sourceOwned: true,
        reportUrl,
        verdict,
        linkId: scan.linkId,
      }
    });
  }

  return { status, verdict, reportUrl };
}