Skip to content

Commit b8ed4ac

Browse files
committed
fixed bug in memory block stuff
1 parent 624e78c commit b8ed4ac

14 files changed

+70
-326
lines changed

crates/pattern_cli/src/commands/builder/agent.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ impl AgentBuilder {
10341034
if !content.is_empty() {
10351035
doc.set_text(&content, true)
10361036
.map_err(|e| miette::miette!("Failed to set content: {}", e))?;
1037+
memory.mark_dirty(&id, label);
10371038
memory
10381039
.persist_block(&id, label)
10391040
.await
@@ -1067,6 +1068,7 @@ impl AgentBuilder {
10671068
doc.set_text(&content, true).map_err(|e| {
10681069
miette::miette!("Failed to set content: {:?}", e)
10691070
})?;
1071+
memory.mark_dirty(&id, label);
10701072
memory.persist_block(&id, label).await.map_err(|e| {
10711073
miette::miette!("Failed to persist block: {:?}", e)
10721074
})?;

crates/pattern_core/src/context/builder.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ impl<'a> ContextBuilder<'a> {
204204

205205
// Get tools in genai format if tool registry is provided
206206
let tools = self.tools.map(|registry| registry.to_genai_tools());
207+
for s in system.iter() {
208+
tracing::debug!(
209+
"{}\n{}",
210+
s.chars().take(300).collect::<String>(),
211+
s.chars().rev().take(300).collect::<String>()
212+
);
213+
}
207214

208215
Ok(Request {
209216
system: if system.is_empty() {
@@ -366,8 +373,6 @@ impl<'a> ContextBuilder<'a> {
366373
// Incomplete non-active: dropped
367374
}
368375

369-
// INCORRECT SORT REMOVED
370-
371376
Ok(messages)
372377
}
373378

crates/pattern_core/src/memory/cache.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ impl MemoryCache {
181181
// Override with effective permission (may differ for shared blocks)
182182
metadata.permission = effective_permission;
183183

184+
// Get and apply any updates since the snapshot
185+
// TODO: use the checkpoint here as the starting snapshot
186+
let (_checkpoint, updates) = pattern_db::queries::get_checkpoint_and_updates(
187+
self.dbs.constellation.pool(),
188+
&block.id,
189+
)
190+
.await?;
191+
184192
// Create StructuredDocument from snapshot with metadata
185193
let doc = if block.loro_snapshot.is_empty() {
186194
StructuredDocument::new_with_metadata(metadata.clone(), Some(agent_id.to_string()))
@@ -192,13 +200,6 @@ impl MemoryCache {
192200
)?
193201
};
194202

195-
// Get and apply any updates since the snapshot
196-
let (_, updates) = pattern_db::queries::get_checkpoint_and_updates(
197-
self.dbs.constellation.pool(),
198-
&block.id,
199-
)
200-
.await?;
201-
202203
for update in &updates {
203204
doc.apply_updates(&update.update_blob)?;
204205
}
@@ -285,10 +286,12 @@ impl MemoryCache {
285286
Some(preview.as_str())
286287
};
287288

288-
pattern_db::queries::update_block_content(
289+
// Only update the preview, don't touch loro_snapshot.
290+
// The snapshot may contain imported data (e.g., from CAR files) that
291+
// we must not overwrite. Incremental updates go to memory_block_updates.
292+
pattern_db::queries::update_block_preview(
289293
self.dbs.constellation.pool(),
290294
&block_id,
291-
&[], // Don't update snapshot on every write
292295
preview_str,
293296
)
294297
.await?;
@@ -442,6 +445,8 @@ impl MemoryStore for MemoryCache {
442445
serde_json::to_value(&schema).map_err(|e| MemoryError::Other(e.to_string()))?,
443446
);
444447
let metadata_json = JsonValue::Object(db_metadata);
448+
let loro_snapshot = doc.export_snapshot()?;
449+
let frontier = doc.current_version().get_frontiers();
445450

446451
// Create MemoryBlock for DB
447452
let db_block = pattern_db::models::MemoryBlock {
@@ -453,12 +458,12 @@ impl MemoryStore for MemoryCache {
453458
char_limit: effective_char_limit as i64,
454459
permission: pattern_db::models::MemoryPermission::ReadWrite,
455460
pinned: false,
456-
loro_snapshot: vec![],
461+
loro_snapshot: loro_snapshot,
457462
content_preview: None,
458463
metadata: Some(SqlxJson(metadata_json)),
459464
embedding_model: None,
460465
is_active: true,
461-
frontier: None,
466+
frontier: Some(frontier.encode()),
462467
last_seq: 0,
463468
created_at: now,
464469
updated_at: now,
@@ -471,7 +476,7 @@ impl MemoryStore for MemoryCache {
471476
let cached_block = CachedBlock {
472477
doc: doc.clone(),
473478
last_seq: 0,
474-
last_persisted_frontier: None,
479+
last_persisted_frontier: Some(doc.current_version()),
475480
dirty: false,
476481
last_accessed: now,
477482
};

crates/pattern_core/src/runtime/router.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,15 @@ impl AgentMessageRouter {
306306

307307
let target_agent_id = target_agent.id;
308308

309-
// Check recent message cache to prevent rapid loops
310-
{
309+
// Check recent message cache to prevent rapid loops.
310+
// Skip rate limiting for data sources - they legitimately send multiple
311+
// messages quickly (e.g., Bluesky firehose batches).
312+
let is_data_source = matches!(
313+
&origin,
314+
Some(MessageOrigin::DataSource { .. }) | Some(MessageOrigin::Bluesky { .. })
315+
);
316+
317+
if !is_data_source {
311318
let mut recent = self.recent_messages.write().await;
312319
let mut agents = vec![self.agent_id.clone(), target_agent_id.clone()];
313320
agents.sort();

crates/pattern_db/.sqlx/query-1ad0bf4651203855d83959e4c36eda04e46b30ab306967a67ef91cff0d66e0df.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/pattern_db/.sqlx/query-362988f0299c8ae16f922d169f9e22ea43c87acc6b21506a7d7fcb5344b2ca50.json

Lines changed: 0 additions & 56 deletions
This file was deleted.

crates/pattern_db/.sqlx/query-3a995b0d5e259a9ae9e2f92a942124d918a4af6f2d60d888d18468227a897bd9.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pattern_db/.sqlx/query-3ef498e24961c51eee524dd5304eedabef2acaeb51c4d352247774ac0ed93dec.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/pattern_db/.sqlx/query-419b8badc2948b8c257e3ab5fd61a2ffdba886862f45c1e2e4ef1f35bec9cdae.json

Lines changed: 0 additions & 68 deletions
This file was deleted.

crates/pattern_db/.sqlx/query-570bbec1b3269186324aae852045bb3ed9becdbf6b661a80e4e8124a5c5faf4a.json

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)