← Files Daybreak Defense FactoryARCHIVED FILE

desktop/scan-context-relay.mjs

3.51 KB · Nov 27, 2026 · 12:13 UTC

↓ Download file

See the change to this file →

import { once } from "node:events";
import path from "node:path";
import { createInterface } from "node:readline";

const desktopTools = new Set([
  "list_codex_security_global_findings",
  "start_codex_security_scan",
  "inspect_codex_security_setup",
  "open_codex_security_workspace",
  "submit_codex_security_setup",
  "claim_codex_security_scan_handoff_delivery",
  "attach_codex_security_scan_continuation_thread",
  "cancel_codex_security_scan_from_app",
  "release_codex_security_scan_handoff_delivery",
  "list_codex_security_scans",
]);

const skillDirectories = new Map([
  ["standard", "security-scan"],
  ["diff", "security-diff-scan"],
  ["deep", "deep-security-scan"],
]);

function parseMessage(line) {
  try {
    return JSON.parse(line);
  } catch {
    return undefined;
  }
}

// Keep desktop tools out of model discovery. The upstream server still owns
// workbench operations and verifies the scan ID and claim token.
export async function relayDaybreakMcp(input, output, server, bundleRoot) {
  const requests = createInterface({ input, crlfDelay: Infinity });
  const responses = createInterface({
    input: server.stdout,
    crlfDelay: Infinity,
  });
  const pending = new Map();

  async function forwardRequests() {
    for await (const line of requests) {
      const request = parseMessage(line);
      if (request?.id != null) {
        if (request.method === "tools/list") {
          pending.set(request.id, "tools");
        } else if (
          request.method === "tools/call" &&
          request.params?.name === "get_codex_security_scan_context"
        ) {
          pending.set(request.id, "context");
        }
      }
      if (!server.stdin.write(line + "\n")) {
        await once(server.stdin, "drain");
      }
    }
    server.stdin.end();
  }

  async function forwardResponses() {
    for await (const line of responses) {
      const response = parseMessage(line);
      const kind = pending.get(response?.id);
      pending.delete(response?.id);
      const result = response?.result;
      let enriched = line;
      if (kind === "tools" && Array.isArray(result?.tools)) {
        const tools = result.tools.map((tool) =>
          desktopTools.has(tool.name)
            ? {
                ...tool,
                _meta: {
                  ...tool._meta,
                  ui: { ...tool._meta?.ui, visibility: ["app"] },
                },
              }
            : tool,
        );
        enriched = JSON.stringify({ ...response, result: { ...result, tools } });
      } else if (kind === "context" && !result?.isError) {
        const context = result?.structuredContent;
        const skillDirectory = skillDirectories.get(context?.scan?.mode);
        if (skillDirectory != null) {
          enriched = JSON.stringify({
            ...response,
            result: {
              ...result,
              structuredContent: {
                ...context,
                daybreakWorkflow: {
                  bundleRoot,
                  skillPath: path.join(
                    bundleRoot,
                    "skills",
                    skillDirectory,
                    "SKILL.md",
                  ),
                },
              },
            },
          });
        }
      }
      if (!output.write(enriched + "\n")) {
        await once(output, "drain");
      }
    }
  }

  const responseEnd = forwardResponses();
  try {
    await Promise.race([
      responseEnd,
      forwardRequests().then(() => responseEnd),
    ]);
  } finally {
    requests.close();
    responses.close();
    input.pause();
  }
}

SHA-256: c3503de00934e04753fc988232244e1468b208500a62ff613b74fe7084f37ad4