feat(tools): scaffold ruview MCP server + CLI + ADR-104 (#705)

Adds two new npm packages that expose RuView's WiFi-DensePose
sensing capabilities outside the Cognitum appliance ecosystem:

- tools/ruview-mcp/ (@ruv/ruview-mcp) — MCP server with 6 tools:
  ruview_csi_latest, ruview_pose_infer, ruview_count_infer,
  ruview_registry_list, ruview_train_count, ruview_job_status.
  Uses @modelcontextprotocol/sdk with stdio transport.
  6/6 smoke tests pass. TypeScript strict mode, Node 20.

- tools/ruview-cli/ (@ruv/ruview-cli) — Yargs CLI with matching
  subcommands: csi tail, pose infer, count infer, cogs list,
  train count, job status. Same fail-open pattern as the cog
  binaries (WARN to stderr, exit 0 on unavailable sensing-server).

- docs/adr/ADR-104-ruview-mcp-cli-distribution.md — design rationale,
  6-row threat table, packaging plan, acceptance gates, failure modes.

- docs/research/sota-2026-05-22/HORIZON.md — 12-hour horizon plan
  with 7 milestones tracked (M1 complete in this commit).

Both packages are private:true pending the user's publish decision.
Inference is via subprocess to the signed cog binaries (ADR-100/101/103)
— no JS/WASM ML engine bundled.
This commit is contained in:
rUv
2026-05-21 23:33:18 -04:00
committed by GitHub
parent bb92419ccb
commit 3f462a254d
32 changed files with 11597 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
/** @type {import('jest').Config} */
export default {
preset: "ts-jest/presets/default-esm",
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
useESM: true,
},
],
},
testMatch: ["**/tests/**/*.test.ts"],
};
+3843
View File
File diff suppressed because it is too large Load Diff
+49
View File
@@ -0,0 +1,49 @@
{
"name": "@ruv/ruview-cli",
"version": "0.0.1",
"description": "RuView CLI — shell access to WiFi-DensePose sensing, inference, and training capabilities",
"private": true,
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"ruview": "dist/index.js"
},
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"lint": "eslint src --ext .ts",
"typecheck": "tsc --noEmit"
},
"keywords": [
"ruview",
"wifi",
"csi",
"pose-estimation",
"cognitum",
"cli"
],
"author": "ruv <ruv@ruv.net>",
"license": "Apache-2.0",
"dependencies": {
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/node": "^20.14.0",
"@types/yargs": "^17.0.32",
"jest": "^29.7.0",
"ts-jest": "^29.1.0",
"typescript": "^5.4.5"
},
"engines": {
"node": ">=20.0.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
+44
View File
@@ -0,0 +1,44 @@
/**
* Subprocess wrapper for Cognitum Cog binaries (CLI variant).
* Mirrors tools/ruview-mcp/src/cog.ts.
*/
import { spawn } from "node:child_process";
export type Result<T> = { ok: true; data: T } | { ok: false; error: string };
const COG_TIMEOUT_MS = 15_000;
export async function runCog(binary: string, args: string[]): Promise<Result<string>> {
return new Promise((resolve) => {
let stdout = "";
let stderr = "";
const child = spawn(binary, args, {
timeout: COG_TIMEOUT_MS,
stdio: ["ignore", "pipe", "pipe"],
});
child.stdout?.on("data", (chunk: Buffer) => { stdout += chunk.toString(); });
child.stderr?.on("data", (chunk: Buffer) => { stderr += chunk.toString(); });
child.on("error", (e) => {
resolve(err(
`Failed to launch "${binary}" (${args.join(" ")}): ${e.message}. ` +
`Set RUVIEW_POSE_COG_BINARY / RUVIEW_COUNT_COG_BINARY or install the cog.`
));
});
child.on("close", (code) => {
if (code !== 0) {
resolve(err(`Cog "${binary} ${args.join(" ")}" exited with code ${code}. stderr: ${stderr.trim() || "(empty)"}`));
} else {
resolve({ ok: true, data: stdout });
}
});
});
}
function err(error: string): { ok: false; error: string } {
return { ok: false, error };
}
+88
View File
@@ -0,0 +1,88 @@
/**
* ruview cogs — Cognitum edge module registry commands.
*
* cogs list — list cogs from the registry (via sensing-server ADR-102 proxy).
*/
import type { Argv } from "yargs";
import { sensingGet } from "../http.js";
import { loadConfig } from "../config.js";
export function cogsCommand(cli: Argv): void {
cli.command(
"cogs <action>",
"Edge module registry commands",
(y) =>
y
.positional("action", {
choices: ["list"] as const,
description: "Action to perform",
})
.option("category", {
type: "string",
description:
"Filter by category: health, security, building, retail, industrial, " +
"research, ai, swarm, signal, network, developer",
})
.option("search", {
type: "string",
description: "Search substring matched against cog id and name (case-insensitive)",
})
.option("refresh", {
type: "boolean",
default: false,
description: "Bypass the 1-hour registry cache",
})
.option("url", {
type: "string",
description: "Override the sensing-server URL",
}),
async (args) => {
const config = loadConfig();
const baseUrl = (args["url"] as string | undefined) ?? config.sensingServerUrl;
if (args.action === "list") {
const qs = args.refresh ? "?refresh=1" : "";
const result = await sensingGet<{
registry?: { cogs?: object[]; apps?: object[] };
}>(baseUrl, `/api/v1/edge/registry${qs}`, config.apiToken);
if (!result.ok) {
process.stderr.write(`[WARN] ${result.error}\n`);
process.stdout.write(
JSON.stringify({ ok: false, warn: true, error: result.error }) + "\n"
);
process.exit(0);
}
const payload = result.data;
let cogs: object[] =
payload.registry?.cogs ?? payload.registry?.apps ?? [];
if (args.category) {
const cat = (args.category as string).toLowerCase();
cogs = cogs.filter(
(c) =>
(c as Record<string, unknown>)["category"]
?.toString()
.toLowerCase() === cat
);
}
if (args.search) {
const q = (args.search as string).toLowerCase();
cogs = cogs.filter((c) => {
const rec = c as Record<string, unknown>;
return (
rec["id"]?.toString().toLowerCase().includes(q) ||
rec["name"]?.toString().toLowerCase().includes(q)
);
});
}
process.stdout.write(
JSON.stringify({ ok: true, total: cogs.length, cogs }, null, 2) + "\n"
);
}
}
);
}
+83
View File
@@ -0,0 +1,83 @@
/**
* ruview count — Person count commands.
*
* count infer — run single-shot person-count inference.
*/
import type { Argv } from "yargs";
import { runCog } from "../cog.js";
import { loadConfig } from "../config.js";
export function countCommand(cli: Argv): void {
cli.command(
"count <action>",
"Person count commands",
(y) =>
y
.positional("action", {
choices: ["infer"] as const,
description: "Action to perform",
})
.option("window", {
type: "string",
description: "Path to a CSI window JSON file (omit to use live sensing-server)",
})
.option("binary", {
type: "string",
description: "Path to cog-person-count binary (default: RUVIEW_COUNT_COG_BINARY)",
})
.option("max-persons", {
type: "number",
default: 7,
description: "Upper bound on person count (17, default: 7)",
}),
async (args) => {
const config = loadConfig();
const binary = (args["binary"] as string | undefined) ?? config.countCogBinary;
if (args.action === "infer") {
const health = await runCog(binary, ["health"]);
if (!health.ok) {
process.stderr.write(
`[WARN] Cog health check failed: ${health.error}\n` +
`Set RUVIEW_COUNT_COG_BINARY or install cog-person-count (ADR-103).\n`
);
process.stdout.write(
JSON.stringify({
ok: false,
warn: true,
error: health.error,
stub: true,
result: {
count: 0,
confidence: 0,
count_p95_low: 0,
count_p95_high: 0,
backend: "stub",
latency_ms: 0,
},
}) + "\n"
);
process.exit(0);
}
process.stdout.write(
JSON.stringify({
ok: true,
stub: true,
note: "M1 stub — real inference wired in M2. Cog health passed.",
result: {
ts: Date.now() / 1000,
count: 0,
confidence: 0,
count_p95_low: 0,
count_p95_high: 0,
backend: "stub",
latency_ms: 0,
},
}) + "\n"
);
}
}
);
}
+64
View File
@@ -0,0 +1,64 @@
/**
* ruview csi — CSI frame commands.
*
* csi tail — stream live CSI frames from the sensing-server.
*/
import type { Argv } from "yargs";
import { sensingGet } from "../http.js";
import { loadConfig } from "../config.js";
export function csiCommand(cli: Argv): void {
cli.command(
"csi <action>",
"CSI frame commands",
(y) =>
y
.positional("action", {
choices: ["tail"] as const,
description: "Action to perform",
})
.option("url", {
type: "string",
description:
"Sensing-server URL (default: RUVIEW_SENSING_SERVER_URL or http://localhost:3000)",
})
.option("interval", {
type: "number",
default: 500,
description: "Polling interval in milliseconds (default: 500)",
}),
async (args) => {
const config = loadConfig();
const baseUrl = (args["url"] as string | undefined) ?? config.sensingServerUrl;
if (args.action === "tail") {
process.stderr.write(
`[ruview csi tail] Streaming from ${baseUrl} every ${args.interval}ms. Ctrl-C to stop.\n`
);
// Streaming poll loop.
// eslint-disable-next-line no-constant-condition
while (true) {
const result = await sensingGet<object>(
baseUrl,
"/api/v1/sensing/latest",
config.apiToken
);
if (!result.ok) {
process.stderr.write(
`[WARN] ${result.error} — retrying in ${args.interval}ms\n`
);
} else {
process.stdout.write(JSON.stringify(result.data) + "\n");
}
await new Promise<void>((resolve) =>
setTimeout(resolve, args.interval as number)
);
}
}
}
);
}
+73
View File
@@ -0,0 +1,73 @@
/**
* ruview job — Job management commands.
*
* job status --id <job_id> — poll a background training job.
*/
import type { Argv } from "yargs";
import { readFileSync, existsSync } from "node:fs";
import { loadConfig } from "../config.js";
export function jobCommand(cli: Argv): void {
cli.command(
"job <action>",
"Job management commands",
(y) =>
y
.positional("action", {
choices: ["status"] as const,
description: "Action to perform",
})
.option("id", {
type: "string",
demandOption: true,
description: "Job ID returned by ruview train count",
}),
async (args) => {
const config = loadConfig();
if (args.action === "status") {
const jobId = args.id as string;
const { default: path } = await import("node:path");
const logPath = path.join(config.jobsDir, `${jobId}.log`);
if (!existsSync(logPath)) {
process.stdout.write(
JSON.stringify({
ok: false,
error: `Job ${jobId} not found at ${logPath}. ` +
"The CLI process that started the job may have been restarted.",
}) + "\n"
);
process.exit(0);
}
const content = readFileSync(logPath, "utf8");
const lines = content.split("\n");
const recentLog = lines.slice(Math.max(0, lines.length - 20));
// Derive status from the log content.
let status: string = "running";
if (content.includes("# exit code: 0")) {
status = "done";
} else if (content.includes("# exit code:") || content.includes("# ERROR:")) {
status = "failed";
}
process.stdout.write(
JSON.stringify(
{
ok: true,
job_id: jobId,
status,
log_path: logPath,
recent_log: recentLog,
},
null,
2
) + "\n"
);
}
}
);
}
+70
View File
@@ -0,0 +1,70 @@
/**
* ruview pose — Pose estimation commands.
*
* pose infer — run single-shot 17-keypoint inference.
*/
import type { Argv } from "yargs";
import { runCog } from "../cog.js";
import { loadConfig } from "../config.js";
export function poseCommand(cli: Argv): void {
cli.command(
"pose <action>",
"Pose estimation commands",
(y) =>
y
.positional("action", {
choices: ["infer"] as const,
description: "Action to perform",
})
.option("window", {
type: "string",
description: "Path to a CSI window JSON file (omit to use live sensing-server)",
})
.option("binary", {
type: "string",
description: "Path to cog-pose-estimation binary (default: RUVIEW_POSE_COG_BINARY)",
}),
async (args) => {
const config = loadConfig();
const binary = (args["binary"] as string | undefined) ?? config.poseCogBinary;
if (args.action === "infer") {
// M1: verify health, emit stub.
const health = await runCog(binary, ["health"]);
if (!health.ok) {
process.stderr.write(
`[WARN] Cog health check failed: ${health.error}\n` +
`Set RUVIEW_POSE_COG_BINARY or install cog-pose-estimation (ADR-101).\n`
);
process.stdout.write(
JSON.stringify({
ok: false,
warn: true,
error: health.error,
stub: true,
result: { n_persons: 0, persons: [], backend: "stub", latency_ms: 0 },
}) + "\n"
);
process.exit(0); // Fail-open; non-zero would break pipelines.
}
process.stdout.write(
JSON.stringify({
ok: true,
stub: true,
note: "M1 stub — real inference wired in M2. Cog health passed.",
result: {
ts: Date.now() / 1000,
n_persons: 0,
persons: [],
backend: "stub",
latency_ms: 0,
},
}) + "\n"
);
}
}
);
}
+119
View File
@@ -0,0 +1,119 @@
/**
* ruview train — Training commands.
*
* train count --paired <jsonl> — kick off a count-cog training run.
*/
import type { Argv } from "yargs";
import { randomUUID } from "node:crypto";
import { mkdirSync, appendFileSync, openSync } from "node:fs";
import path from "node:path";
import os from "node:os";
import { spawn } from "node:child_process";
import { loadConfig } from "../config.js";
export function trainCommand(cli: Argv): void {
cli.command(
"train <task>",
"Training commands",
(y) =>
y
.positional("task", {
choices: ["count"] as const,
description: "Which cog to train",
})
.option("paired", {
type: "string",
demandOption: true,
description:
"Path to the paired JSONL training file (produced by scripts/align-ground-truth.js)",
})
.option("epochs", {
type: "number",
default: 400,
description: "Training epochs (default: 400)",
})
.option("lr", {
type: "number",
default: 1e-3,
description: "Initial learning rate (default: 0.001)",
})
.option("output-dir", {
type: "string",
description: "Output directory for model artifacts",
}),
async (args) => {
const config = loadConfig();
const jobId = randomUUID();
const logDir = config.jobsDir;
mkdirSync(logDir, { recursive: true });
const logPath = path.join(logDir, `${jobId}.log`);
const queuedAt = Date.now() / 1000;
const outputDir =
(args["output-dir"] as string | undefined) ??
"v2/crates/cog-person-count/cog/artifacts";
const header = [
`# RuView training job ${jobId}`,
`# started: ${new Date().toISOString()}`,
`# task: ${args.task}`,
`# paired: ${args.paired}`,
`# epochs: ${args.epochs}`,
`# lr: ${args.lr}`,
`# output-dir: ${outputDir}`,
"",
].join("\n");
appendFileSync(logPath, header);
const logFdOut = openSync(logPath, "a");
const logFdErr = openSync(logPath, "a");
const cargoArgs = [
"run",
"--release",
"-p",
"wifi-densepose-train",
"--",
"--task",
"count",
"--paired",
args.paired as string,
"--epochs",
String(args.epochs),
"--lr",
String(args.lr),
"--output-dir",
outputDir,
];
const child = spawn("cargo", cargoArgs, {
detached: true,
stdio: ["ignore", logFdOut, logFdErr],
});
child.unref();
child.on("error", (e) => {
appendFileSync(logPath, `\n# ERROR: ${e.message}\n`);
});
child.on("close", (code) => {
appendFileSync(logPath, `\n# exit code: ${code}\n`);
});
process.stdout.write(
JSON.stringify(
{
ok: true,
job_id: jobId,
status: "running",
log_path: logPath,
queued_at: queuedAt,
note: `Poll with: ruview job status --id ${jobId}`,
},
null,
2
) + "\n"
);
}
);
}
+35
View File
@@ -0,0 +1,35 @@
/**
* Configuration loader for the RuView CLI.
* Mirrors tools/ruview-mcp/src/config.ts — sourced from environment variables.
*/
import os from "node:os";
import path from "node:path";
export interface RuviewCliConfig {
sensingServerUrl: string;
apiToken: string | undefined;
poseCogBinary: string;
countCogBinary: string;
jobsDir: string;
}
function envOrDefault(key: string, fallback: string): string {
return process.env[key] ?? fallback;
}
export function loadConfig(): RuviewCliConfig {
return {
sensingServerUrl: envOrDefault(
"RUVIEW_SENSING_SERVER_URL",
"http://localhost:3000"
),
apiToken: process.env["RUVIEW_API_TOKEN"],
poseCogBinary: envOrDefault("RUVIEW_POSE_COG_BINARY", "cog-pose-estimation"),
countCogBinary: envOrDefault("RUVIEW_COUNT_COG_BINARY", "cog-person-count"),
jobsDir: envOrDefault(
"RUVIEW_JOBS_DIR",
path.join(os.homedir(), ".ruview", "jobs")
),
};
}
+53
View File
@@ -0,0 +1,53 @@
/**
* Lightweight HTTP client (re-used in CLI commands).
* Identical to tools/ruview-mcp/src/http.ts but kept separate to avoid a
* workspace dependency — both packages are standalone and independently publishable.
*/
const REQUEST_TIMEOUT_MS = 10_000;
export type Ok<T> = { ok: true; data: T };
export type Err = { ok: false; error: string };
export type Result<T> = Ok<T> | Err;
export function ok<T>(data: T): Ok<T> {
return { ok: true, data };
}
export function err(error: string): Err {
return { ok: false, error };
}
export async function sensingGet<T>(
baseUrl: string,
path: string,
token: string | undefined
): Promise<Result<T>> {
const url = `${baseUrl.replace(/\/$/, "")}${path}`;
const headers: Record<string, string> = { Accept: "application/json" };
if (token) headers["Authorization"] = `Bearer ${token}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
try {
const res = await fetch(url, { headers, signal: controller.signal });
clearTimeout(timer);
if (!res.ok) {
return err(`HTTP ${res.status} from ${url}: ${await res.text().catch(() => "(no body)")}`);
}
let body: unknown;
try {
body = await res.json();
} catch {
return err(`Non-JSON response from ${url}`);
}
return ok(body as T);
} catch (e: unknown) {
clearTimeout(timer);
if (e instanceof Error && e.name === "AbortError") {
return err(`Request to ${url} timed out after ${REQUEST_TIMEOUT_MS}ms`);
}
return err(`Network error fetching ${url}: ${String(e)}`);
}
}
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env node
/**
* @ruv/ruview-cli — RuView CLI
*
* Shell access to RuView sensing, inference, and training capabilities.
*
* Subcommands:
* ruview csi tail [--url <url>] stream live CSI frames
* ruview pose infer [--window <path>] 17-keypoint pose estimation
* ruview count infer [--window <path>] person-count inference
* ruview cogs list [--category <cat>] [--search q] list edge module registry
* ruview train count --paired <jsonl> kick off count-cog training
* ruview job status --id <job_id> poll a training job
*
* All subcommands write JSON to stdout and exit 0 on success.
* WARN-level outputs write to stderr; the exit code is still 0 so pipelines
* are not broken by a temporarily unreachable sensing-server.
*
* Usage:
* npx ruview --version
* npx ruview csi tail
* npx ruview pose infer --window ./window.json
* RUVIEW_SENSING_SERVER_URL=http://cognitum-v0:3000 npx ruview cogs list
*
* See ADR-104 for the full design rationale and security model.
*/
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { csiCommand } from "./commands/csi.js";
import { poseCommand } from "./commands/pose.js";
import { countCommand } from "./commands/count.js";
import { cogsCommand } from "./commands/cogs.js";
import { trainCommand } from "./commands/train.js";
import { jobCommand } from "./commands/job.js";
const cli = yargs(hideBin(process.argv))
.scriptName("ruview")
.version("0.0.1")
.usage("$0 <command> [options]")
.strict()
.help()
.wrap(100);
// Register all top-level commands.
csiCommand(cli);
poseCommand(cli);
countCommand(cli);
cogsCommand(cli);
trainCommand(cli);
jobCommand(cli);
cli.demandCommand(1, "Specify a subcommand. Use --help for a list.").parse();
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"lib": ["ES2022"],
"outDir": "dist",
"rootDir": "src",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}