use std::collections::BTreeMap;
use anyhow::Result;
use rusqlite::types::{ToSqlOutput, Value, ValueRef};
use rusqlite::{Connection, OptionalExtension, Row, ToSql, Transaction, params};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::payload_redaction::scan_payload;
use super::{
MEMORY_COLS, RewriteOutcome, SqliteStore, parse_relation_type, parse_uuid, row_to_entity,
row_to_memory,
};
use crate::maintenance::EmbedBatchFn;
use crate::redact::{Finding, redact};
use crate::schema::{Entity, Relation, SyncOp};
const STATE_KEY: &str = "stored_redaction_v1";
const SKIPPED_KEY: &str = "stored_redaction_v1_skipped";
const SKIPPED_CAP: usize = 1000;
const BATCH: i64 = 256;
const REWRITE_ATTEMPTS: usize = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(tag = "phase", content = "after", rename_all = "snake_case")]
pub enum RedactionPhase {
Memories(i64),
Chunks(i64),
Entities(i64),
Relations,
SyncLog(i64),
Done,
}
impl RedactionPhase {
fn encode(self) -> String {
match self {
Self::Memories(n) => format!("memories:{n}"),
Self::Chunks(n) => format!("chunks:{n}"),
Self::Entities(n) => format!("entities:{n}"),
Self::Relations => "relations".to_string(),
Self::SyncLog(n) => format!("sync_log:{n}"),
Self::Done => "done".to_string(),
}
}
fn decode(value: &str) -> Self {
let (phase, after) = value.split_once(':').unwrap_or((value, "0"));
let Ok(after) = after.parse() else {
return Self::Memories(0);
};
match phase {
"memories" => Self::Memories(after),
"chunks" => Self::Chunks(after),
"entities" => Self::Entities(after),
"relations" => Self::Relations,
"sync_log" => Self::SyncLog(after),
"done" => Self::Done,
_ => Self::Memories(0),
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct RedactionRun {
pub memories_rewritten: u64,
pub memories_conflicted: u64,
pub chunk_sets_dropped: u64,
pub entities_rewritten: u64,
pub relations_rewritten: u64,
pub sync_rows_scrubbed: u64,
pub rows_skipped: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SkippedRow {
pub table: String,
pub row: String,
pub reason: String,
}
impl SkippedRow {
fn new(table: &str, row: &str, reason: &str) -> Self {
Self {
table: table.to_string(),
row: redact(row).text,
reason: redact(reason).text,
}
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
struct SkippedRows {
rows: Vec<SkippedRow>,
omitted: u64,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct KindCounts {
pub rows: u64,
pub findings: BTreeMap<String, u64>,
}
impl KindCounts {
fn add(&mut self, findings: &[Finding]) {
if findings.is_empty() {
return;
}
self.rows += 1;
for finding in findings {
*self
.findings
.entry(finding.kind.as_str().to_string())
.or_default() += finding.count as u64;
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RedactionReport {
pub state: RedactionPhase,
pub memories: KindCounts,
pub chunks: KindCounts,
pub entities: KindCounts,
pub relations: KindCounts,
pub sync_log: KindCounts,
pub skipped: Vec<SkippedRow>,
pub skipped_omitted: u64,
}
impl SqliteStore {
pub fn redaction_phase(&self) -> Result<RedactionPhase> {
let value: Option<String> = self
.reader()
.query_row(
"SELECT value FROM index_meta WHERE key = ?1",
params![STATE_KEY],
|r| r.get(0),
)
.optional()?;
Ok(value
.as_deref()
.map(RedactionPhase::decode)
.unwrap_or(RedactionPhase::Memories(0)))
}
fn write_phase(tx: &Transaction<'_>, phase: RedactionPhase) -> Result<()> {
tx.execute(
"INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
params![STATE_KEY, phase.encode()],
)?;
Ok(())
}
fn settle_skips(tx: &Transaction<'_>, skipped: &[Skip]) -> Result<u64> {
if skipped.is_empty() {
return Ok(0);
}
for skip in skipped {
skip.raw.scrub(tx)?;
}
let mut stored = read_skipped(tx)?;
let mut recorded = 0;
for Skip { row, .. } in skipped {
let listed = stored
.rows
.iter()
.any(|r| r.table == row.table && r.row == row.row);
if listed {
continue;
}
recorded += 1;
if stored.rows.len() < SKIPPED_CAP {
stored.rows.push(row.clone());
} else {
stored.omitted += 1;
}
}
if recorded > 0 {
tx.execute(
"INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
params![SKIPPED_KEY, serde_json::to_string(&stored)?],
)?;
}
Ok(recorded)
}
pub fn run_stored_redaction(&self, embed: Option<EmbedBatchFn<'_>>) -> Result<RedactionRun> {
let mut run = RedactionRun::default();
loop {
let phase = self.redaction_phase()?;
let mut skipped = Vec::new();
let next = match phase {
RedactionPhase::Memories(after) => {
self.redact_memory_batch(after, embed, &mut run, &mut skipped)?
}
RedactionPhase::Chunks(after) => {
self.redact_chunk_batch(after, &mut run, &mut skipped)?
}
RedactionPhase::Entities(after) => {
self.redact_entity_batch(after, &mut run, &mut skipped)?
}
RedactionPhase::Relations => self.redact_relations(&mut run)?,
RedactionPhase::SyncLog(after) => self.scrub_sync_log_batch(after, &mut run)?,
RedactionPhase::Done => return Ok(run),
};
if !matches!(phase, RedactionPhase::SyncLog(_)) {
run.rows_skipped += self.with_txn(|tx| {
Self::write_phase(tx, next)?;
Self::settle_skips(tx, &skipped)
})?;
}
}
}
fn rewrite_with_retries(
&self,
id: Uuid,
embed: Option<EmbedBatchFn<'_>>,
) -> Result<RewriteOutcome> {
let rewrite = |text: &str| redact(text).text;
let mut outcome = RewriteOutcome::Conflict;
for _ in 0..REWRITE_ATTEMPTS {
outcome = self.rewrite_memory_content(id, &rewrite, embed)?;
if outcome != RewriteOutcome::Conflict {
break;
}
}
Ok(outcome)
}
fn redact_memory_batch(
&self,
after: i64,
embed: Option<EmbedBatchFn<'_>>,
run: &mut RedactionRun,
skipped: &mut Vec<Skip>,
) -> Result<RedactionPhase> {
let rows: Vec<(i64, rusqlite::Result<Uuid>)> = {
let conn = self.reader();
let mut stmt = conn.prepare(&format!(
"SELECT {MEMORY_COLS}, embedding, rowid AS row_key FROM memories WHERE rowid > ?1 \
ORDER BY rowid LIMIT ?2"
))?;
let rows = stmt.query_map(params![after, BATCH], |r| {
Ok((r.get("row_key")?, row_to_memory(r, true).map(|m| m.id)))
})?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
let Some(&(last, _)) = rows.last() else {
return Ok(RedactionPhase::Chunks(0));
};
let mut done = after;
for (rowid, id) in rows {
let Some(id) = parsed(id, || RawRow::Memory(rowid), skipped)? else {
done = rowid;
continue;
};
match self.rewrite_with_retries(id, embed)? {
RewriteOutcome::Rewritten => run.memories_rewritten += 1,
RewriteOutcome::Conflict => {
run.memories_conflicted += 1;
tracing::warn!(memory_id = %id, "redaction pass lost every rewrite race; retrying");
return Ok(RedactionPhase::Memories(done));
}
RewriteOutcome::Unchanged | RewriteOutcome::NotFound => {}
}
done = rowid;
}
Ok(RedactionPhase::Memories(last))
}
fn redact_chunk_batch(
&self,
after: i64,
run: &mut RedactionRun,
skipped: &mut Vec<Skip>,
) -> Result<RedactionPhase> {
let rows: Vec<(i64, rusqlite::Result<(Uuid, bool)>)> = {
let conn = self.reader();
let mut stmt = conn.prepare(
"SELECT rowid, memory_id, text FROM memory_chunks WHERE rowid > ?1 \
ORDER BY rowid LIMIT ?2",
)?;
let rows = stmt.query_map(params![after, BATCH], |r| {
let chunk = || -> rusqlite::Result<(Uuid, bool)> {
let text: String = r.get(2)?;
Ok((
parse_uuid(&r.get::<_, String>(1)?)?,
redact(&text).changed(),
))
};
Ok((r.get(0)?, chunk()))
})?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
let Some(&(last, _)) = rows.last() else {
return Ok(RedactionPhase::Entities(0));
};
let mut dirty = Vec::new();
for (rowid, chunk) in rows {
if let Some((id, true)) = parsed(chunk, || RawRow::Chunk(rowid), skipped)? {
dirty.push(id);
}
}
dirty.sort_unstable();
dirty.dedup();
self.with_txn(|tx| {
for id in &dirty {
tx.execute(
"DELETE FROM memory_chunks WHERE memory_id = ?1",
params![id.to_string()],
)?;
Self::append_outbox(tx, *id, "upsert")?;
}
Ok(())
})?;
run.chunk_sets_dropped += dirty.len() as u64;
Ok(RedactionPhase::Chunks(last))
}
fn redact_entity_batch(
&self,
after: i64,
run: &mut RedactionRun,
skipped: &mut Vec<Skip>,
) -> Result<RedactionPhase> {
let rows: Vec<(i64, rusqlite::Result<Entity>)> = {
let conn = self.reader();
let mut stmt = conn.prepare(
"SELECT id, name, entity_type, embedding, aliases, rowid FROM entities \
WHERE rowid > ?1 ORDER BY rowid LIMIT ?2",
)?;
let rows =
stmt.query_map(params![after, BATCH], |r| Ok((r.get(5)?, row_to_entity(r))))?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
let Some(&(last, _)) = rows.last() else {
return Ok(RedactionPhase::Relations);
};
for (rowid, before) in rows {
let Some(before) = parsed(before, || RawRow::Entity(rowid), skipped)? else {
continue;
};
let Some(after) = redacted_entity(&before) else {
continue;
};
let aliases = serde_json::to_string(&before.aliases)?;
let written = self.with_txn(|tx| {
let unchanged = tx
.query_row(
"SELECT 1 FROM entities WHERE id = ?1 AND name = ?2 AND aliases = ?3",
params![before.id.to_string(), before.name, aliases],
|_| Ok(()),
)
.optional()?
.is_some();
if unchanged {
self.store_entity_on_tx(tx, &after, SyncOp::Update)?;
}
Ok(unchanged)
})?;
if written {
run.entities_rewritten += 1;
}
}
Ok(RedactionPhase::Entities(last))
}
fn redact_relations(&self, run: &mut RedactionRun) -> Result<RedactionPhase> {
let mut cursor = [KeyPart::empty(), KeyPart::empty(), KeyPart::empty()];
loop {
let rows: Vec<([KeyPart; 3], rusqlite::Result<Relation>)> = {
let conn = self.reader();
let mut stmt = conn.prepare(
"SELECT from_id, to_id, relation_type, strength, context FROM relations \
WHERE (from_id, to_id, relation_type) > (?1, ?2, ?3) \
ORDER BY from_id, to_id, relation_type LIMIT ?4",
)?;
let rows =
stmt.query_map(params![cursor[0], cursor[1], cursor[2], BATCH], |r| {
let key = [
KeyPart::read(r, 0)?,
KeyPart::read(r, 1)?,
KeyPart::read(r, 2)?,
];
Ok((key, row_to_relation(r)))
})?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
let Some((last, _)) = rows.last() else {
return Ok(RedactionPhase::SyncLog(0));
};
cursor = last.clone();
let mut skipped = Vec::new();
let mut parsed_rows = Vec::new();
for (key, relation) in rows {
let raw = || RawRow::Relation(key.clone());
if let Some(relation) = parsed(relation, raw, &mut skipped)? {
parsed_rows.push((key, relation));
}
}
run.rows_skipped += self.with_txn(|tx| Self::settle_skips(tx, &skipped))?;
for ([from, to, kind], before) in parsed_rows {
let Some(context) = before.context.as_deref() else {
continue;
};
let redacted = redact(context);
if !redacted.changed() {
continue;
}
let written = self.with_txn(|tx| {
let strength: Option<Option<f64>> = tx
.query_row(
"SELECT strength FROM relations WHERE from_id = ?1 AND to_id = ?2 \
AND relation_type = ?3 AND context = ?4",
params![from, to, kind, context],
|r| r.get(0),
)
.optional()?;
let Some(strength) = strength else {
return Ok(false);
};
let after = Relation {
strength: strength.unwrap_or(1.0) as f32,
context: Some(redacted.text.clone()),
..before.clone()
};
self.store_relation_on_tx(tx, &after)?;
Ok(true)
})?;
if written {
run.relations_rewritten += 1;
}
}
}
}
fn scrub_sync_log_batch(&self, after: i64, run: &mut RedactionRun) -> Result<RedactionPhase> {
let rows: Vec<(i64, rusqlite::Result<(String, String)>)> = {
let conn = self.reader();
let mut stmt = conn.prepare(
"SELECT local_seq, node_type, data FROM sync_log WHERE local_seq > ?1 \
AND data IS NOT NULL ORDER BY local_seq LIMIT ?2",
)?;
let rows = stmt.query_map(params![after, BATCH], |r| {
Ok((r.get(0)?, r.get(1).and_then(|t| Ok((t, r.get(2)?)))))
})?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
let next = match rows.last() {
Some(&(last, _)) => RedactionPhase::SyncLog(last),
None => RedactionPhase::Done,
};
let mut skipped = Vec::new();
let mut scrubbed: Vec<(i64, String)> = Vec::new();
for (seq, payload) in rows {
let Some((node_type, data)) = parsed(payload, || RawRow::SyncLog(seq), &mut skipped)?
else {
continue;
};
match scan_payload(&node_type, &data) {
Some((Some(data), _)) => scrubbed.push((seq, data)),
Some((None, _)) => {}
None => {
let reason = match serde_json::from_str::<serde_json::Value>(&data) {
Err(err) => format!("payload is not JSON: {err}"),
Ok(_) => "payload is not JSON".to_string(),
};
skipped.push(Skip::new(RawRow::SyncLog(seq), &reason));
}
}
}
let recorded = self.with_txn(|tx| {
for (seq, data) in &scrubbed {
tx.execute(
"UPDATE sync_log SET data = ?2 WHERE local_seq = ?1",
params![seq, data],
)?;
}
Self::write_phase(tx, next)?;
Self::settle_skips(tx, &skipped)
})?;
run.sync_rows_scrubbed += scrubbed.len() as u64;
run.rows_skipped += recorded;
Ok(next)
}
pub fn stored_redaction_report(&self) -> Result<RedactionReport> {
let state = self.redaction_phase()?;
let conn = self.reader();
let skipped = read_skipped(&conn)?;
let mut report = RedactionReport {
state,
memories: KindCounts::default(),
chunks: KindCounts::default(),
entities: KindCounts::default(),
relations: KindCounts::default(),
sync_log: KindCounts::default(),
skipped: skipped.rows,
skipped_omitted: skipped.omitted,
};
let mut stmt = conn.prepare("SELECT content, context, retired_reason FROM memories")?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
let mut findings = Vec::new();
for col in 0..3 {
findings.extend(text_findings(row, col)?);
}
report.memories.add(&findings);
}
let mut stmt = conn.prepare("SELECT text FROM memory_chunks")?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
report.chunks.add(&text_findings(row, 0)?);
}
let mut stmt = conn.prepare("SELECT name, aliases FROM entities")?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
let mut findings = text_findings(row, 0)?;
if let Some(raw) = lossy_text(row, 1)? {
let aliases =
serde_json::from_str::<Vec<String>>(&raw).unwrap_or_else(|_| vec![raw]);
findings.extend(aliases.iter().flat_map(|alias| redact(alias).findings));
}
report.entities.add(&findings);
}
let mut stmt = conn.prepare("SELECT context FROM relations WHERE context IS NOT NULL")?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
report.relations.add(&text_findings(row, 0)?);
}
let mut stmt =
conn.prepare("SELECT node_type, data FROM sync_log WHERE data IS NOT NULL")?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
let (Some(node_type), Some(data)) = (lossy_text(row, 0)?, lossy_text(row, 1)?) else {
continue;
};
if let Some((_, findings)) = scan_payload(&node_type, &data) {
report.sync_log.add(&findings);
}
}
Ok(report)
}
}
fn unparseable(err: &rusqlite::Error) -> bool {
matches!(
err,
rusqlite::Error::FromSqlConversionFailure(..)
| rusqlite::Error::InvalidColumnType(..)
| rusqlite::Error::Utf8Error(..)
| rusqlite::Error::IntegralValueOutOfRange(..)
)
}
fn parsed<T>(
value: rusqlite::Result<T>,
raw: impl FnOnce() -> RawRow,
skipped: &mut Vec<Skip>,
) -> Result<Option<T>> {
match value {
Ok(value) => Ok(Some(value)),
Err(err) if unparseable(&err) => {
skipped.push(Skip::new(raw(), &err.to_string()));
Ok(None)
}
Err(err) => Err(err.into()),
}
}
struct Skip {
row: SkippedRow,
raw: RawRow,
}
impl Skip {
fn new(raw: RawRow, reason: &str) -> Self {
Self {
row: SkippedRow::new(raw.table(), &raw.label(), reason),
raw,
}
}
}
enum RawRow {
Memory(i64),
Chunk(i64),
Entity(i64),
Relation([KeyPart; 3]),
SyncLog(i64),
}
impl RawRow {
fn table(&self) -> &'static str {
match self {
Self::Memory(_) => "memories",
Self::Chunk(_) => "memory_chunks",
Self::Entity(_) => "entities",
Self::Relation(_) => "relations",
Self::SyncLog(_) => "sync_log",
}
}
fn label(&self) -> String {
match self {
Self::Memory(rowid) | Self::Chunk(rowid) | Self::Entity(rowid) => {
format!("rowid {rowid}")
}
Self::Relation([from, to, kind]) => format!("{from}/{to}/{kind}"),
Self::SyncLog(seq) => format!("local_seq {seq}"),
}
}
fn scrub(&self, tx: &Transaction<'_>) -> Result<()> {
match self {
Self::Memory(rowid) => scrub_text(
tx,
"memories",
&["content", "context", "retired_reason"],
"rowid = ?",
&[rowid],
),
Self::Chunk(rowid) => {
tx.execute("DELETE FROM memory_chunks WHERE rowid = ?1", [rowid])?;
Ok(())
}
Self::Entity(rowid) => {
scrub_text(tx, "entities", &["name", "aliases"], "rowid = ?", &[rowid])
}
Self::Relation([from, to, kind]) => scrub_text(
tx,
"relations",
&["from_id", "to_id", "relation_type", "context"],
"from_id IS ? AND to_id IS ? AND relation_type IS ?",
&[from, to, kind],
),
Self::SyncLog(seq) => scrub_text(tx, "sync_log", &["data"], "local_seq = ?", &[seq]),
}
}
}
fn scrub_text(
tx: &Transaction<'_>,
table: &str,
cols: &[&str],
key: &str,
key_params: &[&dyn ToSql],
) -> Result<()> {
let values: Option<Vec<Option<String>>> = tx
.query_row(
&format!("SELECT {} FROM {table} WHERE {key}", cols.join(", ")),
key_params,
|r| (0..cols.len()).map(|col| lossy_text(r, col)).collect(),
)
.optional()?;
let changed: Vec<(&str, String)> = cols
.iter()
.zip(values.into_iter().flatten())
.filter_map(|(col, value)| {
let redacted = redact(&value?);
redacted.changed().then_some((*col, redacted.text))
})
.collect();
if changed.is_empty() {
return Ok(());
}
let sets: Vec<String> = changed
.iter()
.map(|(col, _)| format!("{col} = ?"))
.collect();
let mut bound: Vec<&dyn ToSql> = changed.iter().map(|(_, text)| text as &dyn ToSql).collect();
bound.extend_from_slice(key_params);
tx.execute(
&format!(
"UPDATE OR REPLACE {table} SET {} WHERE {key}",
sets.join(", ")
),
bound.as_slice(),
)?;
Ok(())
}
fn read_skipped(conn: &Connection) -> Result<SkippedRows> {
let value: Option<String> = conn
.query_row(
"SELECT value FROM index_meta WHERE key = ?1",
params![SKIPPED_KEY],
|r| r.get(0),
)
.optional()?;
let Some(value) = value else {
return Ok(SkippedRows::default());
};
Ok(serde_json::from_str(&value).unwrap_or_else(|err| {
tracing::warn!("unreadable skipped-row list, starting a new one: {err}");
SkippedRows::default()
}))
}
fn lossy_text(row: &Row<'_>, col: usize) -> rusqlite::Result<Option<String>> {
Ok(match row.get_ref(col)? {
ValueRef::Text(bytes) | ValueRef::Blob(bytes) => {
Some(String::from_utf8_lossy(bytes).into_owned())
}
_ => None,
})
}
fn text_findings(row: &Row<'_>, col: usize) -> rusqlite::Result<Vec<Finding>> {
Ok(lossy_text(row, col)?
.map(|text| redact(&text).findings)
.unwrap_or_default())
}
fn row_to_relation(r: &Row<'_>) -> rusqlite::Result<Relation> {
Ok(Relation {
from_id: parse_uuid(&r.get::<_, String>(0)?)?,
to_id: parse_uuid(&r.get::<_, String>(1)?)?,
relation_type: parse_relation_type(&r.get::<_, String>(2)?)?,
strength: r.get::<_, Option<f64>>(3)?.unwrap_or(1.0) as f32,
context: r.get(4)?,
})
}
#[derive(Clone)]
enum KeyPart {
Text(Vec<u8>),
Other(Value),
}
impl KeyPart {
fn empty() -> Self {
Self::Text(Vec::new())
}
fn read(row: &Row<'_>, col: usize) -> rusqlite::Result<Self> {
Ok(match row.get_ref(col)? {
ValueRef::Text(bytes) => Self::Text(bytes.to_vec()),
ValueRef::Blob(bytes) => Self::Other(Value::Blob(bytes.to_vec())),
ValueRef::Integer(n) => Self::Other(Value::Integer(n)),
ValueRef::Real(n) => Self::Other(Value::Real(n)),
ValueRef::Null => Self::Other(Value::Null),
})
}
}
impl ToSql for KeyPart {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Self::Text(bytes) => Ok(ToSqlOutput::Borrowed(ValueRef::Text(bytes))),
Self::Other(value) => value.to_sql(),
}
}
}
impl std::fmt::Display for KeyPart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(bytes) => f.write_str(&String::from_utf8_lossy(bytes)),
Self::Other(Value::Blob(bytes)) => write!(f, "<{} byte blob>", bytes.len()),
Self::Other(value) => write!(f, "{value:?}"),
}
}
}
fn redacted_entity(entity: &Entity) -> Option<Entity> {
let name = redact(&entity.name);
let aliases: Vec<_> = entity.aliases.iter().map(|a| redact(a)).collect();
if !name.changed() && !aliases.iter().any(|a| a.changed()) {
return None;
}
Some(Entity {
id: entity.id,
embedding: if name.changed() {
Vec::new()
} else {
entity.embedding.clone()
},
name: name.text,
entity_type: entity.entity_type.clone(),
aliases: aliases.into_iter().map(|a| a.text).collect(),
})
}