π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
feat(watermark): @claude-flow/watermark npm package (WASM bindings) (#3040)
rUv72fb1293972b
diff --git a/v3/@claude-flow/watermark/README.md b/v3/@claude-flow/watermark/README.mdnew file mode 100644index 0000000000..be4f993bea--- /dev/null+++ b/v3/@claude-flow/watermark/README.md@@ -0,0 +1,73 @@+# @claude-flow/watermark++SynthID-Text-style **LLM text watermarking** (generation + detection) as a+WebAssembly module β no native addon, runs in Node. A watermark rides the+tie-break randomness among *already-plausible* tokens: it never injects an+out-of-distribution word, costs no extra tokens, and is detectable only by a+holder of the secret key. Built for EU AI Act AI-content marking and for+measuring watermark robustness.++Backed by the Rust crate [`ruflo-watermark`](https://github.com/ruvnet/ruflo/tree/main/v3/crates/ruflo-watermark).++> This package intentionally ships **no watermark-removal / laundering tool**.+> The legitimate way to produce un-marked output of your own model is not to+> apply the mark (or to regenerate) β see the crate's `unmarked.rs`.++## Install++```bash+npm install @claude-flow/watermark+```++## Use++The module is model-agnostic: at each step you pass the model's candidate token+ids and their probabilities (typically a top-k slice); the watermarker picks+which candidate to emit.++```js+const { Watermarker, detect } = require('@claude-flow/watermark');++const key = '8F3A91C7'; // secret key material (carries no user info)++// Generate a watermarked sequence.+const wm = new Watermarker({ key, scheme: 'gumbel' });+const tokens = Uint32Array.from({ length: 128 }, (_, i) => i);+const probs = new Float32Array(128).fill(1 / 128);+const out = new Uint32Array(600);+for (let i = 0; i < out.length; i++) out[i] = tokens[wm.step(tokens, probs)];+wm.free();++// Detect it.+const r = detect(out, { key, scheme: 'gumbel' });+console.log(r.zScore, r.log10P, r.isWatermarked(1e-6)); // strong, true+```++## Schemes++| `scheme` | Property |+|---|---|+| `gumbel` (default) | Aaronson/Kuditipudi exponential-min β **per-instance distortion-free** (marginal token distribution unchanged). |+| `tournament` | SynthID-Text tournament (Nature 2024) β strong, mildly distortionary; strength grows with `layers`. |+| `tournament_nd` | Tournament with continuous g + masking β **key-averaged non-distortionary** (measured < 0.3% drift). |++## API++- `new Watermarker({ key, scheme?, contextWidth?, layers? })` β `.step(tokens, probs)`, `.free()`+- `detect(tokens, { key, scheme?, contextWidth?, layers? })` β `Detection`+- `detectSelfSync(tokens, { key, contextWidth? })` β indel-robust (stronger on edited / repetitive text)+- `detectExact(tokens, { key, contextWidth? })` β exact Gamma-tail p-values for short texts++`Detection` = `{ zScore, pValue, log10P, scoredPositions, isWatermarked(alpha = 1e-6) }`.+`key` may be a string (UTF-8) or `Uint8Array`. Detection is key-specific: a wrong+key sees nothing. Confidence grows with the number of low-stakes token choices,+so short or low-entropy (factual/code) text carries little to no mark.++## Scope++This build targets Node (CommonJS). The bindings are generated from the Rust+crate with `npm run build:wasm` (`wasm-pack --target nodejs`); browser/bundler+targets can be produced the same way (`--target web`/`bundler`). The crate+exposes more than this WASM surface β the Bayesian/Higher-Criticism detectors,+the robustness-evaluation harness, the Darwin/flywheel detector tuner, and the+authorized un-marked-generation governance path are Rust-only for now.diff --git a/v3/@claude-flow/watermark/index.d.ts b/v3/@claude-flow/watermark/index.d.tsnew file mode 100644index 0000000000..95442983e6--- /dev/null+++ b/v3/@claude-flow/watermark/index.d.ts@@ -0,0 +1,53 @@+// Type definitions for @claude-flow/watermark.++export type Scheme = 'tournament' | 'tournament_nd' | 'gumbel';++export interface Detection {+ /** Standardized deviation from the null. Larger => stronger evidence. */+ zScore: number;+ /** Upper-tail p-value under the null (floors at 0 for extreme z). */+ pValue: number;+ /** log10(pValue), stable even when pValue underflows to 0. */+ log10P: number;+ /** Number of watermarked positions that were scored. */+ scoredPositions: number;+ /** True if the evidence clears the given false-positive rate (default 1e-6). */+ isWatermarked(alpha?: number): boolean;+}++export interface WatermarkerOptions {+ /** Secret key material. Carries no user-identifying information. */+ key: string | Uint8Array;+ /** Scheme (default `gumbel`, the distortion-free option). */+ scheme?: Scheme;+ /** Context window H β preceding tokens seeding each draw (default 4). */+ contextWidth?: number;+ /** Tournament depth (default 6; ignored by `gumbel`). */+ layers?: number;+}++export interface DetectOptions extends WatermarkerOptions {}+export interface SelfSyncOptions {+ key: string | Uint8Array;+ contextWidth?: number;+}++/** Streaming watermarked sampler. */+export class Watermarker {+ constructor(opts: WatermarkerOptions);+ /** Emit one token; returns the index of the chosen candidate. */+ step(tokens: Uint32Array | number[], probs: Float32Array | number[]): number;+ /** Release the WASM instance. */+ free(): void;+}++/** Detect a watermark over an emitted token-id sequence. */+export function detect(tokens: Uint32Array | number[], opts: DetectOptions): Detection;++/** Indel-robust detection (Gumbel self-sync) β stronger on edited/repetitive text. */+export function detectSelfSync(tokens: Uint32Array | number[], opts: SelfSyncOptions): Detection;++/** Exact-null short-text detection (Gumbel, exact Gamma tail). */+export function detectExact(tokens: Uint32Array | number[], opts: SelfSyncOptions): Detection;++export const SCHEMES: ReadonlySet<Scheme>;diff --git a/v3/@claude-flow/watermark/index.js b/v3/@claude-flow/watermark/index.jsnew file mode 100644index 0000000000..0a3311fe7d--- /dev/null+++ b/v3/@claude-flow/watermark/index.js@@ -0,0 +1,87 @@+// @claude-flow/watermark β ergonomic wrapper over the ruflo-watermark WASM core.+//+// SynthID-Text-style LLM text watermarking (generation + detection). The+// watermark rides the tie-break randomness among plausible tokens β it never+// injects an out-of-distribution word, costs no extra tokens, and is detectable+// only with the key. This package deliberately does NOT include a watermark+// remover / laundering tool.+'use strict';++const wasm = require('./wasm/ruflo_watermark.js');++const SCHEMES = new Set(['tournament', 'tournament_nd', 'gumbel']);++function toKeyBytes(key) {+ if (typeof key === 'string') return new TextEncoder().encode(key);+ if (key instanceof Uint8Array) return key;+ throw new TypeError('key must be a string or Uint8Array');+}+function toU32(a) {+ return a instanceof Uint32Array ? a : Uint32Array.from(a);+}+function toF32(a) {+ return a instanceof Float32Array ? a : Float32Array.from(a);+}+function normScheme(scheme) {+ const s = scheme || 'gumbel';+ if (!SCHEMES.has(s)) throw new RangeError(`unknown scheme "${s}" (use tournament | tournament_nd | gumbel)`);+ return s;+}++/** Shape a raw WasmDetection into a plain object with an `isWatermarked` helper. */+function shape(r) {+ const out = {+ zScore: r.z_score,+ pValue: r.p_value,+ log10P: r.log10_p,+ scoredPositions: r.scored_positions,+ /** True if the evidence clears the given false-positive rate (e.g. 1e-6). */+ isWatermarked(alpha = 1e-6) {+ return out.log10P <= Math.log10(alpha);+ },+ };+ r.free();+ return out;+}++/**+ * Streaming watermarked sampler. Hold one per generated sequence; feed it the+ * model's candidate token ids and their probabilities per step.+ */+class Watermarker {+ /**+ * @param {object} opts+ * @param {string|Uint8Array} opts.key secret key material (carries no user info)+ * @param {'tournament'|'tournament_nd'|'gumbel'} [opts.scheme='gumbel']+ * @param {number} [opts.contextWidth=4] H β preceding tokens seeding each draw+ * @param {number} [opts.layers=6] tournament depth (ignored by gumbel)+ */+ constructor({ key, scheme = 'gumbel', contextWidth = 4, layers = 6 } = {}) {+ this._inner = new wasm.WasmWatermarker(toKeyBytes(key), contextWidth, layers, normScheme(scheme));+ }+ /** Emit one token: returns the index of the chosen candidate. */+ step(tokens, probs) {+ return this._inner.step(toU32(tokens), toF32(probs));+ }+ /** Release the WASM instance. */+ free() {+ this._inner.free();+ }+}++/** Detect a watermark over an emitted token-id sequence using the named scheme. */+function detect(tokens, { key, scheme = 'gumbel', contextWidth = 4, layers = 6 } = {}) {+ return shape(wasm.detect(toU32(tokens), toKeyBytes(key), contextWidth, layers, normScheme(scheme)));+}++/** Indel-robust detection (Gumbel self-sync) β stronger on edited/repetitive text. */+function detectSelfSync(tokens, { key, contextWidth = 4 } = {}) {+ return shape(wasm.detect_selfsync(toU32(tokens), toKeyBytes(key), contextWidth));+}++/** Exact-null short-text detection (Gumbel, exact Gamma tail). */+function detectExact(tokens, { key, contextWidth = 4 } = {}) {+ return shape(wasm.detect_exact(toU32(tokens), toKeyBytes(key), contextWidth));+}++module.exports = { Watermarker, detect, detectSelfSync, detectExact, SCHEMES };diff --git a/v3/@claude-flow/watermark/package.json b/v3/@claude-flow/watermark/package.jsonnew file mode 100644index 0000000000..4d45f0bca3--- /dev/null+++ b/v3/@claude-flow/watermark/package.json@@ -0,0 +1,46 @@+{+ "name": "@claude-flow/watermark",+ "version": "0.1.0",+ "description": "SynthID-Text-style LLM text watermarking (generation + detection) as a WASM module β Tournament / non-distortionary / distortion-free schemes, indel-robust and short-text detectors. No watermark-removal tooling.",+ "license": "MIT",+ "author": "rUv <[email protected]>",+ "homepage": "https://github.com/ruvnet/ruflo/tree/main/v3/crates/ruflo-watermark",+ "repository": {+ "type": "git",+ "url": "git+https://github.com/ruvnet/ruflo.git",+ "directory": "v3/@claude-flow/watermark"+ },+ "bugs": {+ "url": "https://github.com/ruvnet/ruflo/issues"+ },+ "keywords": [+ "watermark",+ "synthid",+ "llm",+ "provenance",+ "eu-ai-act",+ "wasm"+ ],+ "type": "commonjs",+ "main": "index.js",+ "types": "index.d.ts",+ "files": [+ "index.js",+ "index.d.ts",+ "wasm/ruflo_watermark.js",+ "wasm/ruflo_watermark_bg.wasm",+ "wasm/ruflo_watermark.d.ts",+ "wasm/ruflo_watermark_bg.wasm.d.ts",+ "README.md"+ ],+ "engines": {+ "node": ">=18"+ },+ "publishConfig": {+ "access": "public"+ },+ "scripts": {+ "build:wasm": "bash scripts/build-wasm.sh",+ "test": "node test/smoke.cjs"+ }+}diff --git a/v3/@claude-flow/watermark/scripts/build-wasm.sh b/v3/@claude-flow/watermark/scripts/build-wasm.shnew file mode 100755index 0000000000..dce87cb8a9--- /dev/null+++ b/v3/@claude-flow/watermark/scripts/build-wasm.sh@@ -0,0 +1,26 @@+#!/usr/bin/env bash+# Regenerate the WASM bindings in ./wasm from the ruflo-watermark Rust crate.+#+# Requires: rustup + wasm32-unknown-unknown target + wasm-pack.+# rustup target add wasm32-unknown-unknown+# cargo install wasm-pack+#+# RUSTFLAGS is cleared for the wasm target because a machine-global+# `-C link-arg=-fuse-ld=mold` (if present) is rejected by wasm's rust-lld.+set -euo pipefail++here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"+crate="$here/../../crates/ruflo-watermark"++CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS="" RUSTFLAGS="" \+ wasm-pack build --release --target nodejs \+ --out-dir "$crate/pkg-nodejs" --out-name ruflo_watermark \+ "$crate" -- --features wasm++cp "$crate/pkg-nodejs/ruflo_watermark.js" \+ "$crate/pkg-nodejs/ruflo_watermark_bg.wasm" \+ "$crate/pkg-nodejs/ruflo_watermark.d.ts" \+ "$crate/pkg-nodejs/ruflo_watermark_bg.wasm.d.ts" \+ "$here/wasm/"++echo "Rebuilt wasm/ from $crate"diff --git a/v3/@claude-flow/watermark/test/smoke.cjs b/v3/@claude-flow/watermark/test/smoke.cjsnew file mode 100644index 0000000000..6dc360364e--- /dev/null+++ b/v3/@claude-flow/watermark/test/smoke.cjs@@ -0,0 +1,49 @@+// Smoke test: generate a watermarked stream with each scheme and confirm the+// detector flags it under the right key, rejects the wrong key, and does not+// fire on a null stream. Exercises the specialized detectors too.+'use strict';++const { Watermarker, detect, detectSelfSync, detectExact } = require('..');++const KEY = '8F3A91C7';+const VOCAB = 128;+const tokens = Uint32Array.from({ length: VOCAB }, (_, i) => i);+const probs = new Float32Array(VOCAB).fill(1 / VOCAB);++function generate(scheme, n = 600) {+ const wm = new Watermarker({ key: KEY, scheme, contextWidth: 4, layers: 6 });+ const out = new Uint32Array(n);+ for (let i = 0; i < n; i++) out[i] = tokens[wm.step(tokens, probs)];+ wm.free();+ return out;+}++let ok = true;+function check(name, cond, detail) {+ if (!cond) {+ ok = false;+ console.error(` FAIL: ${name} β ${detail}`);+ } else {+ console.log(` ok: ${name} β ${detail}`);+ }+}++for (const scheme of ['tournament', 'tournament_nd', 'gumbel']) {+ const stream = generate(scheme);+ const hit = detect(stream, { key: KEY, scheme, layers: 6 });+ const wrong = detect(stream, { key: 'WRONGKEY', scheme, layers: 6 });+ const nul = detect(Uint32Array.from({ length: 600 }, (_, i) => (i * 2654435761 >>> 0) % VOCAB), { key: KEY, scheme, layers: 6 });+ check(`${scheme}: detects own output`, hit.isWatermarked(1e-6), `z=${hit.zScore.toFixed(1)}`);+ check(`${scheme}: wrong key not flagged`, !wrong.isWatermarked(1e-6), `z=${wrong.zScore.toFixed(2)}`);+ check(`${scheme}: null not flagged`, !nul.isWatermarked(1e-6), `z=${nul.zScore.toFixed(2)}`);+}++// Specialized detectors on a Gumbel stream.+const g = generate('gumbel');+const ss = detectSelfSync(g, { key: KEY });+const ex = detectExact(g.slice(0, 120), { key: KEY });+check('self-sync detector fires', ss.zScore > 8, `z=${ss.zScore.toFixed(1)}`);+check('exact short-text detector fires', ex.log10P < -3, `log10p=${ex.log10P.toFixed(1)}`);++console.log(ok ? '\nSMOKE OK' : '\nSMOKE FAILED');+process.exit(ok ? 0 : 1);diff --git a/v3/@claude-flow/watermark/wasm/ruflo_watermark.d.ts b/v3/@claude-flow/watermark/wasm/ruflo_watermark.d.tsnew file mode 100644index 0000000000..92ce30ab4b--- /dev/null+++ b/v3/@claude-flow/watermark/wasm/ruflo_watermark.d.ts@@ -0,0 +1,50 @@+/* tslint:disable */+/* eslint-disable */++/**+ * Detection result, JS-facing (fields via getters).+ */+export class WasmDetection {+ private constructor();+ free(): void;+ [Symbol.dispose](): void;+ readonly log10_p: number;+ readonly p_value: number;+ readonly scored_positions: number;+ readonly z_score: number;+}++/**+ * Streaming watermarked sampler, JS-facing.+ */+export class WasmWatermarker {+ free(): void;+ [Symbol.dispose](): void;+ /**+ * `key_material`: arbitrary secret bytes (e.g. a hex string's bytes).+ * `scheme`: `"tournament"` | `"tournament_nd"` | `"gumbel"` (default gumbel).+ */+ constructor(key_material: Uint8Array, context_width: number, layers: number, scheme: string);+ /**+ * Emit one token: returns the index into `tokens`/`probs` of the chosen+ * candidate. Advances the rolling context.+ */+ step(tokens: Uint32Array, probs: Float32Array): number;+}++/**+ * Detect a watermark over an emitted token id sequence, using the named scheme.+ */+export function detect(tokens: Uint32Array, key_material: Uint8Array, context_width: number, layers: number, scheme: string): WasmDetection;++/**+ * Exact-null short-text detection (Gumbel, exact Gamma tail): correct p-values+ * at small token counts where the normal approximation misleads. See `bayes.rs`.+ */+export function detect_exact(tokens: Uint32Array, key_material: Uint8Array, context_width: number): WasmDetection;++/**+ * Indel-robust detection (Gumbel self-sync): far stronger than the standard+ * detector on edited / repetitive text. See `align.rs`.+ */+export function detect_selfsync(tokens: Uint32Array, key_material: Uint8Array, context_width: number): WasmDetection;diff --git a/v3/@claude-flow/watermark/wasm/ruflo_watermark.js b/v3/@claude-flow/watermark/wasm/ruflo_watermark.jsnew file mode 100644index 0000000000..ae48f69473--- /dev/null+++ b/v3/@claude-flow/watermark/wasm/ruflo_watermark.js@@ -0,0 +1,303 @@+/* @ts-self-types="./ruflo_watermark.d.ts" */++/**+ * Detection result, JS-facing (fields via getters).+ */+class WasmDetection {+ static __wrap(ptr) {+ const obj = Object.create(WasmDetection.prototype);+ obj.__wbg_ptr = ptr;+ WasmDetectionFinalization.register(obj, obj.__wbg_ptr, obj);+ return obj;+ }+ __destroy_into_raw() {+ const ptr = this.__wbg_ptr;+ this.__wbg_ptr = 0;+ WasmDetectionFinalization.unregister(this);+ return ptr;+ }+ free() {+ const ptr = this.__destroy_into_raw();+ wasm.__wbg_wasmdetection_free(ptr, 0);+ }+ /**+ * @returns {number}+ */+ get log10_p() {+ const ret = wasm.wasmdetection_log10_p(this.__wbg_ptr);+ return ret;+ }+ /**+ * @returns {number}+ */+ get p_value() {+ const ret = wasm.wasmdetection_p_value(this.__wbg_ptr);+ return ret;+ }+ /**+ * @returns {number}+ */+ get scored_positions() {+ const ret = wasm.wasmdetection_scored_positions(this.__wbg_ptr);+ return ret >>> 0;+ }+ /**+ * @returns {number}+ */+ get z_score() {+ const ret = wasm.wasmdetection_z_score(this.__wbg_ptr);+ return ret;+ }+}+if (Symbol.dispose) WasmDetection.prototype[Symbol.dispose] = WasmDetection.prototype.free;+exports.WasmDetection = WasmDetection;++/**+ * Streaming watermarked sampler, JS-facing.+ */+class WasmWatermarker {+ __destroy_into_raw() {+ const ptr = this.__wbg_ptr;+ this.__wbg_ptr = 0;+ WasmWatermarkerFinalization.unregister(this);+ return ptr;+ }+ free() {+ const ptr = this.__destroy_into_raw();+ wasm.__wbg_wasmwatermarker_free(ptr, 0);+ }+ /**+ * `key_material`: arbitrary secret bytes (e.g. a hex string's bytes).+ * `scheme`: `"tournament"` | `"tournament_nd"` | `"gumbel"` (default gumbel).+ * @param {Uint8Array} key_material+ * @param {number} context_width+ * @param {number} layers+ * @param {string} scheme+ */+ constructor(key_material, context_width, layers, scheme) {+ const ptr0 = passArray8ToWasm0(key_material, wasm.__wbindgen_malloc);+ const len0 = WASM_VECTOR_LEN;+ const ptr1 = passStringToWasm0(scheme, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);+ const len1 = WASM_VECTOR_LEN;+ const ret = wasm.wasmwatermarker_new(ptr0, len0, context_width, layers, ptr1, len1);+ this.__wbg_ptr = ret;+ WasmWatermarkerFinalization.register(this, this.__wbg_ptr, this);+ return this;+ }+ /**+ * Emit one token: returns the index into `tokens`/`probs` of the chosen+ * candidate. Advances the rolling context.+ * @param {Uint32Array} tokens+ * @param {Float32Array} probs+ * @returns {number}+ */+ step(tokens, probs) {+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_malloc);+ const len0 = WASM_VECTOR_LEN;+ const ptr1 = passArrayF32ToWasm0(probs, wasm.__wbindgen_malloc);+ const len1 = WASM_VECTOR_LEN;+ const ret = wasm.wasmwatermarker_step(this.__wbg_ptr, ptr0, len0, ptr1, len1);+ return ret >>> 0;+ }+}+if (Symbol.dispose) WasmWatermarker.prototype[Symbol.dispose] = WasmWatermarker.prototype.free;+exports.WasmWatermarker = WasmWatermarker;++/**+ * Detect a watermark over an emitted token id sequence, using the named scheme.+ * @param {Uint32Array} tokens+ * @param {Uint8Array} key_material+ * @param {number} context_width+ * @param {number} layers+ * @param {string} scheme+ * @returns {WasmDetection}+ */+function detect(tokens, key_material, context_width, layers, scheme) {+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_malloc);+ const len0 = WASM_VECTOR_LEN;+ const ptr1 = passArray8ToWasm0(key_material, wasm.__wbindgen_malloc);+ const len1 = WASM_VECTOR_LEN;+ const ptr2 = passStringToWasm0(scheme, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);+ const len2 = WASM_VECTOR_LEN;+ const ret = wasm.detect(ptr0, len0, ptr1, len1, context_width, layers, ptr2, len2);+ return WasmDetection.__wrap(ret);+}+exports.detect = detect;++/**+ * Exact-null short-text detection (Gumbel, exact Gamma tail): correct p-values+ * at small token counts where the normal approximation misleads. See `bayes.rs`.+ * @param {Uint32Array} tokens+ * @param {Uint8Array} key_material+ * @param {number} context_width+ * @returns {WasmDetection}+ */+function detect_exact(tokens, key_material, context_width) {+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_malloc);+ const len0 = WASM_VECTOR_LEN;+ const ptr1 = passArray8ToWasm0(key_material, wasm.__wbindgen_malloc);+ const len1 = WASM_VECTOR_LEN;+ const ret = wasm.detect_exact(ptr0, len0, ptr1, len1, context_width);+ return WasmDetection.__wrap(ret);+}+exports.detect_exact = detect_exact;++/**+ * Indel-robust detection (Gumbel self-sync): far stronger than the standard+ * detector on edited / repetitive text. See `align.rs`.+ * @param {Uint32Array} tokens+ * @param {Uint8Array} key_material+ * @param {number} context_width+ * @returns {WasmDetection}+ */+function detect_selfsync(tokens, key_material, context_width) {+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_malloc);+ const len0 = WASM_VECTOR_LEN;+ const ptr1 = passArray8ToWasm0(key_material, wasm.__wbindgen_malloc);+ const len1 = WASM_VECTOR_LEN;+ const ret = wasm.detect_selfsync(ptr0, len0, ptr1, len1, context_width);+ return WasmDetection.__wrap(ret);+}+exports.detect_selfsync = detect_selfsync;+function __wbg_get_imports() {+ const import0 = {+ __proto__: null,+ __wbg___wbindgen_throw_bb96b2010945f0bc: function(arg0, arg1) {+ throw new Error(getStringFromWasm0(arg0, arg1));+ },+ __wbindgen_init_externref_table: function() {+ const table = wasm.__wbindgen_externrefs;+ const offset = table.grow(4);+ table.set(0, undefined);+ table.set(offset + 0, undefined);+ table.set(offset + 1, null);+ table.set(offset + 2, true);+ table.set(offset + 3, false);+ },+ };+ return {+ __proto__: null,+ "./ruflo_watermark_bg.js": import0,+ };+}++const WasmDetectionFinalization = (typeof FinalizationRegistry === 'undefined')+ ? { register: () => {}, unregister: () => {} }+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmdetection_free(ptr, 1));+const WasmWatermarkerFinalization = (typeof FinalizationRegistry === 'undefined')+ ? { register: () => {}, unregister: () => {} }+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmwatermarker_free(ptr, 1));++let cachedFloat32ArrayMemory0 = null;+function getFloat32ArrayMemory0() {+ if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) {+ cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer);+ }+ return cachedFloat32ArrayMemory0;+}++function getStringFromWasm0(ptr, len) {+ return decodeText(ptr >>> 0, len);+}++let cachedUint32ArrayMemory0 = null;+function getUint32ArrayMemory0() {+ if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {+ cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);+ }+ return cachedUint32ArrayMemory0;+}++let cachedUint8ArrayMemory0 = null;+function getUint8ArrayMemory0() {+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);+ }+ return cachedUint8ArrayMemory0;+}++function passArray32ToWasm0(arg, malloc) {+ const ptr = malloc(arg.length * 4, 4) >>> 0;+ getUint32ArrayMemory0().set(arg, ptr / 4);+ WASM_VECTOR_LEN = arg.length;+ return ptr;+}++function passArray8ToWasm0(arg, malloc) {+ const ptr = malloc(arg.length * 1, 1) >>> 0;+ getUint8ArrayMemory0().set(arg, ptr / 1);+ WASM_VECTOR_LEN = arg.length;+ return ptr;+}++function passArrayF32ToWasm0(arg, malloc) {+ const ptr = malloc(arg.length * 4, 4) >>> 0;+ getFloat32ArrayMemory0().set(arg, ptr / 4);+ WASM_VECTOR_LEN = arg.length;+ return ptr;+}++function passStringToWasm0(arg, malloc, realloc) {+ if (realloc === undefined) {+ const buf = cachedTextEncoder.encode(arg);+ const ptr = malloc(buf.length, 1) >>> 0;+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);+ WASM_VECTOR_LEN = buf.length;+ return ptr;+ }++ let len = arg.length;+ let ptr = malloc(len, 1) >>> 0;++ const mem = getUint8ArrayMemory0();++ let offset = 0;++ for (; offset < len; offset++) {+ const code = arg.charCodeAt(offset);+ if (code > 0x7F) break;+ mem[ptr + offset] = code;+ }+ if (offset !== len) {+ if (offset !== 0) {+ arg = arg.slice(offset);+ }+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);+ const ret = cachedTextEncoder.encodeInto(arg, view);++ offset += ret.written;+ ptr = realloc(ptr, len, offset, 1) >>> 0;+ }++ WASM_VECTOR_LEN = offset;+ return ptr;+}++let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });+cachedTextDecoder.decode();+function decodeText(ptr, len) {+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));+}++const cachedTextEncoder = new TextEncoder();++if (!('encodeInto' in cachedTextEncoder)) {+ cachedTextEncoder.encodeInto = function (arg, view) {+ const buf = cachedTextEncoder.encode(arg);+ view.set(buf);+ return {+ read: arg.length,+ written: buf.length+ };+ };+}++let WASM_VECTOR_LEN = 0;++const wasmPath = `${__dirname}/ruflo_watermark_bg.wasm`;+const wasmBytes = require('fs').readFileSync(wasmPath);+const wasmModule = new WebAssembly.Module(wasmBytes);+let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());+let wasm = wasmInstance.exports;+wasm.__wbindgen_start();diff --git a/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasm b/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasmnew file mode 100644index 0000000000..14e572235dBinary files /dev/null and b/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasm differdiff --git a/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasm.d.ts b/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasm.d.tsnew file mode 100644index 0000000000..ef4372a905--- /dev/null+++ b/v3/@claude-flow/watermark/wasm/ruflo_watermark_bg.wasm.d.ts@@ -0,0 +1,18 @@+/* tslint:disable */+/* eslint-disable */+export const memory: WebAssembly.Memory;+export const __wbg_wasmdetection_free: (a: number, b: number) => void;+export const __wbg_wasmwatermarker_free: (a: number, b: number) => void;+export const detect: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => number;+export const detect_exact: (a: number, b: number, c: number, d: number, e: number) => number;+export const detect_selfsync: (a: number, b: number, c: number, d: number, e: number) => number;+export const wasmdetection_log10_p: (a: number) => number;+export const wasmdetection_p_value: (a: number) => number;+export const wasmdetection_scored_positions: (a: number) => number;+export const wasmdetection_z_score: (a: number) => number;+export const wasmwatermarker_new: (a: number, b: number, c: number, d: number, e: number, f: number) => number;+export const wasmwatermarker_step: (a: number, b: number, c: number, d: number, e: number) => number;+export const __wbindgen_externrefs: WebAssembly.Table;+export const __wbindgen_malloc: (a: number, b: number) => number;+export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;+export const __wbindgen_start: () => void;diff --git a/v3/crates/ruflo-watermark/.gitignore b/v3/crates/ruflo-watermark/.gitignoreindex f9cb237d05..a104ff3806 100644--- a/v3/crates/ruflo-watermark/.gitignore+++ b/v3/crates/ruflo-watermark/.gitignore@@ -10,3 +10,5 @@.swarm/.ruvnet-brain/.ruvector/+/pkg+/pkg-nodejsdiff --git a/v3/crates/ruflo-watermark/src/wasm.rs b/v3/crates/ruflo-watermark/src/wasm.rsindex 05f3a5d1fb..76a69af48f 100644--- a/v3/crates/ruflo-watermark/src/wasm.rs+++ b/v3/crates/ruflo-watermark/src/wasm.rs@@ -5,11 +5,14 @@//! without a native addon. Token/probability slices marshal as typed arrays//! (`Uint32Array` / `Float32Array`); detection results are a small struct with//! getters.+//!+//! Schemes are selected by string: `"tournament"`, `"tournament_nd"`, or+//! `"gumbel"`. Detectors exposed: the per-scheme detector, the indel-robust+//! self-sync detector, and the exact-Gamma short-text detector (Gumbel).use wasm_bindgen::prelude::*;use crate::context::WatermarkConfig;-use crate::detect::{detect_gumbel, detect_tournament};use crate::hash::WatermarkKey;use crate::{Scheme, Watermarker};@@ -19,12 +22,11 @@ fn cfg_from(material: &[u8], context_width: usize, layers: u32) -> WatermarkConf.with_layers(layers)}-#[inline]-fn scheme_of(gumbel: bool) -> Scheme {- if gumbel {- Scheme::Gumbel- } else {- Scheme::Tournament+fn scheme_of(scheme: &str) -> Scheme {+ match scheme {+ "tournament" => Scheme::Tournament,+ "tournament_nd" => Scheme::TournamentNd,+ _ => Scheme::Gumbel, // default: distortion-free}}@@ -37,12 +39,12 @@ pub struct WasmWatermarker {#[wasm_bindgen]impl WasmWatermarker {/// `key_material`: arbitrary secret bytes (e.g. a hex string's bytes).- /// `gumbel`: `true` for the distortion-free scheme, `false` for tournament.+ /// `scheme`: `"tournament"` | `"tournament_nd"` | `"gumbel"` (default gumbel).#[wasm_bindgen(constructor)]- pub fn new(key_material: &[u8], context_width: usize, layers: u32, gumbel: bool) -> WasmWatermarker {+ pub fn new(key_material: &[u8], context_width: usize, layers: u32, scheme: &str) -> WasmWatermarker {let cfg = cfg_from(key_material, context_width, layers);WasmWatermarker {- inner: Watermarker::new(cfg, scheme_of(gumbel)),+ inner: Watermarker::new(cfg, scheme_of(scheme)),}}@@ -82,25 +84,47 @@ impl WasmDetection {}}-/// Detect a watermark over an emitted token id sequence.+impl From<crate::detect::DetectionResult> for WasmDetection {+ fn from(r: crate::detect::DetectionResult) -> Self {+ WasmDetection {+ scored_positions: r.scored_positions,+ z_score: r.z_score,+ p_value: r.p_value,+ log10_p: r.log10_p,+ }+ }+}++/// Detect a watermark over an emitted token id sequence, using the named scheme.#[wasm_bindgen]pub fn detect(tokens: &[u32],key_material: &[u8],context_width: usize,layers: u32,- gumbel: bool,+ scheme: &str,) -> WasmDetection {let cfg = cfg_from(key_material, context_width, layers);- let r = if gumbel {- detect_gumbel(tokens, cfg)- } else {- detect_tournament(tokens, cfg)- };- WasmDetection {- scored_positions: r.scored_positions,- z_score: r.z_score,- p_value: r.p_value,- log10_p: r.log10_p,+ match scheme_of(scheme) {+ Scheme::Tournament => crate::detect::detect_tournament(tokens, cfg),+ Scheme::TournamentNd => crate::detect::detect_tournament_nd(tokens, cfg),+ Scheme::Gumbel => crate::detect::detect_gumbel(tokens, cfg),}+ .into()+}++/// Indel-robust detection (Gumbel self-sync): far stronger than the standard+/// detector on edited / repetitive text. See `align.rs`.+#[wasm_bindgen]+pub fn detect_selfsync(tokens: &[u32], key_material: &[u8], context_width: usize) -> WasmDetection {+ let cfg = cfg_from(key_material, context_width, 1);+ crate::align::detect_gumbel_selfsync(tokens, cfg).into()+}++/// Exact-null short-text detection (Gumbel, exact Gamma tail): correct p-values+/// at small token counts where the normal approximation misleads. See `bayes.rs`.+#[wasm_bindgen]+pub fn detect_exact(tokens: &[u32], key_material: &[u8], context_width: usize) -> WasmDetection {+ let cfg = cfg_from(key_material, context_width, 1);+ crate::bayes::detect_gumbel_exact(tokens, cfg).into()}