Skip to content

Commit 57db320

Browse files
stonks-gitclaude
andcommitted
Fix source_tag not propagating through exit gate to permanent memories
Memories persisted via exit gate flush and FIFO prune were missing source_tag (user input vs agent response indistinguishable). Now extracts source_tag from scratch metadata and passes it through store_memory to the source_tag column. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 50383c2 commit 57db320

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/loop.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ async def _flush_scratch_through_exit_gate(
215215
if should_persist:
216216
source_info = entry.get("source", "conversation")
217217
tags = entry.get("tags", [])
218+
# Extract source_tag from scratch metadata
219+
scratch_meta = entry.get("metadata")
220+
if isinstance(scratch_meta, str):
221+
import json as _json
222+
try:
223+
scratch_meta = _json.loads(scratch_meta)
224+
except (ValueError, TypeError):
225+
scratch_meta = {}
226+
scratch_meta = scratch_meta or {}
227+
entry_source_tag = scratch_meta.get("source_tag")
218228
await memory.store_memory(
219229
content=content,
220230
memory_type="episodic",
@@ -229,6 +239,7 @@ async def _flush_scratch_through_exit_gate(
229239
for k, v in meta.items()
230240
},
231241
},
242+
source_tag=entry_source_tag,
232243
)
233244
persisted += 1
234245
else:
@@ -526,6 +537,7 @@ async def cognitive_loop(config, layers, memory, shutdown_event, input_queue: as
526537
confidence=score,
527538
importance=score,
528539
metadata={"gate_score": score, "pruned_from": "fifo"},
540+
source_tag=msg.get("source_tag"),
529541
)
530542
conversation = kept
531543

src/memory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ async def store_memory(
164164
importance: float = 0.5,
165165
evidence_count: int = 0,
166166
metadata: dict | None = None,
167+
source_tag: str | None = None,
167168
) -> str:
168169
"""Embed and store a memory chunk. Returns the memory ID."""
169170
memory_id = f"mem_{uuid.uuid4().hex[:12]}"
@@ -176,8 +177,9 @@ async def store_memory(
176177
await self.pool.execute(
177178
"""
178179
INSERT INTO memories (id, content, type, embedding, created_at, updated_at,
179-
source, tags, confidence, importance, evidence_count, metadata)
180-
VALUES ($1, $2, $3, $4::halfvec, $5, $6, $7, $8, $9, $10, $11, $12)
180+
source, tags, confidence, importance, evidence_count, metadata,
181+
source_tag)
182+
VALUES ($1, $2, $3, $4::halfvec, $5, $6, $7, $8, $9, $10, $11, $12, $13)
181183
""",
182184
memory_id,
183185
content,
@@ -191,6 +193,7 @@ async def store_memory(
191193
importance,
192194
evidence_count,
193195
json.dumps(metadata or {}),
196+
source_tag,
194197
)
195198

196199
logger.info(f"Stored memory {memory_id}: {content[:80]}...")

0 commit comments

Comments
 (0)