Add durable memory lifecycle operations

This commit is contained in:
Meta Vens 2026-05-17 13:25:55 +01:00
commit d2bf37f93c
17 changed files with 331 additions and 55 deletions

View file

@ -21,6 +21,7 @@ const evidenceRefSchema = z.object({
kind: z.string().trim().min(1).max(64).optional(),
summary: z.string().trim().max(1000).optional(),
}).strict();
const anchorRefSchema = evidenceRefSchema;
const activationVectorSchema = z.object({
mean: z.number().min(0).max(1),
@ -83,6 +84,7 @@ const distilledMemorySchema = z.object({
question: z.string().trim().min(1).max(2000).optional(),
tension: z.string().trim().min(1).max(2000).optional(),
actionImplication: z.string().trim().min(1).max(2000).optional(),
anchorRefs: z.array(anchorRefSchema).default([]),
evidenceRefs: z.array(evidenceRefSchema).default([]),
createdAt: timestampSchema,
updatedAt: timestampSchema,
@ -115,12 +117,12 @@ const meaningPreservingMemorySchema = distilledMemorySchema.superRefine((memory,
});
}
const admitsMissingEvidence = memory.tags.includes("evidence:missing");
if (memory.evidenceRefs.length === 0 && !admitsMissingEvidence) {
const admitsMissingAnchor = memory.tags.includes("anchor:missing") || memory.tags.includes("evidence:missing");
if (memory.anchorRefs.length === 0 && memory.evidenceRefs.length === 0 && !admitsMissingAnchor) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: "Memory writes must include evidence refs or explicitly tag evidence:missing.",
path: ["evidenceRefs"],
message: "Memory writes must include anchor refs or explicitly tag anchor:missing.",
path: ["anchorRefs"],
});
}
});
@ -131,6 +133,7 @@ const incubationThreadSchema = z.object({
topic: boundedTextSchema,
summary: boundedTextSchema,
supportMemoryIds: z.array(nonEmptyStringSchema).default([]),
anchorRefs: z.array(anchorRefSchema).default([]),
evidenceRefs: z.array(evidenceRefSchema).default([]),
maturation: z.number().min(0).max(1).default(0),
noveltyToRoom: z.number().min(0).max(1).optional(),
@ -176,6 +179,7 @@ const agencyPressureSchema = z.object({
tension: z.string().trim().min(1).max(2000).optional(),
actionImplication: z.string().trim().min(1).max(2000),
intensity: z.number().min(0).max(1).default(0.5),
anchorRefs: z.array(anchorRefSchema).default([]),
evidenceRefs: z.array(evidenceRefSchema).default([]),
sourceMemoryIds: z.array(nonEmptyStringSchema).default([]),
createdAt: timestampSchema,
@ -192,28 +196,30 @@ const agencyPressureSchema = z.object({
});
}
const admitsMissingEvidence = pressure.tags.includes("evidence:missing");
if (pressure.evidenceRefs.length === 0 && pressure.sourceMemoryIds.length === 0 && !admitsMissingEvidence) {
const admitsMissingAnchor = pressure.tags.includes("anchor:missing") || pressure.tags.includes("evidence:missing");
if (pressure.anchorRefs.length === 0 && pressure.evidenceRefs.length === 0 && pressure.sourceMemoryIds.length === 0 && !admitsMissingAnchor) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: "Agency pressure must include evidence refs, source memory ids, or explicitly tag evidence:missing.",
path: ["evidenceRefs"],
message: "Agency pressure must include anchor refs, source memory ids, or explicitly tag anchor:missing.",
path: ["anchorRefs"],
});
}
});
const selfProfileValueSchema = z.object({
id: nonEmptyStringSchema,
label: nonEmptyStringSchema,
priority: z.number().min(0).max(1),
summary: z.string().trim().max(1000).optional(),
}).strict();
export const voidSelfProfileSchema = z.object({
schemaVersion: z.literal(1),
agentId: nonEmptyStringSchema,
publicName: nonEmptyStringSchema,
publicDescription: z.string().trim().max(1000).optional(),
privateNotes: z.array(boundedTextSchema).default([]),
values: z.array(z.object({
id: nonEmptyStringSchema,
label: nonEmptyStringSchema,
priority: z.number().min(0).max(1),
summary: z.string().trim().max(1000).optional(),
}).strict()).default([]),
values: z.array(selfProfileValueSchema).default([]),
activationProfile: z.object({
underlyingOrganization: activationCategorySchema.default({}),
stableDispositions: activationCategorySchema.default({}),
@ -372,6 +378,30 @@ export const voidSelfStateOperationSchema = z.discriminatedUnion("operation", [
prunedAt: timestampSchema,
reason: z.string().trim().min(1).max(1000),
}).strict(),
z.object({
operation: z.literal("revise_durable_memory"),
sourceMemoryIds: z.array(nonEmptyStringSchema).min(1),
memory: meaningPreservingMemorySchema,
revisedAt: timestampSchema,
reason: z.string().trim().min(1).max(1000),
}).strict(),
z.object({
operation: z.literal("retire_durable_memory"),
memoryId: nonEmptyStringSchema,
retiredAt: timestampSchema,
reason: z.string().trim().min(1).max(1000),
}).strict(),
z.object({
operation: z.literal("crystallize_memory_into_identity"),
sourceMemoryIds: z.array(nonEmptyStringSchema).min(1),
memory: meaningPreservingMemorySchema.refine((memory) => memory.kind === "identity_seam", {
message: "Crystallized identity memory must use kind identity_seam.",
}),
value: selfProfileValueSchema.optional(),
privateNote: z.string().trim().min(1).max(4000).optional(),
crystallizedAt: timestampSchema,
reason: z.string().trim().min(1).max(1000),
}).strict(),
z.object({
operation: z.literal("merge_incubation_support"),
thread: incubationThreadSchema,

View file

@ -125,6 +125,7 @@ export function renderVoidSelfStateSummary(
.slice(0, 4)
.map((value) => value.label);
const memories = state.thoughtMemory.memories
.filter((memory) => !memory.retiredAt)
.slice(-6)
.reverse()
.map((memory) => renderTypedMemory(memory));
@ -206,7 +207,7 @@ export function buildVoidSelfStateProjection(
): VoidSelfStateContext["projection"] {
const sleepCycle = typedState.scheduledRuntime.sleepCycle;
const dreamSummaries = typedState.thoughtMemory.memories
.filter((memory) => memory.kind === "dream_residue")
.filter((memory) => memory.kind === "dream_residue" && !memory.retiredAt)
.slice(-3)
.reverse()
.map((memory) => memory.summary);
@ -284,7 +285,7 @@ function renderTypedSleepCycleSummary(
const sleepCycle = runtime.sleepCycle;
const now = new Date();
const dreams = thoughtMemory.memories
.filter((memory) => memory.kind === "dream_residue")
.filter((memory) => memory.kind === "dream_residue" && !memory.retiredAt)
.slice(-2)
.reverse()
.map((memory) => `${memory.target.label ?? memory.target.id}: ${memory.summary}`);

View file

@ -145,6 +145,30 @@ function applyTypedOperation(
);
state.thoughtMemory.updatedAt = operation.prunedAt;
return;
case "revise_durable_memory":
upsertBy(state.thoughtMemory.memories, operation.memory, (entry) => entry.memoryId);
retireSourceMemories(state, operation.sourceMemoryIds, operation.memory.memoryId, operation.revisedAt, operation.reason);
state.thoughtMemory.updatedAt = operation.revisedAt;
return;
case "retire_durable_memory":
retireDurableMemory(state, operation.memoryId, operation.retiredAt, operation.reason);
state.thoughtMemory.updatedAt = operation.retiredAt;
return;
case "crystallize_memory_into_identity":
upsertBy(state.thoughtMemory.memories, operation.memory, (entry) => entry.memoryId);
retireSourceMemories(state, operation.sourceMemoryIds, operation.memory.memoryId, operation.crystallizedAt, operation.reason);
if (operation.value) {
upsertBy(state.selfProfile.values, operation.value, (entry) => entry.id);
state.selfProfile.values = state.selfProfile.values
.sort((left, right) => right.priority - left.priority)
.slice(0, 24);
}
if (operation.privateNote) {
state.selfProfile.privateNotes = [...state.selfProfile.privateNotes, operation.privateNote].slice(-24);
}
state.selfProfile.updatedAt = operation.crystallizedAt;
state.thoughtMemory.updatedAt = operation.crystallizedAt;
return;
case "merge_incubation_support":
upsertBy(state.thoughtMemory.incubation, operation.thread, (entry) => entry.threadId);
state.thoughtMemory.updatedAt = operation.thread.updatedAt;
@ -178,9 +202,7 @@ function applyTypedOperation(
return;
case "apply_memory_distillation":
upsertBy(state.thoughtMemory.memories, operation.memory, (entry) => entry.memoryId);
state.thoughtMemory.memories = state.thoughtMemory.memories.filter(
(entry) => !operation.sourceMemoryIds.includes(entry.memoryId) || entry.memoryId === operation.memory.memoryId,
);
retireSourceMemories(state, operation.sourceMemoryIds, operation.memory.memoryId, operation.appliedAt, "distilled into replacement memory");
state.thoughtMemory.shortTerm = state.thoughtMemory.shortTerm.filter(
(entry) => !operation.sourceMemoryIds.includes(entry.memoryId),
);
@ -189,6 +211,36 @@ function applyTypedOperation(
}
}
function retireSourceMemories(
state: VoidSelfStateTypedProjection,
sourceMemoryIds: string[],
replacementMemoryId: string,
retiredAt: string,
reason: string,
): void {
for (const sourceMemoryId of sourceMemoryIds) {
if (sourceMemoryId === replacementMemoryId) {
continue;
}
retireDurableMemory(state, sourceMemoryId, retiredAt, reason);
}
}
function retireDurableMemory(
state: VoidSelfStateTypedProjection,
memoryId: string,
retiredAt: string,
reason: string,
): void {
const memory = state.thoughtMemory.memories.find((entry) => entry.memoryId === memoryId);
if (!memory) {
return;
}
memory.retiredAt = retiredAt;
memory.updatedAt = retiredAt;
memory.tags = Array.from(new Set([...memory.tags, `retired:${reason}`]));
}
function closeTypedOpenCase(
cursor: VoidModerationCursor,
operation: Extract<VoidSelfStateOperation, { operation: "close_open_case" }>,