@sdotwinter/openclaw-deterministic 0.4.1 → 0.5.1

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/doctor.js CHANGED
@@ -2,58 +2,102 @@
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 workspace = path.join(HOME, ".openclaw", "workspace");
8
8
 
9
- const OPENCLAW_DIR = path.join(os.homedir(), ".openclaw");
10
- const WORKSPACE_DIR = path.join(OPENCLAW_DIR, "workspace");
9
+ const paths = {
10
+ operating: path.join(workspace, "OPERATING_RULES.md"),
11
+ soul: path.join(workspace, "SOUL.md"),
12
+ detSoul: path.join(workspace, "SOUL.deterministic.md"),
13
+ compactor: path.join(workspace, "skills", "memory-compactor", "SKILL.md"),
14
+ semantic: path.join(workspace, "memory", "semantic", "openclaw.md"),
15
+ };
11
16
 
12
- function checkFile(filePath, label) {
13
- if (!fs.existsSync(filePath)) {
14
- console.log(`❌ Missing: ${label}`);
15
- return;
16
- }
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";
17
21
 
18
- const content = fs.readFileSync(filePath, "utf8");
22
+ const HARD_LIMIT = 1200;
23
+ const RISK_THRESHOLD = 1020;
19
24
 
20
- if (!content.includes("Installed by openclaw-deterministic")) {
21
- console.log(`⚠️ ${label} exists but is not deterministic-managed.`);
22
- return;
25
+ function exists(p) {
26
+ try {
27
+ fs.accessSync(p);
28
+ return true;
29
+ } catch {
30
+ return false;
23
31
  }
32
+ }
24
33
 
25
- if (!content.includes(`v${pkg.version}`)) {
26
- console.log(`⚠️ ${label} version drift detected.`);
27
- return;
28
- }
34
+ function read(p) {
35
+ return fs.readFileSync(p, "utf8");
36
+ }
29
37
 
30
- console.log(`✅ ${label} OK (v${pkg.version})`);
38
+ // cheap, consistent estimate (good enough for thresholds)
39
+ function tokenEstimate(text) {
40
+ if (!text) return 0;
41
+ const words = text.trim().split(/\s+/).filter(Boolean).length;
42
+ return Math.ceil(words * 1.3);
31
43
  }
32
44
 
33
- function runDoctor() {
34
- console.log("Running deterministic doctor...\n");
45
+ function ok(msg) { console.log(`✅ ${msg}`); }
46
+ function warn(msg) { console.log(`⚠ ${msg}`); }
47
+ function fail(msg) { console.log(`❌ ${msg}`); }
35
48
 
36
- if (!fs.existsSync(OPENCLAW_DIR)) {
37
- console.log("❌ OpenClaw not installed.");
38
- process.exit(1);
39
- }
49
+ console.log("\nRunning deterministic doctor...\n");
50
+
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.");
40
56
 
41
- checkFile(
42
- path.join(WORKSPACE_DIR, "OPERATING_RULES.md"),
43
- "OPERATING_RULES.md"
44
- );
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
- checkFile(
47
- path.join(WORKSPACE_DIR, "SOUL.deterministic.md"),
48
- "SOUL.deterministic.md"
49
- );
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).");
50
67
 
51
- checkFile(
52
- path.join(WORKSPACE_DIR, "skills", "memory-compactor", "SKILL.md"),
53
- "memory-compactor SKILL.md"
54
- );
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).");
55
72
 
56
- console.log("\nDoctor complete.");
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).");
75
+
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);
88
+
89
+ console.log(`\nSemantic memory tokens (est): ${tokens}`);
90
+
91
+ if (tokens > HARD_LIMIT) {
92
+ warn("ABOVE HARD LIMIT (Tier C enforced for semantic operations).");
93
+ } else if (tokens > RISK_THRESHOLD) {
94
+ warn("Near risk threshold (85%). Semantic appends should behave as Tier C per compactor rules.");
95
+ } else {
96
+ ok("Semantic memory within safe bounds.");
97
+ }
98
+ } else {
99
+ console.log("\nℹ No semantic memory file detected (ok).");
57
100
  }
58
101
 
59
- runDoctor();
102
+ console.log("\nDoctor complete.\n");
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.4.1",
3
+ "version": "0.5.1",
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