diff --git a/web/public/style.css b/web/public/style.css
index 57d748c..3cf431b 100644
--- a/web/public/style.css
+++ b/web/public/style.css
@@ -3227,3 +3227,65 @@ details.ag-sec > summary::-webkit-details-marker { display: none; }
.dash-sev-med .dash-val-big { color: var(--sev-medium-fg, #fefcbf); }
.dash-sev-low { border-color: var(--sev-low-bar, #38a169); }
.dash-sev-low .dash-val-big { color: var(--sev-low-fg, #9ae6b4); }
+
+/* ── Chat ─────────────────────────────────────────────────────────────── */
+.chat-shell { display: grid; grid-template-columns: 268px 1fr; height: 100%; min-height: 0; }
+.chat-side { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; padding: 14px; gap: 12px; }
+.chat-new { width: 100%; }
+.chat-list { overflow-y: auto; display: flex; flex-direction: column; gap: 4px; min-height: 0; }
+.chat-list-empty { color: var(--text-faint); font-size: var(--fs-sm); padding: 12px 4px; }
+.chat-item { position: relative; text-align: left; background: none; border: 1px solid transparent; border-radius: 8px; padding: 9px 11px; cursor: pointer; display: flex; flex-direction: column; gap: 3px; color: var(--text); }
+.chat-item:hover { background: var(--surface-2); }
+.chat-item.active { background: var(--surface-2); border-color: var(--border); }
+.chat-item-title { font-size: var(--fs-sm); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.chat-item-meta { font-size: 11px; color: var(--text-faint); }
+.chat-item-del { position: absolute; right: 7px; top: 7px; opacity: 0; color: var(--text-faint); padding: 0 4px; border-radius: 4px; }
+.chat-item:hover .chat-item-del { opacity: 1; }
+.chat-item-del:hover { background: var(--surface-3, var(--surface)); color: var(--sev-critical-fg); }
+
+.chat-main { display: flex; flex-direction: column; min-height: 0; }
+.chat-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 16px 22px; border-bottom: 1px solid var(--border); }
+.chat-title { font-size: var(--fs-lg); font-weight: 650; }
+.chat-sub { font-size: var(--fs-xs); color: var(--text-faint); margin-top: 2px; }
+.chat-head-right select { max-width: 280px; }
+
+.chat-stream { flex: 1; overflow-y: auto; padding: 22px; display: flex; flex-direction: column; gap: 16px; min-height: 0; }
+.chat-empty { margin: auto; text-align: center; max-width: 420px; color: var(--text-dim); }
+.chat-empty b { display: block; font-size: var(--fs-lg); color: var(--text); margin-bottom: 8px; }
+.chat-empty p { font-size: var(--fs-sm); margin: 6px 0; }
+.chat-empty-hint { color: var(--text-faint); }
+
+.chat-msg { display: flex; max-width: 100%; }
+.chat-msg.user { justify-content: flex-end; }
+.chat-msg-body { border-radius: 12px; padding: 11px 15px; font-size: var(--fs-sm); line-height: 1.6; max-width: 76%; word-wrap: break-word; overflow-wrap: anywhere; }
+.chat-msg.user .chat-msg-body { background: var(--accent, #7c5cff); color: #fff; }
+.chat-msg.assistant .chat-msg-body { background: var(--surface-2); border: 1px solid var(--border); }
+.chat-msg.pending .chat-msg-body { color: var(--text-faint); }
+.chat-msg-body code { background: var(--surface-3, var(--surface)); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
+.chat-msg.user .chat-msg-body code { background: rgba(255,255,255,.18); }
+.chat-code { background: var(--surface-3, var(--surface)); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; overflow-x: auto; font-size: 12px; margin: 8px 0; }
+.chat-msg-body h3, .chat-msg-body h4 { margin: 10px 0 5px; font-size: var(--fs-sm); }
+.chat-note { margin-top: 9px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 11px; color: var(--text-faint); }
+.chat-err { color: var(--sev-critical-fg); }
+
+.chat-refs { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 22px 10px; }
+.chat-ref { font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 3px 8px; color: var(--text-dim); }
+.chat-ref b { margin-left: 6px; cursor: pointer; color: var(--text-faint); }
+.chat-ref b:hover { color: var(--sev-critical-fg); }
+
+.chat-input { display: flex; gap: 10px; align-items: flex-end; padding: 14px 22px 20px; border-top: 1px solid var(--border); }
+.chat-input textarea { flex: 1; resize: none; min-height: 42px; max-height: 200px; padding: 11px 14px; border-radius: 10px; font-family: inherit; font-size: var(--fs-sm); line-height: 1.5; }
+.chat-input .btn { height: 42px; width: 42px; padding: 0; font-size: 17px; }
+
+.chat-picker { position: absolute; bottom: 86px; left: 290px; right: 40px; max-height: 260px; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-float); z-index: 40; }
+.chat-pick { display: flex; align-items: baseline; gap: 10px; width: 100%; text-align: left; background: none; border: 0; padding: 9px 13px; cursor: pointer; color: var(--text); font-size: var(--fs-sm); }
+.chat-pick:hover, .chat-pick.active { background: var(--surface-2); }
+.chat-pick-kind { font-size: 11px; color: var(--accent, #7c5cff); font-weight: 650; min-width: 62px; }
+.chat-pick-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.chat-pick-meta { font-size: 11px; color: var(--text-faint); }
+
+@media (max-width: 900px) {
+ .chat-shell { grid-template-columns: 1fr; }
+ .chat-side { display: none; }
+ .chat-picker { left: 20px; }
+}
diff --git a/web/server.js b/web/server.js
index ff86926..cc945dc 100644
--- a/web/server.js
+++ b/web/server.js
@@ -168,6 +168,71 @@ const WEB_DIR = __dirname;
const ROOT = path.resolve(WEB_DIR, '..'); // repo root — holds agents_md/, runs/
const AGENTS_DIR = path.join(ROOT, 'agents_md');
const RUNS_DIR = path.join(ROOT, 'runs');
+
+// ---------------------------------------------------------------------------
+// Chat — conversations about the engagement, answered from its own records.
+// ---------------------------------------------------------------------------
+
+const CHAT_DIR = path.join(ROOT, '.neurosploit', 'chats');
+
+async function chatList() {
+ try {
+ const names = await fsp.readdir(CHAT_DIR);
+ const out = [];
+ for (const n of names) {
+ if (!n.endsWith('.json')) continue;
+ const c = await readJsonSafe(path.join(CHAT_DIR, n), null);
+ if (!c) continue;
+ out.push({
+ id: c.id,
+ title: c.title || 'New conversation',
+ at: c.at || 0,
+ turns: (c.messages || []).length,
+ model: c.model || '',
+ });
+ }
+ return out.sort((a, b) => b.at - a.at);
+ } catch { return []; }
+}
+
+async function chatRead(id) {
+ if (!/^[\w-]{1,64}$/.test(id)) return null;
+ return readJsonSafe(path.join(CHAT_DIR, `${id}.json`), null);
+}
+
+async function chatWrite(c) {
+ await fsp.mkdir(CHAT_DIR, { recursive: true });
+ await fsp.writeFile(path.join(CHAT_DIR, `${c.id}.json`), JSON.stringify(c, null, 2));
+}
+
+/// Drive `neurosploit chat`, which streams JSON lines. Resolved separately
+/// from runCli because that one buffers to completion, and the point here is
+/// that the operator sees the answer as it lands.
+function chatAsk(request, onEvent) {
+ return new Promise((resolve) => {
+ if (!BIN) return resolve({ error: 'neurosploit binary not found' });
+ const child = spawn(BIN, ['chat'], { cwd: ROOT, env: { ...process.env, ...envOverrides() } });
+ let buf = '';
+ let stderr = '';
+ child.stdout.on('data', (d) => {
+ buf += d.toString('utf8');
+ let i;
+ while ((i = buf.indexOf('\n')) >= 0) {
+ const line = buf.slice(0, i).trim();
+ buf = buf.slice(i + 1);
+ if (!line.startsWith('{')) continue; // the CLI's [auto] banner lines
+ try { onEvent(JSON.parse(line)); } catch { /* not ours */ }
+ }
+ });
+ child.stderr.on('data', (d) => { stderr += d.toString('utf8'); });
+ child.on('close', (code) => resolve(code === 0 ? {} : { error: stripAnsi(stderr).trim() || `chat exited ${code}` }));
+ child.on('error', (e) => resolve({ error: e.message }));
+ child.stdin.write(JSON.stringify(request));
+ child.stdin.end();
+ });
+}
+
+
const NAMES_FILE = path.join(ROOT, '.neurosploit', 'web-engagement-names.json');
// Engagement names are set by the operator in the wizard before launch (not
@@ -3472,6 +3537,68 @@ const server = http.createServer(async (req, res) => {
}
// ---- REPL (real CLI harness session) ----
+ // ── Chat ──────────────────────────────────────────────────────────────
+ if (req.method === 'GET' && p === '/api/chat') {
+ return sendJson(res, 200, await chatList());
+ }
+ if (req.method === 'POST' && p === '/api/chat') {
+ if (!sameOrigin(req)) return sendJson(res, 403, { error: 'cross-origin request refused' });
+ const c = { id: crypto.randomUUID(), title: 'New conversation', at: Date.now(), messages: [], model: '' };
+ await chatWrite(c);
+ return sendJson(res, 201, c);
+ }
+ {
+ const cm = p.match(/^\/api\/chat\/([\w-]{1,64})$/);
+ if (cm && req.method === 'GET') {
+ const c = await chatRead(cm[1]);
+ return c ? sendJson(res, 200, c) : sendJson(res, 404, { error: 'no such conversation' });
+ }
+ if (cm && req.method === 'DELETE') {
+ if (!sameOrigin(req)) return sendJson(res, 403, { error: 'cross-origin request refused' });
+ await fsp.rm(path.join(CHAT_DIR, `${cm[1]}.json`), { force: true });
+ return sendJson(res, 200, { ok: true });
+ }
+ }
+ {
+ const sm = p.match(/^\/api\/chat\/([\w-]{1,64})\/send$/);
+ if (sm && req.method === 'POST') {
+ if (!sameOrigin(req)) return sendJson(res, 403, { error: 'cross-origin request refused' });
+ const c = await chatRead(sm[1]);
+ if (!c) return sendJson(res, 404, { error: 'no such conversation' });
+ const body = await readBody(req);
+ const message = String(body.message || '').trim();
+ if (!message) return sendJson(res, 400, { error: 'empty message' });
+
+ c.messages.push({ role: 'user', content: message, at: Date.now() });
+ // The first message names the conversation: a list of "New
+ // conversation" is unusable once there are more than two.
+ if (c.title === 'New conversation') c.title = message.slice(0, 60);
+ if (body.model) c.model = String(body.model);
+ await chatWrite(c);
+
+ sseInit(res);
+ let answer = '';
+ const r = await chatAsk({
+ message,
+ history: c.messages.slice(0, -1).map(({ role, content }) => ({ role, content })),
+ refs: Array.isArray(body.refs) ? body.refs : [],
+ model: c.model || '',
+ }, (ev) => {
+ if (ev.type === 'text') answer += ev.text;
+ sseSend(res, 'data', ev);
+ });
+ if (r.error) sseSend(res, 'data', { type: 'error', message: r.error });
+
+ if (answer) {
+ c.messages.push({ role: 'assistant', content: answer, at: Date.now() });
+ c.at = Date.now();
+ await chatWrite(c);
+ }
+ sseSend(res, 'data', { type: 'saved' });
+ return res.end();
+ }
+ }
+
if (req.method === 'POST' && p === '/api/repl') {
const session = startRepl();
return sendJson(res, 200, { id: session.id });