devfortress-sdk 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.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +7 -0
- data/LICENSE +63 -0
- data/README.md +121 -0
- data/Rakefile +11 -0
- data/lib/devfortress/agent_adapter.rb +185 -0
- data/lib/devfortress/agent_loop_detector.rb +167 -0
- data/lib/devfortress/agent_output_exfiltration_detector.rb +115 -0
- data/lib/devfortress/agent_security.rb +431 -0
- data/lib/devfortress/alias_verify.rb +87 -0
- data/lib/devfortress/api_key_aliasing.rb +240 -0
- data/lib/devfortress/bounded_map.rb +80 -0
- data/lib/devfortress/circuit_breaker.rb +149 -0
- data/lib/devfortress/classifier.rb +133 -0
- data/lib/devfortress/client.rb +380 -0
- data/lib/devfortress/client_ip.rb +28 -0
- data/lib/devfortress/clock_drift_detector.rb +71 -0
- data/lib/devfortress/credential_isolation.rb +340 -0
- data/lib/devfortress/credential_vault.rb +93 -0
- data/lib/devfortress/delivery_result.rb +9 -0
- data/lib/devfortress/graduated_rate_limiter.rb +270 -0
- data/lib/devfortress/internal_closed_loop_engine.rb +658 -0
- data/lib/devfortress/license_manager.rb +297 -0
- data/lib/devfortress/observer.rb +62 -0
- data/lib/devfortress/prompt_injection_filter.rb +73 -0
- data/lib/devfortress/response_webhook_client.rb +165 -0
- data/lib/devfortress/shadow_agent_registry.rb +123 -0
- data/lib/devfortress/signer.rb +134 -0
- data/lib/devfortress/telemetry.rb +407 -0
- data/lib/devfortress/tier_gate.rb +294 -0
- data/lib/devfortress/tool_call_sequence_validator.rb +126 -0
- data/lib/devfortress/transport.rb +31 -0
- data/lib/devfortress/unified_audit_trail.rb +213 -0
- data/lib/devfortress/version.rb +5 -0
- data/lib/devfortress/webhook_verification.rb +9 -0
- data/lib/devfortress/webhook_verifier.rb +93 -0
- data/lib/devfortress.rb +96 -0
- metadata +110 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DevFortress
|
|
4
|
+
# Graduated Rate Limiter (Invention A3).
|
|
5
|
+
#
|
|
6
|
+
# Mirrors packages/devfortress-sdk/src/graduated-rate-limiter.ts.
|
|
7
|
+
# Implements alias-scoped graduated rate limiting with multi-stage
|
|
8
|
+
# response escalation. Addresses GAP-A3-1 through GAP-A3-4:
|
|
9
|
+
#
|
|
10
|
+
# GAP-A3-1: Latency jitter (+/-20%) in soft-limit injection
|
|
11
|
+
# GAP-A3-2: Per-endpoint hierarchical rate limits (configurable)
|
|
12
|
+
# GAP-A3-3: Burst detection (1-second micro-window)
|
|
13
|
+
# GAP-A3-4: Escalating cooldowns (60s->120s->240s, revoke after 3)
|
|
14
|
+
#
|
|
15
|
+
# Four-stage graduated response:
|
|
16
|
+
#
|
|
17
|
+
# Stage 1: Observation (< 50% of limit)
|
|
18
|
+
# Stage 2: Warning (50%-75% of limit)
|
|
19
|
+
# Stage 3: Soft-limit with latency injection (75%-100% of limit)
|
|
20
|
+
# Stage 4: Hard-block with escalating cooldowns (>= 100% of limit)
|
|
21
|
+
# Stage 5: Alias revocation (>= 120% of limit or 3 consecutive blocks)
|
|
22
|
+
class GraduatedRateLimiter
|
|
23
|
+
RateLimitResult = Struct.new(
|
|
24
|
+
:stage, :current_rate, :limit, :ratio, :injected_latency_ms, :blocked,
|
|
25
|
+
:revoke, :cooldown_seconds, :remaining, :burst_detected,
|
|
26
|
+
keyword_init: true
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
Entry = Struct.new(:timestamps, :cooldown_until, :consecutive_blocks, :revoked, :burst_timestamps)
|
|
30
|
+
|
|
31
|
+
def initialize(default_limit: 100, window_seconds: 60, soft_limit_base_latency_ms: 2000,
|
|
32
|
+
max_entries: 50_000, endpoint_limits: {}, burst_limit: 20,
|
|
33
|
+
initial_cooldown_seconds: 60, max_consecutive_blocks: 3)
|
|
34
|
+
@default_limit = default_limit
|
|
35
|
+
@window_seconds = window_seconds
|
|
36
|
+
@soft_limit_base_latency_ms = soft_limit_base_latency_ms
|
|
37
|
+
@max_entries = max_entries
|
|
38
|
+
@endpoint_limits = endpoint_limits.dup
|
|
39
|
+
@burst_limit = burst_limit
|
|
40
|
+
@initial_cooldown_seconds = initial_cooldown_seconds
|
|
41
|
+
@max_consecutive_blocks = max_consecutive_blocks
|
|
42
|
+
|
|
43
|
+
@entries = {}
|
|
44
|
+
# Insertion order for oldest-first eviction, mirroring the
|
|
45
|
+
# iteration order of Node's Map (a key's position is set once, at
|
|
46
|
+
# first insertion, unaffected by later updates to the same key).
|
|
47
|
+
@order = []
|
|
48
|
+
@mutex = Mutex.new
|
|
49
|
+
@stop = false
|
|
50
|
+
@stop_cv = ConditionVariable.new
|
|
51
|
+
@cleanup_thread = Thread.new { periodic_cleanup }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Stops the periodic cleanup thread and clears tracked state.
|
|
55
|
+
def destroy
|
|
56
|
+
@mutex.synchronize do
|
|
57
|
+
@stop = true
|
|
58
|
+
@stop_cv.broadcast
|
|
59
|
+
end
|
|
60
|
+
@cleanup_thread&.join
|
|
61
|
+
@mutex.synchronize do
|
|
62
|
+
@entries = {}
|
|
63
|
+
@order = []
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Checks the rate limit for an alias (identified by key, typically
|
|
68
|
+
# an IP or session ID). Records the request and returns the
|
|
69
|
+
# graduated response. endpoint is optional and enables
|
|
70
|
+
# per-endpoint limits (GAP-A3-2).
|
|
71
|
+
def check(key, endpoint = nil)
|
|
72
|
+
@mutex.synchronize { check_locked(key, endpoint) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Manually revokes an alias (e.g. from an external security
|
|
76
|
+
# decision).
|
|
77
|
+
def revoke_alias(key)
|
|
78
|
+
@mutex.synchronize do
|
|
79
|
+
entry = @entries[key]
|
|
80
|
+
if entry.nil?
|
|
81
|
+
entry = Entry.new([], Time.at(0), 0, true, [])
|
|
82
|
+
@order << key
|
|
83
|
+
else
|
|
84
|
+
entry.revoked = true
|
|
85
|
+
end
|
|
86
|
+
@entries[key] = entry
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Resets an alias's rate limit state (e.g. after an admin
|
|
91
|
+
# override).
|
|
92
|
+
def reset_alias(key)
|
|
93
|
+
@mutex.synchronize { @entries.delete(key) }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Returns whether an alias has been revoked.
|
|
97
|
+
def revoked?(key)
|
|
98
|
+
@mutex.synchronize { @entries[key]&.revoked || false }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Sets per-endpoint rate limits (merged into any existing endpoint
|
|
102
|
+
# limits). GAP-A3-2.
|
|
103
|
+
def set_endpoint_limits(limits)
|
|
104
|
+
@mutex.synchronize { @endpoint_limits.merge!(limits) }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Returns the current number of tracked aliases.
|
|
108
|
+
def size
|
|
109
|
+
@mutex.synchronize { @entries.size }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def check_locked(key, endpoint)
|
|
115
|
+
now = Time.now
|
|
116
|
+
entry = @entries[key] || Entry.new([], Time.at(0), 0, false, [])
|
|
117
|
+
existed = @entries.key?(key)
|
|
118
|
+
|
|
119
|
+
if entry.revoked
|
|
120
|
+
return RateLimitResult.new(
|
|
121
|
+
stage: :revocation, current_rate: 0, limit: 0, ratio: Float::INFINITY,
|
|
122
|
+
injected_latency_ms: 0, blocked: true, revoke: false, cooldown_seconds: 0,
|
|
123
|
+
remaining: 0, burst_detected: false
|
|
124
|
+
)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
if entry.cooldown_until > now
|
|
128
|
+
cooldown_remaining = (entry.cooldown_until - now).ceil
|
|
129
|
+
return RateLimitResult.new(
|
|
130
|
+
stage: :hard_block, current_rate: entry.timestamps.size, limit: effective_limit(endpoint),
|
|
131
|
+
ratio: 1.0, injected_latency_ms: 0, blocked: true, revoke: false,
|
|
132
|
+
cooldown_seconds: cooldown_remaining, remaining: 0, burst_detected: false
|
|
133
|
+
)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
window_start = now - @window_seconds
|
|
137
|
+
entry.timestamps = entry.timestamps.select { |t| t > window_start }
|
|
138
|
+
|
|
139
|
+
# GAP-A3-3: clean burst micro-window (1 second).
|
|
140
|
+
burst_window_start = now - 1
|
|
141
|
+
entry.burst_timestamps = entry.burst_timestamps.select { |t| t > burst_window_start }
|
|
142
|
+
|
|
143
|
+
entry.timestamps << now
|
|
144
|
+
entry.burst_timestamps << now
|
|
145
|
+
|
|
146
|
+
if !existed && @entries.size >= @max_entries
|
|
147
|
+
evict_oldest
|
|
148
|
+
end
|
|
149
|
+
@order << key unless existed
|
|
150
|
+
@entries[key] = entry
|
|
151
|
+
|
|
152
|
+
limit = effective_limit(endpoint)
|
|
153
|
+
current_rate = entry.timestamps.size
|
|
154
|
+
ratio = current_rate.to_f / limit
|
|
155
|
+
|
|
156
|
+
burst_detected = entry.burst_timestamps.size > @burst_limit
|
|
157
|
+
|
|
158
|
+
if ratio >= 1.2 || entry.consecutive_blocks >= @max_consecutive_blocks
|
|
159
|
+
entry.revoked = true
|
|
160
|
+
return RateLimitResult.new(
|
|
161
|
+
stage: :revocation, current_rate: current_rate, limit: limit, ratio: ratio,
|
|
162
|
+
injected_latency_ms: 0, blocked: true, revoke: true, cooldown_seconds: 0,
|
|
163
|
+
remaining: 0, burst_detected: burst_detected
|
|
164
|
+
)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
if ratio >= 1.0 || burst_detected
|
|
168
|
+
entry.consecutive_blocks += 1
|
|
169
|
+
cooldown_multiplier = 2**(entry.consecutive_blocks - 1)
|
|
170
|
+
cooldown_seconds = @initial_cooldown_seconds * cooldown_multiplier
|
|
171
|
+
entry.cooldown_until = now + cooldown_seconds
|
|
172
|
+
|
|
173
|
+
return RateLimitResult.new(
|
|
174
|
+
stage: :hard_block, current_rate: current_rate, limit: limit, ratio: ratio,
|
|
175
|
+
injected_latency_ms: 0, blocked: true, revoke: false, cooldown_seconds: cooldown_seconds,
|
|
176
|
+
remaining: 0, burst_detected: burst_detected
|
|
177
|
+
)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
if ratio >= 0.75
|
|
181
|
+
entry.consecutive_blocks = 0
|
|
182
|
+
|
|
183
|
+
progress = (ratio - 0.75) / 0.25
|
|
184
|
+
base_latency = @soft_limit_base_latency_ms * (progress**2)
|
|
185
|
+
jitter = 0.8 + (0.4 * rand)
|
|
186
|
+
injected_latency_ms = (base_latency * jitter).round
|
|
187
|
+
|
|
188
|
+
return RateLimitResult.new(
|
|
189
|
+
stage: :soft_limit, current_rate: current_rate, limit: limit, ratio: ratio,
|
|
190
|
+
injected_latency_ms: injected_latency_ms, blocked: false, revoke: false,
|
|
191
|
+
cooldown_seconds: 0, remaining: [0, limit - current_rate].max, burst_detected: burst_detected
|
|
192
|
+
)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
if ratio >= 0.5
|
|
196
|
+
entry.consecutive_blocks = 0
|
|
197
|
+
return RateLimitResult.new(
|
|
198
|
+
stage: :warning, current_rate: current_rate, limit: limit, ratio: ratio,
|
|
199
|
+
injected_latency_ms: 0, blocked: false, revoke: false, cooldown_seconds: 0,
|
|
200
|
+
remaining: [0, limit - current_rate].max, burst_detected: false
|
|
201
|
+
)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
entry.consecutive_blocks = 0
|
|
205
|
+
RateLimitResult.new(
|
|
206
|
+
stage: :observation, current_rate: current_rate, limit: limit, ratio: ratio,
|
|
207
|
+
injected_latency_ms: 0, blocked: false, revoke: false, cooldown_seconds: 0,
|
|
208
|
+
remaining: [0, limit - current_rate].max, burst_detected: false
|
|
209
|
+
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def effective_limit(endpoint)
|
|
213
|
+
return @default_limit if endpoint.nil? || @endpoint_limits.empty?
|
|
214
|
+
|
|
215
|
+
return @endpoint_limits[endpoint] if @endpoint_limits.key?(endpoint)
|
|
216
|
+
|
|
217
|
+
@endpoint_limits.each do |pattern, limit|
|
|
218
|
+
if pattern.end_with?("*") && endpoint.start_with?(pattern[0..-2])
|
|
219
|
+
return limit
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
@default_limit
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def evict_oldest
|
|
227
|
+
while (oldest = @order.shift)
|
|
228
|
+
if @entries.key?(oldest)
|
|
229
|
+
@entries.delete(oldest)
|
|
230
|
+
return
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Mirrors the Node SDK's 30-second cleanup interval: removes
|
|
236
|
+
# entries with no recent activity that are not in cooldown and not
|
|
237
|
+
# revoked.
|
|
238
|
+
def periodic_cleanup
|
|
239
|
+
loop do
|
|
240
|
+
stop = @mutex.synchronize do
|
|
241
|
+
# Check @stop before waiting: if destroy already set it and
|
|
242
|
+
# broadcast before this thread reached here, waiting again
|
|
243
|
+
# would miss that wakeup and block for the full interval.
|
|
244
|
+
if @stop
|
|
245
|
+
true
|
|
246
|
+
else
|
|
247
|
+
@stop_cv.wait(@mutex, 30)
|
|
248
|
+
@stop
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
break if stop
|
|
252
|
+
|
|
253
|
+
cleanup
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def cleanup
|
|
258
|
+
@mutex.synchronize do
|
|
259
|
+
now = Time.now
|
|
260
|
+
window_start = now - @window_seconds
|
|
261
|
+
|
|
262
|
+
@entries.delete_if do |_key, entry|
|
|
263
|
+
has_recent_activity = entry.timestamps.any? { |t| t > window_start }
|
|
264
|
+
in_cooldown = entry.cooldown_until > now
|
|
265
|
+
!has_recent_activity && !in_cooldown && !entry.revoked
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|