gent-daemon 0.1.4

Local daemon, storage and synchronization for Gent chat clusters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Keep immutable retry evidence and status fields, without another policy copy.
//! Call mutations inside the proposal/result or maintenance transaction.
use super::*;
use serde::{Deserialize, Serialize};
pub(in crate::store) mod handoff;

const CLEAN_BATCH: usize = 8;
const MAX_RECORDS: u64 = 65536;
const MAX_BYTES: u64 = 64 * 1024 * 1024;
pub(super) const MAX_REASON_BYTES: usize = 1024;
// Includes JSON escaping of a rejection (up to six bytes per input byte).
const RESULT_RESERVE: usize = 8192;
const MAX_AGENT_RECORDS: u64 = 8192;
const MAX_PER_AGENT: u64 = 1024;
const MAX_AGENT_PENDING: u64 = 64;
const MAX_AGENT_BYTES: u64 = 16 * 1024 * 1024;

fn agent_usage(db: &Connection, administrator: &str) -> Result<(u64, u64, u64, u64)> {
    Ok(db.query_row(
        "SELECT COUNT(*),COALESCE(SUM(json_extract(edit,'$.administrator')=?1),0),
        COALESCE(SUM(json_extract(edit,'$.administrator')=?1 AND result IS NULL),0),
        COALESCE(SUM(length(CAST(origin AS BLOB))+length(CAST(id AS BLOB))+length(CAST(writer AS BLOB))+
        length(CAST(fingerprint AS BLOB))+length(edit)+COALESCE(length(result),?2)),0)
        FROM configuration_edits WHERE CASE WHEN json_valid(edit) THEN json_extract(edit,'$.op') END IN ('agent_permissions','agent_permission_rules','agent_groups')",
        params![administrator, RESULT_RESERVE], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?)))?)
}

#[derive(Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
enum Summary {
    EnrollNode,
    RemoveNode {
        node_id: String,
    },
    DelegateNode {
        node_id: String,
        allow: bool,
    },
    HostNode {
        node_id: String,
        parent: String,
    },
    CreateGroup {
        name: String,
    },
    DeleteGroup {
        name: String,
    },
    GroupMembers {
        group: String,
        members: Vec<String>,
        action: gent_protocol::administration::GroupAction,
    },
    GrantGroupAdmin {
        grant_id: String,
    },
    RevokeGroupAdmin {
        grant_id: String,
    },
    AgentGroups {
        administrator: String,
        request_id: String,
        changes: Vec<gent_protocol::administration::GroupChange>,
    },
    SetPermissionRule,
    Policy,
    Replace,
    RenameNode {
        name: String,
    },
    RenameMember {
        node_id: String,
        name: String,
    },
    SetReleaseChannel {
        channel: gent_protocol::ReleaseChannel,
    },
    Renew,
    GrantRelay {
        grant_id: String,
    },
    RevokeRelay {
        grant_id: String,
    },
    GrantPermissionAdmin {
        grant_id: String,
    },
    RevokePermissionAdmin {
        grant_id: String,
    },
    AgentPermissions {
        administrator: String,
        request_id: String,
        changes: Vec<gent_protocol::administration::PermissionChange>,
    },
    AgentPermissionRules {
        administrator: String,
        request_id: String,
        changes: Vec<gent_protocol::administration::PermissionRuleChange>,
    },
}

impl From<&ConfigurationEdit> for Summary {
    fn from(edit: &ConfigurationEdit) -> Self {
        match edit {
            ConfigurationEdit::EnrollNode { .. } => Self::EnrollNode,
            ConfigurationEdit::RemoveNode { node_id } => Self::RemoveNode {
                node_id: node_id.clone(),
            },
            ConfigurationEdit::DelegateNode { node_id, allow } => Self::DelegateNode {
                node_id: node_id.clone(),
                allow: *allow,
            },
            ConfigurationEdit::HostNode { node_id, parent } => Self::HostNode {
                node_id: node_id.clone(),
                parent: parent.clone(),
            },
            ConfigurationEdit::CreateGroup { name } => Self::CreateGroup { name: name.clone() },
            ConfigurationEdit::DeleteGroup { name } => Self::DeleteGroup { name: name.clone() },
            ConfigurationEdit::GroupMembers {
                group,
                members,
                action,
            } => Self::GroupMembers {
                group: group.clone(),
                members: members.clone(),
                action: *action,
            },
            ConfigurationEdit::GrantGroupAdmin { grant_id, .. } => Self::GrantGroupAdmin {
                grant_id: grant_id.clone(),
            },
            ConfigurationEdit::RevokeGroupAdmin { grant_id } => Self::RevokeGroupAdmin {
                grant_id: grant_id.clone(),
            },
            ConfigurationEdit::AgentGroups {
                administrator,
                request_id,
                changes,
            } => Self::AgentGroups {
                administrator: administrator.clone(),
                request_id: request_id.clone(),
                changes: changes.clone(),
            },
            ConfigurationEdit::SetPermissionRule { .. } => Self::SetPermissionRule,
            ConfigurationEdit::Policy { .. } => Self::Policy,
            ConfigurationEdit::Replace { .. } => Self::Replace,
            ConfigurationEdit::RenameNode { name } => Self::RenameNode { name: name.clone() },
            ConfigurationEdit::RenameMember { node_id, name } => Self::RenameMember {
                node_id: node_id.clone(),
                name: name.clone(),
            },
            ConfigurationEdit::SetReleaseChannel { channel } => {
                Self::SetReleaseChannel { channel: *channel }
            }
            ConfigurationEdit::Renew => Self::Renew,
            ConfigurationEdit::GrantRelay { grant_id, .. } => Self::GrantRelay {
                grant_id: grant_id.clone(),
            },
            ConfigurationEdit::RevokeRelay { grant_id } => Self::RevokeRelay {
                grant_id: grant_id.clone(),
            },
            ConfigurationEdit::GrantPermissionAdmin { grant_id, .. } => {
                Self::GrantPermissionAdmin {
                    grant_id: grant_id.clone(),
                }
            }
            ConfigurationEdit::RevokePermissionAdmin { grant_id } => Self::RevokePermissionAdmin {
                grant_id: grant_id.clone(),
            },
            ConfigurationEdit::AgentPermissions {
                administrator,
                request_id,
                changes,
            } => Self::AgentPermissions {
                administrator: administrator.clone(),
                request_id: request_id.clone(),
                changes: changes.clone(),
            },
            ConfigurationEdit::AgentPermissionRules {
                administrator,
                request_id,
                changes,
            } => Self::AgentPermissionRules {
                administrator: administrator.clone(),
                request_id: request_id.clone(),
                changes: changes.clone(),
            },
        }
    }
}

pub(super) fn schema(db: &Connection) -> Result<()> {
    let tx = db.unchecked_transaction()?;
    tx.execute_batch("CREATE TABLE IF NOT EXISTS configuration_edits(origin TEXT NOT NULL,id TEXT NOT NULL,writer TEXT NOT NULL,fingerprint TEXT NOT NULL,edit BLOB NOT NULL,result BLOB,PRIMARY KEY(origin,id));")?;
    let columns = tx
        .prepare("PRAGMA table_info(configuration_edits)")?
        .query_map([], |r| r.get::<_, String>(1))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    if !columns.iter().any(|c| c == "compact") {
        tx.execute_batch("ALTER TABLE configuration_edits ADD COLUMN compact INTEGER NOT NULL DEFAULT 0 CHECK(compact IN (0,1))")?;
    }
    tx.execute_batch(
        "CREATE INDEX IF NOT EXISTS configuration_edits_format ON configuration_edits(compact)",
    )?;
    pending::schema(&tx, 0)?;
    super::routing::schema(&tx, 0)?;
    tx.commit()?;
    Ok(())
}

struct Usage {
    records: u64,
    pending: u64,
    legacy: u64,
    bytes: u64,
    reserved: u64,
}

fn usage(db: &Connection) -> Result<Usage> {
    Ok(db.query_row("SELECT COUNT(*),COALESCE(SUM(result IS NULL),0),COALESCE(SUM(compact=0),0),
        COALESCE(SUM(length(CAST(origin AS BLOB))+length(CAST(id AS BLOB))+length(CAST(writer AS BLOB))+
        length(CAST(fingerprint AS BLOB))+length(edit)+COALESCE(length(result),0)),0)
        FROM configuration_edits", [], |r| {
        let pending: u64 = r.get(1)?;
        let bytes: u64 = r.get(3)?;
        Ok(Usage { records: r.get(0)?, pending, legacy: r.get(2)?, bytes,
            reserved: bytes.saturating_add(pending.saturating_mul(RESULT_RESERVE as u64)) })
    })?)
}

pub(in crate::store) fn status(db: &Connection) -> Result<Value> {
    let used = usage(db)?;
    let (agent_records, _, _, agent_bytes) = agent_usage(db, "")?;
    Ok(
        json!({"records":used.records,"pending":used.pending,"legacy_records":used.legacy,
        "stored_bytes":used.bytes,"reserved_bytes":used.reserved,"max_records":MAX_RECORDS,
        "max_reserved_bytes":MAX_BYTES,"pending_result_reservation_bytes":RESULT_RESERVE,
        "cleanup_batch_limit":CLEAN_BATCH,"pending_payloads":pending::status(db)?,"retired_inputs":super::routing::retired_status(db)?,"agent_administration":{"records":agent_records,"reserved_bytes":agent_bytes,
        "max_records":MAX_AGENT_RECORDS,"max_reserved_bytes":MAX_AGENT_BYTES,"max_records_per_agent":MAX_PER_AGENT,
        "max_pending_per_agent":MAX_AGENT_PENDING}}),
    )
}

pub(super) fn result_size(result: &[u8]) -> Result<()> {
    ensure!(
        result.len() <= RESULT_RESERVE,
        "configuration result exceeds reserved space"
    );
    Ok(())
}

pub(in crate::store) fn rejection_reason(mut reason: String) -> String {
    if reason.len() > MAX_REASON_BYTES {
        let mut end = MAX_REASON_BYTES;
        while !reason.is_char_boundary(end) {
            end -= 1;
        }
        reason.truncate(end);
    }
    reason
}

pub(in crate::store) fn insert(
    db: &Connection,
    origin: &str,
    id: &str,
    writer: &str,
    fingerprint: &str,
    edit: &ConfigurationEdit,
    result: Option<&[u8]>,
) -> Result<()> {
    if let Some(result) = result {
        result_size(result)?;
    }
    let summary = serde_json::to_vec(&Summary::from(edit))?;
    clean(db, false)?;
    let used = usage(db)?;
    let bytes = [
        origin.len(),
        id.len(),
        writer.len(),
        fingerprint.len(),
        summary.len(),
        result.map_or(RESULT_RESERVE, <[u8]>::len),
    ]
    .into_iter()
    .fold(0u64, |total, size| total.saturating_add(size as u64));
    ensure!(
        used.records < MAX_RECORDS && used.reserved.saturating_add(bytes) <= MAX_BYTES,
        "configuration edit history quota exhausted; inspect gent history status; clean legacy bodies with gent history clean; retry records remain retained"
    );
    if let Some((administrator, _)) = administration::edit_identity(edit) {
        // Keep agent requests from consuming the owner history budget. Retained
        // audit records and retries cannot be evicted to admit new mutations.
        let (records, own, pending, reserved) = agent_usage(db, administrator)?;
        ensure!(
            records < MAX_AGENT_RECORDS
                && own < MAX_PER_AGENT
                && (result.is_some() || pending < MAX_AGENT_PENDING)
                && reserved.saturating_add(bytes) <= MAX_AGENT_BYTES,
            "chat administration history capacity exhausted; inspect gent history status"
        );
    }
    db.execute("INSERT INTO configuration_edits(origin,id,writer,fingerprint,edit,result,compact) VALUES(?1,?2,?3,?4,?5,?6,1)",
        params![origin,id,writer,fingerprint,summary,result])?;
    Ok(())
}

pub(in crate::store) fn clean(db: &Connection, dry_run: bool) -> Result<Value> {
    let rows = db.prepare("SELECT rowid,fingerprint,edit,origin,id,result IS NULL FROM configuration_edits WHERE compact=0 ORDER BY rowid LIMIT ?1")?
        .query_map([CLEAN_BATCH], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?, r.get::<_, Vec<u8>>(2)?,r.get::<_,String>(3)?,r.get::<_,String>(4)?,r.get::<_,bool>(5)?)))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    let mut removed = 0u64;
    for (row, expected, body, origin, id, is_pending) in &rows {
        let edit: ConfigurationEdit =
            serde_json::from_slice(body).context("invalid legacy configuration edit")?;
        // Rejected historical edits may be invalid policies. Verify their retry
        // identity without imposing today's admission rules on their contents.
        ensure!(
            fingerprint(&edit)? == *expected,
            "legacy configuration edit fingerprint mismatch"
        );
        let summary = serde_json::to_vec(&Summary::from(&edit))?;
        removed += body.len().saturating_sub(summary.len()) as u64;
        if !dry_run {
            if *is_pending {
                pending::retain(db, origin, id, &edit)?;
            }
            db.execute(
                "UPDATE configuration_edits SET edit=?1,compact=1 WHERE rowid=?2",
                params![summary, row],
            )?;
        }
    }
    let due: u64 = db.query_row(
        "SELECT COUNT(*) FROM configuration_edits WHERE compact=0",
        [],
        |r| r.get(0),
    )?;
    Ok(
        json!({"records_compacted":rows.len(),"body_bytes_removed":removed,
        "more_due":if dry_run {due.saturating_sub(rows.len() as u64)} else {due},"batch_limit":CLEAN_BATCH}),
    )
}

pub(super) fn describe(body: &[u8], compact: bool, status: &mut Value) -> Result<()> {
    let summary: Summary = if compact {
        serde_json::from_slice(body)?
    } else {
        Summary::from(&serde_json::from_slice::<ConfigurationEdit>(body)?)
    };
    match summary {
        Summary::SetReleaseChannel { channel } => status["channel"] = json!(channel),
        Summary::RenameMember { node_id, name } => {
            status["node_id"] = node_id.into();
            status["name"] = name.into();
        }
        Summary::RenameNode { name }
        | Summary::CreateGroup { name }
        | Summary::DeleteGroup { name } => status["name"] = name.into(),
        Summary::GroupMembers {
            group,
            members,
            action,
        } => {
            status["group"] = group.into();
            status["members"] = json!(members);
            status["action"] = json!(action);
        }
        Summary::AgentGroups {
            administrator,
            request_id,
            changes,
        } => {
            status["administrator"] = administrator.into();
            status["agent_request_id"] = request_id.into();
            status["changes"] = json!(changes);
            status["group_membership"] = true.into();
        }
        Summary::GrantGroupAdmin { grant_id }
        | Summary::RevokeGroupAdmin { grant_id }
        | Summary::GrantRelay { grant_id }
        | Summary::RevokeRelay { grant_id }
        | Summary::GrantPermissionAdmin { grant_id }
        | Summary::RevokePermissionAdmin { grant_id } => status["grant_id"] = grant_id.into(),
        Summary::AgentPermissions {
            administrator,
            request_id,
            changes,
        } => {
            status["administrator"] = administrator.into();
            status["agent_request_id"] = request_id.into();
            status["changes"] = json!(changes);
        }
        Summary::AgentPermissionRules {
            administrator,
            request_id,
            changes,
        } => {
            status["administrator"] = administrator.into();
            status["agent_request_id"] = request_id.into();
            status["changes"] = json!(changes);
            status["selector_rules"] = true.into();
        }
        _ => {}
    }
    Ok(())
}

#[cfg(test)]
mod tests;