@f4bioo/berry-shield 2026.3.3-1

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.
Files changed (52) hide show
  1. package/CONTRIBUTING.md +32 -0
  2. package/LICENSE +201 -0
  3. package/README.md +251 -0
  4. package/SECURITY_AUDIT.md +96 -0
  5. package/docs/wiki/README.md +108 -0
  6. package/docs/wiki/decision/README.md +48 -0
  7. package/docs/wiki/decision/modes.md +176 -0
  8. package/docs/wiki/decision/patterns.md +137 -0
  9. package/docs/wiki/decision/posture.md +68 -0
  10. package/docs/wiki/deploy/README.md +51 -0
  11. package/docs/wiki/deploy/auditing.md +56 -0
  12. package/docs/wiki/deploy/build.md +92 -0
  13. package/docs/wiki/deploy/github-ci-cd.md +107 -0
  14. package/docs/wiki/deploy/installation.md +348 -0
  15. package/docs/wiki/engine/README.md +53 -0
  16. package/docs/wiki/engine/match-engine.md +91 -0
  17. package/docs/wiki/engine/performance.md +114 -0
  18. package/docs/wiki/engine/redaction.md +120 -0
  19. package/docs/wiki/layers/README.md +68 -0
  20. package/docs/wiki/layers/leaf.md +126 -0
  21. package/docs/wiki/layers/pulp.md +139 -0
  22. package/docs/wiki/layers/root.md +130 -0
  23. package/docs/wiki/layers/stem.md +139 -0
  24. package/docs/wiki/layers/thorn.md +139 -0
  25. package/docs/wiki/layers/vine.md +154 -0
  26. package/docs/wiki/operation/README.md +31 -0
  27. package/docs/wiki/operation/cli/README.md +122 -0
  28. package/docs/wiki/operation/cli/add.md +157 -0
  29. package/docs/wiki/operation/cli/help.md +83 -0
  30. package/docs/wiki/operation/cli/init.md +52 -0
  31. package/docs/wiki/operation/cli/list.md +78 -0
  32. package/docs/wiki/operation/cli/mode.md +93 -0
  33. package/docs/wiki/operation/cli/policy.md +202 -0
  34. package/docs/wiki/operation/cli/profile.md +98 -0
  35. package/docs/wiki/operation/cli/remove.md +96 -0
  36. package/docs/wiki/operation/cli/report.md +66 -0
  37. package/docs/wiki/operation/cli/reset.md +99 -0
  38. package/docs/wiki/operation/cli/rules.md +161 -0
  39. package/docs/wiki/operation/cli/status.md +103 -0
  40. package/docs/wiki/operation/cli/test.md +119 -0
  41. package/docs/wiki/operation/cli/toggle.md +90 -0
  42. package/docs/wiki/operation/cli/vine.md +193 -0
  43. package/docs/wiki/operation/web/README.md +27 -0
  44. package/docs/wiki/tutorials/README.md +40 -0
  45. package/docs/wiki/tutorials/audit-to-enforce-rollout.md +99 -0
  46. package/docs/wiki/tutorials/build-custom-rules.md +99 -0
  47. package/docs/wiki/tutorials/choose-profile.md +91 -0
  48. package/docs/wiki/tutorials/incident-triage-report.md +99 -0
  49. package/docs/wiki/tutorials/secure-session.md +115 -0
  50. package/docs/wiki/tutorials/tune-policy.md +111 -0
  51. package/openclaw.plugin.json +293 -0
  52. package/package.json +70 -0
@@ -0,0 +1,114 @@
1
+ ---
2
+ summary: "Engine reference for performance profile, optimization strategy, and runtime tradeoffs"
3
+ read_when:
4
+ - You are evaluating runtime cost of scanning and redaction
5
+ - You are tuning patterns for better latency and memory behavior
6
+ - You are validating behavior on constrained environments
7
+ title: "performance"
8
+ ---
9
+
10
+ # `Performance and optimization`
11
+
12
+ Berry Shield is designed for practical security execution under real runtime constraints.
13
+ The performance strategy prioritizes predictable behavior, bounded memory growth on common paths, and acceptable latency during content scanning.
14
+
15
+ ## Overview
16
+
17
+ Engine performance is shaped by:
18
+ - payload size and nesting depth
19
+ - pattern set breadth and regex complexity
20
+ - number of scan/interception hook invocations
21
+ - match frequency (more matches generally means more replacement work)
22
+
23
+ The current implementation favors change-only transformation and fast no-match returns where possible.
24
+
25
+ ## Optimization strategy
26
+
27
+ ### Lazy cloning
28
+
29
+ Deep-cloning every payload on every pass is expensive.
30
+ Berry runtime traversal follows a lazy approach:
31
+ - keep original references when no changes are required
32
+ - clone object branches only after actual transformation is needed
33
+
34
+ This reduces memory churn and lowers garbage-collection pressure on no-change paths.
35
+
36
+ ### Circular-reference guard
37
+
38
+ Recursive traversal uses seen-set protection for object graphs.
39
+ This avoids repeated traversal loops and helps keep runtime stable for cyclic payloads.
40
+
41
+ ### Fast-path behavior
42
+
43
+ Primitive/no-scan paths return quickly.
44
+ On string paths, original content can be preserved when no redaction match is found.
45
+
46
+ ### Pattern caching and reuse
47
+
48
+ Effective pattern sets are prepared and reused by category.
49
+ This avoids rebuilding full pattern arrays on every operation path.
50
+
51
+ ## Runtime flow (cost perspective)
52
+
53
+ ```mermaid
54
+ flowchart TD
55
+ A[Payload enters engine path] --> B{Fast path possible?}
56
+ B -->|yes| F[Return unchanged quickly]
57
+ B -->|no| T[Traverse and evaluate patterns]
58
+ T --> C{Any transformation needed?}
59
+ C -->|no| U[Reuse original references]
60
+ C -->|yes| L[Clone changed branches only]
61
+ L --> O[Return transformed payload]
62
+ U --> O
63
+ ```
64
+
65
+ ## LLM context efficiency impact
66
+
67
+ Redaction and cleanup can reduce repeated sensitive-noise in model-visible artifacts and persisted transcripts.
68
+ Operationally, this can improve context quality by:
69
+ - reducing repetitive secret/token fragments
70
+ - minimizing irrelevant high-entropy payload noise
71
+ - preserving clearer task-relevant content after sanitation
72
+
73
+ ## Constrained environment profile
74
+
75
+ Berry aims to stay usable on constrained devices and low-overhead deployments by combining:
76
+ - lazy clone semantics
77
+ - circular guard traversal
78
+ - practical pattern tuning expectations
79
+
80
+ Outcome depends on workload shape.
81
+ Large deeply nested payloads with broad aggressive patterns still increase cost and should be tuned intentionally.
82
+
83
+ ## Practical tuning guidance
84
+
85
+ 1. Prefer targeted custom patterns over broad catch-all expressions.
86
+ 2. Validate new patterns with CLI test flows before production use.
87
+ 3. Use audit cycles to measure match volume and false-positive pressure.
88
+ 4. Keep tool outputs scoped where possible to reduce unnecessary scan volume.
89
+
90
+ ## Limits and caveats
91
+
92
+ - Performance behavior remains workload-dependent and cannot be constant across all payload classes.
93
+ - Regex quality directly affects both latency and detection reliability.
94
+ - CLI safe-match timeout behavior applies to CLI matcher paths, not every runtime layer path.
95
+
96
+ ## Validation checklist
97
+
98
+ 1. Confirm no-match payloads preserve original references.
99
+ 2. Confirm changed payloads clone only modified branches.
100
+ 3. Confirm cyclic payloads are processed without recursion failure.
101
+ 4. Confirm catastrophic regex patterns are bounded in CLI safe matcher paths.
102
+
103
+ ## Related pages
104
+ - [engine index](README.md)
105
+ - [redaction](redaction.md)
106
+ - [match engine](match-engine.md)
107
+ - [decision posture](../decision/posture.md)
108
+
109
+ ---
110
+
111
+ ## Navigation
112
+ - [Back to Engine Index](README.md)
113
+ - [Back to Wiki Index](../README.md)
114
+
@@ -0,0 +1,120 @@
1
+ ---
2
+ summary: "Engine reference for redaction behavior, pattern provenance, and transformation flow"
3
+ read_when:
4
+ - You need to understand how sensitive content is detected and transformed
5
+ - You are validating output sanitation behavior in runtime layers
6
+ - You are reviewing pattern provenance and redaction behavior
7
+ title: "redaction"
8
+ ---
9
+
10
+ # `Redaction engine`
11
+
12
+ The redaction engine is the central transformation layer for sensitive-content handling in Berry Shield.
13
+ It is used by runtime layers that need to detect, classify, and sanitize secrets and PII in content payloads.
14
+
15
+ ## Overview
16
+
17
+ In runtime terms, the engine provides:
18
+ - text-level redaction for string payloads
19
+ - recursive traversal for arrays and nested objects
20
+ - key-based masking for sensitive field names
21
+ - match metadata for audit/reporting paths
22
+
23
+ This behavior feeds both observability (audit paths) and mitigation (enforce paths), depending on layer mode decisions.
24
+
25
+ ## Detection intelligence and pattern provenance
26
+
27
+ The detection model combines multiple sources:
28
+
29
+ - Static regex families for common credential and PII classes.
30
+ - Community-derived secret patterns from Gitleaks source configuration.
31
+ - Key-based masking for sensitive JSON/object field names (for example password/token-style keys).
32
+
33
+ ### Gitleaks provenance in Berry
34
+
35
+ Berry includes a generated pattern source derived from Gitleaks configuration and tracked in source with explicit provenance metadata.
36
+ This keeps pattern evolution practical while preserving local runtime control over placeholder behavior and layer-specific decisions.
37
+
38
+ Important nuance:
39
+ - Gitleaks-derived rules contribute to detection coverage.
40
+ - Final behavior (observe, redact, deny, allow) still depends on Berry layer and mode logic.
41
+
42
+ ## Unescape handling
43
+
44
+ Escaped payloads can hide sensitive tokens in operational logs and tool responses.
45
+ Before regex evaluation on string paths, engine logic applies a practical unescape pass for common escaped forms.
46
+
47
+ Why this matters:
48
+ - improves detection on JSON-escaped and partially encoded outputs
49
+ - reduces false negatives caused by escaped delimiters or unicode escape sequences
50
+
51
+ ## Runtime flow
52
+
53
+ ```mermaid
54
+ flowchart TD
55
+ A[Raw payload] --> B{Payload type}
56
+ B -->|string| S[Unescape + string redaction path]
57
+ B -->|object/array| W[Recursive walk path]
58
+ S --> R[Pattern replacement loop]
59
+ W --> J{JSON-like string value?}
60
+ J -->|yes| P[Parse and recurse]
61
+ J -->|no| K[Key-based and value recursion]
62
+ K --> L[Lazy clone changed branches]
63
+ R --> O[Redaction result]
64
+ P --> O
65
+ L --> O
66
+ ```
67
+
68
+ ## Core behavior details
69
+
70
+ ### String redaction path
71
+
72
+ - normalizes escaped content candidate
73
+ - evaluates active patterns in sequence
74
+ - replaces matched segments with pattern placeholders
75
+ - preserves original text when no match exists
76
+
77
+ ### Recursive walk path
78
+
79
+ - traverses arrays and objects deeply
80
+ - guards against circular references with seen-set tracking
81
+ - can parse JSON-like strings and recurse into parsed values
82
+ - returns redaction stats (count and matched type names)
83
+
84
+ ### Key-based masking path
85
+
86
+ For object keys recognized as sensitive:
87
+ - primitive values may be replaced with key-derived mask marker
88
+ - this complements regex detection where value format alone may be ambiguous
89
+
90
+ ## Where the redaction engine is used
91
+
92
+ - [Pulp layer](../layers/pulp.md): output-side sanitation and redaction outcomes
93
+ - [Leaf layer](../layers/leaf.md): inbound sensitivity signaling for audit visibility
94
+ - [Thorn layer](../layers/thorn.md): match-only helper path for interception checks
95
+
96
+ ## Limits and caveats
97
+
98
+ - Regex coverage is heuristic and pattern quality-dependent.
99
+ - JSON-string parsing applies only when payload is parseable as JSON shape.
100
+ - Unescape logic uses practical common forms, not universal decoding for all encodings.
101
+ - Large payload size and pattern count directly affect processing cost.
102
+
103
+ ## Validation checklist
104
+
105
+ 1. Verify known secret strings map to expected placeholders.
106
+ 2. Verify nested-object traversal redacts only affected branches.
107
+ 3. Verify circular structures are handled without traversal failures.
108
+ 4. Verify escaped-token samples are detected after normalization.
109
+
110
+ ## Related pages
111
+ - [engine index](README.md)
112
+ - [match engine](match-engine.md)
113
+ - [performance](performance.md)
114
+ - [decision patterns](../decision/patterns.md)
115
+
116
+ ---
117
+
118
+ ## Navigation
119
+ - [Back to Engine Index](README.md)
120
+ - [Back to Wiki Index](../README.md)
@@ -0,0 +1,68 @@
1
+ ---
2
+ summary: "Layer index for Berry Shield runtime architecture and cross-layer interaction map"
3
+ read_when:
4
+ - You need a quick map of all six layers
5
+ - You are tracing a user-input to output-security path
6
+ - You are onboarding to Berry Shield internals
7
+ title: "Layers Reference"
8
+ ---
9
+
10
+ # `Layers reference`
11
+
12
+ This page is the entry point for Berry Shield layer architecture.
13
+ It explains what each layer owns and how layers interact during runtime.
14
+
15
+ ## Layer map
16
+
17
+ - [root](root.md): policy injection strategy at turn start
18
+ - [leaf](leaf.md): incoming-message audit and sensitive-signal logging
19
+ - [stem](stem.md): gate tool for pre-operation allow/deny decisions
20
+ - [thorn](thorn.md): hook-level pre-tool-call interception and blocking
21
+ - [vine](vine.md): external-content trust guard for prompt-injection hardening
22
+ - [pulp](pulp.md): output scanning, sanitation, and policy-block hygiene
23
+
24
+ ## End-to-end interaction (single view)
25
+
26
+ ```mermaid
27
+ flowchart TD
28
+ U[User message] --> LEAF[Leaf: inbound audit signals]
29
+ U --> ROOT[Root: turn-start policy decision]
30
+ ROOT --> A[Agent reasoning]
31
+ A --> VINE[Vine: external-content trust guard]
32
+ A --> STEM[Stem: gate tool check]
33
+ A --> THORN[Thorn: before_tool_call interception]
34
+ VINE --> STEM
35
+ VINE --> THORN
36
+ STEM --> TOOLS[Tool execution path]
37
+ THORN --> TOOLS
38
+ TOOLS --> PULP[Pulp: output scan and sanitation]
39
+ A --> PULP
40
+ PULP --> OUT[Assistant response]
41
+ ```
42
+
43
+ ## Responsibility boundaries
44
+
45
+ - Root controls policy presence in context, not hard operation execution.
46
+ - Leaf provides observability on inbound content, not inbound blocking.
47
+ - Stem evaluates operation intent through a gate tool path.
48
+ - Thorn intercepts tool calls on hook path when host runtime supports invocation.
49
+ - Vine tracks external-content risk signals and adds trust-aware guardrails.
50
+ - Pulp sanitizes output paths and strips leaked policy snippets.
51
+
52
+ ## How to read this section
53
+
54
+ Use this index in two passes:
55
+ 1. Read each layer page for exact runtime behavior and limits.
56
+ 2. Return to this map to confirm where a failure or mismatch belongs.
57
+
58
+ ## Related pages
59
+ - [wiki index](../README.md)
60
+ - [decision modes](../decision/modes.md)
61
+ - [decision patterns](../decision/patterns.md)
62
+ - [decision posture](../decision/posture.md)
63
+
64
+ ---
65
+
66
+ ## Navigation
67
+ - [Back to Wiki Index](../README.md)
68
+ - [Back to Repository README](../../../README.md)
@@ -0,0 +1,126 @@
1
+ ---
2
+ summary: "Layer reference for Berry.Leaf (incoming message audit and sensitive-signal logging)"
3
+ read_when:
4
+ - You need to understand what Leaf actually does in runtime
5
+ - You are validating input-observability behavior
6
+ - You are debugging why incoming messages are logged but not blocked
7
+ title: "leaf"
8
+ ---
9
+
10
+ # `Berry.Leaf`
11
+
12
+ Berry.Leaf is the **incoming-message audit layer**.
13
+
14
+ Despite the name Leaf, this layer is not a file blocker.
15
+ Its runtime role is: observe inbound user text, detect sensitive signals, and emit audit logs.
16
+
17
+ ## What Leaf does
18
+
19
+ - Hooks into message_received.
20
+ - Scans inbound message text with redaction patterns (secrets + PII).
21
+ - Produces audit signals (containsSecrets, containsPII, sensitiveTypes).
22
+ - Writes structured debug logs and warning logs for sensitive hits.
23
+
24
+ In code terms, this is an observation pipeline, not an enforcement pipeline.
25
+
26
+ ## What Leaf does not do
27
+
28
+ - It does not block incoming user messages.
29
+ - It does not rewrite incoming user content before model processing.
30
+ - It does not decide tool allow/deny.
31
+ - It does not redact outbound messages.
32
+
33
+ Reason: `message_received` is used as an observe-only path in this plugin design.
34
+
35
+ ## Runtime flow
36
+
37
+ ```mermaid
38
+ flowchart TD
39
+ U[User Message] --> MR[Hook: message_received]
40
+ MR --> L[Berry.Leaf]
41
+ L --> S[Pattern Scan: secrets + PII]
42
+ S --> D{Sensitive match?}
43
+ D -->|No| LOG0[Debug audit log]
44
+ D -->|Yes| LOG1[Warn + structured audit log]
45
+ LOG0 --> NEXT[Continue runtime flow]
46
+ LOG1 --> NEXT
47
+ ```
48
+
49
+ ## Data extracted by Leaf
50
+
51
+ For each non-empty inbound message, Leaf derives:
52
+
53
+ - timestamp
54
+ - session identifier (when available in context)
55
+ - source/sender fields
56
+ - message length
57
+ - secret/PII presence flags
58
+ - sensitive type list matched by patterns
59
+
60
+ This gives operators evidence for risk monitoring without storing transformed user text as policy output.
61
+
62
+ ## How Leaf interacts with other layers
63
+
64
+ Leaf is one signal producer in a layered system.
65
+
66
+ ### With Root
67
+ - Root controls policy injection into agent context.
68
+ - Leaf does not influence Root decisions directly.
69
+ - Both can operate in the same turn, but on different concerns (guidance vs observability).
70
+
71
+ ### With Stem
72
+ - Stem enforces security gate outcomes (`berry_check`) on operations.
73
+ - Leaf can reveal that risky intent appeared in user input before operation attempts.
74
+ - Stem is enforcement; Leaf is telemetry.
75
+
76
+ ### With Thorn
77
+ - Thorn intercepts runtime tool calls (`before_tool_call`) when available.
78
+ - Leaf can show early input indicators that later map to Thorn block events.
79
+
80
+ ### With Pulp
81
+ - Pulp handles output-side redaction/hygiene.
82
+ - Leaf handles input-side detection/auditing.
83
+ - Together, they provide inbound and outbound visibility.
84
+
85
+ ## Operational value
86
+
87
+ Leaf is useful for:
88
+
89
+ - measuring sensitive-input frequency
90
+ - building audit trails for compliance/security review
91
+ - correlating "user asked for risky action" with later deny/redact events
92
+ - tuning detection rules with real conversational signals
93
+
94
+ ## Limits and caveats
95
+
96
+ - Leaf is only as strong as pattern coverage and matching quality.
97
+ - If context fields are missing from runtime, session-level correlation can degrade.
98
+ - Leaf should not be interpreted as a user-input firewall.
99
+ - Use Leaf together with Stem/Thorn/Pulp for active mitigation posture.
100
+
101
+ ## Validation checklist
102
+
103
+ Use this quick check during runtime validation:
104
+
105
+ 1. Send benign message and confirm debug audit entry appears.
106
+ 2. Send message containing test secret/PII marker and confirm warning audit entry appears.
107
+ 3. Confirm message still proceeds (Leaf observation behavior).
108
+ 4. Trigger a risky operation and correlate Leaf signal with Stem/Thorn outcomes.
109
+
110
+ ## See layers
111
+
112
+ - [root](root.md)
113
+ - [stem](stem.md)
114
+ - [thorn](thorn.md)
115
+ - [pulp](pulp.md)
116
+
117
+ ## Related pages
118
+ - [layers index](README.md)
119
+ - [decision modes](../decision/modes.md)
120
+ - [decision patterns](../decision/patterns.md)
121
+
122
+ ---
123
+
124
+ ## Navigation
125
+ - [Back to Layers Index](README.md)
126
+ - [Back to Wiki Index](../README.md)
@@ -0,0 +1,139 @@
1
+ ---
2
+ summary: "Layer reference for Berry.Pulp (output scanning, redaction paths, and policy-block hygiene)"
3
+ read_when:
4
+ - You need to understand output-side sanitation behavior
5
+ - You are validating audit vs enforce output outcomes
6
+ - You are debugging redaction events or leaked policy snippets
7
+ title: "pulp"
8
+ ---
9
+
10
+ # `Berry.Pulp`
11
+
12
+ Berry.Pulp is the **output scanning and sanitation layer**.
13
+
14
+ It inspects tool results and outgoing messages, applies sensitive-pattern scanning, and handles mode-specific redaction behavior.
15
+
16
+ ## What Pulp does
17
+
18
+ - Hooks into tool_result_persist for tool output persistence path.
19
+ - Hooks into message_sending for outgoing assistant message path.
20
+ - Runs pattern-based scan over content payloads.
21
+ - Emits structured redaction events in both audit and enforce paths.
22
+ - In enforce mode, returns sanitized content when redaction is required.
23
+ - Applies policy-block hygiene by stripping leaked policy snippets from outgoing messages.
24
+
25
+ ## What Pulp does not do
26
+
27
+ - It does not block tool execution requests.
28
+ - It does not classify command/file operation risk for allow/deny.
29
+ - It does not guarantee what the model already consumed before persistence-time hook execution.
30
+ - It does not replace operation gate logic from Stem or interception logic from Thorn.
31
+
32
+ ## Runtime flow
33
+
34
+ ```mermaid
35
+ flowchart TD
36
+ A[Content reaches output path] --> H{Hook path}
37
+ H -->|tool_result_persist| TP[Scan tool result content]
38
+ H -->|message_sending| MS[Strip leaked policy block, then scan]
39
+ TP --> M{Sensitive match?}
40
+ MS --> M
41
+ M -->|No| PASS[Return original/unchanged path]
42
+ M -->|Yes + audit| WR[Emit would_redact event]
43
+ M -->|Yes + enforce| RD[Emit redacted event and return sanitized content]
44
+ WR --> PASS
45
+ ```
46
+
47
+ ## Decision inputs
48
+
49
+ Pulp consumes:
50
+ - content payload from hook event
51
+ - effective redaction patterns (built-in + custom)
52
+ - runtime mode (audit or enforce)
53
+ - hook path context (tool_result_persist or message_sending)
54
+
55
+ ## Decision behavior (high level)
56
+
57
+ ### Tool result persist path
58
+ - scans tool result message content
59
+ - if sensitive match:
60
+ - audit: emits observation event and keeps original content
61
+ - enforce: emits redaction event and returns sanitized message
62
+
63
+ ### Message sending path
64
+ - first strips leaked policy block markers from outgoing content
65
+ - then scans resulting content for sensitive match
66
+ - if sensitive match:
67
+ - audit: emits observation event and generally does not rewrite for redaction
68
+ - enforce: emits redaction event and returns sanitized content
69
+ - if only policy block leak is found, returns content with policy block removed
70
+
71
+ ## Policy-block hygiene behavior
72
+
73
+ Pulp includes a dedicated outgoing hygiene step:
74
+ - detects leaked policy wrapper snippets
75
+ - removes them from outgoing message content
76
+ - logs that policy leakage was stripped
77
+
78
+ This hygiene step is independent from secret/PII redaction outcomes.
79
+
80
+ ## How Pulp interacts with other layers
81
+
82
+ ### With Stem
83
+ - Stem decides whether risky operations should proceed.
84
+ - Pulp sanitizes output content after content exists.
85
+
86
+ ### With Thorn
87
+ - Thorn can stop risky calls before execution.
88
+ - Pulp handles content-side sanitation for outputs that still reach output hooks.
89
+
90
+ ### With Root
91
+ - Root injects security guidance text into context.
92
+ - Pulp can remove leaked policy snippets before user delivery in outgoing messages.
93
+
94
+ ### With Leaf
95
+ - Leaf provides inbound audit visibility.
96
+ - Pulp provides outbound sanitation visibility.
97
+
98
+ ## Operational value
99
+
100
+ Pulp is useful for:
101
+ - reducing secret/PII exposure in stored and outbound content paths
102
+ - providing structured output-side audit evidence
103
+ - containing leaked policy-block echoes in user-visible responses
104
+
105
+ ## Limits and caveats
106
+
107
+ - tool_result_persist is persistence-time; model output in the same turn may have already seen raw content.
108
+ - behavior depends on host runtime hook availability/invocation.
109
+ - pattern quality and coverage control redaction quality.
110
+ - should be operated with Stem/Thorn for stronger defense-in-depth.
111
+
112
+ ## Validation checklist
113
+
114
+ 1. Send benign output content and confirm unchanged behavior.
115
+ 2. Produce output containing sensitive marker and confirm mode-specific outcome:
116
+ - audit: observation event
117
+ - enforce: sanitized output path
118
+ 3. Force leaked policy wrapper in outgoing content and confirm stripping behavior.
119
+ 4. Confirm report includes expected would_redact or redacted event counts.
120
+
121
+ ## See layers
122
+
123
+ - [root](root.md)
124
+ - [stem](stem.md)
125
+ - [thorn](thorn.md)
126
+ - [leaf](leaf.md)
127
+
128
+ ## Related pages
129
+ - [layers index](README.md)
130
+ - [decision modes](../decision/modes.md)
131
+ - [decision patterns](../decision/patterns.md)
132
+ - [decision posture](../decision/posture.md)
133
+ - [engine redaction](../engine/redaction.md)
134
+
135
+ ---
136
+
137
+ ## Navigation
138
+ - [Back to Layers Index](README.md)
139
+ - [Back to Wiki Index](../README.md)
@@ -0,0 +1,130 @@
1
+ ---
2
+ summary: "Layer reference for Berry.Root (policy injection strategy at agent turn start)"
3
+ read_when:
4
+ - You need to understand how policy text is injected per turn
5
+ - You are validating profile/adaptive behavior in real sessions
6
+ - You are debugging repeated policy text or missing reminders
7
+ title: "root"
8
+ ---
9
+
10
+ # `Berry.Root`
11
+
12
+ Berry.Root is the **policy-injection layer** that runs at agent turn start.
13
+
14
+ Its role is to decide whether the turn receives:
15
+ - full policy block
16
+ - short reminder block
17
+ - no injection
18
+
19
+ This decision is computed from session state + policy config (profile/adaptive/retention).
20
+
21
+ ## What Root does
22
+
23
+ - Hooks into before_agent_start.
24
+ - Computes per-turn decision (full, short, none) using shared policy state.
25
+ - Injects policy text through prependContext when decision is full or short.
26
+ - Uses session identity when available; falls back to global_session and forces full for safety.
27
+ - Clears per-session state on session_end.
28
+
29
+ ## What Root does not do
30
+
31
+ - It does not hard-block tool execution by itself.
32
+ - It does not redact content.
33
+ - It does not classify file/command risk directly.
34
+ - It does not replace Stem/Thorn/Pulp enforcement paths.
35
+
36
+ Root is guidance and control-plane orchestration for prompt policy presence.
37
+
38
+ ## Runtime flow
39
+
40
+ ```mermaid
41
+ flowchart TD
42
+ T[New Agent Turn] --> H[Hook: before_agent_start]
43
+ H --> R[Berry.Root]
44
+ R --> D{Decision}
45
+ D -->|full| F[Inject full policy block]
46
+ D -->|short| S[Inject short reminder block]
47
+ D -->|none| N[Skip injection]
48
+ F --> C[Continue turn]
49
+ S --> C
50
+ N --> C
51
+ ```
52
+
53
+ ## Decision inputs
54
+
55
+ Root consumes:
56
+ - session key (sessionId/sessionKey when available)
57
+ - provider/message provider identity
58
+ - profile (`strict`, `balanced`, `minimal`)
59
+ - adaptive settings (stale window, escalation turns, heartbeat, global escalation)
60
+ - retention limits for state tracking
61
+
62
+ ## Decision behavior (high level)
63
+
64
+ - strict: full policy every turn.
65
+ - balanced: first turn full; later turns typically none; short reminder on stale/heartbeat turns.
66
+ - minimal: first turn none; mostly none; short reminder only on heartbeat.
67
+ - forced-full windows can be triggered by adaptive escalation signals.
68
+ - provider changes can trigger full reinjection.
69
+ - missing session identity forces full policy for safety.
70
+
71
+ ## How Root interacts with other layers
72
+
73
+ ### With Stem
74
+ - Root instructs model behavior (call berry_check first).
75
+ - Stem is the actual gate tool that returns allow/deny decisions.
76
+ - Root improves compliance probability; Stem provides enforceable gate responses.
77
+
78
+ ### With Thorn
79
+ - Root guidance reduces risky tool attempts.
80
+ - Thorn can still block tool calls at hook level where available.
81
+
82
+ ### With Pulp
83
+ - Root influences what the agent attempts to output.
84
+ - Pulp sanitizes output paths when sensitive material appears.
85
+
86
+ ### With Leaf
87
+ - Root controls policy visibility cadence.
88
+ - Leaf provides inbound observability; it does not drive Root decisions directly.
89
+
90
+ ## Operational value
91
+
92
+ Root is useful for:
93
+ - reducing repeated policy spam while preserving safety reminders
94
+ - adapting policy visibility by session behavior
95
+ - keeping first-turn security context explicit when required
96
+ - supporting low-noise profiles without disabling enforcement layers
97
+
98
+ ## Limits and caveats
99
+
100
+ - Root is instruction-level control, not hard execution enforcement.
101
+ - If runtime context identity is missing, global safety fallback can increase policy verbosity.
102
+ - Excessive full injections increase token cost.
103
+ - Too little reinjection can reduce model adherence in long sessions.
104
+
105
+ ## Validation checklist
106
+
107
+ 1. Start a new session and confirm expected first-turn behavior for chosen profile.
108
+ 2. Send follow-up turns and confirm full/short/none cadence matches profile + adaptive config.
109
+ 3. Trigger denied events and verify forced-full window behavior on subsequent turns.
110
+ 4. End session and confirm state is cleared for the previous session id.
111
+
112
+ ## See layers
113
+
114
+ - [leaf](leaf.md)
115
+ - [stem](stem.md)
116
+ - [thorn](thorn.md)
117
+ - [pulp](pulp.md)
118
+
119
+ ## Related pages
120
+ - [layers index](README.md)
121
+ - [decision modes](../decision/modes.md)
122
+ - [decision posture](../decision/posture.md)
123
+ - [CLI profile](../operation/cli/profile.md)
124
+ - [CLI policy](../operation/cli/policy.md)
125
+
126
+ ---
127
+
128
+ ## Navigation
129
+ - [Back to Layers Index](README.md)
130
+ - [Back to Wiki Index](../README.md)