Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions App/backend/local-api-contracts/src/memory-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const JobTypeSchema = z.enum([
"skill_trial_resolve",
"decision_repair",
"work_memory_extract",
"work_memory_idle_flush",
"feedback_experience"
]);
export type JobType = z.infer<typeof JobTypeSchema>;
Expand Down
1 change: 1 addition & 0 deletions Memory/src/contracts/memory-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const JobTypeSchema = z.enum([
"skill_trial_resolve",
"decision_repair",
"work_memory_extract",
"work_memory_idle_flush",
"feedback_experience"
]);
export type JobType = z.infer<typeof JobTypeSchema>;
Expand Down
3 changes: 2 additions & 1 deletion Memory/src/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ function jobTypeTag(value: string): string {
episode_idle_close: "episode.close",
skill_trial_resolve: "skill.trial_resolve",
decision_repair: "decision.repair",
l2_association: "l2.association"
l2_association: "l2.association",
work_memory_idle_flush: "work.memory.idle_flush"
};
return tags[value] ?? value.replace(/_/g, ".");
}
Expand Down
21 changes: 18 additions & 3 deletions Memory/src/service/memory-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ export class MemoryService {
embedUserMemory: (job) => this.embeddingJobs.embedUserMemory(job)
},
workMemory: {
extract: (job) => this.workMemory.extract(job)
extract: (job) => this.workMemory.extract(job),
flushIdle: (job) => {
this.workMemory.flushIdle(job);
}
},
episodeTitle: {
generate: (job) => this.episodeTitle.generate(job)
Expand Down Expand Up @@ -661,6 +664,8 @@ export class MemoryService {
shouldDeferBudgetedEvolutionLlm: () => this.shouldDeferBudgetedEvolutionLlm(),
firstLine,
memoryLayersForIntent,
armWorkMemoryIdleFlush: this.armWorkMemoryIdleFlush.bind(this),
extractUnextractedWorkMemory: this.extractUnextractedWorkMemory.bind(this),
namespaceIdFromContext,
namespaceIdFromMemory,
namespaceIdFromSession,
Expand Down Expand Up @@ -1183,6 +1188,16 @@ export class MemoryService {
return this.sessionTurns.closeSession(sessionId, this.withTimeZone(request));
}

/** Arm the Work Memory idle flush for a Session inside the caller's transaction. */
private armWorkMemoryIdleFlush(sessionId: string, at: string): void {
this.workMemory.armIdleFlush(sessionId, at);
}

/** Extract unextracted Work Memory for a Session inside the caller's transaction. */
private extractUnextractedWorkMemory(sessionId: string, throughTraceSeq: number, at: string): void {
this.workMemory.extractUnextracted(sessionId, throughTraceSeq, at);
}

l3WorldModelTraceHead(
sessionId: string,
request: L3WorldModelRequestEnvelope
Expand All @@ -1208,8 +1223,8 @@ export class MemoryService {
trigger: request.trigger,
throughL1MemoryId: request.throughL1MemoryId
}, (frozen) => {
if (request.trigger === "token_compaction" && frozen.batchIds.length > 0) {
this.workMemory.scheduleBatchesInTransaction(frozen.batchIds, nowIso());
if (request.trigger === "token_compaction" && frozen.throughTraceSeq) {
this.workMemory.extractUnextracted(sessionId, frozen.throughTraceSeq, nowIso());
}
});
if (!result.throughTraceSeq) {
Expand Down
19 changes: 19 additions & 0 deletions Memory/src/service/session/session-turn-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,13 @@ export class SessionTurnService {
});
this.deps.finalizeClosedEpisode(episode, at, "session_closed");
}
if (session.meta.l3_world_model_protocol_version === 2) {
this.deps.extractUnextractedWorkMemory(
session.id,
this.deps.repos.l3WorldModels.maxInputTraceSeq(session.id),
at
);
}
this.deps.repos.runtime.appendChange({
memoryId: session.id,
namespaceId: this.deps.namespaceIdFromSession(closedWithMeta),
Expand Down Expand Up @@ -895,6 +902,11 @@ export class SessionTurnService {
trigger: "session_close",
at
});
this.deps.extractUnextractedWorkMemory(
sessionId,
this.deps.repos.l3WorldModels.maxInputTraceSeq(sessionId),
at
);
}
const changeSeq = this.deps.repos.runtime.appendChange({
memoryId: sessionId,
Expand Down Expand Up @@ -2255,6 +2267,13 @@ export class SessionTurnService {
}));
}
}
if (
rawTurnFirstCompleted &&
!completedEndTopicDecision &&
session.meta.l3_world_model_protocol_version === 2
) {
this.deps.armWorkMemoryIdleFlush(session.id, at);
}
const uniqueClosedEpisodeIds = uniq(closedEpisodeIds);
const responseChangeSeq = this.deps.repos.runtime.latestChangeSeq(session.userId, this.deps.namespaceIdFromSession(session));
const body: CompleteTurnResponse = {
Expand Down
Loading
Loading