@ai-rpg-engine/campaign-memory 2.1.6 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,107 +2,107 @@
2
2
  <a href="README.ja.md">日本語</a> | <a href="README.zh.md">中文</a> | <a href="README.es.md">Español</a> | <a href="README.fr.md">Français</a> | <a href="README.hi.md">हिन्दी</a> | <a href="README.it.md">Italiano</a> | <a href="README.pt-BR.md">Português (BR)</a>
3
3
  </p>
4
4
 
5
- <p align="center">
6
- <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
- </p>
8
-
9
- # @ai-rpg-engine/campaign-memory
10
-
11
- [![npm](https://img.shields.io/npm/v/@ai-rpg-engine/campaign-memory)](https://www.npmjs.com/package/@ai-rpg-engine/campaign-memory)
12
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/mcp-tool-shop-org/ai-rpg-engine/blob/main/LICENSE)
13
-
14
- Persistent NPC memory, multi-axis relationships, and campaign journal for [AI RPG Engine](https://github.com/mcp-tool-shop-org/ai-rpg-engine).
15
-
16
- ## Install
17
-
18
- ```bash
19
- npm install @ai-rpg-engine/campaign-memory
20
- ```
21
-
22
- ## What It Does
23
-
24
- NPCs remember what happened. Not just "hostile: true" — they track trust, fear, admiration, and familiarity across every interaction. Memories fade over time: vivid → faded → dim → forgotten. The campaign journal persists significant events across sessions.
25
-
26
- ## Usage
27
-
28
- ### Campaign Journal
29
-
30
- ```typescript
31
- import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
-
33
- const journal = new CampaignJournal();
34
-
35
- // Record a significant event
36
- const record = journal.record({
37
- tick: 42,
38
- category: 'kill',
39
- actorId: 'player',
40
- targetId: 'merchant_1',
41
- zoneId: 'market',
42
- description: 'Player killed the merchant during a robbery',
43
- significance: 0.9,
44
- witnesses: ['guard_1', 'bystander_2'],
45
- data: { weapon: 'dagger' },
46
- });
47
-
48
- // Query the journal
49
- const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
- const merchantHistory = journal.getInvolving('merchant_1');
51
- ```
52
-
53
- ### NPC Memory Bank
54
-
55
- ```typescript
56
- import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
-
58
- const guardMemory = new NpcMemoryBank('guard_1');
59
-
60
- // Guard witnesses the player killing a merchant
61
- guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
- applyRelationshipEffect(guardMemory, record, 'witness');
63
-
64
- // Check how the guard feels about the player
65
- const rel = guardMemory.getRelationship('player');
66
- // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
-
68
- // Later, memories fade
69
- guardMemory.consolidate(currentTick);
70
- const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
- ```
72
-
73
- ### Relationship Axes
74
-
75
- | Axis | Range | Meaning |
76
- |------|-------|---------|
77
- | trust | -1 to 1 | Distrust → Trust |
78
- | fear | 0 to 1 | Unafraid → Terrified |
79
- | admiration | -1 to 1 | Contempt → Admiration |
80
- | familiarity | 0 to 1 | Stranger → Intimate |
81
-
82
- ### Record Categories
83
-
84
- `action` · `combat` · `kill` · `betrayal` · `gift` · `theft` · `debt` · `discovery` · `alliance` · `insult` · `rescue` · `death`
85
-
86
- Each category has default relationship effects. A rescue builds trust (+0.4) and admiration (+0.3). A betrayal destroys trust (-0.5).
87
-
88
- ### Memory Consolidation
89
-
90
- Memories decay over time through three stages:
91
-
92
- - **vivid** — high salience, recently formed
93
- - **faded** — salience dropped below fade threshold
94
- - **dim** — barely remembered, about to be forgotten
95
-
96
- Configure decay rates per-NPC or globally.
97
-
98
- ## Serialization
99
-
100
- Both `CampaignJournal` and `NpcMemoryBank` support `serialize()` and `deserialize()` for persistence across sessions.
101
-
102
- ## Part of AI RPG Engine
103
-
104
- This package is part of the [AI RPG Engine](https://github.com/mcp-tool-shop-org/ai-rpg-engine) monorepo. It works standalone or integrates with the engine's cognition and rumor systems.
105
-
106
- ## License
107
-
108
- MIT
5
+ <p align="center">
6
+ <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
+ </p>
8
+
9
+ # @ai-rpg-engine/campaign-memory
10
+
11
+ [![npm](https://img.shields.io/npm/v/@ai-rpg-engine/campaign-memory)](https://www.npmjs.com/package/@ai-rpg-engine/campaign-memory)
12
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/mcp-tool-shop-org/ai-rpg-engine/blob/main/LICENSE)
13
+
14
+ Persistent NPC memory, multi-axis relationships, and campaign journal for [AI RPG Engine](https://github.com/mcp-tool-shop-org/ai-rpg-engine).
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ npm install @ai-rpg-engine/campaign-memory
20
+ ```
21
+
22
+ ## What It Does
23
+
24
+ NPCs remember what happened. Not just "hostile: true" — they track trust, fear, admiration, and familiarity across every interaction. Memories fade over time: vivid → faded → dim → forgotten. The campaign journal persists significant events across sessions.
25
+
26
+ ## Usage
27
+
28
+ ### Campaign Journal
29
+
30
+ ```typescript
31
+ import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
+
33
+ const journal = new CampaignJournal();
34
+
35
+ // Record a significant event
36
+ const record = journal.record({
37
+ tick: 42,
38
+ category: 'kill',
39
+ actorId: 'player',
40
+ targetId: 'merchant_1',
41
+ zoneId: 'market',
42
+ description: 'Player killed the merchant during a robbery',
43
+ significance: 0.9,
44
+ witnesses: ['guard_1', 'bystander_2'],
45
+ data: { weapon: 'dagger' },
46
+ });
47
+
48
+ // Query the journal
49
+ const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
+ const merchantHistory = journal.getInvolving('merchant_1');
51
+ ```
52
+
53
+ ### NPC Memory Bank
54
+
55
+ ```typescript
56
+ import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
+
58
+ const guardMemory = new NpcMemoryBank('guard_1');
59
+
60
+ // Guard witnesses the player killing a merchant
61
+ guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
+ applyRelationshipEffect(guardMemory, record, 'witness');
63
+
64
+ // Check how the guard feels about the player
65
+ const rel = guardMemory.getRelationship('player');
66
+ // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
+
68
+ // Later, memories fade
69
+ guardMemory.consolidate(currentTick);
70
+ const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
+ ```
72
+
73
+ ### Relationship Axes
74
+
75
+ | Axis | Range | Meaning |
76
+ |------|-------|---------|
77
+ | trust | -1 to 1 | Distrust → Trust |
78
+ | fear | 0 to 1 | Unafraid → Terrified |
79
+ | admiration | -1 to 1 | Contempt → Admiration |
80
+ | familiarity | 0 to 1 | Stranger → Intimate |
81
+
82
+ ### Record Categories
83
+
84
+ `action` · `combat` · `kill` · `betrayal` · `gift` · `theft` · `debt` · `discovery` · `alliance` · `insult` · `rescue` · `death`
85
+
86
+ Each category has default relationship effects. A rescue builds trust (+0.4) and admiration (+0.3). A betrayal destroys trust (-0.5).
87
+
88
+ ### Memory Consolidation
89
+
90
+ Memories decay over time through three stages:
91
+
92
+ - **vivid** — high salience, recently formed
93
+ - **faded** — salience dropped below fade threshold
94
+ - **dim** — barely remembered, about to be forgotten
95
+
96
+ Configure decay rates per-NPC or globally.
97
+
98
+ ## Serialization
99
+
100
+ Both `CampaignJournal` and `NpcMemoryBank` support `serialize()` and `deserialize()` for persistence across sessions.
101
+
102
+ ## Part of AI RPG Engine
103
+
104
+ This package is part of the [AI RPG Engine](https://github.com/mcp-tool-shop-org/ai-rpg-engine) monorepo. It works standalone or integrates with the engine's cognition and rumor systems.
105
+
106
+ ## License
107
+
108
+ MIT
package/README.pt-BR.md CHANGED
@@ -2,9 +2,9 @@
2
2
  <a href="README.ja.md">日本語</a> | <a href="README.zh.md">中文</a> | <a href="README.es.md">Español</a> | <a href="README.fr.md">Français</a> | <a href="README.hi.md">हिन्दी</a> | <a href="README.it.md">Italiano</a> | <a href="README.md">English</a>
3
3
  </p>
4
4
 
5
- <p align="center">
6
- <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
- </p>
5
+ <p align="center">
6
+ <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
+ </p>
8
8
 
9
9
  # @ai-rpg-engine/campaign-memory
10
10
 
@@ -15,9 +15,9 @@ Memória persistente de NPCs, relações multi-eixos e diário de campanha para
15
15
 
16
16
  ## Instalação
17
17
 
18
- ```bash
19
- npm install @ai-rpg-engine/campaign-memory
20
- ```
18
+ ```bash
19
+ npm install @ai-rpg-engine/campaign-memory
20
+ ```
21
21
 
22
22
  ## O que ele faz
23
23
 
@@ -27,48 +27,48 @@ Os NPCs se lembram do que aconteceu. Não apenas "hostil: verdadeiro" — eles r
27
27
 
28
28
  ### Diário de Campanha
29
29
 
30
- ```typescript
31
- import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
-
33
- const journal = new CampaignJournal();
34
-
35
- // Record a significant event
36
- const record = journal.record({
37
- tick: 42,
38
- category: 'kill',
39
- actorId: 'player',
40
- targetId: 'merchant_1',
41
- zoneId: 'market',
42
- description: 'Player killed the merchant during a robbery',
43
- significance: 0.9,
44
- witnesses: ['guard_1', 'bystander_2'],
45
- data: { weapon: 'dagger' },
46
- });
47
-
48
- // Query the journal
49
- const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
- const merchantHistory = journal.getInvolving('merchant_1');
51
- ```
30
+ ```typescript
31
+ import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
+
33
+ const journal = new CampaignJournal();
34
+
35
+ // Record a significant event
36
+ const record = journal.record({
37
+ tick: 42,
38
+ category: 'kill',
39
+ actorId: 'player',
40
+ targetId: 'merchant_1',
41
+ zoneId: 'market',
42
+ description: 'Player killed the merchant during a robbery',
43
+ significance: 0.9,
44
+ witnesses: ['guard_1', 'bystander_2'],
45
+ data: { weapon: 'dagger' },
46
+ });
47
+
48
+ // Query the journal
49
+ const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
+ const merchantHistory = journal.getInvolving('merchant_1');
51
+ ```
52
52
 
53
53
  ### Banco de Memória de NPCs
54
54
 
55
- ```typescript
56
- import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
-
58
- const guardMemory = new NpcMemoryBank('guard_1');
59
-
60
- // Guard witnesses the player killing a merchant
61
- guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
- applyRelationshipEffect(guardMemory, record, 'witness');
63
-
64
- // Check how the guard feels about the player
65
- const rel = guardMemory.getRelationship('player');
66
- // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
-
68
- // Later, memories fade
69
- guardMemory.consolidate(currentTick);
70
- const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
- ```
55
+ ```typescript
56
+ import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
+
58
+ const guardMemory = new NpcMemoryBank('guard_1');
59
+
60
+ // Guard witnesses the player killing a merchant
61
+ guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
+ applyRelationshipEffect(guardMemory, record, 'witness');
63
+
64
+ // Check how the guard feels about the player
65
+ const rel = guardMemory.getRelationship('player');
66
+ // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
+
68
+ // Later, memories fade
69
+ guardMemory.consolidate(currentTick);
70
+ const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
+ ```
72
72
 
73
73
  ### Eixos de Relação
74
74
 
package/README.zh.md CHANGED
@@ -2,9 +2,9 @@
2
2
  <a href="README.ja.md">日本語</a> | <a href="README.md">English</a> | <a href="README.es.md">Español</a> | <a href="README.fr.md">Français</a> | <a href="README.hi.md">हिन्दी</a> | <a href="README.it.md">Italiano</a> | <a href="README.pt-BR.md">Português (BR)</a>
3
3
  </p>
4
4
 
5
- <p align="center">
6
- <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
- </p>
5
+ <p align="center">
6
+ <img src="https://raw.githubusercontent.com/mcp-tool-shop-org/brand/main/logos/ai-rpg-engine/readme.png" width="400" alt="AI RPG Engine">
7
+ </p>
8
8
 
9
9
  # @ai-rpg-engine/campaign-memory
10
10
 
@@ -15,9 +15,9 @@
15
15
 
16
16
  ## 安装
17
17
 
18
- ```bash
19
- npm install @ai-rpg-engine/campaign-memory
20
- ```
18
+ ```bash
19
+ npm install @ai-rpg-engine/campaign-memory
20
+ ```
21
21
 
22
22
  ## 功能
23
23
 
@@ -27,48 +27,48 @@ NPC 会记住发生的事情。 不仅仅是“敌对:true”,他们会跟
27
27
 
28
28
  ### 战役日志
29
29
 
30
- ```typescript
31
- import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
-
33
- const journal = new CampaignJournal();
34
-
35
- // Record a significant event
36
- const record = journal.record({
37
- tick: 42,
38
- category: 'kill',
39
- actorId: 'player',
40
- targetId: 'merchant_1',
41
- zoneId: 'market',
42
- description: 'Player killed the merchant during a robbery',
43
- significance: 0.9,
44
- witnesses: ['guard_1', 'bystander_2'],
45
- data: { weapon: 'dagger' },
46
- });
47
-
48
- // Query the journal
49
- const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
- const merchantHistory = journal.getInvolving('merchant_1');
51
- ```
30
+ ```typescript
31
+ import { CampaignJournal } from '@ai-rpg-engine/campaign-memory';
32
+
33
+ const journal = new CampaignJournal();
34
+
35
+ // Record a significant event
36
+ const record = journal.record({
37
+ tick: 42,
38
+ category: 'kill',
39
+ actorId: 'player',
40
+ targetId: 'merchant_1',
41
+ zoneId: 'market',
42
+ description: 'Player killed the merchant during a robbery',
43
+ significance: 0.9,
44
+ witnesses: ['guard_1', 'bystander_2'],
45
+ data: { weapon: 'dagger' },
46
+ });
47
+
48
+ // Query the journal
49
+ const playerActions = journal.query({ actorId: 'player', category: 'kill' });
50
+ const merchantHistory = journal.getInvolving('merchant_1');
51
+ ```
52
52
 
53
53
  ### NPC 记忆库
54
54
 
55
- ```typescript
56
- import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
-
58
- const guardMemory = new NpcMemoryBank('guard_1');
59
-
60
- // Guard witnesses the player killing a merchant
61
- guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
- applyRelationshipEffect(guardMemory, record, 'witness');
63
-
64
- // Check how the guard feels about the player
65
- const rel = guardMemory.getRelationship('player');
66
- // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
-
68
- // Later, memories fade
69
- guardMemory.consolidate(currentTick);
70
- const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
- ```
55
+ ```typescript
56
+ import { NpcMemoryBank, applyRelationshipEffect } from '@ai-rpg-engine/campaign-memory';
57
+
58
+ const guardMemory = new NpcMemoryBank('guard_1');
59
+
60
+ // Guard witnesses the player killing a merchant
61
+ guardMemory.remember(record, 0.9, -0.8); // high salience, very negative
62
+ applyRelationshipEffect(guardMemory, record, 'witness');
63
+
64
+ // Check how the guard feels about the player
65
+ const rel = guardMemory.getRelationship('player');
66
+ // { trust: -0.15, fear: 0.25, admiration: -0.05, familiarity: 0 }
67
+
68
+ // Later, memories fade
69
+ guardMemory.consolidate(currentTick);
70
+ const memories = guardMemory.recall({ aboutEntity: 'player', minSalience: 0.5 });
71
+ ```
72
72
 
73
73
  ### 关系维度
74
74
 
package/dist/journal.d.ts CHANGED
@@ -8,6 +8,12 @@ export type JournalQueryFilters = {
8
8
  };
9
9
  export declare class CampaignJournal {
10
10
  private records;
11
+ /**
12
+ * Per-instance ID counter. Record IDs depend only on (this journal's history),
13
+ * never on cross-instance order — see CP-01. Two journals number independently.
14
+ */
15
+ private nextRecordId;
16
+ private generateId;
11
17
  /** Record a significant event. Returns the created record with generated ID. */
12
18
  record(entry: Omit<CampaignRecord, 'id'>): CampaignRecord;
13
19
  /** Get a specific record by ID */
@@ -20,7 +26,13 @@ export declare class CampaignJournal {
20
26
  size(): number;
21
27
  /** Serializable state for persistence */
22
28
  serialize(): CampaignRecord[];
23
- /** Restore from serialized state */
29
+ /**
30
+ * Restore from serialized state.
31
+ *
32
+ * CA-06: guards malformed input with a clear, actionable error instead of letting a raw
33
+ * TypeError escape ("records is not iterable", "cannot read 'id' of null"). The thrown
34
+ * Error names the offending element index/field and how to fix it.
35
+ */
24
36
  static deserialize(records: CampaignRecord[]): CampaignJournal;
25
37
  }
26
38
  //# sourceMappingURL=journal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"journal.d.ts","sourceRoot":"","sources":["../src/journal.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAQjE,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAA0C;IAEzD,gFAAgF;IAChF,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,IAAI,CAAC,GAAG,cAAc;IAMzD,kCAAkC;IAClC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAI3C,gEAAgE;IAChE,KAAK,CAAC,OAAO,EAAE,mBAAmB,GAAG,cAAc,EAAE;IAsBrD,+DAA+D;IAC/D,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,EAAE;IAMhD,uCAAuC;IACvC,IAAI,IAAI,MAAM;IAId,yCAAyC;IACzC,SAAS,IAAI,cAAc,EAAE;IAI7B,oCAAoC;IACpC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,eAAe;CAgB/D"}
1
+ {"version":3,"file":"journal.d.ts","sourceRoot":"","sources":["../src/journal.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAA0C;IACzD;;;OAGG;IACH,OAAO,CAAC,YAAY,CAAK;IAEzB,OAAO,CAAC,UAAU;IAIlB,gFAAgF;IAChF,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,IAAI,CAAC,GAAG,cAAc;IAMzD,kCAAkC;IAClC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAI3C,gEAAgE;IAChE,KAAK,CAAC,OAAO,EAAE,mBAAmB,GAAG,cAAc,EAAE;IAsBrD,+DAA+D;IAC/D,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,EAAE;IAMhD,uCAAuC;IACvC,IAAI,IAAI,MAAM;IAId,yCAAyC;IACzC,SAAS,IAAI,cAAc,EAAE;IAI7B;;;;;;OAMG;IACH,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,eAAe;CAgC/D"}
package/dist/journal.js CHANGED
@@ -1,13 +1,17 @@
1
1
  // CampaignJournal — persistent record of significant campaign events
2
- let nextRecordId = 1;
3
- function generateId() {
4
- return `cr_${nextRecordId++}`;
5
- }
6
2
  export class CampaignJournal {
7
3
  records = new Map();
4
+ /**
5
+ * Per-instance ID counter. Record IDs depend only on (this journal's history),
6
+ * never on cross-instance order — see CP-01. Two journals number independently.
7
+ */
8
+ nextRecordId = 1;
9
+ generateId() {
10
+ return `cr_${this.nextRecordId++}`;
11
+ }
8
12
  /** Record a significant event. Returns the created record with generated ID. */
9
13
  record(entry) {
10
- const record = { id: generateId(), ...entry };
14
+ const record = { id: this.generateId(), ...entry };
11
15
  this.records.set(record.id, record);
12
16
  return record;
13
17
  }
@@ -49,13 +53,29 @@ export class CampaignJournal {
49
53
  serialize() {
50
54
  return Array.from(this.records.values()).sort((a, b) => a.tick - b.tick);
51
55
  }
52
- /** Restore from serialized state */
56
+ /**
57
+ * Restore from serialized state.
58
+ *
59
+ * CA-06: guards malformed input with a clear, actionable error instead of letting a raw
60
+ * TypeError escape ("records is not iterable", "cannot read 'id' of null"). The thrown
61
+ * Error names the offending element index/field and how to fix it.
62
+ */
53
63
  static deserialize(records) {
64
+ if (!Array.isArray(records)) {
65
+ throw new Error(`CampaignJournal.deserialize: expected an array of records (got ${records === null ? 'null' : typeof records}) — pass the value returned by serialize()`);
66
+ }
54
67
  const journal = new CampaignJournal();
55
- for (const record of records) {
68
+ for (let i = 0; i < records.length; i++) {
69
+ const record = records[i];
70
+ if (typeof record !== 'object' || record === null) {
71
+ throw new Error(`CampaignJournal.deserialize: record[${i}] must be an object (got ${record === null ? 'null' : typeof record})`);
72
+ }
73
+ if (typeof record.id !== 'string') {
74
+ throw new Error(`CampaignJournal.deserialize: record[${i}] is missing a string "id" — each record needs an id like "cr_1"`);
75
+ }
56
76
  journal.records.set(record.id, record);
57
77
  }
58
- // Ensure ID counter is ahead of existing records
78
+ // Advance THIS instance's counter past the highest restored id (CP-01).
59
79
  let maxNum = 0;
60
80
  for (const r of records) {
61
81
  const match = r.id.match(/^cr_(\d+)$/);
@@ -63,7 +83,7 @@ export class CampaignJournal {
63
83
  maxNum = Math.max(maxNum, parseInt(match[1], 10));
64
84
  }
65
85
  }
66
- nextRecordId = maxNum + 1;
86
+ journal.nextRecordId = maxNum + 1;
67
87
  return journal;
68
88
  }
69
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"journal.js","sourceRoot":"","sources":["../src/journal.ts"],"names":[],"mappings":"AAAA,qEAAqE;AAIrE,IAAI,YAAY,GAAG,CAAC,CAAC;AAErB,SAAS,UAAU;IACjB,OAAO,MAAM,YAAY,EAAE,EAAE,CAAC;AAChC,CAAC;AAUD,MAAM,OAAO,eAAe;IAClB,OAAO,GAAgC,IAAI,GAAG,EAAE,CAAC;IAEzD,gFAAgF;IAChF,MAAM,CAAC,KAAiC;QACtC,MAAM,MAAM,GAAmB,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,GAAG,KAAK,EAAE,CAAC;QAC9D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACpC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,kCAAkC;IAClC,GAAG,CAAC,EAAU;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,gEAAgE;IAChE,KAAK,CAAC,OAA4B;QAChC,IAAI,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAEhD,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;YAClC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;QACjE,CAAC;QACD,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACnC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,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{
25
25
  getEntry(subjectId: string): NpcMemoryEntry | undefined;
26
26
  /** Serializable state */
27
27
  serialize(): NpcMemoryState;
28
- /** Restore from serialized state */
28
+ /**
29
+ * Restore from serialized state.
30
+ *
31
+ * CA-06: guards malformed state with a clear, actionable error instead of building a
32
+ * half-formed bank that throws a raw TypeError on the next call. The thrown Error names
33
+ * the offending field and how to fix it.
34
+ */
29
35
  static deserialize(state: NpcMemoryState, config?: CampaignMemoryConfig): NpcMemoryBank;
30
36
  private getOrCreateEntry;
31
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"memory-bank.d.ts","sourceRoot":"","sources":["../src/memory-bank.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,cAAc,EACd,WAAW,EACX,cAAc,EACd,cAAc,EACd,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAUpB,qBAAa,aAAa;IACxB,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,MAAM,CAAiC;gBAEnC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,oBAAoB;IAK3D,oDAAoD;IACpD,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,kEAAkE;IAClE,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,IAAI;IA8BjF,8CAA8C;IAC9C,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB;IAMpD,oEAAoE;IACpE,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAkB9E,mDAAmD;IACnD,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,cAAc,EAAE;IA2B5C,gDAAgD;IAChD,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAa9D,gFAAgF;IAChF,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG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;IAChF,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAyBtC,+CAA+C;IAC/C,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IASpC,qDAAqD;IACrD,aAAa,IAAI,MAAM,EAAE;IAIzB,8CAA8C;IAC9C,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAUvD,yBAAyB;IACzB,SAAS,IAAI,cAAc;IAI3B;;;;;;OAMG;IACH,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,oBAAoB,GAAG,aAAa;IA0BvF,OAAO,CAAC,gBAAgB;CAYzB"}
@@ -115,6 +115,7 @@ export class NpcMemoryBank {
115
115
  continue;
116
116
  // Decay salience
117
117
  mem.salience = Math.max(0, mem.salience - this.config.decayRate * elapsed);
118
+ mem.tick = currentTick;
118
119
  // Update consolidation state
119
120
  if (mem.salience < this.config.dimThreshold) {
120
121
  mem.consolidation = 'dim';
@@ -142,14 +143,37 @@ export class NpcMemoryBank {
142
143
  }
143
144
  /** Get the full memory entry for a subject */
144
145
  getEntry(subjectId) {
145
- return this.state.subjects[subjectId];
146
+ const entry = this.state.subjects[subjectId];
147
+ if (!entry)
148
+ return undefined;
149
+ return {
150
+ ...entry,
151
+ relationship: { ...entry.relationship },
152
+ memories: entry.memories.map((m) => ({ ...m })),
153
+ };
146
154
  }
147
155
  /** Serializable state */
148
156
  serialize() {
149
157
  return JSON.parse(JSON.stringify(this.state));
150
158
  }
151
- /** Restore from serialized state */
159
+ /**
160
+ * Restore from serialized state.
161
+ *
162
+ * CA-06: guards malformed state with a clear, actionable error instead of building a
163
+ * half-formed bank that throws a raw TypeError on the next call. The thrown Error names
164
+ * the offending field and how to fix it.
165
+ */
152
166
  static deserialize(state, config) {
167
+ if (typeof state !== 'object' || state === null || Array.isArray(state)) {
168
+ throw new Error(`NpcMemoryBank.deserialize: expected a state object (got ${state === null ? 'null' : Array.isArray(state) ? 'array' : typeof state}) — pass the value returned by serialize()`);
169
+ }
170
+ if (typeof state.entityId !== 'string') {
171
+ throw new Error('NpcMemoryBank.deserialize: state is missing a string "entityId" — this is the NPC the bank belongs to');
172
+ }
173
+ const subjects = state.subjects;
174
+ if (typeof subjects !== 'object' || subjects === null || Array.isArray(subjects)) {
175
+ throw new Error(`NpcMemoryBank.deserialize: state.subjects must be an object mapping subjectId → entry (got ${subjects === undefined ? 'undefined' : subjects === null ? 'null' : Array.isArray(subjects) ? 'array' : typeof subjects})`);
176
+ }
153
177
  const bank = new NpcMemoryBank(state.entityId, config);
154
178
  bank.state = JSON.parse(JSON.stringify(state));
155
179
  return bank;