chronicle-graph 0.1.0

Event-centric narrative knowledge graphs with temporal verification
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
//! Core data model: entity types, relationships, and graph structure.

use serde::{Deserialize, Serialize};

// ── Entity ID ──────────────────────────────────────────────

/// Stable string identifier for any entity in the graph.
pub type EntityId = String;

// ── Time ───────────────────────────────────────────────────

/// A span of time. Both bounds are inclusive integer years.
/// For point events, start == end.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TimeSpan {
    /// First year of the span (inclusive).
    pub start: i32,
    /// Last year of the span (inclusive).
    pub end: i32,
}

// ── Status ─────────────────────────────────────────────────

/// Current state of an entity, potentially changed by events.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Status {
    /// Entity is alive / operational / intact.
    Active,
    /// Character has died (terminal by default).
    Dead,
    /// Place has been destroyed (terminal by default).
    Destroyed,
    /// Faction or organization has been dissolved (terminal by default).
    Dissolved,
    /// Place has been evacuated but still exists.
    Evacuated,
    /// Entity has been captured by another force.
    Captured,
    /// Entity has fundamentally changed form.
    Transformed,
    /// Entity has been corrupted or tainted.
    Corrupted,
    /// Status is not yet established.
    Unknown,
}

// ── Sentiment ──────────────────────────────────────────────

/// How a participant feels about an event they were involved in.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Sentiment {
    /// Victorious elation.
    Triumphant,
    /// Justified by the outcome.
    Vindicated,
    /// Fulfilled obligation without strong emotion.
    Dutiful,
    /// Resolved to continue despite difficulty.
    Determined,
    /// Morally certain of the cause.
    Righteous,
    /// Fundamentally changed by the experience.
    Transformative,
    /// No strong feeling either way.
    Neutral,
    /// Grief or mourning.
    Sorrowful,
    /// Overcome by fear.
    Fearful,
    /// Acting out of last-resort urgency.
    Desperate,
    /// Resisting despite the odds.
    Defiant,
    /// Emotionally shattered by the event.
    Devastating,
    /// Bitter anger toward the outcome or participants.
    Resentful,
    /// Internally divided or broken by the event.
    Fractured,
}

// ── Participant Role ───────────────────────────────────────

/// The role an actor plays in an event.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Role {
    /// Initiated or led an offensive action.
    Attacker,
    /// Resisted or protected against an action.
    Defender,
    /// Commanded or directed the event.
    Leader,
    /// Took part without a specialized role.
    Participant,
    /// Observed without direct involvement.
    Witness,
    /// Made a discovery or revelation.
    Discoverer,
    /// Provoked or triggered the event.
    Instigator,
    /// Suffered harm as a result of the event.
    Victim,
    /// Attempted to resolve conflict between parties.
    Mediator,
}

// ── Fidelity ───────────────────────────────────────────────

/// How closely a subjective account matches the objective graph.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Fidelity {
    /// Matches objective graph exactly (e.g., archive-drone with intact records).
    Canonical,
    /// Omissions but no fabrication (e.g., eyewitness with incomplete view).
    Partial,
    /// Genuine misremembering (e.g., elderly NPC).
    Distorted,
    /// Deliberate spin, selective truth (e.g., faction propagandist).
    Biased,
    /// Intentional lies (e.g., trickster NPC, corrupted record).
    Fabricated,
    /// Damaged/degraded record (e.g., storm-damaged archive-drone).
    Corrupted,
}

// ── State Change ───────────────────────────────────────────

/// A change to an entity's state caused by an event.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum StateChange {
    /// Entity transitions to a new [`Status`].
    StatusChange(Status),
    /// Entity is newly created or established.
    Founded,
    /// Entity gains an affiliation with the referenced entity.
    AffiliationAdded(EntityId),
    /// Entity loses an affiliation with the referenced entity.
    AffiliationRemoved(EntityId),
}

// ── Event Participant ──────────────────────────────────────

/// An actor's participation in an event, with role and emotional response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Participant {
    /// The actor who participated.
    pub actor: EntityId,
    /// What role the actor played.
    pub role: Role,
    /// How the actor felt about the event.
    pub sentiment: Sentiment,
}

// ── Event State Change Record ──────────────────────────────

/// Links a [`StateChange`] to the entity it affects within an event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventStateChange {
    /// The entity whose state changed.
    pub entity: EntityId,
    /// The change that occurred.
    pub change: StateChange,
}

// ── Entity Types ───────────────────────────────────────────

/// Classification of an [`Actor`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ActorType {
    /// An individual person or named character.
    Character,
    /// A political, military, or ideological group.
    Faction,
    /// A divine or supernatural being.
    Deity,
    /// A structured group (guild, company, order).
    Organization,
    /// A non-humanoid living entity.
    Creature,
}

/// Classification of a [`Place`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum PlaceType {
    /// A city, town, or village.
    Settlement,
    /// A broad geographic area containing other places.
    Region,
    /// An underground or enclosed adventure site.
    Dungeon,
    /// A notable geographic or constructed feature.
    Landmark,
    /// A destroyed or abandoned former settlement.
    Ruin,
}

/// Classification of an [`Event`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum EventType {
    /// Open armed conflict.
    Battle,
    /// Prolonged encirclement or assault on a fortified place.
    Siege,
    /// Revelation of new knowledge or territory.
    Discovery,
    /// Governance, diplomacy, or power-structure change.
    Political,
    /// Large-scale movement of people.
    Migration,
    /// The death of a significant entity.
    Death,
    /// Establishment of a new settlement, faction, or institution.
    Founding,
    /// A large-scale disaster (natural or otherwise).
    Catastrophe,
    /// A magical, religious, or ceremonial event.
    Ritual,
}

/// Classification of a [`Concept`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ConceptType {
    /// A belief system or faith.
    Religion,
    /// A technique, invention, or body of knowledge.
    Technology,
    /// A unique or significant object.
    Artifact,
    /// A codified rule or legal framework.
    Law,
    /// A cultural practice or custom.
    Tradition,
}

// ── Entities (RON-deserializable) ──────────────────────────

/// A character, faction, deity, organization, or creature.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Actor {
    /// Unique identifier for this actor.
    pub id: EntityId,
    /// Display name.
    pub name: String,
    /// What kind of actor this is.
    #[serde(rename = "type")]
    pub actor_type: ActorType,
    /// Current status (defaults to [`Status::Active`]).
    #[serde(default = "default_status")]
    pub status: Status,
    /// The event that caused the current status, if any.
    #[serde(default)]
    pub status_since_event: Option<EntityId>,
    /// Factions or organizations this actor belongs to.
    #[serde(default)]
    pub affiliations: Vec<EntityId>,
    /// Birth-to-death (or founding-to-dissolution) time span.
    #[serde(default)]
    pub lifespan: Option<TimeSpan>,
    /// Free-text description.
    #[serde(default)]
    pub description: String,
}

/// A settlement, region, dungeon, landmark, or ruin.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Place {
    /// Unique identifier for this place.
    pub id: EntityId,
    /// Display name.
    pub name: String,
    /// What kind of place this is.
    #[serde(rename = "type")]
    pub place_type: PlaceType,
    /// Current status (defaults to [`Status::Active`]).
    #[serde(default = "default_status")]
    pub status: Status,
    /// The event that caused the current status, if any.
    #[serde(default)]
    pub status_since_event: Option<EntityId>,
    /// Parent region this place is located within.
    #[serde(default)]
    pub region: Option<EntityId>,
    /// Free-text description.
    #[serde(default)]
    pub description: String,
}

/// Something that happened: a battle, discovery, political shift, etc.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
    /// Unique identifier for this event.
    pub id: EntityId,
    /// Display name.
    pub name: String,
    /// What kind of event this is.
    #[serde(rename = "type")]
    pub event_type: EventType,
    /// When the event occurred (inclusive year range).
    pub time_span: TimeSpan,
    /// Where the event took place, if applicable.
    #[serde(default)]
    pub location: Option<EntityId>,
    /// Events that caused or led to this one.
    #[serde(default)]
    pub caused_by: Vec<EntityId>,
    /// Actors involved and their roles.
    #[serde(default)]
    pub participants: Vec<Participant>,
    /// State changes this event inflicted on entities.
    #[serde(default)]
    pub state_changes: Vec<EventStateChange>,
    /// Free-text description.
    #[serde(default)]
    pub description: String,
}

/// An abstract idea: religion, technology, artifact, law, or tradition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Concept {
    /// Unique identifier for this concept.
    pub id: EntityId,
    /// Display name.
    pub name: String,
    /// What kind of concept this is.
    #[serde(rename = "type")]
    pub concept_type: ConceptType,
    /// The event that introduced this concept, if any.
    #[serde(default)]
    pub origin_event: Option<EntityId>,
    /// Free-text description.
    #[serde(default)]
    pub description: String,
}

/// A subjective narrative fragment attributed to a source actor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Account {
    /// Unique identifier for this account.
    pub id: EntityId,
    /// The actor who authored or narrated this account.
    pub source: EntityId,
    /// How reliable this account is relative to the objective graph.
    pub fidelity: Fidelity,
    /// Events this account describes or references.
    #[serde(default)]
    pub event_refs: Vec<EntityId>,
    /// The narrative text, potentially containing `{entity_id}` references.
    pub text: String,
}

fn default_status() -> Status {
    Status::Active
}

// ── Validation Config ──────────────────────────────────────

/// Policy configuration for validation rules.
#[derive(Debug, Clone)]
pub struct ValidationConfig {
    /// Statuses that prevent an entity from participating in future events.
    pub terminal_statuses: std::collections::HashSet<Status>,
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            terminal_statuses: [Status::Dead, Status::Destroyed, Status::Dissolved].into(),
        }
    }
}

// ── Graph Node / Edge Enums ────────────────────────────────

/// A node in the chronicle graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Entity {
    /// An actor node (character, faction, etc.).
    Actor(Actor),
    /// A place node (settlement, region, etc.).
    Place(Place),
    /// An event node (battle, discovery, etc.).
    Event(Event),
    /// A concept node (religion, artifact, etc.).
    Concept(Concept),
    /// A subjective account node.
    Account(Account),
}

impl Entity {
    /// Returns the [`EntityId`] of the wrapped entity.
    pub fn id(&self) -> &str {
        match self {
            Entity::Actor(a) => &a.id,
            Entity::Place(p) => &p.id,
            Entity::Event(e) => &e.id,
            Entity::Concept(c) => &c.id,
            Entity::Account(a) => &a.id,
        }
    }
}

/// An edge in the chronicle graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Relationship {
    /// Actor → Event
    ParticipatedIn {
        /// The role the actor played in the event.
        role: Role,
        /// How the actor felt about the event.
        sentiment: Sentiment,
    },
    /// Event → Place
    OccurredAt,
    /// Event → Event (effect → cause)
    CausedBy,
    /// Event → affected Entity
    HasStateChange {
        /// The state change that was applied.
        change: StateChange,
    },
    /// Actor → Actor (member → faction)
    AffiliatedWith,
    /// Concept → Event
    OriginatedFrom,
    /// Account → Event
    AccountOf,
    /// Account → Actor (source)
    AuthoredBy,
    /// Account → any Entity (from {entity_id} refs in text)
    Mentions,
    /// Place → Place (child → parent region)
    LocatedIn,
}