Skip to content

API Reference parrot_formdesigner.services.snippets.base

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.snippets.base

View source

Snippet source protocol and the stable resolver closure (FEAT-459 / M3).

The event registry (services/event_registry.py) has no unregister API — register_form_event() raises ValueError on a duplicate (tenant, handler_ref). This module is why a DB-backed snippet can be re-published without hitting that guard: exactly ONE resolver closure is registered per key, and the closure looks up the CURRENT bundle on every dispatch through whatever SnippetSourceProtocol implementation owns that key.

See spec sdd/specs/formbuilder-custom-code.spec.md §2 "Dual-source storage" and §3 Module 3.

Attribute ContextProjectorFn

Alias EventResolution

Re-exported from parrot_formdesigner.core.events.EventResolution.

Alias FormEventAbort

Re-exported from parrot_formdesigner.core.events.FormEventAbort.

Alias FormEventContext

Re-exported from parrot_formdesigner.core.events.FormEventContext.

Alias SandboxContext

Re-exported from parrot_formdesigner.core.snippets.SandboxContext.

Attribute SandboxExecutorFn

Alias SandboxOutcome

Re-exported from parrot_formdesigner.core.snippets.SandboxOutcome.

Alias SnippetBundle

Re-exported from parrot_formdesigner.core.snippets.SnippetBundle.

Class SnippetSourceProtocol

What both the git loader (TASK-3164) and DB store (TASK-3165) implement.

Method SnippetSourceProtocol.resolve_current

Return the currently published bundle for this key, or None.

Called on EVERY dispatch by the resolver closure — implementations must be cheap (a read-through cache for DB; an in-memory dict for git, since git bundles never change without a redeploy).

resolve_current(*, tenant: str | None, handler_ref: str) -> SnippetBundle | None

Parameter Type Default Description
tenant str | None
handler_ref str

Returns: SnippetBundle | None

Attribute logger

Function make_resolver_adapter

Build the FormEventHandler-shaped closure registered exactly once.

make_resolver_adapter(source: SnippetSourceProtocol, *, tenant: str | None, handler_ref: str, project_context: ContextProjectorFn, execute: SandboxExecutorFn) -> Callable[[FormEventContext], Awaitable[EventResolution | None]]

Parameter Type Default Description
source SnippetSourceProtocol Resolves the current SnippetBundle for (tenant, handler_ref) on every call — see SnippetSourceProtocol.
tenant str | None The tenant this closure is registered under (None = global).
handler_ref str The handler_ref this closure answers for.
project_context ContextProjectorFn Converts the live FormEventContext + resolved bundle into a serialisable SandboxContext (TASK-3167's ContextProjector.project, injected — never imported here).
execute SandboxExecutorFn Runs the bundle against the projected context and returns a SandboxOutcome (TASK-3172's TierRouter.execute, injected).

Returns: Callable[[FormEventContext], Awaitable[EventResolution | None]] — An async callable matching FormEventHandler's signature, suitable for register_form_event().

Raises:

  • RuntimeError — If the source resolves no bundle at all for this key (e.g. a DB snippet was revoked with no fallback registered under this exact tenant slot — the fallback-to-global case is handled by get_form_event()'s own precedence, not here).

Alias register_form_event

Re-exported from parrot_formdesigner.services.event_registry.register_form_event.

Function register_resolver

Register the stable resolver closure for (tenant, handler_ref).

Call this EXACTLY ONCE per key at loader startup — never on republish. Re-raises register_form_event()'s ValueError unchanged on a duplicate key; that guard IS the two-snippets-one-ref safety check (spec §3 M4).

register_resolver(handler_ref: str, *, tenant: str | None, source: SnippetSourceProtocol, project_context: ContextProjectorFn, execute: SandboxExecutorFn) -> None

Parameter Type Default Description
handler_ref str Logical handler reference, e.g. "survey_v1.onBeforeSubmit".
tenant str | None None for a git-backed platform snippet; a tenant slug for a DB-backed tenant snippet.
source SnippetSourceProtocol The SnippetSourceProtocol owning this key.
project_context ContextProjectorFn See make_resolver_adapter.
execute SandboxExecutorFn See make_resolver_adapter.

Raises:

  • ValueError — Propagated from register_form_event() on a duplicate (tenant, handler_ref) — do not catch it here.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally