
















@@ -68,10 +68,12 @@ export type MemoryHostDreamCompletedEvent = {
6868/** Append-only memory host event schema stored as JSONL. */
6969export type MemoryHostEvent =
7070| MemoryHostRecallRecordedEvent
71-| MemoryHostRecallSkippedEvent
7271| MemoryHostPromotionAppliedEvent
7372| MemoryHostDreamCompletedEvent;
747374+/** Full event-log record schema, including opt-in diagnostic variants. */
75+export type MemoryHostEventRecord = MemoryHostEvent | MemoryHostRecallSkippedEvent;
76+7577/** Resolve the event log path inside a workspace without touching the filesystem. */
7678export function resolveMemoryHostEventLogPath(workspaceDir: string): string {
7779return path.join(workspaceDir, MEMORY_HOST_EVENT_LOG_RELATIVE_PATH);
@@ -80,7 +82,7 @@ export function resolveMemoryHostEventLogPath(workspaceDir: string): string {
8082/** Append one memory host event, creating the dreams directory with symlink-safe writes. */
8183export async function appendMemoryHostEvent(
8284workspaceDir: string,
83-event: MemoryHostEvent,
85+event: MemoryHostEventRecord,
8486): Promise<void> {
8587const eventLogPath = resolveMemoryHostEventLogPath(workspaceDir);
8688await fs.mkdir(path.dirname(eventLogPath), { recursive: true });
@@ -91,11 +93,28 @@ export async function appendMemoryHostEvent(
9193});
9294}
939594-/** Read recent memory host events, ignoring corrupt JSONL lines left by partial writes. */
95-export async function readMemoryHostEvents(params: {
96+function parseMemoryHostEventRecord(line: string): MemoryHostEventRecord | null {
97+try {
98+const record = JSON.parse(line) as MemoryHostEventRecord;
99+if (
100+record.type === "memory.recall.recorded" ||
101+record.type === "memory.recall.skipped" ||
102+record.type === "memory.promotion.applied" ||
103+record.type === "memory.dream.completed"
104+) {
105+return record;
106+}
107+} catch {
108+// The log is best-effort diagnostics; one malformed line must not hide
109+// later valid events or break memory status rendering.
110+}
111+return null;
112+}
113+114+async function readMemoryHostEventRecordsRaw(params: {
96115workspaceDir: string;
97116limit?: number;
98-}): Promise<MemoryHostEvent[]> {
117+}): Promise<MemoryHostEventRecord[]> {
99118const eventLogPath = resolveMemoryHostEventLogPath(params.workspaceDir);
100119const raw = await fs.readFile(eventLogPath, "utf8").catch((err: unknown) => {
101120if ((err as NodeJS.ErrnoException)?.code === "ENOENT") {
@@ -111,17 +130,40 @@ export async function readMemoryHostEvents(params: {
111130.map((line) => line.trim())
112131.filter(Boolean)
113132.flatMap((line) => {
114-try {
115-return [JSON.parse(line) as MemoryHostEvent];
116-} catch {
117-// The log is best-effort diagnostics; one malformed line must not hide
118-// later valid events or break memory status rendering.
119-return [];
120-}
133+const record = parseMemoryHostEventRecord(line);
134+return record ? [record] : [];
121135});
122136if (!Number.isFinite(params.limit)) {
123137return events;
124138}
125139const limit = Math.max(0, Math.floor(params.limit as number));
126140return limit === 0 ? [] : events.slice(-limit);
127141}
142+143+function applyMemoryHostEventLimit<T>(events: T[], limit: number | undefined): T[] {
144+if (!Number.isFinite(limit)) {
145+return events;
146+}
147+const normalizedLimit = Math.max(0, Math.floor(limit as number));
148+return normalizedLimit === 0 ? [] : events.slice(-normalizedLimit);
149+}
150+151+/** Read recent memory host events, ignoring corrupt JSONL lines left by partial writes. */
152+export async function readMemoryHostEvents(params: {
153+workspaceDir: string;
154+limit?: number;
155+}): Promise<MemoryHostEvent[]> {
156+const events = await readMemoryHostEventRecordsRaw({ workspaceDir: params.workspaceDir });
157+const legacyEvents = events.filter(
158+(event): event is MemoryHostEvent => event.type !== "memory.recall.skipped",
159+);
160+return applyMemoryHostEventLimit(legacyEvents, params.limit);
161+}
162+163+/** Read recent memory host event records, including opt-in diagnostic variants. */
164+export async function readMemoryHostEventRecords(params: {
165+workspaceDir: string;
166+limit?: number;
167+}): Promise<MemoryHostEventRecord[]> {
168+return await readMemoryHostEventRecordsRaw(params);
169+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。