mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-06-26 10:01:28 +08:00
feat(layer4): live messages-table wiring for proximity triggers
Finishes the steer/transmit loop — advisories now reach the agents' sessions. - message-sink.js: createEccMessageSink() delivers via the canonical writer 'ecc-tui messages send' (maps steer/hold -> conflict kind, transmit -> query), resolving the binary from override/env/built target/PATH. Injectable runner; best-effort (a missing binary/failed send is counted skipped, never blocks). - proximity.js: createProximityDispatcher() adds per-trigger cooldown so a persistent collision fires once then stays quiet (agents get steered, not spammed); runProximityTick() builds the snapshot and dispatches. - scripts/proximity-tick.js: thin CLI — one-shot, --dry-run, --watch <sec>. Messages are internal ECC agent-to-agent coordination, not any external channel. - 14 new tests (sink argv/kind mapping, cooldown dedup, tick dispatch/dry-run, CLI parse). Full suite 2891/2891; lint green.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Concrete message sink for proximity triggers: delivers a session-to-session
|
||||
* message through the canonical writer, the `ecc-tui messages send` CLI. The CLI
|
||||
* owns the ecc2 session DB (the `messages` table the control pane reads), so we
|
||||
* shell out to it rather than writing the SQLite directly and racing the daemon.
|
||||
*
|
||||
* Best-effort: if the binary is not found / the command fails, the call throws,
|
||||
* and the dispatcher counts it as skipped — proximity never blocks on delivery.
|
||||
* The command runner and binary path are injectable for tests.
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execFileSync } = require('child_process');
|
||||
|
||||
// Proximity trigger type → ecc-tui message kind (value_enum on `--kind`).
|
||||
// A steer/hold is a collision warning; a transmit is a "what are you doing" query.
|
||||
const KIND_BY_TYPE = {
|
||||
proximity_steer: 'conflict',
|
||||
proximity_hold: 'conflict',
|
||||
proximity_transmit: 'query'
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve the ecc-tui binary: explicit override, env var, a built target in the
|
||||
* repo, then the bare name (hope it's on PATH).
|
||||
*/
|
||||
function resolveEccBin(deps = {}) {
|
||||
if (deps.binPath) return deps.binPath;
|
||||
if (process.env.ECC_TUI_BIN && process.env.ECC_TUI_BIN.trim()) return process.env.ECC_TUI_BIN.trim();
|
||||
const repoRoot = deps.repoRoot || path.join(__dirname, '..', '..', '..');
|
||||
for (const rel of ['ecc2/target/release/ecc-tui', 'ecc2/target/debug/ecc-tui']) {
|
||||
const candidate = path.join(repoRoot, rel);
|
||||
try {
|
||||
if (fs.existsSync(candidate)) return candidate;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return 'ecc-tui';
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the `messages send` argv for a proximity message.
|
||||
*/
|
||||
function buildSendArgs({ fromSession, toSession, content, msgType }) {
|
||||
const kind = KIND_BY_TYPE[msgType] || 'query';
|
||||
return ['messages', 'send', '--from', String(fromSession), '--to', String(toSession), '--kind', kind, '--text', String(content)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `sendMessage({ fromSession, toSession, content, msgType })` sink that
|
||||
* delivers via `ecc-tui messages send`. Inject `runCommand(bin, args)` for tests.
|
||||
*/
|
||||
function createEccMessageSink(deps = {}) {
|
||||
const run = deps.runCommand || ((bin, args) => execFileSync(bin, args, { encoding: 'utf8', timeout: 5000, stdio: ['ignore', 'pipe', 'pipe'] }));
|
||||
const bin = resolveEccBin(deps);
|
||||
return function sendMessage(message) {
|
||||
run(bin, buildSendArgs(message));
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
KIND_BY_TYPE,
|
||||
resolveEccBin,
|
||||
buildSendArgs,
|
||||
createEccMessageSink
|
||||
};
|
||||
@@ -179,11 +179,84 @@ function dispatchProximityTriggers(triggers, deps = {}) {
|
||||
return { dispatched, skipped };
|
||||
}
|
||||
|
||||
/**
|
||||
* Stateful dispatcher with per-trigger cooldown, so a collision that persists
|
||||
* across many ticks fires once and then stays quiet until it clears or the
|
||||
* cooldown lapses — agents get steered, not spammed. Inject `sendMessage`
|
||||
* (e.g. createEccMessageSink) and optionally `now`/`cooldownMs` for tests.
|
||||
*/
|
||||
function createProximityDispatcher(deps = {}) {
|
||||
const send = deps.sendMessage;
|
||||
const cooldownMs = Number.isFinite(deps.cooldownMs) ? deps.cooldownMs : 5 * 60 * 1000;
|
||||
const now = typeof deps.now === 'function' ? deps.now : () => Date.now();
|
||||
const lastFired = new Map();
|
||||
const keyOf = t => `${t.to}<-${t.from}:${t.type}`;
|
||||
|
||||
return {
|
||||
dispatch(triggers) {
|
||||
let dispatched = 0;
|
||||
let suppressed = 0;
|
||||
let skipped = 0;
|
||||
for (const t of triggers || []) {
|
||||
const key = keyOf(t);
|
||||
const last = lastFired.get(key);
|
||||
const ts = now();
|
||||
if (last !== undefined && ts - last < cooldownMs) {
|
||||
suppressed += 1;
|
||||
continue;
|
||||
}
|
||||
if (typeof send !== 'function') {
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
send({ fromSession: t.from, toSession: t.to, content: t.content, msgType: t.type });
|
||||
lastFired.set(key, ts);
|
||||
dispatched += 1;
|
||||
} catch {
|
||||
skipped += 1;
|
||||
}
|
||||
}
|
||||
return { dispatched, suppressed, skipped };
|
||||
},
|
||||
reset() {
|
||||
lastFired.clear();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* One proximity tick: build the snapshot, then dispatch its triggers (steer the
|
||||
* agents). `buildSnapshot()` returns a control-pane snapshot with a `proximity`
|
||||
* field; `dispatcher` is a createProximityDispatcher. `dryRun` reports what
|
||||
* would fire without sending. Both are injected so the CLI stays a thin wrapper
|
||||
* and the logic is unit-testable.
|
||||
*/
|
||||
async function runProximityTick(deps = {}) {
|
||||
const snapshot = await deps.buildSnapshot();
|
||||
const prox = (snapshot && snapshot.proximity) || { advisories: [], triggers: [], counts: {} };
|
||||
const triggers = prox.triggers || [];
|
||||
let result;
|
||||
if (deps.dryRun || !deps.dispatcher) {
|
||||
result = { dispatched: 0, suppressed: 0, skipped: triggers.length, dryRun: Boolean(deps.dryRun) };
|
||||
} else {
|
||||
result = deps.dispatcher.dispatch(triggers);
|
||||
}
|
||||
return {
|
||||
counts: prox.counts || {},
|
||||
advisories: prox.advisories || [],
|
||||
triggers,
|
||||
result
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildProximitySnapshot,
|
||||
sessionsToAgents,
|
||||
defaultWorkingSetFor,
|
||||
defaultChangedFilesFor,
|
||||
parseDiffRanges,
|
||||
dispatchProximityTriggers
|
||||
dispatchProximityTriggers,
|
||||
createProximityDispatcher,
|
||||
runProximityTick
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user