@arnilo/prism 0.0.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 (106) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/LICENSE +21 -0
  3. package/README.md +139 -0
  4. package/dist/agents.d.ts +5 -0
  5. package/dist/agents.js +439 -0
  6. package/dist/cli-runner.d.ts +33 -0
  7. package/dist/cli-runner.js +167 -0
  8. package/dist/cli.d.ts +2 -0
  9. package/dist/cli.js +10 -0
  10. package/dist/compaction.d.ts +9 -0
  11. package/dist/compaction.js +67 -0
  12. package/dist/config.d.ts +17 -0
  13. package/dist/config.js +69 -0
  14. package/dist/contracts.d.ts +670 -0
  15. package/dist/contracts.js +2 -0
  16. package/dist/contributions.d.ts +35 -0
  17. package/dist/contributions.js +47 -0
  18. package/dist/credentials.d.ts +22 -0
  19. package/dist/credentials.js +63 -0
  20. package/dist/extensions.d.ts +25 -0
  21. package/dist/extensions.js +131 -0
  22. package/dist/index.d.ts +47 -0
  23. package/dist/index.js +28 -0
  24. package/dist/input.d.ts +57 -0
  25. package/dist/input.js +225 -0
  26. package/dist/manifests.d.ts +28 -0
  27. package/dist/manifests.js +108 -0
  28. package/dist/middleware.d.ts +15 -0
  29. package/dist/middleware.js +49 -0
  30. package/dist/mock-provider.d.ts +6 -0
  31. package/dist/mock-provider.js +14 -0
  32. package/dist/models.d.ts +8 -0
  33. package/dist/models.js +25 -0
  34. package/dist/node/config.d.ts +9 -0
  35. package/dist/node/config.js +51 -0
  36. package/dist/node/session-store-jsonl.d.ts +17 -0
  37. package/dist/node/session-store-jsonl.js +134 -0
  38. package/dist/node/settings.d.ts +7 -0
  39. package/dist/node/settings.js +26 -0
  40. package/dist/node/trust.d.ts +13 -0
  41. package/dist/node/trust.js +56 -0
  42. package/dist/provider-events.d.ts +15 -0
  43. package/dist/provider-events.js +30 -0
  44. package/dist/provider-packages.d.ts +4 -0
  45. package/dist/provider-packages.js +12 -0
  46. package/dist/provider-request-policy.d.ts +9 -0
  47. package/dist/provider-request-policy.js +49 -0
  48. package/dist/providers/openai-compatible.d.ts +9 -0
  49. package/dist/providers/openai-compatible.js +197 -0
  50. package/dist/providers.d.ts +8 -0
  51. package/dist/providers.js +25 -0
  52. package/dist/redaction.d.ts +11 -0
  53. package/dist/redaction.js +68 -0
  54. package/dist/resources.d.ts +5 -0
  55. package/dist/resources.js +30 -0
  56. package/dist/retry.d.ts +11 -0
  57. package/dist/retry.js +48 -0
  58. package/dist/rpc.d.ts +18 -0
  59. package/dist/rpc.js +187 -0
  60. package/dist/security.d.ts +51 -0
  61. package/dist/security.js +60 -0
  62. package/dist/session-stores.d.ts +25 -0
  63. package/dist/session-stores.js +116 -0
  64. package/dist/settings.d.ts +3 -0
  65. package/dist/settings.js +26 -0
  66. package/dist/skills.d.ts +8 -0
  67. package/dist/skills.js +34 -0
  68. package/dist/system-prompts.d.ts +6 -0
  69. package/dist/system-prompts.js +47 -0
  70. package/dist/testing/provider-conformance.d.ts +36 -0
  71. package/dist/testing/provider-conformance.js +164 -0
  72. package/dist/tools.d.ts +25 -0
  73. package/dist/tools.js +109 -0
  74. package/docs/agent-session-runtime.md +167 -0
  75. package/docs/api-page-template.md +32 -0
  76. package/docs/cli-rpc.md +140 -0
  77. package/docs/compaction-and-retry.md +177 -0
  78. package/docs/compaction-llm.md +108 -0
  79. package/docs/compaction-observational-memory.md +123 -0
  80. package/docs/configuration-and-manifests.md +142 -0
  81. package/docs/context-and-skills.md +113 -0
  82. package/docs/contribution-registries.md +118 -0
  83. package/docs/credentials-and-redaction.md +122 -0
  84. package/docs/extensions.md +139 -0
  85. package/docs/index.md +56 -0
  86. package/docs/input-and-prompt-assembly.md +168 -0
  87. package/docs/middleware-hooks.md +123 -0
  88. package/docs/node-filesystem-config.md +85 -0
  89. package/docs/node-jsonl-session-store.md +81 -0
  90. package/docs/provider-conformance.md +109 -0
  91. package/docs/provider-layer.md +172 -0
  92. package/docs/provider-packages.md +171 -0
  93. package/docs/providers/kimi.md +110 -0
  94. package/docs/providers/openai-compatible.md +125 -0
  95. package/docs/providers/openai.md +131 -0
  96. package/docs/providers/opencode-go.md +103 -0
  97. package/docs/providers/openrouter.md +105 -0
  98. package/docs/providers/zai.md +108 -0
  99. package/docs/public-contracts.md +375 -0
  100. package/docs/release-and-install.md +141 -0
  101. package/docs/resource-loading.md +97 -0
  102. package/docs/session-stores-and-branching.md +119 -0
  103. package/docs/settings-auth-trust-security.md +73 -0
  104. package/docs/system-prompts.md +116 -0
  105. package/docs/tools.md +151 -0
  106. package/package.json +93 -0
@@ -0,0 +1,140 @@
1
+ # CLI/RPC
2
+
3
+ ## What it does
4
+
5
+ The `prism` bin is a thin adapter over `AgentSession`:
6
+
7
+ - `prism -p "prompt"`: print assistant text deltas.
8
+ - `prism --mode json -p "prompt"`: write one normalized event envelope per line.
9
+ - `prism --mode rpc`: read LF-delimited JSON requests from stdin and write correlated JSON responses/events to stdout.
10
+
11
+ It does not add a TUI, app tools, provider globals, extension discovery, resource discovery, or credential storage.
12
+
13
+ ## When to use it
14
+
15
+ Use the CLI for terminal smoke tests, scriptable JSON event streams, and simple non-Node clients that can speak newline-delimited JSON.
16
+
17
+ Use the SDK directly when an app needs custom providers, tools, resources, credentials, trust prompts, or UI behavior.
18
+
19
+ ## Inputs / request
20
+
21
+ CLI flags:
22
+
23
+ | Flag | Purpose |
24
+ | --- | --- |
25
+ | `-p`, `--prompt <text>` | Prompt for print/json modes. |
26
+ | `--mode print\|json\|rpc` | Select output/protocol mode. Defaults to `print`. |
27
+ | `--provider <name>` | Explicit provider id. The built-in `mock` id is only a smoke-test provider. |
28
+ | `--model <name>` | Explicit model name. |
29
+ | `--session <id>` | Session id. |
30
+ | `--config <path>` | Explicit config path recorded by the adapter; not auto-loaded. |
31
+ | `--resource <uri>` | Explicit resource URI recorded by the adapter; not auto-loaded. |
32
+ | `--extension <name>` | Explicit extension name recorded by the adapter; not auto-loaded/imported. |
33
+ | `--tool <name>` | Explicit tool name recorded by the adapter; not auto-enabled. |
34
+ | `--system <text>` | System instructions. |
35
+ | `--context <text>` | Context text reserved for host adapters. |
36
+ | `--compact <entries>` | Auto-compaction threshold for the run. |
37
+ | `--max-tool-rounds <n>` | Maximum runtime tool rounds. |
38
+ | `--help` | Print usage. |
39
+
40
+ RPC request envelope:
41
+
42
+ ```ts
43
+ { id: string | number; command: string; params?: Record<string, unknown> }
44
+ ```
45
+
46
+ Supported command names: `prompt`, `steer`, `followUp`, `abort`, `state`, `messages`, `setModel`, `compact`, `switchSession`, `forkSession`, `cloneSession`, and `command`.
47
+
48
+ ## Outputs / response / events
49
+
50
+ Print mode writes only assistant text deltas to stdout. Errors write a short line to stderr and return non-zero.
51
+
52
+ JSON mode writes newline-delimited event envelopes:
53
+
54
+ ```ts
55
+ { type: "event"; sessionId?: string; runId?: string; event: AgentEvent }
56
+ ```
57
+
58
+ RPC writes responses and async events:
59
+
60
+ ```ts
61
+ { id: string | number | null; ok: true; result?: unknown }
62
+ { id: string | number | null; ok: false; error: ErrorInfo }
63
+ { type: "event"; id: string | number; sessionId?: string; runId?: string; event: AgentEvent }
64
+ ```
65
+
66
+ Invalid CLI flags return exit code `2`. Invalid JSON, missing ids, unknown RPC commands, unsupported `steer`, unknown command contributions, and runtime failures return `ok: false` response envelopes without executing unknown tools or commands.
67
+
68
+ ## Request/response example
69
+
70
+ ```json
71
+ {"id":"1","command":"prompt","params":{"input":"Hi"}}
72
+ {"type":"event","id":"1","sessionId":"s1","runId":"run_1","event":{"type":"message_delta"}}
73
+ {"id":"1","ok":true,"result":{"sessionId":"s1"}}
74
+ ```
75
+
76
+ ## Active run behavior
77
+
78
+ `prompt` and `followUp` start an asynchronous run and write their final response only when the run finishes. While a run is active, the RPC loop continues to read and respond to other requests:
79
+
80
+ - `abort` cancels the active run for the current session and responds immediately.
81
+ - `state`, `messages`, `setModel`, `switchSession`, `forkSession`, `cloneSession`, and registered `command` requests are processed immediately.
82
+ - `compact` is fail-closed: if the current session has an active run, it returns `ok: false` because the session rejects compaction during a run.
83
+ - A second `prompt` or `followUp` for the same session while it already has an active run returns `ok: false` immediately instead of blocking the input loop.
84
+
85
+ Events streamed during a run keep the original prompt request id, even when an `abort` with a different request id cancels the run. The completion or error response for the prompt also uses the original prompt request id.
86
+
87
+ ```json
88
+ {"id":"run-1","command":"prompt","params":{"input":"Hi"}}
89
+ {"id":"abort-1","command":"abort","params":{"reason":"stop"}}
90
+ {"type":"event","id":"run-1","sessionId":"s1","runId":"run_1","event":{"type":"error","error":{"message":"Agent run aborted"}}}
91
+ {"id":"abort-1","ok":true,"result":{"sessionId":"s1"}}
92
+ {"id":"run-1","ok":false,"error":{"message":"Agent run aborted"}}
93
+ ```
94
+
95
+ ## Implementation example
96
+
97
+ ```sh
98
+ prism --provider mock --model demo -p "Hi"
99
+ prism --provider mock --mode json -p "Hi"
100
+ printf '{"id":"1","command":"prompt","params":{"input":"Hi"}}\n' | prism --provider mock --mode rpc
101
+ ```
102
+
103
+ Programmatic hosts should use the public runtime directly:
104
+
105
+ ```ts
106
+ import { createAgent, createMockProvider, providerDone, providerTextDelta } from "@arnilo/prism";
107
+
108
+ const agent = createAgent({
109
+ model: { provider: "mock", model: "demo" },
110
+ provider: createMockProvider([providerTextDelta("Hello"), providerDone()]),
111
+ });
112
+ await agent.createSession({ id: "s1" }).run("Hi");
113
+ ```
114
+
115
+ ## Extension and configuration notes
116
+
117
+ CLI/RPC are adapters over `AgentSession`. They do not scan packages, import extensions, read config files, fetch resources, resolve credentials, or register tools unless a host adapter explicitly wires those primitives in.
118
+
119
+ RPC `command` executes only explicitly registered `CommandDefinition` values. `setModel` stores a model override for later prompt/follow-up calls. `compact`, `switchSession`, `forkSession`, and `cloneSession` call the existing session APIs.
120
+
121
+ ## Security and performance notes
122
+
123
+ - No built-in app tools ship in core.
124
+ - No hidden provider, credential, extension, resource, config, settings, or tool globals are created.
125
+ - No full TUI or sandbox is provided or implied.
126
+ - JSONL is processed line by line with Node stdlib; no parser dependency, worker, watcher, or queue is added.
127
+ - Unknown or malformed CLI/RPC input fails closed.
128
+ - Do not put resolved credential values, tokens, headers, or secrets in prompts, CLI flags, config, events, or docs examples.
129
+
130
+ ## Related APIs
131
+
132
+ - [Agent/session runtime](agent-session-runtime.md): runtime API used by CLI/RPC.
133
+ - [Contribution registries](contribution-registries.md): command contributions are inert until explicitly wired.
134
+ - [Configuration and manifests](configuration-and-manifests.md): config data stays separate from CLI/RPC execution.
135
+ - [Node filesystem config loader](node-filesystem-config.md): optional explicit config file loading for Node hosts.
136
+ - [Resource loading](resource-loading.md): explicit resource loading primitives.
137
+ - [Credentials and redaction](credentials-and-redaction.md): secret redaction helpers and credential boundaries.
138
+ - [Observational memory compaction package](compaction-observational-memory.md): optional `om:status` and `om:view` command factories for explicitly wired hosts.
139
+
140
+ The CLI records flags but does not auto-load project-local resources, extensions, tools, or config. Hosts must make explicit trust and permission decisions before wiring any future local loading.
@@ -0,0 +1,177 @@
1
+ # Compaction and retry policies
2
+
3
+ ## What it does
4
+
5
+ Compaction helpers summarize older branch history without deleting raw session entries. Retry helpers retry transient provider-turn failures before any assistant output is observed.
6
+
7
+ Current APIs:
8
+
9
+ - `createDefaultCompactionStrategy(options?)`
10
+ - `isCompactionEntryData(value)`
11
+ - `CompactionEntryData`
12
+ - `CompactionOptions`
13
+ - `DefaultCompactionStrategyOptions`
14
+ - `AgentSession.compact(options?)`
15
+ - `AgentConfig.compaction` / `RunOptions.compaction`
16
+ - `createDefaultRetryPolicy(options?)`
17
+ - `isTransientErrorInfo(error)`
18
+ - `waitForRetry(decision, signal?)`
19
+ - `RetryPolicy`, `RetryContext`, `RetryDecision`, `RetryOptions`, `RetryMiddlewarePayload`
20
+ - `AgentConfig.retry` / `RunOptions.retry`
21
+
22
+ ## When to use it
23
+
24
+ Use compaction when a host wants provider input rebuilt from a summary plus recent messages while preserving the full branch in the session store. Use `session.compact()` for explicit compaction or `thresholdEntries` for opt-in auto-compaction before provider input.
25
+
26
+ Do not use it as vector memory, semantic search, provider-backed summarization, a store rewrite, a database migration, CLI/RPC command, provider-specific HTTP adapter, or whole-run retry loop.
27
+
28
+ ## Inputs / request
29
+
30
+ ```ts
31
+ createDefaultCompactionStrategy(options?: DefaultCompactionStrategyOptions): CompactionStrategy
32
+ ```
33
+
34
+ `DefaultCompactionStrategyOptions`:
35
+
36
+ | Field | Purpose |
37
+ | --- | --- |
38
+ | `name` | Optional strategy name; defaults to `default-compaction`. |
39
+ | `keepRecentEntries` | Number of recent message entries to keep in provider context; defaults to `8`. |
40
+ | `maxSummaryChars` | Maximum summary length; defaults to `4000`. |
41
+ | `secrets` | Exact known secret strings to redact from generated summaries. |
42
+
43
+ `CompactionOptions` can be placed on `AgentConfig.compaction`, `RunOptions.compaction`, or passed to `session.compact(options?)`:
44
+
45
+ | Field | Purpose |
46
+ | --- | --- |
47
+ | `strategy` | Optional `CompactionStrategy`; defaults to `createDefaultCompactionStrategy()`. |
48
+ | `thresholdEntries` | Enables auto-compaction when current branch entries exceed this count. Omit it for no auto-compaction. |
49
+ | `keepRecentEntries` | Number of recent message entries kept in provider context. |
50
+ | `maxSummaryChars` | Maximum default summary length. |
51
+ | `secrets` | Exact known secret strings to redact from summaries/events/store text. |
52
+ | `metadata` | Explicit host metadata passed to the compaction strategy only. |
53
+ | `signal` | Optional manual compaction abort signal. |
54
+
55
+ `RunOptions.compaction: false` disables configured auto-compaction for that run. `CompactionContext` also accepts optional `keepRecentEntries`, `trigger`, and `secrets`. Context values override or add to strategy defaults for that compaction call.
56
+
57
+ Retry policy setup:
58
+
59
+ ```ts
60
+ createDefaultRetryPolicy(options?: DefaultRetryPolicyOptions): RetryPolicy
61
+ ```
62
+
63
+ `RetryOptions` can be placed on `AgentConfig.retry` or `RunOptions.retry`:
64
+
65
+ | Field | Purpose |
66
+ | --- | --- |
67
+ | `policy` | Optional `RetryPolicy`; defaults to `createDefaultRetryPolicy(options)`. |
68
+ | `maxAttempts` | Total provider-turn attempts; defaults to `3`. |
69
+ | `baseDelayMs` | First retry delay; defaults to `100`. |
70
+ | `maxDelayMs` | Backoff cap; defaults to `1000`. |
71
+ | `secrets` | Exact known secret strings to redact from retry errors/events. |
72
+ | `metadata` | Explicit host metadata for retry policy context. |
73
+
74
+ `RunOptions.retry: false` disables configured retry for that run. Default classification retries generic transient codes/messages such as `ETIMEDOUT`, `ECONNRESET`, `429`, `500`, `502`, `503`, `504`, `timeout`, `rate_limit`, and `temporarily_unavailable`; aborts and non-transient errors fail closed.
75
+
76
+ `CompactionEntryData` is stored in `SessionEntry.data` for compaction entries:
77
+
78
+ | Field | Purpose |
79
+ | --- | --- |
80
+ | `throughEntryId` | Last older branch entry covered by the summary. |
81
+ | `keepEntryIds` | Recent message entry ids to keep as raw provider context. |
82
+ | `strategy` | Strategy that produced the entry. |
83
+ | `trigger` | `manual`, `auto`, or a host-defined trigger string. |
84
+
85
+ ## Outputs / response / events
86
+
87
+ `createDefaultCompactionStrategy().compact(context)` returns a `CompactionResult` with:
88
+
89
+ - `summary`: a conservative text summary of older user/assistant text, summary entries, model changes, labels, and tool-call/result labels.
90
+ - `entries`: one `kind: "compaction"` session entry whose parent is the current branch leaf.
91
+
92
+ `session.compact(options?)` emits `compaction_started`, runs the strategy on the current branch, runs `middleware.run("compaction", { context, result })` when middleware is configured, appends one standard `kind: "compaction"` entry under the current leaf, emits `compaction_finished`, and returns the appended result. Manual compaction rejects while a run is active.
93
+
94
+ Auto-compaction checks at most once per `run()`, after input/model-change entries are appended and before provider input assembly. It runs only when `AgentConfig.compaction` or `RunOptions.compaction` supplies `thresholdEntries`, and it is skipped by `RunOptions.compaction: false`.
95
+
96
+ `rebuildSessionContext()` detects the latest compaction entry on a branch. Its returned `entries` still contains the raw full branch, while `messages` contains only messages after the compaction boundary plus `keepEntryIds`, and `summaries` contains the compaction summary plus later summary entries.
97
+
98
+ Provider-turn retry wraps only the current provider request after input assembly. It emits `retry_scheduled`, waits with native abort-aware timers, and retries only if the failure happened before `message_started`, `message_delta`, or tool-call output was emitted. It does not retry missing providers, aborts, tool dispatch failures, unknown tools, validation failures, or post-output provider failures.
99
+
100
+ ## Request/response example
101
+
102
+ ```json
103
+ {
104
+ "kind": "compaction",
105
+ "summary": "user: older question\nassistant: older answer",
106
+ "data": {
107
+ "throughEntryId": "entry_10",
108
+ "keepEntryIds": ["entry_11", "entry_12"],
109
+ "strategy": "default-compaction",
110
+ "trigger": "manual"
111
+ }
112
+ }
113
+ ```
114
+
115
+ ## Implementation example
116
+
117
+ ```ts
118
+ import { createAgent, createDefaultCompactionStrategy, createDefaultRetryPolicy, rebuildSessionContext } from "@arnilo/prism";
119
+
120
+ const strategy = createDefaultCompactionStrategy({
121
+ keepRecentEntries: 4,
122
+ maxSummaryChars: 2000,
123
+ secrets: [apiKey],
124
+ });
125
+
126
+ const result = await strategy.compact({
127
+ sessionId: "s1",
128
+ entries: await store.list("s1"),
129
+ trigger: "manual",
130
+ });
131
+
132
+ for (const entry of result.entries ?? []) await store.append(entry);
133
+ const snapshot = rebuildSessionContext(await store.list("s1"));
134
+
135
+ const agent = createAgent({
136
+ model,
137
+ provider,
138
+ compaction: { strategy, thresholdEntries: 40, keepRecentEntries: 8 },
139
+ retry: { policy: createDefaultRetryPolicy({ maxAttempts: 3, baseDelayMs: 50 }) },
140
+ });
141
+ const session = agent.createSession({ id: "s1" });
142
+ await session.run("hello", { compaction: { thresholdEntries: 20 } });
143
+ await session.compact({ keepRecentEntries: 4 });
144
+ ```
145
+
146
+ ## Extension and configuration notes
147
+
148
+ Retry policies are ordinary `RetryPolicy` implementations and can be registered as `retryPolicy` contributions. Hosts must still pass the selected policy/config to `createAgent()` or `session.run()`. Retry middleware receives `{ context, decision }` before a retry is scheduled and can reduce delay or stop retrying.
149
+
150
+ Compaction strategies are ordinary `CompactionStrategy` implementations. Extensions can register strategies through the existing compaction strategy contribution registry, but registration is inert until a host explicitly selects and passes a strategy to runtime code. Extensions can also register `compaction` middleware; the runtime calls it only when the agent/session has that middleware registry configured.
151
+
152
+ The default strategy does not call a provider. Hosts that need model-generated summaries can use the optional [`@arnilo/prism-compaction-llm` package](compaction-llm.md). Hosts that need prepared source-backed memory without a compaction-time model call can use [`@arnilo/prism-compaction-observational-memory`](compaction-observational-memory.md).
153
+
154
+ ## Security and performance notes
155
+
156
+ - Raw session entries are never deleted or rewritten by these helpers or by runtime compaction.
157
+ - Default compaction is O(n) over supplied branch entries and uses only arrays/strings.
158
+ - Runtime compaction context excludes provider requests, provider objects, credential resolvers, resolved credentials, settings, and hidden metadata.
159
+ - The default summary excludes metadata, provider requests, provider objects, credential resolvers, resolved credentials, settings, and full tool result values.
160
+ - Redaction only removes exact known secret strings passed in `secrets`.
161
+ - Default retry uses native abort-aware timers only when a retry is scheduled; no worker, queue, network probe, or dependency is used.
162
+ - Retry context excludes provider request messages/content, provider objects, credentials, credential resolvers, and settings.
163
+
164
+ ## Related APIs
165
+
166
+ - [LLM compaction package](compaction-llm.md): optional provider-backed compaction preparation helpers.
167
+ - [Observational memory compaction package](compaction-observational-memory.md): optional source-backed memory ledger, worker runtime, fast no-model compaction strategy, projection, render, and recall utilities.
168
+ - [Session stores and branching](session-stores-and-branching.md): branch entries, compaction entries, and `rebuildSessionContext()` behavior.
169
+ - [Input and prompt assembly](input-and-prompt-assembly.md): compacted summaries become default summary messages for provider input.
170
+ - [Agent/session runtime](agent-session-runtime.md): `session.compact()`, opt-in auto-compaction, `RunOptions.retry`, and `retry_scheduled` runtime behavior.
171
+ - [Middleware hooks](middleware-hooks.md): `compaction` and `retry` middleware payload timing.
172
+ - [Contribution registries](contribution-registries.md): compaction strategy and retry policy contributions.
173
+ - [Configuration and manifests](configuration-and-manifests.md): `compactionStrategy` and `retryPolicy` manifest contribution kinds.
174
+ - [Provider layer](provider-layer.md): safe provider error codes used by retry classification.
175
+ - [Credentials and redaction](credentials-and-redaction.md): exact secret redaction helper used by default compaction and retry error handling.
176
+
177
+ Runtime redaction composes with compaction and retry secret lists: configured redactors apply at session serialization boundaries, while compaction/retry `secrets` still redact their local summaries and errors.
@@ -0,0 +1,108 @@
1
+ # LLM compaction package
2
+
3
+ ## What it does
4
+ `@arnilo/prism-compaction-llm` is an optional provider-backed compaction package. It prepares branch history, calls an explicit summary provider/model, and returns a standard Prism `CompactionStrategy`. The core default compaction remains local and conservative.
5
+
6
+ ## When to use it
7
+ Use it when a host wants model-generated summaries while preserving raw append-only session history. Do not use it as a core default, provider SDK loader, hidden credential discovery layer, vector memory, or store rewrite.
8
+
9
+ ## Inputs / request
10
+ Key exports:
11
+
12
+ | Export | Purpose |
13
+ | --- | --- |
14
+ | `createLlmCompactionStrategy(options)` | Returns a provider-backed `CompactionStrategy`. |
15
+ | `createLlmCompactionExtension(options)` | Registers the strategy into an explicit extension kernel compaction registry. |
16
+ | `prepareLlmCompaction(context, options?)` | Splits branch entries into summary input, kept suffix, optional split-turn prefix, file details, and compaction data. |
17
+ | `findLlmCompactionCutPoint(entries, options?)` | Finds the last entry covered by a summary using approximate token budgets. |
18
+ | `serializeCompactionConversation(entries, options?)` | Serializes entries with role labels and tool-result truncation, then redacts known secrets. |
19
+ | `collectFileOperations(messages)` / `formatFileOperations(details)` | Extracts and formats `read`, `write`, and `edit` tool-call paths. |
20
+ | `estimateTextTokens`, `estimateMessageTokens`, `estimateEntryTokens` | Cheap chars/4 token estimates. |
21
+ | prompt constants | Structured markdown summary prompts for host override/reference. |
22
+
23
+ `LlmCompactionStrategyOptions`:
24
+
25
+ | Field | Purpose |
26
+ | --- | --- |
27
+ | `provider` / `summaryProvider` | Explicit `AIProvider`, or factory receiving a resolved credential. |
28
+ | `model` / `summaryModel` | Explicit summary `ModelConfig`. |
29
+ | `credential`, `credentialRequest` | Optional per-call credential resolution for provider factories. |
30
+ | `providerOptions` | Generic `ProviderRequest.options`, including cache fields. |
31
+ | `providerRequestPolicies` | Optional Prism provider request policies applied before the summary call. |
32
+ | `customInstructions` | Additional summary focus appended to prompts. |
33
+ | `thinkingLevel` | Passed as `ProviderRequest.options.extra.thinkingLevel`. |
34
+ | `reserveTokens` | Output budget basis; defaults to `16384`. |
35
+ | `keepRecentTokens` | Approximate recent-token budget; defaults to `20000`. |
36
+ | `maxSummaryTokens` / `maxOutputTokens` | Sets generic `model.parameters.maxTokens` and truncates oversized collected summaries. |
37
+ | `maxToolResultChars` | Tool-result JSON truncation limit; defaults to `2000`. |
38
+ | `trackFileOperations`, `includeFileOperations` | Control file path extraction and final summary blocks. |
39
+ | `secrets` | Exact strings to redact from serialized prompts and final summaries. |
40
+
41
+ ## Outputs / response / events
42
+ `createLlmCompactionStrategy().compact(context)` returns a `CompactionResult` with `summary` and one `kind: "compaction"` entry. The entry data includes core `CompactionEntryData` plus `firstKeptEntryId`, token estimates, optional split-turn flag, and optional `readFiles`/`modifiedFiles` lists.
43
+
44
+ Provider `error` events, empty summaries, or abort signals throw before returning a result, so the runtime appends no compaction entry.
45
+
46
+ ## Request/response example
47
+ ```json
48
+ {
49
+ "throughEntryId": "entry_10",
50
+ "keepEntryIds": ["entry_11", "entry_12"],
51
+ "strategy": "llm-compaction",
52
+ "firstKeptEntryId": "entry_11",
53
+ "estimatedTokensBefore": 12000,
54
+ "estimatedTokensAfter": 1900
55
+ }
56
+ ```
57
+
58
+ ## Implementation example
59
+ ```ts
60
+ import { createLlmCompactionStrategy } from "@arnilo/prism-compaction-llm";
61
+
62
+ const strategy = createLlmCompactionStrategy({
63
+ provider: summaryProvider,
64
+ model: { provider: "mock", model: "cheap-summary" },
65
+ keepRecentTokens: 20_000,
66
+ reserveTokens: 16_384,
67
+ providerOptions: { cacheRetention: "short" },
68
+ customInstructions: "Focus on current files and failing tests.",
69
+ });
70
+
71
+ await session.compact({ strategy, secrets: [apiKey] });
72
+ ```
73
+
74
+ Credential factory example:
75
+
76
+ ```ts
77
+ const strategy = createLlmCompactionStrategy({
78
+ summaryProvider: (apiKey) => createProvider({ apiKey }),
79
+ credential: credentials,
80
+ credentialRequest: { provider: "example", name: "apiKey" },
81
+ summaryModel: { provider: "example", model: "cheap-summary" },
82
+ });
83
+ ```
84
+
85
+ ## Extension and configuration notes
86
+ This package is inert until imported. Direct strategy use works with `session.compact({ strategy })` and opt-in auto-compaction through existing `thresholdEntries` when the host selects the strategy.
87
+
88
+ ```ts
89
+ import { createAgent, createExtensionKernel } from "@arnilo/prism";
90
+ import { createLlmCompactionExtension } from "@arnilo/prism-compaction-llm";
91
+
92
+ const kernel = createExtensionKernel();
93
+ await kernel.load([createLlmCompactionExtension({ provider: summaryProvider, model: summaryModel })]);
94
+ const strategy = kernel.registries.compactionStrategies.resolve("llm-compaction");
95
+
96
+ const agent = createAgent({ model, provider, compaction: { strategy, thresholdEntries: 40 } });
97
+ ```
98
+
99
+ Registration only contributes an inert strategy. The host must resolve and pass it to runtime config.
100
+
101
+ ## Security and performance notes
102
+ Preparation is O(n) over branch entries and uses only arrays, strings, and JSON serialization. The strategy makes only the needed provider call(s): one history summary plus one split-turn prefix summary when needed. It does not discover credentials, read files, start background jobs, or add provider SDK dependencies. Redaction is exact-string only; pass every known secret that may appear in history or provider output.
103
+
104
+ ## Related APIs
105
+ - [Compaction and retry policies](compaction-and-retry.md): core compaction strategy surface.
106
+ - [Agent/session runtime](agent-session-runtime.md): `AgentSession.compact()` and opt-in auto-compaction.
107
+ - [Provider layer](provider-layer.md): mock providers and provider request contracts.
108
+ - [Credentials and redaction](credentials-and-redaction.md): exact known-secret redaction behavior.
@@ -0,0 +1,123 @@
1
+ # Observational memory compaction package
2
+
3
+ ## What it does
4
+
5
+ `@arnilo/prism-compaction-observational-memory` is an optional package for source-backed observational memory and fast compaction.
6
+
7
+ Current status: ledger/projection/render/recall utilities, explicit worker runtime, fast compaction strategy, inert extension helper, recall tool, and status/view command factories are available.
8
+
9
+ ## When to use it
10
+
11
+ Use it when a host wants to opt in to long-session memory that records observations/reflections as session custom entries, renders prepared memory during compaction, and supports exact-id recall.
12
+
13
+ Use `createObservationalMemoryCompactionStrategy()` when compaction should render prepared memory without a model call. Prism core still does not select this package by default.
14
+
15
+ ## Inputs / request
16
+
17
+ Memory records use `SessionEntry.kind: "custom"` with `entry.data.type` markers:
18
+
19
+ | Type | Payload |
20
+ | --- | --- |
21
+ | `om.observations.recorded` | `{ observations, coversUpToId? }` |
22
+ | `om.reflections.recorded` | `{ reflections, coversUpToId? }` |
23
+ | `om.observations.dropped` | `{ observationIds, coversUpToId? }` |
24
+ | `om.folded` | Compaction `data.memory` folded details. |
25
+
26
+ Ids are known, source-backed 12-character lowercase hex strings matching `^[a-f0-9]{12}$`.
27
+
28
+ ## Outputs / response / events
29
+
30
+ Key exports:
31
+
32
+ | Export | Purpose |
33
+ | --- | --- |
34
+ | `foldObservationalMemoryLedger()` | Fold custom memory entries into observations, reflections, drops, and coverage markers. |
35
+ | `buildObservationalMemoryProjection()` | Build active/full/folded projections from current branch entries. |
36
+ | `createFoldedMemoryDetails()` | Create JSON details for compaction `data.memory`. |
37
+ | `renderObservationalMemory()` | Render reflections and observations into a prepared memory summary. |
38
+ | `recallObservationalMemory()` | Recover source evidence for a known observation/reflection id from supplied current-branch entries. |
39
+ | `createMemoryId()` / `isMemoryId()` | Create/check 12-character ids. |
40
+ | `resolveObservationalMemorySettings()` | Merge `observational-memory` settings with defaults and overrides. |
41
+ | `createObservationalMemoryRuntime()` | Explicitly run observer/reflector/dropper workers for a supplied session/store/provider. |
42
+ | `createObservationalMemoryCompactionStrategy()` | Render existing folded memory as a standard Prism compaction summary with `data.memory`. |
43
+ | `createObservationalMemoryExtension()` | Inert extension helper that registers the strategy contribution unless disabled. |
44
+ | `createRecallMemoryTool()` | Optional exact-id `recall` tool factory backed by host-supplied current-branch entries. |
45
+ | `createMemoryStatusCommand()` / `createMemoryViewCommand()` | Optional `om:status` and `om:view` command factories. |
46
+ | `createObservationalMemoryCommands()` | Convenience factory returning status and view commands. |
47
+
48
+ Pure utilities create no events, workers, tools, commands, credentials, or provider requests. `createObservationalMemoryRuntime()` runs workers only when the host explicitly constructs it and calls `flush()`. The compaction strategy is O(n) over supplied entries and makes no provider call. Tool and command factories are inert until a host registers/selects them.
49
+
50
+ ## Request/response example
51
+
52
+ ```json
53
+ {"id":"aaaaaaaaaaaa","kind":"observation","found":true}
54
+ ```
55
+
56
+ ## Implementation example
57
+
58
+ ```ts
59
+ import {
60
+ buildObservationalMemoryProjection,
61
+ createObservationalMemoryCompactionStrategy,
62
+ createObservationalMemoryExtension,
63
+ createObservationalMemoryCommands,
64
+ createObservationalMemoryRuntime,
65
+ createRecallMemoryTool,
66
+ recallObservationalMemory,
67
+ renderObservationalMemory,
68
+ } from "@arnilo/prism-compaction-observational-memory";
69
+
70
+ const entries = await session.entries();
71
+ const projection = buildObservationalMemoryProjection(entries);
72
+ const summary = renderObservationalMemory(projection.reflections, projection.observations);
73
+ const evidence = recallObservationalMemory(entries, "aaaaaaaaaaaa");
74
+
75
+ const memory = createObservationalMemoryRuntime({
76
+ session,
77
+ store,
78
+ workerProvider,
79
+ workerModel: { provider: "mock", model: "memory" },
80
+ });
81
+ await memory.flush();
82
+ await session.compact({ strategy: createObservationalMemoryCompactionStrategy({ keepRecentEntries: 8 }) });
83
+
84
+ const getEntries = (sessionId: string) => sessions.get(sessionId)?.entries() ?? [];
85
+ const recallTool = createRecallMemoryTool({ getEntries, secrets: [apiKey] });
86
+ const commands = createObservationalMemoryCommands({ getEntries });
87
+
88
+ await kernel.load([createObservationalMemoryExtension({ recallTool: { getEntries }, commands: { getEntries } })]);
89
+ ```
90
+
91
+ ## Extension and configuration notes
92
+
93
+ Settings are read from the `observational-memory` key only when a host calls `resolveObservationalMemorySettings()` or `runtime.flush()`. Defaults are `observeAfterTokens: 10000`, `reflectAfterTokens: 20000`, `compactAfterTokens: 81000`, `observationsPoolMaxTokens: 20000`, `observationsPoolTargetTokens: 10000`, `agentMaxTurns: 16`, `passive: false`, and `debugLog: false`.
94
+
95
+ The runtime requires host-supplied `session`, matching `store`, `workerProvider`, and `workerModel`. Optional credential resolution is explicit; missing requested credentials skip worker execution.
96
+
97
+ `createObservationalMemoryCompactionStrategy()` keeps recent message entries like the default compaction strategy, renders existing observations/reflections as the summary, and returns a standard Prism compaction entry. Its `data` includes `throughEntryId`, `keepEntryIds`, `strategy`, `trigger`, and `memory: { type: "om.folded", version: 1, fullFold, observations, reflections, droppedObservationIds }`. When active observations exceed `observationsPoolMaxTokens`, it performs a full fold into `data.memory`.
98
+
99
+ `createRecallMemoryTool()` requires `args.id` to match `^[a-f0-9]{12}$`; invalid ids fail before entry lookup. Recall returns text and structured details for observations/reflections, dropped observations, supporting observations, source entries, and missing source ids. It does not search by topic.
100
+
101
+ `createMemoryStatusCommand()` reports recorded/dropped/active/visible observations, recorded/visible reflections, pool token counts, and optional runtime in-flight/last-error state. `createMemoryViewCommand()` renders visible memory by default or full active recorded memory with `{ mode: "full" }`; other modes return `Usage: /om:view [full]`.
102
+
103
+ `createObservationalMemoryExtension()` registers only inert contributions. It does not start workers, compact sessions, read settings, resolve credentials, call providers, or execute tools/commands during setup.
104
+
105
+ ## Security and performance notes
106
+
107
+ - Recall is exact-id only; there is no semantic search, vector store, or transcript browser.
108
+ - Recall tool and commands only see current-branch entries supplied by the host callback.
109
+ - Invalid or missing ids fail closed; invalid recall tool ids skip entry lookup.
110
+ - Utilities and fast compaction are O(n) over supplied entries and use no provider, network, filesystem, timer, worker, credential, or settings access.
111
+ - Workers serialize only supplied branch entries, enforce `agentMaxTurns`, and run one consolidation pipeline at a time per runtime.
112
+ - Compaction preserves raw history; Prism appends one standard compaction entry and rebuilds provider context from its summary plus kept recent messages.
113
+ - Pass known secrets to render/recall/runtime/tool/command helpers to redact exact values from prompts, records, structured results, and text output.
114
+ - Live tests are opt-in with `PRISM_LIVE_OBSERVATIONAL_MEMORY_TESTS=1`.
115
+
116
+ ## Related APIs
117
+
118
+ - [Compaction and retry policies](compaction-and-retry.md): replaceable compaction strategy boundary.
119
+ - [LLM compaction package](compaction-llm.md): existing optional compaction-package pattern.
120
+ - [Session stores and branching](session-stores-and-branching.md): branch entries that observational memory reads and appends to.
121
+ - [Extensions](extensions.md): inert registration pattern for optional package contributions.
122
+ - [Tools](tools.md): host activation and dispatch for optional recall tool contributions.
123
+ - [CLI/RPC](cli-rpc.md): command contributions through explicitly wired RPC hosts.