I need to understand the guardrail HTTP handlers and the DynamicGuardrailManager in the KNIRVSERVER codebase. Please examine:
/home/gperry/Documents/GitHub/KNIRV/KNIRV_NETWORK/packages/KNIRVSERVER/backend/internal/web/guardrail_handlers.go - the ValidateResourceUsage handler and how violations are recorded
The DynamicGuardrailManager from the guardrails package (search for it in backend/internal/services/guardrails/ or similar)
The GuardrailViolation struct
Return the exact code for:
ValidateResourceUsage handler
RecordViolation method
DynamicGuardrailManager struct
GuardrailViolation struct
Any interfaces that DynamicGuardrailManager implements
Be thorough - return full file paths and line numbers.
Assistant
I'll start by searching for the relevant files and structures simultaneously.
Assistant
Now let me read the guardrail manager file for the structs and methods:
Assistant
Now let me also check if DynamicGuardrailManager explicitly satisfies any defined interfaces in the codebase:
Assistant
Let me also check for any GuardrailManager interface that DynamicGuardrailManager might be satisfying:
Assistant
Now let me check the cognitive engine guardrail file and wasm guardrail middleware for additional interfaces:
Assistant
Here is a thorough breakdown of all the requested code, with exact file paths and line numbers.
3. Interfaces that DynamicGuardrailManager satisfies
DynamicGuardrailManager does not explicitly implement any named interface that is defined in the codebase with the phrase type ... interface and DynamicGuardrailManager as a receiver. However, it implicitly satisfies several interface contracts used by other packages:
3a. HasherValidator interface (defined in the same file, lines 39–42)
DynamicGuardrailManagerconsumes this interface (stored as hasherValidator field), rather than implementing it. The manager delegates to it via ValidateWithHasher (line 109).
3b. The anonymous eventBroadcaster interface (lines 79–82, shown above)
DynamicGuardrailManager stores this as a field; the setter is SetEventBroadcaster (lines 98–103).
3c. Usage as a dependency in other structs
DynamicGuardrailManager is consumed by pointer in three other packages, each treating it as a concrete type, not an interface:
GuardrailHandlers in guardrail_handlers.go line 19:
HasherIntegration in hasher_integration.go line 22:
go
guardrailMgr *guardrails.DynamicGuardrailManager
PolicyEngine in guardrail_manager.go line 533:
go
guardrailManager *DynamicGuardrailManager
3d. The only interface that DynamicGuardrailManager is used as (not shown above)
In hasher_integration.go (line 95), HasherIntegration defines an OnGuardrailViolation method that accepts a *guardrails.GuardrailViolation — but this does not define an interface. The DynamicGuardrailManager is used directly here.
func (gm *DynamicGuardrailManager) ValidateResourceUsage(nodeID string, metrics map[string]interface{}) (*GuardrailViolation, bool) {
gm.mu.RLock()
defer gm.mu.RUnlock()
violations := make([]*GuardrailViolation, 0)
for guardrailType, config := range gm.configurations {
if !config.Enabled {
continue
}
if !gm.isNodeScope(guardrailType, nodeID) {
continue
}
if gm.isInCooldown(guardrailType) {
continue
}
var value float64
switch config.Type {
case GuardrailTypeMemory:
if mem, ok := metrics["memory_usage"].(float64); ok {
value = mem
}
case GuardrailTypeCPU:
if cpu, ok := metrics["cpu_usage"].(float64); ok {
value = cpu
}
case GuardrailTypeNetwork:
if net, ok := metrics["network_bandwidth"].(float64); ok {
value = net
}
}
if value > config.MaxValue && config.MaxValue > 0 {
violation := &GuardrailViolation{
ID: uuid.New().String(),
NodeID: nodeID,
GuardrailType: config.Type,
ViolationType: "resource_exceeded",
Message: fmt.Sprintf("%s usage %.2f exceeds max %.2f", config.Type, value, config.MaxValue),
Severity: gm.determineSeverity(config.Type, value, config.MaxValue),
Context: metrics,
Timestamp: time.Now(),
}
violations = append(violations, violation)
gm.setCooldown(guardrailType, config.Cooldown)
}
}
if len(violations) > 0 {
highest := violations[0]
for _, v := range violations[1:] {
if gm.severityOrder(v.Severity) > gm.severityOrder(highest.Severity) {
highest = v
}
}
return highest, true
}
return nil, false
}
The method iterates over all configured guardrails, checks:
Is the guardrail enabled?
Does it scope to this nodeID?
Is it not in cooldown?
Does the incoming metric value exceed the configured MaxValue?
If violated, it creates a GuardrailViolation, applies a cooldown, and returns the highest-severity violation if multiple guardrails tripped simultaneously.