@agent-e/core 1.1.1 → 1.1.3

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 ADDED
@@ -0,0 +1,136 @@
1
+ # @agent-e/core
2
+
3
+ Autonomous economic balancer SDK. 60 built-in principles, 5-stage pipeline, zero dependencies.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @agent-e/core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { AgentE } from '@agent-e/core';
15
+
16
+ const agent = new AgentE({
17
+ adapter: {
18
+ getState: () => ({
19
+ tick: currentTick,
20
+ agentBalances: { /* id → gold */ },
21
+ agentRoles: { /* id → role */ },
22
+ agentInventories: { /* id → { resource → qty } */ },
23
+ marketPrices: { /* resource → price */ },
24
+ agentSatisfaction: { /* id → 0-100 */ },
25
+ poolSizes: { /* pool → amount */ },
26
+ }),
27
+ setParam: async (param, value) => {
28
+ // Apply parameter change to your economy
29
+ },
30
+ },
31
+ mode: 'advisor', // or 'autonomous'
32
+ });
33
+
34
+ agent.connect(agent.adapter).start();
35
+
36
+ // Call once per tick in your loop:
37
+ await agent.tick();
38
+ ```
39
+
40
+ ## The 5-Stage Pipeline
41
+
42
+ 1. **Observer** — Translates raw state into 40+ metrics at 3 resolutions (fine/medium/coarse)
43
+ 2. **Diagnoser** — Runs 60 principles, returns violations sorted by severity
44
+ 3. **Simulator** — Monte Carlo forward projection (≥100 iterations) before any action
45
+ 4. **Planner** — Lag-aware, cooldown-aware planning with rollback conditions
46
+ 5. **Executor** — Applies actions and monitors for rollback triggers
47
+
48
+ ## Modes
49
+
50
+ | Mode | Behavior |
51
+ |------|----------|
52
+ | `autonomous` | Full pipeline — executes parameter changes automatically |
53
+ | `advisor` | Full pipeline but stops before execution — emits recommendations via `onDecision` |
54
+
55
+ ## 60 Principles
56
+
57
+ Organized across 15 categories: supply chain, incentives, population, currency flow, bootstrap, feedback loops, regulator, market dynamics, measurement, statistical, system dynamics, resource management, player experience, open economy, and liveops.
58
+
59
+ Each principle has a `check(metrics, thresholds)` function that returns either `{ violated: false }` or a violation with severity, evidence, suggested action, confidence, and estimated lag.
60
+
61
+ ## Developer API
62
+
63
+ ```typescript
64
+ // Lock a parameter from automated adjustment
65
+ agent.lock('craftingCost');
66
+ agent.unlock('craftingCost');
67
+
68
+ // Constrain a parameter to a range
69
+ agent.constrain('auctionFee', { min: 0.01, max: 0.50 });
70
+
71
+ // Add a custom principle
72
+ agent.addPrinciple(myPrinciple);
73
+
74
+ // Register a custom metric
75
+ agent.registerCustomMetric('myMetric', (state) => compute(state));
76
+
77
+ // Veto actions
78
+ agent.on('beforeAction', (plan) => {
79
+ if (plan.targetValue > 2.0) return false;
80
+ });
81
+
82
+ // Query decision history
83
+ const decisions = agent.getDecisions({ outcome: 'applied' });
84
+
85
+ // Health score (0-100)
86
+ const health = agent.getHealth();
87
+
88
+ // Metric time-series
89
+ const latest = agent.metrics.latest('fine');
90
+ ```
91
+
92
+ ## Custom Principles
93
+
94
+ ```typescript
95
+ import type { Principle } from '@agent-e/core';
96
+
97
+ const myRule: Principle = {
98
+ id: 'MY_01',
99
+ name: 'Healer Population Floor',
100
+ category: 'population',
101
+ description: 'Healer share below 5% is a crisis',
102
+ check(metrics, thresholds) {
103
+ const share = metrics.roleShares['Healer'] ?? 0;
104
+ if (share < 0.05) {
105
+ return {
106
+ violated: true,
107
+ severity: 8,
108
+ evidence: { share },
109
+ suggestedAction: {
110
+ parameter: 'healerReward',
111
+ direction: 'increase',
112
+ magnitude: 0.25,
113
+ reasoning: 'Healer population critically low.',
114
+ },
115
+ confidence: 0.90,
116
+ estimatedLag: 10,
117
+ };
118
+ }
119
+ return { violated: false };
120
+ },
121
+ };
122
+
123
+ agent.addPrinciple(myRule);
124
+ ```
125
+
126
+ ## Performance
127
+
128
+ - **O(N) event classification** — single-pass instead of 6 separate filters
129
+ - **Cached diagnosis** — no redundant principle checks within the same tick
130
+ - **Numerical stability** — clamped inputs to prevent NaN edge cases
131
+
132
+ Typical for 1,000 agents, 100 events/tick: ~60ms end-to-end.
133
+
134
+ ## License
135
+
136
+ MIT
package/dist/index.js CHANGED
@@ -2576,6 +2576,10 @@ var ALL_PRINCIPLES = [
2576
2576
  var Simulator = class {
2577
2577
  constructor() {
2578
2578
  this.diagnoser = new Diagnoser(ALL_PRINCIPLES);
2579
+ // Cache beforeViolations for the *current* tick only (one entry max).
2580
+ // Using a Map here is intentional but the cache must be bounded — we only
2581
+ // care about the tick that is currently being evaluated, so we evict any
2582
+ // entries whose key differs from the incoming tick.
2579
2583
  this.beforeViolationsCache = /* @__PURE__ */ new Map();
2580
2584
  }
2581
2585
  /**
@@ -2600,6 +2604,9 @@ var Simulator = class {
2600
2604
  const mean = this.averageMetrics(outcomes);
2601
2605
  const netImprovement = this.checkImprovement(currentMetrics, p50, action);
2602
2606
  const tick = currentMetrics.tick;
2607
+ if (this.beforeViolationsCache.size > 0 && !this.beforeViolationsCache.has(tick)) {
2608
+ this.beforeViolationsCache.clear();
2609
+ }
2603
2610
  let beforeViolations = this.beforeViolationsCache.get(tick);
2604
2611
  if (!beforeViolations) {
2605
2612
  beforeViolations = new Set(