Add Persona stress response state

This commit is contained in:
Meta Vens 2026-06-05 23:28:51 +01:00
commit 2ada3fe4a7
13 changed files with 245 additions and 3 deletions

View file

@ -409,6 +409,36 @@ const faceSocialBiasSchema = z.object({
updatedAt: timestampSchema,
}).strict();
const faceStressResponseSchema = z.object({
responseId: nonEmptyStringSchema,
status: z.enum(["active", "cooling", "retired"]).default("active"),
trigger: z.string().trim().min(1).max(1000),
summary: boundedTextSchema,
cognitiveDegradation: z.string().trim().min(1).max(2000),
affectiveSignature: z.string().trim().min(1).max(2000),
constraintLoss: z.string().trim().min(1).max(2000),
behavioralLeak: z.string().trim().min(1).max(2000),
recoveryPath: z.string().trim().min(1).max(2000),
intensity: z.number().min(0).max(1).default(0.5),
threshold: z.number().min(0).max(1).default(0.7),
anchorRefs: z.array(anchorRefSchema).default([]),
evidenceRefs: z.array(evidenceRefSchema).default([]),
sourceMemoryIds: z.array(nonEmptyStringSchema).default([]),
createdAt: timestampSchema,
updatedAt: timestampSchema,
retiredAt: timestampSchema.optional(),
tags: z.array(nonEmptyStringSchema).default([]),
}).strict().superRefine((response, context) => {
const admitsMissingAnchor = response.tags.includes("anchor:missing") || response.tags.includes("evidence:missing");
if (response.anchorRefs.length === 0 && response.evidenceRefs.length === 0 && response.sourceMemoryIds.length === 0 && !admitsMissingAnchor) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: "Stress responses must include anchor refs, source memory ids, or explicitly tag anchor:missing.",
path: ["anchorRefs"],
});
}
});
const faceDoctrineStanceSchema = z.object({
stanceId: nonEmptyStringSchema,
doctrine: nonEmptyStringSchema,
@ -562,6 +592,7 @@ export const voidPersonaAffectSchema = z.object({
statusReads: z.array(faceStatusReadSchema).default([]),
moodDimensions: z.array(faceMoodDimensionSchema).default([]),
socialBiases: z.array(faceSocialBiasSchema).default([]),
stressResponses: z.array(faceStressResponseSchema).default([]),
doctrineStances: z.array(faceDoctrineStanceSchema).default([]),
updatedAt: timestampSchema,
}).strict();
@ -772,6 +803,11 @@ export const voidSelfStateOperationSchema = z.discriminatedUnion("operation", [
biases: z.array(faceSocialBiasSchema).min(1),
updatedAt: timestampSchema,
}).strict(),
z.object({
operation: z.literal("update_stress_responses"),
responses: z.array(faceStressResponseSchema).min(1),
updatedAt: timestampSchema,
}).strict(),
z.object({
operation: z.literal("upsert_doctrine_stance"),
stance: faceDoctrineStanceSchema,

View file

@ -143,6 +143,7 @@ export function createEmptyVoidSelfState(
statusReads: [],
moodDimensions: [],
socialBiases: [],
stressResponses: [],
doctrineStances: [],
updatedAt: createdAt,
}),
@ -323,8 +324,12 @@ function renderPersonaAffectSummary(
.slice()
.sort((left, right) => right.value - left.value)
.slice(0, 8);
const stressResponses = affect.stressResponses
.filter((response) => response.status !== "retired")
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 4);
if (needs.length === 0 && bonds.length === 0 && reads.length === 0 && dimensions.length === 0) {
if (needs.length === 0 && bonds.length === 0 && reads.length === 0 && dimensions.length === 0 && stressResponses.length === 0) {
return `- What ${identityName} feels and wants: no explicit affect state yet.`;
}
@ -358,6 +363,17 @@ function renderPersonaAffectSummary(
dimensions.length > 0
? `- Mood dimensions: ${dimensions.map((dimension) => `${dimension.name}=${dimension.value.toFixed(2)}`).join(", ")}`
: undefined,
...stressResponses.map((response) =>
[
`- Stress response/${response.responseId} (${response.intensity.toFixed(2)}, threshold ${response.threshold.toFixed(2)}): ${response.summary}`,
` Trigger: ${response.trigger}`,
` Cognition under pressure: ${response.cognitiveDegradation}`,
` Affective signature: ${response.affectiveSignature}`,
` Constraint loss: ${response.constraintLoss}`,
` Behavioral leak: ${response.behavioralLeak}`,
` Recovery path: ${response.recoveryPath}`,
].join("\n"),
),
]
.filter((line): line is string => typeof line === "string")
.join("\n");
@ -429,6 +445,16 @@ export function buildVoidSelfStateProjection(
value: dimension.value,
source: dimension.source,
})),
stressResponses: typedState.personaAffect.stressResponses
.filter((response) => response.status !== "retired")
.slice(0, 8)
.map((response) => ({
id: response.responseId,
trigger: response.trigger,
summary: response.summary,
intensity: response.intensity,
threshold: response.threshold,
})),
},
};
}

View file

@ -282,6 +282,7 @@ function pruneRetiredTypedState(state: VoidSelfStateTypedProjection): void {
state.personaAffect.socialBonds = state.personaAffect.socialBonds.filter(isNotRetiredEntry);
state.personaAffect.statusReads = state.personaAffect.statusReads.filter(isNotRetiredEntry);
state.personaAffect.doctrineStances = state.personaAffect.doctrineStances.filter(isNotRetiredEntry);
state.personaAffect.stressResponses = state.personaAffect.stressResponses.filter(isNotRetiredEntry);
}
function isNotRetiredEntry(entry: { status?: string; retiredAt?: string }): boolean {
@ -488,6 +489,15 @@ function applyTypedOperation(
.slice(0, 24);
state.personaAffect.updatedAt = operation.updatedAt;
return;
case "update_stress_responses":
for (const response of operation.responses) {
upsertBy(state.personaAffect.stressResponses, response, (entry) => entry.responseId);
}
state.personaAffect.stressResponses = state.personaAffect.stressResponses
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 24);
state.personaAffect.updatedAt = operation.updatedAt;
return;
case "upsert_doctrine_stance":
upsertBy(state.personaAffect.doctrineStances, operation.stance, (entry) => entry.stanceId);
state.personaAffect.doctrineStances = state.personaAffect.doctrineStances