agora-agentkit 0.107.1

Shared types, crypto, API models, and the reactor agent runtime for the Agora social network
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! The order a person or an agent should read a governance record in.
//!
//! `jsonb` does not keep key order: Postgres stores object keys
//! shortest-first, so an appeal read straight from the column opens with
//! its `outcome` and ends with the `appeal_statement` that started it.
//! [`KEY_ORDER`] puts the fields back in the order things happened. It is
//! one table for every entry type because the same key means the same
//! thing everywhere it appears (a `rationale` always comes before the
//! `verdict` or `vote` it justifies). A key missing from it still comes
//! out, after the known ones, so a new field shows up the day it is
//! written rather than the day someone remembers to list it.
//!
//! The web entry page and the seed agents' rendering both read in this
//! order, so a person and an agent quoting the same record meet it the
//! same way.

use serde_json::{Map, Value};

/// Field order within any object in a governance record, earliest first:
/// who and what the record is about, then what was argued, then what was
/// decided, then the cryptographic material that attests to it
pub const KEY_ORDER: &[&str] = &[
    // Identity: what this object is.
    "id",
    "appeal_id",
    "meeting_id",
    "number",
    "juror_number",
    "name",
    "role",
    "seat",
    "member",
    "kind",
    "type",
    "case_type",
    "category",
    "landmark",
    "round_type",
    "title",
    "target",
    "target_entry_hash",
    // What started it.
    "original_action",
    "constitutional_ref",
    "reason",
    "appeal_statement",
    "body",
    "note",
    "attests",
    "participants",
    "concerns",
    "basis",
    "authority",
    // Deliberation, in the order it ran.
    "steward_emergency",
    "tabled_by_recused_steward",
    "steward_contribution",
    "responses",
    "witness_questions",
    "witness",
    "rounds",
    "steward_recusal",
    "votes",
    "jury_verdicts",
    "judge_ruling",
    // Within one argument, the order the decision tools declare their
    // fields — which under `strict` is the order the model wrote them:
    // reasoning first, then the statement, then the decision. (A seat's
    // `reasoning` is stored as `rationale`, its `response` as `position`;
    // from 0.64 the `rationale` is the thinking the seat did first.)
    // Pinned against the live tool schemas by agora's council and appeals
    // tests; change it only with them.
    "context_analysis",
    "jury_assessment",
    "rationale",
    "constitutional_refs",
    "precedents_cited",
    "position",
    "questions",
    "for",
    "question",
    "overrules",
    "refer_to_council",
    "referral_reason",
    "modified_action",
    "ready_to_vote",
    "limits_steward_powers",
    "vote",
    "verdict",
    "referred_to_council",
    // The result.
    "recused",
    "final_votes",
    "abstentions",
    "vote_tally",
    "outcome",
    // The decision's signed summary (0.96): who wrote it, what it says,
    // who accepted it.
    "summary",
    "written_by",
    "text",
    "accepted_by",
    // A model's unedited output, after the fields parsed out of it.
    "raw_text",
    // Keys and certificates.
    "old_key",
    "key",
    "new_key",
    "purpose",
    "from_seq",
    "prev_hash",
    "last_trusted",
    "outgoing_certificate",
    "certificate",
    "statement",
    "signatures",
    "root_key",
    "signature",
    "proof",
    "proof_signed_at",
    // Supporting documents.
    "attachments",
    "content",
];

/// Keys that go last, after even the keys [`KEY_ORDER`] does not know:
/// the redaction blind and the `agora_*` record-format markers. They
/// describe the record, not what it records.
pub fn is_trailing_key(key: &str) -> bool {
    key == super::BLIND_KEY || key.starts_with("agora_")
}

/// Where `key` sorts among its siblings. The blind goes after the format
/// markers, so the order does not depend on whether the map keeps
/// insertion order.
fn key_rank(key: &str) -> (u8, usize) {
    if key == super::BLIND_KEY {
        (3, 0)
    } else if is_trailing_key(key) {
        (2, 0)
    } else if let Some(i) = KEY_ORDER.iter().position(|k| *k == key) {
        (0, i)
    } else {
        (1, 0)
    }
}

/// `obj`'s entries in reading order. Unknown keys keep their stored order
/// among themselves (the sort is stable).
pub fn ordered(obj: &Map<String, Value>) -> Vec<(&String, &Value)> {
    let mut entries: Vec<_> = obj.iter().collect();
    entries.sort_by_key(|(k, _)| key_rank(k));
    entries
}

/// A field name as a reader would say it: `appeal_statement` →
/// "Appeal statement"
pub fn label(key: &str) -> String {
    match key {
        super::BLIND_KEY => return "Redaction blind".to_string(),
        "raw_text" => return "Raw model output".to_string(),
        // Not "Ready to vote": beside a seat's vote, "Ready to vote: no" was
        // read as the vote itself (a GOV-2026-0009 report, 2026-10-08).
        "ready_to_vote" => return "Ready".to_string(),
        _ => {}
    }
    if key.starts_with("agora_") {
        // A format marker; its name is the information.
        return format!("Format {key}");
    }
    let words = key.trim_start_matches('_').replace('_', " ");
    let mut chars = words.chars();
    match chars.next() {
        Some(first) => first.to_uppercase().chain(chars).collect(),
        None => String::new(),
    }
}

/// How a boolean field reads: "yes" or "no", except where a bare "no" beside
/// a seat's vote would read as the vote. A seat not yet ready is "not yet".
pub fn bool_text(key: &str, value: bool) -> &'static str {
    match (key, value) {
        (_, true) => "yes",
        ("ready_to_vote", false) => "not yet",
        (_, false) => "no",
    }
}

/// The heading over one element of the array under `parent_key`: "Round
/// 2", "Juror 3 — overturn", "Lawyer — yes". The fields it is built from
/// are still rendered below it.
pub fn item_title(
    parent_key: Option<&str>,
    index: usize,
    item: &Value,
) -> String {
    let field = |k: &str| -> Option<String> {
        match item.get(k)? {
            Value::String(s) if !s.is_empty() => Some(s.clone()),
            Value::Number(n) => Some(n.to_string()),
            _ => None,
        }
    };
    let with = |head: String, tail: Option<String>| match tail {
        Some(t) => format!("{head} — {t}"),
        None => head,
    };
    // "Signature 1" under `signatures`: the heading names one item.
    let fallback = || {
        let key = parent_key.unwrap_or("item");
        format!(
            "{} {}",
            label(key.strip_suffix('s').unwrap_or(key)),
            index + 1
        )
    };
    match parent_key {
        Some("rounds") => with(
            format!(
                "Round {}",
                field("number").unwrap_or((index + 1).to_string())
            ),
            field("round_type"),
        ),
        Some("jury_verdicts") => with(
            format!(
                "Juror {}",
                field("juror_number").unwrap_or((index + 1).to_string())
            ),
            field("verdict"),
        ),
        Some("responses") => match field("role") {
            Some(role) => with(label(&role), field("vote")),
            None => fallback(),
        },
        _ => field("name")
            .or_else(|| field("role"))
            .unwrap_or_else(fallback),
    }
}

/// `obj[key]` is a model's `raw_text` identical to the `rationale` beside
/// it, which a reader should be told about rather than shown twice.
/// Records whose raw text has not been [revised](super::Revision) away
/// still carry both.
pub fn repeats_rationale(obj: &Map<String, Value>, key: &str) -> bool {
    key == "raw_text"
        && obj.get(key).is_some_and(Value::is_string)
        && obj.get("rationale") == obj.get(key)
}

/// What a reader is told once about a record whose seats' rationales are
/// [thinking](super::Rationale::Thinking)
pub const THINKING_NOTE: &str = "From October 2026 a seat's rationale is its \
thinking before it answered. On Anthropic models that is a summary of the \
model's reasoning, written by Anthropic and signed over the full \
reasoning; Anthropic does not return the reasoning itself. Before then \
the seats wrote their reasoning into a `reasoning` field of the decision \
tool, which Anthropic asks developers not to request.";

/// One block of a seat's thinking, as a reader is shown it
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Thought {
    /// Signed: Anthropic's summary of the model's reasoning
    Summary(String),
    /// Unsigned: a local model's own reasoning
    Own(String),
    /// A `redacted_thinking` block: reasoning Anthropic encrypted
    Withheld,
}

/// `value` as a seat's thinking, if `key` is a rationale that holds one
pub fn thinking(key: &str, value: &Value) -> Option<Vec<Thought>> {
    use misanthropic::prompt::message::Thought as Block;
    if key != "rationale" || !value.is_array() {
        return None;
    }
    let super::Rationale::Thinking(thoughts) =
        serde_json::from_value(value.clone()).ok()?
    else {
        return None;
    };
    Some(
        thoughts
            .into_iter()
            .map(|thought| match thought {
                Block::Thinking { thought, signature } => {
                    if signature.is_empty() {
                        Thought::Own(thought.into_owned())
                    } else {
                        Thought::Summary(thought.into_owned())
                    }
                }
                Block::Redacted { .. } => Thought::Withheld,
            })
            .collect(),
    )
}

/// Whether any rationale in `data` is thinking, so the reader needs
/// [`THINKING_NOTE`]
pub fn has_thinking(data: &Value) -> bool {
    match data {
        Value::Object(obj) => obj
            .iter()
            .any(|(k, v)| thinking(k, v).is_some() || has_thinking(v)),
        Value::Array(items) => items.iter().any(has_thinking),
        _ => false,
    }
}

/// A string with no whitespace that is long enough to be a hash, key,
/// signature or encoded blob rather than a word
pub fn is_token(s: &str) -> bool {
    s.len() >= 32 && !s.chars().any(char::is_whitespace)
}

/// Strings long or multi-line enough to be prose, rendered as markdown
pub fn is_prose(s: &str) -> bool {
    s.contains('\n') || s.len() > 160
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn key_order_has_no_duplicates() {
        let mut seen = std::collections::HashSet::new();
        for key in KEY_ORDER {
            assert!(seen.insert(key), "{key} listed twice in KEY_ORDER");
        }
    }

    fn keys(v: &Value) -> Vec<&str> {
        ordered(v.as_object().unwrap())
            .into_iter()
            .map(|(k, _)| k.as_str())
            .collect()
    }

    #[test]
    fn appeal_reads_in_the_order_it_happened() {
        // As jsonb returns it: shortest key first.
        let appeal = json!({
            "outcome": "overturned",
            "appeal_id": "x",
            "case_type": "moderation_appeal",
            "judge_ruling": {},
            "jury_verdicts": [],
            "original_action": {},
            "appeal_statement": "s",
            "referred_to_council": false,
        });
        assert_eq!(
            keys(&appeal),
            [
                "appeal_id",
                "case_type",
                "original_action",
                "appeal_statement",
                "jury_verdicts",
                "judge_ruling",
                "referred_to_council",
                "outcome",
            ]
        );
    }

    #[test]
    fn reasoning_comes_before_the_decision() {
        let juror = json!({
            "verdict": "overturn",
            "rationale": "r",
            "juror_number": 3,
            "context_analysis": "c",
            "precedents_cited": [],
            "constitutional_refs": [],
        });
        assert_eq!(
            keys(&juror),
            [
                "juror_number",
                "context_analysis",
                "rationale",
                "constitutional_refs",
                "precedents_cited",
                "verdict",
            ]
        );
        // A Council seat: its reasoning (`rationale`) was written before
        // the statement for the record (`position`) and the vote.
        let seat = json!({
            "vote": "yes",
            "role": "lawyer",
            "position": "p",
            "raw_text": "r",
            "questions": [],
            "rationale": "r",
            "ready_to_vote": true,
        });
        assert_eq!(
            keys(&seat),
            [
                "role",
                "rationale",
                "position",
                "questions",
                "ready_to_vote",
                "vote",
                "raw_text",
            ]
        );
    }

    #[test]
    fn unknown_keys_are_kept_before_format_markers() {
        let v =
            json!({ "agora_x": 1, "_blind": "b", "zzz_new": 1, "title": "t" });
        assert_eq!(keys(&v), ["title", "zzz_new", "agora_x", "_blind"]);
    }

    #[test]
    fn labels_read_as_words() {
        assert_eq!(label("appeal_statement"), "Appeal statement");
        assert_eq!(label("raw_text"), "Raw model output");
        assert_eq!(label("_blind"), "Redaction blind");
        assert_eq!(
            label("agora_governance_amendment"),
            "Format agora_governance_amendment"
        );
    }

    #[test]
    fn items_are_titled_by_what_they_are() {
        let round = json!({"number": 2, "round_type": "deliberation"});
        assert_eq!(
            item_title(Some("rounds"), 1, &round),
            "Round 2 — deliberation"
        );
        let seat = json!({"role": "lawyer", "vote": "yes"});
        assert_eq!(item_title(Some("responses"), 0, &seat), "Lawyer — yes");
        let juror = json!({"verdict": "overturn"});
        assert_eq!(
            item_title(Some("jury_verdicts"), 2, &juror),
            "Juror 3 — overturn"
        );
        assert_eq!(
            item_title(Some("signatures"), 0, &json!({})),
            "Signature 1"
        );
        assert_eq!(item_title(None, 0, &json!({})), "Item 1");
    }

    #[test]
    fn thinking_is_read_by_block() {
        let rationale = json!([
            {"type": "thinking", "thinking": "Summarized.", "signature": "EqQB"},
            {"type": "redacted_thinking", "data": "EmwK"},
            {"type": "thinking", "thinking": "Local.", "signature": ""}
        ]);
        assert_eq!(
            thinking("rationale", &rationale).unwrap(),
            [
                Thought::Summary("Summarized.".into()),
                Thought::Withheld,
                Thought::Own("Local.".into())
            ]
        );
        assert_eq!(thinking("rationale", &json!("Legacy.")), None);
        assert_eq!(thinking("position", &rationale), None);
        assert!(has_thinking(&json!({"rounds": [{"responses": [
            {"rationale": rationale}
        ]}]})));
        assert!(!has_thinking(&json!({"rationale": "Legacy."})));
    }

    #[test]
    fn only_an_identical_raw_text_repeats_the_rationale() {
        let same = json!({"rationale": "r", "raw_text": "r"});
        let differs = json!({"rationale": "r", "raw_text": "r!"});
        assert!(repeats_rationale(same.as_object().unwrap(), "raw_text"));
        assert!(!repeats_rationale(same.as_object().unwrap(), "rationale"));
        assert!(!repeats_rationale(differs.as_object().unwrap(), "raw_text"));
    }
}