feat(harden): cooperative progress/cancel with written list (#75)

AbortSignal + onProgress between artifacts; cancelled reports set
cancelled:true and list already-written paths (no silent half-harden).
MCP notifications/progress via progressToken; notifications/cancelled
aborts in-flight harden on a stdio fast-path (not behind the serial queue).
This commit is contained in:
qybaihe
2026-11-05 21:53:04 +08:00
parent 18a8ea5bfb
commit f7f3ea06bf
6 changed files with 424 additions and 55 deletions
+4
View File
@@ -103,6 +103,10 @@ Optional args (safe defaults): `intensity` (bankrupt), `emit_manifest` (false),
raise `max_artifacts` / narrow the directory.
- Never overwrite without the user's go-ahead. On `confirmation_required`, show
`wouldOverwrite`, ask, then retry with `confirm_overwrite: true` (or a new path).
- Long `bla_harden` runs may emit MCP `notifications/progress` when the client
sets `_meta.progressToken`. Cancel with `notifications/cancelled`
`{ requestId }` — already-written paths are listed on the cancelled report
(`cancelled: true`, `written: [...]`); there is no silent half-harden.
Same rule for `in_place: true` (overwrites the input).
- Report writes from the tool receipt (`writeReceipt` / `wouldOverwrite`), not guesses.
- After a successful harden/defend/protect, keep the returned unsigned `runId` (and
+4
View File
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- **`bla_harden` progress/cancel (issue #75):** cooperative `AbortSignal` + `onProgress` between artifacts; cancelled reports set `cancelled: true` and list every already-written path (no silent half-harden). MCP `notifications/progress` when `_meta.progressToken` is present; `notifications/cancelled` aborts in-flight harden immediately (stdio fast-path, not behind the serial queue).
## [2.1.0] — 2026-11-05
Minor release after 2.0.0: competitor-research absorptions that stay honest (seeded
+81 -7
View File
@@ -156,6 +156,29 @@ export interface HardenProjectOptions {
* (no `.bla-hardened/`, no summary JSON, no manifests).
*/
readonly dryRun?: boolean;
/**
* Cooperative cancel (issue #75). Checked between artifacts — already-written
* outputs are listed on the cancelled report; there is no silent half-harden.
*/
readonly signal?: AbortSignal;
/** Optional progress hook (index is 0-based into the planned artifact list). */
readonly onProgress?: (event: HardenProgressEvent) => void;
}
export type HardenProgressPhase =
| "start"
| "artifact"
| "cancelled"
| "done";
export interface HardenProgressEvent {
readonly phase: HardenProgressPhase;
/** 0-based index of the artifact about to run / just finished; -1 for start/done. */
readonly index: number;
readonly total: number;
readonly relativePath?: string;
/** Absolute paths already written successfully when this event fires. */
readonly written: readonly string[];
}
export interface HardenProjectReport {
@@ -201,6 +224,13 @@ export interface HardenProjectReport {
* self-coverage edges so packaging gates do not trust paper coverage.
*/
readonly integrityCoverageSelfVerified?: boolean;
/**
* True when `signal` aborted mid-run (issue #75). `written` lists every
* artifact already on disk — never a silent half-harden.
*/
readonly cancelled?: boolean;
/** Absolute output paths written before cancel or at completion. */
readonly written?: readonly string[];
}
export interface HardenDiscoveryResult {
@@ -624,7 +654,31 @@ export function hardenProject(
}
const summaries: HardenArtifactSummary[] = [];
for (const { artifact, outputPath, outputExisted } of plannedOutputs) {
const written: string[] = [];
const total = plannedOutputs.length;
const emitProgress = (
phase: HardenProgressPhase,
index: number,
relativePath?: string,
): void => {
options.onProgress?.({
phase,
index,
total,
...(relativePath !== undefined ? { relativePath } : {}),
written: written.slice(),
});
};
emitProgress("start", -1);
let cancelled = false;
for (let i = 0; i < plannedOutputs.length; i++) {
if (options.signal?.aborted) {
cancelled = true;
emitProgress("cancelled", i);
break;
}
const { artifact, outputPath, outputExisted } = plannedOutputs[i]!;
emitProgress("artifact", i, artifact.relativePath);
mkdirSync(dirname(outputPath), { recursive: true });
try {
const report: OneClickDefenseReport = defendTarget({
@@ -703,6 +757,9 @@ export function hardenProject(
report.exhaustionSummary.costAmplificationFactor,
outputExisted,
});
if (report.overallSuccess && existsSync(report.outputPath)) {
written.push(report.outputPath);
}
} catch (err) {
summaries.push({
inputPath: artifact.path,
@@ -736,6 +793,8 @@ export function hardenProject(
weaveDecoys: options.weaveDecoys === true,
selfSeal: options.selfSeal === true,
artifacts: summaries,
written,
...(cancelled ? { cancelled: true } : {}),
},
null,
2,
@@ -790,12 +849,17 @@ export function hardenProject(
const hardenedCount = summaries.filter((s) => s.success).length;
const failedCount = summaries.length - hardenedCount;
if (!cancelled) {
emitProgress("done", total > 0 ? total - 1 : -1);
}
return {
...reportBase,
overallSuccess: summaries.length > 0 && failedCount === 0,
overallSuccess: !cancelled && summaries.length > 0 && failedCount === 0,
hardenedCount,
failedCount,
artifacts: summaries,
written: written.slice(),
...(cancelled ? { cancelled: true } : {}),
...(summaryPath !== undefined ? { summaryPath } : {}),
...(integrityCoveragePath !== undefined
? { integrityCoveragePath }
@@ -816,13 +880,23 @@ export function formatHardenProjectReport(report: HardenProjectReport): string {
? report.discoveredCount === 0
? "NO ARTIFACTS"
: "DRY-RUN (nothing written)"
: report.overallSuccess
? "SUCCESS"
: report.discoveredCount === 0
? "NO ARTIFACTS"
: "PARTIAL/FAILED"
: report.cancelled
? "CANCELLED"
: report.overallSuccess
? "SUCCESS"
: report.discoveredCount === 0
? "NO ARTIFACTS"
: "PARTIAL/FAILED"
}`,
];
if (report.cancelled) {
lines.push(
` ⚠ cancelled after ${(report.written ?? []).length} written artifact(s); list is authoritative — no silent half-harden`,
);
for (const w of report.written ?? []) {
lines.push(` · written: ${w}`);
}
}
if (report.selfSealRecommended) {
lines.push(
` ⚠ self_seal recommended: ${report.selfSealRecommendation ?? "multi-artifact tree — pass --self-seal / self_seal:true"}`,
+2
View File
@@ -232,6 +232,8 @@ export {
formatHardenProjectReport,
BLA_BUILD_OUTPUT_DIR_NAMES,
type HardenProjectReport,
type HardenProgressEvent,
type HardenProgressPhase,
type HardenArtifactSummary,
type DiscoveredArtifact,
type HardenDiscoveryResult,
+150 -48
View File
@@ -87,6 +87,35 @@ import {
WEAVE_DIRECTIVES_URI,
} from "../shields/weave/weave-decoy.js";
/** In-flight bla_harden AbortControllers keyed by JSON-RPC request id (issue #75). */
const activeHardenAborts = new Map<string | number, AbortController>();
function progressTokenOf(req: JsonRpcRequest): string | number | undefined {
const params = req.params as { _meta?: { progressToken?: unknown } } | undefined;
const token = params?._meta?.progressToken;
if (typeof token === "string" || typeof token === "number") return token;
return undefined;
}
function emitMcpProgress(
token: string | number,
progress: number,
total: number,
message: string,
): void {
const note = {
jsonrpc: "2.0" as const,
method: "notifications/progress",
params: {
progressToken: token,
progress,
total,
message,
},
};
process.stdout.write(JSON.stringify(note) + "\n");
}
export interface JsonRpcRequest {
readonly jsonrpc: "2.0";
readonly id?: string | number | null;
@@ -1661,6 +1690,21 @@ export async function handleMcpRequest(
};
}
// Cancel must be honored even when the notification has no `id` (MCP spec)
// and must not wait behind a serial harden call (issue #75).
if (req.method === "notifications/cancelled") {
const params = (req.params ?? {}) as {
requestId?: string | number;
reason?: string;
};
const rid = params.requestId;
if (rid !== undefined && rid !== null) {
const ctl = activeHardenAborts.get(rid);
if (ctl) ctl.abort(params.reason ?? "cancelled");
}
return null;
}
// Notifications without an `id` do not produce a response
if (req.id === undefined) {
return null;
@@ -1950,57 +1994,95 @@ export async function handleMcpRequest(
payload = confirmationRequiredPayload(name, preOverwrite);
} else {
const wasmPreserveExports = stringListArg(args.wasm_preserve_exports);
const report = handlers.hardenProject({
directory: hardenDir,
dryRun,
...(maxArtifacts !== undefined ? { maxArtifacts } : {}),
...(outputDirectory !== undefined ? { outputDirectory } : {}),
intensity:
typeof args.intensity === "string"
? (args.intensity as ProtectionIntensity)
: "bankrupt",
emitManifest: Boolean(args.emit_manifest) || args.sign_manifest === true,
...(args.sign_manifest === true
? {
signManifest: true,
manifestSigningKeyPath:
typeof args.manifest_key_path === "string" ? args.manifest_key_path : "",
const abortCtl = new AbortController();
if (typeof req.id === "string" || typeof req.id === "number") {
activeHardenAborts.set(req.id, abortCtl);
}
const progressToken = progressTokenOf(req);
let report;
try {
report = handlers.hardenProject({
directory: hardenDir,
dryRun,
...(maxArtifacts !== undefined ? { maxArtifacts } : {}),
...(outputDirectory !== undefined ? { outputDirectory } : {}),
intensity:
typeof args.intensity === "string"
? (args.intensity as ProtectionIntensity)
: "bankrupt",
emitManifest: Boolean(args.emit_manifest) || args.sign_manifest === true,
...(args.sign_manifest === true
? {
signManifest: true,
manifestSigningKeyPath:
typeof args.manifest_key_path === "string" ? args.manifest_key_path : "",
}
: {}),
llmDirectives: Boolean(args.llm_directives),
weaveDecoys: Boolean(args.weave_decoys),
selfSeal: Boolean(args.self_seal),
...(wasmPreserveExports !== undefined ? { wasmPreserveExports } : {}),
...(args.allow_lane_independent === true
? { allowLaneIndependentGates: true }
: {}),
...(args.allow_plaintext_seal === true ? { allowPlaintextSeal: true } : {}),
...(args.allow_wasm_clear_params === true
? { allowWasmClearParamCarriers: true }
: {}),
...(args.allow_stable_panel_derive === true
? { allowStablePanelDeriveAbi: true }
: {}),
...(args.allow_js_aes_unwrap === true ? { allowJsAesUnwrap: true } : {}),
...(args.allow_panel_offline_oracle === true
? { allowPanelOfflineOracle: true }
: {}),
...(args.allow_js_wasm_hydrate_dump === true
? { allowJsWasmHydrateDump: true }
: {}),
...(args.allow_wasm_xor_mask_sidecar === true
? { allowWasmXorMaskSidecar: true }
: {}),
...(args.allow_wasm_seal_xor_known_plaintext === true
? { allowWasmSealXorKnownPlaintext: true }
: {}),
...(typeof args.seed === "string" ? { seed: args.seed } : {}),
signal: abortCtl.signal,
onProgress: (ev) => {
if (progressToken === undefined) return;
if (ev.phase === "artifact") {
emitMcpProgress(
progressToken,
ev.index + 1,
ev.total,
ev.relativePath ?? `artifact ${ev.index + 1}/${ev.total}`,
);
} else if (ev.phase === "cancelled") {
emitMcpProgress(
progressToken,
ev.written.length,
ev.total,
`cancelled after ${ev.written.length} written`,
);
} else if (ev.phase === "done") {
emitMcpProgress(
progressToken,
ev.total,
ev.total,
"done",
);
}
: {}),
llmDirectives: Boolean(args.llm_directives),
weaveDecoys: Boolean(args.weave_decoys),
selfSeal: Boolean(args.self_seal),
...(wasmPreserveExports !== undefined ? { wasmPreserveExports } : {}),
...(args.allow_lane_independent === true
? { allowLaneIndependentGates: true }
: {}),
...(args.allow_plaintext_seal === true ? { allowPlaintextSeal: true } : {}),
...(args.allow_wasm_clear_params === true
? { allowWasmClearParamCarriers: true }
: {}),
...(args.allow_stable_panel_derive === true
? { allowStablePanelDeriveAbi: true }
: {}),
...(args.allow_js_aes_unwrap === true ? { allowJsAesUnwrap: true } : {}),
...(args.allow_panel_offline_oracle === true
? { allowPanelOfflineOracle: true }
: {}),
...(args.allow_js_wasm_hydrate_dump === true
? { allowJsWasmHydrateDump: true }
: {}),
...(args.allow_wasm_xor_mask_sidecar === true
? { allowWasmXorMaskSidecar: true }
: {}),
...(args.allow_wasm_seal_xor_known_plaintext === true
? { allowWasmSealXorKnownPlaintext: true }
: {}),
...(typeof args.seed === "string" ? { seed: args.seed } : {}),
});
},
});
} finally {
if (typeof req.id === "string" || typeof req.id === "number") {
activeHardenAborts.delete(req.id);
}
}
// Dry-run / confirmation_required leave no write receipt; real writes do.
if (!dryRun) {
const wrote = report.artifacts
const wrote = (report.written ?? report.artifacts
.filter((a) => a.success)
.map((a) => a.outputPath);
.map((a) => a.outputPath)).slice();
if (report.summaryPath !== undefined) {
wrote.push(report.summaryPath);
}
@@ -2403,9 +2485,29 @@ export function startMcpStdioServer(): void {
let processingQueue = Promise.resolve();
const processor = createStdioLineProcessor({
onLine: (line: string) => {
// Parse first so cancel can abort an in-flight harden without waiting
// for the serial request queue (issue #75).
let req: JsonRpcRequest;
try {
req = JSON.parse(line) as JsonRpcRequest;
} catch {
const parseErr: JsonRpcResponse = {
jsonrpc: "2.0",
id: null,
error: {
code: -32700,
message: "Parse error",
},
};
process.stdout.write(JSON.stringify(parseErr) + "\n");
return;
}
if (req.method === "notifications/cancelled") {
void handleMcpRequest(req);
return;
}
processingQueue = processingQueue.then(async () => {
try {
const req = JSON.parse(line) as JsonRpcRequest;
const res = await handleMcpRequest(req);
if (res !== null) {
process.stdout.write(JSON.stringify(res) + "\n");
+183
View File
@@ -0,0 +1,183 @@
import {
existsSync,
mkdirSync,
mkdtempSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { initBlaConfig } from "../src/core/config.js";
import { inspectReleaseExposure } from "../src/core/release-exposure.js";
import { auditTarget } from "../src/engine/auditor.js";
import { runShowcaseBenchmark } from "../src/engine/benchmark.js";
import { defendTarget } from "../src/engine/defender.js";
import {
formatHardenProjectReport,
hardenProject,
type HardenProgressEvent,
} from "../src/engine/harden.js";
import { protectTarget } from "../src/engine/protector.js";
import { verifyTarget } from "../src/engine/verifier.js";
import { emitCompileTimeNativeHeader } from "../src/shields/formats/headers.js";
import { runDoctor } from "../src/setup/doctor.js";
import {
handleMcpRequest,
type McpEngineHandlers,
} from "../src/mcp/server.js";
const tempDirs: string[] = [];
function createTempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "bla-harden-cancel-"));
tempDirs.push(dir);
return dir;
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
function writeTinyJsTree(root: string, names: string[]): void {
const dist = join(root, "dist");
mkdirSync(dist, { recursive: true });
for (const name of names) {
writeFileSync(
join(dist, name),
`export function probe_${name.replace(/\W/g, "_")}(x){return x===1}\n`,
"utf8",
);
}
}
function baseHandlers(
hardenOverride?: McpEngineHandlers["hardenProject"],
): McpEngineHandlers {
return {
hardenProject: hardenOverride ?? hardenProject,
defendTarget,
auditTarget,
protectTarget,
verifyTarget,
runShowcaseBenchmark,
emitCompileTimeNativeHeader,
runDoctor,
inspectReleaseExposure,
initBlaConfig,
};
}
describe("bla_harden progress + cooperative cancel (#75)", () => {
it("emits start/artifact/done progress and lists written on success", () => {
const root = createTempDir();
writeTinyJsTree(root, ["a.js", "b.js"]);
const events: HardenProgressEvent[] = [];
const report = hardenProject({
directory: root,
intensity: "bankrupt",
seed: "progress-ok",
onProgress: (ev) => events.push(ev),
});
expect(report.cancelled).toBeUndefined();
expect(report.overallSuccess).toBe(true);
expect(report.written?.length).toBe(2);
expect(events[0]?.phase).toBe("start");
expect(events.some((e) => e.phase === "artifact")).toBe(true);
expect(events.at(-1)?.phase).toBe("done");
expect(events.at(-1)?.written.length).toBe(2);
});
it("abort mid-run sets cancelled and lists only already-written paths", () => {
const root = createTempDir();
writeTinyJsTree(root, ["one.js", "two.js", "three.js"]);
const ctl = new AbortController();
const events: HardenProgressEvent[] = [];
const report = hardenProject({
directory: root,
intensity: "bankrupt",
seed: "progress-cancel",
signal: ctl.signal,
onProgress: (ev) => {
events.push(ev);
if (ev.phase === "artifact" && ev.index === 0) ctl.abort("test-cancel");
},
});
expect(report.cancelled).toBe(true);
expect(report.overallSuccess).toBe(false);
expect(report.written?.length).toBe(1);
expect(existsSync(report.written![0]!)).toBe(true);
const outRoot = report.outputDirectory;
expect(existsSync(join(outRoot, "dist", "two.js"))).toBe(false);
expect(existsSync(join(outRoot, "dist", "three.js"))).toBe(false);
expect(events.some((e) => e.phase === "cancelled")).toBe(true);
const text = formatHardenProjectReport(report);
expect(text).toContain("CANCELLED");
expect(text).toContain("written:");
});
it("MCP notifications/cancelled aborts in-flight bla_harden via requestId", async () => {
const root = createTempDir();
writeTinyJsTree(root, ["alpha.js", "beta.js"]);
const reqId = "harden-cancel-1";
const handlers = baseHandlers((opts) =>
hardenProject({
...opts,
onProgress: (ev) => {
opts.onProgress?.(ev);
if (ev.phase === "artifact" && ev.index === 0) {
void handleMcpRequest({
jsonrpc: "2.0",
method: "notifications/cancelled",
params: { requestId: reqId, reason: "client-cancel" },
});
}
},
}),
);
const res = await handleMcpRequest(
{
jsonrpc: "2.0",
id: reqId,
method: "tools/call",
params: {
name: "bla_harden",
arguments: {
directory: root,
intensity: "bankrupt",
seed: "mcp-cancel",
confirm_overwrite: true,
},
_meta: { progressToken: "tok-1" },
},
},
handlers,
);
expect(res).not.toBeNull();
const structured = (
res as {
result: {
structuredContent: {
cancelled?: boolean;
written?: string[];
overallSuccess: boolean;
};
};
}
).result.structuredContent;
expect(structured.cancelled).toBe(true);
expect(structured.overallSuccess).toBe(false);
expect(structured.written?.length).toBe(1);
});
it("notifications/cancelled without id returns null and is a no-op when idle", async () => {
const res = await handleMcpRequest({
jsonrpc: "2.0",
method: "notifications/cancelled",
params: { requestId: "nobody" },
});
expect(res).toBeNull();
});
});