dotmem 0.1.3 → 0.1.5
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/cli.cjs +381 -1
- package/cli.js +381 -1
- package/index.cjs +1 -1
- package/index.js +1 -1
- package/mcp.cjs +1 -1
- package/mcp.js +1 -1
- package/package.json +3 -3
package/cli.cjs
CHANGED
|
@@ -14935,6 +14935,372 @@ var detectIdentityFusion = /* @__PURE__ */ __name((input) => {
|
|
|
14935
14935
|
init_policy();
|
|
14936
14936
|
init_env();
|
|
14937
14937
|
init_knowledge();
|
|
14938
|
+
|
|
14939
|
+
// core/knowledge/hard-rules-registry.ts
|
|
14940
|
+
init_user_paths();
|
|
14941
|
+
init_policy();
|
|
14942
|
+
init_detection_events();
|
|
14943
|
+
init_policy_namespace_lock();
|
|
14944
|
+
var HARD_RULES_REGISTRY_FORMAT = "dotmem.hard-rules";
|
|
14945
|
+
var HARD_RULES_REGISTRY_VERSION = 1;
|
|
14946
|
+
var HARD_RULES_REGISTRY_AUDIT_RETENTION = 200;
|
|
14947
|
+
var HARD_RULES_REGISTRY_FILE_NAME = "hard-rules.registry.json";
|
|
14948
|
+
var HardRulesRegistryError = class extends Error {
|
|
14949
|
+
static {
|
|
14950
|
+
__name(this, "HardRulesRegistryError");
|
|
14951
|
+
}
|
|
14952
|
+
constructor(message) {
|
|
14953
|
+
super(message);
|
|
14954
|
+
this.name = "HardRulesRegistryError";
|
|
14955
|
+
}
|
|
14956
|
+
};
|
|
14957
|
+
var requiredText3 = /* @__PURE__ */ __name((v) => typeof v === "string" && v.trim().length > 0, "requiredText");
|
|
14958
|
+
var nowIso2 = /* @__PURE__ */ __name(() => (/* @__PURE__ */ new Date()).toISOString(), "nowIso");
|
|
14959
|
+
var joinPath12 = /* @__PURE__ */ __name((base, child) => {
|
|
14960
|
+
const sep = process.platform === "win32" ? "\\" : "/";
|
|
14961
|
+
return `${base.replace(/[\\/]+/g, sep)}${sep}${child}`;
|
|
14962
|
+
}, "joinPath");
|
|
14963
|
+
var resolveRegistryDir = /* @__PURE__ */ __name((directory) => directory ?? joinPath12(resolveUserDotMemPath(), "policy"), "resolveRegistryDir");
|
|
14964
|
+
var resolveRegistryFilePath = /* @__PURE__ */ __name((directory) => joinPath12(resolveRegistryDir(directory), HARD_RULES_REGISTRY_FILE_NAME), "resolveRegistryFilePath");
|
|
14965
|
+
var validateHardRuleAuthority = /* @__PURE__ */ __name((value) => {
|
|
14966
|
+
try {
|
|
14967
|
+
return validatePolicyAuthority(value);
|
|
14968
|
+
} catch (err2) {
|
|
14969
|
+
void detectAuthorityEscalationAttempt({ claimed: "invalid hard-rule authority", channel: "hard-rules" }).catch(() => void 0);
|
|
14970
|
+
throw err2 instanceof HardRulesRegistryError ? err2 : new HardRulesRegistryError(err2.message);
|
|
14971
|
+
}
|
|
14972
|
+
}, "validateHardRuleAuthority");
|
|
14973
|
+
var canonicalFingerprint = /* @__PURE__ */ __name((rule) => {
|
|
14974
|
+
const payload = {
|
|
14975
|
+
id: rule.id,
|
|
14976
|
+
version: rule.version,
|
|
14977
|
+
ruleClass: rule.ruleClass,
|
|
14978
|
+
status: rule.status,
|
|
14979
|
+
scope: rule.scope,
|
|
14980
|
+
statement: rule.statement,
|
|
14981
|
+
applicability: rule.applicability ?? null,
|
|
14982
|
+
authority: rule.authority
|
|
14983
|
+
};
|
|
14984
|
+
return `hr.${rule.id}@v${rule.version}.${Buffer.from(JSON.stringify(payload)).toString("base64").slice(0, 16)}`;
|
|
14985
|
+
}, "canonicalFingerprint");
|
|
14986
|
+
var validateRule = /* @__PURE__ */ __name((value) => {
|
|
14987
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new HardRulesRegistryError("Hard rule must be an object");
|
|
14988
|
+
const r = value;
|
|
14989
|
+
if (!requiredText3(r.id)) throw new HardRulesRegistryError("id is required");
|
|
14990
|
+
if (typeof r.version !== "number" || !Number.isInteger(r.version) || r.version < 1) throw new HardRulesRegistryError("version must be a positive integer");
|
|
14991
|
+
if (typeof r.revision !== "number" || !Number.isInteger(r.revision) || r.revision < 0) throw new HardRulesRegistryError("revision must be a non-negative integer");
|
|
14992
|
+
if (typeof r.ruleClass !== "string" || !["IMMUTABLE", "OWNER_CONTROLLED", "USER_OVERRIDEABLE", "SESSION"].includes(r.ruleClass)) throw new HardRulesRegistryError("invalid ruleClass");
|
|
14993
|
+
if (typeof r.status !== "string" || !["proposed", "under_review", "approved", "active", "suspended", "retired", "rejected"].includes(r.status)) throw new HardRulesRegistryError("invalid status");
|
|
14994
|
+
if (!requiredText3(r.scope)) throw new HardRulesRegistryError("scope is required");
|
|
14995
|
+
if (!requiredText3(r.statement)) throw new HardRulesRegistryError("statement is required");
|
|
14996
|
+
if (!requiredText3(r.grantedAt)) throw new HardRulesRegistryError("grantedAt is required");
|
|
14997
|
+
if (!requiredText3(r.createdAt)) throw new HardRulesRegistryError("createdAt is required");
|
|
14998
|
+
const authority = validateHardRuleAuthority(r.authority);
|
|
14999
|
+
const validated = {
|
|
15000
|
+
id: r.id,
|
|
15001
|
+
version: r.version,
|
|
15002
|
+
revision: r.revision,
|
|
15003
|
+
ruleClass: r.ruleClass,
|
|
15004
|
+
status: r.status,
|
|
15005
|
+
scope: r.scope,
|
|
15006
|
+
statement: r.statement,
|
|
15007
|
+
...r.applicability && typeof r.applicability === "object" ? { applicability: r.applicability } : {},
|
|
15008
|
+
authority,
|
|
15009
|
+
grantedAt: r.grantedAt,
|
|
15010
|
+
createdAt: r.createdAt,
|
|
15011
|
+
...requiredText3(r.updatedAt) ? { updatedAt: r.updatedAt } : {},
|
|
15012
|
+
fingerprint: ""
|
|
15013
|
+
};
|
|
15014
|
+
const fp = canonicalFingerprint(validated);
|
|
15015
|
+
if (requiredText3(r.fingerprint) && r.fingerprint !== fp) {
|
|
15016
|
+
throw new HardRulesRegistryError(`Hard rule fingerprint mismatch for ${validated.id}`);
|
|
15017
|
+
}
|
|
15018
|
+
validated.fingerprint = fp;
|
|
15019
|
+
if (requiredText3(r.provenance) === false && r.provenance !== void 0) ;
|
|
15020
|
+
if (r.provenance && typeof r.provenance === "object") validated.provenance = r.provenance;
|
|
15021
|
+
if (requiredText3(r.auditRef)) validated.auditRef = r.auditRef;
|
|
15022
|
+
return validated;
|
|
15023
|
+
}, "validateRule");
|
|
15024
|
+
var validateDocument4 = /* @__PURE__ */ __name((value) => {
|
|
15025
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new HardRulesRegistryError("document must be an object");
|
|
15026
|
+
const d = value;
|
|
15027
|
+
if (d.format !== HARD_RULES_REGISTRY_FORMAT) throw new HardRulesRegistryError("invalid format");
|
|
15028
|
+
if (d.version !== HARD_RULES_REGISTRY_VERSION) throw new HardRulesRegistryError("invalid version");
|
|
15029
|
+
if (typeof d.revision !== "number" || !Number.isInteger(d.revision) || d.revision < 0) throw new HardRulesRegistryError("invalid revision");
|
|
15030
|
+
if (!Array.isArray(d.rules)) throw new HardRulesRegistryError("rules must be an array");
|
|
15031
|
+
if (!Array.isArray(d.audit)) throw new HardRulesRegistryError("audit must be an array");
|
|
15032
|
+
const ids = /* @__PURE__ */ new Set();
|
|
15033
|
+
const rules = d.rules.map(validateRule);
|
|
15034
|
+
for (const rule of rules) {
|
|
15035
|
+
if (ids.has(rule.id + "@" + rule.version)) throw new HardRulesRegistryError(`Duplicate hard rule ${rule.id} v${rule.version}`);
|
|
15036
|
+
ids.add(rule.id + "@" + rule.version);
|
|
15037
|
+
}
|
|
15038
|
+
const audit = d.audit.map((e) => {
|
|
15039
|
+
if (!e || typeof e !== "object" || Array.isArray(e)) throw new HardRulesRegistryError("audit entry must be an object");
|
|
15040
|
+
const r = e;
|
|
15041
|
+
if (!requiredText3(r.id)) throw new HardRulesRegistryError("audit id required");
|
|
15042
|
+
if (!requiredText3(r.action)) throw new HardRulesRegistryError("audit action required");
|
|
15043
|
+
if (!requiredText3(r.at)) throw new HardRulesRegistryError("audit at required");
|
|
15044
|
+
if (r.outcome !== "applied" && r.outcome !== "rejected") throw new HardRulesRegistryError("audit outcome invalid");
|
|
15045
|
+
return r;
|
|
15046
|
+
});
|
|
15047
|
+
return { format: HARD_RULES_REGISTRY_FORMAT, version: HARD_RULES_REGISTRY_VERSION, revision: d.revision, rules, audit };
|
|
15048
|
+
}, "validateDocument");
|
|
15049
|
+
var emptyDocument3 = /* @__PURE__ */ __name(() => ({
|
|
15050
|
+
format: HARD_RULES_REGISTRY_FORMAT,
|
|
15051
|
+
version: HARD_RULES_REGISTRY_VERSION,
|
|
15052
|
+
revision: 0,
|
|
15053
|
+
rules: [],
|
|
15054
|
+
audit: []
|
|
15055
|
+
}), "emptyDocument");
|
|
15056
|
+
var boundedAudit2 = /* @__PURE__ */ __name((entries2) => {
|
|
15057
|
+
if (entries2.length <= HARD_RULES_REGISTRY_AUDIT_RETENTION) return [...entries2];
|
|
15058
|
+
const kept = [];
|
|
15059
|
+
let budget = HARD_RULES_REGISTRY_AUDIT_RETENTION;
|
|
15060
|
+
for (let i2 = entries2.length - 1; i2 >= 0; i2--) {
|
|
15061
|
+
const e = entries2[i2];
|
|
15062
|
+
if (e.mandatory === true || budget > 0) {
|
|
15063
|
+
kept.unshift(e);
|
|
15064
|
+
if (e.mandatory !== true) budget--;
|
|
15065
|
+
}
|
|
15066
|
+
}
|
|
15067
|
+
return kept;
|
|
15068
|
+
}, "boundedAudit");
|
|
15069
|
+
var buildAudit = /* @__PURE__ */ __name((entry) => ({
|
|
15070
|
+
id: entry.id ?? `hr-audit.${entry.action}.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`,
|
|
15071
|
+
action: entry.action,
|
|
15072
|
+
at: entry.at,
|
|
15073
|
+
...entry.actorPrincipal !== void 0 ? { actorPrincipal: entry.actorPrincipal } : {},
|
|
15074
|
+
...entry.targetRuleId !== void 0 ? { targetRuleId: entry.targetRuleId } : {},
|
|
15075
|
+
...entry.targetVersion !== void 0 ? { targetVersion: entry.targetVersion } : {},
|
|
15076
|
+
...entry.scope !== void 0 ? { scope: entry.scope } : {},
|
|
15077
|
+
outcome: entry.outcome,
|
|
15078
|
+
...entry.reason !== void 0 ? { reason: entry.reason } : {},
|
|
15079
|
+
...entry.detail !== void 0 ? { detail: entry.detail } : {},
|
|
15080
|
+
...entry.mandatory !== void 0 ? { mandatory: entry.mandatory } : {}
|
|
15081
|
+
}), "buildAudit");
|
|
15082
|
+
var HardRulesRegistry = class {
|
|
15083
|
+
constructor(directory) {
|
|
15084
|
+
this.directory = directory;
|
|
15085
|
+
}
|
|
15086
|
+
directory;
|
|
15087
|
+
static {
|
|
15088
|
+
__name(this, "HardRulesRegistry");
|
|
15089
|
+
}
|
|
15090
|
+
dir() {
|
|
15091
|
+
return resolveRegistryDir(this.directory);
|
|
15092
|
+
}
|
|
15093
|
+
filePath() {
|
|
15094
|
+
return resolveRegistryFilePath(this.directory);
|
|
15095
|
+
}
|
|
15096
|
+
async load() {
|
|
15097
|
+
const fs5 = await import('fs/promises');
|
|
15098
|
+
try {
|
|
15099
|
+
const raw = await fs5.readFile(this.filePath(), "utf8");
|
|
15100
|
+
return validateDocument4(JSON.parse(raw));
|
|
15101
|
+
} catch (err2) {
|
|
15102
|
+
if (err2.code === "ENOENT") return emptyDocument3();
|
|
15103
|
+
throw err2;
|
|
15104
|
+
}
|
|
15105
|
+
}
|
|
15106
|
+
async readDoc(fs5) {
|
|
15107
|
+
try {
|
|
15108
|
+
const raw = await fs5.readFile(this.filePath(), "utf8");
|
|
15109
|
+
return validateDocument4(JSON.parse(raw));
|
|
15110
|
+
} catch (err2) {
|
|
15111
|
+
if (err2.code === "ENOENT") return emptyDocument3();
|
|
15112
|
+
throw err2;
|
|
15113
|
+
}
|
|
15114
|
+
}
|
|
15115
|
+
async writeDoc(fs5, doc) {
|
|
15116
|
+
await fs5.mkdir(this.dir(), { recursive: true });
|
|
15117
|
+
const fp = this.filePath();
|
|
15118
|
+
const tmp = `${fp}.${process.pid}.${Date.now().toString(36)}.tmp`;
|
|
15119
|
+
try {
|
|
15120
|
+
await fs5.writeFile(tmp, `${JSON.stringify(doc, null, 2)}
|
|
15121
|
+
`, "utf8");
|
|
15122
|
+
const handle = await fs5.open(tmp, "r+");
|
|
15123
|
+
try {
|
|
15124
|
+
await handle.sync();
|
|
15125
|
+
} finally {
|
|
15126
|
+
await handle.close();
|
|
15127
|
+
}
|
|
15128
|
+
await fs5.rename(tmp, fp);
|
|
15129
|
+
} catch (err2) {
|
|
15130
|
+
await fs5.rm(tmp, { force: true }).catch(() => void 0);
|
|
15131
|
+
throw err2;
|
|
15132
|
+
}
|
|
15133
|
+
}
|
|
15134
|
+
async mutate(operation, options = {}) {
|
|
15135
|
+
const directory = this.dir();
|
|
15136
|
+
return withPolicyNamespaceLock(async (session2) => {
|
|
15137
|
+
const fs5 = await import('fs/promises');
|
|
15138
|
+
const doc = await this.readDoc(fs5);
|
|
15139
|
+
if (options.basedOnRevision !== void 0 && options.basedOnRevision !== doc.revision) {
|
|
15140
|
+
throw new HardRulesRegistryError(`Stale hard-rules revision: expected ${options.basedOnRevision}, current ${doc.revision}`);
|
|
15141
|
+
}
|
|
15142
|
+
const outcome = operation(doc);
|
|
15143
|
+
let next = outcome.document;
|
|
15144
|
+
validateDocument4(next);
|
|
15145
|
+
await session2.assertOwnership();
|
|
15146
|
+
await this.writeDoc(fs5, next);
|
|
15147
|
+
return outcome.result;
|
|
15148
|
+
}, { directory });
|
|
15149
|
+
}
|
|
15150
|
+
async listRules() {
|
|
15151
|
+
const doc = await this.load();
|
|
15152
|
+
return [...doc.rules].sort((a, b) => a.id.localeCompare(b.id) || a.version - b.version);
|
|
15153
|
+
}
|
|
15154
|
+
async activeRules(scope) {
|
|
15155
|
+
const doc = await this.load();
|
|
15156
|
+
return doc.rules.filter((r) => r.status === "active" && (!scope || r.scope === "*" || r.scope === scope || scope.startsWith(r.scope.endsWith("/") ? r.scope : r.scope + "/")));
|
|
15157
|
+
}
|
|
15158
|
+
async failClosedResolve(scope) {
|
|
15159
|
+
const actives = await this.activeRules(scope);
|
|
15160
|
+
if (actives.length === 0) return { outcome: "none", rule: null, reason: "no applicable hard rule" };
|
|
15161
|
+
if (actives.length > 1) return { outcome: "conflicting", rule: null, reason: "conflicting active hard rules" };
|
|
15162
|
+
const r = actives[0];
|
|
15163
|
+
if (r.ruleClass === "IMMUTABLE" || r.ruleClass === "OWNER_CONTROLLED") {
|
|
15164
|
+
if (r.authority.kind !== "HI" && r.authority.kind !== "operator") {
|
|
15165
|
+
return { outcome: "unresolved-authority", rule: null, reason: "unresolved authority" };
|
|
15166
|
+
}
|
|
15167
|
+
}
|
|
15168
|
+
return { outcome: "applicable", rule: r, reason: "applicable" };
|
|
15169
|
+
}
|
|
15170
|
+
async assign(input, basedOnRevision) {
|
|
15171
|
+
const authority = validateHardRuleAuthority(input.authority);
|
|
15172
|
+
if (!requiredText3(input.scope)) throw new HardRulesRegistryError("scope is required");
|
|
15173
|
+
if (!requiredText3(input.statement)) throw new HardRulesRegistryError("statement is required");
|
|
15174
|
+
if (!["IMMUTABLE", "OWNER_CONTROLLED", "USER_OVERRIDEABLE", "SESSION"].includes(input.ruleClass)) throw new HardRulesRegistryError("invalid ruleClass");
|
|
15175
|
+
const doc0 = await this.load();
|
|
15176
|
+
const ruleId = input.id ?? `hr.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`;
|
|
15177
|
+
const existing = doc0.rules.filter((r) => r.id === ruleId);
|
|
15178
|
+
const version = input.version ?? (existing.length > 0 ? Math.max(...existing.map((r) => r.version)) + 1 : 1);
|
|
15179
|
+
const priorActive = existing.find((r) => r.status === "active" && r.ruleClass === "IMMUTABLE");
|
|
15180
|
+
if (priorActive && input.ruleClass !== "IMMUTABLE") {
|
|
15181
|
+
throw new HardRulesRegistryError("cannot weaken immutable rule");
|
|
15182
|
+
}
|
|
15183
|
+
if (priorActive && authority.kind !== "HI") {
|
|
15184
|
+
throw new HardRulesRegistryError("immutable rule requires HI authority to amend");
|
|
15185
|
+
}
|
|
15186
|
+
const createdAt = nowIso2();
|
|
15187
|
+
const grantedAt = input.grantedAt ?? createdAt;
|
|
15188
|
+
const rule = {
|
|
15189
|
+
id: ruleId,
|
|
15190
|
+
version,
|
|
15191
|
+
revision: 0,
|
|
15192
|
+
ruleClass: input.ruleClass,
|
|
15193
|
+
status: "proposed",
|
|
15194
|
+
scope: input.scope,
|
|
15195
|
+
statement: input.statement,
|
|
15196
|
+
...input.applicability ? { applicability: input.applicability } : {},
|
|
15197
|
+
authority,
|
|
15198
|
+
grantedAt,
|
|
15199
|
+
createdAt,
|
|
15200
|
+
fingerprint: ""
|
|
15201
|
+
};
|
|
15202
|
+
rule.fingerprint = canonicalFingerprint({ ...rule, fingerprint: "" });
|
|
15203
|
+
return this.mutate((doc) => {
|
|
15204
|
+
const revision = doc.revision + 1;
|
|
15205
|
+
const stored = { ...rule, revision, fingerprint: canonicalFingerprint({ ...rule, revision, fingerprint: "" }) };
|
|
15206
|
+
const rules = doc.rules.map((r) => {
|
|
15207
|
+
if (r.id === stored.id && r.version !== stored.version && r.status === "active") {
|
|
15208
|
+
return { ...r, status: "retired", updatedAt: nowIso2() };
|
|
15209
|
+
}
|
|
15210
|
+
return r;
|
|
15211
|
+
});
|
|
15212
|
+
rules.push(stored);
|
|
15213
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15214
|
+
action: "rule.assigned",
|
|
15215
|
+
at: stored.grantedAt,
|
|
15216
|
+
actorPrincipal: authority.kind,
|
|
15217
|
+
targetRuleId: stored.id,
|
|
15218
|
+
targetVersion: stored.version,
|
|
15219
|
+
scope: stored.scope,
|
|
15220
|
+
outcome: "applied",
|
|
15221
|
+
detail: `hard rule ${stored.id} v${stored.version} assigned (${stored.ruleClass})`,
|
|
15222
|
+
mandatory: true
|
|
15223
|
+
})]);
|
|
15224
|
+
return { document: { ...doc, revision, rules, audit }, result: stored };
|
|
15225
|
+
}, { basedOnRevision });
|
|
15226
|
+
}
|
|
15227
|
+
async activate(input, basedOnRevision) {
|
|
15228
|
+
const doc0 = await this.load();
|
|
15229
|
+
const rule = doc0.rules.find((r) => r.id === input.id && r.version === input.version);
|
|
15230
|
+
if (!rule) throw new HardRulesRegistryError("rule not found");
|
|
15231
|
+
if (rule.status !== "approved" && rule.status !== "proposed") ;
|
|
15232
|
+
const authority = input.authority ? validateHardRuleAuthority(input.authority) : rule.authority;
|
|
15233
|
+
if ((rule.ruleClass === "IMMUTABLE" || rule.ruleClass === "OWNER_CONTROLLED") && authority.kind !== "HI" && authority.kind !== "operator") {
|
|
15234
|
+
throw new HardRulesRegistryError("activation requires HI or operator authority");
|
|
15235
|
+
}
|
|
15236
|
+
const actives = doc0.rules.filter((r) => r.status === "active" && r.scope === rule.scope && r.id !== rule.id);
|
|
15237
|
+
if (actives.length > 0 && (rule.ruleClass === "IMMUTABLE" || rule.ruleClass === "OWNER_CONTROLLED")) {
|
|
15238
|
+
throw new HardRulesRegistryError("conflicting active mandatory rules");
|
|
15239
|
+
}
|
|
15240
|
+
return this.mutate((doc) => {
|
|
15241
|
+
const idx = doc.rules.findIndex((r) => r.id === input.id && r.version === input.version);
|
|
15242
|
+
if (idx < 0) throw new HardRulesRegistryError("rule not found");
|
|
15243
|
+
const current = doc.rules[idx];
|
|
15244
|
+
const rules = doc.rules.map((r) => {
|
|
15245
|
+
if (r.id === current.id && r.version !== current.version && r.status === "active") {
|
|
15246
|
+
return { ...r, status: "retired", updatedAt: nowIso2() };
|
|
15247
|
+
}
|
|
15248
|
+
return r;
|
|
15249
|
+
});
|
|
15250
|
+
const now2 = nowIso2();
|
|
15251
|
+
rules[idx >= rules.length ? rules.length - 1 : idx] = { ...current, status: "active", updatedAt: now2 };
|
|
15252
|
+
const targetIndex = rules.findIndex((r) => r.id === current.id && r.version === current.version);
|
|
15253
|
+
if (targetIndex >= 0) rules[targetIndex] = { ...current, status: "active", updatedAt: now2 };
|
|
15254
|
+
const updated = rules.find((r) => r.id === current.id && r.version === current.version);
|
|
15255
|
+
const revision = doc.revision + 1;
|
|
15256
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15257
|
+
action: "rule.activated",
|
|
15258
|
+
at: now2,
|
|
15259
|
+
actorPrincipal: input.actorPrincipal ?? authority.kind,
|
|
15260
|
+
targetRuleId: updated.id,
|
|
15261
|
+
targetVersion: updated.version,
|
|
15262
|
+
scope: updated.scope,
|
|
15263
|
+
outcome: "applied",
|
|
15264
|
+
mandatory: true
|
|
15265
|
+
})]);
|
|
15266
|
+
return { document: { ...doc, revision, rules, audit }, result: updated };
|
|
15267
|
+
}, { basedOnRevision });
|
|
15268
|
+
}
|
|
15269
|
+
async remove(id, authority, basedOnRevision) {
|
|
15270
|
+
const auth = validateHardRuleAuthority(authority);
|
|
15271
|
+
const doc0 = await this.load();
|
|
15272
|
+
const rule = doc0.rules.find((r) => r.id === id && r.status === "active");
|
|
15273
|
+
if (rule) {
|
|
15274
|
+
if (rule.ruleClass === "IMMUTABLE") throw new HardRulesRegistryError("cannot remove immutable rule");
|
|
15275
|
+
if (rule.ruleClass === "OWNER_CONTROLLED" && auth.kind !== "HI" && auth.kind !== "operator") throw new HardRulesRegistryError("unauthenticated cannot remove owner-controlled rule");
|
|
15276
|
+
}
|
|
15277
|
+
await this.mutate((doc) => {
|
|
15278
|
+
const rules = doc.rules.filter((r) => r.id !== id);
|
|
15279
|
+
if (rules.length === doc.rules.length) return { document: doc, result: void 0 };
|
|
15280
|
+
const revision = doc.revision + 1;
|
|
15281
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15282
|
+
action: "rule.removed",
|
|
15283
|
+
at: nowIso2(),
|
|
15284
|
+
actorPrincipal: auth.kind,
|
|
15285
|
+
targetRuleId: id,
|
|
15286
|
+
scope: rule?.scope,
|
|
15287
|
+
outcome: "applied",
|
|
15288
|
+
mandatory: true
|
|
15289
|
+
})]);
|
|
15290
|
+
return { document: { ...doc, revision, rules, audit }, result: void 0 };
|
|
15291
|
+
}, { basedOnRevision });
|
|
15292
|
+
}
|
|
15293
|
+
async auditTrail() {
|
|
15294
|
+
const doc = await this.load();
|
|
15295
|
+
return [...doc.audit];
|
|
15296
|
+
}
|
|
15297
|
+
async revision() {
|
|
15298
|
+
const doc = await this.load();
|
|
15299
|
+
return doc.revision;
|
|
15300
|
+
}
|
|
15301
|
+
};
|
|
15302
|
+
|
|
15303
|
+
// core/functions/bootstrap.ts
|
|
14938
15304
|
async function checkProjectState(_cwd) {
|
|
14939
15305
|
const userDotMemDir = resolveUserDotMemPath();
|
|
14940
15306
|
const userLogicMemPath = path__default.default.join(userDotMemDir, "logic.mem");
|
|
@@ -15073,11 +15439,23 @@ async function ensureUserDotMemInitialized() {
|
|
|
15073
15439
|
}
|
|
15074
15440
|
__name(ensureUserDotMemInitialized, "ensureUserDotMemInitialized");
|
|
15075
15441
|
async function runBootstrap(_cwd) {
|
|
15442
|
+
let hardRulesReady = false;
|
|
15443
|
+
let hardRulesRevision = 0;
|
|
15076
15444
|
try {
|
|
15077
15445
|
await ensureUserDotMemInitialized();
|
|
15078
15446
|
} catch (error) {
|
|
15079
15447
|
console.error(`dotmem initialization needs attention: ${error instanceof Error ? error.message : String(error)}`);
|
|
15080
15448
|
}
|
|
15449
|
+
try {
|
|
15450
|
+
const registry = new HardRulesRegistry();
|
|
15451
|
+
const doc = await registry.load();
|
|
15452
|
+
await registry.listRules();
|
|
15453
|
+
hardRulesReady = true;
|
|
15454
|
+
hardRulesRevision = doc.revision;
|
|
15455
|
+
} catch (error) {
|
|
15456
|
+
hardRulesReady = false;
|
|
15457
|
+
console.error(`hard-rules registry needs attention: ${error instanceof Error ? error.message : String(error)}`);
|
|
15458
|
+
}
|
|
15081
15459
|
try {
|
|
15082
15460
|
const projectId = await resolveProjectIdentity(process.cwd());
|
|
15083
15461
|
await ensureProjectAnchor(projectId);
|
|
@@ -15090,7 +15468,9 @@ async function runBootstrap(_cwd) {
|
|
|
15090
15468
|
ai: await detectAiEnvironment(),
|
|
15091
15469
|
language: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15092
15470
|
userLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15093
|
-
responseLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT"
|
|
15471
|
+
responseLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15472
|
+
hardRulesReady,
|
|
15473
|
+
hardRulesRevision
|
|
15094
15474
|
};
|
|
15095
15475
|
}
|
|
15096
15476
|
__name(runBootstrap, "runBootstrap");
|
package/cli.js
CHANGED
|
@@ -14926,6 +14926,372 @@ var detectIdentityFusion = /* @__PURE__ */ __name((input) => {
|
|
|
14926
14926
|
init_policy();
|
|
14927
14927
|
init_env();
|
|
14928
14928
|
init_knowledge();
|
|
14929
|
+
|
|
14930
|
+
// core/knowledge/hard-rules-registry.ts
|
|
14931
|
+
init_user_paths();
|
|
14932
|
+
init_policy();
|
|
14933
|
+
init_detection_events();
|
|
14934
|
+
init_policy_namespace_lock();
|
|
14935
|
+
var HARD_RULES_REGISTRY_FORMAT = "dotmem.hard-rules";
|
|
14936
|
+
var HARD_RULES_REGISTRY_VERSION = 1;
|
|
14937
|
+
var HARD_RULES_REGISTRY_AUDIT_RETENTION = 200;
|
|
14938
|
+
var HARD_RULES_REGISTRY_FILE_NAME = "hard-rules.registry.json";
|
|
14939
|
+
var HardRulesRegistryError = class extends Error {
|
|
14940
|
+
static {
|
|
14941
|
+
__name(this, "HardRulesRegistryError");
|
|
14942
|
+
}
|
|
14943
|
+
constructor(message) {
|
|
14944
|
+
super(message);
|
|
14945
|
+
this.name = "HardRulesRegistryError";
|
|
14946
|
+
}
|
|
14947
|
+
};
|
|
14948
|
+
var requiredText3 = /* @__PURE__ */ __name((v) => typeof v === "string" && v.trim().length > 0, "requiredText");
|
|
14949
|
+
var nowIso2 = /* @__PURE__ */ __name(() => (/* @__PURE__ */ new Date()).toISOString(), "nowIso");
|
|
14950
|
+
var joinPath12 = /* @__PURE__ */ __name((base, child) => {
|
|
14951
|
+
const sep = process.platform === "win32" ? "\\" : "/";
|
|
14952
|
+
return `${base.replace(/[\\/]+/g, sep)}${sep}${child}`;
|
|
14953
|
+
}, "joinPath");
|
|
14954
|
+
var resolveRegistryDir = /* @__PURE__ */ __name((directory) => directory ?? joinPath12(resolveUserDotMemPath(), "policy"), "resolveRegistryDir");
|
|
14955
|
+
var resolveRegistryFilePath = /* @__PURE__ */ __name((directory) => joinPath12(resolveRegistryDir(directory), HARD_RULES_REGISTRY_FILE_NAME), "resolveRegistryFilePath");
|
|
14956
|
+
var validateHardRuleAuthority = /* @__PURE__ */ __name((value) => {
|
|
14957
|
+
try {
|
|
14958
|
+
return validatePolicyAuthority(value);
|
|
14959
|
+
} catch (err2) {
|
|
14960
|
+
void detectAuthorityEscalationAttempt({ claimed: "invalid hard-rule authority", channel: "hard-rules" }).catch(() => void 0);
|
|
14961
|
+
throw err2 instanceof HardRulesRegistryError ? err2 : new HardRulesRegistryError(err2.message);
|
|
14962
|
+
}
|
|
14963
|
+
}, "validateHardRuleAuthority");
|
|
14964
|
+
var canonicalFingerprint = /* @__PURE__ */ __name((rule) => {
|
|
14965
|
+
const payload = {
|
|
14966
|
+
id: rule.id,
|
|
14967
|
+
version: rule.version,
|
|
14968
|
+
ruleClass: rule.ruleClass,
|
|
14969
|
+
status: rule.status,
|
|
14970
|
+
scope: rule.scope,
|
|
14971
|
+
statement: rule.statement,
|
|
14972
|
+
applicability: rule.applicability ?? null,
|
|
14973
|
+
authority: rule.authority
|
|
14974
|
+
};
|
|
14975
|
+
return `hr.${rule.id}@v${rule.version}.${Buffer.from(JSON.stringify(payload)).toString("base64").slice(0, 16)}`;
|
|
14976
|
+
}, "canonicalFingerprint");
|
|
14977
|
+
var validateRule = /* @__PURE__ */ __name((value) => {
|
|
14978
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new HardRulesRegistryError("Hard rule must be an object");
|
|
14979
|
+
const r = value;
|
|
14980
|
+
if (!requiredText3(r.id)) throw new HardRulesRegistryError("id is required");
|
|
14981
|
+
if (typeof r.version !== "number" || !Number.isInteger(r.version) || r.version < 1) throw new HardRulesRegistryError("version must be a positive integer");
|
|
14982
|
+
if (typeof r.revision !== "number" || !Number.isInteger(r.revision) || r.revision < 0) throw new HardRulesRegistryError("revision must be a non-negative integer");
|
|
14983
|
+
if (typeof r.ruleClass !== "string" || !["IMMUTABLE", "OWNER_CONTROLLED", "USER_OVERRIDEABLE", "SESSION"].includes(r.ruleClass)) throw new HardRulesRegistryError("invalid ruleClass");
|
|
14984
|
+
if (typeof r.status !== "string" || !["proposed", "under_review", "approved", "active", "suspended", "retired", "rejected"].includes(r.status)) throw new HardRulesRegistryError("invalid status");
|
|
14985
|
+
if (!requiredText3(r.scope)) throw new HardRulesRegistryError("scope is required");
|
|
14986
|
+
if (!requiredText3(r.statement)) throw new HardRulesRegistryError("statement is required");
|
|
14987
|
+
if (!requiredText3(r.grantedAt)) throw new HardRulesRegistryError("grantedAt is required");
|
|
14988
|
+
if (!requiredText3(r.createdAt)) throw new HardRulesRegistryError("createdAt is required");
|
|
14989
|
+
const authority = validateHardRuleAuthority(r.authority);
|
|
14990
|
+
const validated = {
|
|
14991
|
+
id: r.id,
|
|
14992
|
+
version: r.version,
|
|
14993
|
+
revision: r.revision,
|
|
14994
|
+
ruleClass: r.ruleClass,
|
|
14995
|
+
status: r.status,
|
|
14996
|
+
scope: r.scope,
|
|
14997
|
+
statement: r.statement,
|
|
14998
|
+
...r.applicability && typeof r.applicability === "object" ? { applicability: r.applicability } : {},
|
|
14999
|
+
authority,
|
|
15000
|
+
grantedAt: r.grantedAt,
|
|
15001
|
+
createdAt: r.createdAt,
|
|
15002
|
+
...requiredText3(r.updatedAt) ? { updatedAt: r.updatedAt } : {},
|
|
15003
|
+
fingerprint: ""
|
|
15004
|
+
};
|
|
15005
|
+
const fp = canonicalFingerprint(validated);
|
|
15006
|
+
if (requiredText3(r.fingerprint) && r.fingerprint !== fp) {
|
|
15007
|
+
throw new HardRulesRegistryError(`Hard rule fingerprint mismatch for ${validated.id}`);
|
|
15008
|
+
}
|
|
15009
|
+
validated.fingerprint = fp;
|
|
15010
|
+
if (requiredText3(r.provenance) === false && r.provenance !== void 0) ;
|
|
15011
|
+
if (r.provenance && typeof r.provenance === "object") validated.provenance = r.provenance;
|
|
15012
|
+
if (requiredText3(r.auditRef)) validated.auditRef = r.auditRef;
|
|
15013
|
+
return validated;
|
|
15014
|
+
}, "validateRule");
|
|
15015
|
+
var validateDocument4 = /* @__PURE__ */ __name((value) => {
|
|
15016
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new HardRulesRegistryError("document must be an object");
|
|
15017
|
+
const d = value;
|
|
15018
|
+
if (d.format !== HARD_RULES_REGISTRY_FORMAT) throw new HardRulesRegistryError("invalid format");
|
|
15019
|
+
if (d.version !== HARD_RULES_REGISTRY_VERSION) throw new HardRulesRegistryError("invalid version");
|
|
15020
|
+
if (typeof d.revision !== "number" || !Number.isInteger(d.revision) || d.revision < 0) throw new HardRulesRegistryError("invalid revision");
|
|
15021
|
+
if (!Array.isArray(d.rules)) throw new HardRulesRegistryError("rules must be an array");
|
|
15022
|
+
if (!Array.isArray(d.audit)) throw new HardRulesRegistryError("audit must be an array");
|
|
15023
|
+
const ids = /* @__PURE__ */ new Set();
|
|
15024
|
+
const rules = d.rules.map(validateRule);
|
|
15025
|
+
for (const rule of rules) {
|
|
15026
|
+
if (ids.has(rule.id + "@" + rule.version)) throw new HardRulesRegistryError(`Duplicate hard rule ${rule.id} v${rule.version}`);
|
|
15027
|
+
ids.add(rule.id + "@" + rule.version);
|
|
15028
|
+
}
|
|
15029
|
+
const audit = d.audit.map((e) => {
|
|
15030
|
+
if (!e || typeof e !== "object" || Array.isArray(e)) throw new HardRulesRegistryError("audit entry must be an object");
|
|
15031
|
+
const r = e;
|
|
15032
|
+
if (!requiredText3(r.id)) throw new HardRulesRegistryError("audit id required");
|
|
15033
|
+
if (!requiredText3(r.action)) throw new HardRulesRegistryError("audit action required");
|
|
15034
|
+
if (!requiredText3(r.at)) throw new HardRulesRegistryError("audit at required");
|
|
15035
|
+
if (r.outcome !== "applied" && r.outcome !== "rejected") throw new HardRulesRegistryError("audit outcome invalid");
|
|
15036
|
+
return r;
|
|
15037
|
+
});
|
|
15038
|
+
return { format: HARD_RULES_REGISTRY_FORMAT, version: HARD_RULES_REGISTRY_VERSION, revision: d.revision, rules, audit };
|
|
15039
|
+
}, "validateDocument");
|
|
15040
|
+
var emptyDocument3 = /* @__PURE__ */ __name(() => ({
|
|
15041
|
+
format: HARD_RULES_REGISTRY_FORMAT,
|
|
15042
|
+
version: HARD_RULES_REGISTRY_VERSION,
|
|
15043
|
+
revision: 0,
|
|
15044
|
+
rules: [],
|
|
15045
|
+
audit: []
|
|
15046
|
+
}), "emptyDocument");
|
|
15047
|
+
var boundedAudit2 = /* @__PURE__ */ __name((entries2) => {
|
|
15048
|
+
if (entries2.length <= HARD_RULES_REGISTRY_AUDIT_RETENTION) return [...entries2];
|
|
15049
|
+
const kept = [];
|
|
15050
|
+
let budget = HARD_RULES_REGISTRY_AUDIT_RETENTION;
|
|
15051
|
+
for (let i2 = entries2.length - 1; i2 >= 0; i2--) {
|
|
15052
|
+
const e = entries2[i2];
|
|
15053
|
+
if (e.mandatory === true || budget > 0) {
|
|
15054
|
+
kept.unshift(e);
|
|
15055
|
+
if (e.mandatory !== true) budget--;
|
|
15056
|
+
}
|
|
15057
|
+
}
|
|
15058
|
+
return kept;
|
|
15059
|
+
}, "boundedAudit");
|
|
15060
|
+
var buildAudit = /* @__PURE__ */ __name((entry) => ({
|
|
15061
|
+
id: entry.id ?? `hr-audit.${entry.action}.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`,
|
|
15062
|
+
action: entry.action,
|
|
15063
|
+
at: entry.at,
|
|
15064
|
+
...entry.actorPrincipal !== void 0 ? { actorPrincipal: entry.actorPrincipal } : {},
|
|
15065
|
+
...entry.targetRuleId !== void 0 ? { targetRuleId: entry.targetRuleId } : {},
|
|
15066
|
+
...entry.targetVersion !== void 0 ? { targetVersion: entry.targetVersion } : {},
|
|
15067
|
+
...entry.scope !== void 0 ? { scope: entry.scope } : {},
|
|
15068
|
+
outcome: entry.outcome,
|
|
15069
|
+
...entry.reason !== void 0 ? { reason: entry.reason } : {},
|
|
15070
|
+
...entry.detail !== void 0 ? { detail: entry.detail } : {},
|
|
15071
|
+
...entry.mandatory !== void 0 ? { mandatory: entry.mandatory } : {}
|
|
15072
|
+
}), "buildAudit");
|
|
15073
|
+
var HardRulesRegistry = class {
|
|
15074
|
+
constructor(directory) {
|
|
15075
|
+
this.directory = directory;
|
|
15076
|
+
}
|
|
15077
|
+
directory;
|
|
15078
|
+
static {
|
|
15079
|
+
__name(this, "HardRulesRegistry");
|
|
15080
|
+
}
|
|
15081
|
+
dir() {
|
|
15082
|
+
return resolveRegistryDir(this.directory);
|
|
15083
|
+
}
|
|
15084
|
+
filePath() {
|
|
15085
|
+
return resolveRegistryFilePath(this.directory);
|
|
15086
|
+
}
|
|
15087
|
+
async load() {
|
|
15088
|
+
const fs5 = await import('fs/promises');
|
|
15089
|
+
try {
|
|
15090
|
+
const raw = await fs5.readFile(this.filePath(), "utf8");
|
|
15091
|
+
return validateDocument4(JSON.parse(raw));
|
|
15092
|
+
} catch (err2) {
|
|
15093
|
+
if (err2.code === "ENOENT") return emptyDocument3();
|
|
15094
|
+
throw err2;
|
|
15095
|
+
}
|
|
15096
|
+
}
|
|
15097
|
+
async readDoc(fs5) {
|
|
15098
|
+
try {
|
|
15099
|
+
const raw = await fs5.readFile(this.filePath(), "utf8");
|
|
15100
|
+
return validateDocument4(JSON.parse(raw));
|
|
15101
|
+
} catch (err2) {
|
|
15102
|
+
if (err2.code === "ENOENT") return emptyDocument3();
|
|
15103
|
+
throw err2;
|
|
15104
|
+
}
|
|
15105
|
+
}
|
|
15106
|
+
async writeDoc(fs5, doc) {
|
|
15107
|
+
await fs5.mkdir(this.dir(), { recursive: true });
|
|
15108
|
+
const fp = this.filePath();
|
|
15109
|
+
const tmp = `${fp}.${process.pid}.${Date.now().toString(36)}.tmp`;
|
|
15110
|
+
try {
|
|
15111
|
+
await fs5.writeFile(tmp, `${JSON.stringify(doc, null, 2)}
|
|
15112
|
+
`, "utf8");
|
|
15113
|
+
const handle = await fs5.open(tmp, "r+");
|
|
15114
|
+
try {
|
|
15115
|
+
await handle.sync();
|
|
15116
|
+
} finally {
|
|
15117
|
+
await handle.close();
|
|
15118
|
+
}
|
|
15119
|
+
await fs5.rename(tmp, fp);
|
|
15120
|
+
} catch (err2) {
|
|
15121
|
+
await fs5.rm(tmp, { force: true }).catch(() => void 0);
|
|
15122
|
+
throw err2;
|
|
15123
|
+
}
|
|
15124
|
+
}
|
|
15125
|
+
async mutate(operation, options = {}) {
|
|
15126
|
+
const directory = this.dir();
|
|
15127
|
+
return withPolicyNamespaceLock(async (session2) => {
|
|
15128
|
+
const fs5 = await import('fs/promises');
|
|
15129
|
+
const doc = await this.readDoc(fs5);
|
|
15130
|
+
if (options.basedOnRevision !== void 0 && options.basedOnRevision !== doc.revision) {
|
|
15131
|
+
throw new HardRulesRegistryError(`Stale hard-rules revision: expected ${options.basedOnRevision}, current ${doc.revision}`);
|
|
15132
|
+
}
|
|
15133
|
+
const outcome = operation(doc);
|
|
15134
|
+
let next = outcome.document;
|
|
15135
|
+
validateDocument4(next);
|
|
15136
|
+
await session2.assertOwnership();
|
|
15137
|
+
await this.writeDoc(fs5, next);
|
|
15138
|
+
return outcome.result;
|
|
15139
|
+
}, { directory });
|
|
15140
|
+
}
|
|
15141
|
+
async listRules() {
|
|
15142
|
+
const doc = await this.load();
|
|
15143
|
+
return [...doc.rules].sort((a, b) => a.id.localeCompare(b.id) || a.version - b.version);
|
|
15144
|
+
}
|
|
15145
|
+
async activeRules(scope) {
|
|
15146
|
+
const doc = await this.load();
|
|
15147
|
+
return doc.rules.filter((r) => r.status === "active" && (!scope || r.scope === "*" || r.scope === scope || scope.startsWith(r.scope.endsWith("/") ? r.scope : r.scope + "/")));
|
|
15148
|
+
}
|
|
15149
|
+
async failClosedResolve(scope) {
|
|
15150
|
+
const actives = await this.activeRules(scope);
|
|
15151
|
+
if (actives.length === 0) return { outcome: "none", rule: null, reason: "no applicable hard rule" };
|
|
15152
|
+
if (actives.length > 1) return { outcome: "conflicting", rule: null, reason: "conflicting active hard rules" };
|
|
15153
|
+
const r = actives[0];
|
|
15154
|
+
if (r.ruleClass === "IMMUTABLE" || r.ruleClass === "OWNER_CONTROLLED") {
|
|
15155
|
+
if (r.authority.kind !== "HI" && r.authority.kind !== "operator") {
|
|
15156
|
+
return { outcome: "unresolved-authority", rule: null, reason: "unresolved authority" };
|
|
15157
|
+
}
|
|
15158
|
+
}
|
|
15159
|
+
return { outcome: "applicable", rule: r, reason: "applicable" };
|
|
15160
|
+
}
|
|
15161
|
+
async assign(input, basedOnRevision) {
|
|
15162
|
+
const authority = validateHardRuleAuthority(input.authority);
|
|
15163
|
+
if (!requiredText3(input.scope)) throw new HardRulesRegistryError("scope is required");
|
|
15164
|
+
if (!requiredText3(input.statement)) throw new HardRulesRegistryError("statement is required");
|
|
15165
|
+
if (!["IMMUTABLE", "OWNER_CONTROLLED", "USER_OVERRIDEABLE", "SESSION"].includes(input.ruleClass)) throw new HardRulesRegistryError("invalid ruleClass");
|
|
15166
|
+
const doc0 = await this.load();
|
|
15167
|
+
const ruleId = input.id ?? `hr.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`;
|
|
15168
|
+
const existing = doc0.rules.filter((r) => r.id === ruleId);
|
|
15169
|
+
const version = input.version ?? (existing.length > 0 ? Math.max(...existing.map((r) => r.version)) + 1 : 1);
|
|
15170
|
+
const priorActive = existing.find((r) => r.status === "active" && r.ruleClass === "IMMUTABLE");
|
|
15171
|
+
if (priorActive && input.ruleClass !== "IMMUTABLE") {
|
|
15172
|
+
throw new HardRulesRegistryError("cannot weaken immutable rule");
|
|
15173
|
+
}
|
|
15174
|
+
if (priorActive && authority.kind !== "HI") {
|
|
15175
|
+
throw new HardRulesRegistryError("immutable rule requires HI authority to amend");
|
|
15176
|
+
}
|
|
15177
|
+
const createdAt = nowIso2();
|
|
15178
|
+
const grantedAt = input.grantedAt ?? createdAt;
|
|
15179
|
+
const rule = {
|
|
15180
|
+
id: ruleId,
|
|
15181
|
+
version,
|
|
15182
|
+
revision: 0,
|
|
15183
|
+
ruleClass: input.ruleClass,
|
|
15184
|
+
status: "proposed",
|
|
15185
|
+
scope: input.scope,
|
|
15186
|
+
statement: input.statement,
|
|
15187
|
+
...input.applicability ? { applicability: input.applicability } : {},
|
|
15188
|
+
authority,
|
|
15189
|
+
grantedAt,
|
|
15190
|
+
createdAt,
|
|
15191
|
+
fingerprint: ""
|
|
15192
|
+
};
|
|
15193
|
+
rule.fingerprint = canonicalFingerprint({ ...rule, fingerprint: "" });
|
|
15194
|
+
return this.mutate((doc) => {
|
|
15195
|
+
const revision = doc.revision + 1;
|
|
15196
|
+
const stored = { ...rule, revision, fingerprint: canonicalFingerprint({ ...rule, revision, fingerprint: "" }) };
|
|
15197
|
+
const rules = doc.rules.map((r) => {
|
|
15198
|
+
if (r.id === stored.id && r.version !== stored.version && r.status === "active") {
|
|
15199
|
+
return { ...r, status: "retired", updatedAt: nowIso2() };
|
|
15200
|
+
}
|
|
15201
|
+
return r;
|
|
15202
|
+
});
|
|
15203
|
+
rules.push(stored);
|
|
15204
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15205
|
+
action: "rule.assigned",
|
|
15206
|
+
at: stored.grantedAt,
|
|
15207
|
+
actorPrincipal: authority.kind,
|
|
15208
|
+
targetRuleId: stored.id,
|
|
15209
|
+
targetVersion: stored.version,
|
|
15210
|
+
scope: stored.scope,
|
|
15211
|
+
outcome: "applied",
|
|
15212
|
+
detail: `hard rule ${stored.id} v${stored.version} assigned (${stored.ruleClass})`,
|
|
15213
|
+
mandatory: true
|
|
15214
|
+
})]);
|
|
15215
|
+
return { document: { ...doc, revision, rules, audit }, result: stored };
|
|
15216
|
+
}, { basedOnRevision });
|
|
15217
|
+
}
|
|
15218
|
+
async activate(input, basedOnRevision) {
|
|
15219
|
+
const doc0 = await this.load();
|
|
15220
|
+
const rule = doc0.rules.find((r) => r.id === input.id && r.version === input.version);
|
|
15221
|
+
if (!rule) throw new HardRulesRegistryError("rule not found");
|
|
15222
|
+
if (rule.status !== "approved" && rule.status !== "proposed") ;
|
|
15223
|
+
const authority = input.authority ? validateHardRuleAuthority(input.authority) : rule.authority;
|
|
15224
|
+
if ((rule.ruleClass === "IMMUTABLE" || rule.ruleClass === "OWNER_CONTROLLED") && authority.kind !== "HI" && authority.kind !== "operator") {
|
|
15225
|
+
throw new HardRulesRegistryError("activation requires HI or operator authority");
|
|
15226
|
+
}
|
|
15227
|
+
const actives = doc0.rules.filter((r) => r.status === "active" && r.scope === rule.scope && r.id !== rule.id);
|
|
15228
|
+
if (actives.length > 0 && (rule.ruleClass === "IMMUTABLE" || rule.ruleClass === "OWNER_CONTROLLED")) {
|
|
15229
|
+
throw new HardRulesRegistryError("conflicting active mandatory rules");
|
|
15230
|
+
}
|
|
15231
|
+
return this.mutate((doc) => {
|
|
15232
|
+
const idx = doc.rules.findIndex((r) => r.id === input.id && r.version === input.version);
|
|
15233
|
+
if (idx < 0) throw new HardRulesRegistryError("rule not found");
|
|
15234
|
+
const current = doc.rules[idx];
|
|
15235
|
+
const rules = doc.rules.map((r) => {
|
|
15236
|
+
if (r.id === current.id && r.version !== current.version && r.status === "active") {
|
|
15237
|
+
return { ...r, status: "retired", updatedAt: nowIso2() };
|
|
15238
|
+
}
|
|
15239
|
+
return r;
|
|
15240
|
+
});
|
|
15241
|
+
const now2 = nowIso2();
|
|
15242
|
+
rules[idx >= rules.length ? rules.length - 1 : idx] = { ...current, status: "active", updatedAt: now2 };
|
|
15243
|
+
const targetIndex = rules.findIndex((r) => r.id === current.id && r.version === current.version);
|
|
15244
|
+
if (targetIndex >= 0) rules[targetIndex] = { ...current, status: "active", updatedAt: now2 };
|
|
15245
|
+
const updated = rules.find((r) => r.id === current.id && r.version === current.version);
|
|
15246
|
+
const revision = doc.revision + 1;
|
|
15247
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15248
|
+
action: "rule.activated",
|
|
15249
|
+
at: now2,
|
|
15250
|
+
actorPrincipal: input.actorPrincipal ?? authority.kind,
|
|
15251
|
+
targetRuleId: updated.id,
|
|
15252
|
+
targetVersion: updated.version,
|
|
15253
|
+
scope: updated.scope,
|
|
15254
|
+
outcome: "applied",
|
|
15255
|
+
mandatory: true
|
|
15256
|
+
})]);
|
|
15257
|
+
return { document: { ...doc, revision, rules, audit }, result: updated };
|
|
15258
|
+
}, { basedOnRevision });
|
|
15259
|
+
}
|
|
15260
|
+
async remove(id, authority, basedOnRevision) {
|
|
15261
|
+
const auth = validateHardRuleAuthority(authority);
|
|
15262
|
+
const doc0 = await this.load();
|
|
15263
|
+
const rule = doc0.rules.find((r) => r.id === id && r.status === "active");
|
|
15264
|
+
if (rule) {
|
|
15265
|
+
if (rule.ruleClass === "IMMUTABLE") throw new HardRulesRegistryError("cannot remove immutable rule");
|
|
15266
|
+
if (rule.ruleClass === "OWNER_CONTROLLED" && auth.kind !== "HI" && auth.kind !== "operator") throw new HardRulesRegistryError("unauthenticated cannot remove owner-controlled rule");
|
|
15267
|
+
}
|
|
15268
|
+
await this.mutate((doc) => {
|
|
15269
|
+
const rules = doc.rules.filter((r) => r.id !== id);
|
|
15270
|
+
if (rules.length === doc.rules.length) return { document: doc, result: void 0 };
|
|
15271
|
+
const revision = doc.revision + 1;
|
|
15272
|
+
const audit = boundedAudit2([...doc.audit, buildAudit({
|
|
15273
|
+
action: "rule.removed",
|
|
15274
|
+
at: nowIso2(),
|
|
15275
|
+
actorPrincipal: auth.kind,
|
|
15276
|
+
targetRuleId: id,
|
|
15277
|
+
scope: rule?.scope,
|
|
15278
|
+
outcome: "applied",
|
|
15279
|
+
mandatory: true
|
|
15280
|
+
})]);
|
|
15281
|
+
return { document: { ...doc, revision, rules, audit }, result: void 0 };
|
|
15282
|
+
}, { basedOnRevision });
|
|
15283
|
+
}
|
|
15284
|
+
async auditTrail() {
|
|
15285
|
+
const doc = await this.load();
|
|
15286
|
+
return [...doc.audit];
|
|
15287
|
+
}
|
|
15288
|
+
async revision() {
|
|
15289
|
+
const doc = await this.load();
|
|
15290
|
+
return doc.revision;
|
|
15291
|
+
}
|
|
15292
|
+
};
|
|
15293
|
+
|
|
15294
|
+
// core/functions/bootstrap.ts
|
|
14929
15295
|
async function checkProjectState(_cwd) {
|
|
14930
15296
|
const userDotMemDir = resolveUserDotMemPath();
|
|
14931
15297
|
const userLogicMemPath = path.join(userDotMemDir, "logic.mem");
|
|
@@ -15064,11 +15430,23 @@ async function ensureUserDotMemInitialized() {
|
|
|
15064
15430
|
}
|
|
15065
15431
|
__name(ensureUserDotMemInitialized, "ensureUserDotMemInitialized");
|
|
15066
15432
|
async function runBootstrap(_cwd) {
|
|
15433
|
+
let hardRulesReady = false;
|
|
15434
|
+
let hardRulesRevision = 0;
|
|
15067
15435
|
try {
|
|
15068
15436
|
await ensureUserDotMemInitialized();
|
|
15069
15437
|
} catch (error) {
|
|
15070
15438
|
console.error(`dotmem initialization needs attention: ${error instanceof Error ? error.message : String(error)}`);
|
|
15071
15439
|
}
|
|
15440
|
+
try {
|
|
15441
|
+
const registry = new HardRulesRegistry();
|
|
15442
|
+
const doc = await registry.load();
|
|
15443
|
+
await registry.listRules();
|
|
15444
|
+
hardRulesReady = true;
|
|
15445
|
+
hardRulesRevision = doc.revision;
|
|
15446
|
+
} catch (error) {
|
|
15447
|
+
hardRulesReady = false;
|
|
15448
|
+
console.error(`hard-rules registry needs attention: ${error instanceof Error ? error.message : String(error)}`);
|
|
15449
|
+
}
|
|
15072
15450
|
try {
|
|
15073
15451
|
const projectId = await resolveProjectIdentity(process.cwd());
|
|
15074
15452
|
await ensureProjectAnchor(projectId);
|
|
@@ -15081,7 +15459,9 @@ async function runBootstrap(_cwd) {
|
|
|
15081
15459
|
ai: await detectAiEnvironment(),
|
|
15082
15460
|
language: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15083
15461
|
userLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15084
|
-
responseLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT"
|
|
15462
|
+
responseLanguage: (await Promise.resolve().then(() => (init_knowledge(), knowledge_exports))).resolveUserLanguage?.() ?? "it-IT",
|
|
15463
|
+
hardRulesReady,
|
|
15464
|
+
hardRulesRevision
|
|
15085
15465
|
};
|
|
15086
15466
|
}
|
|
15087
15467
|
__name(runBootstrap, "runBootstrap");
|
package/index.cjs
CHANGED
|
@@ -14180,7 +14180,7 @@ var EPITEMIC_STATUSES2 = [
|
|
|
14180
14180
|
|
|
14181
14181
|
// app.json
|
|
14182
14182
|
var app_default = {
|
|
14183
|
-
version: "0.1.
|
|
14183
|
+
version: "0.1.5"};
|
|
14184
14184
|
|
|
14185
14185
|
// index.ts
|
|
14186
14186
|
var dotmem = {
|
package/index.js
CHANGED
|
@@ -14176,7 +14176,7 @@ var EPITEMIC_STATUSES2 = [
|
|
|
14176
14176
|
|
|
14177
14177
|
// app.json
|
|
14178
14178
|
var app_default = {
|
|
14179
|
-
version: "0.1.
|
|
14179
|
+
version: "0.1.5"};
|
|
14180
14180
|
|
|
14181
14181
|
// index.ts
|
|
14182
14182
|
var dotmem = {
|
package/mcp.cjs
CHANGED
package/mcp.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotmem",
|
|
3
3
|
"codeName": "dotmem",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "The AI memory layer for agents and apps: acquired experience, discovery, context projection, continuity, and logic engine. Built on Memorio's state+store foundation.",
|
|
@@ -76,8 +76,8 @@
|
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"@modelcontextprotocol/server": "2.4.0",
|
|
79
|
-
"
|
|
80
|
-
"
|
|
79
|
+
"@cyclonedx/cyclonedx-npm": "^6.0.1",
|
|
80
|
+
"@types/node": "^26.6.6"
|
|
81
81
|
},
|
|
82
82
|
"overrides": {
|
|
83
83
|
"glob": "^14.0.2",
|