# Runtime strategies (/docs/runtime-strategies)



`SessionRuntimeConfig` carries a second set of fields the substrate
calls **strategies** — typed slots the host fills with domain code.
A strategy is not a plugin. Plugins contribute through named
slots on the `HarnessPlugin` contract; strategies are individual
typed fields on the runtime config, each with one well-defined
shape and one consumer inside the runtime or graph. When a
strategy is unset, the consumer gracefully degrades — the runtime
stays functional, the strategy-backed behavior goes inert.

Plugins and strategies meet at the same constructor. The split is
about shape, not lifecycle. A strategy is one typed field with one
caller — easy to type, easy to swap in a test. A plugin is an
extensible contributor that fills many slots on the `HarnessPlugin`
contract (tier nodes, stream observers, prompts, fabrication
detectors). Reach for a strategy when the runtime needs one answer
from a host-specific function; reach for a plugin when a package
wants to ship a bundle of contributions.

For the plugin counterpart see [Plugin contract](/docs/plugin-contract);
for how a host wires both at once see [Host adapter](/docs/host-adapter).

## Representative strategy slots [#representative-strategy-slots]

| Field                                   | Type                                                                                                   | Purpose                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `summaryExtractor`                      | `SummaryExtractor`                                                                                     | Domain-aware tool-result summarizer wired through the data-silo and recall-guard nodes. Default: empty-string no-op.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `manifest`                              | `ChatManifestStrategy`                                                                                 | Side-effect surface for chat-manifest persistence (system notices, provider switches). Default: every method no-op.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `toolCatalog`                           | `ToolCatalogProvider`                                                                                  | Tool descriptors, couplings, and `get_data` resolution. The graph asks here instead of importing a catalog directly. Default: empty.                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `toolExpansionTracker`                  | `ToolExpansionTracker`                                                                                 | Tracks tool-schema expansion requests across a session. Default: no-op tracker (zero pending always).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `responseSanitizer`                     | `ResponseSanitizer`                                                                                    | Identity-by-default pass over the final synthesis text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `fabricationGuardStrategy`              | `FabricationGuardStrategyBundle`                                                                       | Typed bundle the post-graph fabrication node consumes. `quantitativePatterns` is required; every other slot is optional and degrades cleanly when omitted. See [Fabrication detection](/docs/fabrication-detection).                                                                                                                                                                                                                                                                                                                                                                                               |
| `fabricationGuard&#x60; &#x2A;(legacy)* | `FabricationGuard`                                                                                     | Older single-call shape. Retained for hosts that haven't migrated; the typed `fabricationGuardStrategy` bundle is the recommended path.                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `intentMentionDetector`                 | `IntentMentionDetector`                                                                                | Extracts intent mentions used by the post-graph synthesis pipeline.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `toolQualityEvaluator`                  | `ToolQualityEvaluatorStrategy`                                                                         | Scores completed tool calls; the post-tool quality node reads the scored results. Default: empty array (branch skipped).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `subagentExecutor`                      | `SubagentExecutorFn`                                                                                   | Spawns a child agent and returns its terminal result. Default: a no-op executor that emits a "not configured" failure so guard paths still emit `subagent.failed` without throwing.                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `capabilityRegistry`                    | `CapabilityRegistry`                                                                                   | Slim mirror of an orchestrator-side capability index. Surfaces tool clusters for diagnostics and fallback ordering. Default: empty stub.                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `planGuardProvider`                     | `PlanGuardProvider`                                                                                    | Plan-stage guard forwarded to `DefaultAgentGraphConfig` at graph-build time. Default: no-op.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `repetitionGuard`                       | `RepetitionGuardSingleton`                                                                             | Per-session repetition tracker forwarded to the graph. Default: no-op.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `domainContextStrategy`                 | `DomainContextStrategy`                                                                                | Per-runtime singleton consumed by the streamSingleTurn body to gate domain-aware detectors (token sets, regex predicates). Hosts wiring a domain (drug discovery, antibody engineering, etc.) supply a strategy whose predicate bodies enumerate that domain. Default: `createDefaultDomainContextStrategy()` — predicates return `false`, detectors fire on linguistic signal alone.                                                                                                                                                                                                                              |
| `byokResolver`                          | `(family: ProviderFamily) => string \| undefined`                                                      | Synchronous bring-your-own-key resolver. It lives on `SessionRuntimeConfig`, not on `createPleachRuntime`'s own config. The runtime consults it when it gates direct-family model access, and never for `openrouter`; those keys come from `byokKeys`. Returning `undefined` falls back to `byokKeys`.                                                                                                                                                                                                                                                                                                             |
| `sandboxToolNamePrefix`                 | `string`                                                                                               | Namespace prefix for the host's sandbox tools (default `"sandbox_"`). Override when your sandbox surface uses a different convention.                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `delegationToolPrefix`                  | `string`                                                                                               | Namespace prefix for subagent-dispatch tools (default `"delegate_to_"`).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `asyncPlannerLlmCaller`                 | `AsyncPlannerLlmCaller`                                                                                | Host-supplied LLM caller for the async planner. The runtime stays provider-agnostic; the caller routes through your model-family selection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `asyncPlannerStepParser`                | `AsyncPlannerStepParser`                                                                               | Companion step parser. When both planner strategies are set, the constructor installs an `AsyncPlannerHandler` so the cold-start gate can wait for an LLM plan. `createPleachAgent` and `createPleachRoute` set both by default, so their runtimes plan multi-step requests with one extra model call; pass `asyncPlanner: false` to turn that off.                                                                                                                                                                                                                                                                |
| `planMode`                              | `"in-node" \| "legacy"`                                                                                | Where the plan comes from. Absent or `"legacy"` keeps the async planner exactly as before. `"in-node"` makes the `planGenerator` node await one planner call on the `utility` seam (stage `anchor-plan`), and `planReconciler` makes one on a user pivot. It needs `asyncPlannerStepParser`. The cold-start gate, crash-fallback plan and decorate pass do not run, and `asyncPlannerLlmCaller` is ignored. The call sends up to 4000 output tokens at temperature 0.2, and is cut off after 60 seconds. Plan ids start with `inplan_`. Core reads no environment variable for it; map your own flag onto the key. |
| `inNodePlannerCallOptions`              | `(ctx: { nodeId; messageId; family }) => { reasoningEffort?; hostCallOptions?; refuse? } \| undefined` | Per-call options for an in-node planner call, read only under `planMode: "in-node"`. `reasoningEffort` sets the call's effort, and `null` sends none. `hostCallOptions` goes to the adapter unread. A non-empty `refuse` skips the call, leaves the turn with no plan and writes an error row. Absent, the route's `planner` effort applies, then the utility seam's own policy.                                                                                                                                                                                                                                   |

The full set lives in `SessionRuntimeConfig` (from `@pleach/core`)
and the shapes in `@pleach/core/types/strategies`.

An in-node planner call ends in one of seven outcomes: `ok`, `call-failed`, `call-timeout`, `aborted`, `truncated`, `parse-empty` or `empty-by-model`. Only `ok` yields a plan; the rest leave the turn with none, never a fallback plan. Stop ends the call at once as `aborted` and writes no error row. A call that hits the output cap is `truncated`, because a cut-off plan is no plan. The request stays open for an abandoned call just long enough for its cost row to land.

## Wiring strategies at construction [#wiring-strategies-at-construction]

```typescript
import { SessionRuntime } from "@pleach/core";
import type {
  ToolCatalogProvider,
  SummaryExtractor,
  SubagentExecutorFn,
} from "@pleach/core/types/strategies";

const toolCatalog: ToolCatalogProvider = {
  /* host implementation */
};
const summaryExtractor: SummaryExtractor = {
  extractSummary: (_toolName, result) => ({
    description: typeof result === "string" ? result.slice(0, 280) : "",
  }),
  extractChainingFields: () => undefined,
};
const subagentExecutor: SubagentExecutorFn = async (_config, _parent, options) => {
  /* host implementation */
  return {
    id: options.id ?? crypto.randomUUID(),
    status: "completed",
    content: "",
    toolCalls: [],
    metrics: { totalSteps: 0, durationMs: 0 },
  };
};

import { SupabaseAdapter } from "@pleach/core/sessions";
import { createClient } from "@supabase/supabase-js";

const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!);

const runtime = new SessionRuntime({
  storage: new SupabaseAdapter({ client: supabase }),
  userId: "user_123",
  toolCatalog,
  summaryExtractor,
  subagentExecutor,
});
```

## `fabricationGuardStrategy` (typed bundle) [#fabricationguardstrategy-typed-bundle]

The `fabricationGuardStrategy` slot takes the typed bundle. The empty
default is the floor — `quantitativePatterns` is required, every other
slot is optional and degrades cleanly when omitted.

```typescript
import { SessionRuntime } from "@pleach/core";
import {
  DEFAULT_FABRICATION_GUARD_STRATEGY_BUNDLE,
  type FabricationGuardStrategyBundle,
} from "@pleach/core/types/strategies";

const fabricationGuardStrategy: FabricationGuardStrategyBundle = {
  ...DEFAULT_FABRICATION_GUARD_STRATEGY_BUNDLE,
  quantitativePatterns: [/\b\d+(?:\.\d+)?%/g, /\$\d[\d,]*/g],
};

const runtime = new SessionRuntime({
  storage,
  userId: "user_123",
  fabricationGuardStrategy,
});
```

## Strategy dep-injection via `harnessRuntime` accessors [#strategy-dep-injection-via-harnessruntime-accessors]

A second category of strategy plumbing rides on `OrchestratorConfig.harnessRuntime`
— a structural shape the runtime threads into the orchestrator config so the
streamSingleTurn body and graph nodes can read per-runtime strategies without
importing `SessionRuntime` directly. Each accessor is optional; CLI and raw
orchestrator entry points leave the shape minimal (often only
`getIntentPriorityTools`) and the consumer falls back to a default.

The body lift (PO-A1) cohort threads four strategy slots this way:

| Accessor                             | Returns                                 | Read site                                                    |
| ------------------------------------ | --------------------------------------- | ------------------------------------------------------------ |
| `getDomainContextStrategy?()`        | `DomainContextStrategy`                 | streamSingleTurn body — domain predicate gating.             |
| `getHallucinationDetectorFactory?()` | `HallucinationDetectorFactory`          | Per-turn detector factory the body calls once at turn entry. |
| `getRepetitionGuard?()`              | `RepetitionGuardSingleton \| undefined` | Per-session repetition tracker.                              |
| `getContinuationMetaToolNames?()`    | `ReadonlySet<string>`                   | Depth-zero continuation gate exemption set.                  |

Sibling accessors carry plugin-contributed strategies and registries that
share the same dep-inversion shape:

| Accessor                              | Returns                                                           | Purpose                                                                                                                         |
| ------------------------------------- | ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `collectFabricationGuard?()`          | `FabricationGuardImpl \| null`                                    | First-non-null fabrication-guard contribution from plugins. Threaded from `runtime.plugins.collectFabricationGuard()`.          |
| `collectGuestDeniedTools?()`          | `GuestDeniedToolsThunk \| null`                                   | Guest-mode tool denylist thunk. Invoked lazily so mid-session guest-mode flips reflect the contributing plugin's current state. |
| `collectContinuationPolicy?()`        | `ContinuationPolicy \| null`                                      | Continuation-policy contribution consumed by the continuation resolver.                                                         |
| `collectRuntimeAwareMiddleware?(ctx)` | `readonly MiddlewareContribution[]`                               | Per-turn middleware contributions resolved against backend, chat, and budget context.                                           |
| `collectRetryPolicy?()`               | `RetryPolicyContribution \| null`                                 | Last-wins retry-policy contribution consumed by the guarded stream runner.                                                      |
| `collectCitationEntityExtractor?()`   | `CitationEntityExtractor \| null`                                 | First-non-null citation-entity extractor for the post-response citation injection step.                                         |
| `getSafetyRegistry?()`                | `{ collectActiveContributions(): readonly SafetyContribution[] }` | Per-runtime `SafetyPolicyRegistry` accessor consumed by the resolver and BYOK / modal-llm fallback path.                        |

The pattern is consistent: hosts wire strategies through plugin contributions
(see [Plugin contract](/docs/plugin-contract)), the `SessionRuntime` exposes
them through facet accessors (see [Facets](/docs/facets)), and the body lift
reads them through `harnessRuntime?.getX?.()` or `harnessRuntime?.collectX?.()`
optional chains. CLI and raw orchestrator paths leave the accessors undefined
and the consumer falls back to an inline default — typically a no-op or a
direct module import retained for back-compat.

## Newer injection hooks [#newer-injection-hooks]

The substrate keeps growing strategy slots as more body-level
behaviors get lifted into the typed contract. The hooks below ship
today and follow the same shape rules as the slots above — typed
field on `SessionRuntimeConfig`, one consumer. Each falls back to a
graceful no-op when omitted, except `hallucinationDetectorFactory`,
which falls back to core's own detector.

| Hook                                      | Shape                                                                      | Purpose                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ----------------------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `hallucinationDetectorFactory`            | `(input: { availableToolNames, intent }) => HallucinationDetectorStrategy` | Per-turn factory the stream body calls once with the per-turn input bundle. Returns a detector that consumes streamed content via `feed` / `onToolUse` / `onMetaToolUse` and surfaces `hallucinationDetected` / `narratedWithoutExecution` getters. Default: `createCoreHallucinationDetector` (from `@pleach/core/streaming/hallucinatedToolDetector`), so detection is on when you pass no factory. To opt out, pass `createNoOpHallucinationDetector` (from `@pleach/core/strategies/hallucinationDetector`), whose getters report `false` and whose mutators do nothing. With `createPleachRuntime`, set it as `host.strategies.hallucinationDetectorFactory`.                                                                                                                                                                                                                                                                                                                             |
| `subagentHallucinationDetectorFactory`    | `HallucinationDetectorFactory`                                             | The detector a spawned subagent's runtime is built with. Default: undefined, so a child inherits its parent's resolved `hallucinationDetectorFactory`. Set it when children need a different detector, for example the no-op one.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `continuationMetaToolNames`               | `ReadonlySet<string>`                                                      | Names of "continuation meta-tools" the depth-zero continuation gate exempts from tool-call counting. Distinct from `metaToolNames` so a host can keep two exemption sets — the recovery-primer exemption set (`metaToolNames`) and the continuation exemption set — without conflating one surface. Default: undefined; consumer falls back to an empty set and counts every tool as continuation-relevant.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `repetitionGuard` (extended)              | `RepetitionGuardSingleton` with new optional methods                       | The existing `repetitionGuard` slot grew three optional methods consumed by the lifted body: `setPlanActive(active)` (tightens budget behavior while a deterministic plan is active), `getPriorTurnRunawayDirective()` (reads the runaway directive carried forward from the previous turn boundary, or `null`), and `clearPriorTurnRunawayDirective()` (paired consumer-side clear after injection). All three are optional — hosts that don't surface runaway directives leave them undefined and the body silently no-ops.                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `resolveCallReasoningEffort`              | `(ctx: { callClass, family, modelId }) => ReasoningEffort \| undefined`    | Per-call reasoning-budget control. The runtime calls it before each provider stream with the resolved call class, provider family, and model id. The returned `ReasoningEffort` (one rung of `REASONING_EFFORT_LADDER`: `"none"`, `"minimal"`, `"low"`, `"medium"`, `"high"`, `"xhigh"`, `"max"`; or `undefined` to leave it untouched) threads onto `StreamChatOptions.reasoningEffort`, and the adapter lowers it to the provider request. The provider adapter then snaps the level to the ones the model's catalog row lists (`snapReasoningEffort`). `StreamChatOptions.reasoningEffort: null` sends no field at all. A model with no catalog row gets the requested level as-is, and a mandatory-reasoning model can still answer `400`. DeepSeek always sends the field. `ReasoningEffort` is opaque to core — the host owns the policy of which call class on which family to cap. On the default provider stream, an unset effort means `"medium"`; see the quickstart default below. |
| `orchestrator.providers.providerFallback` | Registry-seam key                                                          | Module-loader registry-seam (not a config field) consumed by the body's provider-fallback path. Host wires a real implementation through the orchestrator's dynamic-import registry. This key has no substrate default: absent a registration, `dynamicImportApp` rejects, the body's catch attempts a legacy relative import that no longer resolves post-lift, and the call throws `MODULE_NOT_FOUND`. The consumer must wire the loader for this path to succeed. Lives next to the existing `orchestrator.providers.{circuitBreaker, fallbackExecutor, modelAvailabilityChecker}` cluster.                                                                                                                                                                                                                                                                                                                                                                                                 |
| `EMPTY_SINGLE_TURN_FLAGS`                 | Typed `Readonly<SingleTurnFlags>` export                                   | Canonical per-turn reset baseline — a frozen object with the four boolean defaults (`truncated`, `modelFallback`, `contextOverflow`, `hallucinationDetected`). Not a config slot; a typed export hosts spread (`{ ...EMPTY_SINGLE_TURN_FLAGS }`) at turn boundaries instead of re-deriving the defaults inline. Reachable from `@pleach/core/types/singleTurn`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |

When the detector catches a model narrating tool calls as prose
instead of calling them, the runtime retries the turn. When the
session's model derives no provider family (a BYOK slug, a gateway
alias, or a non-matrix id), the runtime never switches models, so it
re-streams on the same model instead. This applies to every runtime,
not only to `createPleachRuntime`. Those same-model
retries, together with the repetition-guard, post-model-hook (a hook
that rejects the content and asks for a retry) and garbled-output
retries, draw on one per-turn budget, `sameModelRetryCap` on
`createPleachRuntime` (an integer of 0 or more; default `3`; `0` means
never retry; anything else throws a `RangeError`). A runtime built
without that option uses the default. Every retry is a billed call. Once the budget is spent,
the turn delivers the model's real last answer instead of retrying
again. After a hallucinated-tool retry, that answer carries a note that
it may describe tool actions that did not run. A model a provider family
derives from retries within its own family instead, and the cap does
not apply. The exception is a failure to load the model-family
helpers: then every family re-streams its served model from this same
budget and never switches model. `legacyNonMatrixFallback: true`
(deprecated, removed in `1.0.0`) restores the old cross-family picks
for a model no family derives from; see
[Family-locked routing](/docs/family-lock#the-deprecated-legacy-fallback).

The factory-shaped hook (`hallucinationDetectorFactory`) is the
shape to reach for when a strategy needs **per-turn allocation** —
the consumer calls the factory once at turn entry with a per-turn
input bundle, and the returned detector instance is discarded at
turn end. The other strategy slots above are **per-runtime
singletons** the consumer calls many times across many turns.

`resolveCallReasoningEffort` is **per-call**: the runtime consults
it before every provider stream, so one turn can cap its final
synthesis call while leaving a reasoning-heavy planning call
untouched. It resolves synchronously and fails soft — a throw is
caught and treated as `undefined`, so the stream proceeds with the
request unchanged.

The quickstart factories install a default resolver. Every call class
asks for `"medium"`, and the default async planner asks for `"high"`.
Each request is then lowered with `snapReasoningEffort` to a level the
model's catalog row lists: the requested level when listed, else the
nearest rung of `REASONING_EFFORT_LADDER` (the lower on a tie). A model
whose row turns reasoning off by default gets no field unless you asked
explicitly. A row that lists no usable level gets no field either.

The default planner's output cap is `DEFAULT_ASYNC_PLANNER_MAX_TOKENS`,
now 4000 tokens, up from 1024. At `"high"` a reasoning model spent the
old cap on reasoning and returned empty or malformed plans. Set the
planner's setting to `medium` or `off` for lighter, cheaper planning. Raise or turn off a class with the route's
`reasoningEffortByCallClass` option (`"medium"`, `"high"` or `"off"`
per class) or its [environment variable](/docs/env-vars#runtime-flags).

```typescript
import { SessionRuntime } from "@pleach/core";
import type { HallucinationDetectorFactory } from "@pleach/core/strategies/hallucinationDetector";

const hallucinationDetectorFactory: HallucinationDetectorFactory = (input) => {
  // Host builds a detector parameterized by this turn's
  // `input.availableToolNames` + `input.intent`. The body calls
  // `feed(chunk)` on every content delta and reads the two getters
  // after the turn settles.
  return /* host implementation */ {} as ReturnType<HallucinationDetectorFactory>;
};

const runtime = new SessionRuntime({
  storage,
  userId: "user_123",
  hallucinationDetectorFactory,
  continuationMetaToolNames: new Set(["set_step_complete", "wait_for_jobs"]),
});
```

See [Providers](/docs/providers) for the provider-fallback registry
shape, [Fabrication detection](/docs/fabrication-detection) for the
broader fabrication-guard surface (separate contract from
hallucination detection — fabrication is per-call quantitative
pattern matching; hallucination is per-turn narration vs. execution
analysis), and the [Plugin contract](/docs/plugin-contract) for the
plugin counterpart to the strategy injection pattern.

## Where to go next [#where-to-go-next]

<Cards>
  <Card title="SessionRuntime" href="/docs/session-runtime" description="The class strategies attach to — construction, sessions, executeMessage, abort." />

  <Card title="Plugin contract" href="/docs/plugin-contract" description="The companion extension surface — many slots per contributor." />

  <Card title="Host adapter" href="/docs/host-adapter" description="The host integration that wires strategies and plugins side-by-side." />

  <Card title="Fabrication detection" href="/docs/fabrication-detection" description="The fabrication-guard strategy bundle and its detector surface." />
</Cards>
