safeloop 0.1.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.
@@ -0,0 +1,240 @@
1
+ export interface BreakerConfig {
2
+ maxRetries?: number;
3
+ maxRepeatedErrors?: number;
4
+ tokenBudget?: {
5
+ perStep?: number;
6
+ perTask?: number;
7
+ };
8
+ scopeFreeze?: boolean;
9
+ }
10
+ export interface BreakerResult {
11
+ success: boolean;
12
+ stoppedBy: string;
13
+ attempts: number;
14
+ tokenEstimate: number;
15
+ lastError: string | null;
16
+ escalationMessage: string | null;
17
+ auditEntries: AuditEntry[];
18
+ data?: unknown;
19
+ }
20
+ export interface AuditEntry {
21
+ timestamp: number;
22
+ type: 'attempt' | 'retry' | 'failure' | 'budget_check' | 'breaker_trip' | 'kill_switch' | 'escalation' | 'scope_denied' | 'scope_proposed' | 'token_usage';
23
+ message: string;
24
+ metadata?: Record<string, unknown>;
25
+ }
26
+ export interface BreakerStatus {
27
+ isTripped: boolean;
28
+ isKilled: boolean;
29
+ attempts: number;
30
+ tripReason: string | null;
31
+ }
32
+ export interface BreakerContext {
33
+ attempt: number;
34
+ tokenUsed: number;
35
+ signal: AbortSignal;
36
+ log: (entry: {
37
+ type: AuditEntry['type'];
38
+ message: string;
39
+ metadata?: Record<string, unknown>;
40
+ }) => void;
41
+ recordTokenUsage: (cost: number) => void;
42
+ proposeScopeChange: (description: string, newGoals: string[]) => boolean;
43
+ }
44
+ export interface Breaker {
45
+ run<T>(taskFn: (ctx: BreakerContext) => Promise<T>): Promise<BreakerResult>;
46
+ trip(reason: string): void;
47
+ reset(): void;
48
+ status(): BreakerStatus;
49
+ log(): AuditEntry[];
50
+ }
51
+ export type AgentRunLedgerCloseStatus = 'completed' | 'failed' | 'blocked' | 'escalated';
52
+ export type AgentRunLedgerValidationStatus = 'passed' | 'failed' | 'skipped';
53
+ export type AgentRunLedgerApprovalDecision = 'approved' | 'rejected' | 'needs_changes';
54
+ export interface AgentRunLedgerMetadata {
55
+ runId?: string;
56
+ agent: string;
57
+ executor: string;
58
+ repo: string;
59
+ task?: string;
60
+ allowedFiles?: string[];
61
+ startedAt?: string;
62
+ }
63
+ export interface AgentRunLedgerCommandResult {
64
+ exitCode?: number;
65
+ summary?: string;
66
+ }
67
+ export interface AgentRunLedgerScopeCheck {
68
+ ok: boolean;
69
+ allowed?: string[] | boolean;
70
+ requiresApproval?: boolean;
71
+ reasons?: string[];
72
+ violations?: string[];
73
+ message?: string;
74
+ oversightMode?: OversightMode;
75
+ risk?: PolicyRisk;
76
+ }
77
+ export interface AgentRunLedgerScopeCheckInput {
78
+ ok?: boolean;
79
+ allowed?: string[] | boolean;
80
+ requiresApproval?: boolean;
81
+ reasons?: string[];
82
+ violations?: string[];
83
+ message?: string;
84
+ oversightMode?: OversightMode;
85
+ risk?: PolicyRisk;
86
+ }
87
+ export interface AgentRunLedgerApproval {
88
+ approver: string;
89
+ decision: AgentRunLedgerApprovalDecision;
90
+ note?: string;
91
+ }
92
+ export interface AgentRunLedgerValidation {
93
+ name: string;
94
+ status: AgentRunLedgerValidationStatus;
95
+ details?: string;
96
+ }
97
+ export interface AgentRunLedgerCommand {
98
+ command: string;
99
+ result?: AgentRunLedgerCommandResult;
100
+ }
101
+ export interface AgentRunLedgerPromptEvent {
102
+ timestamp: number;
103
+ type: 'prompt';
104
+ data: {
105
+ prompt: string;
106
+ };
107
+ }
108
+ export interface AgentRunLedgerCommandEvent {
109
+ timestamp: number;
110
+ type: 'command';
111
+ data: {
112
+ command: string;
113
+ result?: AgentRunLedgerCommandResult;
114
+ };
115
+ }
116
+ export interface AgentRunLedgerChangedFilesEvent {
117
+ timestamp: number;
118
+ type: 'changed_files';
119
+ data: {
120
+ files: string[];
121
+ };
122
+ }
123
+ export interface AgentRunLedgerValidationEvent {
124
+ timestamp: number;
125
+ type: 'validation';
126
+ data: AgentRunLedgerValidation;
127
+ }
128
+ export interface AgentRunLedgerScopeCheckEvent {
129
+ timestamp: number;
130
+ type: 'scope_check';
131
+ data: AgentRunLedgerScopeCheck;
132
+ }
133
+ export interface AgentRunLedgerApprovalEvent {
134
+ timestamp: number;
135
+ type: 'approval';
136
+ data: AgentRunLedgerApproval;
137
+ }
138
+ export interface AgentRunLedgerCloseEvent {
139
+ timestamp: number;
140
+ type: 'close';
141
+ data: {
142
+ status: AgentRunLedgerCloseStatus;
143
+ };
144
+ }
145
+ export type AgentRunLedgerEvent = AgentRunLedgerPromptEvent | AgentRunLedgerCommandEvent | AgentRunLedgerChangedFilesEvent | AgentRunLedgerValidationEvent | AgentRunLedgerScopeCheckEvent | AgentRunLedgerApprovalEvent | AgentRunLedgerCloseEvent;
146
+ export interface AgentRunLedgerJSON {
147
+ metadata: AgentRunLedgerMetadata;
148
+ status: AgentRunLedgerCloseStatus | 'open';
149
+ closedAt: string | null;
150
+ prompts: string[];
151
+ commands: AgentRunLedgerCommand[];
152
+ changedFiles: string[][];
153
+ validations: AgentRunLedgerValidation[];
154
+ scopeChecks: AgentRunLedgerScopeCheck[];
155
+ approvals: AgentRunLedgerApproval[];
156
+ events: AgentRunLedgerEvent[];
157
+ }
158
+ export interface AgentRunLedger {
159
+ recordPrompt(prompt: string): void;
160
+ recordCommand(command: string, result?: AgentRunLedgerCommandResult): void;
161
+ recordChangedFiles(files: string[]): void;
162
+ recordValidation(name: string, status: AgentRunLedgerValidationStatus, details?: string): void;
163
+ recordScopeCheck(result: AgentRunLedgerScopeCheckInput): void;
164
+ recordApproval(approver: string, decision: AgentRunLedgerApprovalDecision, note?: string): void;
165
+ close(status: AgentRunLedgerCloseStatus): void;
166
+ toJSON(): AgentRunLedgerJSON;
167
+ toMarkdown(): string;
168
+ }
169
+ export type OversightMode = 'HITL' | 'HOTL' | 'HOOTL';
170
+ export type PolicyRisk = 'low' | 'medium' | 'high';
171
+ export interface PolicyGateConfig {
172
+ oversightMode: OversightMode;
173
+ allowedFiles?: string[];
174
+ allowedCommands?: string[];
175
+ blockedCommands?: string[];
176
+ requireApprovalFor?: string[];
177
+ maxRisk?: PolicyRisk;
178
+ }
179
+ export interface PolicyGateRequest {
180
+ task: string;
181
+ requestedFiles?: string[];
182
+ requestedCommands?: string[];
183
+ risk?: PolicyRisk;
184
+ hasHumanApproval?: boolean;
185
+ }
186
+ export interface PolicyGateDecision {
187
+ allowed: boolean;
188
+ requiresApproval: boolean;
189
+ reasons: string[];
190
+ violations: string[];
191
+ oversightMode: OversightMode;
192
+ risk: PolicyRisk;
193
+ message: string;
194
+ }
195
+ export interface PolicyGate {
196
+ evaluate(request: PolicyGateRequest): PolicyGateDecision;
197
+ }
198
+ export declare const DEFAULTS: {
199
+ readonly maxRetries: 3;
200
+ readonly maxRepeatedErrors: 2;
201
+ readonly tokenBudget: {
202
+ readonly perStep: number;
203
+ readonly perTask: number;
204
+ };
205
+ readonly scopeFreeze: true;
206
+ };
207
+ export declare const BREAKER_PRESETS: {
208
+ conservativeCodingAgent: {
209
+ maxRetries: number;
210
+ maxRepeatedErrors: number;
211
+ tokenBudget: {
212
+ perStep: number;
213
+ perTask: number;
214
+ };
215
+ scopeFreeze: true;
216
+ };
217
+ standardCodingAgent: {
218
+ maxRetries: number;
219
+ maxRepeatedErrors: number;
220
+ tokenBudget: {
221
+ perStep: number;
222
+ perTask: number;
223
+ };
224
+ scopeFreeze: true;
225
+ };
226
+ exploratoryResearchAgent: {
227
+ maxRetries: number;
228
+ maxRepeatedErrors: number;
229
+ tokenBudget: {
230
+ perStep: number;
231
+ perTask: number;
232
+ };
233
+ scopeFreeze: false;
234
+ };
235
+ };
236
+ export declare function createCodingAgentBreaker(config?: BreakerConfig): Breaker;
237
+ export declare function createPolicyGate(policy: PolicyGateConfig): PolicyGate;
238
+ export declare function createAgentRunLedger(initialMetadata: AgentRunLedgerMetadata): AgentRunLedger;
239
+ export declare function toMarkdownReport(result: BreakerResult): string;
240
+ export declare function createBreaker(config?: BreakerConfig): Breaker;