Skip to content

The one and only agent harness for complex codebases. Project memory, planning, execution, and verified completion inside Codex.

chore: sync Codex marketplace v5.1.46

YeonGyu-Kim15af12497b1f
diff --git a/plugins/omo/.codex-plugin/plugin.json b/plugins/omo/.codex-plugin/plugin.json
index 0fcc0c55..53c968a2 100644
--- a/plugins/omo/.codex-plugin/plugin.json
+++ b/plugins/omo/.codex-plugin/plugin.json
@@ -1,6 +1,6 @@
{
"name": "omo",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "One Codex plugin namespace for Yeongyu's local Codex components.",
"author": {
"name": "Yeongyu Kim",
diff --git a/plugins/omo/components/bootstrap/dist/cli.js b/plugins/omo/components/bootstrap/dist/cli.js
index 00b1086b..bbe939f9 100755
--- a/plugins/omo/components/bootstrap/dist/cli.js
+++ b/plugins/omo/components/bootstrap/dist/cli.js
@@ -7147,6 +7147,7 @@ var OmoMemoryReflectionSchema = object({
merge: _enum(["auto", "integration"]).default("auto"),
category: string2().min(1).default("quick"),
timeout_minutes: number2().int().positive().default(15),
+ max_concurrent_runs: number2().int().positive().default(1),
sandbox: _enum(["auto", "required", "off"]).default("auto")
}).strict();
var OmoMemorySyncSchema = object({
@@ -7218,6 +7219,7 @@ var OmoMemoryReflectionLayerSchema = object({
merge: _enum(["auto", "integration"]).optional(),
category: string2().min(1).optional(),
timeout_minutes: number2().int().positive().optional(),
+ max_concurrent_runs: number2().int().positive().optional(),
sandbox: _enum(["auto", "required", "off"]).optional()
}).strict();
var OmoMemorySyncLayerSchema = object({
@@ -7282,7 +7284,7 @@ var OmoMemoryProjectionLayerSchema = object({
var OmoMemoryAgentOverridesSchema = object({
enabled: boolean2().optional(),
agent: string2().min(1).optional(),
- reflection: OmoMemoryReflectionLayerSchema.optional(),
+ reflection: OmoMemoryReflectionLayerSchema.omit({ max_concurrent_runs: true }).optional(),
nudge: OmoMemoryNudgeLayerSchema.optional(),
facts: OmoMemoryFactsLayerSchema.optional(),
dream: OmoMemoryDreamLayerSchema.optional(),
@@ -7307,6 +7309,7 @@ var OmoMemorySettingsSchema = object({
merge: "auto",
category: "quick",
timeout_minutes: 15,
+ max_concurrent_runs: 1,
sandbox: "auto"
}),
nudge: OmoMemoryNudgeSchema.default({ enabled: true, every_user_turns: 10 }),
@@ -7650,9 +7653,6 @@ var OmoFormatOnMutationSchema = OmoFormatOnMutationLayerSchema.extend({
timeoutMs: number2().int().positive().default(3000)
}).strict();
-// ../../omo-config-core/src/schema/dori.ts
-var OmoDoriSectionSchema = record(string2(), unknown()).describe("Chat-surface dori settings, owned and validated by a separately installed dori package. omo accepts the key and never reads it.");
-
// ../../omo-config-core/src/schema/provenance.ts
var OmoProvenanceSettingsSchema = object({
workspaces: array(object({
@@ -7669,7 +7669,6 @@ var OmoExcludedModelsSchema = array(string2());
var OmoTypedHarnessConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -7688,7 +7687,6 @@ var OmoTypedHarnessConfigSchema = object({
var OmoConfigProfileSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -7711,7 +7709,6 @@ var OmoConfigProfileSchema = object({
var OmoConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -7738,7 +7735,6 @@ var OmoConfigSchema = object({
var OmoConfigLayerSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -8762,6 +8758,9 @@ function printParseErrorCode(code) {
return "<unknown ParseErrorCode>";
}
+// ../../omo-config-core/src/loader/layer-validation.ts
+import { createHash as createHash3 } from "node:crypto";
+
// ../../omo-config-core/src/loader/merge.ts
var DANGEROUS_KEYS = new Set(["__proto__", "constructor", "prototype"]);
function isUnsafeObjectKey(key) {
@@ -8935,6 +8934,14 @@ function unrecognizedKeyIssues(issues) {
function isRecord9(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+var RETIRED_SECTION_DIGESTS = new Set([
+ "447f75a8ebdbdb79a57f0b8cebbfba183820cf54a8119ac01449b30499e5ef0e",
+ "4ea5ee68fea05586106890ded5733820bb77d919cda27bc4b8139b7cd33b8889"
+]);
+function isRetiredSection(key) {
+ return RETIRED_SECTION_DIGESTS.has(createHash3("sha256").update(key.toLowerCase()).digest("hex"));
+}
+var HARNESS_BLOCKS = new Set(["[opencode]", "[native]", "[senpi]", "[codex]"]);
function sanitizeUnsafeKeys(value, path = []) {
if (Array.isArray(value)) {
const issues = [];
@@ -8953,6 +8960,9 @@ function sanitizeUnsafeKeys(value, path = []) {
}
const sanitized = {};
for (const [key, entry] of Object.entries(value)) {
+ const isConfigContainer = path.length === 0 || path.length === 1 && HARNESS_BLOCKS.has(path[0] ?? "") || path.length === 2 && path[0] === "profiles" || path.length === 3 && path[0] === "profiles" && HARNESS_BLOCKS.has(path[2] ?? "");
+ if (isConfigContainer && isRetiredSection(key))
+ continue;
if (isUnsafeObjectKey(key)) {
issues.push({ keys: [key], path });
continue;
@@ -10502,7 +10512,7 @@ function nodeErrorCode2(error) {
}
// ../src/install/default-agent-role.ts
-import { createHash as createHash3 } from "node:crypto";
+import { createHash as createHash4 } from "node:crypto";
import { lstat as lstat5, readFile as readFile7, rm as rm7 } from "node:fs/promises";
import { join as join13 } from "node:path";
@@ -10670,7 +10680,7 @@ async function readIfPresent(path) {
}
}
function hash(content) {
- return createHash3("sha256").update(content).digest("hex");
+ return createHash4("sha256").update(content).digest("hex");
}
// ../src/install/link-cached-plugin-agents.ts
@@ -12081,7 +12091,7 @@ async function stampGitBashMcpEnv(input) {
}
// ../src/install/codex-hook-trust.ts
-import { createHash as createHash4 } from "node:crypto";
+import { createHash as createHash5 } from "node:crypto";
import { readFile as readFile15 } from "node:fs/promises";
import { join as join22 } from "node:path";
var EVENT_LABELS = new Map([
@@ -12173,7 +12183,7 @@ function commandHookHash(eventName, matcher, handler, command) {
if (typeof matcher === "string")
identity.matcher = matcher;
const canonical = JSON.stringify(canonicalJson(identity));
- return `sha256:${createHash4("sha256").update(canonical).digest("hex")}`;
+ return `sha256:${createHash5("sha256").update(canonical).digest("hex")}`;
}
function canonicalJson(value) {
if (Array.isArray(value))
diff --git a/plugins/omo/components/bootstrap/hooks/hooks.json b/plugins/omo/components/bootstrap/hooks/hooks.json
index 00d69943..cd104c25 100644
--- a/plugins/omo/components/bootstrap/hooks/hooks.json
+++ b/plugins/omo/components/bootstrap/hooks/hooks.json
@@ -8,7 +8,7 @@
"command": "node \"${PLUGIN_ROOT}/components/bootstrap/dist/cli.js\" hook session-start",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\bootstrap.ps1\"",
"timeout": 30,
- "statusMessage": "(OmO 5.1.45) Checking Bootstrap Provisioning"
+ "statusMessage": "(OmO 5.1.46) Checking Bootstrap Provisioning"
}
]
}
diff --git a/plugins/omo/components/bootstrap/package.json b/plugins/omo/components/bootstrap/package.json
index 2884859a..2f880735 100644
--- a/plugins/omo/components/bootstrap/package.json
+++ b/plugins/omo/components/bootstrap/package.json
@@ -1,6 +1,6 @@
{
"name": "@sisyphuslabs/codex-bootstrap",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex SessionStart bootstrap component that provisions LazyCodex runtime dependencies from a detached worker.",
"type": "module",
"private": true,
diff --git a/plugins/omo/components/comment-checker/hooks/hooks.json b/plugins/omo/components/comment-checker/hooks/hooks.json
index 7424d486..ac5e146c 100644
--- a/plugins/omo/components/comment-checker/hooks/hooks.json
+++ b/plugins/omo/components/comment-checker/hooks/hooks.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-tool-use",
"timeout": 30,
- "statusMessage": "(OmO 5.1.45) Checking Comments"
+ "statusMessage": "(OmO 5.1.46) Checking Comments"
}
]
}
diff --git a/plugins/omo/components/comment-checker/package.json b/plugins/omo/components/comment-checker/package.json
index 46cacfde..590ab436 100644
--- a/plugins/omo/components/comment-checker/package.json
+++ b/plugins/omo/components/comment-checker/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-comment-checker",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin that runs comment-checker after edit-like PostToolUse hooks.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/git-bash/hooks/hooks.json b/plugins/omo/components/git-bash/hooks/hooks.json
index d314944f..6c9e22f3 100644
--- a/plugins/omo/components/git-bash/hooks/hooks.json
+++ b/plugins/omo/components/git-bash/hooks/hooks.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook pre-tool-use",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Recommending Git Bash MCP"
+ "statusMessage": "(OmO 5.1.46) Recommending Git Bash MCP"
}
]
}
@@ -20,7 +20,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-compact",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Resetting Git Bash MCP Reminder"
+ "statusMessage": "(OmO 5.1.46) Resetting Git Bash MCP Reminder"
}
]
}
diff --git a/plugins/omo/components/git-bash/package.json b/plugins/omo/components/git-bash/package.json
index 02152334..b036f82f 100644
--- a/plugins/omo/components/git-bash/package.json
+++ b/plugins/omo/components/git-bash/package.json
@@ -1,6 +1,6 @@
{
"name": "@sisyphuslabs/codex-git-bash-hook",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex hook component that reminds Windows sessions to prefer the OMO git_bash MCP.",
"type": "module",
"private": true,
diff --git a/plugins/omo/components/lazycodex-executor-verify/hooks/hooks.json b/plugins/omo/components/lazycodex-executor-verify/hooks/hooks.json
index c56c203e..971f76fa 100644
--- a/plugins/omo/components/lazycodex-executor-verify/hooks/hooks.json
+++ b/plugins/omo/components/lazycodex-executor-verify/hooks/hooks.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/lazycodex-executor-verify/dist/cli.js\" hook subagent-stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Verifying LazyCodex Executor Evidence"
+ "statusMessage": "(OmO 5.1.46) Verifying LazyCodex Executor Evidence"
}
]
}
diff --git a/plugins/omo/components/lazycodex-executor-verify/package.json b/plugins/omo/components/lazycodex-executor-verify/package.json
index d1991145..46194f99 100644
--- a/plugins/omo/components/lazycodex-executor-verify/package.json
+++ b/plugins/omo/components/lazycodex-executor-verify/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-lazycodex-executor-verify",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex SubagentStop evidence verifier for LazyCodex executor completions.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/lsp-daemon/dist/cli.js b/plugins/omo/components/lsp-daemon/dist/cli.js
index ee541a46..714cd089 100644
--- a/plugins/omo/components/lsp-daemon/dist/cli.js
+++ b/plugins/omo/components/lsp-daemon/dist/cli.js
@@ -6299,6 +6299,7 @@ function coerceToolArguments(value) {
// ../lsp-core/src/mcp.ts
var SERVER_NAME = "lsp";
var SERVER_VERSION = "0.1.0";
+var PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
async function handleLspMcpRequest(input, options = {}) {
if (!isPlainRecord(input)) {
return errorResponse(null, -32600, "Invalid Request");
@@ -6355,9 +6356,8 @@ function describeTool(tool) {
};
}
function requestedProtocolVersion(params) {
- if (!isPlainRecord(params) || typeof params["protocolVersion"] !== "string")
- return "2024-11-05";
- return params["protocolVersion"];
+ const requested = isPlainRecord(params) ? params["protocolVersion"] : undefined;
+ return typeof requested === "string" && PROTOCOL_VERSIONS.includes(requested) ? requested : "2025-06-18";
}
// src/daemon-client.ts
diff --git a/plugins/omo/components/lsp-daemon/dist/client.js b/plugins/omo/components/lsp-daemon/dist/client.js
index d57d2e66..1b9360cd 100644
--- a/plugins/omo/components/lsp-daemon/dist/client.js
+++ b/plugins/omo/components/lsp-daemon/dist/client.js
@@ -6535,6 +6535,7 @@ function coerceToolArguments(value) {
// ../lsp-core/src/mcp.ts
var SERVER_NAME = "lsp";
var SERVER_VERSION = "0.1.0";
+var PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
async function handleLspMcpRequest(input, options = {}) {
if (!isPlainRecord(input)) {
return errorResponse(null, -32600, "Invalid Request");
@@ -6591,9 +6592,8 @@ function describeTool(tool) {
};
}
function requestedProtocolVersion(params) {
- if (!isPlainRecord(params) || typeof params["protocolVersion"] !== "string")
- return "2024-11-05";
- return params["protocolVersion"];
+ const requested = isPlainRecord(params) ? params["protocolVersion"] : undefined;
+ return typeof requested === "string" && PROTOCOL_VERSIONS.includes(requested) ? requested : "2025-06-18";
}
// src/request-routing.ts
diff --git a/plugins/omo/components/lsp-daemon/dist/index.js b/plugins/omo/components/lsp-daemon/dist/index.js
index 70cd2b61..6de34b96 100644
--- a/plugins/omo/components/lsp-daemon/dist/index.js
+++ b/plugins/omo/components/lsp-daemon/dist/index.js
@@ -6530,6 +6530,7 @@ function coerceToolArguments(value) {
// ../lsp-core/src/mcp.ts
var SERVER_NAME = "lsp";
var SERVER_VERSION = "0.1.0";
+var PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
async function handleLspMcpRequest(input, options = {}) {
if (!isPlainRecord(input)) {
return errorResponse(null, -32600, "Invalid Request");
@@ -6586,9 +6587,8 @@ function describeTool(tool) {
};
}
function requestedProtocolVersion(params) {
- if (!isPlainRecord(params) || typeof params["protocolVersion"] !== "string")
- return "2024-11-05";
- return params["protocolVersion"];
+ const requested = isPlainRecord(params) ? params["protocolVersion"] : undefined;
+ return typeof requested === "string" && PROTOCOL_VERSIONS.includes(requested) ? requested : "2025-06-18";
}
// src/request-routing.ts
diff --git a/plugins/omo/components/lsp-tools-mcp/dist/cli.js b/plugins/omo/components/lsp-tools-mcp/dist/cli.js
index 47fcf2ee..9d2e7f91 100755
--- a/plugins/omo/components/lsp-tools-mcp/dist/cli.js
+++ b/plugins/omo/components/lsp-tools-mcp/dist/cli.js
@@ -5868,6 +5868,7 @@ function coerceToolArguments(value) {
// ../lsp-core/src/mcp.ts
var SERVER_NAME = "lsp";
var SERVER_VERSION = "0.1.0";
+var PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
async function handleLspMcpRequest(input, options = {}) {
if (!isPlainRecord(input)) {
return errorResponse(null, -32600, "Invalid Request");
@@ -5935,9 +5936,8 @@ function describeTool(tool) {
};
}
function requestedProtocolVersion(params) {
- if (!isPlainRecord(params) || typeof params["protocolVersion"] !== "string")
- return "2024-11-05";
- return params["protocolVersion"];
+ const requested = isPlainRecord(params) ? params["protocolVersion"] : undefined;
+ return typeof requested === "string" && PROTOCOL_VERSIONS.includes(requested) ? requested : "2025-06-18";
}
// src/cli.ts
async function main() {
diff --git a/plugins/omo/components/lsp-tools-mcp/dist/mcp.js b/plugins/omo/components/lsp-tools-mcp/dist/mcp.js
index 922b2210..3c5e55b8 100644
--- a/plugins/omo/components/lsp-tools-mcp/dist/mcp.js
+++ b/plugins/omo/components/lsp-tools-mcp/dist/mcp.js
@@ -5864,6 +5864,7 @@ function coerceToolArguments(value) {
// ../lsp-core/src/mcp.ts
var SERVER_NAME = "lsp";
var SERVER_VERSION = "0.1.0";
+var PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
async function handleLspMcpRequest(input, options = {}) {
if (!isPlainRecord(input)) {
return errorResponse(null, -32600, "Invalid Request");
@@ -5931,9 +5932,8 @@ function describeTool(tool) {
};
}
function requestedProtocolVersion(params) {
- if (!isPlainRecord(params) || typeof params["protocolVersion"] !== "string")
- return "2024-11-05";
- return params["protocolVersion"];
+ const requested = isPlainRecord(params) ? params["protocolVersion"] : undefined;
+ return typeof requested === "string" && PROTOCOL_VERSIONS.includes(requested) ? requested : "2025-06-18";
}
export {
handleLspMcpRequest,
diff --git a/plugins/omo/components/lsp/dist/.omo-runtime-manifest.json b/plugins/omo/components/lsp/dist/.omo-runtime-manifest.json
index 5087acd3..6a424ef1 100644
--- a/plugins/omo/components/lsp/dist/.omo-runtime-manifest.json
+++ b/plugins/omo/components/lsp/dist/.omo-runtime-manifest.json
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
- "version": "5.1.45",
- "inputDigest": "sha256:1dfcf866d737eda759d86d37d2c6e945010a7591f33c463dff7fb2dc50640bd0",
+ "version": "5.1.46",
+ "inputDigest": "sha256:a4cbfb8ffa442b654814e1d86e70434c1d19d06f473eccc93bf7bbc7485336e7",
"outputs": [
{
"path": "cli.d.ts",
diff --git a/plugins/omo/components/lsp/hooks/hooks.json b/plugins/omo/components/lsp/hooks/hooks.json
index 711289fe..155bf95a 100644
--- a/plugins/omo/components/lsp/hooks/hooks.json
+++ b/plugins/omo/components/lsp/hooks/hooks.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-tool-use",
"timeout": 60,
- "statusMessage": "(OmO 5.1.45) Checking LSP Diagnostics"
+ "statusMessage": "(OmO 5.1.46) Checking LSP Diagnostics"
}
]
}
@@ -21,7 +21,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-compact",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Resetting LSP Diagnostics Cache"
+ "statusMessage": "(OmO 5.1.46) Resetting LSP Diagnostics Cache"
}
]
}
diff --git a/plugins/omo/components/lsp/package.json b/plugins/omo/components/lsp/package.json
index ee1825dd..b8a3b2c5 100644
--- a/plugins/omo/components/lsp/package.json
+++ b/plugins/omo/components/lsp/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-lsp",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin that exposes Language Server Protocol tools and post-edit diagnostics.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/rules/hooks/hooks.json b/plugins/omo/components/rules/hooks/hooks.json
index f92c2ef4..1cc77abb 100644
--- a/plugins/omo/components/rules/hooks/hooks.json
+++ b/plugins/omo/components/rules/hooks/hooks.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook session-start",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Loading Project Rules"
+ "statusMessage": "(OmO 5.1.46) Loading Project Rules"
}
]
}
@@ -19,7 +19,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook user-prompt-submit",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Loading Project Rules"
+ "statusMessage": "(OmO 5.1.46) Loading Project Rules"
}
]
}
@@ -32,7 +32,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-tool-use",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Matching Project Rules"
+ "statusMessage": "(OmO 5.1.46) Matching Project Rules"
}
]
}
@@ -45,7 +45,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-compact",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Resetting Project Rule Cache"
+ "statusMessage": "(OmO 5.1.46) Resetting Project Rule Cache"
}
]
}
diff --git a/plugins/omo/components/rules/package.json b/plugins/omo/components/rules/package.json
index d64e0810..e622df9c 100644
--- a/plugins/omo/components/rules/package.json
+++ b/plugins/omo/components/rules/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-rules",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin that injects project rule files into model context through lifecycle hooks.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/teammode/hooks/hooks.json b/plugins/omo/components/teammode/hooks/hooks.json
index 8b6391e7..468789a5 100644
--- a/plugins/omo/components/teammode/hooks/hooks.json
+++ b/plugins/omo/components/teammode/hooks/hooks.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-tool-use",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Thread Title Hygiene"
+ "statusMessage": "(OmO 5.1.46) Checking Thread Title Hygiene"
}
]
}
diff --git a/plugins/omo/components/teammode/package.json b/plugins/omo/components/teammode/package.json
index 46929095..482fc459 100644
--- a/plugins/omo/components/teammode/package.json
+++ b/plugins/omo/components/teammode/package.json
@@ -1,6 +1,6 @@
{
"name": "@sisyphuslabs/codex-teammode",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex team-mode hook component that keeps background thread titles descriptive after create_thread.",
"type": "module",
"private": true,
diff --git a/plugins/omo/components/telemetry/hooks/hooks.json b/plugins/omo/components/telemetry/hooks/hooks.json
index fa7e21c2..ad6d88b6 100644
--- a/plugins/omo/components/telemetry/hooks/hooks.json
+++ b/plugins/omo/components/telemetry/hooks/hooks.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook session-start",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Recording Session Telemetry"
+ "statusMessage": "(OmO 5.1.46) Recording Session Telemetry"
}
]
}
diff --git a/plugins/omo/components/telemetry/package.json b/plugins/omo/components/telemetry/package.json
index 71e0cea4..e8cabf2e 100644
--- a/plugins/omo/components/telemetry/package.json
+++ b/plugins/omo/components/telemetry/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-telemetry",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin component that emits omo-codex anonymous daily-active telemetry on SessionStart.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/ultrawork/hooks/hooks.json b/plugins/omo/components/ultrawork/hooks/hooks.json
index 9b1b4861..50d1ccdf 100644
--- a/plugins/omo/components/ultrawork/hooks/hooks.json
+++ b/plugins/omo/components/ultrawork/hooks/hooks.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook user-prompt-submit",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Checking Ultrawork Trigger"
+ "statusMessage": "(OmO 5.1.46) Checking Ultrawork Trigger"
}
]
}
diff --git a/plugins/omo/components/ultrawork/package.json b/plugins/omo/components/ultrawork/package.json
index 93b00b63..35ff32f5 100644
--- a/plugins/omo/components/ultrawork/package.json
+++ b/plugins/omo/components/ultrawork/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-ultrawork",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin that injects the ultrawork orchestration directive and ships LazyCodex planning, review, QA, and gate agent roles.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/ulw-execute-continuation/hooks/hooks.json b/plugins/omo/components/ulw-execute-continuation/hooks/hooks.json
index ebbf9f1a..0182d653 100644
--- a/plugins/omo/components/ulw-execute-continuation/hooks/hooks.json
+++ b/plugins/omo/components/ulw-execute-continuation/hooks/hooks.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-execute-continuation/dist/cli.js\" hook stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Execute Continuation"
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Execute Continuation"
}
]
}
diff --git a/plugins/omo/components/ulw-execute-continuation/package.json b/plugins/omo/components/ulw-execute-continuation/package.json
index 7a48d250..fc3c6960 100644
--- a/plugins/omo/components/ulw-execute-continuation/package.json
+++ b/plugins/omo/components/ulw-execute-continuation/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-ulw-execute-continuation",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex Stop hook continuation injector for omo-codex ulw-execute plans.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/components/ulw-loop/hooks/hooks.json b/plugins/omo/components/ulw-loop/hooks/hooks.json
index d0d76038..a7b47bc8 100644
--- a/plugins/omo/components/ulw-loop/hooks/hooks.json
+++ b/plugins/omo/components/ulw-loop/hooks/hooks.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook user-prompt-submit --with-ultrawork",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Loop Steering"
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Loop Steering"
}
]
}
@@ -20,7 +20,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook post-tool-use-spawn",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Recording Spawn Admission"
+ "statusMessage": "(OmO 5.1.46) Recording Spawn Admission"
}
]
}
@@ -33,7 +33,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook pre-tool-use",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Enforcing Unlimited Ulw-Loop Budget"
+ "statusMessage": "(OmO 5.1.46) Enforcing Unlimited Ulw-Loop Budget"
}
]
},
@@ -44,7 +44,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook pre-tool-use-spawn",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Guarding Ulw-Loop Spawns"
+ "statusMessage": "(OmO 5.1.46) Guarding Ulw-Loop Spawns"
}
]
}
@@ -56,7 +56,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/dist/cli.js\" hook stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Loop Resume"
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Loop Resume"
}
]
}
diff --git a/plugins/omo/components/ulw-loop/package.json b/plugins/omo/components/ulw-loop/package.json
index 340c17f0..91e0170b 100644
--- a/plugins/omo/components/ulw-loop/package.json
+++ b/plugins/omo/components/ulw-loop/package.json
@@ -1,6 +1,6 @@
{
"name": "@code-yeongyu/codex-ulw-loop",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Codex plugin: durable repo-native multi-goal orchestration with embedded success criteria and observable evidence audit.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/dist/cli-node/index.js b/plugins/omo/dist/cli-node/index.js
index d69bcf3a..96821d22 100755
--- a/plugins/omo/dist/cli-node/index.js
+++ b/plugins/omo/dist/cli-node/index.js
@@ -2133,7 +2133,7 @@ var package_default;
var init_package = __esm(() => {
package_default = {
name: "oh-my-opencode",
- version: "5.1.45",
+ version: "5.1.46",
description: "The Best AI Agent Harness - Batteries-Included OpenCode Plugin with Multi-Model Orchestration, Parallel Background Agents, and Crafted LSP/AST Tools",
main: "./dist/index.js",
types: "dist/index.d.ts",
@@ -2379,18 +2379,18 @@ var init_package = __esm(() => {
typescript: "^7.0.2"
},
optionalDependencies: {
- "oh-my-opencode-darwin-arm64": "5.1.45",
- "oh-my-opencode-darwin-x64": "5.1.45",
- "oh-my-opencode-darwin-x64-baseline": "5.1.45",
- "oh-my-opencode-linux-arm64": "5.1.45",
- "oh-my-opencode-linux-arm64-musl": "5.1.45",
- "oh-my-opencode-linux-x64": "5.1.45",
- "oh-my-opencode-linux-x64-baseline": "5.1.45",
- "oh-my-opencode-linux-x64-musl": "5.1.45",
- "oh-my-opencode-linux-x64-musl-baseline": "5.1.45",
- "oh-my-opencode-windows-arm64": "5.1.45",
- "oh-my-opencode-windows-x64": "5.1.45",
- "oh-my-opencode-windows-x64-baseline": "5.1.45"
+ "oh-my-opencode-darwin-arm64": "5.1.46",
+ "oh-my-opencode-darwin-x64": "5.1.46",
+ "oh-my-opencode-darwin-x64-baseline": "5.1.46",
+ "oh-my-opencode-linux-arm64": "5.1.46",
+ "oh-my-opencode-linux-arm64-musl": "5.1.46",
+ "oh-my-opencode-linux-x64": "5.1.46",
+ "oh-my-opencode-linux-x64-baseline": "5.1.46",
+ "oh-my-opencode-linux-x64-musl": "5.1.46",
+ "oh-my-opencode-linux-x64-musl-baseline": "5.1.46",
+ "oh-my-opencode-windows-arm64": "5.1.46",
+ "oh-my-opencode-windows-x64": "5.1.46",
+ "oh-my-opencode-windows-x64-baseline": "5.1.46"
},
overrides: {
hono: "^4.13.8",
@@ -76982,6 +76982,7 @@ var init_memory = __esm(() => {
merge: _enum(["auto", "integration"]).default("auto"),
category: string2().min(1).default("quick"),
timeout_minutes: number2().int().positive().default(15),
+ max_concurrent_runs: number2().int().positive().default(1),
sandbox: _enum(["auto", "required", "off"]).default("auto")
}).strict();
OmoMemorySyncSchema = object({
@@ -77053,6 +77054,7 @@ var init_memory = __esm(() => {
merge: _enum(["auto", "integration"]).optional(),
category: string2().min(1).optional(),
timeout_minutes: number2().int().positive().optional(),
+ max_concurrent_runs: number2().int().positive().optional(),
sandbox: _enum(["auto", "required", "off"]).optional()
}).strict();
OmoMemorySyncLayerSchema = object({
@@ -77117,7 +77119,7 @@ var init_memory = __esm(() => {
OmoMemoryAgentOverridesSchema = object({
enabled: boolean2().optional(),
agent: string2().min(1).optional(),
- reflection: OmoMemoryReflectionLayerSchema.optional(),
+ reflection: OmoMemoryReflectionLayerSchema.omit({ max_concurrent_runs: true }).optional(),
nudge: OmoMemoryNudgeLayerSchema.optional(),
facts: OmoMemoryFactsLayerSchema.optional(),
dream: OmoMemoryDreamLayerSchema.optional(),
@@ -77142,6 +77144,7 @@ var init_memory = __esm(() => {
merge: "auto",
category: "quick",
timeout_minutes: 15,
+ max_concurrent_runs: 1,
sandbox: "auto"
}),
nudge: OmoMemoryNudgeSchema.default({ enabled: true, every_user_turns: 10 }),
@@ -77516,13 +77519,6 @@ var init_format_on_mutation = __esm(() => {
}).strict();
});
-// packages/omo-config-core/src/schema/dori.ts
-var OmoDoriSectionSchema;
-var init_dori = __esm(() => {
- init_zod();
- OmoDoriSectionSchema = record(string2(), unknown()).describe("Chat-surface dori settings, owned and validated by a separately installed dori package. omo accepts the key and never reads it.");
-});
-
// packages/omo-config-core/src/schema/provenance.ts
var OmoProvenanceSettingsSchema;
var init_provenance = __esm(() => {
@@ -77552,7 +77548,6 @@ var init_config = __esm(() => {
init_team();
init_telemetry();
init_format_on_mutation();
- init_dori();
init_provenance();
OmoOpenCodeHarnessConfigSchema = record(string2(), unknown());
OmoDisabledSkillsSchema = array(string2());
@@ -77560,7 +77555,6 @@ var init_config = __esm(() => {
OmoTypedHarnessConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -77579,7 +77573,6 @@ var init_config = __esm(() => {
OmoConfigProfileSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -77602,7 +77595,6 @@ var init_config = __esm(() => {
OmoConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -77629,7 +77621,6 @@ var init_config = __esm(() => {
OmoConfigLayerSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -77771,54 +77762,6 @@ var init_legacy_harness_names = __esm(() => {
init_harness();
});
-// packages/omo-config-core/src/schema/legacy-dori-section.ts
-function isRecord11(value) {
- return value !== null && typeof value === "object" && !Array.isArray(value);
-}
-function migrateLegacyDoriSection(document) {
- if (!isRecord11(document))
- return { document: {}, diagnostics: [] };
- const diagnostics = [];
- const rename = (container, path) => {
- const result = { ...container };
- if (Object.hasOwn(container, "gateway")) {
- if (Object.hasOwn(container, "dori")) {
- diagnostics.push(`${[...path, "gateway"].join(".")} removed: dori is already configured`);
- } else {
- result.dori = container.gateway;
- diagnostics.push(`${[...path, "gateway"].join(".")} renamed to dori`);
- }
- delete result.gateway;
- }
- return result;
- };
- const layer = (container, path) => {
- const result = rename(container, path);
- for (const key of HARNESS_BLOCKS) {
- const block = container[key];
- if (isRecord11(block))
- result[key] = rename(block, [...path, key]);
- }
- return result;
- };
- const result = layer(document, []);
- if (isRecord11(document.profiles)) {
- const profiles = {};
- for (const [name, profile] of Object.entries(document.profiles)) {
- profiles[name] = isRecord11(profile) ? layer(profile, ["profiles", name]) : profile;
- }
- result.profiles = profiles;
- }
- return { document: result, diagnostics };
-}
-function hasLegacyGatewaySection(document) {
- return migrateLegacyDoriSection(document).diagnostics.length > 0;
-}
-var HARNESS_BLOCKS;
-var init_legacy_dori_section = __esm(() => {
- HARNESS_BLOCKS = ["[opencode]", "[native]", "[senpi]", "[codex]"];
-});
-
// packages/omo-config-core/src/schema/index.ts
var init_schema = __esm(() => {
init_agent();
@@ -77826,12 +77769,10 @@ var init_schema = __esm(() => {
init_config();
init_fallback_models();
init_format_on_mutation();
- init_dori();
init_git_master();
init_harness();
init_legacy_category_names();
init_legacy_harness_names();
- init_legacy_dori_section();
init_memory();
init_model_catalog();
init_model_profile();
@@ -78102,6 +78043,7 @@ function pruneInvalidConfigPaths(config, issues, validate, maxPasses = MAX_PRUNE
var MAX_PRUNE_PASSES = 32;
// packages/omo-config-core/src/loader/layer-validation.ts
+import { createHash as createHash2 } from "node:crypto";
function validationDiagnostic(path, issues) {
const issuePaths = issues.map((issue) => issue.path.map((segment) => String(segment)).join("."));
return {
@@ -78122,9 +78064,12 @@ function invalidValueDiagnostics(path, dropped) {
function unrecognizedKeyIssues(issues) {
return issues.flatMap((issue) => issue.code === "unrecognized_keys" ? [{ keys: issue.keys, path: issue.path.map((segment) => String(segment)) }] : []);
}
-function isRecord12(value) {
+function isRecord11(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+function isRetiredSection(key) {
+ return RETIRED_SECTION_DIGESTS.has(createHash2("sha256").update(key.toLowerCase()).digest("hex"));
+}
function sanitizeUnsafeKeys(value, path = []) {
if (Array.isArray(value)) {
const issues = [];
@@ -78135,7 +78080,7 @@ function sanitizeUnsafeKeys(value, path = []) {
});
return { issues, value: sanitized };
}
- if (!isRecord12(value))
+ if (!isRecord11(value))
return { issues: [], value };
const issues = [];
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {
@@ -78143,6 +78088,9 @@ function sanitizeUnsafeKeys(value, path = []) {
}
const sanitized = {};
for (const [key, entry] of Object.entries(value)) {
+ const isConfigContainer = path.length === 0 || path.length === 1 && HARNESS_BLOCKS.has(path[0] ?? "") || path.length === 2 && path[0] === "profiles" || path.length === 3 && path[0] === "profiles" && HARNESS_BLOCKS.has(path[2] ?? "");
+ if (isConfigContainer && isRetiredSection(key))
+ continue;
if (isUnsafeObjectKey2(key)) {
issues.push({ keys: [key], path });
continue;
@@ -78161,13 +78109,13 @@ function containerAt(record, path) {
if (!Number.isInteger(index) || index < 0 || index >= node.length)
return null;
node = node[index];
- } else if (isRecord12(node) && Object.hasOwn(node, segment)) {
+ } else if (isRecord11(node) && Object.hasOwn(node, segment)) {
node = node[segment];
} else {
return null;
}
}
- return isRecord12(node) ? node : null;
+ return isRecord11(node) ? node : null;
}
function stripUnrecognizedKeys(record, issues) {
const stripped = structuredClone(record);
@@ -78229,13 +78177,18 @@ function validateConfigLayer(path, data) {
return rejected;
return { loaded: true, diagnostics: [...diagnostics, ...invalidValueDiagnostics(path, pruned.dropped)], value: pruned.config };
}
-var validateLayerRecord = (record) => {
+var RETIRED_SECTION_DIGESTS, HARNESS_BLOCKS, validateLayerRecord = (record) => {
const parsed = OmoConfigLayerSchema.safeParse(record);
return parsed.success ? { success: true } : { success: false, issues: parsed.error.issues };
};
var init_layer_validation = __esm(() => {
init_schema();
init_merge();
+ RETIRED_SECTION_DIGESTS = new Set([
+ "447f75a8ebdbdb79a57f0b8cebbfba183820cf54a8119ac01449b30499e5ef0e",
+ "4ea5ee68fea05586106890ded5733820bb77d919cda27bc4b8139b7cd33b8889"
+ ]);
+ HARNESS_BLOCKS = new Set(["[opencode]", "[native]", "[senpi]", "[codex]"]);
});
// packages/omo-config-core/src/internal/posix-path.ts
@@ -81795,18 +81748,6 @@ function subscriptionProviderRenamePlan(targetPath) {
transform: inspect
};
}
-function doriSectionRenamePlan(targetPath, rename) {
- const inspect = (sources) => rename.transform(sources[0]?.value);
- return {
- id: DORI_SECTION_RENAME_MIGRATION_ID,
- inspect,
- mode: "replace-target",
- shouldRun: rename.shouldRun,
- sources: [],
- targetPath,
- transform: inspect
- };
-}
function existingOmoConfigPath(directory, options) {
const fileSystem = discoveryFileSystem(options);
for (const fileName of ["omo.jsonc", "omo.json"]) {
@@ -81868,7 +81809,6 @@ function createLegacyConfigMigrationPlans(options) {
addReasoningTarget(plan.targetPath);
const inPlaceTargets = [...reasoningTargets.values()];
return [
- ...inPlaceTargets.map((targetPath) => doriSectionRenamePlan(targetPath, options.doriSectionRename)),
...legacyPlans,
...inPlaceTargets.map(reasoningPlan),
...inPlaceTargets.map(categoryDeepSplitPlan),
@@ -81876,7 +81816,6 @@ function createLegacyConfigMigrationPlans(options) {
...inPlaceTargets.map(subscriptionProviderRenamePlan)
];
}
-var DORI_SECTION_RENAME_MIGRATION_ID = "2026-10-dori-section-rename";
var init_migration_plans = __esm(() => {
init_discovery2();
init_discovery_paths2();
@@ -81903,16 +81842,6 @@ var init_config_migration = __esm(() => {
init_reasoning_unification();
});
-// packages/omo-opencode/src/config-migration/dori-section-rename.ts
-var doriSectionRename;
-var init_dori_section_rename = __esm(() => {
- init_src4();
- doriSectionRename = {
- shouldRun: hasLegacyGatewaySection,
- transform: migrateLegacyDoriSection
- };
-});
-
// packages/omo-opencode/src/startup-migration.ts
import { posix as posix5 } from "node:path";
function homeDirectory(options) {
@@ -81952,7 +81881,6 @@ function runOpenCodeStartupMigration(options) {
...options.backupTimestamp === undefined ? {} : { backupTimestamp: options.backupTimestamp },
cwd: options.cwd,
...options.discoveryFileSystem === undefined ? {} : { fileSystem: options.discoveryFileSystem },
- doriSectionRename,
environment: options.environment ?? process.env,
homeDir,
pathOperations: options.pathOperations ?? posix5,
@@ -81989,7 +81917,6 @@ function runOpenCodeStartupMigration(options) {
var init_startup_migration = __esm(() => {
init_src4();
init_config_migration();
- init_dori_section_rename();
});
// packages/omo-opencode/src/cli/config-manager/deep-merge-record.ts
@@ -82731,7 +82658,7 @@ function shouldRetainLine(line, cutoffMs) {
function parseDiagnosticLine(line) {
try {
const parsed = JSON.parse(line);
- if (!isRecord14(parsed)) {
+ if (!isRecord13(parsed)) {
return null;
}
return parsed;
@@ -82742,7 +82669,7 @@ function parseDiagnosticLine(line) {
throw error;
}
}
-function isRecord14(value) {
+function isRecord13(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function trimToMaxBytes(lines) {
@@ -82862,13 +82789,13 @@ var init_event_id = __esm(() => {
});
// packages/telemetry-core/src/machine-id.ts
-import { createHash as createHash5 } from "node:crypto";
+import { createHash as createHash6 } from "node:crypto";
import os4 from "node:os";
function getDefaultTelemetryOsProvider() {
return os4;
}
function getTelemetryDistinctId(machineIdPrefix, osProvider = getDefaultTelemetryOsProvider()) {
- return createHash5("sha256").update(`${machineIdPrefix}${osProvider.hostname()}`).digest("hex");
+ return createHash6("sha256").update(`${machineIdPrefix}${osProvider.hostname()}`).digest("hex");
}
var init_machine_id = () => {};
@@ -90430,7 +90357,7 @@ function coerceBool(value) {
function coerceString(value) {
return typeof value == "string" ? value : undefined;
}
-function isRecord15(value) {
+function isRecord14(value) {
return typeof value == "object" && value !== null && !Array.isArray(value);
}
function toRfc3339(timestamp) {
@@ -90452,7 +90379,7 @@ function relocateInto(properties, key, value) {
properties[key] = value;
}
function buildV1Event(message) {
- const sourceProperties = isRecord15(message.properties) ? message.properties : {};
+ const sourceProperties = isRecord14(message.properties) ? message.properties : {};
const properties = {
...sourceProperties
};
@@ -92533,7 +92460,7 @@ var package_default2;
var init_package2 = __esm(() => {
package_default2 = {
name: "@oh-my-opencode/omo-codex",
- version: "5.1.45",
+ version: "5.1.46",
type: "module",
private: true,
description: "Codex harness adapter for oh-my-openagent. Vendored Codex plugin namespace (omo) + TypeScript installer + telemetry.",
@@ -96703,10 +96630,10 @@ function readCatalogMultiAgentVersion(model, cachePath) {
} catch {
return null;
}
- if (!isRecord13(cache) || !Array.isArray(cache.models))
+ if (!isRecord12(cache) || !Array.isArray(cache.models))
return null;
for (const entry of cache.models) {
- if (!isRecord13(entry))
+ if (!isRecord12(entry))
continue;
if (entry.slug !== model && entry.id !== model)
continue;
@@ -96731,7 +96658,7 @@ function readRootModelCatalogPath(config) {
const single = config.match(/^\s*model_catalog_json\s*=\s*'([^']+)'/m);
return single?.[1] ?? null;
}
-function isRecord13(value) {
+function isRecord12(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function removeFeatureFlagSetting(config, featureName) {
@@ -96849,7 +96776,7 @@ function isMissingFileError(error) {
}
// packages/omo-codex/src/install/codex-hook-trust.ts
-import { createHash as createHash2 } from "node:crypto";
+import { createHash as createHash3 } from "node:crypto";
import { readFile as readFile12 } from "node:fs/promises";
import { join as join38 } from "node:path";
var EVENT_LABELS = new Map([
@@ -96941,7 +96868,7 @@ function commandHookHash(eventName, matcher, handler, command) {
if (typeof matcher === "string")
identity.matcher = matcher;
const canonical = JSON.stringify(canonicalJson(identity));
- return `sha256:${createHash2("sha256").update(canonical).digest("hex")}`;
+ return `sha256:${createHash3("sha256").update(canonical).digest("hex")}`;
}
function canonicalJson(value) {
if (Array.isArray(value))
@@ -97620,7 +97547,7 @@ function nodeErrorCode2(error) {
}
// packages/omo-codex/src/install/default-agent-role.ts
-import { createHash as createHash3 } from "node:crypto";
+import { createHash as createHash4 } from "node:crypto";
import { lstat as lstat9, readFile as readFile16, rm as rm10 } from "node:fs/promises";
import { join as join43 } from "node:path";
var REGISTRATION = { name: "default", configFile: "./agents/default.toml" };
@@ -97676,7 +97603,7 @@ async function readIfPresent(path) {
}
}
function hash(content) {
- return createHash3("sha256").update(content).digest("hex");
+ return createHash4("sha256").update(content).digest("hex");
}
// packages/omo-codex/src/install/link-cached-plugin-agents.ts
@@ -98341,7 +98268,7 @@ function formatUnknownError(error) {
}
// packages/omo-codex/src/install/lsp-daemon-reaper.ts
-import { createHash as createHash4 } from "node:crypto";
+import { createHash as createHash5 } from "node:crypto";
import { lstat as lstat12, readFile as readFile22, readdir as readdir10, rm as rm12 } from "node:fs/promises";
import { tmpdir as tmpdir3 } from "node:os";
import { join as join48, posix as posix6 } from "node:path";
@@ -98619,7 +98546,7 @@ async function readRegularTrimmedFile(path) {
return content.length > 0 ? content : null;
}
function shortDigest(value) {
- return createHash4("sha256").update(value).digest("hex").slice(0, 16);
+ return createHash5("sha256").update(value).digest("hex").slice(0, 16);
}
async function removeVersionDir(path) {
await rm12(path, { recursive: true, force: true });
@@ -99690,7 +99617,7 @@ node "%~dp0omo" %*\r
}
// packages/omo-senpi/src/install/plugin-artifacts.ts
-import { createHash as createHash6 } from "node:crypto";
+import { createHash as createHash7 } from "node:crypto";
import { constants as constants9 } from "node:fs";
import { access as access2, readFile as readFile29, stat as stat6 } from "node:fs/promises";
import { dirname as dirname24, join as join63 } from "node:path";
@@ -99729,7 +99656,7 @@ async function readSettings(settingsPath) {
throw error;
}
const parsed = JSON.parse(raw);
- if (!isRecord16(parsed))
+ if (!isRecord15(parsed))
throw new Error(`${settingsPath} must contain a JSON object`);
return parsed;
}
@@ -99790,7 +99717,7 @@ async function nextBackupPath(settingsPath) {
function timestampForBackup() {
return new Date().toISOString().replace(/[-:.]/g, "");
}
-function isRecord16(value) {
+function isRecord15(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
async function readPackageName(packagePath) {
@@ -99810,7 +99737,7 @@ async function readPackageName(packagePath) {
return;
throw error;
}
- return isRecord16(parsed) && typeof parsed.name === "string" ? parsed.name : undefined;
+ return isRecord15(parsed) && typeof parsed.name === "string" ? parsed.name : undefined;
}
async function fileExists(path) {
try {
@@ -99838,7 +99765,6 @@ var REQUIRED_PLUGIN_ARTIFACTS = [
join63("extensions", "omo-memory-commands.js"),
join63("extensions", "omo-session-history.js"),
join63("extensions", "omo-recall-store.js"),
- join63("extensions", "omo-dori-migration.js"),
join63("extensions", "omo-telemetry-posthog.js"),
join63("extensions", "omo-memory-sync.js"),
join63("extensions", "loro_wasm_bg.wasm"),
@@ -99935,7 +99861,7 @@ async function verifyAstGrepRuntimeIntegrity(pluginPath, platform) {
}
let actualSha256;
try {
- actualSha256 = createHash6("sha256").update(await readFile29(runtimeEntry)).digest("hex");
+ actualSha256 = createHash7("sha256").update(await readFile29(runtimeEntry)).digest("hex");
} catch (error) {
throw astGrepIntegrityError(runtimeEntry, `runtime hash could not be computed: ${messageOf(error)}`);
}
@@ -99948,7 +99874,7 @@ async function verifyAstGrepRuntimeIntegrity(pluginPath, platform) {
}
}
function isAstGrepRuntimeManifest(value) {
- if (!isRecord16(value))
+ if (!isRecord15(value))
return false;
return typeof value.sha256 === "string" && /^[a-f0-9]{64}$/.test(value.sha256) && typeof value.mode === "number" && Number.isInteger(value.mode) && typeof value.stagedAtUtc === "string" && !Number.isNaN(Date.parse(value.stagedAtUtc));
}
@@ -108046,13 +107972,11 @@ async function checkModels() {
// packages/omo-opencode/src/cli/doctor/checks/legacy-config-leftovers.ts
init_config_migration();
-init_dori_section_rename();
import { posix as posix12, win32 as win329 } from "node:path";
function findLegacyConfigLeftovers(options) {
const pathOperations = options.pathOperations ?? (process.platform === "win32" ? win329 : posix12);
const paths = createLegacyConfigMigrationPlans({
cwd: options.cwd,
- doriSectionRename,
environment: options.environment ?? process.env,
homeDir: options.homeDir,
pathOperations,
@@ -108251,14 +108175,14 @@ var CANONICAL_REPLACEMENT = new Map([
var MIGRATE_SUFFIX = ", or run: oh-my-openagent config migrate";
var TUNING_CONTAINERS = new Set(["agents", "categories", "models"]);
var PASSTHROUGH_CONTAINERS = new Set(["provider_options", "providerOptions"]);
-function isRecord17(value) {
+function isRecord16(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function isHarnessBlock(key) {
return key.startsWith("[") && key.endsWith("]");
}
function collectIssues(configPath, value, prefix, fixSuffix) {
- if (!isRecord17(value))
+ if (!isRecord16(value))
return [];
const issues = [];
for (const [key, child] of Object.entries(value)) {
@@ -108274,7 +108198,7 @@ function collectIssues(configPath, value, prefix, fixSuffix) {
});
continue;
}
- if (!isRecord17(child))
+ if (!isRecord16(child))
continue;
if (PASSTHROUGH_CONTAINERS.has(key))
continue;
@@ -108293,7 +108217,7 @@ function collectIssues(configPath, value, prefix, fixSuffix) {
return issues;
}
function migrateFixSuffix(parsed) {
- if (isRecord17(parsed) && hasMigrationMarker(parsed, REASONING_UNIFICATION_MIGRATION_ID))
+ if (isRecord16(parsed) && hasMigrationMarker(parsed, REASONING_UNIFICATION_MIGRATION_ID))
return "";
return MIGRATE_SUFFIX;
}
@@ -109158,10 +109082,6 @@ var package_default3 = {
types: "./src/cli/service-auth/index.ts",
import: "./src/cli/service-auth/index.ts"
},
- "./config-migration/dori-section-rename": {
- types: "./src/config-migration/dori-section-rename.ts",
- import: "./src/config-migration/dori-section-rename.ts"
- },
"./binary-downloader": {
types: "./src/shared/binary-downloader.ts",
import: "./src/shared/binary-downloader.ts"
@@ -109592,12 +109512,12 @@ async function auditBundleTargets(pluginRoot) {
let referencedCount = 0;
for (const manifestPath of await findManifestPaths(pluginRoot, ".mcp.json")) {
const manifest = await readJson2(manifestPath);
- if (manifest === null || !isRecord19(manifest["mcpServers"]))
+ if (manifest === null || !isRecord18(manifest["mcpServers"]))
continue;
const manifestRoot = dirname39(manifestPath);
const isRootManifest = resolve28(manifestRoot) === resolve28(pluginRoot);
for (const server of Object.values(manifest["mcpServers"])) {
- if (!isRecord19(server) || !Array.isArray(server["args"]))
+ if (!isRecord18(server) || !Array.isArray(server["args"]))
continue;
for (const arg of server["args"]) {
if (typeof arg !== "string" || !isPluginRuntimePathArg(arg))
@@ -109683,7 +109603,7 @@ function collectHookCommands(value, commands) {
collectHookCommands(item, commands);
return;
}
- if (!isRecord19(value))
+ if (!isRecord18(value))
return;
if (value["type"] === "command") {
if (typeof value["command"] === "string")
@@ -109738,7 +109658,7 @@ function parseDegradedEntries(value) {
return [];
const entries = [];
for (const item of value) {
- if (!isRecord19(item))
+ if (!isRecord18(item))
continue;
if (typeof item["component"] !== "string" || typeof item["reason"] !== "string")
continue;
@@ -109772,7 +109692,7 @@ function degradedDetailLines(entries) {
async function readJson2(path) {
try {
const parsed = JSON.parse(await readFile31(path, "utf8"));
- return isRecord19(parsed) ? parsed : null;
+ return isRecord18(parsed) ? parsed : null;
} catch (error) {
if (error instanceof Error)
return null;
@@ -109793,7 +109713,7 @@ function normalizeRelative(root, target) {
function normalizePathSeparators(path) {
return path.split("\\").join("/");
}
-function isRecord19(value) {
+function isRecord18(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
@@ -110338,7 +110258,7 @@ Doctor failed unexpectedly: ${message}`];
init_commander();
// packages/mcp-client-core/src/mcp-oauth/storage.ts
-import { createHash as createHash7 } from "node:crypto";
+import { createHash as createHash8 } from "node:crypto";
import {
chmodSync as chmodSync5,
existsSync as existsSync65,
@@ -110433,7 +110353,7 @@ function getMcpOauthStorageDir() {
return join101(getOpenCodeCliConfigDir(), STORAGE_DIR_NAME);
}
function getMcpOauthServerHash(serverHost, resource) {
- return createHash7("sha256").update(buildKey(serverHost, resource)).digest("hex").slice(0, 32);
+ return createHash8("sha256").update(buildKey(serverHost, resource)).digest("hex").slice(0, 32);
}
function getMcpOauthStoragePath(serverHost, resource) {
return join101(getMcpOauthStorageDir(), `${getMcpOauthServerHash(serverHost, resource)}.json`);
@@ -110807,13 +110727,13 @@ async function findAvailablePort2(startPort = DEFAULT_PORT) {
// packages/mcp-client-core/src/mcp-oauth/oauth-authorization-flow.ts
import { spawn as spawn4 } from "node:child_process";
-import { createHash as createHash8, randomBytes as randomBytes2 } from "node:crypto";
+import { createHash as createHash9, randomBytes as randomBytes2 } from "node:crypto";
import { createServer as createServer2 } from "node:http";
function generateCodeVerifier() {
return randomBytes2(32).toString("base64url");
}
function generateCodeChallenge(verifier) {
- return createHash8("sha256").update(verifier).digest("base64url");
+ return createHash9("sha256").update(verifier).digest("base64url");
}
function buildAuthorizationUrl(authorizationEndpoint, options) {
const url = new URL(authorizationEndpoint);
@@ -112175,7 +112095,7 @@ async function loginDevice(session, options) {
}
// packages/omo-opencode/src/cli/service-auth/keystore.ts
-import { createHash as createHash9 } from "node:crypto";
+import { createHash as createHash10 } from "node:crypto";
// packages/omo-opencode/src/cli/service-auth/secret-service-lock.ts
import { spawn as spawn5 } from "node:child_process";
@@ -112236,7 +112156,7 @@ var stderrNoticeOncePerProcess = (message) => {
};
var isLocked = (error) => error instanceof Error && (LOCKED.test(error.message) || LOCKED.test(String(error.code ?? "")));
function credentialId(api) {
- return createHash9("sha256").update(serviceOrigin(api)).digest("hex");
+ return createHash10("sha256").update(serviceOrigin(api)).digest("hex");
}
function createCredentialStore(api, secrets = typeof Bun === "undefined" ? null : Bun.secrets, platform = process.platform, lockProbe = probeSecretServiceLock, notice = stderrNoticeOncePerProcess) {
if (!secrets)
@@ -112300,7 +112220,7 @@ function createCredentialStore(api, secrets = typeof Bun === "undefined" ? null
}
// packages/omo-opencode/src/cli/service-auth/loopback.ts
-import { createHash as createHash10, randomBytes as randomBytes3, timingSafeEqual } from "node:crypto";
+import { createHash as createHash11, randomBytes as randomBytes3, timingSafeEqual } from "node:crypto";
import { createServer as createServer3 } from "node:http";
class BrowserUnavailable extends SignInError {
}
@@ -112346,7 +112266,7 @@ async function authorize(options) {
const url = new URL(serviceOrigin(options.accounts));
url.search = new URLSearchParams({
redirect_uri: `${origin}/callback`,
- code_challenge: createHash10("sha256").update(verifier).digest("base64url"),
+ code_challenge: createHash11("sha256").update(verifier).digest("base64url"),
code_challenge_method: "S256",
state
}).toString();
@@ -112535,7 +112455,7 @@ async function publishLockRecord(lockPath, record) {
}
// packages/team-core/src/team-state-store/lock-reclaim.ts
-import { createHash as createHash11 } from "node:crypto";
+import { createHash as createHash12 } from "node:crypto";
import { unlink as unlink3 } from "node:fs/promises";
var RECLAIM_UNLINK_ATTEMPTS = 3;
var RECLAIM_UNLINK_RETRY_MS = 25;
@@ -112545,7 +112465,7 @@ function delay2(ms) {
});
}
function reclaimGuardPath(lockPath, record) {
- return `${lockPath}.reclaim-${createHash11("sha256").update(record).digest("hex").slice(0, 16)}`;
+ return `${lockPath}.reclaim-${createHash12("sha256").update(record).digest("hex").slice(0, 16)}`;
}
async function unlinkWithRetry(path, deps) {
const wait = deps.delay ?? delay2;
diff --git a/plugins/omo/dist/cli/index.js b/plugins/omo/dist/cli/index.js
index ccc30f65..19af7029 100755
--- a/plugins/omo/dist/cli/index.js
+++ b/plugins/omo/dist/cli/index.js
@@ -2132,7 +2132,7 @@ var package_default;
var init_package = __esm(() => {
package_default = {
name: "oh-my-opencode",
- version: "5.1.45",
+ version: "5.1.46",
description: "The Best AI Agent Harness - Batteries-Included OpenCode Plugin with Multi-Model Orchestration, Parallel Background Agents, and Crafted LSP/AST Tools",
main: "./dist/index.js",
types: "dist/index.d.ts",
@@ -2378,18 +2378,18 @@ var init_package = __esm(() => {
typescript: "^7.0.2"
},
optionalDependencies: {
- "oh-my-opencode-darwin-arm64": "5.1.45",
- "oh-my-opencode-darwin-x64": "5.1.45",
- "oh-my-opencode-darwin-x64-baseline": "5.1.45",
- "oh-my-opencode-linux-arm64": "5.1.45",
- "oh-my-opencode-linux-arm64-musl": "5.1.45",
- "oh-my-opencode-linux-x64": "5.1.45",
- "oh-my-opencode-linux-x64-baseline": "5.1.45",
- "oh-my-opencode-linux-x64-musl": "5.1.45",
- "oh-my-opencode-linux-x64-musl-baseline": "5.1.45",
- "oh-my-opencode-windows-arm64": "5.1.45",
- "oh-my-opencode-windows-x64": "5.1.45",
- "oh-my-opencode-windows-x64-baseline": "5.1.45"
+ "oh-my-opencode-darwin-arm64": "5.1.46",
+ "oh-my-opencode-darwin-x64": "5.1.46",
+ "oh-my-opencode-darwin-x64-baseline": "5.1.46",
+ "oh-my-opencode-linux-arm64": "5.1.46",
+ "oh-my-opencode-linux-arm64-musl": "5.1.46",
+ "oh-my-opencode-linux-x64": "5.1.46",
+ "oh-my-opencode-linux-x64-baseline": "5.1.46",
+ "oh-my-opencode-linux-x64-musl": "5.1.46",
+ "oh-my-opencode-linux-x64-musl-baseline": "5.1.46",
+ "oh-my-opencode-windows-arm64": "5.1.46",
+ "oh-my-opencode-windows-x64": "5.1.46",
+ "oh-my-opencode-windows-x64-baseline": "5.1.46"
},
overrides: {
hono: "^4.13.8",
@@ -76903,6 +76903,7 @@ var init_memory = __esm(() => {
merge: _enum(["auto", "integration"]).default("auto"),
category: string2().min(1).default("quick"),
timeout_minutes: number2().int().positive().default(15),
+ max_concurrent_runs: number2().int().positive().default(1),
sandbox: _enum(["auto", "required", "off"]).default("auto")
}).strict();
OmoMemorySyncSchema = object({
@@ -76974,6 +76975,7 @@ var init_memory = __esm(() => {
merge: _enum(["auto", "integration"]).optional(),
category: string2().min(1).optional(),
timeout_minutes: number2().int().positive().optional(),
+ max_concurrent_runs: number2().int().positive().optional(),
sandbox: _enum(["auto", "required", "off"]).optional()
}).strict();
OmoMemorySyncLayerSchema = object({
@@ -77038,7 +77040,7 @@ var init_memory = __esm(() => {
OmoMemoryAgentOverridesSchema = object({
enabled: boolean2().optional(),
agent: string2().min(1).optional(),
- reflection: OmoMemoryReflectionLayerSchema.optional(),
+ reflection: OmoMemoryReflectionLayerSchema.omit({ max_concurrent_runs: true }).optional(),
nudge: OmoMemoryNudgeLayerSchema.optional(),
facts: OmoMemoryFactsLayerSchema.optional(),
dream: OmoMemoryDreamLayerSchema.optional(),
@@ -77063,6 +77065,7 @@ var init_memory = __esm(() => {
merge: "auto",
category: "quick",
timeout_minutes: 15,
+ max_concurrent_runs: 1,
sandbox: "auto"
}),
nudge: OmoMemoryNudgeSchema.default({ enabled: true, every_user_turns: 10 }),
@@ -77437,13 +77440,6 @@ var init_format_on_mutation = __esm(() => {
}).strict();
});
-// packages/omo-config-core/src/schema/dori.ts
-var OmoDoriSectionSchema;
-var init_dori = __esm(() => {
- init_zod();
- OmoDoriSectionSchema = record(string2(), unknown()).describe("Chat-surface dori settings, owned and validated by a separately installed dori package. omo accepts the key and never reads it.");
-});
-
// packages/omo-config-core/src/schema/provenance.ts
var OmoProvenanceSettingsSchema;
var init_provenance = __esm(() => {
@@ -77473,7 +77469,6 @@ var init_config = __esm(() => {
init_team();
init_telemetry();
init_format_on_mutation();
- init_dori();
init_provenance();
OmoOpenCodeHarnessConfigSchema = record(string2(), unknown());
OmoDisabledSkillsSchema = array(string2());
@@ -77481,7 +77476,6 @@ var init_config = __esm(() => {
OmoTypedHarnessConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -77500,7 +77494,6 @@ var init_config = __esm(() => {
OmoConfigProfileSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
git_master: OmoGitMasterSettingsLayerSchema.optional(),
@@ -77523,7 +77516,6 @@ var init_config = __esm(() => {
OmoConfigSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -77550,7 +77542,6 @@ var init_config = __esm(() => {
OmoConfigLayerSchema = object({
provenance: OmoProvenanceSettingsSchema.optional(),
formatOnMutation: OmoFormatOnMutationLayerSchema.optional(),
- dori: OmoDoriSectionSchema.optional(),
$schema: string2().optional(),
categories: OmoCategoriesConfigSchema.optional(),
agents: OmoAgentsConfigSchema.optional(),
@@ -77692,54 +77683,6 @@ var init_legacy_harness_names = __esm(() => {
init_harness();
});
-// packages/omo-config-core/src/schema/legacy-dori-section.ts
-function isRecord11(value) {
- return value !== null && typeof value === "object" && !Array.isArray(value);
-}
-function migrateLegacyDoriSection(document) {
- if (!isRecord11(document))
- return { document: {}, diagnostics: [] };
- const diagnostics = [];
- const rename = (container, path) => {
- const result = { ...container };
- if (Object.hasOwn(container, "gateway")) {
- if (Object.hasOwn(container, "dori")) {
- diagnostics.push(`${[...path, "gateway"].join(".")} removed: dori is already configured`);
- } else {
- result.dori = container.gateway;
- diagnostics.push(`${[...path, "gateway"].join(".")} renamed to dori`);
- }
- delete result.gateway;
- }
- return result;
- };
- const layer = (container, path) => {
- const result = rename(container, path);
- for (const key of HARNESS_BLOCKS) {
- const block = container[key];
- if (isRecord11(block))
- result[key] = rename(block, [...path, key]);
- }
- return result;
- };
- const result = layer(document, []);
- if (isRecord11(document.profiles)) {
- const profiles = {};
- for (const [name, profile] of Object.entries(document.profiles)) {
- profiles[name] = isRecord11(profile) ? layer(profile, ["profiles", name]) : profile;
- }
- result.profiles = profiles;
- }
- return { document: result, diagnostics };
-}
-function hasLegacyGatewaySection(document) {
- return migrateLegacyDoriSection(document).diagnostics.length > 0;
-}
-var HARNESS_BLOCKS;
-var init_legacy_dori_section = __esm(() => {
- HARNESS_BLOCKS = ["[opencode]", "[native]", "[senpi]", "[codex]"];
-});
-
// packages/omo-config-core/src/schema/index.ts
var init_schema = __esm(() => {
init_agent();
@@ -77747,12 +77690,10 @@ var init_schema = __esm(() => {
init_config();
init_fallback_models();
init_format_on_mutation();
- init_dori();
init_git_master();
init_harness();
init_legacy_category_names();
init_legacy_harness_names();
- init_legacy_dori_section();
init_memory();
init_model_catalog();
init_model_profile();
@@ -78023,6 +77964,7 @@ function pruneInvalidConfigPaths(config, issues, validate, maxPasses = MAX_PRUNE
var MAX_PRUNE_PASSES = 32;
// packages/omo-config-core/src/loader/layer-validation.ts
+import { createHash as createHash2 } from "crypto";
function validationDiagnostic(path, issues) {
const issuePaths = issues.map((issue) => issue.path.map((segment) => String(segment)).join("."));
return {
@@ -78043,9 +77985,12 @@ function invalidValueDiagnostics(path, dropped) {
function unrecognizedKeyIssues(issues) {
return issues.flatMap((issue) => issue.code === "unrecognized_keys" ? [{ keys: issue.keys, path: issue.path.map((segment) => String(segment)) }] : []);
}
-function isRecord12(value) {
+function isRecord11(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+function isRetiredSection(key) {
+ return RETIRED_SECTION_DIGESTS.has(createHash2("sha256").update(key.toLowerCase()).digest("hex"));
+}
function sanitizeUnsafeKeys(value, path = []) {
if (Array.isArray(value)) {
const issues = [];
@@ -78056,7 +78001,7 @@ function sanitizeUnsafeKeys(value, path = []) {
});
return { issues, value: sanitized };
}
- if (!isRecord12(value))
+ if (!isRecord11(value))
return { issues: [], value };
const issues = [];
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {
@@ -78064,6 +78009,9 @@ function sanitizeUnsafeKeys(value, path = []) {
}
const sanitized = {};
for (const [key, entry] of Object.entries(value)) {
+ const isConfigContainer = path.length === 0 || path.length === 1 && HARNESS_BLOCKS.has(path[0] ?? "") || path.length === 2 && path[0] === "profiles" || path.length === 3 && path[0] === "profiles" && HARNESS_BLOCKS.has(path[2] ?? "");
+ if (isConfigContainer && isRetiredSection(key))
+ continue;
if (isUnsafeObjectKey2(key)) {
issues.push({ keys: [key], path });
continue;
@@ -78082,13 +78030,13 @@ function containerAt(record, path) {
if (!Number.isInteger(index) || index < 0 || index >= node.length)
return null;
node = node[index];
- } else if (isRecord12(node) && Object.hasOwn(node, segment)) {
+ } else if (isRecord11(node) && Object.hasOwn(node, segment)) {
node = node[segment];
} else {
return null;
}
}
- return isRecord12(node) ? node : null;
+ return isRecord11(node) ? node : null;
}
function stripUnrecognizedKeys(record, issues) {
const stripped = structuredClone(record);
@@ -78150,13 +78098,18 @@ function validateConfigLayer(path, data) {
return rejected;
return { loaded: true, diagnostics: [...diagnostics, ...invalidValueDiagnostics(path, pruned.dropped)], value: pruned.config };
}
-var validateLayerRecord = (record) => {
+var RETIRED_SECTION_DIGESTS, HARNESS_BLOCKS, validateLayerRecord = (record) => {
const parsed = OmoConfigLayerSchema.safeParse(record);
return parsed.success ? { success: true } : { success: false, issues: parsed.error.issues };
};
var init_layer_validation = __esm(() => {
init_schema();
init_merge();
+ RETIRED_SECTION_DIGESTS = new Set([
+ "447f75a8ebdbdb79a57f0b8cebbfba183820cf54a8119ac01449b30499e5ef0e",
+ "4ea5ee68fea05586106890ded5733820bb77d919cda27bc4b8139b7cd33b8889"
+ ]);
+ HARNESS_BLOCKS = new Set(["[opencode]", "[native]", "[senpi]", "[codex]"]);
});
// packages/omo-config-core/src/internal/posix-path.ts
@@ -81716,18 +81669,6 @@ function subscriptionProviderRenamePlan(targetPath) {
transform: inspect
};
}
-function doriSectionRenamePlan(targetPath, rename) {
- const inspect = (sources) => rename.transform(sources[0]?.value);
- return {
- id: DORI_SECTION_RENAME_MIGRATION_ID,
- inspect,
- mode: "replace-target",
- shouldRun: rename.shouldRun,
- sources: [],
- targetPath,
- transform: inspect
- };
-}
function existingOmoConfigPath(directory, options) {
const fileSystem = discoveryFileSystem(options);
for (const fileName of ["omo.jsonc", "omo.json"]) {
@@ -81789,7 +81730,6 @@ function createLegacyConfigMigrationPlans(options) {
addReasoningTarget(plan.targetPath);
const inPlaceTargets = [...reasoningTargets.values()];
return [
- ...inPlaceTargets.map((targetPath) => doriSectionRenamePlan(targetPath, options.doriSectionRename)),
...legacyPlans,
...inPlaceTargets.map(reasoningPlan),
...inPlaceTargets.map(categoryDeepSplitPlan),
@@ -81797,7 +81737,6 @@ function createLegacyConfigMigrationPlans(options) {
...inPlaceTargets.map(subscriptionProviderRenamePlan)
];
}
-var DORI_SECTION_RENAME_MIGRATION_ID = "2026-10-dori-section-rename";
var init_migration_plans = __esm(() => {
init_discovery2();
init_discovery_paths2();
@@ -81824,16 +81763,6 @@ var init_config_migration = __esm(() => {
init_reasoning_unification();
});
-// packages/omo-opencode/src/config-migration/dori-section-rename.ts
-var doriSectionRename;
-var init_dori_section_rename = __esm(() => {
- init_src4();
- doriSectionRename = {
- shouldRun: hasLegacyGatewaySection,
- transform: migrateLegacyDoriSection
- };
-});
-
// packages/omo-opencode/src/startup-migration.ts
import { posix as posix5 } from "path";
function homeDirectory(options) {
@@ -81873,7 +81802,6 @@ function runOpenCodeStartupMigration(options) {
...options.backupTimestamp === undefined ? {} : { backupTimestamp: options.backupTimestamp },
cwd: options.cwd,
...options.discoveryFileSystem === undefined ? {} : { fileSystem: options.discoveryFileSystem },
- doriSectionRename,
environment: options.environment ?? process.env,
homeDir,
pathOperations: options.pathOperations ?? posix5,
@@ -81910,7 +81838,6 @@ function runOpenCodeStartupMigration(options) {
var init_startup_migration = __esm(() => {
init_src4();
init_config_migration();
- init_dori_section_rename();
});
// packages/omo-opencode/src/cli/config-manager/deep-merge-record.ts
@@ -82652,7 +82579,7 @@ function shouldRetainLine(line, cutoffMs) {
function parseDiagnosticLine(line) {
try {
const parsed = JSON.parse(line);
- if (!isRecord14(parsed)) {
+ if (!isRecord13(parsed)) {
return null;
}
return parsed;
@@ -82663,7 +82590,7 @@ function parseDiagnosticLine(line) {
throw error;
}
}
-function isRecord14(value) {
+function isRecord13(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function trimToMaxBytes(lines) {
@@ -82783,13 +82710,13 @@ var init_event_id = __esm(() => {
});
// packages/telemetry-core/src/machine-id.ts
-import { createHash as createHash5 } from "crypto";
+import { createHash as createHash6 } from "crypto";
import os4 from "os";
function getDefaultTelemetryOsProvider() {
return os4;
}
function getTelemetryDistinctId(machineIdPrefix, osProvider = getDefaultTelemetryOsProvider()) {
- return createHash5("sha256").update(`${machineIdPrefix}${osProvider.hostname()}`).digest("hex");
+ return createHash6("sha256").update(`${machineIdPrefix}${osProvider.hostname()}`).digest("hex");
}
var init_machine_id = () => {};
@@ -90351,7 +90278,7 @@ function coerceBool(value) {
function coerceString(value) {
return typeof value == "string" ? value : undefined;
}
-function isRecord15(value) {
+function isRecord14(value) {
return typeof value == "object" && value !== null && !Array.isArray(value);
}
function toRfc3339(timestamp) {
@@ -90373,7 +90300,7 @@ function relocateInto(properties, key, value) {
properties[key] = value;
}
function buildV1Event(message) {
- const sourceProperties = isRecord15(message.properties) ? message.properties : {};
+ const sourceProperties = isRecord14(message.properties) ? message.properties : {};
const properties = {
...sourceProperties
};
@@ -92454,7 +92381,7 @@ var package_default2;
var init_package2 = __esm(() => {
package_default2 = {
name: "@oh-my-opencode/omo-codex",
- version: "5.1.45",
+ version: "5.1.46",
type: "module",
private: true,
description: "Codex harness adapter for oh-my-openagent. Vendored Codex plugin namespace (omo) + TypeScript installer + telemetry.",
@@ -96624,10 +96551,10 @@ function readCatalogMultiAgentVersion(model, cachePath) {
} catch {
return null;
}
- if (!isRecord13(cache) || !Array.isArray(cache.models))
+ if (!isRecord12(cache) || !Array.isArray(cache.models))
return null;
for (const entry of cache.models) {
- if (!isRecord13(entry))
+ if (!isRecord12(entry))
continue;
if (entry.slug !== model && entry.id !== model)
continue;
@@ -96652,7 +96579,7 @@ function readRootModelCatalogPath(config) {
const single = config.match(/^\s*model_catalog_json\s*=\s*'([^']+)'/m);
return single?.[1] ?? null;
}
-function isRecord13(value) {
+function isRecord12(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function removeFeatureFlagSetting(config, featureName) {
@@ -96770,7 +96697,7 @@ function isMissingFileError(error) {
}
// packages/omo-codex/src/install/codex-hook-trust.ts
-import { createHash as createHash2 } from "crypto";
+import { createHash as createHash3 } from "crypto";
import { readFile as readFile12 } from "fs/promises";
import { join as join38 } from "path";
var EVENT_LABELS = new Map([
@@ -96862,7 +96789,7 @@ function commandHookHash(eventName, matcher, handler, command) {
if (typeof matcher === "string")
identity.matcher = matcher;
const canonical = JSON.stringify(canonicalJson(identity));
- return `sha256:${createHash2("sha256").update(canonical).digest("hex")}`;
+ return `sha256:${createHash3("sha256").update(canonical).digest("hex")}`;
}
function canonicalJson(value) {
if (Array.isArray(value))
@@ -97541,7 +97468,7 @@ function nodeErrorCode2(error) {
}
// packages/omo-codex/src/install/default-agent-role.ts
-import { createHash as createHash3 } from "crypto";
+import { createHash as createHash4 } from "crypto";
import { lstat as lstat9, readFile as readFile16, rm as rm10 } from "fs/promises";
import { join as join43 } from "path";
var REGISTRATION = { name: "default", configFile: "./agents/default.toml" };
@@ -97597,7 +97524,7 @@ async function readIfPresent(path) {
}
}
function hash(content) {
- return createHash3("sha256").update(content).digest("hex");
+ return createHash4("sha256").update(content).digest("hex");
}
// packages/omo-codex/src/install/link-cached-plugin-agents.ts
@@ -98262,7 +98189,7 @@ function formatUnknownError(error) {
}
// packages/omo-codex/src/install/lsp-daemon-reaper.ts
-import { createHash as createHash4 } from "crypto";
+import { createHash as createHash5 } from "crypto";
import { lstat as lstat12, readFile as readFile22, readdir as readdir10, rm as rm12 } from "fs/promises";
import { tmpdir as tmpdir3 } from "os";
import { join as join48, posix as posix6 } from "path";
@@ -98540,7 +98467,7 @@ async function readRegularTrimmedFile(path) {
return content.length > 0 ? content : null;
}
function shortDigest(value) {
- return createHash4("sha256").update(value).digest("hex").slice(0, 16);
+ return createHash5("sha256").update(value).digest("hex").slice(0, 16);
}
async function removeVersionDir(path) {
await rm12(path, { recursive: true, force: true });
@@ -99611,7 +99538,7 @@ node "%~dp0omo" %*\r
}
// packages/omo-senpi/src/install/plugin-artifacts.ts
-import { createHash as createHash6 } from "crypto";
+import { createHash as createHash7 } from "crypto";
import { constants as constants9 } from "fs";
import { access as access2, readFile as readFile29, stat as stat6 } from "fs/promises";
import { dirname as dirname24, join as join63 } from "path";
@@ -99650,7 +99577,7 @@ async function readSettings(settingsPath) {
throw error;
}
const parsed = JSON.parse(raw);
- if (!isRecord16(parsed))
+ if (!isRecord15(parsed))
throw new Error(`${settingsPath} must contain a JSON object`);
return parsed;
}
@@ -99711,7 +99638,7 @@ async function nextBackupPath(settingsPath) {
function timestampForBackup() {
return new Date().toISOString().replace(/[-:.]/g, "");
}
-function isRecord16(value) {
+function isRecord15(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
async function readPackageName(packagePath) {
@@ -99731,7 +99658,7 @@ async function readPackageName(packagePath) {
return;
throw error;
}
- return isRecord16(parsed) && typeof parsed.name === "string" ? parsed.name : undefined;
+ return isRecord15(parsed) && typeof parsed.name === "string" ? parsed.name : undefined;
}
async function fileExists(path) {
try {
@@ -99759,7 +99686,6 @@ var REQUIRED_PLUGIN_ARTIFACTS = [
join63("extensions", "omo-memory-commands.js"),
join63("extensions", "omo-session-history.js"),
join63("extensions", "omo-recall-store.js"),
- join63("extensions", "omo-dori-migration.js"),
join63("extensions", "omo-telemetry-posthog.js"),
join63("extensions", "omo-memory-sync.js"),
join63("extensions", "loro_wasm_bg.wasm"),
@@ -99856,7 +99782,7 @@ async function verifyAstGrepRuntimeIntegrity(pluginPath, platform) {
}
let actualSha256;
try {
- actualSha256 = createHash6("sha256").update(await readFile29(runtimeEntry)).digest("hex");
+ actualSha256 = createHash7("sha256").update(await readFile29(runtimeEntry)).digest("hex");
} catch (error) {
throw astGrepIntegrityError(runtimeEntry, `runtime hash could not be computed: ${messageOf(error)}`);
}
@@ -99869,7 +99795,7 @@ async function verifyAstGrepRuntimeIntegrity(pluginPath, platform) {
}
}
function isAstGrepRuntimeManifest(value) {
- if (!isRecord16(value))
+ if (!isRecord15(value))
return false;
return typeof value.sha256 === "string" && /^[a-f0-9]{64}$/.test(value.sha256) && typeof value.mode === "number" && Number.isInteger(value.mode) && typeof value.stagedAtUtc === "string" && !Number.isNaN(Date.parse(value.stagedAtUtc));
}
@@ -107967,13 +107893,11 @@ async function checkModels() {
// packages/omo-opencode/src/cli/doctor/checks/legacy-config-leftovers.ts
init_config_migration();
-init_dori_section_rename();
import { posix as posix12, win32 as win329 } from "path";
function findLegacyConfigLeftovers(options) {
const pathOperations = options.pathOperations ?? (process.platform === "win32" ? win329 : posix12);
const paths = createLegacyConfigMigrationPlans({
cwd: options.cwd,
- doriSectionRename,
environment: options.environment ?? process.env,
homeDir: options.homeDir,
pathOperations,
@@ -108172,14 +108096,14 @@ var CANONICAL_REPLACEMENT = new Map([
var MIGRATE_SUFFIX = ", or run: oh-my-openagent config migrate";
var TUNING_CONTAINERS = new Set(["agents", "categories", "models"]);
var PASSTHROUGH_CONTAINERS = new Set(["provider_options", "providerOptions"]);
-function isRecord17(value) {
+function isRecord16(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function isHarnessBlock(key) {
return key.startsWith("[") && key.endsWith("]");
}
function collectIssues(configPath, value, prefix, fixSuffix) {
- if (!isRecord17(value))
+ if (!isRecord16(value))
return [];
const issues = [];
for (const [key, child] of Object.entries(value)) {
@@ -108195,7 +108119,7 @@ function collectIssues(configPath, value, prefix, fixSuffix) {
});
continue;
}
- if (!isRecord17(child))
+ if (!isRecord16(child))
continue;
if (PASSTHROUGH_CONTAINERS.has(key))
continue;
@@ -108214,7 +108138,7 @@ function collectIssues(configPath, value, prefix, fixSuffix) {
return issues;
}
function migrateFixSuffix(parsed) {
- if (isRecord17(parsed) && hasMigrationMarker(parsed, REASONING_UNIFICATION_MIGRATION_ID))
+ if (isRecord16(parsed) && hasMigrationMarker(parsed, REASONING_UNIFICATION_MIGRATION_ID))
return "";
return MIGRATE_SUFFIX;
}
@@ -109079,10 +109003,6 @@ var package_default3 = {
types: "./src/cli/service-auth/index.ts",
import: "./src/cli/service-auth/index.ts"
},
- "./config-migration/dori-section-rename": {
- types: "./src/config-migration/dori-section-rename.ts",
- import: "./src/config-migration/dori-section-rename.ts"
- },
"./binary-downloader": {
types: "./src/shared/binary-downloader.ts",
import: "./src/shared/binary-downloader.ts"
@@ -109513,12 +109433,12 @@ async function auditBundleTargets(pluginRoot) {
let referencedCount = 0;
for (const manifestPath of await findManifestPaths(pluginRoot, ".mcp.json")) {
const manifest = await readJson2(manifestPath);
- if (manifest === null || !isRecord19(manifest["mcpServers"]))
+ if (manifest === null || !isRecord18(manifest["mcpServers"]))
continue;
const manifestRoot = dirname39(manifestPath);
const isRootManifest = resolve28(manifestRoot) === resolve28(pluginRoot);
for (const server of Object.values(manifest["mcpServers"])) {
- if (!isRecord19(server) || !Array.isArray(server["args"]))
+ if (!isRecord18(server) || !Array.isArray(server["args"]))
continue;
for (const arg of server["args"]) {
if (typeof arg !== "string" || !isPluginRuntimePathArg(arg))
@@ -109604,7 +109524,7 @@ function collectHookCommands(value, commands) {
collectHookCommands(item, commands);
return;
}
- if (!isRecord19(value))
+ if (!isRecord18(value))
return;
if (value["type"] === "command") {
if (typeof value["command"] === "string")
@@ -109659,7 +109579,7 @@ function parseDegradedEntries(value) {
return [];
const entries = [];
for (const item of value) {
- if (!isRecord19(item))
+ if (!isRecord18(item))
continue;
if (typeof item["component"] !== "string" || typeof item["reason"] !== "string")
continue;
@@ -109693,7 +109613,7 @@ function degradedDetailLines(entries) {
async function readJson2(path) {
try {
const parsed = JSON.parse(await readFile31(path, "utf8"));
- return isRecord19(parsed) ? parsed : null;
+ return isRecord18(parsed) ? parsed : null;
} catch (error) {
if (error instanceof Error)
return null;
@@ -109714,7 +109634,7 @@ function normalizeRelative(root, target) {
function normalizePathSeparators(path) {
return path.split("\\").join("/");
}
-function isRecord19(value) {
+function isRecord18(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
@@ -110259,7 +110179,7 @@ Doctor failed unexpectedly: ${message}`];
init_commander();
// packages/mcp-client-core/src/mcp-oauth/storage.ts
-import { createHash as createHash7 } from "crypto";
+import { createHash as createHash8 } from "crypto";
import {
chmodSync as chmodSync5,
existsSync as existsSync65,
@@ -110354,7 +110274,7 @@ function getMcpOauthStorageDir() {
return join101(getOpenCodeCliConfigDir(), STORAGE_DIR_NAME);
}
function getMcpOauthServerHash(serverHost, resource) {
- return createHash7("sha256").update(buildKey(serverHost, resource)).digest("hex").slice(0, 32);
+ return createHash8("sha256").update(buildKey(serverHost, resource)).digest("hex").slice(0, 32);
}
function getMcpOauthStoragePath(serverHost, resource) {
return join101(getMcpOauthStorageDir(), `${getMcpOauthServerHash(serverHost, resource)}.json`);
@@ -110728,13 +110648,13 @@ async function findAvailablePort2(startPort = DEFAULT_PORT) {
// packages/mcp-client-core/src/mcp-oauth/oauth-authorization-flow.ts
import { spawn as spawn4 } from "child_process";
-import { createHash as createHash8, randomBytes as randomBytes2 } from "crypto";
+import { createHash as createHash9, randomBytes as randomBytes2 } from "crypto";
import { createServer as createServer2 } from "http";
function generateCodeVerifier() {
return randomBytes2(32).toString("base64url");
}
function generateCodeChallenge(verifier) {
- return createHash8("sha256").update(verifier).digest("base64url");
+ return createHash9("sha256").update(verifier).digest("base64url");
}
function buildAuthorizationUrl(authorizationEndpoint, options) {
const url = new URL(authorizationEndpoint);
@@ -112096,7 +112016,7 @@ async function loginDevice(session, options) {
}
// packages/omo-opencode/src/cli/service-auth/keystore.ts
-import { createHash as createHash9 } from "crypto";
+import { createHash as createHash10 } from "crypto";
// packages/omo-opencode/src/cli/service-auth/secret-service-lock.ts
import { spawn as spawn5 } from "child_process";
@@ -112157,7 +112077,7 @@ var stderrNoticeOncePerProcess = (message) => {
};
var isLocked = (error) => error instanceof Error && (LOCKED.test(error.message) || LOCKED.test(String(error.code ?? "")));
function credentialId(api) {
- return createHash9("sha256").update(serviceOrigin(api)).digest("hex");
+ return createHash10("sha256").update(serviceOrigin(api)).digest("hex");
}
function createCredentialStore(api, secrets = typeof Bun === "undefined" ? null : Bun.secrets, platform = process.platform, lockProbe = probeSecretServiceLock, notice = stderrNoticeOncePerProcess) {
if (!secrets)
@@ -112221,7 +112141,7 @@ function createCredentialStore(api, secrets = typeof Bun === "undefined" ? null
}
// packages/omo-opencode/src/cli/service-auth/loopback.ts
-import { createHash as createHash10, randomBytes as randomBytes3, timingSafeEqual } from "crypto";
+import { createHash as createHash11, randomBytes as randomBytes3, timingSafeEqual } from "crypto";
import { createServer as createServer3 } from "http";
class BrowserUnavailable extends SignInError {
}
@@ -112267,7 +112187,7 @@ async function authorize(options) {
const url = new URL(serviceOrigin(options.accounts));
url.search = new URLSearchParams({
redirect_uri: `${origin}/callback`,
- code_challenge: createHash10("sha256").update(verifier).digest("base64url"),
+ code_challenge: createHash11("sha256").update(verifier).digest("base64url"),
code_challenge_method: "S256",
state
}).toString();
@@ -112456,7 +112376,7 @@ async function publishLockRecord(lockPath, record) {
}
// packages/team-core/src/team-state-store/lock-reclaim.ts
-import { createHash as createHash11 } from "crypto";
+import { createHash as createHash12 } from "crypto";
import { unlink as unlink3 } from "fs/promises";
var RECLAIM_UNLINK_ATTEMPTS = 3;
var RECLAIM_UNLINK_RETRY_MS = 25;
@@ -112466,7 +112386,7 @@ function delay2(ms) {
});
}
function reclaimGuardPath(lockPath, record) {
- return `${lockPath}.reclaim-${createHash11("sha256").update(record).digest("hex").slice(0, 16)}`;
+ return `${lockPath}.reclaim-${createHash12("sha256").update(record).digest("hex").slice(0, 16)}`;
}
async function unlinkWithRetry(path, deps) {
const wait = deps.delay ?? delay2;
diff --git a/plugins/omo/hooks/post-compact-resetting-git-bash-mcp-reminder.json b/plugins/omo/hooks/post-compact-resetting-git-bash-mcp-reminder.json
index 4780366d..eb903cfc 100644
--- a/plugins/omo/hooks/post-compact-resetting-git-bash-mcp-reminder.json
+++ b/plugins/omo/hooks/post-compact-resetting-git-bash-mcp-reminder.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/git-bash/dist/cli.js\" hook post-compact",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Resetting Git Bash MCP Reminder",
+ "statusMessage": "(OmO 5.1.46) Resetting Git Bash MCP Reminder",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\git-bash\\dist\\cli.js\" hook post-compact"
}
],
diff --git a/plugins/omo/hooks/post-compact-resetting-lsp-diagnostics-cache.json b/plugins/omo/hooks/post-compact-resetting-lsp-diagnostics-cache.json
index 9e3bab8e..8efd972c 100644
--- a/plugins/omo/hooks/post-compact-resetting-lsp-diagnostics-cache.json
+++ b/plugins/omo/hooks/post-compact-resetting-lsp-diagnostics-cache.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/lsp/dist/cli.js\" hook post-compact",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Resetting LSP Diagnostics Cache",
+ "statusMessage": "(OmO 5.1.46) Resetting LSP Diagnostics Cache",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\lsp\\dist\\cli.js\" hook post-compact"
}
],
diff --git a/plugins/omo/hooks/post-compact-resetting-project-rule-cache.json b/plugins/omo/hooks/post-compact-resetting-project-rule-cache.json
index 4d4365d6..08adcd1a 100644
--- a/plugins/omo/hooks/post-compact-resetting-project-rule-cache.json
+++ b/plugins/omo/hooks/post-compact-resetting-project-rule-cache.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/rules/dist/cli.js\" hook post-compact",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Resetting Project Rule Cache",
+ "statusMessage": "(OmO 5.1.46) Resetting Project Rule Cache",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\rules\\dist\\cli.js\" hook post-compact"
}
],
diff --git a/plugins/omo/hooks/post-tool-use-checking-comments.json b/plugins/omo/hooks/post-tool-use-checking-comments.json
index 2c3b45e8..948c34a5 100644
--- a/plugins/omo/hooks/post-tool-use-checking-comments.json
+++ b/plugins/omo/hooks/post-tool-use-checking-comments.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/comment-checker/dist/cli.js\" hook post-tool-use",
"timeout": 30,
- "statusMessage": "(OmO 5.1.45) Checking Comments",
+ "statusMessage": "(OmO 5.1.46) Checking Comments",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\comment-checker\\dist\\cli.js\" hook post-tool-use"
}
],
diff --git a/plugins/omo/hooks/post-tool-use-checking-lsp-diagnostics.json b/plugins/omo/hooks/post-tool-use-checking-lsp-diagnostics.json
index 2a8ada2d..66488198 100644
--- a/plugins/omo/hooks/post-tool-use-checking-lsp-diagnostics.json
+++ b/plugins/omo/hooks/post-tool-use-checking-lsp-diagnostics.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/lsp/dist/cli.js\" hook post-tool-use",
"timeout": 60,
- "statusMessage": "(OmO 5.1.45) Checking LSP Diagnostics",
+ "statusMessage": "(OmO 5.1.46) Checking LSP Diagnostics",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\lsp\\dist\\cli.js\" hook post-tool-use"
}
],
diff --git a/plugins/omo/hooks/post-tool-use-checking-thread-title-hygiene.json b/plugins/omo/hooks/post-tool-use-checking-thread-title-hygiene.json
index 25ea188b..23c538fd 100644
--- a/plugins/omo/hooks/post-tool-use-checking-thread-title-hygiene.json
+++ b/plugins/omo/hooks/post-tool-use-checking-thread-title-hygiene.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/teammode/dist/cli.js\" hook post-tool-use",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Thread Title Hygiene",
+ "statusMessage": "(OmO 5.1.46) Checking Thread Title Hygiene",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\teammode\\dist\\cli.js\" hook post-tool-use"
}
]
diff --git a/plugins/omo/hooks/post-tool-use-matching-project-rules.json b/plugins/omo/hooks/post-tool-use-matching-project-rules.json
index e5426640..1c36b61b 100644
--- a/plugins/omo/hooks/post-tool-use-matching-project-rules.json
+++ b/plugins/omo/hooks/post-tool-use-matching-project-rules.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/rules/dist/cli.js\" hook post-tool-use",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Matching Project Rules",
+ "statusMessage": "(OmO 5.1.46) Matching Project Rules",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\rules\\dist\\cli.js\" hook post-tool-use"
}
],
diff --git a/plugins/omo/hooks/post-tool-use-recording-spawn-admission.json b/plugins/omo/hooks/post-tool-use-recording-spawn-admission.json
index 906ec0b4..b5354c1e 100644
--- a/plugins/omo/hooks/post-tool-use-recording-spawn-admission.json
+++ b/plugins/omo/hooks/post-tool-use-recording-spawn-admission.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-loop/dist/cli.js\" hook post-tool-use-spawn",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Recording Spawn Admission",
+ "statusMessage": "(OmO 5.1.46) Recording Spawn Admission",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-loop\\dist\\cli.js\" hook post-tool-use-spawn"
}
]
diff --git a/plugins/omo/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json b/plugins/omo/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json
index cb7a5b36..836cf82d 100644
--- a/plugins/omo/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json
+++ b/plugins/omo/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-loop/dist/cli.js\" hook pre-tool-use",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Enforcing Unlimited Goal Budget",
+ "statusMessage": "(OmO 5.1.46) Enforcing Unlimited Goal Budget",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-loop\\dist\\cli.js\" hook pre-tool-use"
}
],
diff --git a/plugins/omo/hooks/pre-tool-use-guarding-ulw-loop-spawns.json b/plugins/omo/hooks/pre-tool-use-guarding-ulw-loop-spawns.json
index 49a7f4c8..6f45d55f 100644
--- a/plugins/omo/hooks/pre-tool-use-guarding-ulw-loop-spawns.json
+++ b/plugins/omo/hooks/pre-tool-use-guarding-ulw-loop-spawns.json
@@ -8,7 +8,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-loop/dist/cli.js\" hook pre-tool-use-spawn",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Guarding Ulw-Loop Spawns",
+ "statusMessage": "(OmO 5.1.46) Guarding Ulw-Loop Spawns",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-loop\\dist\\cli.js\" hook pre-tool-use-spawn"
}
]
diff --git a/plugins/omo/hooks/pre-tool-use-recommending-git-bash-mcp.json b/plugins/omo/hooks/pre-tool-use-recommending-git-bash-mcp.json
index a6df7cb2..988d6b0d 100644
--- a/plugins/omo/hooks/pre-tool-use-recommending-git-bash-mcp.json
+++ b/plugins/omo/hooks/pre-tool-use-recommending-git-bash-mcp.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/git-bash/dist/cli.js\" hook pre-tool-use",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Recommending Git Bash MCP",
+ "statusMessage": "(OmO 5.1.46) Recommending Git Bash MCP",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\git-bash\\dist\\cli.js\" hook pre-tool-use"
}
],
diff --git a/plugins/omo/hooks/session-start-checking-auto-update.json b/plugins/omo/hooks/session-start-checking-auto-update.json
index 3c0a8f8e..06b94dba 100644
--- a/plugins/omo/hooks/session-start-checking-auto-update.json
+++ b/plugins/omo/hooks/session-start-checking-auto-update.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/scripts/auto-update.mjs\" hook session-start",
"timeout": 15,
- "statusMessage": "(OmO 5.1.45) Checking Auto Update",
+ "statusMessage": "(OmO 5.1.46) Checking Auto Update",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\scripts\\auto-update.mjs\" hook session-start"
}
],
diff --git a/plugins/omo/hooks/session-start-checking-bootstrap-provisioning.json b/plugins/omo/hooks/session-start-checking-bootstrap-provisioning.json
index 00d69943..cd104c25 100644
--- a/plugins/omo/hooks/session-start-checking-bootstrap-provisioning.json
+++ b/plugins/omo/hooks/session-start-checking-bootstrap-provisioning.json
@@ -8,7 +8,7 @@
"command": "node \"${PLUGIN_ROOT}/components/bootstrap/dist/cli.js\" hook session-start",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\bootstrap.ps1\"",
"timeout": 30,
- "statusMessage": "(OmO 5.1.45) Checking Bootstrap Provisioning"
+ "statusMessage": "(OmO 5.1.46) Checking Bootstrap Provisioning"
}
]
}
diff --git a/plugins/omo/hooks/session-start-loading-project-rules.json b/plugins/omo/hooks/session-start-loading-project-rules.json
index 655f000a..871d0591 100644
--- a/plugins/omo/hooks/session-start-loading-project-rules.json
+++ b/plugins/omo/hooks/session-start-loading-project-rules.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/rules/dist/cli.js\" hook session-start",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Loading Project Rules",
+ "statusMessage": "(OmO 5.1.46) Loading Project Rules",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\rules\\dist\\cli.js\" hook session-start"
}
]
diff --git a/plugins/omo/hooks/session-start-recording-session-telemetry.json b/plugins/omo/hooks/session-start-recording-session-telemetry.json
index 15d5b36f..c3843c6b 100644
--- a/plugins/omo/hooks/session-start-recording-session-telemetry.json
+++ b/plugins/omo/hooks/session-start-recording-session-telemetry.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/telemetry/dist/cli.js\" hook session-start",
"timeout": 15,
- "statusMessage": "(OmO 5.1.45) Recording Session Telemetry",
+ "statusMessage": "(OmO 5.1.46) Recording Session Telemetry",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\telemetry\\dist\\cli.js\" hook session-start"
}
]
diff --git a/plugins/omo/hooks/stop-checking-ulw-execute-continuation.json b/plugins/omo/hooks/stop-checking-ulw-execute-continuation.json
index 782e3571..96889699 100644
--- a/plugins/omo/hooks/stop-checking-ulw-execute-continuation.json
+++ b/plugins/omo/hooks/stop-checking-ulw-execute-continuation.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-execute-continuation/dist/cli.js\" hook stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Execute Continuation",
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Execute Continuation",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-execute-continuation\\dist\\cli.js\" hook stop"
}
]
diff --git a/plugins/omo/hooks/stop-checking-ulw-loop-resume.json b/plugins/omo/hooks/stop-checking-ulw-loop-resume.json
index 91659068..87e753b5 100644
--- a/plugins/omo/hooks/stop-checking-ulw-loop-resume.json
+++ b/plugins/omo/hooks/stop-checking-ulw-loop-resume.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-loop/dist/cli.js\" hook stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Loop Resume",
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Loop Resume",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-loop\\dist\\cli.js\" hook stop"
}
]
diff --git a/plugins/omo/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json b/plugins/omo/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json
index fb78a036..86c50518 100644
--- a/plugins/omo/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json
+++ b/plugins/omo/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/lazycodex-executor-verify/dist/cli.js\" hook subagent-stop",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Verifying LazyCodex Executor Evidence",
+ "statusMessage": "(OmO 5.1.46) Verifying LazyCodex Executor Evidence",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\lazycodex-executor-verify\\dist\\cli.js\" hook subagent-stop"
}
],
diff --git a/plugins/omo/hooks/user-prompt-submit-checking-ultrawork-trigger.json b/plugins/omo/hooks/user-prompt-submit-checking-ultrawork-trigger.json
index 4d27f36c..99c2de45 100644
--- a/plugins/omo/hooks/user-prompt-submit-checking-ultrawork-trigger.json
+++ b/plugins/omo/hooks/user-prompt-submit-checking-ultrawork-trigger.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ultrawork/dist/cli.js\" hook user-prompt-submit",
"timeout": 5,
- "statusMessage": "(OmO 5.1.45) Checking Ultrawork Trigger",
+ "statusMessage": "(OmO 5.1.46) Checking Ultrawork Trigger",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ultrawork\\dist\\cli.js\" hook user-prompt-submit"
}
]
diff --git a/plugins/omo/hooks/user-prompt-submit-checking-ulw-loop-steering.json b/plugins/omo/hooks/user-prompt-submit-checking-ulw-loop-steering.json
index 5c9889a9..80e25174 100644
--- a/plugins/omo/hooks/user-prompt-submit-checking-ulw-loop-steering.json
+++ b/plugins/omo/hooks/user-prompt-submit-checking-ulw-loop-steering.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/ulw-loop/dist/cli.js\" hook user-prompt-submit",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Checking Ulw-Loop Steering",
+ "statusMessage": "(OmO 5.1.46) Checking Ulw-Loop Steering",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\ulw-loop\\dist\\cli.js\" hook user-prompt-submit"
}
]
diff --git a/plugins/omo/hooks/user-prompt-submit-loading-project-rules.json b/plugins/omo/hooks/user-prompt-submit-loading-project-rules.json
index dfe1e615..8bcbd4a8 100644
--- a/plugins/omo/hooks/user-prompt-submit-loading-project-rules.json
+++ b/plugins/omo/hooks/user-prompt-submit-loading-project-rules.json
@@ -7,7 +7,7 @@
"type": "command",
"command": "node \"${PLUGIN_ROOT}/components/rules/dist/cli.js\" hook user-prompt-submit",
"timeout": 10,
- "statusMessage": "(OmO 5.1.45) Loading Project Rules",
+ "statusMessage": "(OmO 5.1.46) Loading Project Rules",
"commandWindows": "powershell -NoProfile -ExecutionPolicy Bypass -File \"${PLUGIN_ROOT}\\components\\bootstrap\\scripts\\node-dispatch.ps1\" \"${PLUGIN_ROOT}\\components\\rules\\dist\\cli.js\" hook user-prompt-submit"
}
]
diff --git a/plugins/omo/package-lock.json b/plugins/omo/package-lock.json
index 8ca13458..3b66741a 100644
--- a/plugins/omo/package-lock.json
+++ b/plugins/omo/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@sisyphuslabs/omo-codex-plugin",
- "version": "5.1.45",
+ "version": "5.1.46",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@sisyphuslabs/omo-codex-plugin",
- "version": "5.1.45",
+ "version": "5.1.46",
"workspaces": [
"components/comment-checker",
"components/git-bash",
@@ -100,7 +100,7 @@
},
"components/comment-checker": {
"name": "@code-yeongyu/codex-comment-checker",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"omo-comment-checker": "dist/cli.js"
@@ -122,7 +122,7 @@
},
"components/git-bash": {
"name": "@sisyphuslabs/codex-git-bash-hook",
- "version": "5.1.45",
+ "version": "5.1.46",
"bin": {
"omo-git-bash-hook": "dist/cli.js"
},
@@ -136,7 +136,7 @@
},
"components/lazycodex-executor-verify": {
"name": "@code-yeongyu/codex-lazycodex-executor-verify",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"lazycodex-executor-verify": "dist/cli.js"
@@ -154,7 +154,7 @@
},
"components/lsp": {
"name": "@code-yeongyu/codex-lsp",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"dependencies": {
"@code-yeongyu/lsp-daemon": "file:../../../../lsp-daemon",
@@ -176,7 +176,7 @@
},
"components/rules": {
"name": "@code-yeongyu/codex-rules",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"dependencies": {
"picomatch": "^4.0.7"
@@ -199,7 +199,7 @@
},
"components/teammode": {
"name": "@sisyphuslabs/codex-teammode",
- "version": "5.1.45",
+ "version": "5.1.46",
"devDependencies": {
"@types/node": "^26.6.2",
"@vitest/coverage-v8": "5.0.1",
@@ -213,7 +213,7 @@
},
"components/telemetry": {
"name": "@code-yeongyu/codex-telemetry",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"omo-telemetry": "dist/cli.js"
@@ -232,7 +232,7 @@
},
"components/ultrawork": {
"name": "@code-yeongyu/codex-ultrawork",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"omo-ultrawork": "dist/cli.js"
@@ -251,7 +251,7 @@
},
"components/ulw-execute-continuation": {
"name": "@code-yeongyu/codex-ulw-execute-continuation",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"omo-ulw-execute-continuation": "dist/cli.js"
@@ -269,7 +269,7 @@
},
"components/ulw-loop": {
"name": "@code-yeongyu/codex-ulw-loop",
- "version": "5.1.45",
+ "version": "5.1.46",
"license": "MIT",
"bin": {
"omo-ulw-loop": "dist/cli.js",
diff --git a/plugins/omo/package.json b/plugins/omo/package.json
index a206f1b3..24e95767 100644
--- a/plugins/omo/package.json
+++ b/plugins/omo/package.json
@@ -1,6 +1,6 @@
{
"name": "@sisyphuslabs/omo-codex-plugin",
- "version": "5.1.45",
+ "version": "5.1.46",
"description": "Aggregate Codex plugin root for OMO components.",
"type": "module",
"packageManager": "[email protected]",
diff --git a/plugins/omo/scripts/sync-skills.mjs b/plugins/omo/scripts/sync-skills.mjs
index b77720d6..8e353f4f 100644
--- a/plugins/omo/scripts/sync-skills.mjs
+++ b/plugins/omo/scripts/sync-skills.mjs
@@ -171,8 +171,13 @@ private logs, or PII; summarize with lengths, hashes, and short non-sensitive
prefixes when identity is needed.
`;
-const ulwResearchOriginalDeliveryGates = "Reading the source markup is not visual QA; inspect the pixels.\n4. **Proofread gate: `task(category=\"writing\", ...)`.** Hand the final text to a dedicated `writing` worker whose only job is language: grammar, spelling, punctuation, terminology consistency, and whether the prose reads NATIVELY in the report's own language. It proofreads only and never composes. It returns a defect list; fix every item and re-run the gate on the delta. Deliver only on a clean pass. This gate runs BEFORE the first delivery, not after the user finds the typo.";
-const ulwResearchCodexDeliveryGate = "Reading the source markup is not visual QA; inspect the pixels.";
+// Codex has no `task(category="writing")` worker, so its ulw-research edition drops the proofread gate
+// and keeps every tool-based delivery gate (static, fidelity, layout, visual QA). The item is matched by
+// its text right after the Visual QA sentence, not by its list numeral, so adding or removing an earlier
+// delivery gate does not unhook the overlay.
+const ulwResearchProofreadGate = "**Proofread gate: `task(category=\"writing\", ...)`.** Hand the final text to a dedicated `writing` worker whose only job is language: grammar, spelling, punctuation, terminology consistency, and whether the prose reads NATIVELY in the report's own language. It proofreads only and never composes. It returns a defect list; fix every item and re-run the gate on the delta. Deliver only on a clean pass. This gate runs BEFORE the first delivery, not after the user finds the typo.";
+const ulwResearchProofreadGateSource = ulwResearchProofreadGate.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+const ulwResearchProofreadGatePattern = new RegExp(`(inspect the pixels\\.)\\n\\d+\\. ${ulwResearchProofreadGateSource}`);
export const ulwExecuteOriginalCompletion = [
"When all top-level checkboxes in `## TODOs` and `## Final Verification Wave` are complete:",
@@ -216,7 +221,7 @@ const ulwExecuteCodexHardRule = [
export function applyCodexSkillOverlays(skillName, content) {
if (skillName === "ulw-research") {
- return content.replace(ulwResearchOriginalDeliveryGates, ulwResearchCodexDeliveryGate);
+ return content.replace(ulwResearchProofreadGatePattern, "$1");
}
if (skillName === "ulw-execute") {
return content
diff --git a/plugins/omo/skills/ast-grep/AGENTS.md b/plugins/omo/skills/ast-grep/AGENTS.md
index 72f65ddf..fd79579a 100644
--- a/plugins/omo/skills/ast-grep/AGENTS.md
+++ b/plugins/omo/skills/ast-grep/AGENTS.md
@@ -4,7 +4,7 @@
## OVERVIEW
-Vendored skill teaching agents ast-grep (`sg`) structural search/rewrite, plus a stdlib-Python helper CLI that wraps the `sg` binary. Vendoring pin in `SOURCE`: upstream `code-yeongyu/ast-grep-skill @ 3148c69` (ast-grep 0.45.0), "vendored as a sync; do not fork-drift". Earned this file: own executable surface (749-LOC helper + installers + smoke tests) and a no-fork-drift upstream contract that has no analogue elsewhere in `shared-skills`.
+Vendored skill teaching agents ast-grep (`sg`) structural search/rewrite, plus a stdlib-Python helper CLI that wraps the `sg` binary. Upstream pin in `SOURCE`: upstream `code-yeongyu/ast-grep-skill @ 3148c69` (ast-grep 0.45.0), "vendored as a sync; do not fork-drift". Earned this file: own executable surface (749-LOC helper + installers + smoke tests) and a no-fork-drift upstream contract that has no analogue elsewhere in `shared-skills`.
## STRUCTURE
diff --git a/plugins/omo/skills/browser/runtime/omowright/manifest.json b/plugins/omo/skills/browser/runtime/omowright/manifest.json
index 50d1abfc..048d90fc 100644
--- a/plugins/omo/skills/browser/runtime/omowright/manifest.json
+++ b/plugins/omo/skills/browser/runtime/omowright/manifest.json
@@ -5,5 +5,5 @@
"index.js": "d67d3a847fe0bbc43e824feded48b280890d31879538309e671cd5f671133995",
"page-bundle.js": "bfd51e8383cf47200d57387bbbed258bce5a3a3d00c38fc108034d2888e23f0c"
},
- "stagedAtUtc": "2026-10-24T12:07:57.178Z"
+ "stagedAtUtc": "2026-10-25T01:05:33.130Z"
}
diff --git a/plugins/omo/skills/frontend/ATTRIBUTION.md b/plugins/omo/skills/frontend/ATTRIBUTION.md
index 6a7d07a6..0e808821 100644
--- a/plugins/omo/skills/frontend/ATTRIBUTION.md
+++ b/plugins/omo/skills/frontend/ATTRIBUTION.md
@@ -215,7 +215,7 @@ operating guide for consulting the beui.dev animated-component registry during i
and motion work, written from live endpoint verification; no beui.dev source code,
documentation text, or component implementations are vendored. beui.dev names remain the
property of their owner, and the guide instructs reading component source for mechanism
-extraction only, not vendoring it into references or projects.
+extraction only, not copying it into references or projects.
`frontend/references/design/ambience-skill.md` is a project-original, curl-only operating
guide for consulting the react-bits (reactbits.dev) animated-component registry during hero
@@ -223,7 +223,7 @@ atmosphere, background, typographic-motion, and card-surface work, written from
verification; no react-bits source code, documentation text, or component implementations are
vendored. react-bits is distributed under MIT plus the Commons Clause, which permits use inside
applications but forbids redistributing or porting the components themselves; the guide
-therefore instructs reading component source for mechanism extraction only and never vendoring
+therefore instructs reading component source for mechanism extraction only and never copying
it into references, design documents, or this repository. React Bits names remain the property
of their owner.
diff --git a/plugins/omo/skills/ultimate-browsing/engine/AGENTS.md b/plugins/omo/skills/ultimate-browsing/engine/AGENTS.md
index 8f0677db..2c7dd47a 100644
--- a/plugins/omo/skills/ultimate-browsing/engine/AGENTS.md
+++ b/plugins/omo/skills/ultimate-browsing/engine/AGENTS.md
@@ -15,7 +15,7 @@ we run on is a deliberate choice, not an accident of neglect.
| Fact | Value |
|---|---|
| Upstream project | `https://github.com/fivetaku/insane-search` |
-| Vendoring commit | `a4e4ed797` (2026-06-21) `feat(ultimate-browsing): vendor insane-search engine (junk-excluded)` |
+| Import commit | `a4e4ed797` (2026-06-21) `feat(ultimate-browsing): vendor insane-search engine (junk-excluded)` |
| De-personalization | `4743199a5` (2026-06-21) |
| Pinned upstream baseline | upstream state as of 2026-06-21, **pre-0.7.0** (0.7.0 is dated 2026-06-22) |
| Re-vendors since | none — every later change here is ours |
diff --git a/plugins/omo/skills/ulw-research/AGENTS.md b/plugins/omo/skills/ulw-research/AGENTS.md
index 998eafc0..b42c73e0 100644
--- a/plugins/omo/skills/ulw-research/AGENTS.md
+++ b/plugins/omo/skills/ulw-research/AGENTS.md
@@ -14,6 +14,9 @@ ulw-research/
│ ├── report-gates.md # one row per defect code: severity, integrity, what it detects, how to fix
│ ├── report-visualization.md # which visual fits which finding, render commands (charts, Mermaid, PlotNeuralNet), quality bar, visual QA checklist
│ ├── fact-ledger-grammar.md # the one definition of a valid fact ledger; its per-kind table is rendered from KIND_RULES
+│ ├── fact-ledger.md # working guide to the ledger: files, row fields, locators, operations
+│ ├── fidelity-claim-kinds.md # claim kinds a sentence can be marked with (data-claim-kind); its table is rendered from CLAIM_KINDS
+│ ├── unit-rules.md # per-operation unit rules for derived facts; the reviewer's checklist for unit-rules.mjs
│ └── latex-report.md
└── scripts/
├── report-tools.mjs # CLI entry + dispatcher (COMMANDS, run, main)
@@ -30,15 +33,24 @@ ulw-research/
├── fact-ledger.mjs # fact ledger IO and writers (addRow, retractRow, supersedeRow, --view table)
├── fact-ledger-schema.mjs # ledger vocabulary, validateLedger, the status machine (transitionError) shared with the writers
├── fact-ledger-kinds.mjs # KIND_RULES: every per-kind column rule; drives the validator, renders the grammar table (--render-grammar)
+ ├── fact-derive.mjs / fact-recompute.mjs # script-computed derived rows and their tamper detector, both driven by unit-rules.mjs
+ ├── unit-rules.mjs # OPERATION_RULES: arity, context, guards, result unit, row shape, and malformed shapes per operation
+ ├── unit-pairs.mjs # typed operand-unit rules (equal, non-affine, +/- pairs) shared by operations and expr nodes
+ ├── unit-algebra.mjs / fact-expr.mjs # canonical unit text and composites; the expr parser and evaluator
+ ├── claim-kinds.mjs # CLAIM_KINDS: claim kind -> placeholder mark, accepted rows, defect, unmarked warning; renders its doc table (--render-doc)
├── claims-index.mjs # claim-graph.md index: the claim table plus the verified-claims digest
├── fidelity-state.mjs # per-sentence failure counts for the fidelity repair loop
+ ├── fact-commands.mjs # `fact add|quote|retract|export|fill|derive` (lazy-loaded by report-tools)
+ ├── fact-commands-verify.mjs # `fact verify` and the Markdown projection it checks
+ ├── fidelity-records.mjs # FIDELITY_RECORDS table + the delivery barrier of outcome verify/finish (fidelity_unresolved, edition_unverified)
+ ├── source-commands.mjs # `source snapshot`, `claims index`, snapshot readers
├── *.test.ts # co-located bun tests, one per module (unshipped)
└── tests/fixtures/ # good/bad reports, boxes.json, reference docs (unshipped)
```
## SURFACE
-`node "$SKILL_DIR/scripts/report-tools.mjs" <command>`: `check`, `layout-probe`, `repair decide`, `outcome init`, `outcome set`, `outcome gate`, `outcome render`, `outcome state`, `outcome verify`, `outcome finish`, `outcome briefing`, `format-extract`, `--help --json`. Gates recorded by `outcome gate`, in order: `static`, `fidelity`, `layout`, `visual`, `proofread` (`fidelity` is recorded `not_run` until the fidelity verifier ships). Exit codes: `0` pass, `1` semantic failure (blockers, a failed verify, a blocked repair), `2` usage or IO. JSON on stdout, one summary line on stderr. Flags per command: `references/deliverable-phase.md` section 10.
+`node "$SKILL_DIR/scripts/report-tools.mjs" <command>`: `check`, `layout-probe`, `repair decide`, `outcome init`, `outcome set`, `outcome gate`, `outcome render`, `outcome state`, `outcome verify`, `outcome finish`, `outcome briefing`, `format-extract`, `fact add`, `fact quote`, `fact retract`, `fact export`, `fact fill`, `fact verify`, `fact derive`, `source snapshot`, `claims index`, `--help --json`. Gates recorded by `outcome gate`, in order: `static`, `fidelity`, `layout`, `visual`, `proofread` (`fidelity` is `fact verify` on every reader-facing edition; HTML only in this stage). Exit codes: `0` pass, `1` semantic failure (blockers, a failed verify, a blocked repair), `2` usage or IO. JSON on stdout, one summary line on stderr. Flags per command: `references/deliverable-phase.md` section 10.
## CONVENTIONS
@@ -47,7 +59,7 @@ ulw-research/
- The CLI never launches a browser. The layout probe is printed as one async expression; the orchestrator evaluates it through the browser skill's owned headless engine and passes the result back with `check --layout`.
- Time is always injected (`--now`, `startedAt`); no module reads the clock inside a decision.
- Tests are co-located given/when/then bun tests; CLI tests spawn `node` with bun removed from `PATH`. Fixtures live only under `scripts/tests/fixtures/`, are depersonalized, and never ship.
-- The senpi source dir (`packages/omo-senpi/skills/ulw-research/`) intentionally has no `scripts/`, `references/report-gates.md`, `references/deliverable-phase.md`, `references/report-visualization.md`, or `references/fact-ledger-grammar.md`: the senpi sync overlays them from here byte-for-byte (`sharedAssets` in `packages/omo-senpi/plugin/scripts/native-skill-sources.mjs`), and both editions' sync suites assert the shipped copies equal these sources.
+- The senpi source dir (`packages/omo-senpi/skills/ulw-research/`) intentionally has no `scripts/`, `references/report-gates.md`, `references/deliverable-phase.md`, `references/report-visualization.md`, `references/fact-ledger-grammar.md`, `references/fidelity-claim-kinds.md`, `references/unit-rules.md`, or `references/fact-ledger.md`: the senpi sync overlays them from here byte-for-byte (`sharedAssets` in `packages/omo-senpi/plugin/scripts/native-skill-sources.mjs`), and both editions' sync suites assert the shipped copies equal these sources.
- References and scripts carry no Hangul (Korean match strings are `\u` escapes) and never three consecutive newlines (the senpi sync normalizes those and would break byte equality).
## ANTI-PATTERNS
diff --git a/plugins/omo/skills/ulw-research/SKILL.md b/plugins/omo/skills/ulw-research/SKILL.md
index 78404e33..38d3cff5 100644
--- a/plugins/omo/skills/ulw-research/SKILL.md
+++ b/plugins/omo/skills/ulw-research/SKILL.md
@@ -57,7 +57,7 @@ The research is done when all of these hold:
- Every claim in the deliverable cites a source or a verification artifact.
- Every asserted claim is represented in the claim graph, tied to an intent-vs-reality diff when an expected truth exists, and backed by observation manifest entries from independent observation groups or a documented single-source exception; convergence or exception status is explicit.
- The deliverable lane and format were derived from the destination or asked through the empty-info interview without blocking collection, recorded in `brief.md` with `answered_by`, and the final materials match that record.
-- The delivered artifact passed the delivery gates in order (static gates, layout gates, visual QA, and the proofread pass where the harness provides one), each status is in `outcome.json`, and `outcome verify` passed.
+- The delivered artifact passed the delivery gates in order (static gates, layout gates, fidelity gate, visual QA, and the proofread pass where the harness provides one), each status is in `outcome.json`, and `outcome verify` passed.
- Every excursion opened during the run was closed by an EXIT rule, folded back into the claim or axis that triggered it, and recorded in both `excursion-log.md` and the ulw-loop ledger.
- When the harness can show an HTML page inline, the report was shown inline before the artifact paths.
- When the ask reproduces a visual source, the report carries side-by-side pairs of real source frames and the output rendered at the same moments, each with its critique, and the differences they surfaced were fixed or listed as gaps. A simple or well-understood topic shortens the research, never this comparison.
@@ -69,7 +69,7 @@ The research is done when all of these hold:
Saturation is not just more searching; it is a knowledge-production protocol. The session journal must make the path from observation to claim to verdict auditable. The orchestrator owns these artifacts:
- `intent-diff.md` — one row per expected truth derived from the user intent, design/spec text, branch history, or authoritative docs. Required fields: `intent_id`, expected truth, observed reality, diff, violated invariant, intent source, supporting observations, status (`true`, `violated`, or `unknown`), and linked claim ids.
-- `claim-graph.md` — the single claim store; one node per claim. Required fields: `claim_id`, statement, claim type, risk tier, scope, intent ids, supporting observations, contradicting observations, independent observation groups, convergence status, counter-search result, primary source backing, dependencies, status (`supported`, `partial`, `refuted`, or `unresolved`), and final synthesis location. High-risk non-code nodes that clear the Phase 3b gate are mirrored into a `verified-claims` digest section at the top of the file — the sole allowlist the synthesis draws non-code claims from.
+- `claim-graph.md` — the single claim store; one node per claim. Required fields: `claim_id`, statement, claim type, risk tier, scope, intent ids, supporting observations, contradicting observations, independent observation groups, convergence status, counter-search result, primary source backing, dependencies, status (`supported`, `partial`, `refuted`, or `unresolved`), `fact_ids` (the ledger rows the claim rests on), and final synthesis location. High-risk non-code nodes that clear the Phase 3b gate are mirrored into a `verified-claims` digest section at the top of the file — the sole allowlist the synthesis draws non-code claims from.
- `observation-manifest.md` — one row per observation. Required fields: `observation_id`, source path or URL, evidence layer, observer group, independence basis, observer, `observed_at`, `valid_at` or `claim_valid_at`, artifact path, quote or line anchor, and contamination notes.
- `verification-economics.md` — one row per proof decision. Required fields: claim, risk, error cost, verification cost/time, chosen verification path, defer/verify decision, outcome, and residual risk.
- `cause-disappearance.md` — one row per causal finding. Required fields: cause id, expected truth, previous observation, `last_seen`, disconfirming observation, replacement cause if any, current status, and whether the violation is no longer observed.
@@ -142,6 +142,7 @@ This is `$SESSION_DIR`. The orchestrator owns the journal: you write every file
- `brief.md` — the analysis block, the axis list, the expected truths, and the `## Deliverable` record from the interview below (lane, state, formats, each field with `answered_by`).
- `sources-ledger.md` — one line per source the moment it is read: `[S<n>] <url> — <what it is>`. The closing briefing counts sources and domains from this file.
+- `sources/` and `fact-ledger.json` — the source snapshots and the verbatim fact ledger ([references/fact-ledger.md](references/fact-ledger.md)). Every number and every direct quotation in the report comes from a ledger row through a `{{F<n>:label}}` or `{{Q<n>:label}}` placeholder; nobody types a value into a report or into `SYNTHESIS.md`.
- `wave-<N>-<kind>-<axis>.md` — your digest of each worker return: key findings, sources with URLs, and the worker's EXPAND markers verbatim.
- `expansion-log.md` — per wave: workers spawned, markers gained, leads opened and closed.
- `excursion-log.md` — one ENTER row and one EXIT row per excursion: `excursion_id`, parent claim or axis, ENTER trigger, depth, workers spent, the EXIT rule that closed it, what it changed in the top-level answer (`none` is a valid, required answer), and the ulw-loop steer/evidence id it was mirrored into.
@@ -207,7 +208,7 @@ End your reply with the ## EXPAND tail: '- LEAD: <discovery> — WHY: <why> —
This loop is what makes the mode research rather than search. Collect returns as they land — and in team mode, act on each lead the moment a member raises it, never waiting for the full wave or a member's final reply:
-1. Journal the return the moment it lands, never at the end of the wave: digest plus verbatim EXPAND markers into `wave-<N>-<kind>-<axis>.md`, appending each new source, quote, and number to the observation manifest in the same beat — after a compaction the journal, not your memory, is the state.
+1. Journal the return the moment it lands, never at the end of the wave: digest plus verbatim EXPAND markers into `wave-<N>-<kind>-<axis>.md`, appending each new source, quote, and number to the observation manifest in the same beat — after a compaction the journal, not your memory, is the state. Snapshot each source the moment it is read (`node "$SKILL_DIR/scripts/report-tools.mjs" source snapshot --id S<n> --url <url> --session-dir "$SESSION_DIR"`; a page read in the browser is saved and imported with `--html <file>`, recorded as lower-trust `model` extraction), then register every number and quote you will use from that snapshot: `fact add --source S<n> --find "<anchor text>" --label <label>` (or `--table <id> --row <label> --col <label>`, or `--page <n> --line <n>`) and `fact quote --source S<n> --match "<words>" --label <label>`. Read the printed row and its 60 characters of source context before using it; a wrong row is retracted (`fact retract F<n> --reason <why>`), never edited. The observation's quote or line anchor in the manifest is that fact id.
2. Deduplicate new markers against `expansion-log.md` — every lead ever seen, not just confirmed ones, or rejected leads resurface each wave.
3. Spawn an expansion worker immediately for each new unchecked lead:
@@ -289,6 +290,8 @@ Anything that fails goes to an `Unresolved` (insufficient evidence) or `Refuted`
- CLAIM: <non-code assertion> — RISK: high|normal — SOURCES: <domain1, domain2> — COUNTER: <refutation search result> — PRIMARY: <primary source or none>
```
+**The claim table is machine-read.** Keep the nodes of `claim-graph.md` in one Markdown table whose header names `claim_id | statement | risk tier | status` (plus `fact_ids`, the ledger rows the node rests on, and any other node fields as further columns), one row per node: ids `C<n>`, risk `high` or `normal`, status `supported`, `partial`, `refuted`, or `unresolved`. List the nodes that clear the Phase 3b gate under a `## verified-claims` heading as `- C<n>: <statement>`. `node "$SKILL_DIR/scripts/report-tools.mjs" claims index "$SESSION_DIR/claim-graph.md" --out "$SESSION_DIR/claims.json"` indexes it, and the fidelity gate reads `claims.json` to check every `data-claim` binding in the report.
+
## Phase 4 — Synthesize
After convergence and all verifications, re-read the whole journal, start from `intent-diff.md`, `claim-graph.md`, and `observation-manifest.md`, then write `SYNTHESIS.md`:
@@ -310,6 +313,8 @@ Workers: <total> · Waves: <count> · Excursions: <count> · Sources: <count> (<
`SYNTHESIS.md` is the citation source of truth for final materials: every claim carries inline `[Source N]` citations, and every high-risk non-code claim you assert must be a verified-claims row from Phase 3b. Assert nothing the gate left in the unresolved/refuted annex.
+**Write numbers and quotes by reference.** `SYNTHESIS.md` and every report source carry placeholders, never typed values: `{{F12:span_len}}` renders the row's value and unit, `{{F12:span_len:value}}` or `:unit` one part, `:q` the value with its source qualifier (up to, at least, about); `{{Q7:completion}}` renders the whole quotation, `{{Q7:completion:lead=8}}` its first words, `{{Q7:completion:from="no earlier":to="2027"}}` one span of it. A derived number is computed by the script, never by hand: `fact derive --op sum|diff|growth|ratio|share|convert|expr ...` checks the operands' units, periods, and populations, refuses an incompatible operation, and prints the new row id the placeholder uses. A value nobody measured is registered with `fact add --assumed <value> --unit <unit> --rationale <why> --label <label>` and renders as ASSUMED. Every findings sentence binds to a fact, a quote, a `[S<n>]` citation, or a claim node (`data-claim="C3"`). Reviewers and the refinement team read the rendered text through `node "$SKILL_DIR/scripts/report-tools.mjs" fact fill "$SESSION_DIR/SYNTHESIS.md" --view --session-dir "$SESSION_DIR"`, never the raw placeholders.
+
**Write the skeleton early and fill it as claims lock.** The moment the brief records the deliverable, create the deliverable file with its section headings and a `STATUS: draft — <n> sections open` line as line 2 (an HTML comment in HTML); that line is the partial-state marker `outcome state` reads, removed only when the deliverable is complete. An interrupted run must leave a partial report on disk, never an empty directory and a lost conversation.
**Keep sourced numbers, assumptions, and derived results visibly apart.** Every quantitative claim carries its lineage: `MEASURED` (a number a source states, cited), `ASSUMED` (a coefficient, distribution, or scope you chose — say why), `DERIVED` (computed from those, showing the formula), plus a sensitivity line whenever the assumption moves the answer. Presenting a derived estimate with the confidence of a measured one is the most damaging thing this mode can ship.
@@ -327,13 +332,15 @@ The promised formats recorded in `brief.md` and `outcome.json` are binding; `pdf
| Slides / deck | `uv run --with python-pptx python` — one claim per slide, a chart or diagram per claim. |
| Standalone HTML / Markdown | The authored source itself. |
+**Every reader-facing edition is filled and verified.** The main report, the inline edition, LaTeX sections, and slide sources are all filled by `fact fill` from the same placeholder source, and each is verified on its own by `fact verify`; PDF and DOCX are renders of a verified source (`pandoc <report.html> -o <out.docx>` when the DOCX comes from the filled HTML). LaTeX per-section `.tex` sources and slide sources are filled with `fact fill --format tex` or `--format md` and verified with `fact verify --format tex` or `--format md`; this stage verifies HTML only and refuses the other formats with exit 2, so until Markdown support lands record such an edition `blocked_capability` with that reason instead of delivering it unverified. Record a delivered edition with `outcome set <format> delivered --path <file> --verified-by <record>`, where `<record>` is the `fact verify` output whose `artifact_sha256` equals the sha256 of the delivered source; a PDF or DOCX adds `--rendered-from <source>` and is matched against that source. `outcome verify` refuses a delivered row without a matching record (`edition_unverified`).
+
**Write `design-spec.md` the moment the deliverable is recorded — before any asset worker spawns.** When a document was pointed at, extract it instead of retyping its stylesheet: `node "$SKILL_DIR/scripts/report-tools.mjs" format-extract <reference> --out "$SESSION_DIR/design-spec.md"` (`--from-url` for a URL), then fill every `TODO: ask` from the interview answers, never by guessing. It is the one design contract every asset and assembly worker receives: template family (absent a reference, the clean analyst-report register — one accent over neutral tones, generous margins, styled section headings, no emoji, no clipart), palette tokens, body/heading fonts (a real gothic CJK webfont — Pretendard, Noto Sans KR — when the report language needs one, with `word-break: keep-all` for Korean), responsive breakpoints and HTML/CSS charts for web deliverables, the figure standard below, and the lineage mode on its own line: `Lineage: inline` (analyst default: every unit-bearing number tagged MEASURED / ASSUMED / DERIVED or cited in the same element) or `Lineage: section` (web and blog register: every section cited, lineage in the figure captions). The reference's section 6 lists the full defaults. One font family and one palette govern prose, charts, Mermaid, and generated images alike; a diagram rendering in a random default font inside a styled report is a defect, not a style choice.
**The figure standard — binding for every image, chart, and diagram.** Each figure sits in a fixed-size container styled from the spec (border, background, caption); the image scales to fit entirely inside it with its original aspect ratio preserved — object-fit: contain semantics — never stretched, never cropped, never spilling out. Every chart carries a title, axis labels, units, and value labels in the report's language; a bare number the reader cannot name is a defect.
Asset workers (background, parallel, each fed `design-spec.md`) — visuals are the DEFAULT deliverable of this phase, not garnish the user must ask for; a delivered report without figures is an incomplete run:
-- **Charts for every quantitative finding, computed from real data.** Pull the numbers into an actual table first (CSV/JSON under `$SESSION_DIR`), then plot from that table, never from prose. Follow the data-scientist tool doctrine — numpy always, Polars for filtering/sorting/transforms, DuckDB for joins/aggregations/window functions, never pandas — and load the `data-scientist` skill when this session has it: `uv run --with numpy --with polars --with duckdb --with pyarrow --with matplotlib python`. Keep `pyarrow` in that set — the DuckDB-to-Polars handoff (`.pl()`) fails without it, and `.df()` fails without pandas, so hand data across through `.pl()`, never `.df()`. Save to `$SESSION_DIR/assets/`.
+- **Charts for every quantitative finding, computed from real data.** Pull the numbers into an actual table first (CSV/JSON under `$SESSION_DIR`), then plot from that table, never from prose. Follow the data-scientist tool doctrine — numpy always, Polars for filtering/sorting/transforms, DuckDB for joins/aggregations/window functions, never pandas — and load the `data-scientist` skill when this session has it: `uv run --with numpy --with polars --with duckdb --with pyarrow --with matplotlib python`. Keep `pyarrow` in that set — the DuckDB-to-Polars handoff (`.pl()`) fails without it, and `.df()` fails without pandas, so hand data across through `.pl()`, never `.df()`. Save to `$SESSION_DIR/assets/`. Plot ONLY from the ledger: export the rows with `node "$SKILL_DIR/scripts/report-tools.mjs" fact export --csv --out "$SESSION_DIR/assets/<chart>.csv" --session-dir "$SESSION_DIR"` and draw from that CSV; every chart figure carries `data-chart-source="assets/<chart>.csv"`, and its axis tick labels sit inside a `<g class="ticks" data-tick-step=".." data-tick-min=".." data-tick-max="..">` group, so the fidelity gate can check every plotted value and tick (the deliverable reference's section 6 tick rule).
- **Mermaid graphs** for process, architecture, argument, timeline, and evidence-flow structure, themed to the spec's fonts and palette. Render each to SVG and confirm the file exists before the document references it.
- **Generated visuals through the imagegen skill whenever the session has it:** a cover plus a concept illustration per major theme, prompted from the spec's style, palette, and mood — document-styled illustration, never generic stock art dropped into a designed page.
- **Full-page screenshots** of the top 5-10 sources, saved by the web workers while reading, as provenance you can show.
@@ -344,26 +351,41 @@ Asset workers (background, parallel, each fed `design-spec.md`) — visuals are
**Verify the asset manifest before rendering.** List every asset the document references, assert each file exists and is non-empty on disk, and re-render whatever is missing. A document that renders with three broken diagrams is a document you will publish twice.
-Assembly worker — `task(category="deep-low", load_skills=["frontend", "visual-qa", "open-design", "data-scientist", "imagegen", "ulw-loop"], run_in_background=true, ...)`: before writing, read every available design and visualization skill and apply it — the report is a designed artifact, not a text dump; the worker's prompt carries `design-spec.md`. Use the template the user approved; absent a stronger house style the default skeleton is executive summary → key findings by theme → detailed analysis (quotes under 20 words with attribution, charts, Mermaid graphs, generated visuals, SHA-pinned permalinks, verification results) → comparative analysis when options compete → numbered sources with access dates → methodology appendix (workers, waves, searches, verifications, debate rounds) → correction log naming what verification overturned. Write it long and specific: every claim cites `[Source N]`, and the sources section lists every source the run actually used rather than a curated few.
+Assembly worker — `task(category="deep-low", load_skills=["frontend", "visual-qa", "open-design", "data-scientist", "imagegen", "ulw-loop"], run_in_background=true, ...)`: before writing, read every available design and visualization skill and apply it — the report is a designed artifact, not a text dump; the worker's prompt carries `design-spec.md`. Use the template the user approved; absent a stronger house style the default skeleton is executive summary → key findings by theme → detailed analysis (quotes under 20 words with attribution, charts, Mermaid graphs, generated visuals, SHA-pinned permalinks, verification results) → comparative analysis when options compete → numbered sources with access dates → methodology appendix (workers, waves, searches, verifications, debate rounds) → correction log naming what verification overturned. Write it long and specific: every claim cites `[Source N]`, and the sources section lists every source the run actually used rather than a curated few. The assembly worker writes `report.src.html` with placeholders, never typed values; the orchestrator runs `node "$SKILL_DIR/scripts/report-tools.mjs" fact fill "$SESSION_DIR/report.src.html" --out "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR"` and gates the filled file. The inline edition for a PDF, DOCX, or oversized HTML deliverable comes from the same source (`fact fill "$SESSION_DIR/report.src.html" --inline --out "$SESSION_DIR/report.inline.html"`, which inlines local stylesheets and makes image paths absolute on the filled output), never from a hand copy.
### The delivery gates — every gate for the QA tier must PASS, in order
-Nothing reaches the user until the gates pass. Record each result with `node "$SKILL_DIR/scripts/report-tools.mjs" outcome gate <static|fidelity|layout|visual|proofread> <pass|fail|not_run> --session-dir "$SESSION_DIR"`; `fidelity` is recorded not_run until the fidelity verifier ships in this edition; a gate this harness does not provide is recorded `not_run`. A chat or post answer (light tier) runs the static gates and one render; every paginated or web deliverable runs them all (the reference's sections 3 and 7).
+Nothing reaches the user until the gates pass. Record each result with `node "$SKILL_DIR/scripts/report-tools.mjs" outcome gate <static|fidelity|layout|visual|proofread> <pass|fail|not_run> --session-dir "$SESSION_DIR"`; a gate this harness does not provide is recorded `not_run --reason "<why>"` (`fidelity not_run` refuses without the reason). A chat or post answer (light tier) runs the static gates and one render; every paginated or web deliverable runs them all (the reference's sections 3 and 7).
1. **Static gates (always, before anyone looks at pixels).** `node "$SKILL_DIR/scripts/report-tools.mjs" check "$SESSION_DIR/report.html" --design-spec "$SESSION_DIR/design-spec.md" > "$SESSION_DIR/defects.json"`: keep-all, palette tokens, emoji, em dashes, heading length, unsourced numbers, figure containers, chart text, a report with no visual, images without alt text, gothic-only fonts, citations, the closing and sources sections, broken assets. Codes and fixes: [references/report-gates.md](references/report-gates.md).
-2. **Layout gates.** Print the probe with `node "$SKILL_DIR/scripts/report-tools.mjs" layout-probe --json`, evaluate its `source` in the rendered page through the `browser` skill's owned headless engine, save the result as `$SESSION_DIR/boxes.json`, and re-run `check` with `--layout "$SESSION_DIR/boxes.json"` (overflow, clipped text, distorted images, overlapping siblings).
-3. **Visual QA.** Render the produced artifact back to images — PDF pages to PNG, the HTML through the browser skill at desktop and phone widths — and look at them: missing or broken figures, images stretched or spilling their containers, diagram or chart text rendered off the spec's font or palette, clipped tables, overflowing CJK text, blank pages, unlabeled chart values, wrong page breaks. Reading the source markup is not visual QA; inspect the pixels.
+2. **Fidelity gate.** `node "$SKILL_DIR/scripts/report-tools.mjs" fact verify "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR"` checks every number, quote, chart value, and sentence binding against the ledger, the snapshots, `claims.json`, and the locked `## Fidelity` block of `brief.md`, and writes `$SESSION_DIR/fidelity-defects.json` with the verified file's `artifact_sha256`. Exit `1` on any blocker or major defect: record `outcome gate fidelity fail` and repair; minors alone exit `0` and are listed. Verify every other edition the same way into its own record (`--out "$SESSION_DIR/fidelity-inline.json"`). Codes and fixes: [references/report-gates.md](references/report-gates.md); grammar: [references/deliverable-phase.md](references/deliverable-phase.md), Fidelity gate.
+
+**Meaning verifier (a separate session reads every sentence against its source).** When the brief's `meaning check` is `on`, run it after the fidelity gate passes; when it is `off`, `fact pairs` writes `{"skipped": true}` and the step ends there. `node "$SKILL_DIR/scripts/report-tools.mjs" fact pairs "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR"` writes `$SESSION_DIR/pairs.json`: one pair per distinct source quote a findings sentence cites, per sentence bound only by `data-claim` (against the claim's statement), and per `data-translates` span, with `force_strong` set where the lexicon or the hedge position says a strong reader must look. Then spawn one reader worker on the cheapest category, `task(category="quick", ...)`, whose prompt carries ONLY the path of `pairs.json` and the verdict schema, in exactly this text:
+
+```text
+TASK: meaning reader, <cheap|strong> tier. Read ONLY the pair file <SESSION_DIR>/<pairs.json or pairs-strong.json> and the verdict schema in this prompt. Do not open, search, or fetch any other file, page, or tool output: not the report, the ledger, the sources, or the claim graph.
+For each pair, compare `sentence` with `quote` (with `statement` for a claim pair) and decide whether the sentence says something its source does not.
+Write <SESSION_DIR>/meaning-<cheap|strong>.json as {"model_tier": "<cheap|strong>", "verdicts": [...]} with one verdict per pair_id: {"pair_id": "P<n>", "verdict": "ok" or "drift", "drift_kind": ..., "fragment": ..., "source_fragment": ..., "explanation": "<one sentence>"}.
+drift_kind is exactly one of: subject_changed, scope_widened, hedge_dropped, certainty_added, causality_added, direction_flipped, population_changed, evidence_grade_raised, other.
+fragment is copied verbatim from the sentence and source_fragment verbatim from the quote or statement: the shortest exact substrings that show the drift, never paraphrased, never empty. A verdict whose fragments are not found word for word is discarded.
+A sentence that shortens, summarizes, or translates its source without changing subject, scope, certainty, causality, direction, population, or evidence grade is ok. Do not rewrite anything; return verdicts only.
+```
+
+Next, write `$SESSION_DIR/pairs-strong.json` (the same file, keeping only the pairs the cheap reader flagged plus every `force_strong` pair) and spawn one reader on the strong category, `task(category="ultrabrain", ...)`, with the same prompt for the strong tier. Then `node "$SKILL_DIR/scripts/report-tools.mjs" fact meaning-check --pairs "$SESSION_DIR/pairs.json" --verdicts "$SESSION_DIR/meaning-cheap.json" --verdicts "$SESSION_DIR/meaning-strong.json" --session-dir "$SESSION_DIR"` writes `$SESSION_DIR/fidelity-meaning-defects.json`: both tiers naming the same `drift_kind` (never `other`) is `meaning_drift_confirmed` (major, exit `1`); one tier alone is `meaning_drift_warn` (minor); a verdict whose fragments are not in the pair is dropped and counted. Repair every confirmed drift by rewriting the sentence from its quote, re-fill, re-verify, and re-read only the changed sentences (`fact pairs ... --changed-since <the new fill map>`); or, when the sentence is right, dispute it with `--dispute P<n> --reason "<why>"`, which keeps the defect for this run and logs the dispute. Pass the record to `repair decide` as one more `--defects` file and list the warnings in the closing briefing. Each flagged pair adds one line to `$SESSION_DIR/fidelity-calibration.jsonl`, whose header records the promotion criterion: `meaning_drift_confirmed` moves from major to an integrity code only after at least 20 runs with disputed-to-confirmed below 10 percent and no more than 2 report-level false blocks in the last 20 runs. These are starting values, changed only with evidence; the scripts never call a model.
+
+3. **Layout gates.** Print the probe with `node "$SKILL_DIR/scripts/report-tools.mjs" layout-probe --json`, evaluate its `source` in the rendered page through the `browser` skill's owned headless engine, save the result as `$SESSION_DIR/boxes.json`, and re-run `check` with `--layout "$SESSION_DIR/boxes.json"` (overflow, clipped text, distorted images, overlapping siblings).
+4. **Visual QA.** Render the produced artifact back to images — PDF pages to PNG, the HTML through the browser skill at desktop and phone widths — and look at them: missing or broken figures, images stretched or spilling their containers, diagram or chart text rendered off the spec's font or palette, clipped tables, overflowing CJK text, blank pages, unlabeled chart values, wrong page breaks. Reading the source markup is not visual QA; inspect the pixels.
-**Repair is bounded.** After every gate run that found defects, ask `node "$SKILL_DIR/scripts/report-tools.mjs" repair decide --state "$SESSION_DIR/repair-state.json" --defects "$SESSION_DIR/defects.json" --artifact-bytes <bytes> --renders <pages> --session-dir "$SESSION_DIR"` and obey it: `repair` means fix and re-run; `deliver` means stop and ship with the residual defects the manifest now lists; `block` means a content-integrity defect remains (or there is no usable artifact), so do not deliver: tell the user what blocks it and record the row as `failed` with the reason. Never repair past a stop decision (3 attempts, a 2-attempt plateau, an oscillation, or 15 minutes; the reference's section 8).
+**Repair is bounded.** After every gate run that found defects, ask `node "$SKILL_DIR/scripts/report-tools.mjs" repair decide --state "$SESSION_DIR/repair-state.json" --defects "$SESSION_DIR/defects.json" --defects "$SESSION_DIR/fidelity-defects.json" --artifact-bytes <bytes> --renders <pages> --session-dir "$SESSION_DIR"` and obey it: `repair` means fix and re-run; `deliver` means stop and ship with the residual defects the manifest now lists; `block` means a content-integrity defect remains (or there is no usable artifact), so do not deliver: tell the user what blocks it and record the row as `failed` with the reason. Never repair past a stop decision (3 attempts, a 2-attempt plateau, an oscillation, or 15 minutes; the reference's section 8). Fidelity defects ride the same loop (`--defects "$SESSION_DIR/defects.json" --defects "$SESSION_DIR/fidelity-defects.json"`), but a `deliver` decision does not release them: while `fidelity-defects.json` lists `untraceable_number`, `value_mismatch`, `unit_mismatch`, `quote_not_verbatim`, `quote_unbound`, `unbound_sentence`, or `chart_value_untraceable`, `outcome verify` fails with `fidelity_unresolved` and `outcome finish` refuses, even with the repair budget spent. The only ways out are a ledger row, an `assumed` row with its rationale, or removing the sentence.
-Then resolve every manifest row (`outcome set <format> delivered --path <file>` or `blocked_capability` / `skipped` / `failed` with `--reason`) and deliver. **Show the report inline first when the harness can.** When this session has a tool that shows an HTML page inline in the conversation (`show_html_page`, `html_render`), the requester's first view of the result is the report itself: the gated `report.html` when it fits the tool, otherwise an inline edition built from the same synthesis and assets, with the reference-compare pairs when the run has them ([references/deliverable-phase.md](references/deliverable-phase.md), section 9, Showing the report inline). Then give the artifact paths and a compact chat-readable summary of what the report says — the answer in a few sentences, the numbers that matter, and what to look at first. This holds on every tier, a chat answer included, and when the thing built is itself a page (an app, a 3D scene), showing it does not replace the report page. Without an inline page tool, deliver the artifact paths plus that summary. The document is the deliverable; the summary is what gets it read.
+Then resolve every manifest row (`outcome set <format> delivered --path <file> --verified-by <record>` (plus `--rendered-from <source>` for a PDF or DOCX) or `blocked_capability` / `skipped` / `failed` with `--reason`) and deliver. **Show the report inline first when the harness can.** When this session has a tool that shows an HTML page inline in the conversation (`show_html_page`, `html_render`), the requester's first view of the result is the report itself: the gated `report.html` when it fits the tool, otherwise an inline edition built from the same synthesis and assets, with the reference-compare pairs when the run has them ([references/deliverable-phase.md](references/deliverable-phase.md), section 9, Showing the report inline). Then give the artifact paths and a compact chat-readable summary of what the report says — the answer in a few sentences, the numbers that matter, and what to look at first. This holds on every tier, a chat answer included, and when the thing built is itself a page (an app, a 3D scene), showing it does not replace the report page. Without an inline page tool, deliver the artifact paths plus that summary. The document is the deliverable; the summary is what gets it read.
### The closing briefing — every run ends with it
The last thing the user reads states, in one compact block, what the answer is made of:
- **The printed block.** `node "$SKILL_DIR/scripts/report-tools.mjs" outcome verify --session-dir "$SESSION_DIR" && node "$SKILL_DIR/scripts/report-tools.mjs" outcome finish --session-dir "$SESSION_DIR" && node "$SKILL_DIR/scripts/report-tools.mjs" outcome briefing --session-dir "$SESSION_DIR"`: sources and distinct domains counted from `sources-ledger.md`, elapsed minutes from the session directory's own timestamp, every promised deliverable with its status, the gate results, the residual defects, and the repair summary. A failing `verify` means the run is not done.
-- **Effort.** Workers, waves, excursions, and verifications — the same counters as the `SYNTHESIS.md` header — plus how many sources were primary and how many claims went to the unresolved/refuted annex.
+- **Effort.** Workers, waves, excursions, and verifications — the same counters as the `SYNTHESIS.md` header — plus how many sources were primary and how many claims went to the unresolved/refuted annex. `fact export` also prints a coverage line (`N of M sources snapshotted, by extractor: ...`); paste it into the briefing's effort line.
Never ship the artifact without this block, and never fill it from memory — paste the printed block and read the rest off the journal.
@@ -411,6 +433,9 @@ High-yield combinations: official docs (`site:<docs domain>`), GitHub implementa
| Batching findings into an end-of-run journal dump | Journal each return as it lands; the journal is what survives a compaction |
| Ending the run because every worker finished | Reserve the final fifth of the run for synthesis and materials |
| A derived estimate presented as a measured number | MEASURED / ASSUMED / DERIVED lineage on every quantitative claim, plus a sensitivity line |
+| A number typed into the report or `SYNTHESIS.md` | Register it (`fact add`) and write `{{F<n>:label}}`; `fact verify` reports a typed number as `untraceable_number`, and `outcome verify` refuses delivery until it is a ledger row, an `assumed` row with its rationale, or gone |
+| A filled value edited by hand | Never edit a filled value: fix the ledger row or the placeholder and re-run `fact fill`, then `fact verify` (`value_mismatch`, `unit_mismatch`) |
+| A findings sentence bound to nothing | Bind it to a fact, a quote, a `[S<n>]` citation, or a claim node with `data-claim`; an `unbound_sentence` blocks delivery like an untraceable number |
| Delivering before the delivery gates pass | Static and layout gates, visual QA on rendered pages, plus the harness's proofread gate — a typo the user finds means a gate did not run |
| Skipping the static gates | `report-tools check` runs before any pixel review; its defects are cheaper than a visual pass |
| Delivering with a pending manifest row | `outcome verify` must pass; every promised format ends `delivered`, `blocked_capability`, `skipped`, or `failed` with a reason |
diff --git a/plugins/omo/skills/ulw-research/references/deliverable-phase.md b/plugins/omo/skills/ulw-research/references/deliverable-phase.md
index 8814686a..0f52c3fc 100644
--- a/plugins/omo/skills/ulw-research/references/deliverable-phase.md
+++ b/plugins/omo/skills/ulw-research/references/deliverable-phase.md
@@ -50,7 +50,7 @@ The request and its destination usually answer the format question already. Deri
| A prior report of the same kind in this workspace, not pointed at | that report's format, offered as the Q1 default | `template-vibe` | full |
| A complete existing deliverable for this question (section 2) | its existing format | `edit-existing` | the original's tier |
-QA tiers: **light** runs the static gates and one render check of the answer, and records the fidelity gate as `not_run` until Markdown deliverable support ships for it. **Full** runs the static gates, the fidelity gate, the layout gates, visual QA of every page, and the proofread gate (section 7).
+QA tiers: **light** runs the static gates and one render check of the answer, and records the fidelity gate as `not_run --reason "<why>"` (the reason is required) until Markdown deliverable support ships for it. **Full** runs the static gates, the fidelity gate, the layout gates, visual QA of every page, and the proofread gate (section 7).
## 4. The empty-info interview
@@ -105,7 +105,7 @@ Write `$SESSION_DIR/design-spec.md` the moment the deliverable fields are known,
The extractor copies the CSS custom properties (light and dark), font stacks by role, the most-used colors, px breakpoints, the section skeleton, the heading numbering, the citation marker style, the lineage mode, and the figure containers, under the fixed sections `Extracted from`, `Tokens`, `Typography`, `Layout`, `Structure`, `Figures`, `Citations`, `Open questions`. A field it cannot determine reads `TODO: ask`. A PDF reference is unsupported: ask for its HTML or Markdown source.
2. Fill every `TODO: ask` from the interview answers, then from the clean analyst register below. Never guess a value.
3. Apply the binding figure standard and the defaults:
- - Every figure sits in a fixed-size container with a caption; the image scales inside it with contain-fit, its aspect ratio preserved, never stretched, cropped, or spilling out. Every chart carries a title, axis labels, units, and value labels as real text, with deterministic ticks (round values computed from the data range, never left to a renderer's auto-scaling).
+ - Every figure sits in a fixed-size container with a caption; the image scales inside it with contain-fit, its aspect ratio preserved, never stretched, cropped, or spilling out. Every chart carries a title, axis labels, units, and value labels as real text, with deterministic ticks (round values computed from the data range, never left to a renderer's auto-scaling). A chart is plotted only from a `fact export --csv` file saved under `$SESSION_DIR/assets/`, its figure carries `data-chart-source` pointing at that file, and its axis tick labels sit inside one `<g class="ticks" data-tick-step=".." data-tick-min=".." data-tick-max="..">` group, so the fidelity gate can check every value label against the ledger and every tick against the declared grid.
- One accent color over neutral background, text, and rule tones. Every color in prose, charts, and diagrams is a spec token.
- For web deliverables, HTML/CSS bars and inline SVG are preferred over image charts, and the page has responsive breakpoints so it reads on a phone, a tablet, and a desktop.
- Gothic (sans) type only, as a real embedded webfont for the report language; Korean text uses `word-break: keep-all; overflow-wrap: anywhere`.
@@ -123,7 +123,11 @@ Nothing reaches the requester until the gates for its QA tier pass, in this orde
node "$SKILL_DIR/scripts/report-tools.mjs" check "$SESSION_DIR/report.html" --design-spec "$SESSION_DIR/design-spec.md" > "$SESSION_DIR/defects.json"
```
It checks keep-all, palette tokens, emoji, em dashes in prose (en dashes outside numeric ranges are advisory), heading length and generic headings, unsourced numbers (section 6), figure containers and captions, chart SVG text, a report with no visual at all, images without alt text, Korean serif fonts, sections without citations, the closing section, the sources section, broken local assets, and any `--require-section <regex>` the brief requires. Every code, severity, and fix is in [report-gates.md](report-gates.md).
-2. **Fidelity gate.** The fidelity verifier checks every number, quote, and claim binding against the fact ledger and the source snapshots; its codes are the Fidelity Gate table in [report-gates.md](report-gates.md). It runs before layout and visual QA because a content failure would otherwise force an expensive re-render. Until the fidelity verifier ships in this edition, record `outcome gate fidelity not_run`.
+2. **Fidelity gate.** The fidelity verifier checks every number, quote, and claim binding against the fact ledger and the source snapshots; its codes are the Fidelity Gate table in [report-gates.md](report-gates.md). It runs before layout and visual QA because a content failure would otherwise force an expensive re-render.
+ ```bash
+ node "$SKILL_DIR/scripts/report-tools.mjs" fact verify "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR" # writes fidelity-defects.json
+ ```
+ Exit `1` on any blocker or major defect: record `outcome gate fidelity fail`; minors alone exit `0` and are listed. The grammar, the fill step, and the derivations are in section 11; this stage verifies HTML only.
3. **Layout gates.** Print the probe, evaluate it in the rendered page through the browser skill's owned headless engine, save the returned object as `$SESSION_DIR/boxes.json`, and re-run `check` with `--layout "$SESSION_DIR/boxes.json"`. It adds overflow past the container, clipped text, scroll containers, distorted images, and overlapping siblings.
```bash
node "$SKILL_DIR/scripts/report-tools.mjs" layout-probe --json # .source is one async expression
@@ -131,20 +135,28 @@ Nothing reaches the requester until the gates for its QA tier pass, in this orde
4. **Visual QA.** Render the artifact to images (PDF pages to PNG, the HTML in the browser skill's engine at desktop and phone widths) and look at the pixels for what a machine cannot see: composition, hierarchy, page breaks, figure legibility.
5. **Proofread.** A dedicated writing lane proofreads the final text only (grammar, spelling, punctuation, terminology, native register); it never composes or rewrites content.
-The light tier runs gate 1 and one render (recorded as `visual`) and records `fidelity`, `layout`, and `proofread` as `not_run`; `fidelity` stays `not_run` in the light tier until Markdown deliverable support ships for the fidelity verifier. The full tier runs all five.
+**Every reader-facing edition is verified.** The main report, the inline edition (section 9), LaTeX per-section sources, and slide sources are filled by `fact fill` from one placeholder source and each is verified by `fact verify` into its own record; PDF and DOCX are renders of a verified source. A delivered row is recorded with `outcome set <format> delivered --path <file> --verified-by <record>`, where the record's `artifact_sha256` equals the sha256 of the delivered source; a PDF or DOCX adds `--rendered-from <source>` and is matched against that source. In a session with a fact ledger, `outcome verify` refuses a delivered row without a matching record (`edition_unverified`, blocker, integrity). Markdown and LaTeX verification arrive with the Markdown stage; until then `fact verify --format md|tex` exits `2`, and such an edition is recorded `blocked_capability` with that reason rather than delivered unverified.
+
+The light tier runs gate 1 and one render (recorded as `visual`) and records `fidelity`, `layout`, and `proofread` as `not_run` (`fidelity not_run` needs `--reason "<why>"`); `fidelity` stays `not_run` in the light tier until Markdown deliverable support ships for the fidelity verifier. The full tier runs all five.
## 8. Bounded repair loop
After every gate run that produced defects, ask the tracker what to do next:
```bash
-node "$SKILL_DIR/scripts/report-tools.mjs" repair decide --state "$SESSION_DIR/repair-state.json" --defects "$SESSION_DIR/defects.json" --artifact-bytes <report bytes> --renders <rendered page count> --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" repair decide --state "$SESSION_DIR/repair-state.json" --defects "$SESSION_DIR/defects.json" --defects "$SESSION_DIR/fidelity-defects.json" --artifact-bytes <report bytes> --renders <rendered page count> --session-dir "$SESSION_DIR"
```
+`--defects` repeats: every file (a `check` result, a `fact verify` record, or a bare defect array) joins one defect list, so one decision covers both gates.
+
- `action: repair`: fix the listed defects, re-render, re-run the gate, and ask again.
- `action: deliver`: stop repairing and deliver. `reason` is `clean` or the stop condition that fired; the residual defects are written into the manifest and listed in the closing briefing.
- `action: block` (exit `1`): do not deliver. Either a content-integrity defect remains (`missing_closing_section`, `missing_citation_section`, `broken_asset_reference`, `missing_required_section`, `missing_promised_deliverable`, or a fidelity integrity code: `unresolved_placeholder`, `value_mismatch`, `unit_mismatch`, `quote_not_verbatim`, `claim_status_invalid`, `incompatible_operands`, `pointer_label_mismatch`, `quote_anchor_unresolved`, `unit_duplicated`, `polish_diff_fail`, `fidelity_interview_incomplete`, `derived_not_allowed`, `source_outside_floor`, `snapshot_hash_mismatch`, `chart_value_untraceable`, `quote_unbound`, `edition_unverified`), or a stop fired without proof of a usable artifact (`--artifact-bytes` and `--renders`). Tell the requester what blocks delivery and record the affected rows as `failed` with the reason.
+**The fidelity barrier is separate from the budget.** A `deliver` decision does not release fidelity defects: `outcome verify` and `outcome finish` use the fidelity gate's `blocksDelivery` predicate over `DEFECT_CODES` (integrity, blocker, or major), never a CLI code list or the row's claimed severity. Unknown codes refuse with `unknown defect code <code>`; known blockers produce `fidelity_unresolved`. Fix the defect and re-verify the edition before delivery. The table and the record schema in `scripts/fidelity-records.mjs` are the spec for the barrier. Every fidelity record carries one envelope (`record_kind`, `artifact_sha256`, `report`, `written_at`, `defects`) plus the fields its kind declares; a record with a missing, unknown, or ill-typed field refuses as malformed (`edition_unverified`). `FIDELITY_RECORDS` has one row per record kind, each rule a column: where its records are, when a delivered session must carry one, the envelope field that binds it to the delivered bytes (`artifact_sha256`), and which defects block. The rows are `fidelity` (`fact verify`, one record per delivered edition, named by `--verified-by`) and `meaning` (`fidelity-meaning-defects.json` from `fact meaning-check`, one per session, required while the locked brief's `meaning check` is `on` or its `## Fidelity` block is malformed; missing: `meaning gate not run`, the same `edition_unverified` refusal, released only by the logged `fidelity not_run` exemption in a session with no ledger or fidelity record). A record whose `artifact_sha256` matches no delivered edition's source bytes refuses as `is for a different edition`, so a meaning record from an earlier draft never passes a rewritten one: rerun `fact pairs` and the readers after every rewrite. A `meaning_drift_confirmed` or `meaning_unverified` in that record holds the barrier like any other major.
+
+**After a dispute.** `--dispute P<n> --reason` records the disagreement in the calibration log; it does not clear the defect, so `fact meaning-check` keeps exiting `1` and the barrier keeps holding by design. Proceed through `repair decide` with `fidelity-meaning-defects.json` among its `--defects` (the dispute stays recorded on that run): the writer changes the sentence (rewrite it from the quote, or `fact fill --replace-failed` for a sentence that failed twice), then rerun `fact pairs` and the readers. There is no path that delivers a disputed `meaning_drift_confirmed` sentence unchanged.
+
Defaults: 3 repair attempts, a plateau of 2 consecutive attempts that do not improve the (blocker, major, minor) counts compared in that order, and 15 minutes of wall clock from the first decision. A defect set that repeats an earlier attempt exactly stops the loop as an oscillation. When several conditions hold at once the reported reason follows `clean`, `blocking`, `oscillation`, `plateau`, `max_attempts`, `wall_clock`.
## 9. Outcome manifest and closing briefing
@@ -153,7 +165,7 @@ Defaults: 3 repair attempts, a plateau of 2 consecutive attempts that do not imp
```bash
node "$SKILL_DIR/scripts/report-tools.mjs" outcome init --promised pdf,docx --lane template-vibe --session-dir "$SESSION_DIR"
-node "$SKILL_DIR/scripts/report-tools.mjs" outcome set pdf delivered --path "$SESSION_DIR/report.pdf" --pages 12 --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" outcome set pdf delivered --path "$SESSION_DIR/report.pdf" --rendered-from "$SESSION_DIR/report.html" --verified-by "$SESSION_DIR/fidelity-defects.json" --pages 12 --session-dir "$SESSION_DIR"
node "$SKILL_DIR/scripts/report-tools.mjs" outcome set docx blocked_capability --reason "no document converter in this session" --session-dir "$SESSION_DIR"
node "$SKILL_DIR/scripts/report-tools.mjs" outcome render "$SESSION_DIR/renders/page-01.png" --page 1 --session-dir "$SESSION_DIR"
node "$SKILL_DIR/scripts/report-tools.mjs" outcome verify --session-dir "$SESSION_DIR"
@@ -162,8 +174,9 @@ node "$SKILL_DIR/scripts/report-tools.mjs" outcome briefing --session-dir "$SESS
```
- `init` runs when the brief is written (section 4): one `pending` row per promised format (`pdf`, `docx`, `html`, `md`, `latex`, `slides`, `post`).
-- `set` resolves a row: `delivered` (absolute path; bytes are read from disk; pages when paginated), `blocked_capability` (this session cannot produce it), `skipped` (the requester or the lane dropped it), or `failed`. Every status except `delivered` needs `--reason`.
-- `verify` runs before delivery and exits `1` while a row is `pending`, a status lacks its reason, or a delivered file is missing or empty.
+- `set` resolves a row: `delivered` (absolute path; bytes are read from disk; pages when paginated), `blocked_capability` (this session cannot produce it), `skipped` (the requester or the lane dropped it), or `failed`. Every status except `delivered` needs `--reason`; `outcome set <format> delivered` records intent permissively, while the required `outcome verify` and `outcome finish` enforce the delivery barrier.
+- `verify` runs before delivery and exits `1` while a row is `pending`, a status lacks its reason, a delivered file is missing or empty, a registry blocker remains (`fidelity_unresolved`), or a delivered edition has no matching verify record (`edition_unverified`, section 7). Delivered HTML/PDF/DOCX without any ledger or record also refuse with `fidelity gate not run`; explicitly record `outcome gate fidelity not_run --reason "<text>"` to exempt an unverified edition. With `meaning check: on`, or a malformed `## Fidelity` block (which cannot prove the check off), a delivered session without `fidelity-meaning-defects.json` refuses with `meaning gate not run` under the same rule; a meaning record for bytes no delivered edition has refuses as `is for a different edition`. This choice is logged in `gateHistory` with its timestamp and reason; it never releases an existing ledger requirement, record, or defect, and a subsequent fidelity gate status replaces the exemption.
+- `finish` refuses (exit `1`, nothing stamped) while the fidelity barrier holds.
- `finish` stamps the finish time, the elapsed minutes from the session directory's start stamp, and the source and domain counts from `sources-ledger.md`.
- `briefing` prints the closing block from the manifest and the ledger: sources and distinct domains, elapsed minutes, every deliverable with its status, the gate results, the residual defects, and the repair summary. That text is the closing briefing, pasted as printed, never filled in from memory.
@@ -185,14 +198,61 @@ Invoke every command as `node "$SKILL_DIR/scripts/report-tools.mjs" <command>`.
| --- | --- | --- |
| `check <report.html>` | `--design-spec <spec>` (required), `--layout <boxes.json>`, `--require-section <regex>` (repeatable), `--max-defects N` | any blocker defect |
| `layout-probe` | `--cap N` (default 400), `--root <selector>` (default `main, article`), `--json` | never |
-| `repair decide` | `--state <file>` and `--defects <json>` (required), `--artifact-bytes N`, `--renders N`, `--session-dir <dir>`, `--max-attempts N`, `--plateau-limit N`, `--wall-clock-ms N`, `--now <ISO>` | the decision is `block` |
+| `repair decide` | `--state <file>` and `--defects <json>` (required; repeatable, the files are concatenated), `--artifact-bytes N`, `--renders N`, `--session-dir <dir>`, `--max-attempts N`, `--plateau-limit N`, `--wall-clock-ms N`, `--now <ISO>` | the decision is `block` |
| `outcome init` | `--promised <list>` (required), `--lane <lane>`, `--session-dir <dir>`, `--now <ISO>` | never |
-| `outcome set <format> <status>` | `--path <file>`, `--pages N`, `--reason <text>`, `--session-dir <dir>`, `--now <ISO>` | never |
-| `outcome gate <gate> <status>` | `--session-dir <dir>` | never |
+| `outcome set <format> <status>` | `--path <file>`, `--pages N`, `--reason <text>`, `--verified-by <fidelity record>`, `--rendered-from <source>`, `--session-dir <dir>`, `--now <ISO>` | never |
+| `outcome gate <gate> <status>` | `--session-dir <dir>`, `--now <ISO>`, `--reason <text>` (required for `fidelity not_run`; logged in `gateHistory`) | never (a missing reason exits `2`) |
| `outcome render <png>` | `--page N` (required), `--session-dir <dir>` | never |
| `outcome state` | `--deliverable <path>` (required), `--session-dir <dir>` | never |
-| `outcome verify` | `--session-dir <dir>` | a promise is unresolved or a delivered file is missing or empty |
-| `outcome finish` | `--ledger <file>`, `--session-dir <dir>`, `--now <ISO>` | never |
+| `outcome verify` | `--session-dir <dir>` | a promise is unresolved, a delivered file is missing or empty, `fidelity_unresolved`, or `edition_unverified` |
+| `outcome finish` | `--ledger <file>`, `--session-dir <dir>`, `--now <ISO>` | the fidelity barrier holds (section 8) |
| `outcome briefing` | `--json`, `--ledger <file>`, `--session-dir <dir>`, `--now <ISO>` | never |
| `format-extract <reference>` | `--out <design-spec.md>` (required), `--from-url` | never (an unsupported reference exits `2`) |
| `--help` | `--json` | never |
+| `source snapshot` | `--id S<n>` (required), one of `--url <u>`, `--html <file>`, `--text <file>` (with optional `--extractor NAME@VERSION`), `--command '<argv json>' --cwd <dir>`; `--kind page\|command\|document`, `--provenance primary\|secondary`, `--revision <r>`, `--session-dir <dir>` | the id already holds different content (`snapshot_exists`) |
+| `fact add` | `--source S<n>` with `--find <text>` (`--unit-expected <unit>`), or `--table <id> --row <label> --col <label>`, or `--page N --line N`; `--label`, `--entity`, `--valid-at`, `--population`, `--accounting-basis`, `--observation O<n>`, `--json`, `--session-dir <dir>`. Or `--assumed <value> --unit <unit> --rationale <text> --label <label>` | `fact_not_located`, `fact_ambiguous`, `unit_mismatch` (no row written) |
+| `fact quote` | `--source S<n> --match <text>` (required), `--label`, `--json`, `--session-dir <dir>` | `fact_not_located`, `fact_ambiguous` |
+| `fact retract <id>` | `--reason <text>` (required), `--replacement <id>`, `--session-dir <dir>` | never (an illegal transition exits `2`) |
+| `fact export` | `--csv`, `--out <file>`, `--session-dir <dir>` | never |
+| `fact fill <src>` | `--out <file>` or `--view`, `--format html\|md\|tex`, `--inline`, `--replace-failed`, `--session-dir <dir>` | a fill error (nothing is written) |
+| `fact verify <report>` | `--format html\|md` (from the extension by default), `--sources <dir>`, `--claims <claims.json>`, `--fill-map <file>` (a Markdown edition reads `<report>.fill-map.json` by default), `--brief <brief.md>`, `--out <record>`, `--session-dir <dir>` | any blocker or major defect |
+| `fact pairs <report>` | `--format html\|md`, `--ledger <fact-ledger.json>`, `--claims <claims.json>`, `--changed-since <fill-map.json>` (pair only the facts that map filled), `--brief <brief.md>`, `--out <pairs.json>`, `--session-dir <dir>` | the brief has no locked `## Fidelity` block (with `meaning check: off` it writes `{"skipped": true}` and exits `0`) |
+| `fact meaning-check` | `--pairs <pairs.json>` and `--verdicts <file>` (required; repeatable, one per reader tier), `--dispute P<n> --reason <text>`, `--run-id <id>`, `--calibration <file>` (default `fidelity-calibration.jsonl`), `--out <record>` (default `fidelity-meaning-defects.json`), `--session-dir <dir>` | `meaning_drift_confirmed` (stderr carries one JSON line with the defect and dropped-verdict counts) |
+| `fact derive` | `--op <op>` (required) with the operation's operands (section 11), `--unit`, `--to`, `--rationale`, `--disjoint`, `--label`, `--json`, `--session-dir <dir>` | `incompatible_operands` (no row written) |
+| `claims index <claim-graph.md>` | `--out <claims.json>` (required) | never (a malformed claim table exits `2`) |
+
+## 11. Fidelity gate
+
+Every number, quotation, and chart value in a reader-facing edition comes from the fact ledger (`$SESSION_DIR/fact-ledger.json`, schema and operations in [fact-ledger.md](fact-ledger.md)), which is built only from source snapshots (`$SESSION_DIR/sources/S<n>/`). Nobody types a value into a report. The commands, in the order a run uses them:
+
+```bash
+node "$SKILL_DIR/scripts/report-tools.mjs" source snapshot --id S4 --url https://example.org/bridge --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" fact add --source S4 --find "Span length" --label span_len --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" fact quote --source S4 --match "no earlier than 2027" --label completion --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" fact derive --op convert F1 --to mm --label span_mm --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" claims index "$SESSION_DIR/claim-graph.md" --out "$SESSION_DIR/claims.json"
+node "$SKILL_DIR/scripts/report-tools.mjs" fact fill "$SESSION_DIR/report.src.html" --out "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR"
+node "$SKILL_DIR/scripts/report-tools.mjs" fact verify "$SESSION_DIR/report.html" --session-dir "$SESSION_DIR"
+```
+
+- **Snapshot.** A source directory is immutable: the same id with identical text is a no-op, different text fails with `snapshot_exists`, because ledger rows pin the old hash; use a new id. `--url` fetches with plain fetch (30 s timeout, no browser) and records extractor `tool:source-snapshot@1`. `--html` and `--text` import a capture the browser lane saved and record extractor `model` (lower trust), or the named extractor with `--text --extractor NAME@VERSION`. `--command` runs an argv without a shell, snapshots its stdout, and writes `command.json` with the argv, working directory, exit status, and output hashes.
+- **Register.** `fact add` locates one quantity deterministically (`--find`: the first numeral within 80 characters after a unique anchor, in the same sentence; `--table`/`--row`/`--col` by header labels; `--page`/`--line`) and prints the row with 60 characters of source context on each side (`--json` for the full row). `fact quote` stores the whole containing sentence. The row records the source's kind, URL, host, and whether the brief's source floor points at it. A failed locate writes nothing.
+- **Write by reference.** Placeholders: `{{F12:span_len}}` (value and unit), `{{F12:span_len:value}}`, `{{F12:span_len:unit}}`, `{{F12:span_len:q}}` (with the source qualifier), `{{Q7:completion}}`, `{{Q7:completion:lead=8}}`, `{{Q7:completion:from="no earlier":to="2027"}}`. The label must equal the row's label.
+- **Fill.** `fact fill` is all or nothing: on `pointer_label_mismatch`, `unresolved_placeholder`, `quote_anchor_unresolved`, or `unit_duplicated` it exits `1` and writes nothing. On success it writes the output and `<out>.fill-map.json`. `--view` prints the rendered text without writing; `--inline` builds the inline edition (local stylesheets and scripts inlined, image paths made absolute) from the same source; `--format md` and `--format tex` fill Markdown and LaTeX sources; `--replace-failed` replaces a sentence that failed twice with an attributed block quote of its source.
+- **Verify.** `fact verify` writes its record (default `fidelity-defects.json`; `--out` for another edition) with the verified file's `artifact_sha256`, reads `claims.json` and the locked `## Fidelity` block of `brief.md`, and exits `1` on any blocker or major defect; minors alone exit `0`. A Markdown edition (`--format md`, or a `.md` path) is verified with its fill map; a LaTeX edition is verified through its Markdown source, so `--format tex` exits `2`.
+- **Meaning.** With `meaning check: on`, `fact pairs` writes the pairs a separate reader session judges, and `fact meaning-check` turns the readers' verdicts into `fidelity-meaning-defects.json` and the calibration log (verdict schema in [fact-ledger.md](fact-ledger.md)). No script calls a model.
+- **Export.** `fact export --csv --out "$SESSION_DIR/assets/<chart>.csv"` writes `fact_id,label,plot_value,unit` for every active value row, the only data a chart may plot, and prints `N of M sources snapshotted, by extractor: ...` for the closing briefing.
+
+Derivations are computed in exact rationals from the operands' stored values and rendered at the operands' fewest significant figures; a refused operation exits `1` with `incompatible_operands` and writes no row.
+
+| Operation | Operands | Checks |
+| --- | --- | --- |
+| `sum` | two or more ids, `--unit` | one unit; summing `%` needs `--disjoint` and `--rationale` |
+| `diff` | two ids (minuend, subtrahend) | one unit; a difference of `%` is in `pp` |
+| `growth` | `--from <id> --to <id>` | one declared entity, two different `valid_at` periods, a positive start; result in `%` |
+| `ratio` | `--num <id> --den <id>`, `--unit` | a non-zero denominator |
+| `share` | `--part <id> --whole <id>` | one unit; result in `%` |
+| `convert` | one id, `--to <unit>` | a known conversion between the units |
+| `expr` | a formula over ids, `+ - * /`, `0`, `1`, `100`, `--unit`, `--rationale` | none (recorded `unchecked`) |
+
+Exit codes: `0` ok; `1` a semantic failure (`fact_not_located`, `fact_ambiguous`, a fill error, `incompatible_operands`, `snapshot_exists`, or a verify record with a blocker or major defect); `2` usage, an unreadable input, or an invalid ledger write.
diff --git a/plugins/omo/skills/ulw-research/references/fact-ledger-grammar.md b/plugins/omo/skills/ulw-research/references/fact-ledger-grammar.md
index dac4e9b5..71e8b3b5 100644
--- a/plugins/omo/skills/ulw-research/references/fact-ledger-grammar.md
+++ b/plugins/omo/skills/ulw-research/references/fact-ledger-grammar.md
@@ -49,7 +49,11 @@ Always required, never null: `fact_id`, `kind`, `raw` (string), `quote` (string)
| `locator` | object | object | object or null | object or null | object or null |
| `snapshot_sha256` | sha256 | sha256 | sha256 or null | sha256 or null | sha256 or null |
| `rationale` | string or null | string or null | string or null | non-empty | string or null |
-| `exact` | from `value` | null | written by derive (rational or null) | rational or null | written by derive (rational or null) |
+| `exact` | from `value` | null | reduced rational | rational or null | reduced rational |
+| `context` | null | null | written by derive (object or null) | null | written by derive (object or null) |
+| `operation` | null | null | sum/diff/growth/share/ratio/expr | null | `convert` |
+| `precision` | null | null | `{ places, sigfigs }` | null | `{ places, sigfigs }` |
+| `compatibility` | null | null | checked or unchecked (expr) | null | checked or unchecked (expr) |
<!-- kind-rules:end -->
- `exact` is `{ num: integer string, den: positive integer string }` or null. A measured row's
@@ -61,6 +65,20 @@ Always required, never null: `fact_id`, `kind`, `raw` (string), `quote` (string)
col_label? }`.
- All other fields are nullable with the types in `fact-ledger-schema.mjs`.
+Exact rational integer strings are canonical: no leading zeros, negative zero, negative denominator,
+or common factors. Unit aliases normalize at input, with numeric scaling for basis-point factors
+in composites. Derived rows store canonical units, compared exactly, including symbols' case; a source
+row's registered alias is canonicalized when the ledger is read (`unit-rules.md` dimension 31).
+
+A ledger written before `context`, `precision`, and `compatibility` existed still loads: source rows
+read those columns as null, and older derived or converted rows load but are never used as operands.
+
+Derived and converted rows require a matching operation, its closed context, distinct existing operands,
+formula, derived_from, precision, and compatibility. Source rows cannot carry derivation fields.
+Precision records nonnegative decimal places and positive significant figures, independently of display
+text. Recomputation verifies redundant audit fields and recursively verifies each shared dependency once.
+New work may consume only active transitive dependencies; historical verification ignores current eligibility.
+
## Labels
A label is unique across ALL rows, whatever their status. Retracting or superseding a row does
diff --git a/plugins/omo/skills/ulw-research/references/fact-ledger.md b/plugins/omo/skills/ulw-research/references/fact-ledger.md
new file mode 100644
index 00000000..445a125c
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/references/fact-ledger.md
@@ -0,0 +1,95 @@
+# Fact ledger
+
+`$SESSION_DIR/fact-ledger.json` holds every number and every direct quotation a research run may put in front of a reader. Rows come only from a source snapshot (through a deterministic locator), from a script-computed derivation, or from an explicit assumption with a rationale. A report source never carries a typed value: it carries a placeholder, and `fact fill` renders the row. The validity rules (ids, per-kind columns, the status machine) are in [fact-ledger-grammar.md](fact-ledger-grammar.md); this file is the working guide.
+
+Every command is `node "$SKILL_DIR/scripts/report-tools.mjs" <command> --session-dir "$SESSION_DIR"`; the flags and exit codes are in [deliverable-phase.md](deliverable-phase.md), sections 10 and 11.
+
+## Files
+
+| Path | Written by | Holds |
+| --- | --- | --- |
+| `sources/S<n>/text.txt` | `source snapshot` | the extracted text the locators read (one line per block; pages joined by a form feed); `sha256` covers this file |
+| `sources/S<n>/source.html` | `source snapshot` (page sources) | the captured page, read by the table locator |
+| `sources/S<n>/meta.json` | `source snapshot` | `source_id`, `url`, `observed_at`, `sha256`, `extractor`, `revision`, `kind`, `provenance` |
+| `sources/S<n>/sentences.jsonl`, `acronyms.json` | `source snapshot` | the sentence index (`S<n>:s<k>`) and the acronym table the fidelity gate reads |
+| `sources/S<n>/command.json` | `source snapshot --command` | argv, working directory, exit status, stdout and stderr hashes |
+| `fact-ledger.json` | `fact add`, `fact quote`, `fact derive`, `fact retract` | `{ schemaVersion: 1, rows, events }` |
+| `claims.json` | `claims index` | the claim table and the `verified-claims` membership |
+| `fidelity-defects.json` | `fact verify` | the verify record: the fidelity envelope (`record_kind: "fidelity"`, `artifact_sha256`, `report`, `written_at`, `defects`) plus `command`, `status`, `exitCode`, `format`, `counts`, `gatesRun` |
+| `fidelity-meaning-defects.json` | `fact meaning-check` | the meaning record: the fidelity envelope (`record_kind: "meaning"`) plus the run fields under Meaning verdicts |
+
+A source directory is immutable. Snapshotting the same id again with the same text is a no-op; different text fails with `snapshot_exists`, because rows pin the snapshot hash.
+
+## Row fields
+
+| Field | Meaning |
+| --- | --- |
+| `fact_id` | `F<n>` for a value, `Q<n>` for a quotation; assigned by the writer |
+| `kind` | `measured` (located in a snapshot), `quote`, `derived`, `converted`, or `assumed` |
+| `label` | the name a placeholder must repeat exactly (`[a-z0-9_-]`, up to 32 characters) |
+| `value`, `unit`, `exact` | the decimal value as written, its normalized unit, and the exact rational the arithmetic uses |
+| `raw`, `quote`, `value_span` | the source's own characters for the value, the whole containing sentence or cell, and where `raw` sits inside `quote` |
+| `qualifier` | a bounding word the source puts on the value: `upper_inclusive` (up to, at most), `lower_inclusive` (at least), `approximate` (about), `near` (nearly), `lower_exclusive` (over, more than), `upper_exclusive` (under, less than) |
+| `source_id`, `snapshot_sha256`, `sentence_id` | the snapshot the row was read from, its hash at that moment, and the sentence id |
+| `locator` | where `quote` sits in `text.txt` (`artifact`, `line`, `page`, `char_start`, `char_end`, and `table`, `row_label`, `col_label` for a table cell), plus the source's `source_kind`, `url`, `host`, and `pointed` (whether the brief's source floor names it) |
+| `extracted_by` | the snapshot's extractor: `tool:source-snapshot@1` (fetched or executed by the script), `model` (a capture the browser lane imported; lower trust), or `extractor:<name>@<version>`; `fact-derive` for a derivation |
+| `observed_at`, `valid_at` | when the row was registered, and the period the value describes (`--valid-at`) |
+| `entity`, `population`, `accounting_basis`, `observation_id` | declarations the derivations check (`--entity`, `--population`, `--accounting-basis`, `--observation O<n>`) |
+| `operation`, `operands`, `derived_from`, `formula`, `factor`, `render` | for a derived or converted row: what was computed from which rows, and the rendered text |
+| `rationale` | why an assumed value was chosen (required for `assumed`), or why an `expr` derivation is sound |
+| `status` | `active`, `retracted`, or `superseded`; rows are never deleted, and each change appends an event |
+
+## Locators
+
+| Flags | Resolves |
+| --- | --- |
+| `--find "<anchor>"` | one normalized anchor occurrence, then the first numeral starting within 80 characters after it, in the same sentence; `--unit-expected` checks the unit |
+| `--table <id or caption> --row <label> --col <label>` | the cell by header labels, never by index; duplicate labels fail |
+| `--page N --line N` | exactly one numeral on that line of an extracted document |
+| `--match "<words>"` (`fact quote`) | the unique sentence containing the words; the whole sentence is stored, so a later partial quote cannot drop its hedge |
+
+A locator that finds nothing returns `fact_not_located`, more than one candidate `fact_ambiguous`, and nothing is written. Read the printed row and its 60 characters of context on each side before using it; a wrong row is retracted, never edited.
+
+## Meaning verdicts
+
+`ingestVerdicts(pairs, verdictFiles, options)` (`scripts/meaning-check.mjs`) folds the meaning readers' verdict files over the pairs of `extractPairs` and returns four lists:
+
+| Field | Holds |
+| --- | --- |
+| `defects` | one `meaning_drift_confirmed` (both tiers named the same kind, never `other`) or `meaning_drift_warn` (one tier) per flagged pair, in pair order; the fragment comes from the verdict whose kind is reported |
+| `dropped` | every verdict that was not accepted, with its file, pair, tier, and reason (unknown pair, invalid tier or verdict, unknown or empty drift kind, a fragment missing from the sentence or the source) |
+| `unverified` | every pair no reader judged (`no_verdict`) and every `force_strong` pair no strong reader judged (`no_strong_verdict`), in pair order; a dropped verdict does not count as read |
+| `calibrationLines` | one JSON line per flagged pair, appended to `fidelity-calibration.jsonl` when `calibrationPath` is given |
+
+Coverage is fail-closed. A consumer treats a non-empty `unverified` list as blocking: those pairs were never read, so an empty `defects` list does not mean the report is clean. An unreadable verdict path or invalid JSON raises a `TypeError` naming the file, which the CLI maps to exit code 2.
+
+`fact meaning-check` writes these lists into `fidelity-meaning-defects.json` through the one fidelity record writer (`writeFidelityRecord` in `scripts/fidelity-records.mjs`). The record is the envelope every fidelity record carries, `record_kind` (`meaning`), `artifact_sha256` and `report` (copied from `pairs.json`, so the record names the edition the readers judged), `written_at`, and `defects` (the folded defects plus one `meaning_unverified` per unread pair), followed by the run fields `command`, `status`, `exitCode`, `run_id`, `pairs_sha256`, `counts`, `dropped`, `unverified`, `calibration_path`, `calibration_written`, `calibration_replaced`, and `promotion_eligibility`. Any other field makes the record malformed. A pairs file without `artifact_sha256` and `report` exits 2 before anything is written. The delivery barrier refuses the record when no delivered edition's source bytes hash to its `artifact_sha256`, so a rewrite needs a fresh `fact pairs` and reader run.
+
+## Operations
+
+- `fact add --source S<n> <locator> --label <label>` registers a measured row; `fact add --assumed <value> --unit <unit> --rationale <why> --label <label>` registers an assumption.
+- `fact quote --source S<n> --match "<words>" --label <label>` registers a quotation.
+- `fact derive --op <sum|diff|growth|ratio|share|convert|expr> ...` computes a new row from active rows and prints its id; the placeholder uses that id. Operands go by position for sum, diff, and convert (`--to <unit>`), as `--from/--to`, `--part/--whole`, or `--num/--den` for growth, share, and ratio, and as one formula argument for expr. Every op except convert declares its result unit with `--unit <unit>` (`--unit null` for dimensionless); a `%` sum also needs `--disjoint --reference <denominator> --rationale <why>`. The unit rules are in `unit-rules.md`.
+- `fact retract <id> --reason <why>` retires a row; `--replacement <id>` marks it superseded by another active row. Rows that depend on a retired row fail at fill time.
+- `fact export --csv --out <file>` writes `fact_id,label,plot_value,unit` for every active value row: the only data a chart may plot.
+- `fact fill <src> --out <file>` renders every placeholder from the ledger; `fact verify <file>` checks the filled edition against the ledger, the snapshots, and the claim index; `outcome set <format> delivered` records intent permissively, while the required `outcome verify` and `outcome finish` enforce the delivery barrier.
+- `fact pairs <file>` writes `pairs.json` for the meaning readers; `fact meaning-check --pairs pairs.json --verdicts <file> ...` ingests their verdicts.
+
+## Meaning command
+
+`pairs.json` is `{ schemaVersion, skipped, report, format, artifact_sha256, pairs_sha256, counts, verdict_schema, pairs }`. `verdict_schema` is the machine-read verdict contract (the allowed `model_tier`, `verdict`, and `drift_kind` values and the required fields), built from the registries in `meaning-check.mjs`; hand it to the reader with the pairs. Each pair has `pair_id` (`P<n>` in document order), `kind` (`quote`, `claim`, or `translation`), `sentence_key`, `sentence`, `source_lang`, `report_lang`, `force_strong`, and either `fact_ids` with `quote` or `claim_id` with `statement`.
+
+A reader writes one verdict file per tier: `{ "model_tier": "cheap" | "strong", "verdicts": [...] }` (a bare array works when each verdict names its own `model_tier`). One verdict per pair:
+
+| Field | Value |
+| --- | --- |
+| `pair_id` | a `pair_id` from `pairs.json`; any other id is dropped (`unknown_pair`) |
+| `verdict` | `ok` or `drift` |
+| `drift_kind` | for `drift`, exactly one of `subject_changed`, `scope_widened`, `hedge_dropped`, `certainty_added`, `causality_added`, `direction_flipped`, `population_changed`, `evidence_grade_raised`, `other` |
+| `fragment` | for `drift`, a non-empty verbatim substring of the pair's `sentence` (whitespace-normalized) |
+| `source_fragment` | for `drift`, a non-empty verbatim substring of the pair's `quote` or `statement` |
+| `explanation` | one sentence, for the briefing; never read by the scripts |
+
+A verdict that breaks any rule is dropped and listed in the record's `dropped` with its reason; it never becomes a defect. Both tiers naming the same `drift_kind` other than `other` on one pair is `meaning_drift_confirmed` (major); one tier, or two tiers that disagree, is `meaning_drift_warn` (minor). `--dispute P<n> --reason` keeps the defect and marks its calibration line `disputed`; the command still exits `1`, and the way forward is the repair loop described in deliverable-phase.md ("After a dispute"). A `{"skipped": true}` pairs file is accepted only when the locked brief says `meaning check: off`; with the check on (`meaning_check_required`) or no locked block (`fidelity_interview_incomplete`) it exits `1`. Every entry of `unverified` becomes one `meaning_unverified` defect (major), so a pair no reader judged fails the command (exit 1) through the same registry policy as any other defect. A malformed `pairs.json` or an unreadable verdict file exits 2.
+
+`fidelity-calibration.jsonl` starts with a header line `{ "header": true, "promotion": ... }` holding the promotion criterion, then per run one run line `{ "run": true, run_id, pairs_sha256, flagged }` (written even when nothing was flagged, so `min_runs` counts real runs) and one line per flagged pair: `kind`, `pair_id`, `tiers`, `drift_kind`, `confirmed`, `disputed`, `reason`, `run_id`, `pairs_sha256`. The run id (`--run-id`, or a hash of the pairs and verdict files only, never the dispute) keys the run: rerunning the same inputs, with or without `--dispute`, replaces that run's lines in place, and a dispute recorded earlier carries forward to the rewritten lines. A corrupt log line exits `2` naming `<path>:<line>`. The record's `promotion_eligibility` evaluates the criterion over the log, counting report-level false blocks over the last `false_block_window` runs.
diff --git a/plugins/omo/skills/ulw-research/references/fidelity-claim-kinds.md b/plugins/omo/skills/ulw-research/references/fidelity-claim-kinds.md
new file mode 100644
index 00000000..0bc6fad8
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/references/fidelity-claim-kinds.md
@@ -0,0 +1,19 @@
+# Fidelity Claim Kinds
+
+A report sentence states a plain fact or quote by default. When it claims more than that (that we ran or checked something, that a number is derived, that it is an assumption), the writer marks the claim kind in the placeholder, fill renders the mark as the `data-claim-kind` attribute on the element it writes, and the fidelity gate checks that every marked fact cites a ledger row the kind accepts. The gate never infers a claim kind from wording: an unmarked sentence is checked as a plain fact or quote (so an unmarked `assumed` row is a `claim_kind_mismatch`: an unmarked assumption reads as a fact and needs its `{{A:F12:label}}` mark), and only the unmarked-warning rule raises a warning (severity minor, never blocking) when a sentence reads like the kind and cites a fact not marked with it.
+
+A mark binds per cited fact. Each `data-fact` element takes the kind of its nearest mark (its own attribute, else the closest marked ancestor), and only that kind is checked against its row; a parent mark applies to the facts it binds, never to the sentence as a whole. A fact under marks of two different kinds is a `claim_kind_mismatch`, and a mark that binds no fact of its own (all of them are shadowed by another kind, or it cites none) is its kind's defect: shadowing never satisfies a parent.
+
+A `verified` claim is satisfied only when the cited row itself has a locator artifact inside `sources/<source_id>/` whose snapshot `meta.json` has `kind: "command"` (recorded command or test output); a different command row elsewhere in the sentence never satisfies it. Korean phrases are shown as `\u` escapes, exactly as `claim-kinds.mjs` stores them. The defect codes are in [report-gates.md](report-gates.md).
+
+<!-- claim-kinds:begin (rendered from CLAIM_KINDS; do not edit by hand) -->
+| Kind | Placeholder | Attribute fill emits | Accepted row kinds | Required source kind | Defect when unsatisfied | Unmarked warning | Notes |
+|---|---|---|---|---|---|---|---|
+| `fact` | `{{F12:label}}` | none (or `data-claim-kind="fact"`) | `measured`, `converted`, `derived` | any | `claim_kind_mismatch` | none | Accepts exactly what an unmarked numeral accepts, so marking fact is never stricter than not marking. Neither accepts an `assumed` row: an unmarked assumption reads as a fact, so it needs its `{{A:F12:label}}` mark (`claim_kind_mismatch` otherwise). |
+| `quote` | `{{Q7:label}}` | none (or `data-claim-kind="quote"`) | `quote` | any | `claim_kind_mismatch` | none | - |
+| `verified` | `{{V:F12:label}}` | `data-claim-kind="verified"` | `measured`, `quote` | `command` | `unsupported_verification` | `verification_claim_unmarked` on a first-person run: we/I (have) ran, run, executed, verified by (not ran out of); by running after we/our in the sentence; \uC6B0\uB9AC or \uC800\uD76C as the subject word (+ \uAC00, \uB294, or \uD300\uC774), then \uC2E4\uD589\uD588, \uC2E4\uD589\uD558\uC5EC, \uC2E4\uD589\uD574, \uB3CC\uB838, or \uB3CC\uB824 in the clause with no third-party subject between; \uC9C1\uC811 with no third-party subject, then \uC2E4\uD589 or \uB3CC\uB9AC in the clause | The cited row itself must have a locator artifact inside `sources/<source_id>/` whose `meta.json` kind is `command`; another command row in the sentence never counts. Whether it is the right command for the claim is the meaning verifier's call (minor), not this gate's. |
+| `derived` | `{{D:F12:label}}` | `data-claim-kind="derived"` | `derived`, `converted` | any | `claim_kind_mismatch` | none | - |
+| `assumed` | `{{A:F12:label}}` | `data-claim-kind="assumed"` | `assumed` | any | `claim_kind_mismatch` | none | Numeric assumptions only (an assumed ledger row carries a decimal value); a qualitative assumption stays prose. |
+<!-- claim-kinds:end -->
+
+Regenerate the table with `node scripts/claim-kinds.mjs --render-doc`.
diff --git a/plugins/omo/skills/ulw-research/references/report-gates.md b/plugins/omo/skills/ulw-research/references/report-gates.md
index 69c051bb..a9fe47bc 100644
--- a/plugins/omo/skills/ulw-research/references/report-gates.md
+++ b/plugins/omo/skills/ulw-research/references/report-gates.md
@@ -44,7 +44,7 @@ Raised only by the fidelity verifier (the fact ledger, fill, verify, meaning, an
| `pointer_label_mismatch` | blocker | true | A placeholder's label differs from the referenced ledger row's label (a typo guard on the id). | The placeholder's label is not the row's label; check the id, then the label. |
| `quote_anchor_unresolved` | blocker | true | A quote cut's from or to anchor is absent, occurs more than once, or appears in reverse order in the quote. | A from or to anchor is missing or occurs more than once in the quote; pick unique anchor words. |
| `unit_duplicated` | blocker | true | A unit-bearing placeholder is followed by another copy of its own unit. | The unit after the placeholder repeats the row's unit; use :value or remove the unit. |
-| `polish_diff_fail` | blocker | true | The polish pass changed protected fact values, numerals outside fact spans, tickers, or table row counts. | The polish pass changed numbers, tickers, or table rows; revert to the pre-polish artifact and apply the language fixes only. |
+| `polish_diff_fail` | blocker | true | The polish pass changed protected fact spans, numerals or units, tickers, table rows, or any markup byte (including every attribute, comments, and script/style/template content). | Revert to the pre-polish artifact and apply text-node edits only; markup changes require the full fidelity gates. |
| `fidelity_interview_incomplete` | blocker | true | A fidelity field in the brief is still pending or missing when a fidelity command runs. | A fidelity field is pending or missing in brief.md; answer the open question or record its default, then rerun. |
| `derived_not_allowed` | blocker | true | A placeholder resolves to a derived, converted, or assumed row while the brief allows no derived numbers. | The brief allows no derived or assumed numbers; cite a source row directly or change the brief with the requester. |
| `source_outside_floor` | blocker | true | A ledger row's source lies outside the source floor the brief records (pointed sources only, or the allowed source classes). | The row's source is outside the source floor the brief records; use a source the brief allows or remove the fact. |
@@ -53,7 +53,8 @@ Raised only by the fidelity verifier (the fact ledger, fill, verify, meaning, an
| `quote_unbound` | blocker | true | A direct quotation in the report has no ledger quote row behind it; a citation marker alone does not bind it. | A direct quotation has no ledger quote row; register it with fact quote and insert it through its placeholder. |
| `edition_unverified` | blocker | true | A delivered edition has no fact verify record whose artifact hash matches the delivered source. | The delivered file has no matching fact verify record; run fact verify on that edition and record it with --verified-by. |
| `untraceable_number` | major | false | A number or date in prose or a table cell resolves to no ledger row and is not structural; a citation does not exempt it. | Replace the typed number with a {{F<n>}} placeholder backed by a ledger row, or register it as assumed with a rationale. |
-| `unsupported_verification` | major | false | A sentence asserts a command or tool check that no recorded command-output row backs. | The sentence claims a tool check that no recorded command output backs; link the recorded output row or remove the claim. |
+| `unsupported_verification` | major | false | A fact bound to a `verified` mark (`data-claim-kind="verified"`, from a `{{V:F<n>:label}}` placeholder) cites a row whose own locator artifact does not sit in a recorded command-output snapshot, or the mark binds no fact of its own. Another command row in the same sentence never counts. Unmarked prose is never raised here. | A sentence marked verified (data-claim-kind="verified") cites a row that is not recorded command output; cite the recorded output row or drop the verified mark. |
+| `claim_kind_mismatch` | major | false | A fact bound to a claim kind cites a row of a kind that claim kind does not accept, a fact sits under marks of two different kinds (nested marks), a mark binds no fact of its own, or a mark names an unknown kind; the kinds are in [fidelity-claim-kinds.md](fidelity-claim-kinds.md). | An element marked with a claim kind (data-claim-kind) cites a row that kind does not accept, or the kind is unknown; cite a row of that kind or fix the mark (references/fidelity-claim-kinds.md). |
| `conflicting_readings` | major | false | Two active ledger rows with the same value and unit but different periods or qualifiers are cited as one fact. | Two ledger rows with different periods or qualifiers are cited as one fact; split the sentence or retire one row. |
| `hedge_dropped` | major | false | A hedge, condition, or negation class present in the source quote is absent from the report sentence. | Restore the qualifier the source uses (estimated, about, not yet). |
| `direction_flipped` | major | false | The source states an increase and the sentence a decrease, or the reverse. | The source states the opposite direction; rewrite from the quote. |
@@ -61,20 +62,22 @@ Raised only by the fidelity verifier (the fact ledger, fill, verify, meaning, an
| `qualifier_dropped` | major | false | A bounding or approximation qualifier recorded on the ledger row (up to, at least, about) is absent near the value in the sentence. | The source bounds this value (up to, at least, about); restore the qualifier or use :q. |
| `unbound_sentence` | major | false | A findings sentence carries no fact, quote, citation marker, or claim binding and is not marked as opinion or recommendation. | Bind the sentence to a fact, a quote, a citation, or a claim-graph node with data-claim. |
| `meaning_drift_confirmed` | major | false | Two independent meaning readers flag the same drift kind on the same sentence and quote pair. | Two independent readers found the same drift; rewrite from the quote or dispute with a reason. |
+| `meaning_unverified` | major | false | `fact meaning-check` found a pair no reader judged, or a force_strong pair no strong reader judged; an empty drift list is not a clean check. | No meaning reader judged this pair, or no strong reader judged a force_strong pair; run the missing reader on pairs.json and rerun fact meaning-check. |
| `polish_edit_rejected` | major | false | A proposed polish edit changes a protected number, hedge, qualifier, link, or claim id. | The edit changed a number, a hedge, a qualifier, a link, or a claim id; re-propose the language fix only. |
| `polish_hedging_decreased` | major | false | The polish pass removed a hedge the source grounds. | The polish pass removed qualifiers; restore them. |
-| `polish_text_churn` | major | false | More than half of the visible prose changed during the polish pass. | More than half the text changed during polish; the pass composed instead of proofreading. |
+| `polish_text_churn` | major | false | More than half of the visible prose changed during the polish pass (`POLISH_CHURN_LIMIT` in `scripts/polish-rules.mjs`). | More than half the text changed during polish; the pass composed instead of proofreading. |
| `snapshot_mismatch` | major | false | An operation that needs one collection window uses operands from unequal windows or observations outside the window. | The operands come from different collection windows; use rows from one window or state the windows separately. |
| `join_integrity_fail` | major | false | A table row combines facts from different source observation rows, or a declared observation id contradicts the row's lineage. | A table row combines facts from different source observations; keep each row to one observation or split it. |
| `quote_stale_version` | major | false | The quoted source revision differs from the revision the report targets for the same scope. | The quoted source revision differs from the revision the report targets; re-snapshot the target revision or date the quote. |
| `quote_fuzzy_match` | minor | false | An exact quote match failed but a candidate with similarity of at least 0.95 and identical protected tokens exists (OCR noise); diagnostic only. | The source text is OCR-noisy; confirm the snapshot and prefer the raw document. |
| `name_outside_quote` | minor | false | An uppercase Latin token of 2 to 6 letters in the sentence is absent from the supporting source text and that source's acronym table. | A name in the sentence is not in the cited quote; add the source or remove the name. |
+| `verification_claim_unmarked` | minor | false | A sentence reads as a first-person run ("we ran", "we have run", "I ran", "we executed", "we verified by", "by running" after we/our, or the Korean rule in fidelity-claim-kinds.md) and cites a fact not marked `verified` (or cites none and its block is unmarked). A warning only; it never blocks. | The sentence reads as a check we ran but carries no verified mark; mark it {{V:F<n>:label}} on a recorded command-output row, or reword it. |
| `scope_widened_warn` | minor | false | The sentence uses a broader scope term than the quote's narrow term, and no supporting fact carries the broad scope. | The sentence uses a broader scope word than the quote; confirm the scope or narrow it. |
| `particle_unverified` | minor | false | A particle after a filled value could not be checked against the value's final reading. | The particle after this value could not be checked; read it aloud and fix it by hand if wrong. |
| `fallback_without_entity` | minor | false | A quote that replaced a twice-failed sentence has no entity for its attribution line. | The replacement quote has no entity for attribution; set one on the row. |
| `meaning_drift_warn` | minor | false | One meaning reader flags a change of scope, subject, or certainty without independent agreement. | A separate reader found the sentence changes scope, subject, or certainty; rewrite from the quote. |
| `polish_edit_unlocated` | minor | false | A polish edit's before-text matches zero or several places in the current text. | The edit's before-text was not found exactly once; re-propose against the current text. |
-| `polish_link_changed` | minor | false | The polish pass altered or removed a link target, or an internal target stopped resolving. | A link was altered or removed during polish; restore it. |
+| `polish_link_changed` | minor | false | The polish pass altered or removed an href link target, or an internal target stopped resolving. | A link was altered or removed during polish; restore it. |
| `polish_name_changed` | minor | false | A ledger-backed proper noun present before polish is absent afterward. | A proper noun changed during polish; restore it. |
### Layout Gate (Rendering & Presentation)
@@ -101,6 +104,20 @@ Gates emit a manifest-level outcome with one of three statuses:
The `layout` gate only runs when a probe file (a rendering artifact: PDF, PNG, or HTML snapshot) is provided alongside the manifest. If no probe file is available, the manifest records `layout: not_run` and the gate does not emit defects.
+## Polish Text-Node Contract
+
+Polish edits change text nodes only; markup changes go through the full fidelity gates, not polish.
+
+The primary rule is a whole-document skeleton comparison. Both gates scan the pre-polish and the polished document with the same tokenizer (`scripts/html-markup.mjs`) and require the skeleton to be byte-identical in document order: every tag token, comment, bogus comment, the frozen content of `script`, `style` and `template`, and every character reference of a text node (`&` up to the first `;` or non-name character). An accepted edit carries no `<`, `>` or `&` and may not touch a character reference, so an edited span holds no skeleton token and a legitimate polish never moves this sequence. Any difference names the first differing token and its byte offset: `polish-apply` refuses the edit, and `polish-diff` reports the blocker `polish_diff_fail` ("skeleton changed at token ..."). The character guards below are the early, actionable refusals; this comparison is what makes the guarantee complete, so a hand edit from `R&amp;D` to `R&lt;D` is blocked even though no edit was involved.
+
+An edit's `before` and `after` may not contain `<`, `>` or `&` at all. `polish-apply` refuses such an edit before any other check, as `polish_edit_rejected` with the reason `edit contains markup characters (<, >, &): route this section through the full fidelity gates`, so the refusal is visible, never a silent drop. The rule holds whatever the tokenizer models: an edit without these characters cannot create, alter, close or escape a tag, comment, bogus comment, CDATA section, entity or script. The characters alone are not enough, because a located region can start right after an existing `&` or inside a character reference: `polish-apply` also refuses, as `edit touches a character reference: route this section through the full fidelity gates`, any edit whose located region overlaps a character reference or opens one. Literal `&` and `<` in prose are entities in the source (`R&amp;D`, `p &lt; 0.05`) and stay usable only by leaving their bytes untouched: the proofread lane narrows an edit to the words around an entity or tag (`budget grew` to `budget rose`), never one that spans it. Wording that cannot be fixed without touching an entity or markup goes through the full fidelity gates.
+
+Before comparing, both gates refuse with `polish_diff_fail` ("ambiguous markup: ...") any document holding a construct the polish tokenizer does not model exactly: `</` followed by a non-letter (`</>` included), `<!--` inside a `script` or `style` element, `<![CDATA[`, `<?`, `--!>`, or `<` inside a tag or attribute value. Renderer output contains none of these. The tokenizer still follows the HTML spec for the bogus comment (`</` + non-letter, `<?`, `<!` up to the next `>`), the abrupt comment closes `<!-->` and `<!--->`, and the script-data escaped and double-escaped states.
+
+The shared rule in `scripts/polish-rules.mjs` runs per edit against the current and original report in `polish-apply`, and against the whole document in `polish-diff`. The sequence of opening and closing tags, every attribute name and value (including `href`, `id`, `class`, and `style`), comments, declarations, and all content of `script`, `style`, and `template` elements must be byte-identical. Attribute reordering, different quoting, tag case, whitespace inside a tag, and a self-closing slash are markup changes too. Any difference raises the blocker `polish_diff_fail`; apply rejects it with reason `markup changed`.
+
+Text nodes remain subject to the existing fact-span, number/unit, ticker, hedge, qualifier, name, and churn checks. Protected numbers, tickers, and required hedges cannot migrate between text nodes. A text edit inside an existing fact span is still a fact-span change, including under `aria-hidden`. Script/style/template content is frozen through the end of input when unclosed. No CSS or HTML visibility model is used by polish.
+
## Lineage and Citation Markers
Defect codes and their hints are delivered via the outcome manifest under `residualDefects` (if repair did not exhaust). Each defect entry includes:
diff --git a/plugins/omo/skills/ulw-research/references/report-visualization.md b/plugins/omo/skills/ulw-research/references/report-visualization.md
index 72edc99f..7d5f065f 100644
--- a/plugins/omo/skills/ulw-research/references/report-visualization.md
+++ b/plugins/omo/skills/ulw-research/references/report-visualization.md
@@ -44,6 +44,8 @@ uv run --with numpy --with polars --with matplotlib python plot_<name>.py
- Keep chart text as real text: set `matplotlib.rcParams["svg.fonttype"] = "none"` before saving an SVG. The default converts text to paths, and the static gate then sees a chart with no title, ticks, or labels.
- Use the design spec's font family and palette tokens (`rcParams["font.family"]`, explicit colors from the spec). One accent color for the series that carries the finding; neutral tones for the rest.
- Title, axis labels with units, value labels on bars or end points, ticks at round values computed from the data range.
+- Plot only from the fact ledger: export the rows with `node "$SKILL_DIR/scripts/report-tools.mjs" fact export --csv --out "$SESSION_DIR/assets/<chart>.csv" --session-dir "$SESSION_DIR"`, draw from that CSV, and give the chart figure `data-chart-source="assets/<chart>.csv"`.
+- Emit the axis tick labels inside one `<g class="ticks" data-tick-step=".." data-tick-min=".." data-tick-max="..">` group, so the fidelity gate can check every tick against the declared grid and every value label against the ledger.
- One message per chart. A chart that needs a paragraph to explain is two charts.
- For a web deliverable, HTML/CSS bars or an inline SVG built from the same table are preferred over an image.
diff --git a/plugins/omo/skills/ulw-research/references/unit-rules.md b/plugins/omo/skills/ulw-research/references/unit-rules.md
new file mode 100644
index 00000000..cc8a8e30
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/references/unit-rules.md
@@ -0,0 +1,157 @@
+# Unit rules for derived facts
+
+The one definition of which unit a derived or converted fact row carries, and the reviewer's checklist for
+`scripts/unit-rules.mjs`. ONE registry (`OPERATION_RULES`) owns, per operation: arity, operand-unit guards, context
+fields, result-unit inference, and malformed shapes. `derive()` (`fact-derive.mjs`) and `recompute()`
+(`fact-recompute.mjs`) both call it; neither has op-specific unit code. `row.unit` is NEVER used to choose arithmetic;
+numeric guards may read `exact` values, unit selection may not. The property tests in
+`scripts/fact-unit-rules.test.ts` iterate the registry, so a new operation needs a table row here and cases there.
+
+## Units
+- Canonical unit text is stored and compared exactly (case-sensitive symbols). Registered aliases
+ (`pp` -> `%p`, `kilometre` -> `km`, ...) are canonicalized ONLY at input (derive/fact add); a relabel to an alias
+ at rest is a relabel and is detected. Unit text with no alias is stored as given: whitespace is trimmed only around
+ `*`, `/`, `^`, and parentheses, so `million people`, `metric ton`, and `USD per person` keep their inner spaces.
+- A scale word (`million`, `M`, `bn`, `billion`, `thousand`, `e6`, ...; the `scale` rows of `units.mjs`) is a
+ multiplier, not a unit, and denomination scaling is out of scope, so it never becomes a unit atom: `million`,
+ `USD/M`, and `million*person` are refused at input (derive, and every operand read). Either store the scaled numeral
+ in the counted unit (5 million people as `5000000 person`) or keep the whole phrase as plain unit text:
+ `million people` has no table alias as a whole, so it is stored as given and sums only with `million people`, never
+ with `person`.
+- `null` = dimensionless. Omitted unit on a derived spec is an error, not null (distinguish omission from declared null).
+- Percent `%` stores percentage numerals (20% stores 20). Delta `%p` (percentage points). 1 basis point = 0.01 %p
+ (input alias `bp` -> `%p` with /100 scaling).
+- Composite units are canonical algebraic strings built by the registry (`USD/kg`, `person`), never by concatenating
+ operand strings: equal factors cancel (`USD/(USD/person)` -> `person`).
+- Affine scales (temperature: `C`, `F`) are flagged in the unit table; sum/diff/growth/share/ratio and every expr
+ node refuse them (temperatures are not summed or differenced as absolute values); only convert takes them.
+ Affine atoms in composites are refused before cancellation. Exponents are exact signed bigint integers, including zero.
+
+## Operation table
+
+| op | arity | operand guards | context fields (persisted on the row) | result unit | malformed -> recompute ok:false |
+|---|---|---|---|---|---|
+| sum | n>=2 | non-affine units, all equal after canonicalization; if `%`: `disjoint: true` AND `reference` (one denominator id/text) equal on every operand AND rationale | disjoint, reference, rationale | the common unit (`%` stays `%`) | <2 operands; missing id; no context; unequal/affine units; `%` without disjoint, reference, or rationale |
+| diff | 2 | non-affine units; typed pairs only: U-U -> U (same non-% unit); %-% -> `%p`; %-%p -> `%`; %p-%p -> `%p`; %p-% REFUSED | none | per the pair rule | arity; invalid pair (`%p - %`, `m - person`); affine units; stray context |
+| growth | 2 (from,to) | equal non-affine units; same entity; two different valid_at; from.exact > 0 | none | `%` (= 100(to-from)/from) | arity; unequal/affine units; entity/valid_at; from<=0; stray context |
+| share | 2 (part,whole) | equal non-affine units; whole.exact > 0; 0 <= part <= whole | none | `%` (= 100 part/whole) | arity; units; whole<=0; part outside [0,whole]; stray context |
+| ratio | 2 (num,den) | non-affine units; den.exact != 0; unequal `%`/`%p` REFUSED | none | registry composite: equal units -> null; else canonical `U(num)/U(den)` with cancellation | arity; den=0; stray context |
+| convert | 1 | U(a) has a registered factor to `target`; currencies REFUSED (use expr with a dated rate row) | target (persisted as canonical text; the ONLY accepted declared unit; `row.unit` is compared to it exactly, never canonicalized at rest) | the persisted target | arity; no context; target missing; target not canonical (an alias at rest); currency target; no factor; declared unit != persisted target |
+| expr | n>=1 | formula ids == operand set; node rules: `+`/`-` follow the sum/diff typed-pair rules, affine refusal included (so `20% - 10%` is `%p`, never `%`, and `25 C + 30 C` is refused); `+` also takes `%` + `%p` in either order -> `%` (as diff's `%-%p`), while `%` + `%` is refused (use sum); `*` and `/` compose canonical composites, `%`/`%p` operands normalize by /100 to dimensionless first; result must be fully inferred: NO rationale escape | none | the inferred unit; an explicit null->`%` conversion is a separate `convert`-like node (`pct()`) | formula/operand id mismatch; parse error; /0; uninferable unit; affine operands; stray context |
+
+Every row's malformed column also holds the two operand-identity shapes below (`repeated operand`, `self reference`).
+
+## Operand identity (applies to every row)
+- Operand ids are DISTINCT within one derived row. sum of F1, F1 is not a sum of two shares, and diff, ratio, share,
+ growth, or convert of an operand with itself is meaningless and would dodge every pair guard. A repeated id is
+ refused by `derive()` (`operand F1 is repeated`) and is `ok:false` at `recompute()` (malformed shape
+ `repeated operand`). The check sits in the registry (`expectedUnit` and `malformed`), so no operation can skip it.
+- expr may reference the same id in several leaves (`F1 * F1` is a legitimate square). Its stored operand list is the
+ formula's id SET, which must equal the formula ids (`formula id mismatch`); an id listed twice in that list is the
+ `repeated operand` shape too.
+- A derived row may not cite itself, directly (`self reference`: its own id among its operands) or through nested
+ derived operands (a cycle). `recompute()` refuses both. `derive()` recomputes every derived operand first and
+ refuses one that does not recompute (tampered, cyclic, or self-citing), so nothing is built on such a row.
+
+## Invariants (property tests iterate the table)
+1. For each op: generate valid typed operands (rational boundaries, aliases at input, composites, %/%p, affine,
+ metadata, conversion targets, repeated expr refs), derive, assert `recompute(row).ok === true`.
+2. Relabel: for each op, set `unit` to every different canonical string/null (incl. aliases and other valid targets),
+ regenerate render, keep exact: `recompute(row).ok === false` naming the expected unit; and `derive()` with that
+ declared unit is refused.
+3. Malformed: per op, every shape in the table's malformed column has one case, named in the registry's
+ `OPERATION_RULES[op].malformed` list (the test asserts its case names equal that list): `recompute` returns
+ `ok:false` for that shape's own reason, never throws.
+4. Nested derived rows are verified recursively (operands may be derived rows); cycles and self references are
+ refused, by `recompute()` and by `derive()` before it builds on a derived operand.
+5. Render precision (`renderPrecision` in `unit-rules.mjs`): fewest decimal places for sum, diff, an expr whose root
+ node is `+` or `-`, and every `%p` result; fewest significant figures otherwise (an expr `*` or `/` root with a
+ non-`%p` result included). Operand values and expr literals both count.
+
+## Decisions
+- Q1: the canonical composite goes in `unit` (consumers compare `unit` text); no side field.
+- Q2: expr with an unresolved unit is REFUSED; a known-unit `a*b/c` is inferable, so no rationale escape exists.
+- Q3: canonical `%p`, input alias `pp` (matches units.mjs).
+- Q4: strict null metadata: null matches only null; required metadata must be present.
+
+## Out of scope (follow-ups)
+Per-capita identity, named index/base and index-point differences, weighted means, dated FX / nominal-real bases,
+same-currency denomination scaling (M/B), summing ratios over a common base. Each needs its own table row + tests.
+
+## Dimensions
+
+1. Enforced: kind and operation agree; a changed discriminator cannot disable recursive verification.
+2. Enforced: expression and direct arithmetic check population, snapshot, and accounting metadata; common metadata propagates.
+3. Enforced: stored `precision: { places, sigfigs }` carries operand precision independently of display trailing zeros.
+4. Enforced: scaled aliases normalize at every composite factor and exponent, scaling the input numeral exactly.
+5. Enforced: derive and recompute return classified refusals with `shape`, `expected`, and `detail`; derive never appends on refusal.
+6. Enforced: aliases canonicalize at input; noncanonical stored results and conversion targets are refused, and a source unit that needs rescaling (a scaled alias) is refused as an operand.
+7. Enforced: stored rationals are reduced integer strings with positive denominators and canonical zero.
+8. Enforced: operation contexts have closed keys and types, including non-percent sums and context-free operations.
+9. Enforced: formula, factor, derived_from, raw, render, metadata, precision, and compatibility are compared on replay.
+10. Enforced: metadata object equality ignores key insertion order; missing and invalid metadata never equal explicit null.
+11. Enforced: new derivations require active transitive dependencies; historical arithmetic remains reproducible after retraction.
+12. Enforced: graph and expression traversal use explicit stacks; shared nodes are evaluated once per verification.
+13. Enforced: factor exponents use bigint, preserve grouping and quotient direction, and canonicalize idempotently.
+14. Enforced: affine atoms cannot hide in composites or cancellation; scale words are never unit atoms.
+15. Enforced: disjoint percentage shares are individually in [0,100], total at most 100, and use exactly one reference.
+16. Enforced: currency table conversion is refused; FX expressions require a dated rate and persist compatibility `unchecked`.
+17. Enforced: symbols are case-sensitive; only registered spelling aliases normalize; opaque Unicode and phrases stay distinct.
+18. Enforced: missing, undefined, and empty required fields do not substitute for explicit null; zero remains valid.
+19. Enforced: identifiers allocate exactly beyond 2^53 using bigint; operand lists remain distinct.
+20. Enforced: a supplied expression operand list must equal the formula's unique leaf set.
+21. Enforced: additive expression roots and percentage-point results use decimal places; other roots count literal significant figures.
+22. Enforced: cloned snapshots with the same spec and timestamp produce identical rows; replay appends a new identity.
+23. Documented limitation: coherent rewrites of trusted operands, context, and all dependent audit fields require external provenance to detect.
+24. Enforced: arity belongs to each table row; sums are permutation-invariant while ordered operations keep positional meaning.
+25. Enforced: denominators, positive growth starts, and bounded shares obey numeric domains; ordinary negative arithmetic remains valid.
+26. Enforced: percent, percentage-point, and fraction semantics follow typed pairs, multiplicative normalization, and explicit pct nodes.
+27. Enforced: table factors and affine offsets compose exactly; unknown and cross-dimension conversions are refused.
+28. Enforced: only decimal literals, fact identifiers, binary arithmetic, parentheses, and pct parse; unsupported syntax is refused.
+29. Enforced: rounding is half away from zero; values are plain decimals and display uses canonical units and grouping.
+30. Enforced: seeded factor trees and conversion compositions have expected values computed independently of OPERATION_RULES.
+31. Enforced: a ledger written before `context`, `precision`, and `compatibility` existed still loads and accepts new derivations. On measured, quote, and assumed rows a missing column reads as null, and a registered alias unit is canonicalized on read (unknown or scaled text stays as given); the file changes only on the next write. Derived and converted rows without those columns load, but `recompute` reports shape `legacy derived row` and `derive` refuses them as operands with the same shape until they are regenerated.
+32. Enforced: value/exact agreement. When a row stores both, `exact` must equal `value` at the value's decimal places; a legacy row that disagrees loads and is flagged `dimension 32`, and `addRow` refuses a new one.
+
+## Precedence: dimension 31 wins at read time
+
+Anything dev's shipped writer could write loads and validates. Every row is first read through `readRow`: exacts that
+are integer pairs are reduced (sign on the numerator, no leading zeros, positive denominator), decimal values lose
+leading zeros, and source rows get canonical units and null later columns. Measured exacts stay as stored because they
+are always the reduced exact of `value`. Only then do the strict dimensions run. They apply in full to derived and
+converted rows that carry this version's columns, and to every row this version writes. A source row that loads but
+still breaks a strict rule (a scale word as unit, empty metadata, an operation or derivation fields) is flagged when it
+is used: `derive` and `recompute` refuse it with shape `dimension 6`, `dimension 18`, or `dimension 1`, and `addRow`
+refuses to write a new one. Normalization never strips user data: an `exact` carrying keys beyond `num` and `den` is left exactly as written and flagged `dimension 7` on use, never silently reduced to `{ num, den }`. Reading a normalized file again changes nothing. The legacy test fixture is generated by
+dev's own `addRow` (`scripts/tests/fixtures/fidelity/gen-legacy-dev.mjs`, which pins the dev commit).
+
+## Trust boundary
+
+Recomputation verifies arithmetic against the ledger's trusted operands and operation context, not against an external
+immutable source. A zero-metre conversion rewritten from km to cm together with its target, formula, factor, precision,
+and display is indistinguishable from an honestly generated cm conversion. Single-field relabels are detected; a coherent
+rewrite needs an immutable ledger baseline or source provenance outside this arithmetic verifier.
+
+Rows carry no per-row schema marker, so a current derived row with `context`, `precision`, and `compatibility` all
+deleted reads as a legacy row: `validateLedger` skips its at-rest unit and row-shape checks, and a relabeled unit on
+it passes validation. It never becomes trusted again: `recompute` and `derive` refuse it as `legacy derived row`.
+
+## Implementation notes
+
+- Context fields live in the row's `context` column (null or an object on derived and converted rows, always null on
+ measured, quote, and assumed rows: a `KIND_RULES` column):
+ sum stores `{ disjoint, reference }`, convert stores `{ target }`. The sum rationale stays in `rationale`.
+ Schema and recomputation both require the operation's context. Context-free rows carry explicit null.
+- `schemaVersion` remains 1. A derived or converted row from an older writer (no `context`, `precision`, or
+ `compatibility`) is kept as written and never used as an operand: regenerate it with `derive` to carry precision,
+ compatibility, and complete audit fields. Older source rows need nothing: they are read with null later columns.
+- `reference` is either one text (the caller asserts one shared denominator) or a map from each operand id to its
+ denominator; the map passes only when every operand names the same non-empty denominator.
+- `addRow` canonicalizes source units and scales alias numerals, including `100 bp/person` to `1 %p/person`.
+ At rest, source-row aliases are canonicalized on read (dimension 31); current derived rows with alias units are refused. A declared result unit with a nontrivial alias scale is refused.
+ Scale exponents with absolute value above 10000 are explicitly refused instead of allocating an unbounded numeral.
+- A ratio over `%` or `%p` operands with unequal units is refused (use expr, where `%` enters `*` and `/` as a fraction).
+- convert reads its target from `to`; a declared `unit` is optional and must equal that target.
+- expr persists compatibility `unchecked` on the row as well as returning it. Population, snapshot, and accounting
+ predicates apply; a currency-rate operand needs a nonempty `valid_at`. This is arithmetic and unit verification,
+ not a guarantee of rate freshness, matching dates, population identity, or financial comparability.
diff --git a/plugins/omo/skills/ulw-research/scripts/claim-kinds.mjs b/plugins/omo/skills/ulw-research/scripts/claim-kinds.mjs
new file mode 100644
index 00000000..b73b5984
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/claim-kinds.mjs
@@ -0,0 +1,116 @@
+// claim-kinds.mjs - the claim kinds a report sentence can carry, as one table, CLAIM_KINDS.
+//
+// A claim kind is structural: the writer marks it in the placeholder ({{V:F12:label}}), fill
+// emits it as the data-claim-kind attribute on the element it renders, and the fidelity gate
+// (fidelity-claims.mjs) checks that every marked element cites a row the kind accepts. An
+// unmarked element is a plain fact or quote and is checked only by the value and quote checks.
+// The one lexical piece is `unmarked`: a few first-person run patterns that raise a minor warning
+// when a sentence reads like the kind but carries no mark; they never raise a major. The table in
+// references/fidelity-claim-kinds.md is rendered from CLAIM_KINDS (`node claim-kinds.mjs --render-doc`).
+// Pure; node: builtins only. Korean phrases are \u escapes.
+
+import { isCliEntry } from "./entry-guard.mjs"
+
+export const CLAIM_ATTRIBUTE = "data-claim-kind"
+
+const kind = (entry) => Object.freeze({ ...entry, rowKinds: Object.freeze(entry.rowKinds), sourceKinds: entry.sourceKinds && Object.freeze(entry.sourceKinds), unmarked: entry.unmarked && Object.freeze({ ...entry.unmarked, patterns: Object.freeze(entry.unmarked.patterns) }) })
+
+// Korean pieces as \u escapes: WE_SUBJECT = uri|jeohui as a standalone subject word (+ ga|neun, or " tim-i"),
+// WE_RUN = silhaeng-haet/hayeo/hae | dol-lyeot/lyeo, DIRECT = jikjeop, RUN = silhaeng | dol-li/ryeo/ryeot/rin/ril,
+// SUBJECT = a word ending in the particle ga|i|eun|neun|kkeseo (a third party ran it, not us).
+const KO_WE_SUBJECT = "(?:^|[\\s,.;!?(])(?:\uC6B0\uB9AC|\uC800\uD76C)(?:[\uAC00\uB294]|\\s+\uD300\uC774)\\s+"
+const KO_WE_RUN = "(?:\uC2E4\uD589(?:\uD588|\uD558\uC5EC|\uD574)|\uB3CC[\uB838\uB824])"
+const KO_RUN = "(?:\uC2E4\uD589|\uB3CC[\uB9AC\uB824\uB838\uB9B0\uB9B4])"
+const KO_SUBJECT = "[^\\s,.;!?]*(?:[\uAC00\uC774\uC740\uB294]|\uAED8\uC11C)(?=\\s)"
+
+/**
+ * kind -> { prefix: placeholder prefix (null = the default form), placeholder: an example,
+ * rowKinds: ledger row kinds it accepts, sourceKinds: meta.json kinds the snapshot holding the
+ * cited row's own locator artifact must have (null = any), defect: code when a marked fact cites a
+ * row it refuses, note: the rule's limits, unmarked: { code, rule, patterns } for an unmarked
+ * sentence that reads like the kind (a minor warning only), or null }.
+ */
+export const CLAIM_KINDS = Object.freeze({
+ fact: kind({
+ prefix: null,
+ placeholder: "{{F12:label}}",
+ rowKinds: ["measured", "converted", "derived"],
+ sourceKinds: null,
+ defect: "claim_kind_mismatch",
+ note: "Accepts exactly what an unmarked numeral accepts, so marking fact is never stricter than not marking. Neither accepts an `assumed` row: an unmarked assumption reads as a fact, so it needs its `{{A:F12:label}}` mark (`claim_kind_mismatch` otherwise).",
+ unmarked: null,
+ }),
+ quote: kind({ prefix: null, placeholder: "{{Q7:label}}", rowKinds: ["quote"], sourceKinds: null, defect: "claim_kind_mismatch", note: "", unmarked: null }),
+ verified: kind({
+ prefix: "V",
+ placeholder: "{{V:F12:label}}",
+ rowKinds: ["measured", "quote"],
+ sourceKinds: ["command"],
+ defect: "unsupported_verification",
+ note: "The cited row itself must have a locator artifact inside `sources/<source_id>/` whose `meta.json` kind is `command`; another command row in the sentence never counts. Whether it is the right command for the claim is the meaning verifier's call (minor), not this gate's.",
+ unmarked: {
+ code: "verification_claim_unmarked",
+ rule: "a first-person run: we/I (have) ran, run, executed, verified by (not ran out of); by running after we/our in the sentence; \\uC6B0\\uB9AC or \\uC800\\uD76C as the subject word (+ \\uAC00, \\uB294, or \\uD300\\uC774), then \\uC2E4\\uD589\\uD588, \\uC2E4\\uD589\\uD558\\uC5EC, \\uC2E4\\uD589\\uD574, \\uB3CC\\uB838, or \\uB3CC\\uB824 in the clause with no third-party subject between; \\uC9C1\\uC811 with no third-party subject, then \\uC2E4\\uD589 or \\uB3CC\\uB9AC in the clause",
+ patterns: [
+ /\b(?:we|I)(?:'ve)?\s+(?:(?:have|had|also|then|first|just|again)\s+)*(?:ran|run|re-?ran|re-?run|executed|verified\s+by)\b(?!\s+out\s+of\b)/i,
+ /\b(?:we|our)\b.*?\bby\s+running\b/i,
+ new RegExp(`${KO_WE_SUBJECT}(?:(?!${KO_SUBJECT})[^\\s,.;!?]+\\s+)*?${KO_WE_RUN}`),
+ new RegExp(`(?:^|[,.;!?])\\s*(?:(?!${KO_SUBJECT})[^\\s,.;!?]+\\s+)*\uC9C1\uC811\\s*(?:[^\\s,.;!?]+\\s*)??${KO_RUN}`),
+ ],
+ },
+ }),
+ derived: kind({ prefix: "D", placeholder: "{{D:F12:label}}", rowKinds: ["derived", "converted"], sourceKinds: null, defect: "claim_kind_mismatch", note: "", unmarked: null }),
+ assumed: kind({
+ prefix: "A",
+ placeholder: "{{A:F12:label}}",
+ rowKinds: ["assumed"],
+ sourceKinds: null,
+ defect: "claim_kind_mismatch",
+ note: "Numeric assumptions only (an assumed ledger row carries a decimal value); a qualitative assumption stays prose.",
+ unmarked: null,
+ }),
+})
+
+/** Whether a row satisfies a claim kind, given the meta.json kind of the snapshot holding its own locator artifact. */
+export function claimSatisfied(name, row, sourceKind) {
+ const entry = CLAIM_KINDS[name]
+ return Boolean(entry && row && entry.rowKinds.includes(row.kind) && (entry.sourceKinds === null || entry.sourceKinds.includes(sourceKind)))
+}
+
+/** The kind an unmarked element citing `row` must be marked with (an assumed row needs `assumed`), or null when unmarked is fine. */
+export function unmarkedRequires(row) {
+ if (!row || ["fact", "quote"].some((name) => CLAIM_KINDS[name].rowKinds.includes(row.kind))) return null
+ return Object.keys(CLAIM_KINDS).find((name) => CLAIM_KINDS[name].rowKinds.includes(row.kind)) ?? null
+}
+
+/** The kinds whose unmarked patterns match the sentence. */
+export function unmarkedKinds(text) {
+ return Object.entries(CLAIM_KINDS)
+ .filter(([, entry]) => entry.unmarked?.patterns.some((re) => re.test(text)))
+ .map(([name]) => name)
+}
+
+export const CLAIM_TABLE_BEGIN = "<!-- claim-kinds:begin (rendered from CLAIM_KINDS; do not edit by hand) -->"
+export const CLAIM_TABLE_END = "<!-- claim-kinds:end -->"
+
+const code = (text) => `\`${text}\``
+
+/** The Markdown table of references/fidelity-claim-kinds.md. */
+export function renderClaimKindTable() {
+ const head = ["| Kind | Placeholder | Attribute fill emits | Accepted row kinds | Required source kind | Defect when unsatisfied | Unmarked warning | Notes |", "|---|---|---|---|---|---|---|---|"]
+ const lines = Object.entries(CLAIM_KINDS).map(([name, entry]) => {
+ const attribute = entry.prefix === null ? `none (or ${code(`${CLAIM_ATTRIBUTE}="${name}"`)})` : code(`${CLAIM_ATTRIBUTE}="${name}"`)
+ const source = entry.sourceKinds === null ? "any" : entry.sourceKinds.map(code).join(", ")
+ const warn = entry.unmarked ? `${code(entry.unmarked.code)} on ${entry.unmarked.rule}` : "none"
+ return `| ${code(name)} | ${code(entry.placeholder)} | ${attribute} | ${entry.rowKinds.map(code).join(", ")} | ${source} | ${code(entry.defect)} | ${warn} | ${entry.note || "-"} |`
+ })
+ return `${[...head, ...lines].join("\n")}\n`
+}
+
+if (isCliEntry(import.meta.url)) {
+ if (process.argv[2] === "--render-doc" && process.argv.length === 3) process.stdout.write(renderClaimKindTable())
+ else {
+ process.stderr.write("usage: node claim-kinds.mjs --render-doc\n")
+ process.exitCode = 2
+ }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/cli-support.mjs b/plugins/omo/skills/ulw-research/scripts/cli-support.mjs
index 1fec8f53..13902f02 100644
--- a/plugins/omo/skills/ulw-research/scripts/cli-support.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/cli-support.mjs
@@ -1,7 +1,8 @@
// cli-support.mjs - argument parsing, typed usage errors, exit-code mapping, and JSON file IO
// shared by every report-tools subcommand. node: builtins only.
-import { readFileSync, renameSync, writeFileSync } from "node:fs"
+import { createHash } from "node:crypto"
+import { readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"
import { basename, dirname, join } from "node:path"
/** A usage or input error: always exit code 2. */
@@ -87,11 +88,24 @@ export function readJsonFile(path) {
}
}
-/** Write JSON through a sibling temp file + rename, so a crash never leaves a half-written file. */
-export function writeJsonAtomic(path, value) {
+/** The sha256 hex digest of a string or bytes: the one hash every record and receipt carries. */
+export const sha256Of = (content) => createHash("sha256").update(content).digest("hex")
+
+/** Write text through a sibling temp file + rename; a failed write removes the temp file. */
+export function writeTextAtomic(path, text) {
const temp = join(dirname(path), `.${basename(path)}.${process.pid}.${Date.now()}.tmp`)
- writeFileSync(temp, `${JSON.stringify(value, null, 2)}\n`)
- renameSync(temp, path)
+ try {
+ writeFileSync(temp, text)
+ renameSync(temp, path)
+ } catch (error) {
+ rmSync(temp, { force: true })
+ throw error
+ }
+}
+
+/** Write JSON through writeTextAtomic, so a crash never leaves a half-written file. */
+export function writeJsonAtomic(path, value) {
+ writeTextAtomic(path, `${JSON.stringify(value, null, 2)}\n`)
}
/** Print a result: JSON on stdout, one summary line on stderr. */
diff --git a/plugins/omo/skills/ulw-research/scripts/contracts.mjs b/plugins/omo/skills/ulw-research/scripts/contracts.mjs
index 09b0ec01..27cbe39d 100644
--- a/plugins/omo/skills/ulw-research/scripts/contracts.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/contracts.mjs
@@ -6,6 +6,16 @@
export const SCHEMA_VERSION = 1
+/** The severity vocabulary in rank order; every DEFECT_CODES entry uses one of these. */
+export const SEVERITIES = Object.freeze(["blocker", "major", "minor"])
+
+/** Defect counts per severity, then total. */
+export function countDefects(defects) {
+ const counts = Object.fromEntries(SEVERITIES.map((severity) => [severity, 0]))
+ for (const defect of defects) counts[defect.severity] += 1
+ return { ...counts, total: defects.length }
+}
+
function deepFreeze(value) {
if (value === null || typeof value !== "object") return value
for (const key of Object.keys(value)) deepFreeze(value[key])
@@ -48,7 +58,7 @@ export const DEFECT_CODES = deepFreeze({
pointer_label_mismatch: { severity: "blocker", integrity: true, hint: "The placeholder's label is not the row's label; check the id, then the label." },
quote_anchor_unresolved: { severity: "blocker", integrity: true, hint: "A from or to anchor is missing or occurs more than once in the quote; pick unique anchor words." },
unit_duplicated: { severity: "blocker", integrity: true, hint: "The unit after the placeholder repeats the row's unit; use :value or remove the unit." },
- polish_diff_fail: { severity: "blocker", integrity: true, hint: "The polish pass changed numbers, tickers, or table rows; revert to the pre-polish artifact and apply the language fixes only." },
+ polish_diff_fail: { severity: "blocker", integrity: true, hint: "The polish pass changed numbers, tickers, table rows, claim ids, markup, or hidden content; revert to the pre-polish artifact and apply the language fixes only." },
fidelity_interview_incomplete: { severity: "blocker", integrity: true, hint: "A fidelity field is pending or missing in brief.md; answer the open question or record its default, then rerun." },
derived_not_allowed: { severity: "blocker", integrity: true, hint: "The brief allows no derived or assumed numbers; cite a source row directly or change the brief with the requester." },
source_outside_floor: { severity: "blocker", integrity: true, hint: "The row's source is outside the source floor the brief records; use a source the brief allows or remove the fact." },
@@ -57,7 +67,8 @@ export const DEFECT_CODES = deepFreeze({
quote_unbound: { severity: "blocker", integrity: true, hint: "A direct quotation has no ledger quote row; register it with fact quote and insert it through its placeholder." },
edition_unverified: { severity: "blocker", integrity: true, hint: "The delivered file has no matching fact verify record; run fact verify on that edition and record it with --verified-by." },
untraceable_number: { severity: "major", integrity: false, hint: "Replace the typed number with a {{F<n>}} placeholder backed by a ledger row, or register it as assumed with a rationale." },
- unsupported_verification: { severity: "major", integrity: false, hint: "The sentence claims a tool check that no recorded command output backs; link the recorded output row or remove the claim." },
+ unsupported_verification: { severity: "major", integrity: false, hint: "A sentence marked verified (data-claim-kind=\"verified\") cites a row that is not recorded command output; cite the recorded output row or drop the verified mark." },
+ claim_kind_mismatch: { severity: "major", integrity: false, hint: "An element marked with a claim kind (data-claim-kind) cites a row that kind does not accept, or the kind is unknown; cite a row of that kind or fix the mark (references/fidelity-claim-kinds.md)." },
conflicting_readings: { severity: "major", integrity: false, hint: "Two ledger rows with different periods or qualifiers are cited as one fact; split the sentence or retire one row." },
hedge_dropped: { severity: "major", integrity: false, hint: "Restore the qualifier the source uses (estimated, about, not yet)." },
direction_flipped: { severity: "major", integrity: false, hint: "The source states the opposite direction; rewrite from the quote." },
@@ -65,7 +76,8 @@ export const DEFECT_CODES = deepFreeze({
qualifier_dropped: { severity: "major", integrity: false, hint: "The source bounds this value (up to, at least, about); restore the qualifier or use :q." },
unbound_sentence: { severity: "major", integrity: false, hint: "Bind the sentence to a fact, a quote, a citation, or a claim-graph node with data-claim." },
meaning_drift_confirmed: { severity: "major", integrity: false, hint: "Two independent readers found the same drift; rewrite from the quote or dispute with a reason." },
- polish_edit_rejected: { severity: "major", integrity: false, hint: "The edit changed a number, a hedge, a qualifier, a link, or a claim id; re-propose the language fix only." },
+ meaning_unverified: { severity: "major", integrity: false, hint: "No meaning reader judged this pair, or no strong reader judged a force_strong pair; run the missing reader on pairs.json and rerun fact meaning-check." },
+ polish_edit_rejected: { severity: "major", integrity: false, hint: "The edit changed a number, a hedge, a qualifier, a link, or a claim id, or contained <, > or &; re-propose the language fix only, narrowed to the words around any entity or tag." },
polish_hedging_decreased: { severity: "major", integrity: false, hint: "The polish pass removed qualifiers; restore them." },
polish_text_churn: { severity: "major", integrity: false, hint: "More than half the text changed during polish; the pass composed instead of proofreading." },
snapshot_mismatch: { severity: "major", integrity: false, hint: "The operands come from different collection windows; use rows from one window or state the windows separately." },
@@ -73,6 +85,7 @@ export const DEFECT_CODES = deepFreeze({
quote_stale_version: { severity: "major", integrity: false, hint: "The quoted source revision differs from the revision the report targets; re-snapshot the target revision or date the quote." },
quote_fuzzy_match: { severity: "minor", integrity: false, hint: "The source text is OCR-noisy; confirm the snapshot and prefer the raw document." },
name_outside_quote: { severity: "minor", integrity: false, hint: "A name in the sentence is not in the cited quote; add the source or remove the name." },
+ verification_claim_unmarked: { severity: "minor", integrity: false, hint: "The sentence reads as a check we ran but carries no verified mark; mark it {{V:F<n>:label}} on a recorded command-output row, or reword it." },
scope_widened_warn: { severity: "minor", integrity: false, hint: "The sentence uses a broader scope word than the quote; confirm the scope or narrow it." },
particle_unverified: { severity: "minor", integrity: false, hint: "The particle after this value could not be checked; read it aloud and fix it by hand if wrong." },
fallback_without_entity: { severity: "minor", integrity: false, hint: "The replacement quote has no entity for attribution; set one on the row." },
@@ -87,10 +100,10 @@ export const FIDELITY_CODES = Object.freeze([
"unresolved_placeholder", "value_mismatch", "unit_mismatch", "quote_not_verbatim", "claim_status_invalid",
"incompatible_operands", "pointer_label_mismatch", "quote_anchor_unresolved", "unit_duplicated", "polish_diff_fail",
"fidelity_interview_incomplete", "derived_not_allowed", "source_outside_floor", "snapshot_hash_mismatch",
- "chart_value_untraceable", "quote_unbound", "edition_unverified", "untraceable_number", "unsupported_verification",
+ "chart_value_untraceable", "quote_unbound", "edition_unverified", "untraceable_number", "unsupported_verification", "claim_kind_mismatch",
"conflicting_readings", "hedge_dropped", "direction_flipped", "causality_added", "qualifier_dropped", "unbound_sentence",
- "meaning_drift_confirmed", "polish_edit_rejected", "polish_hedging_decreased", "polish_text_churn", "snapshot_mismatch",
- "join_integrity_fail", "quote_stale_version", "quote_fuzzy_match", "name_outside_quote", "scope_widened_warn",
+ "meaning_drift_confirmed", "meaning_unverified", "polish_edit_rejected", "polish_hedging_decreased", "polish_text_churn", "snapshot_mismatch",
+ "join_integrity_fail", "quote_stale_version", "quote_fuzzy_match", "name_outside_quote", "verification_claim_unmarked", "scope_widened_warn",
"particle_unverified", "fallback_without_entity", "meaning_drift_warn", "polish_edit_unlocated", "polish_link_changed",
"polish_name_changed",
])
@@ -184,6 +197,14 @@ export function validateOutcomeManifest(manifest) {
if (isObject(manifest.gates)) {
for (const gate of GATE_NAMES) check(errors, GATE_STATUSES.includes(manifest.gates[gate]), `gates.${gate} must be one of ${GATE_STATUSES.join(", ")}`)
} else errors.push("gates must be an object")
+ if (manifest.gateHistory !== undefined) {
+ if (!Array.isArray(manifest.gateHistory)) errors.push("gateHistory must be an array")
+ else for (const entry of manifest.gateHistory) {
+ check(errors, isObject(entry) && GATE_NAMES.includes(entry.gate) && GATE_STATUSES.includes(entry.status)
+ && typeof entry.at === "string" && ISO_DATE.test(entry.at)
+ && (entry.reason === undefined || typeof entry.reason === "string"), "gateHistory entries must have a known gate, status, timestamp, and optional reason")
+ }
+ }
if (Array.isArray(manifest.residualDefects)) manifest.residualDefects.forEach((defect, index) => validateDefect(defect, `residualDefects[${index}]`, errors))
else errors.push("residualDefects must be an array")
check(errors, manifest.repair === null || isObject(manifest.repair), "repair must be null or an object")
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-commands-meaning.mjs b/plugins/omo/skills/ulw-research/scripts/fact-commands-meaning.mjs
new file mode 100644
index 00000000..ba03f979
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-commands-meaning.mjs
@@ -0,0 +1,191 @@
+// fact-commands-meaning.mjs - `fact pairs` and `fact meaning-check`, the script half of the
+// separate-session meaning verifier. fact pairs writes the (source, report sentence) pairs a
+// reader lane gets and nothing else; it honours the brief's `meaning check` field and writes
+// `{ skipped: true }` when it is off. fact meaning-check ingests the readers' verdict files
+// (meaning-check.mjs), writes fidelity-meaning-defects.json (a fidelity record bound to the pairs'
+// artifact_sha256 and report, through writeFidelityRecord), and records the run in
+// fidelity-calibration.jsonl: a header with the promotion criterion on first write, then per run
+// one run line plus one line per flagged pair. The run id hashes the inputs only, so a rerun or a
+// --dispute rerun of the same inputs replaces that run's lines in place (disputes carry forward)
+// instead of counting a second run. Neither command calls a model or the network.
+
+import { existsSync, readFileSync } from "node:fs"
+import { dirname, join, resolve } from "node:path"
+
+import { CliError, parseArgs, readJsonFile, sha256Of, writeJsonAtomic, writeTextAtomic } from "./cli-support.mjs"
+import { countDefects } from "./contracts.mjs"
+import { markdownAsHtml, readEdition } from "./fact-commands-verify.mjs"
+import { readLedger } from "./fact-ledger.mjs"
+import { validateLedger } from "./fact-ledger-schema.mjs"
+import { extractPairs, PAIR_KINDS } from "./fact-pairs.mjs"
+import { FIDELITY_INCOMPLETE, meaningCheckEnabled, requireLockedBrief } from "./fidelity-brief.mjs"
+import { MEANING_RECORD, writeFidelityRecord } from "./fidelity-records.mjs"
+import { blocksDelivery } from "./gates-fidelity.mjs"
+import { ingestVerdicts, unverifiedDefects, VERDICT_SCHEMA } from "./meaning-check.mjs"
+
+export const PAIRS_FILE = "pairs.json"
+export const CALIBRATION_FILE = "fidelity-calibration.jsonl"
+/** Starting values, recorded in the calibration log header; changed only with evidence. */
+export const PROMOTION = Object.freeze({ code: "meaning_drift_confirmed", to: "integrity", min_runs: 20, max_disputed_to_confirmed: 0.1, false_block_window: 20, max_report_false_blocks_in_window: 2 })
+
+/** The command status: fail when any defect blocks delivery under the registry policy. */
+export const meaningStatus = (defects) => (defects.some(blocksDelivery) ? "fail" : "pass")
+
+const sessionOf = (flags) => {
+ const dir = flags["session-dir"] ?? process.env.SESSION_DIR
+ return dir && dir !== true ? resolve(dir) : null
+}
+const inSession = (session, name, flag) => {
+ if (session === null) throw new CliError(`${flag} is required without --session-dir (or SESSION_DIR)`)
+ return join(session, name)
+}
+
+function readLedgerFrom(flags, session) {
+ if (flags.ledger === undefined) {
+ if (session === null) throw new CliError("--ledger is required without --session-dir (or SESSION_DIR)")
+ return readLedger(session)
+ }
+ const ledger = readJsonFile(flags.ledger)
+ const errors = validateLedger(ledger)
+ if (errors.length > 0) throw new CliError(`${flags.ledger} is not a valid fact ledger: ${errors[0]}`)
+ return ledger
+}
+
+function briefState(flags, session) {
+ const path = flags.brief ?? inSession(session, "brief.md", "--brief")
+ const lock = requireLockedBrief(path)
+ if (lock.exitCode === 2) throw new CliError(`${path}: malformed ## Fidelity block: ${lock.errors.join("; ")}`)
+ return { path, locked: lock.locked, off: !meaningCheckEnabled(lock.parsed) }
+}
+
+function runPairs(args) {
+ const [report] = args.positionals
+ const { flags } = args
+ if (!report) throw new CliError("usage: fact pairs <report> [--format html|md] [--ledger F] [--claims claims.json] [--changed-since <fill-map.json>] [--brief brief.md] [--out pairs.json]")
+ const session = sessionOf(flags)
+ const out = resolve(flags.out ?? inSession(session, PAIRS_FILE, "--out"))
+ const brief = briefState(flags, session)
+ if (!brief.locked) {
+ const json = { command: "fact pairs", status: "fail", exitCode: 1, error: "fidelity_interview_incomplete", message: `${brief.path} has no locked ## Fidelity block` }
+ return { json, summary: `fact pairs: ${json.message}`, exitCode: 1 }
+ }
+ if (brief.off) {
+ const record = { schemaVersion: 1, skipped: true, reason: "meaning check: off", pairs: [] }
+ writeJsonAtomic(out, record)
+ return { json: { command: "fact pairs", status: "skipped", exitCode: 0, data: { out, ...record } }, summary: "fact pairs: skipped (meaning check: off)", exitCode: 0 }
+ }
+ const edition = readEdition(report, flags)
+ const claimsPath = flags.claims ?? (session === null ? null : join(session, "claims.json"))
+ const claims = claimsPath !== null && (flags.claims !== undefined || existsSync(claimsPath)) ? readJsonFile(claimsPath) : null
+ const changed = flags["changed-since"] === undefined ? undefined : readJsonFile(flags["changed-since"]).entries
+ const ledger = readLedgerFrom(flags, session)
+ const html = edition.format === "md" ? markdownAsHtml(edition.text, edition.entries, ledger).html : edition.text
+ const pairs = extractPairs(html, ledger, claims, changed)
+ const counts = { total: pairs.length, force_strong: pairs.filter((pair) => pair.force_strong).length }
+ for (const kind of PAIR_KINDS) counts[kind] = pairs.filter((pair) => pair.kind === kind).length
+ const record = { schemaVersion: 1, skipped: false, report: resolve(report), format: edition.format, artifact_sha256: sha256Of(edition.text), pairs_sha256: sha256Of(JSON.stringify(pairs)), counts, verdict_schema: VERDICT_SCHEMA, pairs }
+ writeJsonAtomic(out, record)
+ const summary = `fact pairs: ${counts.total} pair(s), ${counts.force_strong} routed to the strong reader`
+ return { json: { command: "fact pairs", status: "pass", exitCode: 0, data: { out, ...record, pairs: undefined } }, summary, exitCode: 0 }
+}
+
+/** Every line of the log after the header, in file order; a corrupt line is a usage error naming path:line. */
+function calibrationRows(path) {
+ if (!existsSync(path)) return []
+ const rows = []
+ for (const [index, line] of readFileSync(path, "utf8").split("\n").entries()) {
+ if (line.trim() === "") continue
+ try {
+ rows.push(JSON.parse(line))
+ } catch (error) {
+ throw new CliError(`${path}:${index + 1}: corrupt calibration log line (${error.message}); repair or move the file`)
+ }
+ }
+ return rows.filter((row) => row.header !== true)
+}
+
+/** The promotion criterion evaluated over the log: runs, disputed-to-confirmed, report-level false blocks. */
+export function promotionEligibility(rows) {
+ const runs = [...new Set(rows.map((row) => row.run_id))]
+ const confirmed = rows.filter((row) => row.run !== true && row.confirmed)
+ const disputed = confirmed.filter((row) => row.disputed).length
+ const ratio = confirmed.length === 0 ? null : disputed / confirmed.length
+ const falseBlocks = runs.slice(-PROMOTION.false_block_window).filter((run) => {
+ const blocking = confirmed.filter((row) => row.run_id === run)
+ return blocking.length > 0 && blocking.every((row) => row.disputed)
+ }).length
+ const eligible = runs.length >= PROMOTION.min_runs && ratio !== null && ratio < PROMOTION.max_disputed_to_confirmed && falseBlocks <= PROMOTION.max_report_false_blocks_in_window
+ return { runs: runs.length, confirmed: confirmed.length, disputed_confirmed: disputed, disputed_to_confirmed: ratio, report_false_blocks_in_window: falseBlocks, eligible }
+}
+
+/**
+ * Writes this run's lines: one run line (so a run with no flagged pair still counts) and one line per
+ * flagged pair. A run id already in the log is replaced in place, keeping its position in the false
+ * block window and any earlier dispute on a pair the new lines do not dispute.
+ */
+function recordCalibration(path, lines, runId, pairsSha) {
+ const before = calibrationRows(path)
+ const previous = before.filter((row) => row.run_id === runId)
+ const disputes = new Map(previous.filter((row) => row.disputed).map((row) => [row.pair_id, row.reason]))
+ const pairRows = lines.map((line) => {
+ const row = { ...JSON.parse(line), run_id: runId, pairs_sha256: pairsSha }
+ return !row.disputed && disputes.has(row.pair_id) ? { ...row, disputed: true, reason: disputes.get(row.pair_id) } : row
+ })
+ const rows = [{ run: true, run_id: runId, pairs_sha256: pairsSha, flagged: pairRows.length }, ...pairRows]
+ const at = previous.length === 0 ? before.length : before.indexOf(previous[0])
+ const after = [...before.slice(0, at).filter((row) => row.run_id !== runId), ...rows, ...before.slice(at).filter((row) => row.run_id !== runId)]
+ const header = { header: true, schemaVersion: 1, promotion: PROMOTION, note: "starting values; change them only with evidence" }
+ writeTextAtomic(path, `${[header, ...after].map((row) => JSON.stringify(row)).join("\n")}\n`)
+ return { written: pairRows.length, replaced: previous.length > 0, rows: after }
+}
+
+/** A `{ skipped: true }` pairs file is accepted only when the locked brief turns the meaning check off. */
+function skippedRefusal(flags, session) {
+ const brief = briefState(flags, session)
+ if (!brief.locked) return { error: FIDELITY_INCOMPLETE, message: `${brief.path} has no locked ## Fidelity block, so a skipped meaning check cannot be accepted` }
+ if (!brief.off) return { error: "meaning_check_required", message: `${flags.pairs} says skipped but ${brief.path} has meaning check on; rerun fact pairs` }
+ return null
+}
+
+function runMeaningCheck(args) {
+ const { flags } = args
+ const usage = "usage: fact meaning-check --pairs pairs.json --verdicts cheap.json [--verdicts strong.json] [--dispute P<n> --reason TEXT] [--run-id ID] [--calibration F] [--out F]"
+ if (flags.pairs === undefined || !Array.isArray(flags.verdicts)) throw new CliError(usage)
+ const session = sessionOf(flags)
+ const pairsText = readFileSync(flags.pairs, "utf8")
+ const pairsDoc = readJsonFile(flags.pairs)
+ if (pairsDoc?.skipped === true) {
+ const refusal = skippedRefusal(flags, session)
+ if (refusal !== null) return { json: { command: "fact meaning-check", status: "fail", exitCode: 1, ...refusal }, summary: `fact meaning-check: ${refusal.message}`, exitCode: 1 }
+ return { json: { command: "fact meaning-check", status: "skipped", exitCode: 0, defects: [] }, summary: JSON.stringify({ command: "fact meaning-check", status: "skipped" }), exitCode: 0 }
+ }
+ if (typeof pairsDoc?.artifact_sha256 !== "string" || typeof pairsDoc.report !== "string") throw new CliError(`${flags.pairs} names no artifact_sha256 and report, so the meaning record cannot be bound to an edition; rerun fact pairs`)
+ for (const file of flags.verdicts) readJsonFile(file)
+ const verdictTexts = flags.verdicts.map((file) => readFileSync(file, "utf8"))
+ const dispute = flags.dispute === undefined ? undefined : { pair_id: flags.dispute, reason: args.required("reason") }
+ let result
+ try {
+ result = ingestVerdicts(pairsDoc, flags.verdicts, dispute ? { dispute } : {})
+ } catch (error) {
+ throw new CliError(`invalid verdict input: ${error.message}`)
+ }
+ const out = resolve(flags.out ?? inSession(session, MEANING_RECORD, "--out"))
+ const calibration = resolve(flags.calibration ?? join(session ?? dirname(out), CALIBRATION_FILE))
+ const runId = flags["run-id"] ?? sha256Of([pairsText, ...verdictTexts].join("\0")).slice(0, 16)
+ const pairsSha = pairsDoc.pairs_sha256 ?? sha256Of(pairsText)
+ const log = recordCalibration(calibration, result.calibrationLines, runId, pairsSha)
+ const defects = [...result.defects, ...unverifiedDefects(result.unverified)]
+ const counts = countDefects(defects)
+ const status = meaningStatus(defects)
+ const exitCode = status === "fail" ? 1 : 0
+ const record = writeFidelityRecord(out, "meaning", { command: "fact meaning-check", status, exitCode, artifact_sha256: pairsDoc.artifact_sha256, report: pairsDoc.report, run_id: runId, pairs_sha256: pairsSha, defects, counts, dropped: result.dropped, unverified: result.unverified, calibration_path: calibration, calibration_written: log.written, calibration_replaced: log.replaced, promotion_eligibility: promotionEligibility(log.rows) })
+ const confirmed = result.defects.filter((defect) => defect.code === "meaning_drift_confirmed").length
+ const summary = JSON.stringify({ command: "fact meaning-check", status, defects: defects.length, confirmed, warn: result.defects.length - confirmed, dropped: result.dropped.length, unverified: result.unverified.length })
+ return { json: { ...record, out }, summary, exitCode }
+}
+
+/** @param {string[]} argv the arguments after `fact pairs` or `fact meaning-check` */
+export function runFactMeaning(sub, argv) {
+ const args = parseArgs(argv, { repeatable: ["verdicts"] })
+ return sub === "pairs" ? runPairs(args) : runMeaningCheck(args)
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-commands-verify.mjs b/plugins/omo/skills/ulw-research/scripts/fact-commands-verify.mjs
new file mode 100644
index 00000000..ce269796
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-commands-verify.mjs
@@ -0,0 +1,164 @@
+// fact-commands-verify.mjs - `fact verify` and the Markdown projection it checks.
+//
+// fact verify runs the deterministic fidelity gate over one filled HTML or Markdown edition and
+// writes a fidelity record (default $SESSION_DIR/fidelity-defects.json, through
+// writeFidelityRecord) carrying the artifact's sha256. It
+// exits 1 on any blocker or major defect (the orchestrator then records `outcome gate fidelity
+// fail`); minors alone exit 0 and are listed. A Markdown edition is read through md-lite into an
+// escaped HTML projection (same line numbers) and checked with its fill map, which is the only
+// record of where its values sit; LaTeX is verified through its Markdown source.
+//
+// The delivery barrier that reads these records (and every other fidelity record) is
+// fidelity-records.mjs.
+
+import { existsSync, readFileSync } from "node:fs"
+import { dirname, join, resolve } from "node:path"
+
+import { CliError, readJsonFile, sha256Of } from "./cli-support.mjs"
+import { countDefects } from "./contracts.mjs"
+import { recompute } from "./fact-derive.mjs"
+import { readLedger } from "./fact-ledger.mjs"
+import { FIDELITY_RECORD, writeFidelityRecord } from "./fidelity-records.mjs"
+import { blocksDelivery, checkFidelity } from "./gates-fidelity.mjs"
+import { parseMarkdown } from "./md-lite.mjs"
+import { escapeHtml, parseRenderedQuote, renderQuote, renderValueHtml } from "./placeholder.mjs"
+import { sessionDirOf } from "./source-commands.mjs"
+
+function readText(path) {
+ try {
+ return readFileSync(path, "utf8")
+ } catch (error) {
+ throw new CliError(`cannot read ${path}: ${error.code ?? error.message}`)
+ }
+}
+
+/**
+ * md-lite block kind -> its projection: the element tag (null = h<level>) and, for a run of one kind,
+ * the container that wraps the run. Code and table rows also take the specialized bodies below.
+ */
+export const MD_HTML_BLOCK_RULES = Object.freeze({
+ heading: Object.freeze({ tag: null }),
+ paragraph: Object.freeze({ tag: "p" }),
+ list_item: Object.freeze({ tag: "li", group: Object.freeze(["<ul>", "</ul>"]) }),
+ blockquote: Object.freeze({ tag: "blockquote" }),
+ code: Object.freeze({ tag: "code" }),
+ table_row: Object.freeze({ tag: "tr", group: Object.freeze(["<table>", "</table>"]) }),
+})
+
+/** Escaped text with inline links and code spans kept as elements, so a URL is never read as prose. */
+export const projectMarkdownInline = (text) =>
+ escapeHtml(text)
+ .replace(/\[([^\]]+)\]\(([^)\s]+)\)/gu, '<a href="$2">$1</a>')
+ .replace(/`([^`]+)`/gu, "<code>$1</code>")
+
+/** The element the HTML fill writes for one fill-map entry, or null when its bytes were edited since. */
+function entryHtml(text, entry, rows) {
+ const body = text.slice(entry.start, entry.end)
+ if (body !== entry.rendered) return null
+ const row = rows.get(entry.fact_id)
+ if (entry.translates) return `<span data-translates="${escapeHtml(entry.translates)}">${projectMarkdownInline(body.replace(/^<!--[\s\S]*?-->\s*/u, ""))}</span>`
+ if (!row) return `<data data-fact="${escapeHtml(entry.fact_id)}">${escapeHtml(body)}</data>`
+ const quoted = row.kind === "quote" ? parseRenderedQuote(body) : null
+ if (quoted) return renderQuote(row, entry.selector ?? "full", quoted.text, "html")
+ return row.kind === "quote" ? null : renderValueHtml(row, body)
+}
+
+/** [from, to) of the filled text as HTML, each intact fill-map entry rebuilt as its fill element. */
+function spanHtml(text, from, to, entries, rows, stale) {
+ let html = ""
+ let cursor = from
+ for (const entry of entries) {
+ if (entry.start < cursor || entry.end > to || entry.start < from || entry.replaced !== undefined) continue
+ const element = entryHtml(text, entry, rows)
+ if (element === null) continue
+ stale.delete(entry)
+ html += projectMarkdownInline(text.slice(cursor, entry.start)) + element
+ cursor = entry.end
+ }
+ return html + projectMarkdownInline(text.slice(cursor, to))
+}
+
+/**
+ * An escaped HTML projection of a filled Markdown edition: one element per md-lite block, each on
+ * the source line it starts on, lists and table rows grouped. Never a rendering for readers.
+ * @param {string} text the filled Markdown
+ * @param {{ fact_id: string, start: number, end: number, rendered: string, replaced?: string, translates?: string }[]} entries its fill map
+ * @param {{ rows: object[] }} ledger
+ * @returns {{ html: string, stale: object[] }} stale: entries whose bytes changed after fill (checked by text)
+ */
+export function markdownAsHtml(text, entries, ledger) {
+ const sorted = [...(entries ?? [])].sort((a, b) => a.start - b.start)
+ const rows = new Map((ledger?.rows ?? []).map((row) => [row.fact_id, row]))
+ const stale = new Set(sorted.filter((entry) => entry.replaced === undefined))
+ const lineAt = (offset) => text.slice(0, offset).split("\n").length
+ const parsed = parseMarkdown(text)
+ if (parsed.problems.length > 0) {
+ throw new CliError(`the Markdown edition is malformed: ${parsed.problems.map((problem) => `${problem.code} at line ${problem.line}`).join(", ")}; fix it before verifying`)
+ }
+ let html = "<!doctype html><html><head><title>md</title></head><body>"
+ let line = 1
+ let open = null
+ for (const block of parsed.blocks) {
+ const rule = MD_HTML_BLOCK_RULES[block.kind]
+ if (rule === undefined) throw new CliError(`md-lite block kind ${block.kind} has no projection rule`)
+ if (open && open !== block.kind) html += MD_HTML_BLOCK_RULES[open].group[1]
+ const target = lineAt(block.start)
+ if (target > line) html += "\n".repeat(target - line)
+ line = Math.max(line, target) + (text.slice(block.start, block.end).match(/\n/gu)?.length ?? 0)
+ if (rule.group && open !== block.kind) html += rule.group[0]
+ open = rule.group ? block.kind : null
+ const inner = (from, to) => spanHtml(text, from, to, sorted, rows, stale)
+ if (block.kind === "table_row") html += `<${rule.tag}>${block.cells.map((cell) => `<td>${inner(cell.start, cell.end)}</td>`).join("")}</${rule.tag}>`
+ else if (block.kind === "code") html += `<pre><${rule.tag}>${escapeHtml(block.text)}</${rule.tag}></pre>`
+ else {
+ const tag = rule.tag ?? `h${block.level}`
+ const replaced = block.kind === "blockquote" ? sorted.find((entry) => entry.replaced !== undefined && entry.start <= block.start && entry.end >= block.end) : undefined
+ const attrs = replaced ? ` data-fact="${escapeHtml(replaced.fact_id)}" data-replaced="${escapeHtml(replaced.replaced)}"` : ""
+ html += `<${tag}${attrs}>${inner(block.start, block.end)}</${tag}>`
+ }
+ }
+ return { html: `${html}${open ? MD_HTML_BLOCK_RULES[open].group[1] : ""}</body></html>\n`, stale: [...stale] }
+}
+
+const MARKDOWN_EXTENSION = /\.(md|markdown)$/iu
+/** The edition format: the explicit flag, else md for a .md or .markdown path (any case), else html. */
+export const editionFormatOf = (path, flag) => flag ?? (MARKDOWN_EXTENSION.test(path) ? "md" : "html")
+
+/** The report's format (from --format or its extension) and, for Markdown, its fill map entries. */
+export function readEdition(report, flags) {
+ const format = editionFormatOf(report, flags.format)
+ if (format !== "html" && format !== "md") throw new CliError(`--format ${format}: verify html or md (a LaTeX edition is verified through its Markdown source)`)
+ const text = readText(report)
+ if (format === "html") return { format, text, entries: flags["fill-map"] === undefined ? null : readJsonFile(flags["fill-map"]).entries }
+ const map = flags["fill-map"] ?? `${report}.fill-map.json`
+ if (!existsSync(map)) throw new CliError(`a Markdown edition needs its fill map: ${map} is missing (pass --fill-map)`)
+ return { format, text, entries: readJsonFile(map).entries }
+}
+
+/** @param {{ positionals: string[], flags: Record<string, any> }} args parsed by runFact */
+export function runFactVerify(args) {
+ const [report] = args.positionals
+ const { flags } = args
+ if (!report) throw new CliError("usage: fact verify <report> [--format html|md] [--sources D] [--claims claims.json] [--fill-map F] [--brief brief.md] [--out fidelity-defects.json]")
+ const edition = readEdition(report, flags)
+ const sessionDir = sessionDirOf(flags)
+ const claimsPath = flags.claims ?? join(sessionDir, "claims.json")
+ const briefPath = flags.brief ?? join(sessionDir, "brief.md")
+ const ledger = readLedger(sessionDir)
+ const projected = edition.format === "md" ? markdownAsHtml(edition.text, edition.entries, ledger) : null
+ const result = checkFidelity(projected?.html ?? edition.text, {
+ ledger,
+ sourcesDir: resolve(flags.sources ?? join(sessionDir, "sources")),
+ claims: flags.claims !== undefined || existsSync(claimsPath) ? readJsonFile(claimsPath) : null,
+ fillMap: projected?.stale ?? edition.entries,
+ brief: existsSync(briefPath) ? readText(briefPath) : "",
+ baseDir: dirname(resolve(report)),
+ recompute,
+ })
+ const counts = countDefects(result.defects)
+ const status = result.defects.some(blocksDelivery) ? "fail" : "pass"
+ const exitCode = status === "fail" ? 1 : 0
+ const out = resolve(flags.out ?? join(sessionDir, FIDELITY_RECORD))
+ const record = writeFidelityRecord(out, "fidelity", { command: "fact verify", status, exitCode, format: edition.format, report: resolve(report), artifact_sha256: sha256Of(edition.text), defects: result.defects, counts, gatesRun: result.gatesRun })
+ return { json: { ...record, out }, summary: `fact verify: ${status.toUpperCase()} - ${counts.blocker} blocker, ${counts.major} major, ${counts.minor} minor`, exitCode }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-commands.mjs b/plugins/omo/skills/ulw-research/scripts/fact-commands.mjs
new file mode 100644
index 00000000..233caf37
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-commands.mjs
@@ -0,0 +1,239 @@
+// fact-commands.mjs - the `fact` subcommands of report-tools: add, quote, retract, export, fill,
+// derive, verify (fact-commands-verify.mjs), pairs and meaning-check (fact-commands-meaning.mjs). Every value reaches the ledger from a source
+// snapshot through a deterministic locator or from a declared derivation; nothing here accepts a
+// typed value. add and quote print the row with 60 chars of source context on each side (JSON
+// with --json). Exit 0 ok, 1 semantic failure (fact_not_located, fill errors, a refused
+// derivation), 2 usage. node: builtins and sibling imports only.
+
+import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
+import { dirname, extname, join, resolve } from "node:path"
+
+import { CliError, parseArgs, sha256Of, writeJsonAtomic } from "./cli-support.mjs"
+import { DEFECT_CODES } from "./contracts.mjs"
+import { derive } from "./fact-derive.mjs"
+import { fill } from "./fact-fill.mjs"
+import { addRow, readLedger, retractRow, supersedeRow, writeLedger } from "./fact-ledger.mjs"
+import { locateFind, locateMatch, locatePageLine, locateTable } from "./fact-locate.mjs"
+import { QUALIFIER_FORMS } from "./fidelity-lexicon.mjs"
+import { readState, writeState } from "./fidelity-state.mjs"
+import { exportRows } from "./fidelity-values.mjs"
+import { pointedFlag, sessionDirOf, snapshotOf } from "./source-commands.mjs"
+
+const CONTEXT = 60
+const BOOLEANS = ["json", "csv", "md", "inline", "view", "replace-failed", "disjoint"]
+const QUALIFIER_OF = new Map(Object.entries(QUALIFIER_FORMS).flatMap(([term, forms]) => forms.map((form) => [form, term])))
+const slug = (text) => String(text).toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 32) || "fact"
+
+function readText(path) {
+ try {
+ return readFileSync(path, "utf8")
+ } catch (error) {
+ throw new CliError(`cannot read ${path}: ${error.code ?? error.message}`)
+ }
+}
+
+function nowIso(flags) {
+ const date = flags.now === undefined ? new Date() : new Date(flags.now)
+ if (Number.isNaN(date.getTime())) throw new CliError("--now requires an ISO timestamp")
+ return date.toISOString()
+}
+
+function failure(command, error, detail) {
+ const json = { command, status: "fail", exitCode: 1, error, ...detail }
+ return { json, summary: `${command}: ${error}${detail.message ? ` (${detail.message})` : ""}`, exitCode: 1 }
+}
+
+function locate(flags, snapshot, id, quote) {
+ const sourceId = id
+ if (quote) return locateMatch(snapshot.text, { match: flags.match, sourceId })
+ if (flags.find !== undefined) return locateFind(snapshot.text, { find: flags.find, unitExpected: flags["unit-expected"], sourceId })
+ if (flags.table !== undefined) {
+ if (snapshot.html === null) throw new CliError(`source ${id} has no source.html; --table needs a page snapshot`)
+ return locateTable(snapshot.html, { table: flags.table, row: flags.row, col: flags.col, sourceId })
+ }
+ if (flags.page !== undefined) return locatePageLine(snapshot.text, { page: Number(flags.page), line: Number(flags.line), sourceId })
+ throw new CliError("usage: fact add --source S<n> (--find TEXT | --table T --row R --col C | --page P --line L) --label LABEL")
+}
+
+function contextOf(text, row) {
+ const start = row.locator.char_start + (row.value_span?.[0] ?? 0)
+ const end = row.value_span ? row.locator.char_start + row.value_span[1] : row.locator.char_end
+ return { before: text.slice(Math.max(0, start - CONTEXT), start), match: text.slice(start, end), after: text.slice(end, end + CONTEXT) }
+}
+
+/** `fact add --assumed V --rationale R`: a value nobody measured, rendered ASSUMED; the rationale is required. */
+function runAssumed(args) {
+ const sessionDir = sessionDirOf(args.flags)
+ const ledger = readLedger(sessionDir)
+ const value = String(args.flags.assumed)
+ const row = addRow(ledger, { kind: "assumed", value, unit: args.flags.unit ?? null, raw: value, quote: "", extracted_by: "model", observed_at: nowIso(args.flags), label: args.required("label"), rationale: args.required("rationale") })
+ writeLedger(sessionDir, ledger)
+ return { json: { command: "fact add", status: "pass", exitCode: 0, defects: [], data: { row } }, exitCode: 0 }
+}
+
+function runAdd(args, quote) {
+ const command = quote ? "fact quote" : "fact add"
+ const { flags } = args
+ if (!quote && flags.assumed !== undefined) return runAssumed(args)
+ const id = args.required("source")
+ if (quote) args.required("match")
+ const sessionDir = sessionDirOf(flags)
+ const snapshot = snapshotOf(sessionDir, id)
+ const found = locate(flags, snapshot, id, quote)
+ if (found.error === "unit_expected_mismatch") return failure(command, "unit_mismatch", { message: `expected ${found.expected}, found ${found.found}`, candidates: found.candidates })
+ if (found.error) return failure(command, found.error, { message: `no single match in ${id}`, candidates: found.candidates })
+ const { meta } = snapshot
+ const host = URL.canParse(meta.url) ? new URL(meta.url).hostname : null
+ const labelSource = flags.find ?? flags.match ?? (flags.table !== undefined ? `${flags.row}_${flags.col}` : `p${flags.page}_l${flags.line}`)
+ const ledger = readLedger(sessionDir)
+ const row = addRow(ledger, {
+ kind: quote ? "quote" : "measured",
+ ...(quote ? {} : { value: found.value, unit: found.unit, value_span: found.value_span, qualifier: QUALIFIER_OF.get(found.qualifier) ?? null }),
+ raw: found.raw,
+ quote: found.quote,
+ source_id: id,
+ locator: { ...found.locator, source_kind: meta.kind, url: meta.url, host, pointed: pointedFlag(sessionDir, meta.url) },
+ snapshot_sha256: meta.sha256,
+ sentence_id: found.sentence_id ?? null,
+ extracted_by: meta.extractor,
+ observed_at: nowIso(flags),
+ label: flags.label ?? slug(labelSource),
+ entity: flags.entity ?? null,
+ valid_at: flags["valid-at"] ?? null,
+ population: flags.population ?? null,
+ accounting_basis: flags["accounting-basis"] ?? null,
+ observation_id: flags.observation ?? null,
+ fuzzy: found.fuzzy === true,
+ })
+ writeLedger(sessionDir, ledger)
+ const context = contextOf(snapshot.text, row)
+ const json = { command, status: "pass", exitCode: 0, defects: [], data: { row, context } }
+ if (flags.json) return { json, exitCode: 0 }
+ const shown = quote ? row.quote : `${row.raw} (value ${row.value}${row.unit ? `, unit ${row.unit}` : ""})`
+ return { text: `${row.fact_id} ${row.label}: ${shown} [${id} ${row.sentence_id ?? "-"}]\n ...${context.before}[${context.match}]${context.after}...`, exitCode: 0 }
+}
+
+function runRetract(args) {
+ const [id] = args.positionals
+ if (!id) throw new CliError("usage: fact retract <F<n>|Q<n>> --reason TEXT [--replacement ID]")
+ const sessionDir = sessionDirOf(args.flags)
+ const ledger = readLedger(sessionDir)
+ const reason = args.required("reason")
+ const row = args.flags.replacement === undefined ? retractRow(ledger, id, reason) : supersedeRow(ledger, id, args.flags.replacement, reason)
+ writeLedger(sessionDir, ledger)
+ return { json: { command: "fact retract", status: "pass", exitCode: 0, data: { fact_id: id, status: row.status, replacement: args.flags.replacement ?? null } }, exitCode: 0 }
+}
+
+const csvCell = (value) => (/[",\n]/.test(String(value)) ? `"${String(value).replace(/"/g, '""')}"` : String(value))
+
+/** "N of M sources snapshotted, by extractor ...": M also counts sources-ledger ids not yet snapshotted. */
+function coverageLine(sessionDir) {
+ const dir = join(sessionDir, "sources")
+ const snapped = existsSync(dir) ? readdirSync(dir).filter((name) => existsSync(join(dir, name, "meta.json"))) : []
+ const ledgerFile = join(sessionDir, "sources-ledger.md")
+ const listed = existsSync(ledgerFile) ? [...readText(ledgerFile).matchAll(/^\[S(\d+)\]/gm)].map((match) => `S${match[1]}`) : []
+ const byExtractor = {}
+ for (const id of snapped) {
+ const extractor = JSON.parse(readText(join(dir, id, "meta.json"))).extractor
+ byExtractor[extractor] = (byExtractor[extractor] ?? 0) + 1
+ }
+ const total = new Set([...snapped, ...listed]).size
+ const parts = Object.entries(byExtractor).map(([name, count]) => `${name} ${count}`)
+ return `${snapped.length} of ${total} sources snapshotted, by extractor: ${parts.join(", ") || "none"}`
+}
+
+function runExport(args) {
+ const sessionDir = sessionDirOf(args.flags)
+ const ledger = readLedger(sessionDir)
+ const coverage = coverageLine(sessionDir)
+ let text
+ if (args.flags.csv) {
+ const rows = exportRows(ledger)
+ text = `${["fact_id,label,plot_value,unit", ...rows.map((row) => [row.fact_id, row.label, row.plot_value, row.unit].map(csvCell).join(","))].join("\n")}\n`
+ } else text = `${JSON.stringify({ schemaVersion: ledger.schemaVersion, rows: ledger.rows, coverage }, null, 2)}\n`
+ if (args.flags.out === undefined) return { text, summary: coverage, exitCode: 0 }
+ writeFileSync(args.flags.out, text)
+ return { json: { command: "fact export", status: "pass", exitCode: 0, data: { out: resolve(args.flags.out), format: args.flags.csv ? "csv" : "json", sha256: sha256Of(text), coverage } }, summary: coverage, exitCode: 0 }
+}
+
+/** The inline edition: local stylesheets and scripts inlined, relative image paths made absolute. */
+function inlineAssets(html, baseDir) {
+ const local = (ref) => !/^[a-z][a-z0-9+.-]*:|^\/\//i.test(ref)
+ return html
+ .replace(/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi, (tag) => {
+ const href = /\bhref=["']([^"']+)["']/i.exec(tag)?.[1]
+ return href && local(href) ? `<style>${readText(resolve(baseDir, href))}</style>` : tag
+ })
+ .replace(/<script\b([^>]*)\bsrc=["']([^"']+)["']([^>]*)><\/script>/gi, (tag, pre, src, post) => (local(src) ? `<script${pre}${post}>${readText(resolve(baseDir, src))}</script>` : tag))
+ .replace(/(<img\b[^>]*\bsrc=["'])([^"']+)(["'])/gi, (tag, pre, src, post) => (local(src) && !src.startsWith("/") ? `${pre}${resolve(baseDir, src)}${post}` : tag))
+}
+
+function runFill(args) {
+ const [source] = args.positionals
+ const { flags } = args
+ if (!source || (flags.out === undefined && !flags.view)) throw new CliError("usage: fact fill <src> (--out <path> | --view) [--format html|md|tex] [--inline] [--replace-failed]")
+ const format = flags.format ?? ({ ".md": "md", ".tex": "tex" }[extname(source).toLowerCase()] ?? "html")
+ if (!["html", "md", "tex"].includes(format)) throw new CliError("--format must be html, md, or tex")
+ if (flags.inline && format !== "html") throw new CliError("--inline applies to the html edition only")
+ const sessionDir = sessionDirOf(flags)
+ const state = flags["replace-failed"] ? readState(sessionDir) : null
+ const result = fill(readText(source), readLedger(sessionDir), { format: format === "tex" ? "md" : format, view: flags.view === true, ...(state ? { replaceFailed: state } : {}) })
+ const warnings = result.warnings
+ if (result.out === null) {
+ const defects = result.errors.map((error) => ({ ...error, severity: DEFECT_CODES[error.code]?.severity ?? "blocker", hint: DEFECT_CODES[error.code]?.hint ?? null }))
+ return { json: { command: "fact fill", status: "fail", exitCode: 1, defects, warnings }, summary: `fact fill: FAIL - ${defects.length} error(s), nothing written`, exitCode: 1 }
+ }
+ const out = flags.inline ? inlineAssets(result.out, dirname(resolve(source))) : result.out
+ if (flags.view) return { text: out, summary: `fact fill --view: ${result.fillMap.length} pointer(s), ${warnings.length} warning(s)`, exitCode: 0 }
+ writeFileSync(flags.out, out)
+ if (state) writeState(sessionDir, state)
+ const artifact = sha256Of(out)
+ const map = `${resolve(flags.out)}.fill-map.json`
+ writeJsonAtomic(map, { schemaVersion: 1, format, artifact: resolve(flags.out), artifact_sha256: artifact, entries: result.fillMap })
+ const data = { out: resolve(flags.out), map, artifact_sha256: artifact, occurrences: result.fillMap.length, warnings }
+ return { json: { command: "fact fill", status: "pass", exitCode: 0, defects: [], data }, summary: `fact fill: wrote ${flags.out} (${result.fillMap.length} pointer(s))`, exitCode: 0 }
+}
+
+function runDerive(args) {
+ const { flags } = args
+ const op = args.required("op")
+ const sessionDir = sessionDirOf(flags)
+ const ledger = readLedger(sessionDir)
+ // The registry refuses any field outside the operation's inputFields, so only the given flags go in; --unit null declares a dimensionless result.
+ const picked = ["to", "from", "num", "den", "part", "whole", "reference", "rationale", "label"].filter((key) => flags[key] !== undefined)
+ const spec = { op, now: nowIso(flags), ...Object.fromEntries(picked.map((key) => [key, flags[key]])) }
+ if (op === "expr") spec.formula = args.positionals[0]
+ else if (args.positionals.length > 0) spec.operands = args.positionals
+ if (flags.disjoint === true) spec.disjoint = true
+ if (flags.unit !== undefined) spec.unit = flags.unit === "null" ? null : flags.unit
+ const result = derive(ledger, spec)
+ if (result.error) return failure("fact derive", result.error, { message: result.detail, ...(result.hint ? { hint: result.hint } : {}) })
+ writeLedger(sessionDir, ledger)
+ const { row } = result
+ const json = { command: "fact derive", status: "pass", exitCode: 0, defects: [], data: { row, compatibility: result.compatibility } }
+ return flags.json ? { json, exitCode: 0 } : { text: `${row.fact_id} ${row.label}: ${row.render} (${row.operation} of ${row.operands.join(", ")}, ${result.compatibility})`, exitCode: 0 }
+}
+
+export async function runFact(argv) {
+ const [sub, ...rest] = argv
+ if (sub === "pairs" || sub === "meaning-check") return (await import("./fact-commands-meaning.mjs")).runFactMeaning(sub, rest)
+ const args = parseArgs(rest, { booleans: BOOLEANS })
+ switch (sub) {
+ case "add":
+ return runAdd(args, false)
+ case "quote":
+ return runAdd(args, true)
+ case "retract":
+ return runRetract(args)
+ case "export":
+ return runExport(args)
+ case "fill":
+ return runFill(args)
+ case "derive":
+ return runDerive(args)
+ case "verify":
+ return (await import("./fact-commands-verify.mjs")).runFactVerify(args)
+ default:
+ throw new CliError("usage: fact <add|quote|retract|export|fill|verify|derive|pairs|meaning-check> ...")
+ }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-derive.mjs b/plugins/omo/skills/ulw-research/scripts/fact-derive.mjs
new file mode 100644
index 00000000..7bdb6a58
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-derive.mjs
@@ -0,0 +1,66 @@
+// Public append boundary: dispatch through the operation table, verify transitive trust, append only on success.
+import { leaves, parseExpr } from "./fact-expr.mjs"
+import { readRow } from "./fact-ledger-kinds.mjs"
+import { addRow, nextId, validateLedger } from "./fact-ledger.mjs"
+import { recompute, verifyGraph } from "./fact-recompute.mjs"
+import { canonicalUnit, inputScale, refuse } from "./unit-algebra.mjs"
+import { refusal } from "./unit-errors.mjs"
+import { object, sameDeclared } from "./unit-metadata.mjs"
+import { display, renderValue, resultPrecision, toText } from "./unit-precision.mjs"
+import { expectedUnit, renderPrecision, ruleOf, unitMismatch } from "./unit-rules.mjs"
+export { recompute, display }
+
+/** Arithmetic and generated fields are chosen exclusively by the registry. */
+export function compute(op, rows, unit, ast, context, formula = null) {
+ const rule = ruleOf(op)
+ const out = rule.calculate(rows, context, ast, formula)
+ const mode = renderPrecision(op, unit, ast)
+ const precision = resultPrecision(rows, ast ? leaves(ast).literals : [], out.exact, mode)
+ const value = renderValue(out.exact, precision, mode)
+ const render = rule.render(display(value, unit), rows)
+ return { exact: toText(out.exact), value, render, raw: render, formula: out.formula, factor: out.factor, precision, compatibility: rule.compatibility, ...rule.metadataResult(rows) }
+}
+
+function declaredUnit(spec, rule) {
+ const own = Object.hasOwn(spec, "unit")
+ if ((!own && !rule.defaultUnit) || (own && spec.unit === undefined)) refuse("declare the result unit: pass unit (null for a dimensionless result)", undefined, "missing_unit", "an explicit unit or null")
+ const declared = own ? spec.unit : rule.defaultUnit(spec)
+ if (inputScale(declared)) refuse(`${declared} needs rescaling: declare %p and scale the value`)
+ return canonicalUnit(declared)
+}
+
+export function derive(ledger, spec) {
+ try {
+ if (!object(spec)) refuse("spec must be an object")
+ const errors = validateLedger(ledger)
+ if (errors.length) refuse(errors.join("; "), undefined, "ledger_schema", "a ledger conforming to KIND_RULES and canonical storage")
+ const rule = ruleOf(spec.op)
+ const allowed = ["op", "unit", "rationale", "label", "now", "context", ...rule.inputFields]
+ if (Object.keys(spec).some((key) => !allowed.includes(key))) refuse("spec carries fields outside its operation", undefined, "spec_fields", allowed)
+ const ast = rule.expression ? parseExpr(spec.formula) : null
+ const ids = rule.operandIds(spec, ast)
+ if (!Array.isArray(ids) || !ids.every((id) => typeof id === "string")) refuse(`${spec.op} needs its operand fact ids`, undefined, "operands", "an array of existing fact ids")
+ const verified = verifyGraph(ledger, ids, true)
+ if (!verified.ok) return { error: "incompatible_operands", ...verified }
+ const byId = new Map(ledger.rows.map((r) => [r.fact_id, r]))
+ const rows = ids.map((id) => readRow(byId.get(id)))
+ const context = rule.context(spec)
+ if (Object.hasOwn(spec, "context") && !sameDeclared(spec.context, context)) refuse("supplied context differs from the operation's input fields", undefined, "context", context ?? "explicit null")
+ const shape = rule.shape({ operation: spec.op, operands: ids, formula: spec.formula, context })
+ if (shape) refuse(shape, undefined, "context_or_formula", "the operation's declared context and matching formula operand set")
+ const expected = expectedUnit(spec.op, rows, { ast, context: { ...context, rationale: spec.rationale ?? null } })
+ const declared = declaredUnit(spec, rule)
+ if (declared !== expected) refuse(unitMismatch(spec.op, rows, declared, expected), undefined, "result_unit", { unit: expected })
+ const result = compute(spec.op, rows, expected, ast, context, spec.formula)
+ const row = addRow(ledger, {
+ kind: rule.kind, unit: expected, quote: "", extracted_by: "fact-derive",
+ observed_at: spec.now ?? new Date().toISOString(),
+ label: spec.label ?? `${spec.op}-${nextId(ledger, rule.kind).toLowerCase()}`,
+ ...result, operation: spec.op, operands: ids, derived_from: ids,
+ rationale: spec.rationale ?? null, context,
+ })
+ return { row, compatibility: rule.compatibility }
+ } catch (error) {
+ return { error: "incompatible_operands", ...refusal(error) }
+ }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-expr.mjs b/plugins/omo/skills/ulw-research/scripts/fact-expr.mjs
new file mode 100644
index 00000000..a83f061e
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-expr.mjs
@@ -0,0 +1,108 @@
+// fact-expr.mjs - the expression language of `fact derive --op expr` (references/unit-rules.md, expr row).
+// Leaves are fact ids and decimal literals; nodes are + - * / and pct(x), the explicit dimensionless-to-% node.
+// evaluate() is exact rational arithmetic. Under * and / a child whose inferred unit is % or %p enters as its
+// fraction (20% as 0.2): that unit comes from the registry's inference over the operand rows, never from the row's
+// own declared unit.
+
+import { add, div, fromDecimal, mul, sub } from "./rational.mjs"
+import { isPercentish, refuse } from "./unit-algebra.mjs"
+
+const TOKEN = /\s*(?:(F\d+)|(\d+(?:\.\d+)?)|(pct)\b|([-+*/()]))/y
+const HUNDRED = fromDecimal("100")
+
+function tokenize(formula) {
+ const text = String(formula ?? "")
+ const tokens = []
+ TOKEN.lastIndex = 0
+ while (/\S/.test(text.slice(TOKEN.lastIndex))) {
+ const at = TOKEN.lastIndex
+ const match = TOKEN.exec(text)
+ if (!match) refuse(`unsupported expression text at ${at}: "${text.slice(at).trim().slice(0, 16)}"`)
+ tokens.push(match[1] ? { id: match[1] } : match[2] ? { literal: match[2] } : match[3] ? { fn: "pct" } : { op: match[4] })
+ }
+ if (tokens.length === 0) refuse("expr needs an expression")
+ return tokens
+}
+
+/** Shunting-yard parser: parentheses and AST depth do not consume the JS call stack. */
+export function parseExpr(formula) {
+ if (typeof formula !== "string") refuse("expr needs an expression")
+ const tokens = tokenize(formula)
+ const values = []
+ const ops = []
+ const precedence = (op) => op === "*" || op === "/" ? 2 : 1
+ const reduce = () => {
+ const op = ops.pop()
+ if (op === "pct") values.push({ fn: op, arg: values.pop() })
+ else { const right = values.pop(); values.push({ op, left: values.pop(), right }) }
+ }
+ let need = true
+ for (let i = 0; i < tokens.length; i++) {
+ const token = tokens[i]
+ if (need) {
+ if (token.id || token.literal) { values.push(token); need = false }
+ else if (token.fn) {
+ if (tokens[i + 1]?.op !== "(") refuse("pct needs a parenthesized argument")
+ ops.push("pct")
+ } else if (token.op === "(") ops.push("(")
+ else refuse(`unexpected "${token.op}" in the expression`)
+ } else if (token.op === ")") {
+ while (ops.length && ops.at(-1) !== "(") reduce()
+ if (ops.pop() !== "(") refuse("unexpected closing parenthesis")
+ if (ops.at(-1) === "pct") reduce()
+ } else if (["+", "-", "*", "/"].includes(token.op)) {
+ while (ops.length && ops.at(-1) !== "(" && precedence(ops.at(-1)) >= precedence(token.op)) reduce()
+ ops.push(token.op)
+ need = true
+ } else refuse("unexpected text after the expression")
+ }
+ if (need) refuse("the expression ends early")
+ if (ops.includes("(")) refuse("the expression is missing a closing parenthesis")
+ while (ops.length) reduce()
+ return values[0]
+}
+
+const childrenOf = (node) => (node.op ? [node.left, node.right] : node.fn ? [node.arg] : [])
+
+export function leaves(node, found = { ids: [], literals: [] }) {
+ const ids = new Set(found.ids)
+ const stack = [node]
+ while (stack.length) {
+ const current = stack.pop()
+ if (current.id) ids.add(current.id)
+ if (current.literal) found.literals.push(current.literal)
+ stack.push(...childrenOf(current).reverse())
+ }
+ found.ids = [...ids]
+ return found
+}
+
+export function postorder(node) {
+ const stack = [node]
+ const order = []
+ while (stack.length) { const current = stack.pop(); order.push(current); stack.push(...childrenOf(current)) }
+ return order.reverse()
+}
+
+/** Exact value of the tree; `units` maps each node to its inferred canonical unit (inferExpr in unit-rules.mjs). */
+export function evaluate(node, values, units) {
+ const results = new Map()
+ for (const current of postorder(node)) {
+ let value
+ if (current.id) value = values.get(current.id)
+ else if (current.literal) value = fromDecimal(current.literal)
+ else if (current.fn === "pct") value = mul(results.get(current.arg), HUNDRED)
+ else {
+ const operand = (child) => {
+ const v = results.get(child)
+ return (current.op === "*" || current.op === "/") && isPercentish(units.get(child)) ? div(v, HUNDRED) : v
+ }
+ const left = operand(current.left)
+ const right = operand(current.right)
+ if (current.op === "/" && right.num === 0n) refuse("the expression divides by zero", undefined, "divides_by_zero", "a nonzero denominator")
+ value = { "+": add, "-": sub, "*": mul, "/": div }[current.op](left, right)
+ }
+ results.set(current, value)
+ }
+ return results.get(node)
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-fill.mjs b/plugins/omo/skills/ulw-research/scripts/fact-fill.mjs
new file mode 100644
index 00000000..3a233124
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-fill.mjs
@@ -0,0 +1,194 @@
+// fact-fill.mjs - the fill step: substitute every fact pointer in a report source from the
+// ledger, all or nothing. Fatal errors (pointer_label_mismatch, unresolved_placeholder,
+// quote_anchor_unresolved, unit_duplicated) return out null; warnings (particle_unverified,
+// fallback_without_entity) never block. A twice-failed sentence can be replaced by an attributed
+// block quote of its source row (replaceFailed). No IO, but not pure: unless options.view is set,
+// it records each replaced sentence in options.replaceFailed through markReplaced. node: builtins only.
+
+import { finalReading, rewriteParticle } from "./particles-ko.mjs"
+import { failureCount, markReplaced, sentenceKey } from "./fidelity-state.mjs"
+import { findPlaceholders, lead, qualifiedText, renderBlockquote, renderQuote, renderValueHtml, subSpan, valueParts } from "./placeholder.mjs"
+import { splitSentences } from "./sentence-split.mjs"
+
+const FORMATS = Object.freeze(["html", "md"])
+const TRANSLATION = Object.freeze({
+ html: /^\s*<span data-translates="(Q\d+)">[\s\S]*?<\/span>/u,
+ md: /^\s*<!-- translates: (Q\d+) -->[^\n]*/u,
+})
+const HEADING = Object.freeze({ html: /<h[1-6]\b([^>]*)>([\s\S]*?)<\/h[1-6]>/giu, md: /^#{1,6}[ \t]+(.+)$/gmu })
+
+const lineOf = (text, at) => text.slice(0, at).split("\n").length
+
+function resolveValue(row, parsed, format) {
+ const { valueText, unitText } = valueParts(row)
+ const text = { full: row.raw, value: valueText, unit: unitText, q: qualifiedText(row) }[parsed.mode]
+ if (text === null) return { code: "quote_anchor_unresolved", message: `${row.fact_id} has qualifier ${row.qualifier}, but its quote holds no qualifier beside "${row.raw}"` }
+ return { row, parsed, visible: text, rendered: format === "html" ? renderValueHtml(row, text) : text }
+}
+
+function resolveQuote(row, parsed, format) {
+ const whole = { text: row.quote, start: 0, end: row.quote.length }
+ const selection = parsed.mode === "lead" ? lead(row.quote, parsed.lead) : parsed.mode === "span" ? subSpan(row.quote, parsed.from, parsed.to) : whole
+ if (selection === null) {
+ return { code: "quote_anchor_unresolved", message: `from "${parsed.from}" and to "${parsed.to}" must each occur exactly once in ${row.fact_id}'s quote, in that order` }
+ }
+ const rendered = renderQuote(row, parsed.selector, selection.text, format)
+ return { row, parsed, visible: `[${row.source_id}]`, rendered, selector: parsed.selector, quote_span: [selection.start, selection.end] }
+}
+
+function resolve(found, rows, format) {
+ const { parsed } = found
+ if (parsed.error) return { code: "unresolved_placeholder", message: parsed.error }
+ const row = rows.get(parsed.id)
+ if (row === undefined) return { code: "unresolved_placeholder", message: `${parsed.id} is not in the fact ledger` }
+ if (row.status !== "active") return { code: "unresolved_placeholder", message: `${parsed.id} is ${row.status}; cite its replacement` }
+ if (row.label !== parsed.label) return { code: "pointer_label_mismatch", message: `${parsed.id} has label "${row.label}", not "${parsed.label}"` }
+ return parsed.kind === "Q" ? resolveQuote(row, parsed, format) : resolveValue(row, parsed, format)
+}
+
+/** A unit typed right after a pointer that already renders it: never absorbed, always an error. */
+function unitDuplicated(text, item) {
+ if (item.parsed.kind !== "F" || (item.parsed.mode !== "full" && item.parsed.mode !== "q")) return false
+ const suffix = item.row.raw.replace(/^[\s\S]*\d/u, "").trim()
+ const units = [item.row.unit, valueParts(item.row).unitText, suffix].filter((unit) => typeof unit === "string" && unit !== "")
+ const following = text.slice(item.end).replace(/^[ \t]+/u, "")
+ return units.some((unit) => following.startsWith(unit) && !/[A-Za-z0-9]/u.test(following[unit.length] ?? ""))
+}
+
+/*
+ * Sentence-key rules (fidelity-state.mjs sentenceKey(slug, ids)); any other producer of keys must match:
+ * - slug source: an HTML heading's id attribute wins over its text; md headings use their text.
+ * - slugify: strip tags, lowercase, turn each run of non-letters/digits (\p{L}\p{N}) into "-",
+ * trim leading and trailing "-".
+ * - a sentence takes the nearest heading that starts strictly before the sentence's start offset;
+ * a sentence before the first heading gets slug "".
+ * - fact ids cited in the sentence are de-duplicated before sentenceKey sorts them.
+ */
+function slugify(text) {
+ return text
+ .replace(/<[^>]*>/gu, "")
+ .toLowerCase()
+ .replace(/[^\p{L}\p{N}]+/gu, "-")
+ .replace(/^-+|-+$/gu, "")
+}
+
+function headings(text, format) {
+ return [...text.matchAll(HEADING[format])].map((match) => {
+ const id = format === "html" ? /\bid="([^"]*)"/u.exec(match[1])?.[1] : undefined
+ return { at: match.index, slug: slugify(id ?? match[match.length - 1]) }
+ })
+}
+
+/** Sentence offsets over the source with pointers, comments and (html) tags blanked to equal length. */
+function sentencesOf(text, found, format) {
+ let masked = text
+ for (const item of found) masked = masked.slice(0, item.start) + "x".repeat(item.end - item.start) + masked.slice(item.end)
+ const blank = (part) => part.replace(/[^\n]/gu, " ")
+ masked = masked.replace(/<!--[\s\S]*?-->/gu, blank)
+ if (format === "html") masked = masked.replace(/<[^>]*>/gu, blank)
+ return splitSentences(masked)
+}
+
+function replacements(text, found, items, options, warnings) {
+ const { format, replaceFailed: state } = options
+ const marks = headings(text, format)
+ const out = []
+ for (const sentence of sentencesOf(text, found, format)) {
+ const inside = items.filter((item) => item.start >= sentence.start && item.end <= sentence.end)
+ if (inside.length === 0) continue
+ const slug = marks.filter((mark) => mark.at < sentence.start).at(-1)?.slug ?? ""
+ const key = sentenceKey(slug, [...new Set(inside.map((item) => item.row.fact_id))])
+ if (failureCount(state, key) < 2) continue
+ const rows = inside.map((item) => item.row)
+ const source = rows.find((row) => row.kind === "quote") ?? rows.find((row) => row.source_id !== null && row.quote !== "")
+ if (source === undefined) continue
+ let rendered = options.fallback === "omit" ? "" : renderBlockquote(source, key, format)
+ if (rendered !== "" && format === "md") {
+ if (sentence.start > 0 && text[sentence.start - 1] !== "\n") rendered = `\n\n${rendered}`
+ if (sentence.end < text.length && text[sentence.end] !== "\n") rendered = `${rendered}\n\n`
+ }
+ if (rendered !== "" && source.entity === null) {
+ warnings.push({ code: "fallback_without_entity", token: key, line: lineOf(text, sentence.start), message: `${source.fact_id} has no entity; the block quote cites [${source.source_id}] alone` })
+ }
+ out.push({ start: sentence.start, end: sentence.end, key, source, rendered })
+ }
+ return out
+}
+
+/** The text after a pointer: rewrite a following Korean particle, and map a translation span. */
+function afterPlaceholder(segment, item, context) {
+ const { format, text, warnings, fillMap } = context
+ const probeEnd = format === "html" && segment.includes("<") ? segment.indexOf("<") : segment.length
+ const result = rewriteParticle(segment.slice(0, probeEnd), finalReading(item.visible, null))
+ let next = segment
+ if (result.applied) next = result.text + segment.slice(probeEnd)
+ else if (result.candidate) {
+ warnings.push({ code: "particle_unverified", token: item.token, line: lineOf(text, item.start), message: `the particle after ${item.token} was not rewritten; check it by eye` })
+ }
+ const translation = item.parsed.kind === "Q" ? TRANSLATION[format].exec(next) : null
+ if (translation !== null && translation[1] === item.row.fact_id) {
+ const span = translation[0].trimStart()
+ const start = context.out.length + translation[0].length - span.length
+ fillMap.push({ token: span, fact_id: translation[1], translates: translation[1], start, end: start + span.length, rendered: span })
+ }
+ return next
+}
+
+function assemble(text, parts, context) {
+ let cursor = 0
+ let previous = null
+ const flush = (until) => {
+ const segment = text.slice(cursor, until)
+ context.out += previous?.parsed ? afterPlaceholder(segment, previous, context) : segment
+ }
+ for (const part of parts) {
+ flush(part.start)
+ const start = context.out.length
+ if (part.parsed) {
+ const extra = part.selector === undefined ? {} : { selector: part.selector, quote_span: part.quote_span }
+ context.fillMap.push({ token: part.token, fact_id: part.row.fact_id, start, end: start + part.rendered.length, rendered: part.rendered, ...extra })
+ } else if (part.rendered !== "") {
+ context.fillMap.push({ token: null, fact_id: part.source.fact_id, replaced: part.key, start, end: start + part.rendered.length, rendered: part.rendered })
+ }
+ context.out += part.rendered
+ cursor = part.end
+ previous = part
+ }
+ flush(text.length)
+ return context.out
+}
+
+/**
+ * @param {string} sourceText the report source with {{F..}} / {{Q..}} pointers
+ * @param {{ rows: object[] }} ledger
+ * @param {{ format?: "html"|"md", replaceFailed?: object, fallback?: "quote"|"omit", view?: boolean }} [options]
+ * @returns {{ out: string|null, fillMap: object[], errors: object[], warnings: object[] }}
+ */
+export function fill(sourceText, ledger, options = {}) {
+ const format = options.format ?? "html"
+ if (!FORMATS.includes(format)) throw new TypeError(`format must be one of ${FORMATS.join(", ")}`)
+ const rows = new Map(ledger.rows.map((row) => [row.fact_id, row]))
+ const found = findPlaceholders(sourceText)
+ const errors = []
+ const warnings = []
+ const items = []
+ for (const pointer of found) {
+ const resolved = resolve(pointer, rows, format)
+ const line = lineOf(sourceText, pointer.start)
+ if (resolved.code) errors.push({ code: resolved.code, token: pointer.token, line, message: resolved.message })
+ else {
+ const item = { ...pointer, ...resolved }
+ if (unitDuplicated(sourceText, item)) {
+ errors.push({ code: "unit_duplicated", token: pointer.token, line, message: `${pointer.token} already renders its unit; use {{${item.row.fact_id}:${item.row.label}:value}}` })
+ } else items.push(item)
+ }
+ }
+ if (errors.length > 0) return { out: null, fillMap: [], errors, warnings }
+ const replaced = options.replaceFailed ? replacements(sourceText, found, items, { ...options, format }, warnings) : []
+ const kept = items.filter((item) => !replaced.some((block) => item.start >= block.start && item.end <= block.end))
+ const parts = [...kept, ...replaced].sort((a, b) => a.start - b.start)
+ const context = { format, text: sourceText, warnings, fillMap: [], out: "" }
+ const out = assemble(sourceText, parts, context)
+ if (!options.view) for (const block of replaced) markReplaced(options.replaceFailed, block.key)
+ return { out, fillMap: context.fillMap, errors, warnings }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-ledger-kinds.mjs b/plugins/omo/skills/ulw-research/scripts/fact-ledger-kinds.mjs
index 80b1e5b2..197d7f5d 100644
--- a/plugins/omo/skills/ulw-research/scripts/fact-ledger-kinds.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/fact-ledger-kinds.mjs
@@ -5,6 +5,11 @@
// Also the value primitives the schema shares. Pure functions with no IO; node: builtins only.
import { isCliEntry } from "./entry-guard.mjs"
+import { canonicalUnit, inputScale, RuleError } from "./unit-algebra.mjs"
+import { metadataErrors } from "./unit-metadata.mjs"
+import { mul } from "./rational.mjs"
+import { placesOf, roundPlaces, toBig } from "./unit-precision.mjs"
+import { OPERATION_RULES } from "./unit-rules.mjs"
export const DECIMAL = /^-?\d+(?:\.\d+)?$/
const INTEGER = /^-?\d+$/
@@ -16,7 +21,15 @@ export const isString = (value) => typeof value === "string"
export const isNonEmpty = (value) => typeof value === "string" && value.length > 0
export const isCount = (value) => Number.isInteger(value) && value >= 0
const isDecimal = (value) => isString(value) && DECIMAL.test(value)
-const isRational = (exact) => isObject(exact) && isString(exact.num) && isString(exact.den) && INTEGER.test(exact.num) && /^\d+$/.test(exact.den) && !/^0+$/.test(exact.den)
+export function isRational(exact) {
+ if (!isObject(exact) || Object.keys(exact).sort().join(",") !== "den,num" || !isString(exact.num) || !isString(exact.den) || !INTEGER.test(exact.num) || !/^[1-9]\d*$/.test(exact.den)) return false
+ let a = BigInt(exact.num)
+ let b = BigInt(exact.den)
+ if (String(a) !== exact.num) return false
+ if (a < 0n) a = -a
+ while (b !== 0n) [a, b] = [b, a % b]
+ return a === 1n
+}
/** The exact rational of a decimal string, reduced. */
export function exactFromDecimal(value) {
@@ -72,22 +85,27 @@ export const RULES = Object.freeze({
test: (v, row) => isRational(v) && (!isDecimal(row.value) || (v.num === exactFromDecimal(row.value).num && v.den === exactFromDecimal(row.value).den)),
},
rational_or_null: { cell: "rational or null", says: "null or { num: integer string, den: positive integer string }", test: orNull(isRational) },
- by_derive: { cell: "written by derive (rational or null)", says: "null or { num: integer string, den: positive integer string }", test: orNull(isRational) },
+ by_derive: { cell: "reduced rational", says: "a reduced { num: integer string, den: positive integer string }", test: isRational },
+ context_by_derive: { cell: "written by derive (object or null)", says: "null or an object", test: orNull(isObject) },
+ derived_operation: { cell: "sum/diff/growth/share/ratio/expr", says: "a derived operation", test: (v) => Object.hasOwn(OPERATION_RULES, v) && OPERATION_RULES[v].kind === "derived" },
+ convert_operation: { cell: "`convert`", says: "convert", test: (v) => Object.hasOwn(OPERATION_RULES, v) && OPERATION_RULES[v].kind === "converted" },
+ precision: { cell: "`{ places, sigfigs }`", says: "nonnegative places and positive sigfigs", test: (v) => isObject(v) && Object.keys(v).sort().join(",") === "places,sigfigs" && Number.isSafeInteger(v.places) && v.places >= 0 && Number.isSafeInteger(v.sigfigs) && v.sigfigs >= 1 },
+ compatibility: { cell: "checked or unchecked (expr)", says: "the operation's compatibility", test: (v, r) => Object.hasOwn(OPERATION_RULES, r.operation) && v === OPERATION_RULES[r.operation].compatibility },
})
/** The grammar's per-kind columns, in table order; `exact` follows `value` because it reads it. */
-export const KIND_COLUMNS = Object.freeze(["fact_id", "value", "source_id", "locator", "snapshot_sha256", "rationale", "exact"])
+export const KIND_COLUMNS = Object.freeze(["fact_id", "value", "source_id", "locator", "snapshot_sha256", "rationale", "exact", "context", "operation", "precision", "compatibility"])
const sourced = { source_id: "source", locator: "locator", snapshot_sha256: "sha256" }
const unsourced = { source_id: "source_or_null", locator: "locator_or_null", snapshot_sha256: "sha256_or_null" }
-const row = (cells) => Object.freeze(Object.fromEntries(KIND_COLUMNS.map((column) => [column, cells[column]])))
+const row = (cells) => Object.freeze(Object.fromEntries(KIND_COLUMNS.map((column) => [column, cells[column] ?? (["precision", "compatibility"].includes(column) && cells.operation !== "null" ? column : "null")])))
export const KIND_RULES = Object.freeze({
- measured: row({ fact_id: "f_id", value: "decimal", ...sourced, rationale: "string_or_null", exact: "from_value" }),
- quote: row({ fact_id: "q_id", value: "null", ...sourced, rationale: "string_or_null", exact: "null" }),
- derived: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "string_or_null", exact: "by_derive" }),
- assumed: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "non_empty", exact: "rational_or_null" }),
- converted: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "string_or_null", exact: "by_derive" }),
+ measured: row({ fact_id: "f_id", value: "decimal", ...sourced, rationale: "string_or_null", exact: "from_value", context: "null", operation: "null" }),
+ quote: row({ fact_id: "q_id", value: "null", ...sourced, rationale: "string_or_null", exact: "null", context: "null", operation: "null" }),
+ derived: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "string_or_null", exact: "by_derive", context: "context_by_derive", operation: "derived_operation" }),
+ assumed: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "non_empty", exact: "rational_or_null", context: "null", operation: "null" }),
+ converted: row({ fact_id: "f_id", value: "decimal", ...unsourced, rationale: "string_or_null", exact: "by_derive", context: "context_by_derive", operation: "convert_operation" }),
})
/** The id prefix nextId assigns for a kind; an unknown kind gets F and fails validation later. */
@@ -100,15 +118,101 @@ export function exactComesFromValue(kind) {
return KIND_RULES[kind]?.exact === "from_value"
}
+/** Columns added after schema version 1 shipped; a ledger written before them still loads (dimension 31). */
+export const LATER_COLUMNS = Object.freeze(["context", "precision", "compatibility"])
+const DERIVED_KINDS = Object.freeze(["derived", "converted"])
+const PRE_REGISTRY_COLUMNS = KIND_COLUMNS.slice(0, KIND_COLUMNS.indexOf("exact") + 1)
+
+/** A derived or converted row written before the later columns existed: loadable, never an operand. */
+export const isLegacyDerived = (row) => isObject(row) && DERIVED_KINDS.includes(row.kind) && LATER_COLUMNS.every((column) => !Object.hasOwn(row, column))
+/** A derived or converted row carrying this version's columns: the only rows checked strictly at read time. */
+export const isCurrentDerived = (row) => isObject(row) && DERIVED_KINDS.includes(row.kind) && !isLegacyDerived(row)
+
+/** An exact carrying keys beyond num and den: never normalized, never silently stripped, flagged on use. */
+export const hasUnknownExactKeys = (exact) => isObject(exact) && isString(exact.num) && isString(exact.den) && Object.keys(exact).sort().join(",") !== "den,num"
+/** Any integer pair dev's writer accepted, reduced: sign on num, no leading zeros, gcd 1; anything else as given. */
+function readExact(exact) {
+ if (!isObject(exact) || hasUnknownExactKeys(exact) || !isString(exact.num) || !isString(exact.den) || !INTEGER.test(exact.num) || !/^\d+$/.test(exact.den) || /^0+$/.test(exact.den)) return exact
+ const { num, den } = mul({ num: BigInt(exact.num), den: BigInt(exact.den) }, 1n)
+ return { num: String(num), den: String(den) }
+}
+
+const readDecimal = (value) => (isDecimal(value) ? value.replace(/^(-?)0+(?=\d)/, "$1") : value)
+
+/** A source unit read through the alias table; unknown, scaled, or unparseable text stays as given. */
+export function sourceUnit(unit) {
+ try {
+ const canonical = canonicalUnit(unit)
+ return isString(canonical) && !inputScale(unit) ? canonical : unit
+ } catch (error) {
+ if (error instanceof RuleError) return unit
+ throw error
+ }
+}
+
+/**
+ * A row as every reader sees it (dimension 31 wins at read time): rows not written by derive in this
+ * version get reduced exacts and decimals; source rows also get canonical units and null later columns.
+ * Current derived rows stay as stored so tampering is still detected. Idempotent.
+ */
+export function readRow(row) {
+ if (!isObject(row) || !Object.hasOwn(KIND_RULES, row.kind) || isCurrentDerived(row)) return row
+ const read = { ...row, value: readDecimal(row.value), exact: exactComesFromValue(row.kind) ? row.exact : readExact(row.exact) }
+ if (DERIVED_KINDS.includes(row.kind)) return read
+ read.unit = sourceUnit(row.unit)
+ for (const column of LATER_COLUMNS) if (!Object.hasOwn(read, column)) read[column] = null
+ return read
+}
+
+const carriesDerivation = (row) => row.operation !== null || row.operands?.length || row.derived_from?.length || row.formula !== null || row.factor !== null
+
+/** Dimension 32: a stored exact must equal the row's own value at the value's decimal places. */
+function valueExactDisagreement(row) {
+ const exact = readExact(row.exact)
+ const value = readDecimal(row.value)
+ if (!isRational(exact) || !isDecimal(value) || roundPlaces(toBig(exact), placesOf(value)) === value) return null
+ return `value ${value} disagrees with exact ${exact.num}/${exact.den} at ${placesOf(value)} decimal places`
+}
+function unitFlaw(unit) {
+ try {
+ return isString(unit) && (canonicalUnit(unit) !== unit || inputScale(unit)) ? `unit ${unit} has no canonical stored text` : null
+ } catch (error) {
+ if (error instanceof RuleError) return `unit ${unit}: ${error.message}`
+ throw error
+ }
+}
+
+/**
+ * What a loadable source row (read through readRow) still breaks under the strict dimensions: dev's
+ * writer accepted it, so it loads, but derive and recompute refuse it as an operand with this shape.
+ * addRow refuses a new row that has any. Empty for derived and converted rows.
+ */
+export function readFlaws(row) {
+ if (!isObject(row) || DERIVED_KINDS.includes(row.kind)) return []
+ const flaws = []
+ if (carriesDerivation(row)) flaws.push({ shape: "dimension 1", detail: "a source row carries an operation or derivation fields", expected: "null operation and empty derivation fields on measured, quote, and assumed rows" })
+ const unit = unitFlaw(row.unit)
+ if (unit) flaws.push({ shape: "dimension 6", detail: unit, expected: "a registered or canonical unit without scale" })
+ for (const detail of metadataErrors(row)) flaws.push({ shape: "dimension 18", detail, expected: "present, type-valid metadata; explicit null when unknown" })
+ const unknownKeys = hasUnknownExactKeys(row.exact)
+ const disagreement = unknownKeys ? null : valueExactDisagreement(row)
+ if (unknownKeys) flaws.push({ shape: "dimension 7", detail: `exact has unknown keys: ${Object.keys(row.exact).filter((key) => key !== "num" && key !== "den").join(", ")}`, expected: "an exact with exactly num and den (the stored row is kept as written)" })
+ if (disagreement) flaws.push({ shape: "dimension 32", detail: disagreement, expected: "an exact equal to value at the value's decimal places" })
+ return flaws
+}
+
/** @returns {string[]} every per-kind column error of one row; empty for an unknown kind. */
export function kindRuleErrors(candidate, at) {
const rules = KIND_RULES[candidate.kind]
if (rules === undefined) return []
const errors = []
- for (const column of KIND_COLUMNS) {
- const value = candidate[column]
- if (value === undefined) continue
- const rule = RULES[rules[column]]
+ const legacy = isLegacyDerived(candidate)
+ const columns = legacy ? PRE_REGISTRY_COLUMNS : isCurrentDerived(candidate) ? KIND_COLUMNS : KIND_COLUMNS.filter((column) => column !== "operation")
+ for (const column of columns) {
+ const value = candidate[column] === undefined && LATER_COLUMNS.includes(column) ? null : candidate[column]
+ const rule = RULES[legacy && column === "exact" ? "rational_or_null" : rules[column]]
+ // An exact with unknown keys loads as written (never stripped); readFlaws refuses it at use and at write.
+ if (column === "exact" && hasUnknownExactKeys(value) && !isCurrentDerived(candidate)) continue
if (rule.test(value, candidate)) continue
errors.push(`${at}.${column} must be ${rule.says} for a ${candidate.kind} row`)
if (rule.detail) errors.push(...rule.detail(value, `${at}.${column}`))
@@ -116,6 +220,16 @@ export function kindRuleErrors(candidate, at) {
return errors
}
+/** At-rest rules of current derived rows; source rows answer to readFlaws at use and at write instead. */
+export function storedErrors(row) {
+ if (!isCurrentDerived(row)) return []
+ const errors = metadataErrors(row)
+ try {
+ if (canonicalUnit(row.unit) !== row.unit || inputScale(row.unit)) errors.push("unit must be canonical stored text")
+ } catch (error) { errors.push(error.message) }
+ return errors
+}
+
export const KIND_TABLE_BEGIN = "<!-- kind-rules:begin (rendered from KIND_RULES; do not edit by hand) -->"
export const KIND_TABLE_END = "<!-- kind-rules:end -->"
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-ledger-schema.mjs b/plugins/omo/skills/ulw-research/scripts/fact-ledger-schema.mjs
index 382ff7ef..35809d08 100644
--- a/plugins/omo/skills/ulw-research/scripts/fact-ledger-schema.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/fact-ledger-schema.mjs
@@ -4,7 +4,8 @@
// KIND_RULES (fact-ledger-kinds.mjs), never here.
// Pure functions with no IO; fact-ledger.mjs owns reading, writing, and row changes. node: builtins only.
-import { DECIMAL, isCount, isNonEmpty, isObject, isString, kindRuleErrors } from "./fact-ledger-kinds.mjs"
+import { DECIMAL, isCount, isCurrentDerived, isNonEmpty, isObject, isString, kindRuleErrors, LATER_COLUMNS, readRow, storedErrors } from "./fact-ledger-kinds.mjs"
+import { malformed } from "./unit-rules.mjs"
export { DECIMAL, isNonEmpty, isString } from "./fact-ledger-kinds.mjs"
@@ -50,8 +51,13 @@ export const ROW_DEFAULTS = Object.freeze({
formula: null,
factor: null,
rationale: null,
+ context: null,
+ precision: null,
+ compatibility: null,
status: "active",
})
+/** Fields added after schema version 1 shipped: written on every new row, optional when reading an older ledger. */
+const OPTIONAL_FIELDS = new Set(LATER_COLUMNS)
const REQUIRED = ["fact_id", "kind", "raw", "quote", "extracted_by", "observed_at", "label"]
export const ROW_FIELDS = Object.freeze([...REQUIRED, ...Object.keys(ROW_DEFAULTS)])
@@ -70,10 +76,16 @@ function nullOr(value, test) {
function validateRow(row, index, errors) {
const at = `rows[${index}]`
if (!isObject(row)) return errors.push(`${at} must be an object`)
- for (const key of ROW_FIELDS) check(errors, Object.hasOwn(row, key), `${at}.${key} is required`)
+ for (const key of ROW_FIELDS) check(errors, OPTIONAL_FIELDS.has(key) || Object.hasOwn(row, key), `${at}.${key} is required`)
for (const key of Object.keys(row)) check(errors, ROW_FIELDS.includes(key), `${at}.${key} is not a ledger field`)
check(errors, FACT_KINDS.includes(row.kind), `${at}.kind must be one of ${FACT_KINDS.join(", ")}`)
- errors.push(...kindRuleErrors(row, at))
+ // Dimension 31 wins at read time: per-kind rules see the row as readRow normalizes it.
+ errors.push(...kindRuleErrors(readRow(row), at))
+ errors.push(...storedErrors(row).map((e) => `${at}.${e}`))
+ if (isCurrentDerived(row)) {
+ const problem = malformed(row)
+ if (problem) errors.push(`${at}: ${problem}`)
+ }
check(errors, row.unit === undefined || nullOr(row.unit, isNonEmpty), `${at}.unit must be null or a non-empty token`)
for (const key of ["raw", "quote"]) check(errors, row[key] === undefined || isString(row[key]), `${at}.${key} must be a string`)
check(errors, row.extracted_by === undefined || isNonEmpty(row.extracted_by), `${at}.extracted_by must be a non-empty string`)
@@ -100,6 +112,12 @@ function validateRow(row, index, errors) {
check(errors, row.status === undefined || FACT_STATUSES.includes(row.status), `${at}.status must be one of ${FACT_STATUSES.join(", ")}`)
}
+export function rowErrors(row) {
+ const errors = []
+ validateRow(row, 0, errors)
+ return errors
+}
+
// The status machine and event rule of references/fact-ledger-grammar.md. validateLedger replays
// events through transitionError, and every writer in fact-ledger.mjs calls it before appending.
export const TRANSITIONS = Object.freeze({ active: Object.freeze(["retracted", "superseded"]), retracted: Object.freeze([]), superseded: Object.freeze([]) })
@@ -163,20 +181,19 @@ export function provenanceErrors(rows) {
}
})
const state = new Map()
- const visit = (id, path) => {
- if (state.get(id) === "done") return null
- if (state.get(id) === "open") return [...path.slice(path.indexOf(id)), id]
- state.set(id, "open")
- for (const next of refs.get(id)) {
- const cycle = next !== id && refs.has(next) ? visit(next, [...path, id]) : null
- if (cycle) return cycle
+ for (const root of refs.keys()) {
+ const stack = [{ id: root, finish: false }]
+ const path = []
+ while (stack.length) {
+ const { id, finish } = stack.pop()
+ if (state.get(id) === "done") continue
+ if (finish) { state.set(id, "done"); path.pop(); continue }
+ if (state.get(id) === "open") return [...errors, `${id} derives from itself through cycle ${[...path, id].join(" -> ")}`]
+ state.set(id, "open")
+ path.push(id)
+ stack.push({ id, finish: true })
+ for (const next of refs.get(id)) if (next !== id && refs.has(next)) stack.push({ id: next, finish: false })
}
- state.set(id, "done")
- return null
- }
- for (const id of refs.keys()) {
- const cycle = visit(id, [])
- if (cycle) return [...errors, `${cycle[0]} derives from itself through ${cycle.join(" -> ")}`]
}
return errors
}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-ledger.mjs b/plugins/omo/skills/ulw-research/scripts/fact-ledger.mjs
index cc31bb44..8d2b5b21 100644
--- a/plugins/omo/skills/ulw-research/scripts/fact-ledger.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/fact-ledger.mjs
@@ -7,8 +7,9 @@ import { existsSync } from "node:fs"
import { join } from "node:path"
import { CliError, readJsonFile, writeJsonAtomic } from "./cli-support.mjs"
-import { DECIMAL, exactComesFromValue, exactFromDecimal, idPrefix, isString } from "./fact-ledger-kinds.mjs"
+import { DECIMAL, exactComesFromValue, exactFromDecimal, idPrefix, isString, readFlaws, readRow } from "./fact-ledger-kinds.mjs"
import { LEDGER_SCHEMA_VERSION, ROW_DEFAULTS, ROW_FIELDS, transitionError, validateLedger } from "./fact-ledger-schema.mjs"
+import { normalizeInput } from "./unit-algebra.mjs"
export { exactFromDecimal } from "./fact-ledger-kinds.mjs"
export { FACT_KINDS, FACT_STATUSES, LEDGER_SCHEMA_VERSION, OPERATIONS, QUALIFIERS, ROW_FIELDS, TRANSITIONS, transitionError, validateLedger } from "./fact-ledger-schema.mjs"
@@ -24,11 +25,24 @@ function assertValid(ledger) {
if (errors.length > 0) throw new CliError(`invalid fact ledger: ${errors.join("; ")}`)
}
+/** The row's fields in ROW_FIELDS order, then any unknown field (which validation reports). */
+function ordered(row) {
+ const out = Object.fromEntries(ROW_FIELDS.filter((key) => Object.hasOwn(row, key)).map((key) => [key, row[key]]))
+ for (const key of Object.keys(row)) if (!ROW_FIELDS.includes(key)) out[key] = row[key]
+ return out
+}
+
+/**
+ * Every row comes back as readRow normalizes it (reduced exacts and decimals; source rows with canonical
+ * units and null later columns) before any strict rule runs; the file changes on the next write.
+ */
export function readLedger(dir) {
const file = join(dir, LEDGER_FILENAME)
if (!existsSync(file)) return emptyLedger()
const ledger = readJsonFile(file)
assertValid(ledger)
+ ledger.rows = ledger.rows.map((row) => ordered(readRow(row)))
+ assertValid(ledger)
return ledger
}
@@ -44,12 +58,12 @@ const ID_NUMBER = Object.freeze({ F: /^F(\d+)$/, Q: /^Q(\d+)$/ })
/** The next id with the kind's prefix from KIND_RULES (F<n> for value rows, Q<n> for quotes). */
export function nextId(ledger, kind) {
const prefix = idPrefix(kind)
- let max = 0
+ let max = 0n
for (const row of ledger.rows) {
const match = ID_NUMBER[prefix].exec(row.fact_id ?? "")
- if (match) max = Math.max(max, Number(match[1]))
+ if (match && BigInt(match[1]) > max) max = BigInt(match[1])
}
- return `${prefix}${max + 1}`
+ return `${prefix}${max + 1n}`
}
/**
@@ -60,14 +74,20 @@ export function nextId(ledger, kind) {
*/
export function addRow(ledger, row) {
const filled = { ...structuredClone(ROW_DEFAULTS), ...row, fact_id: row.fact_id ?? nextId(ledger, row.kind) }
+ if (!["derived", "converted"].includes(filled.kind)) {
+ const normalized = normalizeInput(filled.unit, filled.value)
+ if (normalized.value !== filled.value && filled.exact !== null) throw new CliError("scaled input must omit exact; it is computed after normalization")
+ Object.assign(filled, readRow({ ...filled, ...normalized }))
+ }
if (exactComesFromValue(filled.kind) && filled.exact === null && isString(filled.value) && DECIMAL.test(filled.value)) {
filled.exact = exactFromDecimal(filled.value)
}
- const ordered = Object.fromEntries(ROW_FIELDS.filter((key) => Object.hasOwn(filled, key)).map((key) => [key, filled[key]]))
- for (const key of Object.keys(filled)) if (!ROW_FIELDS.includes(key)) ordered[key] = filled[key]
- assertValid({ ...ledger, rows: [...ledger.rows, ordered] })
- ledger.rows.push(ordered)
- return ordered
+ const added = ordered(filled)
+ const flaws = readFlaws(added)
+ if (flaws.length > 0) throw new CliError(`invalid fact ledger: ${flaws.map((flaw) => `${flaw.shape}: ${flaw.detail}`).join("; ")}`)
+ assertValid({ ...ledger, rows: [...ledger.rows, added] })
+ ledger.rows.push(added)
+ return added
}
/** Build the event from the row's current status and append it only if transitionError allows it. */
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-locate.mjs b/plugins/omo/skills/ulw-research/scripts/fact-locate.mjs
new file mode 100644
index 00000000..230f3d9d
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-locate.mjs
@@ -0,0 +1,231 @@
+// fact-locate.mjs - deterministic fact locators over a source snapshot.
+//
+// Each locator returns { value, unit, raw, quote, value_span, sentence_id, qualifier, locator } or
+// { error: "fact_not_located" | "fact_ambiguous", candidates }; it never guesses. `quote` is the
+// whole containing sentence, cell, or line of text.txt, `value_span` is [start, end] inside
+// `quote` with quote.slice(...value_span) === raw, and locator.char_start/char_end place `quote`
+// in text.txt. Every call takes `sourceId` (S<n>) so sentence ids match sentences.jsonl.
+// Plain Node ESM, sibling imports only; no network, no OCR.
+
+import { proseText } from "./html-lite.mjs"
+import { QUALIFIERS } from "./lexicon.mjs"
+import { parseNumeral } from "./numerals.mjs"
+import { indexSentences, snapshotBlocks } from "./source-snapshot.mjs"
+import { findNormalized, normalizeText, tokenize } from "./text-normalize.mjs"
+import { similarity } from "./text-similarity.mjs"
+import { normalizeUnit } from "./units.mjs"
+
+const FIND_WINDOW = 80
+const CONTEXT = 60
+const FUZZY_FLOOR = 0.95
+const PHRASES = QUALIFIERS.map((phrase) => phrase.split(" "))
+const SINGLE_QUALIFIERS = new Set(QUALIFIERS.filter((phrase) => !phrase.includes(" ")))
+
+const notLocated = (candidates = []) => ({ error: "fact_not_located", candidates })
+const ambiguous = (candidates) => ({ error: "fact_ambiguous", candidates })
+const label = (text) => normalizeText(text).toLowerCase()
+const clean = (token) => token.replace(/^[^\p{L}\p{N}%]+|[^\p{L}\p{N}%]+$/gu, "").toLowerCase()
+const sentenceAt = (sentences, offset) => sentences.find((row) => row.start <= offset && offset < row.end) ?? null
+
+function requireSource(sourceId) {
+ if (!/^S[1-9]\d*$/.test(String(sourceId))) throw new TypeError(`sourceId must be S<n>, got ${sourceId}`)
+}
+
+/** Every normalized occurrence of `needle`, as raw [start, end) offsets. */
+function findAll(text, needle) {
+ const hits = []
+ for (let from = 0; from < text.length; ) {
+ const hit = findNormalized(text.slice(from), needle)
+ if (!hit) break
+ hits.push({ start: from + hit.start, end: from + hit.end })
+ from += hit.start + 1
+ }
+ return hits
+}
+
+function locatorFor(text, sourceId, start, end, extra = {}) {
+ const before = text.slice(0, start)
+ const pageStart = before.lastIndexOf("\f") + 1
+ const page = text.includes("\f") ? { page: before.split("\f").length } : {}
+ return { artifact: `${sourceId}/text.txt`, ...page, line: before.slice(pageStart).split("\n").length, char_start: start, char_end: end, ...extra }
+}
+
+function candidate(text, sentences, start, end) {
+ return { sentence_id: sentenceAt(sentences, start)?.sid ?? null, char_start: start, char_end: end, context: text.slice(Math.max(0, start - CONTEXT), end + CONTEXT) }
+}
+
+/** A QUALIFIERS token ending within the 2 tokens before the value, or the 1 token right after it. */
+function qualifierAround(quote, [start, end]) {
+ const before = quote.slice(0, start).split(/\s+/).filter(Boolean).map(clean)
+ for (let skip = 0; skip < 2; skip++) {
+ for (const words of PHRASES) {
+ const stop = before.length - skip
+ if (stop - words.length >= 0 && before.slice(stop - words.length, stop).join(" ") === words.join(" ")) return words.join(" ")
+ }
+ }
+ const next = /^\s*(\S+)/.exec(quote.slice(end))
+ return next && SINGLE_QUALIFIERS.has(clean(next[1])) ? clean(next[1]) : null
+}
+
+/** A located quantity: `holder` is the quote's { sid, start, end, text }, `numeral` is parsed from holder.text. */
+function quantityResult(text, sourceId, holder, numeral, extra = {}, unitFallback = null) {
+ const unit = numeral.unitRaw ? (normalizeUnit(numeral.unitRaw) ?? numeral.unitRaw) : unitFallback
+ return {
+ value: numeral.value,
+ unit,
+ raw: numeral.raw,
+ quote: holder.text,
+ value_span: [numeral.span[0], numeral.span[1]],
+ sentence_id: holder.sid,
+ qualifier: qualifierAround(holder.text, numeral.span),
+ locator: locatorFor(text, sourceId, holder.start, holder.end, extra),
+ }
+}
+
+/** --find: one anchor occurrence, then the first numeral starting within 80 chars after it in the same sentence. */
+export function locateFind(text, { find, unitExpected, sourceId }) {
+ requireSource(sourceId)
+ const sentences = indexSentences(text, sourceId)
+ const hits = findAll(text, find)
+ if (hits.length === 0) return notLocated()
+ if (hits.length > 1) return ambiguous(hits.map((hit) => candidate(text, sentences, hit.start, hit.end)))
+ const [hit] = hits
+ const sentence = sentenceAt(sentences, hit.start)
+ if (!sentence || hit.end > sentence.end) return notLocated()
+ const from = hit.end - sentence.start
+ const numeral = parseNumeral(sentence.text, from)
+ if (!numeral || numeral.span[0] - from > FIND_WINDOW) return notLocated([candidate(text, sentences, hit.start, hit.end)])
+ const result = quantityResult(text, sourceId, sentence, numeral)
+ if (unitExpected === undefined) return result
+ const expected = normalizeUnit(unitExpected) ?? unitExpected
+ return result.unit === expected ? result : { error: "unit_expected_mismatch", expected, found: result.unit, candidates: [result] }
+}
+
+function cellsOf(row) {
+ const cells = []
+ for (const cell of row.children.filter((child) => child.tag === "td" || child.tag === "th")) {
+ const span = Math.max(1, Number.parseInt(cell.attrs.colspan ?? "1", 10) || 1)
+ for (let k = 0; k < span; k++) cells.push(cell)
+ }
+ return cells
+}
+
+function rowsOf(table) {
+ const rows = []
+ const walk = (node) => {
+ for (const child of node.children) {
+ if (child.tag === "tr") rows.push(child)
+ else if (child.tag !== "table" && child.tag !== "#text") walk(child)
+ }
+ }
+ walk(table)
+ return rows
+}
+
+function tablesOf(root) {
+ const tables = []
+ const walk = (node) => {
+ for (const child of node.children) {
+ if (child.tag === "table") tables.push(child)
+ if (child.tag !== "#text") walk(child)
+ }
+ }
+ walk(root)
+ return tables.map((table) => {
+ const rows = rowsOf(table)
+ const header = rows.find((row) => row.children.some((child) => child.tag === "th")) ?? rows[0]
+ const caption = table.children.find((child) => child.tag === "caption")
+ return { table, header, rows: rows.filter((row) => row !== header), caption: caption ? label(proseText(caption)) : null }
+ })
+}
+
+const cellLabel = (cell) => label(proseText(cell))
+const indexesOf = (labels, wanted) => labels.flatMap((value, index) => (value === wanted ? [index] : []))
+
+/** --table/--row/--col: the table by id, caption, or a header label; row and column by normalized label, never by index. */
+export function locateTable(sourceHtml, { table, row, col, sourceId }) {
+ requireSource(sourceId)
+ const { root, text, blocks } = snapshotBlocks(sourceHtml)
+ const key = label(table)
+ const tables = tablesOf(root).filter((entry) => entry.table.attrs.id === table || entry.caption === key || (entry.header && cellsOf(entry.header).map(cellLabel).includes(key)))
+ if (tables.length !== 1) return tables.length === 0 ? notLocated() : ambiguous(tables.map((entry) => ({ table: entry.table.attrs.id ?? entry.caption })))
+ const [{ header, rows }] = tables
+ const headerCells = header ? cellsOf(header) : []
+ const columns = indexesOf(headerCells.map(cellLabel), label(col))
+ const matches = rows.filter((candidateRow) => cellsOf(candidateRow).length > 0 && cellLabel(cellsOf(candidateRow)[0]) === label(row))
+ if (columns.length > 1 || matches.length > 1) return ambiguous([...columns.map((index) => ({ col: index })), ...matches.map((match) => ({ row_line: match.line }))])
+ const cell = columns.length === 1 && matches.length === 1 ? cellsOf(matches[0])[columns[0]] : null
+ const block = cell ? blocks.find((entry) => entry.node === cell) : null
+ if (!block) return notLocated()
+ const holder = { sid: sentenceAt(indexSentences(text, sourceId), block.start)?.sid ?? null, start: block.start, end: block.end, text: text.slice(block.start, block.end) }
+ const numeral = parseNumeral(holder.text)
+ if (!numeral) return notLocated()
+ const headerUnit = /\(([^()]+)\)\s*$/.exec(cellLabel(headerCells[columns[0]]))
+ return quantityResult(text, sourceId, holder, numeral, { table, row_label: row, col_label: col }, headerUnit ? normalizeUnit(headerUnit[1]) : null)
+}
+
+/** --page/--line: exactly one quantity on that line of that page (pages split on a form feed). */
+export function locatePageLine(text, { page, line, sourceId }) {
+ requireSource(sourceId)
+ const pages = text.split("\f")
+ if (!Number.isInteger(page) || page < 1 || page > pages.length) return notLocated()
+ const lines = pages[page - 1].split("\n")
+ if (!Number.isInteger(line) || line < 1 || line > lines.length) return notLocated()
+ let start = pages.slice(0, page - 1).reduce((sum, body) => sum + body.length + 1, 0)
+ for (const body of lines.slice(0, line - 1)) start += body.length + 1
+ const holder = { start, end: start + lines[line - 1].length, text: lines[line - 1] }
+ const numerals = []
+ for (let numeral = parseNumeral(holder.text); numeral; numeral = parseNumeral(holder.text, numeral.span[1])) numerals.push(numeral)
+ const sentences = indexSentences(text, sourceId)
+ if (numerals.length !== 1) return numerals.length === 0 ? notLocated() : ambiguous(numerals.map((numeral) => candidate(text, sentences, start + numeral.span[0], start + numeral.span[1])))
+ holder.sid = sentenceAt(sentences, start + numerals[0].span[0])?.sid ?? null
+ return quantityResult(text, sourceId, holder, numerals[0])
+}
+
+/** Token-aligned windows of `sentence` whose similarity to `match` reaches the fuzzy floor; best first. */
+function fuzzyWindow(sentence, match) {
+ const target = normalizeText(match).length
+ const wanted = new Set(tokenize(match))
+ const present = new Set(tokenize(sentence.text))
+ if (wanted.size === 0 || [...wanted].filter((token) => present.has(token)).length / wanted.size < 0.5) return null
+ const bounds = [...sentence.text.matchAll(/[\p{L}\p{N}]+/gu)].map((m) => [m.index, m.index + m[0].length])
+ let best = null
+ for (let i = 0; i < bounds.length; i++) {
+ for (let j = i; j < bounds.length; j++) {
+ const window = sentence.text.slice(bounds[i][0], bounds[j][1])
+ if (window.length > target * 1.06 + 2) break
+ if (window.length < target * 0.94 - 2) continue
+ const score = similarity(window, match)
+ if (score >= FUZZY_FLOOR && (!best || score > best.score)) best = { score, start: sentence.start + bounds[i][0], end: sentence.start + bounds[j][1] }
+ }
+ }
+ return best
+}
+
+function quoteResult(text, sourceId, sentences, start, end, fuzzy) {
+ const first = sentenceAt(sentences, start)
+ const last = sentenceAt(sentences, end - 1) ?? first
+ return {
+ value: null,
+ unit: null,
+ raw: text.slice(start, end),
+ quote: text.slice(first.start, last.end),
+ value_span: null,
+ sentence_id: first.sid,
+ qualifier: null,
+ fuzzy,
+ locator: locatorFor(text, sourceId, first.start, last.end),
+ }
+}
+
+/** --match: a normalized exact match (fuzzy false), else the one window with similarity >= 0.95 (fuzzy true). */
+export function locateMatch(text, { match, sourceId }) {
+ requireSource(sourceId)
+ const sentences = indexSentences(text, sourceId)
+ const hits = findAll(text, match)
+ if (hits.length > 1) return ambiguous(hits.map((hit) => candidate(text, sentences, hit.start, hit.end)))
+ if (hits.length === 1) return quoteResult(text, sourceId, sentences, hits[0].start, hits[0].end, false)
+ const windows = sentences.map((sentence) => fuzzyWindow(sentence, match)).filter(Boolean)
+ if (windows.length > 1) return ambiguous(windows.map((window) => candidate(text, sentences, window.start, window.end)))
+ return windows.length === 1 ? quoteResult(text, sourceId, sentences, windows[0].start, windows[0].end, true) : notLocated()
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-pairs.mjs b/plugins/omo/skills/ulw-research/scripts/fact-pairs.mjs
new file mode 100644
index 00000000..bd7ac047
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-pairs.mjs
@@ -0,0 +1,187 @@
+// fact-pairs.mjs - (source, report sentence) pairs for the meaning verifier.
+//
+// extractPairs walks the prose sentences of a filled HTML report and emits, in document order:
+// a quote pair per distinct ledger quote cited by a findings sentence (facts sharing one quote
+// share one pair), a claim pair per sentence bound only by data-claim (the statement comes from
+// the claim index), and a translation pair per data-translates span. force_strong routes a pair
+// to the strong reader when the lexicon raised name_outside_quote or scope_widened_warn, when the
+// hedge-position heuristic fired, or when the source hedges and the sentence's last six tokens do
+// not. No model, no IO: the readers run as separate lanes. Plain Node ESM; no Hangul here.
+
+import { isExemptKind, proseSentences } from "./fidelity-binding.mjs"
+import { isKoreanTerm, lexicalPattern, lexiconChecks } from "./fidelity-lexicon.mjs"
+import { sentenceKey } from "./fidelity-state.mjs"
+import { buildContext } from "./gates-static.mjs"
+import { parseHtml, proseText } from "./html-lite.mjs"
+import { HEDGES } from "./lexicon.mjs"
+import { normalizeText } from "./text-normalize.mjs"
+
+/** Every pair kind the producers below emit; consumers count and schema pairs by this list. */
+export const PAIR_KINDS = Object.freeze(["quote", "claim", "translation"])
+const TAIL = 6
+
+const lang = (text) => (isKoreanTerm(text) ? "ko" : "en")
+/** Sentence text with its fact spans cut out, so a Korean value does not make an English sentence Korean. */
+const outsideFacts = (sentence) => sentence.facts.reduceRight((text, fact) => text.slice(0, fact.start - sentence.start) + text.slice(fact.end - sentence.start), sentence.text)
+
+/** Latin hedges match whole words ("exact"); Korean hedges match as substrings, one syllable as a token prefix ("stem"). */
+const HEDGE_RES = HEDGES.map((term) => lexicalPattern(term, isKoreanTerm(term) ? "stem" : "exact"))
+
+/** Hedge terms that are also month names; capitalized and followed by a date number, they are months. */
+const MONTH_WORDS = new Set(["may"])
+
+/** A capitalized Latin match after the first word is a name or a month ("in May"), not a hedge. */
+const isHedgeAt = (text, match) => {
+ if (!/^\p{Lu}/u.test(match[0]) || isKoreanTerm(match[0])) return true
+ if (match.index !== 0) return false
+ // At the start of a text a capital is only sentence case, so the word still hedges - unless it is a
+ // month name followed by a date number ("May 2024 sales rose.").
+ return !(MONTH_WORDS.has(match[0].toLowerCase()) && /^\s+\p{Nd}/u.test(text.slice(match.index + match[0].length)))
+}
+
+export const hasHedge = (text) => {
+ const normal = normalizeText(text)
+ return HEDGE_RES.some((re) => [...normal.matchAll(re)].some((match) => isHedgeAt(normal, match)))
+}
+
+/** The source hedges and none of the sentence's last six tokens carries a hedge. */
+function tailDropsHedge(source, sentence) {
+ if (!hasHedge(source)) return false
+ return !hasHedge(normalizeText(sentence).split(/\s+/u).filter(Boolean).slice(-TAIL).join(" "))
+}
+
+function slugOf(scope) {
+ const heading = scope.section?.heading
+ if (!heading) return ""
+ return (heading.attrs.id ?? proseText(heading))
+ .toLowerCase()
+ .replace(/[^\p{L}\p{N}]+/gu, "-")
+ .replace(/^-+|-+$/gu, "")
+}
+
+function claimMap(claims) {
+ const list = Array.isArray(claims) ? claims : (claims?.claims ?? [])
+ return new Map(list.map((claim) => [claim.claim_id, claim]))
+}
+
+const idsOf = (attr) => String(attr ?? "").split(/[\s,]+/u).filter(Boolean)
+
+/** Claim ids that bind a sentence: on the block or an ancestor, or on an inner element whose text the sentence holds. */
+function claimIdsFor(ctx, block, sentence) {
+ const { closest, descendants } = ctx.util
+ const up = block.owner.attrs["data-claim"] !== undefined ? block.owner : closest(block.owner, (node) => node.attrs?.["data-claim"] !== undefined)
+ const ids = up ? idsOf(up.attrs["data-claim"]) : []
+ const text = normalizeText(sentence.text)
+ for (const node of descendants(block.owner)) {
+ if (node.attrs?.["data-claim"] === undefined) continue
+ const inner = normalizeText(proseText(node))
+ if (inner !== "" && text.includes(inner)) ids.push(...idsOf(node.attrs["data-claim"]))
+ }
+ return [...new Set(ids)]
+}
+
+function exempt(ctx, node) {
+ return isExemptKind(node.attrs["data-kind"]) || Boolean(ctx.util.closest(node, (cur) => isExemptKind(cur.attrs?.["data-kind"])))
+}
+
+/** Lexicon routing for every fact span of one quote group in one sentence. */
+function lexiconRouted(group, sentence) {
+ const probes = group.facts.map((fact, index) => ({
+ pair_id: String(index),
+ quote: group.quote,
+ sentence: sentence.text,
+ span: [fact.start - sentence.start, fact.end - sentence.start],
+ qualifier: group.row.kind === "quote" ? null : (group.rows.get(fact.id)?.qualifier ?? null),
+ }))
+ return lexiconChecks(probes).forceStrong.length > 0
+}
+
+function quotePairs(sentence, rows, filled, slug, reportLang) {
+ const groups = new Map()
+ for (const fact of sentence.facts) {
+ const row = rows.get(fact.id)
+ if (!row?.quote || (filled && !filled.has(fact.id))) continue
+ const group = groups.get(row.quote) ?? { quote: row.quote, row, rows, facts: [], ids: [] }
+ group.facts.push(fact)
+ if (!group.ids.includes(fact.id)) group.ids.push(fact.id)
+ groups.set(row.quote, group)
+ }
+ const allIds = [...new Set(sentence.facts.map((fact) => fact.id))]
+ return [...groups.values()].map((group) => ({
+ kind: "quote",
+ sentence_key: sentenceKey(slug, allIds),
+ fact_ids: [...group.ids].sort(),
+ quote: group.quote,
+ sentence: sentence.text,
+ source_lang: lang(group.quote),
+ report_lang: reportLang ?? lang(outsideFacts(sentence)),
+ force_strong: lexiconRouted(group, sentence) || tailDropsHedge(group.quote, sentence.text),
+ }))
+}
+
+function claimPairs(ctx, block, sentence, byClaim, slug, reportLang) {
+ if (sentence.facts.length > 0 || exempt(ctx, block.owner)) return []
+ return claimIdsFor(ctx, block, sentence)
+ .map((id) => byClaim.get(id))
+ .filter(Boolean)
+ .map((claim) => ({
+ kind: "claim",
+ sentence_key: sentenceKey(slug, [claim.claim_id]),
+ claim_id: claim.claim_id,
+ statement: claim.statement,
+ sentence: sentence.text,
+ source_lang: lang(claim.statement),
+ report_lang: reportLang ?? lang(sentence.text),
+ force_strong: lexiconChecks([{ pair_id: "c", quote: claim.statement, sentence: sentence.text }]).forceStrong.length > 0 || tailDropsHedge(claim.statement, sentence.text),
+ }))
+}
+
+function translationPair(node, rows, filled, slug, reportLang) {
+ const id = node.attrs["data-translates"]
+ const row = rows.get(id)
+ const text = proseText(node)
+ if (!row?.quote || text === "" || (filled && !filled.has(id))) return null
+ return {
+ kind: "translation",
+ sentence_key: sentenceKey(slug, [id]),
+ fact_ids: [id],
+ quote: row.quote,
+ sentence: text,
+ source_lang: lang(row.quote),
+ report_lang: reportLang ?? lang(text),
+ force_strong: tailDropsHedge(row.quote, text),
+ }
+}
+
+/**
+ * @param {string} html the filled report
+ * @param {{ rows: object[] }} ledger
+ * @param {{ claims: object[] } | object[] | null} claims indexClaims(...) output or its claims array
+ * @param {{ fact_id: string }[]} [fillMap] fact fill's map; when given, only facts it filled are paired
+ * @returns {object[]} pairs with pair_id P1, P2, ... in document order
+ */
+export function extractPairs(html, ledger, claims, fillMap) {
+ const ctx = buildContext(parseHtml(html), {})
+ const rows = new Map((ledger?.rows ?? []).map((row) => [row.fact_id, row]))
+ const filled = Array.isArray(fillMap) ? new Set(fillMap.map((entry) => entry.fact_id)) : null
+ const byClaim = claimMap(claims)
+ const htmlLang = ctx.all.find((node) => node.tag === "html")?.attrs.lang
+ const reportLang = htmlLang ? htmlLang.toLowerCase().split("-")[0] : null
+ const out = []
+ const seenSpans = new Set()
+ for (const block of proseSentences(ctx)) {
+ const findings = Boolean(block.scope.section) && block.kind === null
+ if (!findings) continue
+ const slug = slugOf(block.scope)
+ for (const sentence of block.sentences) {
+ out.push(...quotePairs(sentence, rows, filled, slug, reportLang), ...claimPairs(ctx, block, sentence, byClaim, slug, reportLang))
+ }
+ for (const node of ctx.util.descendants(block.owner)) {
+ if (node.attrs?.["data-translates"] === undefined || seenSpans.has(node)) continue
+ seenSpans.add(node)
+ const pair = translationPair(node, rows, filled, slug, reportLang)
+ if (pair) out.push(pair)
+ }
+ }
+ return out.map((pair, index) => ({ pair_id: `P${index + 1}`, ...pair }))
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fact-recompute.mjs b/plugins/omo/skills/ulw-research/scripts/fact-recompute.mjs
new file mode 100644
index 00000000..298c333d
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fact-recompute.mjs
@@ -0,0 +1,89 @@
+// Iterative postorder verifier: one snapshot and one arithmetic evaluation per reachable node.
+import { compute } from "./fact-derive.mjs"
+import { parseExpr } from "./fact-expr.mjs"
+import { isLegacyDerived, kindRuleErrors, readFlaws, readRow } from "./fact-ledger-kinds.mjs"
+import { rowErrors } from "./fact-ledger-schema.mjs"
+import { refuse } from "./unit-algebra.mjs"
+import { refusal } from "./unit-errors.mjs"
+import { object, sameDeclared } from "./unit-metadata.mjs"
+import { expectedUnit, malformed, ruleOf, unitMismatch } from "./unit-rules.mjs"
+
+const derived = (row) => row.kind === "derived" || row.kind === "converted"
+const fail = (detail, shape, expected) => ({ ok: false, detail, shape, expected })
+function refuseLegacy(row) {
+ if (isLegacyDerived(row)) refuse(`${row.fact_id} was written before precision, compatibility, and context existed; regenerate it with derive`, undefined, "legacy derived row", "a derived row regenerated by derive")
+}
+
+function verifyRow(row, rows) {
+ const rule = ruleOf(row.operation)
+ const ast = rule.expression ? parseExpr(row.formula) : null
+ const context = { ...row.context, rationale: row.rationale }
+ const unit = expectedUnit(row.operation, rows, { ast, context })
+ const expected = { unit, ...compute(row.operation, rows, unit, ast, context, row.formula), derived_from: rows.map((r) => r.fact_id) }
+ if (row.unit !== unit) return fail(`${row.fact_id}: ${unitMismatch(row.operation, rows, row.unit, unit)}`, "result_unit", expected)
+ for (const field of Object.keys(expected)) {
+ if (!sameDeclared(row[field], expected[field])) return fail(`${row.fact_id}: stored ${field} differs from the recomputed result`, `${field}_mismatch`, expected)
+ }
+ return { ok: true, expected }
+}
+
+export function verifyGraph(ledger, roots, requireActive = false, override = null) {
+ try {
+ if (!object(ledger) || !Array.isArray(ledger.rows)) refuse("ledger rows must be an array")
+ const byId = new Map()
+ for (const r of ledger.rows) {
+ if (!object(r) || typeof r.fact_id !== "string" || byId.has(r.fact_id)) refuse("ledger rows need distinct fact ids", undefined, "identity", "distinct fact ids")
+ byId.set(r.fact_id, r)
+ }
+ if (override) byId.set(override.fact_id, override)
+ const states = new Map()
+ const snapshots = new Map()
+ const results = new Map()
+ const stack = roots.map((id) => ({ id, finish: false }))
+ while (stack.length) {
+ const { id, finish } = stack.pop()
+ if (states.get(id) === "done") continue
+ if (!finish) {
+ if (states.get(id) === "open") refuse(`${id}: operands form a cycle`, undefined, "cycle", "an acyclic dependency graph")
+ const original = byId.get(id)
+ if (!original) refuse(`no ledger row ${id}`, undefined, "missing_operand", "an existing fact id")
+ const row = { ...readRow(original) }
+ snapshots.set(id, row)
+ if (requireActive && row.status !== "active") refuse(`${id} is ${row.status}`, undefined, "inactive_dependency", "active dependencies at every depth")
+ const [flaw] = readFlaws(row)
+ if (flaw) refuse(`${id}: ${flaw.detail}`, undefined, flaw.shape, flaw.expected)
+ if (derived(row)) {
+ refuseLegacy(row)
+ const shape = malformed(row)
+ if (shape) refuse(`${id}: ${shape}`, undefined, "malformed_row", "the operation's registry row shape")
+ }
+ const errors = derived(row) ? kindRuleErrors(row, id) : rowErrors(row)
+ if (errors.length) refuse(errors.join("; "), undefined, "row_schema", "a canonical row matching its kind and operation")
+ if (row.exact === null || row.value === null) refuse(`${id} has no exact value to derive from`)
+ states.set(id, "open")
+ stack.push({ id, finish: true })
+ if (derived(row)) for (const dependency of row.operands) stack.push({ id: dependency, finish: false })
+ } else {
+ const row = snapshots.get(id)
+ if (derived(row)) {
+ const result = verifyRow(row, row.operands.map((ref) => snapshots.get(ref)))
+ if (!result.ok) return result
+ const errors = rowErrors(row)
+ if (errors.length) refuse(errors.join("; "), undefined, "row_schema", "a complete canonical row")
+ results.set(id, result)
+ }
+ states.set(id, "done")
+ }
+ }
+ return results.get(roots[0]) ?? { ok: true }
+ } catch (error) { return { ok: false, ...refusal(error) } }
+}
+
+export function recompute(ledger, row) {
+ try {
+ refuseLegacy(row)
+ const shape = malformed(row)
+ if (shape) refuse(shape, undefined, "malformed_row", "a valid derived or converted row")
+ return verifyGraph(ledger, [row.fact_id], false, row)
+ } catch (error) { return { ok: false, ...refusal(error) } }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-binding.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-binding.mjs
new file mode 100644
index 00000000..aef5735f
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-binding.mjs
@@ -0,0 +1,130 @@
+// fidelity-binding.mjs - sentence binding and claim-status checks of the fidelity gate.
+//
+// proseSentences walks every leaf prose element (p, li) outside figures, tables and page chrome,
+// keeps the offsets of each data-fact element inside its text, and splits it into sentences, so
+// every check sees which ledger rows a sentence cites. bindingChecks raises unbound_sentence (a
+// findings sentence with no fact, placeholder, citation, or data-claim), claim_status_invalid (a
+// data-claim id that is unknown, refuted or unresolved outside a Gaps or Contradictions section,
+// or high-risk and missing from verified-claims), and conflicting_readings (two active rows with
+// one value and unit but different periods or qualifiers cited in one sentence). Plain Node ESM.
+
+import { proseText } from "./html-lite.mjs"
+import { fromDecimal, eq } from "./rational.mjs"
+import { splitSentences } from "./sentence-split.mjs"
+import { normalizeUnit } from "./units.mjs"
+
+const SKIP = new Set(["code", "pre", "kbd", "samp", "script", "style", "svg", "template", "noscript"])
+const CONTAINERS = new Set(["figure", "table", "nav", "header", "footer"])
+/** data-kind values whose sentences need no binding (opinion and recommendation blocks). */
+export const EXEMPT_KINDS = Object.freeze(["opinion", "recommendation"])
+export const isExemptKind = (kind) => EXEMPT_KINDS.includes(kind)
+const DECIMAL = /^-?\d+(?:\.\d+)?$/
+/** Gaps and Contradictions headings (English, and the Korean gap/limit/contradiction/conflict words). */
+const GAPS_HEADING = /\b(?:gaps?|contradictions?|unresolved)\b|\uACF5\uBC31|\uD55C\uACC4|\uBAA8\uC21C|\uC0C1\uCDA9/i
+
+/** Canonical unit token, or the raw unit without spaces, lowercased; "" for none. */
+export function unitKey(unit) {
+ if (unit === null || unit === undefined) return ""
+ return normalizeUnit(String(unit)) ?? String(unit).replace(/\s+/g, "").toLowerCase()
+}
+
+/** Exact decimal equality; false when either side is not a plain decimal. */
+export function sameValue(a, b) {
+ const [x, y] = [String(a ?? "").replace(/,/g, ""), String(b ?? "").replace(/,/g, "")]
+ return DECIMAL.test(x) && DECIMAL.test(y) && eq(fromDecimal(x), fromDecimal(y))
+}
+
+function serialize(node, out) {
+ for (const child of node.children) {
+ if (child.tag === "#text") out.text += child.text
+ else if (child.tag === "br") out.text += " "
+ else if (SKIP.has(child.tag)) continue
+ else if (child.attrs["data-fact"] !== undefined) {
+ const text = proseText(child)
+ out.facts.push({ id: child.attrs["data-fact"], start: out.text.length, end: out.text.length + text.length, text, node: child })
+ out.text += text
+ } else serialize(child, out)
+ }
+ return out
+}
+
+/**
+ * Leaf prose elements with their sentences and the facts each sentence cites.
+ * @returns {{ owner: object, scope: object, kind: string|null, sentences: { text: string, start: number, end: number, facts: object[] }[] }[]}
+ */
+export function proseSentences(ctx) {
+ const { closest, descendants } = ctx.util
+ const blocks = []
+ for (const { node, scope } of ctx.elements) {
+ if (node.tag !== "p" && node.tag !== "li") continue
+ if (node.tag === "li" && descendants(node).some((child) => child.tag === "p" || child.tag === "li")) continue
+ if (closest(node, (cur) => CONTAINERS.has(cur.tag))) continue
+ const { text, facts } = serialize(node, { text: "", facts: [] })
+ const sentences = splitSentences(text).map((s) => ({ ...s, facts: facts.filter((f) => f.start < s.end && f.end > s.start) }))
+ blocks.push({ owner: node, scope, kind: ctx.kindOf(scope), sentences })
+ }
+ return blocks
+}
+
+function claimList(claims) {
+ const list = Array.isArray(claims) ? claims : (claims?.claims ?? [])
+ return new Map(list.map((claim) => [claim.claim_id, claim]))
+}
+
+const selfOrUp = (ctx, node, test) => test(node) || Boolean(ctx.util.closest(node, test))
+
+function claimDefects(ctx, claims) {
+ const byId = claimList(claims)
+ const out = []
+ for (const { node, scope } of ctx.elements) {
+ const attr = node.attrs["data-claim"]
+ if (attr === undefined) continue
+ const heading = scope.section?.heading
+ const inGaps = Boolean(heading) && GAPS_HEADING.test(proseText(heading))
+ for (const id of attr.split(/[\s,]+/).filter(Boolean)) {
+ const claim = byId.get(id)
+ let why = null
+ if (!claim) why = `data-claim ${id} is not in the claim index`
+ else if (/refuted|unresolved/i.test(claim.status) && !inGaps) why = `claim ${id} is ${claim.status} but asserted outside Gaps or Contradictions`
+ else if (!inGaps && /high/i.test(claim.risk) && !claim.in_verified) why = `high-risk claim ${id} is not in verified-claims`
+ if (why) out.push(ctx.defect("claim_status_invalid", node, why))
+ }
+ }
+ return out
+}
+
+function conflict(rows, facts) {
+ const cited = [...new Set(facts.map((f) => f.id))].map((id) => rows.get(id)).filter((row) => row && row.status === "active" && row.kind !== "quote")
+ for (let i = 0; i < cited.length; i++) {
+ for (let k = i + 1; k < cited.length; k++) {
+ const [a, b] = [cited[i], cited[k]]
+ if (!sameValue(a.value, b.value) || unitKey(a.unit) !== unitKey(b.unit)) continue
+ if (a.valid_at !== b.valid_at || a.qualifier !== b.qualifier) return [a, b]
+ }
+ }
+ return null
+}
+
+/**
+ * @param {object} ctx buildContext(...) from gates-static.mjs
+ * @param {{ claims: object[] } | object[] | null} claims indexClaims(...) output or its claims array
+ * @param {{ rows: object[] } | null} ledger
+ */
+export function bindingChecks(ctx, claims, ledger) {
+ const rows = new Map((ledger?.rows ?? []).map((row) => [row.fact_id, row]))
+ const out = []
+ for (const block of proseSentences(ctx)) {
+ const findings = Boolean(block.scope.section) && block.kind === null
+ const exempt = selfOrUp(ctx, block.owner, (cur) => isExemptKind(cur.attrs["data-kind"]) || cur.attrs["data-claim"] !== undefined)
+ for (const sentence of block.sentences) {
+ const bound = sentence.facts.length > 0 || ctx.util.citationMarker.test(sentence.text) || sentence.text.includes("{{")
+ if (findings && !exempt && !bound) out.push(ctx.defect("unbound_sentence", block.owner, `no fact, citation, or data-claim binds: "${sentence.text.slice(0, 80)}"`))
+ const pair = conflict(rows, sentence.facts)
+ if (pair) {
+ const [a, b] = pair
+ out.push(ctx.defect("conflicting_readings", block.owner, `${a.fact_id} (${a.valid_at ?? "no period"}) and ${b.fact_id} (${b.valid_at ?? "no period"}) share ${a.value} ${a.unit ?? ""} in one sentence`))
+ }
+ }
+ }
+ return [...out, ...claimDefects(ctx, claims)]
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-brief.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-brief.mjs
index a8333afa..baba39e2 100644
--- a/plugins/omo/skills/ulw-research/scripts/fidelity-brief.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-brief.mjs
@@ -109,6 +109,11 @@ export function isLocked(parsed) {
return FIDELITY_FIELDS.every((field) => Object.hasOwn(parsed.fields, field) && LOCKED_ANSWERED_BY.has(parsed.answered_by[field]))
}
+/** The brief's `meaning check` field turns the meaning verifier off only when it reads "off". */
+export function meaningCheckEnabled(parsed) {
+ return !/^off\b/iu.test(parsed?.fields?.["meaning check"] ?? "")
+}
+
function incompleteDefect(briefPath, parsed) {
const reasons = []
if (!parsed.present) reasons.push("no ## Fidelity block")
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-claims.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-claims.mjs
new file mode 100644
index 00000000..274dd58c
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-claims.mjs
@@ -0,0 +1,89 @@
+// fidelity-claims.mjs - the claim-kind checks of the deterministic fidelity gate.
+//
+// A mark (the data-claim-kind attribute) binds per cited fact: a data-fact element takes the kind
+// of its nearest mark (its own, else the closest marked ancestor), and only that kind is checked
+// against its ledger row (claim-kinds.mjs CLAIM_KINDS); a row that fails is the kind's defect
+// (unsupported_verification for `verified`). A fact under two marks of different kinds is a
+// claim_kind_mismatch, and a mark that owns no fact of its own kind (every fact under it cites
+// nothing or is shadowed by another kind) is its kind's defect: shadowing never satisfies a parent.
+// An unmarked fact citing a row neither fact nor quote accepts (an assumed row) is a
+// claim_kind_mismatch naming the mark it needs. A sentence that matches a kind's unmarked patterns warns (minor, verification_claim_unmarked)
+// unless every fact it cites is marked that kind; a sentence citing no fact warns unless its block
+// is marked. No model, no network.
+
+import { CLAIM_ATTRIBUTE, CLAIM_KINDS, claimSatisfied, unmarkedKinds, unmarkedRequires } from "./claim-kinds.mjs"
+import { proseSentences } from "./fidelity-binding.mjs"
+import { clip } from "./fidelity-trace.mjs"
+import { proseText } from "./html-lite.mjs"
+
+const marked = (node) => node.attrs?.[CLAIM_ATTRIBUTE] !== undefined
+const kindOf = (node) => node.attrs[CLAIM_ATTRIBUTE].trim()
+const isFact = (node) => node.attrs["data-fact"] !== undefined
+
+/** The marks over a node, nearest first: its own mark, then each marked ancestor. */
+function marksOver(node) {
+ const marks = []
+ for (let cur = node; cur; cur = cur.parent) if (marked(cur)) marks.push(cur)
+ return marks
+}
+
+const nearestKind = (node) => {
+ const [mark] = marksOver(node)
+ return mark ? kindOf(mark) : null
+}
+
+/** Whether `mark` binds `fact`: every mark from the fact up to `mark` names the same kind. */
+function binds(mark, fact) {
+ const marks = marksOver(fact)
+ const at = marks.indexOf(mark)
+ return at >= 0 && marks.slice(0, at + 1).every((cur) => kindOf(cur) === kindOf(mark))
+}
+
+/**
+ * @param {object} ctx buildContext(...) from gates-static.mjs
+ * @param {Map<string, object>} rows ledger rows by fact_id
+ * @param {(row: object) => string | undefined} provenanceOf meta.json kind of the snapshot holding the row's own locator artifact
+ * @param {(code: string, node: object, message: string) => void} flag
+ */
+export function claimChecks(ctx, rows, provenanceOf, flag) {
+ for (const node of ctx.all.filter(marked)) {
+ const name = kindOf(node)
+ const entry = CLAIM_KINDS[name]
+ if (!entry) {
+ flag("claim_kind_mismatch", node, `${CLAIM_ATTRIBUTE}="${name}" is not a claim kind (${Object.keys(CLAIM_KINDS).join(", ")})`)
+ continue
+ }
+ const cited = [node, ...ctx.util.descendants(node)].filter(isFact)
+ if (cited.some((fact) => binds(node, fact))) continue
+ const why = cited.length === 0 ? "cites no fact" : "cites no fact of its own (a nested mark of another kind never satisfies it)"
+ flag(entry.defect, node, `"${clip(proseText(node))}" is marked ${name} but ${why}`)
+ }
+ for (const fact of ctx.all.filter(isFact)) {
+ const [nearest, ...outer] = marksOver(fact)
+ const id = fact.attrs["data-fact"]
+ if (!nearest) {
+ const need = unmarkedRequires(rows.get(id))
+ const mark = need && `{{${CLAIM_KINDS[need].prefix}:${id}}} (${CLAIM_ATTRIBUTE}="${need}")`
+ if (need) flag("claim_kind_mismatch", fact, `${id} is a ${rows.get(id).kind} row cited with no mark; an unmarked numeral reads as a fact, so mark it ${mark}`)
+ continue
+ }
+ const name = kindOf(nearest)
+ const conflict = outer.find((mark) => kindOf(mark) !== name)
+ if (conflict) flag("claim_kind_mismatch", fact, `${id} is marked ${name} inside a ${kindOf(conflict)} mark; a fact carries one claim kind`)
+ const entry = CLAIM_KINDS[name]
+ const row = rows.get(id)
+ if (!entry || !row || claimSatisfied(name, row, provenanceOf(row))) continue
+ const source = entry.sourceKinds ? ` and not one whose own locator artifact sits in a ${entry.sourceKinds.join(" or ")} snapshot` : ""
+ flag(entry.defect, fact, `${row.fact_id} is marked ${name}, but it is a ${row.kind} row${source}`)
+ }
+ for (const block of proseSentences(ctx)) {
+ if (block.kind === "sources" || block.kind === "toc") continue
+ for (const sentence of block.sentences) {
+ for (const name of unmarkedKinds(sentence.text)) {
+ const covered = sentence.facts.length > 0 ? sentence.facts.every((fact) => nearestKind(fact.node) === name) : nearestKind(block.owner) === name
+ if (covered) continue
+ flag(CLAIM_KINDS[name].unmarked.code, block.owner, `"${clip(sentence.text)}" reads as a ${name} claim but cites a fact with no ${CLAIM_ATTRIBUTE}="${name}" mark`)
+ }
+ }
+ }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-lexicon.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-lexicon.mjs
new file mode 100644
index 00000000..bf4c9026
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-lexicon.mjs
@@ -0,0 +1,145 @@
+// fidelity-lexicon.mjs - closed-list lexical checks over (source quote, report sentence) pairs.
+//
+// lexiconChecks compares each pair with the lexicon.mjs lists: hedge_dropped, direction_flipped,
+// causality_added, qualifier_dropped (six-token window around the fact span; a :q rendering puts
+// the qualifier inside the span), scope_widened_warn, and the acronym-aware name_outside_quote.
+// The hedge-position heuristic never yields a code; it only sets force_strong for the meaning
+// verifier, as do name_outside_quote and scope_widened_warn. Latin terms match whole words (terms
+// of four or more letters also match their inflections); Korean terms match as substrings, single
+// syllables as tokens. No model, no IO. Plain Node ESM; Korean strings are \u escapes.
+
+import { CAUSAL, CONCESSIVE, DIRECTION, HEDGES, NEGATION, QUALIFIERS, SCOPE_NARROW_TO_BROAD } from "./lexicon.mjs"
+import { normalizeText } from "./text-normalize.mjs"
+import { normalizeUnit } from "./units.mjs"
+
+/** Ledger qualifier terms -> their surface forms in either language (closed mapping). */
+export const QUALIFIER_FORMS = Object.freeze({
+ upper_inclusive: ["up to", "at most", "\uCD5C\uB300", "\uC774\uD558", "\uAE4C\uC9C0"],
+ lower_inclusive: ["at least", "\uCD5C\uC18C", "\uC774\uC0C1"],
+ approximate: ["about", "approximately", "\uC57D", "\uB0B4\uC678"],
+ near: ["nearly"],
+ lower_exclusive: ["over", "more than", "\uCD08\uACFC"],
+ upper_exclusive: ["under", "less than", "\uBBF8\uB9CC"],
+})
+/** Irregular and synonym direction forms the stem match cannot reach ("rose", "reduced"). */
+const DIRECTION_FORMS = { up: ["rose", "risen", "rising", "grew", "grown", "gain"], down: ["reduc", "fell", "fallen", "falling", "drop", "lower"] }
+const KOREAN = /[\uAC00-\uD7A3]/
+/** True when the term holds a Hangul syllable (Korean terms match by the Korean rules below). */
+export const isKoreanTerm = (term) => KOREAN.test(term)
+/** The guarded hedge and qualifier vocabulary: the union of HEDGES, QUALIFIERS and every QUALIFIER_FORMS surface form. */
+export const guardedTerms = () => [...new Set([...HEDGES, ...QUALIFIERS, ...Object.values(QUALIFIER_FORMS).flat()])]
+const ACRONYM = /(?<![\p{L}\p{N}])[A-Z]{2,6}(?![\p{L}\p{N}])/gu
+const WINDOW = 6
+const cache = new Map()
+
+const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
+
+/**
+ * The lexical pattern engine. mode: "stem" (inflections, Korean one-syllable token prefix), "exact",
+ * "word", "suffix" (Korean attached to a value), "guard" (single Latin words of four or more letters
+ * take inflections, multiword terms never do; Korean terms match as plain substrings).
+ */
+export function lexicalPattern(term, mode) {
+ const key = `${mode}\u0001${term}`
+ if (cache.has(key)) return cache.get(key)
+ let source
+ if (!KOREAN.test(term)) {
+ const stems = term.length >= 4 && (mode === "stem" || (mode === "guard" && !term.includes(" ")))
+ source = `(?<![\\p{L}\\p{N}])${escape(term).replace(/ /g, "\\s+")}${stems ? "\\p{L}*" : ""}(?![\\p{L}\\p{N}])`
+ } else if (mode === "guard") source = escape(term)
+ else if (mode === "suffix") source = `(?<=^|[\\s\\p{P}\\p{N}A-Za-z])${escape(term)}(?=$|[\\s\\p{P}])`
+ else if ([...term].length >= 2 || mode === "word") source = escape(term)
+ else source = `(?<=^|[\\s\\p{P}])${escape(term)}${mode === "stem" ? "" : "(?=$|[\\s\\p{P}])"}`
+ const re = new RegExp(source, "giu")
+ cache.set(key, re)
+ return re
+}
+
+function positions(text, terms, mode) {
+ const out = []
+ for (const term of terms) for (const match of text.matchAll(lexicalPattern(term, mode))) out.push({ term, at: match.index })
+ return out.sort((a, b) => a.at - b.at)
+}
+
+const has = (text, terms, mode) => positions(text, terms, mode).length > 0
+const tokens = (text) => text.split(/\s+/).filter(Boolean)
+const stripQualifiers = (text) => QUALIFIERS.reduce((rest, term) => rest.replace(lexicalPattern(term, "exact"), " "), text)
+
+function directions(text) {
+ const rest = stripQualifiers(text)
+ return {
+ up: has(rest, DIRECTION.up, "stem") || has(rest, DIRECTION_FORMS.up, "stem"),
+ down: has(rest, DIRECTION.down, "stem") || has(rest, DIRECTION_FORMS.down, "stem"),
+ }
+}
+
+/** The fact span plus up to six tokens on each side, never crossing the sentence. */
+function qualifierWindow(sentence, span) {
+ const [start, end] = span
+ return [...tokens(sentence.slice(0, start)).slice(-WINDOW), sentence.slice(start, end), ...tokens(sentence.slice(end)).slice(0, WINDOW)].join(" ")
+}
+
+function qualifierDropped(pair) {
+ if (!pair.qualifier || !Array.isArray(pair.span)) return false
+ const forms = QUALIFIER_FORMS[pair.qualifier] ?? [pair.qualifier]
+ const window = qualifierWindow(pair.sentence, pair.span)
+ return !forms.some((form) => has(window, [form], KOREAN.test(form) ? "suffix" : "exact"))
+}
+
+/** A hedge before the sentence's last concessive while the quote hedges its own final clause. */
+function hedgeMoved(quote, sentence) {
+ const lastConcessive = (text) => positions(text, CONCESSIVE, "word").at(-1)?.at ?? -1
+ const sentenceHedge = positions(sentence, HEDGES, "stem")[0]?.at
+ const quoteHedge = positions(quote, HEDGES, "stem").at(-1)?.at
+ const sentenceConcessive = lastConcessive(sentence)
+ return sentenceHedge !== undefined && quoteHedge !== undefined && sentenceHedge < sentenceConcessive && quoteHedge > lastConcessive(quote)
+}
+
+function pairFindings(pair) {
+ const quote = normalizeText(pair.quote)
+ const sentence = normalizeText(pair.sentence)
+ const found = []
+ const add = (code, message) => found.push({ pair_id: pair.pair_id, code, message })
+ const dropped = qualifierDropped(pair)
+ if (dropped) add("qualifier_dropped", `the source bounds the value (${pair.qualifier}) and the sentence drops it`)
+ const sourceHedges = positions(quote, HEDGES, "stem")
+ const qualifierOnly = sourceHedges.every(({ term }) => QUALIFIERS.includes(term))
+ if (sourceHedges.length > 0 && !has(sentence, HEDGES, "stem") && !(dropped && qualifierOnly)) {
+ add("hedge_dropped", `the source hedges ("${sourceHedges[0].term}") and the sentence does not`)
+ }
+ const [from, to] = [directions(quote), directions(sentence)]
+ const negated = has(quote, NEGATION, "word") || has(sentence, NEGATION, "word")
+ if (!negated && ((from.up && !from.down && to.down && !to.up) || (from.down && !from.up && to.up && !to.down))) {
+ add("direction_flipped", `the source says ${from.up ? "up" : "down"} and the sentence says ${to.up ? "up" : "down"}`)
+ }
+ const causal = positions(sentence, CAUSAL, "word").find(({ term }) => !has(quote, [term], "word"))
+ if (causal) add("causality_added", `"${causal.term}" states a cause the source does not`)
+ const context = [quote, ...(pair.context ?? []).map(normalizeText)]
+ for (const [narrow, broad] of SCOPE_NARROW_TO_BROAD) {
+ if (has(quote, [narrow], "word") && has(sentence, [broad], "word") && !context.some((text) => has(text, [broad], "word"))) {
+ add("scope_widened_warn", `the source says "${narrow}" and the sentence says "${broad}"`)
+ }
+ }
+ const acronyms = pair.acronyms ?? {}
+ const quoteNames = new Set(quote.match(ACRONYM) ?? [])
+ const stray = (sentence.match(ACRONYM) ?? []).find((name) => !quoteNames.has(name) && !Object.hasOwn(acronyms, name) && !normalizeUnit(name))
+ if (stray) add("name_outside_quote", `"${stray}" is in neither the quote nor the source's acronym table`)
+ return found
+}
+
+/**
+ * @param {{ pair_id: string, quote: string, sentence: string, span?: [number, number] | null,
+ * qualifier?: string | null, acronyms?: Record<string, string>, context?: string[] }[]} pairs
+ * @returns {{ findings: { pair_id: string, code: string, message: string }[], forceStrong: string[] }}
+ */
+export function lexiconChecks(pairs) {
+ const findings = []
+ const forceStrong = []
+ for (const pair of pairs) {
+ const found = pairFindings(pair)
+ findings.push(...found)
+ const routed = found.some(({ code }) => code === "name_outside_quote" || code === "scope_widened_warn")
+ if (routed || hedgeMoved(normalizeText(pair.quote), normalizeText(pair.sentence))) forceStrong.push(pair.pair_id)
+ }
+ return { findings, forceStrong }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-records.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-records.mjs
new file mode 100644
index 00000000..ea612989
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-records.mjs
@@ -0,0 +1,163 @@
+// fidelity-records.mjs - the fidelity record schema and the delivery barrier of `outcome verify`
+// and `outcome finish`.
+//
+// The table and the schema are the spec for the barrier. Every fidelity record is written through
+// writeFidelityRecord, which stamps one envelope (RECORD_ENVELOPE: record_kind, artifact_sha256,
+// report, written_at, defects) plus the fields its kind declares, and read through
+// readFidelityRecord, which refuses a missing, unknown, or ill-typed field (fail closed).
+// FIDELITY_RECORDS holds every per-record rule as a column: where the records are (targets), when a
+// delivered session must carry one (requiredWhen), which envelope field binds it to the delivered
+// bytes (bindsTo), and which defects block delivery (blocking). The barrier is one loop over the
+// table with no record-specific branch; a new fidelity step adds a row here and nothing else.
+//
+// HTML/PDF/DOCX need a gate record or an explicitly logged not_run reason, even without a ledger.
+// That logged exemption covers deliveries this harness never gated: once a ledger or any session
+// fidelity record exists, the barrier refuses the unverified edition regardless of it, and the same
+// exemption is the only way a required record may be absent.
+
+import { existsSync, readFileSync } from "node:fs"
+import { join } from "node:path"
+
+import { CliError, readJsonFile, sha256Of, writeJsonAtomic } from "./cli-support.mjs"
+import { LEDGER_FILENAME } from "./fact-ledger.mjs"
+import { meaningCheckEnabled, requireLockedBrief } from "./fidelity-brief.mjs"
+import { blocksDelivery, deliveryBlockReason } from "./gates-fidelity.mjs"
+
+export const FIDELITY_RECORD = "fidelity-defects.json"
+export const MEANING_RECORD = "fidelity-meaning-defects.json"
+
+/** The fields every fidelity record carries, whatever its kind. */
+export const RECORD_ENVELOPE = Object.freeze(["record_kind", "artifact_sha256", "report", "written_at", "defects"])
+
+/** Formats that need a fidelity record even in a session with no ledger and no record. */
+const GATED_FORMATS = Object.freeze(["html", "pdf", "docx"])
+
+/** The meaning record is required unless the locked brief turns the check off; a malformed block cannot prove off. */
+function meaningRequired({ sessionDir }) {
+ const lock = requireLockedBrief(join(sessionDir, "brief.md"))
+ return lock.exitCode === 2 || (lock.locked && meaningCheckEnabled(lock.parsed))
+}
+
+const subjectOf = (row) => row.renderedFrom ?? row.path ?? null
+
+/** One record per delivered edition: the file its row names with --verified-by, bound to that edition's bytes. */
+const perEdition = (sessionDir, delivered) =>
+ delivered.map((row) => ({ path: row.verifiedBy ?? null, declared: row.verifiedBy !== undefined, format: row.format, at: `${row.format}: `, subjects: [subjectOf(row)].filter((subject) => subject !== null) }))
+
+/** One record per session, in the session directory, bound to any delivered edition's bytes. */
+const perSession = (file) => (sessionDir, delivered) => [{ path: join(sessionDir, file), declared: false, format: null, at: "", subjects: delivered.map(subjectOf).filter((subject) => subject !== null) }]
+
+/** Every fidelity record the barrier reads, in check order; each column is one rule the barrier applies. */
+export const FIDELITY_RECORDS = Object.freeze([
+ Object.freeze({
+ kind: "fidelity",
+ file: FIDELITY_RECORD,
+ step: "fidelity gate",
+ targets: perEdition,
+ requiredWhen: ({ hasFidelity }, target) => hasFidelity || GATED_FORMATS.includes(target.format),
+ remedy: (target) => `run fact verify on ${target.subjects[0] ?? "the edition"} and set it with --verified-by`,
+ bindsTo: "artifact_sha256",
+ blocking: blocksDelivery,
+ payload: Object.freeze(["command", "status", "exitCode", "format", "counts", "gatesRun"]),
+ }),
+ Object.freeze({
+ kind: "meaning",
+ file: MEANING_RECORD,
+ step: "meaning gate",
+ targets: perSession(MEANING_RECORD),
+ requiredWhen: meaningRequired,
+ remedy: () => "run fact pairs and fact meaning-check before delivery",
+ bindsTo: "artifact_sha256",
+ blocking: blocksDelivery,
+ payload: Object.freeze(["command", "status", "exitCode", "run_id", "pairs_sha256", "counts", "dropped", "unverified", "calibration_path", "calibration_written", "calibration_replaced", "promotion_eligibility"]),
+ }),
+])
+
+const entryOf = (kind) => {
+ const entry = FIDELITY_RECORDS.find((row) => row.kind === kind)
+ if (entry === undefined) throw new CliError(`unknown fidelity record kind ${kind}`)
+ return entry
+}
+
+/** Why a value is not a valid record of this kind, or null. */
+function envelopeProblem(record, entry) {
+ if (record === null || typeof record !== "object" || Array.isArray(record)) return "not a record object"
+ const missing = RECORD_ENVELOPE.filter((key) => !Object.hasOwn(record, key))
+ if (missing.length > 0) return `missing ${missing.join(", ")}`
+ const unknown = Object.keys(record).filter((key) => !RECORD_ENVELOPE.includes(key) && !entry.payload.includes(key))
+ if (unknown.length > 0) return `unknown field(s) ${unknown.join(", ")}`
+ if (record.record_kind !== entry.kind) return `record_kind ${JSON.stringify(record.record_kind)} is not ${entry.kind}`
+ if (typeof record.artifact_sha256 !== "string" || !/^[0-9a-f]{64}$/u.test(record.artifact_sha256)) return "artifact_sha256 is not a sha256 hex digest"
+ if (typeof record.report !== "string" || record.report === "") return "report is not a path"
+ if (typeof record.written_at !== "string" || Number.isNaN(Date.parse(record.written_at))) return "written_at is not an ISO time"
+ if (!Array.isArray(record.defects) || !record.defects.every((defect) => typeof defect?.code === "string")) return "defects is not a list of coded defects"
+ return null
+}
+
+/**
+ * Write one fidelity record of a kind in the table: the envelope plus that kind's payload fields.
+ * @param {string} path
+ * @param {string} kind a FIDELITY_RECORDS kind
+ * @param {{ artifact_sha256: string, report: string, defects: object[], [field: string]: unknown }} fields the envelope values plus the kind's payload
+ */
+export function writeFidelityRecord(path, kind, { artifact_sha256, report, defects, ...payload }, now = new Date()) {
+ const entry = entryOf(kind)
+ const record = { record_kind: kind, artifact_sha256, report, written_at: now.toISOString(), defects, ...payload }
+ const problem = envelopeProblem(record, entry)
+ if (problem !== null) throw new CliError(`refusing to write a malformed ${kind} record: ${problem}`)
+ writeJsonAtomic(path, record)
+ return record
+}
+
+/** Read one fidelity record of a kind; anything but a valid envelope is a CliError naming the path. */
+export function readFidelityRecord(path, kind) {
+ const entry = entryOf(kind)
+ let record
+ try {
+ record = readJsonFile(path)
+ } catch (error) {
+ throw new CliError(`${kind} record ${path} is malformed: ${error.message}`)
+ }
+ const problem = envelopeProblem(record, entry)
+ if (problem !== null) throw new CliError(`${kind} record ${path} is malformed: ${problem}`)
+ return record
+}
+
+const unresolved = (format, codes, path) => ({ code: "fidelity_unresolved", format, codes, message: codes.map((code) => deliveryBlockReason({ code })).join("; "), path })
+const hashOf = (path) => (existsSync(path) ? sha256Of(readFileSync(path)) : null)
+
+/** Barrier problems for `outcome verify` and `outcome finish`; empty when nothing blocks delivery. */
+export function fidelityProblems(sessionDir, manifest) {
+ const delivered = manifest.deliverables.filter((row) => row.status === "delivered")
+ const hasFidelity = existsSync(join(sessionDir, LEDGER_FILENAME)) || FIDELITY_RECORDS.some((entry) => existsSync(join(sessionDir, entry.file)))
+ const lastFidelity = manifest.gateHistory?.findLast((entry) => entry.gate === "fidelity")
+ const exempt = !hasFidelity && manifest.gates.fidelity === "not_run" && lastFidelity?.status === "not_run" && Boolean(lastFidelity.reason?.trim())
+ const context = { sessionDir, hasFidelity }
+ const problems = []
+ const counted = new Set()
+ for (const entry of FIDELITY_RECORDS) {
+ for (const target of entry.targets(sessionDir, delivered)) {
+ const refuse = (message, path = target.path) => problems.push({ code: "edition_unverified", format: target.format, message: `${target.at}${message}`, path })
+ if (target.path === null || !existsSync(target.path)) {
+ if (target.declared) refuse(`${entry.kind} record ${target.path} is missing`)
+ else if (delivered.length > 0 && !exempt && entry.requiredWhen(context, target)) refuse(`${entry.step} not run; ${entry.remedy(target)}`, target.path ?? target.subjects[0] ?? null)
+ continue
+ }
+ let record
+ try {
+ record = readFidelityRecord(target.path, entry.kind)
+ } catch (error) {
+ refuse(error.message)
+ continue
+ }
+ if (target.subjects.length > 0 && !target.subjects.some((subject) => hashOf(subject) === record[entry.bindsTo])) {
+ refuse(`${entry.kind} record ${target.path} is for a different edition than ${target.subjects.join(", ")}`)
+ }
+ if (counted.has(target.path)) continue
+ counted.add(target.path)
+ const codes = [...new Set(record.defects.filter(entry.blocking).map((defect) => defect.code))].sort()
+ if (codes.length > 0) problems.push(unresolved(target.format, codes, target.path))
+ }
+ }
+ return problems
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-state.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-state.mjs
index 902f6ccc..dfa88b41 100644
--- a/plugins/omo/skills/ulw-research/scripts/fidelity-state.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-state.mjs
@@ -1,6 +1,7 @@
// fidelity-state.mjs - per-sentence failure counts for the fidelity repair loop. A sentence is
// keyed by its section slug and the sorted fact ids it cites, so a reworded sentence that
-// cites the same facts keeps its count. node: builtins only.
+// cites the same facts keeps its count. The exact slug and id rules are in the comment above
+// slugify in fact-fill.mjs. node: builtins only.
import { existsSync } from "node:fs"
import { join } from "node:path"
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-trace.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-trace.mjs
new file mode 100644
index 00000000..b9331aee
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-trace.mjs
@@ -0,0 +1,85 @@
+// fidelity-trace.mjs - the numeral and quotation trace of the deterministic fidelity gate.
+//
+// Every numeral in prose and table cells outside sources, toc and chart figures (dates included)
+// must sit in a data-fact, data-translates, q or blockquote element, or at the site of a fill-map
+// entry; otherwise untraceable_number. Quoted runs with no Q row are quote_unbound, and leftover
+// {{...}} pointers unresolved_placeholder. No model, no network.
+//
+// Fill-map assumption (fact-fill.mjs fill(), #11141): format "html" wraps every value and quote in
+// <data data-fact> / <q data-fact> / <blockquote data-fact>, which this trace skips by element, so
+// it needs no fill map. Format "md" writes bare text, and each fillMap entry is
+// { fact_id, rendered, start, end } with start/end indexing the filled Markdown, not the HTML this
+// gate reads. fill() pushes entries in output order and the Markdown-to-HTML step keeps text order,
+// so each entry, in array order, is matched to the next untraced occurrence of its rendered text in
+// document order that is a whole numeral and reads as the entry's row there (fits: "1,237 million"
+// is not F2's 1,237), and strips that one occurrence only. A copy of the same text that no entry
+// accounts for stays in the scan; an entry whose text is gone strips nothing.
+
+import { NOT_CLAIMS } from "./numerals.mjs"
+
+/** Structural numerals: versions, figure/table/section references, times (NOT_CLAIMS 7-11), pages, citations, placeholders. */
+export const STRUCTURAL = [...NOT_CLAIMS.slice(7, 12), /\b(?:p|pp|page|pages)\.?\s*\d+(?:\s*[-\u2013]\s*\d+)?/gi, /\[(?:S|Source\s*)\d+\]|\[\^\d+\]/gi, /\{\{[^}]*\}\}/g]
+const NUMERAL = /\d+(?:[.,\-/:]\d+)*/g
+const TRACED = "\u0000"
+const QUOTE_RUNS = [/"([^"]+)"/g, /\u201C([^\u201C\u201D]+)\u201D/g, /\u300C([^\u300C\u300D]+)\u300D/g, /\u300E([^\u300E\u300F]+)\u300F/g]
+
+export const clip = (text) => String(text).replace(/\s+/g, " ").trim().slice(0, 80)
+
+function ownerTexts(ctx, charts) {
+ const owners = new Map()
+ for (const entry of ctx.texts) {
+ const kind = ctx.kindOf(entry.scope)
+ if (kind === "sources" || kind === "toc") continue
+ if (charts.has(ctx.util.closest(entry.node, (cur) => cur.tag === "figure"))) continue
+ const hidden = ctx.util.closest(entry.node, (cur) => cur.attrs["data-fact"] !== undefined || cur.attrs["data-translates"] !== undefined || cur.tag === "q" || cur.tag === "blockquote")
+ owners.set(entry.owner, (owners.get(entry.owner) ?? "") + (hidden ? ` ${TRACED} ` : entry.node.text))
+ }
+ return owners
+}
+
+function scanOwner(ctx, owner, raw, flag) {
+ let text = raw
+ if (/^h[1-6]$/.test(owner.tag)) text = text.replace(/^\s*\d+(?:[.-]\d+)*[.):]?\s+/, " ")
+ if (text.includes("{{")) flag("unresolved_placeholder", owner, `leftover placeholder: "${clip(text.slice(text.indexOf("{{")))}"`)
+ for (const re of QUOTE_RUNS) {
+ for (const match of text.matchAll(re)) {
+ const inner = match[1].replaceAll(TRACED, " ").trim()
+ const after = text.slice(match.index + match[0].length).trim().split(/\s+/).slice(0, 6).join(" ")
+ if (inner && (inner.split(/\s+/).length >= 3 || ctx.util.citationMarker.test(after))) flag("quote_unbound", owner, `direct quotation with no Q row: "${clip(match[0])}"`)
+ }
+ }
+ const rest = STRUCTURAL.reduce((acc, re) => acc.replace(re, " "), text)
+ for (const match of rest.matchAll(NUMERAL)) flag("untraceable_number", owner, `number "${match[0]}" has no ledger row: "${clip(rest.slice(Math.max(0, match.index - 30), match.index + 30))}"`)
+}
+
+/** Strip each fill-map entry at its own fill site: the next untraced occurrence after the previous entry's site. */
+function traceFills(owners, fillMap, fits) {
+ const keys = [...owners.keys()]
+ const texts = [...owners.values()]
+ let [index, from] = [0, 0]
+ for (const entry of fillMap ?? []) {
+ const { rendered } = entry
+ if (!rendered) continue
+ for (let i = index, at = from; i < texts.length; i += 1, at = 0) {
+ let hit = texts[i].indexOf(rendered, at)
+ const glued = (pos) => /\d/.test(texts[i][pos - 1] ?? "") || /\d/.test(texts[i][pos + rendered.length] ?? "")
+ while (hit !== -1 && (glued(hit) || !fits(entry, texts[i].slice(hit)))) hit = texts[i].indexOf(rendered, hit + 1)
+ if (hit === -1) continue
+ texts[i] = `${texts[i].slice(0, hit)}${TRACED}${texts[i].slice(hit + rendered.length)}`
+ ;[index, from] = [i, hit + TRACED.length]
+ break
+ }
+ }
+ return new Map(keys.map((key, i) => [key, texts[i]]))
+}
+
+/**
+ * @param {object} ctx buildContext(...) from gates-static.mjs
+ * @param {Map<object, unknown>} charts chart figures, whose numerals chart checks own
+ * @param {{ fact_id: string, rendered: string, start?: number, end?: number }[] | null} fillMap Markdown fill map, or null
+ * @param {(code: string, node: object, message: string) => void} flag
+ * @param {(entry: object, tail: string) => boolean} fits whether the text from a candidate site on reads as the entry's row
+ */
+export function traceChecks(ctx, charts, fillMap, flag, fits) {
+ for (const [owner, text] of traceFills(ownerTexts(ctx, charts), fillMap, fits)) scanOwner(ctx, owner, text, flag)
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/fidelity-values.mjs b/plugins/omo/skills/ulw-research/scripts/fidelity-values.mjs
new file mode 100644
index 00000000..7dbd828c
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/fidelity-values.mjs
@@ -0,0 +1,229 @@
+// fidelity-values.mjs - value, quote, and chart checks of the deterministic fidelity gate.
+//
+// Each data-fact element must match its ledger row, and each Q element the script's
+// reconstruction of its selection (report text never gets a fuzzy pass). Cited rows, table rows,
+// snapshots, and chart CSVs are rechecked; the numeral and quotation trace is fidelity-trace.mjs,
+// and the claim-kind checks (data-claim-kind, unsupported_verification) are fidelity-claims.mjs.
+// No model, no network.
+
+import { existsSync, readFileSync } from "node:fs"
+import { join, resolve, sep } from "node:path"
+
+import { sha256Of } from "./cli-support.mjs"
+import { sameValue, unitKey } from "./fidelity-binding.mjs"
+import { claimChecks } from "./fidelity-claims.mjs"
+import { STRUCTURAL, clip, traceChecks } from "./fidelity-trace.mjs"
+import { parseHtml, proseText } from "./html-lite.mjs"
+import { parseNumeral } from "./numerals.mjs"
+import { div, fromDecimal, sub } from "./rational.mjs"
+import { normalizeText } from "./text-normalize.mjs"
+
+const DECIMAL = /^-?\d+(?:\.\d+)?$/
+
+const bare = (text) => normalizeText(String(text ?? "").replace(/\s*\[(?:S|Source\s*)\d+\]\s*$/i, "")).replace(/\u2026/g, "...").replace(/\s*\.\.\.\s*/g, "...").replace(/^["']+|["']+$/g, "").trim()
+
+/** The text fill renders for a quote selection: full, lead=<n>, or from=<a>;to=<b> (placeholder form accepted). */
+export function selectQuote(quote, selector) {
+ const text = String(quote ?? "").trim()
+ if (selector === "full") return text
+ const lead = /^lead=(\d+)$/.exec(selector)
+ const words = text.split(/\s+/)
+ if (lead) return words.length <= Number(lead[1]) ? text : `${words.slice(0, Number(lead[1])).join(" ")}...`
+ const range = /^from=(.+);to=(.+)$/.exec(selector) ?? /^from="(.+)":to="(.+)"$/.exec(selector)
+ if (!range) return null
+ const once = (anchor) => text.indexOf(anchor) !== -1 && text.indexOf(anchor) === text.lastIndexOf(anchor)
+ if (!once(range[1]) || !once(range[2])) return null
+ const start = text.indexOf(range[1])
+ const end = text.indexOf(range[2]) + range[2].length
+ if (end <= start) return null
+ return `${start > 0 ? "..." : ""}${text.slice(start, end)}${end < text.length ? "..." : ""}`
+}
+
+function quoteProblem(row, shown, selector) {
+ const want = selectQuote(row.quote, selector)
+ if (want === null) return ["quote_not_verbatim", `selection "${selector}" does not resolve in ${row.fact_id}'s quote`]
+ return bare(shown) === bare(want) ? null : ["quote_not_verbatim", `"${clip(shown)}" is not the ${selector} selection of ${row.fact_id}: "${clip(want)}"`]
+}
+
+function factProblem(row, shown, attrs = {}) {
+ if (attrs["data-unit"] !== undefined && unitKey(attrs["data-unit"]) !== unitKey(row.unit)) return ["unit_mismatch", `data-unit "${attrs["data-unit"]}" but ${row.fact_id} is in "${row.unit}"`]
+ if (attrs.value !== undefined && !sameValue(attrs.value, row.value)) return ["value_mismatch", `value="${attrs.value}" but ${row.fact_id} is ${row.value}`]
+ const text = normalizeText(shown)
+ const forms = [row.raw, row.render, row.value, row.unit && `${row.value} ${row.unit}`].filter(Boolean).map(normalizeText)
+ if (forms.some((form) => text === form || (text.endsWith(` ${form}`) && !/\d/.test(text.slice(0, -form.length))))) return null
+ const parsed = parseNumeral(text)
+ if (!parsed) return unitKey(text) === unitKey(row.unit) ? null : ["value_mismatch", `"${clip(shown)}" shows no value of ${row.fact_id} (${row.value})`]
+ if (!sameValue(parsed.value, row.value)) return ["value_mismatch", `"${clip(shown)}" reads ${parsed.value}, but ${row.fact_id} is ${row.value}`]
+ if (parsed.unitRaw && unitKey(parsed.unitRaw) !== unitKey(row.unit)) return ["unit_mismatch", `"${clip(shown)}" is in ${parsed.unitRaw}, but ${row.fact_id} is in ${row.unit}`]
+ return null
+}
+
+/** Whether a candidate fill site reads as what fill wrote there or as the entry's row ("1,237 million" is neither 1,237 nor F2). */
+const fillSiteFits = (rows) => (entry, tail) => {
+ const [row, site, own] = [rows.get(entry.fact_id), parseNumeral(tail, 0), parseNumeral(entry.rendered, 0)]
+ if (!row || row.kind === "quote" || !site || !own) return true
+ if (sameValue(site.value, own.value) && unitKey(site.unitRaw ?? "") === unitKey(own.unitRaw ?? "")) return true
+ return factProblem(row, tail.slice(0, Math.max(entry.rendered.length, site.span[1]))) === null
+}
+
+const withoutFooter = (node) => proseText({ children: node.children.filter((child) => child.tag !== "footer") })
+
+/** One row per active value row, as `fact export --csv` writes it. */
+export function exportRows(ledger) {
+ return (ledger?.rows ?? []).filter((row) => row.status === "active" && row.kind !== "quote" && row.value !== null).map((row) => ({ fact_id: row.fact_id, label: row.label, plot_value: row.value, unit: row.unit ?? "" }))
+}
+
+function readCsv(path) {
+ if (!existsSync(path)) return null
+ const lines = readFileSync(path, "utf8").split(/\r?\n/).filter((line) => line.trim() !== "")
+ const cells = (line) => line.split(",").map((cell) => cell.trim().replace(/^"(.*)"$/, "$1"))
+ const head = cells(lines[0] ?? "")
+ const [id, value, unit] = ["fact_id", "plot_value", "unit"].map((name) => head.indexOf(name))
+ if (id < 0 || value < 0 || unit < 0 || lines.length < 2) return null
+ return lines.slice(1).map((line) => cells(line)).map((c) => ({ fact_id: c[id], plot_value: c[value], unit: c[unit] ?? "" }))
+}
+
+const walk = (node) => node.children.filter((child) => child.tag !== "#text").flatMap((child) => [child, ...walk(child)])
+
+/** SVG text nodes of a chart: data labels, or ticks with their declared grid. */
+function svgLabels(raw) {
+ return walk(parseHtml(raw)).filter((node) => node.tag === "text").map((node) => {
+ let tick = null
+ for (let cur = node.parent; cur && !tick; cur = cur.parent) if (cur.tag === "g" && /(?:^|\s)ticks(?:\s|$)/.test(cur.attrs.class ?? "")) tick = cur.attrs
+ return { text: proseText(node), tick }
+ })
+}
+
+function onGrid(value, tick) {
+ const [step, min, max] = [tick["data-tick-step"], tick["data-tick-min"], tick["data-tick-max"]]
+ if (![value, step, min, max].every((v) => DECIMAL.test(String(v ?? ""))) || /^-?0+(?:\.0+)?$/.test(step)) return false
+ const offset = sub(fromDecimal(value), fromDecimal(min))
+ return offset.num >= 0n && sub(fromDecimal(max), fromDecimal(value)).num >= 0n && div(offset, fromDecimal(step)).den === 1n
+}
+
+function numerals(text) {
+ const out = []
+ for (let at = 0, n = parseNumeral(text, 0); n; at = n.span[1], n = parseNumeral(text, at)) out.push(n.value)
+ return out
+}
+
+function chartFigures(ctx) {
+ const charts = new Map()
+ for (const fig of ctx.all.filter((node) => node.tag === "figure")) {
+ const inner = ctx.util.descendants(fig)
+ const labels = inner.filter((node) => node.tag === "svg").flatMap((svg) => svgLabels(svg.raw ?? ""))
+ const marked = fig.attrs["data-chart-source"] !== undefined || ctx.util.classTokens(fig).includes("chart")
+ if (marked || inner.some((node) => node.tag === "img" && ctx.util.classTokens(node).includes("chart")) || labels.some((label) => /\d/.test(label.text))) charts.set(fig, labels)
+ }
+ return charts
+}
+
+function chartProblem(ctx, fig, labels, ledger) {
+ const source = fig.attrs["data-chart-source"]
+ if (!source) return "chart figure has no data-chart-source"
+ const csv = readCsv(resolve(ctx.baseDir ?? ".", source))
+ if (!csv) return `chart data ${source} is missing or lacks fact_id, plot_value, unit columns`
+ const exported = new Map(exportRows(ledger).map((row) => [row.fact_id, row]))
+ const drift = csv.find((row) => exported.get(row.fact_id)?.plot_value !== row.plot_value || exported.get(row.fact_id)?.unit !== row.unit)
+ if (drift) return `chart data row ${drift.fact_id} differs from the ledger export`
+ const plotted = (value) => csv.some((row) => sameValue(row.plot_value, value))
+ for (const label of labels) {
+ const bad = numerals(label.text).find((value) => (label.tick ? !onGrid(value, label.tick) : !plotted(value)))
+ if (bad !== undefined) return `${label.tick ? "tick" : "label"} "${label.text}" is not ${label.tick ? "on the declared tick grid" : `a plot_value of ${source}`}`
+ }
+ const caption = ctx.util.descendants(fig).find((node) => node.tag === "figcaption")
+ const stray = numerals(caption ? STRUCTURAL.reduce((text, re) => text.replace(re, " "), proseText(caption)) : "").find((value) => !plotted(value))
+ return stray === undefined ? null : `caption numeral ${stray} is not a plot_value of ${source}`
+}
+
+/**
+ * @param {object} ctx buildContext(...) from gates-static.mjs
+ * @param {{ rows: object[] }} ledger
+ * @param {{ fact_id: string, rendered: string, selector?: string }[] | null} fillMap Markdown fill map, or null
+ * @param {string | null} sourcesDir directory of <S-id>/{text.txt, meta.json} snapshots
+ * @param {{ recompute?: (ledger: object, row: object) => { ok: boolean }, asOf?: string | null }} options
+ */
+export function valueChecks(ctx, ledger, fillMap, sourcesDir, options = {}) {
+ const out = []
+ const flag = (code, node, message) => out.push(ctx.defect(code, node, message))
+ const rows = new Map((ledger?.rows ?? []).map((row) => [row.fact_id, row]))
+ const cited = new Map()
+ const check = (id, node, problemOf) => {
+ const row = rows.get(id)
+ if (!row) return flag("unresolved_placeholder", node, `${id} names no ledger row`)
+ if (!cited.has(id)) cited.set(id, node)
+ const problem = problemOf(row)
+ if (problem) flag(problem[0], node, problem[1])
+ }
+ for (const node of ctx.all.filter((cur) => cur.attrs["data-fact"] !== undefined)) {
+ const selector = node.attrs["data-quote-sel"] ?? "full"
+ check(node.attrs["data-fact"], node, (row) => (row.kind === "quote" ? quoteProblem(row, withoutFooter(node), selector) : factProblem(row, proseText(node), node.attrs)))
+ }
+ const docText = bare(proseText(ctx.body))
+ for (const entry of fillMap ?? []) {
+ check(entry.fact_id, ctx.body, (row) => {
+ const problem = row.kind === "quote" ? quoteProblem(row, entry.rendered, entry.selector ?? "full") : factProblem(row, entry.rendered)
+ if (problem || docText.includes(bare(entry.rendered))) return problem
+ return [row.kind === "quote" ? "quote_not_verbatim" : "value_mismatch", `filled text "${clip(entry.rendered)}" of ${row.fact_id} is no longer in the report`]
+ })
+ }
+ rowChecks(ctx, ledger, rows, cited, options, flag)
+ for (const tr of ctx.all.filter((node) => node.tag === "tr" && node.attrs["data-row-kind"] !== "comparison")) {
+ const groups = new Set(ctx.util.descendants(tr).map((node) => rows.get(node.attrs["data-fact"])?.observation_id).filter(Boolean))
+ if (groups.size > 1) flag("join_integrity_fail", tr, `one table row joins observations ${[...groups].join(", ")}`)
+ }
+ const meta = sourceChecks(ctx, rows, cited, sourcesDir, flag)
+ claimChecks(ctx, rows, (row) => provenance(sourcesDir, meta, row), flag)
+ const charts = chartFigures(ctx)
+ for (const { node, scope } of ctx.elements) {
+ const kind = ctx.kindOf(scope)
+ if ((node.tag === "q" || node.tag === "blockquote") && node.attrs["data-fact"] === undefined && kind !== "sources" && kind !== "toc") flag("quote_unbound", node, `<${node.tag}> with no Q row: "${clip(proseText(node))}"`)
+ }
+ traceChecks(ctx, charts, fillMap, flag, fillSiteFits(rows))
+ for (const [fig, labels] of charts) {
+ const problem = chartProblem(ctx, fig, labels, ledger)
+ if (problem) flag("chart_value_untraceable", fig, problem)
+ }
+ return out
+}
+
+function rowChecks(ctx, ledger, rows, cited, options, flag) {
+ // An as-of of "latest ..." or "... per snapshot" names no fixed revision, so quote_stale_version is skipped.
+ const asOf = options.asOf && !/^latest\b|per snapshot/i.test(options.asOf) ? options.asOf : null
+ for (const [id, node] of cited) {
+ const row = rows.get(id)
+ if (row.fuzzy === true) flag("quote_fuzzy_match", node, `${id} was registered from a similarity match on an OCR-noisy snapshot`)
+ const revision = typeof row.revision === "string" ? row.revision : (row.revision?.id ?? row.revision?.tag ?? null)
+ if (asOf && revision && !asOf.includes(revision)) flag("quote_stale_version", node, `${id} is from revision ${revision}; the brief targets "${asOf}"`)
+ if ((row.kind === "derived" || row.kind === "converted") && options.recompute?.(ledger, row)?.ok === false) flag("value_mismatch", node, `${id} does not recompute from its operands`)
+ const window = row.snapshot_window
+ if (window && typeof window === "object" && window.start && window.end) {
+ const [start, end] = [Date.parse(window.start), Date.parse(window.end)]
+ const outside = (row.operands ?? []).find((op) => !(Date.parse(rows.get(op)?.observed_at) >= start && Date.parse(rows.get(op)?.observed_at) < end))
+ if (outside) flag("snapshot_mismatch", node, `operand ${outside} of ${id} was observed outside ${window.start} to ${window.end}`)
+ }
+ }
+}
+
+/** meta.json kind of the snapshot holding the row's own locator artifact; undefined when the artifact is absent or escapes sources/<source_id>/. */
+function provenance(sourcesDir, meta, row) {
+ const artifact = row.locator?.artifact
+ if (!sourcesDir || typeof row.source_id !== "string" || typeof artifact !== "string") return undefined
+ const dir = resolve(sourcesDir, row.source_id)
+ const path = resolve(dir, artifact)
+ return path.startsWith(`${dir}${sep}`) && existsSync(path) ? meta.get(row.source_id)?.kind : undefined
+}
+
+function sourceChecks(ctx, rows, cited, sourcesDir, flag) {
+ const ids = new Set([...cited.keys()].map((id) => rows.get(id)?.source_id).filter(Boolean))
+ for (const match of proseText(ctx.body).matchAll(/\[S(\d+)\]/g)) ids.add(`S${match[1]}`)
+ const meta = new Map()
+ for (const id of sourcesDir ? ids : []) {
+ const [metaPath, textPath] = [join(sourcesDir, id, "meta.json"), join(sourcesDir, id, "text.txt")]
+ if (!existsSync(metaPath) || !existsSync(textPath)) continue
+ meta.set(id, JSON.parse(readFileSync(metaPath, "utf8")))
+ const actual = sha256Of(readFileSync(textPath))
+ if (meta.get(id).sha256 !== actual) flag("snapshot_hash_mismatch", ctx.body, `${id}/text.txt changed after its snapshot was pinned`)
+ }
+ return meta
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/gates-fidelity.mjs b/plugins/omo/skills/ulw-research/scripts/gates-fidelity.mjs
new file mode 100644
index 00000000..5ff6cadc
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/gates-fidelity.mjs
@@ -0,0 +1,113 @@
+// gates-fidelity.mjs - the deterministic fidelity gate over a filled HTML report.
+//
+// checkFidelity refuses to judge a report whose brief has no locked `## Fidelity` block (one
+// fidelity_interview_incomplete defect). Otherwise it builds the static-gate document context
+// once and runs the value, binding, and lexicon checks over it, returning defects in the shape
+// `check` emits plus integrity, sorted blocker, major, minor, then line, selector, code. It never
+// calls a model or reads the network; the meaning verifier consumes its pairs separately.
+// Plain Node ESM, sibling imports only.
+
+import { existsSync, readFileSync } from "node:fs"
+import { join } from "node:path"
+
+import { DEFECT_CODES } from "./contracts.mjs"
+import { bindingChecks, proseSentences } from "./fidelity-binding.mjs"
+import { isLocked, parseFidelityBlock } from "./fidelity-brief.mjs"
+import { lexiconChecks } from "./fidelity-lexicon.mjs"
+import { valueChecks } from "./fidelity-values.mjs"
+import { buildContext } from "./gates-static.mjs"
+import { parseHtml } from "./html-lite.mjs"
+
+/** Every code this gate can raise. */
+export const FIDELITY_GATE_CODES = Object.freeze([
+ "fidelity_interview_incomplete", "unresolved_placeholder", "value_mismatch", "unit_mismatch", "quote_not_verbatim",
+ "quote_unbound", "claim_status_invalid", "snapshot_hash_mismatch", "chart_value_untraceable", "untraceable_number",
+ "unsupported_verification", "conflicting_readings", "hedge_dropped", "direction_flipped", "causality_added",
+ "qualifier_dropped", "unbound_sentence", "snapshot_mismatch", "join_integrity_fail", "quote_stale_version",
+ "quote_fuzzy_match", "name_outside_quote", "scope_widened_warn", "claim_kind_mismatch", "verification_claim_unmarked",
+])
+const RANK = { blocker: 0, major: 1, minor: 2 }
+
+/** The gate and delivery barrier share registry policy; caller severities cannot demote it. */
+export function blocksDelivery(defect) {
+ if (!Object.hasOwn(DEFECT_CODES, defect?.code)) return true
+ const entry = DEFECT_CODES[defect.code]
+ return entry.integrity || entry.severity !== "minor"
+}
+
+export function deliveryBlockReason(defect) {
+ return Object.hasOwn(DEFECT_CODES, defect?.code) ? `fidelity_unresolved: ${defect.code}` : `unknown defect code ${defect?.code}`
+}
+
+function withIntegrity(defect) {
+ const { code, severity, selector, line, message, hint } = defect
+ return { code, severity, integrity: DEFECT_CODES[code].integrity, selector, line, message, hint }
+}
+
+function compareDefects(a, b) {
+ const text = (x, y) => (x < y ? -1 : x > y ? 1 : 0)
+ return RANK[a.severity] - RANK[b.severity] || (a.line ?? 0) - (b.line ?? 0) || text(a.selector ?? "", b.selector ?? "") || text(a.code, b.code)
+}
+
+function readAcronyms(sourcesDir, sourceId, cache) {
+ if (!sourcesDir || !sourceId) return {}
+ if (!cache.has(sourceId)) {
+ const path = join(sourcesDir, sourceId, "acronyms.json")
+ cache.set(sourceId, existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : {})
+ }
+ return cache.get(sourceId)
+}
+
+/** One (row quote, report sentence) pair per fact cited in a prose sentence outside sources and toc. */
+export function fidelityPairs(ctx, ledger, sourcesDir) {
+ const rows = new Map((ledger?.rows ?? []).map((row) => [row.fact_id, row]))
+ const cache = new Map()
+ const pairs = []
+ for (const block of proseSentences(ctx)) {
+ if (block.kind === "sources" || block.kind === "toc") continue
+ block.sentences.forEach((sentence, index) => {
+ for (const fact of sentence.facts) {
+ const row = rows.get(fact.id)
+ if (!row?.quote) continue
+ pairs.push({
+ pair_id: `${fact.id}@${block.owner.line}:${index + 1}`,
+ fact_id: fact.id,
+ quote: row.quote,
+ sentence: sentence.text,
+ span: [fact.start - sentence.start, fact.end - sentence.start],
+ qualifier: row.kind === "quote" ? null : row.qualifier,
+ acronyms: readAcronyms(sourcesDir, row.source_id, cache),
+ node: block.owner,
+ })
+ }
+ })
+ }
+ return pairs
+}
+
+function incomplete(parsed) {
+ const reasons = !parsed?.present ? "no ## Fidelity block" : [...(parsed.errors ?? []), ...parsed.missing.map((f) => `missing: ${f}`), ...parsed.pending.map((f) => `pending: ${f}`)].join("; ")
+ const entry = DEFECT_CODES.fidelity_interview_incomplete
+ return { code: "fidelity_interview_incomplete", severity: entry.severity, integrity: entry.integrity, selector: null, line: null, message: `fidelity interview incomplete (${reasons})`, hint: entry.hint }
+}
+
+/**
+ * @param {string} html the filled report
+ * @param {{ ledger: { rows: object[] }, sourcesDir?: string | null, claims?: object | null, fillMap?: object[] | null,
+ * brief: string | object, baseDir?: string, recompute?: (ledger: object, row: object) => { ok: boolean } }} options
+ * `brief` is the brief.md text or parseFidelityBlock output; `baseDir` resolves data-chart-source paths.
+ * @returns {{ defects: object[], gatesRun: string[] }}
+ */
+export function checkFidelity(html, { ledger, sourcesDir = null, claims = null, fillMap = null, brief, baseDir, recompute } = {}) {
+ const parsed = brief && typeof brief === "object" ? brief : parseFidelityBlock(String(brief ?? ""))
+ if (!isLocked(parsed)) return { defects: [incomplete(parsed)], gatesRun: ["fidelity_interview_incomplete"] }
+ const ctx = buildContext(parseHtml(html), { baseDir })
+ const defects = [
+ ...valueChecks(ctx, ledger, fillMap, sourcesDir, { recompute, asOf: parsed.fields["as of"] }),
+ ...bindingChecks(ctx, claims, ledger),
+ ]
+ const pairs = fidelityPairs(ctx, ledger, sourcesDir)
+ const byId = new Map(pairs.map((pair) => [pair.pair_id, pair]))
+ for (const finding of lexiconChecks(pairs).findings) defects.push(ctx.defect(finding.code, byId.get(finding.pair_id).node, `${finding.pair_id}: ${finding.message}`))
+ return { defects: defects.map(withIntegrity).sort(compareDefects), gatesRun: [...FIDELITY_GATE_CODES] }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/gates-static.mjs b/plugins/omo/skills/ulw-research/scripts/gates-static.mjs
index 52eba46a..5a560b7e 100644
--- a/plugins/omo/skills/ulw-research/scripts/gates-static.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/gates-static.mjs
@@ -18,14 +18,19 @@ import { PROSE_TAGS, proseText, selectorPath } from "./html-lite.mjs"
const CITATION_MARKER = /\[S\d+\]|\[Source\s*\d+\]|\[\uCD9C\uCC98[^\]]*\]|\[\^\d+\]/i
const ANCHOR_HREF = /^#(?:s|src|source|ref|fn)/i
const ANCHOR_TEXT = /^\[(?:\uCD9C\uCC98|Source\s*\d+|S\d+)\]$/i
-/** Closing ("how this report was made") heading; Korean alternates cover the same register. */
-export const CLOSING_HEADING = /\b(?:method(?:ology)?|how)\b|\uB4B7\uBA74|\uBC29\uBC95|\uC5B4\uB5BB\uAC8C|\uB9CC\uB4E4\uC5B4/i
+/**
+ * Closing ("how this report was made") heading; Korean alternates cover the same register. A
+ * heading that is exactly "Closing" (or the Korean word for it) also counts; a heading that only
+ * starts with it ("Closing the gap") does not.
+ */
+export const CLOSING_HEADING = /\b(?:method(?:ology)?|how)\b|\uB4B7\uBA74|\uBC29\uBC95|\uC5B4\uB5BB\uAC8C|\uB9CC\uB4E4\uC5B4|^\s*(?:closing|\uB9C8\uBB34\uB9AC)\s*$/i
export const SOURCES_HEADING = /references|sources|bibliography|\uCD9C\uCC98|\uCC38\uACE0\s*(?:\uBB38\uD5CC|\uC790\uB8CC)/i
-const OTHER_KINDS = [
+/** Section kinds after sources and closing, in match order; HTML and Markdown share this table. */
+export const OTHER_KINDS = Object.freeze([
["toc", /\b(?:contents|toc)\b|\uBAA9\uCC28/i],
["appendix", /appendix|\uBD80\uB85D/i],
["glossary", /glossary|\uC6A9\uC5B4/i],
-]
+])
const TEXT_SKIP = new Set(["code", "pre", "kbd", "samp", "script", "style", "svg", "template", "noscript"])
const HEADING_TAG = /^h[1-6]$/
/** The one sanctioned severity override: section-level lineage demotes unsourced_number. */
@@ -75,9 +80,25 @@ function findBody(doc) {
return doc
}
-function sectionKind(text, closingTest) {
+/**
+ * Closing-heading test for both editions: CLOSING_HEADING plus the spec's optional pattern.
+ * @param {RegExp|string|null} [pattern]
+ * @returns {(text: string) => boolean}
+ */
+export function closingTest(pattern) {
+ const extra = pattern ? toRegExp(pattern) : null
+ return (text) => CLOSING_HEADING.test(text) || Boolean(extra?.test(text))
+}
+
+/**
+ * Section kind of an h2 heading text: sources, closing, toc, appendix, glossary, or null.
+ * @param {string} text
+ * @param {(text: string) => boolean} isClosing
+ * @returns {string|null}
+ */
+export function sectionKind(text, isClosing) {
if (SOURCES_HEADING.test(text)) return "sources"
- if (closingTest(text)) return "closing"
+ if (isClosing(text)) return "closing"
return OTHER_KINDS.find(([, re]) => re.test(text))?.[0] ?? null
}
@@ -111,7 +132,6 @@ function walkBody(ctx, node, state) {
export function buildContext(doc, options) {
const lineageMode = options.lineageMode ?? "inline"
if (!LINEAGE_MODES.includes(lineageMode)) throw new TypeError(`lineageMode must be one of ${LINEAGE_MODES.join(", ")}`)
- const closingPattern = options.closingPattern ? toRegExp(options.closingPattern) : null
const all = descendants(doc)
const styleBlocks = all.filter((node) => node.tag === "style").map((node) => ({ node, css: node.raw ?? "" }))
const styleAttrs = all.filter((node) => node.attrs.style).map((node) => ({ node, css: node.attrs.style }))
@@ -130,7 +150,7 @@ export function buildContext(doc, options) {
requireSections: (options.requireSections ?? []).map(toRegExp),
baseDir: options.baseDir ?? null,
fs: options.fs ?? nodeFs,
- isClosing: (text) => CLOSING_HEADING.test(text) || Boolean(closingPattern?.test(text)),
+ isClosing: closingTest(options.closingPattern),
util: { classTokens, closest, descendants, hasCitation, citationMarker: CITATION_MARKER },
kindOf: (scope) => (scope.closingH3 ? "closing" : (scope.section?.kind ?? null)),
defect(code, node, message) {
diff --git a/plugins/omo/skills/ulw-research/scripts/html-lite.mjs b/plugins/omo/skills/ulw-research/scripts/html-lite.mjs
index 64e64532..5a1d4541 100644
--- a/plugins/omo/skills/ulw-research/scripts/html-lite.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/html-lite.mjs
@@ -54,8 +54,8 @@ function skipSpaces(source, at) {
return at
}
-/** Read `<name attrs...>` starting just after `<`. */
-function readTag(source, start) {
+/** Read `<name attrs...>` starting just after `<`; the one attribute parser (any quoting). */
+export function readTag(source, start) {
const end = source.length
let at = start
while (at < end && !isSpace(source[at]) && source[at] !== ">" && source[at] !== "/") at++
diff --git a/plugins/omo/skills/ulw-research/scripts/html-markup.mjs b/plugins/omo/skills/ulw-research/scripts/html-markup.mjs
new file mode 100644
index 00000000..1dfb1a4a
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/html-markup.mjs
@@ -0,0 +1,182 @@
+// Byte-preserving HTML boundaries for polish. No CSS or visibility inference.
+import { readTag } from "./html-lite.mjs"
+
+const RAW = new Set(["script", "style"])
+const FROZEN = new Set([...RAW, "template"])
+const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"])
+const isLetter = (ch) => /[a-z]/i.test(ch ?? "")
+
+function scan(src, at, visit) {
+ while (at < src.length) {
+ const start = src.indexOf("<", at)
+ if (start === -1) return
+ const next = src[start + 1]
+ let kind
+ let end
+ let tag = null
+ if (src.startsWith("<!--", start)) {
+ // From start + 2 so the abrupt closes <!--> and <!---> end where the HTML tokenizer ends them.
+ kind = "skip"
+ const stop = src.indexOf("-->", start + 2)
+ end = stop === -1 ? src.length : stop + 3
+ } else if (next === "!" || next === "?" || (next === "/" && !isLetter(src[start + 2]))) {
+ // Declarations, <?...> and </ + non-letter (</> included) are bogus comments up to the next ">".
+ kind = "skip"
+ const stop = src.indexOf(">", start + 2)
+ end = stop === -1 ? src.length : stop + 1
+ } else if (isLetter(next) || (next === "/" && isLetter(src[start + 2]))) {
+ kind = next === "/" ? "close" : "open"
+ tag = readTag(src, start + (kind === "close" ? 2 : 1))
+ end = tag.end
+ } else {
+ at = start + 1
+ continue
+ }
+ const resume = visit(kind, start, end, tag)
+ if (resume === Infinity) return
+ at = resume ?? (kind === "open" && RAW.has(tag.name) ? closeOf(src, end, tag.name) : end)
+ }
+}
+
+// Script data with its escaped and double-escaped states: a </script> inside <!--<script>...-->
+// does not end the script. Returns the end of the closing tag, or the end of input.
+const SCRIPT_MARKS = { data: /<!--|<\/script(?=[\s/>])/gi, escaped: /-->|<\/?script(?=[\s/>])/gi, double: /-->|<\/script(?=[\s/>])/gi }
+function scriptEnd(src, from) {
+ let state = "data"
+ let at = from
+ for (;;) {
+ const marks = SCRIPT_MARKS[state]
+ marks.lastIndex = at
+ const match = marks.exec(src)
+ if (!match) return src.length
+ const token = match[0].toLowerCase()
+ if (token === "-->") [state, at] = ["data", match.index + 3]
+ else if (token === "<!--") [state, at] = ["escaped", match.index + 2]
+ else if (token === "<script") [state, at] = ["double", match.index + 7]
+ else if (state === "double") [state, at] = ["escaped", match.index + 8]
+ else return readTag(src, match.index + 2).end
+ }
+}
+
+function closeOf(src, from, name) {
+ if (name === "script") return scriptEnd(src, from)
+ if (RAW.has(name)) {
+ const close = new RegExp(`</${name}(?=[\\s/>])[^>]*>`, "gi")
+ close.lastIndex = from
+ const match = close.exec(src)
+ return match ? match.index + match[0].length : src.length
+ }
+ let depth = 0
+ let stop = src.length
+ scan(src, from, (kind, _start, end, tag) => {
+ if (tag?.name !== name) return undefined
+ if (kind === "open") depth++
+ else if (depth === 0) {
+ stop = end
+ return Infinity
+ } else depth--
+ return undefined
+ })
+ return stop
+}
+
+const endOf = (src, end, tag) => VOID.has(tag.name) ? end : closeOf(src, end, tag.name)
+
+/** Raw markup sequence, corresponding text slots, and markup with frozen elements omitted. */
+export function markupParts(html) {
+ const src = String(html)
+ const markup = []
+ const text = []
+ let source = ""
+ let kept = 0
+ scan(src, 0, (kind, start, end, tag) => {
+ const frozen = kind === "open" && FROZEN.has(tag.name)
+ const stop = frozen ? endOf(src, end, tag) : end
+ text.push(src.slice(kept, start))
+ markup.push(src.slice(start, stop))
+ source += src.slice(kept, start) + (frozen || kind === "skip" ? " " : src.slice(start, stop))
+ kept = stop
+ return stop
+ })
+ text.push(src.slice(kept))
+ source += src.slice(kept)
+ return { markup, text, source }
+}
+
+/** Whole elements accepted by the caller; nested matches stay within the outer match. */
+export function elementSpans(html, accept) {
+ const src = String(html)
+ const spans = []
+ scan(src, 0, (kind, start, end, tag) => {
+ if (kind !== "open" || !accept(tag.name, tag.attrs)) return undefined
+ const stop = endOf(src, end, tag)
+ spans.push({ start, end: stop, tag })
+ return stop
+ })
+ return spans
+}
+
+export function tagAttributes(html) {
+ const tags = []
+ scan(String(html), 0, (kind, _start, _end, tag) => {
+ if (kind === "open") tags.push({ name: tag.name, attrs: tag.attrs })
+ return undefined
+ })
+ return tags
+}
+
+/**
+ * Constructs this tokenizer does not model exactly; renderer output never contains them, so polish
+ * refuses a document holding any of them instead of trusting the markup comparison.
+ */
+export function ambiguousMarkup(html) {
+ const src = String(html)
+ const found = new Set()
+ if (/<\/(?![a-z])/i.test(src)) found.add("</ before a non-letter")
+ if (/<!\[CDATA\[/i.test(src)) found.add("<![CDATA[")
+ if (src.includes("<?")) found.add("<?")
+ if (src.includes("--!>")) found.add("--!>")
+ scan(src, 0, (kind, start, end, tag) => {
+ if (kind !== "skip" && src.slice(start + 1, end).includes("<")) found.add("< inside a tag or attribute value")
+ if (kind === "open" && RAW.has(tag.name) && src.slice(end, closeOf(src, end, tag.name)).includes("<!--")) found.add(`<!-- inside ${tag.name}`)
+ return undefined
+ })
+ return [...found]
+}
+
+/** One character reference token of a text node: & up to the first ; or non-name character. */
+export const CHARACTER_REFERENCE = /&[^;\s<&]*;?/g
+
+/**
+ * The skeleton of a document: every markup token (tags, comments, bogus comments and the frozen
+ * content of script/style/template) and every character reference of a text node, in document
+ * order, each with its byte offset. Polish may change text node characters only, and an edit can
+ * carry neither <, > nor & and may not touch a character reference, so an edited span holds no
+ * skeleton token: a polished document keeps this sequence byte-identical.
+ */
+export function skeletonTokens(html) {
+ const { markup, text } = markupParts(html)
+ const tokens = []
+ let at = 0
+ text.forEach((slot, index) => {
+ for (const match of slot.matchAll(CHARACTER_REFERENCE)) tokens.push({ kind: "reference", value: match[0], offset: at + match.index })
+ at += slot.length
+ if (index < markup.length) {
+ tokens.push({ kind: "markup", value: markup[index], offset: at })
+ at += markup[index].length
+ }
+ })
+ return tokens
+}
+
+const describeToken = (token) => (token ? `${token.kind} ${JSON.stringify(token.value)} at offset ${token.offset}` : "end of document")
+
+/** The first skeleton token two documents disagree on, as a message, or null when they agree. */
+export function skeletonChange(beforeHtml, afterHtml) {
+ const [before, after] = [skeletonTokens(beforeHtml), skeletonTokens(afterHtml)]
+ for (let index = 0; index < Math.max(before.length, after.length); index++) {
+ if (before[index]?.value === after[index]?.value) continue
+ return `skeleton changed at token ${index}: ${describeToken(before[index])} became ${describeToken(after[index])}`
+ }
+ return null
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/md-lite.mjs b/plugins/omo/skills/ulw-research/scripts/md-lite.mjs
new file mode 100644
index 00000000..b0490417
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/md-lite.mjs
@@ -0,0 +1,193 @@
+// md-lite.mjs - a light Markdown block model for md/post deliverables.
+//
+// parseMarkdown(text) returns { blocks } in document order, each block one of heading,
+// paragraph, list_item, table_row, code, blockquote, with start/end offsets into the raw text
+// so text.slice(start, end) === block.text (table cells carry their own offsets). Headings carry
+// level 1-6. Code blocks are fenced (``` or ~~~); an unterminated fence runs to the end of the
+// text and is reported in problems as unterminated_fence, which a caller must refuse rather than
+// skip. Callers skip kind 'code' when they look for numbers. sectionKindsMd(blocks) classifies
+// sections with the gates-static classifier itself (sectionKind, closingTest). It never renders
+// HTML. Plain Node ESM, sibling imports only.
+
+import { closingTest, sectionKind } from "./gates-static.mjs"
+
+export { closingTest, sectionKind }
+
+/** A fence opener: a backtick fence's info string holds no backtick (else it is an inline span). */
+const FENCE = /^ {0,3}(?:(`{3,})[^`]*|(~{3,}).*)$/
+const HEADING = /^ {0,3}(#{1,6})(?=[ \t]|$)/
+const BLOCKQUOTE = /^ {0,3}> ?/
+/** A bullet or an ordered marker (digits, then . or ), then a space); group 1 is the number. */
+const LIST_ITEM = /^[ \t]*(?:[-*+]|(\d{1,9})[.)])[ \t]+/
+const TABLE_SEPARATOR = /^[ \t]*\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/
+const CLOSING_HASHES = /[ \t]+#+[ \t]*$|^#+[ \t]*$/
+const THEMATIC_BREAK = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/
+
+/** The Markdown block grammar, one named pattern per syntax. */
+export const MD_BLOCK_PATTERNS = Object.freeze({ FENCE, HEADING, BLOCKQUOTE, LIST_ITEM, TABLE_SEPARATOR, CLOSING_HASHES, THEMATIC_BREAK })
+
+/** Split into lines with offsets; end excludes the newline and a trailing carriage return. */
+function linesOf(text) {
+ const lines = []
+ let start = 0
+ while (start <= text.length) {
+ const newline = text.indexOf("\n", start)
+ const next = newline === -1 ? text.length + 1 : newline + 1
+ let end = newline === -1 ? text.length : newline
+ if (end > start && text[end - 1] === "\r") end--
+ lines.push({ start, end, next, raw: text.slice(start, end) })
+ start = next
+ }
+ return lines
+}
+
+/** Narrow [start, end) to its non-whitespace core. */
+function trimRange(text, start, end) {
+ let from = start
+ let to = end
+ while (from < to && /\s/.test(text[from])) from++
+ while (to > from && /\s/.test(text[to - 1])) to--
+ return { start: from, end: to }
+}
+
+function block(text, kind, range, extra = {}) {
+ return { kind, ...extra, text: text.slice(range.start, range.end), start: range.start, end: range.end }
+}
+
+function isSeparator(line) {
+ return line !== undefined && line.raw.includes("-") && TABLE_SEPARATOR.test(line.raw)
+}
+
+/** Cells split on unescaped pipes, outer pipes dropped, each cell trimmed with offsets. */
+function tableCells(text, range) {
+ const bounds = []
+ for (let k = range.start; k < range.end; k++) {
+ let slashes = 0
+ while (k - slashes - 1 >= range.start && text[k - slashes - 1] === "\\") slashes++
+ if (text[k] === "|" && slashes % 2 === 0) bounds.push(k)
+ }
+ const edges = [range.start - 1, ...bounds, range.end]
+ if (bounds[0] === range.start) edges.shift()
+ if (bounds.at(-1) === range.end - 1 && bounds.length > 0) edges.pop()
+ const cells = []
+ for (let k = 0; k + 1 < edges.length; k++) {
+ const cell = trimRange(text, edges[k] + 1, edges[k + 1])
+ cells.push({ text: text.slice(cell.start, cell.end), start: cell.start, end: cell.end })
+ }
+ return cells
+}
+
+function headingBlock(text, line, marker) {
+ const after = line.start + line.raw.indexOf(marker) + marker.length
+ const range = trimRange(text, after, line.end)
+ const closing = text.slice(range.start, range.end).match(CLOSING_HASHES)
+ if (closing) range.end = trimRange(text, range.start, range.start + (closing.index ?? 0)).end
+ return block(text, "heading", range, { level: marker.length })
+}
+
+/**
+ * Parse Markdown into a flat block list.
+ * @param {string} text
+ * @returns {{ blocks: { kind: string, level?: number, text: string, start: number, end: number,
+ * cells?: { text: string, start: number, end: number }[] }[],
+ * problems: { code: "unterminated_fence", line: number, start: number }[] }}
+ */
+export function parseMarkdown(text) {
+ const source = String(text ?? "")
+ const lines = linesOf(source)
+ const blocks = []
+ const problems = []
+ let paragraph = null
+ let inTable = false
+ const flush = () => {
+ if (paragraph) blocks.push(block(source, "paragraph", paragraph))
+ paragraph = null
+ }
+ for (let i = 0; i < lines.length; i++) {
+ const line = lines[i]
+ const fence = line.raw.match(FENCE)
+ if (fence) {
+ flush()
+ inTable = false
+ const open = fence[1] ?? fence[2]
+ const codeStart = Math.min(line.next, source.length)
+ let close = lines.length
+ for (let k = i + 1; k < lines.length; k++) {
+ const candidate = lines[k].raw.trim()
+ if (candidate.startsWith(open[0].repeat(open.length)) && /^(`+|~+)$/.test(candidate)) {
+ close = k
+ break
+ }
+ }
+ let codeEnd = source.length
+ if (close === lines.length) problems.push({ code: "unterminated_fence", line: i + 1, start: line.start })
+ else {
+ codeEnd = Math.max(codeStart, lines[close].start - 1)
+ if (codeEnd > codeStart && source[codeEnd - 1] === "\r") codeEnd--
+ }
+ blocks.push(block(source, "code", { start: codeStart, end: codeEnd }))
+ i = close
+ continue
+ }
+ if (line.raw.trim() === "") {
+ flush()
+ inTable = false
+ continue
+ }
+ const range = trimRange(source, line.start, line.end)
+ if (!inTable && line.raw.includes("|") && isSeparator(lines[i + 1])) inTable = true
+ if (inTable && line.raw.includes("|")) {
+ flush()
+ if (!isSeparator(line)) blocks.push(block(source, "table_row", range, { cells: tableCells(source, range) }))
+ continue
+ }
+ inTable = false
+ const heading = line.raw.match(HEADING)
+ if (heading) {
+ flush()
+ blocks.push(headingBlock(source, line, heading[1]))
+ continue
+ }
+ if (THEMATIC_BREAK.test(line.raw)) {
+ flush()
+ continue
+ }
+ const marker = line.raw.match(BLOCKQUOTE) ?? line.raw.match(LIST_ITEM)
+ // Only an ordered marker numbered 1 may interrupt a paragraph (CommonMark); else it is text.
+ const continues = paragraph !== null && marker?.[1] !== undefined && Number(marker[1]) !== 1
+ if (marker && !continues) {
+ flush()
+ const kind = BLOCKQUOTE.test(line.raw) ? "blockquote" : "list_item"
+ const content = trimRange(source, line.start + marker[0].length, line.end)
+ if (content.end > content.start) blocks.push(block(source, kind, content))
+ continue
+ }
+ if (paragraph) paragraph.end = range.end
+ else paragraph = { start: range.start, end: range.end }
+ }
+ flush()
+ return { blocks, problems }
+}
+
+/**
+ * Section kind of every block, parallel to blocks: an h2 opens a section whose kind comes from
+ * its heading text (sources, closing, toc, appendix, glossary, or null for findings); an h3 that
+ * reads as a closing heading marks its blocks closing until the next h2 or h3, as in gates-static.
+ * @param {{ kind: string, level?: number, text: string }[]} blocks
+ * @param {{ closingPattern?: RegExp|string }} [options] extra closing heading pattern (the spec's)
+ * @returns {(string|null)[]}
+ */
+export function sectionKindsMd(blocks, options = {}) {
+ const isClosing = closingTest(options.closingPattern)
+ let section = null
+ let closingH3 = false
+ return blocks.map((entry) => {
+ if (entry.kind === "heading" && entry.level === 2) {
+ section = sectionKind(entry.text, isClosing)
+ closingH3 = false
+ } else if (entry.kind === "heading" && entry.level === 3) {
+ closingH3 = isClosing(entry.text)
+ }
+ return closingH3 ? "closing" : section
+ })
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/meaning-check.mjs b/plugins/omo/skills/ulw-research/scripts/meaning-check.mjs
new file mode 100644
index 00000000..41e61577
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/meaning-check.mjs
@@ -0,0 +1,196 @@
+// meaning-check.mjs - ingest the meaning readers' verdicts over fact-pairs output.
+//
+// ingestVerdicts reads verdict files written by separate reader lanes (cheap and strong tiers),
+// keeps a drift verdict only when its pair exists, its tier and drift_kind are in the closed sets,
+// and both its fragment (from the sentence) and source_fragment (from the quote or statement) are
+// non-empty substrings after whitespace normalization; every other verdict is dropped and logged.
+// Both tiers naming the same drift_kind (not "other") on a pair is meaning_drift_confirmed (major,
+// never integrity); one tier is meaning_drift_warn (minor). Each flagged pair yields one
+// calibration line; a dispute is recorded there and never removes the defect. Coverage is
+// fail-closed: every pair no reader judged, and every force_strong pair no strong reader judged,
+// is listed in `unverified`, so a gate can tell a clean check apart from a pair nobody read.
+// No model, no fetch.
+
+import { appendFileSync, readFileSync } from "node:fs"
+
+import { DEFECT_CODES } from "./contracts.mjs"
+
+export const DRIFT_KINDS = Object.freeze([
+ "subject_changed", "scope_widened", "hedge_dropped", "certainty_added", "causality_added",
+ "direction_flipped", "population_changed", "evidence_grade_raised", "other",
+])
+export const MODEL_TIERS = Object.freeze(["cheap", "strong"])
+export const VERDICTS = Object.freeze(["drift", "ok"])
+export const isDriftKind = (kind) => DRIFT_KINDS.includes(kind)
+/** The machine-read verdict contract a reader lane receives with pairs.json; built from the registries above. */
+export const VERDICT_SCHEMA = Object.freeze({
+ model_tier: MODEL_TIERS,
+ verdict: VERDICTS,
+ drift_kind: DRIFT_KINDS,
+ required: Object.freeze(["pair_id", "verdict"]),
+ drift_required: Object.freeze(["drift_kind", "fragment", "source_fragment"]),
+})
+
+/** NFC, every whitespace run collapsed to one space, trimmed. */
+const squash = (text) => String(text ?? "").normalize("NFC").replace(/\s+/gu, " ").trim()
+
+/** Read a verdict file, mapping an unreadable path or invalid JSON to a short typed error. */
+function parseFile(path) {
+ let text
+ try {
+ text = readFileSync(path, "utf8")
+ } catch {
+ throw new TypeError(`${path}: cannot read the verdict file`)
+ }
+ try {
+ return JSON.parse(text)
+ } catch {
+ throw new TypeError(`${path}: the verdict file is not valid JSON`)
+ }
+}
+
+function verdictsOf(input) {
+ const label = typeof input === "string" ? input : "<inline>"
+ const parsed = typeof input === "string" ? parseFile(input) : input
+ const list = Array.isArray(parsed) ? parsed : parsed?.verdicts
+ if (!Array.isArray(list)) throw new TypeError(`${label}: a verdict file is an array of verdicts or { verdicts: [...] }`)
+ const tier = Array.isArray(parsed) ? undefined : parsed.model_tier
+ return list.map((verdict) => ({ verdict: tier === undefined || verdict?.model_tier !== undefined ? verdict : { ...verdict, model_tier: tier }, file: label }))
+}
+
+/** The reason a verdict is dropped, or null when it is accepted. */
+function dropReason(verdict, pair) {
+ if (verdict === null || typeof verdict !== "object") return "malformed_verdict"
+ if (!pair) return "unknown_pair"
+ if (!VERDICTS.includes(verdict.verdict)) return "invalid_verdict"
+ if (!MODEL_TIERS.includes(verdict.model_tier)) return "invalid_model_tier"
+ if (verdict.verdict === "ok") return null
+ if (!isDriftKind(verdict.drift_kind)) return "unknown_drift_kind"
+ const [fragment, sourceFragment] = [squash(verdict.fragment), squash(verdict.source_fragment)]
+ if (fragment === "" || sourceFragment === "") return "empty_fragment"
+ if (!squash(pair.sentence).includes(fragment)) return "fragment_not_found"
+ const source = pair.kind === "claim" ? pair.statement : pair.quote
+ if (!squash(source).includes(sourceFragment)) return "source_fragment_not_found"
+ return null
+}
+
+function defect(code, pair, driftKind, tiers, verdicts) {
+ const entry = DEFECT_CODES[code]
+ // The fragment must come from the verdict whose drift_kind is being reported, not from whichever
+ // verdict happened to arrive first on the pair.
+ const first = verdicts.find((verdict) => verdict.drift_kind === driftKind) ?? verdicts[0]
+ return {
+ code,
+ severity: entry.severity,
+ integrity: entry.integrity,
+ pair_id: pair.pair_id,
+ sentence_key: pair.sentence_key ?? null,
+ drift_kind: driftKind,
+ tiers,
+ fragment: first.fragment,
+ source_fragment: first.source_fragment,
+ selector: null,
+ line: null,
+ message: `${pair.pair_id}: ${tiers.join(" and ")} reader${tiers.length > 1 ? "s" : ""} found ${driftKind} at "${squash(first.fragment)}"`,
+ hint: entry.hint,
+ }
+}
+
+/** One meaning_unverified defect per coverage gap, so blocksDelivery sees an unread pair through the registry. */
+export function unverifiedDefects(unverified) {
+ const entry = DEFECT_CODES.meaning_unverified
+ return unverified.map((gap) => ({
+ code: "meaning_unverified",
+ severity: entry.severity,
+ integrity: entry.integrity,
+ pair_id: gap.pair_id,
+ reason: gap.reason,
+ selector: null,
+ line: null,
+ message: `${gap.pair_id}: ${gap.reason === "no_strong_verdict" ? "no strong reader judged this force_strong pair" : "no reader judged this pair"}`,
+ hint: entry.hint,
+ }))
+}
+
+/** Pairs with no accepted verdict, and force_strong pairs with no accepted strong verdict, in pair order. */
+function coverageGaps(list, accepted) {
+ const tiersByPair = new Map()
+ for (const verdict of accepted) {
+ tiersByPair.set(verdict.pair_id, (tiersByPair.get(verdict.pair_id) ?? new Set()).add(verdict.model_tier))
+ }
+ const gaps = []
+ for (const pair of list) {
+ const tiers = tiersByPair.get(pair.pair_id)
+ if (tiers === undefined || tiers.size === 0) gaps.push({ pair_id: pair.pair_id, reason: "no_verdict" })
+ else if (pair.force_strong === true && !tiers.has("strong")) gaps.push({ pair_id: pair.pair_id, reason: "no_strong_verdict" })
+ }
+ return gaps
+}
+
+/** Group accepted drift verdicts per pair, then per tier. */
+function flaggedPairs(accepted) {
+ const byPair = new Map()
+ for (const verdict of accepted) {
+ if (verdict.verdict !== "drift") continue
+ const tiers = byPair.get(verdict.pair_id) ?? new Map()
+ tiers.set(verdict.model_tier, [...(tiers.get(verdict.model_tier) ?? []), verdict])
+ byPair.set(verdict.pair_id, tiers)
+ }
+ return byPair
+}
+
+/** The drift_kind both tiers named (never "other"), or null. */
+function agreedKind(tiers) {
+ const [cheap, strong] = MODEL_TIERS.map((tier) => tiers.get(tier) ?? [])
+ return cheap.map((verdict) => verdict.drift_kind).find((kind) => kind !== "other" && strong.some((verdict) => verdict.drift_kind === kind)) ?? null
+}
+
+/**
+ * @param {object[] | { pairs: object[] }} pairs extractPairs(...) output
+ * @param {(string | object[] | { verdicts: object[], model_tier?: string })[]} verdictFiles paths, or parsed verdict lists
+ * @param {{ dispute?: { pair_id: string, reason: string }, calibrationPath?: string }} [options]
+ * calibrationPath: append each calibration line to this JSONL file (fidelity-calibration.jsonl)
+ * @returns {{ defects: object[], dropped: object[], unverified: { pair_id: string, reason: string }[], calibrationLines: string[] }}
+ * unverified lists, in pair order, every pair no reader judged (`no_verdict`) and every
+ * force_strong pair no strong reader judged (`no_strong_verdict`); a verdict that was dropped
+ * does not count as read. A consumer MUST treat a non-empty list as blocking: the pairs were
+ * never checked, so an empty `defects` list does not mean the report is clean.
+ * @throws {TypeError} when pairs is neither an array nor `{ pairs: [...] }` (fail closed: a missing
+ * pairs input never reads as a fully verified report)
+ */
+export function ingestVerdicts(pairs, verdictFiles, options = {}) {
+ const list = Array.isArray(pairs) ? pairs : pairs?.pairs
+ if (!Array.isArray(list)) throw new TypeError("pairs: expected an array of pairs or { pairs: [...] }")
+ const byId = new Map(list.map((pair) => [pair.pair_id, pair]))
+ const accepted = []
+ const dropped = []
+ for (const input of verdictFiles) {
+ for (const { verdict, file } of verdictsOf(input)) {
+ const reason = dropReason(verdict, byId.get(verdict?.pair_id))
+ if (reason === null) accepted.push(verdict)
+ else dropped.push({ file, pair_id: verdict?.pair_id ?? null, model_tier: verdict?.model_tier ?? null, reason })
+ }
+ }
+ const dispute = options.dispute ?? null
+ const defects = []
+ const calibrationLines = []
+ const flagged = flaggedPairs(accepted)
+ for (const pair of list) {
+ const tiers = flagged.get(pair.pair_id)
+ if (!tiers) continue
+ const tierNames = MODEL_TIERS.filter((tier) => tiers.has(tier))
+ const agreed = agreedKind(tiers)
+ const verdicts = MODEL_TIERS.flatMap((tier) => tiers.get(tier) ?? [])
+ const kind = agreed ?? verdicts[0].drift_kind
+ const confirmed = agreed !== null
+ defects.push(defect(confirmed ? "meaning_drift_confirmed" : "meaning_drift_warn", pair, kind, tierNames, verdicts))
+ const disputed = dispute !== null && dispute.pair_id === pair.pair_id
+ calibrationLines.push(JSON.stringify({ kind: pair.kind, pair_id: pair.pair_id, tiers: tierNames, drift_kind: kind, confirmed, disputed, reason: disputed ? String(dispute.reason ?? "") : null }))
+ }
+ const unverified = coverageGaps(list, accepted)
+ if (dispute !== null && !flagged.has(dispute.pair_id)) {
+ dropped.push({ file: "<dispute>", pair_id: dispute.pair_id, model_tier: null, reason: "dispute_not_flagged" })
+ }
+ if (options.calibrationPath && calibrationLines.length > 0) appendFileSync(options.calibrationPath, `${calibrationLines.join("\n")}\n`)
+ return { defects, dropped, unverified, calibrationLines }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/outcome.mjs b/plugins/omo/skills/ulw-research/scripts/outcome.mjs
index ea688d54..8a901398 100644
--- a/plugins/omo/skills/ulw-research/scripts/outcome.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/outcome.mjs
@@ -1,6 +1,5 @@
-// outcome.mjs - the per-run outcome manifest (`outcome.json`) and the closing briefing.
-// Manifest functions are pure (they return a new manifest); the briefing reads only the
-// manifest, the sources-ledger text, and an injected `now`. node: builtins only.
+// outcome.mjs - the per-run outcome manifest (`outcome.json`) and the closing briefing. Manifest functions are pure (they return a new manifest).
+// The briefing reads only the manifest, the sources-ledger text, and an injected `now`. node: builtins only.
import * as nodeFs from "node:fs"
import { basename, dirname, join } from "node:path"
@@ -86,7 +85,7 @@ export function setDeliverable(manifest, format, status, detail = {}) {
const next = clone(manifest)
const index = next.deliverables.findIndex((row) => row.format === format)
const row = { format, status, promised: index >= 0 ? next.deliverables[index].promised : false, updatedAt: stamp(detail.now) }
- for (const key of ["path", "reason", "pages", "bytes"]) {
+ for (const key of ["path", "reason", "pages", "bytes", "verifiedBy", "renderedFrom"]) {
if (detail[key] !== undefined) row[key] = detail[key]
}
if (index >= 0) next.deliverables[index] = row
@@ -94,11 +93,13 @@ export function setDeliverable(manifest, format, status, detail = {}) {
return next
}
-export function setGate(manifest, gate, status) {
+export function setGate(manifest, gate, status, detail = {}) {
assertOneOf("unknown_gate", "gate", gate, GATE_NAMES)
assertOneOf("unknown_gate_status", "gate status", status, GATE_STATUSES)
+ if (gate === "fidelity" && status === "not_run" && (typeof detail.reason !== "string" || !detail.reason.trim())) throw new OutcomeError("gate_reason_required", "fidelity not_run requires a non-empty --reason")
const next = clone(manifest)
next.gates[gate] = status
+ next.gateHistory = [...(next.gateHistory ?? []), { gate, status, ...(detail.reason !== undefined ? { reason: detail.reason.trim() } : {}), at: stamp(detail.now) }]
return next
}
diff --git a/plugins/omo/skills/ulw-research/scripts/placeholder.mjs b/plugins/omo/skills/ulw-research/scripts/placeholder.mjs
new file mode 100644
index 00000000..bfd6f785
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/placeholder.mjs
@@ -0,0 +1,184 @@
+// placeholder.mjs - the fact pointer grammar of the fill step and its render helpers.
+// Value pointers: {{F12:label}}, {{F12:label:value}}, {{F12:label:unit}}, {{F12:label:q}}.
+// Quote pointers: {{Q7:label}}, {{Q7:label:lead=18}}, {{Q7:label:from="x":to="y"}} (anchors are
+// JSON strings, so they may hold a colon or an escaped quote). Labels are compared exactly by the
+// caller; this module never guesses an id. Pure functions; node: builtins only.
+
+import { parseNumeral } from "./numerals.mjs"
+
+export const ELLIPSIS = "\u2026"
+
+const ID = /^([FQ])\d+$/
+const LABEL = /^[^\s:{}"]+$/
+const VALUE_MODES = new Set(["value", "unit", "q"])
+const LEAD = /^lead=([1-9]\d*)$/
+const LINEAGE = Object.freeze({ measured: "MEASURED", assumed: "ASSUMED", derived: "DERIVED", converted: "DERIVED" })
+
+/** End index (exclusive) of a JSON string starting at `at`, or -1 when it is not closed on its line. */
+function jsonStringEnd(text, at) {
+ let i = at + 1
+ while (i < text.length && text[i] !== '"' && text[i] !== "\n") i += text[i] === "\\" ? 2 : 1
+ return text[i] === '"' ? i + 1 : -1
+}
+
+function readJsonString(text, at) {
+ if (text[at] !== '"') return null
+ const end = jsonStringEnd(text, at)
+ if (end === -1) return null
+ try {
+ return { value: JSON.parse(text.slice(at, end)), end }
+ } catch {
+ return null
+ }
+}
+
+function parseAnchors(tail) {
+ if (!tail.startsWith("from=")) return null
+ const from = readJsonString(tail, 5)
+ if (from === null || !tail.startsWith(":to=", from.end)) return null
+ const to = readJsonString(tail, from.end + 4)
+ if (to === null || to.end !== tail.length || from.value === "" || to.value === "") return null
+ return { from: from.value, to: to.value }
+}
+
+/** @returns the parsed pointer, or { error } for a malformed one. */
+export function parsePlaceholder(token) {
+ if (!token.startsWith("{{") || !token.endsWith("}}")) return { error: "a placeholder is {{ID:label...}}" }
+ const body = token.slice(2, -2)
+ const colon = body.indexOf(":")
+ const id = colon === -1 ? body : body.slice(0, colon)
+ const idMatch = ID.exec(id)
+ if (idMatch === null) return { error: `"${id}" is not an F<n> or Q<n> id` }
+ if (colon === -1) return { error: `${id} has no label; write {{${id}:label}}` }
+ const rest = body.slice(colon + 1)
+ const second = rest.indexOf(":")
+ const label = second === -1 ? rest : rest.slice(0, second)
+ if (!LABEL.test(label)) return { error: `"${label}" is not a label` }
+ const tail = second === -1 ? null : rest.slice(second + 1)
+ const base = { id, kind: idMatch[1], label }
+ if (idMatch[1] === "F") {
+ if (tail === null) return { ...base, mode: "full" }
+ return VALUE_MODES.has(tail) ? { ...base, mode: tail } : { error: `"${tail}" is not a value mode (value, unit, q)` }
+ }
+ if (tail === null) return { ...base, mode: "full", selector: "full" }
+ const lead = LEAD.exec(tail)
+ if (lead !== null) return { ...base, mode: "lead", lead: Number(lead[1]), selector: `lead=${lead[1]}` }
+ const anchors = parseAnchors(tail)
+ if (anchors === null) return { error: `"${tail}" is not a quote selector (lead=N or from="..":to="..")` }
+ return { ...base, mode: "span", ...anchors, selector: `from=${anchors.from};to=${anchors.to}` }
+}
+
+/** End index (exclusive) of the pointer opened at `at`, or -1 when it is not closed on its line. */
+function placeholderEnd(text, at) {
+ let i = at + 2
+ while (i < text.length && text[i] !== "\n") {
+ if (text[i] === '"') {
+ const end = jsonStringEnd(text, i)
+ if (end === -1) return -1
+ i = end
+ } else if (text.startsWith("}}", i)) return i + 2
+ else i++
+ }
+ return -1
+}
+
+/** Every `{{...}}` pointer in source order: [{ token, start, end, parsed }]. */
+export function findPlaceholders(text) {
+ const found = []
+ let at = text.indexOf("{{")
+ while (at !== -1) {
+ const end = placeholderEnd(text, at)
+ if (end === -1) {
+ found.push({ token: text.slice(at, text.indexOf("\n", at) === -1 ? text.length : text.indexOf("\n", at)), start: at, end: at + 2, parsed: { error: "the placeholder is not closed on its line" } })
+ at = text.indexOf("{{", at + 2)
+ continue
+ }
+ const token = text.slice(at, end)
+ found.push({ token, start: at, end, parsed: parsePlaceholder(token) })
+ at = text.indexOf("{{", end)
+ }
+ return found
+}
+
+function occurrences(text, needle) {
+ let count = 0
+ for (let at = text.indexOf(needle); at !== -1; at = text.indexOf(needle, at + 1)) count++
+ return count
+}
+
+function cut(quote, start, end) {
+ return { text: `${start > 0 ? ELLIPSIS : ""}${quote.slice(start, end)}${end < quote.length ? ELLIPSIS : ""}`, start, end }
+}
+
+/** The contiguous span from `from` through `to`; each anchor must occur exactly once, in order. */
+export function subSpan(quote, from, to) {
+ if (occurrences(quote, from) !== 1 || occurrences(quote, to) !== 1) return null
+ const start = quote.indexOf(from)
+ const toAt = quote.indexOf(to)
+ if (toAt < start || toAt + to.length < start + from.length) return null
+ return cut(quote, start, toAt + to.length)
+}
+
+/** The first `n` whitespace-delimited words, with an ellipsis when words were cut. */
+export function lead(quote, n) {
+ const words = [...quote.matchAll(/\S+/g)]
+ if (words.length <= n) return { text: quote, start: 0, end: quote.length }
+ const last = words[n - 1]
+ return cut(quote, 0, last.index + last[0].length)
+}
+
+/** The value and unit parts of a row's raw text ("4.2 eok won" -> value "4.2 eok", unit "won"). */
+export function valueParts(row) {
+ const unitText = parseNumeral(row.raw)?.unitRaw ?? row.unit
+ const valueText = unitText && row.raw.endsWith(unitText) ? row.raw.slice(0, -unitText.length).trimEnd() : (row.value ?? row.raw)
+ return { valueText, unitText: unitText ?? "" }
+}
+
+/** The quote interval holding the row's qualifier and its raw quantity, or null if absent. */
+export function qualifiedText(row) {
+ if (row.qualifier === null) return row.raw
+ for (let numeral = parseNumeral(row.quote); numeral !== null; numeral = parseNumeral(row.quote, numeral.span[1])) {
+ if (numeral.raw !== row.raw || numeral.qualifierRaw === null) continue
+ const [start, end] = numeral.span
+ const before = row.quote.lastIndexOf(numeral.qualifierRaw, start)
+ const between = before === -1 ? [] : row.quote.slice(before + numeral.qualifierRaw.length, start).split(/\s+/u).filter(Boolean)
+ if (before !== -1 && between.length <= 1) return row.quote.slice(before, end)
+ const after = row.quote.indexOf(numeral.qualifierRaw, end)
+ if (after !== -1) return row.quote.slice(start, after + numeral.qualifierRaw.length)
+ }
+ return null
+}
+
+export function escapeHtml(text) {
+ return String(text).replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;")
+}
+
+/** The elements the renderers below emit as fact spans; recognizers build their patterns from this. */
+export const FACT_ELEMENT_TAGS = Object.freeze(["data", "q", "blockquote"])
+
+export function renderValueHtml(row, text) {
+ const unit = row.unit === null ? "" : ` data-unit="${escapeHtml(row.unit)}"`
+ return `<data class="fact" value="${escapeHtml(row.value)}" data-fact="${row.fact_id}"${unit} data-lineage="${LINEAGE[row.kind]}">${escapeHtml(text)}</data>`
+}
+
+export function renderQuote(row, selector, text, format) {
+ const marker = `[${row.source_id}]`
+ if (format === "md") return `"${text}" ${marker}`
+ return `<q data-fact="${row.fact_id}" data-quote-sel="${escapeHtml(selector)}">${escapeHtml(text)}</q> ${marker}`
+}
+
+const RENDERED_MD_QUOTE = /^"([\s\S]*)" (\[S\d+\])$/u
+
+/** The quote text and source marker of bytes renderQuote wrote for md, or null for any other layout. */
+export function parseRenderedQuote(body) {
+ const match = RENDERED_MD_QUOTE.exec(body)
+ return match ? { text: match[1], marker: match[2] } : null
+}
+
+/** The attributed block that replaces a twice-failed sentence; entity and valid_at stay outside the quote. */
+export function renderBlockquote(row, key, format) {
+ const attribution = row.entity === null ? "" : `, on ${row.entity}${row.valid_at === null ? "" : ` (${row.valid_at})`}`
+ const footer = `[${row.source_id}]${attribution}`
+ if (format === "md") return `> "${row.quote}"\n> ${footer}`
+ return `<blockquote data-fact="${row.fact_id}" data-replaced="${escapeHtml(key)}">"${escapeHtml(row.quote)}" <footer>${escapeHtml(footer)}</footer></blockquote>`
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/polish-apply.mjs b/plugins/omo/skills/ulw-research/scripts/polish-apply.mjs
new file mode 100644
index 00000000..3fa084a4
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/polish-apply.mjs
@@ -0,0 +1,115 @@
+// polish-apply.mjs - checked, per-edit application of a proofread lane's polish edits.
+//
+// An edit whose before or after holds <, > or & is polish_edit_rejected (POLISH_MARKUP_REASON)
+// before anything else, so no edit can create, alter, close or escape a tag, comment,
+// bogus comment, CDATA section, entity or script, whatever the tokenizer models; a located region
+// that overlaps a character reference or opens one (it starts inside `&name`) is rejected with
+// POLISH_REFERENCE_REASON, so an edit can neither change nor complete an existing reference.
+// Those two guards are the early, actionable refusals; the guarantee itself is the whole-document
+// skeleton comparison (html-markup.skeletonChange) that polish-rules runs in both gates: every
+// markup token and every character reference stays byte-identical, and an accepted edit's span
+// holds no skeleton token, so any difference refuses the edit and reports polish_diff_fail.
+// applyEdits(html, edits, ledger) applies each {before, after, reason} edit in edits-file order,
+// against the output of the edits accepted before it. `before` must occur exactly once (runs of
+// whitespace match any whitespace run) and must not cut through a fact span; otherwise the edit is
+// polish_edit_unlocated. A located edit is applied whole or not at all: it is polish_edit_rejected
+// when polish-rules.polishDefects(..., {ordered: true}) finds any defect between the current
+// document and the document with the edit (per edit), or between the ORIGINAL input and the
+// document with the edit (whole document: the baseline polish-diff uses). Ordered is stricter than
+// polish-diff's multiset comparison and the last accepted candidate is the output, so a clean run
+// is always polish-diff clean: no cross-edit case is left to the backstop. The rules see the
+// text-node projection and freeze every markup byte, including script/style/template content.
+// validatePolishEdits and POLISH_EDIT_FIELDS are the edit
+// contract every caller (the CLI included) uses. No IO, no model. Plain Node ESM.
+
+import { CHARACTER_REFERENCE } from "./html-markup.mjs"
+import { factSpans } from "./polish-project.mjs"
+import { polishDefects } from "./polish-rules.mjs"
+
+export { numeralKeys, project, SLOT, termCounts, textOf } from "./polish-project.mjs"
+
+/** The fields of one polish edit: before and after are required strings, reason an optional string. */
+export const POLISH_EDIT_FIELDS = Object.freeze({ before: "required", after: "required", reason: "optional" })
+
+/** Throws a TypeError naming the first edit that breaks POLISH_EDIT_FIELDS; returns the edits. */
+export function validatePolishEdits(edits) {
+ if (!Array.isArray(edits)) throw new TypeError("edits must be an array")
+ edits.forEach((edit, index) => {
+ if (edit === null || typeof edit !== "object" || Array.isArray(edit)) throw new TypeError(`edit ${index} must be an object`)
+ for (const [field, need] of Object.entries(POLISH_EDIT_FIELDS)) {
+ if (need === "optional" && edit[field] === undefined) continue
+ if (typeof edit[field] !== "string") throw new TypeError(`edit ${index} needs ${need === "optional" ? "a string" : "string"} ${field}`)
+ }
+ })
+ return edits
+}
+
+/** Characters an edit's before and after may never contain. */
+export const POLISH_MARKUP_CHARACTERS = /[<>&]/
+/** The actionable rejection reason for an edit holding a markup character. */
+export const POLISH_MARKUP_REASON = "edit contains markup characters (<, >, &): route this section through the full fidelity gates"
+/** The actionable rejection reason for a located region touching a character reference. */
+export const POLISH_REFERENCE_REASON = "edit touches a character reference: route this section through the full fidelity gates"
+
+/** True when [start, end) overlaps a character reference of html or starts inside one. */
+export function touchesReference(html, start, end) {
+ for (const match of String(html).matchAll(CHARACTER_REFERENCE)) {
+ if (match.index < end && match.index + match[0].length > start) return true
+ }
+ return /&[#\w]*$/.test(html.slice(Math.max(0, start - 16), start))
+}
+
+const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
+
+/** The single whitespace-normalized match of `before` in html as {start, end}, or {count} otherwise. */
+function locate(html, before) {
+ if (before.trim() === "") return { count: 0 }
+ const pattern = new RegExp(before.split(/\s+/).map(escape).join("\\s+"), "g")
+ const hits = [...html.matchAll(pattern)]
+ return hits.length === 1 ? { count: 1, start: hits[0].index, end: hits[0].index + hits[0][0].length } : { count: hits.length }
+}
+
+/** The id of a fact span the region [start, end) cuts through (overlaps without containing), or null. */
+export function cutSpan(html, start, end) {
+ return factSpans(html).find((span) => span.start < end && span.end > start && !(start <= span.start && span.end <= end))?.id ?? null
+}
+
+/**
+ * @param {string} html the filled report
+ * @param {{ before: string, after: string, reason?: string }[]} edits in edits-file order
+ * @param {{ rows: object[] }} ledger
+ * @returns {{ out: string, applied: number[], rejected: { index: number, code: string, reason: string }[],
+ * unlocated: { index: number, code: string, reason: string }[] }}
+ */
+export function applyEdits(html, edits, ledger) {
+ validatePolishEdits(edits)
+ const original = String(html)
+ const result = { out: original, applied: [], rejected: [], unlocated: [] }
+ const defects = (candidate) => {
+ const messages = [result.out, original].flatMap((base) => polishDefects(base, candidate, ledger, { ordered: true }).map((d) => d.message))
+ return [...new Set(messages)]
+ }
+ edits.forEach((edit, index) => {
+ const touches = POLISH_MARKUP_CHARACTERS.test(edit.before) || POLISH_MARKUP_CHARACTERS.test(edit.after)
+ const where = locate(result.out, edit.before)
+ if (where.count !== 1 && !touches) {
+ result.unlocated.push({ index, code: "polish_edit_unlocated", reason: `before matches ${where.count} times; it must match exactly once` })
+ return
+ }
+ const reasons = touches ? [POLISH_MARKUP_REASON] : []
+ const candidate = where.count === 1 ? result.out.slice(0, where.start) + edit.after + result.out.slice(where.end) : result.out
+ if (where.count === 1) {
+ const cut = cutSpan(result.out, where.start, where.end)
+ if (touchesReference(result.out, where.start, where.end)) reasons.push(POLISH_REFERENCE_REASON)
+ reasons.push(...defects(candidate))
+ if (cut !== null) reasons.push(`the edit cuts through fact span ${cut}`)
+ }
+ if (reasons.length > 0) {
+ result.rejected.push({ index, code: "polish_edit_rejected", reason: reasons.join("; ") })
+ return
+ }
+ result.out = candidate
+ result.applied.push(index)
+ })
+ return result
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/polish-diff.mjs b/plugins/omo/skills/ulw-research/scripts/polish-diff.mjs
new file mode 100644
index 00000000..66a019eb
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/polish-diff.mjs
@@ -0,0 +1,31 @@
+// polish-diff.mjs - whole-document backstop between a pre-polish artifact and its polished result.
+//
+// diffPolish(beforeHtml, afterHtml, ledger) runs the polish-rules.mjs rule set (the same rules
+// polish-apply runs per edit) over the whole document, as multisets, and returns at most one defect
+// per code. Severity is looked up in contracts.DEFECT_CODES for every emitted code; a code missing
+// there throws. polish-apply checks every edit against this same whole-document baseline (ordered,
+// so stricter), so a clean polish-apply run is clean here; this catches hand edits. No IO, no model.
+// Plain Node ESM.
+
+import { DEFECT_CODES } from "./contracts.mjs"
+import { POLISH_CODES, polishDefects } from "./polish-rules.mjs"
+
+export { churnRatio, TICKER } from "./polish-rules.mjs"
+
+/** The DEFECT_CODES entry of a polish code; refuses a code the registry does not define. */
+function registryEntry(code) {
+ if (!Object.hasOwn(DEFECT_CODES, code)) throw new Error(`polish code ${code} is not in DEFECT_CODES`)
+ return DEFECT_CODES[code]
+}
+
+/**
+ * @param {string} beforeHtml the pre-polish artifact
+ * @param {string} afterHtml the polished artifact
+ * @param {{ rows: object[] }} ledger
+ * @returns {{ code: string, severity: string, message: string }[]} at most one defect per code
+ */
+export function diffPolish(beforeHtml, afterHtml, ledger) {
+ const found = new Map()
+ for (const { code, message } of polishDefects(beforeHtml, afterHtml, ledger)) found.set(code, [...(found.get(code) ?? []), message])
+ return POLISH_CODES.filter((code) => found.has(code)).map((code) => ({ code, severity: registryEntry(code).severity, message: found.get(code).join("; ") }))
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/polish-project.mjs b/plugins/omo/skills/ulw-research/scripts/polish-project.mjs
new file mode 100644
index 00000000..85024e23
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/polish-project.mjs
@@ -0,0 +1,125 @@
+// polish-project.mjs - the protected projection of report markup that every polish check compares.
+//
+// project(markup) yields the ordered fact spans {id, html}, the visible text, the masked text (each
+// fact span replaced by SLOT) and the ordered href values. Callers pass markup already reduced by
+// html-markup.markupParts().source, with frozen script/style/template content omitted. Fact spans are the
+// placeholder.mjs FACT_ELEMENT_TAGS elements carrying data-fact (any quoting), found by
+// html-markup.elementSpans (a same-name nested element and a quoted ">" stay inside). Hedge and
+// qualifier terms come from fidelity-lexicon (guardedTerms, lexicalPattern "guard" mode).
+// No IO, no model. Plain Node ESM.
+
+import { decodeEntities } from "./entities.mjs"
+import { guardedTerms, lexicalPattern, QUALIFIER_FORMS } from "./fidelity-lexicon.mjs"
+import { elementSpans, markupParts, tagAttributes } from "./html-markup.mjs"
+import { HEDGES } from "./lexicon.mjs"
+import { parseNumeral } from "./numerals.mjs"
+import { FACT_ELEMENT_TAGS } from "./placeholder.mjs"
+import { splitSentences } from "./sentence-split.mjs"
+import { normalizeUnit } from "./units.mjs"
+
+/** The link attributes polish_link_changed owns; a data-claim id change is a markup (fact-binding) change. */
+export const POLISH_LINK_ATTRIBUTES = Object.freeze(["href"])
+/** The bare digit runs compared outside fact spans when no quantity parses around them. */
+export const POLISH_DIGITS = /\d+(?:[.,]\d+)*/g
+/** Stands in for one fact span in masked text; a private-use char, so no numeral or word matches it. */
+export const SLOT = "\uE000"
+const FACT_TAGS = new Set(FACT_ELEMENT_TAGS)
+const isFactElement = (name, attrs) => FACT_TAGS.has(name) && (attrs["data-fact"] ?? "") !== ""
+
+/** Each fact span of the markup as {id, start, end, html}, in order; an unclosed one runs to the end. */
+export function factSpans(markup) {
+ const src = String(markup)
+ return elementSpans(src, isFactElement).map(({ start, end, tag }) => ({ id: tag.attrs["data-fact"], start, end, html: src.slice(start, end) }))
+}
+
+/**
+ * Visible text of an HTML fragment: the scanner's text slots joined by a space (so a quoted ">"
+ * inside a tag stays markup), entities decoded, whitespace collapsed. One tokenization is shared
+ * with the markup rules; no second tag-stripping regex.
+ */
+export function textOf(html) {
+ return decodeEntities(markupParts(html).text.join(" ")).replace(/\s+/g, " ")
+}
+
+/** The protected projection of (already visible) markup; see the header. */
+export function project(markup) {
+ const facts = []
+ let visible = ""
+ let masked = ""
+ let at = 0
+ for (const span of factSpans(markup)) {
+ const outside = textOf(markup.slice(at, span.start))
+ facts.push({ id: span.id, html: span.html.replace(/\s+/g, " ") })
+ visible += `${outside}${textOf(span.html)}`
+ masked += `${outside} ${SLOT} `
+ at = span.end
+ }
+ const rest = textOf(markup.slice(at))
+ const links = tagAttributes(markup).flatMap(({ attrs }) =>
+ POLISH_LINK_ATTRIBUTES.filter((name) => Object.hasOwn(attrs, name)).map((name) => `${name}=${attrs[name]}`),
+ )
+ return { facts, visible: (visible + rest).replace(/\s+/g, " ").trim(), masked: (masked + rest).replace(/\s+/g, " ").trim(), links }
+}
+
+const unitKey = (raw) => (raw === null ? "" : (normalizeUnit(raw) ?? raw.toLowerCase()))
+
+/** Ordered numeral keys: a parsed quantity as "value unit" (once), any other digit run as written. */
+export function numeralKeys(text) {
+ const quantities = []
+ for (let found = parseNumeral(text); found !== null; found = parseNumeral(text, found.span[1])) quantities.push(found)
+ const keys = []
+ let k = 0
+ for (const match of text.matchAll(POLISH_DIGITS)) {
+ while (k < quantities.length && quantities[k].span[1] <= match.index) k++
+ const quantity = quantities[k]
+ if (!quantity || quantity.span[0] > match.index) keys.push(match[0])
+ else if (!quantity.counted) {
+ quantity.counted = true
+ keys.push(`${quantity.value}${quantity.range ? `~${quantity.valueEnd}` : ""} ${unitKey(quantity.unitRaw)}`.trim())
+ }
+ }
+ return keys
+}
+
+/** term -> occurrence count of each listed term in the text (guard mode of the lexical engine). */
+export function termCounts(text, terms = guardedTerms()) {
+ const counts = new Map()
+ for (const term of terms) {
+ const n = [...text.matchAll(lexicalPattern(term, "guard"))].length
+ if (n > 0) counts.set(term, n)
+ }
+ return counts
+}
+
+const add = (into, term, n) => into.set(term, (into.get(term) ?? 0) + n)
+
+/**
+ * The hedge and qualifier tokens the text before a polish holds and must keep (term -> count), per
+ * sentence of the masked text: a sentence without fact spans keeps all its guarded tokens; a
+ * sentence with fact spans keeps those of its tokens that the referenced rows' quote hedges or
+ * qualifier forms name. A ledger term the sentence never held is never required.
+ */
+export function requiredTerms(projection, rows) {
+ const required = new Map()
+ let cursor = 0
+ for (const { text } of splitSentences(projection.masked)) {
+ const slots = text.split(SLOT).length - 1
+ const facts = projection.facts.slice(cursor, cursor + slots)
+ cursor += slots
+ const present = termCounts(text)
+ const sourced = new Set()
+ for (const { id } of facts) {
+ const row = rows.get(id)
+ for (const term of termCounts(row?.quote ?? "", HEDGES).keys()) sourced.add(term)
+ for (const form of QUALIFIER_FORMS[row?.qualifier] ?? []) sourced.add(form)
+ }
+ for (const [term, n] of present) if (slots === 0 || sourced.has(term)) add(required, term, n)
+ }
+ return required
+}
+
+/** Required terms the text no longer holds as many times, as "term" names. */
+export function missingTerms(required, text) {
+ const have = termCounts(text, [...required.keys()])
+ return [...required].filter(([term, n]) => (have.get(term) ?? 0) < n).map(([term]) => term)
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/polish-rules.mjs b/plugins/omo/skills/ulw-research/scripts/polish-rules.mjs
new file mode 100644
index 00000000..60a82340
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/polish-rules.mjs
@@ -0,0 +1,170 @@
+// polish-rules.mjs - the one rule set behind polish-apply (per edit) and polish-diff (whole document).
+//
+// polishDefects(beforeHtml, afterHtml, ledger, {ordered}) compares the visible projections
+// (html-markup.markupParts, then polish-project.project) and returns {code, message} per failure.
+// Polish may change text nodes only. The primary rule is the whole-document skeleton comparison
+// (html-markup.skeletonChange): every markup token (tags, comments, bogus comments and frozen
+// script/style/template content) and every character reference of a text node must be byte-identical
+// in document order; an edited span holds no skeleton token, so any difference is polish_diff_fail
+// naming the first differing token and its offset. The remaining checks compare the projections:
+// polish_diff_fail: fact spans (id + rendered markup), numerals outside fact spans (with units),
+// tickers (TICKER: 1-5 capitals with an optional ".XX" class, or a 6-digit KRX code), or a
+// table's row count changed; any tag, attribute, comment, declaration, or script/style/template
+// content changed; protected tokens also stay within their original text-node boundaries;
+// polish_hedging_decreased: a hedge or qualifier the before text holds and must keep is gone;
+// polish_text_churn: more than POLISH_CHURN_LIMIT of the normalized visible prose changed;
+// polish_link_changed: href values changed or an internal #target stopped resolving;
+// polish_name_changed: a proper noun (POLISH_NAME) of a ledger quote, present before, is gone.
+// A document holding a construct html-markup.ambiguousMarkup lists (</ + non-letter, <!-- inside
+// script/style, <![CDATA[, <?, --!>, or < inside a tag) is refused first with polish_diff_fail
+// "ambiguous markup": the byte comparison is only trusted where the tokenizer is exact.
+// ordered (polish-apply) also refuses a reordering of fact spans, numerals, tickers and links;
+// without it they compare as multisets. Severity lives only in contracts.DEFECT_CODES.
+// No IO, no model. Plain Node ESM.
+
+import { ambiguousMarkup, markupParts, skeletonChange, tagAttributes } from "./html-markup.mjs"
+import { missingTerms, numeralKeys, project, requiredTerms, termCounts, textOf } from "./polish-project.mjs"
+import { splitSentences } from "./sentence-split.mjs"
+import { normalizeText } from "./text-normalize.mjs"
+
+/** The codes polishDefects can emit, in report order. */
+export const POLISH_CODES = Object.freeze(["polish_diff_fail", "polish_hedging_decreased", "polish_text_churn", "polish_link_changed", "polish_name_changed"])
+export const TICKER = /(?<![\p{L}\p{N}])(?:[A-Z]{1,5}(?:\.[A-Z]{1,2})?|\d{6})(?![\p{L}\p{N}])/gu
+/** The capitalized-token heuristic for proper nouns in ledger quotes. */
+export const POLISH_NAME = /\p{Lu}[\p{L}\p{N}&'-]+/gu
+/** Above this share of changed normalized prose a polish composed instead of proofreading. */
+export const POLISH_CHURN_LIMIT = 0.5
+/** Above this many cells a gap counts as fully rewritten instead of running the quadratic distance. */
+const DISTANCE_CELLS = 25_000_000
+
+const sorted = (list) => [...list].sort()
+const same = (a, b) => a.length === b.length && a.every((item, k) => item === b[k])
+
+/** null when unchanged; a reorder counts only when ordered. */
+function listChange(label, before, after, ordered) {
+ if (same(before, after)) return null
+ const remaining = sorted(before)
+ const added = []
+ for (const item of after) {
+ const at = remaining.indexOf(item)
+ if (at === -1) added.push(item)
+ else remaining.splice(at, 1)
+ }
+ if (remaining.length > 0 || added.length > 0) return `${label} changed: removed [${remaining.join(", ")}], added [${added.join(", ")}]`
+ return ordered ? `${label} reordered: [${before.join(", ")}] became [${after.join(", ")}]` : null
+}
+
+const tableRows = (html) => html.split(/<table\b/i).slice(1).map((part) => (part.split(/<\/table\s*>/i)[0].match(/<tr\b/gi) ?? []).length)
+
+function levenshtein(a, b) {
+ if (a === b) return 0
+ if (a.length * b.length > DISTANCE_CELLS) return Math.max(a.length, b.length)
+ let previous = Array.from({ length: b.length + 1 }, (_, k) => k)
+ for (let i = 1; i <= a.length; i++) {
+ const current = [i]
+ for (let j = 1; j <= b.length; j++) {
+ current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1))
+ }
+ previous = current
+ }
+ return previous[b.length]
+}
+
+/** Changed share of normalized prose: edit distance over unmatched sentence runs / longer length. */
+export function churnRatio(beforeText, afterText) {
+ const [a, b] = [normalizeText(beforeText), normalizeText(afterText)]
+ const longest = Math.max(a.length, b.length)
+ if (longest === 0) return 0
+ const [sa, sb] = [splitSentences(a).map((s) => s.text), splitSentences(b).map((s) => s.text)]
+ const lcs = Array.from({ length: sa.length + 1 }, () => new Array(sb.length + 1).fill(0))
+ for (let i = sa.length - 1; i >= 0; i--) {
+ for (let j = sb.length - 1; j >= 0; j--) lcs[i][j] = sa[i] === sb[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1])
+ }
+ let distance = 0
+ let gap = [[], []]
+ const flush = () => {
+ distance += levenshtein(gap[0].join(" "), gap[1].join(" "))
+ gap = [[], []]
+ }
+ let [i, j] = [0, 0]
+ while (i < sa.length || j < sb.length) {
+ if (i < sa.length && j < sb.length && sa[i] === sb[j]) {
+ flush()
+ i++
+ j++
+ } else if (j >= sb.length || (i < sa.length && lcs[i + 1][j] >= lcs[i][j + 1])) gap[0].push(sa[i++])
+ else gap[1].push(sb[j++])
+ }
+ flush()
+ return distance / longest
+}
+
+function linkChange(from, to, before, after, ordered) {
+ const change = listChange("href and data-claim values", from.links, to.links, ordered)
+ if (change) return change
+ const ids = (html) => new Set(tagAttributes(html).flatMap(({ attrs }) => (Object.hasOwn(attrs, "id") ? [attrs.id] : [])))
+ const [idsBefore, idsAfter] = [ids(before), ids(after)]
+ const targets = to.links.filter((link) => link.startsWith("href=#")).map((link) => link.slice(5))
+ const broken = targets.filter((href) => idsBefore.has(href.slice(1)) && !idsAfter.has(href.slice(1)))
+ return broken.length > 0 ? `internal link targets stopped resolving: [${[...new Set(broken)].join(", ")}]` : null
+}
+
+/** Capitalized tokens of the ledger quotes, minus each sentence's first word. */
+function ledgerNames(rows) {
+ const names = new Set()
+ for (const row of rows) {
+ for (const { text } of splitSentences(row.quote ?? "")) {
+ const first = text.match(/^\P{L}*\p{L}+/u)?.[0].length ?? 0
+ for (const match of text.matchAll(POLISH_NAME)) if (match.index >= first) names.add(match[0])
+ }
+ }
+ return names
+}
+
+const hasWord = (text, word) => new RegExp(`(?<![\\p{L}\\p{N}])${word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\p{L}\\p{N}])`, "u").test(text)
+
+/**
+ * @param {string} beforeHtml
+ * @param {string} afterHtml
+ * @param {{ rows: object[] }} ledger
+ * @param {{ ordered?: boolean }} [options]
+ * @returns {{ code: string, message: string }[]} in POLISH_CODES order
+ */
+export function polishDefects(beforeHtml, afterHtml, ledger, { ordered = false } = {}) {
+ const ambiguous = [...new Set([...ambiguousMarkup(beforeHtml), ...ambiguousMarkup(afterHtml)])]
+ if (ambiguous.length > 0) return [{ code: "polish_diff_fail", message: `ambiguous markup: ${ambiguous.join(", ")}` }]
+ const [beforeParts, afterParts] = [markupParts(beforeHtml), markupParts(afterHtml)]
+ const [before, after] = [beforeParts.source, afterParts.source]
+ const [from, to] = [project(before), project(after)]
+ const rows = ledger?.rows ?? []
+ const defects = []
+ const note = (code, message) => message && defects.push({ code, message })
+ const factKey = (fact) => `${fact.id} ${fact.html}`
+ const tickers = (text) => text.match(TICKER) ?? []
+ const markupEqual = same(beforeParts.markup, afterParts.markup)
+ note("polish_diff_fail", markupEqual ? null : "markup changed")
+ note("polish_diff_fail", skeletonChange(beforeHtml, afterHtml))
+ note("polish_diff_fail", listChange("fact spans", from.facts.map(factKey), to.facts.map(factKey), ordered))
+ note("polish_diff_fail", listChange("numerals outside fact spans", numeralKeys(from.masked), numeralKeys(to.masked), ordered))
+ note("polish_diff_fail", listChange("tickers", tickers(from.masked), tickers(to.masked), ordered))
+ const [rowsBefore, rowsAfter] = [tableRows(before), tableRows(after)]
+ if (!same(rowsBefore, rowsAfter)) note("polish_diff_fail", `table row counts changed: [${rowsBefore}] became [${rowsAfter}]`)
+ const required = requiredTerms(from, new Map(rows.map((row) => [row.fact_id, row])))
+ if (markupEqual) {
+ for (const [index, raw] of beforeParts.text.entries()) {
+ const [a, b] = [raw, afterParts.text[index]].map(textOf)
+ note("polish_diff_fail", listChange("text-node numerals", numeralKeys(a), numeralKeys(b), ordered))
+ note("polish_diff_fail", listChange("text-node tickers", tickers(a), tickers(b), ordered))
+ const lost = missingTerms(termCounts(a, [...required.keys()]), b)
+ if (lost.length) note("polish_hedging_decreased", `text-node hedges removed: ${lost.join(", ")}`)
+ }
+ }
+ const missing = missingTerms(required, to.visible)
+ if (missing.length > 0) note("polish_hedging_decreased", `hedges or qualifiers removed: ${missing.map((term) => `"${term}"`).join(", ")}`)
+ const ratio = churnRatio(from.visible, to.visible)
+ if (ratio > POLISH_CHURN_LIMIT) note("polish_text_churn", `${(ratio * 100).toFixed(1)} percent of the normalized prose changed`)
+ note("polish_link_changed", linkChange(from, to, before, after, ordered))
+ const lost = [...ledgerNames(rows)].filter((name) => hasWord(from.visible, name) && !hasWord(to.visible, name))
+ if (lost.length > 0) note("polish_name_changed", `ledger-quote names missing after polish: ${lost.join(", ")}`)
+ return defects.sort((a, b) => POLISH_CODES.indexOf(a.code) - POLISH_CODES.indexOf(b.code))
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/report-tools-commands.mjs b/plugins/omo/skills/ulw-research/scripts/report-tools-commands.mjs
index 3bef4669..caf4e60b 100644
--- a/plugins/omo/skills/ulw-research/scripts/report-tools-commands.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/report-tools-commands.mjs
@@ -80,12 +80,16 @@ export function runLayoutProbe(argv) {
}
export function runRepairDecide(argv) {
- const args = parseArgs(argv)
+ const args = parseArgs(argv, { repeatable: ["defects"] })
const statePath = args.required("state")
const now = nowFrom(args.flags)
- const defectsInput = readJsonFile(args.required("defects"))
- const defects = Array.isArray(defectsInput) ? defectsInput : defectsInput.defects
- if (!Array.isArray(defects)) throw new CliError("--defects must be a JSON array or a check result with a defects array")
+ // Every --defects file (check result, fidelity record, or bare array) joins one defect list.
+ const defects = args.required("defects").flatMap((path) => {
+ const input = readJsonFile(path)
+ const list = Array.isArray(input) ? input : input.defects
+ if (!Array.isArray(list)) throw new CliError(`--defects ${path} must be a JSON array or a check result with a defects array`)
+ return list
+ })
const state = existsSync(statePath)
? deserializeState(readText(statePath))
: createTracker({
@@ -122,7 +126,7 @@ function manifestPath(flags) {
return join(sessionDirFrom(flags), outcome.MANIFEST_FILENAME)
}
-export function runOutcome(argv) {
+export async function runOutcome(argv) {
const [sub, ...rest] = argv
const args = parseArgs(rest, { booleans: ["json"] })
const file = sub === "state" ? null : manifestPath(args.flags)
@@ -135,7 +139,7 @@ export function runOutcome(argv) {
}
case "set": {
const [format, status] = args.positionals
- if (!format || !status) throw new CliError("usage: outcome set <format> <status> [--path p] [--reason r] [--pages n]")
+ if (!format || !status) throw new CliError("usage: outcome set <format> <status> [--path p] [--reason r] [--pages n] [--verified-by fidelity-defects.json] [--rendered-from source]")
const manifest = outcome.readManifest(file)
const detail = {}
if (args.flags.path !== undefined) {
@@ -144,13 +148,14 @@ export function runOutcome(argv) {
}
if (args.flags.reason !== undefined) detail.reason = args.flags.reason
if (args.flags.pages !== undefined) detail.pages = args.int("pages")
+ for (const [flag, key] of [["verified-by", "verifiedBy"], ["rendered-from", "renderedFrom"]]) if (args.flags[flag] !== undefined) detail[key] = resolve(args.flags[flag])
outcome.writeManifest(file, outcome.setDeliverable(manifest, format, status, { ...detail, now: nowFrom(args.flags) }))
return { json: { command: "outcome set", format, status }, exitCode: 0 }
}
case "gate": {
const [gate, status] = args.positionals
if (!gate || !status) throw new CliError(`usage: outcome gate <${GATE_NAMES.join("|")}> <${GATE_STATUSES.join("|")}>`)
- outcome.writeManifest(file, outcome.setGate(outcome.readManifest(file), gate, status))
+ outcome.writeManifest(file, outcome.setGate(outcome.readManifest(file), gate, status, { reason: args.flags.reason, now: nowFrom(args.flags) }))
return { json: { command: "outcome gate", gate, status }, exitCode: 0 }
}
case "render": {
@@ -169,11 +174,17 @@ export function runOutcome(argv) {
return { json: { command: "outcome state", state }, summary: `state: ${state}`, exitCode: 0 }
}
case "verify": {
- const result = outcome.verifyManifest(outcome.readManifest(file))
+ const manifest = outcome.readManifest(file)
+ const base = outcome.verifyManifest(manifest)
+ const { fidelityProblems } = await import("./fidelity-records.mjs")
+ const problems = [...base.problems, ...fidelityProblems(sessionDirFrom(args.flags), manifest)]
+ const result = { ok: problems.length === 0, problems }
return { json: { command: "outcome verify", ...result }, summary: `verify: ${result.ok ? "OK" : `${result.problems.length} problem(s)`}`, exitCode: result.ok ? 0 : 1 }
}
case "finish": {
const manifest = outcome.readManifest(file)
+ const refused = (await import("./fidelity-records.mjs")).fidelityProblems(sessionDirFrom(args.flags), manifest)
+ if (refused.length > 0) return { json: { command: "outcome finish", refused: true, problems: refused }, summary: `finish: refused (${refused.map((p) => p.code).join(", ")})`, exitCode: 1 }
const ledgerPath = args.flags.ledger ?? join(sessionDirFrom(args.flags), "sources-ledger.md")
const now = nowFrom(args.flags)
const facts = outcome.briefingFacts(manifest, existsSync(ledgerPath) ? readText(ledgerPath) : "", now)
diff --git a/plugins/omo/skills/ulw-research/scripts/report-tools.mjs b/plugins/omo/skills/ulw-research/scripts/report-tools.mjs
index 4ffa27d1..3fd64a2e 100644
--- a/plugins/omo/skills/ulw-research/scripts/report-tools.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/report-tools.mjs
@@ -20,6 +20,15 @@ export const COMMANDS = Object.freeze([
"outcome finish",
"outcome briefing",
"format-extract",
+ "fact add",
+ "fact quote",
+ "fact retract",
+ "fact export",
+ "fact fill",
+ "fact verify",
+ "fact derive",
+ "source snapshot",
+ "claims index",
])
export async function run(argv) {
@@ -36,6 +45,12 @@ export async function run(argv) {
return runOutcome(rest)
case "format-extract":
return runFormatExtract(rest)
+ case "fact":
+ return (await import("./fact-commands.mjs")).runFact(rest)
+ case "source":
+ return (await import("./source-commands.mjs")).runSource(rest)
+ case "claims":
+ return (await import("./source-commands.mjs")).runClaims(rest)
case "--help":
case "help":
if (rest.includes("--json")) return { json: { commands: COMMANDS }, exitCode: 0 }
diff --git a/plugins/omo/skills/ulw-research/scripts/source-commands.mjs b/plugins/omo/skills/ulw-research/scripts/source-commands.mjs
new file mode 100644
index 00000000..244e1589
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/source-commands.mjs
@@ -0,0 +1,140 @@
+// source-commands.mjs - the `source snapshot` and `claims index` subcommands of report-tools.
+// A source dir is immutable: the same id with identical text is a no-op, different text fails
+// (exit 1, snapshot_exists), because ledger rows pin the old hash. `--url` fetches with plain
+// fetch (30 s timeout, no browser) and records extractor tool:source-snapshot@1; `--html` and
+// `--text` import a browser-lane capture and record extractor model (lower trust), or the named
+// extractor for `--text --extractor NAME@VERSION`; `--command` runs an argv without a shell and
+// snapshots its stdout, with the argv, cwd, exit status, and output hashes in command.json.
+// node: builtins and sibling imports only.
+
+import { spawnSync } from "node:child_process"
+import { existsSync, readFileSync, writeFileSync } from "node:fs"
+import { join, resolve } from "node:path"
+
+import { indexClaims } from "./claims-index.mjs"
+import { CliError, parseArgs, sha256Of, writeJsonAtomic } from "./cli-support.mjs"
+import { parseFidelityBlock } from "./fidelity-brief.mjs"
+import { writeSnapshot } from "./source-snapshot.mjs"
+
+const FETCH_TIMEOUT_MS = 30_000
+const TOOL_EXTRACTOR = "tool:source-snapshot@1"
+const SNAPSHOT_USAGE =
+ "usage: source snapshot --id S<n> (--url <u> | --html <file> | --text <file> [--extractor NAME@VERSION] | --command '<argv json>' --cwd DIR) [--kind page|command|document] [--provenance primary|secondary] [--revision R] --session-dir D"
+
+function readInput(path) {
+ try {
+ return readFileSync(path, "utf8")
+ } catch (error) {
+ throw new CliError(`cannot read ${path}: ${error.code ?? error.message}`)
+ }
+}
+
+/** The session dir from --session-dir or SESSION_DIR; a usage error when neither is set. */
+export function sessionDirOf(flags) {
+ const dir = flags["session-dir"] ?? process.env.SESSION_DIR
+ if (!dir || dir === true) throw new CliError("--session-dir is required (or set SESSION_DIR)")
+ return resolve(dir)
+}
+
+/** The snapshot of one source: meta.json, text.txt, and source.html when the source was a page. */
+export function snapshotOf(sessionDir, id) {
+ const dir = join(sessionDir, "sources", String(id))
+ if (!existsSync(join(dir, "meta.json"))) throw new CliError(`source ${id} has no snapshot; run source snapshot --id ${id} first`)
+ const html = join(dir, "source.html")
+ return { meta: JSON.parse(readInput(join(dir, "meta.json"))), text: readInput(join(dir, "text.txt")), html: existsSync(html) ? readInput(html) : null }
+}
+
+/** Whether the brief's source floor lists this source as pointed at (`pointed only: a, b`). */
+export function pointedFlag(sessionDir, url) {
+ const brief = join(sessionDir, "brief.md")
+ const floor = existsSync(brief) ? (parseFidelityBlock(readInput(brief)).fields?.["source floor"] ?? "") : ""
+ const match = /^pointed only:\s*(.+)$/i.exec(floor.trim())
+ return match ? match[1].split(",").map((entry) => entry.trim()).includes(url) : false
+}
+
+async function fetchSource(url) {
+ if (!URL.canParse(url)) throw new CliError(`--url is not a URL: ${url}`)
+ let response
+ try {
+ response = await fetch(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) })
+ } catch (error) {
+ throw new CliError(`fetch failed for ${url}: ${error.message}`)
+ }
+ if (!response.ok) {
+ await response.body?.cancel()
+ throw new CliError(`fetch failed: HTTP ${response.status} for ${url}`)
+ }
+ const type = response.headers.get("content-type") ?? ""
+ if (/pdf/i.test(type)) {
+ await response.body?.cancel()
+ throw new CliError(`pdf response: ${url}; extract its text and use --text <file> --extractor NAME@VERSION`)
+ }
+ const body = await response.text()
+ return /html|xml/i.test(type) || /^\s*</.test(body) ? { url, html: body } : { url, text: body }
+}
+
+function runCommand(argvJson, cwd) {
+ let argv
+ try {
+ argv = JSON.parse(argvJson)
+ } catch (error) {
+ throw new CliError(`--command must be a JSON array of strings: ${error.message}`)
+ }
+ if (!Array.isArray(argv) || argv.length === 0 || !argv.every((part) => typeof part === "string" && part.length > 0)) {
+ throw new CliError("--command must be a non-empty JSON array of non-empty strings")
+ }
+ if (!cwd || cwd === true) throw new CliError("--command needs --cwd DIR")
+ const proc = spawnSync(argv[0], argv.slice(1), { cwd: resolve(cwd), encoding: "utf8", shell: false, maxBuffer: 64 * 1024 * 1024 })
+ if (proc.error) throw new CliError(`--command could not start: ${proc.error.message}`)
+ const receipt = { argv, cwd: resolve(cwd), exit_status: proc.status, stdout_sha256: sha256Of(proc.stdout), stderr_sha256: sha256Of(proc.stderr) }
+ return { path: `command:${argv.join(" ")}`, text: proc.stdout, kind: "command", receipt }
+}
+
+async function acquire(flags) {
+ const given = ["url", "html", "text", "command"].filter((name) => flags[name] !== undefined)
+ if (given.length !== 1) throw new CliError(SNAPSHOT_USAGE)
+ if (flags.extractor !== undefined && flags.text === undefined) throw new CliError("--extractor goes with --text only")
+ if (flags.url !== undefined) return { ...(await fetchSource(flags.url)), extractor: TOOL_EXTRACTOR }
+ if (flags.html !== undefined) return { path: resolve(flags.html), html: readInput(flags.html), extractor: "model" }
+ if (flags.text !== undefined) return { path: resolve(flags.text), text: readInput(flags.text), extractor: flags.extractor === undefined ? "model" : `extractor:${flags.extractor}` }
+ return { ...runCommand(flags.command, flags.cwd), extractor: TOOL_EXTRACTOR }
+}
+
+export async function runSource(argv) {
+ const [sub, ...rest] = argv
+ if (sub !== "snapshot") throw new CliError(SNAPSHOT_USAGE)
+ const args = parseArgs(rest)
+ const id = args.required("id")
+ const sourcesDir = join(sessionDirOf(args.flags), "sources")
+ const { receipt, ...input } = await acquire(args.flags)
+ if (args.flags.kind !== undefined) input.kind = args.flags.kind
+ if (args.flags.provenance !== undefined) input.provenance = args.flags.provenance
+ if (args.flags.revision !== undefined) input.revision = args.flags.revision
+ let written
+ try {
+ written = writeSnapshot(sourcesDir, id, input)
+ } catch (error) {
+ if (String(error.message).startsWith("snapshot_exists")) {
+ return { json: { command: "source snapshot", status: "fail", exitCode: 1, error: "snapshot_exists", message: error.message }, summary: `source snapshot: ${error.message}`, exitCode: 1 }
+ }
+ if (error instanceof TypeError) throw new CliError(error.message)
+ throw error
+ }
+ if (receipt) writeFileSync(join(written.dir, "command.json"), `${JSON.stringify(receipt, null, 2)}\n`)
+ const meta = JSON.parse(readFileSync(join(written.dir, "meta.json"), "utf8"))
+ const data = { source_id: id, directory: written.dir, snapshot_sha256: written.sha256, extractor: meta.extractor, kind: meta.kind, provenance: meta.provenance }
+ return { json: { command: "source snapshot", status: "pass", exitCode: 0, data }, summary: `source snapshot: ${id} ${written.sha256.slice(0, 12)} (${meta.extractor})`, exitCode: 0 }
+}
+
+export function runClaims(argv) {
+ const [sub, ...rest] = argv
+ const args = parseArgs(rest)
+ const [graph] = args.positionals
+ if (sub !== "index" || !graph) throw new CliError("usage: claims index <claim-graph.md> --out claims.json")
+ const out = args.required("out")
+ const index = indexClaims(readInput(graph))
+ if (index.errors.length > 0) throw new CliError(`claims index ${graph}: ${index.errors.join("; ")}`)
+ writeJsonAtomic(out, { schemaVersion: 1, graph: resolve(graph), graph_sha256: sha256Of(readFileSync(graph)), ...index })
+ const verified = index.claims.filter((claim) => claim.in_verified).length
+ return { json: { command: "claims index", out: resolve(out), claims: index.claims.length, verified }, summary: `claims index: ${index.claims.length} claim(s), ${verified} verified`, exitCode: 0 }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/source-snapshot.mjs b/plugins/omo/skills/ulw-research/scripts/source-snapshot.mjs
new file mode 100644
index 00000000..f14f0013
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/source-snapshot.mjs
@@ -0,0 +1,176 @@
+// source-snapshot.mjs - the per-source snapshot that fact locators read.
+//
+// snapshotFromHtml turns a page into text.txt form: one line per block (prose blocks via
+// proseText, one table cell per line; head, scripts, styles and code are dropped). writeSnapshot
+// writes <sourcesDir>/<S-id>/ with text.txt (pages joined by a form feed), source.html when html
+// was given, meta.json, sentences.jsonl (sid S<n>:s<k>) and acronyms.json. The sha256 covers
+// text.txt only. Nothing here touches the network: the caller supplies the html or text.
+// Plain Node ESM, node: builtins and sibling imports only.
+
+import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
+import { join } from "node:path"
+
+import { sha256Of } from "./cli-support.mjs"
+import { parseHtml, PROSE_TAGS, proseText } from "./html-lite.mjs"
+import { splitSentences } from "./sentence-split.mjs"
+
+const SKIP = new Set(["head", "script", "style", "svg", "code", "pre", "kbd", "samp", "template", "noscript"])
+const INLINE = new Set([
+ "a", "abbr", "b", "bdi", "bdo", "cite", "data", "del", "dfn", "em", "font", "i", "ins", "label", "mark", "q", "s",
+ "small", "span", "strong", "sub", "sup", "time", "u", "var",
+])
+const SOURCE_ID = /^S[1-9]\d*$/
+const EXTRACTOR = /^(?:tool:source-snapshot@1|model|extractor:[\w.@+-]+)$/
+const KINDS = new Set(["page", "command", "document"])
+const PROVENANCE = new Set(["primary", "secondary", "unknown"])
+const ACRONYM_AFTER = /\(([A-Z]{2,6})\)/g
+const ACRONYM_BEFORE = /(?<![\p{L}\p{N}])([A-Z]{2,6})\s?\(([A-Z][^()\n]{2,79})\)/gu
+const NAME_WORD = /^[A-Z][\p{L}\p{N}-]*$/u
+const CONNECTOR = new Set(["of", "and", "for", "the", "in", "on", "to", "&"])
+
+/**
+ * Block lines of a page: { root, text, blocks: [{ node, start, end }] } where text joins the block
+ * texts with "\n" and text.slice(start, end) is one block. fact-locate reads cells through `node`.
+ */
+export function snapshotBlocks(html) {
+ const root = parseHtml(html)
+ const lines = []
+ let buffer = ""
+ const flush = (node) => {
+ const line = buffer.replace(/\s+/g, " ").trim()
+ buffer = ""
+ if (line) lines.push({ node, text: line })
+ }
+ const walk = (node) => {
+ for (const child of node.children) {
+ if (child.tag === "#text") buffer += child.text
+ else if (SKIP.has(child.tag)) continue
+ else if (child.tag === "br") flush(node)
+ else if (INLINE.has(child.tag)) walk(child)
+ else if (PROSE_TAGS.has(child.tag)) {
+ flush(node)
+ const line = proseText(child)
+ if (line) lines.push({ node: child, text: line })
+ } else {
+ flush(node)
+ walk(child)
+ flush(child)
+ }
+ }
+ }
+ walk(root)
+ flush(root)
+ const blocks = []
+ let at = 0
+ for (const line of lines) {
+ blocks.push({ node: line.node, start: at, end: at + line.text.length })
+ at += line.text.length + 1
+ }
+ return { root, text: lines.map((line) => line.text).join("\n"), blocks }
+}
+
+/** text.txt form of an html page. */
+export function snapshotFromHtml(html) {
+ return snapshotBlocks(html).text
+}
+
+/** Sentence index rows { sid: "S<n>:s<k>", start, end, text }; a form feed (page break) always ends a sentence. */
+export function indexSentences(text, sourceId) {
+ return splitSentences(text.replace(/\f/g, "\n")).map((row) => ({
+ sid: `${sourceId}:${row.sid}`,
+ start: row.start,
+ end: row.end,
+ text: text.slice(row.start, row.end),
+ }))
+}
+
+const sentenceIdAt = (sentences, offset) => sentences.find((row) => row.start <= offset && offset < row.end)?.sid ?? null
+
+/** "High Bandwidth Memory (HBM)": the capitalized words before the parenthesis, at most one per letter. */
+function nameBefore(text, at, letters) {
+ const prefix = text.slice(Math.max(0, at - 160), at)
+ if (!/\S[ \t]?$/.test(prefix)) return null
+ const words = [...prefix.matchAll(/\S+/g)]
+ const picked = []
+ let content = 0
+ for (let k = words.length - 1; k >= 0 && content < letters; k--) {
+ const word = words[k][0]
+ if (NAME_WORD.test(word)) content++
+ else if (!CONNECTOR.has(word) || picked.length === 0) break
+ picked.unshift(words[k])
+ }
+ while (picked.length > 0 && CONNECTOR.has(picked[0][0])) picked.shift()
+ if (picked.length === 0) return null
+ const start = at - prefix.length + picked[0].index
+ const end = at - prefix.length + picked[picked.length - 1].index + picked[picked.length - 1][0].length
+ return { start, expansion: text.slice(start, end) }
+}
+
+/** Every `Full Name (ACR)` and `ACR (Full Name)` in the text, ordered by position. */
+export function findAcronyms(text, sentences) {
+ const entries = []
+ for (const match of text.matchAll(ACRONYM_AFTER)) {
+ const name = nameBefore(text, match.index, match[1].length)
+ if (!name || name.expansion === match[1]) continue
+ entries.push({ acronym: match[1], expansion: name.expansion, span: [name.start, match.index + match[0].length] })
+ }
+ for (const match of text.matchAll(ACRONYM_BEFORE)) {
+ const expansion = match[2].trim()
+ if (!/\s/.test(expansion) || expansion === expansion.toUpperCase()) continue
+ entries.push({ acronym: match[1], expansion, span: [match.index, match.index + match[0].length] })
+ }
+ return entries
+ .sort((a, b) => a.span[0] - b.span[0])
+ .map((entry) => ({ acronym: entry.acronym, expansion: entry.expansion, sentence_id: sentenceIdAt(sentences, entry.span[0]), span: entry.span }))
+}
+
+function snapshotText(input) {
+ if (Array.isArray(input.pages)) return input.pages.join("\f")
+ if (typeof input.text === "string") return input.text
+ if (typeof input.html === "string") return snapshotFromHtml(input.html)
+ throw new TypeError("writeSnapshot needs text, pages, or html")
+}
+
+function checkInput(sourceId, input) {
+ if (!SOURCE_ID.test(String(sourceId))) throw new TypeError(`source id must be S<n>, got ${sourceId}`)
+ if (!EXTRACTOR.test(String(input.extractor))) throw new TypeError(`extractor must be tool:source-snapshot@1, model, or extractor:<name>, got ${input.extractor}`)
+ if (typeof (input.url ?? input.path) !== "string") throw new TypeError("writeSnapshot needs a url or a path")
+ if (input.kind !== undefined && !KINDS.has(input.kind)) throw new TypeError(`kind must be page, command, or document, got ${input.kind}`)
+ if (input.provenance !== undefined && !PROVENANCE.has(input.provenance)) throw new TypeError(`provenance must be primary, secondary, or unknown, got ${input.provenance}`)
+}
+
+/**
+ * Write <sourcesDir>/<sourceId>/ for one source; returns { dir, sha256 } with sha256 over text.txt.
+ * input: { url | path, text | pages | html, extractor, observed_at?, revision?, kind?, provenance? }.
+ * Re-writing the same text is a no-op change; a different text over an existing snapshot throws
+ * snapshot_exists, because ledger rows pin the old hash.
+ */
+export function writeSnapshot(sourcesDir, sourceId, input) {
+ checkInput(sourceId, input)
+ const text = snapshotText(input)
+ const sha256 = sha256Of(Buffer.from(text, "utf8"))
+ const dir = join(sourcesDir, sourceId)
+ const textPath = join(dir, "text.txt")
+ if (existsSync(textPath) && sha256Of(readFileSync(textPath)) !== sha256) {
+ throw new Error(`snapshot_exists: ${sourceId} already holds a different text.txt`)
+ }
+ mkdirSync(dir, { recursive: true })
+ writeFileSync(textPath, text)
+ if (typeof input.html === "string") writeFileSync(join(dir, "source.html"), input.html)
+ const meta = {
+ source_id: sourceId,
+ url: input.url ?? input.path,
+ observed_at: input.observed_at ?? new Date().toISOString(),
+ sha256,
+ extractor: input.extractor,
+ revision: input.revision ?? null,
+ kind: input.kind ?? (input.url !== undefined || typeof input.html === "string" ? "page" : "document"),
+ provenance: input.provenance ?? "unknown",
+ }
+ writeFileSync(join(dir, "meta.json"), `${JSON.stringify(meta, null, 2)}\n`)
+ const sentences = indexSentences(text, sourceId)
+ writeFileSync(join(dir, "sentences.jsonl"), sentences.map((row) => `${JSON.stringify(row)}\n`).join(""))
+ const acronyms = { schemaVersion: 1, source_id: sourceId, snapshot_sha256: sha256, entries: findAcronyms(text, sentences) }
+ writeFileSync(join(dir, "acronyms.json"), `${JSON.stringify(acronyms, null, 2)}\n`)
+ return { dir, sha256 }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-algebra.mjs b/plugins/omo/skills/ulw-research/scripts/unit-algebra.mjs
new file mode 100644
index 00000000..92ffc5a2
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-algebra.mjs
@@ -0,0 +1,101 @@
+// Canonical factor algebra. Exponents are bigint; aliases enter only through normalizeInput.
+import { fromDecimal, mul, sigFigsOf, toDecimal } from "./rational.mjs"
+import { dimensionOf, normalizeUnit } from "./units.mjs"
+import { refuse } from "./unit-errors.mjs"
+export { refuse, RuleError } from "./unit-errors.mjs"
+
+const SCALED = new Set(["bp", "basispoint", "basispoints"])
+export const isPercentish = (unit) => unit === "%" || unit === "%p"
+const scaled = (text) => SCALED.has(text.replace(/\s+/g, "").toLowerCase())
+
+function atomOf(text) {
+ if (text === "1") return null
+ const atom = scaled(text) ? "%p" : normalizeUnit(text) ?? text
+ if (dimensionOf(atom) === "scale") refuse(`"${text}" is a scale word (${atom}), not a unit`, undefined, "scale_atom", "a counted unit with scale in the numeral")
+ return atom
+}
+
+const combine = (a, b, sign) => {
+ for (const [atom, power] of b) a.set(atom, (a.get(atom) ?? 0n) + sign * power)
+ return a
+}
+
+/** Iterative unit parser: factor maps plus the input's decimal scaling exponent. */
+function parse(raw) {
+ if (raw === null) return { factors: new Map(), shift: 0n }
+ if (typeof raw !== "string" || !raw.trim()) refuse("unit must be text or explicit null", undefined, "unit_text", "canonical unit text or null")
+ const tokens = raw.match(/[*/()^]|[^*/()^]+/g).map((x) => x.trim()).filter(Boolean)
+ const frames = [{ factors: new Map(), shift: 0n, sign: 1n, need: true }]
+ let affine = false
+ const apply = (value, i) => {
+ if (tokens[i] === "^") {
+ const power = tokens[i + 1]
+ if (!/^[+-]?\d+$/.test(power ?? "")) refuse(`unit "${raw}" has a bad exponent`, undefined, "exponent", "an integer exponent")
+ const n = BigInt(power)
+ value = { factors: new Map([...value.factors].map(([a, p]) => [a, p * n])), shift: value.shift * n }
+ i += 2
+ }
+ const frame = frames.at(-1)
+ combine(frame.factors, value.factors, frame.sign)
+ frame.shift += frame.sign * value.shift
+ frame.need = false
+ return i
+ }
+ for (let i = 0; i < tokens.length;) {
+ const frame = frames.at(-1)
+ const token = tokens[i]
+ if (frame.need) {
+ if (token === "(") {
+ frames.push({ factors: new Map(), shift: 0n, sign: 1n, need: true })
+ i++
+ } else {
+ if (/^[*/()^]$/.test(token)) refuse(`unexpected "${token}" in unit "${raw}"`)
+ const atom = atomOf(token)
+ affine ||= dimensionOf(atom) === "temperature"
+ i = apply({ factors: new Map(atom === null ? [] : [[atom, 1n]]), shift: scaled(token) ? -2n : 0n }, i + 1)
+ }
+ } else if (token === ")" && frames.length > 1) {
+ const value = frames.pop()
+ i = apply(value, i + 1)
+ } else if (token === "*" || token === "/") {
+ frame.sign = token === "*" ? 1n : -1n
+ frame.need = true
+ i++
+ } else refuse(`unexpected "${token}" in unit "${raw}"`)
+ }
+ if (frames.length !== 1 || frames[0].need) refuse(`unit "${raw}" ends early or is missing a closing parenthesis`)
+ // Refuse affine composites before cancellation loses the atom that made them unsafe.
+ if (affine && (tokens.length !== 1 || frames[0].shift !== 0n)) refuse("affine (temperature) units cannot occur in composites", undefined, "affine_units", "a non-affine composite or bare temperature for convert")
+ return frames[0]
+}
+
+function format(factors) {
+ const side = (sign) => [...factors].filter(([, p]) => sign * p > 0n).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
+ .map(([a, p]) => { const n = sign * p; return n === 1n ? a : `${a}^${n}` })
+ const top = side(1n)
+ const bottom = side(-1n)
+ if (!top.length && !bottom.length) return null
+ return `${top.join("*") || "1"}${bottom.length ? `/${bottom.length === 1 ? bottom[0] : `(${bottom.join("*")})`}` : ""}`
+}
+
+export const canonicalUnit = (raw) => format(parse(raw).factors)
+export function inputScale(raw) {
+ const { shift } = parse(raw)
+ if (shift === 0n) return null
+ if (shift > 10000n || shift < -10000n) refuse("scaled alias exponent exceeds exact input capacity", undefined, "scale_range", "an absolute decimal scale exponent <= 10000")
+ return shift > 0n ? `1${"0".repeat(Number(shift))}` : `0.${"0".repeat(Number(-shift - 1n))}1`
+}
+
+export function normalizeInput(unit, value) {
+ const canonical = canonicalUnit(unit)
+ const factor = inputScale(unit)
+ return { unit: canonical, value: factor ? toDecimal(mul(fromDecimal(value), fromDecimal(factor)), sigFigsOf(value)) : value }
+}
+
+export const unitTimes = (a, b) => format(combine(parse(a).factors, parse(b).factors, 1n))
+export const unitOver = (a, b) => format(combine(parse(a).factors, parse(b).factors, -1n))
+export const isAffine = (unit) => typeof unit === "string" && dimensionOf(unit) === "temperature"
+export function isCurrencyRate(unit) {
+ const currencies = [...parse(unit).factors].filter(([a, n]) => n !== 0n && dimensionOf(a)?.startsWith("currency:"))
+ return currencies.some(([, n]) => n > 0n) && currencies.some(([, n]) => n < 0n)
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-errors.mjs b/plugins/omo/skills/ulw-research/scripts/unit-errors.mjs
new file mode 100644
index 00000000..e1278edd
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-errors.mjs
@@ -0,0 +1,23 @@
+// Classified refusals shared by public derivation boundaries and the registry.
+export class RuleError extends Error {
+ constructor(detail, hint, shape = "invalid_input", expected = "a valid ledger and derivation specification") {
+ super(detail)
+ this.detail = detail
+ this.shape = shape
+ this.expected = expected
+ if (hint) this.hint = hint
+ }
+}
+
+export const refuse = (detail, hint, shape, expected) => {
+ throw new RuleError(detail, hint, shape, expected)
+}
+
+export function refusal(error) {
+ return {
+ shape: error instanceof RuleError ? error.shape : "invalid_input",
+ expected: error instanceof RuleError ? error.expected : "valid JSON fields matching the fact ledger grammar",
+ detail: error instanceof Error ? error.message : "invalid input",
+ ...(error instanceof RuleError && error.hint ? { hint: error.hint } : {}),
+ }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-metadata.mjs b/plugins/omo/skills/ulw-research/scripts/unit-metadata.mjs
new file mode 100644
index 00000000..cc7864d6
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-metadata.mjs
@@ -0,0 +1,31 @@
+// Metadata contracts and order-independent JSON equality, shared by schema and algebra.
+import { isDeepStrictEqual } from "node:util"
+import { refuse } from "./unit-errors.mjs"
+
+export const METADATA = ["population", "accounting_basis", "snapshot_window", "entity", "valid_at"]
+export const sameDeclared = isDeepStrictEqual
+export const nonEmpty = (value) => typeof value === "string" && value.trim().length > 0
+export const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
+
+export function metadataErrors(row) {
+ return METADATA.filter((field) => {
+ const value = row[field]
+ return value !== null && !(nonEmpty(value) || (["population", "snapshot_window"].includes(field) && object(value)))
+ }).map((field) => `${field} must be explicitly null or valid non-empty metadata`)
+}
+
+export function checkMetadata(rows, fields) {
+ for (const row of rows) {
+ const errors = metadataErrors(row)
+ if (errors.length) refuse(`${row.fact_id}: ${errors.join("; ")}`, undefined, "metadata", "present, type-valid metadata; explicit null when unknown")
+ }
+ for (const field of fields) {
+ if (rows.some((row) => !sameDeclared(row[field], rows[0][field]))) {
+ refuse(`${field} differs across ${rows.map((row) => row.fact_id).join(", ")}`, undefined, "metadata", `equal ${field}`)
+ }
+ }
+}
+
+export function propagatedMetadata(rows) {
+ return Object.fromEntries(METADATA.map((field) => [field, rows.every((row) => sameDeclared(row[field], rows[0][field])) ? structuredClone(rows[0][field]) : null]))
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-pairs.mjs b/plugins/omo/skills/ulw-research/scripts/unit-pairs.mjs
new file mode 100644
index 00000000..6524ca44
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-pairs.mjs
@@ -0,0 +1,72 @@
+// unit-pairs.mjs - the typed operand-unit rules shared by the operation registry and expr nodes (references/unit-rules.md).
+// equalUnits and notAffine guard whole operations; minusUnit and plusUnit are the typed-pair rules that diff and sum
+// use and that expr's `-` and `+` nodes follow; inferExpr assigns a canonical unit to every node of an expr tree.
+
+import { isAffine, isPercentish, refuse, unitOver, unitTimes } from "./unit-algebra.mjs"
+import { postorder } from "./fact-expr.mjs"
+import { checkMetadata, propagatedMetadata } from "./unit-metadata.mjs"
+
+export const show = (unit) => JSON.stringify(unit ?? null)
+export const listed = (rows, units) => rows.map((row, index) => `${row.fact_id} ${units[index] ?? "null"}`).join(", ")
+
+export function equalUnits(rows, units) {
+ if (units.some((unit) => unit !== units[0])) refuse(`unit differs across ${listed(rows, units)}`)
+ return units[0]
+}
+
+export function notAffine(op, rows, units) {
+ if (units.some(isAffine)) refuse(`${op} refuses affine (temperature) units: ${listed(rows, units)}`)
+}
+
+/** The typed-pair rule shared by diff and expr `-`: U-U is U; %-% is %p; %-%p is %; %p-%p is %p; %p-% is refused. */
+export function minusUnit(a, b, where) {
+ if (isAffine(a) || isAffine(b)) refuse(`${where}: subtracting affine (temperature) values as absolute values has no meaning`)
+ if (a === b) return a === "%" ? "%p" : a
+ if (a === "%" && b === "%p") return "%"
+ refuse(`${where}: ${show(a)} - ${show(b)} is not a typed pair (U-U, %-%, %-%p, %p-%p)`)
+}
+
+export function plusUnit(a, b, where) {
+ if (isAffine(a) || isAffine(b)) refuse(`${where}: adding affine (temperature) values has no meaning`)
+ if (a === b && a === "%") refuse(`${where}: adding % values needs disjoint shares of one reference: use --op sum`)
+ if (a === b) return a
+ if ((a === "%" && b === "%p") || (a === "%p" && b === "%")) return "%"
+ refuse(`${where}: ${show(a)} + ${show(b)} has no common unit`)
+}
+
+/** Unit of every node of an expr tree; refuses when a node's unit cannot be inferred. */
+export function inferExpr(ast, unitsById, rowsById = null) {
+ const units = new Map()
+ const metadata = new Map()
+ for (const node of postorder(ast)) {
+ let unit
+ if (node.id) unit = unitsById.get(node.id)
+ else if (node.literal) unit = null
+ else if (node.fn === "pct") {
+ unit = units.get(node.arg)
+ if (unit !== null) refuse(`pct() takes a dimensionless value, got ${show(unit)}`)
+ unit = "%"
+ } else {
+ const left = units.get(node.left)
+ const right = units.get(node.right)
+ if (node.op === "+") unit = plusUnit(left, right, "expr")
+ else if (node.op === "-") unit = minusUnit(left, right, "expr")
+ else {
+ if (isAffine(left) || isAffine(right)) refuse(`expr: ${node.op} on affine (temperature) units has no meaning`)
+ const fraction = (value) => (isPercentish(value) ? null : value)
+ unit = (node.op === "*" ? unitTimes : unitOver)(fraction(left), fraction(right))
+ }
+ }
+ units.set(node, unit)
+ if (rowsById) {
+ const children = node.id ? [rowsById.get(node.id)] : node.literal ? [] : node.fn ? [metadata.get(node.arg)] : [metadata.get(node.left), metadata.get(node.right)]
+ const rows = children.filter(Boolean)
+ if (rows.length) {
+ const fields = node.op === "+" || node.op === "-" ? ["population", "snapshot_window", "accounting_basis"] : node.op === "/" ? ["population", "snapshot_window"] : []
+ checkMetadata(rows, fields)
+ metadata.set(node, { fact_id: "expression", ...propagatedMetadata(rows) })
+ }
+ }
+ }
+ return { unit: units.get(ast), units, metadata: metadata.get(ast) }
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-precision.mjs b/plugins/omo/skills/ulw-research/scripts/unit-precision.mjs
new file mode 100644
index 00000000..a69b3545
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-precision.mjs
@@ -0,0 +1,44 @@
+// Precision is stored independently from display strings; generated fields are rederived on replay.
+import { sigFigsOf, toDecimal } from "./rational.mjs"
+import { refuse } from "./unit-errors.mjs"
+
+export const toBig = (r) => ({ num: BigInt(r.num), den: BigInt(r.den) })
+export const toText = (r) => ({ num: String(r.num), den: String(r.den) })
+export const placesOf = (decimal) => (String(decimal).split(".")[1] ?? "").length
+export function terminates(r) {
+ let den = r.den
+ for (const prime of [2n, 5n]) while (den % prime === 0n) den /= prime
+ return den === 1n
+}
+export function precisionOf(row) {
+ if (row.precision !== null && row.precision !== undefined) {
+ const p = row.precision
+ if (!p || typeof p !== "object" || Object.keys(p).sort().join(",") !== "places,sigfigs" || !Number.isSafeInteger(p.places) || p.places < 0 || !Number.isSafeInteger(p.sigfigs) || p.sigfigs < 1) refuse("invalid stored precision", undefined, "precision_field", "{ places: nonnegative integer, sigfigs: positive integer }")
+ return p
+ }
+ return { places: placesOf(row.value), sigfigs: sigFigsOf(row.value) }
+}
+export function resultPrecision(rows, literals, exact, mode) {
+ const inputs = [...rows.map(precisionOf), ...literals.map((value) => precisionOf({ value }))]
+ const places = Math.min(...inputs.map((p) => p.places))
+ const sigfigs = Math.min(...inputs.map((p) => p.sigfigs))
+ if (mode === "places") return { places, sigfigs: sigFigsOf(roundPlaces(exact, places)) }
+ const rendered = toDecimal(exact, sigfigs)
+ return { places: placesOf(rendered), sigfigs }
+}
+export function roundPlaces(r, places) {
+ const digits = ((r.num < 0n ? -r.num : r.num) * 10n ** BigInt(places) * 2n + r.den) / (r.den * 2n)
+ const text = digits.toString().padStart(places + 1, "0")
+ return (r.num < 0n && digits !== 0n ? "-" : "") + (places ? `${text.slice(0, -places)}.${text.slice(-places)}` : text)
+}
+export function renderValue(exact, precision, mode) {
+ const plain = terminates(exact) ? toDecimal(exact) : null
+ const n = precision[mode]
+ if (plain !== null && (mode === "places" ? placesOf(plain) <= n : sigFigsOf(plain) < n)) return plain
+ return mode === "places" ? roundPlaces(exact, n) : toDecimal(exact, n)
+}
+export function display(value, unit) {
+ const [whole, fraction] = value.split(".")
+ const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (fraction === undefined ? "" : `.${fraction}`)
+ return unit === null ? grouped : unit === "%" ? `${grouped}%` : `${grouped} ${unit}`
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-rules-shapes.mjs b/plugins/omo/skills/ulw-research/scripts/unit-rules-shapes.mjs
new file mode 100644
index 00000000..e140b02c
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-rules-shapes.mjs
@@ -0,0 +1,64 @@
+// Structural rules used by the operation table, shared by schema and recomputation.
+import { leaves, parseExpr } from "./fact-expr.mjs"
+import { isRational } from "./fact-ledger-kinds.mjs"
+import { canonicalUnit, RuleError } from "./unit-algebra.mjs"
+import { nonEmpty, object } from "./unit-metadata.mjs"
+import { show } from "./unit-pairs.mjs"
+
+export const COMMON = ["operands null", "operands empty", "wrong arity", "missing operand id", "invalid rational", "no operation", "repeated operand", "self reference",
+ "kind operation", "audit fields", "precision field", "compatibility field", "metadata fields", "noncanonical operand"]
+export const shapes = (...own) => Object.freeze([...COMMON, ...own])
+export const needsContext = (row) => object(row.context) ? null : `${row.operation} rows carry a context object, got ${show(row.context)}`
+export const noContext = (row) => row.context === null ? null : `${row.operation} rows carry no context, got ${show(row.context)}`
+export const repeatedOperand = (ids) => new Set(ids).size !== ids.length ? `operand ${ids.find((id, i) => ids.indexOf(id) !== i)} is repeated: the operands of one row are distinct facts` : null
+
+export function sumShape(row) {
+ const missing = needsContext(row)
+ if (missing) return missing
+ const c = row.context
+ if (Object.keys(c).sort().join(",") !== "disjoint,reference" || typeof c.disjoint !== "boolean" || !(c.reference === null || nonEmpty(c.reference) || object(c.reference))) return "sum context needs exactly boolean disjoint and null, text, or map reference"
+ if (object(c.reference) && (Object.keys(c.reference).sort().join(",") !== [...row.operands].sort().join(",") || !Object.values(c.reference).every(nonEmpty))) return "reference map must name exactly the supplied operands"
+ return null
+}
+
+export function convertShape(row) {
+ const target = object(row.context) ? row.context.target : undefined
+ if (!nonEmpty(target)) return needsContext(row) ?? "a convert row carries its target in context.target"
+ if (Object.keys(row.context).join(",") !== "target") return "convert context has exactly target"
+ try {
+ const canonical = canonicalUnit(target)
+ return canonical === target ? null : `context.target ${show(target)} is not canonical unit text (canonical: ${show(canonical)})`
+ } catch (error) {
+ if (error instanceof RuleError) return `context.target ${show(target)} is not unit text: ${error.detail}`
+ throw error
+ }
+}
+
+export function exprShape(row) {
+ try {
+ const ids = leaves(parseExpr(row.formula)).ids
+ if (ids.length !== row.operands.length || ids.some((id) => !row.operands.includes(id))) return `formula ids ${ids.join(", ")} do not match operands ${row.operands.join(", ")}`
+ return null
+ } catch (error) {
+ if (error instanceof RuleError) return error.detail
+ throw error
+ }
+}
+
+export function rowShape(row, rules) {
+ if (!object(row)) return "the row is not an object"
+ if (!Object.hasOwn(rules, row.operation)) return `operation ${show(row.operation)} is not one of ${Object.keys(rules).join(", ")}`
+ const rule = rules[row.operation]
+ const { operands, exact } = row
+ if (!Array.isArray(operands) || !operands.length || !operands.every(nonEmpty)) return `operands must be a non-empty array of fact ids, got ${show(operands)}`
+ const repeated = repeatedOperand(operands)
+ if (repeated) return repeated
+ if (operands.includes(row.fact_id)) return `${row.fact_id} cites itself as an operand`
+ if (!rule.arity(operands.length)) return `${row.operation} takes ${rule.arityText}, got ${operands.length}`
+ if (!isRational(exact)) return `exact must be { num, den } reduced integer strings, got ${show(exact)}`
+ if (row.kind !== rule.kind) return `kind ${row.kind} must be ${rule.kind} for ${row.operation}`
+ const own = rule.shape(row)
+ if (own) return own
+ if (!nonEmpty(row.formula) || !Array.isArray(row.derived_from) || row.derived_from.length !== operands.length) return "formula and derived_from are required audit fields"
+ return null
+}
diff --git a/plugins/omo/skills/ulw-research/scripts/unit-rules.mjs b/plugins/omo/skills/ulw-research/scripts/unit-rules.mjs
new file mode 100644
index 00000000..11d6de69
--- /dev/null
+++ b/plugins/omo/skills/ulw-research/scripts/unit-rules.mjs
@@ -0,0 +1,173 @@
+// The sole per-operation registry: guards, context, arithmetic, precision, audit and compatibility.
+import { evaluate, leaves } from "./fact-expr.mjs"
+import { add, div, mul, sub, toDecimal } from "./rational.mjs"
+import { canonicalUnit, inputScale, isAffine, isCurrencyRate, isPercentish, refuse, RuleError, unitOver } from "./unit-algebra.mjs"
+import { checkMetadata, nonEmpty, object, propagatedMetadata } from "./unit-metadata.mjs"
+import { equalUnits, inferExpr, listed, minusUnit, notAffine, show } from "./unit-pairs.mjs"
+import { display, terminates, toBig } from "./unit-precision.mjs"
+import { convertShape, exprShape, noContext, repeatedOperand, rowShape, shapes, sumShape } from "./unit-rules-shapes.mjs"
+import { convertFactor, dimensionOf } from "./units.mjs"
+export { canonicalUnit, inferExpr, isAffine, RuleError }
+
+const EXPR_HINT = "currencies never convert by table: add a dated rate row and use --op expr"
+const compare = (a, b) => a.num * b.den - b.num * a.den
+const SNAPSHOT = ["population", "snapshot_window"]
+const ADDITIVE = [...SNAPSHOT, "accounting_basis"]
+const pair = (a, b) => (spec) => [spec[a], spec[b]]
+const listOf = (spec) => spec.operands
+const generated = (exact, formula, factor = null) => ({ exact, formula, factor })
+const idsOf = (rows) => rows.map((r) => r.fact_id)
+const valuesOf = (rows) => rows.map((r) => toBig(r.exact))
+const ordinary = {
+ kind: "derived", compatibility: "checked", context: () => null, shape: noContext,
+ precision: () => "sigfigs", render: (shown) => shown, metadataResult: propagatedMetadata,
+}
+const binary = { ...ordinary, arity: (n) => n === 2, arityText: "two operands" }
+
+function sharedReference(rows, reference) {
+ if (nonEmpty(reference)) return
+ if (object(reference)) {
+ const named = rows.map((r) => reference[r.fact_id])
+ if (Object.keys(reference).length === rows.length && named.every(nonEmpty) && named.every((v) => v === named[0])) return
+ refuse("% operands name different references or missing/extra ids", undefined, "reference", "one denominator for exactly the operands")
+ }
+ refuse("summing % operands needs one shared reference (denominator): pass reference")
+}
+
+/** Registry rows own every operation-specific choice; consumers only dispatch through these rows. */
+export const OPERATION_RULES = Object.freeze({
+ sum: Object.freeze({
+ ...ordinary, arity: (n) => n >= 2, arityText: "two or more operands", operandIds: listOf,
+ inputFields: ["operands", "disjoint", "reference"],
+ context: (s) => ({ disjoint: Object.hasOwn(s, "disjoint") ? s.disjoint : false, reference: Object.hasOwn(s, "reference") ? s.reference : null }),
+ metadata: ADDITIVE, precision: () => "places", shape: sumShape,
+ resultUnit(rows, units, context) {
+ notAffine("sum", rows, units)
+ const unit = equalUnits(rows, units)
+ if (unit === "%") {
+ if (context.disjoint !== true) refuse("summing % operands needs disjoint shares: pass disjoint")
+ sharedReference(rows, context.reference)
+ if (!nonEmpty(context.rationale)) refuse("summing % operands needs a rationale for the disjoint shares")
+ const values = valuesOf(rows)
+ if (values.some((r) => r.num < 0n || r.num > 100n * r.den) || compare(values.reduce(add), { num: 100n, den: 1n }) > 0n) refuse("disjoint shares must each be in [0, 100] and total <= 100", undefined, "share_total", "valid disjoint shares totaling at most 100%")
+ }
+ return unit
+ },
+ calculate: (rows) => generated(valuesOf(rows).reduce(add), idsOf(rows).join(" + ")),
+ malformed: shapes("no context", "% without disjoint", "% without reference", "% without rationale", "unequal units", "affine units", "closed context", "share total"),
+ }),
+ diff: Object.freeze({
+ ...binary, operandIds: listOf, metadata: ADDITIVE, precision: () => "places",
+ inputFields: ["operands"],
+ resultUnit: (rows, units) => minusUnit(units[0], units[1], `diff ${listed(rows, units)}`),
+ calculate: (rows) => generated(sub(...valuesOf(rows)), idsOf(rows).join(" - ")),
+ malformed: shapes("%p - % pair", "unit - other unit pair", "affine units", "stray context"),
+ }),
+ growth: Object.freeze({
+ ...binary, arityText: "two operands (from, to)", operandIds: pair("from", "to"), metadata: ["population", "accounting_basis"],
+ inputFields: ["from", "to"],
+ resultUnit(rows, units) {
+ const [from, to] = rows
+ notAffine("growth", rows, units)
+ equalUnits(rows, units)
+ if (!nonEmpty(from.entity) || from.entity !== to.entity) refuse(`growth needs one declared entity: ${from.entity} vs ${to.entity}`)
+ if (!nonEmpty(from.valid_at) || !nonEmpty(to.valid_at) || from.valid_at === to.valid_at) refuse(`growth needs two different valid_at periods: ${from.valid_at} vs ${to.valid_at}`)
+ if (toBig(from.exact).num <= 0n) refuse(`growth needs a positive starting value in ${from.fact_id}`)
+ return "%"
+ },
+ calculate(rows) { const [a, b] = valuesOf(rows); const [x, y] = idsOf(rows); return generated(mul(div(sub(b, a), a), 100n), `(${y} - ${x}) / ${x} * 100`) },
+ malformed: shapes("affine units", "unequal units", "entity mismatch", "one period", "from <= 0", "stray context"),
+ }),
+ share: Object.freeze({
+ ...binary, arityText: "two operands (part, whole)", operandIds: pair("part", "whole"), metadata: SNAPSHOT,
+ inputFields: ["part", "whole"],
+ resultUnit(rows, units) {
+ notAffine("share", rows, units)
+ equalUnits(rows, units)
+ const [part, whole] = valuesOf(rows)
+ if (whole.num <= 0n) refuse(`share needs a positive whole in ${rows[1].fact_id}`)
+ if (part.num < 0n || compare(part, whole) > 0n) refuse(`share needs 0 <= part <= whole: ${rows[0].fact_id} is outside [0, ${rows[1].fact_id}]`)
+ return "%"
+ },
+ calculate: (rows) => generated(mul(div(...valuesOf(rows)), 100n), `${idsOf(rows).join(" / ")} * 100`),
+ malformed: shapes("affine units", "unequal units", "whole <= 0", "part < 0", "part above whole", "stray context"),
+ }),
+ ratio: Object.freeze({
+ ...binary, arityText: "two operands (num, den)", operandIds: pair("num", "den"), metadata: SNAPSHOT,
+ inputFields: ["num", "den"],
+ resultUnit(rows, units) {
+ notAffine("ratio", rows, units)
+ if (toBig(rows[1].exact).num === 0n) refuse(`ratio denominator ${rows[1].fact_id} is zero`)
+ if (units[0] === units[1]) return null
+ if (units.some(isPercentish)) refuse(`a ratio over % or %p needs equal units: ${listed(rows, units)}; use expr`)
+ return unitOver(units[0], units[1])
+ },
+ calculate: (rows) => generated(div(...valuesOf(rows)), idsOf(rows).join(" / ")),
+ malformed: shapes("affine units", "divides by zero", "stray context"),
+ }),
+ convert: Object.freeze({
+ ...ordinary, kind: "converted", arity: (n) => n === 1, arityText: "one operand", operandIds: listOf, metadata: [],
+ inputFields: ["operands", "to"],
+ context: (s) => ({ target: canonicalUnit(s.to) }), shape: convertShape,
+ defaultUnit: (s) => s.to,
+ resultUnit(rows, units, context) {
+ if (!nonEmpty(context.target)) refuse("convert needs a target unit")
+ const [from] = units
+ const target = context.target
+ if (from === target) refuse(`${rows[0].fact_id} is already in ${target}`)
+ if ([from, target].some((u) => dimensionOf(u)?.startsWith("currency:"))) refuse(`${rows[0].fact_id} in ${show(from)} has no table conversion to ${target} (a currency)`, EXPR_HINT)
+ if (!convertFactor(from, target)) refuse(`${rows[0].fact_id} in ${show(from)} has no table conversion to ${target}`)
+ return target
+ },
+ calculate(rows, context) {
+ const row = rows[0]
+ const f = convertFactor(row.unit, context.target)
+ const scale = { num: f.num, den: f.den }
+ const text = terminates(scale) ? toDecimal(scale) : `${scale.num}/${scale.den}`
+ return generated(add(mul(toBig(row.exact), scale), f.offset ?? 0n), `${row.fact_id} * ${text}${f.offset ? ` + ${f.offset.num}/${f.offset.den}` : ""}`, terminates(scale) ? text : null)
+ },
+ render: (shown, rows) => `${shown} (${display(rows[0].value, rows[0].unit)})`,
+ malformed: shapes("no context", "empty target", "alias target", "currency target", "no factor to target", "closed context"),
+ }),
+ expr: Object.freeze({
+ ...ordinary, arity: (n) => n >= 1, arityText: "one or more operands", compatibility: "unchecked",
+ expression: true, operandIds: (s, ast) => Object.hasOwn(s, "operands") ? s.operands : leaves(ast).ids,
+ inputFields: ["formula", "operands"],
+ metadata: [], precision: (ast) => ast?.op === "+" || ast?.op === "-" ? "places" : "sigfigs",
+ resultUnit(rows, units, _context, ast) {
+ for (const r of rows) if (isCurrencyRate(r.unit) && !nonEmpty(r.valid_at)) refuse(`FX rate ${r.fact_id} needs a dated valid_at; date compatibility remains unchecked`, EXPR_HINT, "undated_rate", "a dated FX rate row")
+ const inferred = inferExpr(ast, new Map(rows.map((r, i) => [r.fact_id, units[i]])), new Map(rows.map((r) => [r.fact_id, r]))).unit
+ notAffine("expr", rows, units)
+ return inferred
+ },
+ calculate(rows, _context, ast, formula) {
+ const { units } = inferExpr(ast, new Map(rows.map((r) => [r.fact_id, r.unit])))
+ return generated(evaluate(ast, new Map(rows.map((r) => [r.fact_id, toBig(r.exact)])), units), formula.trim())
+ },
+ shape: (row) => noContext(row) ?? exprShape(row),
+ malformed: shapes("formula id mismatch", "parse error", "divides by zero", "uninferable unit", "affine sum", "stray context", "undated rate"),
+ }),
+})
+
+export function ruleOf(op) {
+ if (!Object.hasOwn(OPERATION_RULES, op)) refuse(`unknown operation ${String(op)}; use one of ${Object.keys(OPERATION_RULES).join(", ")}`, undefined, "operation", Object.keys(OPERATION_RULES))
+ return OPERATION_RULES[op]
+}
+export function operandUnit(row) {
+ if (canonicalUnit(row.unit) !== row.unit || inputScale(row.unit)) refuse(`${row.fact_id} unit must be canonical stored text`, undefined, "noncanonical_operand", { unit: canonicalUnit(row.unit) })
+ return row.unit
+}
+export function expectedUnit(op, rows, { ast = null, context = null } = {}) {
+ const rule = ruleOf(op)
+ const repeated = repeatedOperand(idsOf(rows))
+ if (repeated) refuse(repeated, undefined, "repeated_operand", "distinct operand ids")
+ if (!rule.arity(rows.length)) refuse(`${op} takes ${rule.arityText}, got ${rows.length}`, undefined, "arity", rule.arityText)
+ const units = rows.map(operandUnit)
+ checkMetadata(rows, rule.metadata)
+ return rule.resultUnit(rows, units, context ?? {}, ast)
+}
+export function renderPrecision(op, unit, ast = null) {
+ return unit === "%p" ? "places" : ruleOf(op).precision(ast)
+}
+export const unitMismatch = (op, rows, unit, expected) => `unit ${show(unit)} does not follow from ${op} over ${rows.map((r) => `${r.fact_id} ${r.unit ?? "null"}`).join(", ")}: expected unit ${show(expected)}`
+export const malformed = (row) => rowShape(row, OPERATION_RULES)
diff --git a/plugins/omo/skills/ulw-research/scripts/units.mjs b/plugins/omo/skills/ulw-research/scripts/units.mjs
index 89cd3605..3b5cb46b 100644
--- a/plugins/omo/skills/ulw-research/scripts/units.mjs
+++ b/plugins/omo/skills/ulw-research/scripts/units.mjs
@@ -10,10 +10,10 @@ import { div, fromDecimal, sub } from "./rational.mjs"
/** [token, dimension, factor, reading, aliases, offset?]; the Korean reading is noted per row. */
const TABLE = [
- ["mm", "length", "0.001", "open", ["millimeter", "millimeters", "\uBC00\uB9AC\uBBF8\uD130"]], // millimiteo
- ["cm", "length", "0.01", "open", ["centimeter", "centimeters", "\uC13C\uD2F0\uBBF8\uD130"]], // sentimiteo
+ ["mm", "length", "0.001", "open", ["millimeter", "millimeters", "millimetre", "millimetres", "\uBC00\uB9AC\uBBF8\uD130"]], // millimiteo
+ ["cm", "length", "0.01", "open", ["centimeter", "centimeters", "centimetre", "centimetres", "\uC13C\uD2F0\uBBF8\uD130"]], // sentimiteo
["m", "length", "1", "open", ["meter", "meters", "metre", "metres", "\uBBF8\uD130"]], // miteo
- ["km", "length", "1000", "open", ["kilometer", "kilometers", "\uD0AC\uB85C\uBBF8\uD130"]], // killomiteo
+ ["km", "length", "1000", "open", ["kilometer", "kilometers", "kilometre", "kilometres", "\uD0AC\uB85C\uBBF8\uD130"]], // killomiteo
["g", "mass", "1", "closed", ["gram", "grams", "\uADF8\uB7A8"]], // geuraem
["kg", "mass", "1000", "closed", ["kilogram", "kilograms", "\uD0AC\uB85C\uADF8\uB7A8"]], // killogeuraem
["t", "mass", "1000000", "closed", ["ton", "tons", "tonne", "tonnes", "\uD1A4"]], // ton
@@ -65,7 +65,7 @@ for (const [token, dimension, factor, reading, aliases, offset] of TABLE) {
for (const alias of [token, ...aliases]) {
const key = squash(alias)
EXACT.set(key, entry)
- if (key.length > 1 && /^[A-Za-z]+$/.test(key) && !WORD.has(key.toLowerCase())) WORD.set(key.toLowerCase(), entry)
+ if (alias !== token && key.length > 2 && /^[a-z]+$/.test(key) && !WORD.has(key.toLowerCase())) WORD.set(key.toLowerCase(), entry)
}
}