-
Notifications
You must be signed in to change notification settings - Fork 1
feat: distill long-horizon agent transcripts into durable memory #2272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lsm
wants to merge
6
commits into
dev
Choose a base branch
from
space/distill-long-horizon-agent-session-transcripts-into-durable
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dbe0ee2
feat: distill long-horizon agent transcripts into durable memory
lsm 01934e2
fix: address review — distillation guard parity, failure backoff, own…
lsm 6601ddc
fix: address 2nd-round review — stall, message loss, keyspace, stale-…
lsm 2dac855
fix: address round-4 review — malformed JSON, provider resolution, co…
lsm dd9eaff
fix: distillation prompt/failed-delivery/archived-space (post-approva…
lsm e25c96b
fix: address round-7 review — coordinator dedup, ACP providers, reser…
lsm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/daemon/src/lib/job-handlers/memory-distillation.handler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import type { Job, JobQueueRepository } from '../../storage/repositories/job-queue-repository'; | ||
| import type { MemoryDistillationService } from '../space/memory-distillation-service'; | ||
| import { MEMORY_DISTILLATION } from '../job-queue-constants'; | ||
|
|
||
| /** | ||
| * Distillation cadence. Shorter than memory_consolidation's 24h because the | ||
| * per-agent cursor makes idle runs cheap (no LLM call when nothing is new), so | ||
| * active long-horizon agents get knowledge into memory within minutes of | ||
| * producing it instead of waiting up to a day. | ||
| */ | ||
| const NEXT_RUN_DELAY_MS = 30 * 60 * 1000; | ||
|
|
||
| export interface MemoryDistillationJobPayload { | ||
| /** Present (and non-empty) on per-agent jobs; absent on the coordinator tick. */ | ||
| agentId?: string; | ||
| } | ||
|
|
||
| export function createMemoryDistillationHandler( | ||
| service: MemoryDistillationService, | ||
| jobQueue?: JobQueueRepository | ||
| ) { | ||
| return async (job: Job): Promise<Record<string, unknown>> => { | ||
| const agentId = readOptionalString(job.payload.agentId); | ||
|
|
||
| // Per-agent job: distill a single agent. These are short (one bounded LLM | ||
| // call, or none when idle/in-backoff), so they finish well under the job | ||
| // queue's 5-min stale-reclaim threshold — and even if one were reclaimed | ||
| // and re-run, the per-agent cursor makes it idempotent. | ||
|
lsm marked this conversation as resolved.
|
||
| if (agentId) { | ||
| const result = await service.distillAgentById(agentId); | ||
| return { | ||
| agentId, | ||
| distilled: result?.distilled ?? false, | ||
| messagesRead: result?.messagesRead ?? 0, | ||
| memoriesWritten: result?.memoriesWritten ?? 0, | ||
| skipped: result?.skipped, | ||
| }; | ||
| } | ||
|
|
||
| // Coordinator tick: self-schedule the next tick, then fan out one short | ||
| // per-agent job per active LH agent. Splitting work this way (instead of | ||
| // one long distillAll) keeps every job short enough to never be reclaimed | ||
| // as stale and re-executed concurrently — which would duplicate paid LLM | ||
| // calls and race on cursor/memory writes. | ||
| const nextRunAt = Date.now() + NEXT_RUN_DELAY_MS; | ||
| if (jobQueue) enqueueMemoryDistillationIfMissing(jobQueue, nextRunAt); | ||
|
|
||
| const agentIds = service.listActiveAgentIds(); | ||
| let dispatched = 0; | ||
| if (jobQueue) { | ||
| for (const id of agentIds) { | ||
| jobQueue.enqueue({ | ||
| queue: MEMORY_DISTILLATION, | ||
| payload: { agentId: id }, | ||
| runAt: Date.now(), | ||
| }); | ||
| dispatched++; | ||
| } | ||
| } | ||
|
|
||
| return { coordinator: true, agentsDispatched: dispatched, nextRunAt }; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue the next coordinator tick (payload `{}`, i.e. no agentId) unless one | ||
| * is already pending. Per-agent jobs (payload `{ agentId }`) are NOT counted — | ||
| * they're transient work, not the cadence. | ||
| * | ||
| * Uses a targeted payload query rather than `listJobs`+filter: `listJobs` is | ||
| * newest-first and bounded, and the coordinator is enqueued before the per-agent | ||
| * fan-out, so with many pending per-agent jobs the coordinator is the oldest row | ||
| * and would fall outside a `LIMIT` window — producing duplicate coordinators. | ||
| */ | ||
| export function enqueueMemoryDistillationIfMissing( | ||
| jobQueue: JobQueueRepository, | ||
| runAt = Date.now() | ||
| ): void { | ||
| if (!jobQueue.hasPendingJobWithoutPayloadField(MEMORY_DISTILLATION, 'agentId')) { | ||
| jobQueue.enqueue({ queue: MEMORY_DISTILLATION, payload: {}, runAt }); | ||
| } | ||
| } | ||
|
|
||
| function readOptionalString(value: unknown): string | undefined { | ||
| return typeof value === 'string' && value.trim() ? value.trim() : undefined; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.