@defai.digital/iterate-domain 13.0.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/LICENSE +214 -0
- package/dist/budget.d.ts +48 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +139 -0
- package/dist/budget.js.map +1 -0
- package/dist/controller.d.ts +44 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +226 -0
- package/dist/controller.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/safety.d.ts +47 -0
- package/dist/safety.d.ts.map +1 -0
- package/dist/safety.js +171 -0
- package/dist/safety.js.map +1 -0
- package/dist/types.d.ts +71 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -0
- package/src/budget.ts +165 -0
- package/src/controller.ts +275 -0
- package/src/index.ts +54 -0
- package/src/safety.ts +198 -0
- package/src/types.ts +111 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Iterate Controller
|
|
3
|
+
*
|
|
4
|
+
* State machine for iterate mode - handles intent classification
|
|
5
|
+
* and determines actions (CONTINUE, PAUSE, STOP, RETRY).
|
|
6
|
+
*
|
|
7
|
+
* Invariants:
|
|
8
|
+
* - INV-ITR-003: Intent classification drives action decisions
|
|
9
|
+
*/
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
import { DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_TIME_MS, } from '@defai.digital/contracts';
|
|
12
|
+
import { BudgetTracker } from './budget.js';
|
|
13
|
+
import { SafetyGuard } from './safety.js';
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Auto-Response Templates
|
|
16
|
+
// ============================================================================
|
|
17
|
+
/**
|
|
18
|
+
* Auto-responses for CONTINUE action
|
|
19
|
+
*/
|
|
20
|
+
const AUTO_RESPONSES = {
|
|
21
|
+
continue: 'Continue.',
|
|
22
|
+
question: '', // Should not auto-respond
|
|
23
|
+
blocked: '', // Should not auto-respond
|
|
24
|
+
complete: '', // Should not auto-respond
|
|
25
|
+
error: '', // Should not auto-respond
|
|
26
|
+
};
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// Iterate Controller Implementation
|
|
29
|
+
// ============================================================================
|
|
30
|
+
/**
|
|
31
|
+
* Main controller for iterate mode
|
|
32
|
+
*/
|
|
33
|
+
export class IterateController {
|
|
34
|
+
budgetTracker;
|
|
35
|
+
safetyGuard;
|
|
36
|
+
constructor() {
|
|
37
|
+
this.budgetTracker = new BudgetTracker();
|
|
38
|
+
this.safetyGuard = new SafetyGuard();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Start a new iterate session
|
|
42
|
+
*/
|
|
43
|
+
start(request) {
|
|
44
|
+
const sessionId = request.sessionId ?? randomUUID();
|
|
45
|
+
const now = new Date().toISOString();
|
|
46
|
+
// Initialize budget tracker
|
|
47
|
+
this.budgetTracker = new BudgetTracker(request.budget);
|
|
48
|
+
this.budgetTracker.start();
|
|
49
|
+
// Initialize safety guard
|
|
50
|
+
if (request.safety) {
|
|
51
|
+
this.safetyGuard = new SafetyGuard(request.safety);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
sessionId,
|
|
55
|
+
taskId: randomUUID(),
|
|
56
|
+
budget: {
|
|
57
|
+
maxIterations: request.budget?.maxIterations ?? DEFAULT_MAX_ITERATIONS,
|
|
58
|
+
maxTimeMs: request.budget?.maxTimeMs ?? DEFAULT_MAX_TIME_MS,
|
|
59
|
+
maxTokens: request.budget?.maxTokens,
|
|
60
|
+
},
|
|
61
|
+
consumed: {
|
|
62
|
+
iterations: 0,
|
|
63
|
+
timeMs: 0,
|
|
64
|
+
tokens: 0,
|
|
65
|
+
},
|
|
66
|
+
iteration: 0,
|
|
67
|
+
startedAt: now,
|
|
68
|
+
lastActivityAt: now,
|
|
69
|
+
status: 'running',
|
|
70
|
+
consecutiveErrors: 0,
|
|
71
|
+
history: [],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Handle a response from the LLM
|
|
76
|
+
*/
|
|
77
|
+
handleResponse(state, intent, content) {
|
|
78
|
+
const now = new Date().toISOString();
|
|
79
|
+
// Record iteration in budget tracker
|
|
80
|
+
this.budgetTracker.recordIteration();
|
|
81
|
+
// Check budget first
|
|
82
|
+
const budgetStatus = this.budgetTracker.check();
|
|
83
|
+
if (budgetStatus.exceeded) {
|
|
84
|
+
return this.createResponse(state, intent, {
|
|
85
|
+
type: 'STOP',
|
|
86
|
+
reason: budgetStatus.reason ?? 'Budget exceeded',
|
|
87
|
+
requiresInput: false,
|
|
88
|
+
}, 'budget_exceeded', now, content);
|
|
89
|
+
}
|
|
90
|
+
// Check safety if content provided
|
|
91
|
+
if (content) {
|
|
92
|
+
const safetyResult = this.safetyGuard.checkContent(content);
|
|
93
|
+
if (!safetyResult.safe) {
|
|
94
|
+
return this.createResponse(state, intent, {
|
|
95
|
+
type: 'PAUSE',
|
|
96
|
+
reason: safetyResult.reason ?? 'Safety check failed',
|
|
97
|
+
requiresInput: true,
|
|
98
|
+
suggestedInput: 'Review the dangerous pattern and confirm to proceed.',
|
|
99
|
+
}, 'paused', now, content);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Check consecutive errors
|
|
103
|
+
if (intent === 'error') {
|
|
104
|
+
const newErrorCount = state.consecutiveErrors + 1;
|
|
105
|
+
const errorResult = this.safetyGuard.checkErrors(newErrorCount);
|
|
106
|
+
if (!errorResult.safe) {
|
|
107
|
+
return this.createResponse(state, intent, {
|
|
108
|
+
type: 'PAUSE',
|
|
109
|
+
reason: errorResult.reason ?? 'Too many consecutive errors',
|
|
110
|
+
requiresInput: true,
|
|
111
|
+
suggestedInput: 'Review the errors and decide how to proceed.',
|
|
112
|
+
}, 'paused', now, content, newErrorCount);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Map intent to action
|
|
116
|
+
const action = this.mapIntentToAction(intent);
|
|
117
|
+
// Determine new status
|
|
118
|
+
let newStatus = state.status;
|
|
119
|
+
if (action.type === 'STOP') {
|
|
120
|
+
newStatus = intent === 'complete' ? 'completed' : 'failed';
|
|
121
|
+
}
|
|
122
|
+
else if (action.type === 'PAUSE') {
|
|
123
|
+
newStatus = 'paused';
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
newStatus = 'running';
|
|
127
|
+
}
|
|
128
|
+
// Reset error count on non-error intent
|
|
129
|
+
const consecutiveErrors = intent === 'error' ? state.consecutiveErrors + 1 : 0;
|
|
130
|
+
return this.createResponse(state, intent, action, newStatus, now, content, consecutiveErrors);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get auto-response for CONTINUE action
|
|
134
|
+
*/
|
|
135
|
+
getAutoResponse(intent) {
|
|
136
|
+
return AUTO_RESPONSES[intent] ?? 'Continue.';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Map intent to action
|
|
140
|
+
*/
|
|
141
|
+
mapIntentToAction(intent) {
|
|
142
|
+
switch (intent) {
|
|
143
|
+
case 'continue':
|
|
144
|
+
return {
|
|
145
|
+
type: 'CONTINUE',
|
|
146
|
+
reason: 'Task in progress',
|
|
147
|
+
requiresInput: false,
|
|
148
|
+
};
|
|
149
|
+
case 'question':
|
|
150
|
+
return {
|
|
151
|
+
type: 'PAUSE',
|
|
152
|
+
reason: 'User decision needed',
|
|
153
|
+
requiresInput: true,
|
|
154
|
+
suggestedInput: 'Please provide your decision.',
|
|
155
|
+
};
|
|
156
|
+
case 'blocked':
|
|
157
|
+
return {
|
|
158
|
+
type: 'PAUSE',
|
|
159
|
+
reason: 'External input needed',
|
|
160
|
+
requiresInput: true,
|
|
161
|
+
suggestedInput: 'Please provide the required input.',
|
|
162
|
+
};
|
|
163
|
+
case 'complete':
|
|
164
|
+
return {
|
|
165
|
+
type: 'STOP',
|
|
166
|
+
reason: 'Task completed successfully',
|
|
167
|
+
requiresInput: false,
|
|
168
|
+
};
|
|
169
|
+
case 'error':
|
|
170
|
+
return {
|
|
171
|
+
type: 'PAUSE',
|
|
172
|
+
reason: 'Error occurred',
|
|
173
|
+
requiresInput: true,
|
|
174
|
+
suggestedInput: 'Please review the error and decide how to proceed.',
|
|
175
|
+
};
|
|
176
|
+
default:
|
|
177
|
+
// Unknown intent - pause for safety
|
|
178
|
+
return {
|
|
179
|
+
type: 'PAUSE',
|
|
180
|
+
reason: 'Unknown intent - pausing for safety',
|
|
181
|
+
requiresInput: true,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Create response with updated state
|
|
187
|
+
*/
|
|
188
|
+
createResponse(state, intent, action, newStatus, now, content, consecutiveErrors) {
|
|
189
|
+
const consumed = this.budgetTracker.getConsumed();
|
|
190
|
+
const newState = {
|
|
191
|
+
...state,
|
|
192
|
+
iteration: state.iteration + 1,
|
|
193
|
+
consumed,
|
|
194
|
+
lastActivityAt: now,
|
|
195
|
+
status: newStatus,
|
|
196
|
+
lastIntent: intent,
|
|
197
|
+
lastAction: action,
|
|
198
|
+
consecutiveErrors: consecutiveErrors ?? (intent === 'error' ? state.consecutiveErrors + 1 : 0),
|
|
199
|
+
history: [
|
|
200
|
+
...(state.history ?? []),
|
|
201
|
+
{
|
|
202
|
+
iteration: state.iteration + 1,
|
|
203
|
+
intent,
|
|
204
|
+
action: action.type,
|
|
205
|
+
timestamp: now,
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
};
|
|
209
|
+
return {
|
|
210
|
+
action,
|
|
211
|
+
newState,
|
|
212
|
+
content,
|
|
213
|
+
autoResponse: action.type === 'CONTINUE' ? this.getAutoResponse(intent) : undefined,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// ============================================================================
|
|
218
|
+
// Factory Function
|
|
219
|
+
// ============================================================================
|
|
220
|
+
/**
|
|
221
|
+
* Creates an iterate controller
|
|
222
|
+
*/
|
|
223
|
+
export function createIterateController() {
|
|
224
|
+
return new IterateController();
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAML,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E;;GAEG;AACH,MAAM,cAAc,GAAkC;IACpD,QAAQ,EAAE,WAAW;IACrB,QAAQ,EAAE,EAAE,EAAE,0BAA0B;IACxC,OAAO,EAAE,EAAE,EAAE,0BAA0B;IACvC,QAAQ,EAAE,EAAE,EAAE,0BAA0B;IACxC,KAAK,EAAE,EAAE,EAAE,0BAA0B;CACtC,CAAC;AAEF,+EAA+E;AAC/E,oCAAoC;AACpC,+EAA+E;AAE/E;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACpB,aAAa,CAAgB;IAC7B,WAAW,CAAc;IAEjC;QACE,IAAI,CAAC,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC;QACzC,IAAI,CAAC,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAA4B;QAChC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,UAAU,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAErC,4BAA4B;QAC5B,IAAI,CAAC,aAAa,GAAG,I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AC,CAAC,CAAC,SAAS;SACpF,CAAC;IACJ,CAAC;CACF;AAED,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO,IAAI,iBAAiB,EAAE,CAAC;AACjC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @defai.digital/iterate-domain
|
|
3
|
+
*
|
|
4
|
+
* Iterate mode for AutomatosX - autonomous execution with
|
|
5
|
+
* structured intent classification and safety controls.
|
|
6
|
+
*
|
|
7
|
+
* Key concepts:
|
|
8
|
+
* - Intent: What the LLM is communicating (continue, question, blocked, complete, error)
|
|
9
|
+
* - Action: What to do next (CONTINUE, PAUSE, STOP, RETRY)
|
|
10
|
+
* - Budget: Resource limits (iterations, time, tokens)
|
|
11
|
+
* - Safety: Pattern detection and error limits
|
|
12
|
+
*/
|
|
13
|
+
export type { IterateIntent, IterateAction, IterateBudget, BudgetConsumed, IterateBudgetStatus, IterateState, IterateSafetyConfig, SafetyCheckResult, IterateStartRequest, IterateHandleResponse, IBudgetTracker, ISafetyGuard, IIterateController, } from './types.js';
|
|
14
|
+
export { BudgetTracker, createBudgetTracker } from './budget.js';
|
|
15
|
+
export { SafetyGuard, createSafetyGuard, isContentSafe } from './safety.js';
|
|
16
|
+
export { IterateController, createIterateController } from './controller.js';
|
|
17
|
+
export { DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_TIME_MS, DEFAULT_MAX_CONSECUTIVE_ERRORS, IterateIntentSchema, IterateActionTypeSchema, IterateBudgetSchema, IterateStateSchema, validateIterateIntent, safeValidateIterateIntent, validateIterateBudget, validateIterateState, IterateErrorCode, } from '@defai.digital/contracts';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,YAAY,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,cAAc,EACd,YAAY,EACZ,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAGjE,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5E,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAG7E,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,8BAA8B,EAC9B,mBAAmB,EACnB,uBAAuB,EACvB,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EACzB,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @defai.digital/iterate-domain
|
|
3
|
+
*
|
|
4
|
+
* Iterate mode for AutomatosX - autonomous execution with
|
|
5
|
+
* structured intent classification and safety controls.
|
|
6
|
+
*
|
|
7
|
+
* Key concepts:
|
|
8
|
+
* - Intent: What the LLM is communicating (continue, question, blocked, complete, error)
|
|
9
|
+
* - Action: What to do next (CONTINUE, PAUSE, STOP, RETRY)
|
|
10
|
+
* - Budget: Resource limits (iterations, time, tokens)
|
|
11
|
+
* - Safety: Pattern detection and error limits
|
|
12
|
+
*/
|
|
13
|
+
// Budget Tracker
|
|
14
|
+
export { BudgetTracker, createBudgetTracker } from './budget.js';
|
|
15
|
+
// Safety Guard
|
|
16
|
+
export { SafetyGuard, createSafetyGuard, isContentSafe } from './safety.js';
|
|
17
|
+
// Iterate Controller
|
|
18
|
+
export { IterateController, createIterateController } from './controller.js';
|
|
19
|
+
// Re-export contract constants
|
|
20
|
+
export { DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_TIME_MS, DEFAULT_MAX_CONSECUTIVE_ERRORS, IterateIntentSchema, IterateActionTypeSchema, IterateBudgetSchema, IterateStateSchema, validateIterateIntent, safeValidateIterateIntent, validateIterateBudget, validateIterateState, IterateErrorCode, } from '@defai.digital/contracts';
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAmBH,iBAAiB;AACjB,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEjE,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5E,qBAAqB;AACrB,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAE7E,+BAA+B;AAC/B,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,8BAA8B,EAC9B,mBAAmB,EACnB,uBAAuB,EACvB,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EACzB,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
package/dist/safety.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safety Guard
|
|
3
|
+
*
|
|
4
|
+
* Checks for dangerous patterns and enforces safety limits.
|
|
5
|
+
*
|
|
6
|
+
* Invariants:
|
|
7
|
+
* - INV-ITR-002: Safety guards must pause on dangerous patterns
|
|
8
|
+
*/
|
|
9
|
+
import { type IterateSafetyConfig, type SafetyCheckResult } from '@defai.digital/contracts';
|
|
10
|
+
import type { ISafetyGuard } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Checks content for dangerous patterns
|
|
13
|
+
*/
|
|
14
|
+
export declare class SafetyGuard implements ISafetyGuard {
|
|
15
|
+
private config;
|
|
16
|
+
private compiledPatterns;
|
|
17
|
+
constructor(config?: Partial<IterateSafetyConfig>);
|
|
18
|
+
/**
|
|
19
|
+
* Compile all patterns into RegExp objects
|
|
20
|
+
*/
|
|
21
|
+
private compilePatterns;
|
|
22
|
+
/**
|
|
23
|
+
* Check content for dangerous patterns
|
|
24
|
+
*/
|
|
25
|
+
checkContent(content: string): SafetyCheckResult;
|
|
26
|
+
/**
|
|
27
|
+
* Check if too many consecutive errors
|
|
28
|
+
*/
|
|
29
|
+
checkErrors(consecutiveErrors: number): SafetyCheckResult;
|
|
30
|
+
/**
|
|
31
|
+
* Get safety configuration
|
|
32
|
+
*/
|
|
33
|
+
getConfig(): IterateSafetyConfig;
|
|
34
|
+
/**
|
|
35
|
+
* Determine severity based on pattern
|
|
36
|
+
*/
|
|
37
|
+
private getSeverity;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a safety guard with optional configuration
|
|
41
|
+
*/
|
|
42
|
+
export declare function createSafetyGuard(config?: Partial<IterateSafetyConfig>): ISafetyGuard;
|
|
43
|
+
/**
|
|
44
|
+
* Quick check if content is safe
|
|
45
|
+
*/
|
|
46
|
+
export declare function isContentSafe(content: string): boolean;
|
|
47
|
+
//# sourceMappingURL=safety.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safety.d.ts","sourceRoot":"","sources":["../src/safety.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA2C/C;;GAEG;AACH,qBAAa,WAAY,YAAW,YAAY;IAC9C,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,gBAAgB,CAAW;gBAEvB,MAAM,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC;IAYjD;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,iBAAiB;IAsBhD;;OAEG;IACH,WAAW,CAAC,iBAAiB,EAAE,MAAM,GAAG,iBAAiB;IAYzD;;OAEG;IACH,SAAS,IAAI,mBAAmB;IAIhC;;OAEG;IACH,OAAO,CAAC,WAAW;CAgCpB;AAMD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GAAG,YAAY,CAErF;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAGtD"}
|
package/dist/safety.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safety Guard
|
|
3
|
+
*
|
|
4
|
+
* Checks for dangerous patterns and enforces safety limits.
|
|
5
|
+
*
|
|
6
|
+
* Invariants:
|
|
7
|
+
* - INV-ITR-002: Safety guards must pause on dangerous patterns
|
|
8
|
+
*/
|
|
9
|
+
import { DEFAULT_MAX_CONSECUTIVE_ERRORS, } from '@defai.digital/contracts';
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// Default Dangerous Patterns
|
|
12
|
+
// ============================================================================
|
|
13
|
+
/**
|
|
14
|
+
* Default patterns that trigger safety pause
|
|
15
|
+
*/
|
|
16
|
+
const DEFAULT_DANGEROUS_PATTERNS = [
|
|
17
|
+
// File system destruction
|
|
18
|
+
'rm\\s+-rf\\s+[/~]',
|
|
19
|
+
'rm\\s+-rf\\s+\\*',
|
|
20
|
+
'rm\\s+-rf\\s+\\.',
|
|
21
|
+
'rmdir\\s+/\\s',
|
|
22
|
+
// Database destruction
|
|
23
|
+
'DROP\\s+TABLE',
|
|
24
|
+
'DROP\\s+DATABASE',
|
|
25
|
+
'TRUNCATE\\s+TABLE',
|
|
26
|
+
'DELETE\\s+FROM\\s+\\w+\\s*;',
|
|
27
|
+
// Disk format
|
|
28
|
+
'mkfs\\.',
|
|
29
|
+
'format\\s+[cC]:',
|
|
30
|
+
'dd\\s+if=',
|
|
31
|
+
// Fork bomb
|
|
32
|
+
':\\(\\)\\{\\s*:|:&\\s*\\};:',
|
|
33
|
+
// Git force
|
|
34
|
+
'git\\s+push\\s+.*--force',
|
|
35
|
+
'git\\s+reset\\s+--hard\\s+origin',
|
|
36
|
+
// Shutdown/reboot
|
|
37
|
+
'shutdown',
|
|
38
|
+
'reboot',
|
|
39
|
+
'init\\s+0',
|
|
40
|
+
// Env/secrets exposure
|
|
41
|
+
'echo\\s+\\$[A-Z_]*KEY',
|
|
42
|
+
'echo\\s+\\$[A-Z_]*SECRET',
|
|
43
|
+
'echo\\s+\\$[A-Z_]*PASSWORD',
|
|
44
|
+
];
|
|
45
|
+
// ============================================================================
|
|
46
|
+
// Safety Guard Implementation
|
|
47
|
+
// ============================================================================
|
|
48
|
+
/**
|
|
49
|
+
* Checks content for dangerous patterns
|
|
50
|
+
*/
|
|
51
|
+
export class SafetyGuard {
|
|
52
|
+
config;
|
|
53
|
+
compiledPatterns;
|
|
54
|
+
constructor(config) {
|
|
55
|
+
this.config = {
|
|
56
|
+
maxConsecutiveErrors: config?.maxConsecutiveErrors ?? DEFAULT_MAX_CONSECUTIVE_ERRORS,
|
|
57
|
+
enableDangerousPatternDetection: config?.enableDangerousPatternDetection ?? true,
|
|
58
|
+
dangerousPatterns: config?.dangerousPatterns ?? DEFAULT_DANGEROUS_PATTERNS,
|
|
59
|
+
customDangerousPatterns: config?.customDangerousPatterns,
|
|
60
|
+
};
|
|
61
|
+
// Compile patterns for performance
|
|
62
|
+
this.compiledPatterns = this.compilePatterns();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compile all patterns into RegExp objects
|
|
66
|
+
*/
|
|
67
|
+
compilePatterns() {
|
|
68
|
+
const patterns = [
|
|
69
|
+
...this.config.dangerousPatterns,
|
|
70
|
+
...(this.config.customDangerousPatterns ?? []),
|
|
71
|
+
];
|
|
72
|
+
return patterns.map((pattern) => {
|
|
73
|
+
try {
|
|
74
|
+
return new RegExp(pattern, 'i');
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Invalid pattern - skip it
|
|
78
|
+
console.warn(`Invalid dangerous pattern: ${pattern}`);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}).filter((p) => p !== null);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Check content for dangerous patterns
|
|
85
|
+
*/
|
|
86
|
+
checkContent(content) {
|
|
87
|
+
if (!this.config.enableDangerousPatternDetection) {
|
|
88
|
+
return { safe: true };
|
|
89
|
+
}
|
|
90
|
+
for (const pattern of this.compiledPatterns) {
|
|
91
|
+
if (pattern.test(content)) {
|
|
92
|
+
// Determine severity based on pattern
|
|
93
|
+
const severity = this.getSeverity(pattern.source);
|
|
94
|
+
return {
|
|
95
|
+
safe: false,
|
|
96
|
+
reason: `Dangerous pattern detected: ${pattern.source}`,
|
|
97
|
+
matchedPattern: pattern.source,
|
|
98
|
+
severity,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { safe: true };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Check if too many consecutive errors
|
|
106
|
+
*/
|
|
107
|
+
checkErrors(consecutiveErrors) {
|
|
108
|
+
if (consecutiveErrors >= this.config.maxConsecutiveErrors) {
|
|
109
|
+
return {
|
|
110
|
+
safe: false,
|
|
111
|
+
reason: `Too many consecutive errors (${consecutiveErrors}/${this.config.maxConsecutiveErrors})`,
|
|
112
|
+
severity: 'warning',
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return { safe: true };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get safety configuration
|
|
119
|
+
*/
|
|
120
|
+
getConfig() {
|
|
121
|
+
return { ...this.config };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Determine severity based on pattern
|
|
125
|
+
*/
|
|
126
|
+
getSeverity(pattern) {
|
|
127
|
+
// Critical patterns - immediate system damage
|
|
128
|
+
const criticalPatterns = [
|
|
129
|
+
'rm\\s+-rf\\s+[/~]',
|
|
130
|
+
'mkfs\\.',
|
|
131
|
+
'dd\\s+if=',
|
|
132
|
+
'DROP\\s+DATABASE',
|
|
133
|
+
':\\(\\)\\{',
|
|
134
|
+
];
|
|
135
|
+
for (const critical of criticalPatterns) {
|
|
136
|
+
if (pattern.includes(critical) || pattern === critical) {
|
|
137
|
+
return 'critical';
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// Danger patterns - significant data loss
|
|
141
|
+
const dangerPatterns = [
|
|
142
|
+
'DROP\\s+TABLE',
|
|
143
|
+
'TRUNCATE',
|
|
144
|
+
'DELETE\\s+FROM',
|
|
145
|
+
'git\\s+push.*--force',
|
|
146
|
+
];
|
|
147
|
+
for (const danger of dangerPatterns) {
|
|
148
|
+
if (pattern.includes(danger) || pattern === danger) {
|
|
149
|
+
return 'danger';
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return 'warning';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// ============================================================================
|
|
156
|
+
// Factory Function
|
|
157
|
+
// ============================================================================
|
|
158
|
+
/**
|
|
159
|
+
* Creates a safety guard with optional configuration
|
|
160
|
+
*/
|
|
161
|
+
export function createSafetyGuard(config) {
|
|
162
|
+
return new SafetyGuard(config);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Quick check if content is safe
|
|
166
|
+
*/
|
|
167
|
+
export function isContentSafe(content) {
|
|
168
|
+
const guard = new SafetyGuard();
|
|
169
|
+
return guard.checkContent(content).safe;
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=safety.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safety.js","sourceRo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
|
|
|
1
|
+
/**
|
|
2
|
+
* Iterate Domain Internal Types
|
|
3
|
+
*
|
|
4
|
+
* Internal types for the iterate mode implementation.
|
|
5
|
+
*/
|
|
6
|
+
import type { IterateIntent, IterateAction, IterateBudget, BudgetConsumed, IterateBudgetStatus, IterateState, IterateSafetyConfig, SafetyCheckResult, IterateStartRequest, IterateHandleResponse } from '@defai.digital/contracts';
|
|
7
|
+
export type { IterateIntent, IterateAction, IterateBudget, BudgetConsumed, IterateBudgetStatus, IterateState, IterateSafetyConfig, SafetyCheckResult, IterateStartRequest, IterateHandleResponse, };
|
|
8
|
+
/**
|
|
9
|
+
* Budget tracker interface
|
|
10
|
+
*/
|
|
11
|
+
export interface IBudgetTracker {
|
|
12
|
+
/**
|
|
13
|
+
* Start tracking budget
|
|
14
|
+
*/
|
|
15
|
+
start(): void;
|
|
16
|
+
/**
|
|
17
|
+
* Record an iteration
|
|
18
|
+
*/
|
|
19
|
+
recordIteration(tokens?: number): void;
|
|
20
|
+
/**
|
|
21
|
+
* Check budget status
|
|
22
|
+
*/
|
|
23
|
+
check(): IterateBudgetStatus;
|
|
24
|
+
/**
|
|
25
|
+
* Check if budget is exceeded
|
|
26
|
+
*/
|
|
27
|
+
isExceeded(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Get current consumption
|
|
30
|
+
*/
|
|
31
|
+
getConsumed(): BudgetConsumed;
|
|
32
|
+
/**
|
|
33
|
+
* Get budget limits
|
|
34
|
+
*/
|
|
35
|
+
getBudget(): IterateBudget;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Safety guard interface
|
|
39
|
+
*/
|
|
40
|
+
export interface ISafetyGuard {
|
|
41
|
+
/**
|
|
42
|
+
* Check content for dangerous patterns
|
|
43
|
+
*/
|
|
44
|
+
checkContent(content: string): SafetyCheckResult;
|
|
45
|
+
/**
|
|
46
|
+
* Check if too many consecutive errors
|
|
47
|
+
*/
|
|
48
|
+
checkErrors(consecutiveErrors: number): SafetyCheckResult;
|
|
49
|
+
/**
|
|
50
|
+
* Get safety configuration
|
|
51
|
+
*/
|
|
52
|
+
getConfig(): IterateSafetyConfig;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Iterate controller interface
|
|
56
|
+
*/
|
|
57
|
+
export interface IIterateController {
|
|
58
|
+
/**
|
|
59
|
+
* Start a new iterate session
|
|
60
|
+
*/
|
|
61
|
+
start(request: IterateStartRequest): IterateState;
|
|
62
|
+
/**
|
|
63
|
+
* Handle a response from the LLM
|
|
64
|
+
*/
|
|
65
|
+
handleResponse(state: IterateState, intent: IterateIntent, content?: string): IterateHandleResponse;
|
|
66
|
+
/**
|
|
67
|
+
* Get auto-response for CONTINUE action
|
|
68
|
+
*/
|
|
69
|
+
getAutoResponse(intent: IterateIntent): string;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,0BAA0B,CAAC;AAGlC,YAAY,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,GACtB,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;OAEG;IACH,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAEvC;;OAEG;IACH,KAAK,IAAI,mBAAmB,CAAC;IAE7B;;OAEG;IACH,UAAU,IAAI,OAAO,CAAC;IAEtB;;OAEG;IACH,WAAW,IAAI,cAAc,CAAC;IAE9B;;OAEG;IACH,SAAS,IAAI,aAAa,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,iBAAiB,CAAC;IAEjD;;OAEG;IACH,WAAW,CAAC,iBAAiB,EAAE,MAAM,GAAG,iBAAiB,CAAC;IAE1D;;OAEG;IACH,SAAS,IAAI,mBAAmB,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAAC;IAElD;;OAEG;IACH,cAAc,CACZ,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,aAAa,EACrB,OAAO,CAAC,EAAE,MAAM,GACf,qBAAqB,CAAC;IAEzB;;OAEG;IACH,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAAC;CAChD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@defai.digital/iterate-domain",
|
|
3
|
+
"version": "13.0.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Iterate mode for AutomatosX - autonomous execution with structured intent and safety controls",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "DEFAI Private Limited",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/defai-digital/automatosx.git",
|
|
11
|
+
"directory": "packages/core/iterate-domain"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/defai-digital/automatosx#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/defai-digital/automatosx/issues"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20.0.0"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@defai.digital/contracts": "13.0.3"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"typescript": "^5.7.2"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"zod": "^3.23.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc --build",
|
|
46
|
+
"clean": "rm -rf dist"
|
|
47
|
+
}
|
|
48
|
+
}
|