@sdotwinter/openclaw-deterministic 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/audit.js ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { execSync } = require("child_process");
6
+
7
+ const HOME = process.env.HOME;
8
+ const workspace = path.join(HOME, ".openclaw", "workspace");
9
+
10
+ const files = [
11
+ {
12
+ name: "OPERATING_RULES.md",
13
+ installed: path.join(workspace, "OPERATING_RULES.md"),
14
+ template: path.join(__dirname, "..", "templates", "OPERATING_RULES.md"),
15
+ },
16
+ {
17
+ name: "SOUL.deterministic.md",
18
+ installed: path.join(workspace, "SOUL.deterministic.md"),
19
+ template: path.join(__dirname, "..", "templates", "SOUL.deterministic.md"),
20
+ },
21
+ {
22
+ name: "memory-compactor SKILL.md",
23
+ installed: path.join(workspace, "skills", "memory-compactor", "SKILL.md"),
24
+ template: path.join(__dirname, "..", "templates", "memory-compactor.SKILL.md"),
25
+ },
26
+ ];
27
+
28
+ function exists(p) {
29
+ try {
30
+ fs.accessSync(p);
31
+ return true;
32
+ } catch {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ console.log("\nRunning deterministic audit...\n");
38
+
39
+ let driftFound = false;
40
+
41
+ for (const file of files) {
42
+ if (!exists(file.installed)) {
43
+ console.log(`❌ ${file.name} missing in workspace.`);
44
+ driftFound = true;
45
+ continue;
46
+ }
47
+
48
+ if (!exists(file.template)) {
49
+ console.log(`❌ Template missing for ${file.name}.`);
50
+ driftFound = true;
51
+ continue;
52
+ }
53
+
54
+ try {
55
+ const diff = execSync(
56
+ `diff -u "${file.template}" "${file.installed}"`,
57
+ { stdio: "pipe" }
58
+ ).toString();
59
+
60
+ if (diff.trim().length === 0) {
61
+ console.log(`✅ ${file.name} matches template.`);
62
+ } else {
63
+ console.log(`⚠ Drift detected in ${file.name}:\n`);
64
+ console.log(diff);
65
+ driftFound = true;
66
+ }
67
+ } catch (err) {
68
+ if (err.status === 1) {
69
+ console.log(`⚠ Drift detected in ${file.name}:\n`);
70
+ console.log(err.stdout.toString());
71
+ driftFound = true;
72
+ } else {
73
+ console.log(`❌ Error auditing ${file.name}`);
74
+ driftFound = true;
75
+ }
76
+ }
77
+ }
78
+
79
+ if (!driftFound) {
80
+ console.log("\nNo drift detected. Files match canonical templates.");
81
+ } else {
82
+ console.log("\nAudit complete. Drift detected.");
83
+ }
84
+
85
+ console.log("");
86
+ process.exit(0);
package/bin/cli.js CHANGED
@@ -30,6 +30,10 @@ switch (command) {
30
30
  case "revert":
31
31
  require(path.join(__dirname, "revert"));
32
32
  break;
33
+
34
+ case "audit":
35
+ require(path.join(__dirname, "audit"));
36
+ break;
33
37
 
34
38
  case "--version":
35
39
  case "-v":
@@ -57,6 +61,7 @@ function showHelp() {
57
61
  console.log(" oc-deterministic install");
58
62
  console.log(" oc-deterministic enable");
59
63
  console.log(" oc-deterministic revert");
64
+ console.log(" oc-deterministic audit");
60
65
  console.log(" oc-deterministic --version");
61
66
  console.log(" oc-deterministic --help\n");
62
67
  }
package/bin/doctor.js CHANGED
@@ -6,15 +6,19 @@ const path = require("path");
6
6
  const HOME = process.env.HOME;
7
7
  const workspace = path.join(HOME, ".openclaw", "workspace");
8
8
 
9
- const files = {
9
+ const paths = {
10
10
  operating: path.join(workspace, "OPERATING_RULES.md"),
11
11
  soul: path.join(workspace, "SOUL.md"),
12
- deterministicSoul: path.join(workspace, "SOUL.deterministic.md"),
13
- skill: path.join(workspace, "skills", "memory-compactor", "SKILL.md"),
12
+ detSoul: path.join(workspace, "SOUL.deterministic.md"),
13
+ compactor: path.join(workspace, "skills", "memory-compactor", "SKILL.md"),
14
14
  semantic: path.join(workspace, "memory", "semantic", "openclaw.md"),
15
15
  };
16
16
 
17
- const MARKER = "## Deterministic Governance Overlay";
17
+ const MARKER_OVERLAY = "## Deterministic Governance Overlay";
18
+ const MARKER_OPERATING_CANON = "Deterministic Execution Contract — Hardened Overlay";
19
+ const MARKER_DETSOUL_CANON = "Deterministic Governance Overlay — Canonical";
20
+ const MARKER_COMPACTOR_CANON = "## Hybrid Trigger Model";
21
+
18
22
  const HARD_LIMIT = 1200;
19
23
  const RISK_THRESHOLD = 1020;
20
24
 
@@ -27,56 +31,73 @@ function exists(p) {
27
31
  }
28
32
  }
29
33
 
34
+ function read(p) {
35
+ return fs.readFileSync(p, "utf8");
36
+ }
37
+
38
+ // cheap, consistent estimate (good enough for thresholds)
30
39
  function tokenEstimate(text) {
31
40
  if (!text) return 0;
32
- return Math.ceil(text.split(/\s+/).length * 1.3);
41
+ const words = text.trim().split(/\s+/).filter(Boolean).length;
42
+ return Math.ceil(words * 1.3);
33
43
  }
34
44
 
45
+ function ok(msg) { console.log(`✅ ${msg}`); }
46
+ function warn(msg) { console.log(`⚠ ${msg}`); }
47
+ function fail(msg) { console.log(`❌ ${msg}`); }
48
+
35
49
  console.log("\nRunning deterministic doctor...\n");
36
50
 
37
- // File checks
38
- console.log(exists(files.operating)
39
- ? "✅ OPERATING_RULES.md present."
40
- : " OPERATING_RULES.md missing.");
51
+ // --- Presence checks
52
+ exists(paths.operating) ? ok("OPERATING_RULES.md present.") : fail("OPERATING_RULES.md missing.");
53
+ exists(paths.detSoul) ? ok("SOUL.deterministic.md present.") : fail("SOUL.deterministic.md missing.");
54
+ exists(paths.compactor) ? ok("memory-compactor SKILL.md present.") : fail("memory-compactor SKILL.md missing.");
55
+ exists(paths.soul) ? ok("SOUL.md present.") : fail("SOUL.md missing.");
41
56
 
42
- console.log(exists(files.deterministicSoul)
43
- ? "✅ SOUL.deterministic.md present."
44
- : " SOUL.deterministic.md missing.");
57
+ // If core files are missing, stop early (still exit cleanly)
58
+ if (!exists(paths.operating) || !exists(paths.detSoul) || !exists(paths.compactor) || !exists(paths.soul)) {
59
+ console.log("\nDoctor complete (incomplete install).\n");
60
+ process.exit(0);
61
+ }
45
62
 
46
- console.log(exists(files.skill)
47
- ? "✅ memory-compactor SKILL.md present."
48
- : " memory-compactor SKILL.md missing.");
63
+ // --- Activation checks
64
+ const soul = read(paths.soul);
65
+ if (soul.includes(MARKER_OVERLAY)) ok("Deterministic overlay ENABLED in SOUL.md.");
66
+ else warn("Deterministic overlay installed but NOT enabled (run: oc-deterministic enable).");
49
67
 
50
- // Activation check
51
- if (exists(files.soul)) {
52
- const soulContent = fs.readFileSync(files.soul, "utf8");
53
- if (soulContent.includes(MARKER)) {
54
- console.log("✅ Deterministic overlay ENABLED.");
55
- } else {
56
- console.log(" Deterministic overlay installed but NOT enabled.");
57
- }
58
- } else {
59
- console.log("❌ SOUL.md missing.");
60
- }
68
+ // --- Integrity checks (cross-file coherence)
69
+ const operating = read(paths.operating);
70
+ if (operating.includes(MARKER_OPERATING_CANON)) ok("OPERATING_RULES.md matches canonical marker.");
71
+ else warn("OPERATING_RULES.md marker missing (file may be customized or outdated).");
72
+
73
+ if (operating.includes("SOUL.deterministic.md")) ok("OPERATING_RULES.md references SOUL.deterministic.md.");
74
+ else warn("OPERATING_RULES.md does not reference SOUL.deterministic.md (recommended).");
61
75
 
62
- // Semantic memory health
63
- if (exists(files.semantic)) {
64
- const semanticContent = fs.readFileSync(files.semantic, "utf8");
65
- const tokens = tokenEstimate(semanticContent);
76
+ const detSoul = read(paths.detSoul);
77
+ if (detSoul.includes(MARKER_DETSOUL_CANON)) ok("SOUL.deterministic.md matches canonical marker.");
78
+ else warn("SOUL.deterministic.md marker missing (file may be customized or outdated).");
79
+
80
+ const compactor = read(paths.compactor);
81
+ if (compactor.includes(MARKER_COMPACTOR_CANON)) ok("memory-compactor includes Hybrid Trigger Model.");
82
+ else warn("memory-compactor missing Hybrid Trigger Model marker (skill may be outdated).");
83
+
84
+ // --- Semantic health
85
+ if (exists(paths.semantic)) {
86
+ const semantic = read(paths.semantic);
87
+ const tokens = tokenEstimate(semantic);
66
88
 
67
89
  console.log(`\nSemantic memory tokens (est): ${tokens}`);
68
90
 
69
91
  if (tokens > HARD_LIMIT) {
70
- console.log("🚨 ABOVE HARD LIMIT (Tier C enforced).");
92
+ warn("ABOVE HARD LIMIT (Tier C enforced for semantic operations).");
71
93
  } else if (tokens > RISK_THRESHOLD) {
72
- console.log("Near risk threshold (85%).");
94
+ warn("Near risk threshold (85%). Semantic appends should behave as Tier C per compactor rules.");
73
95
  } else {
74
- console.log("Semantic memory within safe bounds.");
96
+ ok("Semantic memory within safe bounds.");
75
97
  }
76
98
  } else {
77
- console.log("\nℹ No semantic memory file detected.");
99
+ console.log("\nℹ No semantic memory file detected (ok).");
78
100
  }
79
101
 
80
102
  console.log("\nDoctor complete.\n");
81
-
82
103
  process.exit(0);
package/bin/install.js CHANGED
@@ -2,85 +2,109 @@
2
2
 
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
- const os = require("os");
6
5
 
7
- const pkg = require("../package.json");
6
+ const HOME = process.env.HOME;
7
+ const openclawRoot = path.join(HOME, ".openclaw");
8
+ const workspace = path.join(openclawRoot, "workspace");
9
+ const backupsRoot = path.join(openclawRoot, "backups", "deterministic");
8
10
 
9
- const OPENCLAW_DIR = path.join(os.homedir(), ".openclaw");
10
- const WORKSPACE_DIR = path.join(OPENCLAW_DIR, "workspace");
11
- const BACKUP_ROOT = path.join(OPENCLAW_DIR, "backups", "deterministic");
11
+ const tpl = (name) => path.join(__dirname, "..", "templates", name);
12
12
 
13
- const TEMPLATES_DIR = path.join(__dirname, "..", "templates");
13
+ const target = {
14
+ operating: path.join(workspace, "OPERATING_RULES.md"),
15
+ detSoul: path.join(workspace, "SOUL.deterministic.md"),
16
+ soul: path.join(workspace, "SOUL.md"),
17
+ compactor: path.join(workspace, "skills", "memory-compactor", "SKILL.md"),
18
+ };
14
19
 
15
- function ensureDir(dir) {
16
- if (!fs.existsSync(dir)) {
17
- fs.mkdirSync(dir, { recursive: true });
18
- }
19
- }
20
+ const OVERLAY_BLOCK = `
21
+ ## Deterministic Governance Overlay
22
+
23
+ This system loads and adheres to SOUL.deterministic.md as a governing philosophical constraint.
24
+ `;
20
25
 
21
- function timestamp() {
22
- return new Date().toISOString().replace(/:/g, "-");
26
+ function exists(p) {
27
+ try { fs.accessSync(p); return true; } catch { return false; }
23
28
  }
24
29
 
25
- function addHeader(content) {
26
- return (
27
- `# Installed by openclaw-deterministic v${pkg.version}\n` +
28
- `# Installed at: ${new Date().toISOString()}\n\n` +
29
- content
30
- );
30
+ function ensureDir(p) {
31
+ fs.mkdirSync(p, { recursive: true });
31
32
  }
32
33
 
33
- function backupRelative(filePath, backupDir) {
34
- if (!fs.existsSync(filePath)) return;
34
+ function read(p) {
35
+ return fs.readFileSync(p, "utf8");
36
+ }
35
37
 
36
- const relativePath = path.relative(WORKSPACE_DIR, filePath);
37
- const backupTarget = path.join(backupDir, relativePath);
38
+ function write(p, content) {
39
+ ensureDir(path.dirname(p));
40
+ fs.writeFileSync(p, content);
41
+ }
38
42
 
39
- ensureDir(path.dirname(backupTarget));
40
- fs.copyFileSync(filePath, backupTarget);
43
+ function copyFile(src, dst) {
44
+ ensureDir(path.dirname(dst));
45
+ fs.copyFileSync(src, dst);
41
46
  }
42
47
 
43
- function installFile(templateName, relativeTargetPath, backupDir) {
44
- const templatePath = path.join(TEMPLATES_DIR, templateName);
45
- const targetPath = path.join(WORKSPACE_DIR, relativeTargetPath);
48
+ function stamp() {
49
+ return new Date().toISOString().replaceAll(":", "-");
50
+ }
46
51
 
47
- ensureDir(path.dirname(targetPath));
52
+ function backupSnapshot(pathsToBackup) {
53
+ ensureDir(backupsRoot);
54
+ const snap = path.join(backupsRoot, stamp());
55
+ ensureDir(snap);
56
+
57
+ for (const p of pathsToBackup) {
58
+ if (!exists(p)) continue;
59
+ const rel = path.relative(openclawRoot, p); // keep structure
60
+ const dst = path.join(snap, rel);
61
+ ensureDir(path.dirname(dst));
62
+ fs.copyFileSync(p, dst);
63
+ }
48
64
 
49
- backupRelative(targetPath, backupDir);
65
+ return snap;
66
+ }
50
67
 
51
- const templateContent = fs.readFileSync(templatePath, "utf8");
52
- const stampedContent = addHeader(templateContent);
68
+ function installTemplates() {
69
+ copyFile(tpl("OPERATING_RULES.md"), target.operating);
70
+ console.log("Installed: OPERATING_RULES.md");
53
71
 
54
- fs.writeFileSync(targetPath, stampedContent, "utf8");
72
+ copyFile(tpl("SOUL.deterministic.md"), target.detSoul);
73
+ console.log("Installed: SOUL.deterministic.md");
55
74
 
56
- console.log(`Installed: ${relativeTargetPath}`);
75
+ copyFile(tpl("memory-compactor.SKILL.md"), target.compactor);
76
+ console.log("Installed: skills/memory-compactor/SKILL.md");
57
77
  }
58
78
 
59
- function runInstall() {
60
- if (!fs.existsSync(OPENCLAW_DIR)) {
61
- console.error("OpenClaw directory not found.");
62
- process.exit(1);
79
+ function bootstrapSoulIfMissing() {
80
+ if (exists(target.soul)) {
81
+ console.log("User SOUL.md exists — NOT modified.");
82
+ return;
63
83
  }
64
84
 
65
- ensureDir(WORKSPACE_DIR);
66
- ensureDir(BACKUP_ROOT);
85
+ console.log("No SOUL.md detected — bootstrapping fresh SOUL.md with deterministic overlay.");
86
+ write(target.soul, OVERLAY_BLOCK.trim() + "\n");
87
+ console.log("Created: SOUL.md (deterministic overlay enabled by default)");
88
+ }
67
89
 
68
- const backupDir = path.join(BACKUP_ROOT, timestamp());
69
- ensureDir(backupDir);
90
+ if (!exists(openclawRoot)) {
91
+ console.error("OpenClaw not found at ~/.openclaw");
92
+ console.error("Install OpenClaw first, then rerun oc-deterministic install.");
93
+ process.exit(1);
94
+ }
70
95
 
71
- console.log("Creating deterministic backup snapshot...");
72
- console.log(`Backup location: ${backupDir}`);
96
+ if (!exists(workspace)) {
97
+ console.error("OpenClaw workspace missing at ~/.openclaw/workspace");
98
+ console.error("OpenClaw may not be fully initialized. Run `openclaw onboard` then retry.");
99
+ process.exit(1);
100
+ }
73
101
 
74
- installFile("OPERATING_RULES.md", "OPERATING_RULES.md", backupDir);
75
- installFile("SOUL.deterministic.md", "SOUL.deterministic.md", backupDir);
76
- installFile(
77
- "memory-compactor.SKILL.md",
78
- path.join("skills", "memory-compactor", "SKILL.md"),
79
- backupDir
80
- );
102
+ console.log("Creating deterministic backup snapshot...");
103
+ const snap = backupSnapshot([target.operating, target.detSoul, target.soul, target.compactor]);
104
+ console.log(`Backup location: ${snap}`);
81
105
 
82
- console.log("\nDeterministic governance installed successfully.");
83
- console.log("User SOUL.md was NOT modified.");
84
- }
106
+ installTemplates();
107
+ bootstrapSoulIfMissing();
85
108
 
86
- runInstall();
109
+ console.log("\nDeterministic governance installed successfully.");
110
+ process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdotwinter/openclaw-deterministic",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Deterministic governance and memory compaction layer for OpenClaw",
5
5
  "keywords": [
6
6
  "openclaw",
@@ -1,4 +1,4 @@
1
- # Deterministic Governance Overlay
1
+ # Deterministic Governance Overlay — Canonical
2
2
 
3
3
  ## Purpose
4
4