Skip to content

Feature Spec: apcore Module Bridge (F-002)

Feature ID: F-002 Priority: P0 Phase: Phase 1 (0.20.0-alpha.1) Tech Design Reference: Section 4.3


Purpose

FlowVane registers its capabilities as APCore modules and exposes them through the unified APCore Surfaces SDK and its public protocol adapters. The AST extension scanner discovers executors automatically.


File Changes

New Files

src/flowvane/bridge/__init__.py

"""apcore Module Bridge -- registers FlowVane capabilities as apcore Modules."""

from flowvane.bridge.registry_setup import create_flowvane_registry

__all__ = ["create_flowvane_registry"]

src/flowvane/bridge/module_adapter.py

Contains ExecutableTaskModuleAdapter class and ModuleAnnotations dataclass.

src/flowvane/bridge/scanner_bridge.py

Contains discover_executor_modules() function and _create_adapter_from_metadata() helper.

src/flowvane/bridge/task_modules.py

Exports the original 16 task management classes below. The complete built-in module list is maintained in src/flowvane/bridge/registry_setup.py (the task_modules dict); execution and governance modules live in separate files.

  • Lifecycle: TaskCreateModule, TaskCreateTreeModule, TaskExecuteModule, TaskCancelModule
  • Read/write: TaskGetModule, TaskUpdateModule, TaskListModule, TaskDeleteModule
  • Tree: TaskTreeModule, TaskChildrenModule
  • Composition: TaskLinkModule, TaskCopyModule, TaskArchiveModule, TaskCloneMixedModule
  • Queries: TaskRunningListModule, TaskScheduledListModule

src/flowvane/bridge/schedule_modules.py

Contains 6 scheduling modules:

  • ScheduleSetModule → schedule.set — configure a task's schedule and compute its next run
  • ScheduleDueModule → schedule.due — list scheduled tasks whose next run has arrived
  • ScheduleTriggerModule → schedule.trigger — trigger a scheduled task immediately
  • ScheduleCompleteModule → schedule.complete — record a run as complete and advance the schedule
  • ScheduleHistoryModule → schedule.history — list a scheduled definition's run instances
  • ScheduleExportICalModule → schedule.export_ical — export scheduled tasks as an iCalendar feed

src/flowvane/bridge/execution_modules.py

  • task.submit — durably accept a root for background execution.
  • task.receipt — retrieve a caller-scoped idempotent creation receipt.
  • task.reconcile — compatibility entry that rejects historical in-place recovery; use run.retry instead.
  • run.get — inspect an accepted definition, lifecycle, and retry lineage.
  • run.retry — derive a new Run with new task IDs and frozen completed results.

src/flowvane/bridge/governance_modules.py

  • budget.scope — configure or inspect a shared token budget.
  • budget.reconcile — reconcile an invocation reservation using provider evidence.
  • usage.events — read a caller-scoped page of immutable consumption events.
  • usage.ack — acknowledge an event after the consumer's durable commit.

There are 31 built-in management entries, including the rejecting task.reconcile compatibility entry, plus discovered executors. With the default namespace, REST exposes them at /modules/flowvane.<action> and MCP/A2A use the same registry IDs. CLI promotes the five groups task, schedule, run, budget, and usage; for example:

flowvane run get --run-id ROOT_ID
flowvane run retry --run-id OLD_ROOT_ID --idempotency-key retry-operation-1
flowvane budget scope --scope experiment-1 --limit-tokens 10000
flowvane usage events --after-index 0 --limit 100

The flowvane flowvane run.get command and other dot-namespaced commands remain available. CLI module calls execute against local configured storage; they are not HTTP clients. See REST recovery and budget and usage operations for the lifecycle and consumer contracts.

src/flowvane/bridge/registry_setup.py

Contains create_flowvane_registry() function.

src/flowvane/bridge/protocol.py

PublicExecutor delegates execution, identity, ACL, and approval to apcore and sanitizes returned objects and stream chunks using the declared output schema and FlowVane's credential rules. FlowVaneMCP retains the SDK's configured execution pipeline and default sensitive-name masking, with exceptions for known integer accounting counters that have already passed schema and credential redaction. Declared sensitive fields and credentials remain redacted. The standalone and unified server entry points both use these adapters.

src/flowvane/bridge/output_schema.py

public_output_schema() derives the MCP public output contract from a module's native schema. FlowVaneMCP installs this contract on the SDK's tool definitions so discovery and server/client validation describe the same redacted result. The registry's native schema remains unchanged.

Test Files

tests/bridge/__init__.py
tests/bridge/test_module_adapter.py
tests/bridge/test_scanner_bridge.py
tests/bridge/test_task_modules.py
tests/bridge/test_registry_setup.py
tests/bridge/test_mcp_output_schema.py
tests/integration/test_sdk_compatibility.py

MCP and A2A Operations

The same registered IDs are available through both protocols:

Capability MCP tool / A2A skill ID
Durable submission and creation receipt flowvane.task.submit, flowvane.task.receipt
Run inspection and new retry Run flowvane.run.get, flowvane.run.retry
Shared token budget and evidenced settlement flowvane.budget.scope, flowvane.budget.reconcile
Consumption delivery and acknowledgement flowvane.usage.events, flowvane.usage.ack

flowvane.task.reconcile remains a rejecting compatibility entry. Neither protocol may reset historical Runs. Cancellation, late usage, idempotency, and owner boundaries follow the same module contracts as REST and CLI.

MCP

Start flowvane mcp for stdio, or flowvane mcp --transport streamable-http for HTTP (default http://127.0.0.1:8001/mcp). The unified server exposes /mcp on port 8000. Use an MCP client to initialize the session, discover with tools/list, and invoke a discovered tool with structured arguments:

{
  "jsonrpc": "2.0",
  "id": "retry-1",
  "method": "tools/call",
  "params": {
    "name": "flowvane.run.retry",
    "arguments": {"run_id": "OLD_ROOT_ID", "idempotency_key": "retry-operation-1"}
  }
}

Read the new run_id from the tool's structured result, then call flowvane.task.submit with {"task_id":"NEW_RUN_ID"}. MCP clients handle the transport initialization and HTTP session headers; this JSON is a protocol message, not a standalone unauthenticated HTTP request.

Public Output Schema

Native execution validates the original result against the module's original output schema before public redaction. An invalid native result still fails; redaction does not bypass validation, identity, ACL, approval, or middleware.

MCP tools/list advertises a separate outputSchema for the redacted structuredContent. A sensitive integer, enum, or complete object can become "***REDACTED***", and credentials inside otherwise ordinary strings can be masked. The public schema therefore accepts the marker at each value position, preserves types, required properties, object/array structure, numeric bounds, and container sizes, and relaxes constraints whose truth can change during redaction: enum/const values, string patterns, lengths and formats, uniqueness, containment, and conditional predicates. Exclusive alternatives become non-exclusive alternatives. These original constraints remain enforced by native execution.

Local references, anchors, resource scopes, and recursive schemas are projected into a finite definition graph without remote retrieval. Legacy tuple schemas are represented with Draft 2020-12 array keywords. Dynamic reference assertions are omitted from the public projection; native validation remains authoritative. Consumers should use the advertised schema for public results and the native module schema for executor development.

A2A

For flowvane serve --all, discover at /a2a/.well-known/agent-card.json and send JSON-RPC to /a2a/. Standalone flowvane serve uses /.well-known/agent-card.json and / instead:

curl -X POST localhost:8000/a2a/ \
  -H 'content-type: application/json' -H 'A2A-Version: 1.0' \
  -d '{"jsonrpc":"2.0","id":"retry-1","method":"SendMessage","params":{"message":{"messageId":"retry-message-1","role":"ROLE_USER","parts":[{"data":{"run_id":"OLD_ROOT_ID","idempotency_key":"retry-operation-1"}}],"metadata":{"skillId":"flowvane.run.retry"}}}}'

The returned A2A task/artifact contains the module result. An A2A task ID is a transport invocation ID, not a FlowVane Run ID. A completed task.submit protocol request confirms acceptance, not completion of the submitted workflow; observe the workflow through run.get / task.get. Protocol errors use their native envelopes: MCP returns isError, while A2A can return TASK_STATE_REJECTED for access denial. These are separate from the persisted workflow's execution state.

A2A public cards omit approval-required skills such as budget.reconcile. Authenticated extended-card discovery and invocation remain subject to the deployment's ACL, role checks, and approval policy; omission from the public card does not mean the module is unimplemented. Add the verified Bearer token when calling authenticated HTTP services on either protocol.

A2A protobuf numbers use IEEE-754 doubles. Governance inputs normalize exact integral values such as 100.0 to integers and reject booleans, fractions, and floating values outside the safe integer range. Use REST or MCP when values exceed 2**53 - 1; A2A cannot preserve those accounting integers exactly.

Standalone MCP/A2A submission requires a dispatcher against the same database; the unified server already runs one. Consumers on either protocol must commit and deduplicate events before usage.ack. Pricing and wallets remain application responsibilities. See budget and usage operations.

Embedded Servers

Use the same public output adapters as the CLI servers when embedding either protocol. For MCP:

from flowvane.app import create_app
from flowvane.bridge.protocol import FlowVaneMCP

app = create_app()
FlowVaneMCP(app.registry, name="FlowVane").serve(
    transport="streamable-http", host="127.0.0.1", port=8001
)

For A2A, in a separate server process:

from apcore import Executor
from apcore_surfaces.a2a import serve
from flowvane.app import create_app
from flowvane.bridge.protocol import PublicExecutor

app = create_app()
serve(PublicExecutor(Executor.from_registry(app.registry)), name="FlowVane")

These adapters preserve readable usage and budget integers while honoring schema-sensitive fields and credential masking on returned objects and stream chunks. FlowVaneMCP wraps the executor after SDK initialization so its configured ACL, approval store, and middleware remain active. Configure authentication and authorization for your deployment; the minimal examples above do not configure an identity provider. Run flowvane dispatcher --db DATABASE_URL separately for durable submissions, using the same database configuration as the embedded app.


Method Signatures

ExecutableTaskModuleAdapter

class ExecutableTaskModuleAdapter:
    def __init__(
        self,
        executor_class: type,
        executor_id: str,           # Non-empty string
        executor_name: str,
        executor_description: str,
        input_schema: Dict[str, Any],   # Valid JSON Schema dict
        output_schema: Dict[str, Any],  # Valid JSON Schema dict
        tags: list[str],
        dependencies: list[str],
        always_available: bool,
    ) -> None: ...

    # Plain attributes (not properties) — apcore may set these during registration
    input_schema: Dict[str, Any]
    output_schema: Dict[str, Any]
    description: str
    annotations: ModuleAnnotations

    async def execute(self, inputs: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
        """Execute the wrapped executor.
        Logic:
        1. Validate inputs is a dict.
        2. Try executor_class() (no args).
        3. If TypeError, try executor_class(inputs={}).
        4. Call await executor.execute(inputs).
        5. Return result dict.
        Raises: TypeError if inputs is not dict. RuntimeError on executor failure.
        """
        ...

discover_executor_modules()

def discover_executor_modules() -> list[ExecutableTaskModuleAdapter]:
    """Discover all registered executors and create Module adapters.
    Logic:
    1. Call ExtensionScanner.scan_builtin_executors() to get metadata map.
    2. For each executor metadata:
       a. Call _create_adapter_from_metadata(metadata).
       b. If successful, append to adapters list.
       c. If failed, log error and continue.
    3. Return adapters list.
    """
    ...

_create_adapter_from_metadata()

def _create_adapter_from_metadata(
    metadata: ExecutorMetadata,
) -> ExecutableTaskModuleAdapter | None:
    """Create adapter from AST-scanned metadata.
    Logic:
    1. importlib.import_module(metadata.module_path).
    2. getattr(module, metadata.class_name).
    3. Try to create template instance for schema extraction:
       a. Try executor_class(inputs={}).
       b. If fails, create minimal subclass with no-op __init__.
    4. Call template.get_input_schema() (fallback: {"type": "object", "properties": {}}).
    5. Call template.get_output_schema() (fallback: {"type": "object", "properties": {}}).
    6. Return ExecutableTaskModuleAdapter with all extracted data.
    Returns None if import fails.
    """
    ...

TaskCreateModule

class TaskCreateModule:
    description: str = "Create a new task in the FlowVane task engine."
    input_schema: Dict[str, Any]   # JSON Schema with: name (required), inputs, params,
                                    # parent_id, priority (0-3), dependencies, token_budget,
                                    # cost_policy, max_attempts (1-100), backoff_strategy
    output_schema: Dict[str, Any]  # JSON Schema with: id (required), name, status, created_at

    def __init__(self, task_creator: Any, task_repository: Any) -> None: ...

    async def execute(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """Create a task.
        Logic:
        1. Extract 'name' from input. Validate non-empty.
        2. Build task_data dict from input fields, remove None values.
        3. Call task_creator.create_task_trees_from_array([task_data]).
        4. Return {id, name, status, created_at} from first tree's root task.
        Raises: ValueError if name is missing or empty.
        """
        ...

TaskExecuteModule

class TaskExecuteModule:
    description: str = "Execute an existing task in the FlowVane task engine."
    input_schema: Dict[str, Any]   # {task_id: required, minLength: 1}
    output_schema: Dict[str, Any]  # {task_id, status, result, token_usage}

    def __init__(self, task_manager: Any) -> None: ...

    async def execute(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """Execute a task by ID.
        Logic:
        1. Extract task_id. Validate non-empty.
        2. Call task_manager.execute_task(task_id).
        3. Return result.
        Raises: ValueError if task_id empty.
        """
        ...

TaskListModule

class TaskListModule:
    description: str = "List tasks from the FlowVane task engine with optional filtering."
    input_schema: Dict[str, Any]   # {status: enum, user_id, limit: 1-1000 default 50, offset: >=0}
    output_schema: Dict[str, Any]  # {tasks: array, total: int}

    def __init__(self, task_repository: Any) -> None: ...

    async def execute(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """List tasks.
        Logic:
        1. Clamp limit to [1, 1000], default 50.
        2. Clamp offset to >= 0, default 0.
        3. Call task_repository.list_tasks() with filters.
        4. Call task_repository.count_tasks() for total.
        5. Return {tasks: [{id, name, status, created_at}], total}.
        """
        ...

TaskGetModule

class TaskGetModule:
    description: str = "Get detailed information about a specific task."
    input_schema: Dict[str, Any]   # {task_id: required, minLength: 1}
    output_schema: Dict[str, Any]  # Full task dict

    def __init__(self, task_repository: Any) -> None: ...

    async def execute(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """Get task by ID.
        Logic:
        1. Extract task_id. Validate non-empty.
        2. Call task_repository.get_task_by_id(task_id).
        3. If None, raise KeyError.
        4. Return task.to_dict().
        Raises: ValueError if empty, KeyError if not found.
        """
        ...

TaskDeleteModule

class TaskDeleteModule:
    description: str = "Delete a task from the FlowVane task engine."
    input_schema: Dict[str, Any]   # {task_id: required, minLength: 1}
    output_schema: Dict[str, Any]  # {task_id, deleted: bool}

    def __init__(self, task_repository: Any) -> None: ...

    async def execute(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """Delete task.
        Logic:
        1. Extract task_id. Validate non-empty.
        2. Verify task exists via get_task_by_id. If not, raise KeyError.
        3. Call task_repository.delete_task(task_id).
        4. Return {task_id, deleted: True}.
        Raises: ValueError if empty, KeyError if not found.
        """
        ...

create_flowvane_registry()

def create_flowvane_registry(
    task_manager: Any,
    task_creator: Any,
    task_repository: Any,
    namespace: str = "flowvane",  # Non-empty, default "flowvane"
) -> Registry:
    """Create and populate an apcore Registry with all FlowVane modules.
    Logic:
    1. Create APCore() client.
    2. Access client.registry (APCore provides registry directly, no create_registry() needed).
    3. Call discover_executor_modules() to get executor adapters.
    4. For each adapter, call client.register(f"{namespace}.{adapter.executor_id}", adapter).
    5. Create TaskCreateModule, TaskExecuteModule, TaskListModule, TaskGetModule, TaskDeleteModule.
    6. Register each as f"{namespace}.task.{action}".
    7. Log total module count.
    8. Return registry.
    Raises: RuntimeError if APCore initialization fails.
    """
    ...

Data Models

No data model changes. This feature adds a bridge layer without modifying existing data structures.


Test Requirements

Unit Tests: tests/bridge/test_module_adapter.py

def test_adapter_creation_valid():
    """Adapter initializes with valid parameters."""
    adapter = ExecutableTaskModuleAdapter(
        executor_class=MockExecutor,
        executor_id="mock_executor",
        executor_name="Mock Executor",
        executor_description="A mock executor for testing",
        input_schema={"type": "object", "properties": {"query": {"type": "string"}}},
        output_schema={"type": "object", "properties": {"result": {"type": "string"}}},
        tags=["test"],
        dependencies=[],
        always_available=True,
    )
    assert adapter.description == "A mock executor for testing"
    assert adapter.input_schema["properties"]["query"]["type"] == "string"

def test_adapter_creation_empty_id_raises():
    """Empty executor_id raises ValueError."""
    with pytest.raises(ValueError, match="non-empty"):
        ExecutableTaskModuleAdapter(executor_class=MockExecutor, executor_id="", ...)

def test_adapter_creation_non_class_raises():
    """Non-class executor_class raises TypeError."""
    with pytest.raises(TypeError, match="must be a class"):
        ExecutableTaskModuleAdapter(executor_class="not_a_class", ...)

async def test_adapter_execute_delegates():
    """execute() creates instance and delegates to executor.execute()."""
    adapter = ExecutableTaskModuleAdapter(executor_class=MockExecutor, ...)
    result = await adapter.execute({"query": "test"})
    assert "result" in result

async def test_adapter_execute_non_dict_raises():
    """execute() raises TypeError for non-dict input."""
    adapter = ExecutableTaskModuleAdapter(executor_class=MockExecutor, ...)
    with pytest.raises(TypeError, match="must be a dict"):
        await adapter.execute("not a dict")

def test_adapter_annotations():
    """annotations property returns correct metadata."""
    adapter = ExecutableTaskModuleAdapter(
        executor_class=MockExecutor,
        executor_id="mock",
        ...,
        tags=["http", "api"],
        dependencies=["httpx"],
        always_available=False,
    )
    assert adapter.metadata["executor_id"] == "mock"
    assert adapter.tags == ["http", "api"]
    assert adapter.metadata["executor_dependencies"] == ["httpx"]

Unit Tests: tests/bridge/test_scanner_bridge.py

def test_discover_executor_modules_returns_list():
    """discover_executor_modules returns a list of adapters."""
    adapters = discover_executor_modules()
    assert isinstance(adapters, list)
    assert len(adapters) > 0  # At least REST, Email, AggregateResults, FlowVaneApi

def test_discover_executor_modules_has_rest():
    """REST executor is discovered."""
    adapters = discover_executor_modules()
    ids = [a.executor_id for a in adapters]
    assert "rest_executor" in ids

def test_discover_handles_import_failure(monkeypatch):
    """Import failure for one executor does not block others."""
    # Monkeypatch importlib.import_module to fail for one specific module
    # Verify other executors still discovered

Unit Tests: tests/bridge/test_task_modules.py

async def test_task_create_module_valid():
    """TaskCreateModule creates a task with valid input."""
    module = TaskCreateModule(mock_creator, mock_repo)
    result = await module.execute({"name": "test_task"})
    assert "id" in result
    assert result["name"] == "test_task"
    assert result["status"] == "pending"

async def test_task_create_module_empty_name_raises():
    """TaskCreateModule raises ValueError for empty name."""
    module = TaskCreateModule(mock_creator, mock_repo)
    with pytest.raises(ValueError, match="non-empty"):
        await module.execute({"name": ""})

async def test_task_create_module_missing_name_raises():
    """TaskCreateModule raises ValueError when name is missing."""
    module = TaskCreateModule(mock_creator, mock_repo)
    with pytest.raises(ValueError, match="non-empty"):
        await module.execute({})

async def test_task_execute_module_valid():
    """TaskExecuteModule executes a task by ID."""
    module = TaskExecuteModule(mock_manager)
    result = await module.execute({"task_id": "abc-123"})
    assert "task_id" in result

async def test_task_execute_module_empty_id_raises():
    """TaskExecuteModule raises ValueError for empty task_id."""
    module = TaskExecuteModule(mock_manager)
    with pytest.raises(ValueError):
        await module.execute({"task_id": ""})

async def test_task_list_module_defaults():
    """TaskListModule uses default limit=50, offset=0."""
    module = TaskListModule(mock_repo)
    result = await module.execute({})
    assert "tasks" in result
    assert "total" in result

async def test_task_list_module_clamps_limit():
    """TaskListModule clamps limit to [1, 1000]."""
    module = TaskListModule(mock_repo)
    # limit=0 should become 1
    await module.execute({"limit": 0})
    # limit=5000 should become 1000
    await module.execute({"limit": 5000})

async def test_task_get_module_not_found():
    """TaskGetModule raises KeyError for non-existent task."""
    module = TaskGetModule(mock_repo_returning_none)
    with pytest.raises(KeyError, match="not found"):
        await module.execute({"task_id": "nonexistent"})

async def test_task_delete_module_not_found():
    """TaskDeleteModule raises KeyError for non-existent task."""
    module = TaskDeleteModule(mock_repo_returning_none)
    with pytest.raises(KeyError, match="not found"):
        await module.execute({"task_id": "nonexistent"})

Integration Tests: tests/bridge/test_registry_setup.py

async def test_create_flowvane_registry():
    """create_flowvane_registry returns populated registry."""
    registry = create_flowvane_registry(mock_manager, mock_creator, mock_repo)
    # Verify executor modules registered
    # Verify task modules registered
    # Verify total module count > 5 (at least 5 task modules)

async def test_registry_custom_namespace():
    """Custom namespace prefix is applied."""
    registry = create_flowvane_registry(
        mock_manager, mock_creator, mock_repo, namespace="myapp"
    )
    # Verify modules named "myapp.rest_executor", "myapp.task.create", etc.

Acceptance Criteria

  1. FlowVaneMCP exposes registered modules using the MCP SDK; real MCP requests verify discovery, structured output, accounting counters, schema-sensitive fields, and credential redaction.
  2. A2A exposes the same registry through PublicExecutor; real JSON-RPC requests verify discovery, Run retries, durable submission, budget settlement, usage acknowledgement, and per-chunk redaction.
  3. The APCore Surfaces CLI generates commands for registered modules; real task create/get/list commands verify omitted defaults and JSON payload nulls.
  4. Each module has valid JSON Schema for inputs and outputs (input_schema and output_schema are non-empty dicts with "type" key).
  5. Module registration is automatic: adding a new @executor_register decorated class causes it to appear in the registry without any code changes to the bridge.
  6. All built-in executor modules are registered: rest, send_email, aggregate_results, apflow_api (4 executors).
  7. All 21 modules are registered (see registry_setup.py as source of truth): 16 task management modules (task.create, task.create_tree, task.execute, task.cancel, task.get, task.update, task.list, task.delete, task.tree, task.children, task.link, task.copy, task.archive, task.clone_mixed, task.running, task.scheduled) and 5 schedule modules (schedule.set, schedule.due, schedule.trigger, schedule.complete, schedule.export_ical).