@causyne/agent 1.20.20 → 1.20.22

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.
@@ -1,143 +0,0 @@
1
- /** Shared turn accounting for direct providers and agent peers. */
2
- export interface BudgetUsage {
3
- promptTokens: number;
4
- completionTokens: number;
5
- cachedPromptTokens?: number | undefined;
6
- uncachedPromptTokens?: number | undefined;
7
- measurement?: string | undefined;
8
- }
9
-
10
- export function noncachedTokens(usage: BudgetUsage): number {
11
- const finite = (value: number | undefined): number => Number.isFinite(value) ? Math.max(0, value ?? 0) : 0;
12
- const input = usage.uncachedPromptTokens !== undefined
13
- ? finite(usage.uncachedPromptTokens)
14
- // Cache reads larger than input prove these are separate peer counters.
15
- // Do not erase fresh input by subtracting that counter again.
16
- : finite(usage.cachedPromptTokens) > finite(usage.promptTokens)
17
- ? finite(usage.promptTokens)
18
- : Math.max(0, finite(usage.promptTokens) - finite(usage.cachedPromptTokens));
19
- return input + finite(usage.completionTokens);
20
- }
21
-
22
- export function actionLimitFor(archetype: string): number {
23
- if (archetype === 'desktop_personalization') return 12;
24
- if (archetype === 'media_3d_generation') return 25;
25
- if (archetype === 'game_development' || archetype === 'game') return 250;
26
- if (archetype === 'software_engineering' || archetype === 'backend_engineering') return 200;
27
- if (archetype === 'web_engineering') return 120;
28
- return 35;
29
- }
30
-
31
- export function tokenLimitFor(archetype: string): number {
32
- if (archetype === 'desktop_personalization') return 150_000;
33
- if (archetype === 'game_development' || archetype === 'game') return 3_000_000;
34
- if (archetype === 'software_engineering' || archetype === 'backend_engineering') return 2_500_000;
35
- if (archetype === 'media_3d_generation') return 400_000;
36
- if (archetype === 'web_engineering' || archetype === 'web_fullstack') return 500_000;
37
- return 100_000;
38
- }
39
-
40
- export function turnTokenLimitFor(archetype: string): number {
41
- if (archetype === 'game_development' || archetype === 'game') return 1_000_000;
42
- if (archetype === 'software_engineering' || archetype === 'backend_engineering') return 1_000_000;
43
- if (archetype === 'web_engineering' || archetype === 'web_fullstack') return 250_000;
44
- if (archetype === 'media_3d_generation') return 200_000;
45
- if (archetype === 'desktop_personalization') return 100_000;
46
- return 100_000;
47
- }
48
-
49
- export class EfficiencyCheckpoint extends Error {
50
- readonly code = 'CAUSYNE_EFFICIENCY_CHECKPOINT';
51
- readonly reason: 'tokens' | 'actions';
52
- readonly used: number;
53
- readonly limit: number;
54
- readonly scope: 'turn' | 'task';
55
-
56
- constructor(reason: 'tokens' | 'actions', used: number, limit: number, scope: 'turn' | 'task' = 'task') {
57
- const scopeLabel = scope === 'turn' ? 'single-turn' : 'task';
58
- const guidance = scope === 'turn'
59
- ? 'Auto-compacting context and continuing task execution.'
60
- : 'Session history is retained; continue from this checkpoint without repeating completed actions.';
61
- super(`Efficiency checkpoint: ${used.toLocaleString()} ${reason} reached the ${limit.toLocaleString()} ${scopeLabel} limit. ${guidance}`);
62
- this.name = 'EfficiencyCheckpoint';
63
- this.reason = reason;
64
- this.used = used;
65
- this.limit = limit;
66
- this.scope = scope;
67
- }
68
- }
69
-
70
- export class UnifiedBudgetEnforcer {
71
- maxTurnTokens: number;
72
- maxTurnActions: number;
73
- maxTaskTokens: number;
74
- private tokens = 0;
75
- private actions = 0;
76
- private readonly nativeIds = new Set<string>();
77
-
78
- constructor(maxTurnTokens = 100_000, maxTurnActions = 35, maxTaskTokens?: number) {
79
- if (!Number.isFinite(maxTurnTokens) || maxTurnTokens <= 0
80
- || !Number.isSafeInteger(maxTurnActions) || maxTurnActions <= 0) {
81
- throw new Error('Invalid efficiency limits');
82
- }
83
- this.maxTurnTokens = maxTurnTokens;
84
- this.maxTurnActions = maxTurnActions;
85
- this.maxTaskTokens = typeof maxTaskTokens === 'number' && Number.isFinite(maxTaskTokens) && maxTaskTokens > 0
86
- ? Math.max(maxTaskTokens, maxTurnTokens)
87
- : maxTurnTokens;
88
- }
89
-
90
- extendBudget(extraTokens: number, extraActions: number): void {
91
- this.maxTaskTokens = Math.max(this.maxTaskTokens, this.tokens + extraTokens);
92
- this.maxTurnTokens = Math.max(this.maxTurnTokens, extraTokens);
93
- this.maxTurnActions = Math.max(this.maxTurnActions, this.actions + extraActions);
94
- }
95
-
96
- extendForAutonomousMomentum(actionCount = 50, tokenCount = 500_000): void {
97
- this.maxTurnActions = Math.max(this.maxTurnActions, this.actions + actionCount);
98
- this.maxTurnTokens = Math.max(this.maxTurnTokens, tokenCount);
99
- this.maxTaskTokens = Math.max(this.maxTaskTokens, this.tokens + tokenCount);
100
- }
101
-
102
- recordUsage(usage: BudgetUsage): void {
103
- const fresh = noncachedTokens(usage);
104
- this.tokens += fresh;
105
- if (fresh >= this.maxTurnTokens) {
106
- throw new EfficiencyCheckpoint('tokens', fresh, this.maxTurnTokens, 'turn');
107
- }
108
- if (this.tokens >= this.maxTaskTokens) {
109
- throw new EfficiencyCheckpoint('tokens', this.tokens, this.maxTaskTokens, 'task');
110
- }
111
- }
112
-
113
- recordAction(nativeId?: string): void {
114
- if (nativeId && this.nativeIds.has(nativeId)) return;
115
- if (this.actions >= this.maxTurnActions) {
116
- throw new EfficiencyCheckpoint('actions', this.actions, this.maxTurnActions, 'task');
117
- }
118
- if (nativeId) this.nativeIds.add(nativeId);
119
- this.actions++;
120
- }
121
-
122
- getRemainingTokens(): number {
123
- return Math.max(0, this.maxTaskTokens - this.tokens);
124
- }
125
-
126
- getRemainingTurnTokens(): number {
127
- return this.maxTurnTokens;
128
- }
129
-
130
- getRemainingActions(): number {
131
- return Math.max(0, this.maxTurnActions - this.actions);
132
- }
133
-
134
- snapshot(): { noncachedTokens: number; actions: number; tokenLimit: number; taskTokenLimit: number; actionLimit: number } {
135
- return {
136
- noncachedTokens: this.tokens,
137
- actions: this.actions,
138
- tokenLimit: this.maxTurnTokens,
139
- taskTokenLimit: this.maxTaskTokens,
140
- actionLimit: this.maxTurnActions
141
- };
142
- }
143
- }