feat(memory): add numbered compaction refs

This commit is contained in:
Ralph Chang
2026-05-08 12:18:39 +08:00
parent c538381969
commit 09880c1840
26 changed files with 3030 additions and 106 deletions
+16 -3
View File
@@ -9,6 +9,8 @@ export function usage(): string {
memory-diag rejected [--workspace <path>] [--verbose] [--json]
memory-diag missing [--workspace <path>] [--verbose] [--json]
memory-diag explain [memory-id] [--workspace <path>] [--raw]
memory-diag commands [--workspace <path>] [--verbose] [--json]
memory-diag revert (--memory <replacement-id> | --event <event-id>) [--workspace <path>] [--apply]
Global options:
--workspace <path> Workspace path (default: current directory)
@@ -58,6 +60,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
else if (arg === "--trigger-only") options.triggerOnly = true;
else if (arg === "--include-historical") options.includeHistorical = true;
else if (arg === "--explain") options.explain = true;
else if (arg === "--apply") options.apply = true;
else if (arg === "--workspace") {
const value = rest[++i];
if (!value) return error("--workspace requires a path");
@@ -78,6 +81,10 @@ export function parseArgs(argv: string[]): ParsedArgs {
const value = rest[++i];
if (!value) return error("--memory requires an id");
options.memory = value;
} else if (arg === "--event") {
const value = rest[++i];
if (!value) return error("--event requires an id");
options.event = value;
} else if (!arg.startsWith("--") && command === "explain") {
options.positional?.push(arg);
} else {
@@ -96,12 +103,12 @@ export function parseArgs(argv: string[]): ParsedArgs {
if (command === "status") {
if (options.all) return error(`${command} does not accept --all`);
} else if (command === "rejected" || command === "missing" || command === "coverage" || command === "explain") {
} else if (command === "rejected" || command === "missing" || command === "coverage" || command === "explain" || command === "commands" || command === "revert") {
if (options.all) return error(`${command} does not accept --all`);
} else {
if (options.all || options.workspace) return error(`${command} does not accept --all or --workspace`);
}
if (options.json && command !== "status" && command !== "rejected" && command !== "missing" && command !== "coverage") {
if (options.json && command !== "status" && command !== "rejected" && command !== "missing" && command !== "coverage" && command !== "commands") {
return error(`${command} does not accept --json`);
}
if (command !== "rejected" && (options.softOnly || options.triggerOnly || options.since)) {
@@ -113,9 +120,15 @@ export function parseArgs(argv: string[]): ParsedArgs {
if (command !== "audit" && options.migration) {
return error(`${command} does not accept --migration`);
}
if (command !== "explain" && options.memory) {
if (command !== "explain" && command !== "revert" && options.memory) {
return error(`${command} does not accept --memory`);
}
if (command !== "revert" && options.event) return error(`${command} does not accept --event`);
if (command !== "revert" && options.apply) return error(`${command} does not accept --apply`);
if (command === "revert") {
if (!options.memory && !options.event) return error("revert requires --memory or --event");
if (options.memory && options.event) return error("Use either --memory or --event, not both");
}
if (command === "rejected" && options.since && !isValidSince(options.since)) {
return error(`Invalid --since value: ${options.since}`);
}
+1 -1
View File
@@ -1,4 +1,4 @@
export const VISIBLE_COMMANDS = ["status", "rejected", "missing", "explain"] as const;
export const VISIBLE_COMMANDS = ["status", "rejected", "missing", "explain", "commands", "revert"] as const;
export const HIDDEN_COMMANDS = ["coverage", "audit"] as const;
export type VisibleCommand = typeof VISIBLE_COMMANDS[number];
+4
View File
@@ -1,8 +1,10 @@
import { runAudit } from "./commands/audit.ts";
import { runCommands } from "./commands/commands.ts";
import { runCoverage } from "./commands/coverage.ts";
import { runExplain } from "./commands/explain.ts";
import { runMissing } from "./commands/missing.ts";
import { runRejected } from "./commands/rejected.ts";
import { runRevert } from "./commands/revert.ts";
import { runStatus } from "./commands/status.ts";
import type { CliOptions, Command, CommandResult } from "./types.ts";
@@ -14,5 +16,7 @@ export async function dispatch(command: Command, options: CliOptions): Promise<C
case "coverage": return runCoverage(options);
case "audit": return runAudit(options);
case "explain": return runExplain(options);
case "commands": return runCommands(options);
case "revert": return runRevert(options);
}
}
+187
View File
@@ -0,0 +1,187 @@
import { queryEvidenceEvents, type EvidenceEventV1, type EvidenceOutcome } from "../../../src/evidence-log.ts";
import { objectFromCounts, sortedCounts } from "../text.ts";
import type { CliOptions, CommandResult } from "../types.ts";
type CommandKind = "reinforce" | "replace";
type MemoryCommandSummary = {
version: 1;
generatedAt: string;
compactionsWithCommandEvidence: number;
commands: Record<CommandKind, number>;
outcomes: Record<"reinforced" | "superseded" | "rejected" | "blocked", number>;
invalidMalformedCommands: number;
replacements: {
sameType: number;
crossType: number;
};
protectedReplacements: {
total: number;
protectedReinforcedTarget: number;
protectedMemorySource: number;
};
rejectionReasons: Record<string, number>;
latestEvents: Array<{
eventId: string;
createdAt: string;
type: string;
outcome: EvidenceOutcome;
ref?: string;
memoryId?: string;
reasonCodes: string[];
textPreview?: string;
}>;
};
const INVALID_COMMAND_REASONS = new Set([
"invalid_memory_command",
"invalid_memory_ref",
"invalid_memory_type",
"empty_replacement_text",
]);
function hasReason(event: EvidenceEventV1, reason: string): boolean {
return event.reasonCodes.includes(reason);
}
function isInvalidMalformedCommandEvent(event: EvidenceEventV1): boolean {
return event.type === "extraction_candidate_rejected"
&& event.reasonCodes.some(reason => INVALID_COMMAND_REASONS.has(reason));
}
function isParsedCommandEvent(event: EvidenceEventV1): boolean {
return event.type === "memory_reinforced" || event.type === "memory_replaced_numbered_ref";
}
function isManualRevertEvent(event: EvidenceEventV1): boolean {
return event.type === "memory_reverted_numbered_ref";
}
function isCommandEvidenceEvent(event: EvidenceEventV1): boolean {
return isParsedCommandEvent(event) || isInvalidMalformedCommandEvent(event) || isManualRevertEvent(event);
}
function refFromEvent(event: EvidenceEventV1): string | undefined {
const ref = event.details?.ref;
return typeof ref === "string" ? ref : undefined;
}
function latestEventJSON(event: EvidenceEventV1): MemoryCommandSummary["latestEvents"][number] {
return {
eventId: event.eventId,
createdAt: event.createdAt,
type: event.type,
outcome: event.outcome,
ref: refFromEvent(event),
memoryId: event.memory?.memoryId,
reasonCodes: event.reasonCodes,
textPreview: event.textPreview,
};
}
export function buildMemoryCommandSummary(events: EvidenceEventV1[], generatedAt = new Date().toISOString()): MemoryCommandSummary {
const commandEvents = events.filter(isCommandEvidenceEvent);
const compactionCommandEvents = commandEvents.filter(event => !isManualRevertEvent(event));
const parsedEvents = compactionCommandEvents.filter(isParsedCommandEvent);
const invalidEvents = compactionCommandEvents.filter(isInvalidMalformedCommandEvent);
const sessions = new Set(compactionCommandEvents.map(event => event.sessionHash).filter((value): value is string => typeof value === "string" && value.length > 0));
const replacementSuccesses = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && event.outcome === "superseded");
const rejectedCommandEvents = commandEvents.filter(event => event.outcome === "rejected");
const rejectionReasonCounts = new Map<string, number>();
for (const event of rejectedCommandEvents) {
for (const reason of event.reasonCodes) {
rejectionReasonCounts.set(reason, (rejectionReasonCounts.get(reason) ?? 0) + 1);
}
}
const protectedReinforcedTarget = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && hasReason(event, "protected_reinforced_target")).length;
const protectedMemorySource = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && hasReason(event, "protected_memory_source")).length;
const parsedRejected = parsedEvents.filter(event => event.outcome === "rejected").length;
return {
version: 1,
generatedAt,
compactionsWithCommandEvidence: sessions.size > 0 ? sessions.size : compactionCommandEvents.length > 0 ? 1 : 0,
commands: {
reinforce: parsedEvents.filter(event => event.type === "memory_reinforced").length,
replace: parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref").length,
},
outcomes: {
reinforced: parsedEvents.filter(event => event.outcome === "reinforced").length,
superseded: parsedEvents.filter(event => event.outcome === "superseded").length,
rejected: parsedRejected,
blocked: parsedRejected,
},
invalidMalformedCommands: invalidEvents.length,
replacements: {
sameType: replacementSuccesses.filter(event => hasReason(event, "same_type_replace")).length,
crossType: replacementSuccesses.filter(event => hasReason(event, "cross_type_replace")).length,
},
protectedReplacements: {
total: parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && (hasReason(event, "protected_reinforced_target") || hasReason(event, "protected_memory_source"))).length,
protectedReinforcedTarget,
protectedMemorySource,
},
rejectionReasons: objectFromCounts(rejectionReasonCounts),
latestEvents: commandEvents.slice(-10).reverse().map(latestEventJSON),
};
}
function formatReasonCounts(rejectionReasons: Record<string, number>): string[] {
const counts = new Map(Object.entries(rejectionReasons));
const rows = sortedCounts(counts);
if (rows.length === 0) return [" (none)"];
return rows.map(([reason, count]) => ` - ${reason}: ${count}`);
}
function formatLatestEvents(events: MemoryCommandSummary["latestEvents"]): string[] {
if (events.length === 0) return [" (none)"];
return events.map(event => {
const ref = event.ref ? ` ref=${event.ref}` : "";
const memoryId = event.memoryId ? ` memory=${event.memoryId}` : "";
const textPreview = event.textPreview ? ` text=${JSON.stringify(event.textPreview)}` : "";
return ` - ${event.createdAt} ${event.type} ${event.outcome}${ref}${memoryId} reasons=${event.reasonCodes.join(",") || "none"}${textPreview}`;
});
}
export function formatMemoryCommandSummary(summary: MemoryCommandSummary, options: Pick<CliOptions, "verbose" | "noEmoji"> = {}): string {
const warning = options.noEmoji ? "!" : "⚠";
const lines = [
"Memory command diagnostics",
"",
"Key metrics:",
` - compactions with command evidence: ${summary.compactionsWithCommandEvidence}`,
` - reinforce: ${summary.commands.reinforce}`,
` - replace: ${summary.commands.replace}`,
` - reinforced: ${summary.outcomes.reinforced}`,
` - superseded: ${summary.outcomes.superseded}`,
` - rejected: ${summary.outcomes.rejected}`,
` - blocked: ${summary.outcomes.blocked}`,
` - invalid/malformed commands: ${summary.invalidMalformedCommands}`,
` - same-type replacements: ${summary.replacements.sameType}`,
` - cross-type replacements: ${summary.replacements.crossType}`,
` - ${warning} Protected REPLACE blocked: ${summary.protectedReplacements.total} (reinforced: ${summary.protectedReplacements.protectedReinforcedTarget}, source: ${summary.protectedReplacements.protectedMemorySource})`,
"",
"Rejection reasons:",
...formatReasonCounts(summary.rejectionReasons),
];
if (options.verbose) {
lines.push("", "Latest command events:", ...formatLatestEvents(summary.latestEvents));
}
return lines.join("\n");
}
export async function runCommands(options: CliOptions): Promise<CommandResult> {
const root = options.workspace ?? process.cwd();
const events = await queryEvidenceEvents(root);
const summary = buildMemoryCommandSummary(events);
if (options.json) {
return { stdout: JSON.stringify(summary, null, 2) };
}
return { stdout: formatMemoryCommandSummary(summary, options) };
}
+193
View File
@@ -0,0 +1,193 @@
import { appendEvidenceEvents, queryEvidenceEvents, type EvidenceEventInput, type EvidenceEventV1, type MemoryEvidenceRef } from "../../../src/evidence-log.ts";
import { workspaceMemoryPath } from "../../../src/paths.ts";
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "../../../src/types.ts";
import { updateWorkspaceMemoryWithAccounting } from "../../../src/workspace-memory.ts";
import { readJSONFile } from "../io.ts";
import { cleanText, truncate } from "../text.ts";
import { CliInputError, type CliOptions, type CommandResult } from "../types.ts";
type ReplacementLink = {
event: EvidenceEventV1;
originalId: string;
replacementId: string;
};
type RevertPlan = ReplacementLink & {
original: LongTermMemoryEntry;
replacement: LongTermMemoryEntry;
};
function reject(message: string): never {
throw new CliInputError(`revert rejected: ${message}`);
}
function memoryRef(memory: LongTermMemoryEntry, status: LongTermMemoryEntry["status"] = memory.status): MemoryEvidenceRef {
return {
memoryId: memory.id,
type: memory.type,
source: memory.source,
status,
};
}
function replacementIdFromEvent(event: EvidenceEventV1): string | undefined {
return event.relations?.find(relation => relation.role === "superseded_by")?.memory?.memoryId;
}
function originalIdFromEvent(event: EvidenceEventV1): string | undefined {
return event.memory?.memoryId
?? event.relations?.find(relation => relation.role === "superseded")?.memory?.memoryId;
}
function replacementLinkFromEvent(event: EvidenceEventV1): ReplacementLink {
if (event.type !== "memory_replaced_numbered_ref") {
reject(`event ${event.eventId} is not a memory_replaced_numbered_ref event`);
}
if (event.outcome !== "superseded") {
reject(`event ${event.eventId} is not a successful numbered replacement`);
}
if (!event.reasonCodes.includes("numbered_ref_replace")) {
reject(`event ${event.eventId} is not a numbered replacement`);
}
const originalId = originalIdFromEvent(event);
const replacementId = replacementIdFromEvent(event);
if (!originalId || !replacementId) {
reject(`event ${event.eventId} does not identify original and replacement memories`);
}
return { event, originalId, replacementId };
}
function selectReplacementLink(events: EvidenceEventV1[], options: CliOptions): ReplacementLink {
if (options.event) {
const event = events.find(item => item.eventId === options.event);
if (!event) reject(`event ${options.event} was not found`);
return replacementLinkFromEvent(event);
}
const memoryId = options.memory;
if (!memoryId) reject("missing --memory or --event selector");
const matches = events
.filter(event => event.type === "memory_replaced_numbered_ref")
.filter(event => replacementIdFromEvent(event) === memoryId);
if (matches.length === 0) {
reject(`replacement memory ${memoryId} was not created by memory_replaced_numbered_ref`);
}
if (matches.length > 1) {
reject(`replacement memory ${memoryId} has ${matches.length} replacement events; use --event`);
}
return replacementLinkFromEvent(matches[0]);
}
function validatePlan(link: ReplacementLink, store: WorkspaceMemoryStore): RevertPlan {
const byId = new Map(store.entries.map(entry => [entry.id, entry]));
const original = byId.get(link.originalId);
const replacement = byId.get(link.replacementId);
if (!original) reject(`original memory ${link.originalId} is missing`);
if (!replacement) reject(`replacement memory ${link.replacementId} is missing`);
if (original.status !== "superseded") reject(`original memory ${original.id} is not superseded`);
if (replacement.status !== "active") reject(`replacement memory ${replacement.id} is not active`);
const laterSuperseder = store.entries.find(entry =>
entry.status === "active"
&& entry.id !== original.id
&& entry.id !== replacement.id
&& (entry.supersedes ?? []).includes(replacement.id)
);
if (laterSuperseder) {
reject(`replacement memory ${replacement.id} is superseded by active memory ${laterSuperseder.id}`);
}
return { ...link, original, replacement };
}
async function dryRunPlan(root: string, link: ReplacementLink): Promise<RevertPlan> {
const rawStore = await readJSONFile<WorkspaceMemoryStore>(await workspaceMemoryPath(root));
const store: WorkspaceMemoryStore = rawStore ?? {
version: 1,
workspace: { root, key: "" },
limits: { maxRenderedChars: 0, maxEntries: 0 },
entries: [],
migrations: [],
updatedAt: new Date(0).toISOString(),
};
return validatePlan(link, store);
}
function revertEvidence(plan: RevertPlan): EvidenceEventInput {
const replacement = { ...plan.replacement, status: "superseded" as const };
const original = { ...plan.original, status: "active" as const };
return {
type: "memory_reverted_numbered_ref",
phase: "storage",
outcome: "recovered",
memory: memoryRef(replacement, "superseded"),
relations: [
{ role: "superseded", memory: memoryRef(replacement, "superseded") },
{ role: "recovered", memory: memoryRef(original, "active") },
],
reasonCodes: ["manual_revert_numbered_ref"],
details: {
replacementEventId: plan.event.eventId,
replacementMemoryId: plan.replacementId,
restoredMemoryId: plan.originalId,
},
textPreview: original.text,
};
}
async function applyPlan(root: string, link: ReplacementLink): Promise<RevertPlan> {
let applied: RevertPlan | undefined;
const updateResult = await updateWorkspaceMemoryWithAccounting(root, store => {
const plan = validatePlan(link, store);
const nowIso = new Date().toISOString();
applied = {
...plan,
original: { ...plan.original, status: "active", updatedAt: nowIso },
replacement: { ...plan.replacement, status: "superseded", updatedAt: nowIso },
};
return {
...store,
entries: store.entries.map(entry => {
if (entry.id === plan.originalId) return applied!.original;
if (entry.id === plan.replacementId) return applied!.replacement;
return entry;
}),
updatedAt: nowIso,
lastActivityAt: nowIso,
};
});
if (!applied) reject("unable to apply revert");
await appendEvidenceEvents(root, [...updateResult.evidence, revertEvidence(applied)]);
return applied;
}
function formatPlan(plan: RevertPlan, applied: boolean): string {
const heading = applied ? "Memory revert applied" : "Memory revert dry run";
const nextStep = applied ? "Changes applied." : "No changes applied. Re-run with --apply to mutate workspace memory.";
return [
heading,
"",
"Planned changes:",
` - replacement: ${plan.replacementId} active -> superseded`,
` - original: ${plan.originalId} superseded -> active`,
` - replacement event: ${plan.event.eventId}`,
` - restored text: ${truncate(cleanText(plan.original.text, false), 100)}`,
"",
nextStep,
].join("\n");
}
export async function runRevert(options: CliOptions): Promise<CommandResult> {
const root = options.workspace ?? process.cwd();
const events = await queryEvidenceEvents(root);
const link = selectReplacementLink(events, options);
const plan = options.apply ? await applyPlan(root, link) : await dryRunPlan(root, link);
return { stdout: formatPlan(plan, options.apply === true) };
}
+2
View File
@@ -67,6 +67,8 @@ export type CliOptions = {
since?: string;
migration?: string;
memory?: string;
event?: string;
apply?: boolean;
positional?: string[];
auditMode?: "coverage" | "migrations";
};