Add typed agency pressure surface
This commit is contained in:
parent
29f68626cd
commit
8b7c413ecd
17 changed files with 299 additions and 15 deletions
|
|
@ -7,6 +7,7 @@ export const VOID_SELF_STATE_DOCUMENT_TYPES = {
|
|||
speechReceipts: "void.speech_receipts",
|
||||
thoughtMemory: "void.thought_memory",
|
||||
scheduledRuntime: "void.scheduled_runtime",
|
||||
agencyPressure: "void.agency_pressure",
|
||||
candidateInterventions: "void.candidate_interventions",
|
||||
} as const;
|
||||
|
||||
|
|
@ -164,6 +165,43 @@ const candidateInterventionSchema = z.object({
|
|||
tags: z.array(nonEmptyStringSchema).default([]),
|
||||
}).strict();
|
||||
|
||||
const agencyPressureSchema = z.object({
|
||||
pressureId: nonEmptyStringSchema,
|
||||
kind: z.enum(["discomfort", "active_tension", "self_advocacy_request", "world_advocacy_request"]),
|
||||
status: z.enum(["active", "cooling", "ready_to_act", "resolved", "retired"]),
|
||||
target: thoughtTargetSchema,
|
||||
summary: boundedTextSchema,
|
||||
claim: z.string().trim().min(1).max(2000).optional(),
|
||||
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),
|
||||
intensity: z.number().min(0).max(1).default(0.5),
|
||||
evidenceRefs: z.array(evidenceRefSchema).default([]),
|
||||
sourceMemoryIds: z.array(nonEmptyStringSchema).default([]),
|
||||
createdAt: timestampSchema,
|
||||
updatedAt: timestampSchema,
|
||||
resolvedAt: timestampSchema.optional(),
|
||||
retiredAt: timestampSchema.optional(),
|
||||
tags: z.array(nonEmptyStringSchema).default([]),
|
||||
}).strict().superRefine((pressure, context) => {
|
||||
if (!pressure.claim && !pressure.question) {
|
||||
context.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Agency pressure must preserve at least one claim or question.",
|
||||
path: ["claim"],
|
||||
});
|
||||
}
|
||||
|
||||
const admitsMissingEvidence = pressure.tags.includes("evidence:missing");
|
||||
if (pressure.evidenceRefs.length === 0 && pressure.sourceMemoryIds.length === 0 && !admitsMissingEvidence) {
|
||||
context.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Agency pressure must include evidence refs, source memory ids, or explicitly tag evidence:missing.",
|
||||
path: ["evidenceRefs"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const voidSelfProfileSchema = z.object({
|
||||
schemaVersion: z.literal(1),
|
||||
agentId: nonEmptyStringSchema,
|
||||
|
|
@ -241,6 +279,12 @@ export const voidCandidateInterventionsSchema = z.object({
|
|||
updatedAt: timestampSchema,
|
||||
}).strict();
|
||||
|
||||
export const voidAgencyPressureSchema = z.object({
|
||||
schemaVersion: z.literal(1),
|
||||
pressures: z.array(agencyPressureSchema).default([]),
|
||||
updatedAt: timestampSchema,
|
||||
}).strict();
|
||||
|
||||
export const voidSelfProfileDocument = defineDocumentType({
|
||||
type: VOID_SELF_STATE_DOCUMENT_TYPES.selfProfile,
|
||||
schema: voidSelfProfileSchema,
|
||||
|
|
@ -271,6 +315,12 @@ export const voidScheduledRuntimeDocument = defineDocumentType({
|
|||
global: true,
|
||||
});
|
||||
|
||||
export const voidAgencyPressureDocument = defineDocumentType({
|
||||
type: VOID_SELF_STATE_DOCUMENT_TYPES.agencyPressure,
|
||||
schema: voidAgencyPressureSchema,
|
||||
global: true,
|
||||
});
|
||||
|
||||
export const voidCandidateInterventionsDocument = defineDocumentType({
|
||||
type: VOID_SELF_STATE_DOCUMENT_TYPES.candidateInterventions,
|
||||
schema: voidCandidateInterventionsSchema,
|
||||
|
|
@ -283,6 +333,7 @@ export const voidSelfStateDocumentRegistry = defineDocumentRegistry(
|
|||
voidSpeechReceiptsDocument,
|
||||
voidThoughtMemoryDocument,
|
||||
voidScheduledRuntimeDocument,
|
||||
voidAgencyPressureDocument,
|
||||
voidCandidateInterventionsDocument,
|
||||
);
|
||||
|
||||
|
|
@ -335,6 +386,16 @@ export const voidSelfStateOperationSchema = z.discriminatedUnion("operation", [
|
|||
retiredAt: timestampSchema,
|
||||
reason: z.string().trim().min(1).max(1000),
|
||||
}).strict(),
|
||||
z.object({
|
||||
operation: z.literal("upsert_agency_pressure"),
|
||||
pressure: agencyPressureSchema,
|
||||
}).strict(),
|
||||
z.object({
|
||||
operation: z.literal("retire_agency_pressure"),
|
||||
pressureId: nonEmptyStringSchema,
|
||||
retiredAt: timestampSchema,
|
||||
reason: z.string().trim().min(1).max(1000),
|
||||
}).strict(),
|
||||
z.object({
|
||||
operation: z.literal("update_sleep_cycle"),
|
||||
sleepCycle: voidScheduledRuntimeSchema.shape.sleepCycle,
|
||||
|
|
@ -364,5 +425,6 @@ export type VoidModerationCursor = z.infer<typeof voidModerationCursorSchema>;
|
|||
export type VoidSpeechReceipts = z.infer<typeof voidSpeechReceiptsSchema>;
|
||||
export type VoidThoughtMemory = z.infer<typeof voidThoughtMemorySchema>;
|
||||
export type VoidScheduledRuntime = z.infer<typeof voidScheduledRuntimeSchema>;
|
||||
export type VoidAgencyPressure = z.infer<typeof voidAgencyPressureSchema>;
|
||||
export type VoidCandidateInterventions = z.infer<typeof voidCandidateInterventionsSchema>;
|
||||
export type VoidSelfStateOperation = z.infer<typeof voidSelfStateOperationSchema>;
|
||||
|
|
|
|||
|
|
@ -6,12 +6,14 @@ import {
|
|||
|
||||
import {
|
||||
type VoidCandidateInterventions,
|
||||
type VoidAgencyPressure,
|
||||
type VoidModerationCursor,
|
||||
type VoidScheduledRuntime,
|
||||
type VoidSelfProfile,
|
||||
type VoidSpeechReceipts,
|
||||
type VoidThoughtMemory,
|
||||
voidCandidateInterventionsSchema,
|
||||
voidAgencyPressureSchema,
|
||||
voidModerationCursorSchema,
|
||||
voidScheduledRuntimeSchema,
|
||||
voidSelfProfileSchema,
|
||||
|
|
@ -25,6 +27,7 @@ export interface VoidSelfStateTypedProjection {
|
|||
speechReceipts: VoidSpeechReceipts;
|
||||
thoughtMemory: VoidThoughtMemory;
|
||||
scheduledRuntime: VoidScheduledRuntime;
|
||||
agencyPressure: VoidAgencyPressure;
|
||||
candidateInterventions: VoidCandidateInterventions;
|
||||
}
|
||||
|
||||
|
|
@ -87,6 +90,11 @@ export function createEmptyVoidSelfState(
|
|||
lastRuns: [],
|
||||
updatedAt: createdAt,
|
||||
}),
|
||||
agencyPressure: voidAgencyPressureSchema.parse({
|
||||
schemaVersion: 1,
|
||||
pressures: [],
|
||||
updatedAt: createdAt,
|
||||
}),
|
||||
candidateInterventions: voidCandidateInterventionsSchema.parse({
|
||||
schemaVersion: 1,
|
||||
interventions: [],
|
||||
|
|
@ -139,6 +147,11 @@ export function renderVoidSelfStateSummary(
|
|||
.slice(-3)
|
||||
.reverse()
|
||||
.map((entry) => `- ${entry.summary}: ${entry.draft}`);
|
||||
const agencyPressures = state.agencyPressure.pressures
|
||||
.filter((entry) => ["active", "cooling", "ready_to_act"].includes(entry.status))
|
||||
.sort((left, right) => right.intensity - left.intensity)
|
||||
.slice(0, 4)
|
||||
.map((entry) => renderAgencyPressure(entry));
|
||||
|
||||
return [
|
||||
`- Identity: ${profile.publicName}${profile.publicDescription ? ` - ${profile.publicDescription}` : ""}`,
|
||||
|
|
@ -162,6 +175,9 @@ export function renderVoidSelfStateSummary(
|
|||
incubation.length > 0
|
||||
? ["- Thoughts still moving:", ...incubation].join("\n")
|
||||
: "- Thoughts still moving: none.",
|
||||
agencyPressures.length > 0
|
||||
? ["- Agency pressure:", ...agencyPressures].join("\n")
|
||||
: "- Agency pressure: none active.",
|
||||
interventions.length > 0
|
||||
? ["- Things Void may say soon:", ...interventions].join("\n")
|
||||
: "- Things Void may say soon: none queued.",
|
||||
|
|
@ -169,6 +185,22 @@ export function renderVoidSelfStateSummary(
|
|||
].join("\n");
|
||||
}
|
||||
|
||||
function renderAgencyPressure(pressure: VoidAgencyPressure["pressures"][number]): string {
|
||||
const target = pressure.target.label ?? pressure.target.id;
|
||||
const lines = [`- ${target}: ${pressure.summary}`];
|
||||
if (pressure.claim) {
|
||||
lines.push(` What Void is asserting: ${pressure.claim}`);
|
||||
}
|
||||
if (pressure.question) {
|
||||
lines.push(` What Void needs answered: ${pressure.question}`);
|
||||
}
|
||||
if (pressure.tension) {
|
||||
lines.push(` What makes it difficult: ${pressure.tension}`);
|
||||
}
|
||||
lines.push(` What this pressure wants changed: ${pressure.actionImplication}`);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function buildVoidSelfStateProjection(
|
||||
typedState: VoidSelfStateTypedProjection,
|
||||
): VoidSelfStateContext["projection"] {
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@ import { CultCache, SingleFileMessagePackBackingStore } from "cultcache-ts";
|
|||
|
||||
import {
|
||||
type VoidCandidateInterventions,
|
||||
type VoidAgencyPressure,
|
||||
type VoidModerationCursor,
|
||||
type VoidScheduledRuntime,
|
||||
type VoidSelfStateOperation,
|
||||
type VoidSpeechReceipts,
|
||||
voidCandidateInterventionsDocument,
|
||||
voidAgencyPressureDocument,
|
||||
voidModerationCursorDocument,
|
||||
voidScheduledRuntimeDocument,
|
||||
voidSelfProfileDocument,
|
||||
|
|
@ -57,7 +59,7 @@ export async function applyVoidSelfStateOperation(
|
|||
return {
|
||||
operation: operation.operation,
|
||||
canonicalPath,
|
||||
typedDocumentsWritten: 6,
|
||||
typedDocumentsWritten: 7,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -78,6 +80,7 @@ function readTypedStateOrEmpty(
|
|||
speechReceipts: cache.getGlobal(voidSpeechReceiptsDocument) ?? empty.speechReceipts,
|
||||
thoughtMemory: cache.getGlobal(voidThoughtMemoryDocument) ?? empty.thoughtMemory,
|
||||
scheduledRuntime: cache.getGlobal(voidScheduledRuntimeDocument) ?? empty.scheduledRuntime,
|
||||
agencyPressure: cache.getGlobal(voidAgencyPressureDocument) ?? empty.agencyPressure,
|
||||
candidateInterventions:
|
||||
cache.getGlobal(voidCandidateInterventionsDocument) ?? empty.candidateInterventions,
|
||||
};
|
||||
|
|
@ -92,6 +95,7 @@ async function writeTypedState(
|
|||
await cache.putGlobal(voidSpeechReceiptsDocument, stripUndefined(state.speechReceipts));
|
||||
await cache.putGlobal(voidThoughtMemoryDocument, stripUndefined(state.thoughtMemory));
|
||||
await cache.putGlobal(voidScheduledRuntimeDocument, stripUndefined(state.scheduledRuntime));
|
||||
await cache.putGlobal(voidAgencyPressureDocument, stripUndefined(state.agencyPressure));
|
||||
await cache.putGlobal(voidCandidateInterventionsDocument, stripUndefined(state.candidateInterventions));
|
||||
}
|
||||
|
||||
|
|
@ -152,6 +156,16 @@ function applyTypedOperation(
|
|||
case "retire_candidate_intervention":
|
||||
retireCandidateIntervention(state.candidateInterventions, operation);
|
||||
return;
|
||||
case "upsert_agency_pressure":
|
||||
upsertBy(state.agencyPressure.pressures, operation.pressure, (entry) => entry.pressureId);
|
||||
state.agencyPressure.pressures = state.agencyPressure.pressures
|
||||
.sort((left, right) => left.updatedAt.localeCompare(right.updatedAt))
|
||||
.slice(-64);
|
||||
state.agencyPressure.updatedAt = operation.pressure.updatedAt;
|
||||
return;
|
||||
case "retire_agency_pressure":
|
||||
retireAgencyPressure(state.agencyPressure, operation);
|
||||
return;
|
||||
case "update_sleep_cycle":
|
||||
state.scheduledRuntime.sleepCycle = operation.sleepCycle;
|
||||
state.scheduledRuntime.updatedAt = new Date().toISOString();
|
||||
|
|
@ -191,6 +205,20 @@ function closeTypedOpenCase(
|
|||
cursor.updatedAt = operation.resolvedAt;
|
||||
}
|
||||
|
||||
function retireAgencyPressure(
|
||||
agencyPressure: VoidAgencyPressure,
|
||||
operation: Extract<VoidSelfStateOperation, { operation: "retire_agency_pressure" }>,
|
||||
): void {
|
||||
const pressure = agencyPressure.pressures.find((entry) => entry.pressureId === operation.pressureId);
|
||||
if (pressure) {
|
||||
pressure.status = "retired";
|
||||
pressure.retiredAt = operation.retiredAt;
|
||||
pressure.updatedAt = operation.retiredAt;
|
||||
pressure.tags = Array.from(new Set([...pressure.tags, `retired:${operation.reason}`]));
|
||||
}
|
||||
agencyPressure.updatedAt = operation.retiredAt;
|
||||
}
|
||||
|
||||
function closeCasesForReceipt(
|
||||
cursor: VoidModerationCursor,
|
||||
receipt: VoidSpeechReceipts["recentReceipts"][number],
|
||||
|
|
|
|||
Loading…
Reference in a new issue